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-2008 Analog Devices Inc.
9 * Licensed under the GPL-2 or later.
13 #include <asm/blackfin.h>
14 #include <asm/mach-common/bits/bootrom.h>
15 #include <asm/mach-common/bits/ebiu.h>
16 #include <asm/mach-common/bits/pll.h>
17 #include <asm/mach-common/bits/uart.h>
19 #define BFIN_IN_INITCODE
22 __attribute__((always_inline))
23 static inline void serial_init(void)
26 # ifdef BFIN_BOOT_UART_USE_RTS
27 # define BFIN_UART_USE_RTS 1
29 # define BFIN_UART_USE_RTS 0
31 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
34 /* force RTS rather than relying on auto RTS */
35 bfin_write_UART1_MCR(bfin_read_UART1_MCR() | FCPOL);
37 /* Wait for the line to clear up. We cannot rely on UART
38 * registers as none of them reflect the status of the RSR.
39 * Instead, we'll sleep for ~10 bit times at 9600 baud.
40 * We can precalc things here by assuming boot values for
41 * PLL rather than loading registers and calculating.
42 * baud = SCLK / (16 ^ (1 - EDBO) * Divisor)
44 * Divisor = (SCLK / baud) / 16
45 * SCLK = baud * 16 * Divisor
46 * SCLK = (0x14 * CONFIG_CLKIN_HZ) / 5
47 * CCLK = (16 * Divisor * 5) * (9600 / 10)
48 * In reality, this will probably be just about 1 second delay,
49 * so assuming 9600 baud is OK (both as a very low and too high
50 * speed as this will buffer things enough).
52 #define _NUMBITS (10) /* how many bits to delay */
53 #define _LOWBAUD (9600) /* low baud rate */
54 #define _SCLK ((0x14 * CONFIG_CLKIN_HZ) / 5) /* SCLK based on PLL */
55 #define _DIVISOR ((_SCLK / _LOWBAUD) / 16) /* UART DLL/DLH */
56 #define _NUMINS (3) /* how many instructions in loop */
57 #define _CCLK (((16 * _DIVISOR * 5) * (_LOWBAUD / _NUMBITS)) / _NUMINS)
60 asm volatile("" : : : "memory");
64 if (BFIN_DEBUG_EARLY_SERIAL) {
65 int ucen = *pUART_GCTL & UCEN;
68 /* If the UART is off, that means we need to program
69 * the baud rate ourselves initially.
72 serial_early_set_baud(CONFIG_BAUDRATE);
76 __attribute__((always_inline))
77 static inline void serial_deinit(void)
80 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
81 /* clear forced RTS rather than relying on auto RTS */
82 bfin_write_UART1_MCR(bfin_read_UART1_MCR() & ~FCPOL);
87 __attribute__((always_inline))
88 static inline void serial_putc(char c)
90 if (!BFIN_DEBUG_EARLY_SERIAL)
98 while (!(*pUART_LSR & TEMT))
103 /* Max SCLK can be 133MHz ... dividing that by (2*4) gives
104 * us a freq of 16MHz for SPI which should generally be
105 * slow enough for the slow reads the bootrom uses.
107 #if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
108 ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
109 (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
110 # define BOOTROM_SUPPORTS_SPI_FAST_READ 1
112 # define BOOTROM_SUPPORTS_SPI_FAST_READ 0
114 #ifndef CONFIG_SPI_BAUD_INITBLOCK
115 # define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
118 # define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
121 /* PLL_DIV defines */
122 #ifndef CONFIG_PLL_DIV_VAL
123 # if (CONFIG_CCLK_DIV == 1)
124 # define CONFIG_CCLK_ACT_DIV CCLK_DIV1
125 # elif (CONFIG_CCLK_DIV == 2)
126 # define CONFIG_CCLK_ACT_DIV CCLK_DIV2
127 # elif (CONFIG_CCLK_DIV == 4)
128 # define CONFIG_CCLK_ACT_DIV CCLK_DIV4
129 # elif (CONFIG_CCLK_DIV == 8)
130 # define CONFIG_CCLK_ACT_DIV CCLK_DIV8
132 # define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
134 # define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
137 #ifndef CONFIG_PLL_LOCKCNT_VAL
138 # define CONFIG_PLL_LOCKCNT_VAL 0x0300
141 #ifndef CONFIG_PLL_CTL_VAL
142 # define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
145 #ifndef CONFIG_EBIU_RSTCTL_VAL
146 # define CONFIG_EBIU_RSTCTL_VAL 0 /* only MDDRENABLE is useful */
148 #if ((CONFIG_EBIU_RSTCTL_VAL & 0xFFFFFFC4) != 0)
149 # error invalid EBIU_RSTCTL value: must not set reserved bits
152 #ifndef CONFIG_EBIU_MBSCTL_VAL
153 # define CONFIG_EBIU_MBSCTL_VAL 0
156 #if defined(CONFIG_EBIU_DDRQUE_VAL) && ((CONFIG_EBIU_DDRQUE_VAL & 0xFFFF8000) != 0)
157 # error invalid EBIU_DDRQUE value: must not set reserved bits
160 /* Make sure our voltage value is sane so we don't blow up! */
161 #ifndef CONFIG_VR_CTL_VAL
162 # define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
163 # if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
164 # define CCLK_VLEV_120 400000000
165 # define CCLK_VLEV_125 533000000
166 # elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
167 # define CCLK_VLEV_120 401000000
168 # define CCLK_VLEV_125 401000000
169 # elif defined(__ADSPBF561__)
170 # define CCLK_VLEV_120 300000000
171 # define CCLK_VLEV_125 501000000
173 # if BFIN_CCLK < CCLK_VLEV_120
174 # define CONFIG_VR_CTL_VLEV VLEV_120
175 # elif BFIN_CCLK < CCLK_VLEV_125
176 # define CONFIG_VR_CTL_VLEV VLEV_125
178 # define CONFIG_VR_CTL_VLEV VLEV_130
180 # if defined(__ADSPBF52x__) /* TBD; use default */
181 # undef CONFIG_VR_CTL_VLEV
182 # define CONFIG_VR_CTL_VLEV VLEV_110
183 # elif defined(__ADSPBF54x__) /* TBD; use default */
184 # undef CONFIG_VR_CTL_VLEV
185 # define CONFIG_VR_CTL_VLEV VLEV_120
186 # elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
187 # undef CONFIG_VR_CTL_VLEV
188 # define CONFIG_VR_CTL_VLEV VLEV_125
191 # ifdef CONFIG_BFIN_MAC
192 # define CONFIG_VR_CTL_CLKBUF CLKBUFOE
194 # define CONFIG_VR_CTL_CLKBUF 0
197 # if defined(__ADSPBF52x__)
198 # define CONFIG_VR_CTL_FREQ FREQ_1000
200 # define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
203 # define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
206 BOOTROM_CALLED_FUNC_ATTR
207 void initcode(ADI_BOOT_DATA *bootstruct)
209 /* Save the clock pieces that are used in baud rate calculation */
210 unsigned int sdivB, divB, vcoB;
212 if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
213 sdivB = bfin_read_PLL_DIV() & 0xf;
214 vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
215 divB = serial_early_get_div();
218 #ifdef CONFIG_HW_WATCHDOG
219 # ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
220 # define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
222 /* Program the watchdog with an initial timeout of ~20 seconds.
223 * Hopefully that should be long enough to load the u-boot LDR
224 * (from wherever) and then the common u-boot code can take over.
225 * In bypass mode, the start.S would have already set a much lower
226 * timeout, so don't clobber that.
228 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
229 bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
230 bfin_write_WDOG_CTL(0);
236 /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
237 * fast read, so we need to slow down the SPI clock a lot more during
238 * boot. Once we switch over to u-boot's SPI flash driver, we'll
239 * increase the speed appropriately.
241 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
242 if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
243 bootstruct->dFlags |= BFLAG_FASTREAD;
244 bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
249 /* Disable all peripheral wakeups except for the PLL event. */
251 bfin_write_SIC_IWR0(1);
252 bfin_write_SIC_IWR1(0);
254 bfin_write_SIC_IWR2(0);
256 #elif defined(SICA_IWR0)
257 bfin_write_SICA_IWR0(1);
258 bfin_write_SICA_IWR1(0);
260 bfin_write_SIC_IWR(1);
263 /* With newer bootroms, we use the helper function to set up
264 * the memory controller. Older bootroms lacks such helpers
265 * so we do it ourselves.
267 if (BOOTROM_CAPS_SYSCONTROL) {
270 ADI_SYSCTRL_VALUES memory_settings;
271 memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
272 memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
273 memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
274 memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
275 syscontrol(SYSCTRL_WRITE | SYSCTRL_VRCTL | SYSCTRL_PLLCTL | SYSCTRL_PLLDIV | SYSCTRL_LOCKCNT |
276 (CONFIG_VR_CTL_VAL & FREQ_MASK ? SYSCTRL_INTVOLTAGE : SYSCTRL_EXTVOLTAGE), &memory_settings, NULL);
280 bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
284 /* Only reprogram when needed to avoid triggering unnecessary
285 * PLL relock sequences.
287 if (bfin_read_VR_CTL() != CONFIG_VR_CTL_VAL) {
289 bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
295 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
299 /* Only reprogram when needed to avoid triggering unnecessary
300 * PLL relock sequences.
302 if (bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
304 bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
309 /* Since we've changed the SCLK above, we may need to update
310 * the UART divisors (UART baud rates are based on SCLK).
311 * Do the division by hand as there are no native instructions
312 * for dividing which means we'd generate a libgcc reference.
314 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
315 unsigned int sdivR, vcoR;
316 sdivR = bfin_read_PLL_DIV() & 0xf;
317 vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
318 int dividend = sdivB * divB * vcoR;
319 int divisor = vcoB * sdivR;
320 unsigned int quotient;
321 for (quotient = 0; dividend > 0; ++quotient)
323 serial_early_put_div(quotient - ANOMALY_05000230);
328 /* Program the async banks controller. */
329 bfin_write_EBIU_AMBCTL0(CONFIG_EBIU_AMBCTL0_VAL);
330 bfin_write_EBIU_AMBCTL1(CONFIG_EBIU_AMBCTL1_VAL);
331 bfin_write_EBIU_AMGCTL(CONFIG_EBIU_AMGCTL_VAL);
334 /* Not all parts have these additional MMRs. */
335 bfin_write_EBIU_MBSCTL(CONFIG_EBIU_MBSCTL_VAL);
336 bfin_write_EBIU_MODE(CONFIG_EBIU_MODE_VAL);
337 bfin_write_EBIU_FCTL(CONFIG_EBIU_FCTL_VAL);
342 /* Program the external memory controller. */
344 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
345 bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
346 bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
347 bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
348 # ifdef CONFIG_EBIU_DDRCTL3_VAL
349 /* default is disable, so don't need to force this */
350 bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
353 bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
354 bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
355 bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
360 /* Restore all peripheral wakeups. */
362 bfin_write_SIC_IWR0(-1);
363 bfin_write_SIC_IWR1(-1);
365 bfin_write_SIC_IWR2(-1);
367 #elif defined(SICA_IWR0)
368 bfin_write_SICA_IWR0(-1);
369 bfin_write_SICA_IWR1(-1);
371 bfin_write_SIC_IWR(-1);