blob: 61dc5ab0eb55aefa40a55cd7d240ddbc0a0201aa [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
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 Frysingerce53fc62010-05-05 02:07:44 -0400110__attribute__((always_inline)) static inline void
111program_nmi_handler(void)
112{
113 u32 tmp1, tmp2;
114
115 /* Older bootroms don't create a dummy NMI handler,
116 * so make one ourselves ASAP in case it fires.
117 */
118 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS && !ANOMALY_05000219)
119 return;
120
121 asm volatile (
122 "%0 = RETS;" /* Save current RETS */
123 "CALL 1f;" /* Figure out current PC */
124 "RTN;" /* The simple NMI handler */
125 "1:"
126 "%1 = RETS;" /* Load addr of NMI handler */
127 "RETS = %0;" /* Restore RETS */
128 "[%2] = %1;" /* Write NMI handler */
129 : "=r"(tmp1), "=r"(tmp2) : "ab"(EVT2)
130 );
131}
Mike Frysinger9171fc82008-03-30 15:46:13 -0400132
Mike Frysinger97f265f2008-12-09 17:21:08 -0500133/* Max SCLK can be 133MHz ... dividing that by (2*4) gives
134 * us a freq of 16MHz for SPI which should generally be
Mike Frysinger9171fc82008-03-30 15:46:13 -0400135 * slow enough for the slow reads the bootrom uses.
136 */
Mike Frysinger97f265f2008-12-09 17:21:08 -0500137#if !defined(CONFIG_SPI_FLASH_SLOW_READ) && \
138 ((defined(__ADSPBF52x__) && __SILICON_REVISION__ >= 2) || \
139 (defined(__ADSPBF54x__) && __SILICON_REVISION__ >= 1))
140# define BOOTROM_SUPPORTS_SPI_FAST_READ 1
141#else
142# define BOOTROM_SUPPORTS_SPI_FAST_READ 0
143#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400144#ifndef CONFIG_SPI_BAUD_INITBLOCK
Mike Frysinger97f265f2008-12-09 17:21:08 -0500145# define CONFIG_SPI_BAUD_INITBLOCK (BOOTROM_SUPPORTS_SPI_FAST_READ ? 2 : 4)
146#endif
147#ifdef SPI0_BAUD
148# define bfin_write_SPI_BAUD bfin_write_SPI0_BAUD
Mike Frysinger9171fc82008-03-30 15:46:13 -0400149#endif
150
151/* PLL_DIV defines */
152#ifndef CONFIG_PLL_DIV_VAL
153# if (CONFIG_CCLK_DIV == 1)
154# define CONFIG_CCLK_ACT_DIV CCLK_DIV1
155# elif (CONFIG_CCLK_DIV == 2)
156# define CONFIG_CCLK_ACT_DIV CCLK_DIV2
157# elif (CONFIG_CCLK_DIV == 4)
158# define CONFIG_CCLK_ACT_DIV CCLK_DIV4
159# elif (CONFIG_CCLK_DIV == 8)
160# define CONFIG_CCLK_ACT_DIV CCLK_DIV8
161# else
162# define CONFIG_CCLK_ACT_DIV CONFIG_CCLK_DIV_not_defined_properly
163# endif
164# define CONFIG_PLL_DIV_VAL (CONFIG_CCLK_ACT_DIV | CONFIG_SCLK_DIV)
165#endif
166
167#ifndef CONFIG_PLL_LOCKCNT_VAL
168# define CONFIG_PLL_LOCKCNT_VAL 0x0300
169#endif
170
171#ifndef CONFIG_PLL_CTL_VAL
Mike Frysinger4f6a3132008-06-01 01:26:29 -0400172# define CONFIG_PLL_CTL_VAL (SPORT_HYST | (CONFIG_VCO_MULT << 9) | CONFIG_CLKIN_HALF)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400173#endif
174
175#ifndef CONFIG_EBIU_RSTCTL_VAL
176# define CONFIG_EBIU_RSTCTL_VAL 0 /* only MDDRENABLE is useful */
177#endif
Mike Frysinger67619982008-10-11 21:46:52 -0400178#if ((CONFIG_EBIU_RSTCTL_VAL & 0xFFFFFFC4) != 0)
179# error invalid EBIU_RSTCTL value: must not set reserved bits
180#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400181
182#ifndef CONFIG_EBIU_MBSCTL_VAL
183# define CONFIG_EBIU_MBSCTL_VAL 0
184#endif
185
Mike Frysinger67619982008-10-11 21:46:52 -0400186#if defined(CONFIG_EBIU_DDRQUE_VAL) && ((CONFIG_EBIU_DDRQUE_VAL & 0xFFFF8000) != 0)
187# error invalid EBIU_DDRQUE value: must not set reserved bits
188#endif
189
Mike Frysinger9171fc82008-03-30 15:46:13 -0400190/* Make sure our voltage value is sane so we don't blow up! */
191#ifndef CONFIG_VR_CTL_VAL
192# define BFIN_CCLK ((CONFIG_CLKIN_HZ * CONFIG_VCO_MULT) / CONFIG_CCLK_DIV)
193# if defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__)
194# define CCLK_VLEV_120 400000000
195# define CCLK_VLEV_125 533000000
196# elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__)
197# define CCLK_VLEV_120 401000000
198# define CCLK_VLEV_125 401000000
199# elif defined(__ADSPBF561__)
200# define CCLK_VLEV_120 300000000
201# define CCLK_VLEV_125 501000000
202# endif
203# if BFIN_CCLK < CCLK_VLEV_120
204# define CONFIG_VR_CTL_VLEV VLEV_120
205# elif BFIN_CCLK < CCLK_VLEV_125
206# define CONFIG_VR_CTL_VLEV VLEV_125
207# else
208# define CONFIG_VR_CTL_VLEV VLEV_130
209# endif
210# if defined(__ADSPBF52x__) /* TBD; use default */
211# undef CONFIG_VR_CTL_VLEV
212# define CONFIG_VR_CTL_VLEV VLEV_110
213# elif defined(__ADSPBF54x__) /* TBD; use default */
214# undef CONFIG_VR_CTL_VLEV
215# define CONFIG_VR_CTL_VLEV VLEV_120
Mike Frysinger622a8dc2008-10-11 21:54:00 -0400216# elif defined(__ADSPBF538__) || defined(__ADSPBF539__) /* TBD; use default */
217# undef CONFIG_VR_CTL_VLEV
218# define CONFIG_VR_CTL_VLEV VLEV_125
Mike Frysinger9171fc82008-03-30 15:46:13 -0400219# endif
220
221# ifdef CONFIG_BFIN_MAC
222# define CONFIG_VR_CTL_CLKBUF CLKBUFOE
223# else
224# define CONFIG_VR_CTL_CLKBUF 0
225# endif
226
227# if defined(__ADSPBF52x__)
228# define CONFIG_VR_CTL_FREQ FREQ_1000
229# else
230# define CONFIG_VR_CTL_FREQ (GAIN_20 | FREQ_1000)
231# endif
232
233# define CONFIG_VR_CTL_VAL (CONFIG_VR_CTL_CLKBUF | CONFIG_VR_CTL_VLEV | CONFIG_VR_CTL_FREQ)
234#endif
235
Mike Frysingerd347d572008-10-11 21:56:08 -0400236/* some parts do not have an on-chip voltage regulator */
237#if defined(__ADSPBF51x__)
238# define CONFIG_HAS_VR 0
239# undef CONFIG_VR_CTL_VAL
240# define CONFIG_VR_CTL_VAL 0
241#else
242# define CONFIG_HAS_VR 1
243#endif
244
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500245#if CONFIG_MEM_SIZE
Mike Frysinger0d4f24b2008-06-01 01:28:24 -0400246#ifndef EBIU_RSTCTL
247/* Blackfin with SDRAM */
248#ifndef CONFIG_EBIU_SDBCTL_VAL
249# if CONFIG_MEM_SIZE == 16
250# define CONFIG_EBSZ_VAL EBSZ_16
251# elif CONFIG_MEM_SIZE == 32
252# define CONFIG_EBSZ_VAL EBSZ_32
253# elif CONFIG_MEM_SIZE == 64
254# define CONFIG_EBSZ_VAL EBSZ_64
255# elif CONFIG_MEM_SIZE == 128
256# define CONFIG_EBSZ_VAL EBSZ_128
257# elif CONFIG_MEM_SIZE == 256
258# define CONFIG_EBSZ_VAL EBSZ_256
259# elif CONFIG_MEM_SIZE == 512
260# define CONFIG_EBSZ_VAL EBSZ_512
261# else
262# error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_SIZE
263# endif
264# if CONFIG_MEM_ADD_WDTH == 8
265# define CONFIG_EBCAW_VAL EBCAW_8
266# elif CONFIG_MEM_ADD_WDTH == 9
267# define CONFIG_EBCAW_VAL EBCAW_9
268# elif CONFIG_MEM_ADD_WDTH == 10
269# define CONFIG_EBCAW_VAL EBCAW_10
270# elif CONFIG_MEM_ADD_WDTH == 11
271# define CONFIG_EBCAW_VAL EBCAW_11
272# else
273# error You need to define CONFIG_EBIU_SDBCTL_VAL or CONFIG_MEM_ADD_WDTH
274# endif
275# define CONFIG_EBIU_SDBCTL_VAL (CONFIG_EBCAW_VAL | CONFIG_EBSZ_VAL | EBE)
276#endif
277#endif
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500278#endif
Mike Frysinger0d4f24b2008-06-01 01:28:24 -0400279
Mike Frysinger8ef929a2009-04-04 08:40:13 -0400280/* Conflicting Column Address Widths Causes SDRAM Errors:
281 * EB2CAW and EB3CAW must be the same
282 */
283#if ANOMALY_05000362
284# if ((CONFIG_EBIU_SDBCTL_VAL & 0x30000000) >> 8) != (CONFIG_EBIU_SDBCTL_VAL & 0x00300000)
285# error "Anomaly 05000362: EB2CAW and EB3CAW must be the same"
286# endif
287#endif
288
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500289__attribute__((always_inline)) static inline void
290program_early_devices(ADI_BOOT_DATA *bs, uint *sdivB, uint *divB, uint *vcoB)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400291{
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500292 serial_putc('a');
Mike Frysingerad907322009-02-13 17:10:58 -0500293
Mike Frysingerf790ef62008-12-10 12:33:54 -0500294 /* Save the clock pieces that are used in baud rate calculation */
Mike Frysingerf790ef62008-12-10 12:33:54 -0500295 if (BFIN_DEBUG_EARLY_SERIAL || CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500296 serial_putc('b');
297 *sdivB = bfin_read_PLL_DIV() & 0xf;
298 *vcoB = (bfin_read_PLL_CTL() >> 9) & 0x3f;
299 *divB = serial_early_get_div();
300 serial_putc('c');
Mike Frysingerf790ef62008-12-10 12:33:54 -0500301 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400302
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500303 serial_putc('d');
Mike Frysingerad907322009-02-13 17:10:58 -0500304
Mike Frysinger9171fc82008-03-30 15:46:13 -0400305#ifdef CONFIG_HW_WATCHDOG
306# ifndef CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE
307# define CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE 20000
308# endif
309 /* Program the watchdog with an initial timeout of ~20 seconds.
310 * Hopefully that should be long enough to load the u-boot LDR
311 * (from wherever) and then the common u-boot code can take over.
312 * In bypass mode, the start.S would have already set a much lower
313 * timeout, so don't clobber that.
314 */
315 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500316 serial_putc('e');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400317 bfin_write_WDOG_CNT(MSEC_TO_SCLK(CONFIG_HW_WATCHDOG_TIMEOUT_INITCODE));
318 bfin_write_WDOG_CTL(0);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500319 serial_putc('f');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400320 }
321#endif
322
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500323 serial_putc('g');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400324
325 /* Blackfin bootroms use the SPI slow read opcode instead of the SPI
326 * fast read, so we need to slow down the SPI clock a lot more during
327 * boot. Once we switch over to u-boot's SPI flash driver, we'll
328 * increase the speed appropriately.
329 */
Mike Frysinger97f265f2008-12-09 17:21:08 -0500330 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_SPI_MASTER) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500331 serial_putc('h');
Mike Frysinger97f265f2008-12-09 17:21:08 -0500332 if (BOOTROM_SUPPORTS_SPI_FAST_READ && CONFIG_SPI_BAUD_INITBLOCK < 4)
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500333 bs->dFlags |= BFLAG_FASTREAD;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400334 bfin_write_SPI_BAUD(CONFIG_SPI_BAUD_INITBLOCK);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500335 serial_putc('i');
Mike Frysinger97f265f2008-12-09 17:21:08 -0500336 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400337
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500338 serial_putc('j');
339}
340
341__attribute__((always_inline)) static inline bool
342maybe_self_refresh(ADI_BOOT_DATA *bs)
343{
344 serial_putc('a');
345
346 if (!CONFIG_MEM_SIZE)
347 return false;
348
349 /* If external memory is enabled, put it into self refresh first. */
Mike Frysingercca07412010-12-17 15:25:09 -0500350#if defined(EBIU_RSTCTL)
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500351 if (bfin_read_EBIU_RSTCTL() & DDR_SRESET) {
352 serial_putc('b');
353 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | SRREQ);
354 return true;
355 }
Mike Frysingercca07412010-12-17 15:25:09 -0500356#elif defined(EBIU_SDGCTL)
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500357 if (bfin_read_EBIU_SDBCTL() & EBE) {
358 serial_putc('b');
359 bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() | SRFS);
360 return true;
361 }
362#endif
363
364 serial_putc('c');
365
366 return false;
367}
368
369__attribute__((always_inline)) static inline u16
370program_clocks(ADI_BOOT_DATA *bs, bool put_into_srfs)
371{
372 u16 vr_ctl;
373
374 serial_putc('a');
375
376 vr_ctl = bfin_read_VR_CTL();
377
378 serial_putc('b');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400379
Mike Frysinger74398b22008-10-11 21:58:33 -0400380 /* If we're entering self refresh, make sure it has happened. */
381 if (put_into_srfs)
Mike Frysingercca07412010-12-17 15:25:09 -0500382#if defined(EBIU_RSTCTL)
Mike Frysinger74398b22008-10-11 21:58:33 -0400383 while (!(bfin_read_EBIU_RSTCTL() & SRACK))
Mike Frysinger74398b22008-10-11 21:58:33 -0400384 continue;
Mike Frysingercca07412010-12-17 15:25:09 -0500385#elif defined(EBIU_SDGCTL)
386 while (!(bfin_read_EBIU_SDSTAT() & SDSRA))
387 continue;
388#else
389 ;
390#endif
Mike Frysinger74398b22008-10-11 21:58:33 -0400391
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500392 serial_putc('c');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400393
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400394 /* With newer bootroms, we use the helper function to set up
395 * the memory controller. Older bootroms lacks such helpers
396 * so we do it ourselves.
Mike Frysinger9171fc82008-03-30 15:46:13 -0400397 */
Mike Frysinger74398b22008-10-11 21:58:33 -0400398 if (!ANOMALY_05000386) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500399 serial_putc('d');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400400
Mike Frysingerc2e07442009-04-04 08:29:55 -0400401 /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400402 ADI_SYSCTRL_VALUES memory_settings;
Mike Frysinger5641f342010-10-14 14:29:17 -0400403 uint32_t actions = SYSCTRL_WRITE | SYSCTRL_PLLCTL | SYSCTRL_LOCKCNT;
404 if (!ANOMALY_05000440)
405 actions |= SYSCTRL_PLLDIV;
Mike Frysingerd347d572008-10-11 21:56:08 -0400406 if (CONFIG_HAS_VR) {
407 actions |= SYSCTRL_VRCTL;
408 if (CONFIG_VR_CTL_VAL & FREQ_MASK)
409 actions |= SYSCTRL_INTVOLTAGE;
410 else
411 actions |= SYSCTRL_EXTVOLTAGE;
412 memory_settings.uwVrCtl = CONFIG_VR_CTL_VAL;
413 } else
414 actions |= SYSCTRL_EXTVOLTAGE;
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400415 memory_settings.uwPllCtl = CONFIG_PLL_CTL_VAL;
416 memory_settings.uwPllDiv = CONFIG_PLL_DIV_VAL;
417 memory_settings.uwPllLockCnt = CONFIG_PLL_LOCKCNT_VAL;
Mike Frysinger3986e982008-12-06 18:06:58 -0500418#if ANOMALY_05000432
419 bfin_write_SIC_IWR1(0);
420#endif
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500421 serial_putc('e');
Mike Frysingerd347d572008-10-11 21:56:08 -0400422 bfrom_SysControl(actions, &memory_settings, NULL);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500423 serial_putc('f');
Mike Frysinger5641f342010-10-14 14:29:17 -0400424 if (ANOMALY_05000440)
425 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
Mike Frysinger3986e982008-12-06 18:06:58 -0500426#if ANOMALY_05000432
427 bfin_write_SIC_IWR1(-1);
428#endif
Mike Frysingerce1fe4b2009-04-04 08:09:24 -0400429#if ANOMALY_05000171
430 bfin_write_SICA_IWR0(-1);
431 bfin_write_SICA_IWR1(-1);
432#endif
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500433 serial_putc('g');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400434 } else {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500435 serial_putc('h');
Mike Frysinger74398b22008-10-11 21:58:33 -0400436
437 /* Disable all peripheral wakeups except for the PLL event. */
438#ifdef SIC_IWR0
439 bfin_write_SIC_IWR0(1);
440 bfin_write_SIC_IWR1(0);
441# ifdef SIC_IWR2
442 bfin_write_SIC_IWR2(0);
443# endif
444#elif defined(SICA_IWR0)
445 bfin_write_SICA_IWR0(1);
446 bfin_write_SICA_IWR1(0);
447#else
448 bfin_write_SIC_IWR(1);
449#endif
450
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500451 serial_putc('i');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400452
Mike Frysingerc2e07442009-04-04 08:29:55 -0400453 /* Always programming PLL_LOCKCNT avoids Anomaly 05000430 */
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400454 bfin_write_PLL_LOCKCNT(CONFIG_PLL_LOCKCNT_VAL);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400455
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500456 serial_putc('j');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400457
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400458 /* Only reprogram when needed to avoid triggering unnecessary
459 * PLL relock sequences.
460 */
Mike Frysinger74398b22008-10-11 21:58:33 -0400461 if (vr_ctl != CONFIG_VR_CTL_VAL) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500462 serial_putc('?');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400463 bfin_write_VR_CTL(CONFIG_VR_CTL_VAL);
464 asm("idle;");
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500465 serial_putc('!');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400466 }
467
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500468 serial_putc('k');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400469
470 bfin_write_PLL_DIV(CONFIG_PLL_DIV_VAL);
471
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500472 serial_putc('l');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400473
474 /* Only reprogram when needed to avoid triggering unnecessary
475 * PLL relock sequences.
476 */
Mike Frysinger48ab1502009-04-04 08:10:22 -0400477 if (ANOMALY_05000242 || bfin_read_PLL_CTL() != CONFIG_PLL_CTL_VAL) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500478 serial_putc('?');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400479 bfin_write_PLL_CTL(CONFIG_PLL_CTL_VAL);
480 asm("idle;");
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500481 serial_putc('!');
Mike Frysinger09dc6b02008-06-01 01:29:57 -0400482 }
Mike Frysinger74398b22008-10-11 21:58:33 -0400483
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500484 serial_putc('m');
Mike Frysinger74398b22008-10-11 21:58:33 -0400485
486 /* Restore all peripheral wakeups. */
487#ifdef SIC_IWR0
488 bfin_write_SIC_IWR0(-1);
489 bfin_write_SIC_IWR1(-1);
490# ifdef SIC_IWR2
491 bfin_write_SIC_IWR2(-1);
492# endif
493#elif defined(SICA_IWR0)
494 bfin_write_SICA_IWR0(-1);
495 bfin_write_SICA_IWR1(-1);
496#else
497 bfin_write_SIC_IWR(-1);
498#endif
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500499
500 serial_putc('n');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400501 }
502
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500503 serial_putc('o');
504
505 return vr_ctl;
506}
507
508__attribute__((always_inline)) static inline void
509update_serial_clocks(ADI_BOOT_DATA *bs, uint sdivB, uint divB, uint vcoB)
510{
511 serial_putc('a');
Mike Frysinger74398b22008-10-11 21:58:33 -0400512
Mike Frysinger9171fc82008-03-30 15:46:13 -0400513 /* Since we've changed the SCLK above, we may need to update
514 * the UART divisors (UART baud rates are based on SCLK).
Mike Frysingerf790ef62008-12-10 12:33:54 -0500515 * Do the division by hand as there are no native instructions
516 * for dividing which means we'd generate a libgcc reference.
Mike Frysinger9171fc82008-03-30 15:46:13 -0400517 */
Mike Frysingerf790ef62008-12-10 12:33:54 -0500518 if (CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_UART) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500519 serial_putc('b');
Mike Frysingerf790ef62008-12-10 12:33:54 -0500520 unsigned int sdivR, vcoR;
521 sdivR = bfin_read_PLL_DIV() & 0xf;
522 vcoR = (bfin_read_PLL_CTL() >> 9) & 0x3f;
523 int dividend = sdivB * divB * vcoR;
524 int divisor = vcoB * sdivR;
525 unsigned int quotient;
526 for (quotient = 0; dividend > 0; ++quotient)
527 dividend -= divisor;
Mike Frysinger635f3302011-04-29 23:23:28 -0400528 serial_early_put_div(UART_DLL, quotient - ANOMALY_05000230);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500529 serial_putc('c');
Mike Frysingerf790ef62008-12-10 12:33:54 -0500530 }
Mike Frysinger9171fc82008-03-30 15:46:13 -0400531
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500532 serial_putc('d');
533}
534
535__attribute__((always_inline)) static inline void
536program_memory_controller(ADI_BOOT_DATA *bs, bool put_into_srfs)
537{
538 serial_putc('a');
539
540 if (!CONFIG_MEM_SIZE)
541 return;
542
543 serial_putc('b');
Mike Frysinger74398b22008-10-11 21:58:33 -0400544
545 /* Program the external memory controller before we come out of
546 * self-refresh. This only works with our SDRAM controller.
547 */
Mike Frysingercca07412010-12-17 15:25:09 -0500548#ifdef EBIU_SDGCTL
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500549# ifdef CONFIG_EBIU_SDRRC_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400550 bfin_write_EBIU_SDRRC(CONFIG_EBIU_SDRRC_VAL);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500551# endif
552# ifdef CONFIG_EBIU_SDBCTL_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400553 bfin_write_EBIU_SDBCTL(CONFIG_EBIU_SDBCTL_VAL);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500554# endif
555# ifdef CONFIG_EBIU_SDGCTL_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400556 bfin_write_EBIU_SDGCTL(CONFIG_EBIU_SDGCTL_VAL);
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500557# endif
Mike Frysinger74398b22008-10-11 21:58:33 -0400558#endif
559
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500560 serial_putc('c');
Mike Frysinger74398b22008-10-11 21:58:33 -0400561
562 /* Now that we've reprogrammed, take things out of self refresh. */
563 if (put_into_srfs)
Mike Frysingercca07412010-12-17 15:25:09 -0500564#if defined(EBIU_RSTCTL)
Mike Frysinger74398b22008-10-11 21:58:33 -0400565 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() & ~(SRREQ));
Mike Frysingercca07412010-12-17 15:25:09 -0500566#elif defined(EBIU_SDGCTL)
Mike Frysinger74398b22008-10-11 21:58:33 -0400567 bfin_write_EBIU_SDGCTL(bfin_read_EBIU_SDGCTL() & ~(SRFS));
568#endif
569
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500570 serial_putc('d');
Mike Frysinger74398b22008-10-11 21:58:33 -0400571
572 /* Our DDR controller sucks and cannot be programmed while in
573 * self-refresh. So we have to pull it out before programming.
574 */
575#ifdef EBIU_RSTCTL
Mike Frysinger7527fee2009-11-09 19:38:23 -0500576# ifdef CONFIG_EBIU_RSTCTL_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400577 bfin_write_EBIU_RSTCTL(bfin_read_EBIU_RSTCTL() | 0x1 /*DDRSRESET*/ | CONFIG_EBIU_RSTCTL_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500578# endif
579# ifdef CONFIG_EBIU_DDRCTL0_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400580 bfin_write_EBIU_DDRCTL0(CONFIG_EBIU_DDRCTL0_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500581# endif
582# ifdef CONFIG_EBIU_DDRCTL1_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400583 bfin_write_EBIU_DDRCTL1(CONFIG_EBIU_DDRCTL1_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500584# endif
585# ifdef CONFIG_EBIU_DDRCTL2_VAL
Mike Frysinger74398b22008-10-11 21:58:33 -0400586 bfin_write_EBIU_DDRCTL2(CONFIG_EBIU_DDRCTL2_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500587# endif
Mike Frysinger74398b22008-10-11 21:58:33 -0400588# ifdef CONFIG_EBIU_DDRCTL3_VAL
589 /* default is disable, so don't need to force this */
590 bfin_write_EBIU_DDRCTL3(CONFIG_EBIU_DDRCTL3_VAL);
591# endif
592# ifdef CONFIG_EBIU_DDRQUE_VAL
593 bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE() | CONFIG_EBIU_DDRQUE_VAL);
594# endif
595#endif
596
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500597 serial_putc('e');
598}
599
600__attribute__((always_inline)) static inline void
601check_hibernation(ADI_BOOT_DATA *bs, u16 vr_ctl, bool put_into_srfs)
602{
603 serial_putc('a');
604
605 if (!CONFIG_MEM_SIZE)
606 return;
607
608 serial_putc('b');
Mike Frysinger74398b22008-10-11 21:58:33 -0400609
610 /* Are we coming out of hibernate (suspend to memory) ?
611 * The memory layout is:
612 * 0x0: hibernate magic for anomaly 307 (0xDEADBEEF)
613 * 0x4: return address
614 * 0x8: stack pointer
615 *
616 * SCKELOW is unreliable on older parts (anomaly 307)
617 */
618 if (ANOMALY_05000307 || vr_ctl & 0x8000) {
619 uint32_t *hibernate_magic = 0;
620 __builtin_bfin_ssync(); /* make sure memory controller is done */
621 if (hibernate_magic[0] == 0xDEADBEEF) {
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500622 serial_putc('c');
Mike Frysinger74398b22008-10-11 21:58:33 -0400623 bfin_write_EVT15(hibernate_magic[1]);
624 bfin_write_IMASK(EVT_IVG15);
625 __asm__ __volatile__ (
626 /* load reti early to avoid anomaly 281 */
627 "reti = %0;"
628 /* clear hibernate magic */
629 "[%0] = %1;"
630 /* load stack pointer */
631 "SP = [%0 + 8];"
632 /* lower ourselves from reset ivg to ivg15 */
633 "raise 15;"
634 "rti;"
635 :
636 : "p"(hibernate_magic), "d"(0x2000 /* jump.s 0 */)
637 );
638 }
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500639 serial_putc('d');
Mike Frysinger74398b22008-10-11 21:58:33 -0400640 }
641
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500642 serial_putc('e');
643}
644
645__attribute__((always_inline)) static inline void
646program_async_controller(ADI_BOOT_DATA *bs)
647{
648 serial_putc('a');
Mike Frysinger9171fc82008-03-30 15:46:13 -0400649
650 /* Program the async banks controller. */
651 bfin_write_EBIU_AMBCTL0(CONFIG_EBIU_AMBCTL0_VAL);
652 bfin_write_EBIU_AMBCTL1(CONFIG_EBIU_AMBCTL1_VAL);
653 bfin_write_EBIU_AMGCTL(CONFIG_EBIU_AMGCTL_VAL);
654
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500655 serial_putc('b');
656
Mike Frysinger9171fc82008-03-30 15:46:13 -0400657 /* Not all parts have these additional MMRs. */
Mike Frysingercca07412010-12-17 15:25:09 -0500658#ifdef EBIU_MBSCTL
Mike Frysinger9171fc82008-03-30 15:46:13 -0400659 bfin_write_EBIU_MBSCTL(CONFIG_EBIU_MBSCTL_VAL);
Mike Frysingercca07412010-12-17 15:25:09 -0500660#endif
661#ifdef EBIU_MODE
Mike Frysinger7527fee2009-11-09 19:38:23 -0500662# ifdef CONFIG_EBIU_MODE_VAL
Mike Frysinger9171fc82008-03-30 15:46:13 -0400663 bfin_write_EBIU_MODE(CONFIG_EBIU_MODE_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500664# endif
665# ifdef CONFIG_EBIU_FCTL_VAL
Mike Frysinger9171fc82008-03-30 15:46:13 -0400666 bfin_write_EBIU_FCTL(CONFIG_EBIU_FCTL_VAL);
Mike Frysinger7527fee2009-11-09 19:38:23 -0500667# endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400668#endif
669
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500670 serial_putc('c');
671}
672
673BOOTROM_CALLED_FUNC_ATTR
674void initcode(ADI_BOOT_DATA *bs)
675{
676 ADI_BOOT_DATA bootstruct_scratch;
677
Mike Frysingerce53fc62010-05-05 02:07:44 -0400678 /* Setup NMI handler before anything else */
679 program_nmi_handler();
680
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500681 serial_init();
682
683 serial_putc('A');
684
685 /* If the bootstruct is NULL, then it's because we're loading
686 * dynamically and not via LDR (bootrom). So set the struct to
687 * some scratch space.
688 */
689 if (!bs)
690 bs = &bootstruct_scratch;
691
692 serial_putc('B');
693 bool put_into_srfs = maybe_self_refresh(bs);
694
695 serial_putc('C');
696 uint sdivB, divB, vcoB;
697 program_early_devices(bs, &sdivB, &divB, &vcoB);
698
699 serial_putc('D');
700 u16 vr_ctl = program_clocks(bs, put_into_srfs);
701
702 serial_putc('E');
703 update_serial_clocks(bs, sdivB, divB, vcoB);
704
705 serial_putc('F');
706 program_memory_controller(bs, put_into_srfs);
707
708 serial_putc('G');
709 check_hibernation(bs, vr_ctl, put_into_srfs);
710
711 serial_putc('H');
712 program_async_controller(bs);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400713
Mike Frysinger02778f22009-04-24 23:39:41 -0400714#ifdef CONFIG_BFIN_BOOTROM_USES_EVT1
Mike Frysingerdbda2c62009-11-09 19:44:04 -0500715 serial_putc('I');
Mike Frysingerb30453a2010-04-29 02:49:41 -0400716 /* Tell the bootrom where our entry point is so that it knows
717 * where to jump to when finishing processing the LDR. This
718 * allows us to avoid small jump blocks in the LDR, and also
719 * works around anomaly 05000389 (init address in external
720 * memory causes bootrom to trigger external addressing IVHW).
721 */
Mike Frysinger7e1d2122008-10-18 04:04:49 -0400722 if (CONFIG_BFIN_BOOT_MODE != BFIN_BOOT_BYPASS)
723 bfin_write_EVT1(CONFIG_SYS_MONITOR_BASE);
Mike Frysinger02778f22009-04-24 23:39:41 -0400724#endif
Mike Frysinger7e1d2122008-10-18 04:04:49 -0400725
Mike Frysinger9171fc82008-03-30 15:46:13 -0400726 serial_putc('>');
727 serial_putc('\n');
728
729 serial_deinit();
730}