]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc_chrp.c
negative decr fix
[mirror_qemu.git] / hw / ppc_chrp.c
CommitLineData
64201201
FB
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"
d5295253 27#define VGABIOS_FILENAME "video.x"
64201201
FB
28#define NVRAM_SIZE 0x2000
29
b6b8bd18
FB
30#define KERNEL_LOAD_ADDR 0x01000000
31#define INITRD_LOAD_ADDR 0x01800000
32
267002cd
FB
33/* MacIO devices (mapped inside the MacIO address space): CUDA, DBDMA,
34 NVRAM (not implemented). */
35
36static int dbdma_mem_index;
37static int cuda_mem_index;
0aa6a4a2
FB
38static int ide0_mem_index = -1;
39static int ide1_mem_index = -1;
40static int openpic_mem_index = -1;
41static int heathrow_pic_mem_index = -1;
267002cd
FB
42
43/* DBDMA: currently no op - should suffice right now */
44
45static void dbdma_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
46{
b6b8bd18 47 printf("%s: 0x%08x <= 0x%08x\n", __func__, addr, value);
267002cd
FB
48}
49
50static void dbdma_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
51{
52}
53
54static void dbdma_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
55{
56}
57
58static uint32_t dbdma_readb (void *opaque, target_phys_addr_t addr)
59{
b6b8bd18 60 printf("%s: 0x%08x => 0x00000000\n", __func__, addr);
267002cd
FB
61 return 0;
62}
63
64static uint32_t dbdma_readw (void *opaque, target_phys_addr_t addr)
65{
66 return 0;
67}
68
69static uint32_t dbdma_readl (void *opaque, target_phys_addr_t addr)
70{
71 return 0;
72}
73
74static CPUWriteMemoryFunc *dbdma_write[] = {
75 &dbdma_writeb,
76 &dbdma_writew,
77 &dbdma_writel,
78};
79
80static CPUReadMemoryFunc *dbdma_read[] = {
81 &dbdma_readb,
82 &dbdma_readw,
83 &dbdma_readl,
84};
85
86static void macio_map(PCIDevice *pci_dev, int region_num,
87 uint32_t addr, uint32_t size, int type)
88{
0aa6a4a2
FB
89 if (heathrow_pic_mem_index >= 0) {
90 cpu_register_physical_memory(addr + 0x00000, 0x1000,
91 heathrow_pic_mem_index);
92 }
267002cd
FB
93 cpu_register_physical_memory(addr + 0x08000, 0x1000, dbdma_mem_index);
94 cpu_register_physical_memory(addr + 0x16000, 0x2000, cuda_mem_index);
0aa6a4a2
FB
95 if (ide0_mem_index >= 0)
96 cpu_register_physical_memory(addr + 0x1f000, 0x1000, ide0_mem_index);
97 if (ide1_mem_index >= 0)
98 cpu_register_physical_memory(addr + 0x20000, 0x1000, ide1_mem_index);
99 if (openpic_mem_index >= 0) {
100 cpu_register_physical_memory(addr + 0x40000, 0x40000,
101 openpic_mem_index);
102 }
267002cd
FB
103}
104
46e50e9d 105static void macio_init(PCIBus *bus)
267002cd
FB
106{
107 PCIDevice *d;
108
46e50e9d
FB
109 d = pci_register_device(bus, "macio", sizeof(PCIDevice),
110 -1, NULL, NULL);
267002cd
FB
111 /* Note: this code is strongly inspirated from the corresponding code
112 in PearPC */
113 d->config[0x00] = 0x6b; // vendor_id
114 d->config[0x01] = 0x10;
b6b8bd18 115 d->config[0x02] = 0x22;
267002cd
FB
116 d->config[0x03] = 0x00;
117
118 d->config[0x0a] = 0x00; // class_sub = pci2pci
119 d->config[0x0b] = 0xff; // class_base = bridge
120 d->config[0x0e] = 0x00; // header_type
121
122 d->config[0x3d] = 0x01; // interrupt on pin 1
123
124 dbdma_mem_index = cpu_register_io_memory(0, dbdma_read, dbdma_write, NULL);
125
126 pci_register_io_region(d, 0, 0x80000,
127 PCI_ADDRESS_SPACE_MEM, macio_map);
128}
129
0aa6a4a2
FB
130/* UniN device */
131static void unin_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
132{
133}
134
135static uint32_t unin_readl (void *opaque, target_phys_addr_t addr)
136{
137 return 0;
138}
139
140static CPUWriteMemoryFunc *unin_write[] = {
141 &unin_writel,
142 &unin_writel,
143 &unin_writel,
144};
145
146static CPUReadMemoryFunc *unin_read[] = {
147 &unin_readl,
148 &unin_readl,
149 &unin_readl,
150};
151
152/* temporary frame buffer OSI calls for the video.x driver. The right
153 solution is to modify the driver to use VGA PCI I/Os */
154static int vga_osi_call(CPUState *env)
155{
156 static int vga_vbl_enabled;
157 int linesize;
158
159 // printf("osi_call R5=%d\n", env->gpr[5]);
160
161 /* same handler as PearPC, coming from the original MOL video
162 driver. */
163 switch(env->gpr[5]) {
164 case 4:
165 break;
166 case 28: /* set_vmode */
167 if (env->gpr[6] != 1 || env->gpr[7] != 0)
168 env->gpr[3] = 1;
169 else
170 env->gpr[3] = 0;
171 break;
172 case 29: /* get_vmode_info */
173 if (env->gpr[6] != 0) {
174 if (env->gpr[6] != 1 || env->gpr[7] != 0) {
175 env->gpr[3] = 1;
176 break;
177 }
178 }
179 env->gpr[3] = 0;
180 env->gpr[4] = (1 << 16) | 1; /* num_vmodes, cur_vmode */
181 env->gpr[5] = (1 << 16) | 0; /* num_depths, cur_depth_mode */
182 env->gpr[6] = (graphic_width << 16) | graphic_height; /* w, h */
183 env->gpr[7] = 85 << 16; /* refresh rate */
184 env->gpr[8] = (graphic_depth + 7) & ~7; /* depth (round to byte) */
185 linesize = ((graphic_depth + 7) >> 3) * graphic_width;
186 linesize = (linesize + 3) & ~3;
187 env->gpr[9] = (linesize << 16) | 0; /* row_bytes, offset */
188 break;
189 case 31: /* set_video power */
190 env->gpr[3] = 0;
191 break;
192 case 39: /* video_ctrl */
193 if (env->gpr[6] == 0 || env->gpr[6] == 1)
194 vga_vbl_enabled = env->gpr[6];
195 env->gpr[3] = 0;
196 break;
197 case 47:
198 break;
199 case 59: /* set_color */
200 /* R6 = index, R7 = RGB */
201 env->gpr[3] = 0;
202 break;
203 case 64: /* get color */
204 /* R6 = index */
205 env->gpr[3] = 0;
206 break;
207 case 116: /* set hwcursor */
208 /* R6 = x, R7 = y, R8 = visible, R9 = data */
209 break;
210 default:
211 fprintf(stderr, "unsupported OSI call R5=%08x\n", env->gpr[5]);
212 break;
213 }
214 return 1; /* osi_call handled */
215}
216
3de388f6
FB
217/* XXX: suppress that */
218static void pic_irq_request(void *opaque, int level)
219{
220}
221
0aa6a4a2
FB
222/* PowerPC CHRP hardware initialisation */
223static void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
224 DisplayState *ds, const char **fd_filename,
225 int snapshot,
226 const char *kernel_filename,
227 const char *kernel_cmdline,
228 const char *initrd_filename,
229 int is_heathrow)
64201201
FB
230{
231 char buf[1024];
0aa6a4a2
FB
232 SetIRQFunc *set_irq;
233 void *pic;
64201201 234 m48t59_t *nvram;
0aa6a4a2 235 int PPC_io_memory, unin_memory;
d5295253
FB
236 int linux_boot, i;
237 unsigned long bios_offset, vga_bios_offset;
b6b8bd18 238 uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
3fc6c082 239 ppc_def_t *def;
46e50e9d 240 PCIBus *pci_bus;
0aa6a4a2 241 const char *arch_name;
d5295253 242 int vga_bios_size, bios_size;
46e50e9d 243
64201201
FB
244 linux_boot = (kernel_filename != NULL);
245
246 /* allocate RAM */
247 cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
248
249 /* allocate and load BIOS */
250 bios_offset = ram_size + vga_ram_size;
251 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
d5295253
FB
252 bios_size = load_image(buf, phys_ram_base + bios_offset);
253 if (bios_size < 0 || bios_size > BIOS_SIZE) {
254 fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n", buf);
64201201
FB
255 exit(1);
256 }
d5295253
FB
257 bios_size = (bios_size + 0xfff) & ~0xfff;
258 cpu_register_physical_memory((uint32_t)(-bios_size),
259 bios_size, bios_offset | IO_MEM_ROM);
260
261 /* allocate and load VGA BIOS */
262 vga_bios_offset = bios_offset + bios_size;
263 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
264 vga_bios_size = load_image(buf, phys_ram_base + vga_bios_offset + 8);
265 if (vga_bios_size < 0) {
266 /* if no bios is present, we can still work */
267 fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n", buf);
268 vga_bios_size = 0;
269 } else {
270 /* set a specific header (XXX: find real Apple format for NDRV
271 drivers) */
272 phys_ram_base[vga_bios_offset] = 'N';
273 phys_ram_base[vga_bios_offset + 1] = 'D';
274 phys_ram_base[vga_bios_offset + 2] = 'R';
275 phys_ram_base[vga_bios_offset + 3] = 'V';
276 cpu_to_be32w((uint32_t *)(phys_ram_base + vga_bios_offset + 4),
277 vga_bios_size);
278 vga_bios_size += 8;
279 }
280 vga_bios_size = (vga_bios_size + 0xfff) & ~0xfff;
281
b6b8bd18
FB
282 if (linux_boot) {
283 kernel_base = KERNEL_LOAD_ADDR;
284 /* now we can load the kernel */
285 kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
286 if (kernel_size < 0) {
287 fprintf(stderr, "qemu: could not load kernel '%s'\n",
288 kernel_filename);
289 exit(1);
290 }
291 /* load initrd */
292 if (initrd_filename) {
293 initrd_base = INITRD_LOAD_ADDR;
294 initrd_size = load_image(initrd_filename,
295 phys_ram_base + initrd_base);
296 if (initrd_size < 0) {
297 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
298 initrd_filename);
299 exit(1);
300 }
301 } else {
302 initrd_base = 0;
303 initrd_size = 0;
304 }
305 boot_device = 'm';
306 } else {
307 kernel_base = 0;
308 kernel_size = 0;
309 initrd_base = 0;
310 initrd_size = 0;
311 }
64201201 312 /* Register CPU as a 74x/75x */
3fc6c082
FB
313 /* XXX: CPU model (or PVR) should be provided on command line */
314 // ppc_find_by_name("750gx", &def); // Linux boot OK
315 // ppc_find_by_name("750fx", &def); // Linux boot OK
316 /* Linux does not boot on 750cxe (and probably other 750cx based)
317 * because it assumes it has 8 IBAT & DBAT pairs as it only have 4.
318 */
319 // ppc_find_by_name("750cxe", &def);
320 // ppc_find_by_name("750p", &def);
321 // ppc_find_by_name("740p", &def);
322 ppc_find_by_name("750", &def);
323 // ppc_find_by_name("740", &def);
324 // ppc_find_by_name("G3", &def);
325 // ppc_find_by_name("604r", &def);
326 // ppc_find_by_name("604e", &def);
327 // ppc_find_by_name("604", &def);
328 if (def == NULL) {
329 cpu_abort(cpu_single_env, "Unable to find PowerPC CPU definition\n");
330 }
331 cpu_ppc_register(cpu_single_env, def);
332
fa296b0f
FB
333 /* Set time-base frequency to 100 Mhz */
334 cpu_ppc_tb_init(cpu_single_env, 100UL * 1000UL * 1000UL);
0aa6a4a2
FB
335
336 cpu_single_env->osi_call = vga_osi_call;
337
338 if (is_heathrow) {
339 isa_mem_base = 0x80000000;
340 pci_bus = pci_grackle_init(0xfec00000);
341
342 /* Register 2 MB of ISA IO space */
343 PPC_io_memory = cpu_register_io_memory(0, PPC_io_read, PPC_io_write, NULL);
344 cpu_register_physical_memory(0xfe000000, 0x00200000, PPC_io_memory);
345
346 /* init basic PC hardware */
d5295253
FB
347 vga_initialize(pci_bus, ds, phys_ram_base + ram_size,
348 ram_size, vga_ram_size,
349 vga_bios_offset, vga_bios_size);
0aa6a4a2
FB
350 pic = heathrow_pic_init(&heathrow_pic_mem_index);
351 set_irq = heathrow_pic_set_irq;
352 pci_set_pic(pci_bus, set_irq, pic);
353
354 /* XXX: suppress that */
fa296b0f 355 isa_pic = pic_init(pic_irq_request, NULL);
0aa6a4a2
FB
356
357 /* XXX: use Mac Serial port */
358 serial_init(0x3f8, 4, serial_hds[0]);
359
360 for(i = 0; i < nb_nics; i++) {
361 pci_ne2000_init(pci_bus, &nd_table[i]);
362 }
363
364 pci_cmd646_ide_init(pci_bus, &bs_table[0], 0);
365
366 /* cuda also initialize ADB */
367 cuda_mem_index = cuda_init(set_irq, pic, 0x12);
368
369 adb_kbd_init(&adb_bus);
370 adb_mouse_init(&adb_bus);
371
372 macio_init(pci_bus);
373
374 nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE);
375
376 arch_name = "HEATHROW";
377 } else {
378 isa_mem_base = 0x80000000;
379 pci_bus = pci_pmac_init();
380
381 /* Register 8 MB of ISA IO space */
382 PPC_io_memory = cpu_register_io_memory(0, PPC_io_read, PPC_io_write, NULL);
383 cpu_register_physical_memory(0xF2000000, 0x00800000, PPC_io_memory);
384
385 /* UniN init */
386 unin_memory = cpu_register_io_memory(0, unin_read, unin_write, NULL);
387 cpu_register_physical_memory(0xf8000000, 0x00001000, unin_memory);
388
389 /* init basic PC hardware */
d5295253
FB
390 vga_initialize(pci_bus, ds, phys_ram_base + ram_size,
391 ram_size, vga_ram_size,
392 vga_bios_offset, vga_bios_size);
0aa6a4a2
FB
393 pic = openpic_init(NULL, &openpic_mem_index, 1);
394 set_irq = openpic_set_irq;
395 pci_set_pic(pci_bus, set_irq, pic);
396
397 /* XXX: suppress that */
fa296b0f 398 isa_pic = pic_init(pic_irq_request, NULL);
0aa6a4a2
FB
399
400 /* XXX: use Mac Serial port */
401 serial_init(0x3f8, 4, serial_hds[0]);
402
403 for(i = 0; i < nb_nics; i++) {
404 pci_ne2000_init(pci_bus, &nd_table[i]);
405 }
406
407#if 1
408 ide0_mem_index = pmac_ide_init(&bs_table[0], set_irq, pic, 0x13);
409 ide1_mem_index = pmac_ide_init(&bs_table[2], set_irq, pic, 0x14);
410#else
411 pci_cmd646_ide_init(pci_bus, &bs_table[0], 0);
412#endif
413 /* cuda also initialize ADB */
414 cuda_mem_index = cuda_init(set_irq, pic, 0x19);
415
416 adb_kbd_init(&adb_bus);
417 adb_mouse_init(&adb_bus);
418
419 macio_init(pci_bus);
420
421 nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE);
422
423 arch_name = "MAC99";
64201201 424 }
b6b8bd18
FB
425
426 if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
427 graphic_depth = 15;
64201201 428
0aa6a4a2 429 PPC_NVRAM_set_params(nvram, NVRAM_SIZE, arch_name, ram_size, boot_device,
b6b8bd18
FB
430 kernel_base, kernel_size,
431 kernel_cmdline,
432 initrd_base, initrd_size,
64201201 433 /* XXX: need an option to load a NVRAM image */
b6b8bd18
FB
434 0,
435 graphic_width, graphic_height, graphic_depth);
436 /* No PCI init: the BIOS will do it */
0aa6a4a2
FB
437
438 /* Special port to get debug messages from Open-Firmware */
439 register_ioport_write(0x0F00, 4, 1, &PPC_debug_write, NULL);
440}
441
442static void ppc_core99_init(int ram_size, int vga_ram_size, int boot_device,
443 DisplayState *ds, const char **fd_filename,
444 int snapshot,
445 const char *kernel_filename,
446 const char *kernel_cmdline,
447 const char *initrd_filename)
448{
449 ppc_chrp_init(ram_size, vga_ram_size, boot_device,
450 ds, fd_filename, snapshot,
451 kernel_filename, kernel_cmdline,
452 initrd_filename, 0);
64201201 453}
0aa6a4a2
FB
454
455static void ppc_heathrow_init(int ram_size, int vga_ram_size, int boot_device,
456 DisplayState *ds, const char **fd_filename,
457 int snapshot,
458 const char *kernel_filename,
459 const char *kernel_cmdline,
460 const char *initrd_filename)
461{
462 ppc_chrp_init(ram_size, vga_ram_size, boot_device,
463 ds, fd_filename, snapshot,
464 kernel_filename, kernel_cmdline,
465 initrd_filename, 1);
466}
467
468QEMUMachine core99_machine = {
0289b2c1
FB
469 "mac99",
470 "Mac99 based PowerMAC",
0aa6a4a2
FB
471 ppc_core99_init,
472};
473
474QEMUMachine heathrow_machine = {
0289b2c1 475 "g3bw",
0aa6a4a2
FB
476 "Heathrow based PowerMAC",
477 ppc_heathrow_init,
478};