ddr.c 15 KB

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