xref: /openbmc/qemu/hw/net/lasi_i82596.c (revision 88cd34ee)
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 "qemu/timer.h"
15 #include "hw/sysbus.h"
16 #include "net/eth.h"
17 #include "hw/net/lasi_82596.h"
18 #include "hw/net/i82596.h"
19 #include "trace.h"
20 #include "sysemu/sysemu.h"
21 #include "hw/qdev-properties.h"
22 #include "migration/vmstate.h"
23 
24 #define PA_I82596_RESET         0       /* Offsets relative to LASI-LAN-Addr.*/
25 #define PA_CPU_PORT_L_ACCESS    4
26 #define PA_CHANNEL_ATTENTION    8
27 #define PA_GET_MACADDR          12
28 
29 #define SWAP32(x)   (((uint32_t)(x) << 16) | ((((uint32_t)(x))) >> 16))
30 
31 static void lasi_82596_mem_write(void *opaque, hwaddr addr,
32                             uint64_t val, unsigned size)
33 {
34     SysBusI82596State *d = opaque;
35 
36     trace_lasi_82596_mem_writew(addr, val);
37     switch (addr) {
38     case PA_I82596_RESET:
39         i82596_h_reset(&d->state);
40         break;
41     case PA_CPU_PORT_L_ACCESS:
42         d->val_index++;
43         if (d->val_index == 0) {
44             uint32_t v = d->last_val | (val << 16);
45             v = v & ~0xff;
46             i82596_ioport_writew(&d->state, d->last_val & 0xff, v);
47         }
48         d->last_val = val;
49         break;
50     case PA_CHANNEL_ATTENTION:
51         i82596_ioport_writew(&d->state, PORT_CA, val);
52         break;
53     case PA_GET_MACADDR:
54         /*
55          * Provided for SeaBIOS only. Write MAC of Network card to addr @val.
56          * Needed for the PDC_LAN_STATION_ID_READ PDC call.
57          */
58         address_space_rw(&address_space_memory, val,
59             MEMTXATTRS_UNSPECIFIED, d->state.conf.macaddr.a, ETH_ALEN, 1);
60         break;
61     }
62 }
63 
64 static uint64_t lasi_82596_mem_read(void *opaque, hwaddr addr,
65                                unsigned size)
66 {
67     SysBusI82596State *d = opaque;
68     uint32_t val;
69 
70     if (addr == PA_GET_MACADDR) {
71         val = 0xBEEFBABE;
72     } else {
73         val = i82596_ioport_readw(&d->state, addr);
74     }
75     trace_lasi_82596_mem_readw(addr, val);
76     return val;
77 }
78 
79 static const MemoryRegionOps lasi_82596_mem_ops = {
80     .read = lasi_82596_mem_read,
81     .write = lasi_82596_mem_write,
82     .endianness = DEVICE_BIG_ENDIAN,
83     .valid = {
84         .min_access_size = 4,
85         .max_access_size = 4,
86     },
87 };
88 
89 static NetClientInfo net_lasi_82596_info = {
90     .type = NET_CLIENT_DRIVER_NIC,
91     .size = sizeof(NICState),
92     .can_receive = i82596_can_receive,
93     .receive = i82596_receive,
94     .link_status_changed = i82596_set_link_status,
95 };
96 
97 static const VMStateDescription vmstate_lasi_82596 = {
98     .name = "i82596",
99     .version_id = 1,
100     .minimum_version_id = 1,
101     .fields = (VMStateField[]) {
102         VMSTATE_STRUCT(state, SysBusI82596State, 0, vmstate_i82596,
103                 I82596State),
104         VMSTATE_END_OF_LIST()
105     }
106 };
107 
108 static void lasi_82596_realize(DeviceState *dev, Error **errp)
109 {
110     SysBusI82596State *d = SYSBUS_I82596(dev);
111     I82596State *s = &d->state;
112 
113     memory_region_init_io(&s->mmio, OBJECT(d), &lasi_82596_mem_ops, d,
114                 "lasi_82596-mmio", PA_GET_MACADDR + 4);
115 
116     i82596_common_init(dev, s, &net_lasi_82596_info);
117 }
118 
119 SysBusI82596State *lasi_82596_init(MemoryRegion *addr_space,
120                   hwaddr hpa, qemu_irq lan_irq)
121 {
122     DeviceState *dev;
123     SysBusI82596State *s;
124     static const MACAddr HP_MAC = {
125         .a = { 0x08, 0x00, 0x09, 0xef, 0x34, 0xf6 } };
126 
127     qemu_check_nic_model(&nd_table[0], TYPE_LASI_82596);
128     dev = qdev_create(NULL, TYPE_LASI_82596);
129     s = SYSBUS_I82596(dev);
130     s->state.irq = lan_irq;
131     qdev_set_nic_properties(dev, &nd_table[0]);
132     qdev_init_nofail(dev);
133     s->state.conf.macaddr = HP_MAC; /* set HP MAC prefix */
134 
135     /* LASI 82596 ports in main memory. */
136     memory_region_add_subregion(addr_space, hpa, &s->state.mmio);
137     return s;
138 }
139 
140 static void lasi_82596_reset(DeviceState *dev)
141 {
142     SysBusI82596State *d = SYSBUS_I82596(dev);
143 
144     i82596_h_reset(&d->state);
145 }
146 
147 static void lasi_82596_instance_init(Object *obj)
148 {
149     SysBusI82596State *d = SYSBUS_I82596(obj);
150     I82596State *s = &d->state;
151 
152     device_add_bootindex_property(obj, &s->conf.bootindex,
153                                   "bootindex", "/ethernet-phy@0",
154                                   DEVICE(obj), NULL);
155 }
156 
157 static Property lasi_82596_properties[] = {
158     DEFINE_NIC_PROPERTIES(SysBusI82596State, state.conf),
159     DEFINE_PROP_END_OF_LIST(),
160 };
161 
162 static void lasi_82596_class_init(ObjectClass *klass, void *data)
163 {
164     DeviceClass *dc = DEVICE_CLASS(klass);
165 
166     dc->realize = lasi_82596_realize;
167     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
168     dc->fw_name = "ethernet";
169     dc->reset = lasi_82596_reset;
170     dc->vmsd = &vmstate_lasi_82596;
171     dc->user_creatable = false;
172     device_class_set_props(dc, lasi_82596_properties);
173 }
174 
175 static const TypeInfo lasi_82596_info = {
176     .name          = TYPE_LASI_82596,
177     .parent        = TYPE_SYS_BUS_DEVICE,
178     .instance_size = sizeof(SysBusI82596State),
179     .class_init    = lasi_82596_class_init,
180     .instance_init = lasi_82596_instance_init,
181 };
182 
183 static void lasi_82596_register_types(void)
184 {
185     type_register_static(&lasi_82596_info);
186 }
187 
188 type_init(lasi_82596_register_types)
189