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.
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
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
;
35 #define BYTE(v, b) (((v) >> (24 - 8 * (b))) & 0xff)
37 case FBLOCK_BYTE_LANES
:
38 val
= dev
->byte_lanes
;
39 val
|= (val
^ 0xf) << 4;
44 case FBLOCK_TEST_PATTERN
...FBLOCK_TEST_PATTERN
+ 3:
45 val
= BYTE(FBLOCK_PATTERN_VAL
, addr
- FBLOCK_TEST_PATTERN
);
48 val
= dev
->rom_format
;
50 case FBLOCK_REVISION_LEVEL
:
53 case FBLOCK_CRC
...FBLOCK_CRC
+ 3:
54 val
= BYTE(dev
->rom_crc
, addr
- FBLOCK_CRC
);
56 case FBLOCK_LENGTH
...FBLOCK_LENGTH
+ 3:
57 val
= BYTE(dev
->rom_length
, addr
- FBLOCK_LENGTH
);
59 case FBLOCK_DIRECTORY_OFFSET
...FBLOCK_DIRECTORY_OFFSET
+ 3:
60 val
= BYTE(dev
->directory_offset
, addr
- FBLOCK_DIRECTORY_OFFSET
);
69 static void nubus_fblock_write(void *opaque
, hwaddr addr
, uint64_t val
,
75 static const MemoryRegionOps nubus_format_block_ops
= {
76 .read
= nubus_fblock_read
,
77 .write
= nubus_fblock_write
,
78 .endianness
= DEVICE_BIG_ENDIAN
,
85 static void nubus_register_format_block(NubusDevice
*dev
)
89 fblock_name
= g_strdup_printf("nubus-slot-%d-format-block",
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
,
101 static void mac_nubus_rom_write(void *opaque
, hwaddr addr
, uint64_t val
,
107 static uint64_t mac_nubus_rom_read(void *opaque
, hwaddr addr
,
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
,
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
)
132 /* FIXME : really compute CRC */
136 dev
->rom_rev
= revision
;
137 dev
->rom_format
= format
;
139 dev
->byte_lanes
= byte_lanes
;
140 dev
->directory_offset
= -size
;
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
);
157 static void nubus_device_realize(DeviceState
*dev
, Error
**errp
)
159 NubusBus
*nubus
= NUBUS_BUS(qdev_get_parent_bus(DEVICE(dev
)));
160 NubusDevice
*nd
= NUBUS_DEVICE(dev
);
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");
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
) {
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
,
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
,
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
,
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
)