lowlevel_init.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Low-level initialization for EP93xx
  3. *
  4. * Copyright (C) 2009 Matthias Kaehlcke <matthias@kaehlcke.net>
  5. * Copyright (C) 2013
  6. * Sergey Kostanabev <sergey.kostanbaev <at> fairwaves.ru>
  7. *
  8. * Copyright (C) 2006 Dominic Rath <Dominic.Rath@gmx.de>
  9. * Copyright (C) 2006 Cirrus Logic Inc.
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. #include <config.h>
  17. #include <asm/arch-ep93xx/ep93xx.h>
  18. /*
  19. /* Configure the SDRAM based on the supplied settings.
  20. *
  21. * Input: r0 - SDRAM DEVCFG register
  22. * r2 - configuration for SDRAM chips
  23. * Output: none
  24. * Modifies: r3, r4
  25. */
  26. ep93xx_sdram_config:
  27. /* Program the SDRAM device configuration register. */
  28. ldr r3, =SDRAM_BASE
  29. #ifdef CONFIG_EDB93XX_SDCS0
  30. str r0, [r3, #SDRAM_OFF_DEVCFG0]
  31. #endif
  32. #ifdef CONFIG_EDB93XX_SDCS1
  33. str r0, [r3, #SDRAM_OFF_DEVCFG1]
  34. #endif
  35. #ifdef CONFIG_EDB93XX_SDCS2
  36. str r0, [r3, #SDRAM_OFF_DEVCFG2]
  37. #endif
  38. #ifdef CONFIG_EDB93XX_SDCS3
  39. str r0, [r3, #SDRAM_OFF_DEVCFG3]
  40. #endif
  41. /* Set the Initialize and MRS bits (issue continuous NOP commands
  42. * (INIT & MRS set))
  43. */
  44. ldr r4, =(EP93XX_SDRAMCTRL_GLOBALCFG_INIT | \
  45. EP93XX_SDRAMCTRL_GLOBALCFG_MRS | \
  46. EP93XX_SDRAMCTRL_GLOBALCFG_CKE)
  47. str r4, [r3, #SDRAM_OFF_GLCONFIG]
  48. /* Delay for 200us. */
  49. mov r4, #0x3000
  50. delay1:
  51. subs r4, r4, #1
  52. bne delay1
  53. /* Clear the MRS bit to issue a precharge all. */
  54. ldr r4, =(EP93XX_SDRAMCTRL_GLOBALCFG_INIT | \
  55. EP93XX_SDRAMCTRL_GLOBALCFG_CKE)
  56. str r4, [r3, #SDRAM_OFF_GLCONFIG]
  57. /* Temporarily set the refresh timer to 0x10. Make it really low so
  58. * that refresh cycles are generated.
  59. */
  60. ldr r4, =0x10
  61. str r4, [r3, #SDRAM_OFF_REFRSHTIMR]
  62. /* Delay for at least 80 SDRAM clock cycles. */
  63. mov r4, #80
  64. delay2:
  65. subs r4, r4, #1
  66. bne delay2
  67. /* Set the refresh timer to the fastest required for any device
  68. * that might be used. Set 9.6 ms refresh time.
  69. */
  70. ldr r4, =0x01e0
  71. str r4, [r3, #SDRAM_OFF_REFRSHTIMR]
  72. /* Select mode register update mode. */
  73. ldr r4, =(EP93XX_SDRAMCTRL_GLOBALCFG_CKE | \
  74. EP93XX_SDRAMCTRL_GLOBALCFG_MRS)
  75. str r4, [r3, #SDRAM_OFF_GLCONFIG]
  76. /* Program the mode register on the SDRAM by performing fake read */
  77. ldr r4, [r2]
  78. /* Select normal operating mode. */
  79. ldr r4, =EP93XX_SDRAMCTRL_GLOBALCFG_CKE
  80. str r4, [r3, #SDRAM_OFF_GLCONFIG]
  81. /* Return to the caller. */
  82. mov pc, lr
  83. /*
  84. * Test to see if the SDRAM has been configured in a usable mode.
  85. *
  86. * Input: r0 - Test address of SDRAM
  87. * Output: r0 - 0 -- Test OK, -1 -- Failed
  88. * Modifies: r0-r5
  89. */
  90. ep93xx_sdram_test:
  91. /* Load the test patterns to be written to SDRAM. */
  92. ldr r1, =0xf00dface
  93. ldr r2, =0xdeadbeef
  94. ldr r3, =0x08675309
  95. ldr r4, =0xdeafc0ed
  96. /* Store the test patterns to SDRAM. */
  97. stmia r0, {r1-r4}
  98. /* Load the test patterns from SDRAM one at a time and compare them
  99. * to the actual pattern.
  100. */
  101. ldr r5, [r0]
  102. cmp r5, r1
  103. ldreq r5, [r0, #0x0004]
  104. cmpeq r5, r2
  105. ldreq r5, [r0, #0x0008]
  106. cmpeq r5, r3
  107. ldreq r5, [r0, #0x000c]
  108. cmpeq r5, r4
  109. /* Return -1 if a mismatch was encountered, 0 otherwise. */
  110. mvnne r0, #0xffffffff
  111. moveq r0, #0x00000000
  112. /* Return to the caller. */
  113. mov pc, lr
  114. /*
  115. * Determine the size of the SDRAM. Use data=address for the scan.
  116. *
  117. * Input: r0 - Start SDRAM address
  118. * Return: r0 - Single block size
  119. * r1 - Valid block mask
  120. * r2 - Total block count
  121. * Modifies: r0-r5
  122. */
  123. ep93xx_sdram_size:
  124. /* Store zero at offset zero. */
  125. str r0, [r0]
  126. /* Start checking for an alias at 1MB into SDRAM. */
  127. ldr r1, =0x00100000
  128. /* Store the offset at the current offset. */
  129. check_block_size:
  130. str r1, [r0, r1]
  131. /* Read back from zero. */
  132. ldr r2, [r0]
  133. /* Stop searching of an alias was found. */
  134. cmp r1, r2
  135. beq found_block_size
  136. /* Advance to the next power of two boundary. */
  137. mov r1, r1, lsl #1
  138. /* Loop back if the size has not reached 256MB. */
  139. cmp r1, #0x10000000
  140. bne check_block_size
  141. /* A full 256MB of memory was found, so return it now. */
  142. ldr r0, =0x10000000
  143. ldr r1, =0x00000000
  144. ldr r2, =0x00000001
  145. mov pc, lr
  146. /* An alias was found. See if the first block is 128MB in size. */
  147. found_block_size:
  148. cmp r1, #0x08000000
  149. /* The first block is 128MB, so there is no further memory. Return it
  150. * now.
  151. */
  152. ldreq r0, =0x08000000
  153. ldreq r1, =0x00000000
  154. ldreq r2, =0x00000001
  155. moveq pc, lr
  156. /* Save the block size, set the block address bits to zero, and
  157. * initialize the block count to one.
  158. */
  159. mov r3, r1
  160. ldr r4, =0x00000000
  161. ldr r5, =0x00000001
  162. /* Look for additional blocks of memory by searching for non-aliases. */
  163. find_blocks:
  164. /* Store zero back to address zero. It may be overwritten. */
  165. str r0, [r0]
  166. /* Advance to the next power of two boundary. */
  167. mov r1, r1, lsl #1
  168. /* Store the offset at the current offset. */
  169. str r1, [r0, r1]
  170. /* Read back from zero. */
  171. ldr r2, [r0]
  172. /* See if a non-alias was found. */
  173. cmp r1, r2
  174. /* If a non-alias was found, then or in the block address bit and
  175. * multiply the block count by two (since there are two unique
  176. * blocks, one with this bit zero and one with it one).
  177. */
  178. orrne r4, r4, r1
  179. movne r5, r5, lsl #1
  180. /* Continue searching if there are more address bits to check. */
  181. cmp r1, #0x08000000
  182. bne find_blocks
  183. /* Return the block size, address mask, and count. */
  184. mov r0, r3
  185. mov r1, r4
  186. mov r2, r5
  187. /* Return to the caller. */
  188. mov pc, lr
  189. .globl lowlevel_init
  190. lowlevel_init:
  191. mov r6, lr
  192. /* Make sure caches are off and invalidated. */
  193. ldr r0, =0x00000000
  194. mcr p15, 0, r0, c1, c0, 0
  195. nop
  196. nop
  197. nop
  198. nop
  199. nop
  200. /* Turn off the green LED and turn on the red LED. If the red LED
  201. * is left on for too long, the external reset circuit described
  202. * by application note AN258 will cause the system to reset.
  203. */
  204. ldr r1, =EP93XX_LED_DATA
  205. ldr r0, [r1]
  206. bic r0, r0, #EP93XX_LED_GREEN_ON
  207. orr r0, r0, #EP93XX_LED_RED_ON
  208. str r0, [r1]
  209. /* Undo the silly static memory controller programming performed
  210. * by the boot rom.
  211. */
  212. ldr r0, =SMC_BASE
  213. /* Set WST1 and WST2 to 31 HCLK cycles (slowest access) */
  214. ldr r1, =0x0000fbe0
  215. /* Reset EP93XX_OFF_SMCBCR0 */
  216. ldr r2, [r0]
  217. orr r2, r2, r1
  218. str r2, [r0]
  219. ldr r2, [r0, #EP93XX_OFF_SMCBCR1]
  220. orr r2, r2, r1
  221. str r2, [r0, #EP93XX_OFF_SMCBCR1]
  222. ldr r2, [r0, #EP93XX_OFF_SMCBCR2]
  223. orr r2, r2, r1
  224. str r2, [r0, #EP93XX_OFF_SMCBCR2]
  225. ldr r2, [r0, #EP93XX_OFF_SMCBCR3]
  226. orr r2, r2, r1
  227. str r2, [r0, #EP93XX_OFF_SMCBCR3]
  228. ldr r2, [r0, #EP93XX_OFF_SMCBCR6]
  229. orr r2, r2, r1
  230. str r2, [r0, #EP93XX_OFF_SMCBCR6]
  231. ldr r2, [r0, #EP93XX_OFF_SMCBCR7]
  232. orr r2, r2, r1
  233. str r2, [r0, #EP93XX_OFF_SMCBCR7]
  234. /* Set the PLL1 and processor clock. */
  235. ldr r0, =SYSCON_BASE
  236. #ifdef CONFIG_EDB9301
  237. /* 332MHz, giving a 166MHz processor clock. */
  238. ldr r1, = 0x02b49907
  239. #else
  240. #ifdef CONFIG_EDB93XX_INDUSTRIAL
  241. /* 384MHz, giving a 196MHz processor clock. */
  242. ldr r1, =0x02a4bb38
  243. #else
  244. /* 400MHz, giving a 200MHz processor clock. */
  245. ldr r1, =0x02a4e39e
  246. #endif
  247. #endif
  248. str r1, [r0, #SYSCON_OFF_CLKSET1]
  249. nop
  250. nop
  251. nop
  252. nop
  253. nop
  254. /* Need to make sure that SDRAM is configured correctly before
  255. * coping the code into it.
  256. */
  257. #ifdef CONFIG_EDB93XX_SDCS0
  258. mov r11, #SDRAM_DEVCFG0_BASE
  259. #endif
  260. #ifdef CONFIG_EDB93XX_SDCS1
  261. mov r11, #SDRAM_DEVCFG1_BASE
  262. #endif
  263. #ifdef CONFIG_EDB93XX_SDCS2
  264. mov r11, #SDRAM_DEVCFG2_BASE
  265. #endif
  266. #ifdef CONFIG_EDB93XX_SDCS3
  267. ldr r0, =SYSCON_BASE
  268. ldr r0, [r0, #SYSCON_OFF_SYSCFG]
  269. ands r0, r0, #SYSCON_SYSCFG_LASDO
  270. moveq r11, #SDRAM_DEVCFG3_ASD0_BASE
  271. movne r11, #SDRAM_DEVCFG3_ASD1_BASE
  272. #endif
  273. /* See Table 13-5 in EP93xx datasheet for more info about DRAM
  274. * register mapping */
  275. /* Try a 32-bit wide configuration of SDRAM. */
  276. ldr r0, =(EP93XX_SDRAMCTRL_DEVCFG_BANKCOUNT | \
  277. EP93XX_SDRAMCTRL_DEVCFG_SROMLL | \
  278. EP93XX_SDRAMCTRL_DEVCFG_CASLAT_2 | \
  279. EP93XX_SDRAMCTRL_DEVCFG_RASTOCAS_2)
  280. /* Set burst count: 4 and CAS: 2
  281. * Burst mode [A11:A10]; CAS [A16:A14]
  282. */
  283. orr r2, r11, #0x00008800
  284. bl ep93xx_sdram_config
  285. /* Test the SDRAM. */
  286. mov r0, r11
  287. bl ep93xx_sdram_test
  288. cmp r0, #0x00000000
  289. beq ep93xx_sdram_done
  290. /* Try a 16-bit wide configuration of SDRAM. */
  291. ldr r0, =(EP93XX_SDRAMCTRL_DEVCFG_BANKCOUNT | \
  292. EP93XX_SDRAMCTRL_DEVCFG_SROMLL | \
  293. EP93XX_SDRAMCTRL_DEVCFG_CASLAT_2 | \
  294. EP93XX_SDRAMCTRL_DEVCFG_RASTOCAS_2 | \
  295. EP93XX_SDRAMCTRL_DEVCFG_EXTBUSWIDTH)
  296. /* Set burst count: 8, CAS: 2, sequential burst
  297. * Accoring to Table 13-3 for 16bit operations mapping must be shifted.
  298. * Burst mode [A10:A9]; CAS [A15:A13]
  299. */
  300. orr r2, r11, #0x00004600
  301. bl ep93xx_sdram_config
  302. /* Test the SDRAM. */
  303. mov r0, r11
  304. bl ep93xx_sdram_test
  305. cmp r0, #0x00000000
  306. beq ep93xx_sdram_done
  307. /* Turn off the red LED. */
  308. ldr r0, =EP93XX_LED_DATA
  309. ldr r1, [r0]
  310. bic r1, r1, #EP93XX_LED_RED_ON
  311. str r1, [r0]
  312. /* There is no SDRAM so flash the green LED. */
  313. flash_green:
  314. orr r1, r1, #EP93XX_LED_GREEN_ON
  315. str r1, [r0]
  316. ldr r2, =0x00010000
  317. flash_green_delay_1:
  318. subs r2, r2, #1
  319. bne flash_green_delay_1
  320. bic r1, r1, #EP93XX_LED_GREEN_ON
  321. str r1, [r0]
  322. ldr r2, =0x00010000
  323. flash_green_delay_2:
  324. subs r2, r2, #1
  325. bne flash_green_delay_2
  326. orr r1, r1, #EP93XX_LED_GREEN_ON
  327. str r1, [r0]
  328. ldr r2, =0x00010000
  329. flash_green_delay_3:
  330. subs r2, r2, #1
  331. bne flash_green_delay_3
  332. bic r1, r1, #EP93XX_LED_GREEN_ON
  333. str r1, [r0]
  334. ldr r2, =0x00050000
  335. flash_green_delay_4:
  336. subs r2, r2, #1
  337. bne flash_green_delay_4
  338. b flash_green
  339. ep93xx_sdram_done:
  340. ldr r1, =EP93XX_LED_DATA
  341. ldr r0, [r1]
  342. bic r0, r0, #EP93XX_LED_RED_ON
  343. str r0, [r1]
  344. /* Determine the size of the SDRAM. */
  345. mov r0, r11
  346. bl ep93xx_sdram_size
  347. /* Save the SDRAM characteristics. */
  348. mov r8, r0
  349. mov r9, r1
  350. mov r10, r2
  351. /* Compute total memory size into r1 */
  352. mul r1, r8, r10
  353. #ifdef CONFIG_EDB93XX_SDCS0
  354. ldr r2, [r0, #SDRAM_OFF_DEVCFG0]
  355. #endif
  356. #ifdef CONFIG_EDB93XX_SDCS1
  357. ldr r2, [r0, #SDRAM_OFF_DEVCFG1]
  358. #endif
  359. #ifdef CONFIG_EDB93XX_SDCS2
  360. ldr r2, [r0, #SDRAM_OFF_DEVCFG2]
  361. #endif
  362. #ifdef CONFIG_EDB93XX_SDCS3
  363. ldr r2, [r0, #SDRAM_OFF_DEVCFG3]
  364. #endif
  365. /* Consider small DRAM size as:
  366. * < 32Mb for 32bit bus
  367. * < 64Mb for 16bit bus
  368. */
  369. tst r2, #EP93XX_SDRAMCTRL_DEVCFG_EXTBUSWIDTH
  370. moveq r1, r1, lsr #1
  371. cmp r1, #0x02000000
  372. #if defined(CONFIG_EDB9301)
  373. /* Set refresh counter to 20ms for small DRAM size, otherwise 9.6ms */
  374. movlt r1, #0x03f0
  375. movge r1, #0x01e0
  376. #else
  377. /* Set refresh counter to 30.7ms for small DRAM size, otherwise 15ms */
  378. movlt r1, #0x0600
  379. movge r1, #0x2f0
  380. #endif
  381. str r1, [r0, #SDRAM_OFF_REFRSHTIMR]
  382. /* Save the memory configuration information. */
  383. orr r0, r11, #UBOOT_MEMORYCNF_BANK_SIZE
  384. stmia r0, {r8-r11}
  385. mov lr, r6
  386. mov pc, lr