initcode.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. : "=r"(tmp1), "=r"(tmp2) : "ab"(EVT2)
  236. );
  237. }
  238. /* Max SCLK can be 133MHz ... dividing that by (2*4) gives
  239. * us a freq of 16MHz for SPI which should generally be
  240. * slow enough for the slow reads the bootrom uses.
  241. */
  242. #if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
  243. ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
  244. (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
  245. # define BOOTROM_SUPPORTS_SPI_FAST_READ 1
  246. #else
  247. # define BOOTROM_SUPPORTS_SPI_FAST_READ 0
  248. #endif
  249. #ifndef CONFIG_SPI_BAUD_INITBLOCK
  250. # define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
  251. #endif
  252. #ifdef SPI0_BAUD
  253. # define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
  254. #endif
  255. #ifdef __ADSPBF60x__
  256. #ifndef CONFIG_CGU_CTL_VAL
  257. # define CONFIG_CGU_CTL_VAL ((CONFIG_VCO_MULT << 8) | CONFIG_CLKIN_HALF)
  258. #endif
  259. #ifndef CONFIG_CGU_DIV_VAL
  260. # define CONFIG_CGU_DIV_VAL \
  261. ((CONFIG_CCLK_DIV << CSEL_P) | \
  262. (CONFIG_SCLK0_DIV << S0SEL_P) | \
  263. (CONFIG_SCLK_DIV << SYSSEL_P) | \
  264. (CONFIG_SCLK1_DIV << S1SEL_P) | \
  265. (CONFIG_DCLK_DIV << DSEL_P) | \
  266. (CONFIG_OCLK_DIV << OSEL_P))
  267. #endif
  268. #else /* __ADSPBF60x__ */
  269. /* PLL_DIV defines */
  270. #ifndef CONFIG_PLL_DIV_VAL
  271. # if (CONFIG_CCLK_DIV == 1)
  272. # define CONFIG_CCLK_ACT_DIV CCLK_DIV1
  273. # elif (CONFIG_CCLK_DIV == 2)
  274. # define CONFIG_CCLK_ACT_DIV CCLK_DIV2
  275. # elif (CONFIG_CCLK_DIV == 4)
  276. # define CONFIG_CCLK_ACT_DIV CCLK_DIV4
  277. # elif (CONFIG_CCLK_DIV == 8)
  278. # define CONFIG_CCLK_ACT_DIV CCLK_DIV8
  279. # else
  280. # define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
  281. # endif
  282. # define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
  283. #endif
  284. #ifndef CONFIG_PLL_LOCKCNT_VAL
  285. # define CONFIG_PLL_LOCKCNT_VAL 0x0300
  286. #endif
  287. #ifndef CONFIG_PLL_CTL_VAL
  288. # define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
  289. #endif
  290. /* Make sure our voltage value is sane so we don't blow up! */
  291. #ifndef CONFIG_VR_CTL_VAL
  292. # define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
  293. # if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
  294. # define CCLK_VLEV_120 400000000
  295. # define CCLK_VLEV_125 533000000
  296. # elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
  297. # define CCLK_VLEV_120 401000000
  298. # define CCLK_VLEV_125 401000000
  299. # elif defined(__ADSPBF561__)
  300. # define CCLK_VLEV_120 300000000
  301. # define CCLK_VLEV_125 501000000
  302. # endif
  303. # if BFIN_CCLK < CCLK_VLEV_120
  304. # define CONFIG_VR_CTL_VLEV VLEV_120
  305. # elif BFIN_CCLK < CCLK_VLEV_125
  306. # define CONFIG_VR_CTL_VLEV VLEV_125
  307. # else
  308. # define CONFIG_VR_CTL_VLEV VLEV_130
  309. # endif
  310. # if defined(__ADSPBF52x__) /* TBD; use default */
  311. # undef CONFIG_VR_CTL_VLEV
  312. # define CONFIG_VR_CTL_VLEV VLEV_110
  313. # elif defined(__ADSPBF54x__) /* TBD; use default */
  314. # undef CONFIG_VR_CTL_VLEV
  315. # define CONFIG_VR_CTL_VLEV VLEV_120
  316. # elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
  317. # undef CONFIG_VR_CTL_VLEV
  318. # define CONFIG_VR_CTL_VLEV VLEV_125
  319. # endif
  320. # ifdef CONFIG_BFIN_MAC
  321. # define CONFIG_VR_CTL_CLKBUF CLKBUFOE
  322. # else
  323. # define CONFIG_VR_CTL_CLKBUF 0
  324. # endif
  325. # if defined(__ADSPBF52x__)
  326. # define CONFIG_VR_CTL_FREQ FREQ_1000
  327. # else
  328. # define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
  329. # endif
  330. # define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
  331. #endif
  332. /* some parts do not have an on-chip voltage regulator */
  333. #if defined(__ADSPBF51x__)
  334. # define CONFIG_HAS_VR 0
  335. # undef CONFIG_VR_CTL_VAL
  336. # define CONFIG_VR_CTL_VAL 0
  337. #else
  338. # define CONFIG_HAS_VR 1
  339. #endif
  340. #if CONFIG_MEM_SIZE
  341. #ifndef EBIU_RSTCTL
  342. /* Blackfin with SDRAM */
  343. #ifndef CONFIG_EBIU_SDBCTL_VAL
  344. # if CONFIG_MEM_SIZE == 16
  345. # define CONFIG_EBSZ_VAL EBSZ_16
  346. # elif CONFIG_MEM_SIZE == 32
  347. # define CONFIG_EBSZ_VAL EBSZ_32
  348. # elif CONFIG_MEM_SIZE == 64
  349. # define CONFIG_EBSZ_VAL EBSZ_64
  350. # elif CONFIG_MEM_SIZE == 128
  351. # define CONFIG_EBSZ_VAL EBSZ_128
  352. # elif CONFIG_MEM_SIZE == 256
  353. # define CONFIG_EBSZ_VAL EBSZ_256
  354. # elif CONFIG_MEM_SIZE == 512
  355. # define CONFIG_EBSZ_VAL EBSZ_512
  356. # else
  357. # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
  358. # endif
  359. # if CONFIG_MEM_ADD_WDTH == 8
  360. # define CONFIG_EBCAW_VAL EBCAW_8
  361. # elif CONFIG_MEM_ADD_WDTH == 9
  362. # define CONFIG_EBCAW_VAL EBCAW_9
  363. # elif CONFIG_MEM_ADD_WDTH == 10
  364. # define CONFIG_EBCAW_VAL EBCAW_10
  365. # elif CONFIG_MEM_ADD_WDTH == 11
  366. # define CONFIG_EBCAW_VAL EBCAW_11
  367. # else
  368. # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
  369. # endif
  370. # define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
  371. #endif
  372. #endif
  373. #endif
  374. /* Conflicting Column Address Widths Causes SDRAM Errors:
  375. * EB2CAW and EB3CAW must be the same
  376. */
  377. #if ANOMALY_05000362
  378. # if ((CONFIG_EBIU_SDBCTL_VAL & 0x30000000) >> 8) != (CONFIG_EBIU_SDBCTL_VAL & 0x00300000)
  379. # error "Anomaly 05000362: EB2CAW and EB3CAW must be the same"
  380. # endif
  381. #endif
  382. #endif /* __ADSPBF60x__ */
  383. __attribute__((always_inline)) static inline void
  384. program_early_devices(ADI_BOOT_DATA *bs, uint *sdivB, uint *divB, uint *vcoB)
  385. {
  386. serial_putc('a');
  387. /* Save the clock pieces that are used in baud rate calculation */
  388. if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  389. serial_putc('b');
  390. #ifdef __ADSPBF60x__
  391. *sdivB = bfin_read_CGU_DIV();
  392. *sdivB = ((*sdivB >> 8) & 0x1f) * ((*sdivB >> 5) & 0x7);
  393. *vcoB = (bfin_read_CGU_CTL() >> 8) & 0x7f;
  394. #else
  395. *sdivB = bfin_read_PLL_DIV() & 0xf;
  396. *vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
  397. #endif
  398. *divB = serial_early_get_div();
  399. serial_putc('c');
  400. }
  401. serial_putc('d');
  402. #ifdef CONFIG_HW_WATCHDOG
  403. # ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
  404. # define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
  405. # endif
  406. /* Program the watchdog with an initial timeout of ~20 seconds.
  407. * Hopefully that should be long enough to load the u-boot LDR
  408. * (from wherever) and then the common u-boot code can take over.
  409. * In bypass mode, the start.S would have already set a much lower
  410. * timeout, so don't clobber that.
  411. */
  412. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
  413. serial_putc('e');
  414. #ifdef __ADSPBF60x__
  415. bfin_write_SEC_GCTL(0x2);
  416. SSYNC();
  417. bfin_write_SEC_FCTL(0xc1);
  418. bfin_write_SEC_SCTL(2, bfin_read_SEC_SCTL(2) | 0x6);
  419. bfin_write_SEC_CCTL(0x2);
  420. SSYNC();
  421. bfin_write_SEC_GCTL(0x1);
  422. bfin_write_SEC_CCTL(0x1);
  423. #endif
  424. bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
  425. #if CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_UART
  426. bfin_write_WDOG_CTL(0);
  427. #endif
  428. serial_putc('f');
  429. }
  430. #endif
  431. serial_putc('g');
  432. /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
  433. * fast read, so we need to slow down the SPI clock a lot more during
  434. * boot. Once we switch over to u-boot's SPI flash driver, we'll
  435. * increase the speed appropriately.
  436. */
  437. #ifdef SPI_BAUD
  438. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
  439. serial_putc('h');
  440. if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
  441. bs->dFlags |= BFLAG_FASTREAD;
  442. bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
  443. serial_putc('i');
  444. }
  445. #endif
  446. serial_putc('j');
  447. }
  448. __attribute__((always_inline)) static inline bool
  449. maybe_self_refresh(ADI_BOOT_DATA *bs)
  450. {
  451. serial_putc('a');
  452. if (!CONFIG_MEM_SIZE)
  453. return false;
  454. #ifdef __ADSPBF60x__
  455. /* resume from hibernate, return false let ddr initialize */
  456. if ((bfin_read32(DPM0_STAT) & 0xF0) == 0x50) {
  457. serial_putc('b');
  458. return false;
  459. }
  460. #else /* __ADSPBF60x__ */
  461. /* If external memory is enabled, put it into self refresh first. */
  462. #if defined(EBIU_RSTCTL)
  463. if (bfin_read_EBIU_RSTCTL() & DDR_SRESET) {
  464. serial_putc('b');
  465. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | SRREQ);
  466. return true;
  467. }
  468. #elif defined(EBIU_SDGCTL)
  469. if (bfin_read_EBIU_SDBCTL() & EBE) {
  470. serial_putc('b');
  471. bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() | SRFS);
  472. return true;
  473. }
  474. #endif
  475. #endif /* __ADSPBF60x__ */
  476. serial_putc('c');
  477. return false;
  478. }
  479. __attribute__((always_inline)) static inline u16
  480. program_clocks(ADI_BOOT_DATA *bs, bool put_into_srfs)
  481. {
  482. u16 vr_ctl;
  483. serial_putc('a');
  484. #ifdef __ADSPBF60x__
  485. if (bfin_read_DMC0_STAT() & MEMINITDONE) {
  486. bfin_write_DMC0_CTL(bfin_read_DMC0_CTL() | SRREQ);
  487. SSYNC();
  488. while (!(bfin_read_DMC0_STAT() & SRACK))
  489. continue;
  490. }
  491. /* Don't set the same value of MSEL and DF to CGU_CTL */
  492. if ((bfin_read_CGU_CTL() & (MSEL_MASK | DF_MASK))
  493. != CONFIG_CGU_CTL_VAL) {
  494. bfin_write_CGU_DIV(CONFIG_CGU_DIV_VAL);
  495. bfin_write_CGU_CTL(CONFIG_CGU_CTL_VAL);
  496. while ((bfin_read_CGU_STAT() & (CLKSALGN | PLLBP)) ||
  497. !(bfin_read_CGU_STAT() & PLLLK))
  498. continue;
  499. }
  500. bfin_write_CGU_DIV(CONFIG_CGU_DIV_VAL | UPDT);
  501. while (bfin_read_CGU_STAT() & CLKSALGN)
  502. continue;
  503. if (bfin_read_DMC0_STAT() & MEMINITDONE) {
  504. bfin_write_DMC0_CTL(bfin_read_DMC0_CTL() & ~SRREQ);
  505. SSYNC();
  506. while (bfin_read_DMC0_STAT() & SRACK)
  507. continue;
  508. }
  509. #else /* __ADSPBF60x__ */
  510. vr_ctl = bfin_read_VR_CTL();
  511. serial_putc('b');
  512. /* If we're entering self refresh, make sure it has happened. */
  513. if (put_into_srfs)
  514. #if defined(EBIU_RSTCTL)
  515. while (!(bfin_read_EBIU_RSTCTL() & SRACK))
  516. continue;
  517. #elif defined(EBIU_SDGCTL)
  518. while (!(bfin_read_EBIU_SDSTAT() & SDSRA))
  519. continue;
  520. #else
  521. ;
  522. #endif
  523. serial_putc('c');
  524. /* With newer bootroms, we use the helper function to set up
  525. * the memory controller. Older bootroms lacks such helpers
  526. * so we do it ourselves.
  527. */
  528. if (!ANOMALY_05000386) {
  529. serial_putc('d');
  530. /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
  531. ADI_SYSCTRL_VALUES memory_settings;
  532. uint32_t actions = SYSCTRL_WRITE | SYSCTRL_PLLCTL | SYSCTRL_LOCKCNT;
  533. if (!ANOMALY_05000440)
  534. actions |= SYSCTRL_PLLDIV;
  535. if (CONFIG_HAS_VR) {
  536. actions |= SYSCTRL_VRCTL;
  537. if (CONFIG_VR_CTL_VAL & FREQ_MASK)
  538. actions |= SYSCTRL_INTVOLTAGE;
  539. else
  540. actions |= SYSCTRL_EXTVOLTAGE;
  541. memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
  542. } else
  543. actions |= SYSCTRL_EXTVOLTAGE;
  544. memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
  545. memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
  546. memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
  547. #if ANOMALY_05000432
  548. bfin_write_SIC_IWR1(0);
  549. #endif
  550. serial_putc('e');
  551. bfrom_SysControl(actions, &memory_settings, NULL);
  552. serial_putc('f');
  553. if (ANOMALY_05000440)
  554. bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
  555. #if ANOMALY_05000432
  556. bfin_write_SIC_IWR1(-1);
  557. #endif
  558. #if ANOMALY_05000171
  559. bfin_write_SICA_IWR0(-1);
  560. bfin_write_SICA_IWR1(-1);
  561. #endif
  562. serial_putc('g');
  563. } else {
  564. serial_putc('h');
  565. /* Disable all peripheral wakeups except for the PLL event. */
  566. #ifdef SIC_IWR0
  567. bfin_write_SIC_IWR0(1);
  568. bfin_write_SIC_IWR1(0);
  569. # ifdef SIC_IWR2
  570. bfin_write_SIC_IWR2(0);
  571. # endif
  572. #elif defined(SICA_IWR0)
  573. bfin_write_SICA_IWR0(1);
  574. bfin_write_SICA_IWR1(0);
  575. #elif defined(SIC_IWR)
  576. bfin_write_SIC_IWR(1);
  577. #endif
  578. serial_putc('i');
  579. /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
  580. bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
  581. serial_putc('j');
  582. /* Only reprogram when needed to avoid triggering unnecessary
  583. * PLL relock sequences.
  584. */
  585. if (vr_ctl != CONFIG_VR_CTL_VAL) {
  586. serial_putc('?');
  587. bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
  588. asm("idle;");
  589. serial_putc('!');
  590. }
  591. serial_putc('k');
  592. bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
  593. serial_putc('l');
  594. /* Only reprogram when needed to avoid triggering unnecessary
  595. * PLL relock sequences.
  596. */
  597. if (ANOMALY_05000242 || bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
  598. serial_putc('?');
  599. bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
  600. asm("idle;");
  601. serial_putc('!');
  602. }
  603. serial_putc('m');
  604. /* Restore all peripheral wakeups. */
  605. #ifdef SIC_IWR0
  606. bfin_write_SIC_IWR0(-1);
  607. bfin_write_SIC_IWR1(-1);
  608. # ifdef SIC_IWR2
  609. bfin_write_SIC_IWR2(-1);
  610. # endif
  611. #elif defined(SICA_IWR0)
  612. bfin_write_SICA_IWR0(-1);
  613. bfin_write_SICA_IWR1(-1);
  614. #elif defined(SIC_IWR)
  615. bfin_write_SIC_IWR(-1);
  616. #endif
  617. serial_putc('n');
  618. }
  619. #endif /* __ADSPBF60x__ */
  620. serial_putc('o');
  621. return vr_ctl;
  622. }
  623. __attribute__((always_inline)) static inline void
  624. update_serial_clocks(ADI_BOOT_DATA *bs, uint sdivB, uint divB, uint vcoB)
  625. {
  626. serial_putc('a');
  627. /* Since we've changed the SCLK above, we may need to update
  628. * the UART divisors (UART baud rates are based on SCLK).
  629. * Do the division by hand as there are no native instructions
  630. * for dividing which means we'd generate a libgcc reference.
  631. */
  632. if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
  633. unsigned int sdivR, vcoR;
  634. int dividend = sdivB * divB * vcoR;
  635. int divisor = vcoB * sdivR;
  636. unsigned int quotient;
  637. serial_putc('b');
  638. #ifdef __ADSPBF60x__
  639. sdivR = bfin_read_CGU_DIV();
  640. sdivR = ((sdivR >> 8) & 0x1f) * ((sdivR >> 5) & 0x7);
  641. vcoR = (bfin_read_CGU_CTL() >> 8) & 0x7f;
  642. #else
  643. sdivR = bfin_read_PLL_DIV() & 0xf;
  644. vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
  645. #endif
  646. for (quotient = 0; dividend > 0; ++quotient)
  647. dividend -= divisor;
  648. serial_early_put_div(quotient - ANOMALY_05000230);
  649. serial_putc('c');
  650. }
  651. serial_putc('d');
  652. }
  653. __attribute__((always_inline)) static inline void
  654. program_memory_controller(ADI_BOOT_DATA *bs, bool put_into_srfs)
  655. {
  656. serial_putc('a');
  657. if (!CONFIG_MEM_SIZE)
  658. return;
  659. serial_putc('b');
  660. #ifdef __ADSPBF60x__
  661. int dlldatacycle;
  662. int dll_ctl;
  663. int i = 0;
  664. if (CONFIG_BFIN_GET_DCLK_M == 125)
  665. i = 0;
  666. else if (CONFIG_BFIN_GET_DCLK_M == 133)
  667. i = 1;
  668. else if (CONFIG_BFIN_GET_DCLK_M == 150)
  669. i = 2;
  670. else if (CONFIG_BFIN_GET_DCLK_M == 166)
  671. i = 3;
  672. else if (CONFIG_BFIN_GET_DCLK_M == 200)
  673. i = 4;
  674. else if (CONFIG_BFIN_GET_DCLK_M == 225)
  675. i = 5;
  676. else if (CONFIG_BFIN_GET_DCLK_M == 250)
  677. i = 6;
  678. #if 0
  679. for (i = 0; i < ARRAY_SIZE(ddr_config_table); i++)
  680. if (CONFIG_BFIN_GET_DCLK_M == ddr_config_table[i].ddr_clk)
  681. break;
  682. #endif
  683. #ifndef CONFIG_DMC_DDRCFG
  684. bfin_write_DMC0_CFG(ddr_config_table[i].dmc_ddrcfg);
  685. #else
  686. bfin_write_DMC0_CFG(CONFIG_DMC_DDRCFG);
  687. #endif
  688. #ifndef CONFIG_DMC_DDRTR0
  689. bfin_write_DMC0_TR0(ddr_config_table[i].dmc_ddrtr0);
  690. #else
  691. bfin_write_DMC0_TR0(CONFIG_DMC_DDRTR0);
  692. #endif
  693. #ifndef CONFIG_DMC_DDRTR1
  694. bfin_write_DMC0_TR1(ddr_config_table[i].dmc_ddrtr1);
  695. #else
  696. bfin_write_DMC0_TR1(CONFIG_DMC_DDRTR1);
  697. #endif
  698. #ifndef CONFIG_DMC_DDRTR2
  699. bfin_write_DMC0_TR2(ddr_config_table[i].dmc_ddrtr2);
  700. #else
  701. bfin_write_DMC0_TR2(CONFIG_DMC_DDRTR2);
  702. #endif
  703. #ifndef CONFIG_DMC_DDRMR
  704. bfin_write_DMC0_MR(ddr_config_table[i].dmc_ddrmr);
  705. #else
  706. bfin_write_DMC0_MR(CONFIG_DMC_DDRMR);
  707. #endif
  708. #ifndef CONFIG_DMC_DDREMR1
  709. bfin_write_DMC0_EMR1(ddr_config_table[i].dmc_ddrmr1);
  710. #else
  711. bfin_write_DMC0_EMR1(CONFIG_DMC_DDREMR1);
  712. #endif
  713. #ifndef CONFIG_DMC_DDRCTL
  714. bfin_write_DMC0_CTL(ddr_config_table[i].dmc_ddrctl);
  715. #else
  716. bfin_write_DMC0_CTL(CONFIG_DMC_DDRCTL);
  717. #endif
  718. SSYNC();
  719. while (!(bfin_read_DMC0_STAT() & MEMINITDONE))
  720. continue;
  721. dlldatacycle = (bfin_read_DMC0_STAT() & PHYRDPHASE) >>
  722. PHYRDPHASE_OFFSET;
  723. dll_ctl = bfin_read_DMC0_DLLCTL();
  724. dll_ctl &= 0x0ff;
  725. bfin_write_DMC0_DLLCTL(dll_ctl | (dlldatacycle << DATACYC_OFFSET));
  726. SSYNC();
  727. while (!(bfin_read_DMC0_STAT() & DLLCALDONE))
  728. continue;
  729. serial_putc('!');
  730. #else /* __ADSPBF60x__ */
  731. /* Program the external memory controller before we come out of
  732. * self-refresh. This only works with our SDRAM controller.
  733. */
  734. #ifdef EBIU_SDGCTL
  735. # ifdef CONFIG_EBIU_SDRRC_VAL
  736. bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
  737. # endif
  738. # ifdef CONFIG_EBIU_SDBCTL_VAL
  739. bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
  740. # endif
  741. # ifdef CONFIG_EBIU_SDGCTL_VAL
  742. bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
  743. # endif
  744. #endif
  745. serial_putc('c');
  746. /* Now that we've reprogrammed, take things out of self refresh. */
  747. if (put_into_srfs)
  748. #if defined(EBIU_RSTCTL)
  749. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() & ~(SRREQ));
  750. #elif defined(EBIU_SDGCTL)
  751. bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() & ~(SRFS));
  752. #endif
  753. serial_putc('d');
  754. /* Our DDR controller sucks and cannot be programmed while in
  755. * self-refresh. So we have to pull it out before programming.
  756. */
  757. #ifdef EBIU_RSTCTL
  758. # ifdef CONFIG_EBIU_RSTCTL_VAL
  759. bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
  760. # endif
  761. # ifdef CONFIG_EBIU_DDRCTL0_VAL
  762. bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
  763. # endif
  764. # ifdef CONFIG_EBIU_DDRCTL1_VAL
  765. bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
  766. # endif
  767. # ifdef CONFIG_EBIU_DDRCTL2_VAL
  768. bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
  769. # endif
  770. # ifdef CONFIG_EBIU_DDRCTL3_VAL
  771. /* default is disable, so don't need to force this */
  772. bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
  773. # endif
  774. # ifdef CONFIG_EBIU_DDRQUE_VAL
  775. bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE() | CONFIG_EBIU_DDRQUE_VAL);
  776. # endif
  777. #endif
  778. #endif /* __ADSPBF60x__ */
  779. serial_putc('e');
  780. }
  781. __attribute__((always_inline)) static inline void
  782. check_hibernation(ADI_BOOT_DATA *bs, u16 vr_ctl, bool put_into_srfs)
  783. {
  784. serial_putc('a');
  785. if (!CONFIG_MEM_SIZE)
  786. return;
  787. serial_putc('b');
  788. #ifdef __ADSPBF60x__
  789. if (bfin_read32(DPM0_RESTORE0) != 0) {
  790. uint32_t reg = bfin_read_DMC0_CTL();
  791. reg &= ~0x8;
  792. bfin_write_DMC0_CTL(reg);
  793. while ((bfin_read_DMC0_STAT() & 0x8))
  794. continue;
  795. while (!(bfin_read_DMC0_STAT() & 0x1))
  796. continue;
  797. serial_putc('z');
  798. uint32_t *hibernate_magic = bfin_read32(DPM0_RESTORE4);
  799. SSYNC(); /* make sure memory controller is done */
  800. if (hibernate_magic[0] == 0xDEADBEEF) {
  801. serial_putc('c');
  802. SSYNC();
  803. bfin_write_EVT15(hibernate_magic[1]);
  804. bfin_write_IMASK(EVT_IVG15);
  805. __asm__ __volatile__ (
  806. /* load reti early to avoid anomaly 281 */
  807. "reti = %2;"
  808. /* clear hibernate magic */
  809. "[%0] = %1;"
  810. /* load stack pointer */
  811. "SP = [%0 + 8];"
  812. /* lower ourselves from reset ivg to ivg15 */
  813. "raise 15;"
  814. "nop;nop;nop;"
  815. "rti;"
  816. :
  817. : "p"(hibernate_magic),
  818. "d"(0x2000 /* jump.s 0 */),
  819. "d"(0xffa00000)
  820. );
  821. }
  822. }
  823. #else
  824. /* Are we coming out of hibernate (suspend to memory) ?
  825. * The memory layout is:
  826. * 0x0: hibernate magic for anomaly 307 (0xDEADBEEF)
  827. * 0x4: return address
  828. * 0x8: stack pointer
  829. *
  830. * SCKELOW is unreliable on older parts (anomaly 307)
  831. */
  832. if (ANOMALY_05000307 || vr_ctl & 0x8000) {
  833. uint32_t *hibernate_magic = 0;
  834. SSYNC();
  835. if (hibernate_magic[0] == 0xDEADBEEF) {
  836. serial_putc('c');
  837. bfin_write_EVT15(hibernate_magic[1]);
  838. bfin_write_IMASK(EVT_IVG15);
  839. __asm__ __volatile__ (
  840. /* load reti early to avoid anomaly 281 */
  841. "reti = %0;"
  842. /* clear hibernate magic */
  843. "[%0] = %1;"
  844. /* load stack pointer */
  845. "SP = [%0 + 8];"
  846. /* lower ourselves from reset ivg to ivg15 */
  847. "raise 15;"
  848. "rti;"
  849. :
  850. : "p"(hibernate_magic), "d"(0x2000 /* jump.s 0 */)
  851. );
  852. }
  853. serial_putc('d');
  854. }
  855. #endif
  856. serial_putc('e');
  857. }
  858. BOOTROM_CALLED_FUNC_ATTR
  859. void initcode(ADI_BOOT_DATA *bs)
  860. {
  861. ADI_BOOT_DATA bootstruct_scratch;
  862. /* Setup NMI handler before anything else */
  863. program_nmi_handler();
  864. serial_init();
  865. serial_putc('A');
  866. /* If the bootstruct is NULL, then it's because we're loading
  867. * dynamically and not via LDR (bootrom). So set the struct to
  868. * some scratch space.
  869. */
  870. if (!bs)
  871. bs = &bootstruct_scratch;
  872. serial_putc('B');
  873. bool put_into_srfs = maybe_self_refresh(bs);
  874. serial_putc('C');
  875. uint sdivB, divB, vcoB;
  876. program_early_devices(bs, &sdivB, &divB, &vcoB);
  877. serial_putc('D');
  878. u16 vr_ctl = program_clocks(bs, put_into_srfs);
  879. serial_putc('E');
  880. update_serial_clocks(bs, sdivB, divB, vcoB);
  881. serial_putc('F');
  882. program_memory_controller(bs, put_into_srfs);
  883. serial_putc('G');
  884. check_hibernation(bs, vr_ctl, put_into_srfs);
  885. serial_putc('H');
  886. program_async_controller(bs);
  887. #ifdef CONFIG_BFIN_BOOTROM_USES_EVT1
  888. serial_putc('I');
  889. /* Tell the bootrom where our entry point is so that it knows
  890. * where to jump to when finishing processing the LDR. This
  891. * allows us to avoid small jump blocks in the LDR, and also
  892. * works around anomaly 05000389 (init address in external
  893. * memory causes bootrom to trigger external addressing IVHW).
  894. */
  895. if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
  896. bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);
  897. #endif
  898. serial_putc('>');
  899. serial_putc('\n');
  900. serial_deinit();
  901. }