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.
7 * Copyright (c) 2004-2011 Analog Devices Inc.
9 * Licensed under the GPL-2 or later.
12 #define BFIN_IN_INITCODE
15 #include <asm/blackfin.h>
16 #include <asm/mach-common/bits/bootrom.h>
17 #include <asm/mach-common/bits/core.h>
19 #define BUG() while (1) { asm volatile("emuexcpt;"); }
24 #include <asm/mach-common/bits/ebiu.h>
25 #include <asm/mach-common/bits/pll.h>
26 #else /* __ADSPBF60x__ */
27 #include <asm/mach-common/bits/cgu.h>
29 #define CONFIG_BFIN_GET_DCLK_M \
30 ((CONFIG_CLKIN_HZ*CONFIG_VCO_MULT)/(CONFIG_DCLK_DIV*1000000))
32 #ifndef CONFIG_DMC_DDRCFG
33 #if ((CONFIG_BFIN_GET_DCLK_M != 125) && \
34 (CONFIG_BFIN_GET_DCLK_M != 133) && \
35 (CONFIG_BFIN_GET_DCLK_M != 150) && \
36 (CONFIG_BFIN_GET_DCLK_M != 166) && \
37 (CONFIG_BFIN_GET_DCLK_M != 200) && \
38 (CONFIG_BFIN_GET_DCLK_M != 225) && \
39 (CONFIG_BFIN_GET_DCLK_M != 250))
40 #error "DDR2 CLK must be in (125, 133, 150, 166, 200, 225, 250)MHz"
44 /* DMC control bits */
49 #define MEMINITDONE 0x4
53 #define DLLCALDONE 0x2000
54 #define PENDREF 0xF0000
55 #define PHYRDPHASE 0xF00000
56 #define PHYRDPHASE_OFFSET 20
58 /* DMC DLL control bits */
59 #define DLLCALRDCNT 0xFF
60 #define DATACYC_OFFSET 8
73 static struct ddr_config ddr_config_table[] = {
75 .ddr_clk = 125, /* 125MHz */
76 .dmc_ddrctl = 0x00000904,
77 .dmc_ddrcfg = 0x00000422,
78 .dmc_ddrtr0 = 0x20705212,
79 .dmc_ddrtr1 = 0x201003CF,
80 .dmc_ddrtr2 = 0x00320107,
81 .dmc_ddrmr = 0x00000422,
85 .ddr_clk = 133, /* 133MHz */
86 .dmc_ddrctl = 0x00000904,
87 .dmc_ddrcfg = 0x00000422,
88 .dmc_ddrtr0 = 0x20806313,
89 .dmc_ddrtr1 = 0x2013040D,
90 .dmc_ddrtr2 = 0x00320108,
91 .dmc_ddrmr = 0x00000632,
95 .ddr_clk = 150, /* 150MHz */
96 .dmc_ddrctl = 0x00000904,
97 .dmc_ddrcfg = 0x00000422,
98 .dmc_ddrtr0 = 0x20A07323,
99 .dmc_ddrtr1 = 0x20160492,
100 .dmc_ddrtr2 = 0x00320209,
101 .dmc_ddrmr = 0x00000632,
105 .ddr_clk = 166, /* 166MHz */
106 .dmc_ddrctl = 0x00000904,
107 .dmc_ddrcfg = 0x00000422,
108 .dmc_ddrtr0 = 0x20A07323,
109 .dmc_ddrtr1 = 0x2016050E,
110 .dmc_ddrtr2 = 0x00320209,
111 .dmc_ddrmr = 0x00000632,
115 .ddr_clk = 200, /* 200MHz */
116 .dmc_ddrctl = 0x00000904,
117 .dmc_ddrcfg = 0x00000422,
118 .dmc_ddrtr0 = 0x20a07323,
119 .dmc_ddrtr1 = 0x2016050f,
120 .dmc_ddrtr2 = 0x00320509,
121 .dmc_ddrmr = 0x00000632,
125 .ddr_clk = 225, /* 225MHz */
126 .dmc_ddrctl = 0x00000904,
127 .dmc_ddrcfg = 0x00000422,
128 .dmc_ddrtr0 = 0x20E0A424,
129 .dmc_ddrtr1 = 0x302006DB,
130 .dmc_ddrtr2 = 0x0032020D,
131 .dmc_ddrmr = 0x00000842,
135 .ddr_clk = 250, /* 250MHz */
136 .dmc_ddrctl = 0x00000904,
137 .dmc_ddrcfg = 0x00000422,
138 .dmc_ddrtr0 = 0x20E0A424,
139 .dmc_ddrtr1 = 0x3020079E,
140 .dmc_ddrtr2 = 0x0032050D,
141 .dmc_ddrmr = 0x00000842,
145 #endif /* __ADSPBF60x__ */
147 __attribute__((always_inline))
148 static inline void serial_init(void)
150 uint32_t uart_base = UART_BASE;
152 #if defined(__ADSPBF54x__) || defined(__ADSPBF60x__)
153 # ifdef BFIN_BOOT_UART_USE_RTS
154 # define BFIN_UART_USE_RTS 1
156 # define BFIN_UART_USE_RTS 0
158 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
161 /* force RTS rather than relying on auto RTS */
162 #if BFIN_UART_HW_VER < 4
163 bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) | FCPOL);
165 bfin_write32(&pUART->control, bfin_read32(&pUART->control) |
169 /* Wait for the line to clear up. We cannot rely on UART
170 * registers as none of them reflect the status of the RSR.
171 * Instead, we'll sleep for ~10 bit times at 9600 baud.
172 * We can precalc things here by assuming boot values for
173 * PLL rather than loading registers and calculating.
174 * baud = SCLK / (16 ^ (1 - EDBO) * Divisor)
176 * Divisor = (SCLK / baud) / 16
177 * SCLK = baud * 16 * Divisor
178 * SCLK = (0x14 * CONFIG_CLKIN_HZ) / 5
179 * CCLK = (16 * Divisor * 5) * (9600 / 10)
180 * In reality, this will probably be just about 1 second delay,
181 * so assuming 9600 baud is OK (both as a very low and too high
182 * speed as this will buffer things enough).
184 #define _NUMBITS (10) /* how many bits to delay */
185 #define _LOWBAUD (9600) /* low baud rate */
186 #define _SCLK ((0x14 * CONFIG_CLKIN_HZ) / 5) /* SCLK based on PLL */
187 #define _DIVISOR ((_SCLK / _LOWBAUD) / 16) /* UART DLL/DLH */
188 #define _NUMINS (3) /* how many instructions in loop */
189 #define _CCLK (((16 * _DIVISOR * 5) * (_LOWBAUD / _NUMBITS)) / _NUMINS)
192 asm volatile("" : : : "memory");
196 if (BFIN_DEBUG_EARLY_SERIAL) {
197 serial_early_init(uart_base);
198 serial_early_set_baud(uart_base, CONFIG_BAUDRATE);
202 __attribute__((always_inline))
203 static inline void serial_deinit(void)
205 #if defined(__ADSPBF54x__) || defined(__ADSPBF60x__)
206 uint32_t uart_base = UART_BASE;
208 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
209 /* clear forced RTS rather than relying on auto RTS */
210 #if BFIN_UART_HW_VER < 4
211 bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) & ~FCPOL);
213 bfin_write32(&pUART->control, bfin_read32(&pUART->control) &
220 __attribute__((always_inline))
221 static inline void serial_putc(char c)
223 uint32_t uart_base = UART_BASE;
225 if (!BFIN_DEBUG_EARLY_SERIAL)
231 bfin_write(&pUART->thr, c);
233 while (!(_lsr_read(pUART) & TEMT))
237 #include "initcode.h"
239 __attribute__((always_inline)) static inline void
240 program_nmi_handler(void)
244 /* Older bootroms don't create a dummy NMI handler,
245 * so make one ourselves ASAP in case it fires.
247 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS && !ANOMALY_05000219)
251 "%0 = RETS;" /* Save current RETS */
252 "CALL 1f;" /* Figure out current PC */
253 "RTN;" /* The simple NMI handler */
255 "%1 = RETS;" /* Load addr of NMI handler */
256 "RETS = %0;" /* Restore RETS */
257 "[%2] = %1;" /* Write NMI handler */
258 : "=d"(tmp1), "=d"(tmp2)
263 /* Max SCLK can be 133MHz ... dividing that by (2*4) gives
264 * us a freq of 16MHz for SPI which should generally be
265 * slow enough for the slow reads the bootrom uses.
267 #if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
268 ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
269 (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
270 # define BOOTROM_SUPPORTS_SPI_FAST_READ 1
272 # define BOOTROM_SUPPORTS_SPI_FAST_READ 0
274 #ifndef CONFIG_SPI_BAUD_INITBLOCK
275 # define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
278 # define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
283 #ifndef CONFIG_CGU_CTL_VAL
284 # define CONFIG_CGU_CTL_VAL ((CONFIG_VCO_MULT << 8) | CONFIG_CLKIN_HALF)
287 #ifndef CONFIG_CGU_DIV_VAL
288 # define CONFIG_CGU_DIV_VAL \
289 ((CONFIG_CCLK_DIV << CSEL_P) | \
290 (CONFIG_SCLK0_DIV << S0SEL_P) | \
291 (CONFIG_SCLK_DIV << SYSSEL_P) | \
292 (CONFIG_SCLK1_DIV << S1SEL_P) | \
293 (CONFIG_DCLK_DIV << DSEL_P) | \
294 (CONFIG_OCLK_DIV << OSEL_P))
297 #else /* __ADSPBF60x__ */
299 /* PLL_DIV defines */
300 #ifndef CONFIG_PLL_DIV_VAL
301 # if (CONFIG_CCLK_DIV == 1)
302 # define CONFIG_CCLK_ACT_DIV CCLK_DIV1
303 # elif (CONFIG_CCLK_DIV == 2)
304 # define CONFIG_CCLK_ACT_DIV CCLK_DIV2
305 # elif (CONFIG_CCLK_DIV == 4)
306 # define CONFIG_CCLK_ACT_DIV CCLK_DIV4
307 # elif (CONFIG_CCLK_DIV == 8)
308 # define CONFIG_CCLK_ACT_DIV CCLK_DIV8
310 # define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
312 # define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
315 #ifndef CONFIG_PLL_LOCKCNT_VAL
316 # define CONFIG_PLL_LOCKCNT_VAL 0x0300
319 #ifndef CONFIG_PLL_CTL_VAL
320 # define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
323 /* Make sure our voltage value is sane so we don't blow up! */
324 #ifndef CONFIG_VR_CTL_VAL
325 # define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
326 # if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
327 # define CCLK_VLEV_120 400000000
328 # define CCLK_VLEV_125 533000000
329 # elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
330 # define CCLK_VLEV_120 401000000
331 # define CCLK_VLEV_125 401000000
332 # elif defined(__ADSPBF561__)
333 # define CCLK_VLEV_120 300000000
334 # define CCLK_VLEV_125 501000000
336 # if BFIN_CCLK < CCLK_VLEV_120
337 # define CONFIG_VR_CTL_VLEV VLEV_120
338 # elif BFIN_CCLK < CCLK_VLEV_125
339 # define CONFIG_VR_CTL_VLEV VLEV_125
341 # define CONFIG_VR_CTL_VLEV VLEV_130
343 # if defined(__ADSPBF52x__) /* TBD; use default */
344 # undef CONFIG_VR_CTL_VLEV
345 # define CONFIG_VR_CTL_VLEV VLEV_110
346 # elif defined(__ADSPBF54x__) /* TBD; use default */
347 # undef CONFIG_VR_CTL_VLEV
348 # define CONFIG_VR_CTL_VLEV VLEV_120
349 # elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
350 # undef CONFIG_VR_CTL_VLEV
351 # define CONFIG_VR_CTL_VLEV VLEV_125
354 # ifdef CONFIG_BFIN_MAC
355 # define CONFIG_VR_CTL_CLKBUF CLKBUFOE
357 # define CONFIG_VR_CTL_CLKBUF 0
360 # if defined(__ADSPBF52x__)
361 # define CONFIG_VR_CTL_FREQ FREQ_1000
363 # define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
366 # define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
369 /* some parts do not have an on-chip voltage regulator */
370 #if defined(__ADSPBF51x__)
371 # define CONFIG_HAS_VR 0
372 # undef CONFIG_VR_CTL_VAL
373 # define CONFIG_VR_CTL_VAL 0
375 # define CONFIG_HAS_VR 1
380 /* Blackfin with SDRAM */
381 #ifndef CONFIG_EBIU_SDBCTL_VAL
382 # if CONFIG_MEM_SIZE == 16
383 # define CONFIG_EBSZ_VAL EBSZ_16
384 # elif CONFIG_MEM_SIZE == 32
385 # define CONFIG_EBSZ_VAL EBSZ_32
386 # elif CONFIG_MEM_SIZE == 64
387 # define CONFIG_EBSZ_VAL EBSZ_64
388 # elif CONFIG_MEM_SIZE == 128
389 # define CONFIG_EBSZ_VAL EBSZ_128
390 # elif CONFIG_MEM_SIZE == 256
391 # define CONFIG_EBSZ_VAL EBSZ_256
392 # elif CONFIG_MEM_SIZE == 512
393 # define CONFIG_EBSZ_VAL EBSZ_512
395 # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
397 # if CONFIG_MEM_ADD_WDTH == 8
398 # define CONFIG_EBCAW_VAL EBCAW_8
399 # elif CONFIG_MEM_ADD_WDTH == 9
400 # define CONFIG_EBCAW_VAL EBCAW_9
401 # elif CONFIG_MEM_ADD_WDTH == 10
402 # define CONFIG_EBCAW_VAL EBCAW_10
403 # elif CONFIG_MEM_ADD_WDTH == 11
404 # define CONFIG_EBCAW_VAL EBCAW_11
406 # error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
408 # define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
413 /* Conflicting Column Address Widths Causes SDRAM Errors:
414 * EB2CAW and EB3CAW must be the same
417 # if ((CONFIG_EBIU_SDBCTL_VAL & 0x30000000) >> 8) != (CONFIG_EBIU_SDBCTL_VAL & 0x00300000)
418 # error "Anomaly 05000362: EB2CAW and EB3CAW must be the same"
422 #endif /* __ADSPBF60x__ */
424 __attribute__((always_inline)) static inline void
425 program_early_devices(ADI_BOOT_DATA *bs, uint *sdivB, uint *divB, uint *vcoB)
429 /* Save the clock pieces that are used in baud rate calculation */
430 if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
433 *sdivB = bfin_read_CGU_DIV();
434 *sdivB = ((*sdivB >> 8) & 0x1f) * ((*sdivB >> 5) & 0x7);
435 *vcoB = (bfin_read_CGU_CTL() >> 8) & 0x7f;
437 *sdivB = bfin_read_PLL_DIV() & 0xf;
438 *vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
440 *divB = serial_early_get_div();
446 #ifdef CONFIG_HW_WATCHDOG
447 # ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
448 # define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
450 /* Program the watchdog with an initial timeout of ~20 seconds.
451 * Hopefully that should be long enough to load the u-boot LDR
452 * (from wherever) and then the common u-boot code can take over.
453 * In bypass mode, the start.S would have already set a much lower
454 * timeout, so don't clobber that.
456 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
459 bfin_write_SEC_GCTL(0x2);
461 bfin_write_SEC_FCTL(0xc1);
462 bfin_write_SEC_SCTL(2, bfin_read_SEC_SCTL(2) | 0x6);
464 bfin_write_SEC_CCTL(0x2);
466 bfin_write_SEC_GCTL(0x1);
467 bfin_write_SEC_CCTL(0x1);
469 bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
470 #if CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_UART
471 bfin_write_WDOG_CTL(0);
479 /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
480 * fast read, so we need to slow down the SPI clock a lot more during
481 * boot. Once we switch over to u-boot's SPI flash driver, we'll
482 * increase the speed appropriately.
485 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
487 if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
488 bs->dFlags |= BFLAG_FASTREAD;
489 bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
497 __attribute__((always_inline)) static inline bool
498 maybe_self_refresh(ADI_BOOT_DATA *bs)
502 if (!CONFIG_MEM_SIZE)
506 /* resume from hibernate, return false let ddr initialize */
507 if ((bfin_read32(DPM0_STAT) & 0xF0) == 0x50) {
512 #else /* __ADSPBF60x__ */
514 /* If external memory is enabled, put it into self refresh first. */
515 #if defined(EBIU_RSTCTL)
516 if (bfin_read_EBIU_RSTCTL() & DDR_SRESET) {
518 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | SRREQ);
521 #elif defined(EBIU_SDGCTL)
522 if (bfin_read_EBIU_SDBCTL() & EBE) {
524 bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() | SRFS);
529 #endif /* __ADSPBF60x__ */
535 __attribute__((always_inline)) static inline u16
536 program_clocks(ADI_BOOT_DATA *bs, bool put_into_srfs)
543 if (bfin_read_DMC0_STAT() & MEMINITDONE) {
544 bfin_write_DMC0_CTL(bfin_read_DMC0_CTL() | SRREQ);
546 while (!(bfin_read_DMC0_STAT() & SRACK))
550 /* Don't set the same value of MSEL and DF to CGU_CTL */
551 if ((bfin_read_CGU_CTL() & (MSEL_MASK | DF_MASK))
552 != CONFIG_CGU_CTL_VAL) {
553 bfin_write_CGU_DIV(CONFIG_CGU_DIV_VAL);
554 bfin_write_CGU_CTL(CONFIG_CGU_CTL_VAL);
555 while ((bfin_read_CGU_STAT() & (CLKSALGN | PLLBP)) ||
556 !(bfin_read_CGU_STAT() & PLLLK))
560 bfin_write_CGU_DIV(CONFIG_CGU_DIV_VAL | UPDT);
561 while (bfin_read_CGU_STAT() & CLKSALGN)
564 if (bfin_read_DMC0_STAT() & MEMINITDONE) {
565 bfin_write_DMC0_CTL(bfin_read_DMC0_CTL() & ~SRREQ);
567 while (bfin_read_DMC0_STAT() & SRACK)
571 #else /* __ADSPBF60x__ */
573 vr_ctl = bfin_read_VR_CTL();
577 /* If we're entering self refresh, make sure it has happened. */
579 #if defined(EBIU_RSTCTL)
580 while (!(bfin_read_EBIU_RSTCTL() & SRACK))
582 #elif defined(EBIU_SDGCTL)
583 while (!(bfin_read_EBIU_SDSTAT() & SDSRA))
591 /* With newer bootroms, we use the helper function to set up
592 * the memory controller. Older bootroms lacks such helpers
593 * so we do it ourselves.
595 if (!ANOMALY_05000386) {
598 /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
599 ADI_SYSCTRL_VALUES memory_settings;
600 uint32_t actions = SYSCTRL_WRITE | SYSCTRL_PLLCTL | SYSCTRL_LOCKCNT;
601 if (!ANOMALY_05000440)
602 actions |= SYSCTRL_PLLDIV;
604 actions |= SYSCTRL_VRCTL;
605 if (CONFIG_VR_CTL_VAL & FREQ_MASK)
606 actions |= SYSCTRL_INTVOLTAGE;
608 actions |= SYSCTRL_EXTVOLTAGE;
609 memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
611 actions |= SYSCTRL_EXTVOLTAGE;
612 memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
613 memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
614 memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
616 bfin_write_SIC_IWR1(0);
619 bfrom_SysControl(actions, &memory_settings, NULL);
621 if (ANOMALY_05000440)
622 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
624 bfin_write_SIC_IWR1(-1);
627 bfin_write_SICA_IWR0(-1);
628 bfin_write_SICA_IWR1(-1);
634 /* Disable all peripheral wakeups except for the PLL event. */
636 bfin_write_SIC_IWR0(1);
637 bfin_write_SIC_IWR1(0);
639 bfin_write_SIC_IWR2(0);
641 #elif defined(SICA_IWR0)
642 bfin_write_SICA_IWR0(1);
643 bfin_write_SICA_IWR1(0);
644 #elif defined(SIC_IWR)
645 bfin_write_SIC_IWR(1);
650 /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
651 bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
655 /* Only reprogram when needed to avoid triggering unnecessary
656 * PLL relock sequences.
658 if (vr_ctl != CONFIG_VR_CTL_VAL) {
660 bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
667 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
671 /* Only reprogram when needed to avoid triggering unnecessary
672 * PLL relock sequences.
674 if (ANOMALY_05000242 || bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
676 bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
683 /* Restore all peripheral wakeups. */
685 bfin_write_SIC_IWR0(-1);
686 bfin_write_SIC_IWR1(-1);
688 bfin_write_SIC_IWR2(-1);
690 #elif defined(SICA_IWR0)
691 bfin_write_SICA_IWR0(-1);
692 bfin_write_SICA_IWR1(-1);
693 #elif defined(SIC_IWR)
694 bfin_write_SIC_IWR(-1);
700 #endif /* __ADSPBF60x__ */
707 __attribute__((always_inline)) static inline void
708 update_serial_clocks(ADI_BOOT_DATA *bs, uint sdivB, uint divB, uint vcoB)
710 /* Since we've changed the SCLK above, we may need to update
711 * the UART divisors (UART baud rates are based on SCLK).
712 * Do the division by hand as there are no native instructions
713 * for dividing which means we'd generate a libgcc reference.
715 unsigned int sdivR, vcoR;
716 unsigned int dividend = sdivB * divB * vcoR;
717 unsigned int divisor = vcoB * sdivR;
718 unsigned int quotient;
723 sdivR = bfin_read_CGU_DIV();
724 sdivR = ((sdivR >> 8) & 0x1f) * ((sdivR >> 5) & 0x7);
725 vcoR = (bfin_read_CGU_CTL() >> 8) & 0x7f;
727 sdivR = bfin_read_PLL_DIV() & 0xf;
728 vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
730 quotient = early_division(dividend, divisor);
731 serial_early_put_div(quotient - ANOMALY_05000230);
735 __attribute__((always_inline)) static inline void
736 program_memory_controller(ADI_BOOT_DATA *bs, bool put_into_srfs)
740 if (!CONFIG_MEM_SIZE)
750 if (CONFIG_BFIN_GET_DCLK_M == 125)
752 else if (CONFIG_BFIN_GET_DCLK_M == 133)
754 else if (CONFIG_BFIN_GET_DCLK_M == 150)
756 else if (CONFIG_BFIN_GET_DCLK_M == 166)
758 else if (CONFIG_BFIN_GET_DCLK_M == 200)
760 else if (CONFIG_BFIN_GET_DCLK_M == 225)
762 else if (CONFIG_BFIN_GET_DCLK_M == 250)
766 for (i = 0; i < ARRAY_SIZE(ddr_config_table); i++)
767 if (CONFIG_BFIN_GET_DCLK_M == ddr_config_table[i].ddr_clk)
771 #ifndef CONFIG_DMC_DDRCFG
772 bfin_write_DMC0_CFG(ddr_config_table[i].dmc_ddrcfg);
774 bfin_write_DMC0_CFG(CONFIG_DMC_DDRCFG);
776 #ifndef CONFIG_DMC_DDRTR0
777 bfin_write_DMC0_TR0(ddr_config_table[i].dmc_ddrtr0);
779 bfin_write_DMC0_TR0(CONFIG_DMC_DDRTR0);
781 #ifndef CONFIG_DMC_DDRTR1
782 bfin_write_DMC0_TR1(ddr_config_table[i].dmc_ddrtr1);
784 bfin_write_DMC0_TR1(CONFIG_DMC_DDRTR1);
786 #ifndef CONFIG_DMC_DDRTR2
787 bfin_write_DMC0_TR2(ddr_config_table[i].dmc_ddrtr2);
789 bfin_write_DMC0_TR2(CONFIG_DMC_DDRTR2);
791 #ifndef CONFIG_DMC_DDRMR
792 bfin_write_DMC0_MR(ddr_config_table[i].dmc_ddrmr);
794 bfin_write_DMC0_MR(CONFIG_DMC_DDRMR);
796 #ifndef CONFIG_DMC_DDREMR1
797 bfin_write_DMC0_EMR1(ddr_config_table[i].dmc_ddrmr1);
799 bfin_write_DMC0_EMR1(CONFIG_DMC_DDREMR1);
801 #ifndef CONFIG_DMC_DDRCTL
802 bfin_write_DMC0_CTL(ddr_config_table[i].dmc_ddrctl);
804 bfin_write_DMC0_CTL(CONFIG_DMC_DDRCTL);
808 while (!(bfin_read_DMC0_STAT() & MEMINITDONE))
811 dlldatacycle = (bfin_read_DMC0_STAT() & PHYRDPHASE) >>
813 dll_ctl = bfin_read_DMC0_DLLCTL();
815 bfin_write_DMC0_DLLCTL(dll_ctl | (dlldatacycle << DATACYC_OFFSET));
818 while (!(bfin_read_DMC0_STAT() & DLLCALDONE))
822 #else /* __ADSPBF60x__ */
824 /* Program the external memory controller before we come out of
825 * self-refresh. This only works with our SDRAM controller.
828 # ifdef CONFIG_EBIU_SDRRC_VAL
829 bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
831 # ifdef CONFIG_EBIU_SDBCTL_VAL
832 bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
834 # ifdef CONFIG_EBIU_SDGCTL_VAL
835 bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
841 /* Now that we've reprogrammed, take things out of self refresh. */
843 #if defined(EBIU_RSTCTL)
844 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() & ~(SRREQ));
845 #elif defined(EBIU_SDGCTL)
846 bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() & ~(SRFS));
851 /* Our DDR controller sucks and cannot be programmed while in
852 * self-refresh. So we have to pull it out before programming.
855 # ifdef CONFIG_EBIU_RSTCTL_VAL
856 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
858 # ifdef CONFIG_EBIU_DDRCTL0_VAL
859 bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
861 # ifdef CONFIG_EBIU_DDRCTL1_VAL
862 bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
864 # ifdef CONFIG_EBIU_DDRCTL2_VAL
865 bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
867 # ifdef CONFIG_EBIU_DDRCTL3_VAL
868 /* default is disable, so don't need to force this */
869 bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
871 # ifdef CONFIG_EBIU_DDRQUE_VAL
872 bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE() | CONFIG_EBIU_DDRQUE_VAL);
876 #endif /* __ADSPBF60x__ */
880 __attribute__((always_inline)) static inline void
881 check_hibernation(ADI_BOOT_DATA *bs, u16 vr_ctl, bool put_into_srfs)
885 if (!CONFIG_MEM_SIZE)
890 if (bfin_read32(DPM0_RESTORE0) != 0) {
891 uint32_t reg = bfin_read_DMC0_CTL();
893 bfin_write_DMC0_CTL(reg);
895 while ((bfin_read_DMC0_STAT() & 0x8))
897 while (!(bfin_read_DMC0_STAT() & 0x1))
901 uint32_t *hibernate_magic = bfin_read32(DPM0_RESTORE4);
902 SSYNC(); /* make sure memory controller is done */
903 if (hibernate_magic[0] == 0xDEADBEEF) {
906 bfin_write_EVT15(hibernate_magic[1]);
907 bfin_write_IMASK(EVT_IVG15);
908 __asm__ __volatile__ (
909 /* load reti early to avoid anomaly 281 */
911 /* clear hibernate magic */
913 /* load stack pointer */
915 /* lower ourselves from reset ivg to ivg15 */
920 : "p"(hibernate_magic),
921 "d"(0x2000 /* jump.s 0 */),
929 /* Are we coming out of hibernate (suspend to memory) ?
930 * The memory layout is:
931 * 0x0: hibernate magic for anomaly 307 (0xDEADBEEF)
932 * 0x4: return address
935 * SCKELOW is unreliable on older parts (anomaly 307)
937 if (ANOMALY_05000307 || vr_ctl & 0x8000) {
938 uint32_t *hibernate_magic = 0;
941 if (hibernate_magic[0] == 0xDEADBEEF) {
943 bfin_write_EVT15(hibernate_magic[1]);
944 bfin_write_IMASK(EVT_IVG15);
945 __asm__ __volatile__ (
946 /* load reti early to avoid anomaly 281 */
948 /* clear hibernate magic */
950 /* load stack pointer */
952 /* lower ourselves from reset ivg to ivg15 */
956 : "p"(hibernate_magic), "d"(0x2000 /* jump.s 0 */)
966 BOOTROM_CALLED_FUNC_ATTR
967 void initcode(ADI_BOOT_DATA *bs)
969 ADI_BOOT_DATA bootstruct_scratch;
971 /* Setup NMI handler before anything else */
972 program_nmi_handler();
978 /* If the bootstruct is NULL, then it's because we're loading
979 * dynamically and not via LDR (bootrom). So set the struct to
980 * some scratch space.
983 bs = &bootstruct_scratch;
986 bool put_into_srfs = maybe_self_refresh(bs);
989 uint sdivB, divB, vcoB;
990 program_early_devices(bs, &sdivB, &divB, &vcoB);
993 u16 vr_ctl = program_clocks(bs, put_into_srfs);
996 update_serial_clocks(bs, sdivB, divB, vcoB);
999 program_memory_controller(bs, put_into_srfs);
1002 check_hibernation(bs, vr_ctl, put_into_srfs);
1005 program_async_controller(bs);
1007 #ifdef CONFIG_BFIN_BOOTROM_USES_EVT1
1009 /* Tell the bootrom where our entry point is so that it knows
1010 * where to jump to when finishing processing the LDR. This
1011 * allows us to avoid small jump blocks in the LDR, and also
1012 * works around anomaly 05000389 (init address in external
1013 * memory causes bootrom to trigger external addressing IVHW).
1015 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
1016 bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);