blob: fb3a101c7909c8876b05bbd28bc37979cc623c5d [file] [log] [blame]
Mike Frysinger94bae5c2008-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 *
Mike Frysinger31c7c502011-05-30 13:47:38 -04007 * Copyright (c) 2004-2011 Analog Devices Inc.
Mike Frysinger94bae5c2008-03-30 15:46:13 -04008 *
9 * Licensed under the GPL-2 or later.
10 */
11
Mike Frysinger3343bfa2009-11-09 19:44:04 -050012#define BFIN_IN_INITCODE
13
Mike Frysinger94bae5c2008-03-30 15:46:13 -040014#include <config.h>
15#include <asm/blackfin.h>
16#include <asm/mach-common/bits/bootrom.h>
Mike Frysinger268dbf52008-10-11 21:58:33 -040017#include <asm/mach-common/bits/core.h>
Mike Frysinger94bae5c2008-03-30 15:46:13 -040018#include <asm/mach-common/bits/ebiu.h>
19#include <asm/mach-common/bits/pll.h>
20#include <asm/mach-common/bits/uart.h>
21
Mike Frysingerf05105c2011-06-06 16:47:31 -040022#define BUG() while (1) { asm volatile("emuexcpt;"); }
23
Mike Frysinger94bae5c2008-03-30 15:46:13 -040024#include "serial.h"
25
26__attribute__((always_inline))
Mike Frysinger84451302008-12-10 12:33:54 -050027static inline void serial_init(void)
Mike Frysinger94bae5c2008-03-30 15:46:13 -040028{
Mike Frysinger53ba3222011-04-29 23:23:28 -040029 uint32_t uart_base = UART_DLL;
30
Mike Frysinger94bae5c2008-03-30 15:46:13 -040031#ifdef __ADSPBF54x__
32# ifdef BFIN_BOOT_UART_USE_RTS
33# define BFIN_UART_USE_RTS 1
34# else
35# define BFIN_UART_USE_RTS 0
36# endif
37 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
38 size_t i;
39
40 /* force RTS rather than relying on auto RTS */
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050041 bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) | FCPOL);
Mike Frysinger94bae5c2008-03-30 15:46:13 -040042
43 /* Wait for the line to clear up. We cannot rely on UART
44 * registers as none of them reflect the status of the RSR.
45 * Instead, we'll sleep for ~10 bit times at 9600 baud.
46 * We can precalc things here by assuming boot values for
47 * PLL rather than loading registers and calculating.
48 * baud = SCLK / (16 ^ (1 - EDBO) * Divisor)
49 * EDB0 = 0
50 * Divisor = (SCLK / baud) / 16
51 * SCLK = baud * 16 * Divisor
52 * SCLK = (0x14 * CONFIG_CLKIN_HZ) / 5
53 * CCLK = (16 * Divisor * 5) * (9600 / 10)
54 * In reality, this will probably be just about 1 second delay,
55 * so assuming 9600 baud is OK (both as a very low and too high
56 * speed as this will buffer things enough).
57 */
58#define _NUMBITS (10) /* how many bits to delay */
59#define _LOWBAUD (9600) /* low baud rate */
60#define _SCLK ((0x14 * CONFIG_CLKIN_HZ) / 5) /* SCLK based on PLL */
61#define _DIVISOR ((_SCLK / _LOWBAUD) / 16) /* UART DLL/DLH */
62#define _NUMINS (3) /* how many instructions in loop */
63#define _CCLK (((16 * _DIVISOR * 5) * (_LOWBAUD / _NUMBITS)) / _NUMINS)
64 i = _CCLK;
65 while (i--)
66 asm volatile("" : : : "memory");
67 }
68#endif
69
Mike Frysinger94bae5c2008-03-30 15:46:13 -040070 if (BFIN_DEBUG_EARLY_SERIAL) {
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050071 int ucen = bfin_read16(&pUART->gctl) & UCEN;
Mike Frysinger53ba3222011-04-29 23:23:28 -040072 serial_early_init(uart_base);
Mike Frysinger94bae5c2008-03-30 15:46:13 -040073
74 /* If the UART is off, that means we need to program
75 * the baud rate ourselves initially.
76 */
Mike Frysinger84451302008-12-10 12:33:54 -050077 if (ucen != UCEN)
Mike Frysinger53ba3222011-04-29 23:23:28 -040078 serial_early_set_baud(uart_base, CONFIG_BAUDRATE);
Mike Frysinger94bae5c2008-03-30 15:46:13 -040079 }
Mike Frysinger94bae5c2008-03-30 15:46:13 -040080}
81
82__attribute__((always_inline))
83static inline void serial_deinit(void)
84{
85#ifdef __ADSPBF54x__
Mike Frysinger53ba3222011-04-29 23:23:28 -040086 uint32_t uart_base = UART_DLL;
87
Mike Frysinger94bae5c2008-03-30 15:46:13 -040088 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
89 /* clear forced RTS rather than relying on auto RTS */
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -050090 bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) & ~FCPOL);
Mike Frysinger94bae5c2008-03-30 15:46:13 -040091 }
92#endif
Mike Frysinger94bae5c2008-03-30 15:46:13 -040093}
94
95__attribute__((always_inline))
96static inline void serial_putc(char c)
97{
Mike Frysinger53ba3222011-04-29 23:23:28 -040098 uint32_t uart_base = UART_DLL;
99
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400100 if (!BFIN_DEBUG_EARLY_SERIAL)
101 return;
102
103 if (c == '\n')
Mike Frysingere7851d02009-04-24 23:22:48 -0400104 serial_putc('\r');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400105
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -0500106 bfin_write16(&pUART->thr, c);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400107
Mike Frysinger3b7ed5a2009-11-12 18:42:53 -0500108 while (!(bfin_read16(&pUART->lsr) & TEMT))
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400109 continue;
110}
111
Mike Frysinger31c7c502011-05-30 13:47:38 -0400112#include "initcode.h"
113
Mike Frysingereb2a3992010-05-05 02:07:44 -0400114__attribute__((always_inline)) static inline void
115program_nmi_handler(void)
116{
117 u32 tmp1, tmp2;
118
119 /* Older bootroms don't create a dummy NMI handler,
120 * so make one ourselves ASAP in case it fires.
121 */
122 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS && !ANOMALY_05000219)
123 return;
124
125 asm volatile (
126 "%0 = RETS;" /* Save current RETS */
127 "CALL 1f;" /* Figure out current PC */
128 "RTN;" /* The simple NMI handler */
129 "1:"
130 "%1 = RETS;" /* Load addr of NMI handler */
131 "RETS = %0;" /* Restore RETS */
132 "[%2] = %1;" /* Write NMI handler */
133 : "=r"(tmp1), "=r"(tmp2) : "ab"(EVT2)
134 );
135}
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400136
Mike Frysinger2c001972008-12-09 17:21:08 -0500137/* Max SCLK can be 133MHz ... dividing that by (2*4) gives
138 * us a freq of 16MHz for SPI which should generally be
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400139 * slow enough for the slow reads the bootrom uses.
140 */
Mike Frysinger2c001972008-12-09 17:21:08 -0500141#if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
142 ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
143 (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
144# define BOOTROM_SUPPORTS_SPI_FAST_READ 1
145#else
146# define BOOTROM_SUPPORTS_SPI_FAST_READ 0
147#endif
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400148#ifndef CONFIG_SPI_BAUD_INITBLOCK
Mike Frysinger2c001972008-12-09 17:21:08 -0500149# define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
150#endif
151#ifdef SPI0_BAUD
152# define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400153#endif
154
155/* PLL_DIV defines */
156#ifndef CONFIG_PLL_DIV_VAL
157# if (CONFIG_CCLK_DIV == 1)
158# define CONFIG_CCLK_ACT_DIV CCLK_DIV1
159# elif (CONFIG_CCLK_DIV == 2)
160# define CONFIG_CCLK_ACT_DIV CCLK_DIV2
161# elif (CONFIG_CCLK_DIV == 4)
162# define CONFIG_CCLK_ACT_DIV CCLK_DIV4
163# elif (CONFIG_CCLK_DIV == 8)
164# define CONFIG_CCLK_ACT_DIV CCLK_DIV8
165# else
166# define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
167# endif
168# define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
169#endif
170
171#ifndef CONFIG_PLL_LOCKCNT_VAL
172# define CONFIG_PLL_LOCKCNT_VAL 0x0300
173#endif
174
175#ifndef CONFIG_PLL_CTL_VAL
Mike Frysingerc13fc442008-06-01 01:26:29 -0400176# define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400177#endif
178
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400179/* Make sure our voltage value is sane so we don't blow up! */
180#ifndef CONFIG_VR_CTL_VAL
181# define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
182# if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
183# define CCLK_VLEV_120 400000000
184# define CCLK_VLEV_125 533000000
185# elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
186# define CCLK_VLEV_120 401000000
187# define CCLK_VLEV_125 401000000
188# elif defined(__ADSPBF561__)
189# define CCLK_VLEV_120 300000000
190# define CCLK_VLEV_125 501000000
191# endif
192# if BFIN_CCLK < CCLK_VLEV_120
193# define CONFIG_VR_CTL_VLEV VLEV_120
194# elif BFIN_CCLK < CCLK_VLEV_125
195# define CONFIG_VR_CTL_VLEV VLEV_125
196# else
197# define CONFIG_VR_CTL_VLEV VLEV_130
198# endif
199# if defined(__ADSPBF52x__) /* TBD; use default */
200# undef CONFIG_VR_CTL_VLEV
201# define CONFIG_VR_CTL_VLEV VLEV_110
202# elif defined(__ADSPBF54x__) /* TBD; use default */
203# undef CONFIG_VR_CTL_VLEV
204# define CONFIG_VR_CTL_VLEV VLEV_120
Mike Frysingera7ab10a2008-10-11 21:54:00 -0400205# elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
206# undef CONFIG_VR_CTL_VLEV
207# define CONFIG_VR_CTL_VLEV VLEV_125
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400208# endif
209
210# ifdef CONFIG_BFIN_MAC
211# define CONFIG_VR_CTL_CLKBUF CLKBUFOE
212# else
213# define CONFIG_VR_CTL_CLKBUF 0
214# endif
215
216# if defined(__ADSPBF52x__)
217# define CONFIG_VR_CTL_FREQ FREQ_1000
218# else
219# define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
220# endif
221
222# define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
223#endif
224
Mike Frysinger446d5702008-10-11 21:56:08 -0400225/* some parts do not have an on-chip voltage regulator */
226#if defined(__ADSPBF51x__)
227# define CONFIG_HAS_VR 0
228# undef CONFIG_VR_CTL_VAL
229# define CONFIG_VR_CTL_VAL 0
230#else
231# define CONFIG_HAS_VR 1
232#endif
233
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500234#if CONFIG_MEM_SIZE
Mike Frysingerb0f14682008-06-01 01:28:24 -0400235#ifndef EBIU_RSTCTL
236/* Blackfin with SDRAM */
237#ifndef CONFIG_EBIU_SDBCTL_VAL
238# if CONFIG_MEM_SIZE == 16
239# define CONFIG_EBSZ_VAL EBSZ_16
240# elif CONFIG_MEM_SIZE == 32
241# define CONFIG_EBSZ_VAL EBSZ_32
242# elif CONFIG_MEM_SIZE == 64
243# define CONFIG_EBSZ_VAL EBSZ_64
244# elif CONFIG_MEM_SIZE == 128
245# define CONFIG_EBSZ_VAL EBSZ_128
246# elif CONFIG_MEM_SIZE == 256
247# define CONFIG_EBSZ_VAL EBSZ_256
248# elif CONFIG_MEM_SIZE == 512
249# define CONFIG_EBSZ_VAL EBSZ_512
250# else
251# error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
252# endif
253# if CONFIG_MEM_ADD_WDTH == 8
254# define CONFIG_EBCAW_VAL EBCAW_8
255# elif CONFIG_MEM_ADD_WDTH == 9
256# define CONFIG_EBCAW_VAL EBCAW_9
257# elif CONFIG_MEM_ADD_WDTH == 10
258# define CONFIG_EBCAW_VAL EBCAW_10
259# elif CONFIG_MEM_ADD_WDTH == 11
260# define CONFIG_EBCAW_VAL EBCAW_11
261# else
262# error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
263# endif
264# define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
265#endif
266#endif
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500267#endif
Mike Frysingerb0f14682008-06-01 01:28:24 -0400268
Mike Frysinger8c10be42009-04-04 08:40:13 -0400269/* Conflicting Column Address Widths Causes SDRAM Errors:
270 * EB2CAW and EB3CAW must be the same
271 */
272#if ANOMALY_05000362
273# if ((CONFIG_EBIU_SDBCTL_VAL & 0x30000000) >> 8) != (CONFIG_EBIU_SDBCTL_VAL & 0x00300000)
274# error "Anomaly 05000362: EB2CAW and EB3CAW must be the same"
275# endif
276#endif
277
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500278__attribute__((always_inline)) static inline void
279program_early_devices(ADI_BOOT_DATA *bs, uint *sdivB, uint *divB, uint *vcoB)
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400280{
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500281 serial_putc('a');
Mike Frysinger01986762009-02-13 17:10:58 -0500282
Mike Frysinger84451302008-12-10 12:33:54 -0500283 /* Save the clock pieces that are used in baud rate calculation */
Mike Frysinger84451302008-12-10 12:33:54 -0500284 if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500285 serial_putc('b');
286 *sdivB = bfin_read_PLL_DIV() & 0xf;
287 *vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
288 *divB = serial_early_get_div();
289 serial_putc('c');
Mike Frysinger84451302008-12-10 12:33:54 -0500290 }
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400291
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500292 serial_putc('d');
Mike Frysinger01986762009-02-13 17:10:58 -0500293
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400294#ifdef CONFIG_HW_WATCHDOG
295# ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
296# define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
297# endif
298 /* Program the watchdog with an initial timeout of ~20 seconds.
299 * Hopefully that should be long enough to load the u-boot LDR
300 * (from wherever) and then the common u-boot code can take over.
301 * In bypass mode, the start.S would have already set a much lower
302 * timeout, so don't clobber that.
303 */
304 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500305 serial_putc('e');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400306 bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
307 bfin_write_WDOG_CTL(0);
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500308 serial_putc('f');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400309 }
310#endif
311
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500312 serial_putc('g');
313
314 /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
315 * fast read, so we need to slow down the SPI clock a lot more during
316 * boot. Once we switch over to u-boot's SPI flash driver, we'll
317 * increase the speed appropriately.
318 */
319 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
320 serial_putc('h');
321 if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
322 bs->dFlags |= BFLAG_FASTREAD;
323 bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
324 serial_putc('i');
325 }
326
327 serial_putc('j');
328}
329
330__attribute__((always_inline)) static inline bool
331maybe_self_refresh(ADI_BOOT_DATA *bs)
332{
333 serial_putc('a');
334
335 if (!CONFIG_MEM_SIZE)
336 return false;
Mike Frysinger268dbf52008-10-11 21:58:33 -0400337
338 /* If external memory is enabled, put it into self refresh first. */
Mike Frysinger134db0d2010-12-17 15:25:09 -0500339#if defined(EBIU_RSTCTL)
Mike Frysinger268dbf52008-10-11 21:58:33 -0400340 if (bfin_read_EBIU_RSTCTL() & DDR_SRESET) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500341 serial_putc('b');
Mike Frysinger268dbf52008-10-11 21:58:33 -0400342 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | SRREQ);
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500343 return true;
Mike Frysinger268dbf52008-10-11 21:58:33 -0400344 }
Mike Frysinger134db0d2010-12-17 15:25:09 -0500345#elif defined(EBIU_SDGCTL)
Mike Frysinger268dbf52008-10-11 21:58:33 -0400346 if (bfin_read_EBIU_SDBCTL() & EBE) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500347 serial_putc('b');
Mike Frysinger268dbf52008-10-11 21:58:33 -0400348 bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() | SRFS);
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500349 return true;
Mike Frysinger268dbf52008-10-11 21:58:33 -0400350 }
351#endif
352
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500353 serial_putc('c');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400354
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500355 return false;
356}
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400357
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500358__attribute__((always_inline)) static inline u16
359program_clocks(ADI_BOOT_DATA *bs, bool put_into_srfs)
360{
361 u16 vr_ctl;
362
363 serial_putc('a');
364
365 vr_ctl = bfin_read_VR_CTL();
366
367 serial_putc('b');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400368
Mike Frysinger268dbf52008-10-11 21:58:33 -0400369 /* If we're entering self refresh, make sure it has happened. */
370 if (put_into_srfs)
Mike Frysinger134db0d2010-12-17 15:25:09 -0500371#if defined(EBIU_RSTCTL)
Mike Frysinger268dbf52008-10-11 21:58:33 -0400372 while (!(bfin_read_EBIU_RSTCTL() & SRACK))
Mike Frysinger134db0d2010-12-17 15:25:09 -0500373 continue;
374#elif defined(EBIU_SDGCTL)
Mike Frysinger268dbf52008-10-11 21:58:33 -0400375 while (!(bfin_read_EBIU_SDSTAT() & SDSRA))
Mike Frysinger268dbf52008-10-11 21:58:33 -0400376 continue;
Mike Frysinger134db0d2010-12-17 15:25:09 -0500377#else
378 ;
379#endif
Mike Frysinger268dbf52008-10-11 21:58:33 -0400380
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500381 serial_putc('c');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400382
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400383 /* With newer bootroms, we use the helper function to set up
384 * the memory controller. Older bootroms lacks such helpers
385 * so we do it ourselves.
386 */
Mike Frysinger268dbf52008-10-11 21:58:33 -0400387 if (!ANOMALY_05000386) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500388 serial_putc('d');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400389
Mike Frysingere8aea4a2009-04-04 08:29:55 -0400390 /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400391 ADI_SYSCTRL_VALUES memory_settings;
Mike Frysingerb91d7d92010-10-14 14:29:17 -0400392 uint32_t actions = SYSCTRL_WRITE | SYSCTRL_PLLCTL | SYSCTRL_LOCKCNT;
393 if (!ANOMALY_05000440)
394 actions |= SYSCTRL_PLLDIV;
Mike Frysinger446d5702008-10-11 21:56:08 -0400395 if (CONFIG_HAS_VR) {
396 actions |= SYSCTRL_VRCTL;
397 if (CONFIG_VR_CTL_VAL & FREQ_MASK)
398 actions |= SYSCTRL_INTVOLTAGE;
399 else
400 actions |= SYSCTRL_EXTVOLTAGE;
401 memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
402 } else
403 actions |= SYSCTRL_EXTVOLTAGE;
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400404 memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
405 memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
406 memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
Mike Frysingerf9d004b2008-12-06 18:06:58 -0500407#if ANOMALY_05000432
408 bfin_write_SIC_IWR1(0);
409#endif
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500410 serial_putc('e');
Mike Frysinger446d5702008-10-11 21:56:08 -0400411 bfrom_SysControl(actions, &memory_settings, NULL);
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500412 serial_putc('f');
Mike Frysingerb91d7d92010-10-14 14:29:17 -0400413 if (ANOMALY_05000440)
414 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
Mike Frysingerf9d004b2008-12-06 18:06:58 -0500415#if ANOMALY_05000432
416 bfin_write_SIC_IWR1(-1);
417#endif
Mike Frysinger1f1ac0a2009-04-04 08:09:24 -0400418#if ANOMALY_05000171
419 bfin_write_SICA_IWR0(-1);
420 bfin_write_SICA_IWR1(-1);
421#endif
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500422 serial_putc('g');
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400423 } else {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500424 serial_putc('h');
Mike Frysinger268dbf52008-10-11 21:58:33 -0400425
426 /* Disable all peripheral wakeups except for the PLL event. */
427#ifdef SIC_IWR0
428 bfin_write_SIC_IWR0(1);
429 bfin_write_SIC_IWR1(0);
430# ifdef SIC_IWR2
431 bfin_write_SIC_IWR2(0);
432# endif
433#elif defined(SICA_IWR0)
434 bfin_write_SICA_IWR0(1);
435 bfin_write_SICA_IWR1(0);
436#else
437 bfin_write_SIC_IWR(1);
438#endif
439
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500440 serial_putc('i');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400441
Mike Frysingere8aea4a2009-04-04 08:29:55 -0400442 /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400443 bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400444
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500445 serial_putc('j');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400446
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400447 /* Only reprogram when needed to avoid triggering unnecessary
448 * PLL relock sequences.
449 */
Mike Frysinger268dbf52008-10-11 21:58:33 -0400450 if (vr_ctl != CONFIG_VR_CTL_VAL) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500451 serial_putc('?');
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400452 bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
453 asm("idle;");
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500454 serial_putc('!');
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400455 }
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400456
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500457 serial_putc('k');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400458
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400459 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400460
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500461 serial_putc('l');
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400462
463 /* Only reprogram when needed to avoid triggering unnecessary
464 * PLL relock sequences.
465 */
Mike Frysinger43ed6962009-04-04 08:10:22 -0400466 if (ANOMALY_05000242 || bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500467 serial_putc('?');
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400468 bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
469 asm("idle;");
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500470 serial_putc('!');
Mike Frysinger1114d0e2008-06-01 01:29:57 -0400471 }
Mike Frysinger268dbf52008-10-11 21:58:33 -0400472
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500473 serial_putc('m');
Mike Frysinger268dbf52008-10-11 21:58:33 -0400474
475 /* Restore all peripheral wakeups. */
476#ifdef SIC_IWR0
477 bfin_write_SIC_IWR0(-1);
478 bfin_write_SIC_IWR1(-1);
479# ifdef SIC_IWR2
480 bfin_write_SIC_IWR2(-1);
481# endif
482#elif defined(SICA_IWR0)
483 bfin_write_SICA_IWR0(-1);
484 bfin_write_SICA_IWR1(-1);
485#else
486 bfin_write_SIC_IWR(-1);
487#endif
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500488
489 serial_putc('n');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400490 }
491
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500492 serial_putc('o');
493
494 return vr_ctl;
495}
496
497__attribute__((always_inline)) static inline void
498update_serial_clocks(ADI_BOOT_DATA *bs, uint sdivB, uint divB, uint vcoB)
499{
500 serial_putc('a');
Mike Frysinger268dbf52008-10-11 21:58:33 -0400501
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400502 /* Since we've changed the SCLK above, we may need to update
503 * the UART divisors (UART baud rates are based on SCLK).
Mike Frysinger84451302008-12-10 12:33:54 -0500504 * Do the division by hand as there are no native instructions
505 * for dividing which means we'd generate a libgcc reference.
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400506 */
Mike Frysinger84451302008-12-10 12:33:54 -0500507 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500508 serial_putc('b');
Mike Frysinger84451302008-12-10 12:33:54 -0500509 unsigned int sdivR, vcoR;
510 sdivR = bfin_read_PLL_DIV() & 0xf;
511 vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
512 int dividend = sdivB * divB * vcoR;
513 int divisor = vcoB * sdivR;
514 unsigned int quotient;
515 for (quotient = 0; dividend > 0; ++quotient)
516 dividend -= divisor;
Mike Frysinger53ba3222011-04-29 23:23:28 -0400517 serial_early_put_div(UART_DLL, quotient - ANOMALY_05000230);
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500518 serial_putc('c');
Mike Frysinger84451302008-12-10 12:33:54 -0500519 }
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400520
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500521 serial_putc('d');
522}
523
524__attribute__((always_inline)) static inline void
525program_memory_controller(ADI_BOOT_DATA *bs, bool put_into_srfs)
526{
527 serial_putc('a');
528
529 if (!CONFIG_MEM_SIZE)
530 return;
531
532 serial_putc('b');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400533
Mike Frysinger268dbf52008-10-11 21:58:33 -0400534 /* Program the external memory controller before we come out of
535 * self-refresh. This only works with our SDRAM controller.
536 */
Mike Frysinger134db0d2010-12-17 15:25:09 -0500537#ifdef EBIU_SDGCTL
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500538# ifdef CONFIG_EBIU_SDRRC_VAL
Mike Frysinger268dbf52008-10-11 21:58:33 -0400539 bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500540# endif
541# ifdef CONFIG_EBIU_SDBCTL_VAL
Mike Frysinger268dbf52008-10-11 21:58:33 -0400542 bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500543# endif
544# ifdef CONFIG_EBIU_SDGCTL_VAL
Mike Frysinger268dbf52008-10-11 21:58:33 -0400545 bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500546# endif
Mike Frysinger268dbf52008-10-11 21:58:33 -0400547#endif
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400548
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500549 serial_putc('c');
Mike Frysinger268dbf52008-10-11 21:58:33 -0400550
551 /* Now that we've reprogrammed, take things out of self refresh. */
552 if (put_into_srfs)
Mike Frysinger134db0d2010-12-17 15:25:09 -0500553#if defined(EBIU_RSTCTL)
Mike Frysinger268dbf52008-10-11 21:58:33 -0400554 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() & ~(SRREQ));
Mike Frysinger134db0d2010-12-17 15:25:09 -0500555#elif defined(EBIU_SDGCTL)
Mike Frysinger268dbf52008-10-11 21:58:33 -0400556 bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() & ~(SRFS));
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400557#endif
558
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500559 serial_putc('d');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400560
Mike Frysinger268dbf52008-10-11 21:58:33 -0400561 /* Our DDR controller sucks and cannot be programmed while in
562 * self-refresh. So we have to pull it out before programming.
563 */
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400564#ifdef EBIU_RSTCTL
Mike Frysinger4368ea22009-11-09 19:38:23 -0500565# ifdef CONFIG_EBIU_RSTCTL_VAL
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400566 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
Mike Frysinger4368ea22009-11-09 19:38:23 -0500567# endif
568# ifdef CONFIG_EBIU_DDRCTL0_VAL
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400569 bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
Mike Frysinger4368ea22009-11-09 19:38:23 -0500570# endif
571# ifdef CONFIG_EBIU_DDRCTL1_VAL
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400572 bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
Mike Frysinger4368ea22009-11-09 19:38:23 -0500573# endif
574# ifdef CONFIG_EBIU_DDRCTL2_VAL
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400575 bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
Mike Frysinger4368ea22009-11-09 19:38:23 -0500576# endif
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400577# ifdef CONFIG_EBIU_DDRCTL3_VAL
578 /* default is disable, so don't need to force this */
579 bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
580# endif
Mike Frysinger268dbf52008-10-11 21:58:33 -0400581# ifdef CONFIG_EBIU_DDRQUE_VAL
582 bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE() | CONFIG_EBIU_DDRQUE_VAL);
583# endif
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400584#endif
585
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500586 serial_putc('e');
587}
588
589__attribute__((always_inline)) static inline void
590check_hibernation(ADI_BOOT_DATA *bs, u16 vr_ctl, bool put_into_srfs)
591{
592 serial_putc('a');
593
594 if (!CONFIG_MEM_SIZE)
595 return;
596
597 serial_putc('b');
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400598
Mike Frysinger268dbf52008-10-11 21:58:33 -0400599 /* Are we coming out of hibernate (suspend to memory) ?
600 * The memory layout is:
601 * 0x0: hibernate magic for anomaly 307 (0xDEADBEEF)
602 * 0x4: return address
603 * 0x8: stack pointer
604 *
605 * SCKELOW is unreliable on older parts (anomaly 307)
606 */
607 if (ANOMALY_05000307 || vr_ctl & 0x8000) {
608 uint32_t *hibernate_magic = 0;
609 __builtin_bfin_ssync(); /* make sure memory controller is done */
610 if (hibernate_magic[0] == 0xDEADBEEF) {
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500611 serial_putc('c');
Mike Frysinger268dbf52008-10-11 21:58:33 -0400612 bfin_write_EVT15(hibernate_magic[1]);
613 bfin_write_IMASK(EVT_IVG15);
614 __asm__ __volatile__ (
615 /* load reti early to avoid anomaly 281 */
616 "reti = %0;"
617 /* clear hibernate magic */
618 "[%0] = %1;"
619 /* load stack pointer */
620 "SP = [%0 + 8];"
621 /* lower ourselves from reset ivg to ivg15 */
622 "raise 15;"
623 "rti;"
624 :
625 : "p"(hibernate_magic), "d"(0x2000 /* jump.s 0 */)
626 );
627 }
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500628 serial_putc('d');
Mike Frysinger268dbf52008-10-11 21:58:33 -0400629 }
630
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500631 serial_putc('e');
632}
633
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500634BOOTROM_CALLED_FUNC_ATTR
635void initcode(ADI_BOOT_DATA *bs)
636{
637 ADI_BOOT_DATA bootstruct_scratch;
638
Mike Frysingereb2a3992010-05-05 02:07:44 -0400639 /* Setup NMI handler before anything else */
640 program_nmi_handler();
641
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500642 serial_init();
643
644 serial_putc('A');
645
646 /* If the bootstruct is NULL, then it's because we're loading
647 * dynamically and not via LDR (bootrom). So set the struct to
648 * some scratch space.
649 */
650 if (!bs)
651 bs = &bootstruct_scratch;
652
653 serial_putc('B');
654 bool put_into_srfs = maybe_self_refresh(bs);
655
656 serial_putc('C');
657 uint sdivB, divB, vcoB;
658 program_early_devices(bs, &sdivB, &divB, &vcoB);
659
660 serial_putc('D');
661 u16 vr_ctl = program_clocks(bs, put_into_srfs);
662
663 serial_putc('E');
664 update_serial_clocks(bs, sdivB, divB, vcoB);
665
666 serial_putc('F');
667 program_memory_controller(bs, put_into_srfs);
668
669 serial_putc('G');
670 check_hibernation(bs, vr_ctl, put_into_srfs);
671
672 serial_putc('H');
673 program_async_controller(bs);
Mike Frysinger268dbf52008-10-11 21:58:33 -0400674
Mike Frysingera48e0ed2009-04-24 23:39:41 -0400675#ifdef CONFIG_BFIN_BOOTROM_USES_EVT1
Mike Frysinger3343bfa2009-11-09 19:44:04 -0500676 serial_putc('I');
Mike Frysinger1100b692010-04-29 02:49:41 -0400677 /* Tell the bootrom where our entry point is so that it knows
678 * where to jump to when finishing processing the LDR. This
679 * allows us to avoid small jump blocks in the LDR, and also
680 * works around anomaly 05000389 (init address in external
681 * memory causes bootrom to trigger external addressing IVHW).
682 */
Mike Frysinger99593682008-10-18 04:04:49 -0400683 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
684 bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);
Mike Frysingera48e0ed2009-04-24 23:39:41 -0400685#endif
Mike Frysinger99593682008-10-18 04:04:49 -0400686
Mike Frysinger94bae5c2008-03-30 15:46:13 -0400687 serial_putc('>');
688 serial_putc('\n');
689
690 serial_deinit();
691}