xref: /openbmc/qemu/hw/net/lasi_i82596.c (revision ee48fef0)
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/sysbus.h"
17 #include "sysemu/sysemu.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/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 };
90 
91 static NetClientInfo net_lasi_82596_info = {
92     .type = NET_CLIENT_DRIVER_NIC,
93     .size = sizeof(NICState),
94     .can_receive = i82596_can_receive,
95     .receive = i82596_receive,
96     .link_status_changed = i82596_set_link_status,
97 };
98 
99 static const VMStateDescription vmstate_lasi_82596 = {
100     .name = "i82596",
101     .version_id = 1,
102     .minimum_version_id = 1,
103     .fields = (const VMStateField[]) {
104         VMSTATE_STRUCT(state, SysBusI82596State, 0, vmstate_i82596,
105                 I82596State),
106         VMSTATE_END_OF_LIST()
107     }
108 };
109 
110 static void lasi_82596_realize(DeviceState *dev, Error **errp)
111 {
112     SysBusI82596State *d = SYSBUS_I82596(dev);
113     I82596State *s = &d->state;
114 
115     memory_region_init_io(&s->mmio, OBJECT(d), &lasi_82596_mem_ops, d,
116                 "lasi_82596-mmio", PA_GET_MACADDR + 4);
117 
118     i82596_common_init(dev, s, &net_lasi_82596_info);
119 }
120 
121 SysBusI82596State *lasi_82596_init(MemoryRegion *addr_space, hwaddr hpa,
122                                    qemu_irq lan_irq, gboolean match_default)
123 {
124     DeviceState *dev;
125     SysBusI82596State *s;
126     static const MACAddr HP_MAC = {
127         .a = { 0x08, 0x00, 0x09, 0xef, 0x34, 0xf6 } };
128 
129     dev = qemu_create_nic_device(TYPE_LASI_82596, match_default, "lasi");
130     if (!dev) {
131         return NULL;
132     }
133 
134     s = SYSBUS_I82596(dev);
135     s->state.irq = lan_irq;
136     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
137     s->state.conf.macaddr = HP_MAC; /* set HP MAC prefix */
138 
139     /* LASI 82596 ports in main memory. */
140     memory_region_add_subregion(addr_space, hpa, &s->state.mmio);
141     return s;
142 }
143 
144 static void lasi_82596_reset(DeviceState *dev)
145 {
146     SysBusI82596State *d = SYSBUS_I82596(dev);
147 
148     i82596_h_reset(&d->state);
149 }
150 
151 static void lasi_82596_instance_init(Object *obj)
152 {
153     SysBusI82596State *d = SYSBUS_I82596(obj);
154     I82596State *s = &d->state;
155 
156     device_add_bootindex_property(obj, &s->conf.bootindex,
157                                   "bootindex", "/ethernet-phy@0",
158                                   DEVICE(obj));
159 }
160 
161 static Property lasi_82596_properties[] = {
162     DEFINE_NIC_PROPERTIES(SysBusI82596State, state.conf),
163     DEFINE_PROP_END_OF_LIST(),
164 };
165 
166 static void lasi_82596_class_init(ObjectClass *klass, void *data)
167 {
168     DeviceClass *dc = DEVICE_CLASS(klass);
169 
170     dc->realize = lasi_82596_realize;
171     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
172     dc->fw_name = "ethernet";
173     dc->reset = lasi_82596_reset;
174     dc->vmsd = &vmstate_lasi_82596;
175     dc->user_creatable = false;
176     device_class_set_props(dc, lasi_82596_properties);
177 }
178 
179 static const TypeInfo lasi_82596_info = {
180     .name          = TYPE_LASI_82596,
181     .parent        = TYPE_SYS_BUS_DEVICE,
182     .instance_size = sizeof(SysBusI82596State),
183     .class_init    = lasi_82596_class_init,
184     .instance_init = lasi_82596_instance_init,
185 };
186 
187 static void lasi_82596_register_types(void)
188 {
189     type_register_static(&lasi_82596_info);
190 }
191 
192 type_init(lasi_82596_register_types)
193