target/mips: Remove XBurst Media eXtension Unit dead code
[qemu/ar7.git] / hw / nubus / nubus-device.c
blobffe78a8823146ddbb15dd800e6804b69f416ca95
1 /*
2 * QEMU Macintosh Nubus
4 * Copyright (c) 2013-2018 Laurent Vivier <laurent@vivier.eu>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "hw/nubus/nubus.h"
13 #include "qapi/error.h"
16 /* The Format Block Structure */
18 #define FBLOCK_DIRECTORY_OFFSET 0
19 #define FBLOCK_LENGTH 4
20 #define FBLOCK_CRC 8
21 #define FBLOCK_REVISION_LEVEL 12
22 #define FBLOCK_FORMAT 13
23 #define FBLOCK_TEST_PATTERN 14
24 #define FBLOCK_RESERVED 18
25 #define FBLOCK_BYTE_LANES 19
27 #define FBLOCK_SIZE 20
28 #define FBLOCK_PATTERN_VAL 0x5a932bc7
30 static uint64_t nubus_fblock_read(void *opaque, hwaddr addr, unsigned int size)
32 NubusDevice *dev = opaque;
33 uint64_t val;
35 #define BYTE(v, b) (((v) >> (24 - 8 * (b))) & 0xff)
36 switch (addr) {
37 case FBLOCK_BYTE_LANES:
38 val = dev->byte_lanes;
39 val |= (val ^ 0xf) << 4;
40 break;
41 case FBLOCK_RESERVED:
42 val = 0x00;
43 break;
44 case FBLOCK_TEST_PATTERN...FBLOCK_TEST_PATTERN + 3:
45 val = BYTE(FBLOCK_PATTERN_VAL, addr - FBLOCK_TEST_PATTERN);
46 break;
47 case FBLOCK_FORMAT:
48 val = dev->rom_format;
49 break;
50 case FBLOCK_REVISION_LEVEL:
51 val = dev->rom_rev;
52 break;
53 case FBLOCK_CRC...FBLOCK_CRC + 3:
54 val = BYTE(dev->rom_crc, addr - FBLOCK_CRC);
55 break;
56 case FBLOCK_LENGTH...FBLOCK_LENGTH + 3:
57 val = BYTE(dev->rom_length, addr - FBLOCK_LENGTH);
58 break;
59 case FBLOCK_DIRECTORY_OFFSET...FBLOCK_DIRECTORY_OFFSET + 3:
60 val = BYTE(dev->directory_offset, addr - FBLOCK_DIRECTORY_OFFSET);
61 break;
62 default:
63 val = 0;
64 break;
66 return val;
69 static void nubus_fblock_write(void *opaque, hwaddr addr, uint64_t val,
70 unsigned int size)
72 /* read only */
75 static const MemoryRegionOps nubus_format_block_ops = {
76 .read = nubus_fblock_read,
77 .write = nubus_fblock_write,
78 .endianness = DEVICE_BIG_ENDIAN,
79 .valid = {
80 .min_access_size = 1,
81 .max_access_size = 1,
85 static void nubus_register_format_block(NubusDevice *dev)
87 char *fblock_name;
89 fblock_name = g_strdup_printf("nubus-slot-%d-format-block",
90 dev->slot_nb);
92 hwaddr fblock_offset = memory_region_size(&dev->slot_mem) - FBLOCK_SIZE;
93 memory_region_init_io(&dev->fblock_io, NULL, &nubus_format_block_ops,
94 dev, fblock_name, FBLOCK_SIZE);
95 memory_region_add_subregion(&dev->slot_mem, fblock_offset,
96 &dev->fblock_io);
98 g_free(fblock_name);
101 static void mac_nubus_rom_write(void *opaque, hwaddr addr, uint64_t val,
102 unsigned int size)
104 /* read only */
107 static uint64_t mac_nubus_rom_read(void *opaque, hwaddr addr,
108 unsigned int size)
110 NubusDevice *dev = opaque;
112 return dev->rom[addr];
115 static const MemoryRegionOps mac_nubus_rom_ops = {
116 .read = mac_nubus_rom_read,
117 .write = mac_nubus_rom_write,
118 .endianness = DEVICE_BIG_ENDIAN,
119 .valid = {
120 .min_access_size = 1,
121 .max_access_size = 1,
126 void nubus_register_rom(NubusDevice *dev, const uint8_t *rom, uint32_t size,
127 int revision, int format, uint8_t byte_lanes)
129 hwaddr rom_offset;
130 char *rom_name;
132 /* FIXME : really compute CRC */
133 dev->rom_length = 0;
134 dev->rom_crc = 0;
136 dev->rom_rev = revision;
137 dev->rom_format = format;
139 dev->byte_lanes = byte_lanes;
140 dev->directory_offset = -size;
142 /* ROM */
144 dev->rom = rom;
145 rom_name = g_strdup_printf("nubus-slot-%d-rom", dev->slot_nb);
146 memory_region_init_io(&dev->rom_io, NULL, &mac_nubus_rom_ops,
147 dev, rom_name, size);
148 memory_region_set_readonly(&dev->rom_io, true);
150 rom_offset = memory_region_size(&dev->slot_mem) - FBLOCK_SIZE +
151 dev->directory_offset;
152 memory_region_add_subregion(&dev->slot_mem, rom_offset, &dev->rom_io);
154 g_free(rom_name);
157 static void nubus_device_realize(DeviceState *dev, Error **errp)
159 NubusBus *nubus = NUBUS_BUS(qdev_get_parent_bus(dev));
160 NubusDevice *nd = NUBUS_DEVICE(dev);
161 char *name;
162 hwaddr slot_offset;
164 if (nubus->current_slot < NUBUS_FIRST_SLOT ||
165 nubus->current_slot > NUBUS_LAST_SLOT) {
166 error_setg(errp, "Cannot register nubus card, not enough slots");
167 return;
170 nd->slot_nb = nubus->current_slot++;
171 name = g_strdup_printf("nubus-slot-%d", nd->slot_nb);
173 if (nd->slot_nb < NUBUS_FIRST_SLOT) {
174 /* Super */
175 slot_offset = (nd->slot_nb - 6) * NUBUS_SUPER_SLOT_SIZE;
177 memory_region_init(&nd->slot_mem, OBJECT(dev), name,
178 NUBUS_SUPER_SLOT_SIZE);
179 memory_region_add_subregion(&nubus->super_slot_io, slot_offset,
180 &nd->slot_mem);
181 } else {
182 /* Normal */
183 slot_offset = nd->slot_nb * NUBUS_SLOT_SIZE;
185 memory_region_init(&nd->slot_mem, OBJECT(dev), name, NUBUS_SLOT_SIZE);
186 memory_region_add_subregion(&nubus->slot_io, slot_offset,
187 &nd->slot_mem);
190 g_free(name);
191 nubus_register_format_block(nd);
194 static void nubus_device_class_init(ObjectClass *oc, void *data)
196 DeviceClass *dc = DEVICE_CLASS(oc);
198 dc->realize = nubus_device_realize;
199 dc->bus_type = TYPE_NUBUS_BUS;
202 static const TypeInfo nubus_device_type_info = {
203 .name = TYPE_NUBUS_DEVICE,
204 .parent = TYPE_DEVICE,
205 .abstract = true,
206 .instance_size = sizeof(NubusDevice),
207 .class_init = nubus_device_class_init,
210 static void nubus_register_types(void)
212 type_register_static(&nubus_device_type_info);
215 type_init(nubus_register_types)