blob: 704b791bcbfafcd6ad28f4ed349f603065f7190b [file] [log] [blame]
Mike Frysinger9171fc82008-03-30 15:46:13 -04001/*
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-2008 Analog Devices Inc.
8 *
9 * Licensed under the GPL-2 or later.
10 */
11
12#include <config.h>
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>
18
19#define BFIN_IN_INITCODE
20#include "serial.h"
21
22__attribute__((always_inline))
23static inline uint32_t serial_init(void)
24{
25#ifdef __ADSPBF54x__
26# ifdef BFIN_BOOT_UART_USE_RTS
27# define BFIN_UART_USE_RTS 1
28# else
29# define BFIN_UART_USE_RTS 0
30# endif
31 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
32 size_t i;
33
34 /* force RTS rather than relying on auto RTS */
35 bfin_write_UART1_MCR(bfin_read_UART1_MCR() | FCPOL);
36
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)
43 * EDB0 = 0
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).
51 */
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)
58 i = _CCLK;
59 while (i--)
60 asm volatile("" : : : "memory");
61 }
62#endif
63
Mike Frysingeree1d2002008-10-20 21:08:54 -040064 uint32_t old_baud;
65 if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART)
66 old_baud = serial_early_get_baud();
67 else
68 old_baud = CONFIG_BAUDRATE;
Mike Frysinger9171fc82008-03-30 15:46:13 -040069
70 if (BFIN_DEBUG_EARLY_SERIAL) {
71 serial_early_init();
72
73 /* If the UART is off, that means we need to program
74 * the baud rate ourselves initially.
75 */
76 if (!old_baud) {
77 old_baud = CONFIG_BAUDRATE;
78 serial_early_set_baud(CONFIG_BAUDRATE);
79 }
80 }
81
82 return old_baud;
83}
84
85__attribute__((always_inline))
86static inline void serial_deinit(void)
87{
88#ifdef __ADSPBF54x__
89 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
90 /* clear forced RTS rather than relying on auto RTS */
91 bfin_write_UART1_MCR(bfin_read_UART1_MCR() & ~FCPOL);
92 }
93#endif
94}
95
96/* We need to reset the baud rate when we have early debug turned on
97 * or when we are booting over the UART.
98 * XXX: we should fix this to calc the old baud and restore it rather
99 * than hardcoding it via CONFIG_LDR_LOAD_BAUD ... but we have
100 * to figure out how to avoid the division in the baud calc ...
101 */
102__attribute__((always_inline))
103static inline void serial_reset_baud(uint32_t baud)
104{
105 if (!BFIN_DEBUG_EARLY_SERIAL && CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_UART)
106 return;
107
108#ifndef CONFIG_LDR_LOAD_BAUD
109# define CONFIG_LDR_LOAD_BAUD 115200
110#endif
111
112 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_BYPASS)
113 serial_early_set_baud(baud);
114 else if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART)
115 serial_early_set_baud(CONFIG_LDR_LOAD_BAUD);
116 else
117 serial_early_set_baud(CONFIG_BAUDRATE);
118}
119
120__attribute__((always_inline))
121static inline void serial_putc(char c)
122{
123 if (!BFIN_DEBUG_EARLY_SERIAL)
124 return;
125
126 if (c == '\n')
127 *pUART_THR = '\r';
128
129 *pUART_THR = c;
130
131 while (!(*pUART_LSR & TEMT))
132 continue;
133}
134
135
Mike Frysinger97f265f2008-12-09 17:21:08 -0500136/* Max SCLK can be 133MHz ... dividing that by (2*4) gives
137 * us a freq of 16MHz for SPI which should generally be
Mike Frysinger9171fc82008-03-30 15:46:13 -0400138 * slow enough for the slow reads the bootrom uses.
139 */
Mike Frysinger97f265f2008-12-09 17:21:08 -0500140#if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
141 ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
142 (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
143# define BOOTROM_SUPPORTS_SPI_FAST_READ 1
144#else
145# define BOOTROM_SUPPORTS_SPI_FAST_READ 0
146#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400147#ifndef CONFIG_SPI_BAUD_INITBLOCK
Mike Frysinger97f265f2008-12-09 17:21:08 -0500148# define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
149#endif
150#ifdef SPI0_BAUD
151# define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
Mike Frysinger9171fc82008-03-30 15:46:13 -0400152#endif
153
154/* PLL_DIV defines */
155#ifndef CONFIG_PLL_DIV_VAL
156# if (CONFIG_CCLK_DIV == 1)
157# define CONFIG_CCLK_ACT_DIV CCLK_DIV1
158# elif (CONFIG_CCLK_DIV == 2)
159# define CONFIG_CCLK_ACT_DIV CCLK_DIV2
160# elif (CONFIG_CCLK_DIV == 4)
161# define CONFIG_CCLK_ACT_DIV CCLK_DIV4
162# elif (CONFIG_CCLK_DIV == 8)
163# define CONFIG_CCLK_ACT_DIV CCLK_DIV8
164# else
165# define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
166# endif
167# define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
168#endif
169
170#ifndef CONFIG_PLL_LOCKCNT_VAL
171# define CONFIG_PLL_LOCKCNT_VAL 0x0300
172#endif
173
174#ifndef CONFIG_PLL_CTL_VAL
Mike Frysinger4f6a3132008-06-01 01:26:29 -0400175# define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400176#endif
177
178#ifndef CONFIG_EBIU_RSTCTL_VAL
179# define CONFIG_EBIU_RSTCTL_VAL 0 /* only MDDRENABLE is useful */
180#endif
Mike Frysinger67619982008-10-11 21:46:52 -0400181#if ((CONFIG_EBIU_RSTCTL_VAL & 0xFFFFFFC4) != 0)
182# error invalid EBIU_RSTCTL value: must not set reserved bits
183#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400184
185#ifndef CONFIG_EBIU_MBSCTL_VAL
186# define CONFIG_EBIU_MBSCTL_VAL 0
187#endif
188
Mike Frysinger67619982008-10-11 21:46:52 -0400189#if defined(CONFIG_EBIU_DDRQUE_VAL) && ((CONFIG_EBIU_DDRQUE_VAL & 0xFFFF8000) != 0)
190# error invalid EBIU_DDRQUE value: must not set reserved bits
191#endif
192
Mike Frysinger9171fc82008-03-30 15:46:13 -0400193/* Make sure our voltage value is sane so we don't blow up! */
194#ifndef CONFIG_VR_CTL_VAL
195# define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
196# if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
197# define CCLK_VLEV_120 400000000
198# define CCLK_VLEV_125 533000000
199# elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
200# define CCLK_VLEV_120 401000000
201# define CCLK_VLEV_125 401000000
202# elif defined(__ADSPBF561__)
203# define CCLK_VLEV_120 300000000
204# define CCLK_VLEV_125 501000000
205# endif
206# if BFIN_CCLK < CCLK_VLEV_120
207# define CONFIG_VR_CTL_VLEV VLEV_120
208# elif BFIN_CCLK < CCLK_VLEV_125
209# define CONFIG_VR_CTL_VLEV VLEV_125
210# else
211# define CONFIG_VR_CTL_VLEV VLEV_130
212# endif
213# if defined(__ADSPBF52x__) /* TBD; use default */
214# undef CONFIG_VR_CTL_VLEV
215# define CONFIG_VR_CTL_VLEV VLEV_110
216# elif defined(__ADSPBF54x__) /* TBD; use default */
217# undef CONFIG_VR_CTL_VLEV
218# define CONFIG_VR_CTL_VLEV VLEV_120
Mike Frysinger622a8dc2008-10-11 21:54:00 -0400219# elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
220# undef CONFIG_VR_CTL_VLEV
221# define CONFIG_VR_CTL_VLEV VLEV_125
Mike Frysinger9171fc82008-03-30 15:46:13 -0400222# endif
223
224# ifdef CONFIG_BFIN_MAC
225# define CONFIG_VR_CTL_CLKBUF CLKBUFOE
226# else
227# define CONFIG_VR_CTL_CLKBUF 0
228# endif
229
230# if defined(__ADSPBF52x__)
231# define CONFIG_VR_CTL_FREQ FREQ_1000
232# else
233# define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
234# endif
235
236# define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
237#endif
238
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400239BOOTROM_CALLED_FUNC_ATTR
Mike Frysinger9171fc82008-03-30 15:46:13 -0400240void initcode(ADI_BOOT_DATA *bootstruct)
241{
242 uint32_t old_baud = serial_init();
243
244#ifdef CONFIG_HW_WATCHDOG
245# ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
246# define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
247# endif
248 /* Program the watchdog with an initial timeout of ~20 seconds.
249 * Hopefully that should be long enough to load the u-boot LDR
250 * (from wherever) and then the common u-boot code can take over.
251 * In bypass mode, the start.S would have already set a much lower
252 * timeout, so don't clobber that.
253 */
254 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
255 bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
256 bfin_write_WDOG_CTL(0);
257 }
258#endif
259
260 serial_putc('S');
261
262 /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
263 * fast read, so we need to slow down the SPI clock a lot more during
264 * boot. Once we switch over to u-boot's SPI flash driver, we'll
265 * increase the speed appropriately.
266 */
Mike Frysinger97f265f2008-12-09 17:21:08 -0500267 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
268 if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
269 bootstruct->dFlags |= BFLAG_FASTREAD;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400270 bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
Mike Frysinger97f265f2008-12-09 17:21:08 -0500271 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400272
273 serial_putc('B');
274
275 /* Disable all peripheral wakeups except for the PLL event. */
276#ifdef SIC_IWR0
277 bfin_write_SIC_IWR0(1);
278 bfin_write_SIC_IWR1(0);
279# ifdef SIC_IWR2
280 bfin_write_SIC_IWR2(0);
281# endif
282#elif defined(SICA_IWR0)
283 bfin_write_SICA_IWR0(1);
284 bfin_write_SICA_IWR1(0);
285#else
286 bfin_write_SIC_IWR(1);
287#endif
288
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400289 /* With newer bootroms, we use the helper function to set up
290 * the memory controller. Older bootroms lacks such helpers
291 * so we do it ourselves.
Mike Frysinger9171fc82008-03-30 15:46:13 -0400292 */
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400293 if (BOOTROM_CAPS_SYSCONTROL) {
294 serial_putc('S');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400295
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400296 ADI_SYSCTRL_VALUES memory_settings;
297 memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
298 memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
299 memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
300 memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
301 syscontrol(SYSCTRL_WRITE | SYSCTRL_VRCTL | SYSCTRL_PLLCTL | SYSCTRL_PLLDIV | SYSCTRL_LOCKCNT |
302 (CONFIG_VR_CTL_VAL & FREQ_MASK ? SYSCTRL_INTVOLTAGE : SYSCTRL_EXTVOLTAGE), &memory_settings, NULL);
303 } else {
304 serial_putc('L');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400305
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400306 bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400307
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400308 serial_putc('A');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400309
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400310 /* Only reprogram when needed to avoid triggering unnecessary
311 * PLL relock sequences.
312 */
313 if (bfin_read_VR_CTL() != CONFIG_VR_CTL_VAL) {
314 serial_putc('!');
315 bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
316 asm("idle;");
317 }
318
319 serial_putc('C');
320
321 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
322
323 serial_putc('K');
324
325 /* Only reprogram when needed to avoid triggering unnecessary
326 * PLL relock sequences.
327 */
328 if (bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
329 serial_putc('!');
330 bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
331 asm("idle;");
332 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400333 }
334
335 /* Since we've changed the SCLK above, we may need to update
336 * the UART divisors (UART baud rates are based on SCLK).
337 */
338 serial_reset_baud(old_baud);
339
340 serial_putc('F');
341
342 /* Program the async banks controller. */
343 bfin_write_EBIU_AMBCTL0(CONFIG_EBIU_AMBCTL0_VAL);
344 bfin_write_EBIU_AMBCTL1(CONFIG_EBIU_AMBCTL1_VAL);
345 bfin_write_EBIU_AMGCTL(CONFIG_EBIU_AMGCTL_VAL);
346
347#ifdef EBIU_MODE
348 /* Not all parts have these additional MMRs. */
349 bfin_write_EBIU_MBSCTL(CONFIG_EBIU_MBSCTL_VAL);
350 bfin_write_EBIU_MODE(CONFIG_EBIU_MODE_VAL);
351 bfin_write_EBIU_FCTL(CONFIG_EBIU_FCTL_VAL);
352#endif
353
354 serial_putc('I');
355
356 /* Program the external memory controller. */
357#ifdef EBIU_RSTCTL
358 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
359 bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
360 bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
361 bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
362# ifdef CONFIG_EBIU_DDRCTL3_VAL
363 /* default is disable, so don't need to force this */
364 bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
365# endif
366#else
367 bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
368 bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
369 bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
370#endif
371
372 serial_putc('N');
373
374 /* Restore all peripheral wakeups. */
375#ifdef SIC_IWR0
376 bfin_write_SIC_IWR0(-1);
377 bfin_write_SIC_IWR1(-1);
378# ifdef SIC_IWR2
379 bfin_write_SIC_IWR2(-1);
380# endif
381#elif defined(SICA_IWR0)
382 bfin_write_SICA_IWR0(-1);
383 bfin_write_SICA_IWR1(-1);
384#else
385 bfin_write_SIC_IWR(-1);
386#endif
387
388 serial_putc('>');
389 serial_putc('\n');
390
391 serial_deinit();
392}