1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "cpu.h"
26#include "hw/hw.h"
27#include "hw/timer/m48t59.h"
28#include "hw/i386/pc.h"
29#include "hw/char/serial.h"
30#include "hw/block/fdc.h"
31#include "net/net.h"
32#include "sysemu/sysemu.h"
33#include "hw/isa/isa.h"
34#include "hw/pci/pci.h"
35#include "hw/pci/pci_host.h"
36#include "hw/ppc/ppc.h"
37#include "hw/boards.h"
38#include "qemu/error-report.h"
39#include "qemu/log.h"
40#include "hw/ide.h"
41#include "hw/loader.h"
42#include "hw/timer/mc146818rtc.h"
43#include "hw/isa/pc87312.h"
44#include "sysemu/block-backend.h"
45#include "sysemu/arch_init.h"
46#include "sysemu/qtest.h"
47#include "exec/address-spaces.h"
48#include "trace.h"
49#include "elf.h"
50#include "qemu/cutils.h"
51
52
53#define MAX_CPUS 1
54
55#define MAX_IDE_BUS 2
56
57#define BIOS_SIZE (1024 * 1024)
58#define BIOS_FILENAME "ppc_rom.bin"
59#define KERNEL_LOAD_ADDR 0x01000000
60#define INITRD_LOAD_ADDR 0x01800000
61
62
63static const int ide_iobase[2] = { 0x1f0, 0x170 };
64static const int ide_iobase2[2] = { 0x3f6, 0x376 };
65static const int ide_irq[2] = { 13, 13 };
66
67#define NE2000_NB_MAX 6
68
69static uint32_t ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
70static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
71
72
73#define PPC_IO_BASE 0x80000000
74
75
76#if 0
77static struct {
78
79 uint32_t veni_devi;
80 uint32_t revi;
81
82 uint32_t gcsr;
83 uint32_t xcfr;
84 uint32_t ct32;
85 uint32_t mcsr;
86
87 uint32_t gprg[6];
88
89 uint32_t feen;
90 uint32_t fest;
91 uint32_t fema;
92 uint32_t fecl;
93 uint32_t eeen;
94 uint32_t eest;
95 uint32_t eecl;
96 uint32_t eeint;
97 uint32_t eemck0;
98 uint32_t eemck1;
99
100} XCSR;
101
102static void PPC_XCSR_writeb (void *opaque,
103 hwaddr addr, uint32_t value)
104{
105 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
106 value);
107}
108
109static void PPC_XCSR_writew (void *opaque,
110 hwaddr addr, uint32_t value)
111{
112 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
113 value);
114}
115
116static void PPC_XCSR_writel (void *opaque,
117 hwaddr addr, uint32_t value)
118{
119 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
120 value);
121}
122
123static uint32_t PPC_XCSR_readb (void *opaque, hwaddr addr)
124{
125 uint32_t retval = 0;
126
127 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
128 retval);
129
130 return retval;
131}
132
133static uint32_t PPC_XCSR_readw (void *opaque, hwaddr addr)
134{
135 uint32_t retval = 0;
136
137 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
138 retval);
139
140 return retval;
141}
142
143static uint32_t PPC_XCSR_readl (void *opaque, hwaddr addr)
144{
145 uint32_t retval = 0;
146
147 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
148 retval);
149
150 return retval;
151}
152
153static const MemoryRegionOps PPC_XCSR_ops = {
154 .old_mmio = {
155 .read = { PPC_XCSR_readb, PPC_XCSR_readw, PPC_XCSR_readl, },
156 .write = { PPC_XCSR_writeb, PPC_XCSR_writew, PPC_XCSR_writel, },
157 },
158 .endianness = DEVICE_LITTLE_ENDIAN,
159};
160
161#endif
162
163
164typedef struct sysctrl_t {
165 qemu_irq reset_irq;
166 Nvram *nvram;
167 uint8_t state;
168 uint8_t syscontrol;
169 int contiguous_map;
170 qemu_irq contiguous_map_irq;
171 int endian;
172} sysctrl_t;
173
174enum {
175 STATE_HARDFILE = 0x01,
176};
177
178static sysctrl_t *sysctrl;
179
180static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
181{
182 sysctrl_t *sysctrl = opaque;
183
184 trace_prep_io_800_writeb(addr - PPC_IO_BASE, val);
185 switch (addr) {
186 case 0x0092:
187
188
189 if (val & 0x01) {
190 qemu_irq_raise(sysctrl->reset_irq);
191 } else {
192 qemu_irq_lower(sysctrl->reset_irq);
193 }
194
195 if (val & 0x02) {
196 sysctrl->endian = 1;
197 } else {
198 sysctrl->endian = 0;
199 }
200 break;
201 case 0x0800:
202
203 break;
204 case 0x0802:
205
206 break;
207 case 0x0803:
208
209 break;
210 case 0x0808:
211
212 if (val & 1)
213 sysctrl->state |= STATE_HARDFILE;
214 else
215 sysctrl->state &= ~STATE_HARDFILE;
216 break;
217 case 0x0810:
218
219 if (sysctrl->nvram != NULL) {
220 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
221 (k->toggle_lock)(sysctrl->nvram, 1);
222 }
223 break;
224 case 0x0812:
225
226 if (sysctrl->nvram != NULL) {
227 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
228 (k->toggle_lock)(sysctrl->nvram, 2);
229 }
230 break;
231 case 0x0814:
232
233
234 break;
235 case 0x081C:
236
237 sysctrl->syscontrol = val & 0x0F;
238 break;
239 case 0x0850:
240
241 sysctrl->contiguous_map = val & 0x01;
242 qemu_set_irq(sysctrl->contiguous_map_irq, sysctrl->contiguous_map);
243 break;
244 default:
245 printf("ERROR: unaffected IO port write: %04" PRIx32
246 " => %02" PRIx32"\n", addr, val);
247 break;
248 }
249}
250
251static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
252{
253 sysctrl_t *sysctrl = opaque;
254 uint32_t retval = 0xFF;
255
256 switch (addr) {
257 case 0x0092:
258
259 retval = sysctrl->endian << 1;
260 break;
261 case 0x0800:
262
263 retval = 0xEF;
264 break;
265 case 0x0802:
266
267 retval = 0xAD;
268 break;
269 case 0x0803:
270
271 retval = 0xE0;
272 break;
273 case 0x080C:
274
275
276
277
278
279
280 retval = 0x3C;
281 break;
282 case 0x0810:
283
284 retval = 0x39;
285 break;
286 case 0x0814:
287
288 break;
289 case 0x0818:
290
291 retval = 0x00;
292 break;
293 case 0x081C:
294
295
296
297 retval = sysctrl->syscontrol;
298 break;
299 case 0x0823:
300
301 retval = 0x03;
302 break;
303 case 0x0850:
304
305 retval = sysctrl->contiguous_map;
306 break;
307 default:
308 printf("ERROR: unaffected IO port: %04" PRIx32 " read\n", addr);
309 break;
310 }
311 trace_prep_io_800_readb(addr - PPC_IO_BASE, retval);
312
313 return retval;
314}
315
316
317#define NVRAM_SIZE 0x2000
318
319static void ppc_prep_reset(void *opaque)
320{
321 PowerPCCPU *cpu = opaque;
322
323 cpu_reset(CPU(cpu));
324}
325
326static const MemoryRegionPortio prep_portio_list[] = {
327
328 { 0x0092, 1, 1, .read = PREP_io_800_readb, .write = PREP_io_800_writeb, },
329 { 0x0800, 0x52, 1,
330 .read = PREP_io_800_readb, .write = PREP_io_800_writeb, },
331
332 { 0x0F00, 4, 1, .write = PPC_debug_write, },
333 PORTIO_END_OF_LIST(),
334};
335
336static PortioList prep_port_list;
337
338
339
340static inline uint32_t nvram_read(Nvram *nvram, uint32_t addr)
341{
342 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
343 return (k->read)(nvram, addr);
344}
345
346static inline void nvram_write(Nvram *nvram, uint32_t addr, uint32_t val)
347{
348 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
349 (k->write)(nvram, addr, val);
350}
351
352static void NVRAM_set_byte(Nvram *nvram, uint32_t addr, uint8_t value)
353{
354 nvram_write(nvram, addr, value);
355}
356
357static uint8_t NVRAM_get_byte(Nvram *nvram, uint32_t addr)
358{
359 return nvram_read(nvram, addr);
360}
361
362static void NVRAM_set_word(Nvram *nvram, uint32_t addr, uint16_t value)
363{
364 nvram_write(nvram, addr, value >> 8);
365 nvram_write(nvram, addr + 1, value & 0xFF);
366}
367
368static uint16_t NVRAM_get_word(Nvram *nvram, uint32_t addr)
369{
370 uint16_t tmp;
371
372 tmp = nvram_read(nvram, addr) << 8;
373 tmp |= nvram_read(nvram, addr + 1);
374
375 return tmp;
376}
377
378static void NVRAM_set_lword(Nvram *nvram, uint32_t addr, uint32_t value)
379{
380 nvram_write(nvram, addr, value >> 24);
381 nvram_write(nvram, addr + 1, (value >> 16) & 0xFF);
382 nvram_write(nvram, addr + 2, (value >> 8) & 0xFF);
383 nvram_write(nvram, addr + 3, value & 0xFF);
384}
385
386static void NVRAM_set_string(Nvram *nvram, uint32_t addr, const char *str,
387 uint32_t max)
388{
389 int i;
390
391 for (i = 0; i < max && str[i] != '\0'; i++) {
392 nvram_write(nvram, addr + i, str[i]);
393 }
394 nvram_write(nvram, addr + i, str[i]);
395 nvram_write(nvram, addr + max - 1, '\0');
396}
397
398static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
399{
400 uint16_t tmp;
401 uint16_t pd, pd1, pd2;
402
403 tmp = prev >> 8;
404 pd = prev ^ value;
405 pd1 = pd & 0x000F;
406 pd2 = ((pd >> 4) & 0x000F) ^ pd1;
407 tmp ^= (pd1 << 3) | (pd1 << 8);
408 tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
409
410 return tmp;
411}
412
413static uint16_t NVRAM_compute_crc (Nvram *nvram, uint32_t start, uint32_t count)
414{
415 uint32_t i;
416 uint16_t crc = 0xFFFF;
417 int odd;
418
419 odd = count & 1;
420 count &= ~1;
421 for (i = 0; i != count; i++) {
422 crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
423 }
424 if (odd) {
425 crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
426 }
427
428 return crc;
429}
430
431#define CMDLINE_ADDR 0x017ff000
432
433static int PPC_NVRAM_set_params (Nvram *nvram, uint16_t NVRAM_size,
434 const char *arch,
435 uint32_t RAM_size, int boot_device,
436 uint32_t kernel_image, uint32_t kernel_size,
437 const char *cmdline,
438 uint32_t initrd_image, uint32_t initrd_size,
439 uint32_t NVRAM_image,
440 int width, int height, int depth)
441{
442 uint16_t crc;
443
444
445 NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
446 NVRAM_set_lword(nvram, 0x10, 0x00000002);
447 NVRAM_set_word(nvram, 0x14, NVRAM_size);
448 NVRAM_set_string(nvram, 0x20, arch, 16);
449 NVRAM_set_lword(nvram, 0x30, RAM_size);
450 NVRAM_set_byte(nvram, 0x34, boot_device);
451 NVRAM_set_lword(nvram, 0x38, kernel_image);
452 NVRAM_set_lword(nvram, 0x3C, kernel_size);
453 if (cmdline) {
454
455 pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR,
456 cmdline);
457 NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
458 NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
459 } else {
460 NVRAM_set_lword(nvram, 0x40, 0);
461 NVRAM_set_lword(nvram, 0x44, 0);
462 }
463 NVRAM_set_lword(nvram, 0x48, initrd_image);
464 NVRAM_set_lword(nvram, 0x4C, initrd_size);
465 NVRAM_set_lword(nvram, 0x50, NVRAM_image);
466
467 NVRAM_set_word(nvram, 0x54, width);
468 NVRAM_set_word(nvram, 0x56, height);
469 NVRAM_set_word(nvram, 0x58, depth);
470 crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
471 NVRAM_set_word(nvram, 0xFC, crc);
472
473 return 0;
474}
475
476
477static void ppc_prep_init(MachineState *machine)
478{
479 ram_addr_t ram_size = machine->ram_size;
480 const char *kernel_filename = machine->kernel_filename;
481 const char *kernel_cmdline = machine->kernel_cmdline;
482 const char *initrd_filename = machine->initrd_filename;
483 const char *boot_device = machine->boot_order;
484 MemoryRegion *sysmem = get_system_memory();
485 PowerPCCPU *cpu = NULL;
486 CPUPPCState *env = NULL;
487 Nvram *m48t59;
488#if 0
489 MemoryRegion *xcsr = g_new(MemoryRegion, 1);
490#endif
491 int linux_boot, i, nb_nics1;
492 MemoryRegion *ram = g_new(MemoryRegion, 1);
493 uint32_t kernel_base, initrd_base;
494 long kernel_size, initrd_size;
495 DeviceState *dev;
496 PCIHostState *pcihost;
497 PCIBus *pci_bus;
498 PCIDevice *pci;
499 ISABus *isa_bus;
500 ISADevice *isa;
501 int ppc_boot_device;
502 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
503
504 sysctrl = g_malloc0(sizeof(sysctrl_t));
505
506 linux_boot = (kernel_filename != NULL);
507
508
509 if (machine->cpu_model == NULL)
510 machine->cpu_model = "602";
511 for (i = 0; i < smp_cpus; i++) {
512 cpu = cpu_ppc_init(machine->cpu_model);
513 if (cpu == NULL) {
514 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
515 exit(1);
516 }
517 env = &cpu->env;
518
519 if (env->flags & POWERPC_FLAG_RTC_CLK) {
520
521 cpu_ppc_tb_init(env, 7812500UL);
522 } else {
523
524 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
525 }
526 qemu_register_reset(ppc_prep_reset, cpu);
527 }
528
529
530 memory_region_allocate_system_memory(ram, NULL, "ppc_prep.ram", ram_size);
531 memory_region_add_subregion(sysmem, 0, ram);
532
533 if (linux_boot) {
534 kernel_base = KERNEL_LOAD_ADDR;
535
536 kernel_size = load_image_targphys(kernel_filename, kernel_base,
537 ram_size - kernel_base);
538 if (kernel_size < 0) {
539 error_report("could not load kernel '%s'", kernel_filename);
540 exit(1);
541 }
542
543 if (initrd_filename) {
544 initrd_base = INITRD_LOAD_ADDR;
545 initrd_size = load_image_targphys(initrd_filename, initrd_base,
546 ram_size - initrd_base);
547 if (initrd_size < 0) {
548 error_report("could not load initial ram disk '%s'",
549 initrd_filename);
550 exit(1);
551 }
552 } else {
553 initrd_base = 0;
554 initrd_size = 0;
555 }
556 ppc_boot_device = 'm';
557 } else {
558 kernel_base = 0;
559 kernel_size = 0;
560 initrd_base = 0;
561 initrd_size = 0;
562 ppc_boot_device = '\0';
563
564 for (i = 0; boot_device[i] != '\0'; i++) {
565 if (boot_device[i] >= 'a' && boot_device[i] <= 'f') {
566 ppc_boot_device = boot_device[i];
567 break;
568 }
569 }
570 if (ppc_boot_device == '\0') {
571 fprintf(stderr, "No valid boot device for Mac99 machine\n");
572 exit(1);
573 }
574 }
575
576 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
577 error_report("Only 6xx bus is supported on PREP machine");
578 exit(1);
579 }
580
581 dev = qdev_create(NULL, "raven-pcihost");
582 if (bios_name == NULL) {
583 bios_name = BIOS_FILENAME;
584 }
585 qdev_prop_set_string(dev, "bios-name", bios_name);
586 qdev_prop_set_uint32(dev, "elf-machine", PPC_ELF_MACHINE);
587 pcihost = PCI_HOST_BRIDGE(dev);
588 object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev), NULL);
589 qdev_init_nofail(dev);
590 pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
591 if (pci_bus == NULL) {
592 fprintf(stderr, "Couldn't create PCI host controller.\n");
593 exit(1);
594 }
595 sysctrl->contiguous_map_irq = qdev_get_gpio_in(dev, 0);
596
597
598 pci = pci_create_simple(pci_bus, PCI_DEVFN(1, 0), "i82378");
599 cpu = POWERPC_CPU(first_cpu);
600 qdev_connect_gpio_out(&pci->qdev, 0,
601 cpu->env.irq_inputs[PPC6xx_INPUT_INT]);
602 sysbus_connect_irq(&pcihost->busdev, 0, qdev_get_gpio_in(&pci->qdev, 9));
603 sysbus_connect_irq(&pcihost->busdev, 1, qdev_get_gpio_in(&pci->qdev, 11));
604 sysbus_connect_irq(&pcihost->busdev, 2, qdev_get_gpio_in(&pci->qdev, 9));
605 sysbus_connect_irq(&pcihost->busdev, 3, qdev_get_gpio_in(&pci->qdev, 11));
606 isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(pci), "isa.0"));
607
608
609 isa = isa_create(isa_bus, TYPE_PC87312);
610 dev = DEVICE(isa);
611 qdev_prop_set_uint8(dev, "config", 13);
612 qdev_init_nofail(dev);
613
614
615 pci_vga_init(pci_bus);
616
617 nb_nics1 = nb_nics;
618 if (nb_nics1 > NE2000_NB_MAX)
619 nb_nics1 = NE2000_NB_MAX;
620 for(i = 0; i < nb_nics1; i++) {
621 if (nd_table[i].model == NULL) {
622 nd_table[i].model = g_strdup("ne2k_isa");
623 }
624 if (strcmp(nd_table[i].model, "ne2k_isa") == 0) {
625 isa_ne2000_init(isa_bus, ne2000_io[i], ne2000_irq[i],
626 &nd_table[i]);
627 } else {
628 pci_nic_init_nofail(&nd_table[i], pci_bus, "ne2k_pci", NULL);
629 }
630 }
631
632 ide_drive_get(hd, ARRAY_SIZE(hd));
633 for(i = 0; i < MAX_IDE_BUS; i++) {
634 isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
635 hd[2 * i],
636 hd[2 * i + 1]);
637 }
638 isa_create_simple(isa_bus, "i8042");
639
640 cpu = POWERPC_CPU(first_cpu);
641 sysctrl->reset_irq = cpu->env.irq_inputs[PPC6xx_INPUT_HRESET];
642
643 portio_list_init(&prep_port_list, NULL, prep_portio_list, sysctrl, "prep");
644 portio_list_add(&prep_port_list, isa_address_space_io(isa), 0x0);
645
646
647#if 0
648 memory_region_init_io(xcsr, NULL, &PPC_XCSR_ops, NULL, "ppc-xcsr", 0x1000);
649 memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
650#endif
651
652 if (machine_usb(machine)) {
653 pci_create_simple(pci_bus, -1, "pci-ohci");
654 }
655
656 m48t59 = m48t59_init_isa(isa_bus, 0x0074, NVRAM_SIZE, 2000, 59);
657 if (m48t59 == NULL)
658 return;
659 sysctrl->nvram = m48t59;
660
661
662 PPC_NVRAM_set_params(m48t59, NVRAM_SIZE, "PREP", ram_size,
663 ppc_boot_device,
664 kernel_base, kernel_size,
665 kernel_cmdline,
666 initrd_base, initrd_size,
667
668 0,
669 graphic_width, graphic_height, graphic_depth);
670}
671
672static void prep_machine_init(MachineClass *mc)
673{
674 mc->desc = "PowerPC PREP platform";
675 mc->init = ppc_prep_init;
676 mc->max_cpus = MAX_CPUS;
677 mc->default_boot_order = "cad";
678}
679
680DEFINE_MACHINE("prep", prep_machine_init)
681