]> git.proxmox.com Git - mirror_qemu.git/blame - hw/net/pcnet-pci.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / hw / net / pcnet-pci.c
CommitLineData
661a1799
PB
1/*
2 * QEMU AMD PC-Net II (Am79C970A) PCI emulation
3 *
4 * Copyright (c) 2004 Antony T Curtis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* This software was written to be compatible with the specification:
26 * AMD Am79C970A PCnet-PCI II Ethernet Controller Data-Sheet
27 * AMD Publication# 19436 Rev:E Amendment/0 Issue Date: June 2000
28 */
29
e8d40465 30#include "qemu/osdep.h"
64552b6b 31#include "hw/irq.h"
edf5ca5d 32#include "hw/pci/pci_device.h"
a27bd6c7 33#include "hw/qdev-properties.h"
d6454270 34#include "migration/vmstate.h"
1422e32d 35#include "net/net.h"
0b8fa32f 36#include "qemu/module.h"
1de7afc9 37#include "qemu/timer.h"
9c17d615 38#include "sysemu/dma.h"
ea3b3511 39#include "sysemu/sysemu.h"
32c95249 40#include "trace.h"
661a1799 41
47b43a1f 42#include "pcnet.h"
db1015e9 43#include "qom/object.h"
661a1799
PB
44
45//#define PCNET_DEBUG
46//#define PCNET_DEBUG_IO
47//#define PCNET_DEBUG_BCR
48//#define PCNET_DEBUG_CSR
49//#define PCNET_DEBUG_RMD
50//#define PCNET_DEBUG_TMD
51//#define PCNET_DEBUG_MATCH
52
1f8c7946
PC
53#define TYPE_PCI_PCNET "pcnet"
54
8063396b 55OBJECT_DECLARE_SIMPLE_TYPE(PCIPCNetState, PCI_PCNET)
661a1799 56
db1015e9 57struct PCIPCNetState {
1f8c7946
PC
58 /*< private >*/
59 PCIDevice parent_obj;
60 /*< public >*/
61
661a1799 62 PCNetState state;
bd8d6f7c 63 MemoryRegion io_bar;
db1015e9 64};
661a1799
PB
65
66static void pcnet_aprom_writeb(void *opaque, uint32_t addr, uint32_t val)
67{
68 PCNetState *s = opaque;
32c95249
DK
69
70 trace_pcnet_aprom_writeb(opaque, addr, val);
488a1a5d 71 if (BCR_APROMWE(s)) {
661a1799 72 s->prom[addr & 15] = val;
488a1a5d 73 }
661a1799
PB
74}
75
76static uint32_t pcnet_aprom_readb(void *opaque, uint32_t addr)
77{
78 PCNetState *s = opaque;
79 uint32_t val = s->prom[addr & 15];
32c95249
DK
80
81 trace_pcnet_aprom_readb(opaque, addr, val);
661a1799
PB
82 return val;
83}
84
a8170e5e 85static uint64_t pcnet_ioport_read(void *opaque, hwaddr addr,
bd8d6f7c 86 unsigned size)
661a1799 87{
bd8d6f7c 88 PCNetState *d = opaque;
661a1799 89
32c95249 90 trace_pcnet_ioport_read(opaque, addr, size);
7ba79741
JK
91 if (addr < 0x10) {
92 if (!BCR_DWIO(d) && size == 1) {
93 return pcnet_aprom_readb(d, addr);
94 } else if (!BCR_DWIO(d) && (addr & 1) == 0 && size == 2) {
95 return pcnet_aprom_readb(d, addr) |
96 (pcnet_aprom_readb(d, addr + 1) << 8);
97 } else if (BCR_DWIO(d) && (addr & 3) == 0 && size == 4) {
98 return pcnet_aprom_readb(d, addr) |
99 (pcnet_aprom_readb(d, addr + 1) << 8) |
100 (pcnet_aprom_readb(d, addr + 2) << 16) |
101 (pcnet_aprom_readb(d, addr + 3) << 24);
102 }
103 } else {
104 if (size == 2) {
105 return pcnet_ioport_readw(d, addr);
106 } else if (size == 4) {
107 return pcnet_ioport_readl(d, addr);
108 }
bd8d6f7c
AK
109 }
110 return ((uint64_t)1 << (size * 8)) - 1;
111}
661a1799 112
a8170e5e 113static void pcnet_ioport_write(void *opaque, hwaddr addr,
bd8d6f7c
AK
114 uint64_t data, unsigned size)
115{
116 PCNetState *d = opaque;
661a1799 117
32c95249 118 trace_pcnet_ioport_write(opaque, addr, data, size);
7ba79741
JK
119 if (addr < 0x10) {
120 if (!BCR_DWIO(d) && size == 1) {
121 pcnet_aprom_writeb(d, addr, data);
122 } else if (!BCR_DWIO(d) && (addr & 1) == 0 && size == 2) {
123 pcnet_aprom_writeb(d, addr, data & 0xff);
124 pcnet_aprom_writeb(d, addr + 1, data >> 8);
125 } else if (BCR_DWIO(d) && (addr & 3) == 0 && size == 4) {
126 pcnet_aprom_writeb(d, addr, data & 0xff);
127 pcnet_aprom_writeb(d, addr + 1, (data >> 8) & 0xff);
128 pcnet_aprom_writeb(d, addr + 2, (data >> 16) & 0xff);
129 pcnet_aprom_writeb(d, addr + 3, data >> 24);
130 }
131 } else {
132 if (size == 2) {
133 pcnet_ioport_writew(d, addr, data);
134 } else if (size == 4) {
135 pcnet_ioport_writel(d, addr, data);
136 }
bd8d6f7c 137 }
661a1799
PB
138}
139
bd8d6f7c
AK
140static const MemoryRegionOps pcnet_io_ops = {
141 .read = pcnet_ioport_read,
142 .write = pcnet_ioport_write,
a26405b3 143 .endianness = DEVICE_LITTLE_ENDIAN,
bd8d6f7c
AK
144};
145
661a1799
PB
146static const VMStateDescription vmstate_pci_pcnet = {
147 .name = "pcnet",
148 .version_id = 3,
149 .minimum_version_id = 2,
1de81b42 150 .fields = (const VMStateField[]) {
1f8c7946 151 VMSTATE_PCI_DEVICE(parent_obj, PCIPCNetState),
661a1799
PB
152 VMSTATE_STRUCT(state, PCIPCNetState, 0, vmstate_pcnet, PCNetState),
153 VMSTATE_END_OF_LIST()
154 }
155};
156
157/* PCI interface */
158
bd8d6f7c 159static const MemoryRegionOps pcnet_mmio_ops = {
b187e20f
PM
160 .read = pcnet_ioport_read,
161 .write = pcnet_ioport_write,
5d026de8
PM
162 .valid.min_access_size = 1,
163 .valid.max_access_size = 4,
164 .impl.min_access_size = 1,
165 .impl.max_access_size = 4,
a26405b3 166 .endianness = DEVICE_LITTLE_ENDIAN,
661a1799
PB
167};
168
a8170e5e 169static void pci_physical_memory_write(void *dma_opaque, hwaddr addr,
661a1799
PB
170 uint8_t *buf, int len, int do_bswap)
171{
14fecf26 172 pci_dma_write(dma_opaque, addr, buf, len);
661a1799
PB
173}
174
a8170e5e 175static void pci_physical_memory_read(void *dma_opaque, hwaddr addr,
661a1799
PB
176 uint8_t *buf, int len, int do_bswap)
177{
14fecf26 178 pci_dma_read(dma_opaque, addr, buf, len);
661a1799
PB
179}
180
f90c2bcd 181static void pci_pcnet_uninit(PCIDevice *dev)
661a1799 182{
1f8c7946 183 PCIPCNetState *d = PCI_PCNET(dev);
661a1799 184
9e64f8a3 185 qemu_free_irq(d->state.irq);
bc72ad67 186 timer_free(d->state.poll_timer);
948ecf21 187 qemu_del_nic(d->state.nic);
661a1799
PB
188}
189
190static NetClientInfo net_pci_pcnet_info = {
f394b2e2 191 .type = NET_CLIENT_DRIVER_NIC,
661a1799 192 .size = sizeof(NICState),
661a1799 193 .receive = pcnet_receive,
e1c2008a 194 .link_status_changed = pcnet_set_link_status,
661a1799
PB
195};
196
eb1bef94 197static void pci_pcnet_realize(PCIDevice *pci_dev, Error **errp)
661a1799 198{
1f8c7946 199 PCIPCNetState *d = PCI_PCNET(pci_dev);
661a1799
PB
200 PCNetState *s = &d->state;
201 uint8_t *pci_conf;
202
203#if 0
204 printf("sizeof(RMD)=%d, sizeof(TMD)=%d\n",
205 sizeof(struct pcnet_RMD), sizeof(struct pcnet_TMD));
206#endif
207
208 pci_conf = pci_dev->config;
209
661a1799
PB
210 pci_set_word(pci_conf + PCI_STATUS,
211 PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
661a1799
PB
212
213 pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, 0x0);
214 pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0);
215
817e0b6f 216 pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
661a1799
PB
217 pci_conf[PCI_MIN_GNT] = 0x06;
218 pci_conf[PCI_MAX_LAT] = 0xff;
219
220 /* Handler for memory-mapped I/O */
eedfac6f
PB
221 memory_region_init_io(&d->state.mmio, OBJECT(d), &pcnet_mmio_ops, s,
222 "pcnet-mmio", PCNET_PNPMMIO_SIZE);
661a1799 223
eedfac6f 224 memory_region_init_io(&d->io_bar, OBJECT(d), &pcnet_io_ops, s, "pcnet-io",
bd8d6f7c 225 PCNET_IOPORT_SIZE);
e824b2cc 226 pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->io_bar);
661a1799 227
e824b2cc 228 pci_register_bar(pci_dev, 1, 0, &s->mmio);
661a1799 229
9e64f8a3 230 s->irq = pci_allocate_irq(pci_dev);
661a1799
PB
231 s->phys_mem_read = pci_physical_memory_read;
232 s->phys_mem_write = pci_physical_memory_write;
4cc76287 233 s->dma_opaque = DEVICE(pci_dev);
661a1799 234
4c3b2245 235 pcnet_common_init(DEVICE(pci_dev), s, &net_pci_pcnet_info);
661a1799
PB
236}
237
238static void pci_reset(DeviceState *dev)
239{
1f8c7946 240 PCIPCNetState *d = PCI_PCNET(dev);
661a1799
PB
241
242 pcnet_h_reset(&d->state);
243}
244
ea3b3511
GA
245static void pcnet_instance_init(Object *obj)
246{
247 PCIPCNetState *d = PCI_PCNET(obj);
248 PCNetState *s = &d->state;
249
250 device_add_bootindex_property(obj, &s->conf.bootindex,
251 "bootindex", "/ethernet-phy@0",
40c2281c 252 DEVICE(obj));
ea3b3511
GA
253}
254
40021f08
AL
255static Property pcnet_properties[] = {
256 DEFINE_NIC_PROPERTIES(PCIPCNetState, state.conf),
257 DEFINE_PROP_END_OF_LIST(),
258};
259
260static void pcnet_class_init(ObjectClass *klass, void *data)
261{
39bffca2 262 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
263 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
264
eb1bef94 265 k->realize = pci_pcnet_realize;
40021f08 266 k->exit = pci_pcnet_uninit;
c45e5b5b 267 k->romfile = "efi-pcnet.rom",
40021f08
AL
268 k->vendor_id = PCI_VENDOR_ID_AMD;
269 k->device_id = PCI_DEVICE_ID_AMD_LANCE;
270 k->revision = 0x10;
271 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
39bffca2
AL
272 dc->reset = pci_reset;
273 dc->vmsd = &vmstate_pci_pcnet;
4f67d30b 274 device_class_set_props(dc, pcnet_properties);
125ee0ed 275 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
40021f08
AL
276}
277
8c43a6f0 278static const TypeInfo pcnet_info = {
1f8c7946 279 .name = TYPE_PCI_PCNET,
39bffca2
AL
280 .parent = TYPE_PCI_DEVICE,
281 .instance_size = sizeof(PCIPCNetState),
282 .class_init = pcnet_class_init,
ea3b3511 283 .instance_init = pcnet_instance_init,
fd3b02c8
EH
284 .interfaces = (InterfaceInfo[]) {
285 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
286 { },
287 },
661a1799
PB
288};
289
83f7d43a 290static void pci_pcnet_register_types(void)
661a1799 291{
39bffca2 292 type_register_static(&pcnet_info);
661a1799
PB
293}
294
83f7d43a 295type_init(pci_pcnet_register_types)