blob: 917b7f9b91ce0fd8e1e83ed217d773969e70018d [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 *
Mike Frysinger4150cec2011-05-30 13:47:38 -04007 * Copyright (c) 2004-2011 Analog Devices Inc.
Mike Frysinger9171fc82008-03-30 15:46:13 -04008 *
9 * Licensed under the GPL-2 or later.
10 */
11
Mike Frysingerdbda2c62009-11-09 19:44:04 -050012#define BFIN_IN_INITCODE
13
Mike Frysinger9171fc82008-03-30 15:46:13 -040014#include <config.h>
15#include <asm/blackfin.h>
16#include <asm/mach-common/bits/bootrom.h>
Mike Frysinger74398b22008-10-11 21:58:33 -040017#include <asm/mach-common/bits/core.h>
Mike Frysinger9171fc82008-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 Frysinger9171fc82008-03-30 15:46:13 -040022#include "serial.h"
23
24__attribute__((always_inline))
Mike Frysingerf790ef62008-12-10 12:33:54 -050025static inline void serial_init(void)
Mike Frysinger9171fc82008-03-30 15:46:13 -040026{
Mike Frysinger635f3302011-04-29 23:23:28 -040027 uint32_t uart_base = UART_DLL;
28
Mike Frysinger9171fc82008-03-30 15:46:13 -040029#ifdef __ADSPBF54x__
30# ifdef BFIN_BOOT_UART_USE_RTS
31# define BFIN_UART_USE_RTS 1
32# else
33# define BFIN_UART_USE_RTS 0
34# endif
35 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
36 size_t i;
37
38 /* force RTS rather than relying on auto RTS */
Mike Frysingerf9481582009-11-12 18:42:53 -050039 bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) | FCPOL);
Mike Frysinger9171fc82008-03-30 15:46:13 -040040
41 /* Wait for the line to clear up. We cannot rely on UART
42 * registers as none of them reflect the status of the RSR.
43 * Instead, we'll sleep for ~10 bit times at 9600 baud.
44 * We can precalc things here by assuming boot values for
45 * PLL rather than loading registers and calculating.
46 * baud = SCLK / (16 ^ (1 - EDBO) * Divisor)
47 * EDB0 = 0
48 * Divisor = (SCLK / baud) / 16
49 * SCLK = baud * 16 * Divisor
50 * SCLK = (0x14 * CONFIG_CLKIN_HZ) / 5
51 * CCLK = (16 * Divisor * 5) * (9600 / 10)
52 * In reality, this will probably be just about 1 second delay,
53 * so assuming 9600 baud is OK (both as a very low and too high
54 * speed as this will buffer things enough).
55 */
56#define _NUMBITS (10) /* how many bits to delay */
57#define _LOWBAUD (9600) /* low baud rate */
58#define _SCLK ((0x14 * CONFIG_CLKIN_HZ) / 5) /* SCLK based on PLL */
59#define _DIVISOR ((_SCLK / _LOWBAUD) / 16) /* UART DLL/DLH */
60#define _NUMINS (3) /* how many instructions in loop */
61#define _CCLK (((16 * _DIVISOR * 5) * (_LOWBAUD / _NUMBITS)) / _NUMINS)
62 i = _CCLK;
63 while (i--)
64 asm volatile("" : : : "memory");
65 }
66#endif
67
Mike Frysinger9171fc82008-03-30 15:46:13 -040068 if (BFIN_DEBUG_EARLY_SERIAL) {
Mike Frysingerf9481582009-11-12 18:42:53 -050069 int ucen = bfin_read16(&pUART->gctl) & UCEN;
Mike Frysinger635f3302011-04-29 23:23:28 -040070 serial_early_init(uart_base);
Mike Frysinger9171fc82008-03-30 15:46:13 -040071
72 /* If the UART is off, that means we need to program
73 * the baud rate ourselves initially.
74 */
Mike Frysingerf790ef62008-12-10 12:33:54 -050075 if (ucen != UCEN)
Mike Frysinger635f3302011-04-29 23:23:28 -040076 serial_early_set_baud(uart_base, CONFIG_BAUDRATE);
Mike Frysinger9171fc82008-03-30 15:46:13 -040077 }
Mike Frysinger9171fc82008-03-30 15:46:13 -040078}
79
80__attribute__((always_inline))
81static inline void serial_deinit(void)
82{
83#ifdef __ADSPBF54x__
Mike Frysinger635f3302011-04-29 23:23:28 -040084 uint32_t uart_base = UART_DLL;
85
Mike Frysinger9171fc82008-03-30 15:46:13 -040086 if (BFIN_UART_USE_RTS && CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
87 /* clear forced RTS rather than relying on auto RTS */
Mike Frysingerf9481582009-11-12 18:42:53 -050088 bfin_write16(&pUART->mcr, bfin_read16(&pUART->mcr) & ~FCPOL);
Mike Frysinger9171fc82008-03-30 15:46:13 -040089 }
90#endif
91}
92
Mike Frysinger9171fc82008-03-30 15:46:13 -040093__attribute__((always_inline))
94static inline void serial_putc(char c)
95{
Mike Frysinger635f3302011-04-29 23:23:28 -040096 uint32_t uart_base = UART_DLL;
97
Mike Frysinger9171fc82008-03-30 15:46:13 -040098 if (!BFIN_DEBUG_EARLY_SERIAL)
99 return;
100
101 if (c == '\n')
Mike Frysingeraf2c3732009-04-24 23:22:48 -0400102 serial_putc('\r');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400103
Mike Frysingerf9481582009-11-12 18:42:53 -0500104 bfin_write16(&pUART->thr, c);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400105
Mike Frysingerf9481582009-11-12 18:42:53 -0500106 while (!(bfin_read16(&pUART->lsr) & TEMT))
Mike Frysinger9171fc82008-03-30 15:46:13 -0400107 continue;
108}
109
Mike Frysinger4150cec2011-05-30 13:47:38 -0400110#include "initcode.h"
111
Mike Frysingerce53fc62010-05-05 02:07:44 -0400112__attribute__((always_inline)) static inline void
113program_nmi_handler(void)
114{
115 u32 tmp1, tmp2;
116
117 /* Older bootroms don't create a dummy NMI handler,
118 * so make one ourselves ASAP in case it fires.
119 */
120 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS && !ANOMALY_05000219)
121 return;
122
123 asm volatile (
124 "%0 = RETS;" /* Save current RETS */
125 "CALL 1f;" /* Figure out current PC */
126 "RTN;" /* The simple NMI handler */
127 "1:"
128 "%1 = RETS;" /* Load addr of NMI handler */
129 "RETS = %0;" /* Restore RETS */
130 "[%2] = %1;" /* Write NMI handler */
131 : "=r"(tmp1), "=r"(tmp2) : "ab"(EVT2)
132 );
133}
Mike Frysinger9171fc82008-03-30 15:46:13 -0400134
Mike Frysinger97f265f2008-12-09 17:21:08 -0500135/* Max SCLK can be 133MHz ... dividing that by (2*4) gives
136 * us a freq of 16MHz for SPI which should generally be
Mike Frysinger9171fc82008-03-30 15:46:13 -0400137 * slow enough for the slow reads the bootrom uses.
138 */
Mike Frysinger97f265f2008-12-09 17:21:08 -0500139#if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
140 ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
141 (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
142# define BOOTROM_SUPPORTS_SPI_FAST_READ 1
143#else
144# define BOOTROM_SUPPORTS_SPI_FAST_READ 0
145#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400146#ifndef CONFIG_SPI_BAUD_INITBLOCK
Mike Frysinger97f265f2008-12-09 17:21:08 -0500147# define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
148#endif
149#ifdef SPI0_BAUD
150# define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
Mike Frysinger9171fc82008-03-30 15:46:13 -0400151#endif
152
153/* PLL_DIV defines */
154#ifndef CONFIG_PLL_DIV_VAL
155# if (CONFIG_CCLK_DIV == 1)
156# define CONFIG_CCLK_ACT_DIV CCLK_DIV1
157# elif (CONFIG_CCLK_DIV == 2)
158# define CONFIG_CCLK_ACT_DIV CCLK_DIV2
159# elif (CONFIG_CCLK_DIV == 4)
160# define CONFIG_CCLK_ACT_DIV CCLK_DIV4
161# elif (CONFIG_CCLK_DIV == 8)
162# define CONFIG_CCLK_ACT_DIV CCLK_DIV8
163# else
164# define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
165# endif
166# define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
167#endif
168
169#ifndef CONFIG_PLL_LOCKCNT_VAL
170# define CONFIG_PLL_LOCKCNT_VAL 0x0300
171#endif
172
173#ifndef CONFIG_PLL_CTL_VAL
Mike Frysinger4f6a3132008-06-01 01:26:29 -0400174# define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400175#endif
176
Mike Frysinger9171fc82008-03-30 15:46:13 -0400177/* Make sure our voltage value is sane so we don't blow up! */
178#ifndef CONFIG_VR_CTL_VAL
179# define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
180# if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
181# define CCLK_VLEV_120 400000000
182# define CCLK_VLEV_125 533000000
183# elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
184# define CCLK_VLEV_120 401000000
185# define CCLK_VLEV_125 401000000
186# elif defined(__ADSPBF561__)
187# define CCLK_VLEV_120 300000000
188# define CCLK_VLEV_125 501000000
189# endif
190# if BFIN_CCLK < CCLK_VLEV_120
191# define CONFIG_VR_CTL_VLEV VLEV_120
192# elif BFIN_CCLK < CCLK_VLEV_125
193# define CONFIG_VR_CTL_VLEV VLEV_125
194# else
195# define CONFIG_VR_CTL_VLEV VLEV_130
196# endif
197# if defined(__ADSPBF52x__) /* TBD; use default */
198# undef CONFIG_VR_CTL_VLEV
199# define CONFIG_VR_CTL_VLEV VLEV_110
200# elif defined(__ADSPBF54x__) /* TBD; use default */
201# undef CONFIG_VR_CTL_VLEV
202# define CONFIG_VR_CTL_VLEV VLEV_120
Mike Frysinger622a8dc2008-10-11 21:54:00 -0400203# elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
204# undef CONFIG_VR_CTL_VLEV
205# define CONFIG_VR_CTL_VLEV VLEV_125
Mike Frysinger9171fc82008-03-30 15:46:13 -0400206# endif
207
208# ifdef CONFIG_BFIN_MAC
209# define CONFIG_VR_CTL_CLKBUF CLKBUFOE
210# else
211# define CONFIG_VR_CTL_CLKBUF 0
212# endif
213
214# if defined(__ADSPBF52x__)
215# define CONFIG_VR_CTL_FREQ FREQ_1000
216# else
217# define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
218# endif
219
220# define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
221#endif
222
Mike Frysingerd347d572008-10-11 21:56:08 -0400223/* some parts do not have an on-chip voltage regulator */
224#if defined(__ADSPBF51x__)
225# define CONFIG_HAS_VR 0
226# undef CONFIG_VR_CTL_VAL
227# define CONFIG_VR_CTL_VAL 0
228#else
229# define CONFIG_HAS_VR 1
230#endif
231
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500232#if CONFIG_MEM_SIZE
Mike Frysinger0d4f24b2008-06-01 01:28:24 -0400233#ifndef EBIU_RSTCTL
234/* Blackfin with SDRAM */
235#ifndef CONFIG_EBIU_SDBCTL_VAL
236# if CONFIG_MEM_SIZE == 16
237# define CONFIG_EBSZ_VAL EBSZ_16
238# elif CONFIG_MEM_SIZE == 32
239# define CONFIG_EBSZ_VAL EBSZ_32
240# elif CONFIG_MEM_SIZE == 64
241# define CONFIG_EBSZ_VAL EBSZ_64
242# elif CONFIG_MEM_SIZE == 128
243# define CONFIG_EBSZ_VAL EBSZ_128
244# elif CONFIG_MEM_SIZE == 256
245# define CONFIG_EBSZ_VAL EBSZ_256
246# elif CONFIG_MEM_SIZE == 512
247# define CONFIG_EBSZ_VAL EBSZ_512
248# else
249# error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
250# endif
251# if CONFIG_MEM_ADD_WDTH == 8
252# define CONFIG_EBCAW_VAL EBCAW_8
253# elif CONFIG_MEM_ADD_WDTH == 9
254# define CONFIG_EBCAW_VAL EBCAW_9
255# elif CONFIG_MEM_ADD_WDTH == 10
256# define CONFIG_EBCAW_VAL EBCAW_10
257# elif CONFIG_MEM_ADD_WDTH == 11
258# define CONFIG_EBCAW_VAL EBCAW_11
259# else
260# error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
261# endif
262# define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
263#endif
264#endif
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500265#endif
Mike Frysinger0d4f24b2008-06-01 01:28:24 -0400266
Mike Frysinger8ef929a2009-04-04 08:40:13 -0400267/* Conflicting Column Address Widths Causes SDRAM Errors:
268 * EB2CAW and EB3CAW must be the same
269 */
270#if ANOMALY_05000362
271# if ((CONFIG_EBIU_SDBCTL_VAL & 0x30000000) >> 8) != (CONFIG_EBIU_SDBCTL_VAL & 0x00300000)
272# error "Anomaly 05000362: EB2CAW and EB3CAW must be the same"
273# endif
274#endif
275
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500276__attribute__((always_inline)) static inline void
277program_early_devices(ADI_BOOT_DATA *bs, uint *sdivB, uint *divB, uint *vcoB)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400278{
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500279 serial_putc('a');
Mike Frysingerad907322009-02-13 17:10:58 -0500280
Mike Frysingerf790ef62008-12-10 12:33:54 -0500281 /* Save the clock pieces that are used in baud rate calculation */
Mike Frysingerf790ef62008-12-10 12:33:54 -0500282 if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500283 serial_putc('b');
284 *sdivB = bfin_read_PLL_DIV() & 0xf;
285 *vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
286 *divB = serial_early_get_div();
287 serial_putc('c');
Mike Frysingerf790ef62008-12-10 12:33:54 -0500288 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400289
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500290 serial_putc('d');
Mike Frysingerad907322009-02-13 17:10:58 -0500291
Mike Frysinger9171fc82008-03-30 15:46:13 -0400292#ifdef CONFIG_HW_WATCHDOG
293# ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
294# define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
295# endif
296 /* Program the watchdog with an initial timeout of ~20 seconds.
297 * Hopefully that should be long enough to load the u-boot LDR
298 * (from wherever) and then the common u-boot code can take over.
299 * In bypass mode, the start.S would have already set a much lower
300 * timeout, so don't clobber that.
301 */
302 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500303 serial_putc('e');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400304 bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
305 bfin_write_WDOG_CTL(0);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500306 serial_putc('f');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400307 }
308#endif
309
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500310 serial_putc('g');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400311
312 /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
313 * fast read, so we need to slow down the SPI clock a lot more during
314 * boot. Once we switch over to u-boot's SPI flash driver, we'll
315 * increase the speed appropriately.
316 */
Mike Frysinger97f265f2008-12-09 17:21:08 -0500317 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500318 serial_putc('h');
Mike Frysinger97f265f2008-12-09 17:21:08 -0500319 if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500320 bs->dFlags |= BFLAG_FASTREAD;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400321 bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500322 serial_putc('i');
Mike Frysinger97f265f2008-12-09 17:21:08 -0500323 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400324
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500325 serial_putc('j');
326}
327
328__attribute__((always_inline)) static inline bool
329maybe_self_refresh(ADI_BOOT_DATA *bs)
330{
331 serial_putc('a');
332
333 if (!CONFIG_MEM_SIZE)
334 return false;
335
336 /* If external memory is enabled, put it into self refresh first. */
Mike Frysingercca07412010-12-17 15:25:09 -0500337#if defined(EBIU_RSTCTL)
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500338 if (bfin_read_EBIU_RSTCTL() & DDR_SRESET) {
339 serial_putc('b');
340 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | SRREQ);
341 return true;
342 }
Mike Frysingercca07412010-12-17 15:25:09 -0500343#elif defined(EBIU_SDGCTL)
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500344 if (bfin_read_EBIU_SDBCTL() & EBE) {
345 serial_putc('b');
346 bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() | SRFS);
347 return true;
348 }
349#endif
350
351 serial_putc('c');
352
353 return false;
354}
355
356__attribute__((always_inline)) static inline u16
357program_clocks(ADI_BOOT_DATA *bs, bool put_into_srfs)
358{
359 u16 vr_ctl;
360
361 serial_putc('a');
362
363 vr_ctl = bfin_read_VR_CTL();
364
365 serial_putc('b');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400366
Mike Frysinger74398b22008-10-11 21:58:33 -0400367 /* If we're entering self refresh, make sure it has happened. */
368 if (put_into_srfs)
Mike Frysingercca07412010-12-17 15:25:09 -0500369#if defined(EBIU_RSTCTL)
Mike Frysinger74398b22008-10-11 21:58:33 -0400370 while (!(bfin_read_EBIU_RSTCTL() & SRACK))
Mike Frysinger74398b22008-10-11 21:58:33 -0400371 continue;
Mike Frysingercca07412010-12-17 15:25:09 -0500372#elif defined(EBIU_SDGCTL)
373 while (!(bfin_read_EBIU_SDSTAT() & SDSRA))
374 continue;
375#else
376 ;
377#endif
Mike Frysinger74398b22008-10-11 21:58:33 -0400378
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500379 serial_putc('c');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400380
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400381 /* With newer bootroms, we use the helper function to set up
382 * the memory controller. Older bootroms lacks such helpers
383 * so we do it ourselves.
Mike Frysinger9171fc82008-03-30 15:46:13 -0400384 */
Mike Frysinger74398b22008-10-11 21:58:33 -0400385 if (!ANOMALY_05000386) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500386 serial_putc('d');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400387
Mike Frysingerc2e07442009-04-04 08:29:55 -0400388 /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400389 ADI_SYSCTRL_VALUES memory_settings;
Mike Frysinger5641f342010-10-14 14:29:17 -0400390 uint32_t actions = SYSCTRL_WRITE | SYSCTRL_PLLCTL | SYSCTRL_LOCKCNT;
391 if (!ANOMALY_05000440)
392 actions |= SYSCTRL_PLLDIV;
Mike Frysingerd347d572008-10-11 21:56:08 -0400393 if (CONFIG_HAS_VR) {
394 actions |= SYSCTRL_VRCTL;
395 if (CONFIG_VR_CTL_VAL & FREQ_MASK)
396 actions |= SYSCTRL_INTVOLTAGE;
397 else
398 actions |= SYSCTRL_EXTVOLTAGE;
399 memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
400 } else
401 actions |= SYSCTRL_EXTVOLTAGE;
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400402 memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
403 memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
404 memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
Mike Frysinger3986e982008-12-06 18:06:58 -0500405#if ANOMALY_05000432
406 bfin_write_SIC_IWR1(0);
407#endif
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500408 serial_putc('e');
Mike Frysingerd347d572008-10-11 21:56:08 -0400409 bfrom_SysControl(actions, &memory_settings, NULL);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500410 serial_putc('f');
Mike Frysinger5641f342010-10-14 14:29:17 -0400411 if (ANOMALY_05000440)
412 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
Mike Frysinger3986e982008-12-06 18:06:58 -0500413#if ANOMALY_05000432
414 bfin_write_SIC_IWR1(-1);
415#endif
Mike Frysingerce1fe4b2009-04-04 08:09:24 -0400416#if ANOMALY_05000171
417 bfin_write_SICA_IWR0(-1);
418 bfin_write_SICA_IWR1(-1);
419#endif
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500420 serial_putc('g');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400421 } else {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500422 serial_putc('h');
Mike Frysinger74398b22008-10-11 21:58:33 -0400423
424 /* Disable all peripheral wakeups except for the PLL event. */
425#ifdef SIC_IWR0
426 bfin_write_SIC_IWR0(1);
427 bfin_write_SIC_IWR1(0);
428# ifdef SIC_IWR2
429 bfin_write_SIC_IWR2(0);
430# endif
431#elif defined(SICA_IWR0)
432 bfin_write_SICA_IWR0(1);
433 bfin_write_SICA_IWR1(0);
434#else
435 bfin_write_SIC_IWR(1);
436#endif
437
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500438 serial_putc('i');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400439
Mike Frysingerc2e07442009-04-04 08:29:55 -0400440 /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400441 bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400442
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500443 serial_putc('j');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400444
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400445 /* Only reprogram when needed to avoid triggering unnecessary
446 * PLL relock sequences.
447 */
Mike Frysinger74398b22008-10-11 21:58:33 -0400448 if (vr_ctl != CONFIG_VR_CTL_VAL) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500449 serial_putc('?');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400450 bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
451 asm("idle;");
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500452 serial_putc('!');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400453 }
454
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500455 serial_putc('k');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400456
457 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
458
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500459 serial_putc('l');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400460
461 /* Only reprogram when needed to avoid triggering unnecessary
462 * PLL relock sequences.
463 */
Mike Frysinger48ab1502009-04-04 08:10:22 -0400464 if (ANOMALY_05000242 || bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500465 serial_putc('?');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400466 bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
467 asm("idle;");
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500468 serial_putc('!');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400469 }
Mike Frysinger74398b22008-10-11 21:58:33 -0400470
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500471 serial_putc('m');
Mike Frysinger74398b22008-10-11 21:58:33 -0400472
473 /* Restore all peripheral wakeups. */
474#ifdef SIC_IWR0
475 bfin_write_SIC_IWR0(-1);
476 bfin_write_SIC_IWR1(-1);
477# ifdef SIC_IWR2
478 bfin_write_SIC_IWR2(-1);
479# endif
480#elif defined(SICA_IWR0)
481 bfin_write_SICA_IWR0(-1);
482 bfin_write_SICA_IWR1(-1);
483#else
484 bfin_write_SIC_IWR(-1);
485#endif
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500486
487 serial_putc('n');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400488 }
489
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500490 serial_putc('o');
491
492 return vr_ctl;
493}
494
495__attribute__((always_inline)) static inline void
496update_serial_clocks(ADI_BOOT_DATA *bs, uint sdivB, uint divB, uint vcoB)
497{
498 serial_putc('a');
Mike Frysinger74398b22008-10-11 21:58:33 -0400499
Mike Frysinger9171fc82008-03-30 15:46:13 -0400500 /* Since we've changed the SCLK above, we may need to update
501 * the UART divisors (UART baud rates are based on SCLK).
Mike Frysingerf790ef62008-12-10 12:33:54 -0500502 * Do the division by hand as there are no native instructions
503 * for dividing which means we'd generate a libgcc reference.
Mike Frysinger9171fc82008-03-30 15:46:13 -0400504 */
Mike Frysingerf790ef62008-12-10 12:33:54 -0500505 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500506 serial_putc('b');
Mike Frysingerf790ef62008-12-10 12:33:54 -0500507 unsigned int sdivR, vcoR;
508 sdivR = bfin_read_PLL_DIV() & 0xf;
509 vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
510 int dividend = sdivB * divB * vcoR;
511 int divisor = vcoB * sdivR;
512 unsigned int quotient;
513 for (quotient = 0; dividend > 0; ++quotient)
514 dividend -= divisor;
Mike Frysinger635f3302011-04-29 23:23:28 -0400515 serial_early_put_div(UART_DLL, quotient - ANOMALY_05000230);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500516 serial_putc('c');
Mike Frysingerf790ef62008-12-10 12:33:54 -0500517 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400518
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500519 serial_putc('d');
520}
521
522__attribute__((always_inline)) static inline void
523program_memory_controller(ADI_BOOT_DATA *bs, bool put_into_srfs)
524{
525 serial_putc('a');
526
527 if (!CONFIG_MEM_SIZE)
528 return;
529
530 serial_putc('b');
Mike Frysinger74398b22008-10-11 21:58:33 -0400531
532 /* Program the external memory controller before we come out of
533 * self-refresh. This only works with our SDRAM controller.
534 */
Mike Frysingercca07412010-12-17 15:25:09 -0500535#ifdef EBIU_SDGCTL
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500536# ifdef CONFIG_EBIU_SDRRC_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400537 bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500538# endif
539# ifdef CONFIG_EBIU_SDBCTL_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400540 bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500541# endif
542# ifdef CONFIG_EBIU_SDGCTL_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400543 bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500544# endif
Mike Frysinger74398b22008-10-11 21:58:33 -0400545#endif
546
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500547 serial_putc('c');
Mike Frysinger74398b22008-10-11 21:58:33 -0400548
549 /* Now that we've reprogrammed, take things out of self refresh. */
550 if (put_into_srfs)
Mike Frysingercca07412010-12-17 15:25:09 -0500551#if defined(EBIU_RSTCTL)
Mike Frysinger74398b22008-10-11 21:58:33 -0400552 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() & ~(SRREQ));
Mike Frysingercca07412010-12-17 15:25:09 -0500553#elif defined(EBIU_SDGCTL)
Mike Frysinger74398b22008-10-11 21:58:33 -0400554 bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() & ~(SRFS));
555#endif
556
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500557 serial_putc('d');
Mike Frysinger74398b22008-10-11 21:58:33 -0400558
559 /* Our DDR controller sucks and cannot be programmed while in
560 * self-refresh. So we have to pull it out before programming.
561 */
562#ifdef EBIU_RSTCTL
Mike Frysinger7527fee2009-11-09 19:38:23 -0500563# ifdef CONFIG_EBIU_RSTCTL_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400564 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500565# endif
566# ifdef CONFIG_EBIU_DDRCTL0_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400567 bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500568# endif
569# ifdef CONFIG_EBIU_DDRCTL1_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400570 bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500571# endif
572# ifdef CONFIG_EBIU_DDRCTL2_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400573 bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500574# endif
Mike Frysinger74398b22008-10-11 21:58:33 -0400575# ifdef CONFIG_EBIU_DDRCTL3_VAL
576 /* default is disable, so don't need to force this */
577 bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
578# endif
579# ifdef CONFIG_EBIU_DDRQUE_VAL
580 bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE() | CONFIG_EBIU_DDRQUE_VAL);
581# endif
582#endif
583
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500584 serial_putc('e');
585}
586
587__attribute__((always_inline)) static inline void
588check_hibernation(ADI_BOOT_DATA *bs, u16 vr_ctl, bool put_into_srfs)
589{
590 serial_putc('a');
591
592 if (!CONFIG_MEM_SIZE)
593 return;
594
595 serial_putc('b');
Mike Frysinger74398b22008-10-11 21:58:33 -0400596
597 /* Are we coming out of hibernate (suspend to memory) ?
598 * The memory layout is:
599 * 0x0: hibernate magic for anomaly 307 (0xDEADBEEF)
600 * 0x4: return address
601 * 0x8: stack pointer
602 *
603 * SCKELOW is unreliable on older parts (anomaly 307)
604 */
605 if (ANOMALY_05000307 || vr_ctl & 0x8000) {
606 uint32_t *hibernate_magic = 0;
607 __builtin_bfin_ssync(); /* make sure memory controller is done */
608 if (hibernate_magic[0] == 0xDEADBEEF) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500609 serial_putc('c');
Mike Frysinger74398b22008-10-11 21:58:33 -0400610 bfin_write_EVT15(hibernate_magic[1]);
611 bfin_write_IMASK(EVT_IVG15);
612 __asm__ __volatile__ (
613 /* load reti early to avoid anomaly 281 */
614 "reti = %0;"
615 /* clear hibernate magic */
616 "[%0] = %1;"
617 /* load stack pointer */
618 "SP = [%0 + 8];"
619 /* lower ourselves from reset ivg to ivg15 */
620 "raise 15;"
621 "rti;"
622 :
623 : "p"(hibernate_magic), "d"(0x2000 /* jump.s 0 */)
624 );
625 }
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500626 serial_putc('d');
Mike Frysinger74398b22008-10-11 21:58:33 -0400627 }
628
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500629 serial_putc('e');
630}
631
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500632BOOTROM_CALLED_FUNC_ATTR
633void initcode(ADI_BOOT_DATA *bs)
634{
635 ADI_BOOT_DATA bootstruct_scratch;
636
Mike Frysingerce53fc62010-05-05 02:07:44 -0400637 /* Setup NMI handler before anything else */
638 program_nmi_handler();
639
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500640 serial_init();
641
642 serial_putc('A');
643
644 /* If the bootstruct is NULL, then it's because we're loading
645 * dynamically and not via LDR (bootrom). So set the struct to
646 * some scratch space.
647 */
648 if (!bs)
649 bs = &bootstruct_scratch;
650
651 serial_putc('B');
652 bool put_into_srfs = maybe_self_refresh(bs);
653
654 serial_putc('C');
655 uint sdivB, divB, vcoB;
656 program_early_devices(bs, &sdivB, &divB, &vcoB);
657
658 serial_putc('D');
659 u16 vr_ctl = program_clocks(bs, put_into_srfs);
660
661 serial_putc('E');
662 update_serial_clocks(bs, sdivB, divB, vcoB);
663
664 serial_putc('F');
665 program_memory_controller(bs, put_into_srfs);
666
667 serial_putc('G');
668 check_hibernation(bs, vr_ctl, put_into_srfs);
669
670 serial_putc('H');
671 program_async_controller(bs);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400672
Mike Frysinger02778f22009-04-24 23:39:41 -0400673#ifdef CONFIG_BFIN_BOOTROM_USES_EVT1
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500674 serial_putc('I');
Mike Frysingerb30453a2010-04-29 02:49:41 -0400675 /* Tell the bootrom where our entry point is so that it knows
676 * where to jump to when finishing processing the LDR. This
677 * allows us to avoid small jump blocks in the LDR, and also
678 * works around anomaly 05000389 (init address in external
679 * memory causes bootrom to trigger external addressing IVHW).
680 */
Mike Frysinger7e1d2122008-10-18 04:04:49 -0400681 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
682 bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);
Mike Frysinger02778f22009-04-24 23:39:41 -0400683#endif
Mike Frysinger7e1d2122008-10-18 04:04:49 -0400684
Mike Frysinger9171fc82008-03-30 15:46:13 -0400685 serial_putc('>');
686 serial_putc('\n');
687
688 serial_deinit();
689}