master
c 181 lines 5.01 KB
Raw
1 /*
2 * QEMU LASI NIC i82596 emulation
3 *
4 * Copyright (c) 2019 Helge Deller <deller@gmx.de>
5 * This work is licensed under the GNU GPL license version 2 or later.
6 *
7 *
8 * On PA-RISC, this is the Network part of LASI chip.
9 * See:
10 * https://parisc.wiki.kernel.org/images-parisc/7/79/Lasi_ers.pdf
11 */
12
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/timer.h"
16 #include "hw/core/sysbus.h"
17 #include "system/system.h"
18 #include "net/eth.h"
19 #include "hw/net/lasi_82596.h"
20 #include "hw/net/i82596.h"
21 #include "trace.h"
22 #include "hw/core/qdev-properties.h"
23 #include "migration/vmstate.h"
24
25 #define PA_I82596_RESET 0 /* Offsets relative to LASI-LAN-Addr.*/
26 #define PA_CPU_PORT_L_ACCESS 4
27 #define PA_CHANNEL_ATTENTION 8
28 #define PA_GET_MACADDR 12
29
30 #define SWAP32(x) (((uint32_t)(x) << 16) | ((((uint32_t)(x))) >> 16))
31
32 static void lasi_82596_mem_write(void *opaque, hwaddr addr,
33 uint64_t val, unsigned size)
34 {
35 SysBusI82596State *d = opaque;
36
37 trace_lasi_82596_mem_writew(addr, val);
38 switch (addr) {
39 case PA_I82596_RESET:
40 i82596_h_reset(&d->state);
41 break;
42 case PA_CPU_PORT_L_ACCESS:
43 d->val_index++;
44 if (d->val_index == 0) {
45 uint32_t v = d->last_val | (val << 16);
46 v = v & ~0xff;
47 i82596_ioport_writew(&d->state, d->last_val & 0xff, v);
48 }
49 d->last_val = val;
50 break;
51 case PA_CHANNEL_ATTENTION:
52 i82596_ioport_writew(&d->state, PORT_CA, val);
53 break;
54 case PA_GET_MACADDR:
55 /*
56 * Provided for SeaBIOS only. Write MAC of Network card to addr @val.
57 * Needed for the PDC_LAN_STATION_ID_READ PDC call.
58 */
59 address_space_write(&address_space_memory, val,
60 MEMTXATTRS_UNSPECIFIED, d->state.conf.macaddr.a,
61 ETH_ALEN);
62 break;
63 }
64 }
65
66 static uint64_t lasi_82596_mem_read(void *opaque, hwaddr addr,
67 unsigned size)
68 {
69 SysBusI82596State *d = opaque;
70 uint32_t val;
71
72 if (addr == PA_GET_MACADDR) {
73 val = 0xBEEFBABE;
74 } else {
75 val = i82596_ioport_readw(&d->state, addr);
76 }
77 trace_lasi_82596_mem_readw(addr, val);
78 return val;
79 }
80
81 static const MemoryRegionOps lasi_82596_mem_ops = {
82 .read = lasi_82596_mem_read,
83 .write = lasi_82596_mem_write,
84 .endianness = DEVICE_BIG_ENDIAN,
85 .valid = {
86 .min_access_size = 4,
87 .max_access_size = 4,
88 },
89 .impl = {
90 .min_access_size = 4,
91 .max_access_size = 4,
92 },
93 };
94
95 static NetClientInfo net_lasi_82596_info = {
96 .type = NET_CLIENT_DRIVER_NIC,
97 .size = sizeof(NICState),
98 .can_receive = i82596_can_receive,
99 .receive = i82596_receive,
100 .receive_iov = i82596_receive_iov,
101 .poll = i82596_poll,
102 .link_status_changed = i82596_set_link_status,
103 };
104
105 static const VMStateDescription vmstate_lasi_82596 = {
106 .name = "i82596",
107 .version_id = 1,
108 .minimum_version_id = 1,
109 .fields = (const VMStateField[]) {
110 VMSTATE_STRUCT(state, SysBusI82596State, 0, vmstate_i82596,
111 I82596State),
112 VMSTATE_END_OF_LIST()
113 }
114 };
115
116 static void lasi_82596_realize(DeviceState *dev, Error **errp)
117 {
118 SysBusI82596State *d = SYSBUS_I82596(dev);
119 I82596State *s = &d->state;
120
121 memory_region_init_io(&s->mmio, OBJECT(d), &lasi_82596_mem_ops, d,
122 "lasi_82596-mmio", PA_GET_MACADDR + 4);
123
124 sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
125 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio);
126
127 i82596_common_init(dev, s, &net_lasi_82596_info);
128 }
129
130 static void lasi_82596_reset(DeviceState *dev)
131 {
132 SysBusI82596State *d = SYSBUS_I82596(dev);
133
134 i82596_h_reset(&d->state);
135 }
136
137 static void lasi_82596_instance_init(Object *obj)
138 {
139 SysBusI82596State *d = SYSBUS_I82596(obj);
140 I82596State *s = &d->state;
141 static const MACAddr HP_MAC = {
142 .a = { 0x08, 0x00, 0x09, 0xef, 0x34, 0xf6 } };
143
144 s->conf.macaddr = HP_MAC;
145
146 device_add_bootindex_property(obj, &s->conf.bootindex,
147 "bootindex", "/ethernet-phy@0",
148 DEVICE(obj));
149 }
150
151 static const Property lasi_82596_properties[] = {
152 DEFINE_NIC_PROPERTIES(SysBusI82596State, state.conf),
153 };
154
155 static void lasi_82596_class_init(ObjectClass *klass, const void *data)
156 {
157 DeviceClass *dc = DEVICE_CLASS(klass);
158
159 dc->realize = lasi_82596_realize;
160 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
161 dc->fw_name = "ethernet";
162 device_class_set_legacy_reset(dc, lasi_82596_reset);
163 dc->vmsd = &vmstate_lasi_82596;
164 dc->user_creatable = false;
165 device_class_set_props(dc, lasi_82596_properties);
166 }
167
168 static const TypeInfo lasi_82596_info = {
169 .name = TYPE_LASI_82596,
170 .parent = TYPE_SYS_BUS_DEVICE,
171 .instance_size = sizeof(SysBusI82596State),
172 .class_init = lasi_82596_class_init,
173 .instance_init = lasi_82596_instance_init,
174 };
175
176 static void lasi_82596_register_types(void)
177 {
178 type_register_static(&lasi_82596_info);
179 }
180
181 type_init(lasi_82596_register_types)