]> git.proxmox.com Git - qemu.git/blame - hw/ide/via.c
Merge git://github.com/hw-claudio/qemu-aarch64-queue into tcg-next
[qemu.git] / hw / ide / via.c
CommitLineData
016512f3
HC
1/*
2 * QEMU IDE Emulation: PCI VIA82C686B support.
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
6 * Copyright (c) 2010 Huacai Chen <zltjiangshi@gmail.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26#include <hw/hw.h>
0d09e41a 27#include <hw/i386/pc.h>
a2cb15b0 28#include <hw/pci/pci.h>
0d09e41a 29#include <hw/isa/isa.h>
737e150e 30#include "block/block.h"
9c17d615
PB
31#include "sysemu/sysemu.h"
32#include "sysemu/dma.h"
016512f3
HC
33
34#include <hw/ide/pci.h>
35
a8170e5e 36static uint64_t bmdma_read(void *opaque, hwaddr addr,
a9deb8c6 37 unsigned size)
016512f3
HC
38{
39 BMDMAState *bm = opaque;
40 uint32_t val;
41
a9deb8c6
AK
42 if (size != 1) {
43 return ((uint64_t)1 << (size * 8)) - 1;
44 }
45
016512f3
HC
46 switch (addr & 3) {
47 case 0:
48 val = bm->cmd;
49 break;
50 case 2:
51 val = bm->status;
52 break;
53 default:
54 val = 0xff;
55 break;
56 }
57#ifdef DEBUG_IDE
08406b03 58 printf("bmdma: readb 0x%02x : 0x%02x\n", addr, val);
016512f3
HC
59#endif
60 return val;
61}
62
a8170e5e 63static void bmdma_write(void *opaque, hwaddr addr,
a9deb8c6 64 uint64_t val, unsigned size)
016512f3
HC
65{
66 BMDMAState *bm = opaque;
a9deb8c6
AK
67
68 if (size != 1) {
69 return;
70 }
71
016512f3 72#ifdef DEBUG_IDE
08406b03 73 printf("bmdma: writeb 0x%02x : 0x%02x\n", addr, val);
016512f3
HC
74#endif
75 switch (addr & 3) {
a9deb8c6 76 case 0:
0ed8b6f6
BS
77 bmdma_cmd_writeb(bm, val);
78 break;
016512f3
HC
79 case 2:
80 bm->status = (val & 0x60) | (bm->status & 1) | (bm->status & ~val & 0x06);
81 break;
82 default:;
83 }
84}
85
a348f108 86static const MemoryRegionOps via_bmdma_ops = {
a9deb8c6
AK
87 .read = bmdma_read,
88 .write = bmdma_write,
89};
90
91static void bmdma_setup_bar(PCIIDEState *d)
016512f3 92{
016512f3
HC
93 int i;
94
1437c94b 95 memory_region_init(&d->bmdma_bar, OBJECT(d), "via-bmdma-container", 16);
016512f3
HC
96 for(i = 0;i < 2; i++) {
97 BMDMAState *bm = &d->bmdma[i];
016512f3 98
1437c94b 99 memory_region_init_io(&bm->extra_io, OBJECT(d), &via_bmdma_ops, bm,
a9deb8c6
AK
100 "via-bmdma", 4);
101 memory_region_add_subregion(&d->bmdma_bar, i * 8, &bm->extra_io);
1437c94b
PB
102 memory_region_init_io(&bm->addr_ioport, OBJECT(d),
103 &bmdma_addr_ioport_ops, bm, "bmdma", 4);
a9deb8c6 104 memory_region_add_subregion(&d->bmdma_bar, i * 8 + 4, &bm->addr_ioport);
016512f3
HC
105 }
106}
107
108static void via_reset(void *opaque)
109{
110 PCIIDEState *d = opaque;
111 uint8_t *pci_conf = d->dev.config;
112 int i;
113
114 for (i = 0; i < 2; i++) {
115 ide_bus_reset(&d->bus[i]);
016512f3
HC
116 }
117
118 pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_WAIT);
119 pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_FAST_BACK |
120 PCI_STATUS_DEVSEL_MEDIUM);
121
122 pci_set_long(pci_conf + PCI_BASE_ADDRESS_0, 0x000001f0);
123 pci_set_long(pci_conf + PCI_BASE_ADDRESS_1, 0x000003f4);
124 pci_set_long(pci_conf + PCI_BASE_ADDRESS_2, 0x00000170);
125 pci_set_long(pci_conf + PCI_BASE_ADDRESS_3, 0x00000374);
126 pci_set_long(pci_conf + PCI_BASE_ADDRESS_4, 0x0000cc01); /* BMIBA: 20-23h */
127 pci_set_long(pci_conf + PCI_INTERRUPT_LINE, 0x0000010e);
128
129 /* IDE chip enable, IDE configuration 1/2, IDE FIFO Configuration*/
130 pci_set_long(pci_conf + 0x40, 0x0a090600);
131 /* IDE misc configuration 1/2/3 */
132 pci_set_long(pci_conf + 0x44, 0x00c00068);
133 /* IDE Timing control */
134 pci_set_long(pci_conf + 0x48, 0xa8a8a8a8);
135 /* IDE Address Setup Time */
136 pci_set_long(pci_conf + 0x4c, 0x000000ff);
137 /* UltraDMA Extended Timing Control*/
138 pci_set_long(pci_conf + 0x50, 0x07070707);
139 /* UltraDMA FIFO Control */
140 pci_set_long(pci_conf + 0x54, 0x00000004);
141 /* IDE primary sector size */
142 pci_set_long(pci_conf + 0x60, 0x00000200);
143 /* IDE secondary sector size */
144 pci_set_long(pci_conf + 0x68, 0x00000200);
145 /* PCI PM Block */
146 pci_set_long(pci_conf + 0xc0, 0x00020001);
147}
148
61d9d6b0 149static void vt82c686b_init_ports(PCIIDEState *d) {
4a91d3b3 150 static const struct {
61d9d6b0
SH
151 int iobase;
152 int iobase2;
153 int isairq;
154 } port_info[] = {
155 {0x1f0, 0x3f6, 14},
156 {0x170, 0x376, 15},
157 };
4a91d3b3 158 int i;
61d9d6b0
SH
159
160 for (i = 0; i < 2; i++) {
0ee20e66 161 ide_bus_new(&d->bus[i], &d->dev.qdev, i, 2);
4a91d3b3
RH
162 ide_init_ioport(&d->bus[i], NULL, port_info[i].iobase,
163 port_info[i].iobase2);
48a18b3c 164 ide_init2(&d->bus[i], isa_get_irq(NULL, port_info[i].isairq));
61d9d6b0 165
a9deb8c6 166 bmdma_init(&d->bus[i], &d->bmdma[i], d);
61d9d6b0
SH
167 d->bmdma[i].bus = &d->bus[i];
168 qemu_add_vm_change_state_handler(d->bus[i].dma->ops->restart_cb,
653af235 169 &d->bmdma[i].dma);
61d9d6b0
SH
170 }
171}
172
016512f3
HC
173/* via ide func */
174static int vt82c686b_ide_initfn(PCIDevice *dev)
175{
3a93113a 176 PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);
016512f3
HC
177 uint8_t *pci_conf = d->dev.config;
178
016512f3 179 pci_config_set_prog_interface(pci_conf, 0x8a); /* legacy ATA mode */
016512f3
HC
180 pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
181
182 qemu_register_reset(via_reset, d);
a9deb8c6 183 bmdma_setup_bar(d);
e824b2cc 184 pci_register_bar(&d->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &d->bmdma_bar);
016512f3 185
1724f049 186 vmstate_register(&dev->qdev, 0, &vmstate_ide_pci, d);
016512f3 187
61d9d6b0 188 vt82c686b_init_ports(d);
016512f3
HC
189
190 return 0;
191}
192
f90c2bcd 193static void vt82c686b_ide_exitfn(PCIDevice *dev)
a9deb8c6
AK
194{
195 PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);
196 unsigned i;
197
198 for (i = 0; i < 2; ++i) {
199 memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].extra_io);
200 memory_region_destroy(&d->bmdma[i].extra_io);
201 memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].addr_ioport);
202 memory_region_destroy(&d->bmdma[i].addr_ioport);
203 }
204 memory_region_destroy(&d->bmdma_bar);
a9deb8c6
AK
205}
206
016512f3
HC
207void vt82c686b_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
208{
209 PCIDevice *dev;
210
211 dev = pci_create_simple(bus, devfn, "via-ide");
212 pci_ide_create_devs(dev, hd_table);
213}
214
40021f08
AL
215static void via_ide_class_init(ObjectClass *klass, void *data)
216{
39bffca2 217 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
218 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
219
220 k->init = vt82c686b_ide_initfn;
221 k->exit = vt82c686b_ide_exitfn;
222 k->vendor_id = PCI_VENDOR_ID_VIA;
223 k->device_id = PCI_DEVICE_ID_VIA_IDE;
224 k->revision = 0x06;
225 k->class_id = PCI_CLASS_STORAGE_IDE;
39bffca2 226 dc->no_user = 1;
40021f08
AL
227}
228
8c43a6f0 229static const TypeInfo via_ide_info = {
39bffca2
AL
230 .name = "via-ide",
231 .parent = TYPE_PCI_DEVICE,
232 .instance_size = sizeof(PCIIDEState),
233 .class_init = via_ide_class_init,
016512f3
HC
234};
235
83f7d43a 236static void via_ide_register_types(void)
016512f3 237{
39bffca2 238 type_register_static(&via_ide_info);
016512f3 239}
83f7d43a
AF
240
241type_init(via_ide_register_types)