ppc init fixes
[qemu] / hw / ppc_chrp.c
1 /*
2  * QEMU PPC CHRP/PMAC hardware System Emulator
3  * 
4  * Copyright (c) 2004 Fabrice Bellard
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "vl.h"
25
26 #define BIOS_FILENAME "ppc_rom.bin"
27 #define NVRAM_SIZE        0x2000
28
29 #define KERNEL_LOAD_ADDR 0x01000000
30 #define INITRD_LOAD_ADDR 0x01800000
31
32 /* MacIO devices (mapped inside the MacIO address space): CUDA, DBDMA,
33    NVRAM (not implemented).  */
34
35 static int dbdma_mem_index;
36 static int cuda_mem_index;
37 static int ide0_mem_index;
38 static int ide1_mem_index;
39
40 /* DBDMA: currently no op - should suffice right now */
41
42 static void dbdma_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
43 {
44     printf("%s: 0x%08x <= 0x%08x\n", __func__, addr, value);
45 }
46
47 static void dbdma_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
48 {
49 }
50
51 static void dbdma_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
52 {
53 }
54
55 static uint32_t dbdma_readb (void *opaque, target_phys_addr_t addr)
56 {
57     printf("%s: 0x%08x => 0x00000000\n", __func__, addr);
58     return 0;
59 }
60
61 static uint32_t dbdma_readw (void *opaque, target_phys_addr_t addr)
62 {
63     return 0;
64 }
65
66 static uint32_t dbdma_readl (void *opaque, target_phys_addr_t addr)
67 {
68     return 0;
69 }
70
71 static CPUWriteMemoryFunc *dbdma_write[] = {
72     &dbdma_writeb,
73     &dbdma_writew,
74     &dbdma_writel,
75 };
76
77 static CPUReadMemoryFunc *dbdma_read[] = {
78     &dbdma_readb,
79     &dbdma_readw,
80     &dbdma_readl,
81 };
82
83 static void macio_map(PCIDevice *pci_dev, int region_num, 
84                       uint32_t addr, uint32_t size, int type)
85 {
86     cpu_register_physical_memory(addr + 0x08000, 0x1000, dbdma_mem_index);
87     cpu_register_physical_memory(addr + 0x16000, 0x2000, cuda_mem_index);
88     cpu_register_physical_memory(addr + 0x1f000, 0x1000, ide0_mem_index);
89     cpu_register_physical_memory(addr + 0x20000, 0x1000, ide1_mem_index);
90 }
91
92 static void macio_init(void)
93 {
94     PCIDevice *d;
95
96     d = pci_register_device("macio", sizeof(PCIDevice),
97                             0, -1, 
98                             NULL, NULL);
99     /* Note: this code is strongly inspirated from the corresponding code
100        in PearPC */
101     d->config[0x00] = 0x6b; // vendor_id
102     d->config[0x01] = 0x10;
103     d->config[0x02] = 0x22;
104     d->config[0x03] = 0x00;
105
106     d->config[0x0a] = 0x00; // class_sub = pci2pci
107     d->config[0x0b] = 0xff; // class_base = bridge
108     d->config[0x0e] = 0x00; // header_type
109
110     d->config[0x3d] = 0x01; // interrupt on pin 1
111     
112     dbdma_mem_index = cpu_register_io_memory(0, dbdma_read, dbdma_write, NULL);
113
114     pci_register_io_region(d, 0, 0x80000, 
115                            PCI_ADDRESS_SPACE_MEM, macio_map);
116 }
117
118 /* PowerPC PREP hardware initialisation */
119 void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
120                    DisplayState *ds, const char **fd_filename, int snapshot,
121                    const char *kernel_filename, const char *kernel_cmdline,
122                    const char *initrd_filename)
123 {
124     char buf[1024];
125     openpic_t *openpic;
126     m48t59_t *nvram;
127     int PPC_io_memory;
128     int ret, linux_boot, i, fd;
129     unsigned long bios_offset;
130     uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
131     
132     linux_boot = (kernel_filename != NULL);
133
134     /* allocate RAM */
135     cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
136
137     /* allocate and load BIOS */
138     bios_offset = ram_size + vga_ram_size;
139     snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
140     ret = load_image(buf, phys_ram_base + bios_offset);
141     if (ret != BIOS_SIZE) {
142         fprintf(stderr, "qemu: could not load PPC PREP bios '%s'\n", buf);
143         exit(1);
144     }
145     cpu_register_physical_memory((uint32_t)(-BIOS_SIZE), 
146                                  BIOS_SIZE, bios_offset | IO_MEM_ROM);
147     cpu_single_env->nip = 0xfffffffc;
148
149     if (linux_boot) {
150         kernel_base = KERNEL_LOAD_ADDR;
151         /* now we can load the kernel */
152         kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
153         if (kernel_size < 0) {
154             fprintf(stderr, "qemu: could not load kernel '%s'\n", 
155                     kernel_filename);
156             exit(1);
157         }
158         /* load initrd */
159         if (initrd_filename) {
160             initrd_base = INITRD_LOAD_ADDR;
161             initrd_size = load_image(initrd_filename,
162                                      phys_ram_base + initrd_base);
163             if (initrd_size < 0) {
164                 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 
165                         initrd_filename);
166                 exit(1);
167             }
168         } else {
169             initrd_base = 0;
170             initrd_size = 0;
171         }
172         boot_device = 'm';
173     } else {
174         kernel_base = 0;
175         kernel_size = 0;
176         initrd_base = 0;
177         initrd_size = 0;
178     }
179     /* Register CPU as a 74x/75x */
180     cpu_ppc_register(cpu_single_env, 0x00080000);
181     /* Set time-base frequency to 100 Mhz */
182     cpu_ppc_tb_init(cpu_single_env, 100UL * 1000UL * 1000UL);
183
184     isa_mem_base = 0x80000000;
185     pci_pmac_init();
186
187     /* Register 8 MB of ISA IO space */
188     PPC_io_memory = cpu_register_io_memory(0, PPC_io_read, PPC_io_write, NULL);
189     cpu_register_physical_memory(0xF2000000, 0x00800000, PPC_io_memory);
190
191     /* init basic PC hardware */
192     vga_initialize(ds, phys_ram_base + ram_size, ram_size, 
193                    vga_ram_size, 1);
194     openpic = openpic_init(0x00000000, 0xF0000000, 1);
195
196     /* XXX: suppress that */
197     pic_init();
198
199     /* XXX: use Mac Serial port */
200     fd = serial_open_device();
201     serial_init(0x3f8, 4, fd);
202
203     for(i = 0; i < nb_nics; i++) {
204         pci_ne2000_init(&nd_table[i]);
205     }
206
207     ide0_mem_index = pmac_ide_init(&bs_table[0], openpic, 0x13);
208     ide1_mem_index = pmac_ide_init(&bs_table[2], openpic, 0x13);
209
210     /* cuda also initialize ADB */
211     cuda_mem_index = cuda_init(openpic, 0x19);
212
213     adb_kbd_init(&adb_bus);
214     adb_mouse_init(&adb_bus);
215     
216     macio_init();
217
218     nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE);
219     
220     if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
221         graphic_depth = 15;
222
223     PPC_NVRAM_set_params(nvram, NVRAM_SIZE, "CHRP", ram_size, boot_device,
224                          kernel_base, kernel_size,
225                          kernel_cmdline,
226                          initrd_base, initrd_size,
227                          /* XXX: need an option to load a NVRAM image */
228                          0,
229                          graphic_width, graphic_height, graphic_depth);
230     /* No PCI init: the BIOS will do it */
231 }