OSDN Git Service

516b13d2d53d11d746c1087c00d20d8afa270d6b
[qmiga/qemu.git] / hw / nubus / nubus-device.c
1 /*
2  * QEMU Macintosh Nubus
3  *
4  * Copyright (c) 2013-2018 Laurent Vivier <laurent@vivier.eu>
5  *
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.
8  *
9  */
10
11 #include "qemu/osdep.h"
12 #include "hw/nubus/nubus.h"
13 #include "qapi/error.h"
14
15
16 /* The Format Block Structure */
17
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
26
27 #define FBLOCK_SIZE             20
28 #define FBLOCK_PATTERN_VAL      0x5a932bc7
29
30 static uint64_t nubus_fblock_read(void *opaque, hwaddr addr, unsigned int size)
31 {
32     NubusDevice *dev = opaque;
33     uint64_t val;
34
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;
65     }
66     return val;
67 }
68
69 static void nubus_fblock_write(void *opaque, hwaddr addr, uint64_t val,
70                                unsigned int size)
71 {
72     /* read only */
73 }
74
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,
82     }
83 };
84
85 static void nubus_register_format_block(NubusDevice *dev)
86 {
87     char *fblock_name;
88
89     fblock_name = g_strdup_printf("nubus-slot-%d-format-block",
90                                   dev->slot);
91
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);
97
98     g_free(fblock_name);
99 }
100
101 static void mac_nubus_rom_write(void *opaque, hwaddr addr, uint64_t val,
102                                        unsigned int size)
103 {
104     /* read only */
105 }
106
107 static uint64_t mac_nubus_rom_read(void *opaque, hwaddr addr,
108                                     unsigned int size)
109 {
110     NubusDevice *dev = opaque;
111
112     return dev->rom[addr];
113 }
114
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,
122     },
123 };
124
125
126 void nubus_register_rom(NubusDevice *dev, const uint8_t *rom, uint32_t size,
127                         int revision, int format, uint8_t byte_lanes)
128 {
129     hwaddr rom_offset;
130     char *rom_name;
131
132     /* FIXME : really compute CRC */
133     dev->rom_length = 0;
134     dev->rom_crc = 0;
135
136     dev->rom_rev = revision;
137     dev->rom_format = format;
138
139     dev->byte_lanes = byte_lanes;
140     dev->directory_offset = -size;
141
142     /* ROM */
143
144     dev->rom = rom;
145     rom_name = g_strdup_printf("nubus-slot-%d-rom", dev->slot);
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);
149
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);
153
154     g_free(rom_name);
155 }
156
157 static void nubus_device_realize(DeviceState *dev, Error **errp)
158 {
159     NubusBus *nubus = NUBUS_BUS(qdev_get_parent_bus(dev));
160     NubusDevice *nd = NUBUS_DEVICE(dev);
161     char *name;
162     hwaddr slot_offset;
163
164     /* Super */
165     slot_offset = nd->slot * NUBUS_SUPER_SLOT_SIZE;
166
167     name = g_strdup_printf("nubus-super-slot-%x", nd->slot);
168     memory_region_init(&nd->super_slot_mem, OBJECT(dev), name,
169                        NUBUS_SUPER_SLOT_SIZE);
170     memory_region_add_subregion(&nubus->super_slot_io, slot_offset,
171                                 &nd->super_slot_mem);
172     g_free(name);
173
174     /* Normal */
175     slot_offset = nd->slot * NUBUS_SLOT_SIZE;
176
177     name = g_strdup_printf("nubus-slot-%x", nd->slot);
178     memory_region_init(&nd->slot_mem, OBJECT(dev), name, NUBUS_SLOT_SIZE);
179     memory_region_add_subregion(&nubus->slot_io, slot_offset,
180                                 &nd->slot_mem);
181     g_free(name);
182
183     nubus_register_format_block(nd);
184 }
185
186 static Property nubus_device_properties[] = {
187     DEFINE_PROP_INT32("slot", NubusDevice, slot, -1),
188     DEFINE_PROP_END_OF_LIST()
189 };
190
191 static void nubus_device_class_init(ObjectClass *oc, void *data)
192 {
193     DeviceClass *dc = DEVICE_CLASS(oc);
194
195     dc->realize = nubus_device_realize;
196     dc->bus_type = TYPE_NUBUS_BUS;
197     device_class_set_props(dc, nubus_device_properties);
198 }
199
200 static const TypeInfo nubus_device_type_info = {
201     .name = TYPE_NUBUS_DEVICE,
202     .parent = TYPE_DEVICE,
203     .abstract = true,
204     .instance_size = sizeof(NubusDevice),
205     .class_init = nubus_device_class_init,
206 };
207
208 static void nubus_register_types(void)
209 {
210     type_register_static(&nubus_device_type_info);
211 }
212
213 type_init(nubus_register_types)