xref: /openbmc/qemu/hw/net/lasi_i82596.c (revision 3c6ef471ee67bf5a22a9e0ecfdc45ca7d2393216)
1376b8519SHelge Deller /*
2376b8519SHelge Deller  * QEMU LASI NIC i82596 emulation
3376b8519SHelge Deller  *
4376b8519SHelge Deller  * Copyright (c) 2019 Helge Deller <deller@gmx.de>
5376b8519SHelge Deller  * This work is licensed under the GNU GPL license version 2 or later.
6376b8519SHelge Deller  *
7376b8519SHelge Deller  *
8376b8519SHelge Deller  * On PA-RISC, this is the Network part of LASI chip.
9376b8519SHelge Deller  * See:
10376b8519SHelge Deller  * https://parisc.wiki.kernel.org/images-parisc/7/79/Lasi_ers.pdf
11376b8519SHelge Deller  */
12376b8519SHelge Deller 
13376b8519SHelge Deller #include "qemu/osdep.h"
143e80f690SMarkus Armbruster #include "qapi/error.h"
15376b8519SHelge Deller #include "qemu/timer.h"
16376b8519SHelge Deller #include "hw/sysbus.h"
17376b8519SHelge Deller #include "net/eth.h"
18376b8519SHelge Deller #include "hw/net/lasi_82596.h"
19376b8519SHelge Deller #include "hw/net/i82596.h"
20376b8519SHelge Deller #include "trace.h"
21376b8519SHelge Deller #include "sysemu/sysemu.h"
22376b8519SHelge Deller #include "hw/qdev-properties.h"
23376b8519SHelge Deller #include "migration/vmstate.h"
24376b8519SHelge Deller 
25376b8519SHelge Deller #define PA_I82596_RESET         0       /* Offsets relative to LASI-LAN-Addr.*/
26376b8519SHelge Deller #define PA_CPU_PORT_L_ACCESS    4
27376b8519SHelge Deller #define PA_CHANNEL_ATTENTION    8
28376b8519SHelge Deller #define PA_GET_MACADDR          12
29376b8519SHelge Deller 
30376b8519SHelge Deller #define SWAP32(x)   (((uint32_t)(x) << 16) | ((((uint32_t)(x))) >> 16))
31376b8519SHelge Deller 
32376b8519SHelge Deller static void lasi_82596_mem_write(void *opaque, hwaddr addr,
33376b8519SHelge Deller                             uint64_t val, unsigned size)
34376b8519SHelge Deller {
35376b8519SHelge Deller     SysBusI82596State *d = opaque;
36376b8519SHelge Deller 
37376b8519SHelge Deller     trace_lasi_82596_mem_writew(addr, val);
38376b8519SHelge Deller     switch (addr) {
39376b8519SHelge Deller     case PA_I82596_RESET:
40376b8519SHelge Deller         i82596_h_reset(&d->state);
41376b8519SHelge Deller         break;
42376b8519SHelge Deller     case PA_CPU_PORT_L_ACCESS:
43376b8519SHelge Deller         d->val_index++;
44376b8519SHelge Deller         if (d->val_index == 0) {
45376b8519SHelge Deller             uint32_t v = d->last_val | (val << 16);
46376b8519SHelge Deller             v = v & ~0xff;
47376b8519SHelge Deller             i82596_ioport_writew(&d->state, d->last_val & 0xff, v);
48376b8519SHelge Deller         }
49376b8519SHelge Deller         d->last_val = val;
50376b8519SHelge Deller         break;
51376b8519SHelge Deller     case PA_CHANNEL_ATTENTION:
52376b8519SHelge Deller         i82596_ioport_writew(&d->state, PORT_CA, val);
53376b8519SHelge Deller         break;
54376b8519SHelge Deller     case PA_GET_MACADDR:
55376b8519SHelge Deller         /*
56376b8519SHelge Deller          * Provided for SeaBIOS only. Write MAC of Network card to addr @val.
57376b8519SHelge Deller          * Needed for the PDC_LAN_STATION_ID_READ PDC call.
58376b8519SHelge Deller          */
5919f70347SPeter Maydell         address_space_write(&address_space_memory, val,
6019f70347SPeter Maydell                             MEMTXATTRS_UNSPECIFIED, d->state.conf.macaddr.a,
6119f70347SPeter Maydell                             ETH_ALEN);
62376b8519SHelge Deller         break;
63376b8519SHelge Deller     }
64376b8519SHelge Deller }
65376b8519SHelge Deller 
66376b8519SHelge Deller static uint64_t lasi_82596_mem_read(void *opaque, hwaddr addr,
67376b8519SHelge Deller                                unsigned size)
68376b8519SHelge Deller {
69376b8519SHelge Deller     SysBusI82596State *d = opaque;
70376b8519SHelge Deller     uint32_t val;
71376b8519SHelge Deller 
72376b8519SHelge Deller     if (addr == PA_GET_MACADDR) {
73376b8519SHelge Deller         val = 0xBEEFBABE;
74376b8519SHelge Deller     } else {
75376b8519SHelge Deller         val = i82596_ioport_readw(&d->state, addr);
76376b8519SHelge Deller     }
77376b8519SHelge Deller     trace_lasi_82596_mem_readw(addr, val);
78376b8519SHelge Deller     return val;
79376b8519SHelge Deller }
80376b8519SHelge Deller 
81376b8519SHelge Deller static const MemoryRegionOps lasi_82596_mem_ops = {
82376b8519SHelge Deller     .read = lasi_82596_mem_read,
83376b8519SHelge Deller     .write = lasi_82596_mem_write,
84376b8519SHelge Deller     .endianness = DEVICE_BIG_ENDIAN,
85376b8519SHelge Deller     .valid = {
86376b8519SHelge Deller         .min_access_size = 4,
87376b8519SHelge Deller         .max_access_size = 4,
88376b8519SHelge Deller     },
89376b8519SHelge Deller };
90376b8519SHelge Deller 
91376b8519SHelge Deller static NetClientInfo net_lasi_82596_info = {
92376b8519SHelge Deller     .type = NET_CLIENT_DRIVER_NIC,
93376b8519SHelge Deller     .size = sizeof(NICState),
94376b8519SHelge Deller     .can_receive = i82596_can_receive,
95376b8519SHelge Deller     .receive = i82596_receive,
96376b8519SHelge Deller     .link_status_changed = i82596_set_link_status,
97376b8519SHelge Deller };
98376b8519SHelge Deller 
99376b8519SHelge Deller static const VMStateDescription vmstate_lasi_82596 = {
100376b8519SHelge Deller     .name = "i82596",
101376b8519SHelge Deller     .version_id = 1,
102376b8519SHelge Deller     .minimum_version_id = 1,
103376b8519SHelge Deller     .fields = (VMStateField[]) {
104376b8519SHelge Deller         VMSTATE_STRUCT(state, SysBusI82596State, 0, vmstate_i82596,
105376b8519SHelge Deller                 I82596State),
106376b8519SHelge Deller         VMSTATE_END_OF_LIST()
107376b8519SHelge Deller     }
108376b8519SHelge Deller };
109376b8519SHelge Deller 
110376b8519SHelge Deller static void lasi_82596_realize(DeviceState *dev, Error **errp)
111376b8519SHelge Deller {
112376b8519SHelge Deller     SysBusI82596State *d = SYSBUS_I82596(dev);
113376b8519SHelge Deller     I82596State *s = &d->state;
114376b8519SHelge Deller 
115376b8519SHelge Deller     memory_region_init_io(&s->mmio, OBJECT(d), &lasi_82596_mem_ops, d,
116376b8519SHelge Deller                 "lasi_82596-mmio", PA_GET_MACADDR + 4);
117376b8519SHelge Deller 
118376b8519SHelge Deller     i82596_common_init(dev, s, &net_lasi_82596_info);
119376b8519SHelge Deller }
120376b8519SHelge Deller 
121376b8519SHelge Deller SysBusI82596State *lasi_82596_init(MemoryRegion *addr_space,
122376b8519SHelge Deller                   hwaddr hpa, qemu_irq lan_irq)
123376b8519SHelge Deller {
124376b8519SHelge Deller     DeviceState *dev;
125376b8519SHelge Deller     SysBusI82596State *s;
126376b8519SHelge Deller     static const MACAddr HP_MAC = {
127376b8519SHelge Deller         .a = { 0x08, 0x00, 0x09, 0xef, 0x34, 0xf6 } };
128376b8519SHelge Deller 
129376b8519SHelge Deller     qemu_check_nic_model(&nd_table[0], TYPE_LASI_82596);
1303e80f690SMarkus Armbruster     dev = qdev_new(TYPE_LASI_82596);
131376b8519SHelge Deller     s = SYSBUS_I82596(dev);
132376b8519SHelge Deller     s->state.irq = lan_irq;
133376b8519SHelge Deller     qdev_set_nic_properties(dev, &nd_table[0]);
134*3c6ef471SMarkus Armbruster     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
135376b8519SHelge Deller     s->state.conf.macaddr = HP_MAC; /* set HP MAC prefix */
136376b8519SHelge Deller 
137376b8519SHelge Deller     /* LASI 82596 ports in main memory. */
138376b8519SHelge Deller     memory_region_add_subregion(addr_space, hpa, &s->state.mmio);
139376b8519SHelge Deller     return s;
140376b8519SHelge Deller }
141376b8519SHelge Deller 
142376b8519SHelge Deller static void lasi_82596_reset(DeviceState *dev)
143376b8519SHelge Deller {
144376b8519SHelge Deller     SysBusI82596State *d = SYSBUS_I82596(dev);
145376b8519SHelge Deller 
146376b8519SHelge Deller     i82596_h_reset(&d->state);
147376b8519SHelge Deller }
148376b8519SHelge Deller 
149376b8519SHelge Deller static void lasi_82596_instance_init(Object *obj)
150376b8519SHelge Deller {
151376b8519SHelge Deller     SysBusI82596State *d = SYSBUS_I82596(obj);
152376b8519SHelge Deller     I82596State *s = &d->state;
153376b8519SHelge Deller 
154376b8519SHelge Deller     device_add_bootindex_property(obj, &s->conf.bootindex,
155376b8519SHelge Deller                                   "bootindex", "/ethernet-phy@0",
15640c2281cSMarkus Armbruster                                   DEVICE(obj));
157376b8519SHelge Deller }
158376b8519SHelge Deller 
159376b8519SHelge Deller static Property lasi_82596_properties[] = {
160376b8519SHelge Deller     DEFINE_NIC_PROPERTIES(SysBusI82596State, state.conf),
161376b8519SHelge Deller     DEFINE_PROP_END_OF_LIST(),
162376b8519SHelge Deller };
163376b8519SHelge Deller 
164376b8519SHelge Deller static void lasi_82596_class_init(ObjectClass *klass, void *data)
165376b8519SHelge Deller {
166376b8519SHelge Deller     DeviceClass *dc = DEVICE_CLASS(klass);
167376b8519SHelge Deller 
168376b8519SHelge Deller     dc->realize = lasi_82596_realize;
169376b8519SHelge Deller     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
170376b8519SHelge Deller     dc->fw_name = "ethernet";
171376b8519SHelge Deller     dc->reset = lasi_82596_reset;
172376b8519SHelge Deller     dc->vmsd = &vmstate_lasi_82596;
173376b8519SHelge Deller     dc->user_creatable = false;
174376b8519SHelge Deller     device_class_set_props(dc, lasi_82596_properties);
175376b8519SHelge Deller }
176376b8519SHelge Deller 
177376b8519SHelge Deller static const TypeInfo lasi_82596_info = {
178376b8519SHelge Deller     .name          = TYPE_LASI_82596,
179376b8519SHelge Deller     .parent        = TYPE_SYS_BUS_DEVICE,
180376b8519SHelge Deller     .instance_size = sizeof(SysBusI82596State),
181376b8519SHelge Deller     .class_init    = lasi_82596_class_init,
182376b8519SHelge Deller     .instance_init = lasi_82596_instance_init,
183376b8519SHelge Deller };
184376b8519SHelge Deller 
185376b8519SHelge Deller static void lasi_82596_register_types(void)
186376b8519SHelge Deller {
187376b8519SHelge Deller     type_register_static(&lasi_82596_info);
188376b8519SHelge Deller }
189376b8519SHelge Deller 
190376b8519SHelge Deller type_init(lasi_82596_register_types)
191