OSDN Git Service

ide/via: Rename functions to match device name
[qmiga/qemu.git] / hw / ide / via.c
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 "qemu/osdep.h"
27 #include "hw/hw.h"
28 #include "hw/pci/pci.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/dma.h"
31
32 #include "hw/ide/pci.h"
33 #include "trace.h"
34
35 static const struct {
36     int iobase;
37     int iobase2;
38     int isairq;
39 } port_info[] = {
40     {0x1f0, 0x3f6, 14},
41     {0x170, 0x376, 15},
42 };
43
44 static uint64_t bmdma_read(void *opaque, hwaddr addr,
45                            unsigned size)
46 {
47     BMDMAState *bm = opaque;
48     uint32_t val;
49
50     if (size != 1) {
51         return ((uint64_t)1 << (size * 8)) - 1;
52     }
53
54     switch (addr & 3) {
55     case 0:
56         val = bm->cmd;
57         break;
58     case 2:
59         val = bm->status;
60         break;
61     default:
62         val = 0xff;
63         break;
64     }
65
66     trace_bmdma_read_via(addr, val);
67     return val;
68 }
69
70 static void bmdma_write(void *opaque, hwaddr addr,
71                         uint64_t val, unsigned size)
72 {
73     BMDMAState *bm = opaque;
74
75     if (size != 1) {
76         return;
77     }
78
79     trace_bmdma_write_via(addr, val);
80     switch (addr & 3) {
81     case 0:
82         bmdma_cmd_writeb(bm, val);
83         break;
84     case 2:
85         bm->status = (val & 0x60) | (bm->status & 1) | (bm->status & ~val & 0x06);
86         break;
87     default:;
88     }
89 }
90
91 static const MemoryRegionOps via_bmdma_ops = {
92     .read = bmdma_read,
93     .write = bmdma_write,
94 };
95
96 static void bmdma_setup_bar(PCIIDEState *d)
97 {
98     int i;
99
100     memory_region_init(&d->bmdma_bar, OBJECT(d), "via-bmdma-container", 16);
101     for(i = 0;i < 2; i++) {
102         BMDMAState *bm = &d->bmdma[i];
103
104         memory_region_init_io(&bm->extra_io, OBJECT(d), &via_bmdma_ops, bm,
105                               "via-bmdma", 4);
106         memory_region_add_subregion(&d->bmdma_bar, i * 8, &bm->extra_io);
107         memory_region_init_io(&bm->addr_ioport, OBJECT(d),
108                               &bmdma_addr_ioport_ops, bm, "bmdma", 4);
109         memory_region_add_subregion(&d->bmdma_bar, i * 8 + 4, &bm->addr_ioport);
110     }
111 }
112
113 static void via_ide_reset(void *opaque)
114 {
115     PCIIDEState *d = opaque;
116     PCIDevice *pd = PCI_DEVICE(d);
117     uint8_t *pci_conf = pd->config;
118     int i;
119
120     for (i = 0; i < 2; i++) {
121         ide_bus_reset(&d->bus[i]);
122     }
123
124     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_WAIT);
125     pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_FAST_BACK |
126                  PCI_STATUS_DEVSEL_MEDIUM);
127
128     pci_set_long(pci_conf + PCI_BASE_ADDRESS_0, 0x000001f0);
129     pci_set_long(pci_conf + PCI_BASE_ADDRESS_1, 0x000003f4);
130     pci_set_long(pci_conf + PCI_BASE_ADDRESS_2, 0x00000170);
131     pci_set_long(pci_conf + PCI_BASE_ADDRESS_3, 0x00000374);
132     pci_set_long(pci_conf + PCI_BASE_ADDRESS_4, 0x0000cc01); /* BMIBA: 20-23h */
133     pci_set_long(pci_conf + PCI_INTERRUPT_LINE, 0x0000010e);
134
135     /* IDE chip enable, IDE configuration 1/2, IDE FIFO Configuration*/
136     pci_set_long(pci_conf + 0x40, 0x0a090600);
137     /* IDE misc configuration 1/2/3 */
138     pci_set_long(pci_conf + 0x44, 0x00c00068);
139     /* IDE Timing control */
140     pci_set_long(pci_conf + 0x48, 0xa8a8a8a8);
141     /* IDE Address Setup Time */
142     pci_set_long(pci_conf + 0x4c, 0x000000ff);
143     /* UltraDMA Extended Timing Control*/
144     pci_set_long(pci_conf + 0x50, 0x07070707);
145     /* UltraDMA FIFO Control */
146     pci_set_long(pci_conf + 0x54, 0x00000004);
147     /* IDE primary sector size */
148     pci_set_long(pci_conf + 0x60, 0x00000200);
149     /* IDE secondary sector size */
150     pci_set_long(pci_conf + 0x68, 0x00000200);
151     /* PCI PM Block */
152     pci_set_long(pci_conf + 0xc0, 0x00020001);
153 }
154
155 static void via_ide_realize(PCIDevice *dev, Error **errp)
156 {
157     PCIIDEState *d = PCI_IDE(dev);
158     uint8_t *pci_conf = dev->config;
159     int i;
160
161     pci_config_set_prog_interface(pci_conf, 0x8a); /* legacy ATA mode */
162     pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
163
164     qemu_register_reset(via_ide_reset, d);
165     bmdma_setup_bar(d);
166     pci_register_bar(dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &d->bmdma_bar);
167
168     vmstate_register(DEVICE(dev), 0, &vmstate_ide_pci, d);
169
170     for (i = 0; i < 2; i++) {
171         ide_bus_new(&d->bus[i], sizeof(d->bus[i]), DEVICE(d), i, 2);
172         ide_init_ioport(&d->bus[i], NULL, port_info[i].iobase,
173                         port_info[i].iobase2);
174         ide_init2(&d->bus[i], isa_get_irq(NULL, port_info[i].isairq));
175
176         bmdma_init(&d->bus[i], &d->bmdma[i], d);
177         d->bmdma[i].bus = &d->bus[i];
178         ide_register_restart_cb(&d->bus[i]);
179     }
180 }
181
182 static void via_ide_exitfn(PCIDevice *dev)
183 {
184     PCIIDEState *d = PCI_IDE(dev);
185     unsigned i;
186
187     for (i = 0; i < 2; ++i) {
188         memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].extra_io);
189         memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].addr_ioport);
190     }
191 }
192
193 void via_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
194 {
195     PCIDevice *dev;
196
197     dev = pci_create_simple(bus, devfn, "via-ide");
198     pci_ide_create_devs(dev, hd_table);
199 }
200
201 static void via_ide_class_init(ObjectClass *klass, void *data)
202 {
203     DeviceClass *dc = DEVICE_CLASS(klass);
204     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
205
206     k->realize = via_ide_realize;
207     k->exit = via_ide_exitfn;
208     k->vendor_id = PCI_VENDOR_ID_VIA;
209     k->device_id = PCI_DEVICE_ID_VIA_IDE;
210     k->revision = 0x06;
211     k->class_id = PCI_CLASS_STORAGE_IDE;
212     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
213 }
214
215 static const TypeInfo via_ide_info = {
216     .name          = "via-ide",
217     .parent        = TYPE_PCI_IDE,
218     .class_init    = via_ide_class_init,
219 };
220
221 static void via_ide_register_types(void)
222 {
223     type_register_static(&via_ide_info);
224 }
225
226 type_init(via_ide_register_types)