blob: 45b5f62d6e25d6d776f643c94ba22b281e984f68 [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 */
Andreas Färber75610152013-01-26 20:41:58 +010024#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010025#include "hw/timer/m48t59.h"
26#include "hw/i386/pc.h"
27#include "hw/char/serial.h"
28#include "hw/block/fdc.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020029#include "net/net.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "hw/isa/isa.h"
Andreas Färber75610152013-01-26 20:41:58 +010032#include "hw/pci/pci.h"
33#include "hw/pci/pci_host.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010034#include "hw/ppc/ppc.h"
Andreas Färber75610152013-01-26 20:41:58 +010035#include "hw/boards.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/log.h"
Andreas Färber75610152013-01-26 20:41:58 +010037#include "hw/ide.h"
38#include "hw/loader.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010039#include "hw/timer/mc146818rtc.h"
40#include "hw/isa/pc87312.h"
Markus Armbruster4be74632014-10-07 13:59:18 +020041#include "sysemu/block-backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010042#include "sysemu/arch_init.h"
Andreas Färber97c42c32013-04-27 21:23:23 +020043#include "sysemu/qtest.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010044#include "exec/address-spaces.h"
Andreas Färber97c42c32013-04-27 21:23:23 +020045#include "elf.h"
bellard9fddaa02004-05-21 12:59:32 +000046
bellarda541f292004-04-12 20:39:29 +000047//#define HARD_DEBUG_PPC_IO
48//#define DEBUG_PPC_IO
49
j_mayerfe33cc72007-10-03 01:06:57 +000050/* SMP is not enabled, for now */
51#define MAX_CPUS 1
52
thse4bcb142007-12-02 04:51:10 +000053#define MAX_IDE_BUS 2
54
Paul Brookbba831e2009-05-19 14:52:42 +010055#define BIOS_SIZE (1024 * 1024)
bellardb6b8bd12004-06-21 16:55:53 +000056#define BIOS_FILENAME "ppc_rom.bin"
57#define KERNEL_LOAD_ADDR 0x01000000
58#define INITRD_LOAD_ADDR 0x01800000
bellard64201202004-05-26 22:55:16 +000059
bellarda541f292004-04-12 20:39:29 +000060#if defined (HARD_DEBUG_PPC_IO) && !defined (DEBUG_PPC_IO)
61#define DEBUG_PPC_IO
62#endif
63
64#if defined (HARD_DEBUG_PPC_IO)
Blue Swirl001faf32009-05-13 17:53:17 +000065#define PPC_IO_DPRINTF(fmt, ...) \
bellarda541f292004-04-12 20:39:29 +000066do { \
aliguori8fec2b82009-01-15 22:36:53 +000067 if (qemu_loglevel_mask(CPU_LOG_IOPORT)) { \
Blue Swirl001faf32009-05-13 17:53:17 +000068 qemu_log("%s: " fmt, __func__ , ## __VA_ARGS__); \
bellarda541f292004-04-12 20:39:29 +000069 } else { \
Blue Swirl001faf32009-05-13 17:53:17 +000070 printf("%s : " fmt, __func__ , ## __VA_ARGS__); \
bellarda541f292004-04-12 20:39:29 +000071 } \
72} while (0)
73#elif defined (DEBUG_PPC_IO)
Blue Swirl0bf9e312009-07-20 17:19:25 +000074#define PPC_IO_DPRINTF(fmt, ...) \
75qemu_log_mask(CPU_LOG_IOPORT, fmt, ## __VA_ARGS__)
bellarda541f292004-04-12 20:39:29 +000076#else
Blue Swirl001faf32009-05-13 17:53:17 +000077#define PPC_IO_DPRINTF(fmt, ...) do { } while (0)
bellarda541f292004-04-12 20:39:29 +000078#endif
79
bellard64201202004-05-26 22:55:16 +000080/* Constants for devices init */
bellarda541f292004-04-12 20:39:29 +000081static const int ide_iobase[2] = { 0x1f0, 0x170 };
82static const int ide_iobase2[2] = { 0x3f6, 0x376 };
83static const int ide_irq[2] = { 13, 13 };
84
85#define NE2000_NB_MAX 6
86
87static uint32_t ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
88static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
89
bellard64201202004-05-26 22:55:16 +000090/* ISA IO ports bridge */
bellarda541f292004-04-12 20:39:29 +000091#define PPC_IO_BASE 0x80000000
92
bellard64201202004-05-26 22:55:16 +000093/* PowerPC control and status registers */
94#if 0 // Not used
95static struct {
96 /* IDs */
97 uint32_t veni_devi;
98 uint32_t revi;
99 /* Control and status */
100 uint32_t gcsr;
101 uint32_t xcfr;
102 uint32_t ct32;
103 uint32_t mcsr;
104 /* General purpose registers */
105 uint32_t gprg[6];
106 /* Exceptions */
107 uint32_t feen;
108 uint32_t fest;
109 uint32_t fema;
110 uint32_t fecl;
111 uint32_t eeen;
112 uint32_t eest;
113 uint32_t eecl;
114 uint32_t eeint;
115 uint32_t eemck0;
116 uint32_t eemck1;
117 /* Error diagnostic */
118} XCSR;
bellarda541f292004-04-12 20:39:29 +0000119
j_mayer36081602007-09-17 08:21:54 +0000120static void PPC_XCSR_writeb (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200121 hwaddr addr, uint32_t value)
bellard64201202004-05-26 22:55:16 +0000122{
Blue Swirl90e189e2009-08-16 11:13:18 +0000123 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
124 value);
bellard64201202004-05-26 22:55:16 +0000125}
126
j_mayer36081602007-09-17 08:21:54 +0000127static void PPC_XCSR_writew (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200128 hwaddr addr, uint32_t value)
bellard64201202004-05-26 22:55:16 +0000129{
Blue Swirl90e189e2009-08-16 11:13:18 +0000130 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
131 value);
bellard64201202004-05-26 22:55:16 +0000132}
133
j_mayer36081602007-09-17 08:21:54 +0000134static void PPC_XCSR_writel (void *opaque,
Avi Kivitya8170e52012-10-23 12:30:10 +0200135 hwaddr addr, uint32_t value)
bellard64201202004-05-26 22:55:16 +0000136{
Blue Swirl90e189e2009-08-16 11:13:18 +0000137 printf("%s: 0x" TARGET_FMT_plx " => 0x%08" PRIx32 "\n", __func__, addr,
138 value);
bellard64201202004-05-26 22:55:16 +0000139}
140
Avi Kivitya8170e52012-10-23 12:30:10 +0200141static uint32_t PPC_XCSR_readb (void *opaque, hwaddr addr)
bellard64201202004-05-26 22:55:16 +0000142{
143 uint32_t retval = 0;
144
Blue Swirl90e189e2009-08-16 11:13:18 +0000145 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
146 retval);
bellard64201202004-05-26 22:55:16 +0000147
148 return retval;
149}
150
Avi Kivitya8170e52012-10-23 12:30:10 +0200151static uint32_t PPC_XCSR_readw (void *opaque, hwaddr addr)
bellard64201202004-05-26 22:55:16 +0000152{
153 uint32_t retval = 0;
154
Blue Swirl90e189e2009-08-16 11:13:18 +0000155 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
156 retval);
bellard64201202004-05-26 22:55:16 +0000157
158 return retval;
159}
160
Avi Kivitya8170e52012-10-23 12:30:10 +0200161static uint32_t PPC_XCSR_readl (void *opaque, hwaddr addr)
bellard64201202004-05-26 22:55:16 +0000162{
163 uint32_t retval = 0;
164
Blue Swirl90e189e2009-08-16 11:13:18 +0000165 printf("%s: 0x" TARGET_FMT_plx " <= %08" PRIx32 "\n", __func__, addr,
166 retval);
bellard64201202004-05-26 22:55:16 +0000167
168 return retval;
169}
170
Avi Kivity0c90c522011-09-25 16:57:45 +0300171static const MemoryRegionOps PPC_XCSR_ops = {
172 .old_mmio = {
173 .read = { PPC_XCSR_readb, PPC_XCSR_readw, PPC_XCSR_readl, },
174 .write = { PPC_XCSR_writeb, PPC_XCSR_writew, PPC_XCSR_writel, },
175 },
176 .endianness = DEVICE_LITTLE_ENDIAN,
bellard64201202004-05-26 22:55:16 +0000177};
178
bellardb6b8bd12004-06-21 16:55:53 +0000179#endif
bellard64201202004-05-26 22:55:16 +0000180
bellarda541f292004-04-12 20:39:29 +0000181/* Fake super-io ports for PREP platform (Intel 82378ZB) */
Anthony Liguoric227f092009-10-01 16:12:16 -0500182typedef struct sysctrl_t {
j_mayerc4781a52007-10-29 10:21:12 +0000183 qemu_irq reset_irq;
Hervé Poussineau31688242015-03-02 22:23:27 +0000184 Nvram *nvram;
bellard64201202004-05-26 22:55:16 +0000185 uint8_t state;
186 uint8_t syscontrol;
bellardda9b2662005-04-23 18:18:54 +0000187 int contiguous_map;
Hervé Poussineau9a183912014-03-17 23:00:20 +0100188 qemu_irq contiguous_map_irq;
bellardfb3444b2005-07-03 13:57:11 +0000189 int endian;
Anthony Liguoric227f092009-10-01 16:12:16 -0500190} sysctrl_t;
bellard64201202004-05-26 22:55:16 +0000191
192enum {
193 STATE_HARDFILE = 0x01,
194};
195
Anthony Liguoric227f092009-10-01 16:12:16 -0500196static sysctrl_t *sysctrl;
bellarda541f292004-04-12 20:39:29 +0000197
bellarda541f292004-04-12 20:39:29 +0000198static void PREP_io_800_writeb (void *opaque, uint32_t addr, uint32_t val)
199{
Anthony Liguoric227f092009-10-01 16:12:16 -0500200 sysctrl_t *sysctrl = opaque;
bellard64201202004-05-26 22:55:16 +0000201
j_mayeraae93662007-11-24 02:56:36 +0000202 PPC_IO_DPRINTF("0x%08" PRIx32 " => 0x%02" PRIx32 "\n",
203 addr - PPC_IO_BASE, val);
bellarda541f292004-04-12 20:39:29 +0000204 switch (addr) {
205 case 0x0092:
206 /* Special port 92 */
207 /* Check soft reset asked */
bellard64201202004-05-26 22:55:16 +0000208 if (val & 0x01) {
j_mayerc4781a52007-10-29 10:21:12 +0000209 qemu_irq_raise(sysctrl->reset_irq);
210 } else {
211 qemu_irq_lower(sysctrl->reset_irq);
bellarda541f292004-04-12 20:39:29 +0000212 }
213 /* Check LE mode */
bellard64201202004-05-26 22:55:16 +0000214 if (val & 0x02) {
bellardfb3444b2005-07-03 13:57:11 +0000215 sysctrl->endian = 1;
216 } else {
217 sysctrl->endian = 0;
bellarda541f292004-04-12 20:39:29 +0000218 }
219 break;
bellard64201202004-05-26 22:55:16 +0000220 case 0x0800:
221 /* Motorola CPU configuration register : read-only */
222 break;
223 case 0x0802:
224 /* Motorola base module feature register : read-only */
225 break;
226 case 0x0803:
227 /* Motorola base module status register : read-only */
228 break;
bellarda541f292004-04-12 20:39:29 +0000229 case 0x0808:
bellard64201202004-05-26 22:55:16 +0000230 /* Hardfile light register */
231 if (val & 1)
232 sysctrl->state |= STATE_HARDFILE;
233 else
234 sysctrl->state &= ~STATE_HARDFILE;
bellarda541f292004-04-12 20:39:29 +0000235 break;
236 case 0x0810:
237 /* Password protect 1 register */
Hervé Poussineau31688242015-03-02 22:23:27 +0000238 if (sysctrl->nvram != NULL) {
239 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
240 (k->toggle_lock)(sysctrl->nvram, 1);
241 }
bellarda541f292004-04-12 20:39:29 +0000242 break;
243 case 0x0812:
244 /* Password protect 2 register */
Hervé Poussineau31688242015-03-02 22:23:27 +0000245 if (sysctrl->nvram != NULL) {
246 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
247 (k->toggle_lock)(sysctrl->nvram, 2);
248 }
bellarda541f292004-04-12 20:39:29 +0000249 break;
250 case 0x0814:
bellard64201202004-05-26 22:55:16 +0000251 /* L2 invalidate register */
bellardc68ea702005-11-21 23:33:12 +0000252 // tlb_flush(first_cpu, 1);
bellarda541f292004-04-12 20:39:29 +0000253 break;
254 case 0x081C:
255 /* system control register */
bellard64201202004-05-26 22:55:16 +0000256 sysctrl->syscontrol = val & 0x0F;
bellarda541f292004-04-12 20:39:29 +0000257 break;
258 case 0x0850:
259 /* I/O map type register */
bellardda9b2662005-04-23 18:18:54 +0000260 sysctrl->contiguous_map = val & 0x01;
Hervé Poussineau9a183912014-03-17 23:00:20 +0100261 qemu_set_irq(sysctrl->contiguous_map_irq, sysctrl->contiguous_map);
bellarda541f292004-04-12 20:39:29 +0000262 break;
263 default:
j_mayeraae93662007-11-24 02:56:36 +0000264 printf("ERROR: unaffected IO port write: %04" PRIx32
265 " => %02" PRIx32"\n", addr, val);
bellarda541f292004-04-12 20:39:29 +0000266 break;
267 }
268}
269
270static uint32_t PREP_io_800_readb (void *opaque, uint32_t addr)
271{
Anthony Liguoric227f092009-10-01 16:12:16 -0500272 sysctrl_t *sysctrl = opaque;
bellarda541f292004-04-12 20:39:29 +0000273 uint32_t retval = 0xFF;
274
275 switch (addr) {
276 case 0x0092:
277 /* Special port 92 */
Julio Guerrab6f54b32013-05-05 23:29:48 +0200278 retval = sysctrl->endian << 1;
bellard64201202004-05-26 22:55:16 +0000279 break;
280 case 0x0800:
281 /* Motorola CPU configuration register */
282 retval = 0xEF; /* MPC750 */
283 break;
284 case 0x0802:
285 /* Motorola Base module feature register */
286 retval = 0xAD; /* No ESCC, PMC slot neither ethernet */
287 break;
288 case 0x0803:
289 /* Motorola base module status register */
290 retval = 0xE0; /* Standard MPC750 */
bellarda541f292004-04-12 20:39:29 +0000291 break;
292 case 0x080C:
293 /* Equipment present register:
294 * no L2 cache
295 * no upgrade processor
296 * no cards in PCI slots
297 * SCSI fuse is bad
298 */
bellard64201202004-05-26 22:55:16 +0000299 retval = 0x3C;
300 break;
301 case 0x0810:
302 /* Motorola base module extended feature register */
303 retval = 0x39; /* No USB, CF and PCI bridge. NVRAM present */
bellarda541f292004-04-12 20:39:29 +0000304 break;
bellardda9b2662005-04-23 18:18:54 +0000305 case 0x0814:
306 /* L2 invalidate: don't care */
307 break;
bellarda541f292004-04-12 20:39:29 +0000308 case 0x0818:
309 /* Keylock */
310 retval = 0x00;
311 break;
312 case 0x081C:
313 /* system control register
314 * 7 - 6 / 1 - 0: L2 cache enable
315 */
bellard64201202004-05-26 22:55:16 +0000316 retval = sysctrl->syscontrol;
bellarda541f292004-04-12 20:39:29 +0000317 break;
318 case 0x0823:
319 /* */
320 retval = 0x03; /* no L2 cache */
321 break;
322 case 0x0850:
323 /* I/O map type register */
bellardda9b2662005-04-23 18:18:54 +0000324 retval = sysctrl->contiguous_map;
bellarda541f292004-04-12 20:39:29 +0000325 break;
326 default:
j_mayeraae93662007-11-24 02:56:36 +0000327 printf("ERROR: unaffected IO port: %04" PRIx32 " read\n", addr);
bellarda541f292004-04-12 20:39:29 +0000328 break;
329 }
j_mayeraae93662007-11-24 02:56:36 +0000330 PPC_IO_DPRINTF("0x%08" PRIx32 " <= 0x%02" PRIx32 "\n",
331 addr - PPC_IO_BASE, retval);
bellarda541f292004-04-12 20:39:29 +0000332
333 return retval;
334}
335
bellardda9b2662005-04-23 18:18:54 +0000336
bellard64201202004-05-26 22:55:16 +0000337#define NVRAM_SIZE 0x2000
bellarda541f292004-04-12 20:39:29 +0000338
Blue Swirl4556bd82010-05-22 08:00:52 +0000339static void cpu_request_exit(void *opaque, int irq, int level)
340{
Andreas Färber4917cf42013-05-27 05:17:50 +0200341 CPUState *cpu = current_cpu;
Blue Swirl4556bd82010-05-22 08:00:52 +0000342
Andreas Färber4917cf42013-05-27 05:17:50 +0200343 if (cpu && level) {
344 cpu_exit(cpu);
Blue Swirl4556bd82010-05-22 08:00:52 +0000345 }
346}
347
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100348static void ppc_prep_reset(void *opaque)
349{
Andreas Färber5c3e7352012-05-04 17:46:13 +0200350 PowerPCCPU *cpu = opaque;
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100351
Andreas Färber5c3e7352012-05-04 17:46:13 +0200352 cpu_reset(CPU(cpu));
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100353}
354
Jan Kiszkafd533eb2013-06-22 08:06:58 +0200355static const MemoryRegionPortio prep_portio_list[] = {
356 /* System control ports */
357 { 0x0092, 1, 1, .read = PREP_io_800_readb, .write = PREP_io_800_writeb, },
358 { 0x0800, 0x52, 1,
359 .read = PREP_io_800_readb, .write = PREP_io_800_writeb, },
360 /* Special port to get debug messages from Open-Firmware */
361 { 0x0F00, 4, 1, .write = PPC_debug_write, },
362 PORTIO_END_OF_LIST(),
363};
364
Kirill Batuzov848696b2014-04-29 17:38:39 +0400365static PortioList prep_port_list;
366
Hervé Poussineau31688242015-03-02 22:23:27 +0000367/*****************************************************************************/
368/* NVRAM helpers */
369static inline uint32_t nvram_read(Nvram *nvram, uint32_t addr)
370{
371 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
372 return (k->read)(nvram, addr);
373}
374
375static inline void nvram_write(Nvram *nvram, uint32_t addr, uint32_t val)
376{
377 NvramClass *k = NVRAM_GET_CLASS(sysctrl->nvram);
378 (k->write)(nvram, addr, val);
379}
380
381static void NVRAM_set_byte(Nvram *nvram, uint32_t addr, uint8_t value)
382{
383 nvram_write(nvram, addr, value);
384}
385
386static uint8_t NVRAM_get_byte(Nvram *nvram, uint32_t addr)
387{
388 return nvram_read(nvram, addr);
389}
390
391static void NVRAM_set_word(Nvram *nvram, uint32_t addr, uint16_t value)
392{
393 nvram_write(nvram, addr, value >> 8);
394 nvram_write(nvram, addr + 1, value & 0xFF);
395}
396
397static uint16_t NVRAM_get_word(Nvram *nvram, uint32_t addr)
398{
399 uint16_t tmp;
400
401 tmp = nvram_read(nvram, addr) << 8;
402 tmp |= nvram_read(nvram, addr + 1);
403
404 return tmp;
405}
406
407static void NVRAM_set_lword(Nvram *nvram, uint32_t addr, uint32_t value)
408{
409 nvram_write(nvram, addr, value >> 24);
410 nvram_write(nvram, addr + 1, (value >> 16) & 0xFF);
411 nvram_write(nvram, addr + 2, (value >> 8) & 0xFF);
412 nvram_write(nvram, addr + 3, value & 0xFF);
413}
414
415static void NVRAM_set_string(Nvram *nvram, uint32_t addr, const char *str,
416 uint32_t max)
417{
418 int i;
419
420 for (i = 0; i < max && str[i] != '\0'; i++) {
421 nvram_write(nvram, addr + i, str[i]);
422 }
423 nvram_write(nvram, addr + i, str[i]);
424 nvram_write(nvram, addr + max - 1, '\0');
425}
426
427static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value)
428{
429 uint16_t tmp;
430 uint16_t pd, pd1, pd2;
431
432 tmp = prev >> 8;
433 pd = prev ^ value;
434 pd1 = pd & 0x000F;
435 pd2 = ((pd >> 4) & 0x000F) ^ pd1;
436 tmp ^= (pd1 << 3) | (pd1 << 8);
437 tmp ^= pd2 | (pd2 << 7) | (pd2 << 12);
438
439 return tmp;
440}
441
442static uint16_t NVRAM_compute_crc (Nvram *nvram, uint32_t start, uint32_t count)
443{
444 uint32_t i;
445 uint16_t crc = 0xFFFF;
446 int odd;
447
448 odd = count & 1;
449 count &= ~1;
450 for (i = 0; i != count; i++) {
451 crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i));
452 }
453 if (odd) {
454 crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8);
455 }
456
457 return crc;
458}
459
460#define CMDLINE_ADDR 0x017ff000
461
462static int PPC_NVRAM_set_params (Nvram *nvram, uint16_t NVRAM_size,
463 const char *arch,
464 uint32_t RAM_size, int boot_device,
465 uint32_t kernel_image, uint32_t kernel_size,
466 const char *cmdline,
467 uint32_t initrd_image, uint32_t initrd_size,
468 uint32_t NVRAM_image,
469 int width, int height, int depth)
470{
471 uint16_t crc;
472
473 /* Set parameters for Open Hack'Ware BIOS */
474 NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16);
475 NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */
476 NVRAM_set_word(nvram, 0x14, NVRAM_size);
477 NVRAM_set_string(nvram, 0x20, arch, 16);
478 NVRAM_set_lword(nvram, 0x30, RAM_size);
479 NVRAM_set_byte(nvram, 0x34, boot_device);
480 NVRAM_set_lword(nvram, 0x38, kernel_image);
481 NVRAM_set_lword(nvram, 0x3C, kernel_size);
482 if (cmdline) {
483 /* XXX: put the cmdline in NVRAM too ? */
484 pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR,
485 cmdline);
486 NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR);
487 NVRAM_set_lword(nvram, 0x44, strlen(cmdline));
488 } else {
489 NVRAM_set_lword(nvram, 0x40, 0);
490 NVRAM_set_lword(nvram, 0x44, 0);
491 }
492 NVRAM_set_lword(nvram, 0x48, initrd_image);
493 NVRAM_set_lword(nvram, 0x4C, initrd_size);
494 NVRAM_set_lword(nvram, 0x50, NVRAM_image);
495
496 NVRAM_set_word(nvram, 0x54, width);
497 NVRAM_set_word(nvram, 0x56, height);
498 NVRAM_set_word(nvram, 0x58, depth);
499 crc = NVRAM_compute_crc(nvram, 0x00, 0xF8);
500 NVRAM_set_word(nvram, 0xFC, crc);
501
502 return 0;
503}
504
bellard26aa7d72004-04-28 22:26:05 +0000505/* PowerPC PREP hardware initialisation */
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300506static void ppc_prep_init(MachineState *machine)
bellarda541f292004-04-12 20:39:29 +0000507{
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300508 ram_addr_t ram_size = machine->ram_size;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300509 const char *kernel_filename = machine->kernel_filename;
510 const char *kernel_cmdline = machine->kernel_cmdline;
511 const char *initrd_filename = machine->initrd_filename;
512 const char *boot_device = machine->boot_order;
Avi Kivity0c90c522011-09-25 16:57:45 +0300513 MemoryRegion *sysmem = get_system_memory();
Andreas Färbera9bf3df2012-05-04 17:45:09 +0200514 PowerPCCPU *cpu = NULL;
Andreas Färbere2684c02012-03-14 01:38:23 +0100515 CPUPPCState *env = NULL;
Hervé Poussineau31688242015-03-02 22:23:27 +0000516 Nvram *m48t59;
Avi Kivity0c90c522011-09-25 16:57:45 +0300517#if 0
518 MemoryRegion *xcsr = g_new(MemoryRegion, 1);
519#endif
Hervé Poussineaud0b25422013-11-05 00:09:45 +0100520 int linux_boot, i, nb_nics1;
Avi Kivity0c90c522011-09-25 16:57:45 +0300521 MemoryRegion *ram = g_new(MemoryRegion, 1);
Blue Swirl093209c2010-09-18 05:53:14 +0000522 uint32_t kernel_base, initrd_base;
523 long kernel_size, initrd_size;
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100524 DeviceState *dev;
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100525 PCIHostState *pcihost;
bellard46e50e92004-06-21 19:43:00 +0000526 PCIBus *pci_bus;
Andreas Färber506b7dd2012-01-09 02:04:05 +0100527 PCIDevice *pci;
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100528 ISABus *isa_bus;
Hervé Poussineau52a71bf2012-04-14 22:48:36 +0200529 ISADevice *isa;
j_mayer28c5af52007-11-11 01:50:45 +0000530 int ppc_boot_device;
Gerd Hoffmannf455e982009-08-28 15:47:03 +0200531 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
bellard64201202004-05-26 22:55:16 +0000532
Anthony Liguori7267c092011-08-20 22:09:37 -0500533 sysctrl = g_malloc0(sizeof(sysctrl_t));
bellarda541f292004-04-12 20:39:29 +0000534
535 linux_boot = (kernel_filename != NULL);
j_mayer0a032cb2007-04-16 08:56:52 +0000536
bellardc68ea702005-11-21 23:33:12 +0000537 /* init CPUs */
Bharata B Rao19fb2c32015-07-02 16:23:19 +1000538 if (machine->cpu_model == NULL)
539 machine->cpu_model = "602";
j_mayerfe33cc72007-10-03 01:06:57 +0000540 for (i = 0; i < smp_cpus; i++) {
Bharata B Rao19fb2c32015-07-02 16:23:19 +1000541 cpu = cpu_ppc_init(machine->cpu_model);
Andreas Färbera9bf3df2012-05-04 17:45:09 +0200542 if (cpu == NULL) {
bellardaaed9092007-11-10 15:15:54 +0000543 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
544 exit(1);
545 }
Andreas Färbera9bf3df2012-05-04 17:45:09 +0200546 env = &cpu->env;
547
j_mayer4018bae2007-11-19 01:48:12 +0000548 if (env->flags & POWERPC_FLAG_RTC_CLK) {
549 /* POWER / PowerPC 601 RTC clock frequency is 7.8125 MHz */
550 cpu_ppc_tb_init(env, 7812500UL);
551 } else {
552 /* Set time-base frequency to 100 Mhz */
553 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
554 }
Andreas Färber5c3e7352012-05-04 17:46:13 +0200555 qemu_register_reset(ppc_prep_reset, cpu);
j_mayerfe33cc72007-10-03 01:06:57 +0000556 }
bellarda541f292004-04-12 20:39:29 +0000557
558 /* allocate RAM */
Shreyas B. Prabhue938ba02014-07-10 17:31:03 +0530559 memory_region_allocate_system_memory(ram, NULL, "ppc_prep.ram", ram_size);
Avi Kivity0c90c522011-09-25 16:57:45 +0300560 memory_region_add_subregion(sysmem, 0, ram);
blueswir1cf9c1472009-02-11 18:04:12 +0000561
bellarda541f292004-04-12 20:39:29 +0000562 if (linux_boot) {
bellard64201202004-05-26 22:55:16 +0000563 kernel_base = KERNEL_LOAD_ADDR;
bellarda541f292004-04-12 20:39:29 +0000564 /* now we can load the kernel */
pbrookdcac9672009-04-09 20:05:49 +0000565 kernel_size = load_image_targphys(kernel_filename, kernel_base,
566 ram_size - kernel_base);
bellard64201202004-05-26 22:55:16 +0000567 if (kernel_size < 0) {
Paul Brook2ac71172009-05-08 02:35:15 +0100568 hw_error("qemu: could not load kernel '%s'\n", kernel_filename);
bellarda541f292004-04-12 20:39:29 +0000569 exit(1);
570 }
571 /* load initrd */
bellarda541f292004-04-12 20:39:29 +0000572 if (initrd_filename) {
bellard64201202004-05-26 22:55:16 +0000573 initrd_base = INITRD_LOAD_ADDR;
pbrookdcac9672009-04-09 20:05:49 +0000574 initrd_size = load_image_targphys(initrd_filename, initrd_base,
575 ram_size - initrd_base);
bellarda541f292004-04-12 20:39:29 +0000576 if (initrd_size < 0) {
Paul Brook2ac71172009-05-08 02:35:15 +0100577 hw_error("qemu: could not load initial ram disk '%s'\n",
j_mayer4a057712007-04-19 08:42:21 +0000578 initrd_filename);
bellarda541f292004-04-12 20:39:29 +0000579 }
bellard64201202004-05-26 22:55:16 +0000580 } else {
581 initrd_base = 0;
582 initrd_size = 0;
bellarda541f292004-04-12 20:39:29 +0000583 }
balrog6ac0e822007-10-31 01:54:04 +0000584 ppc_boot_device = 'm';
bellarda541f292004-04-12 20:39:29 +0000585 } else {
bellard64201202004-05-26 22:55:16 +0000586 kernel_base = 0;
587 kernel_size = 0;
588 initrd_base = 0;
589 initrd_size = 0;
j_mayer28c5af52007-11-11 01:50:45 +0000590 ppc_boot_device = '\0';
591 /* For now, OHW cannot boot from the network. */
j_mayer0d913fd2007-11-11 14:44:28 +0000592 for (i = 0; boot_device[i] != '\0'; i++) {
593 if (boot_device[i] >= 'a' && boot_device[i] <= 'f') {
594 ppc_boot_device = boot_device[i];
j_mayer28c5af52007-11-11 01:50:45 +0000595 break;
j_mayer0d913fd2007-11-11 14:44:28 +0000596 }
j_mayer28c5af52007-11-11 01:50:45 +0000597 }
598 if (ppc_boot_device == '\0') {
599 fprintf(stderr, "No valid boot device for Mac99 machine\n");
600 exit(1);
601 }
bellarda541f292004-04-12 20:39:29 +0000602 }
603
j_mayerdd37a5e2007-04-16 07:41:07 +0000604 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
Paul Brook2ac71172009-05-08 02:35:15 +0100605 hw_error("Only 6xx bus is supported on PREP machine\n");
j_mayerdd37a5e2007-04-16 07:41:07 +0000606 }
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100607
608 dev = qdev_create(NULL, "raven-pcihost");
Hervé Poussineaud0b25422013-11-05 00:09:45 +0100609 if (bios_name == NULL) {
610 bios_name = BIOS_FILENAME;
611 }
612 qdev_prop_set_string(dev, "bios-name", bios_name);
613 qdev_prop_set_uint32(dev, "elf-machine", ELF_MACHINE);
Andreas Färber8558d942012-08-20 19:08:08 +0200614 pcihost = PCI_HOST_BRIDGE(dev);
Paolo Bonzinif05f6b42012-03-28 16:34:12 +0200615 object_property_add_child(qdev_get_machine(), "raven", OBJECT(dev), NULL);
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200616 qdev_init_nofail(dev);
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100617 pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
618 if (pci_bus == NULL) {
619 fprintf(stderr, "Couldn't create PCI host controller.\n");
620 exit(1);
621 }
Hervé Poussineau9a183912014-03-17 23:00:20 +0100622 sysctrl->contiguous_map_irq = qdev_get_gpio_in(dev, 0);
Andreas Färber8ca8c7b2012-01-03 02:42:46 +0100623
Andreas Färber506b7dd2012-01-09 02:04:05 +0100624 /* PCI -> ISA bridge */
625 pci = pci_create_simple(pci_bus, PCI_DEVFN(1, 0), "i82378");
Andreas Färber182735e2013-05-29 22:29:20 +0200626 cpu = POWERPC_CPU(first_cpu);
Andreas Färber506b7dd2012-01-09 02:04:05 +0100627 qdev_connect_gpio_out(&pci->qdev, 0,
Andreas Färber182735e2013-05-29 22:29:20 +0200628 cpu->env.irq_inputs[PPC6xx_INPUT_INT]);
Shannon Zhaoaaaee0b2015-05-29 13:27:05 +0800629 qdev_connect_gpio_out(&pci->qdev, 1,
630 qemu_allocate_irq(cpu_request_exit, NULL, 0));
Andreas Färber506b7dd2012-01-09 02:04:05 +0100631 sysbus_connect_irq(&pcihost->busdev, 0, qdev_get_gpio_in(&pci->qdev, 9));
632 sysbus_connect_irq(&pcihost->busdev, 1, qdev_get_gpio_in(&pci->qdev, 11));
633 sysbus_connect_irq(&pcihost->busdev, 2, qdev_get_gpio_in(&pci->qdev, 9));
634 sysbus_connect_irq(&pcihost->busdev, 3, qdev_get_gpio_in(&pci->qdev, 11));
Andreas Färber2ae0e482013-06-07 14:11:07 +0200635 isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(pci), "isa.0"));
Andreas Färber506b7dd2012-01-09 02:04:05 +0100636
Hervé Poussineau52a71bf2012-04-14 22:48:36 +0200637 /* Super I/O (parallel + serial ports) */
638 isa = isa_create(isa_bus, TYPE_PC87312);
Andreas Färber4a17cc42013-06-07 13:49:13 +0200639 dev = DEVICE(isa);
640 qdev_prop_set_uint8(dev, "config", 13); /* fdc, ser0, ser1, par0 */
641 qdev_init_nofail(dev);
Hervé Poussineau52a71bf2012-04-14 22:48:36 +0200642
bellarda541f292004-04-12 20:39:29 +0000643 /* init basic PC hardware */
Gerd Hoffmann78895422010-10-15 11:45:13 +0200644 pci_vga_init(pci_bus);
bellarda541f292004-04-12 20:39:29 +0000645
bellarda541f292004-04-12 20:39:29 +0000646 nb_nics1 = nb_nics;
647 if (nb_nics1 > NE2000_NB_MAX)
648 nb_nics1 = NE2000_NB_MAX;
649 for(i = 0; i < nb_nics1; i++) {
aurel325652ef72009-01-09 13:10:41 +0000650 if (nd_table[i].model == NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500651 nd_table[i].model = g_strdup("ne2k_isa");
aurel325652ef72009-01-09 13:10:41 +0000652 }
653 if (strcmp(nd_table[i].model, "ne2k_isa") == 0) {
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100654 isa_ne2000_init(isa_bus, ne2000_io[i], ne2000_irq[i],
655 &nd_table[i]);
pbrooka41b2ff2006-02-05 04:14:41 +0000656 } else {
David Gibson29b358f2013-06-06 18:48:51 +1000657 pci_nic_init_nofail(&nd_table[i], pci_bus, "ne2k_pci", NULL);
pbrooka41b2ff2006-02-05 04:14:41 +0000658 }
bellarda541f292004-04-12 20:39:29 +0000659 }
bellarda541f292004-04-12 20:39:29 +0000660
John Snowd8f94e12014-10-01 14:19:27 -0400661 ide_drive_get(hd, ARRAY_SIZE(hd));
Aurelien Jarno81aa0642011-02-21 15:53:05 +0100662 for(i = 0; i < MAX_IDE_BUS; i++) {
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100663 isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
thse4bcb142007-12-02 04:51:10 +0000664 hd[2 * i],
665 hd[2 * i + 1]);
bellarda541f292004-04-12 20:39:29 +0000666 }
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100667 isa_create_simple(isa_bus, "i8042");
Blue Swirl4556bd82010-05-22 08:00:52 +0000668
Andreas Färber182735e2013-05-29 22:29:20 +0200669 cpu = POWERPC_CPU(first_cpu);
670 sysctrl->reset_irq = cpu->env.irq_inputs[PPC6xx_INPUT_HRESET];
Jan Kiszkafd533eb2013-06-22 08:06:58 +0200671
Kirill Batuzov848696b2014-04-29 17:38:39 +0400672 portio_list_init(&prep_port_list, NULL, prep_portio_list, sysctrl, "prep");
673 portio_list_add(&prep_port_list, isa_address_space_io(isa), 0x0);
Jan Kiszkafd533eb2013-06-22 08:06:58 +0200674
bellard64201202004-05-26 22:55:16 +0000675 /* PowerPC control and status register group */
bellardb6b8bd12004-06-21 16:55:53 +0000676#if 0
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400677 memory_region_init_io(xcsr, NULL, &PPC_XCSR_ops, NULL, "ppc-xcsr", 0x1000);
Avi Kivity0c90c522011-09-25 16:57:45 +0300678 memory_region_add_subregion(sysmem, 0xFEFF0000, xcsr);
bellardb6b8bd12004-06-21 16:55:53 +0000679#endif
bellarda541f292004-04-12 20:39:29 +0000680
Marcel Apfelbaumde77a242015-01-06 15:29:14 +0200681 if (usb_enabled()) {
Gerd Hoffmannafb9a602012-03-07 15:06:32 +0100682 pci_create_simple(pci_bus, -1, "pci-ohci");
pbrook0d92ed32006-05-21 16:30:15 +0000683 }
684
Mark Cave-Ayland6de04972015-03-02 22:23:27 +0000685 m48t59 = m48t59_init_isa(isa_bus, 0x0074, NVRAM_SIZE, 2000, 59);
j_mayer3cbee152007-10-28 23:42:18 +0000686 if (m48t59 == NULL)
bellard64201202004-05-26 22:55:16 +0000687 return;
j_mayer3cbee152007-10-28 23:42:18 +0000688 sysctrl->nvram = m48t59;
bellard64201202004-05-26 22:55:16 +0000689
690 /* Initialise NVRAM */
Hervé Poussineau31688242015-03-02 22:23:27 +0000691 PPC_NVRAM_set_params(m48t59, NVRAM_SIZE, "PREP", ram_size,
692 ppc_boot_device,
bellard64201202004-05-26 22:55:16 +0000693 kernel_base, kernel_size,
bellardb6b8bd12004-06-21 16:55:53 +0000694 kernel_cmdline,
bellard64201202004-05-26 22:55:16 +0000695 initrd_base, initrd_size,
696 /* XXX: need an option to load a NVRAM image */
bellardb6b8bd12004-06-21 16:55:53 +0000697 0,
698 graphic_width, graphic_height, graphic_depth);
bellarda541f292004-04-12 20:39:29 +0000699}
bellardc0e564d2005-06-05 15:17:28 +0000700
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500701static QEMUMachine prep_machine = {
aliguori4b32e162008-10-07 20:34:35 +0000702 .name = "prep",
703 .desc = "PowerPC PREP platform",
704 .init = ppc_prep_init,
balrog3d878ca2008-10-28 10:59:59 +0000705 .max_cpus = MAX_CPUS,
Markus Armbrusterc1654732013-08-16 13:13:50 +0200706 .default_boot_order = "cad",
bellardc0e564d2005-06-05 15:17:28 +0000707};
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500708
709static void prep_machine_init(void)
710{
711 qemu_register_machine(&prep_machine);
712}
713
714machine_init(prep_machine_init);