Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU AMD PC-Net II (Am79C970A) 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 | |
| 30 | /* |
| 31 | * On Sparc32, this is the Lance (Am7990) part of chip STP2000 (Master I/O), also |
| 32 | * produced as NCR89C100. See |
| 33 | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt |
| 34 | * and |
| 35 | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR92C990.txt |
| 36 | */ |
| 37 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 38 | #include "hw/sysbus.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 39 | #include "net/net.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 40 | #include "qemu/timer.h" |
| 41 | #include "qemu/sockets.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 42 | #include "hw/sparc/sun4m.h" |
Paolo Bonzini | 47b43a1 | 2013-03-18 17:36:02 +0100 | [diff] [blame] | 43 | #include "pcnet.h" |
Blue Swirl | 97bf485 | 2010-10-31 09:24:14 +0000 | [diff] [blame] | 44 | #include "trace.h" |
Gonglei | ea3b351 | 2014-10-07 16:00:16 +0800 | [diff] [blame] | 45 | #include "sysemu/sysemu.h" |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 46 | |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 47 | #define TYPE_LANCE "lance" |
| 48 | #define SYSBUS_PCNET(obj) \ |
| 49 | OBJECT_CHECK(SysBusPCNetState, (obj), TYPE_LANCE) |
| 50 | |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 51 | typedef struct { |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 52 | SysBusDevice parent_obj; |
| 53 | |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 54 | PCNetState state; |
| 55 | } SysBusPCNetState; |
| 56 | |
| 57 | static void parent_lance_reset(void *opaque, int irq, int level) |
| 58 | { |
| 59 | SysBusPCNetState *d = opaque; |
| 60 | if (level) |
| 61 | pcnet_h_reset(&d->state); |
| 62 | } |
| 63 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 64 | static void lance_mem_write(void *opaque, hwaddr addr, |
Avi Kivity | bd8d6f7 | 2011-08-08 16:09:19 +0300 | [diff] [blame] | 65 | uint64_t val, unsigned size) |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 66 | { |
| 67 | SysBusPCNetState *d = opaque; |
Blue Swirl | 97bf485 | 2010-10-31 09:24:14 +0000 | [diff] [blame] | 68 | |
| 69 | trace_lance_mem_writew(addr, val & 0xffff); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 70 | pcnet_ioport_writew(&d->state, addr, val & 0xffff); |
| 71 | } |
| 72 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 73 | static uint64_t lance_mem_read(void *opaque, hwaddr addr, |
Avi Kivity | bd8d6f7 | 2011-08-08 16:09:19 +0300 | [diff] [blame] | 74 | unsigned size) |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 75 | { |
| 76 | SysBusPCNetState *d = opaque; |
| 77 | uint32_t val; |
| 78 | |
| 79 | val = pcnet_ioport_readw(&d->state, addr); |
Blue Swirl | 97bf485 | 2010-10-31 09:24:14 +0000 | [diff] [blame] | 80 | trace_lance_mem_readw(addr, val & 0xffff); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 81 | return val & 0xffff; |
| 82 | } |
| 83 | |
Avi Kivity | bd8d6f7 | 2011-08-08 16:09:19 +0300 | [diff] [blame] | 84 | static const MemoryRegionOps lance_mem_ops = { |
| 85 | .read = lance_mem_read, |
| 86 | .write = lance_mem_write, |
| 87 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 88 | .valid = { |
| 89 | .min_access_size = 2, |
| 90 | .max_access_size = 2, |
| 91 | }, |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 94 | static void lance_cleanup(NetClientState *nc) |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 95 | { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 96 | PCNetState *d = qemu_get_nic_opaque(nc); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 97 | |
| 98 | pcnet_common_cleanup(d); |
| 99 | } |
| 100 | |
Mark McLoughlin | 1fa5148 | 2009-11-25 18:49:15 +0000 | [diff] [blame] | 101 | static NetClientInfo net_lance_info = { |
Laszlo Ersek | 2be64a6 | 2012-07-17 16:17:12 +0200 | [diff] [blame] | 102 | .type = NET_CLIENT_OPTIONS_KIND_NIC, |
Mark McLoughlin | 1fa5148 | 2009-11-25 18:49:15 +0000 | [diff] [blame] | 103 | .size = sizeof(NICState), |
| 104 | .can_receive = pcnet_can_receive, |
| 105 | .receive = pcnet_receive, |
Jan Kiszka | e1c2008 | 2011-10-07 12:27:25 +0200 | [diff] [blame] | 106 | .link_status_changed = pcnet_set_link_status, |
Mark McLoughlin | 1fa5148 | 2009-11-25 18:49:15 +0000 | [diff] [blame] | 107 | .cleanup = lance_cleanup, |
| 108 | }; |
| 109 | |
Juan Quintela | be73cfe | 2009-12-02 12:36:46 +0100 | [diff] [blame] | 110 | static const VMStateDescription vmstate_lance = { |
| 111 | .name = "pcnet", |
| 112 | .version_id = 3, |
| 113 | .minimum_version_id = 2, |
Juan Quintela | 35d0845 | 2014-04-16 16:01:33 +0200 | [diff] [blame] | 114 | .fields = (VMStateField[]) { |
Juan Quintela | be73cfe | 2009-12-02 12:36:46 +0100 | [diff] [blame] | 115 | VMSTATE_STRUCT(state, SysBusPCNetState, 0, vmstate_pcnet, PCNetState), |
| 116 | VMSTATE_END_OF_LIST() |
| 117 | } |
| 118 | }; |
| 119 | |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 120 | static int lance_init(SysBusDevice *sbd) |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 121 | { |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 122 | DeviceState *dev = DEVICE(sbd); |
| 123 | SysBusPCNetState *d = SYSBUS_PCNET(dev); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 124 | PCNetState *s = &d->state; |
| 125 | |
Paolo Bonzini | eedfac6 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 126 | memory_region_init_io(&s->mmio, OBJECT(d), &lance_mem_ops, d, |
| 127 | "lance-mmio", 4); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 128 | |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 129 | qdev_init_gpio_in(dev, parent_lance_reset, 1); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 130 | |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 131 | sysbus_init_mmio(sbd, &s->mmio); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 132 | |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 133 | sysbus_init_irq(sbd, &s->irq); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 134 | |
| 135 | s->phys_mem_read = ledma_memory_read; |
| 136 | s->phys_mem_write = ledma_memory_write; |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 137 | return pcnet_common_init(dev, s, &net_lance_info); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void lance_reset(DeviceState *dev) |
| 141 | { |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 142 | SysBusPCNetState *d = SYSBUS_PCNET(dev); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 143 | |
| 144 | pcnet_h_reset(&d->state); |
| 145 | } |
| 146 | |
Gonglei | ea3b351 | 2014-10-07 16:00:16 +0800 | [diff] [blame] | 147 | static void lance_instance_init(Object *obj) |
| 148 | { |
| 149 | SysBusPCNetState *d = SYSBUS_PCNET(obj); |
| 150 | PCNetState *s = &d->state; |
| 151 | |
| 152 | device_add_bootindex_property(obj, &s->conf.bootindex, |
| 153 | "bootindex", "/ethernet-phy@0", |
| 154 | DEVICE(obj), NULL); |
| 155 | } |
| 156 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 157 | static Property lance_properties[] = { |
| 158 | DEFINE_PROP_PTR("dma", SysBusPCNetState, state.dma_opaque), |
| 159 | DEFINE_NIC_PROPERTIES(SysBusPCNetState, state.conf), |
| 160 | DEFINE_PROP_END_OF_LIST(), |
| 161 | }; |
| 162 | |
| 163 | static void lance_class_init(ObjectClass *klass, void *data) |
| 164 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 165 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 166 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
| 167 | |
| 168 | k->init = lance_init; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 169 | set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 170 | dc->fw_name = "ethernet"; |
| 171 | dc->reset = lance_reset; |
| 172 | dc->vmsd = &vmstate_lance; |
| 173 | dc->props = lance_properties; |
Markus Armbruster | 1b111dc | 2013-11-29 10:43:44 +0100 | [diff] [blame] | 174 | /* Reason: pointer property "dma" */ |
| 175 | dc->cannot_instantiate_with_device_add_yet = true; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 176 | } |
| 177 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 178 | static const TypeInfo lance_info = { |
Andreas Färber | b1a2aaf | 2013-07-27 12:08:14 +0200 | [diff] [blame] | 179 | .name = TYPE_LANCE, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 180 | .parent = TYPE_SYS_BUS_DEVICE, |
| 181 | .instance_size = sizeof(SysBusPCNetState), |
| 182 | .class_init = lance_class_init, |
Gonglei | ea3b351 | 2014-10-07 16:00:16 +0800 | [diff] [blame] | 183 | .instance_init = lance_instance_init, |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 186 | static void lance_register_types(void) |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 187 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 188 | type_register_static(&lance_info); |
Gerd Hoffmann | 94e1a91 | 2009-10-21 15:25:33 +0200 | [diff] [blame] | 189 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 190 | |
| 191 | type_init(lance_register_types) |