initcode.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 uint32_t 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. uint32_t old_baud = serial_early_get_baud();
  59. if (BFIN_DEBUG_EARLY_SERIAL) {
  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 (!old_baud) {
  65. old_baud = CONFIG_BAUDRATE;
  66. serial_early_set_baud(CONFIG_BAUDRATE);
  67. }
  68. }
  69. return old_baud;
  70. }
  71. __attribute__((always_inline))
  72. static inline void serial_deinit(void)
  73. {
  74. #ifdef __ADSPBF54x__
  75. if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  76. /* clear forced RTS rather than relying on auto RTS */
  77. bfin_write_UART1_MCR(bfin_read_UART1_MCR() & ~FCPOL);
  78. }
  79. #endif
  80. }
  81. /* We need to reset the baud rate when we have early debug turned on
  82. * or when we are booting over the UART.
  83. * XXX: we should fix this to calc the old baud and restore it rather
  84. * than hardcoding it via CONFIG_LDR_LOAD_BAUD ... but we have
  85. * to figure out how to avoid the division in the baud calc ...
  86. */
  87. __attribute__((always_inline))
  88. static inline void serial_reset_baud(uint32_t baud)
  89. {
  90. if (!BFIN_DEBUG_EARLY_SERIAL && CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_UART)
  91. return;
  92. #ifndef CONFIG_LDR_LOAD_BAUD
  93. # define CONFIG_LDR_LOAD_BAUD 115200
  94. #endif
  95. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_BYPASS)
  96. serial_early_set_baud(baud);
  97. else if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART)
  98. serial_early_set_baud(CONFIG_LDR_LOAD_BAUD);
  99. else
  100. serial_early_set_baud(CONFIG_BAUDRATE);
  101. }
  102. __attribute__((always_inline))
  103. static inline void serial_putc(char c)
  104. {
  105. if (!BFIN_DEBUG_EARLY_SERIAL)
  106. return;
  107. if (c == '\n')
  108. *pUART_THR = '\r';
  109. *pUART_THR = c;
  110. while (!(*pUART_LSR & TEMT))
  111. continue;
  112. }
  113. /* Max SCLK can be 133MHz ... dividing that by 4 gives
  114. * us a freq of 33MHz for SPI which should generally be
  115. * slow enough for the slow reads the bootrom uses.
  116. */
  117. #ifndef CONFIG_SPI_BAUD_INITBLOCK
  118. # define CONFIG_SPI_BAUD_INITBLOCK 4
  119. #endif
  120. /* PLL_DIV defines */
  121. #ifndef CONFIG_PLL_DIV_VAL
  122. # if (CONFIG_CCLK_DIV == 1)
  123. # define CONFIG_CCLK_ACT_DIV CCLK_DIV1
  124. # elif (CONFIG_CCLK_DIV == 2)
  125. # define CONFIG_CCLK_ACT_DIV CCLK_DIV2
  126. # elif (CONFIG_CCLK_DIV == 4)
  127. # define CONFIG_CCLK_ACT_DIV CCLK_DIV4
  128. # elif (CONFIG_CCLK_DIV == 8)
  129. # define CONFIG_CCLK_ACT_DIV CCLK_DIV8
  130. # else
  131. # define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
  132. # endif
  133. # define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
  134. #endif
  135. #ifndef CONFIG_PLL_LOCKCNT_VAL
  136. # define CONFIG_PLL_LOCKCNT_VAL 0x0300
  137. #endif
  138. #ifndef CONFIG_PLL_CTL_VAL
  139. # define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9))
  140. #endif
  141. #ifndef CONFIG_EBIU_RSTCTL_VAL
  142. # define CONFIG_EBIU_RSTCTL_VAL 0 /* only MDDRENABLE is useful */
  143. #endif
  144. #ifndef CONFIG_EBIU_MBSCTL_VAL
  145. # define CONFIG_EBIU_MBSCTL_VAL 0
  146. #endif
  147. /* Make sure our voltage value is sane so we don't blow up! */
  148. #ifndef CONFIG_VR_CTL_VAL
  149. # define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
  150. # if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
  151. # define CCLK_VLEV_120 400000000
  152. # define CCLK_VLEV_125 533000000
  153. # elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  154. # define CCLK_VLEV_120 401000000
  155. # define CCLK_VLEV_125 401000000
  156. # elif defined(__ADSPBF561__)
  157. # define CCLK_VLEV_120 300000000
  158. # define CCLK_VLEV_125 501000000
  159. # endif
  160. # if BFIN_CCLK < CCLK_VLEV_120
  161. # define CONFIG_VR_CTL_VLEV VLEV_120
  162. # elif BFIN_CCLK < CCLK_VLEV_125
  163. # define CONFIG_VR_CTL_VLEV VLEV_125
  164. # else
  165. # define CONFIG_VR_CTL_VLEV VLEV_130
  166. # endif
  167. # if defined(__ADSPBF52x__) /* TBD; use default */
  168. # undef CONFIG_VR_CTL_VLEV
  169. # define CONFIG_VR_CTL_VLEV VLEV_110
  170. # elif defined(__ADSPBF54x__) /* TBD; use default */
  171. # undef CONFIG_VR_CTL_VLEV
  172. # define CONFIG_VR_CTL_VLEV VLEV_120
  173. # endif
  174. # ifdef CONFIG_BFIN_MAC
  175. # define CONFIG_VR_CTL_CLKBUF CLKBUFOE
  176. # else
  177. # define CONFIG_VR_CTL_CLKBUF 0
  178. # endif
  179. # if defined(__ADSPBF52x__)
  180. # define CONFIG_VR_CTL_FREQ FREQ_1000
  181. # else
  182. # define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
  183. # endif
  184. # define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
  185. #endif
  186. __attribute__((saveall))
  187. void initcode(ADI_BOOT_DATA *bootstruct)
  188. {
  189. uint32_t old_baud = serial_init();
  190. #ifdef CONFIG_HW_WATCHDOG
  191. # ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
  192. # define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
  193. # endif
  194. /* Program the watchdog with an initial timeout of ~20 seconds.
  195. * Hopefully that should be long enough to load the u-boot LDR
  196. * (from wherever) and then the common u-boot code can take over.
  197. * In bypass mode, the start.S would have already set a much lower
  198. * timeout, so don't clobber that.
  199. */
  200. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
  201. bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
  202. bfin_write_WDOG_CTL(0);
  203. }
  204. #endif
  205. serial_putc('S');
  206. /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
  207. * fast read, so we need to slow down the SPI clock a lot more during
  208. * boot. Once we switch over to u-boot's SPI flash driver, we'll
  209. * increase the speed appropriately.
  210. */
  211. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER)
  212. #ifdef SPI0_BAUD
  213. bfin_write_SPI0_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
  214. #else
  215. bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
  216. #endif
  217. serial_putc('B');
  218. /* Disable all peripheral wakeups except for the PLL event. */
  219. #ifdef SIC_IWR0
  220. bfin_write_SIC_IWR0(1);
  221. bfin_write_SIC_IWR1(0);
  222. # ifdef SIC_IWR2
  223. bfin_write_SIC_IWR2(0);
  224. # endif
  225. #elif defined(SICA_IWR0)
  226. bfin_write_SICA_IWR0(1);
  227. bfin_write_SICA_IWR1(0);
  228. #else
  229. bfin_write_SIC_IWR(1);
  230. #endif
  231. serial_putc('L');
  232. bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
  233. serial_putc('A');
  234. /* Only reprogram when needed to avoid triggering unnecessary
  235. * PLL relock sequences.
  236. */
  237. if (bfin_read_VR_CTL() != CONFIG_VR_CTL_VAL) {
  238. serial_putc('!');
  239. bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
  240. asm("idle;");
  241. }
  242. serial_putc('C');
  243. bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
  244. serial_putc('K');
  245. /* Only reprogram when needed to avoid triggering unnecessary
  246. * PLL relock sequences.
  247. */
  248. if (bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
  249. serial_putc('!');
  250. bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
  251. asm("idle;");
  252. }
  253. /* Since we've changed the SCLK above, we may need to update
  254. * the UART divisors (UART baud rates are based on SCLK).
  255. */
  256. serial_reset_baud(old_baud);
  257. serial_putc('F');
  258. /* Program the async banks controller. */
  259. bfin_write_EBIU_AMBCTL0(CONFIG_EBIU_AMBCTL0_VAL);
  260. bfin_write_EBIU_AMBCTL1(CONFIG_EBIU_AMBCTL1_VAL);
  261. bfin_write_EBIU_AMGCTL(CONFIG_EBIU_AMGCTL_VAL);
  262. #ifdef EBIU_MODE
  263. /* Not all parts have these additional MMRs. */
  264. bfin_write_EBIU_MBSCTL(CONFIG_EBIU_MBSCTL_VAL);
  265. bfin_write_EBIU_MODE(CONFIG_EBIU_MODE_VAL);
  266. bfin_write_EBIU_FCTL(CONFIG_EBIU_FCTL_VAL);
  267. #endif
  268. serial_putc('I');
  269. /* Program the external memory controller. */
  270. #ifdef EBIU_RSTCTL
  271. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
  272. bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
  273. bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
  274. bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
  275. # ifdef CONFIG_EBIU_DDRCTL3_VAL
  276. /* default is disable, so don't need to force this */
  277. bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
  278. # endif
  279. #else
  280. bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
  281. bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
  282. bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
  283. #endif
  284. serial_putc('N');
  285. /* Restore all peripheral wakeups. */
  286. #ifdef SIC_IWR0
  287. bfin_write_SIC_IWR0(-1);
  288. bfin_write_SIC_IWR1(-1);
  289. # ifdef SIC_IWR2
  290. bfin_write_SIC_IWR2(-1);
  291. # endif
  292. #elif defined(SICA_IWR0)
  293. bfin_write_SICA_IWR0(-1);
  294. bfin_write_SICA_IWR1(-1);
  295. #else
  296. bfin_write_SIC_IWR(-1);
  297. #endif
  298. serial_putc('>');
  299. serial_putc('\n');
  300. serial_deinit();
  301. }