]>
Commit | Line | Data |
---|---|---|
3d2bf4a1 GH |
1 | /* |
2 | * QEMU IDE Emulation: mmio support (for embedded). | |
3 | * | |
4 | * Copyright (c) 2003 Fabrice Bellard | |
5 | * Copyright (c) 2006 Openedhand Ltd. | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
0b8fa32f | 25 | |
53239262 | 26 | #include "qemu/osdep.h" |
6b2578d6 | 27 | #include "hw/sysbus.h" |
d6454270 | 28 | #include "migration/vmstate.h" |
0b8fa32f | 29 | #include "qemu/module.h" |
9c17d615 | 30 | #include "sysemu/dma.h" |
59f2a787 | 31 | |
a9c94277 | 32 | #include "hw/ide/internal.h" |
a27bd6c7 | 33 | #include "hw/qdev-properties.h" |
db1015e9 | 34 | #include "qom/object.h" |
3d2bf4a1 GH |
35 | |
36 | /***********************************************************/ | |
37 | /* MMIO based ide port | |
38 | * This emulates IDE device connected directly to the CPU bus without | |
39 | * dedicated ide controller, which is often seen on embedded boards. | |
40 | */ | |
41 | ||
6b2578d6 | 42 | #define TYPE_MMIO_IDE "mmio-ide" |
db1015e9 | 43 | typedef struct MMIOIDEState MMIOState; |
6b2578d6 AF |
44 | #define MMIO_IDE(obj) OBJECT_CHECK(MMIOState, (obj), TYPE_MMIO_IDE) |
45 | ||
db1015e9 | 46 | struct MMIOIDEState { |
6b2578d6 AF |
47 | /*< private >*/ |
48 | SysBusDevice parent_obj; | |
49 | /*< public >*/ | |
50 | ||
0ce51e92 | 51 | IDEBus bus; |
6b2578d6 AF |
52 | |
53 | uint32_t shift; | |
54 | qemu_irq irq; | |
9d7f1b9a | 55 | MemoryRegion iomem1, iomem2; |
db1015e9 | 56 | }; |
3d2bf4a1 | 57 | |
6b2578d6 | 58 | static void mmio_ide_reset(DeviceState *dev) |
4a643563 | 59 | { |
6b2578d6 | 60 | MMIOState *s = MMIO_IDE(dev); |
4a643563 BS |
61 | |
62 | ide_bus_reset(&s->bus); | |
63 | } | |
64 | ||
a8170e5e | 65 | static uint64_t mmio_ide_read(void *opaque, hwaddr addr, |
9d7f1b9a | 66 | unsigned size) |
3d2bf4a1 | 67 | { |
18c0fb30 | 68 | MMIOState *s = opaque; |
3d2bf4a1 GH |
69 | addr >>= s->shift; |
70 | if (addr & 7) | |
0ce51e92 | 71 | return ide_ioport_read(&s->bus, addr); |
3d2bf4a1 | 72 | else |
0ce51e92 | 73 | return ide_data_readw(&s->bus, 0); |
3d2bf4a1 GH |
74 | } |
75 | ||
a8170e5e | 76 | static void mmio_ide_write(void *opaque, hwaddr addr, |
9d7f1b9a | 77 | uint64_t val, unsigned size) |
3d2bf4a1 | 78 | { |
18c0fb30 | 79 | MMIOState *s = opaque; |
3d2bf4a1 GH |
80 | addr >>= s->shift; |
81 | if (addr & 7) | |
0ce51e92 | 82 | ide_ioport_write(&s->bus, addr, val); |
3d2bf4a1 | 83 | else |
0ce51e92 | 84 | ide_data_writew(&s->bus, 0, val); |
3d2bf4a1 GH |
85 | } |
86 | ||
9d7f1b9a AK |
87 | static const MemoryRegionOps mmio_ide_ops = { |
88 | .read = mmio_ide_read, | |
89 | .write = mmio_ide_write, | |
1a7044bb | 90 | .endianness = DEVICE_LITTLE_ENDIAN, |
3d2bf4a1 GH |
91 | }; |
92 | ||
a8170e5e | 93 | static uint64_t mmio_ide_status_read(void *opaque, hwaddr addr, |
9d7f1b9a | 94 | unsigned size) |
3d2bf4a1 | 95 | { |
18c0fb30 | 96 | MMIOState *s= opaque; |
0ce51e92 | 97 | return ide_status_read(&s->bus, 0); |
3d2bf4a1 GH |
98 | } |
99 | ||
a8170e5e | 100 | static void mmio_ide_cmd_write(void *opaque, hwaddr addr, |
9d7f1b9a | 101 | uint64_t val, unsigned size) |
3d2bf4a1 | 102 | { |
18c0fb30 | 103 | MMIOState *s = opaque; |
0ce51e92 | 104 | ide_cmd_write(&s->bus, 0, val); |
3d2bf4a1 GH |
105 | } |
106 | ||
9d7f1b9a AK |
107 | static const MemoryRegionOps mmio_ide_cs_ops = { |
108 | .read = mmio_ide_status_read, | |
109 | .write = mmio_ide_cmd_write, | |
1a7044bb | 110 | .endianness = DEVICE_LITTLE_ENDIAN, |
3d2bf4a1 GH |
111 | }; |
112 | ||
24daf35c JQ |
113 | static const VMStateDescription vmstate_ide_mmio = { |
114 | .name = "mmio-ide", | |
115 | .version_id = 3, | |
116 | .minimum_version_id = 0, | |
35d08458 | 117 | .fields = (VMStateField[]) { |
24daf35c JQ |
118 | VMSTATE_IDE_BUS(bus, MMIOState), |
119 | VMSTATE_IDE_DRIVES(bus.ifs, MMIOState), | |
120 | VMSTATE_END_OF_LIST() | |
121 | } | |
122 | }; | |
2bcbf7e4 | 123 | |
6b2578d6 | 124 | static void mmio_ide_realizefn(DeviceState *dev, Error **errp) |
3d2bf4a1 | 125 | { |
6b2578d6 AF |
126 | SysBusDevice *d = SYS_BUS_DEVICE(dev); |
127 | MMIOState *s = MMIO_IDE(dev); | |
3d2bf4a1 | 128 | |
6b2578d6 | 129 | ide_init2(&s->bus, s->irq); |
3d2bf4a1 | 130 | |
1437c94b | 131 | memory_region_init_io(&s->iomem1, OBJECT(s), &mmio_ide_ops, s, |
6b2578d6 | 132 | "ide-mmio.1", 16 << s->shift); |
1437c94b | 133 | memory_region_init_io(&s->iomem2, OBJECT(s), &mmio_ide_cs_ops, s, |
6b2578d6 AF |
134 | "ide-mmio.2", 2 << s->shift); |
135 | sysbus_init_mmio(d, &s->iomem1); | |
136 | sysbus_init_mmio(d, &s->iomem2); | |
137 | } | |
138 | ||
139 | static void mmio_ide_initfn(Object *obj) | |
140 | { | |
141 | SysBusDevice *d = SYS_BUS_DEVICE(obj); | |
142 | MMIOState *s = MMIO_IDE(obj); | |
143 | ||
c6baf942 | 144 | ide_bus_new(&s->bus, sizeof(s->bus), DEVICE(obj), 0, 2); |
6b2578d6 AF |
145 | sysbus_init_irq(d, &s->irq); |
146 | } | |
147 | ||
148 | static Property mmio_ide_properties[] = { | |
149 | DEFINE_PROP_UINT32("shift", MMIOState, shift, 0), | |
150 | DEFINE_PROP_END_OF_LIST() | |
151 | }; | |
152 | ||
153 | static void mmio_ide_class_init(ObjectClass *oc, void *data) | |
154 | { | |
155 | DeviceClass *dc = DEVICE_CLASS(oc); | |
156 | ||
157 | dc->realize = mmio_ide_realizefn; | |
158 | dc->reset = mmio_ide_reset; | |
4f67d30b | 159 | device_class_set_props(dc, mmio_ide_properties); |
6b2578d6 AF |
160 | dc->vmsd = &vmstate_ide_mmio; |
161 | } | |
162 | ||
163 | static const TypeInfo mmio_ide_type_info = { | |
164 | .name = TYPE_MMIO_IDE, | |
165 | .parent = TYPE_SYS_BUS_DEVICE, | |
166 | .instance_size = sizeof(MMIOState), | |
167 | .instance_init = mmio_ide_initfn, | |
168 | .class_init = mmio_ide_class_init, | |
169 | }; | |
170 | ||
171 | static void mmio_ide_register_types(void) | |
172 | { | |
173 | type_register_static(&mmio_ide_type_info); | |
174 | } | |
175 | ||
176 | void mmio_ide_init_drives(DeviceState *dev, DriveInfo *hd0, DriveInfo *hd1) | |
177 | { | |
178 | MMIOState *s = MMIO_IDE(dev); | |
179 | ||
180 | if (hd0 != NULL) { | |
181 | ide_create_drive(&s->bus, 0, hd0); | |
182 | } | |
183 | if (hd1 != NULL) { | |
184 | ide_create_drive(&s->bus, 1, hd1); | |
185 | } | |
3d2bf4a1 GH |
186 | } |
187 | ||
6b2578d6 | 188 | type_init(mmio_ide_register_types) |