initcode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * initcode.c - Initialize the processor. This is usually entails things
  3. * like external memory, voltage regulators, etc... Note that this file
  4. * cannot make any function calls as it may be executed all by itself by
  5. * the Blackfin's bootrom in LDR format.
  6. *
  7. * Copyright (c) 2004-2008 Analog Devices Inc.
  8. *
  9. * Licensed under the GPL-2 or later.
  10. */
  11. #include <config.h>
  12. #include <asm/blackfin.h>
  13. #include <asm/mach-common/bits/bootrom.h>
  14. #include <asm/mach-common/bits/ebiu.h>
  15. #include <asm/mach-common/bits/pll.h>
  16. #include <asm/mach-common/bits/uart.h>
  17. #define BFIN_IN_INITCODE
  18. #include "serial.h"
  19. __attribute__((always_inline))
  20. static inline void serial_init(void)
  21. {
  22. #ifdef __ADSPBF54x__
  23. # ifdef BFIN_BOOT_UART_USE_RTS
  24. # define BFIN_UART_USE_RTS 1
  25. # else
  26. # define BFIN_UART_USE_RTS 0
  27. # endif
  28. if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  29. size_t i;
  30. /* force RTS rather than relying on auto RTS */
  31. bfin_write_UART1_MCR(bfin_read_UART1_MCR() | FCPOL);
  32. /* Wait for the line to clear up. We cannot rely on UART
  33. * registers as none of them reflect the status of the RSR.
  34. * Instead, we'll sleep for ~10 bit times at 9600 baud.
  35. * We can precalc things here by assuming boot values for
  36. * PLL rather than loading registers and calculating.
  37. * baud = SCLK / (16 ^ (1 - EDBO) * Divisor)
  38. * EDB0 = 0
  39. * Divisor = (SCLK / baud) / 16
  40. * SCLK = baud * 16 * Divisor
  41. * SCLK = (0x14 * CONFIG_CLKIN_HZ) / 5
  42. * CCLK = (16 * Divisor * 5) * (9600 / 10)
  43. * In reality, this will probably be just about 1 second delay,
  44. * so assuming 9600 baud is OK (both as a very low and too high
  45. * speed as this will buffer things enough).
  46. */
  47. #define _NUMBITS (10) /* how many bits to delay */
  48. #define _LOWBAUD (9600) /* low baud rate */
  49. #define _SCLK ((0x14 * CONFIG_CLKIN_HZ) / 5) /* SCLK based on PLL */
  50. #define _DIVISOR ((_SCLK / _LOWBAUD) / 16) /* UART DLL/DLH */
  51. #define _NUMINS (3) /* how many instructions in loop */
  52. #define _CCLK (((16 * _DIVISOR * 5) * (_LOWBAUD / _NUMBITS)) / _NUMINS)
  53. i = _CCLK;
  54. while (i--)
  55. asm volatile("" : : : "memory");
  56. }
  57. #endif
  58. if (BFIN_DEBUG_EARLY_SERIAL) {
  59. int ucen = *pUART_GCTL & UCEN;
  60. serial_early_init();
  61. /* If the UART is off, that means we need to program
  62. * the baud rate ourselves initially.
  63. */
  64. if (ucen != UCEN)
  65. serial_early_set_baud(CONFIG_BAUDRATE);
  66. }
  67. }
  68. __attribute__((always_inline))
  69. static inline void serial_deinit(void)
  70. {
  71. #ifdef __ADSPBF54x__
  72. if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  73. /* clear forced RTS rather than relying on auto RTS */
  74. bfin_write_UART1_MCR(bfin_read_UART1_MCR() & ~FCPOL);
  75. }
  76. #endif
  77. }
  78. __attribute__((always_inline))
  79. static inline void serial_putc(char c)
  80. {
  81. if (!BFIN_DEBUG_EARLY_SERIAL)
  82. return;
  83. if (c == '\n')
  84. *pUART_THR = '\r';
  85. *pUART_THR = c;
  86. while (!(*pUART_LSR & TEMT))
  87. continue;
  88. }
  89. /* Max SCLK can be 133MHz ... dividing that by (2*4) gives
  90. * us a freq of 16MHz for SPI which should generally be
  91. * slow enough for the slow reads the bootrom uses.
  92. */
  93. #if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
  94. ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
  95. (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
  96. # define BOOTROM_SUPPORTS_SPI_FAST_READ 1
  97. #else
  98. # define BOOTROM_SUPPORTS_SPI_FAST_READ 0
  99. #endif
  100. #ifndef CONFIG_SPI_BAUD_INITBLOCK
  101. # define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
  102. #endif
  103. #ifdef SPI0_BAUD
  104. # define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
  105. #endif
  106. /* PLL_DIV defines */
  107. #ifndef CONFIG_PLL_DIV_VAL
  108. # if (CONFIG_CCLK_DIV == 1)
  109. # define CONFIG_CCLK_ACT_DIV CCLK_DIV1
  110. # elif (CONFIG_CCLK_DIV == 2)
  111. # define CONFIG_CCLK_ACT_DIV CCLK_DIV2
  112. # elif (CONFIG_CCLK_DIV == 4)
  113. # define CONFIG_CCLK_ACT_DIV CCLK_DIV4
  114. # elif (CONFIG_CCLK_DIV == 8)
  115. # define CONFIG_CCLK_ACT_DIV CCLK_DIV8
  116. # else
  117. # define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
  118. # endif
  119. # define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
  120. #endif
  121. #ifndef CONFIG_PLL_LOCKCNT_VAL
  122. # define CONFIG_PLL_LOCKCNT_VAL 0x0300
  123. #endif
  124. #ifndef CONFIG_PLL_CTL_VAL
  125. # define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
  126. #endif
  127. #ifndef CONFIG_EBIU_RSTCTL_VAL
  128. # define CONFIG_EBIU_RSTCTL_VAL 0 /* only MDDRENABLE is useful */
  129. #endif
  130. #if ((CONFIG_EBIU_RSTCTL_VAL & 0xFFFFFFC4) != 0)
  131. # error invalid EBIU_RSTCTL value: must not set reserved bits
  132. #endif
  133. #ifndef CONFIG_EBIU_MBSCTL_VAL
  134. # define CONFIG_EBIU_MBSCTL_VAL 0
  135. #endif
  136. #if defined(CONFIG_EBIU_DDRQUE_VAL) && ((CONFIG_EBIU_DDRQUE_VAL & 0xFFFF8000) != 0)
  137. # error invalid EBIU_DDRQUE value: must not set reserved bits
  138. #endif
  139. /* Make sure our voltage value is sane so we don't blow up! */
  140. #ifndef CONFIG_VR_CTL_VAL
  141. # define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
  142. # if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
  143. # define CCLK_VLEV_120 400000000
  144. # define CCLK_VLEV_125 533000000
  145. # elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  146. # define CCLK_VLEV_120 401000000
  147. # define CCLK_VLEV_125 401000000
  148. # elif defined(__ADSPBF561__)
  149. # define CCLK_VLEV_120 300000000
  150. # define CCLK_VLEV_125 501000000
  151. # endif
  152. # if BFIN_CCLK < CCLK_VLEV_120
  153. # define CONFIG_VR_CTL_VLEV VLEV_120
  154. # elif BFIN_CCLK < CCLK_VLEV_125
  155. # define CONFIG_VR_CTL_VLEV VLEV_125
  156. # else
  157. # define CONFIG_VR_CTL_VLEV VLEV_130
  158. # endif
  159. # if defined(__ADSPBF52x__) /* TBD; use default */
  160. # undef CONFIG_VR_CTL_VLEV
  161. # define CONFIG_VR_CTL_VLEV VLEV_110
  162. # elif defined(__ADSPBF54x__) /* TBD; use default */
  163. # undef CONFIG_VR_CTL_VLEV
  164. # define CONFIG_VR_CTL_VLEV VLEV_120
  165. # elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
  166. # undef CONFIG_VR_CTL_VLEV
  167. # define CONFIG_VR_CTL_VLEV VLEV_125
  168. # endif
  169. # ifdef CONFIG_BFIN_MAC
  170. # define CONFIG_VR_CTL_CLKBUF CLKBUFOE
  171. # else
  172. # define CONFIG_VR_CTL_CLKBUF 0
  173. # endif
  174. # if defined(__ADSPBF52x__)
  175. # define CONFIG_VR_CTL_FREQ FREQ_1000
  176. # else
  177. # define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
  178. # endif
  179. # define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
  180. #endif
  181. #ifndef EBIU_RSTCTL
  182. /* Blackfin with SDRAM */
  183. #ifndef CONFIG_EBIU_SDBCTL_VAL
  184. # if CONFIG_MEM_SIZE == 16
  185. # define CONFIG_EBSZ_VAL EBSZ_16
  186. # elif CONFIG_MEM_SIZE == 32
  187. # define CONFIG_EBSZ_VAL EBSZ_32
  188. # elif CONFIG_MEM_SIZE == 64
  189. # define CONFIG_EBSZ_VAL EBSZ_64
  190. # elif CONFIG_MEM_SIZE == 128
  191. # define CONFIG_EBSZ_VAL EBSZ_128
  192. # elif CONFIG_MEM_SIZE == 256
  193. # define CONFIG_EBSZ_VAL EBSZ_256
  194. # elif CONFIG_MEM_SIZE == 512
  195. # define CONFIG_EBSZ_VAL EBSZ_512
  196. # else
  197. # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
  198. # endif
  199. # if CONFIG_MEM_ADD_WDTH == 8
  200. # define CONFIG_EBCAW_VAL EBCAW_8
  201. # elif CONFIG_MEM_ADD_WDTH == 9
  202. # define CONFIG_EBCAW_VAL EBCAW_9
  203. # elif CONFIG_MEM_ADD_WDTH == 10
  204. # define CONFIG_EBCAW_VAL EBCAW_10
  205. # elif CONFIG_MEM_ADD_WDTH == 11
  206. # define CONFIG_EBCAW_VAL EBCAW_11
  207. # else
  208. # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
  209. # endif
  210. # define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
  211. #endif
  212. #endif
  213. BOOTROM_CALLED_FUNC_ATTR
  214. void initcode(ADI_BOOT_DATA *bootstruct)
  215. {
  216. /* Save the clock pieces that are used in baud rate calculation */
  217. unsigned int sdivB, divB, vcoB;
  218. serial_init();
  219. if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  220. sdivB = bfin_read_PLL_DIV() & 0xf;
  221. vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
  222. divB = serial_early_get_div();
  223. }
  224. #ifdef CONFIG_HW_WATCHDOG
  225. # ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
  226. # define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
  227. # endif
  228. /* Program the watchdog with an initial timeout of ~20 seconds.
  229. * Hopefully that should be long enough to load the u-boot LDR
  230. * (from wherever) and then the common u-boot code can take over.
  231. * In bypass mode, the start.S would have already set a much lower
  232. * timeout, so don't clobber that.
  233. */
  234. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
  235. bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
  236. bfin_write_WDOG_CTL(0);
  237. }
  238. #endif
  239. serial_putc('S');
  240. /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
  241. * fast read, so we need to slow down the SPI clock a lot more during
  242. * boot. Once we switch over to u-boot's SPI flash driver, we'll
  243. * increase the speed appropriately.
  244. */
  245. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
  246. if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
  247. bootstruct->dFlags |= BFLAG_FASTREAD;
  248. bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
  249. }
  250. serial_putc('B');
  251. /* Disable all peripheral wakeups except for the PLL event. */
  252. #ifdef SIC_IWR0
  253. bfin_write_SIC_IWR0(1);
  254. bfin_write_SIC_IWR1(0);
  255. # ifdef SIC_IWR2
  256. bfin_write_SIC_IWR2(0);
  257. # endif
  258. #elif defined(SICA_IWR0)
  259. bfin_write_SICA_IWR0(1);
  260. bfin_write_SICA_IWR1(0);
  261. #else
  262. bfin_write_SIC_IWR(1);
  263. #endif
  264. /* With newer bootroms, we use the helper function to set up
  265. * the memory controller. Older bootroms lacks such helpers
  266. * so we do it ourselves.
  267. */
  268. #define BOOTROM_CAPS_SYSCONTROL 0
  269. if (BOOTROM_CAPS_SYSCONTROL) {
  270. serial_putc('S');
  271. ADI_SYSCTRL_VALUES memory_settings;
  272. memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
  273. memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
  274. memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
  275. memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
  276. #if ANOMALY_05000432
  277. bfin_write_SIC_IWR1(0);
  278. #endif
  279. syscontrol(SYSCTRL_WRITE | SYSCTRL_VRCTL | SYSCTRL_PLLCTL | SYSCTRL_PLLDIV | SYSCTRL_LOCKCNT |
  280. (CONFIG_VR_CTL_VAL & FREQ_MASK ? SYSCTRL_INTVOLTAGE : SYSCTRL_EXTVOLTAGE), &memory_settings, NULL);
  281. #if ANOMALY_05000432
  282. bfin_write_SIC_IWR1(-1);
  283. #endif
  284. } else {
  285. serial_putc('L');
  286. bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
  287. serial_putc('A');
  288. /* Only reprogram when needed to avoid triggering unnecessary
  289. * PLL relock sequences.
  290. */
  291. if (bfin_read_VR_CTL() != CONFIG_VR_CTL_VAL) {
  292. serial_putc('!');
  293. bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
  294. asm("idle;");
  295. }
  296. serial_putc('C');
  297. bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
  298. serial_putc('K');
  299. /* Only reprogram when needed to avoid triggering unnecessary
  300. * PLL relock sequences.
  301. */
  302. if (bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
  303. serial_putc('!');
  304. bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
  305. asm("idle;");
  306. }
  307. }
  308. /* Since we've changed the SCLK above, we may need to update
  309. * the UART divisors (UART baud rates are based on SCLK).
  310. * Do the division by hand as there are no native instructions
  311. * for dividing which means we'd generate a libgcc reference.
  312. */
  313. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  314. unsigned int sdivR, vcoR;
  315. sdivR = bfin_read_PLL_DIV() & 0xf;
  316. vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
  317. int dividend = sdivB * divB * vcoR;
  318. int divisor = vcoB * sdivR;
  319. unsigned int quotient;
  320. for (quotient = 0; dividend > 0; ++quotient)
  321. dividend -= divisor;
  322. serial_early_put_div(quotient - ANOMALY_05000230);
  323. }
  324. serial_putc('F');
  325. /* Program the async banks controller. */
  326. bfin_write_EBIU_AMBCTL0(CONFIG_EBIU_AMBCTL0_VAL);
  327. bfin_write_EBIU_AMBCTL1(CONFIG_EBIU_AMBCTL1_VAL);
  328. bfin_write_EBIU_AMGCTL(CONFIG_EBIU_AMGCTL_VAL);
  329. #ifdef EBIU_MODE
  330. /* Not all parts have these additional MMRs. */
  331. bfin_write_EBIU_MBSCTL(CONFIG_EBIU_MBSCTL_VAL);
  332. bfin_write_EBIU_MODE(CONFIG_EBIU_MODE_VAL);
  333. bfin_write_EBIU_FCTL(CONFIG_EBIU_FCTL_VAL);
  334. #endif
  335. serial_putc('I');
  336. /* Program the external memory controller. */
  337. #ifdef EBIU_RSTCTL
  338. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
  339. bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
  340. bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
  341. bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
  342. # ifdef CONFIG_EBIU_DDRCTL3_VAL
  343. /* default is disable, so don't need to force this */
  344. bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
  345. # endif
  346. #else
  347. bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
  348. bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
  349. bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
  350. #endif
  351. serial_putc('N');
  352. /* Restore all peripheral wakeups. */
  353. #ifdef SIC_IWR0
  354. bfin_write_SIC_IWR0(-1);
  355. bfin_write_SIC_IWR1(-1);
  356. # ifdef SIC_IWR2
  357. bfin_write_SIC_IWR2(-1);
  358. # endif
  359. #elif defined(SICA_IWR0)
  360. bfin_write_SICA_IWR0(-1);
  361. bfin_write_SICA_IWR1(-1);
  362. #else
  363. bfin_write_SIC_IWR(-1);
  364. #endif
  365. /* tell the bootrom where our entry point is */
  366. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
  367. bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);
  368. serial_putc('>');
  369. serial_putc('\n');
  370. serial_deinit();
  371. }