lowlevel_init.S 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 and u-boot_mod project
  5. */
  6. #include <config.h>
  7. #include <asm/asm.h>
  8. #include <asm/regdef.h>
  9. #include <asm/mipsregs.h>
  10. #include <asm/addrspace.h>
  11. #include <mach/ar71xx_regs.h>
  12. #define SET_BIT(val, bit) ((val) | (1 << (bit)))
  13. #define SET_PLL_PD(val) SET_BIT(val, 30)
  14. #define AHB_DIV_TO_4(val) SET_BIT(SET_BIT(val, 15), 16)
  15. #define PLL_BYPASS(val) SET_BIT(val, 2)
  16. #define MK_PLL_CONF(divint, refdiv, range, outdiv) \
  17. (((0x3F & divint) << 10) | \
  18. ((0x1F & refdiv) << 16) | \
  19. ((0x1 & range) << 21) | \
  20. ((0x7 & outdiv) << 23) )
  21. #define MK_CLK_CNTL(cpudiv, ddrdiv, ahbdiv) \
  22. (((0x3 & (cpudiv - 1)) << 5) | \
  23. ((0x3 & (ddrdiv - 1)) << 10) | \
  24. ((0x3 & (ahbdiv - 1)) << 15) )
  25. /*
  26. * PLL_CPU_CONFIG_VAL
  27. *
  28. * Bit30 is set (CPU_PLLPWD = 1 -> power down control for CPU PLL)
  29. * After PLL configuration we need to clear this bit
  30. *
  31. * Values written into CPU PLL Configuration (CPU_PLL_CONFIG)
  32. *
  33. * bits 10..15 (6bit) DIV_INT (Integer part of the DIV to CPU PLL)
  34. * => 32 (0x20) VCOOUT = XTAL * DIV_INT
  35. * bits 16..20 (5bit) REFDIV (Reference clock divider)
  36. * => 1 (0x1) [Must start at values 1]
  37. * bits 21 (1bit) RANGE (VCO frequency range of the CPU PLL)
  38. * => 0 (0x0) [Doesn't impact clock values]
  39. * bits 23..25 (3bit) OUTDIV (Ratio between VCO and PLL output)
  40. * => 1 (0x1) [0 is illegal!]
  41. * PLLOUT = VCOOUT * (1/2^OUTDIV)
  42. */
  43. /* DIV_INT=32 (25MHz*32/2=400MHz), REFDIV=1, RANGE=0, OUTDIV=1 */
  44. #define PLL_CPU_CONFIG_VAL_40M MK_PLL_CONF(20, 1, 0, 1)
  45. /* DIV_INT=20 (40MHz*20/2=400MHz), REFDIV=1, RANGE=0, OUTDIV=1 */
  46. #define PLL_CPU_CONFIG_VAL_25M MK_PLL_CONF(32, 1, 0, 1)
  47. /*
  48. * PLL_CLK_CONTROL_VAL
  49. *
  50. * In PLL_CLK_CONTROL_VAL bit 2 is set (BYPASS = 1 -> bypass PLL)
  51. * After PLL configuration we need to clear this bit
  52. *
  53. * Values written into CPU Clock Control Register CLOCK_CONTROL
  54. *
  55. * bits 2 (1bit) BYPASS (Bypass PLL. This defaults to 1 for test.
  56. * Software must enable the CPU PLL for normal and
  57. * then set this bit to 0)
  58. * bits 5..6 (2bit) CPU_POST_DIV => 0 (DEFAULT, Ratio = 1)
  59. * CPU_CLK = PLLOUT / CPU_POST_DIV
  60. * bits 10..11 (2bit) DDR_POST_DIV => 0 (DEFAULT, Ratio = 1)
  61. * DDR_CLK = PLLOUT / DDR_POST_DIV
  62. * bits 15..16 (2bit) AHB_POST_DIV => 1 (DEFAULT, Ratio = 2)
  63. * AHB_CLK = PLLOUT / AHB_POST_DIV
  64. *
  65. */
  66. #define PLL_CLK_CONTROL_VAL MK_CLK_CNTL(1, 1, 2)
  67. .text
  68. .set noreorder
  69. LEAF(lowlevel_init)
  70. /* These three WLAN_RESET will avoid original issue */
  71. li t3, 0x03
  72. 1:
  73. li t0, CKSEG1ADDR(AR71XX_RESET_BASE)
  74. lw t1, AR933X_RESET_REG_RESET_MODULE(t0)
  75. ori t1, t1, 0x0800
  76. sw t1, AR933X_RESET_REG_RESET_MODULE(t0)
  77. nop
  78. lw t1, AR933X_RESET_REG_RESET_MODULE(t0)
  79. li t2, 0xfffff7ff
  80. and t1, t1, t2
  81. sw t1, AR933X_RESET_REG_RESET_MODULE(t0)
  82. nop
  83. addi t3, t3, -1
  84. bnez t3, 1b
  85. nop
  86. li t2, 0x20
  87. 2:
  88. beqz t2, 1b
  89. nop
  90. addi t2, t2, -1
  91. lw t5, AR933X_RESET_REG_BOOTSTRAP(t0)
  92. andi t1, t5, 0x10
  93. bnez t1, 2b
  94. nop
  95. li t1, 0x02110E
  96. sw t1, AR933X_RESET_REG_BOOTSTRAP(t0)
  97. nop
  98. /* RTC Force Wake */
  99. li t0, CKSEG1ADDR(AR933X_RTC_BASE)
  100. li t1, 0x03
  101. sw t1, AR933X_RTC_REG_FORCE_WAKE(t0)
  102. nop
  103. nop
  104. /* RTC Reset */
  105. li t1, 0x00
  106. sw t1, AR933X_RTC_REG_RESET(t0)
  107. nop
  108. nop
  109. li t1, 0x01
  110. sw t1, AR933X_RTC_REG_RESET(t0)
  111. nop
  112. nop
  113. /* Wait for RTC in on state */
  114. 1:
  115. lw t1, AR933X_RTC_REG_STATUS(t0)
  116. andi t1, t1, 0x02
  117. beqz t1, 1b
  118. nop
  119. /* Program ki/kd */
  120. li t0, CKSEG1ADDR(AR933X_SRIF_BASE)
  121. andi t1, t5, 0x01 # t5 BOOT_STRAP
  122. bnez t1, 1f
  123. nop
  124. li t1, 0x19e82f01
  125. b 2f
  126. nop
  127. 1:
  128. li t1, 0x18e82f01
  129. 2:
  130. sw t1, AR933X_SRIF_DDR_DPLL2_REG(t0)
  131. /* Program phase shift */
  132. lw t1, AR933X_SRIF_DDR_DPLL3_REG(t0)
  133. li t2, 0xc07fffff
  134. and t1, t1, t2
  135. li t2, 0x800000
  136. or t1, t1, t2
  137. sw t1, AR933X_SRIF_DDR_DPLL3_REG(t0)
  138. nop
  139. /* in some cases, the SoC doesn't start with higher clock on AHB */
  140. li t0, CKSEG1ADDR(AR71XX_PLL_BASE)
  141. li t1, AHB_DIV_TO_4(PLL_BYPASS(PLL_CLK_CONTROL_VAL))
  142. sw t1, AR933X_PLL_CLK_CTRL_REG(t0)
  143. nop
  144. /* Set SETTLE_TIME in CPU PLL */
  145. andi t1, t5, 0x01 # t5 BOOT_STRAP
  146. bnez t1, 1f
  147. nop
  148. li t1, 0x0352
  149. b 2f
  150. nop
  151. 1:
  152. li t1, 0x0550
  153. 2:
  154. sw t1, AR71XX_PLL_REG_SEC_CONFIG(t0)
  155. nop
  156. /* Set nint, frac, refdiv, outdiv, range according to xtal */
  157. 0:
  158. andi t1, t5, 0x01 # t5 BOOT_STRAP
  159. bnez t1, 1f
  160. nop
  161. li t1, SET_PLL_PD(PLL_CPU_CONFIG_VAL_25M)
  162. b 2f
  163. nop
  164. 1:
  165. li t1, SET_PLL_PD(PLL_CPU_CONFIG_VAL_40M)
  166. 2:
  167. sw t1, AR933X_PLL_CPU_CONFIG_REG(t0)
  168. nop
  169. 1:
  170. lw t1, AR933X_PLL_CPU_CONFIG_REG(t0)
  171. li t2, 0x80000000
  172. and t1, t1, t2
  173. bnez t1, 1b
  174. nop
  175. /* Put frac bit19:10 configuration */
  176. li t1, 0x1003E8
  177. sw t1, AR933X_PLL_DITHER_FRAC_REG(t0)
  178. nop
  179. /* Clear PLL power down bit in CPU PLL configuration */
  180. andi t1, t5, 0x01 # t5 BOOT_STRAP
  181. bnez t1, 1f
  182. nop
  183. li t1, PLL_CPU_CONFIG_VAL_25M
  184. b 2f
  185. nop
  186. 1:
  187. li t1, PLL_CPU_CONFIG_VAL_40M
  188. 2:
  189. sw t1, AR933X_PLL_CPU_CONFIG_REG(t0)
  190. nop
  191. /* Wait for PLL update -> bit 31 in CPU_PLL_CONFIG should be 0 */
  192. 1:
  193. lw t1, AR933X_PLL_CPU_CONFIG_REG(t0)
  194. li t2, 0x80000000
  195. and t1, t1, t2
  196. bnez t1, 1b
  197. nop
  198. /* Confirm DDR PLL lock */
  199. li t3, 100
  200. li t4, 0
  201. 2:
  202. addi t4, t4, 1
  203. bgt t4, t3, 0b
  204. nop
  205. li t3, 5
  206. 3:
  207. /* Clear do_meas */
  208. li t0, CKSEG1ADDR(AR933X_SRIF_BASE)
  209. lw t1, AR933X_SRIF_DDR_DPLL3_REG(t0)
  210. li t2, 0xBFFFFFFF
  211. and t1, t1, t2
  212. sw t1, AR933X_SRIF_DDR_DPLL3_REG(t0)
  213. nop
  214. li t2, 10
  215. 1:
  216. subu t2, t2, 1
  217. bnez t2, 1b
  218. nop
  219. /* Set do_meas */
  220. li t2, 0x40000000
  221. or t1, t1, t2
  222. sw t1, AR933X_SRIF_DDR_DPLL3_REG(t0)
  223. nop
  224. /* Check meas_done */
  225. 1:
  226. lw t1, AR933X_SRIF_DDR_DPLL4_REG(t0)
  227. andi t1, t1, 0x8
  228. beqz t1, 1b
  229. nop
  230. lw t1, AR933X_SRIF_DDR_DPLL3_REG(t0)
  231. li t2, 0x007FFFF8
  232. and t1, t1, t2
  233. srl t1, t1, 3
  234. li t2, 0x4000
  235. bgt t1, t2, 2b
  236. nop
  237. addi t3, t3, -1
  238. bnez t3, 3b
  239. nop
  240. /* clear PLL bypass (bit 2) in CPU CLOCK CONTROL register */
  241. li t0, CKSEG1ADDR(AR71XX_PLL_BASE)
  242. li t1, PLL_CLK_CONTROL_VAL
  243. sw t1, AR933X_PLL_CLK_CTRL_REG(t0)
  244. nop
  245. nop
  246. jr ra
  247. nop
  248. END(lowlevel_init)