lowlevel_init.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (c) 2018 Stefan Roese <sr@denx.de>
  4. *
  5. * This code is mostly based on the code extracted from this MediaTek
  6. * github repository:
  7. *
  8. * https://github.com/MediaTek-Labs/linkit-smart-uboot.git
  9. *
  10. * I was not able to find a specific license or other developers
  11. * copyrights here, so I can't add them here.
  12. */
  13. #include <config.h>
  14. #include <asm/regdef.h>
  15. #include <asm/mipsregs.h>
  16. #include <asm/addrspace.h>
  17. #include <asm/asm.h>
  18. #include "mt76xx.h"
  19. #ifndef BIT
  20. #define BIT(nr) (1 << (nr))
  21. #endif
  22. #define DELAY_USEC(us) ((us) / 100)
  23. #define DDR_CFG1_CHIP_WIDTH_MASK (0x3 << 16)
  24. #define DDR_CFG1_BUS_WIDTH_MASK (0x3 << 12)
  25. #if defined(CONFIG_ONBOARD_DDR2_SIZE_256MBIT)
  26. #define DDR_CFG1_SIZE_VAL 0x222e2323
  27. #define DDR_CFG4_SIZE_VAL 7
  28. #endif
  29. #if defined(CONFIG_ONBOARD_DDR2_SIZE_512MBIT)
  30. #define DDR_CFG1_SIZE_VAL 0x22322323
  31. #define DDR_CFG4_SIZE_VAL 9
  32. #endif
  33. #if defined(CONFIG_ONBOARD_DDR2_SIZE_1024MBIT)
  34. #define DDR_CFG1_SIZE_VAL 0x22362323
  35. #define DDR_CFG4_SIZE_VAL 9
  36. #endif
  37. #if defined(CONFIG_ONBOARD_DDR2_SIZE_2048MBIT)
  38. #define DDR_CFG1_SIZE_VAL 0x223a2323
  39. #define DDR_CFG4_SIZE_VAL 9
  40. #endif
  41. #if defined(CONFIG_ONBOARD_DDR2_CHIP_WIDTH_8BIT)
  42. #define DDR_CFG1_CHIP_WIDTH_VAL (0x1 << 16)
  43. #endif
  44. #if defined(CONFIG_ONBOARD_DDR2_CHIP_WIDTH_16BIT)
  45. #define DDR_CFG1_CHIP_WIDTH_VAL (0x2 << 16)
  46. #endif
  47. #if defined(CONFIG_ONBOARD_DDR2_BUS_WIDTH_16BIT)
  48. #define DDR_CFG1_BUS_WIDTH_VAL (0x2 << 12)
  49. #endif
  50. #if defined(CONFIG_ONBOARD_DDR2_BUS_WIDTH_32BIT)
  51. #define DDR_CFG1_BUS_WIDTH_VAL (0x3 << 12)
  52. #endif
  53. .set noreorder
  54. LEAF(lowlevel_init)
  55. /* Load base addresses as physical addresses for later usage */
  56. li s0, CKSEG1ADDR(MT76XX_SYSCTL_BASE)
  57. li s1, CKSEG1ADDR(MT76XX_MEMCTRL_BASE)
  58. li s2, CKSEG1ADDR(MT76XX_RGCTRL_BASE)
  59. /* polling CPLL is ready */
  60. li t1, DELAY_USEC(1000000)
  61. la t5, MT76XX_ROM_STATUS_REG
  62. 1:
  63. lw t2, 0(t5)
  64. andi t2, t2, 0x1
  65. bnez t2, CPLL_READY
  66. subu t1, t1, 1
  67. bgtz t1, 1b
  68. nop
  69. la t0, MT76XX_CLKCFG0_REG
  70. lw t3, 0(t0)
  71. ori t3, t3, 0x1
  72. sw t3, 0(t0)
  73. b CPLL_DONE
  74. nop
  75. CPLL_READY:
  76. la t0, MT76XX_CLKCFG0_REG
  77. lw t1, 0(t0)
  78. li t2, ~0x0c
  79. and t1, t1, t2
  80. ori t1, t1, 0xc
  81. sw t1, 0(t0)
  82. la t0, MT76XX_DYN_CFG0_REG
  83. lw t3, 0(t0)
  84. li t5, ~((0x0f << 8) | (0x0f << 0))
  85. and t3, t3, t5
  86. li t5, (10 << 8) | (1 << 0)
  87. or t3, t3, t5
  88. sw t3, 0(t0)
  89. la t0, MT76XX_CLKCFG0_REG
  90. lw t3, 0(t0)
  91. li t4, ~0x0F
  92. and t3, t3, t4
  93. ori t3, t3, 0xc
  94. sw t3, 0(t0)
  95. lw t3, 0(t0)
  96. ori t3, t3, 0x08
  97. sw t3, 0(t0)
  98. CPLL_DONE:
  99. /*
  100. * SDR and DDR initialization: delay 200us
  101. */
  102. li t0, DELAY_USEC(200 + 40)
  103. li t1, 0x1
  104. 1:
  105. sub t0, t0, t1
  106. bnez t0, 1b
  107. nop
  108. /* set DRAM IO PAD for MT7628IC */
  109. /* DDR LDO Enable */
  110. lw t4, 0x100(s2)
  111. li t2, BIT(31)
  112. or t4, t4, t2
  113. sw t4, 0x100(s2)
  114. lw t4, 0x10c(s2)
  115. j LDO_1P8V
  116. nop
  117. LDO_1P8V:
  118. li t2, ~BIT(6)
  119. and t4, t4, t2
  120. sw t4, 0x10c(s2)
  121. j DDRLDO_SOFT_START
  122. LDO_2P5V:
  123. /* suppose external DDR1 LDO 2.5V */
  124. li t2, BIT(6)
  125. or t4, t4, t2
  126. sw t4, 0x10c(s2)
  127. DDRLDO_SOFT_START:
  128. lw t2, 0x10c(s2)
  129. li t3, BIT(16)
  130. or t2, t2, t3
  131. sw t2, 0x10c(s2)
  132. li t3, DELAY_USEC(250*50)
  133. LDO_DELAY:
  134. subu t3, t3, 1
  135. bnez t3, LDO_DELAY
  136. nop
  137. lw t2, 0x10c(s2)
  138. li t3, BIT(18)
  139. or t2, t2, t3
  140. sw t2, 0x10c(s2)
  141. SET_RG_BUCK_FPWM:
  142. lw t2, 0x104(s2)
  143. ori t2, t2, BIT(10)
  144. sw t2, 0x104(s2)
  145. DDR_PAD_CFG:
  146. /* clean CLK PAD */
  147. lw t2, 0x704(s2)
  148. li t8, 0xfffff0f0
  149. and t2, t2, t8
  150. /* clean CMD PAD */
  151. lw t3, 0x70c(s2)
  152. li t8, 0xfffff0f0
  153. and t3, t3, t8
  154. /* clean DQ IPAD */
  155. lw t4, 0x710(s2)
  156. li t8, 0xfffff8ff
  157. and t4, t4, t8
  158. /* clean DQ OPAD */
  159. lw t5, 0x714(s2)
  160. li t8, 0xfffff0f0
  161. and t5, t5, t8
  162. /* clean DQS IPAD */
  163. lw t6, 0x718(s2)
  164. li t8, 0xfffff8ff
  165. and t6, t6, t8
  166. /* clean DQS OPAD */
  167. lw t7, 0x71c(s2)
  168. li t8, 0xfffff0f0
  169. and t7, t7, t8
  170. lw t9, 0xc(s0)
  171. srl t9, t9, 16
  172. andi t9, t9, 0x1
  173. bnez t9, MT7628_AN_DDR1_PAD
  174. MT7628_KN_PAD:
  175. li t8, 0x00000303
  176. or t2, t2, t8
  177. or t3, t3, t8
  178. or t5, t5, t8
  179. or t7, t7, t8
  180. li t8, 0x00000000
  181. or t4, t4, t8
  182. or t6, t6, t8
  183. j SET_PAD_CFG
  184. MT7628_AN_DDR1_PAD:
  185. lw t1, 0x10(s0)
  186. andi t1, t1, 0x1
  187. beqz t1, MT7628_AN_DDR2_PAD
  188. li t8, 0x00000c0c
  189. or t2, t2, t8
  190. li t8, 0x00000202
  191. or t3, t3, t8
  192. li t8, 0x00000707
  193. or t5, t5, t8
  194. li t8, 0x00000c0c
  195. or t7, t7, t8
  196. li t8, 0x00000000
  197. or t4, t4, t8
  198. or t6, t6, t8
  199. j SET_PAD_CFG
  200. MT7628_AN_DDR2_PAD:
  201. li t8, 0x00000c0c
  202. or t2, t2, t8
  203. li t8, 0x00000202
  204. or t3, t3, t8
  205. li t8, 0x00000404
  206. or t5, t5, t8
  207. li t8, 0x00000c0c
  208. or t7, t7, t8
  209. li t8, 0x00000000 /* ODT off */
  210. or t4, t4, t8
  211. or t6, t6, t8
  212. SET_PAD_CFG:
  213. sw t2, 0x704(s2)
  214. sw t3, 0x70c(s2)
  215. sw t4, 0x710(s2)
  216. sw t5, 0x714(s2)
  217. sw t6, 0x718(s2)
  218. sw t7, 0x71c(s2)
  219. /*
  220. * DDR initialization: reset pin to 0
  221. */
  222. lw t2, 0x34(s0)
  223. and t2, ~BIT(10)
  224. sw t2, 0x34(s0)
  225. nop
  226. /*
  227. * DDR initialization: wait til reg DDR_CFG1 bit 21 equal to 1 (ready)
  228. */
  229. DDR_READY:
  230. li t1, DDR_CFG1_REG
  231. lw t0, 0(t1)
  232. nop
  233. and t2, t0, BIT(21)
  234. beqz t2, DDR_READY
  235. nop
  236. /*
  237. * DDR initialization
  238. *
  239. * Only DDR2 supported right now. DDR2 support can be added, once
  240. * boards using it will get added to mainline U-Boot.
  241. */
  242. li t1, DDR_CFG2_REG
  243. lw t0, 0(t1)
  244. nop
  245. and t0, ~BIT(30)
  246. and t0, ~(7 << 4)
  247. or t0, (4 << 4)
  248. or t0, BIT(30)
  249. or t0, BIT(11)
  250. sw t0, 0(t1)
  251. nop
  252. li t1, DDR_CFG3_REG
  253. lw t2, 0(t1)
  254. /* Disable ODT; reference board ok, ev board fail */
  255. and t2, ~BIT(6)
  256. or t2, BIT(2)
  257. li t0, DDR_CFG4_REG
  258. lw t1, 0(t0)
  259. li t2, ~(0x01f | 0x0f0)
  260. and t1, t1, t2
  261. ori t1, t1, DDR_CFG4_SIZE_VAL
  262. sw t1, 0(t0)
  263. nop
  264. /*
  265. * DDR initialization: config size and width on reg DDR_CFG1
  266. */
  267. li t6, DDR_CFG1_SIZE_VAL
  268. and t6, ~DDR_CFG1_CHIP_WIDTH_MASK
  269. or t6, DDR_CFG1_CHIP_WIDTH_VAL
  270. /* CONFIG DDR_CFG1[13:12] about TOTAL WIDTH */
  271. and t6, ~DDR_CFG1_BUS_WIDTH_MASK
  272. or t6, DDR_CFG1_BUS_WIDTH_VAL
  273. li t5, DDR_CFG1_REG
  274. sw t6, 0(t5)
  275. nop
  276. /*
  277. * DDR: enable self auto refresh for power saving
  278. * enable it by default for both RAM and ROM version (for CoC)
  279. */
  280. lw t1, 0x14(s1)
  281. nop
  282. and t1, 0xff000000
  283. or t1, 0x01
  284. sw t1, 0x14(s1)
  285. nop
  286. lw t1, 0x10(s1)
  287. nop
  288. or t1, 0x10
  289. sw t1, 0x10(s1)
  290. nop
  291. jr ra
  292. nop
  293. END(lowlevel_init)