umc-ld20.c 19 KB

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