initcode.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. #define BUG() while (1) { asm volatile("emuexcpt;"); }
  17. #include "serial.h"
  18. #ifndef __ADSPBF60x__
  19. #include <asm/mach-common/bits/ebiu.h>
  20. #include <asm/mach-common/bits/pll.h>
  21. #else /* __ADSPBF60x__ */
  22. #include <asm/mach-common/bits/cgu.h>
  23. #define CONFIG_BFIN_GET_DCLK_M \
  24. ((CONFIG_CLKIN_HZ*CONFIG_VCO_MULT)/(CONFIG_DCLK_DIV*1000000))
  25. #ifndef CONFIG_DMC_DDRCFG
  26. #if ((CONFIG_BFIN_GET_DCLK_M != 125) && \
  27. (CONFIG_BFIN_GET_DCLK_M != 133) && \
  28. (CONFIG_BFIN_GET_DCLK_M != 150) && \
  29. (CONFIG_BFIN_GET_DCLK_M != 166) && \
  30. (CONFIG_BFIN_GET_DCLK_M != 200) && \
  31. (CONFIG_BFIN_GET_DCLK_M != 225) && \
  32. (CONFIG_BFIN_GET_DCLK_M != 250))
  33. #error "DDR2 CLK must be in (125, 133, 150, 166, 200, 225, 250)MHz"
  34. #endif
  35. #endif
  36. /* DMC control bits */
  37. #define SRREQ 0x8
  38. /* DMC status bits */
  39. #define IDLE 0x1
  40. #define MEMINITDONE 0x4
  41. #define SRACK 0x8
  42. #define PDACK 0x10
  43. #define DPDACK 0x20
  44. #define DLLCALDONE 0x2000
  45. #define PENDREF 0xF0000
  46. #define PHYRDPHASE 0xF00000
  47. #define PHYRDPHASE_OFFSET 20
  48. /* DMC DLL control bits */
  49. #define DLLCALRDCNT 0xFF
  50. #define DATACYC_OFFSET 8
  51. struct ddr_config {
  52. u32 ddr_clk;
  53. u32 dmc_ddrctl;
  54. u32 dmc_ddrcfg;
  55. u32 dmc_ddrtr0;
  56. u32 dmc_ddrtr1;
  57. u32 dmc_ddrtr2;
  58. u32 dmc_ddrmr;
  59. u32 dmc_ddrmr1;
  60. };
  61. static struct ddr_config ddr_config_table[] = {
  62. [0] = {
  63. .ddr_clk = 125, /* 125MHz */
  64. .dmc_ddrctl = 0x00000904,
  65. .dmc_ddrcfg = 0x00000422,
  66. .dmc_ddrtr0 = 0x20705212,
  67. .dmc_ddrtr1 = 0x201003CF,
  68. .dmc_ddrtr2 = 0x00320107,
  69. .dmc_ddrmr = 0x00000422,
  70. .dmc_ddrmr1 = 0x4,
  71. },
  72. [1] = {
  73. .ddr_clk = 133, /* 133MHz */
  74. .dmc_ddrctl = 0x00000904,
  75. .dmc_ddrcfg = 0x00000422,
  76. .dmc_ddrtr0 = 0x20806313,
  77. .dmc_ddrtr1 = 0x2013040D,
  78. .dmc_ddrtr2 = 0x00320108,
  79. .dmc_ddrmr = 0x00000632,
  80. .dmc_ddrmr1 = 0x4,
  81. },
  82. [2] = {
  83. .ddr_clk = 150, /* 150MHz */
  84. .dmc_ddrctl = 0x00000904,
  85. .dmc_ddrcfg = 0x00000422,
  86. .dmc_ddrtr0 = 0x20A07323,
  87. .dmc_ddrtr1 = 0x20160492,
  88. .dmc_ddrtr2 = 0x00320209,
  89. .dmc_ddrmr = 0x00000632,
  90. .dmc_ddrmr1 = 0x4,
  91. },
  92. [3] = {
  93. .ddr_clk = 166, /* 166MHz */
  94. .dmc_ddrctl = 0x00000904,
  95. .dmc_ddrcfg = 0x00000422,
  96. .dmc_ddrtr0 = 0x20A07323,
  97. .dmc_ddrtr1 = 0x2016050E,
  98. .dmc_ddrtr2 = 0x00320209,
  99. .dmc_ddrmr = 0x00000632,
  100. .dmc_ddrmr1 = 0x4,
  101. },
  102. [4] = {
  103. .ddr_clk = 200, /* 200MHz */
  104. .dmc_ddrctl = 0x00000904,
  105. .dmc_ddrcfg = 0x00000422,
  106. .dmc_ddrtr0 = 0x20a07323,
  107. .dmc_ddrtr1 = 0x2016050f,
  108. .dmc_ddrtr2 = 0x00320509,
  109. .dmc_ddrmr = 0x00000632,
  110. .dmc_ddrmr1 = 0x4,
  111. },
  112. [5] = {
  113. .ddr_clk = 225, /* 225MHz */
  114. .dmc_ddrctl = 0x00000904,
  115. .dmc_ddrcfg = 0x00000422,
  116. .dmc_ddrtr0 = 0x20E0A424,
  117. .dmc_ddrtr1 = 0x302006DB,
  118. .dmc_ddrtr2 = 0x0032020D,
  119. .dmc_ddrmr = 0x00000842,
  120. .dmc_ddrmr1 = 0x4,
  121. },
  122. [6] = {
  123. .ddr_clk = 250, /* 250MHz */
  124. .dmc_ddrctl = 0x00000904,
  125. .dmc_ddrcfg = 0x00000422,
  126. .dmc_ddrtr0 = 0x20E0A424,
  127. .dmc_ddrtr1 = 0x3020079E,
  128. .dmc_ddrtr2 = 0x0032050D,
  129. .dmc_ddrmr = 0x00000842,
  130. .dmc_ddrmr1 = 0x4,
  131. },
  132. };
  133. #endif /* __ADSPBF60x__ */
  134. __attribute__((always_inline))
  135. static inline void serial_init(void)
  136. {
  137. uint32_t uart_base = UART_BASE;
  138. #if defined(__ADSPBF54x__) || defined(__ADSPBF60x__)
  139. # ifdef BFIN_BOOT_UART_USE_RTS
  140. # define BFIN_UART_USE_RTS 1
  141. # else
  142. # define BFIN_UART_USE_RTS 0
  143. # endif
  144. if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  145. size_t i;
  146. /* force RTS rather than relying on auto RTS */
  147. #if BFIN_UART_HW_VER < 4
  148. bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) | FCPOL);
  149. #else
  150. bfin_write32(&pUART->control, bfin_read32(&pUART->control) |
  151. FCPOL);
  152. #endif
  153. /* Wait for the line to clear up. We cannot rely on UART
  154. * registers as none of them reflect the status of the RSR.
  155. * Instead, we'll sleep for ~10 bit times at 9600 baud.
  156. * We can precalc things here by assuming boot values for
  157. * PLL rather than loading registers and calculating.
  158. * baud = SCLK / (16 ^ (1 - EDBO) * Divisor)
  159. * EDB0 = 0
  160. * Divisor = (SCLK / baud) / 16
  161. * SCLK = baud * 16 * Divisor
  162. * SCLK = (0x14 * CONFIG_CLKIN_HZ) / 5
  163. * CCLK = (16 * Divisor * 5) * (9600 / 10)
  164. * In reality, this will probably be just about 1 second delay,
  165. * so assuming 9600 baud is OK (both as a very low and too high
  166. * speed as this will buffer things enough).
  167. */
  168. #define _NUMBITS (10) /* how many bits to delay */
  169. #define _LOWBAUD (9600) /* low baud rate */
  170. #define _SCLK ((0x14 * CONFIG_CLKIN_HZ) / 5) /* SCLK based on PLL */
  171. #define _DIVISOR ((_SCLK / _LOWBAUD) / 16) /* UART DLL/DLH */
  172. #define _NUMINS (3) /* how many instructions in loop */
  173. #define _CCLK (((16 * _DIVISOR * 5) * (_LOWBAUD / _NUMBITS)) / _NUMINS)
  174. i = _CCLK;
  175. while (i--)
  176. asm volatile("" : : : "memory");
  177. }
  178. #endif
  179. if (BFIN_DEBUG_EARLY_SERIAL) {
  180. int enabled = serial_early_enabled(uart_base);
  181. serial_early_init(uart_base);
  182. /* If the UART is off, that means we need to program
  183. * the baud rate ourselves initially.
  184. */
  185. if (!enabled)
  186. serial_early_set_baud(uart_base, CONFIG_BAUDRATE);
  187. }
  188. }
  189. __attribute__((always_inline))
  190. static inline void serial_deinit(void)
  191. {
  192. #if defined(__ADSPBF54x__) || defined(__ADSPBF60x__)
  193. uint32_t uart_base = UART_BASE;
  194. if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  195. /* clear forced RTS rather than relying on auto RTS */
  196. #if BFIN_UART_HW_VER < 4
  197. bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) & ~FCPOL);
  198. #else
  199. bfin_write32(&pUART->control, bfin_read32(&pUART->control) &
  200. ~FCPOL);
  201. #endif
  202. }
  203. #endif
  204. }
  205. __attribute__((always_inline))
  206. static inline void serial_putc(char c)
  207. {
  208. uint32_t uart_base = UART_BASE;
  209. if (!BFIN_DEBUG_EARLY_SERIAL)
  210. return;
  211. if (c == '\n')
  212. serial_putc('\r');
  213. bfin_write(&pUART->thr, c);
  214. while (!(_lsr_read(pUART) & TEMT))
  215. continue;
  216. }
  217. #include "initcode.h"
  218. __attribute__((always_inline)) static inline void
  219. program_nmi_handler(void)
  220. {
  221. u32 tmp1, tmp2;
  222. /* Older bootroms don't create a dummy NMI handler,
  223. * so make one ourselves ASAP in case it fires.
  224. */
  225. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS && !ANOMALY_05000219)
  226. return;
  227. asm volatile (
  228. "%0 = RETS;" /* Save current RETS */
  229. "CALL 1f;" /* Figure out current PC */
  230. "RTN;" /* The simple NMI handler */
  231. "1:"
  232. "%1 = RETS;" /* Load addr of NMI handler */
  233. "RETS = %0;" /* Restore RETS */
  234. "[%2] = %1;" /* Write NMI handler */
  235. : "=d"(tmp1), "=d"(tmp2)
  236. : "ab"(EVT2)
  237. );
  238. }
  239. /* Max SCLK can be 133MHz ... dividing that by (2*4) gives
  240. * us a freq of 16MHz for SPI which should generally be
  241. * slow enough for the slow reads the bootrom uses.
  242. */
  243. #if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
  244. ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
  245. (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
  246. # define BOOTROM_SUPPORTS_SPI_FAST_READ 1
  247. #else
  248. # define BOOTROM_SUPPORTS_SPI_FAST_READ 0
  249. #endif
  250. #ifndef CONFIG_SPI_BAUD_INITBLOCK
  251. # define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
  252. #endif
  253. #ifdef SPI0_BAUD
  254. # define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
  255. #endif
  256. #ifdef __ADSPBF60x__
  257. #ifndef CONFIG_CGU_CTL_VAL
  258. # define CONFIG_CGU_CTL_VAL ((CONFIG_VCO_MULT << 8) | CONFIG_CLKIN_HALF)
  259. #endif
  260. #ifndef CONFIG_CGU_DIV_VAL
  261. # define CONFIG_CGU_DIV_VAL \
  262. ((CONFIG_CCLK_DIV << CSEL_P) | \
  263. (CONFIG_SCLK0_DIV << S0SEL_P) | \
  264. (CONFIG_SCLK_DIV << SYSSEL_P) | \
  265. (CONFIG_SCLK1_DIV << S1SEL_P) | \
  266. (CONFIG_DCLK_DIV << DSEL_P) | \
  267. (CONFIG_OCLK_DIV << OSEL_P))
  268. #endif
  269. #else /* __ADSPBF60x__ */
  270. /* PLL_DIV defines */
  271. #ifndef CONFIG_PLL_DIV_VAL
  272. # if (CONFIG_CCLK_DIV == 1)
  273. # define CONFIG_CCLK_ACT_DIV CCLK_DIV1
  274. # elif (CONFIG_CCLK_DIV == 2)
  275. # define CONFIG_CCLK_ACT_DIV CCLK_DIV2
  276. # elif (CONFIG_CCLK_DIV == 4)
  277. # define CONFIG_CCLK_ACT_DIV CCLK_DIV4
  278. # elif (CONFIG_CCLK_DIV == 8)
  279. # define CONFIG_CCLK_ACT_DIV CCLK_DIV8
  280. # else
  281. # define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
  282. # endif
  283. # define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
  284. #endif
  285. #ifndef CONFIG_PLL_LOCKCNT_VAL
  286. # define CONFIG_PLL_LOCKCNT_VAL 0x0300
  287. #endif
  288. #ifndef CONFIG_PLL_CTL_VAL
  289. # define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
  290. #endif
  291. /* Make sure our voltage value is sane so we don't blow up! */
  292. #ifndef CONFIG_VR_CTL_VAL
  293. # define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
  294. # if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
  295. # define CCLK_VLEV_120 400000000
  296. # define CCLK_VLEV_125 533000000
  297. # elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  298. # define CCLK_VLEV_120 401000000
  299. # define CCLK_VLEV_125 401000000
  300. # elif defined(__ADSPBF561__)
  301. # define CCLK_VLEV_120 300000000
  302. # define CCLK_VLEV_125 501000000
  303. # endif
  304. # if BFIN_CCLK < CCLK_VLEV_120
  305. # define CONFIG_VR_CTL_VLEV VLEV_120
  306. # elif BFIN_CCLK < CCLK_VLEV_125
  307. # define CONFIG_VR_CTL_VLEV VLEV_125
  308. # else
  309. # define CONFIG_VR_CTL_VLEV VLEV_130
  310. # endif
  311. # if defined(__ADSPBF52x__) /* TBD; use default */
  312. # undef CONFIG_VR_CTL_VLEV
  313. # define CONFIG_VR_CTL_VLEV VLEV_110
  314. # elif defined(__ADSPBF54x__) /* TBD; use default */
  315. # undef CONFIG_VR_CTL_VLEV
  316. # define CONFIG_VR_CTL_VLEV VLEV_120
  317. # elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
  318. # undef CONFIG_VR_CTL_VLEV
  319. # define CONFIG_VR_CTL_VLEV VLEV_125
  320. # endif
  321. # ifdef CONFIG_BFIN_MAC
  322. # define CONFIG_VR_CTL_CLKBUF CLKBUFOE
  323. # else
  324. # define CONFIG_VR_CTL_CLKBUF 0
  325. # endif
  326. # if defined(__ADSPBF52x__)
  327. # define CONFIG_VR_CTL_FREQ FREQ_1000
  328. # else
  329. # define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
  330. # endif
  331. # define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
  332. #endif
  333. /* some parts do not have an on-chip voltage regulator */
  334. #if defined(__ADSPBF51x__)
  335. # define CONFIG_HAS_VR 0
  336. # undef CONFIG_VR_CTL_VAL
  337. # define CONFIG_VR_CTL_VAL 0
  338. #else
  339. # define CONFIG_HAS_VR 1
  340. #endif
  341. #if CONFIG_MEM_SIZE
  342. #ifndef EBIU_RSTCTL
  343. /* Blackfin with SDRAM */
  344. #ifndef CONFIG_EBIU_SDBCTL_VAL
  345. # if CONFIG_MEM_SIZE == 16
  346. # define CONFIG_EBSZ_VAL EBSZ_16
  347. # elif CONFIG_MEM_SIZE == 32
  348. # define CONFIG_EBSZ_VAL EBSZ_32
  349. # elif CONFIG_MEM_SIZE == 64
  350. # define CONFIG_EBSZ_VAL EBSZ_64
  351. # elif CONFIG_MEM_SIZE == 128
  352. # define CONFIG_EBSZ_VAL EBSZ_128
  353. # elif CONFIG_MEM_SIZE == 256
  354. # define CONFIG_EBSZ_VAL EBSZ_256
  355. # elif CONFIG_MEM_SIZE == 512
  356. # define CONFIG_EBSZ_VAL EBSZ_512
  357. # else
  358. # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
  359. # endif
  360. # if CONFIG_MEM_ADD_WDTH == 8
  361. # define CONFIG_EBCAW_VAL EBCAW_8
  362. # elif CONFIG_MEM_ADD_WDTH == 9
  363. # define CONFIG_EBCAW_VAL EBCAW_9
  364. # elif CONFIG_MEM_ADD_WDTH == 10
  365. # define CONFIG_EBCAW_VAL EBCAW_10
  366. # elif CONFIG_MEM_ADD_WDTH == 11
  367. # define CONFIG_EBCAW_VAL EBCAW_11
  368. # else
  369. # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
  370. # endif
  371. # define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
  372. #endif
  373. #endif
  374. #endif
  375. /* Conflicting Column Address Widths Causes SDRAM Errors:
  376. * EB2CAW and EB3CAW must be the same
  377. */
  378. #if ANOMALY_05000362
  379. # if ((CONFIG_EBIU_SDBCTL_VAL & 0x30000000) >> 8) != (CONFIG_EBIU_SDBCTL_VAL & 0x00300000)
  380. # error "Anomaly 05000362: EB2CAW and EB3CAW must be the same"
  381. # endif
  382. #endif
  383. #endif /* __ADSPBF60x__ */
  384. __attribute__((always_inline)) static inline void
  385. program_early_devices(ADI_BOOT_DATA *bs, uint *sdivB, uint *divB, uint *vcoB)
  386. {
  387. serial_putc('a');
  388. /* Save the clock pieces that are used in baud rate calculation */
  389. if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  390. serial_putc('b');
  391. #ifdef __ADSPBF60x__
  392. *sdivB = bfin_read_CGU_DIV();
  393. *sdivB = ((*sdivB >> 8) & 0x1f) * ((*sdivB >> 5) & 0x7);
  394. *vcoB = (bfin_read_CGU_CTL() >> 8) & 0x7f;
  395. #else
  396. *sdivB = bfin_read_PLL_DIV() & 0xf;
  397. *vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
  398. #endif
  399. *divB = serial_early_get_div();
  400. serial_putc('c');
  401. }
  402. serial_putc('d');
  403. #ifdef CONFIG_HW_WATCHDOG
  404. # ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
  405. # define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
  406. # endif
  407. /* Program the watchdog with an initial timeout of ~20 seconds.
  408. * Hopefully that should be long enough to load the u-boot LDR
  409. * (from wherever) and then the common u-boot code can take over.
  410. * In bypass mode, the start.S would have already set a much lower
  411. * timeout, so don't clobber that.
  412. */
  413. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
  414. serial_putc('e');
  415. #ifdef __ADSPBF60x__
  416. bfin_write_SEC_GCTL(0x2);
  417. SSYNC();
  418. bfin_write_SEC_FCTL(0xc1);
  419. bfin_write_SEC_SCTL(2, bfin_read_SEC_SCTL(2) | 0x6);
  420. bfin_write_SEC_CCTL(0x2);
  421. SSYNC();
  422. bfin_write_SEC_GCTL(0x1);
  423. bfin_write_SEC_CCTL(0x1);
  424. #endif
  425. bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
  426. #if CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_UART
  427. bfin_write_WDOG_CTL(0);
  428. #endif
  429. serial_putc('f');
  430. }
  431. #endif
  432. serial_putc('g');
  433. /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
  434. * fast read, so we need to slow down the SPI clock a lot more during
  435. * boot. Once we switch over to u-boot's SPI flash driver, we'll
  436. * increase the speed appropriately.
  437. */
  438. #ifdef SPI_BAUD
  439. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
  440. serial_putc('h');
  441. if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
  442. bs->dFlags |= BFLAG_FASTREAD;
  443. bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
  444. serial_putc('i');
  445. }
  446. #endif
  447. serial_putc('j');
  448. }
  449. __attribute__((always_inline)) static inline bool
  450. maybe_self_refresh(ADI_BOOT_DATA *bs)
  451. {
  452. serial_putc('a');
  453. if (!CONFIG_MEM_SIZE)
  454. return false;
  455. #ifdef __ADSPBF60x__
  456. /* resume from hibernate, return false let ddr initialize */
  457. if ((bfin_read32(DPM0_STAT) & 0xF0) == 0x50) {
  458. serial_putc('b');
  459. return false;
  460. }
  461. #else /* __ADSPBF60x__ */
  462. /* If external memory is enabled, put it into self refresh first. */
  463. #if defined(EBIU_RSTCTL)
  464. if (bfin_read_EBIU_RSTCTL() & DDR_SRESET) {
  465. serial_putc('b');
  466. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | SRREQ);
  467. return true;
  468. }
  469. #elif defined(EBIU_SDGCTL)
  470. if (bfin_read_EBIU_SDBCTL() & EBE) {
  471. serial_putc('b');
  472. bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() | SRFS);
  473. return true;
  474. }
  475. #endif
  476. #endif /* __ADSPBF60x__ */
  477. serial_putc('c');
  478. return false;
  479. }
  480. __attribute__((always_inline)) static inline u16
  481. program_clocks(ADI_BOOT_DATA *bs, bool put_into_srfs)
  482. {
  483. u16 vr_ctl;
  484. serial_putc('a');
  485. #ifdef __ADSPBF60x__
  486. if (bfin_read_DMC0_STAT() & MEMINITDONE) {
  487. bfin_write_DMC0_CTL(bfin_read_DMC0_CTL() | SRREQ);
  488. SSYNC();
  489. while (!(bfin_read_DMC0_STAT() & SRACK))
  490. continue;
  491. }
  492. /* Don't set the same value of MSEL and DF to CGU_CTL */
  493. if ((bfin_read_CGU_CTL() & (MSEL_MASK | DF_MASK))
  494. != CONFIG_CGU_CTL_VAL) {
  495. bfin_write_CGU_DIV(CONFIG_CGU_DIV_VAL);
  496. bfin_write_CGU_CTL(CONFIG_CGU_CTL_VAL);
  497. while ((bfin_read_CGU_STAT() & (CLKSALGN | PLLBP)) ||
  498. !(bfin_read_CGU_STAT() & PLLLK))
  499. continue;
  500. }
  501. bfin_write_CGU_DIV(CONFIG_CGU_DIV_VAL | UPDT);
  502. while (bfin_read_CGU_STAT() & CLKSALGN)
  503. continue;
  504. if (bfin_read_DMC0_STAT() & MEMINITDONE) {
  505. bfin_write_DMC0_CTL(bfin_read_DMC0_CTL() & ~SRREQ);
  506. SSYNC();
  507. while (bfin_read_DMC0_STAT() & SRACK)
  508. continue;
  509. }
  510. #else /* __ADSPBF60x__ */
  511. vr_ctl = bfin_read_VR_CTL();
  512. serial_putc('b');
  513. /* If we're entering self refresh, make sure it has happened. */
  514. if (put_into_srfs)
  515. #if defined(EBIU_RSTCTL)
  516. while (!(bfin_read_EBIU_RSTCTL() & SRACK))
  517. continue;
  518. #elif defined(EBIU_SDGCTL)
  519. while (!(bfin_read_EBIU_SDSTAT() & SDSRA))
  520. continue;
  521. #else
  522. ;
  523. #endif
  524. serial_putc('c');
  525. /* With newer bootroms, we use the helper function to set up
  526. * the memory controller. Older bootroms lacks such helpers
  527. * so we do it ourselves.
  528. */
  529. if (!ANOMALY_05000386) {
  530. serial_putc('d');
  531. /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
  532. ADI_SYSCTRL_VALUES memory_settings;
  533. uint32_t actions = SYSCTRL_WRITE | SYSCTRL_PLLCTL | SYSCTRL_LOCKCNT;
  534. if (!ANOMALY_05000440)
  535. actions |= SYSCTRL_PLLDIV;
  536. if (CONFIG_HAS_VR) {
  537. actions |= SYSCTRL_VRCTL;
  538. if (CONFIG_VR_CTL_VAL & FREQ_MASK)
  539. actions |= SYSCTRL_INTVOLTAGE;
  540. else
  541. actions |= SYSCTRL_EXTVOLTAGE;
  542. memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
  543. } else
  544. actions |= SYSCTRL_EXTVOLTAGE;
  545. memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
  546. memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
  547. memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
  548. #if ANOMALY_05000432
  549. bfin_write_SIC_IWR1(0);
  550. #endif
  551. serial_putc('e');
  552. bfrom_SysControl(actions, &memory_settings, NULL);
  553. serial_putc('f');
  554. if (ANOMALY_05000440)
  555. bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
  556. #if ANOMALY_05000432
  557. bfin_write_SIC_IWR1(-1);
  558. #endif
  559. #if ANOMALY_05000171
  560. bfin_write_SICA_IWR0(-1);
  561. bfin_write_SICA_IWR1(-1);
  562. #endif
  563. serial_putc('g');
  564. } else {
  565. serial_putc('h');
  566. /* Disable all peripheral wakeups except for the PLL event. */
  567. #ifdef SIC_IWR0
  568. bfin_write_SIC_IWR0(1);
  569. bfin_write_SIC_IWR1(0);
  570. # ifdef SIC_IWR2
  571. bfin_write_SIC_IWR2(0);
  572. # endif
  573. #elif defined(SICA_IWR0)
  574. bfin_write_SICA_IWR0(1);
  575. bfin_write_SICA_IWR1(0);
  576. #elif defined(SIC_IWR)
  577. bfin_write_SIC_IWR(1);
  578. #endif
  579. serial_putc('i');
  580. /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
  581. bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
  582. serial_putc('j');
  583. /* Only reprogram when needed to avoid triggering unnecessary
  584. * PLL relock sequences.
  585. */
  586. if (vr_ctl != CONFIG_VR_CTL_VAL) {
  587. serial_putc('?');
  588. bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
  589. asm("idle;");
  590. serial_putc('!');
  591. }
  592. serial_putc('k');
  593. bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
  594. serial_putc('l');
  595. /* Only reprogram when needed to avoid triggering unnecessary
  596. * PLL relock sequences.
  597. */
  598. if (ANOMALY_05000242 || bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
  599. serial_putc('?');
  600. bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
  601. asm("idle;");
  602. serial_putc('!');
  603. }
  604. serial_putc('m');
  605. /* Restore all peripheral wakeups. */
  606. #ifdef SIC_IWR0
  607. bfin_write_SIC_IWR0(-1);
  608. bfin_write_SIC_IWR1(-1);
  609. # ifdef SIC_IWR2
  610. bfin_write_SIC_IWR2(-1);
  611. # endif
  612. #elif defined(SICA_IWR0)
  613. bfin_write_SICA_IWR0(-1);
  614. bfin_write_SICA_IWR1(-1);
  615. #elif defined(SIC_IWR)
  616. bfin_write_SIC_IWR(-1);
  617. #endif
  618. serial_putc('n');
  619. }
  620. #endif /* __ADSPBF60x__ */
  621. serial_putc('o');
  622. return vr_ctl;
  623. }
  624. __attribute__((always_inline)) static inline void
  625. update_serial_clocks(ADI_BOOT_DATA *bs, uint sdivB, uint divB, uint vcoB)
  626. {
  627. serial_putc('a');
  628. /* Since we've changed the SCLK above, we may need to update
  629. * the UART divisors (UART baud rates are based on SCLK).
  630. * Do the division by hand as there are no native instructions
  631. * for dividing which means we'd generate a libgcc reference.
  632. */
  633. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  634. unsigned int sdivR, vcoR;
  635. int dividend = sdivB * divB * vcoR;
  636. int divisor = vcoB * sdivR;
  637. unsigned int quotient;
  638. serial_putc('b');
  639. #ifdef __ADSPBF60x__
  640. sdivR = bfin_read_CGU_DIV();
  641. sdivR = ((sdivR >> 8) & 0x1f) * ((sdivR >> 5) & 0x7);
  642. vcoR = (bfin_read_CGU_CTL() >> 8) & 0x7f;
  643. #else
  644. sdivR = bfin_read_PLL_DIV() & 0xf;
  645. vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
  646. #endif
  647. for (quotient = 0; dividend > 0; ++quotient)
  648. dividend -= divisor;
  649. serial_early_put_div(quotient - ANOMALY_05000230);
  650. serial_putc('c');
  651. }
  652. serial_putc('d');
  653. }
  654. __attribute__((always_inline)) static inline void
  655. program_memory_controller(ADI_BOOT_DATA *bs, bool put_into_srfs)
  656. {
  657. serial_putc('a');
  658. if (!CONFIG_MEM_SIZE)
  659. return;
  660. serial_putc('b');
  661. #ifdef __ADSPBF60x__
  662. int dlldatacycle;
  663. int dll_ctl;
  664. int i = 0;
  665. if (CONFIG_BFIN_GET_DCLK_M == 125)
  666. i = 0;
  667. else if (CONFIG_BFIN_GET_DCLK_M == 133)
  668. i = 1;
  669. else if (CONFIG_BFIN_GET_DCLK_M == 150)
  670. i = 2;
  671. else if (CONFIG_BFIN_GET_DCLK_M == 166)
  672. i = 3;
  673. else if (CONFIG_BFIN_GET_DCLK_M == 200)
  674. i = 4;
  675. else if (CONFIG_BFIN_GET_DCLK_M == 225)
  676. i = 5;
  677. else if (CONFIG_BFIN_GET_DCLK_M == 250)
  678. i = 6;
  679. #if 0
  680. for (i = 0; i < ARRAY_SIZE(ddr_config_table); i++)
  681. if (CONFIG_BFIN_GET_DCLK_M == ddr_config_table[i].ddr_clk)
  682. break;
  683. #endif
  684. #ifndef CONFIG_DMC_DDRCFG
  685. bfin_write_DMC0_CFG(ddr_config_table[i].dmc_ddrcfg);
  686. #else
  687. bfin_write_DMC0_CFG(CONFIG_DMC_DDRCFG);
  688. #endif
  689. #ifndef CONFIG_DMC_DDRTR0
  690. bfin_write_DMC0_TR0(ddr_config_table[i].dmc_ddrtr0);
  691. #else
  692. bfin_write_DMC0_TR0(CONFIG_DMC_DDRTR0);
  693. #endif
  694. #ifndef CONFIG_DMC_DDRTR1
  695. bfin_write_DMC0_TR1(ddr_config_table[i].dmc_ddrtr1);
  696. #else
  697. bfin_write_DMC0_TR1(CONFIG_DMC_DDRTR1);
  698. #endif
  699. #ifndef CONFIG_DMC_DDRTR2
  700. bfin_write_DMC0_TR2(ddr_config_table[i].dmc_ddrtr2);
  701. #else
  702. bfin_write_DMC0_TR2(CONFIG_DMC_DDRTR2);
  703. #endif
  704. #ifndef CONFIG_DMC_DDRMR
  705. bfin_write_DMC0_MR(ddr_config_table[i].dmc_ddrmr);
  706. #else
  707. bfin_write_DMC0_MR(CONFIG_DMC_DDRMR);
  708. #endif
  709. #ifndef CONFIG_DMC_DDREMR1
  710. bfin_write_DMC0_EMR1(ddr_config_table[i].dmc_ddrmr1);
  711. #else
  712. bfin_write_DMC0_EMR1(CONFIG_DMC_DDREMR1);
  713. #endif
  714. #ifndef CONFIG_DMC_DDRCTL
  715. bfin_write_DMC0_CTL(ddr_config_table[i].dmc_ddrctl);
  716. #else
  717. bfin_write_DMC0_CTL(CONFIG_DMC_DDRCTL);
  718. #endif
  719. SSYNC();
  720. while (!(bfin_read_DMC0_STAT() & MEMINITDONE))
  721. continue;
  722. dlldatacycle = (bfin_read_DMC0_STAT() & PHYRDPHASE) >>
  723. PHYRDPHASE_OFFSET;
  724. dll_ctl = bfin_read_DMC0_DLLCTL();
  725. dll_ctl &= 0x0ff;
  726. bfin_write_DMC0_DLLCTL(dll_ctl | (dlldatacycle << DATACYC_OFFSET));
  727. SSYNC();
  728. while (!(bfin_read_DMC0_STAT() & DLLCALDONE))
  729. continue;
  730. serial_putc('!');
  731. #else /* __ADSPBF60x__ */
  732. /* Program the external memory controller before we come out of
  733. * self-refresh. This only works with our SDRAM controller.
  734. */
  735. #ifdef EBIU_SDGCTL
  736. # ifdef CONFIG_EBIU_SDRRC_VAL
  737. bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
  738. # endif
  739. # ifdef CONFIG_EBIU_SDBCTL_VAL
  740. bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
  741. # endif
  742. # ifdef CONFIG_EBIU_SDGCTL_VAL
  743. bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
  744. # endif
  745. #endif
  746. serial_putc('c');
  747. /* Now that we've reprogrammed, take things out of self refresh. */
  748. if (put_into_srfs)
  749. #if defined(EBIU_RSTCTL)
  750. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() & ~(SRREQ));
  751. #elif defined(EBIU_SDGCTL)
  752. bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() & ~(SRFS));
  753. #endif
  754. serial_putc('d');
  755. /* Our DDR controller sucks and cannot be programmed while in
  756. * self-refresh. So we have to pull it out before programming.
  757. */
  758. #ifdef EBIU_RSTCTL
  759. # ifdef CONFIG_EBIU_RSTCTL_VAL
  760. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
  761. # endif
  762. # ifdef CONFIG_EBIU_DDRCTL0_VAL
  763. bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
  764. # endif
  765. # ifdef CONFIG_EBIU_DDRCTL1_VAL
  766. bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
  767. # endif
  768. # ifdef CONFIG_EBIU_DDRCTL2_VAL
  769. bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
  770. # endif
  771. # ifdef CONFIG_EBIU_DDRCTL3_VAL
  772. /* default is disable, so don't need to force this */
  773. bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
  774. # endif
  775. # ifdef CONFIG_EBIU_DDRQUE_VAL
  776. bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE() | CONFIG_EBIU_DDRQUE_VAL);
  777. # endif
  778. #endif
  779. #endif /* __ADSPBF60x__ */
  780. serial_putc('e');
  781. }
  782. __attribute__((always_inline)) static inline void
  783. check_hibernation(ADI_BOOT_DATA *bs, u16 vr_ctl, bool put_into_srfs)
  784. {
  785. serial_putc('a');
  786. if (!CONFIG_MEM_SIZE)
  787. return;
  788. serial_putc('b');
  789. #ifdef __ADSPBF60x__
  790. if (bfin_read32(DPM0_RESTORE0) != 0) {
  791. uint32_t reg = bfin_read_DMC0_CTL();
  792. reg &= ~0x8;
  793. bfin_write_DMC0_CTL(reg);
  794. while ((bfin_read_DMC0_STAT() & 0x8))
  795. continue;
  796. while (!(bfin_read_DMC0_STAT() & 0x1))
  797. continue;
  798. serial_putc('z');
  799. uint32_t *hibernate_magic = bfin_read32(DPM0_RESTORE4);
  800. SSYNC(); /* make sure memory controller is done */
  801. if (hibernate_magic[0] == 0xDEADBEEF) {
  802. serial_putc('c');
  803. SSYNC();
  804. bfin_write_EVT15(hibernate_magic[1]);
  805. bfin_write_IMASK(EVT_IVG15);
  806. __asm__ __volatile__ (
  807. /* load reti early to avoid anomaly 281 */
  808. "reti = %2;"
  809. /* clear hibernate magic */
  810. "[%0] = %1;"
  811. /* load stack pointer */
  812. "SP = [%0 + 8];"
  813. /* lower ourselves from reset ivg to ivg15 */
  814. "raise 15;"
  815. "nop;nop;nop;"
  816. "rti;"
  817. :
  818. : "p"(hibernate_magic),
  819. "d"(0x2000 /* jump.s 0 */),
  820. "d"(0xffa00000)
  821. );
  822. }
  823. }
  824. #else
  825. /* Are we coming out of hibernate (suspend to memory) ?
  826. * The memory layout is:
  827. * 0x0: hibernate magic for anomaly 307 (0xDEADBEEF)
  828. * 0x4: return address
  829. * 0x8: stack pointer
  830. *
  831. * SCKELOW is unreliable on older parts (anomaly 307)
  832. */
  833. if (ANOMALY_05000307 || vr_ctl & 0x8000) {
  834. uint32_t *hibernate_magic = 0;
  835. SSYNC();
  836. if (hibernate_magic[0] == 0xDEADBEEF) {
  837. serial_putc('c');
  838. bfin_write_EVT15(hibernate_magic[1]);
  839. bfin_write_IMASK(EVT_IVG15);
  840. __asm__ __volatile__ (
  841. /* load reti early to avoid anomaly 281 */
  842. "reti = %0;"
  843. /* clear hibernate magic */
  844. "[%0] = %1;"
  845. /* load stack pointer */
  846. "SP = [%0 + 8];"
  847. /* lower ourselves from reset ivg to ivg15 */
  848. "raise 15;"
  849. "rti;"
  850. :
  851. : "p"(hibernate_magic), "d"(0x2000 /* jump.s 0 */)
  852. );
  853. }
  854. serial_putc('d');
  855. }
  856. #endif
  857. serial_putc('e');
  858. }
  859. BOOTROM_CALLED_FUNC_ATTR
  860. void initcode(ADI_BOOT_DATA *bs)
  861. {
  862. ADI_BOOT_DATA bootstruct_scratch;
  863. /* Setup NMI handler before anything else */
  864. program_nmi_handler();
  865. serial_init();
  866. serial_putc('A');
  867. /* If the bootstruct is NULL, then it's because we're loading
  868. * dynamically and not via LDR (bootrom). So set the struct to
  869. * some scratch space.
  870. */
  871. if (!bs)
  872. bs = &bootstruct_scratch;
  873. serial_putc('B');
  874. bool put_into_srfs = maybe_self_refresh(bs);
  875. serial_putc('C');
  876. uint sdivB, divB, vcoB;
  877. program_early_devices(bs, &sdivB, &divB, &vcoB);
  878. serial_putc('D');
  879. u16 vr_ctl = program_clocks(bs, put_into_srfs);
  880. serial_putc('E');
  881. update_serial_clocks(bs, sdivB, divB, vcoB);
  882. serial_putc('F');
  883. program_memory_controller(bs, put_into_srfs);
  884. serial_putc('G');
  885. check_hibernation(bs, vr_ctl, put_into_srfs);
  886. serial_putc('H');
  887. program_async_controller(bs);
  888. #ifdef CONFIG_BFIN_BOOTROM_USES_EVT1
  889. serial_putc('I');
  890. /* Tell the bootrom where our entry point is so that it knows
  891. * where to jump to when finishing processing the LDR. This
  892. * allows us to avoid small jump blocks in the LDR, and also
  893. * works around anomaly 05000389 (init address in external
  894. * memory causes bootrom to trigger external addressing IVHW).
  895. */
  896. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
  897. bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);
  898. #endif
  899. serial_putc('>');
  900. serial_putc('\n');
  901. serial_deinit();
  902. }