Statistics
| Branch: | Revision:

root / hw / pcnet-pci.c @ d4970b07

History | View | Annotate | Download (9.7 kB)

1 661a1799 Paul Brook
/*
2 661a1799 Paul Brook
 * QEMU AMD PC-Net II (Am79C970A) PCI emulation
3 661a1799 Paul Brook
 *
4 661a1799 Paul Brook
 * Copyright (c) 2004 Antony T Curtis
5 661a1799 Paul Brook
 *
6 661a1799 Paul Brook
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 661a1799 Paul Brook
 * of this software and associated documentation files (the "Software"), to deal
8 661a1799 Paul Brook
 * in the Software without restriction, including without limitation the rights
9 661a1799 Paul Brook
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 661a1799 Paul Brook
 * copies of the Software, and to permit persons to whom the Software is
11 661a1799 Paul Brook
 * furnished to do so, subject to the following conditions:
12 661a1799 Paul Brook
 *
13 661a1799 Paul Brook
 * The above copyright notice and this permission notice shall be included in
14 661a1799 Paul Brook
 * all copies or substantial portions of the Software.
15 661a1799 Paul Brook
 *
16 661a1799 Paul Brook
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 661a1799 Paul Brook
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 661a1799 Paul Brook
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 661a1799 Paul Brook
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 661a1799 Paul Brook
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 661a1799 Paul Brook
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 661a1799 Paul Brook
 * THE SOFTWARE.
23 661a1799 Paul Brook
 */
24 661a1799 Paul Brook
25 661a1799 Paul Brook
/* This software was written to be compatible with the specification:
26 661a1799 Paul Brook
 * AMD Am79C970A PCnet-PCI II Ethernet Controller Data-Sheet
27 661a1799 Paul Brook
 * AMD Publication# 19436  Rev:E  Amendment/0  Issue Date: June 2000
28 661a1799 Paul Brook
 */
29 661a1799 Paul Brook
30 661a1799 Paul Brook
#include "pci.h"
31 661a1799 Paul Brook
#include "net.h"
32 661a1799 Paul Brook
#include "loader.h"
33 661a1799 Paul Brook
#include "qemu-timer.h"
34 661a1799 Paul Brook
35 661a1799 Paul Brook
#include "pcnet.h"
36 661a1799 Paul Brook
37 661a1799 Paul Brook
//#define PCNET_DEBUG
38 661a1799 Paul Brook
//#define PCNET_DEBUG_IO
39 661a1799 Paul Brook
//#define PCNET_DEBUG_BCR
40 661a1799 Paul Brook
//#define PCNET_DEBUG_CSR
41 661a1799 Paul Brook
//#define PCNET_DEBUG_RMD
42 661a1799 Paul Brook
//#define PCNET_DEBUG_TMD
43 661a1799 Paul Brook
//#define PCNET_DEBUG_MATCH
44 661a1799 Paul Brook
45 661a1799 Paul Brook
46 661a1799 Paul Brook
typedef struct {
47 661a1799 Paul Brook
    PCIDevice pci_dev;
48 661a1799 Paul Brook
    PCNetState state;
49 661a1799 Paul Brook
} PCIPCNetState;
50 661a1799 Paul Brook
51 661a1799 Paul Brook
static void pcnet_aprom_writeb(void *opaque, uint32_t addr, uint32_t val)
52 661a1799 Paul Brook
{
53 661a1799 Paul Brook
    PCNetState *s = opaque;
54 661a1799 Paul Brook
#ifdef PCNET_DEBUG
55 661a1799 Paul Brook
    printf("pcnet_aprom_writeb addr=0x%08x val=0x%02x\n", addr, val);
56 661a1799 Paul Brook
#endif
57 661a1799 Paul Brook
    /* Check APROMWE bit to enable write access */
58 661a1799 Paul Brook
    if (pcnet_bcr_readw(s,2) & 0x100)
59 661a1799 Paul Brook
        s->prom[addr & 15] = val;
60 661a1799 Paul Brook
}
61 661a1799 Paul Brook
62 661a1799 Paul Brook
static uint32_t pcnet_aprom_readb(void *opaque, uint32_t addr)
63 661a1799 Paul Brook
{
64 661a1799 Paul Brook
    PCNetState *s = opaque;
65 661a1799 Paul Brook
    uint32_t val = s->prom[addr & 15];
66 661a1799 Paul Brook
#ifdef PCNET_DEBUG
67 661a1799 Paul Brook
    printf("pcnet_aprom_readb addr=0x%08x val=0x%02x\n", addr, val);
68 661a1799 Paul Brook
#endif
69 661a1799 Paul Brook
    return val;
70 661a1799 Paul Brook
}
71 661a1799 Paul Brook
72 661a1799 Paul Brook
static void pcnet_ioport_map(PCIDevice *pci_dev, int region_num,
73 661a1799 Paul Brook
                             pcibus_t addr, pcibus_t size, int type)
74 661a1799 Paul Brook
{
75 661a1799 Paul Brook
    PCNetState *d = &DO_UPCAST(PCIPCNetState, pci_dev, pci_dev)->state;
76 661a1799 Paul Brook
77 661a1799 Paul Brook
#ifdef PCNET_DEBUG_IO
78 661a1799 Paul Brook
    printf("pcnet_ioport_map addr=0x%04"FMT_PCIBUS" size=0x%04"FMT_PCIBUS"\n",
79 661a1799 Paul Brook
           addr, size);
80 661a1799 Paul Brook
#endif
81 661a1799 Paul Brook
82 661a1799 Paul Brook
    register_ioport_write(addr, 16, 1, pcnet_aprom_writeb, d);
83 661a1799 Paul Brook
    register_ioport_read(addr, 16, 1, pcnet_aprom_readb, d);
84 661a1799 Paul Brook
85 661a1799 Paul Brook
    register_ioport_write(addr + 0x10, 0x10, 2, pcnet_ioport_writew, d);
86 661a1799 Paul Brook
    register_ioport_read(addr + 0x10, 0x10, 2, pcnet_ioport_readw, d);
87 661a1799 Paul Brook
    register_ioport_write(addr + 0x10, 0x10, 4, pcnet_ioport_writel, d);
88 661a1799 Paul Brook
    register_ioport_read(addr + 0x10, 0x10, 4, pcnet_ioport_readl, d);
89 661a1799 Paul Brook
}
90 661a1799 Paul Brook
91 661a1799 Paul Brook
static void pcnet_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
92 661a1799 Paul Brook
{
93 661a1799 Paul Brook
    PCNetState *d = opaque;
94 661a1799 Paul Brook
#ifdef PCNET_DEBUG_IO
95 661a1799 Paul Brook
    printf("pcnet_mmio_writeb addr=0x" TARGET_FMT_plx" val=0x%02x\n", addr,
96 661a1799 Paul Brook
           val);
97 661a1799 Paul Brook
#endif
98 661a1799 Paul Brook
    if (!(addr & 0x10))
99 661a1799 Paul Brook
        pcnet_aprom_writeb(d, addr & 0x0f, val);
100 661a1799 Paul Brook
}
101 661a1799 Paul Brook
102 661a1799 Paul Brook
static uint32_t pcnet_mmio_readb(void *opaque, target_phys_addr_t addr)
103 661a1799 Paul Brook
{
104 661a1799 Paul Brook
    PCNetState *d = opaque;
105 661a1799 Paul Brook
    uint32_t val = -1;
106 661a1799 Paul Brook
    if (!(addr & 0x10))
107 661a1799 Paul Brook
        val = pcnet_aprom_readb(d, addr & 0x0f);
108 661a1799 Paul Brook
#ifdef PCNET_DEBUG_IO
109 661a1799 Paul Brook
    printf("pcnet_mmio_readb addr=0x" TARGET_FMT_plx " val=0x%02x\n", addr,
110 661a1799 Paul Brook
           val & 0xff);
111 661a1799 Paul Brook
#endif
112 661a1799 Paul Brook
    return val;
113 661a1799 Paul Brook
}
114 661a1799 Paul Brook
115 661a1799 Paul Brook
static void pcnet_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
116 661a1799 Paul Brook
{
117 661a1799 Paul Brook
    PCNetState *d = opaque;
118 661a1799 Paul Brook
#ifdef PCNET_DEBUG_IO
119 661a1799 Paul Brook
    printf("pcnet_mmio_writew addr=0x" TARGET_FMT_plx " val=0x%04x\n", addr,
120 661a1799 Paul Brook
           val);
121 661a1799 Paul Brook
#endif
122 661a1799 Paul Brook
    if (addr & 0x10)
123 661a1799 Paul Brook
        pcnet_ioport_writew(d, addr & 0x0f, val);
124 661a1799 Paul Brook
    else {
125 661a1799 Paul Brook
        addr &= 0x0f;
126 661a1799 Paul Brook
        pcnet_aprom_writeb(d, addr, val & 0xff);
127 661a1799 Paul Brook
        pcnet_aprom_writeb(d, addr+1, (val & 0xff00) >> 8);
128 661a1799 Paul Brook
    }
129 661a1799 Paul Brook
}
130 661a1799 Paul Brook
131 661a1799 Paul Brook
static uint32_t pcnet_mmio_readw(void *opaque, target_phys_addr_t addr)
132 661a1799 Paul Brook
{
133 661a1799 Paul Brook
    PCNetState *d = opaque;
134 661a1799 Paul Brook
    uint32_t val = -1;
135 661a1799 Paul Brook
    if (addr & 0x10)
136 661a1799 Paul Brook
        val = pcnet_ioport_readw(d, addr & 0x0f);
137 661a1799 Paul Brook
    else {
138 661a1799 Paul Brook
        addr &= 0x0f;
139 661a1799 Paul Brook
        val = pcnet_aprom_readb(d, addr+1);
140 661a1799 Paul Brook
        val <<= 8;
141 661a1799 Paul Brook
        val |= pcnet_aprom_readb(d, addr);
142 661a1799 Paul Brook
    }
143 661a1799 Paul Brook
#ifdef PCNET_DEBUG_IO
144 661a1799 Paul Brook
    printf("pcnet_mmio_readw addr=0x" TARGET_FMT_plx" val = 0x%04x\n", addr,
145 661a1799 Paul Brook
           val & 0xffff);
146 661a1799 Paul Brook
#endif
147 661a1799 Paul Brook
    return val;
148 661a1799 Paul Brook
}
149 661a1799 Paul Brook
150 661a1799 Paul Brook
static void pcnet_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
151 661a1799 Paul Brook
{
152 661a1799 Paul Brook
    PCNetState *d = opaque;
153 661a1799 Paul Brook
#ifdef PCNET_DEBUG_IO
154 661a1799 Paul Brook
    printf("pcnet_mmio_writel addr=0x" TARGET_FMT_plx" val=0x%08x\n", addr,
155 661a1799 Paul Brook
           val);
156 661a1799 Paul Brook
#endif
157 661a1799 Paul Brook
    if (addr & 0x10)
158 661a1799 Paul Brook
        pcnet_ioport_writel(d, addr & 0x0f, val);
159 661a1799 Paul Brook
    else {
160 661a1799 Paul Brook
        addr &= 0x0f;
161 661a1799 Paul Brook
        pcnet_aprom_writeb(d, addr, val & 0xff);
162 661a1799 Paul Brook
        pcnet_aprom_writeb(d, addr+1, (val & 0xff00) >> 8);
163 661a1799 Paul Brook
        pcnet_aprom_writeb(d, addr+2, (val & 0xff0000) >> 16);
164 661a1799 Paul Brook
        pcnet_aprom_writeb(d, addr+3, (val & 0xff000000) >> 24);
165 661a1799 Paul Brook
    }
166 661a1799 Paul Brook
}
167 661a1799 Paul Brook
168 661a1799 Paul Brook
static uint32_t pcnet_mmio_readl(void *opaque, target_phys_addr_t addr)
169 661a1799 Paul Brook
{
170 661a1799 Paul Brook
    PCNetState *d = opaque;
171 661a1799 Paul Brook
    uint32_t val;
172 661a1799 Paul Brook
    if (addr & 0x10)
173 661a1799 Paul Brook
        val = pcnet_ioport_readl(d, addr & 0x0f);
174 661a1799 Paul Brook
    else {
175 661a1799 Paul Brook
        addr &= 0x0f;
176 661a1799 Paul Brook
        val = pcnet_aprom_readb(d, addr+3);
177 661a1799 Paul Brook
        val <<= 8;
178 661a1799 Paul Brook
        val |= pcnet_aprom_readb(d, addr+2);
179 661a1799 Paul Brook
        val <<= 8;
180 661a1799 Paul Brook
        val |= pcnet_aprom_readb(d, addr+1);
181 661a1799 Paul Brook
        val <<= 8;
182 661a1799 Paul Brook
        val |= pcnet_aprom_readb(d, addr);
183 661a1799 Paul Brook
    }
184 661a1799 Paul Brook
#ifdef PCNET_DEBUG_IO
185 661a1799 Paul Brook
    printf("pcnet_mmio_readl addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr,
186 661a1799 Paul Brook
           val);
187 661a1799 Paul Brook
#endif
188 661a1799 Paul Brook
    return val;
189 661a1799 Paul Brook
}
190 661a1799 Paul Brook
191 661a1799 Paul Brook
static const VMStateDescription vmstate_pci_pcnet = {
192 661a1799 Paul Brook
    .name = "pcnet",
193 661a1799 Paul Brook
    .version_id = 3,
194 661a1799 Paul Brook
    .minimum_version_id = 2,
195 661a1799 Paul Brook
    .minimum_version_id_old = 2,
196 661a1799 Paul Brook
    .fields      = (VMStateField []) {
197 661a1799 Paul Brook
        VMSTATE_PCI_DEVICE(pci_dev, PCIPCNetState),
198 661a1799 Paul Brook
        VMSTATE_STRUCT(state, PCIPCNetState, 0, vmstate_pcnet, PCNetState),
199 661a1799 Paul Brook
        VMSTATE_END_OF_LIST()
200 661a1799 Paul Brook
    }
201 661a1799 Paul Brook
};
202 661a1799 Paul Brook
203 661a1799 Paul Brook
/* PCI interface */
204 661a1799 Paul Brook
205 661a1799 Paul Brook
static CPUWriteMemoryFunc * const pcnet_mmio_write[] = {
206 661a1799 Paul Brook
    &pcnet_mmio_writeb,
207 661a1799 Paul Brook
    &pcnet_mmio_writew,
208 661a1799 Paul Brook
    &pcnet_mmio_writel
209 661a1799 Paul Brook
};
210 661a1799 Paul Brook
211 661a1799 Paul Brook
static CPUReadMemoryFunc * const pcnet_mmio_read[] = {
212 661a1799 Paul Brook
    &pcnet_mmio_readb,
213 661a1799 Paul Brook
    &pcnet_mmio_readw,
214 661a1799 Paul Brook
    &pcnet_mmio_readl
215 661a1799 Paul Brook
};
216 661a1799 Paul Brook
217 661a1799 Paul Brook
static void pci_physical_memory_write(void *dma_opaque, target_phys_addr_t addr,
218 661a1799 Paul Brook
                                      uint8_t *buf, int len, int do_bswap)
219 661a1799 Paul Brook
{
220 661a1799 Paul Brook
    cpu_physical_memory_write(addr, buf, len);
221 661a1799 Paul Brook
}
222 661a1799 Paul Brook
223 661a1799 Paul Brook
static void pci_physical_memory_read(void *dma_opaque, target_phys_addr_t addr,
224 661a1799 Paul Brook
                                     uint8_t *buf, int len, int do_bswap)
225 661a1799 Paul Brook
{
226 661a1799 Paul Brook
    cpu_physical_memory_read(addr, buf, len);
227 661a1799 Paul Brook
}
228 661a1799 Paul Brook
229 661a1799 Paul Brook
static void pci_pcnet_cleanup(VLANClientState *nc)
230 661a1799 Paul Brook
{
231 661a1799 Paul Brook
    PCNetState *d = DO_UPCAST(NICState, nc, nc)->opaque;
232 661a1799 Paul Brook
233 661a1799 Paul Brook
    pcnet_common_cleanup(d);
234 661a1799 Paul Brook
}
235 661a1799 Paul Brook
236 661a1799 Paul Brook
static int pci_pcnet_uninit(PCIDevice *dev)
237 661a1799 Paul Brook
{
238 661a1799 Paul Brook
    PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev, dev);
239 661a1799 Paul Brook
240 661a1799 Paul Brook
    cpu_unregister_io_memory(d->state.mmio_index);
241 661a1799 Paul Brook
    qemu_del_timer(d->state.poll_timer);
242 661a1799 Paul Brook
    qemu_free_timer(d->state.poll_timer);
243 661a1799 Paul Brook
    qemu_del_vlan_client(&d->state.nic->nc);
244 661a1799 Paul Brook
    return 0;
245 661a1799 Paul Brook
}
246 661a1799 Paul Brook
247 661a1799 Paul Brook
static NetClientInfo net_pci_pcnet_info = {
248 661a1799 Paul Brook
    .type = NET_CLIENT_TYPE_NIC,
249 661a1799 Paul Brook
    .size = sizeof(NICState),
250 661a1799 Paul Brook
    .can_receive = pcnet_can_receive,
251 661a1799 Paul Brook
    .receive = pcnet_receive,
252 661a1799 Paul Brook
    .cleanup = pci_pcnet_cleanup,
253 661a1799 Paul Brook
};
254 661a1799 Paul Brook
255 661a1799 Paul Brook
static int pci_pcnet_init(PCIDevice *pci_dev)
256 661a1799 Paul Brook
{
257 661a1799 Paul Brook
    PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev, pci_dev);
258 661a1799 Paul Brook
    PCNetState *s = &d->state;
259 661a1799 Paul Brook
    uint8_t *pci_conf;
260 661a1799 Paul Brook
261 661a1799 Paul Brook
#if 0
262 661a1799 Paul Brook
    printf("sizeof(RMD)=%d, sizeof(TMD)=%d\n",
263 661a1799 Paul Brook
        sizeof(struct pcnet_RMD), sizeof(struct pcnet_TMD));
264 661a1799 Paul Brook
#endif
265 661a1799 Paul Brook
266 661a1799 Paul Brook
    pci_conf = pci_dev->config;
267 661a1799 Paul Brook
268 661a1799 Paul Brook
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_AMD);
269 661a1799 Paul Brook
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_AMD_LANCE);
270 661a1799 Paul Brook
    pci_set_word(pci_conf + PCI_STATUS,
271 661a1799 Paul Brook
                 PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
272 661a1799 Paul Brook
    pci_conf[PCI_REVISION_ID] = 0x10;
273 661a1799 Paul Brook
    pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
274 661a1799 Paul Brook
275 661a1799 Paul Brook
    pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, 0x0);
276 661a1799 Paul Brook
    pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0);
277 661a1799 Paul Brook
278 661a1799 Paul Brook
    pci_conf[PCI_INTERRUPT_PIN] = 1; // interrupt pin 0
279 661a1799 Paul Brook
    pci_conf[PCI_MIN_GNT] = 0x06;
280 661a1799 Paul Brook
    pci_conf[PCI_MAX_LAT] = 0xff;
281 661a1799 Paul Brook
282 661a1799 Paul Brook
    /* Handler for memory-mapped I/O */
283 661a1799 Paul Brook
    s->mmio_index =
284 2507c12a Alexander Graf
      cpu_register_io_memory(pcnet_mmio_read, pcnet_mmio_write, &d->state,
285 2507c12a Alexander Graf
                             DEVICE_NATIVE_ENDIAN);
286 661a1799 Paul Brook
287 661a1799 Paul Brook
    pci_register_bar(pci_dev, 0, PCNET_IOPORT_SIZE,
288 661a1799 Paul Brook
                           PCI_BASE_ADDRESS_SPACE_IO, pcnet_ioport_map);
289 661a1799 Paul Brook
290 27a41543 Avi Kivity
    pci_register_bar_simple(pci_dev, 1, PCNET_PNPMMIO_SIZE, 0, s->mmio_index);
291 661a1799 Paul Brook
292 661a1799 Paul Brook
    s->irq = pci_dev->irq[0];
293 661a1799 Paul Brook
    s->phys_mem_read = pci_physical_memory_read;
294 661a1799 Paul Brook
    s->phys_mem_write = pci_physical_memory_write;
295 661a1799 Paul Brook
296 661a1799 Paul Brook
    if (!pci_dev->qdev.hotplugged) {
297 661a1799 Paul Brook
        static int loaded = 0;
298 661a1799 Paul Brook
        if (!loaded) {
299 5ee8ad71 Alex Williamson
            rom_add_option("pxe-pcnet.rom", -1);
300 661a1799 Paul Brook
            loaded = 1;
301 661a1799 Paul Brook
        }
302 661a1799 Paul Brook
    }
303 661a1799 Paul Brook
304 661a1799 Paul Brook
    return pcnet_common_init(&pci_dev->qdev, s, &net_pci_pcnet_info);
305 661a1799 Paul Brook
}
306 661a1799 Paul Brook
307 661a1799 Paul Brook
static void pci_reset(DeviceState *dev)
308 661a1799 Paul Brook
{
309 661a1799 Paul Brook
    PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev.qdev, dev);
310 661a1799 Paul Brook
311 661a1799 Paul Brook
    pcnet_h_reset(&d->state);
312 661a1799 Paul Brook
}
313 661a1799 Paul Brook
314 661a1799 Paul Brook
static PCIDeviceInfo pcnet_info = {
315 661a1799 Paul Brook
    .qdev.name  = "pcnet",
316 661a1799 Paul Brook
    .qdev.size  = sizeof(PCIPCNetState),
317 661a1799 Paul Brook
    .qdev.reset = pci_reset,
318 661a1799 Paul Brook
    .qdev.vmsd  = &vmstate_pci_pcnet,
319 661a1799 Paul Brook
    .init       = pci_pcnet_init,
320 661a1799 Paul Brook
    .exit       = pci_pcnet_uninit,
321 661a1799 Paul Brook
    .qdev.props = (Property[]) {
322 661a1799 Paul Brook
        DEFINE_NIC_PROPERTIES(PCIPCNetState, state.conf),
323 661a1799 Paul Brook
        DEFINE_PROP_END_OF_LIST(),
324 661a1799 Paul Brook
    }
325 661a1799 Paul Brook
};
326 661a1799 Paul Brook
327 661a1799 Paul Brook
static void pci_pcnet_register_devices(void)
328 661a1799 Paul Brook
{
329 661a1799 Paul Brook
    pci_qdev_register(&pcnet_info);
330 661a1799 Paul Brook
}
331 661a1799 Paul Brook
332 661a1799 Paul Brook
device_init(pci_pcnet_register_devices)