more changes on original files
[linux-2.4.git] / arch / ppc / boot / simple / misc-spruce.c
1 /*
2  * arch/ppc/boot/spruce/misc.c
3  *
4  * Misc. bootloader code for IBM Spruce reference platform
5  *
6  * Authors: Johnnie Peters <jpeters@mvista.com>
7  *          Matt Porter <mporter@mvista.com>
8  *
9  * Derived from arch/ppc/boot/prep/misc.c
10  *
11  * 2000-2001 (c) MontaVista, Software, Inc.  This file is licensed under
12  * the terms of the GNU General Public License version 2.  This program
13  * is licensed "as is" without any warranty of any kind, whether express
14  * or implied.
15  */
16
17 #include <linux/types.h>
18 #include <linux/elf.h>
19 #include <linux/config.h>
20 #include <linux/pci.h>
21
22 #include <asm/page.h>
23 #include <asm/processor.h>
24 #include <asm/mmu.h>
25 #include <asm/bootinfo.h>
26
27 #include "zlib.h"
28
29 #ifdef CONFIG_CMDLINE
30 #define CMDLINE CONFIG_CMDLINE
31 #else
32 #define CMDLINE ""
33 #endif
34
35 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_VGA_CONSOLE)
36 #define INTERACTIVE_CONSOLE     1
37 #endif
38
39 /* Define some important locations of the Spruce. */
40 #define SPRUCE_PCI_CONFIG_ADDR  0xfec00000
41 #define SPRUCE_PCI_CONFIG_DATA  0xfec00004
42 #define SPRUCE_ISA_IO_BASE      0xf8000000
43
44 unsigned long com_port;
45
46 char *avail_ram;
47 char *end_avail;
48
49 /* The linker tells us where the image is. */
50 extern char __image_begin, __image_end;
51 extern char __ramdisk_begin, __ramdisk_end;
52 extern char _end[];
53
54 char cmd_preset[] = CMDLINE;
55 char cmd_buf[256];
56 char *cmd_line = cmd_buf;
57
58 unsigned long initrd_size = 0;
59
60 char *zimage_start;
61 int zimage_size;
62
63 extern void udelay(long);
64 extern void puts(const char *);
65 extern void putc(const char c);
66 extern void puthex(unsigned long val);
67 extern int getc(void);
68 extern int tstc(void);
69 extern void gunzip(void *, int, unsigned char *, int *);
70
71 extern unsigned long serial_init(int chan, void *ignored);
72
73 /* PCI configuration space access routines. */
74 unsigned int *pci_config_address = (unsigned int *)SPRUCE_PCI_CONFIG_ADDR;
75 unsigned char *pci_config_data   = (unsigned char *)SPRUCE_PCI_CONFIG_DATA;
76
77 void cpc700_pcibios_read_config_byte(unsigned char bus, unsigned char dev_fn,
78                              unsigned char offset, unsigned char *val)
79 {
80         out_le32(pci_config_address,
81                  (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
82
83         *val= (in_le32((unsigned *)pci_config_data) >> (8 * (offset & 3))) & 0xff;
84 }
85
86 void cpc700_pcibios_write_config_byte(unsigned char bus, unsigned char dev_fn,
87                              unsigned char offset, unsigned char val)
88 {
89         out_le32(pci_config_address,
90                  (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
91
92         out_8(pci_config_data + (offset&3), val);
93 }
94
95 void cpc700_pcibios_read_config_word(unsigned char bus, unsigned char dev_fn,
96                              unsigned char offset, unsigned short *val)
97 {
98         out_le32(pci_config_address,
99                  (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
100
101         *val= in_le16((unsigned short *)(pci_config_data + (offset&3)));
102 }
103
104 void cpc700_pcibios_write_config_word(unsigned char bus, unsigned char dev_fn,
105                              unsigned char offset, unsigned short val)
106 {
107         out_le32(pci_config_address,
108                  (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
109
110         out_le16((unsigned short *)(pci_config_data + (offset&3)), val);
111 }
112
113 void cpc700_pcibios_read_config_dword(unsigned char bus, unsigned char dev_fn,
114                              unsigned char offset, unsigned int *val)
115 {
116         out_le32(pci_config_address,
117                  (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
118
119         *val= in_le32((unsigned *)pci_config_data);
120 }
121
122 void cpc700_pcibios_write_config_dword(unsigned char bus, unsigned char dev_fn,
123                              unsigned char offset, unsigned int val)
124 {
125         out_le32(pci_config_address,
126                  (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
127
128         out_le32((unsigned *)pci_config_data, val);
129 }
130
131 unsigned long isa_io_base = SPRUCE_ISA_IO_BASE;
132
133 #define PCNET32_WIO_RDP         0x10
134 #define PCNET32_WIO_RAP         0x12
135 #define PCNET32_WIO_RESET       0x14
136
137 #define PCNET32_DWIO_RDP        0x10
138 #define PCNET32_DWIO_RAP        0x14
139 #define PCNET32_DWIO_RESET      0x18
140
141 /* Processor interface config register access */
142 #define PIFCFGADDR 0xff500000
143 #define PIFCFGDATA 0xff500004
144
145 #define PLBMIFOPT 0x18 /* PLB Master Interface Options */
146
147 #define MEM_MBEN        0x24
148 #define MEM_TYPE        0x28
149 #define MEM_B1SA        0x3c
150 #define MEM_B1EA        0x5c
151 #define MEM_B2SA        0x40
152 #define MEM_B2EA        0x60
153
154 unsigned long
155 load_kernel(unsigned long load_addr, int num_words, unsigned long cksum)
156 {
157 #ifdef INTERACTIVE_CONSOLE
158         int timer = 0;
159         char ch;
160 #endif
161         char *cp;
162         int loop;
163         int csr0;
164         int csr_id;
165         volatile int *mem_addr = (int *)0xff500008;
166         volatile int *mem_data = (int *)0xff50000c;
167         int mem_size = 0;
168         unsigned long mem_mben;
169         unsigned long mem_type;
170         unsigned long mem_start;
171         unsigned long mem_end;
172         volatile int *pif_addr = (int *)0xff500000;
173         volatile int *pif_data = (int *)0xff500004;
174         int pci_devfn;
175         int found_multi = 0;
176         unsigned short vendor;
177         unsigned short device;
178         unsigned short command;
179         unsigned char header_type;
180         unsigned int bar0;
181
182         /* Initialize the serial console port */
183         com_port = serial_init(0, NULL);
184
185         /*
186          * Gah, these firmware guys need to learn that hardware
187          * byte swapping is evil! Disable all hardware byte
188          * swapping so it doesn't hurt anyone.
189          */
190         *pif_addr = PLBMIFOPT;
191         asm("sync");
192         *pif_data = 0x00000000;
193         asm("sync");
194
195         /* Get the size of memory from the memory controller. */
196         *mem_addr = MEM_MBEN;
197         asm("sync");
198         mem_mben = *mem_data;
199         asm("sync");
200         for(loop = 0; loop < 1000; loop++);
201
202         *mem_addr = MEM_TYPE;
203         asm("sync");
204         mem_type = *mem_data;
205         asm("sync");
206         for(loop = 0; loop < 1000; loop++);
207
208         *mem_addr = MEM_TYPE;
209         /* Confirm bank 1 has DRAM memory */
210         if ((mem_mben & 0x40000000) &&
211                                 ((mem_type & 0x30000000) == 0x10000000)) {
212                 *mem_addr = MEM_B1SA;
213                 asm("sync");
214                 mem_start = *mem_data;
215                 asm("sync");
216                 for(loop = 0; loop < 1000; loop++);
217
218                 *mem_addr = MEM_B1EA;
219                 asm("sync");
220                 mem_end = *mem_data;
221                 asm("sync");
222                 for(loop = 0; loop < 1000; loop++);
223
224                 mem_size = mem_end - mem_start + 0x100000;
225         }
226
227         /* Confirm bank 2 has DRAM memory */
228         if ((mem_mben & 0x20000000) &&
229                                 ((mem_type & 0xc000000) == 0x4000000)) {
230                 *mem_addr = MEM_B2SA;
231                 asm("sync");
232                 mem_start = *mem_data;
233                 asm("sync");
234                 for(loop = 0; loop < 1000; loop++);
235
236                 *mem_addr = MEM_B2EA;
237                 asm("sync");
238                 mem_end = *mem_data;
239                 asm("sync");
240                 for(loop = 0; loop < 1000; loop++);
241
242                 mem_size += mem_end - mem_start + 0x100000;
243         }
244
245         /* Search out and turn off the PcNet ethernet boot device. */
246         for (pci_devfn = 1; pci_devfn < 0xff; pci_devfn++) {
247                 if (PCI_FUNC(pci_devfn) && !found_multi)
248                         continue;
249
250                 cpc700_pcibios_read_config_byte(0, pci_devfn,
251                                 PCI_HEADER_TYPE, &header_type);
252
253                 if (!PCI_FUNC(pci_devfn))
254                         found_multi = header_type & 0x80;
255
256                 cpc700_pcibios_read_config_word(0, pci_devfn, PCI_VENDOR_ID,
257                                 &vendor);
258
259                 if (vendor != 0xffff) {
260                         cpc700_pcibios_read_config_word(0, pci_devfn,
261                                                 PCI_DEVICE_ID, &device);
262
263                         /* If this PCI device is the Lance PCNet board then turn it off */
264                         if ((vendor == PCI_VENDOR_ID_AMD) &&
265                                         (device == PCI_DEVICE_ID_AMD_LANCE)) {
266
267                                 /* Turn on I/O Space on the board. */
268                                 cpc700_pcibios_read_config_word(0, pci_devfn,
269                                                 PCI_COMMAND, &command);
270                                 command |= 0x1;
271                                 cpc700_pcibios_write_config_word(0, pci_devfn,
272                                                 PCI_COMMAND, command);
273
274                                 /* Get the I/O space address */
275                                 cpc700_pcibios_read_config_dword(0, pci_devfn,
276                                                 PCI_BASE_ADDRESS_0, &bar0);
277                                 bar0 &= 0xfffffffe;
278
279                                 /* Reset the PCNet Board */
280                                 inl (bar0+PCNET32_DWIO_RESET);
281                                 inw (bar0+PCNET32_WIO_RESET);
282
283                                 /* First do a work oriented read of csr0.  If the value is
284                                  * 4 then this is the correct mode to access the board.
285                                  * If not try a double word ortiented read.
286                                  */
287                                 outw(0, bar0 + PCNET32_WIO_RAP);
288                                 csr0 = inw(bar0 + PCNET32_WIO_RDP);
289
290                                 if (csr0 == 4) {
291                                         /* Check the Chip id register */
292                                         outw(88, bar0 + PCNET32_WIO_RAP);
293                                         csr_id = inw(bar0 + PCNET32_WIO_RDP);
294
295                                         if (csr_id) {
296                                                 /* This is the valid mode - set the stop bit */
297                                                 outw(0, bar0 + PCNET32_WIO_RAP);
298                                                 outw(csr0, bar0 + PCNET32_WIO_RDP);
299                                         }
300                                 } else {
301                                         outl(0, bar0 + PCNET32_DWIO_RAP);
302                                         csr0 = inl(bar0 + PCNET32_DWIO_RDP);
303                                         if (csr0 == 4) {
304                                                 /* Check the Chip id register */
305                                                 outl(88, bar0 + PCNET32_WIO_RAP);
306                                                 csr_id = inl(bar0 + PCNET32_WIO_RDP);
307
308                                                 if (csr_id) {
309                                                         /* This is the valid mode  - set the stop bit*/
310                                                         outl(0, bar0 + PCNET32_WIO_RAP);
311                                                         outl(csr0, bar0 + PCNET32_WIO_RDP);
312                                                 }
313                                         }
314                                 }
315                         }
316                 }
317         }
318
319         /* assume the chunk below 8M is free */
320         end_avail = (char *)0x00800000;
321
322         /*
323          * We link ourself to 0x00800000.  When we run, we relocate
324          * ourselves there.  So we just need __image_begin for the
325          * start. -- Tom
326          */
327         zimage_start = (char *)(unsigned long)(&__image_begin);
328         zimage_size = (unsigned long)(&__image_end) -
329                         (unsigned long)(&__image_begin);
330
331         initrd_size = (unsigned long)(&__ramdisk_end) -
332                 (unsigned long)(&__ramdisk_begin);
333
334         /*
335          * The zImage and initrd will be between start and _end, so they've
336          * already been moved once.  We're good to go now. -- Tom
337          */
338         avail_ram = (char *)PAGE_ALIGN((unsigned long)_end);
339         puts("zimage at:     "); puthex((unsigned long)zimage_start);
340         puts(" "); puthex((unsigned long)(zimage_size+zimage_start));
341         puts("\n");
342
343         if ( initrd_size ) {
344                 puts("initrd at:     ");
345                 puthex((unsigned long)(&__ramdisk_begin));
346                 puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n");
347         }
348
349         avail_ram = (char *)0x00400000;
350         end_avail = (char *)0x00800000;
351         puts("avail ram:     "); puthex((unsigned long)avail_ram); puts(" ");
352         puthex((unsigned long)end_avail); puts("\n");
353
354         /* Display standard Linux/PPC boot prompt for kernel args */
355         puts("\nLinux/PPC load: ");
356         cp = cmd_line;
357         memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
358         while ( *cp )
359                 putc(*cp++);
360 #ifdef INTERACTIVE_CONSOLE
361         /*
362          * If they have a console, allow them to edit the command line.
363          * Otherwise, don't bother wasting the five seconds.
364          */
365         while (timer++ < 5*1000) {
366                 if (tstc()) {
367                         while ((ch = getc()) != '\n' && ch != '\r') {
368                                 if (ch == '\b') {
369                                         if (cp != cmd_line) {
370                                                 cp--;
371                                                 puts("\b \b");
372                                         }
373                                 } else {
374                                         *cp++ = ch;
375                                         putc(ch);
376                                 }
377                         }
378                         break;  /* Exit 'timer' loop */
379                 }
380                 udelay(1000);  /* 1 msec */
381         }
382 #endif
383         *cp = 0;
384         puts("\n");
385
386         puts("Uncompressing Linux...");
387
388         gunzip(0, 0x400000, zimage_start, &zimage_size);
389
390         puts("done.\n");
391
392         {
393                 struct bi_record *rec;
394                 unsigned long initrd_loc;
395                 unsigned long rec_loc = _ALIGN((unsigned long)(zimage_size) +
396                                 (1 << 20) - 1, (1 << 20));
397                 rec = (struct bi_record *)rec_loc;
398
399                 /* We need to make sure that the initrd and bi_recs do not
400                  * overlap. */
401                 if ( initrd_size ) {
402                         initrd_loc = (unsigned long)(&__ramdisk_begin);
403                         /* If the bi_recs are in the middle of the current
404                          * initrd, move the initrd to the next MB
405                          * boundary. */
406                         if ((rec_loc > initrd_loc) &&
407                                         ((initrd_loc + initrd_size)
408                                          > rec_loc)) {
409                                 initrd_loc = _ALIGN((unsigned long)(zimage_size)
410                                                 + (2 << 20) - 1, (2 << 20));
411                                 memmove((void *)initrd_loc, &__ramdisk_begin,
412                                          initrd_size);
413                                 puts("initrd moved:  "); puthex(initrd_loc);
414                                 puts(" "); puthex(initrd_loc + initrd_size);
415                                 puts("\n");
416                         }
417                 }
418
419                 rec->tag = BI_FIRST;
420                 rec->size = sizeof(struct bi_record);
421                 rec = (struct bi_record *)((unsigned long)rec + rec->size);
422
423                 rec->tag = BI_BOOTLOADER_ID;
424                 memcpy( (void *)rec->data, "spruceboot", 11);
425                 rec->size = sizeof(struct bi_record) + 10 + 1;
426                 rec = (struct bi_record *)((unsigned long)rec + rec->size);
427
428                 rec->tag = BI_MEMSIZE;
429                 rec->data[0] = mem_size;
430                 rec->size = sizeof(struct bi_record) + sizeof(unsigned long);
431                 rec = (struct bi_record *)((unsigned long)rec + rec->size);
432
433                 rec->tag = BI_CMD_LINE;
434                 memcpy( (char *)rec->data, cmd_line, strlen(cmd_line)+1);
435                 rec->size = sizeof(struct bi_record) + strlen(cmd_line) + 1;
436                 rec = (struct bi_record *)((ulong)rec + rec->size);
437
438                 if ( initrd_size ) {
439                         rec->tag = BI_INITRD;
440                         rec->data[0] = initrd_loc;
441                         rec->data[1] = initrd_size;
442                         rec->size = sizeof(struct bi_record) + 2 *
443                                 sizeof(unsigned long);
444                         rec = (struct bi_record *)((unsigned long)rec +
445                                         rec->size);
446                 }
447
448                 rec->tag = BI_LAST;
449                 rec->size = sizeof(struct bi_record);
450                 rec = (struct bi_record *)((unsigned long)rec + rec->size);
451         }
452
453         puts("Now booting the kernel\n");
454
455         return 0;
456 }