xref: /openbmc/qemu/hw/net/lasi_i82596.c (revision de15df5e)
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 "net/eth.h"
18 #include "hw/net/lasi_82596.h"
19 #include "hw/net/i82596.h"
20 #include "trace.h"
21 #include "sysemu/sysemu.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 = (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,
122                   hwaddr hpa, qemu_irq lan_irq)
123 {
124     DeviceState *dev;
125     SysBusI82596State *s;
126     static const MACAddr HP_MAC = {
127         .a = { 0x08, 0x00, 0x09, 0xef, 0x34, 0xf6 } };
128 
129     qemu_check_nic_model(&nd_table[0], TYPE_LASI_82596);
130     dev = qdev_new(TYPE_LASI_82596);
131     s = SYSBUS_I82596(dev);
132     s->state.irq = lan_irq;
133     qdev_set_nic_properties(dev, &nd_table[0]);
134     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
135     s->state.conf.macaddr = HP_MAC; /* set HP MAC prefix */
136 
137     /* LASI 82596 ports in main memory. */
138     memory_region_add_subregion(addr_space, hpa, &s->state.mmio);
139     return s;
140 }
141 
142 static void lasi_82596_reset(DeviceState *dev)
143 {
144     SysBusI82596State *d = SYSBUS_I82596(dev);
145 
146     i82596_h_reset(&d->state);
147 }
148 
149 static void lasi_82596_instance_init(Object *obj)
150 {
151     SysBusI82596State *d = SYSBUS_I82596(obj);
152     I82596State *s = &d->state;
153 
154     device_add_bootindex_property(obj, &s->conf.bootindex,
155                                   "bootindex", "/ethernet-phy@0",
156                                   DEVICE(obj));
157 }
158 
159 static Property lasi_82596_properties[] = {
160     DEFINE_NIC_PROPERTIES(SysBusI82596State, state.conf),
161     DEFINE_PROP_END_OF_LIST(),
162 };
163 
164 static void lasi_82596_class_init(ObjectClass *klass, void *data)
165 {
166     DeviceClass *dc = DEVICE_CLASS(klass);
167 
168     dc->realize = lasi_82596_realize;
169     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
170     dc->fw_name = "ethernet";
171     dc->reset = lasi_82596_reset;
172     dc->vmsd = &vmstate_lasi_82596;
173     dc->user_creatable = false;
174     device_class_set_props(dc, lasi_82596_properties);
175 }
176 
177 static const TypeInfo lasi_82596_info = {
178     .name          = TYPE_LASI_82596,
179     .parent        = TYPE_SYS_BUS_DEVICE,
180     .instance_size = sizeof(SysBusI82596State),
181     .class_init    = lasi_82596_class_init,
182     .instance_init = lasi_82596_instance_init,
183 };
184 
185 static void lasi_82596_register_types(void)
186 {
187     type_register_static(&lasi_82596_info);
188 }
189 
190 type_init(lasi_82596_register_types)
191