ddr_ctrl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. /*
  3. * Cadence DDR Controller
  4. *
  5. * Copyright (C) 2015 Renesas Electronics Europe Ltd
  6. */
  7. /*
  8. * The Cadence DDR Controller has a huge number of registers that principally
  9. * cover two aspects, DDR specific timing information and AXI bus interfacing.
  10. * Cadence's TCL script generates all of the register values for specific
  11. * DDR devices operating at a specific frequency. The TCL script uses Denali
  12. * SOMA files as inputs. The tool also generates the AXI bus register values as
  13. * well, however this driver assumes that users will want to modifiy these to
  14. * meet a specific application's needs.
  15. * Therefore, this driver is passed two arrays containing register values for
  16. * the DDR device specific information, and explicity sets the AXI registers.
  17. *
  18. * AXI bus interfacing:
  19. * The controller has four AXI slaves connections, and each of these can be
  20. * programmed to accept requests from specific AXI masters (using their IDs).
  21. * The regions of DDR that can be accessed by each AXI slave can be set such
  22. * as to isolate DDR used by one AXI master from another. Further, the maximum
  23. * bandwidth allocated to each AXI slave can be set.
  24. */
  25. #include <common.h>
  26. #include <linux/delay.h>
  27. #include <linux/sizes.h>
  28. #include <asm/io.h>
  29. #include <wait_bit.h>
  30. #include <renesas/ddr_ctrl.h>
  31. /* avoid warning for real pr_debug in <linux/printk.h> */
  32. #ifdef pr_debug
  33. #undef pr_debug
  34. #endif
  35. #ifdef DEBUG
  36. #define pr_debug(fmt, args...) printf(fmt, ##args)
  37. #define pr_debug2(fmt, args...) printf(fmt, ##args)
  38. #else
  39. #define pr_debug(fmt, args...)
  40. #define pr_debug2(fmt, args...)
  41. #endif
  42. #define DDR_NR_AXI_PORTS 4
  43. #define DDR_NR_ENTRIES 16
  44. #define DDR_START_REG (0) /* DENALI_CTL_00 */
  45. #define DDR_CS0_MR1_REG (32 * 4) /* DENALI_CTL_32 */
  46. #define DDR_CS0_MR2_REG (32 * 4 + 2) /* DENALI_CTL_32 */
  47. #define DDR_CS1_MR1_REG (34 * 4 + 2) /* DENALI_CTL_34 */
  48. #define DDR_CS1_MR2_REG (35 * 4) /* DENALI_CTL_35 */
  49. #define DDR_ECC_ENABLE_REG (36 * 4 + 2) /* DENALI_CTL_36 */
  50. #define DDR_ECC_DISABLE_W_UC_ERR_REG (37 * 4 + 2) /* DENALI_CTL_37 */
  51. #define DDR_HALF_DATAPATH_REG (54 * 4) /* DENALI_CTL_54 */
  52. #define DDR_INTERRUPT_STATUS (56 * 4) /* DENALI_CTL_56 */
  53. #define DDR_INTERRUPT_ACK (57 * 4) /* DENALI_CTL_57 */
  54. #define DDR_INTERRUPT_MASK (58 * 4) /* DENALI_CTL_58 */
  55. #define DDR_CS0_ODT_MAP_REG (62 * 4 + 2) /* DENALI_CTL_62 */
  56. #define DDR_CS1_ODT_MAP_REG (63 * 4) /* DENALI_CTL_63 */
  57. #define DDR_ODT_TODTL_2CMD (63 * 4 + 2) /* DENALI_CTL_63 */
  58. #define DDR_ODT_TODTH_WR (63 * 4 + 3) /* DENALI_CTL_63 */
  59. #define DDR_ODT_TODTH_RD (64 * 4 + 0) /* DENALI_CTL_64 */
  60. #define DDR_ODT_EN (64 * 4 + 1) /* DENALI_CTL_64 */
  61. #define DDR_ODT_WR_TO_ODTH (64 * 4 + 2) /* DENALI_CTL_64 */
  62. #define DDR_ODT_RD_TO_ODTH (64 * 4 + 3) /* DENALI_CTL_64 */
  63. #define DDR_DIFF_CS_DELAY_REG (66 * 4) /* DENALI_CTL_66 */
  64. #define DDR_SAME_CS_DELAY_REG (67 * 4) /* DENALI_CTL_67 */
  65. #define DDR_RW_PRIORITY_REGS (87 * 4 + 2) /* DENALI_CTL_87 */
  66. #define DDR_RW_FIFO_TYPE_REGS (88 * 4) /* DENALI_CTL_88 */
  67. #define DDR_AXI_PORT_PROT_ENABLE_REG (90 * 4 + 3) /* DENALI_CTL_90 */
  68. #define DDR_ADDR_RANGE_REGS (91 * 4) /* DENALI_CTL_91 */
  69. #define DDR_RANGE_PROT_REGS (218 * 4 + 2) /* DENALI_CTL_218 */
  70. #define DDR_ARB_CMD_Q_THRESHOLD_REG (346 * 4 + 2) /* DENALI_CTL_346 */
  71. #define DDR_AXI_PORT_BANDWIDTH_REG (346 * 4 + 3) /* DENALI_CTL_346 */
  72. #define DDR_OPT_RMODW_REG (372 * 4 + 3) /* DENALI_CTL_372 */
  73. static void ddrc_writeb(u8 val, void *p)
  74. {
  75. pr_debug2("DDR: %p = 0x%02x\n", p, val);
  76. writeb(val, p);
  77. }
  78. static void ddrc_writew(u16 val, void *p)
  79. {
  80. pr_debug2("DDR: %p = 0x%04x\n", p, val);
  81. writew(val, p);
  82. }
  83. static void ddrc_writel(u32 val, void *p)
  84. {
  85. pr_debug2("DDR: %p = 0x%08x\n", p, val);
  86. writel(val, p);
  87. }
  88. void cdns_ddr_set_mr1(void *base, int cs, u16 odt_impedance, u16 drive_strength)
  89. {
  90. void *reg;
  91. u16 tmp;
  92. if (cs == 0)
  93. reg = (u8 *)base + DDR_CS0_MR1_REG;
  94. else
  95. reg = (u8 *)base + DDR_CS1_MR1_REG;
  96. tmp = readw(reg);
  97. tmp &= ~MODE_REGISTER_MASK;
  98. tmp |= MODE_REGISTER_MR1;
  99. tmp &= ~MR1_ODT_IMPEDANCE_MASK;
  100. tmp |= odt_impedance;
  101. tmp &= ~MR1_DRIVE_STRENGTH_MASK;
  102. tmp |= drive_strength;
  103. writew(tmp, reg);
  104. }
  105. void cdns_ddr_set_mr2(void *base, int cs, u16 dynamic_odt, u16 self_refresh_temp)
  106. {
  107. void *reg;
  108. u16 tmp;
  109. if (cs == 0)
  110. reg = (u8 *)base + DDR_CS0_MR2_REG;
  111. else
  112. reg = (u8 *)base + DDR_CS1_MR2_REG;
  113. tmp = readw(reg);
  114. tmp &= ~MODE_REGISTER_MASK;
  115. tmp |= MODE_REGISTER_MR2;
  116. tmp &= ~MR2_DYNAMIC_ODT_MASK;
  117. tmp |= dynamic_odt;
  118. tmp &= ~MR2_SELF_REFRESH_TEMP_MASK;
  119. tmp |= self_refresh_temp;
  120. writew(tmp, reg);
  121. }
  122. void cdns_ddr_set_odt_map(void *base, int cs, u16 odt_map)
  123. {
  124. void *reg;
  125. if (cs == 0)
  126. reg = (u8 *)base + DDR_CS0_ODT_MAP_REG;
  127. else
  128. reg = (u8 *)base + DDR_CS1_ODT_MAP_REG;
  129. writew(odt_map, reg);
  130. }
  131. void cdns_ddr_set_odt_times(void *base, u8 TODTL_2CMD, u8 TODTH_WR, u8 TODTH_RD,
  132. u8 WR_TO_ODTH, u8 RD_TO_ODTH)
  133. {
  134. writeb(TODTL_2CMD, (u8 *)base + DDR_ODT_TODTL_2CMD);
  135. writeb(TODTH_WR, (u8 *)base + DDR_ODT_TODTH_WR);
  136. writeb(TODTH_RD, (u8 *)base + DDR_ODT_TODTH_RD);
  137. writeb(1, (u8 *)base + DDR_ODT_EN);
  138. writeb(WR_TO_ODTH, (u8 *)base + DDR_ODT_WR_TO_ODTH);
  139. writeb(RD_TO_ODTH, (u8 *)base + DDR_ODT_RD_TO_ODTH);
  140. }
  141. void cdns_ddr_set_same_cs_delays(void *base, u8 r2r, u8 r2w, u8 w2r, u8 w2w)
  142. {
  143. u32 val = (w2w << 24) | (w2r << 16) | (r2w << 8) | r2r;
  144. writel(val, (u8 *)base + DDR_SAME_CS_DELAY_REG);
  145. }
  146. void cdns_ddr_set_diff_cs_delays(void *base, u8 r2r, u8 r2w, u8 w2r, u8 w2w)
  147. {
  148. u32 val = (w2w << 24) | (w2r << 16) | (r2w << 8) | r2r;
  149. writel(val, (u8 *)base + DDR_DIFF_CS_DELAY_REG);
  150. }
  151. void cdns_ddr_set_port_rw_priority(void *base, int port,
  152. u8 read_pri, u8 write_pri)
  153. {
  154. u8 *reg8 = (u8 *)base + DDR_RW_PRIORITY_REGS;
  155. reg8 += (port * 3);
  156. pr_debug("%s port %d (reg8=%p, DENALI_CTL_%d)\n",
  157. __func__, port, reg8, (reg8 - (u8 *)base) / 4);
  158. ddrc_writeb(read_pri, reg8++);
  159. ddrc_writeb(write_pri, reg8++);
  160. }
  161. /* The DDR Controller has 16 entries. Each entry can specify an allowed address
  162. * range (with 16KB resolution) for one of the 4 AXI slave ports.
  163. */
  164. void cdns_ddr_enable_port_addr_range(void *base, int port, int entry,
  165. u32 addr_start, u32 size)
  166. {
  167. u32 addr_end;
  168. u32 *reg32 = (u32 *)((u8 *)base + DDR_ADDR_RANGE_REGS);
  169. u32 tmp;
  170. reg32 += (port * DDR_NR_ENTRIES * 2);
  171. reg32 += (entry * 2);
  172. pr_debug("%s port %d, entry %d (reg32=%p, DENALI_CTL_%d)\n",
  173. __func__, port, entry, reg32, ((u8 *)reg32 - (u8 *)base) / 4);
  174. /* These registers represent 16KB address blocks */
  175. addr_start /= SZ_16K;
  176. size /= SZ_16K;
  177. if (size)
  178. addr_end = addr_start + size - 1;
  179. else
  180. addr_end = addr_start;
  181. ddrc_writel(addr_start, reg32++);
  182. /*
  183. * end_addr: Ensure we only set the bottom 18-bits as DENALI_CTL_218
  184. * also contains the AXI0 range protection bits.
  185. */
  186. tmp = readl(reg32);
  187. tmp &= ~(BIT(18) - 1);
  188. tmp |= addr_end;
  189. ddrc_writel(tmp, reg32);
  190. }
  191. void cdns_ddr_enable_addr_range(void *base, int entry,
  192. u32 addr_start, u32 size)
  193. {
  194. int axi;
  195. for (axi = 0; axi < DDR_NR_AXI_PORTS; axi++)
  196. cdns_ddr_enable_port_addr_range(base, axi, entry,
  197. addr_start, size);
  198. }
  199. void cdns_ddr_enable_port_prot(void *base, int port, int entry,
  200. enum cdns_ddr_range_prot range_protection_bits,
  201. u16 range_RID_check_bits,
  202. u16 range_WID_check_bits,
  203. u8 range_RID_check_bits_ID_lookup,
  204. u8 range_WID_check_bits_ID_lookup)
  205. {
  206. /*
  207. * Technically, the offset here points to the byte before the start of
  208. * the range protection registers. However, all entries consist of 8
  209. * bytes, except the first one (which is missing a padding byte) so we
  210. * work around that subtlely.
  211. */
  212. u8 *reg8 = (u8 *)base + DDR_RANGE_PROT_REGS;
  213. reg8 += (port * DDR_NR_ENTRIES * 8);
  214. reg8 += (entry * 8);
  215. pr_debug("%s port %d, entry %d (reg8=%p, DENALI_CTL_%d)\n",
  216. __func__, port, entry, reg8, (reg8 - (u8 *)base) / 4);
  217. if (port == 0 && entry == 0)
  218. ddrc_writeb(range_protection_bits, reg8 + 1);
  219. else
  220. ddrc_writeb(range_protection_bits, reg8);
  221. ddrc_writew(range_RID_check_bits, reg8 + 2);
  222. ddrc_writew(range_WID_check_bits, reg8 + 4);
  223. ddrc_writeb(range_RID_check_bits_ID_lookup, reg8 + 6);
  224. ddrc_writeb(range_WID_check_bits_ID_lookup, reg8 + 7);
  225. }
  226. void cdns_ddr_enable_prot(void *base, int entry,
  227. enum cdns_ddr_range_prot range_protection_bits,
  228. u16 range_RID_check_bits,
  229. u16 range_WID_check_bits,
  230. u8 range_RID_check_bits_ID_lookup,
  231. u8 range_WID_check_bits_ID_lookup)
  232. {
  233. int axi;
  234. for (axi = 0; axi < DDR_NR_AXI_PORTS; axi++)
  235. cdns_ddr_enable_port_prot(base, axi, entry,
  236. range_protection_bits,
  237. range_RID_check_bits,
  238. range_WID_check_bits,
  239. range_RID_check_bits_ID_lookup,
  240. range_WID_check_bits_ID_lookup);
  241. }
  242. void cdns_ddr_set_port_bandwidth(void *base, int port,
  243. u8 max_percent, u8 overflow_ok)
  244. {
  245. u8 *reg8 = (u8 *)base + DDR_AXI_PORT_BANDWIDTH_REG;
  246. reg8 += (port * 3);
  247. pr_debug("%s port %d, (reg8=%p, DENALI_CTL_%d)\n",
  248. __func__, port, reg8, (reg8 - (u8 *)base) / 4);
  249. ddrc_writeb(max_percent, reg8++); /* Maximum bandwidth percentage */
  250. ddrc_writeb(overflow_ok, reg8++); /* Bandwidth overflow allowed */
  251. }
  252. void cdns_ddr_ctrl_init(void *ddr_ctrl_basex, int async,
  253. const u32 *reg0, const u32 *reg350,
  254. u32 ddr_start_addr, u32 ddr_size,
  255. int enable_ecc, int enable_8bit)
  256. {
  257. int i, axi, entry;
  258. u32 *ddr_ctrl_base = (u32 *)ddr_ctrl_basex;
  259. u8 *base8 = (u8 *)ddr_ctrl_basex;
  260. ddrc_writel(*reg0, ddr_ctrl_base + 0);
  261. /* 1 to 6 are read only */
  262. for (i = 7; i <= 26; i++)
  263. ddrc_writel(*(reg0 + i), ddr_ctrl_base + i);
  264. /* 27 to 29 are not changed */
  265. for (i = 30; i <= 87; i++)
  266. ddrc_writel(*(reg0 + i), ddr_ctrl_base + i);
  267. /* Enable/disable ECC */
  268. if (enable_ecc) {
  269. pr_debug("%s enabling ECC\n", __func__);
  270. ddrc_writeb(1, base8 + DDR_ECC_ENABLE_REG);
  271. } else {
  272. ddrc_writeb(0, base8 + DDR_ECC_ENABLE_REG);
  273. }
  274. /* ECC: Disable corruption for read/modify/write operations */
  275. ddrc_writeb(1, base8 + DDR_ECC_DISABLE_W_UC_ERR_REG);
  276. /* Set 8/16-bit data width using reduce bit (enable half datapath)*/
  277. if (enable_8bit) {
  278. pr_debug("%s using 8-bit data\n", __func__);
  279. ddrc_writeb(1, base8 + DDR_HALF_DATAPATH_REG);
  280. } else {
  281. ddrc_writeb(0, base8 + DDR_HALF_DATAPATH_REG);
  282. }
  283. /* Threshold for command queue */
  284. ddrc_writeb(4, base8 + DDR_ARB_CMD_Q_THRESHOLD_REG);
  285. /* AXI port protection => enable */
  286. ddrc_writeb(0x01, base8 + DDR_AXI_PORT_PROT_ENABLE_REG);
  287. /* Set port interface type, default port priority and bandwidths */
  288. for (axi = 0; axi < DDR_NR_AXI_PORTS; axi++) {
  289. /* port interface type: synchronous or asynchronous AXI clock */
  290. u8 *fifo_reg = base8 + DDR_RW_FIFO_TYPE_REGS + (axi * 3);
  291. if (async)
  292. ddrc_writeb(0, fifo_reg);
  293. else
  294. ddrc_writeb(3, fifo_reg);
  295. /* R/W priorities */
  296. cdns_ddr_set_port_rw_priority(ddr_ctrl_base, axi, 2, 2);
  297. /* AXI bandwidth */
  298. cdns_ddr_set_port_bandwidth(ddr_ctrl_base, axi, 50, 1);
  299. }
  300. /*
  301. * The hardware requires that the valid address ranges must not overlap.
  302. * So, we initialise all address ranges to be above the DDR, length 0.
  303. */
  304. for (entry = 0; entry < DDR_NR_ENTRIES; entry++)
  305. cdns_ddr_enable_addr_range(ddr_ctrl_base, entry,
  306. ddr_start_addr + ddr_size, 0);
  307. for (i = 350; i <= 374; i++)
  308. ddrc_writel(*(reg350 - 350 + i), ddr_ctrl_base + i);
  309. /* Disable optimised read-modify-write logic */
  310. ddrc_writeb(0, base8 + DDR_OPT_RMODW_REG);
  311. /*
  312. * Disable all interrupts, we are not handling them.
  313. * For detail of the interrupt mask, ack and status bits, see the
  314. * manual's description of the 'int_status' parameter.
  315. */
  316. ddrc_writel(0, base8 + DDR_INTERRUPT_MASK);
  317. /*
  318. * Default settings to enable full access to the entire DDR.
  319. * Users can set different ranges and access rights by calling these
  320. * functions before calling cdns_ddr_ctrl_start().
  321. */
  322. cdns_ddr_enable_addr_range(ddr_ctrl_base, 0,
  323. ddr_start_addr, ddr_size);
  324. cdns_ddr_enable_prot(ddr_ctrl_base, 0, CDNS_DDR_RANGE_PROT_BITS_FULL,
  325. 0xffff, 0xffff, 0x0f, 0x0f);
  326. }
  327. void cdns_ddr_ctrl_start(void *ddr_ctrl_basex)
  328. {
  329. u32 *ddr_ctrl_base = (u32 *)ddr_ctrl_basex;
  330. u8 *base8 = (u8 *)ddr_ctrl_basex;
  331. /* Start */
  332. ddrc_writeb(1, base8 + DDR_START_REG);
  333. /* Wait for controller to be ready (interrupt status) */
  334. wait_for_bit_le32(base8 + DDR_INTERRUPT_STATUS, 0x100, true, 1000, false);
  335. /* clear all interrupts */
  336. ddrc_writel(~0, base8 + DDR_INTERRUPT_ACK);
  337. /* Step 19 Wait 500us from MRESETB=1 */
  338. udelay(500);
  339. /* Step 20 tCKSRX wait (From supply stable clock for MCK) */
  340. /* DENALI_CTL_19 TREF_ENABLE=0x1(=1), AREFRESH=0x1(=1) */
  341. ddrc_writel(0x01000100, ddr_ctrl_base + 19);
  342. }