TEXT_BASE is in board/sandpoint/config.mk so say so...
[u-boot.git] / board / linkstation / early_init.S
1 /*
2  * board/linkstation/early_init.S
3  *
4  * Begin at some arbitrary location in RAM or Flash
5  *  Initialize core registers
6  *  Configure memory controller (Not executing from RAM)
7  *  Initialize UARTs
8  *  Simple RAM test (currently suspended)
9  *
10  * Copyright (C) 2006 Mihai Georgian <u-boot@linuxnotincluded.org.uk>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  *
27  * Modified for U-Boot from arch/ppc/boot/linkstation/head.S from
28  * the GPL code for the Buffalo Terastation, derived in its turn from:
29  *
30  * arch/ppc/boot/sandpoint/head.S
31  *
32  * Initial board bringup code for Motorola SPS Sandpoint test platform
33  *
34  * Author: Mark A. Greer
35  *         mgreer@mvista.com
36  * Derived from arch/ppc/boot/pcore/head.S (mporter@mvista.com)
37  *
38  * Copyright 2001 MontaVista Software Inc.
39  */
40
41 #include <config.h>
42 #include <ppc_asm.tmpl>
43 #include <mpc824x.h>
44 #include <ppc_defs.h>
45 #include <asm/cache.h> 
46
47 #if defined(CONFIG_LAN) || defined(CONFIG_HLAN)
48 #define RAM_SIZE        0x04000000
49 #elif defined(CONFIG_HGLAN) || defined(CONFIG_HTGL)
50 #define RAM_SIZE        0x08000000
51 #endif
52
53 #define UART1           0x80004500
54 #define UART1_IER       0x80004501
55 #define UART1_FCR       0x80004502
56 #define UART1_LCR       0x80004503
57 #define UART1_DCR       0x80004511
58 #define UART2           0x80004600
59 #define UART2_IER       0x80004601
60 #define UART2_FCR       0x80004602
61 #define UART2_LCR       0x80004603
62 #define UART2_DCR       0x80004611
63
64 #define WM32(address,data) \
65         lis     r3, address@h; \
66         ori     r3, r3, address@l; \
67         lis     r4, data@h; \
68         ori     r4, r4, data@l; \
69         stw     r4, 0x0000(r3); \
70         sync;  \
71         isync;
72
73 #define WM16(address,data) \
74         lis     r3, address@h; \
75         ori     r3, r3, address@l; \
76         li      r4, data; \
77         sth     r4, 0x0000(r3); \
78         sync;  \
79         isync;
80
81 #define WM8(address,data) \
82         lis     r3, address@h; \
83         ori     r3, r3, address@l; \
84         li      r4, data; \
85         stb     r4, 0(r3); \
86         sync;  \
87         isync;
88
89         .text
90
91         .globl  early_init_f
92 early_init_f:
93 /*
94  * Configure core registers
95  */
96
97         /* Establish default MSR value, exception prefix 0xFFF */
98         li      r3,MSR_IP|MSR_FP
99         mtmsr   r3
100
101         /* Clear BATS */
102         li      r8,0
103         mtspr   DBAT0U,r8
104         mtspr   DBAT0L,r8
105         mtspr   DBAT1U,r8
106         mtspr   DBAT1L,r8
107         mtspr   DBAT2U,r8
108         mtspr   DBAT2L,r8
109         mtspr   DBAT3U,r8
110         mtspr   DBAT3L,r8
111         mtspr   IBAT0U,r8
112         mtspr   IBAT0L,r8
113         mtspr   IBAT1U,r8
114         mtspr   IBAT1L,r8
115         mtspr   IBAT2U,r8
116         mtspr   IBAT2L,r8
117         mtspr   IBAT3U,r8
118         mtspr   IBAT3L,r8
119         isync
120         sync
121         sync
122
123         /* Set segment registers */
124         lis     r8, 0x0000
125         isync
126         mtsr    SR0,r8
127         mtsr    SR1,r8
128         mtsr    SR2,r8
129         mtsr    SR3,r8
130         mtsr    SR4,r8
131         mtsr    SR5,r8
132         mtsr    SR6,r8
133         mtsr    SR7,r8
134         mtsr    SR8,r8
135         mtsr    SR9,r8
136         mtsr    SR10,r8
137         mtsr    SR11,r8
138         mtsr    SR12,r8
139         mtsr    SR13,r8
140         mtsr    SR14,r8
141         mtsr    SR15,r8
142         isync
143         sync
144         sync
145
146         /* Disable L1 icache/dcache */
147         li      r4,0x0000
148         isync
149         mtspr   HID0,r4
150         sync
151         isync
152         
153         /* Flash Invalidate L1 icache/dcache */
154         
155         ori     r4,r4,0x8000
156         ori     r8,r4,0x0800
157         isync
158         mtspr   HID0,r8
159         sync
160         isync
161         
162         /* Older cores need to manually clear ICFI bit */
163         
164         mtspr   HID0,r4
165         sync
166         isync
167
168 #if !defined(CFG_RAMBOOT)
169 melco_config_start:
170         /* --- CPU Configration registor setting for LinkStation --- */
171         WM32(0x80041020,0x000000a0) /* Reset EPIC */
172
173         /* errata for latency timer */
174         WM32(0xFEC00000,0x0d000080)
175         WM8(0xFEE00001,0x20)
176         /* cash size */
177         WM32(0xFEC00000,0x0c000080)
178         WM8(0xFEE00000,0x08)
179         /*  PCI configuration command register */
180         WM32(0xFEC00000,0x04000080)
181         WM16(0xFEE00000,0x0600)
182         /* Processor interface configuration register 1 */
183         WM32(0xFEC00000,0xa8000080)
184         /* WM32(0xFEE00000,0xd8131400) */
185         lis     r3, 0xFEE00000@h 
186         ori     r3, r3, 0xFEE00000@l
187         
188         lwz r5, 0(r3)  /* load PCIR1 Config */
189         lis r4, 0x0
190         ori r4, r4, 0x1000
191         and r5, r4, r5  /* Get Bit20(RCS0) */
192                 
193         lis     r4, 0xd8130400@h
194         ori     r4, r4, 0xd8130400@l
195         or  r4, r4, r5   /* Save (RCS0) */
196         
197         stw     r4, 0x0000(r3)
198         sync
199         isync
200         
201         /* Processor interface configuration register 2 */
202         WM32(0xFEC00000,0xac000080)
203         WM32(0xFEE00000,0x00000004)
204         /* Embeded Utility Memory Block Base Address register */
205         WM32(0xFEC00000,0x78000080)
206         WM32(0xFEE00000,0x00000080)
207         /* Address map B option register */
208         WM32(0xFEC00000,0xe0000080)
209         WM8(0xFEE00000,0x20) /* DLL_RESET on */
210         
211         /* Address map B option register */
212         WM32(0xFEC00000,0xe0000080)
213         WM8(0xFEE00000,0xc0)
214         /* PCI arbiter control register */
215         WM32(0xFEC00000,0x46000080)
216         WM16(0xFEE00002,0x00c0)
217
218         /* Added to use the high drive strength for the memory selects & addressing */
219         WM32(0xFEC00000,0x73000080)
220                 /* WM8(0xFEE00003,0x15) */  /*0x17*/
221                 /* Motorola Errata refer to User's Manual Errata#19 */
222                 /* WM8(0xFEE00003,0xD5) */
223         WM8(0xFEE00003,0x95)
224
225         /* set miscellaneous I/O control register 1 */
226         WM32(0xFEC00000,0x76000080)
227         WM8(0xFEE00002,0x00) /*0x02*/
228         /* set miscellaneous I/O control register 2 */
229         WM32(0xFEC00000,0x77000080)
230         WM8(0xFEE00003,0x30)  /* 0x30 */
231
232         /* init memory controller */
233         WM32(0xFEC00000,0x80000080)
234         WM32(0xFEE00000,0x00FFFFFF)
235
236         WM32(0xFEC00000,0x84000080)
237         WM32(0xFEE00000,0xFFFFFFFF)
238         
239         WM32(0xFEC00000,0x90000080)
240 #if defined(CONFIG_LAN) || defined(CONFIG_HLAN)
241         WM32(0xFEE00000,0x3FFFFFFF) /* 64MB */
242 #elif defined(CONFIG_HGLAN) || defined(CONFIG_HTGL)
243         WM32(0xFEE00000,0x7FFFFFFF) /* 128MB */
244 #endif
245
246         WM32(0xFEC00000,0x94000080)
247         WM32(0xFEE00000,0xFFFFFFFF)
248         
249         WM32(0xFEC00000,0x88000080)
250         WM32(0xFEE00000,0x00030303)
251         /* EMSAR2 */
252         WM32(0xFEC00000,0x8C000080)
253         WM32(0xFEE00000,0x03030303)
254         /* select EMSER1 */
255         WM32(0xFEC00000,0x98000080)
256         WM32(0xFEE00000,0x00030303)
257         /* select EMSER2 */
258         WM32(0xFEC00000,0x9C000080)
259         WM32(0xFEE00000,0x03030303)
260
261         /* MMCR1 */
262         WM32(0xFEC00000,0xf0000080)
263 #if defined(CONFIG_LAN) || defined(CONFIG_HLAN)
264         WM32(0xFEE00000,0x0200E005)     /* bank 0 13xnx4 */
265 #elif defined(CONFIG_HGLAN) || defined(CONFIG_HTGL)
266         WM32(0xFEE00000,0x0200E005)     /* bank 0 13xnx4 */
267 #endif
268         /* MCCR2 */
269         WM32(0xFEC00000,0xf4000080)
270 #if defined(CONFIG_LAN) || defined(CONFIG_HLAN)
271         WM32(0xFEE00000,0xe0150000) /* 100MHz Memory bus */
272 #elif defined(CONFIG_HGLAN) || defined(CONFIG_HTGL)
273         WM32(0xFEE00000,0x80150000) /* 133MHz Memory bus */
274 #endif
275         /* MCCR3 */
276         WM32(0xFEC00000,0xf8000080)     
277         WM32(0xFEE00000,0x00000077)     /* BSTOPRE_M =7 / REFREC=8 */
278
279         /* MCCR4 */
280         WM32(0xFEC00000,0xfc000080)
281 #if defined(CONFIG_LAN) || defined(CONFIG_HLAN)
282         WM32(0xFEE00000,0x29233222)     /* CAS latency=2, burst length=8, Ext Rom=eable */
283 #elif defined(CONFIG_HGLAN) || defined(CONFIG_HTGL)
284         WM32(0xFEE00000,0x29323222)     /* CAS latency=3, burst length=4, Ext Rom=eable */
285 #endif
286
287         /* Output driver control register */
288         WM32(0xFEC00000,0x73000080)
289         WM8(0xFEE00003,0x15)                    /*  for all 40 ohm */
290         /* CLK driver Control Register */
291         WM32(0xFEC00000,0x74000080)
292         WM16(0xFEE00000,0x7078)
293         /* select MBEN */
294         WM32(0xFEC00000,0xa0000080)
295         WM8(0xFEE00000, 0x01)
296         /* MPM */
297         WM32(0xFEC00000,0xa3000080)
298 #if defined(CONFIG_LAN) || defined(CONFIG_HLAN)
299         WM8(0xFEE00003,0xF2)    /* PGMAX = 242 */
300 #elif defined(CONFIG_HGLAN) || defined(CONFIG_HTGL)
301         WM8(0xFEE00003,0xC9)    /* PGMAX = 201 */
302 #endif
303         /* ERCR s */
304         WM32(0xFEC00000,0xd0000080) /*  ; select ERCR1 */
305         WM32(0xFEE00000,0xffffff85)
306         WM32(0xFEC00000,0xd4000080) /*  ; select ERCR2 */
307         WM32(0xFEE00000,0xffffff05)
308         WM32(0xFEC00000,0xd8000080) /*  ; select ERCR3 */
309         WM32(0xFEE00000,0x0000f80f)
310         WM32(0xFEC00000,0xdc000080) /*  ; select ERCR4 */
311         WM32(0xFEE00000,0x0e000000)
312
313         /* MCCR1 */
314         WM32(0xFEC00000,0xf0000080)
315         WM32(0xFEE00000,0x0200E805)  /* 11 + 3 clock wait MEMGO on */
316
317         /* Init UART for AVR */
318         WM8(UART1_LCR,0x00) /* clear LCR */
319         WM8(UART1_IER,0x00) /* disable interrupt */
320         WM8(UART1_LCR,0x80) /* set LCR[DLAB] bit */
321         WM8(UART1_DCR,0x01) /* set DUART mode */
322 #if defined(CONFIG_LAN) || defined(CONFIG_HLAN)
323         WM8(UART1,    0x8B) /* set DLL(baudrate 9600bps, 100MHz) */
324         WM8(UART1_IER,0x02) /* set DLM(baudrate 9600bps, 100MHz) */
325 #elif defined(CONFIG_HGLAN) || defined(CONFIG_HTGL)
326         WM8(UART1,    0x61) /* set DLL(baudrate 9600bps, 133MHz) */
327         WM8(UART1_IER,0x03) /* set DLM(baudrate 9600bps, 133MHz) */
328 #endif
329         WM8(UART1_LCR,0x1b) /* set 8data, 1stop, even parity */
330         WM8(UART1,    0x00) /* clear MCR */
331         WM8(UART1_FCR,0x07) /* clear & enable FIFO */
332
333         /* Init UART for CONSOLE */
334         WM8(UART2_LCR,0x00) /* clear LCR */
335         WM8(UART2_IER,0x00) /* disable interrupt */
336         WM8(UART2_LCR,0x80) /* set LCR[DLAB] bit */
337         WM8(UART1_DCR,0x01) /* set DUART mode */
338 #if defined(CONFIG_LAN) || defined(CONFIG_HLAN)
339         WM8(UART2,    0x6C) /* set DLL(baudrate 57600bps, 100MHz) */
340         WM8(UART2_IER,0x00) /* set DLM(baudrate 57600bps, 100MHz) */
341 #elif defined(CONFIG_HGLAN) || defined(CONFIG_HTGL)
342         WM8(UART2,    0x90) /* set DLL(baudrate 57600bps, 133MHz) */
343         WM8(UART2_IER,0x00) /* set DLM(baudrate 57600bps, 133MHz) */
344 #endif
345         WM8(UART2_LCR,0x03) /* set 8data, 1stop, non parity */
346         WM8(UART2,    0x00) /* clear MCR */
347         WM8(UART2_FCR,0x07) /* clear & enable FIFO */
348 #endif /* !defined (CFG_RAMBOOT)
349
350         /* PCI Command Register initialize */
351         lis r3, 0x8000
352         ori r3, r3, 0x0004
353         lis r4, 0xFEC0
354         ori r4, r4, 0x0000
355         stwbrx r4, 0, r3
356         sync
357         isync
358
359         li r6, 0x0006
360         lis r5, 0xFEE0
361         ori r5, r5, 0x0000
362         sthbrx r5, 0, r6
363         sync
364         isync
365
366 #if !defined(CFG_RAMBOOT)
367 check_ram:
368         /* Wait 1 sec  for AVR become enable */
369         li      r3,1000
370         mulli   r3,r3,1000
371         mulli   r4,r3,1000      /* nanoseconds */
372         addi    r4,r4,39
373         li      r5,40           /* 40ns if for 100 Mhz bus */
374         divw    r4,r4,r5        /* BUS ticks */
375 1:      mftbu   r5
376         mftb    r6
377         mftbu   r7
378         cmp     0,r5,r7
379         bne     1b              /* Get [synced] base time */
380         addc    r9,r6,r4        /* Compute end time */
381         addze   r8,r5
382 2:      mftbu   r5
383         cmp     0,r5,r8
384         blt     2b
385         bgt     3f
386         mftb    r6
387         cmp     0,r6,r9
388         blt     2b
389 #if 1
390 3:
391 #else
392         /* Check RAM */
393         /* set start address(0x00000000) */
394 3:      xor r4,r4,r4
395         lis r5,     RAM_SIZE@h
396         ori r5, r5, RAM_SIZE@l
397         lis r6,     0xaaaa  /* mask pattern a */
398         ori r6, r6, 0xaaaa
399         lis r7,     0x5555  /* mask pattern b */
400         ori r7, r7, 0x5555
401         lis r8,     0x0000  /* check step size */
402         ori r8, r8, 0x0100
403 check_ram_loop: 
404         cmp 0,r4,r5
405         beq  check_ram_end
406         stw r6,0(r4)
407         isync
408         lwz r3,0(r4)
409         isync
410         cmp 0,r3,r6
411         bne ram_error
412         stw r7,0x00fc(r4)
413         isync
414         lwz r3,0x00fc(r4)
415         isync
416         cmp 0,r3,r7
417         bne ram_error
418         add r4,r4,r8
419         b   check_ram_loop
420 ram_error:
421 #if defined(CONFIG_LAN)
422         WM8(UART1,0x39)  /* ram error */
423 #elif defined(CONFIG_HGLAN) ||defined(CONFIG_HLAN) || defined(CONFIG_HTGL)
424         WM8(UART1,0x6F)  /* ram error */
425 #endif
426         b   ram_error
427 check_ram_end:
428 #endif /* #if 1 */
429 #endif /* !defined (CFG_RAMBOOT) */
430
431 /* The instruction cache is enabled and the data cache is disabled */
432         blr