initcode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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-2011 Analog Devices Inc.
  8. *
  9. * Licensed under the GPL-2 or later.
  10. */
  11. #define BFIN_IN_INITCODE
  12. #include <config.h>
  13. #include <asm/blackfin.h>
  14. #include <asm/mach-common/bits/bootrom.h>
  15. #include <asm/mach-common/bits/core.h>
  16. #include <asm/mach-common/bits/ebiu.h>
  17. #include <asm/mach-common/bits/pll.h>
  18. #include <asm/mach-common/bits/uart.h>
  19. #define BUG() while (1) { asm volatile("emuexcpt;"); }
  20. #include "serial.h"
  21. __attribute__((always_inline))
  22. static inline void serial_init(void)
  23. {
  24. uint32_t uart_base = UART_DLL;
  25. #ifdef __ADSPBF54x__
  26. # ifdef BFIN_BOOT_UART_USE_RTS
  27. # define BFIN_UART_USE_RTS 1
  28. # else
  29. # define BFIN_UART_USE_RTS 0
  30. # endif
  31. if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  32. size_t i;
  33. /* force RTS rather than relying on auto RTS */
  34. bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) | FCPOL);
  35. /* Wait for the line to clear up. We cannot rely on UART
  36. * registers as none of them reflect the status of the RSR.
  37. * Instead, we'll sleep for ~10 bit times at 9600 baud.
  38. * We can precalc things here by assuming boot values for
  39. * PLL rather than loading registers and calculating.
  40. * baud = SCLK / (16 ^ (1 - EDBO) * Divisor)
  41. * EDB0 = 0
  42. * Divisor = (SCLK / baud) / 16
  43. * SCLK = baud * 16 * Divisor
  44. * SCLK = (0x14 * CONFIG_CLKIN_HZ) / 5
  45. * CCLK = (16 * Divisor * 5) * (9600 / 10)
  46. * In reality, this will probably be just about 1 second delay,
  47. * so assuming 9600 baud is OK (both as a very low and too high
  48. * speed as this will buffer things enough).
  49. */
  50. #define _NUMBITS (10) /* how many bits to delay */
  51. #define _LOWBAUD (9600) /* low baud rate */
  52. #define _SCLK ((0x14 * CONFIG_CLKIN_HZ) / 5) /* SCLK based on PLL */
  53. #define _DIVISOR ((_SCLK / _LOWBAUD) / 16) /* UART DLL/DLH */
  54. #define _NUMINS (3) /* how many instructions in loop */
  55. #define _CCLK (((16 * _DIVISOR * 5) * (_LOWBAUD / _NUMBITS)) / _NUMINS)
  56. i = _CCLK;
  57. while (i--)
  58. asm volatile("" : : : "memory");
  59. }
  60. #endif
  61. if (BFIN_DEBUG_EARLY_SERIAL) {
  62. int ucen = bfin_read16(&pUART->gctl) & UCEN;
  63. serial_early_init(uart_base);
  64. /* If the UART is off, that means we need to program
  65. * the baud rate ourselves initially.
  66. */
  67. if (ucen != UCEN)
  68. serial_early_set_baud(uart_base, CONFIG_BAUDRATE);
  69. }
  70. }
  71. __attribute__((always_inline))
  72. static inline void serial_deinit(void)
  73. {
  74. #ifdef __ADSPBF54x__
  75. uint32_t uart_base = UART_DLL;
  76. if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  77. /* clear forced RTS rather than relying on auto RTS */
  78. bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) & ~FCPOL);
  79. }
  80. #endif
  81. }
  82. __attribute__((always_inline))
  83. static inline void serial_putc(char c)
  84. {
  85. uint32_t uart_base = UART_DLL;
  86. if (!BFIN_DEBUG_EARLY_SERIAL)
  87. return;
  88. if (c == '\n')
  89. serial_putc('\r');
  90. bfin_write16(&pUART->thr, c);
  91. while (!(bfin_read16(&pUART->lsr) & TEMT))
  92. continue;
  93. }
  94. #include "initcode.h"
  95. __attribute__((always_inline)) static inline void
  96. program_nmi_handler(void)
  97. {
  98. u32 tmp1, tmp2;
  99. /* Older bootroms don't create a dummy NMI handler,
  100. * so make one ourselves ASAP in case it fires.
  101. */
  102. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS && !ANOMALY_05000219)
  103. return;
  104. asm volatile (
  105. "%0 = RETS;" /* Save current RETS */
  106. "CALL 1f;" /* Figure out current PC */
  107. "RTN;" /* The simple NMI handler */
  108. "1:"
  109. "%1 = RETS;" /* Load addr of NMI handler */
  110. "RETS = %0;" /* Restore RETS */
  111. "[%2] = %1;" /* Write NMI handler */
  112. : "=r"(tmp1), "=r"(tmp2) : "ab"(EVT2)
  113. );
  114. }
  115. /* Max SCLK can be 133MHz ... dividing that by (2*4) gives
  116. * us a freq of 16MHz for SPI which should generally be
  117. * slow enough for the slow reads the bootrom uses.
  118. */
  119. #if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
  120. ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
  121. (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
  122. # define BOOTROM_SUPPORTS_SPI_FAST_READ 1
  123. #else
  124. # define BOOTROM_SUPPORTS_SPI_FAST_READ 0
  125. #endif
  126. #ifndef CONFIG_SPI_BAUD_INITBLOCK
  127. # define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
  128. #endif
  129. #ifdef SPI0_BAUD
  130. # define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
  131. #endif
  132. /* PLL_DIV defines */
  133. #ifndef CONFIG_PLL_DIV_VAL
  134. # if (CONFIG_CCLK_DIV == 1)
  135. # define CONFIG_CCLK_ACT_DIV CCLK_DIV1
  136. # elif (CONFIG_CCLK_DIV == 2)
  137. # define CONFIG_CCLK_ACT_DIV CCLK_DIV2
  138. # elif (CONFIG_CCLK_DIV == 4)
  139. # define CONFIG_CCLK_ACT_DIV CCLK_DIV4
  140. # elif (CONFIG_CCLK_DIV == 8)
  141. # define CONFIG_CCLK_ACT_DIV CCLK_DIV8
  142. # else
  143. # define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
  144. # endif
  145. # define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
  146. #endif
  147. #ifndef CONFIG_PLL_LOCKCNT_VAL
  148. # define CONFIG_PLL_LOCKCNT_VAL 0x0300
  149. #endif
  150. #ifndef CONFIG_PLL_CTL_VAL
  151. # define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
  152. #endif
  153. /* Make sure our voltage value is sane so we don't blow up! */
  154. #ifndef CONFIG_VR_CTL_VAL
  155. # define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
  156. # if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
  157. # define CCLK_VLEV_120 400000000
  158. # define CCLK_VLEV_125 533000000
  159. # elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  160. # define CCLK_VLEV_120 401000000
  161. # define CCLK_VLEV_125 401000000
  162. # elif defined(__ADSPBF561__)
  163. # define CCLK_VLEV_120 300000000
  164. # define CCLK_VLEV_125 501000000
  165. # endif
  166. # if BFIN_CCLK < CCLK_VLEV_120
  167. # define CONFIG_VR_CTL_VLEV VLEV_120
  168. # elif BFIN_CCLK < CCLK_VLEV_125
  169. # define CONFIG_VR_CTL_VLEV VLEV_125
  170. # else
  171. # define CONFIG_VR_CTL_VLEV VLEV_130
  172. # endif
  173. # if defined(__ADSPBF52x__) /* TBD; use default */
  174. # undef CONFIG_VR_CTL_VLEV
  175. # define CONFIG_VR_CTL_VLEV VLEV_110
  176. # elif defined(__ADSPBF54x__) /* TBD; use default */
  177. # undef CONFIG_VR_CTL_VLEV
  178. # define CONFIG_VR_CTL_VLEV VLEV_120
  179. # elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
  180. # undef CONFIG_VR_CTL_VLEV
  181. # define CONFIG_VR_CTL_VLEV VLEV_125
  182. # endif
  183. # ifdef CONFIG_BFIN_MAC
  184. # define CONFIG_VR_CTL_CLKBUF CLKBUFOE
  185. # else
  186. # define CONFIG_VR_CTL_CLKBUF 0
  187. # endif
  188. # if defined(__ADSPBF52x__)
  189. # define CONFIG_VR_CTL_FREQ FREQ_1000
  190. # else
  191. # define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
  192. # endif
  193. # define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
  194. #endif
  195. /* some parts do not have an on-chip voltage regulator */
  196. #if defined(__ADSPBF51x__)
  197. # define CONFIG_HAS_VR 0
  198. # undef CONFIG_VR_CTL_VAL
  199. # define CONFIG_VR_CTL_VAL 0
  200. #else
  201. # define CONFIG_HAS_VR 1
  202. #endif
  203. #if CONFIG_MEM_SIZE
  204. #ifndef EBIU_RSTCTL
  205. /* Blackfin with SDRAM */
  206. #ifndef CONFIG_EBIU_SDBCTL_VAL
  207. # if CONFIG_MEM_SIZE == 16
  208. # define CONFIG_EBSZ_VAL EBSZ_16
  209. # elif CONFIG_MEM_SIZE == 32
  210. # define CONFIG_EBSZ_VAL EBSZ_32
  211. # elif CONFIG_MEM_SIZE == 64
  212. # define CONFIG_EBSZ_VAL EBSZ_64
  213. # elif CONFIG_MEM_SIZE == 128
  214. # define CONFIG_EBSZ_VAL EBSZ_128
  215. # elif CONFIG_MEM_SIZE == 256
  216. # define CONFIG_EBSZ_VAL EBSZ_256
  217. # elif CONFIG_MEM_SIZE == 512
  218. # define CONFIG_EBSZ_VAL EBSZ_512
  219. # else
  220. # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
  221. # endif
  222. # if CONFIG_MEM_ADD_WDTH == 8
  223. # define CONFIG_EBCAW_VAL EBCAW_8
  224. # elif CONFIG_MEM_ADD_WDTH == 9
  225. # define CONFIG_EBCAW_VAL EBCAW_9
  226. # elif CONFIG_MEM_ADD_WDTH == 10
  227. # define CONFIG_EBCAW_VAL EBCAW_10
  228. # elif CONFIG_MEM_ADD_WDTH == 11
  229. # define CONFIG_EBCAW_VAL EBCAW_11
  230. # else
  231. # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
  232. # endif
  233. # define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
  234. #endif
  235. #endif
  236. #endif
  237. /* Conflicting Column Address Widths Causes SDRAM Errors:
  238. * EB2CAW and EB3CAW must be the same
  239. */
  240. #if ANOMALY_05000362
  241. # if ((CONFIG_EBIU_SDBCTL_VAL & 0x30000000) >> 8) != (CONFIG_EBIU_SDBCTL_VAL & 0x00300000)
  242. # error "Anomaly 05000362: EB2CAW and EB3CAW must be the same"
  243. # endif
  244. #endif
  245. __attribute__((always_inline)) static inline void
  246. program_early_devices(ADI_BOOT_DATA *bs, uint *sdivB, uint *divB, uint *vcoB)
  247. {
  248. serial_putc('a');
  249. /* Save the clock pieces that are used in baud rate calculation */
  250. if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  251. serial_putc('b');
  252. *sdivB = bfin_read_PLL_DIV() & 0xf;
  253. *vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
  254. *divB = serial_early_get_div();
  255. serial_putc('c');
  256. }
  257. serial_putc('d');
  258. #ifdef CONFIG_HW_WATCHDOG
  259. # ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
  260. # define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
  261. # endif
  262. /* Program the watchdog with an initial timeout of ~20 seconds.
  263. * Hopefully that should be long enough to load the u-boot LDR
  264. * (from wherever) and then the common u-boot code can take over.
  265. * In bypass mode, the start.S would have already set a much lower
  266. * timeout, so don't clobber that.
  267. */
  268. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
  269. serial_putc('e');
  270. bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
  271. bfin_write_WDOG_CTL(0);
  272. serial_putc('f');
  273. }
  274. #endif
  275. serial_putc('g');
  276. /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
  277. * fast read, so we need to slow down the SPI clock a lot more during
  278. * boot. Once we switch over to u-boot's SPI flash driver, we'll
  279. * increase the speed appropriately.
  280. */
  281. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
  282. serial_putc('h');
  283. if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
  284. bs->dFlags |= BFLAG_FASTREAD;
  285. bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
  286. serial_putc('i');
  287. }
  288. serial_putc('j');
  289. }
  290. __attribute__((always_inline)) static inline bool
  291. maybe_self_refresh(ADI_BOOT_DATA *bs)
  292. {
  293. serial_putc('a');
  294. if (!CONFIG_MEM_SIZE)
  295. return false;
  296. /* If external memory is enabled, put it into self refresh first. */
  297. #if defined(EBIU_RSTCTL)
  298. if (bfin_read_EBIU_RSTCTL() & DDR_SRESET) {
  299. serial_putc('b');
  300. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | SRREQ);
  301. return true;
  302. }
  303. #elif defined(EBIU_SDGCTL)
  304. if (bfin_read_EBIU_SDBCTL() & EBE) {
  305. serial_putc('b');
  306. bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() | SRFS);
  307. return true;
  308. }
  309. #endif
  310. serial_putc('c');
  311. return false;
  312. }
  313. __attribute__((always_inline)) static inline u16
  314. program_clocks(ADI_BOOT_DATA *bs, bool put_into_srfs)
  315. {
  316. u16 vr_ctl;
  317. serial_putc('a');
  318. vr_ctl = bfin_read_VR_CTL();
  319. serial_putc('b');
  320. /* If we're entering self refresh, make sure it has happened. */
  321. if (put_into_srfs)
  322. #if defined(EBIU_RSTCTL)
  323. while (!(bfin_read_EBIU_RSTCTL() & SRACK))
  324. continue;
  325. #elif defined(EBIU_SDGCTL)
  326. while (!(bfin_read_EBIU_SDSTAT() & SDSRA))
  327. continue;
  328. #else
  329. ;
  330. #endif
  331. serial_putc('c');
  332. /* With newer bootroms, we use the helper function to set up
  333. * the memory controller. Older bootroms lacks such helpers
  334. * so we do it ourselves.
  335. */
  336. if (!ANOMALY_05000386) {
  337. serial_putc('d');
  338. /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
  339. ADI_SYSCTRL_VALUES memory_settings;
  340. uint32_t actions = SYSCTRL_WRITE | SYSCTRL_PLLCTL | SYSCTRL_LOCKCNT;
  341. if (!ANOMALY_05000440)
  342. actions |= SYSCTRL_PLLDIV;
  343. if (CONFIG_HAS_VR) {
  344. actions |= SYSCTRL_VRCTL;
  345. if (CONFIG_VR_CTL_VAL & FREQ_MASK)
  346. actions |= SYSCTRL_INTVOLTAGE;
  347. else
  348. actions |= SYSCTRL_EXTVOLTAGE;
  349. memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
  350. } else
  351. actions |= SYSCTRL_EXTVOLTAGE;
  352. memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
  353. memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
  354. memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
  355. #if ANOMALY_05000432
  356. bfin_write_SIC_IWR1(0);
  357. #endif
  358. serial_putc('e');
  359. bfrom_SysControl(actions, &memory_settings, NULL);
  360. serial_putc('f');
  361. if (ANOMALY_05000440)
  362. bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
  363. #if ANOMALY_05000432
  364. bfin_write_SIC_IWR1(-1);
  365. #endif
  366. #if ANOMALY_05000171
  367. bfin_write_SICA_IWR0(-1);
  368. bfin_write_SICA_IWR1(-1);
  369. #endif
  370. serial_putc('g');
  371. } else {
  372. serial_putc('h');
  373. /* Disable all peripheral wakeups except for the PLL event. */
  374. #ifdef SIC_IWR0
  375. bfin_write_SIC_IWR0(1);
  376. bfin_write_SIC_IWR1(0);
  377. # ifdef SIC_IWR2
  378. bfin_write_SIC_IWR2(0);
  379. # endif
  380. #elif defined(SICA_IWR0)
  381. bfin_write_SICA_IWR0(1);
  382. bfin_write_SICA_IWR1(0);
  383. #else
  384. bfin_write_SIC_IWR(1);
  385. #endif
  386. serial_putc('i');
  387. /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
  388. bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
  389. serial_putc('j');
  390. /* Only reprogram when needed to avoid triggering unnecessary
  391. * PLL relock sequences.
  392. */
  393. if (vr_ctl != CONFIG_VR_CTL_VAL) {
  394. serial_putc('?');
  395. bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
  396. asm("idle;");
  397. serial_putc('!');
  398. }
  399. serial_putc('k');
  400. bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
  401. serial_putc('l');
  402. /* Only reprogram when needed to avoid triggering unnecessary
  403. * PLL relock sequences.
  404. */
  405. if (ANOMALY_05000242 || bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
  406. serial_putc('?');
  407. bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
  408. asm("idle;");
  409. serial_putc('!');
  410. }
  411. serial_putc('m');
  412. /* Restore all peripheral wakeups. */
  413. #ifdef SIC_IWR0
  414. bfin_write_SIC_IWR0(-1);
  415. bfin_write_SIC_IWR1(-1);
  416. # ifdef SIC_IWR2
  417. bfin_write_SIC_IWR2(-1);
  418. # endif
  419. #elif defined(SICA_IWR0)
  420. bfin_write_SICA_IWR0(-1);
  421. bfin_write_SICA_IWR1(-1);
  422. #else
  423. bfin_write_SIC_IWR(-1);
  424. #endif
  425. serial_putc('n');
  426. }
  427. serial_putc('o');
  428. return vr_ctl;
  429. }
  430. __attribute__((always_inline)) static inline void
  431. update_serial_clocks(ADI_BOOT_DATA *bs, uint sdivB, uint divB, uint vcoB)
  432. {
  433. serial_putc('a');
  434. /* Since we've changed the SCLK above, we may need to update
  435. * the UART divisors (UART baud rates are based on SCLK).
  436. * Do the division by hand as there are no native instructions
  437. * for dividing which means we'd generate a libgcc reference.
  438. */
  439. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  440. serial_putc('b');
  441. unsigned int sdivR, vcoR;
  442. sdivR = bfin_read_PLL_DIV() & 0xf;
  443. vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
  444. int dividend = sdivB * divB * vcoR;
  445. int divisor = vcoB * sdivR;
  446. unsigned int quotient;
  447. for (quotient = 0; dividend > 0; ++quotient)
  448. dividend -= divisor;
  449. serial_early_put_div(UART_DLL, quotient - ANOMALY_05000230);
  450. serial_putc('c');
  451. }
  452. serial_putc('d');
  453. }
  454. __attribute__((always_inline)) static inline void
  455. program_memory_controller(ADI_BOOT_DATA *bs, bool put_into_srfs)
  456. {
  457. serial_putc('a');
  458. if (!CONFIG_MEM_SIZE)
  459. return;
  460. serial_putc('b');
  461. /* Program the external memory controller before we come out of
  462. * self-refresh. This only works with our SDRAM controller.
  463. */
  464. #ifdef EBIU_SDGCTL
  465. # ifdef CONFIG_EBIU_SDRRC_VAL
  466. bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
  467. # endif
  468. # ifdef CONFIG_EBIU_SDBCTL_VAL
  469. bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
  470. # endif
  471. # ifdef CONFIG_EBIU_SDGCTL_VAL
  472. bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
  473. # endif
  474. #endif
  475. serial_putc('c');
  476. /* Now that we've reprogrammed, take things out of self refresh. */
  477. if (put_into_srfs)
  478. #if defined(EBIU_RSTCTL)
  479. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() & ~(SRREQ));
  480. #elif defined(EBIU_SDGCTL)
  481. bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() & ~(SRFS));
  482. #endif
  483. serial_putc('d');
  484. /* Our DDR controller sucks and cannot be programmed while in
  485. * self-refresh. So we have to pull it out before programming.
  486. */
  487. #ifdef EBIU_RSTCTL
  488. # ifdef CONFIG_EBIU_RSTCTL_VAL
  489. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
  490. # endif
  491. # ifdef CONFIG_EBIU_DDRCTL0_VAL
  492. bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
  493. # endif
  494. # ifdef CONFIG_EBIU_DDRCTL1_VAL
  495. bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
  496. # endif
  497. # ifdef CONFIG_EBIU_DDRCTL2_VAL
  498. bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
  499. # endif
  500. # ifdef CONFIG_EBIU_DDRCTL3_VAL
  501. /* default is disable, so don't need to force this */
  502. bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
  503. # endif
  504. # ifdef CONFIG_EBIU_DDRQUE_VAL
  505. bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE() | CONFIG_EBIU_DDRQUE_VAL);
  506. # endif
  507. #endif
  508. serial_putc('e');
  509. }
  510. __attribute__((always_inline)) static inline void
  511. check_hibernation(ADI_BOOT_DATA *bs, u16 vr_ctl, bool put_into_srfs)
  512. {
  513. serial_putc('a');
  514. if (!CONFIG_MEM_SIZE)
  515. return;
  516. serial_putc('b');
  517. /* Are we coming out of hibernate (suspend to memory) ?
  518. * The memory layout is:
  519. * 0x0: hibernate magic for anomaly 307 (0xDEADBEEF)
  520. * 0x4: return address
  521. * 0x8: stack pointer
  522. *
  523. * SCKELOW is unreliable on older parts (anomaly 307)
  524. */
  525. if (ANOMALY_05000307 || vr_ctl & 0x8000) {
  526. uint32_t *hibernate_magic = 0;
  527. __builtin_bfin_ssync(); /* make sure memory controller is done */
  528. if (hibernate_magic[0] == 0xDEADBEEF) {
  529. serial_putc('c');
  530. bfin_write_EVT15(hibernate_magic[1]);
  531. bfin_write_IMASK(EVT_IVG15);
  532. __asm__ __volatile__ (
  533. /* load reti early to avoid anomaly 281 */
  534. "reti = %0;"
  535. /* clear hibernate magic */
  536. "[%0] = %1;"
  537. /* load stack pointer */
  538. "SP = [%0 + 8];"
  539. /* lower ourselves from reset ivg to ivg15 */
  540. "raise 15;"
  541. "rti;"
  542. :
  543. : "p"(hibernate_magic), "d"(0x2000 /* jump.s 0 */)
  544. );
  545. }
  546. serial_putc('d');
  547. }
  548. serial_putc('e');
  549. }
  550. BOOTROM_CALLED_FUNC_ATTR
  551. void initcode(ADI_BOOT_DATA *bs)
  552. {
  553. ADI_BOOT_DATA bootstruct_scratch;
  554. /* Setup NMI handler before anything else */
  555. program_nmi_handler();
  556. serial_init();
  557. serial_putc('A');
  558. /* If the bootstruct is NULL, then it's because we're loading
  559. * dynamically and not via LDR (bootrom). So set the struct to
  560. * some scratch space.
  561. */
  562. if (!bs)
  563. bs = &bootstruct_scratch;
  564. serial_putc('B');
  565. bool put_into_srfs = maybe_self_refresh(bs);
  566. serial_putc('C');
  567. uint sdivB, divB, vcoB;
  568. program_early_devices(bs, &sdivB, &divB, &vcoB);
  569. serial_putc('D');
  570. u16 vr_ctl = program_clocks(bs, put_into_srfs);
  571. serial_putc('E');
  572. update_serial_clocks(bs, sdivB, divB, vcoB);
  573. serial_putc('F');
  574. program_memory_controller(bs, put_into_srfs);
  575. serial_putc('G');
  576. check_hibernation(bs, vr_ctl, put_into_srfs);
  577. serial_putc('H');
  578. program_async_controller(bs);
  579. #ifdef CONFIG_BFIN_BOOTROM_USES_EVT1
  580. serial_putc('I');
  581. /* Tell the bootrom where our entry point is so that it knows
  582. * where to jump to when finishing processing the LDR. This
  583. * allows us to avoid small jump blocks in the LDR, and also
  584. * works around anomaly 05000389 (init address in external
  585. * memory causes bootrom to trigger external addressing IVHW).
  586. */
  587. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
  588. bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);
  589. #endif
  590. serial_putc('>');
  591. serial_putc('\n');
  592. serial_deinit();
  593. }