initcode.c 18 KB

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