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