blob: 07ffe72de1d062b550503222fb3c06cb189419b1 [file] [log] [blame]
bellarda541f292004-04-12 20:39:29 +00001/*
2 * QEMU PPC PREP hardware System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
j_mayer47103572007-03-30 09:38:04 +00004 * Copyright (c) 2003-2007 Jocelyn Mayer
ths5fafdf22007-09-16 21:08:06 +00005 *
bellarda541f292004-04-12 20:39:29 +00006 * 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 */
Peter Maydell0d755902016-01-26 18:16:58 +000024#include "qemu/osdep.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010025#include "cpu.h"
Andreas Färber75610152013-01-26 20:41:58 +010026#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010027#include "hw/timer/m48t59.h"
28#include "hw/i386/pc.h"
29#include "hw/char/serial.h"
30#include "hw/block/fdc.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020031#include "net/net.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010032#include "sysemu/sysemu.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010033#include "hw/isa/isa.h"
Andreas Färber75610152013-01-26 20:41:58 +010034#include "hw/pci/pci.h"
35#include "hw/pci/pci_host.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010036#include "hw/ppc/ppc.h"
Andreas Färber75610152013-01-26 20:41:58 +010037#include "hw/boards.h"
Markus Armbrusterc5254362015-12-17 17:35:09 +010038#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/log.h"
Andreas Färber75610152013-01-26 20:41:58 +010040#include "hw/ide.h"
41#include "hw/loader.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010042#include "hw/timer/mc146818rtc.h"
43#include "hw/isa/pc87312.h"
Markus Armbruster4be74632014-10-07 13:59:18 +020044#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010045#include "sysemu/arch_init.h"
Andreas Färber97c42c32013-04-27 21:23:23 +020046#include "sysemu/qtest.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010047#include "exec/address-spaces.h"
Paolo Bonzini659f7f62015-10-16 15:16:11 +020048#include "trace.h"
Andreas Färber97c42c32013-04-27 21:23:23 +020049#include "elf.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020050#include "qemu/cutils.h"
bellard9fddaa02004-05-21 12:59:32 +000051
j_mayerfe33cc72007-10-03 01:06:57 +000052/* SMP is not enabled, for now */
53#define MAX_CPUS 1
54
thse4bcb142007-12-02 04:51:10 +000055#define MAX_IDE_BUS 2
56
Paul Brookbba831e2009-05-19 14:52:42 +010057#define BIOS_SIZE (1024 * 1024)
bellardb6b8bd12004-06-21 16:55:53 +000058#define BIOS_FILENAME "ppc_rom.bin"
59#define KERNEL_LOAD_ADDR 0x01000000
60#define INITRD_LOAD_ADDR 0x01800000
bellard64201202004-05-26 22:55:16 +000061
bellard64201202004-05-26 22:55:16 +000062/* Constants for devices init */
bellarda541f292004-04-12 20:39:29 +000063static 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
bellard64201202004-05-26 22:55:16 +000072/* ISA IO ports bridge */
bellarda541f292004-04-12 20:39:29 +000073#define PPC_IO_BASE 0x80000000
74
bellard64201202004-05-26 22:55:16 +000075/* PowerPC control and status registers */
76#if 0 // Not used
77static struct {
78 /* IDs */
79 uint32_t veni_devi;
80 uint32_t revi;
81 /* Control and status */
82 uint32_t gcsr;
83 uint32_t xcfr;
84 uint32_t ct32;
85 uint32_t mcsr;
86 /* General purpose registers */
87 uint32_t gprg[6];
88 /* Exceptions */
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 /* Error diagnostic */
100} XCSR;
bellarda541f292004-04-12 20:39:29 +0000101
j_mayer36081602007-09-17 08:21:54 +0000102static void PPC_XCSR_writeb (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200103 hwaddr addr, uint32_t value)
bellard64201202004-05-26 22:55:16 +0000104{
Blue Swirl90e189e2009-08-16 11:13:18 +0000105 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
106 value);
bellard64201202004-05-26 22:55:16 +0000107}
108
j_mayer36081602007-09-17 08:21:54 +0000109static void PPC_XCSR_writew (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200110 hwaddr addr, uint32_t value)
bellard64201202004-05-26 22:55:16 +0000111{
Blue Swirl90e189e2009-08-16 11:13:18 +0000112 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
113 value);
bellard64201202004-05-26 22:55:16 +0000114}
115
j_mayer36081602007-09-17 08:21:54 +0000116static void PPC_XCSR_writel (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200117 hwaddr addr, uint32_t value)
bellard64201202004-05-26 22:55:16 +0000118{
Blue Swirl90e189e2009-08-16 11:13:18 +0000119 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
120 value);
bellard64201202004-05-26 22:55:16 +0000121}
122
Avi Kivitya8170e52012-10-23 12:30:10 +0200123static uint32_t PPC_XCSR_readb (void *opaque, hwaddr addr)
bellard64201202004-05-26 22:55:16 +0000124{
125 uint32_t retval = 0;
126
Blue Swirl90e189e2009-08-16 11:13:18 +0000127 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
128 retval);
bellard64201202004-05-26 22:55:16 +0000129
130 return retval;
131}
132
Avi Kivitya8170e52012-10-23 12:30:10 +0200133static uint32_t PPC_XCSR_readw (void *opaque, hwaddr addr)
bellard64201202004-05-26 22:55:16 +0000134{
135 uint32_t retval = 0;
136
Blue Swirl90e189e2009-08-16 11:13:18 +0000137 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
138 retval);
bellard64201202004-05-26 22:55:16 +0000139
140 return retval;
141}
142
Avi Kivitya8170e52012-10-23 12:30:10 +0200143static uint32_t PPC_XCSR_readl (void *opaque, hwaddr addr)
bellard64201202004-05-26 22:55:16 +0000144{
145 uint32_t retval = 0;
146
Blue Swirl90e189e2009-08-16 11:13:18 +0000147 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
148 retval);
bellard64201202004-05-26 22:55:16 +0000149
150 return retval;
151}
152
Avi Kivity0c90c522011-09-25 16:57:45 +0300153static 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,
bellard64201202004-05-26 22:55:16 +0000159};
160
bellardb6b8bd12004-06-21 16:55:53 +0000161#endif
bellard64201202004-05-26 22:55:16 +0000162
bellarda541f292004-04-12 20:39:29 +0000163/* Fake super-io ports for PREP platform (Intel 82378ZB) */
Anthony Liguoric227f092009-10-01 16:12:16 -0500164typedef struct sysctrl_t {
j_mayerc4781a52007-10-29 10:21:12 +0000165 qemu_irq reset_irq;
Hervé Poussineau31688242015-03-02 22:23:27 +0000166 Nvram *nvram;
bellard64201202004-05-26 22:55:16 +0000167 uint8_t state;
168 uint8_t syscontrol;
bellardda9b2662005-04-23 18:18:54 +0000169 int contiguous_map;
Hervé Poussineau9a183912014-03-17 23:00:20 +0100170 qemu_irq contiguous_map_irq;
bellardfb3444b2005-07-03 13:57:11 +0000171 int endian;
Anthony Liguoric227f092009-10-01 16:12:16 -0500172} sysctrl_t;
bellard64201202004-05-26 22:55:16 +0000173
174enum {
175 STATE_HARDFILE = 0x01,
176};
177
Anthony Liguoric227f092009-10-01 16:12:16 -0500178static sysctrl_t *sysctrl;
bellarda541f292004-04-12 20:39:29 +0000179
bellarda541f292004-04-12 20:39:29 +0000180static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
181{
Anthony Liguoric227f092009-10-01 16:12:16 -0500182 sysctrl_t *sysctrl = opaque;
bellard64201202004-05-26 22:55:16 +0000183
Paolo Bonzini659f7f62015-10-16 15:16:11 +0200184 trace_prep_io_800_writeb(addr - PPC_IO_BASE, val);
bellarda541f292004-04-12 20:39:29 +0000185 switch (addr) {
186 case 0x0092:
187 /* Special port 92 */
188 /* Check soft reset asked */
bellard64201202004-05-26 22:55:16 +0000189 if (val & 0x01) {
j_mayerc4781a52007-10-29 10:21:12 +0000190 qemu_irq_raise(sysctrl->reset_irq);
191 } else {
192 qemu_irq_lower(sysctrl->reset_irq);
bellarda541f292004-04-12 20:39:29 +0000193 }
194 /* Check LE mode */
bellard64201202004-05-26 22:55:16 +0000195 if (val & 0x02) {
bellardfb3444b2005-07-03 13:57:11 +0000196 sysctrl->endian = 1;
197 } else {
198 sysctrl->endian = 0;
bellarda541f292004-04-12 20:39:29 +0000199 }
200 break;
bellard64201202004-05-26 22:55:16 +0000201 case 0x0800:
202 /* Motorola CPU configuration register : read-only */
203 break;
204 case 0x0802:
205 /* Motorola base module feature register : read-only */
206 break;
207 case 0x0803:
208 /* Motorola base module status register : read-only */
209 break;
bellarda541f292004-04-12 20:39:29 +0000210 case 0x0808:
bellard64201202004-05-26 22:55:16 +0000211 /* Hardfile light register */
212 if (val & 1)
213 sysctrl->state |= STATE_HARDFILE;
214 else
215 sysctrl->state &= ~STATE_HARDFILE;
bellarda541f292004-04-12 20:39:29 +0000216 break;
217 case 0x0810:
218 /* Password protect 1 register */
Hervé Poussineau31688242015-03-02 22:23:27 +0000219 if (sysctrl->nvram != NULL) {
220 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
221 (k->toggle_lock)(sysctrl->nvram, 1);
222 }
bellarda541f292004-04-12 20:39:29 +0000223 break;
224 case 0x0812:
225 /* Password protect 2 register */
Hervé Poussineau31688242015-03-02 22:23:27 +0000226 if (sysctrl->nvram != NULL) {
227 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
228 (k->toggle_lock)(sysctrl->nvram, 2);
229 }
bellarda541f292004-04-12 20:39:29 +0000230 break;
231 case 0x0814:
bellard64201202004-05-26 22:55:16 +0000232 /* L2 invalidate register */
bellardc68ea702005-11-21 23:33:12 +0000233 // tlb_flush(first_cpu, 1);
bellarda541f292004-04-12 20:39:29 +0000234 break;
235 case 0x081C:
236 /* system control register */
bellard64201202004-05-26 22:55:16 +0000237 sysctrl->syscontrol = val & 0x0F;
bellarda541f292004-04-12 20:39:29 +0000238 break;
239 case 0x0850:
240 /* I/O map type register */
bellardda9b2662005-04-23 18:18:54 +0000241 sysctrl->contiguous_map = val & 0x01;
Hervé Poussineau9a183912014-03-17 23:00:20 +0100242 qemu_set_irq(sysctrl->contiguous_map_irq, sysctrl->contiguous_map);
bellarda541f292004-04-12 20:39:29 +0000243 break;
244 default:
j_mayeraae93662007-11-24 02:56:36 +0000245 printf("ERROR: unaffected IO port write: %04" PRIx32
246 " => %02" PRIx32"\n", addr, val);
bellarda541f292004-04-12 20:39:29 +0000247 break;
248 }
249}
250
251static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
252{
Anthony Liguoric227f092009-10-01 16:12:16 -0500253 sysctrl_t *sysctrl = opaque;
bellarda541f292004-04-12 20:39:29 +0000254 uint32_t retval = 0xFF;
255
256 switch (addr) {
257 case 0x0092:
258 /* Special port 92 */
Julio Guerrab6f54b32013-05-05 23:29:48 +0200259 retval = sysctrl->endian << 1;
bellard64201202004-05-26 22:55:16 +0000260 break;
261 case 0x0800:
262 /* Motorola CPU configuration register */
263 retval = 0xEF; /* MPC750 */
264 break;
265 case 0x0802:
266 /* Motorola Base module feature register */
267 retval = 0xAD; /* No ESCC, PMC slot neither ethernet */
268 break;
269 case 0x0803:
270 /* Motorola base module status register */
271 retval = 0xE0; /* Standard MPC750 */
bellarda541f292004-04-12 20:39:29 +0000272 break;
273 case 0x080C:
274 /* Equipment present register:
275 * no L2 cache
276 * no upgrade processor
277 * no cards in PCI slots
278 * SCSI fuse is bad
279 */
bellard64201202004-05-26 22:55:16 +0000280 retval = 0x3C;
281 break;
282 case 0x0810:
283 /* Motorola base module extended feature register */
284 retval = 0x39; /* No USB, CF and PCI bridge. NVRAM present */
bellarda541f292004-04-12 20:39:29 +0000285 break;
bellardda9b2662005-04-23 18:18:54 +0000286 case 0x0814:
287 /* L2 invalidate: don't care */
288 break;
bellarda541f292004-04-12 20:39:29 +0000289 case 0x0818:
290 /* Keylock */
291 retval = 0x00;
292 break;
293 case 0x081C:
294 /* system control register
295 * 7 - 6 / 1 - 0: L2 cache enable
296 */
bellard64201202004-05-26 22:55:16 +0000297 retval = sysctrl->syscontrol;
bellarda541f292004-04-12 20:39:29 +0000298 break;
299 case 0x0823:
300 /* */
301 retval = 0x03; /* no L2 cache */
302 break;
303 case 0x0850:
304 /* I/O map type register */
bellardda9b2662005-04-23 18:18:54 +0000305 retval = sysctrl->contiguous_map;
bellarda541f292004-04-12 20:39:29 +0000306 break;
307 default:
j_mayeraae93662007-11-24 02:56:36 +0000308 printf("ERROR: unaffected IO port: %04" PRIx32 " read\n", addr);
bellarda541f292004-04-12 20:39:29 +0000309 break;
310 }
Paolo Bonzini659f7f62015-10-16 15:16:11 +0200311 trace_prep_io_800_readb(addr - PPC_IO_BASE, retval);
bellarda541f292004-04-12 20:39:29 +0000312
313 return retval;
314}
315
bellardda9b2662005-04-23 18:18:54 +0000316
bellard64201202004-05-26 22:55:16 +0000317#define NVRAM_SIZE 0x2000
bellarda541f292004-04-12 20:39:29 +0000318
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100319static void ppc_prep_reset(void *opaque)
320{
Andreas Färber5c3e7352012-05-04 17:46:13 +0200321 PowerPCCPU *cpu = opaque;
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100322
Andreas Färber5c3e7352012-05-04 17:46:13 +0200323 cpu_reset(CPU(cpu));
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100324}
325
Jan Kiszkafd533eb2013-06-22 08:06:58 +0200326static const MemoryRegionPortio prep_portio_list[] = {
327 /* System control ports */
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 /* Special port to get debug messages from Open-Firmware */
332 { 0x0F00, 4, 1, .write = PPC_debug_write, },
333 PORTIO_END_OF_LIST(),
334};
335
Kirill Batuzov848696b2014-04-29 17:38:39 +0400336static PortioList prep_port_list;
337
Hervé Poussineau31688242015-03-02 22:23:27 +0000338/*****************************************************************************/
339/* NVRAM helpers */
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 /* Set parameters for Open Hack'Ware BIOS */
445 NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
446 NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
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 /* XXX: put the cmdline in NVRAM too ? */
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
bellard26aa7d72004-04-28 22:26:05 +0000476/* PowerPC PREP hardware initialisation */
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300477static void ppc_prep_init(MachineState *machine)
bellarda541f292004-04-12 20:39:29 +0000478{
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300479 ram_addr_t ram_size = machine->ram_size;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300480 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;
Avi Kivity0c90c522011-09-25 16:57:45 +0300484 MemoryRegion *sysmem = get_system_memory();
Andreas Färbera9bf3df2012-05-04 17:45:09 +0200485 PowerPCCPU *cpu = NULL;
Andreas Färbere2684c02012-03-14 01:38:23 +0100486 CPUPPCState *env = NULL;
Hervé Poussineau31688242015-03-02 22:23:27 +0000487 Nvram *m48t59;
Avi Kivity0c90c522011-09-25 16:57:45 +0300488#if 0
489 MemoryRegion *xcsr = g_new(MemoryRegion, 1);
490#endif
Hervé Poussineaud0b25422013-11-05 00:09:45 +0100491 int linux_boot, i, nb_nics1;
Avi Kivity0c90c522011-09-25 16:57:45 +0300492 MemoryRegion *ram = g_new(MemoryRegion, 1);
Blue Swirl093209c2010-09-18 05:53:14 +0000493 uint32_t kernel_base, initrd_base;
494 long kernel_size, initrd_size;
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100495 DeviceState *dev;
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100496 PCIHostState *pcihost;
bellard46e50e92004-06-21 19:43:00 +0000497 PCIBus *pci_bus;
Andreas Färber506b7dd2012-01-09 02:04:05 +0100498 PCIDevice *pci;
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100499 ISABus *isa_bus;
Hervé Poussineau52a71bf2012-04-14 22:48:36 +0200500 ISADevice *isa;
j_mayer28c5af52007-11-11 01:50:45 +0000501 int ppc_boot_device;
Gerd Hoffmannf455e982009-08-28 15:47:03 +0200502 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
bellard64201202004-05-26 22:55:16 +0000503
Anthony Liguori7267c092011-08-20 22:09:37 -0500504 sysctrl = g_malloc0(sizeof(sysctrl_t));
bellarda541f292004-04-12 20:39:29 +0000505
506 linux_boot = (kernel_filename != NULL);
j_mayer0a032cb2007-04-16 08:56:52 +0000507
bellardc68ea702005-11-21 23:33:12 +0000508 /* init CPUs */
Bharata B Rao19fb2c32015-07-02 16:23:19 +1000509 if (machine->cpu_model == NULL)
510 machine->cpu_model = "602";
j_mayerfe33cc72007-10-03 01:06:57 +0000511 for (i = 0; i < smp_cpus; i++) {
Bharata B Rao19fb2c32015-07-02 16:23:19 +1000512 cpu = cpu_ppc_init(machine->cpu_model);
Andreas Färbera9bf3df2012-05-04 17:45:09 +0200513 if (cpu == NULL) {
bellardaaed9092007-11-10 15:15:54 +0000514 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
515 exit(1);
516 }
Andreas Färbera9bf3df2012-05-04 17:45:09 +0200517 env = &cpu->env;
518
j_mayer4018bae2007-11-19 01:48:12 +0000519 if (env->flags & POWERPC_FLAG_RTC_CLK) {
520 /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
521 cpu_ppc_tb_init(env, 7812500UL);
522 } else {
523 /* Set time-base frequency to 100 Mhz */
524 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
525 }
Andreas Färber5c3e7352012-05-04 17:46:13 +0200526 qemu_register_reset(ppc_prep_reset, cpu);
j_mayerfe33cc72007-10-03 01:06:57 +0000527 }
bellarda541f292004-04-12 20:39:29 +0000528
529 /* allocate RAM */
Shreyas B. Prabhue938ba02014-07-10 17:31:03 +0530530 memory_region_allocate_system_memory(ram, NULL, "ppc_prep.ram", ram_size);
Avi Kivity0c90c522011-09-25 16:57:45 +0300531 memory_region_add_subregion(sysmem, 0, ram);
blueswir1cf9c1472009-02-11 18:04:12 +0000532
bellarda541f292004-04-12 20:39:29 +0000533 if (linux_boot) {
bellard64201202004-05-26 22:55:16 +0000534 kernel_base = KERNEL_LOAD_ADDR;
bellarda541f292004-04-12 20:39:29 +0000535 /* now we can load the kernel */
pbrookdcac9672009-04-09 20:05:49 +0000536 kernel_size = load_image_targphys(kernel_filename, kernel_base,
537 ram_size - kernel_base);
bellard64201202004-05-26 22:55:16 +0000538 if (kernel_size < 0) {
Markus Armbrusterc5254362015-12-17 17:35:09 +0100539 error_report("could not load kernel '%s'", kernel_filename);
bellarda541f292004-04-12 20:39:29 +0000540 exit(1);
541 }
542 /* load initrd */
bellarda541f292004-04-12 20:39:29 +0000543 if (initrd_filename) {
bellard64201202004-05-26 22:55:16 +0000544 initrd_base = INITRD_LOAD_ADDR;
pbrookdcac9672009-04-09 20:05:49 +0000545 initrd_size = load_image_targphys(initrd_filename, initrd_base,
546 ram_size - initrd_base);
bellarda541f292004-04-12 20:39:29 +0000547 if (initrd_size < 0) {
Markus Armbrusterc5254362015-12-17 17:35:09 +0100548 error_report("could not load initial ram disk '%s'",
549 initrd_filename);
550 exit(1);
bellarda541f292004-04-12 20:39:29 +0000551 }
bellard64201202004-05-26 22:55:16 +0000552 } else {
553 initrd_base = 0;
554 initrd_size = 0;
bellarda541f292004-04-12 20:39:29 +0000555 }
balrog6ac0e822007-10-31 01:54:04 +0000556 ppc_boot_device = 'm';
bellarda541f292004-04-12 20:39:29 +0000557 } else {
bellard64201202004-05-26 22:55:16 +0000558 kernel_base = 0;
559 kernel_size = 0;
560 initrd_base = 0;
561 initrd_size = 0;
j_mayer28c5af52007-11-11 01:50:45 +0000562 ppc_boot_device = '\0';
563 /* For now, OHW cannot boot from the network. */
j_mayer0d913fd2007-11-11 14:44:28 +0000564 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];
j_mayer28c5af52007-11-11 01:50:45 +0000567 break;
j_mayer0d913fd2007-11-11 14:44:28 +0000568 }
j_mayer28c5af52007-11-11 01:50:45 +0000569 }
570 if (ppc_boot_device == '\0') {
571 fprintf(stderr, "No valid boot device for Mac99 machine\n");
572 exit(1);
573 }
bellarda541f292004-04-12 20:39:29 +0000574 }
575
j_mayerdd37a5e2007-04-16 07:41:07 +0000576 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
Markus Armbrusterc5254362015-12-17 17:35:09 +0100577 error_report("Only 6xx bus is supported on PREP machine");
578 exit(1);
j_mayerdd37a5e2007-04-16 07:41:07 +0000579 }
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100580
581 dev = qdev_create(NULL, "raven-pcihost");
Hervé Poussineaud0b25422013-11-05 00:09:45 +0100582 if (bios_name == NULL) {
583 bios_name = BIOS_FILENAME;
584 }
585 qdev_prop_set_string(dev, "bios-name", bios_name);
Peter Crosthwaite4ecd4d12015-05-10 23:29:10 -0700586 qdev_prop_set_uint32(dev, "elf-machine", PPC_ELF_MACHINE);
Andreas Färber8558d942012-08-20 19:08:08 +0200587 pcihost = PCI_HOST_BRIDGE(dev);
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200588 object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev), NULL);
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200589 qdev_init_nofail(dev);
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100590 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 }
Hervé Poussineau9a183912014-03-17 23:00:20 +0100595 sysctrl->contiguous_map_irq = qdev_get_gpio_in(dev, 0);
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100596
Andreas Färber506b7dd2012-01-09 02:04:05 +0100597 /* PCI -> ISA bridge */
598 pci = pci_create_simple(pci_bus, PCI_DEVFN(1, 0), "i82378");
Andreas Färber182735e2013-05-29 22:29:20 +0200599 cpu = POWERPC_CPU(first_cpu);
Andreas Färber506b7dd2012-01-09 02:04:05 +0100600 qdev_connect_gpio_out(&pci->qdev, 0,
Andreas Färber182735e2013-05-29 22:29:20 +0200601 cpu->env.irq_inputs[PPC6xx_INPUT_INT]);
Andreas Färber506b7dd2012-01-09 02:04:05 +0100602 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));
Andreas Färber2ae0e482013-06-07 14:11:07 +0200606 isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(pci), "isa.0"));
Andreas Färber506b7dd2012-01-09 02:04:05 +0100607
Hervé Poussineau52a71bf2012-04-14 22:48:36 +0200608 /* Super I/O (parallel + serial ports) */
609 isa = isa_create(isa_bus, TYPE_PC87312);
Andreas Färber4a17cc42013-06-07 13:49:13 +0200610 dev = DEVICE(isa);
611 qdev_prop_set_uint8(dev, "config", 13); /* fdc, ser0, ser1, par0 */
612 qdev_init_nofail(dev);
Hervé Poussineau52a71bf2012-04-14 22:48:36 +0200613
bellarda541f292004-04-12 20:39:29 +0000614 /* init basic PC hardware */
Gerd Hoffmann78895422010-10-15 11:45:13 +0200615 pci_vga_init(pci_bus);
bellarda541f292004-04-12 20:39:29 +0000616
bellarda541f292004-04-12 20:39:29 +0000617 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++) {
aurel325652ef72009-01-09 13:10:41 +0000621 if (nd_table[i].model == NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500622 nd_table[i].model = g_strdup("ne2k_isa");
aurel325652ef72009-01-09 13:10:41 +0000623 }
624 if (strcmp(nd_table[i].model, "ne2k_isa") == 0) {
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100625 isa_ne2000_init(isa_bus, ne2000_io[i], ne2000_irq[i],
626 &nd_table[i]);
pbrooka41b2ff2006-02-05 04:14:41 +0000627 } else {
David Gibson29b358f2013-06-06 18:48:51 +1000628 pci_nic_init_nofail(&nd_table[i], pci_bus, "ne2k_pci", NULL);
pbrooka41b2ff2006-02-05 04:14:41 +0000629 }
bellarda541f292004-04-12 20:39:29 +0000630 }
bellarda541f292004-04-12 20:39:29 +0000631
John Snowd8f94e12014-10-01 14:19:27 -0400632 ide_drive_get(hd, ARRAY_SIZE(hd));
Aurelien Jarno81aa0642011-02-21 15:53:05 +0100633 for(i = 0; i < MAX_IDE_BUS; i++) {
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100634 isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
thse4bcb142007-12-02 04:51:10 +0000635 hd[2 * i],
636 hd[2 * i + 1]);
bellarda541f292004-04-12 20:39:29 +0000637 }
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100638 isa_create_simple(isa_bus, "i8042");
Blue Swirl4556bd82010-05-22 08:00:52 +0000639
Andreas Färber182735e2013-05-29 22:29:20 +0200640 cpu = POWERPC_CPU(first_cpu);
641 sysctrl->reset_irq = cpu->env.irq_inputs[PPC6xx_INPUT_HRESET];
Jan Kiszkafd533eb2013-06-22 08:06:58 +0200642
Kirill Batuzov848696b2014-04-29 17:38:39 +0400643 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);
Jan Kiszkafd533eb2013-06-22 08:06:58 +0200645
bellard64201202004-05-26 22:55:16 +0000646 /* PowerPC control and status register group */
bellardb6b8bd12004-06-21 16:55:53 +0000647#if 0
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400648 memory_region_init_io(xcsr, NULL, &PPC_XCSR_ops, NULL, "ppc-xcsr", 0x1000);
Avi Kivity0c90c522011-09-25 16:57:45 +0300649 memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
bellardb6b8bd12004-06-21 16:55:53 +0000650#endif
bellarda541f292004-04-12 20:39:29 +0000651
Marcel Apfelbaumde77a242015-01-06 15:29:14 +0200652 if (usb_enabled()) {
Gerd Hoffmannafb9a602012-03-07 15:06:32 +0100653 pci_create_simple(pci_bus, -1, "pci-ohci");
pbrook0d92ed32006-05-21 16:30:15 +0000654 }
655
Mark Cave-Ayland6de04972015-03-02 22:23:27 +0000656 m48t59 = m48t59_init_isa(isa_bus, 0x0074, NVRAM_SIZE, 2000, 59);
j_mayer3cbee152007-10-28 23:42:18 +0000657 if (m48t59 == NULL)
bellard64201202004-05-26 22:55:16 +0000658 return;
j_mayer3cbee152007-10-28 23:42:18 +0000659 sysctrl->nvram = m48t59;
bellard64201202004-05-26 22:55:16 +0000660
661 /* Initialise NVRAM */
Hervé Poussineau31688242015-03-02 22:23:27 +0000662 PPC_NVRAM_set_params(m48t59, NVRAM_SIZE, "PREP", ram_size,
663 ppc_boot_device,
bellard64201202004-05-26 22:55:16 +0000664 kernel_base, kernel_size,
bellardb6b8bd12004-06-21 16:55:53 +0000665 kernel_cmdline,
bellard64201202004-05-26 22:55:16 +0000666 initrd_base, initrd_size,
667 /* XXX: need an option to load a NVRAM image */
bellardb6b8bd12004-06-21 16:55:53 +0000668 0,
669 graphic_width, graphic_height, graphic_depth);
bellarda541f292004-04-12 20:39:29 +0000670}
bellardc0e564d2005-06-05 15:17:28 +0000671
Eduardo Habkoste264d292015-09-04 15:37:08 -0300672static void prep_machine_init(MachineClass *mc)
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500673{
Eduardo Habkoste264d292015-09-04 15:37:08 -0300674 mc->desc = "PowerPC PREP platform";
675 mc->init = ppc_prep_init;
676 mc->max_cpus = MAX_CPUS;
677 mc->default_boot_order = "cad";
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500678}
679
Eduardo Habkoste264d292015-09-04 15:37:08 -0300680DEFINE_MACHINE("prep", prep_machine_init)