xref: /qemu/hw/ppc/ppc405_boards.c (revision 5964ed56)
1 /*
2  * QEMU PowerPC 405 evaluation boards emulation
3  *
4  * Copyright (c) 2007 Jocelyn Mayer
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 
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qapi/error.h"
28 #include "qemu-common.h"
29 #include "cpu.h"
30 #include "hw/ppc/ppc.h"
31 #include "ppc405.h"
32 #include "hw/rtc/m48t59.h"
33 #include "hw/block/flash.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/qtest.h"
36 #include "sysemu/reset.h"
37 #include "sysemu/block-backend.h"
38 #include "hw/boards.h"
39 #include "qemu/log.h"
40 #include "qemu/error-report.h"
41 #include "hw/loader.h"
42 #include "exec/address-spaces.h"
43 #include "qemu/cutils.h"
44 
45 #define BIOS_FILENAME "ppc405_rom.bin"
46 #define BIOS_SIZE (2 * MiB)
47 
48 #define KERNEL_LOAD_ADDR 0x00000000
49 #define INITRD_LOAD_ADDR 0x01800000
50 
51 #define USE_FLASH_BIOS
52 
53 /*****************************************************************************/
54 /* PPC405EP reference board (IBM) */
55 /* Standalone board with:
56  * - PowerPC 405EP CPU
57  * - SDRAM (0x00000000)
58  * - Flash (0xFFF80000)
59  * - SRAM  (0xFFF00000)
60  * - NVRAM (0xF0000000)
61  * - FPGA  (0xF0300000)
62  */
63 typedef struct ref405ep_fpga_t ref405ep_fpga_t;
64 struct ref405ep_fpga_t {
65     uint8_t reg0;
66     uint8_t reg1;
67 };
68 
69 static uint64_t ref405ep_fpga_readb(void *opaque, hwaddr addr, unsigned size)
70 {
71     ref405ep_fpga_t *fpga;
72     uint32_t ret;
73 
74     fpga = opaque;
75     switch (addr) {
76     case 0x0:
77         ret = fpga->reg0;
78         break;
79     case 0x1:
80         ret = fpga->reg1;
81         break;
82     default:
83         ret = 0;
84         break;
85     }
86 
87     return ret;
88 }
89 
90 static void ref405ep_fpga_writeb(void *opaque, hwaddr addr, uint64_t value,
91                                  unsigned size)
92 {
93     ref405ep_fpga_t *fpga;
94 
95     fpga = opaque;
96     switch (addr) {
97     case 0x0:
98         /* Read only */
99         break;
100     case 0x1:
101         fpga->reg1 = value;
102         break;
103     default:
104         break;
105     }
106 }
107 
108 static const MemoryRegionOps ref405ep_fpga_ops = {
109     .read = ref405ep_fpga_readb,
110     .write = ref405ep_fpga_writeb,
111     .impl.min_access_size = 1,
112     .impl.max_access_size = 1,
113     .valid.min_access_size = 1,
114     .valid.max_access_size = 4,
115     .endianness = DEVICE_BIG_ENDIAN,
116 };
117 
118 static void ref405ep_fpga_reset (void *opaque)
119 {
120     ref405ep_fpga_t *fpga;
121 
122     fpga = opaque;
123     fpga->reg0 = 0x00;
124     fpga->reg1 = 0x0F;
125 }
126 
127 static void ref405ep_fpga_init(MemoryRegion *sysmem, uint32_t base)
128 {
129     ref405ep_fpga_t *fpga;
130     MemoryRegion *fpga_memory = g_new(MemoryRegion, 1);
131 
132     fpga = g_malloc0(sizeof(ref405ep_fpga_t));
133     memory_region_init_io(fpga_memory, NULL, &ref405ep_fpga_ops, fpga,
134                           "fpga", 0x00000100);
135     memory_region_add_subregion(sysmem, base, fpga_memory);
136     qemu_register_reset(&ref405ep_fpga_reset, fpga);
137 }
138 
139 static void ref405ep_init(MachineState *machine)
140 {
141     MachineClass *mc = MACHINE_GET_CLASS(machine);
142     const char *kernel_filename = machine->kernel_filename;
143     const char *kernel_cmdline = machine->kernel_cmdline;
144     const char *initrd_filename = machine->initrd_filename;
145     char *filename;
146     ppc4xx_bd_info_t bd;
147     CPUPPCState *env;
148     qemu_irq *pic;
149     MemoryRegion *bios;
150     MemoryRegion *sram = g_new(MemoryRegion, 1);
151     ram_addr_t bdloc;
152     MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
153     hwaddr ram_bases[2], ram_sizes[2];
154     target_ulong sram_size;
155     long bios_size;
156     //int phy_addr = 0;
157     //static int phy_addr = 1;
158     target_ulong kernel_base, initrd_base;
159     long kernel_size, initrd_size;
160     int linux_boot;
161     int len;
162     DriveInfo *dinfo;
163     MemoryRegion *sysmem = get_system_memory();
164 
165     if (machine->ram_size != mc->default_ram_size) {
166         char *sz = size_to_str(mc->default_ram_size);
167         error_report("Invalid RAM size, should be %s", sz);
168         g_free(sz);
169         exit(EXIT_FAILURE);
170     }
171 
172     /* XXX: fix this */
173     memory_region_init_alias(&ram_memories[0], NULL, "ef405ep.ram.alias",
174                              machine->ram, 0, machine->ram_size);
175     ram_bases[0] = 0;
176     ram_sizes[0] = machine->ram_size;
177     memory_region_init(&ram_memories[1], NULL, "ef405ep.ram1", 0);
178     ram_bases[1] = 0x00000000;
179     ram_sizes[1] = 0x00000000;
180     env = ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
181                         33333333, &pic, kernel_filename == NULL ? 0 : 1);
182     /* allocate SRAM */
183     sram_size = 512 * KiB;
184     memory_region_init_ram(sram, NULL, "ef405ep.sram", sram_size,
185                            &error_fatal);
186     memory_region_add_subregion(sysmem, 0xFFF00000, sram);
187     /* allocate and load BIOS */
188 #ifdef USE_FLASH_BIOS
189     dinfo = drive_get(IF_PFLASH, 0, 0);
190     if (dinfo) {
191         bios_size = 8 * MiB;
192         pflash_cfi02_register((uint32_t)(-bios_size),
193                               "ef405ep.bios", bios_size,
194                               dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
195                               64 * KiB, 1,
196                               2, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
197                               1);
198     } else
199 #endif
200     {
201         bios = g_new(MemoryRegion, 1);
202         memory_region_init_ram(bios, NULL, "ef405ep.bios", BIOS_SIZE,
203                                &error_fatal);
204 
205         if (bios_name == NULL)
206             bios_name = BIOS_FILENAME;
207         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
208         if (filename) {
209             bios_size = load_image_size(filename,
210                                         memory_region_get_ram_ptr(bios),
211                                         BIOS_SIZE);
212             g_free(filename);
213             if (bios_size < 0) {
214                 error_report("Could not load PowerPC BIOS '%s'", bios_name);
215                 exit(1);
216             }
217             bios_size = (bios_size + 0xfff) & ~0xfff;
218             memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
219         } else if (!qtest_enabled() || kernel_filename != NULL) {
220             error_report("Could not load PowerPC BIOS '%s'", bios_name);
221             exit(1);
222         } else {
223             /* Avoid an uninitialized variable warning */
224             bios_size = -1;
225         }
226         memory_region_set_readonly(bios, true);
227     }
228     /* Register FPGA */
229     ref405ep_fpga_init(sysmem, 0xF0300000);
230     /* Register NVRAM */
231     m48t59_init(NULL, 0xF0000000, 0, 8192, 1968, 8);
232     /* Load kernel */
233     linux_boot = (kernel_filename != NULL);
234     if (linux_boot) {
235         memset(&bd, 0, sizeof(bd));
236         bd.bi_memstart = 0x00000000;
237         bd.bi_memsize = machine->ram_size;
238         bd.bi_flashstart = -bios_size;
239         bd.bi_flashsize = -bios_size;
240         bd.bi_flashoffset = 0;
241         bd.bi_sramstart = 0xFFF00000;
242         bd.bi_sramsize = sram_size;
243         bd.bi_bootflags = 0;
244         bd.bi_intfreq = 133333333;
245         bd.bi_busfreq = 33333333;
246         bd.bi_baudrate = 115200;
247         bd.bi_s_version[0] = 'Q';
248         bd.bi_s_version[1] = 'M';
249         bd.bi_s_version[2] = 'U';
250         bd.bi_s_version[3] = '\0';
251         bd.bi_r_version[0] = 'Q';
252         bd.bi_r_version[1] = 'E';
253         bd.bi_r_version[2] = 'M';
254         bd.bi_r_version[3] = 'U';
255         bd.bi_r_version[4] = '\0';
256         bd.bi_procfreq = 133333333;
257         bd.bi_plb_busfreq = 33333333;
258         bd.bi_pci_busfreq = 33333333;
259         bd.bi_opbfreq = 33333333;
260         bdloc = ppc405_set_bootinfo(env, &bd, 0x00000001);
261         env->gpr[3] = bdloc;
262         kernel_base = KERNEL_LOAD_ADDR;
263         /* now we can load the kernel */
264         kernel_size = load_image_targphys(kernel_filename, kernel_base,
265                                           machine->ram_size - kernel_base);
266         if (kernel_size < 0) {
267             error_report("could not load kernel '%s'", kernel_filename);
268             exit(1);
269         }
270         printf("Load kernel size %ld at " TARGET_FMT_lx,
271                kernel_size, kernel_base);
272         /* load initrd */
273         if (initrd_filename) {
274             initrd_base = INITRD_LOAD_ADDR;
275             initrd_size = load_image_targphys(initrd_filename, initrd_base,
276                                               machine->ram_size - initrd_base);
277             if (initrd_size < 0) {
278                 error_report("could not load initial ram disk '%s'",
279                              initrd_filename);
280                 exit(1);
281             }
282         } else {
283             initrd_base = 0;
284             initrd_size = 0;
285         }
286         env->gpr[4] = initrd_base;
287         env->gpr[5] = initrd_size;
288         if (kernel_cmdline != NULL) {
289             len = strlen(kernel_cmdline);
290             bdloc -= ((len + 255) & ~255);
291             cpu_physical_memory_write(bdloc, kernel_cmdline, len + 1);
292             env->gpr[6] = bdloc;
293             env->gpr[7] = bdloc + len;
294         } else {
295             env->gpr[6] = 0;
296             env->gpr[7] = 0;
297         }
298         env->nip = KERNEL_LOAD_ADDR;
299     } else {
300         kernel_base = 0;
301         kernel_size = 0;
302         initrd_base = 0;
303         initrd_size = 0;
304         bdloc = 0;
305     }
306 }
307 
308 static void ref405ep_class_init(ObjectClass *oc, void *data)
309 {
310     MachineClass *mc = MACHINE_CLASS(oc);
311 
312     mc->desc = "ref405ep";
313     mc->init = ref405ep_init;
314     mc->default_ram_size = 0x08000000;
315     mc->default_ram_id = "ef405ep.ram";
316 }
317 
318 static const TypeInfo ref405ep_type = {
319     .name = MACHINE_TYPE_NAME("ref405ep"),
320     .parent = TYPE_MACHINE,
321     .class_init = ref405ep_class_init,
322 };
323 
324 /*****************************************************************************/
325 /* AMCC Taihu evaluation board */
326 /* - PowerPC 405EP processor
327  * - SDRAM               128 MB at 0x00000000
328  * - Boot flash          2 MB   at 0xFFE00000
329  * - Application flash   32 MB  at 0xFC000000
330  * - 2 serial ports
331  * - 2 ethernet PHY
332  * - 1 USB 1.1 device    0x50000000
333  * - 1 LCD display       0x50100000
334  * - 1 CPLD              0x50100000
335  * - 1 I2C EEPROM
336  * - 1 I2C thermal sensor
337  * - a set of LEDs
338  * - bit-bang SPI port using GPIOs
339  * - 1 EBC interface connector 0 0x50200000
340  * - 1 cardbus controller + expansion slot.
341  * - 1 PCI expansion slot.
342  */
343 typedef struct taihu_cpld_t taihu_cpld_t;
344 struct taihu_cpld_t {
345     uint8_t reg0;
346     uint8_t reg1;
347 };
348 
349 static uint64_t taihu_cpld_read(void *opaque, hwaddr addr, unsigned size)
350 {
351     taihu_cpld_t *cpld;
352     uint32_t ret;
353 
354     cpld = opaque;
355     switch (addr) {
356     case 0x0:
357         ret = cpld->reg0;
358         break;
359     case 0x1:
360         ret = cpld->reg1;
361         break;
362     default:
363         ret = 0;
364         break;
365     }
366 
367     return ret;
368 }
369 
370 static void taihu_cpld_write(void *opaque, hwaddr addr,
371                              uint64_t value, unsigned size)
372 {
373     taihu_cpld_t *cpld;
374 
375     cpld = opaque;
376     switch (addr) {
377     case 0x0:
378         /* Read only */
379         break;
380     case 0x1:
381         cpld->reg1 = value;
382         break;
383     default:
384         break;
385     }
386 }
387 
388 static const MemoryRegionOps taihu_cpld_ops = {
389     .read = taihu_cpld_read,
390     .write = taihu_cpld_write,
391     .impl = {
392         .min_access_size = 1,
393         .max_access_size = 1,
394     },
395     .endianness = DEVICE_NATIVE_ENDIAN,
396 };
397 
398 static void taihu_cpld_reset (void *opaque)
399 {
400     taihu_cpld_t *cpld;
401 
402     cpld = opaque;
403     cpld->reg0 = 0x01;
404     cpld->reg1 = 0x80;
405 }
406 
407 static void taihu_cpld_init(MemoryRegion *sysmem, uint32_t base)
408 {
409     taihu_cpld_t *cpld;
410     MemoryRegion *cpld_memory = g_new(MemoryRegion, 1);
411 
412     cpld = g_malloc0(sizeof(taihu_cpld_t));
413     memory_region_init_io(cpld_memory, NULL, &taihu_cpld_ops, cpld, "cpld", 0x100);
414     memory_region_add_subregion(sysmem, base, cpld_memory);
415     qemu_register_reset(&taihu_cpld_reset, cpld);
416 }
417 
418 static void taihu_405ep_init(MachineState *machine)
419 {
420     MachineClass *mc = MACHINE_GET_CLASS(machine);
421     const char *kernel_filename = machine->kernel_filename;
422     const char *initrd_filename = machine->initrd_filename;
423     char *filename;
424     qemu_irq *pic;
425     MemoryRegion *sysmem = get_system_memory();
426     MemoryRegion *bios;
427     MemoryRegion *ram_memories = g_new(MemoryRegion, 2);
428     hwaddr ram_bases[2], ram_sizes[2];
429     long bios_size;
430     target_ulong kernel_base, initrd_base;
431     long kernel_size, initrd_size;
432     int linux_boot;
433     int fl_idx;
434     DriveInfo *dinfo;
435 
436     if (machine->ram_size != mc->default_ram_size) {
437         char *sz = size_to_str(mc->default_ram_size);
438         error_report("Invalid RAM size, should be %s", sz);
439         g_free(sz);
440         exit(EXIT_FAILURE);
441     }
442 
443     ram_bases[0] = 0;
444     ram_sizes[0] = 0x04000000;
445     memory_region_init_alias(&ram_memories[0], NULL,
446                              "taihu_405ep.ram-0", machine->ram, ram_bases[0],
447                              ram_sizes[0]);
448     ram_bases[1] = 0x04000000;
449     ram_sizes[1] = 0x04000000;
450     memory_region_init_alias(&ram_memories[1], NULL,
451                              "taihu_405ep.ram-1", machine->ram, ram_bases[1],
452                              ram_sizes[1]);
453     ppc405ep_init(sysmem, ram_memories, ram_bases, ram_sizes,
454                   33333333, &pic, kernel_filename == NULL ? 0 : 1);
455     /* allocate and load BIOS */
456     fl_idx = 0;
457 #if defined(USE_FLASH_BIOS)
458     dinfo = drive_get(IF_PFLASH, 0, fl_idx);
459     if (dinfo) {
460         bios_size = 2 * MiB;
461         pflash_cfi02_register(0xFFE00000,
462                               "taihu_405ep.bios", bios_size,
463                               dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
464                               64 * KiB, 1,
465                               4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
466                               1);
467         fl_idx++;
468     } else
469 #endif
470     {
471         if (bios_name == NULL)
472             bios_name = BIOS_FILENAME;
473         bios = g_new(MemoryRegion, 1);
474         memory_region_init_ram(bios, NULL, "taihu_405ep.bios", BIOS_SIZE,
475                                &error_fatal);
476         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
477         if (filename) {
478             bios_size = load_image_size(filename,
479                                         memory_region_get_ram_ptr(bios),
480                                         BIOS_SIZE);
481             g_free(filename);
482             if (bios_size < 0) {
483                 error_report("Could not load PowerPC BIOS '%s'", bios_name);
484                 exit(1);
485             }
486             bios_size = (bios_size + 0xfff) & ~0xfff;
487             memory_region_add_subregion(sysmem, (uint32_t)(-bios_size), bios);
488         } else if (!qtest_enabled()) {
489             error_report("Could not load PowerPC BIOS '%s'", bios_name);
490             exit(1);
491         }
492         memory_region_set_readonly(bios, true);
493     }
494     /* Register Linux flash */
495     dinfo = drive_get(IF_PFLASH, 0, fl_idx);
496     if (dinfo) {
497         bios_size = 32 * MiB;
498         pflash_cfi02_register(0xfc000000, "taihu_405ep.flash", bios_size,
499                               dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
500                               64 * KiB, 1,
501                               4, 0x0001, 0x22DA, 0x0000, 0x0000, 0x555, 0x2AA,
502                               1);
503         fl_idx++;
504     }
505     /* Register CLPD & LCD display */
506     taihu_cpld_init(sysmem, 0x50100000);
507     /* Load kernel */
508     linux_boot = (kernel_filename != NULL);
509     if (linux_boot) {
510         kernel_base = KERNEL_LOAD_ADDR;
511         /* now we can load the kernel */
512         kernel_size = load_image_targphys(kernel_filename, kernel_base,
513                                           machine->ram_size - kernel_base);
514         if (kernel_size < 0) {
515             error_report("could not load kernel '%s'", kernel_filename);
516             exit(1);
517         }
518         /* load initrd */
519         if (initrd_filename) {
520             initrd_base = INITRD_LOAD_ADDR;
521             initrd_size = load_image_targphys(initrd_filename, initrd_base,
522                                               machine->ram_size - initrd_base);
523             if (initrd_size < 0) {
524                 error_report("could not load initial ram disk '%s'",
525                              initrd_filename);
526                 exit(1);
527             }
528         } else {
529             initrd_base = 0;
530             initrd_size = 0;
531         }
532     } else {
533         kernel_base = 0;
534         kernel_size = 0;
535         initrd_base = 0;
536         initrd_size = 0;
537     }
538 }
539 
540 static void taihu_class_init(ObjectClass *oc, void *data)
541 {
542     MachineClass *mc = MACHINE_CLASS(oc);
543 
544     mc->desc = "taihu";
545     mc->init = taihu_405ep_init;
546     mc->default_ram_size = 0x08000000;
547     mc->default_ram_id = "taihu_405ep.ram";
548 }
549 
550 static const TypeInfo taihu_type = {
551     .name = MACHINE_TYPE_NAME("taihu"),
552     .parent = TYPE_MACHINE,
553     .class_init = taihu_class_init,
554 };
555 
556 static void ppc405_machine_init(void)
557 {
558     type_register_static(&ref405ep_type);
559     type_register_static(&taihu_type);
560 }
561 
562 type_init(ppc405_machine_init)
563