initcode.c 19 KB

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