umc-ld20.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * Copyright (C) 2016 Socionext Inc.
  3. *
  4. * based on commit 5e1cb0f1caeabc6c99469dd997cb6b4f46834443 of Diag
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <linux/bitops.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/sizes.h>
  13. #include <asm/processor.h>
  14. #include "../init.h"
  15. #include "ddruqphy-regs.h"
  16. #include "umc64-regs.h"
  17. #define DRAM_CH_NR 3
  18. enum dram_freq {
  19. DRAM_FREQ_1866M,
  20. DRAM_FREQ_NR,
  21. };
  22. enum dram_size {
  23. DRAM_SZ_256M,
  24. DRAM_SZ_512M,
  25. DRAM_SZ_NR,
  26. };
  27. enum dram_board { /* board type */
  28. DRAM_BOARD_LD20_REF, /* LD20 reference */
  29. DRAM_BOARD_LD20_GLOBAL, /* LD20 TV */
  30. DRAM_BOARD_LD20_C1, /* LD20 TV C1 */
  31. DRAM_BOARD_LD21_REF, /* LD21 reference */
  32. DRAM_BOARD_LD21_GLOBAL, /* LD21 TV */
  33. DRAM_BOARD_NR,
  34. };
  35. /* PHY */
  36. static const int ddrphy_adrctrl[DRAM_BOARD_NR][DRAM_CH_NR] = {
  37. {268 - 262, 268 - 263, 268 - 378}, /* LD20 reference */
  38. {268 - 262, 268 - 263, 268 - 378}, /* LD20 TV */
  39. {268 - 262, 268 - 263, 268 - 378}, /* LD20 TV C1 */
  40. {268 - 212, 268 - 268, /* No CH2 */}, /* LD21 reference */
  41. {268 - 212, 268 - 268, /* No CH2 */}, /* LD21 TV */
  42. };
  43. static const int ddrphy_dlltrimclk[DRAM_BOARD_NR][DRAM_CH_NR] = {
  44. {268, 268, 268}, /* LD20 reference */
  45. {268, 268, 268}, /* LD20 TV */
  46. {189, 189, 189}, /* LD20 TV C1 */
  47. {268, 268 + 252, /* No CH2 */}, /* LD21 reference */
  48. {268, 268 + 202, /* No CH2 */}, /* LD21 TV */
  49. };
  50. static const int ddrphy_dllrecalib[DRAM_BOARD_NR][DRAM_CH_NR] = {
  51. {268 - 378, 268 - 263, 268 - 378}, /* LD20 reference */
  52. {268 - 378, 268 - 263, 268 - 378}, /* LD20 TV */
  53. {268 - 378, 268 - 263, 268 - 378}, /* LD20 TV C1 */
  54. {268 - 212, 268 - 536, /* No CH2 */}, /* LD21 reference */
  55. {268 - 212, 268 - 536, /* No CH2 */}, /* LD21 TV */
  56. };
  57. static const u32 ddrphy_phy_pad_ctrl[DRAM_BOARD_NR][DRAM_CH_NR] = {
  58. {0x50B840B1, 0x50B840B1, 0x50B840B1}, /* LD20 reference */
  59. {0x50BB40B1, 0x50BB40B1, 0x50BB40B1}, /* LD20 TV */
  60. {0x50BB40B1, 0x50BB40B1, 0x50BB40B1}, /* LD20 TV C1 */
  61. {0x50BB40B4, 0x50B840B1, /* No CH2 */}, /* LD21 reference */
  62. {0x50BB40B4, 0x50B840B1, /* No CH2 */}, /* LD21 TV */
  63. };
  64. static const u32 ddrphy_scl_gate_timing[DRAM_CH_NR] = {
  65. 0x00000140, 0x00000180, 0x00000140
  66. };
  67. static const int ddrphy_op_dq_shift_val[DRAM_BOARD_NR][DRAM_CH_NR][32] = {
  68. { /* LD20 reference */
  69. {
  70. 2, 1, 0, 1, 2, 1, 1, 1,
  71. 2, 1, 1, 2, 1, 1, 1, 1,
  72. 1, 2, 1, 1, 1, 2, 1, 1,
  73. 2, 2, 0, 1, 1, 2, 2, 1,
  74. },
  75. {
  76. 1, 1, 0, 1, 2, 2, 1, 1,
  77. 1, 1, 1, 1, 1, 1, 1, 1,
  78. 1, 1, 0, 0, 1, 1, 0, 0,
  79. 0, 1, 1, 1, 2, 1, 2, 1,
  80. },
  81. {
  82. 2, 2, 0, 2, 1, 1, 2, 1,
  83. 1, 1, 0, 1, 1, -1, 1, 1,
  84. 2, 2, 2, 2, 1, 1, 1, 1,
  85. 1, 1, 1, 0, 2, 2, 1, 2,
  86. },
  87. },
  88. { /* LD20 TV */
  89. {
  90. 2, 1, 0, 1, 2, 1, 1, 1,
  91. 2, 1, 1, 2, 1, 1, 1, 1,
  92. 1, 2, 1, 1, 1, 2, 1, 1,
  93. 2, 2, 0, 1, 1, 2, 2, 1,
  94. },
  95. {
  96. 1, 1, 0, 1, 2, 2, 1, 1,
  97. 1, 1, 1, 1, 1, 1, 1, 1,
  98. 1, 1, 0, 0, 1, 1, 0, 0,
  99. 0, 1, 1, 1, 2, 1, 2, 1,
  100. },
  101. {
  102. 2, 2, 0, 2, 1, 1, 2, 1,
  103. 1, 1, 0, 1, 1, -1, 1, 1,
  104. 2, 2, 2, 2, 1, 1, 1, 1,
  105. 1, 1, 1, 0, 2, 2, 1, 2,
  106. },
  107. },
  108. { /* LD20 TV C1 */
  109. {
  110. 2, 1, 0, 1, 2, 1, 1, 1,
  111. 2, 1, 1, 2, 1, 1, 1, 1,
  112. 1, 2, 1, 1, 1, 2, 1, 1,
  113. 2, 2, 0, 1, 1, 2, 2, 1,
  114. },
  115. {
  116. 1, 1, 0, 1, 2, 2, 1, 1,
  117. 1, 1, 1, 1, 1, 1, 1, 1,
  118. 1, 1, 0, 0, 1, 1, 0, 0,
  119. 0, 1, 1, 1, 2, 1, 2, 1,
  120. },
  121. {
  122. 2, 2, 0, 2, 1, 1, 2, 1,
  123. 1, 1, 0, 1, 1, -1, 1, 1,
  124. 2, 2, 2, 2, 1, 1, 1, 1,
  125. 1, 1, 1, 0, 2, 2, 1, 2,
  126. },
  127. },
  128. { /* LD21 reference */
  129. {
  130. 1, 1, 0, 1, 1, 1, 1, 1,
  131. 1, 0, 0, 0, 1, 1, 0, 2,
  132. 1, 1, 0, 0, 1, 1, 1, 1,
  133. 1, 0, 0, 0, 1, 0, 0, 1,
  134. },
  135. { 1, 0, 2, 1, 1, 1, 1, 0,
  136. 1, 0, 0, 1, 0, 1, 0, 0,
  137. 1, 0, 1, 0, 1, 1, 1, 0,
  138. 1, 1, 1, 1, 0, 1, 0, 0,
  139. },
  140. /* No CH2 */
  141. },
  142. { /* LD21 TV */
  143. {
  144. 1, 1, 0, 1, 1, 1, 1, 1,
  145. 1, 0, 0, 0, 1, 1, 0, 2,
  146. 1, 1, 0, 0, 1, 1, 1, 1,
  147. 1, 0, 0, 0, 1, 0, 0, 1,
  148. },
  149. { 1, 0, 2, 1, 1, 1, 1, 0,
  150. 1, 0, 0, 1, 0, 1, 0, 0,
  151. 1, 0, 1, 0, 1, 1, 1, 0,
  152. 1, 1, 1, 1, 0, 1, 0, 0,
  153. },
  154. /* No CH2 */
  155. },
  156. };
  157. static int ddrphy_ip_dq_shift_val[DRAM_BOARD_NR][DRAM_CH_NR][32] = {
  158. { /* LD20 reference */
  159. {
  160. 3, 3, 3, 2, 3, 2, 0, 2,
  161. 2, 3, 3, 1, 2, 2, 2, 2,
  162. 2, 2, 2, 2, 0, 1, 1, 1,
  163. 2, 2, 2, 2, 3, 0, 2, 2,
  164. },
  165. {
  166. 2, 2, 1, 1, -1, 1, 1, 1,
  167. 2, 0, 2, 2, 2, 1, 0, 2,
  168. 2, 1, 2, 1, 0, 1, 1, 1,
  169. 2, 2, 2, 2, 2, 2, 2, 2,
  170. },
  171. {
  172. 2, 2, 3, 2, 1, 2, 2, 2,
  173. 2, 3, 4, 2, 3, 4, 3, 3,
  174. 2, 2, 1, 2, 1, 1, 1, 1,
  175. 2, 2, 2, 2, 1, 2, 2, 1,
  176. },
  177. },
  178. { /* LD20 TV */
  179. {
  180. 3, 3, 3, 2, 3, 2, 0, 2,
  181. 2, 3, 3, 1, 2, 2, 2, 2,
  182. 2, 2, 2, 2, 0, 1, 1, 1,
  183. 2, 2, 2, 2, 3, 0, 2, 2,
  184. },
  185. {
  186. 2, 2, 1, 1, -1, 1, 1, 1,
  187. 2, 0, 2, 2, 2, 1, 0, 2,
  188. 2, 1, 2, 1, 0, 1, 1, 1,
  189. 2, 2, 2, 2, 2, 2, 2, 2,
  190. },
  191. {
  192. 2, 2, 3, 2, 1, 2, 2, 2,
  193. 2, 3, 4, 2, 3, 4, 3, 3,
  194. 2, 2, 1, 2, 1, 1, 1, 1,
  195. 2, 2, 2, 2, 1, 2, 2, 1,
  196. },
  197. },
  198. { /* LD20 TV C1 */
  199. {
  200. 3, 3, 3, 2, 3, 2, 0, 2,
  201. 2, 3, 3, 1, 2, 2, 2, 2,
  202. 2, 2, 2, 2, 0, 1, 1, 1,
  203. 2, 2, 2, 2, 3, 0, 2, 2,
  204. },
  205. {
  206. 2, 2, 1, 1, -1, 1, 1, 1,
  207. 2, 0, 2, 2, 2, 1, 0, 2,
  208. 2, 1, 2, 1, 0, 1, 1, 1,
  209. 2, 2, 2, 2, 2, 2, 2, 2,
  210. },
  211. {
  212. 2, 2, 3, 2, 1, 2, 2, 2,
  213. 2, 3, 4, 2, 3, 4, 3, 3,
  214. 2, 2, 1, 2, 1, 1, 1, 1,
  215. 2, 2, 2, 2, 1, 2, 2, 1,
  216. },
  217. },
  218. { /* LD21 reference */
  219. {
  220. 2, 2, 2, 2, 1, 2, 2, 2,
  221. 2, 3, 3, 2, 2, 2, 2, 2,
  222. 2, 1, 2, 2, 1, 1, 1, 1,
  223. 2, 2, 2, 3, 1, 2, 2, 2,
  224. },
  225. {
  226. 3, 4, 4, 1, 0, 1, 1, 1,
  227. 1, 2, 1, 2, 2, 3, 3, 2,
  228. 1, 0, 2, 1, 1, 0, 1, 0,
  229. 0, 1, 0, 0, 1, 1, 0, 1,
  230. },
  231. /* No CH2 */
  232. },
  233. { /* LD21 TV */
  234. {
  235. 2, 2, 2, 2, 1, 2, 2, 2,
  236. 2, 3, 3, 2, 2, 2, 2, 2,
  237. 2, 1, 2, 2, 1, 1, 1, 1,
  238. 2, 2, 2, 3, 1, 2, 2, 2,
  239. },
  240. {
  241. 3, 4, 4, 1, 0, 1, 1, 1,
  242. 1, 2, 1, 2, 2, 3, 3, 2,
  243. 1, 0, 2, 1, 1, 0, 1, 0,
  244. 0, 1, 0, 0, 1, 1, 0, 1,
  245. },
  246. /* No CH2 */
  247. },
  248. };
  249. /* DDR PHY */
  250. static void ddrphy_select_lane(void __iomem *phy_base, unsigned int lane,
  251. unsigned int bit)
  252. {
  253. WARN_ON(lane >= 1 << PHY_LANE_SEL_LANE_WIDTH);
  254. WARN_ON(bit >= 1 << PHY_LANE_SEL_BIT_WIDTH);
  255. writel((bit << PHY_LANE_SEL_BIT_SHIFT) |
  256. (lane << PHY_LANE_SEL_LANE_SHIFT),
  257. phy_base + PHY_LANE_SEL);
  258. }
  259. static void ddrphy_init(void __iomem *phy_base, enum dram_board board, int ch)
  260. {
  261. writel(0x0C001001, phy_base + PHY_UNIQUIFY_TSMC_IO_1);
  262. while (!(readl(phy_base + PHY_UNIQUIFY_TSMC_IO_1) & BIT(1)))
  263. cpu_relax();
  264. writel(0x0C001000, phy_base + PHY_UNIQUIFY_TSMC_IO_1);
  265. writel(0x00000000, phy_base + PHY_DLL_INCR_TRIM_3);
  266. writel(0x00000000, phy_base + PHY_DLL_INCR_TRIM_1);
  267. ddrphy_select_lane(phy_base, 0, 0);
  268. writel(0x00000005, phy_base + PHY_DLL_TRIM_1);
  269. writel(0x0000000a, phy_base + PHY_DLL_TRIM_3);
  270. ddrphy_select_lane(phy_base, 6, 0);
  271. writel(0x00000005, phy_base + PHY_DLL_TRIM_1);
  272. writel(0x0000000a, phy_base + PHY_DLL_TRIM_3);
  273. ddrphy_select_lane(phy_base, 12, 0);
  274. writel(0x00000005, phy_base + PHY_DLL_TRIM_1);
  275. writel(0x0000000a, phy_base + PHY_DLL_TRIM_3);
  276. ddrphy_select_lane(phy_base, 18, 0);
  277. writel(0x00000005, phy_base + PHY_DLL_TRIM_1);
  278. writel(0x0000000a, phy_base + PHY_DLL_TRIM_3);
  279. writel(0x00000001, phy_base + PHY_SCL_WINDOW_TRIM);
  280. writel(0x00000000, phy_base + PHY_UNQ_ANALOG_DLL_1);
  281. writel(ddrphy_phy_pad_ctrl[board][ch], phy_base + PHY_PAD_CTRL);
  282. writel(0x00000070, phy_base + PHY_VREF_TRAINING);
  283. writel(0x01000075, phy_base + PHY_SCL_CONFIG_1);
  284. writel(0x00000501, phy_base + PHY_SCL_CONFIG_2);
  285. writel(0x00000000, phy_base + PHY_SCL_CONFIG_3);
  286. writel(0x000261c0, phy_base + PHY_DYNAMIC_WRITE_BIT_LVL);
  287. writel(0x00000000, phy_base + PHY_SCL_CONFIG_4);
  288. writel(ddrphy_scl_gate_timing[ch], phy_base + PHY_SCL_GATE_TIMING);
  289. writel(0x02a000a0, phy_base + PHY_WRLVL_DYN_ODT);
  290. writel(0x00840004, phy_base + PHY_WRLVL_ON_OFF);
  291. writel(0x0000020d, phy_base + PHY_DLL_ADRCTRL);
  292. ddrphy_select_lane(phy_base, 0, 0);
  293. writel(0x0000008d, phy_base + PHY_DLL_TRIM_CLK);
  294. writel(0xa800100d, phy_base + PHY_DLL_RECALIB);
  295. writel(0x00005076, phy_base + PHY_SCL_LATENCY);
  296. }
  297. static int ddrphy_to_dly_step(void __iomem *phy_base, unsigned int freq,
  298. int delay)
  299. {
  300. int mdl;
  301. mdl = (readl(phy_base + PHY_DLL_ADRCTRL) & PHY_DLL_ADRCTRL_MDL_MASK) >>
  302. PHY_DLL_ADRCTRL_MDL_SHIFT;
  303. return DIV_ROUND_CLOSEST((long)freq * delay * mdl, 2 * 1000000L);
  304. }
  305. static void ddrphy_set_delay(void __iomem *phy_base, unsigned int reg,
  306. u32 mask, u32 incr, int dly_step)
  307. {
  308. u32 tmp;
  309. tmp = readl(phy_base + reg);
  310. tmp &= ~mask;
  311. tmp |= min_t(u32, abs(dly_step), mask);
  312. if (dly_step >= 0)
  313. tmp |= incr;
  314. else
  315. tmp &= ~incr;
  316. writel(tmp, phy_base + reg);
  317. }
  318. static void ddrphy_set_dll_recalib(void __iomem *phy_base, int dly_step)
  319. {
  320. ddrphy_set_delay(phy_base, PHY_DLL_RECALIB,
  321. PHY_DLL_RECALIB_TRIM_MASK, PHY_DLL_RECALIB_INCR,
  322. dly_step);
  323. }
  324. static void ddrphy_set_dll_adrctrl(void __iomem *phy_base, int dly_step)
  325. {
  326. ddrphy_set_delay(phy_base, PHY_DLL_ADRCTRL,
  327. PHY_DLL_ADRCTRL_TRIM_MASK, PHY_DLL_ADRCTRL_INCR,
  328. dly_step);
  329. }
  330. static void ddrphy_set_dll_trim_clk(void __iomem *phy_base, int dly_step)
  331. {
  332. ddrphy_select_lane(phy_base, 0, 0);
  333. ddrphy_set_delay(phy_base, PHY_DLL_TRIM_CLK,
  334. PHY_DLL_TRIM_CLK_MASK, PHY_DLL_TRIM_CLK_INCR,
  335. dly_step);
  336. }
  337. static void ddrphy_init_tail(void __iomem *phy_base, enum dram_board board,
  338. unsigned int freq, int ch)
  339. {
  340. int step;
  341. step = ddrphy_to_dly_step(phy_base, freq, ddrphy_adrctrl[board][ch]);
  342. ddrphy_set_dll_adrctrl(phy_base, step);
  343. step = ddrphy_to_dly_step(phy_base, freq, ddrphy_dlltrimclk[board][ch]);
  344. ddrphy_set_dll_trim_clk(phy_base, step);
  345. step = ddrphy_to_dly_step(phy_base, freq, ddrphy_dllrecalib[board][ch]);
  346. ddrphy_set_dll_recalib(phy_base, step);
  347. }
  348. static void ddrphy_shift_one_dq(void __iomem *phy_base, unsigned int reg,
  349. u32 mask, u32 incr, int shift_val)
  350. {
  351. u32 tmp;
  352. int val;
  353. tmp = readl(phy_base + reg);
  354. val = tmp & mask;
  355. if (!(tmp & incr))
  356. val = -val;
  357. val += shift_val;
  358. tmp &= ~(incr | mask);
  359. tmp |= min_t(u32, abs(val), mask);
  360. if (val >= 0)
  361. tmp |= incr;
  362. writel(tmp, phy_base + reg);
  363. }
  364. static void ddrphy_shift_dq(void __iomem *phy_base, unsigned int reg,
  365. u32 mask, u32 incr, u32 override,
  366. const int *shift_val_array)
  367. {
  368. u32 tmp;
  369. int dx, bit;
  370. tmp = readl(phy_base + reg);
  371. tmp |= override;
  372. writel(tmp, phy_base + reg);
  373. for (dx = 0; dx < 4; dx++) {
  374. for (bit = 0; bit < 8; bit++) {
  375. ddrphy_select_lane(phy_base,
  376. (PHY_BITLVL_DLY_WIDTH + 1) * dx,
  377. bit);
  378. ddrphy_shift_one_dq(phy_base, reg, mask, incr,
  379. shift_val_array[dx * 8 + bit]);
  380. }
  381. }
  382. ddrphy_select_lane(phy_base, 0, 0);
  383. }
  384. static int ddrphy_training(void __iomem *phy_base, enum dram_board board,
  385. int ch)
  386. {
  387. writel(0x0000000f, phy_base + PHY_WRLVL_AUTOINC_TRIM);
  388. writel(0x00010000, phy_base + PHY_DLL_TRIM_2);
  389. writel(0x50000000, phy_base + PHY_SCL_START);
  390. while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE)
  391. cpu_relax();
  392. writel(0x00000000, phy_base + PHY_DISABLE_GATING_FOR_SCL);
  393. writel(0xff00ff00, phy_base + PHY_SCL_DATA_0);
  394. writel(0xff00ff00, phy_base + PHY_SCL_DATA_1);
  395. writel(0xFBF8FFFF, phy_base + PHY_SCL_START_ADDR);
  396. writel(0x11000000, phy_base + PHY_SCL_START);
  397. while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE)
  398. cpu_relax();
  399. writel(0xFBF0FFFF, phy_base + PHY_SCL_START_ADDR);
  400. writel(0x30500000, phy_base + PHY_SCL_START);
  401. while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE)
  402. cpu_relax();
  403. writel(0x00000001, phy_base + PHY_DISABLE_GATING_FOR_SCL);
  404. writel(0x00000010, phy_base + PHY_SCL_MAIN_CLK_DELTA);
  405. writel(0x789b3de0, phy_base + PHY_SCL_DATA_0);
  406. writel(0xf10e4a56, phy_base + PHY_SCL_DATA_1);
  407. writel(0x11000000, phy_base + PHY_SCL_START);
  408. while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE)
  409. cpu_relax();
  410. writel(0x34000000, phy_base + PHY_SCL_START);
  411. while (readl(phy_base + PHY_SCL_START) & PHY_SCL_START_GO_DONE)
  412. cpu_relax();
  413. writel(0x00000003, phy_base + PHY_DISABLE_GATING_FOR_SCL);
  414. writel(0x000261c0, phy_base + PHY_DYNAMIC_WRITE_BIT_LVL);
  415. writel(0x00003270, phy_base + PHY_DYNAMIC_BIT_LVL);
  416. writel(0x011BD0C4, phy_base + PHY_DSCL_CNT);
  417. /* shift ip_dq trim */
  418. ddrphy_shift_dq(phy_base,
  419. PHY_IP_DQ_DQS_BITWISE_TRIM,
  420. PHY_IP_DQ_DQS_BITWISE_TRIM_MASK,
  421. PHY_IP_DQ_DQS_BITWISE_TRIM_INC,
  422. PHY_IP_DQ_DQS_BITWISE_TRIM_OVERRIDE,
  423. ddrphy_ip_dq_shift_val[board][ch]);
  424. /* shift op_dq trim */
  425. ddrphy_shift_dq(phy_base,
  426. PHY_OP_DQ_DM_DQS_BITWISE_TRIM,
  427. PHY_OP_DQ_DM_DQS_BITWISE_TRIM_MASK,
  428. PHY_OP_DQ_DM_DQS_BITWISE_TRIM_INC,
  429. PHY_OP_DQ_DM_DQS_BITWISE_TRIM_OVERRIDE,
  430. ddrphy_op_dq_shift_val[board][ch]);
  431. return 0;
  432. }
  433. /* UMC */
  434. static const u32 umc_initctla[DRAM_FREQ_NR] = {0x71016D11};
  435. static const u32 umc_initctlb[DRAM_FREQ_NR] = {0x07E390AC};
  436. static const u32 umc_initctlc[DRAM_FREQ_NR] = {0x00FF00FF};
  437. static const u32 umc_drmmr0[DRAM_FREQ_NR] = {0x00000114};
  438. static const u32 umc_drmmr2[DRAM_FREQ_NR] = {0x000002a0};
  439. static const u32 umc_memconf0a[DRAM_FREQ_NR][DRAM_SZ_NR] = {
  440. /* 256MB 512MB */
  441. {0x00000601, 0x00000801}, /* 1866 MHz */
  442. };
  443. static const u32 umc_memconf0b[DRAM_FREQ_NR][DRAM_SZ_NR] = {
  444. /* 256MB 512MB */
  445. {0x00000120, 0x00000130}, /* 1866 MHz */
  446. };
  447. static const u32 umc_memconfch[DRAM_FREQ_NR][DRAM_SZ_NR] = {
  448. /* 256MB 512MB */
  449. {0x00033603, 0x00033803}, /* 1866 MHz */
  450. };
  451. static const u32 umc_cmdctla[DRAM_FREQ_NR] = {0x060D0D20};
  452. static const u32 umc_cmdctlb[DRAM_FREQ_NR] = {0x2D211C08};
  453. static const u32 umc_cmdctlc[DRAM_FREQ_NR] = {0x00150C04};
  454. static const u32 umc_cmdctle[DRAM_FREQ_NR][DRAM_SZ_NR] = {
  455. /* 256MB 512MB */
  456. {0x0049071D, 0x0078071D}, /* 1866 MHz */
  457. };
  458. static const u32 umc_rdatactl[DRAM_FREQ_NR] = {0x00000610};
  459. static const u32 umc_wdatactl[DRAM_FREQ_NR] = {0x00000204};
  460. static const u32 umc_odtctl[DRAM_FREQ_NR] = {0x02000002};
  461. static const u32 umc_dataset[DRAM_FREQ_NR] = {0x04000000};
  462. static const u32 umc_flowctla[DRAM_FREQ_NR] = {0x0081E01E};
  463. static const u32 umc_directbusctrla[DRAM_CH_NR] = {
  464. 0x00000000, 0x00000001, 0x00000001
  465. };
  466. static void umc_poll_phy_init_complete(void __iomem *dc_base)
  467. {
  468. /* Wait for PHY Init Complete */
  469. while (!(readl(dc_base + UMC_DFISTCTLC) & BIT(0)))
  470. cpu_relax();
  471. }
  472. static int umc_dc_init(void __iomem *dc_base, unsigned int freq,
  473. unsigned long size, int ch)
  474. {
  475. enum dram_freq freq_e;
  476. enum dram_size size_e;
  477. switch (freq) {
  478. case 1866:
  479. freq_e = DRAM_FREQ_1866M;
  480. break;
  481. default:
  482. pr_err("unsupported DRAM frequency %ud MHz\n", freq);
  483. return -EINVAL;
  484. }
  485. switch (size) {
  486. case 0:
  487. return 0;
  488. case SZ_256M:
  489. size_e = DRAM_SZ_256M;
  490. break;
  491. case SZ_512M:
  492. size_e = DRAM_SZ_512M;
  493. break;
  494. default:
  495. pr_err("unsupported DRAM size 0x%08lx (per 16bit) for ch%d\n",
  496. size, ch);
  497. return -EINVAL;
  498. }
  499. writel(0x00000001, dc_base + UMC_DFICSOVRRD);
  500. writel(0x00000000, dc_base + UMC_DFITURNOFF);
  501. writel(umc_initctla[freq_e], dc_base + UMC_INITCTLA);
  502. writel(umc_initctlb[freq_e], dc_base + UMC_INITCTLB);
  503. writel(umc_initctlc[freq_e], dc_base + UMC_INITCTLC);
  504. writel(umc_drmmr0[freq_e], dc_base + UMC_DRMMR0);
  505. writel(0x00000004, dc_base + UMC_DRMMR1);
  506. writel(umc_drmmr2[freq_e], dc_base + UMC_DRMMR2);
  507. writel(0x00000000, dc_base + UMC_DRMMR3);
  508. writel(umc_memconf0a[freq_e][size_e], dc_base + UMC_MEMCONF0A);
  509. writel(umc_memconf0b[freq_e][size_e], dc_base + UMC_MEMCONF0B);
  510. writel(umc_memconfch[freq_e][size_e], dc_base + UMC_MEMCONFCH);
  511. writel(0x00000008, dc_base + UMC_MEMMAPSET);
  512. writel(umc_cmdctla[freq_e], dc_base + UMC_CMDCTLA);
  513. writel(umc_cmdctlb[freq_e], dc_base + UMC_CMDCTLB);
  514. writel(umc_cmdctlc[freq_e], dc_base + UMC_CMDCTLC);
  515. writel(umc_cmdctle[freq_e][size_e], dc_base + UMC_CMDCTLE);
  516. writel(umc_rdatactl[freq_e], dc_base + UMC_RDATACTL_D0);
  517. writel(umc_rdatactl[freq_e], dc_base + UMC_RDATACTL_D1);
  518. writel(umc_wdatactl[freq_e], dc_base + UMC_WDATACTL_D0);
  519. writel(umc_wdatactl[freq_e], dc_base + UMC_WDATACTL_D1);
  520. writel(umc_odtctl[freq_e], dc_base + UMC_ODTCTL_D0);
  521. writel(umc_odtctl[freq_e], dc_base + UMC_ODTCTL_D1);
  522. writel(umc_dataset[freq_e], dc_base + UMC_DATASET);
  523. writel(0x00400020, dc_base + UMC_DCCGCTL);
  524. writel(0x00000003, dc_base + UMC_ACSSETA);
  525. writel(0x00000103, dc_base + UMC_FLOWCTLG);
  526. writel(0x00010200, dc_base + UMC_ACSSETB);
  527. writel(umc_flowctla[freq_e], dc_base + UMC_FLOWCTLA);
  528. writel(0x00004444, dc_base + UMC_FLOWCTLC);
  529. writel(0x00000000, dc_base + UMC_DFICUPDCTLA);
  530. writel(0x00202000, dc_base + UMC_FLOWCTLB);
  531. writel(0x00000000, dc_base + UMC_BSICMAPSET);
  532. writel(0x00000000, dc_base + UMC_ERRMASKA);
  533. writel(0x00000000, dc_base + UMC_ERRMASKB);
  534. writel(umc_directbusctrla[ch], dc_base + UMC_DIRECTBUSCTRLA);
  535. writel(0x00000001, dc_base + UMC_INITSET);
  536. /* Wait for PHY Init Complete */
  537. while (readl(dc_base + UMC_INITSTAT) & BIT(0))
  538. cpu_relax();
  539. writel(0x2A0A0A00, dc_base + UMC_SPCSETB);
  540. writel(0x00000000, dc_base + UMC_DFICSOVRRD);
  541. return 0;
  542. }
  543. static int umc_ch_init(void __iomem *umc_ch_base, void __iomem *phy_ch_base,
  544. enum dram_board board, unsigned int freq,
  545. unsigned long size, int ch)
  546. {
  547. void __iomem *dc_base = umc_ch_base + 0x00011000;
  548. void __iomem *phy_base = phy_ch_base;
  549. int ret;
  550. /* PHY Update Mode (ON) */
  551. writel(0x8000003f, dc_base + UMC_DFIPUPDCTLA);
  552. /* deassert PHY reset signals */
  553. writel(UMC_DIOCTLA_CTL_NRST | UMC_DIOCTLA_CFG_NRST,
  554. dc_base + UMC_DIOCTLA);
  555. ddrphy_init(phy_base, board, ch);
  556. umc_poll_phy_init_complete(dc_base);
  557. ddrphy_init_tail(phy_base, board, freq, ch);
  558. ret = umc_dc_init(dc_base, freq, size, ch);
  559. if (ret)
  560. return ret;
  561. ret = ddrphy_training(phy_base, board, ch);
  562. if (ret)
  563. return ret;
  564. return 0;
  565. }
  566. static void um_init(void __iomem *um_base)
  567. {
  568. writel(0x000000ff, um_base + UMC_MBUS0);
  569. writel(0x000000ff, um_base + UMC_MBUS1);
  570. writel(0x000000ff, um_base + UMC_MBUS2);
  571. writel(0x00000001, um_base + UMC_MBUS3);
  572. writel(0x00000001, um_base + UMC_MBUS4);
  573. writel(0x00000001, um_base + UMC_MBUS5);
  574. writel(0x00000001, um_base + UMC_MBUS6);
  575. writel(0x00000001, um_base + UMC_MBUS7);
  576. writel(0x00000001, um_base + UMC_MBUS8);
  577. writel(0x00000001, um_base + UMC_MBUS9);
  578. writel(0x00000001, um_base + UMC_MBUS10);
  579. }
  580. int uniphier_ld20_umc_init(const struct uniphier_board_data *bd)
  581. {
  582. void __iomem *um_base = (void __iomem *)0x5b600000;
  583. void __iomem *umc_ch_base = (void __iomem *)0x5b800000;
  584. void __iomem *phy_ch_base = (void __iomem *)0x6e200000;
  585. enum dram_board board;
  586. int ch, ret;
  587. switch (UNIPHIER_BD_BOARD_GET_TYPE(bd->flags)) {
  588. case UNIPHIER_BD_BOARD_LD20_REF:
  589. board = DRAM_BOARD_LD20_REF;
  590. break;
  591. case UNIPHIER_BD_BOARD_LD20_GLOBAL:
  592. board = DRAM_BOARD_LD20_GLOBAL;
  593. break;
  594. case UNIPHIER_BD_BOARD_LD20_C1:
  595. board = DRAM_BOARD_LD20_C1;
  596. break;
  597. case UNIPHIER_BD_BOARD_LD21_REF:
  598. board = DRAM_BOARD_LD21_REF;
  599. break;
  600. case UNIPHIER_BD_BOARD_LD21_GLOBAL:
  601. board = DRAM_BOARD_LD21_GLOBAL;
  602. break;
  603. default:
  604. pr_err("unsupported board type %d\n",
  605. UNIPHIER_BD_BOARD_GET_TYPE(bd->flags));
  606. return -EINVAL;
  607. }
  608. for (ch = 0; ch < bd->dram_nr_ch; ch++) {
  609. unsigned long size = bd->dram_ch[ch].size;
  610. unsigned int width = bd->dram_ch[ch].width;
  611. ret = umc_ch_init(umc_ch_base, phy_ch_base, board,
  612. bd->dram_freq, size / (width / 16), ch);
  613. if (ret) {
  614. pr_err("failed to initialize UMC ch%d\n", ch);
  615. return ret;
  616. }
  617. umc_ch_base += 0x00200000;
  618. phy_ch_base += 0x00004000;
  619. }
  620. um_init(um_base);
  621. return 0;
  622. }