ddr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
  4. * Based on Atheros LSDK/QSDK
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <asm/addrspace.h>
  9. #include <asm/types.h>
  10. #include <mach/ar71xx_regs.h>
  11. #include <mach/ath79.h>
  12. #define DDR_CTRL_UPD_EMR3S BIT(5)
  13. #define DDR_CTRL_UPD_EMR2S BIT(4)
  14. #define DDR_CTRL_PRECHARGE BIT(3)
  15. #define DDR_CTRL_AUTO_REFRESH BIT(2)
  16. #define DDR_CTRL_UPD_EMRS BIT(1)
  17. #define DDR_CTRL_UPD_MRS BIT(0)
  18. #define DDR_REFRESH_EN BIT(14)
  19. #define DDR_REFRESH_M 0x3ff
  20. #define DDR_REFRESH(x) ((x) & DDR_REFRESH_M)
  21. #define DDR_REFRESH_VAL (DDR_REFRESH_EN | DDR_REFRESH(312))
  22. #define DDR_TRAS_S 0
  23. #define DDR_TRAS_M 0x1f
  24. #define DDR_TRAS(x) (((x) & DDR_TRAS_M) << DDR_TRAS_S)
  25. #define DDR_TRCD_M 0xf
  26. #define DDR_TRCD_S 5
  27. #define DDR_TRCD(x) (((x) & DDR_TRCD_M) << DDR_TRCD_S)
  28. #define DDR_TRP_M 0xf
  29. #define DDR_TRP_S 9
  30. #define DDR_TRP(x) (((x) & DDR_TRP_M) << DDR_TRP_S)
  31. #define DDR_TRRD_M 0xf
  32. #define DDR_TRRD_S 13
  33. #define DDR_TRRD(x) (((x) & DDR_TRRD_M) << DDR_TRRD_S)
  34. #define DDR_TRFC_M 0x7f
  35. #define DDR_TRFC_S 17
  36. #define DDR_TRFC(x) (((x) & DDR_TRFC_M) << DDR_TRFC_S)
  37. #define DDR_TMRD_M 0xf
  38. #define DDR_TMRD_S 23
  39. #define DDR_TMRD(x) (((x) & DDR_TMRD_M) << DDR_TMRD_S)
  40. #define DDR_CAS_L_M 0x17
  41. #define DDR_CAS_L_S 27
  42. #define DDR_CAS_L(x) (((x) & DDR_CAS_L_M) << DDR_CAS_L_S)
  43. #define DDR_OPEN BIT(30)
  44. #define DDR1_CONF_REG_VAL (DDR_TRAS(16) | DDR_TRCD(6) | \
  45. DDR_TRP(6) | DDR_TRRD(4) | \
  46. DDR_TRFC(7) | DDR_TMRD(5) | \
  47. DDR_CAS_L(7) | DDR_OPEN)
  48. #define DDR2_CONF_REG_VAL (DDR_TRAS(27) | DDR_TRCD(9) | \
  49. DDR_TRP(9) | DDR_TRRD(7) | \
  50. DDR_TRFC(21) | DDR_TMRD(15) | \
  51. DDR_CAS_L(17) | DDR_OPEN)
  52. #define DDR_BURST_LEN_S 0
  53. #define DDR_BURST_LEN_M 0xf
  54. #define DDR_BURST_LEN(x) ((x) << DDR_BURST_LEN_S)
  55. #define DDR_BURST_TYPE BIT(4)
  56. #define DDR_CNTL_OE_EN BIT(5)
  57. #define DDR_PHASE_SEL BIT(6)
  58. #define DDR_CKE BIT(7)
  59. #define DDR_TWR_S 8
  60. #define DDR_TWR_M 0xf
  61. #define DDR_TWR(x) (((x) & DDR_TWR_M) << DDR_TWR_S)
  62. #define DDR_TRTW_S 12
  63. #define DDR_TRTW_M 0x1f
  64. #define DDR_TRTW(x) (((x) & DDR_TRTW_M) << DDR_TRTW_S)
  65. #define DDR_TRTP_S 17
  66. #define DDR_TRTP_M 0xf
  67. #define DDR_TRTP(x) (((x) & DDR_TRTP_M) << DDR_TRTP_S)
  68. #define DDR_TWTR_S 21
  69. #define DDR_TWTR_M 0x1f
  70. #define DDR_TWTR(x) (((x) & DDR_TWTR_M) << DDR_TWTR_S)
  71. #define DDR_G_OPEN_L_S 26
  72. #define DDR_G_OPEN_L_M 0xf
  73. #define DDR_G_OPEN_L(x) ((x) << DDR_G_OPEN_L_S)
  74. #define DDR_HALF_WIDTH_LOW BIT(31)
  75. #define DDR1_CONF2_REG_VAL (DDR_BURST_LEN(8) | DDR_CNTL_OE_EN | \
  76. DDR_CKE | DDR_TWR(13) | DDR_TRTW(14) | \
  77. DDR_TRTP(8) | DDR_TWTR(14) | \
  78. DDR_G_OPEN_L(6) | DDR_HALF_WIDTH_LOW)
  79. #define DDR2_CONF2_REG_VAL (DDR_BURST_LEN(8) | DDR_CNTL_OE_EN | \
  80. DDR_CKE | DDR_TWR(1) | DDR_TRTW(14) | \
  81. DDR_TRTP(9) | DDR_TWTR(21) | \
  82. DDR_G_OPEN_L(8) | DDR_HALF_WIDTH_LOW)
  83. #define DDR_TWR_MSB BIT(3)
  84. #define DDR_TRAS_MSB BIT(2)
  85. #define DDR_TRFC_MSB_M 0x3
  86. #define DDR_TRFC_MSB(x) (x)
  87. #define DDR1_CONF3_REG_VAL 0
  88. #define DDR2_CONF3_REG_VAL (DDR_TWR_MSB | DDR_TRFC_MSB(2))
  89. #define DDR_CTL_SRAM_TSEL BIT(30)
  90. #define DDR_CTL_SRAM_GE0_SYNC BIT(20)
  91. #define DDR_CTL_SRAM_GE1_SYNC BIT(19)
  92. #define DDR_CTL_SRAM_USB_SYNC BIT(18)
  93. #define DDR_CTL_SRAM_PCIE_SYNC BIT(17)
  94. #define DDR_CTL_SRAM_WMAC_SYNC BIT(16)
  95. #define DDR_CTL_SRAM_MISC1_SYNC BIT(15)
  96. #define DDR_CTL_SRAM_MISC2_SYNC BIT(14)
  97. #define DDR_CTL_PAD_DDR2_SEL BIT(6)
  98. #define DDR_CTL_HALF_WIDTH BIT(1)
  99. #define DDR_CTL_CONFIG_VAL (DDR_CTL_SRAM_TSEL | \
  100. DDR_CTL_SRAM_GE0_SYNC | \
  101. DDR_CTL_SRAM_GE1_SYNC | \
  102. DDR_CTL_SRAM_USB_SYNC | \
  103. DDR_CTL_SRAM_PCIE_SYNC | \
  104. DDR_CTL_SRAM_WMAC_SYNC | \
  105. DDR_CTL_HALF_WIDTH)
  106. #define DDR_BURST_GE0_MAX_BL_S 0
  107. #define DDR_BURST_GE0_MAX_BL_M 0xf
  108. #define DDR_BURST_GE0_MAX_BL(x) \
  109. (((x) & DDR_BURST_GE0_MAX_BL_M) << DDR_BURST_GE0_MAX_BL_S)
  110. #define DDR_BURST_GE1_MAX_BL_S 4
  111. #define DDR_BURST_GE1_MAX_BL_M 0xf
  112. #define DDR_BURST_GE1_MAX_BL(x) \
  113. (((x) & DDR_BURST_GE1_MAX_BL_M) << DDR_BURST_GE1_MAX_BL_S)
  114. #define DDR_BURST_PCIE_MAX_BL_S 8
  115. #define DDR_BURST_PCIE_MAX_BL_M 0xf
  116. #define DDR_BURST_PCIE_MAX_BL(x) \
  117. (((x) & DDR_BURST_PCIE_MAX_BL_M) << DDR_BURST_PCIE_MAX_BL_S)
  118. #define DDR_BURST_USB_MAX_BL_S 12
  119. #define DDR_BURST_USB_MAX_BL_M 0xf
  120. #define DDR_BURST_USB_MAX_BL(x) \
  121. (((x) & DDR_BURST_USB_MAX_BL_M) << DDR_BURST_USB_MAX_BL_S)
  122. #define DDR_BURST_CPU_MAX_BL_S 16
  123. #define DDR_BURST_CPU_MAX_BL_M 0xf
  124. #define DDR_BURST_CPU_MAX_BL(x) \
  125. (((x) & DDR_BURST_CPU_MAX_BL_M) << DDR_BURST_CPU_MAX_BL_S)
  126. #define DDR_BURST_RD_MAX_BL_S 20
  127. #define DDR_BURST_RD_MAX_BL_M 0xf
  128. #define DDR_BURST_RD_MAX_BL(x) \
  129. (((x) & DDR_BURST_RD_MAX_BL_M) << DDR_BURST_RD_MAX_BL_S)
  130. #define DDR_BURST_WR_MAX_BL_S 24
  131. #define DDR_BURST_WR_MAX_BL_M 0xf
  132. #define DDR_BURST_WR_MAX_BL(x) \
  133. (((x) & DDR_BURST_WR_MAX_BL_M) << DDR_BURST_WR_MAX_BL_S)
  134. #define DDR_BURST_RWP_MASK_EN_S 28
  135. #define DDR_BURST_RWP_MASK_EN_M 0x3
  136. #define DDR_BURST_RWP_MASK_EN(x) \
  137. (((x) & DDR_BURST_RWP_MASK_EN_M) << DDR_BURST_RWP_MASK_EN_S)
  138. #define DDR_BURST_CPU_PRI_BE BIT(30)
  139. #define DDR_BURST_CPU_PRI BIT(31)
  140. #define DDR_BURST_VAL (DDR_BURST_CPU_PRI_BE | \
  141. DDR_BURST_RWP_MASK_EN(3) | \
  142. DDR_BURST_WR_MAX_BL(4) | \
  143. DDR_BURST_RD_MAX_BL(4) | \
  144. DDR_BURST_CPU_MAX_BL(4) | \
  145. DDR_BURST_USB_MAX_BL(4) | \
  146. DDR_BURST_PCIE_MAX_BL(4) | \
  147. DDR_BURST_GE1_MAX_BL(4) | \
  148. DDR_BURST_GE0_MAX_BL(4))
  149. #define DDR_BURST_WMAC_MAX_BL_S 0
  150. #define DDR_BURST_WMAC_MAX_BL_M 0xf
  151. #define DDR_BURST_WMAC_MAX_BL(x) \
  152. (((x) & DDR_BURST_WMAC_MAX_BL_M) << DDR_BURST_WMAC_MAX_BL_S)
  153. #define DDR_BURST2_VAL DDR_BURST_WMAC_MAX_BL(4)
  154. #define DDR2_CONF_TWL_S 10
  155. #define DDR2_CONF_TWL_M 0xf
  156. #define DDR2_CONF_TWL(x) \
  157. (((x) & DDR2_CONF_TWL_M) << DDR2_CONF_TWL_S)
  158. #define DDR2_CONF_ODT BIT(9)
  159. #define DDR2_CONF_TFAW_S 2
  160. #define DDR2_CONF_TFAW_M 0x3f
  161. #define DDR2_CONF_TFAW(x) \
  162. (((x) & DDR2_CONF_TFAW_M) << DDR2_CONF_TFAW_S)
  163. #define DDR2_CONF_EN BIT(0)
  164. #define DDR2_CONF_VAL (DDR2_CONF_TWL(5) | \
  165. DDR2_CONF_TFAW(31) | \
  166. DDR2_CONF_ODT | \
  167. DDR2_CONF_EN)
  168. #define DDR1_EXT_MODE_VAL 0
  169. #define DDR2_EXT_MODE_VAL 0x402
  170. #define DDR2_EXT_MODE_OCD_VAL 0x782
  171. #define DDR1_MODE_DLL_VAL 0x133
  172. #define DDR2_MODE_DLL_VAL 0x143
  173. #define DDR1_MODE_VAL 0x33
  174. #define DDR2_MODE_VAL 0x43
  175. #define DDR1_TAP_VAL 0x20
  176. #define DDR2_TAP_VAL 0x10
  177. #define DDR_REG_BIST_MASK_ADDR_0 0x2c
  178. #define DDR_REG_BIST_MASK_ADDR_1 0x30
  179. #define DDR_REG_BIST_MASK_AHB_GE0_0 0x34
  180. #define DDR_REG_BIST_COMP_AHB_GE0_0 0x38
  181. #define DDR_REG_BIST_MASK_AHB_GE1_0 0x3c
  182. #define DDR_REG_BIST_COMP_AHB_GE1_0 0x40
  183. #define DDR_REG_BIST_COMP_ADDR_0 0x64
  184. #define DDR_REG_BIST_COMP_ADDR_1 0x68
  185. #define DDR_REG_BIST_MASK_AHB_GE0_1 0x6c
  186. #define DDR_REG_BIST_COMP_AHB_GE0_1 0x70
  187. #define DDR_REG_BIST_MASK_AHB_GE1_1 0x74
  188. #define DDR_REG_BIST_COMP_AHB_GE1_1 0x78
  189. #define DDR_REG_BIST 0x11c
  190. #define DDR_REG_BIST_STATUS 0x120
  191. #define DDR_BIST_COMP_CNT_S 1
  192. #define DDR_BIST_COMP_CNT_M 0xff
  193. #define DDR_BIST_COMP_CNT(x) \
  194. (((x) & DDR_BIST_COMP_CNT_M) << DDR_BIST_COMP_CNT_S)
  195. #define DDR_BIST_COMP_CNT_MASK \
  196. (DDR_BIST_COMP_CNT_M << DDR_BIST_COMP_CNT_S)
  197. #define DDR_BIST_TEST_START BIT(0)
  198. #define DDR_BIST_STATUS_DONE BIT(0)
  199. /* 4 Row Address Bits, 4 Column Address Bits, 2 BA bits */
  200. #define DDR_BIST_MASK_ADDR_VAL 0xfa5de83f
  201. #define DDR_TAP_MAGIC_VAL 0xaa55aa55
  202. #define DDR_TAP_MAX_VAL 0x40
  203. void ddr_init(void)
  204. {
  205. void __iomem *regs;
  206. u32 val;
  207. regs = map_physmem(AR71XX_DDR_CTRL_BASE, AR71XX_DDR_CTRL_SIZE,
  208. MAP_NOCACHE);
  209. val = ath79_get_bootstrap();
  210. if (val & QCA953X_BOOTSTRAP_DDR1) {
  211. writel(DDR_CTL_CONFIG_VAL, regs + QCA953X_DDR_REG_CTL_CONF);
  212. udelay(10);
  213. /* For 16-bit DDR */
  214. writel(0xffff, regs + AR71XX_DDR_REG_RD_CYCLE);
  215. udelay(100);
  216. /* Burst size */
  217. writel(DDR_BURST_VAL, regs + QCA953X_DDR_REG_BURST);
  218. udelay(100);
  219. writel(DDR_BURST2_VAL, regs + QCA953X_DDR_REG_BURST2);
  220. udelay(100);
  221. /* AHB maximum timeout */
  222. writel(0xfffff, regs + QCA953X_DDR_REG_TIMEOUT_MAX);
  223. udelay(100);
  224. /* DRAM timing */
  225. writel(DDR1_CONF_REG_VAL, regs + AR71XX_DDR_REG_CONFIG);
  226. udelay(100);
  227. writel(DDR1_CONF2_REG_VAL, regs + AR71XX_DDR_REG_CONFIG2);
  228. udelay(100);
  229. writel(DDR1_CONF3_REG_VAL, regs + QCA953X_DDR_REG_CONFIG3);
  230. udelay(100);
  231. /* Precharge All */
  232. writel(DDR_CTRL_PRECHARGE, regs + AR71XX_DDR_REG_CONTROL);
  233. udelay(100);
  234. /* ODT disable, Full strength, Enable DLL */
  235. writel(DDR1_EXT_MODE_VAL, regs + AR71XX_DDR_REG_EMR);
  236. udelay(100);
  237. /* Update Extended Mode Register Set (EMRS) */
  238. writel(DDR_CTRL_UPD_EMRS, regs + AR71XX_DDR_REG_CONTROL);
  239. udelay(100);
  240. /* Reset DLL, CAS Latency 3, Burst Length 8 */
  241. writel(DDR1_MODE_DLL_VAL, regs + AR71XX_DDR_REG_MODE);
  242. udelay(100);
  243. /* Update Mode Register Set (MRS) */
  244. writel(DDR_CTRL_UPD_MRS, regs + AR71XX_DDR_REG_CONTROL);
  245. udelay(100);
  246. /* Precharge All */
  247. writel(DDR_CTRL_PRECHARGE, regs + AR71XX_DDR_REG_CONTROL);
  248. udelay(100);
  249. /* Auto Refresh */
  250. writel(DDR_CTRL_AUTO_REFRESH, regs + AR71XX_DDR_REG_CONTROL);
  251. udelay(100);
  252. writel(DDR_CTRL_AUTO_REFRESH, regs + AR71XX_DDR_REG_CONTROL);
  253. udelay(100);
  254. /* Normal DLL, CAS Latency 3, Burst Length 8 */
  255. writel(DDR1_MODE_VAL, regs + AR71XX_DDR_REG_MODE);
  256. udelay(100);
  257. /* Update Mode Register Set (MRS) */
  258. writel(DDR_CTRL_UPD_MRS, regs + AR71XX_DDR_REG_CONTROL);
  259. udelay(100);
  260. /* Refresh time control */
  261. writel(DDR_REFRESH_VAL, regs + AR71XX_DDR_REG_REFRESH);
  262. udelay(100);
  263. /* DQS 0 Tap Control */
  264. writel(DDR1_TAP_VAL, regs + AR71XX_DDR_REG_TAP_CTRL0);
  265. /* DQS 1 Tap Control */
  266. writel(DDR1_TAP_VAL, regs + AR71XX_DDR_REG_TAP_CTRL1);
  267. } else {
  268. writel(DDR_CTRL_UPD_EMR2S, regs + AR71XX_DDR_REG_CONTROL);
  269. udelay(10);
  270. writel(DDR_CTRL_UPD_EMR3S, regs + AR71XX_DDR_REG_CONTROL);
  271. udelay(10);
  272. writel(DDR_CTL_CONFIG_VAL | DDR_CTL_PAD_DDR2_SEL,
  273. regs + QCA953X_DDR_REG_CTL_CONF);
  274. udelay(10);
  275. /* For 16-bit DDR */
  276. writel(0xffff, regs + AR71XX_DDR_REG_RD_CYCLE);
  277. udelay(100);
  278. /* Burst size */
  279. writel(DDR_BURST_VAL, regs + QCA953X_DDR_REG_BURST);
  280. udelay(100);
  281. writel(DDR_BURST2_VAL, regs + QCA953X_DDR_REG_BURST2);
  282. udelay(100);
  283. /* AHB maximum timeout */
  284. writel(0xfffff, regs + QCA953X_DDR_REG_TIMEOUT_MAX);
  285. udelay(100);
  286. /* DRAM timing */
  287. writel(DDR2_CONF_REG_VAL, regs + AR71XX_DDR_REG_CONFIG);
  288. udelay(100);
  289. writel(DDR2_CONF2_REG_VAL, regs + AR71XX_DDR_REG_CONFIG2);
  290. udelay(100);
  291. writel(DDR2_CONF3_REG_VAL, regs + QCA953X_DDR_REG_CONFIG3);
  292. udelay(100);
  293. /* Enable DDR2 */
  294. writel(DDR2_CONF_VAL, regs + QCA953X_DDR_REG_DDR2_CONFIG);
  295. udelay(100);
  296. /* Precharge All */
  297. writel(DDR_CTRL_PRECHARGE, regs + AR71XX_DDR_REG_CONTROL);
  298. udelay(100);
  299. /* Update Extended Mode Register 2 Set (EMR2S) */
  300. writel(DDR_CTRL_UPD_EMR2S, regs + AR71XX_DDR_REG_CONTROL);
  301. udelay(100);
  302. /* Update Extended Mode Register 3 Set (EMR3S) */
  303. writel(DDR_CTRL_UPD_EMR3S, regs + AR71XX_DDR_REG_CONTROL);
  304. udelay(100);
  305. /* 150 ohm, Reduced strength, Enable DLL */
  306. writel(DDR2_EXT_MODE_VAL, regs + AR71XX_DDR_REG_EMR);
  307. udelay(100);
  308. /* Update Extended Mode Register Set (EMRS) */
  309. writel(DDR_CTRL_UPD_EMRS, regs + AR71XX_DDR_REG_CONTROL);
  310. udelay(100);
  311. /* Reset DLL, CAS Latency 4, Burst Length 8 */
  312. writel(DDR2_MODE_DLL_VAL, regs + AR71XX_DDR_REG_MODE);
  313. udelay(100);
  314. /* Update Mode Register Set (MRS) */
  315. writel(DDR_CTRL_UPD_MRS, regs + AR71XX_DDR_REG_CONTROL);
  316. udelay(100);
  317. /* Precharge All */
  318. writel(DDR_CTRL_PRECHARGE, regs + AR71XX_DDR_REG_CONTROL);
  319. udelay(100);
  320. /* Auto Refresh */
  321. writel(DDR_CTRL_AUTO_REFRESH, regs + AR71XX_DDR_REG_CONTROL);
  322. udelay(100);
  323. writel(DDR_CTRL_AUTO_REFRESH, regs + AR71XX_DDR_REG_CONTROL);
  324. udelay(100);
  325. /* Normal DLL, CAS Latency 4, Burst Length 8 */
  326. writel(DDR2_MODE_VAL, regs + AR71XX_DDR_REG_MODE);
  327. udelay(100);
  328. /* Mode Register Set (MRS) */
  329. writel(DDR_CTRL_UPD_MRS, regs + AR71XX_DDR_REG_CONTROL);
  330. udelay(100);
  331. /* Enable OCD, Enable DLL, Reduced Drive Strength */
  332. writel(DDR2_EXT_MODE_OCD_VAL, regs + AR71XX_DDR_REG_EMR);
  333. udelay(100);
  334. /* Update Extended Mode Register Set (EMRS) */
  335. writel(DDR_CTRL_UPD_EMRS, regs + AR71XX_DDR_REG_CONTROL);
  336. udelay(100);
  337. /* OCD diable, Enable DLL, Reduced Drive Strength */
  338. writel(DDR2_EXT_MODE_VAL, regs + AR71XX_DDR_REG_EMR);
  339. udelay(100);
  340. /* Update Extended Mode Register Set (EMRS) */
  341. writel(DDR_CTRL_UPD_EMRS, regs + AR71XX_DDR_REG_CONTROL);
  342. udelay(100);
  343. /* Refresh time control */
  344. writel(DDR_REFRESH_VAL, regs + AR71XX_DDR_REG_REFRESH);
  345. udelay(100);
  346. /* DQS 0 Tap Control */
  347. writel(DDR2_TAP_VAL, regs + AR71XX_DDR_REG_TAP_CTRL0);
  348. /* DQS 1 Tap Control */
  349. writel(DDR2_TAP_VAL, regs + AR71XX_DDR_REG_TAP_CTRL1);
  350. }
  351. }
  352. void ddr_tap_tuning(void)
  353. {
  354. void __iomem *regs;
  355. u32 val, pass, tap, cnt, tap_val, last, first;
  356. regs = map_physmem(AR71XX_DDR_CTRL_BASE, AR71XX_DDR_CTRL_SIZE,
  357. MAP_NOCACHE);
  358. tap_val = readl(regs + AR71XX_DDR_REG_TAP_CTRL0);
  359. first = DDR_TAP_MAGIC_VAL;
  360. last = 0;
  361. cnt = 0;
  362. tap = 0;
  363. do {
  364. writel(tap, regs + AR71XX_DDR_REG_TAP_CTRL0);
  365. writel(tap, regs + AR71XX_DDR_REG_TAP_CTRL1);
  366. writel(DDR_BIST_COMP_CNT(8), regs + DDR_REG_BIST_COMP_ADDR_1);
  367. writel(DDR_BIST_MASK_ADDR_VAL, regs + DDR_REG_BIST_MASK_ADDR_0);
  368. writel(0xffff, regs + DDR_REG_BIST_COMP_AHB_GE0_1);
  369. writel(0xffff, regs + DDR_REG_BIST_COMP_AHB_GE1_0);
  370. writel(0xffff, regs + DDR_REG_BIST_COMP_AHB_GE1_1);
  371. writel(0xffff, regs + DDR_REG_BIST_MASK_AHB_GE0_0);
  372. writel(0xffff, regs + DDR_REG_BIST_MASK_AHB_GE0_1);
  373. writel(0xffff, regs + DDR_REG_BIST_MASK_AHB_GE1_0);
  374. writel(0xffff, regs + DDR_REG_BIST_MASK_AHB_GE1_1);
  375. writel(0xffff, regs + DDR_REG_BIST_COMP_AHB_GE0_0);
  376. /* Start BIST test */
  377. writel(DDR_BIST_TEST_START, regs + DDR_REG_BIST);
  378. do {
  379. val = readl(regs + DDR_REG_BIST_STATUS);
  380. } while (!(val & DDR_BIST_STATUS_DONE));
  381. /* Stop BIST test */
  382. writel(0, regs + DDR_REG_BIST);
  383. pass = val & DDR_BIST_COMP_CNT_MASK;
  384. pass ^= DDR_BIST_COMP_CNT(8);
  385. if (!pass) {
  386. if (first != DDR_TAP_MAGIC_VAL) {
  387. last = tap;
  388. } else {
  389. first = tap;
  390. last = tap;
  391. }
  392. cnt++;
  393. }
  394. tap++;
  395. } while (tap < DDR_TAP_MAX_VAL);
  396. if (cnt) {
  397. tap_val = (first + last) / 2;
  398. tap_val %= DDR_TAP_MAX_VAL;
  399. }
  400. writel(tap_val, regs + AR71XX_DDR_REG_TAP_CTRL0);
  401. writel(tap_val, regs + AR71XX_DDR_REG_TAP_CTRL1);
  402. }