2 * arch/ppc/boot/spruce/misc.c
4 * Misc. bootloader code for IBM Spruce reference platform
6 * Authors: Johnnie Peters <jpeters@mvista.com>
7 * Matt Porter <mporter@mvista.com>
9 * Derived from arch/ppc/boot/prep/misc.c
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
17 #include <linux/types.h>
18 #include <linux/elf.h>
19 #include <linux/config.h>
20 #include <linux/pci.h>
23 #include <asm/processor.h>
25 #include <asm/bootinfo.h>
30 #define CMDLINE CONFIG_CMDLINE
35 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_VGA_CONSOLE)
36 #define INTERACTIVE_CONSOLE 1
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
44 unsigned long com_port;
49 /* The linker tells us where the image is. */
50 extern char __image_begin, __image_end;
51 extern char __ramdisk_begin, __ramdisk_end;
54 char cmd_preset[] = CMDLINE;
56 char *cmd_line = cmd_buf;
58 unsigned long initrd_size = 0;
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 *);
71 extern unsigned long serial_init(int chan, void *ignored);
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;
77 void cpc700_pcibios_read_config_byte(unsigned char bus, unsigned char dev_fn,
78 unsigned char offset, unsigned char *val)
80 out_le32(pci_config_address,
81 (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
83 *val= (in_le32((unsigned *)pci_config_data) >> (8 * (offset & 3))) & 0xff;
86 void cpc700_pcibios_write_config_byte(unsigned char bus, unsigned char dev_fn,
87 unsigned char offset, unsigned char val)
89 out_le32(pci_config_address,
90 (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
92 out_8(pci_config_data + (offset&3), val);
95 void cpc700_pcibios_read_config_word(unsigned char bus, unsigned char dev_fn,
96 unsigned char offset, unsigned short *val)
98 out_le32(pci_config_address,
99 (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
101 *val= in_le16((unsigned short *)(pci_config_data + (offset&3)));
104 void cpc700_pcibios_write_config_word(unsigned char bus, unsigned char dev_fn,
105 unsigned char offset, unsigned short val)
107 out_le32(pci_config_address,
108 (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
110 out_le16((unsigned short *)(pci_config_data + (offset&3)), val);
113 void cpc700_pcibios_read_config_dword(unsigned char bus, unsigned char dev_fn,
114 unsigned char offset, unsigned int *val)
116 out_le32(pci_config_address,
117 (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
119 *val= in_le32((unsigned *)pci_config_data);
122 void cpc700_pcibios_write_config_dword(unsigned char bus, unsigned char dev_fn,
123 unsigned char offset, unsigned int val)
125 out_le32(pci_config_address,
126 (((bus & 0xff)<<16) | (dev_fn<<8) | (offset&0xfc) | 0x80000000));
128 out_le32((unsigned *)pci_config_data, val);
131 unsigned long isa_io_base = SPRUCE_ISA_IO_BASE;
133 #define PCNET32_WIO_RDP 0x10
134 #define PCNET32_WIO_RAP 0x12
135 #define PCNET32_WIO_RESET 0x14
137 #define PCNET32_DWIO_RDP 0x10
138 #define PCNET32_DWIO_RAP 0x14
139 #define PCNET32_DWIO_RESET 0x18
141 /* Processor interface config register access */
142 #define PIFCFGADDR 0xff500000
143 #define PIFCFGDATA 0xff500004
145 #define PLBMIFOPT 0x18 /* PLB Master Interface Options */
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
155 load_kernel(unsigned long load_addr, int num_words, unsigned long cksum)
157 #ifdef INTERACTIVE_CONSOLE
165 volatile int *mem_addr = (int *)0xff500008;
166 volatile int *mem_data = (int *)0xff50000c;
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;
176 unsigned short vendor;
177 unsigned short device;
178 unsigned short command;
179 unsigned char header_type;
182 /* Initialize the serial console port */
183 com_port = serial_init(0, NULL);
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.
190 *pif_addr = PLBMIFOPT;
192 *pif_data = 0x00000000;
195 /* Get the size of memory from the memory controller. */
196 *mem_addr = MEM_MBEN;
198 mem_mben = *mem_data;
200 for(loop = 0; loop < 1000; loop++);
202 *mem_addr = MEM_TYPE;
204 mem_type = *mem_data;
206 for(loop = 0; loop < 1000; loop++);
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;
214 mem_start = *mem_data;
216 for(loop = 0; loop < 1000; loop++);
218 *mem_addr = MEM_B1EA;
222 for(loop = 0; loop < 1000; loop++);
224 mem_size = mem_end - mem_start + 0x100000;
227 /* Confirm bank 2 has DRAM memory */
228 if ((mem_mben & 0x20000000) &&
229 ((mem_type & 0xc000000) == 0x4000000)) {
230 *mem_addr = MEM_B2SA;
232 mem_start = *mem_data;
234 for(loop = 0; loop < 1000; loop++);
236 *mem_addr = MEM_B2EA;
240 for(loop = 0; loop < 1000; loop++);
242 mem_size += mem_end - mem_start + 0x100000;
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)
250 cpc700_pcibios_read_config_byte(0, pci_devfn,
251 PCI_HEADER_TYPE, &header_type);
253 if (!PCI_FUNC(pci_devfn))
254 found_multi = header_type & 0x80;
256 cpc700_pcibios_read_config_word(0, pci_devfn, PCI_VENDOR_ID,
259 if (vendor != 0xffff) {
260 cpc700_pcibios_read_config_word(0, pci_devfn,
261 PCI_DEVICE_ID, &device);
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)) {
267 /* Turn on I/O Space on the board. */
268 cpc700_pcibios_read_config_word(0, pci_devfn,
269 PCI_COMMAND, &command);
271 cpc700_pcibios_write_config_word(0, pci_devfn,
272 PCI_COMMAND, command);
274 /* Get the I/O space address */
275 cpc700_pcibios_read_config_dword(0, pci_devfn,
276 PCI_BASE_ADDRESS_0, &bar0);
279 /* Reset the PCNet Board */
280 inl (bar0+PCNET32_DWIO_RESET);
281 inw (bar0+PCNET32_WIO_RESET);
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.
287 outw(0, bar0 + PCNET32_WIO_RAP);
288 csr0 = inw(bar0 + PCNET32_WIO_RDP);
291 /* Check the Chip id register */
292 outw(88, bar0 + PCNET32_WIO_RAP);
293 csr_id = inw(bar0 + PCNET32_WIO_RDP);
296 /* This is the valid mode - set the stop bit */
297 outw(0, bar0 + PCNET32_WIO_RAP);
298 outw(csr0, bar0 + PCNET32_WIO_RDP);
301 outl(0, bar0 + PCNET32_DWIO_RAP);
302 csr0 = inl(bar0 + PCNET32_DWIO_RDP);
304 /* Check the Chip id register */
305 outl(88, bar0 + PCNET32_WIO_RAP);
306 csr_id = inl(bar0 + PCNET32_WIO_RDP);
309 /* This is the valid mode - set the stop bit*/
310 outl(0, bar0 + PCNET32_WIO_RAP);
311 outl(csr0, bar0 + PCNET32_WIO_RDP);
319 /* assume the chunk below 8M is free */
320 end_avail = (char *)0x00800000;
323 * We link ourself to 0x00800000. When we run, we relocate
324 * ourselves there. So we just need __image_begin for the
327 zimage_start = (char *)(unsigned long)(&__image_begin);
328 zimage_size = (unsigned long)(&__image_end) -
329 (unsigned long)(&__image_begin);
331 initrd_size = (unsigned long)(&__ramdisk_end) -
332 (unsigned long)(&__ramdisk_begin);
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
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));
345 puthex((unsigned long)(&__ramdisk_begin));
346 puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n");
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");
354 /* Display standard Linux/PPC boot prompt for kernel args */
355 puts("\nLinux/PPC load: ");
357 memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
360 #ifdef INTERACTIVE_CONSOLE
362 * If they have a console, allow them to edit the command line.
363 * Otherwise, don't bother wasting the five seconds.
365 while (timer++ < 5*1000) {
367 while ((ch = getc()) != '\n' && ch != '\r') {
369 if (cp != cmd_line) {
378 break; /* Exit 'timer' loop */
380 udelay(1000); /* 1 msec */
386 puts("Uncompressing Linux...");
388 gunzip(0, 0x400000, zimage_start, &zimage_size);
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;
399 /* We need to make sure that the initrd and bi_recs do not
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
406 if ((rec_loc > initrd_loc) &&
407 ((initrd_loc + initrd_size)
409 initrd_loc = _ALIGN((unsigned long)(zimage_size)
410 + (2 << 20) - 1, (2 << 20));
411 memmove((void *)initrd_loc, &__ramdisk_begin,
413 puts("initrd moved: "); puthex(initrd_loc);
414 puts(" "); puthex(initrd_loc + initrd_size);
420 rec->size = sizeof(struct bi_record);
421 rec = (struct bi_record *)((unsigned long)rec + rec->size);
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);
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);
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);
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 +
449 rec->size = sizeof(struct bi_record);
450 rec = (struct bi_record *)((unsigned long)rec + rec->size);
453 puts("Now booting the kernel\n");