blob: 1352fd65db39266fe78754acaa49e6db93ab52d4 [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))
Mike Frysingerf790ef62008-12-10 12:33:54 -050023static inline void serial_init(void)
Mike Frysinger9171fc82008-03-30 15:46:13 -040024{
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 Frysinger9171fc82008-03-30 15:46:13 -040064 if (BFIN_DEBUG_EARLY_SERIAL) {
Mike Frysingerf790ef62008-12-10 12:33:54 -050065 int ucen = *pUART_GCTL & UCEN;
Mike Frysinger9171fc82008-03-30 15:46:13 -040066 serial_early_init();
67
68 /* If the UART is off, that means we need to program
69 * the baud rate ourselves initially.
70 */
Mike Frysingerf790ef62008-12-10 12:33:54 -050071 if (ucen != UCEN)
Mike Frysinger9171fc82008-03-30 15:46:13 -040072 serial_early_set_baud(CONFIG_BAUDRATE);
Mike Frysinger9171fc82008-03-30 15:46:13 -040073 }
Mike Frysinger9171fc82008-03-30 15:46:13 -040074}
75
76__attribute__((always_inline))
77static inline void serial_deinit(void)
78{
79#ifdef __ADSPBF54x__
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);
83 }
84#endif
85}
86
Mike Frysinger9171fc82008-03-30 15:46:13 -040087__attribute__((always_inline))
88static inline void serial_putc(char c)
89{
90 if (!BFIN_DEBUG_EARLY_SERIAL)
91 return;
92
93 if (c == '\n')
94 *pUART_THR = '\r';
95
96 *pUART_THR = c;
97
98 while (!(*pUART_LSR & TEMT))
99 continue;
100}
101
102
Mike Frysinger97f265f2008-12-09 17:21:08 -0500103/* Max SCLK can be 133MHz ... dividing that by (2*4) gives
104 * us a freq of 16MHz for SPI which should generally be
Mike Frysinger9171fc82008-03-30 15:46:13 -0400105 * slow enough for the slow reads the bootrom uses.
106 */
Mike Frysinger97f265f2008-12-09 17:21:08 -0500107#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
111#else
112# define BOOTROM_SUPPORTS_SPI_FAST_READ 0
113#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400114#ifndef CONFIG_SPI_BAUD_INITBLOCK
Mike Frysinger97f265f2008-12-09 17:21:08 -0500115# define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
116#endif
117#ifdef SPI0_BAUD
118# define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
Mike Frysinger9171fc82008-03-30 15:46:13 -0400119#endif
120
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
131# else
132# define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
133# endif
134# define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
135#endif
136
137#ifndef CONFIG_PLL_LOCKCNT_VAL
138# define CONFIG_PLL_LOCKCNT_VAL 0x0300
139#endif
140
141#ifndef CONFIG_PLL_CTL_VAL
Mike Frysinger4f6a3132008-06-01 01:26:29 -0400142# define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400143#endif
144
145#ifndef CONFIG_EBIU_RSTCTL_VAL
146# define CONFIG_EBIU_RSTCTL_VAL 0 /* only MDDRENABLE is useful */
147#endif
Mike Frysinger67619982008-10-11 21:46:52 -0400148#if ((CONFIG_EBIU_RSTCTL_VAL & 0xFFFFFFC4) != 0)
149# error invalid EBIU_RSTCTL value: must not set reserved bits
150#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400151
152#ifndef CONFIG_EBIU_MBSCTL_VAL
153# define CONFIG_EBIU_MBSCTL_VAL 0
154#endif
155
Mike Frysinger67619982008-10-11 21:46:52 -0400156#if defined(CONFIG_EBIU_DDRQUE_VAL) && ((CONFIG_EBIU_DDRQUE_VAL & 0xFFFF8000) != 0)
157# error invalid EBIU_DDRQUE value: must not set reserved bits
158#endif
159
Mike Frysinger9171fc82008-03-30 15:46:13 -0400160/* 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
172# endif
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
177# else
178# define CONFIG_VR_CTL_VLEV VLEV_130
179# endif
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
Mike Frysinger622a8dc2008-10-11 21:54:00 -0400186# elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
187# undef CONFIG_VR_CTL_VLEV
188# define CONFIG_VR_CTL_VLEV VLEV_125
Mike Frysinger9171fc82008-03-30 15:46:13 -0400189# endif
190
191# ifdef CONFIG_BFIN_MAC
192# define CONFIG_VR_CTL_CLKBUF CLKBUFOE
193# else
194# define CONFIG_VR_CTL_CLKBUF 0
195# endif
196
197# if defined(__ADSPBF52x__)
198# define CONFIG_VR_CTL_FREQ FREQ_1000
199# else
200# define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
201# endif
202
203# define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
204#endif
205
Mike Frysinger0d4f24b2008-06-01 01:28:24 -0400206#ifndef EBIU_RSTCTL
207/* Blackfin with SDRAM */
208#ifndef CONFIG_EBIU_SDBCTL_VAL
209# if CONFIG_MEM_SIZE == 16
210# define CONFIG_EBSZ_VAL EBSZ_16
211# elif CONFIG_MEM_SIZE == 32
212# define CONFIG_EBSZ_VAL EBSZ_32
213# elif CONFIG_MEM_SIZE == 64
214# define CONFIG_EBSZ_VAL EBSZ_64
215# elif CONFIG_MEM_SIZE == 128
216# define CONFIG_EBSZ_VAL EBSZ_128
217# elif CONFIG_MEM_SIZE == 256
218# define CONFIG_EBSZ_VAL EBSZ_256
219# elif CONFIG_MEM_SIZE == 512
220# define CONFIG_EBSZ_VAL EBSZ_512
221# else
222# error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
223# endif
224# if CONFIG_MEM_ADD_WDTH == 8
225# define CONFIG_EBCAW_VAL EBCAW_8
226# elif CONFIG_MEM_ADD_WDTH == 9
227# define CONFIG_EBCAW_VAL EBCAW_9
228# elif CONFIG_MEM_ADD_WDTH == 10
229# define CONFIG_EBCAW_VAL EBCAW_10
230# elif CONFIG_MEM_ADD_WDTH == 11
231# define CONFIG_EBCAW_VAL EBCAW_11
232# else
233# error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
234# endif
235# define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
236#endif
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{
Mike Frysingerf790ef62008-12-10 12:33:54 -0500242 /* Save the clock pieces that are used in baud rate calculation */
243 unsigned int sdivB, divB, vcoB;
244 serial_init();
245 if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
246 sdivB = bfin_read_PLL_DIV() & 0xf;
247 vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
248 divB = serial_early_get_div();
249 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400250
251#ifdef CONFIG_HW_WATCHDOG
252# ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
253# define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
254# endif
255 /* Program the watchdog with an initial timeout of ~20 seconds.
256 * Hopefully that should be long enough to load the u-boot LDR
257 * (from wherever) and then the common u-boot code can take over.
258 * In bypass mode, the start.S would have already set a much lower
259 * timeout, so don't clobber that.
260 */
261 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
262 bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
263 bfin_write_WDOG_CTL(0);
264 }
265#endif
266
267 serial_putc('S');
268
269 /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
270 * fast read, so we need to slow down the SPI clock a lot more during
271 * boot. Once we switch over to u-boot's SPI flash driver, we'll
272 * increase the speed appropriately.
273 */
Mike Frysinger97f265f2008-12-09 17:21:08 -0500274 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
275 if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
276 bootstruct->dFlags |= BFLAG_FASTREAD;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400277 bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
Mike Frysinger97f265f2008-12-09 17:21:08 -0500278 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400279
280 serial_putc('B');
281
282 /* Disable all peripheral wakeups except for the PLL event. */
283#ifdef SIC_IWR0
284 bfin_write_SIC_IWR0(1);
285 bfin_write_SIC_IWR1(0);
286# ifdef SIC_IWR2
287 bfin_write_SIC_IWR2(0);
288# endif
289#elif defined(SICA_IWR0)
290 bfin_write_SICA_IWR0(1);
291 bfin_write_SICA_IWR1(0);
292#else
293 bfin_write_SIC_IWR(1);
294#endif
295
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400296 /* With newer bootroms, we use the helper function to set up
297 * the memory controller. Older bootroms lacks such helpers
298 * so we do it ourselves.
Mike Frysinger9171fc82008-03-30 15:46:13 -0400299 */
Mike Frysingere1ffaee2009-02-19 01:20:27 -0500300#define BOOTROM_CAPS_SYSCONTROL 0
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400301 if (BOOTROM_CAPS_SYSCONTROL) {
302 serial_putc('S');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400303
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400304 ADI_SYSCTRL_VALUES memory_settings;
305 memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
306 memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
307 memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
308 memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
Mike Frysinger3986e982008-12-06 18:06:58 -0500309#if ANOMALY_05000432
310 bfin_write_SIC_IWR1(0);
311#endif
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400312 syscontrol(SYSCTRL_WRITE | SYSCTRL_VRCTL | SYSCTRL_PLLCTL | SYSCTRL_PLLDIV | SYSCTRL_LOCKCNT |
313 (CONFIG_VR_CTL_VAL & FREQ_MASK ? SYSCTRL_INTVOLTAGE : SYSCTRL_EXTVOLTAGE), &memory_settings, NULL);
Mike Frysinger3986e982008-12-06 18:06:58 -0500314#if ANOMALY_05000432
315 bfin_write_SIC_IWR1(-1);
316#endif
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400317 } else {
318 serial_putc('L');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400319
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400320 bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400321
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400322 serial_putc('A');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400323
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400324 /* Only reprogram when needed to avoid triggering unnecessary
325 * PLL relock sequences.
326 */
327 if (bfin_read_VR_CTL() != CONFIG_VR_CTL_VAL) {
328 serial_putc('!');
329 bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
330 asm("idle;");
331 }
332
333 serial_putc('C');
334
335 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
336
337 serial_putc('K');
338
339 /* Only reprogram when needed to avoid triggering unnecessary
340 * PLL relock sequences.
341 */
342 if (bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
343 serial_putc('!');
344 bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
345 asm("idle;");
346 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400347 }
348
349 /* Since we've changed the SCLK above, we may need to update
350 * the UART divisors (UART baud rates are based on SCLK).
Mike Frysingerf790ef62008-12-10 12:33:54 -0500351 * Do the division by hand as there are no native instructions
352 * for dividing which means we'd generate a libgcc reference.
Mike Frysinger9171fc82008-03-30 15:46:13 -0400353 */
Mike Frysingerf790ef62008-12-10 12:33:54 -0500354 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
355 unsigned int sdivR, vcoR;
356 sdivR = bfin_read_PLL_DIV() & 0xf;
357 vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
358 int dividend = sdivB * divB * vcoR;
359 int divisor = vcoB * sdivR;
360 unsigned int quotient;
361 for (quotient = 0; dividend > 0; ++quotient)
362 dividend -= divisor;
363 serial_early_put_div(quotient - ANOMALY_05000230);
364 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400365
366 serial_putc('F');
367
368 /* Program the async banks controller. */
369 bfin_write_EBIU_AMBCTL0(CONFIG_EBIU_AMBCTL0_VAL);
370 bfin_write_EBIU_AMBCTL1(CONFIG_EBIU_AMBCTL1_VAL);
371 bfin_write_EBIU_AMGCTL(CONFIG_EBIU_AMGCTL_VAL);
372
373#ifdef EBIU_MODE
374 /* Not all parts have these additional MMRs. */
375 bfin_write_EBIU_MBSCTL(CONFIG_EBIU_MBSCTL_VAL);
376 bfin_write_EBIU_MODE(CONFIG_EBIU_MODE_VAL);
377 bfin_write_EBIU_FCTL(CONFIG_EBIU_FCTL_VAL);
378#endif
379
380 serial_putc('I');
381
382 /* Program the external memory controller. */
383#ifdef EBIU_RSTCTL
384 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
385 bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
386 bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
387 bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
388# ifdef CONFIG_EBIU_DDRCTL3_VAL
389 /* default is disable, so don't need to force this */
390 bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
391# endif
392#else
393 bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
394 bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
395 bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
396#endif
397
398 serial_putc('N');
399
400 /* Restore all peripheral wakeups. */
401#ifdef SIC_IWR0
402 bfin_write_SIC_IWR0(-1);
403 bfin_write_SIC_IWR1(-1);
404# ifdef SIC_IWR2
405 bfin_write_SIC_IWR2(-1);
406# endif
407#elif defined(SICA_IWR0)
408 bfin_write_SICA_IWR0(-1);
409 bfin_write_SICA_IWR1(-1);
410#else
411 bfin_write_SIC_IWR(-1);
412#endif
413
Mike Frysinger7e1d2122008-10-18 04:04:49 -0400414 /* tell the bootrom where our entry point is */
415 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
416 bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);
417
Mike Frysinger9171fc82008-03-30 15:46:13 -0400418 serial_putc('>');
419 serial_putc('\n');
420
421 serial_deinit();
422}