2 * QEMU LASI NIC i82596 emulation
4 * Copyright (c) 2019 Helge Deller <deller@gmx.de>
5 * This work is licensed under the GNU GPL license version 2 or later.
8 * On PA-RISC, this is the Network part of LASI chip.
10 * https://parisc.wiki.kernel.org/images-parisc/7/79/Lasi_ers.pdf
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/timer.h"
16 #include "hw/sysbus.h"
18 #include "hw/net/lasi_82596.h"
19 #include "hw/net/i82596.h"
21 #include "sysemu/sysemu.h"
22 #include "hw/qdev-properties.h"
23 #include "migration/vmstate.h"
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
30 #define SWAP32(x) (((uint32_t)(x) << 16) | ((((uint32_t)(x))) >> 16))
32 static void lasi_82596_mem_write(void *opaque
, hwaddr addr
,
33 uint64_t val
, unsigned size
)
35 SysBusI82596State
*d
= opaque
;
37 trace_lasi_82596_mem_writew(addr
, val
);
40 i82596_h_reset(&d
->state
);
42 case PA_CPU_PORT_L_ACCESS
:
44 if (d
->val_index
== 0) {
45 uint32_t v
= d
->last_val
| (val
<< 16);
47 i82596_ioport_writew(&d
->state
, d
->last_val
& 0xff, v
);
51 case PA_CHANNEL_ATTENTION
:
52 i82596_ioport_writew(&d
->state
, PORT_CA
, val
);
56 * Provided for SeaBIOS only. Write MAC of Network card to addr @val.
57 * Needed for the PDC_LAN_STATION_ID_READ PDC call.
59 address_space_write(&address_space_memory
, val
,
60 MEMTXATTRS_UNSPECIFIED
, d
->state
.conf
.macaddr
.a
,
66 static uint64_t lasi_82596_mem_read(void *opaque
, hwaddr addr
,
69 SysBusI82596State
*d
= opaque
;
72 if (addr
== PA_GET_MACADDR
) {
75 val
= i82596_ioport_readw(&d
->state
, addr
);
77 trace_lasi_82596_mem_readw(addr
, val
);
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
,
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
,
99 static const VMStateDescription vmstate_lasi_82596
= {
102 .minimum_version_id
= 1,
103 .fields
= (VMStateField
[]) {
104 VMSTATE_STRUCT(state
, SysBusI82596State
, 0, vmstate_i82596
,
106 VMSTATE_END_OF_LIST()
110 static void lasi_82596_realize(DeviceState
*dev
, Error
**errp
)
112 SysBusI82596State
*d
= SYSBUS_I82596(dev
);
113 I82596State
*s
= &d
->state
;
115 memory_region_init_io(&s
->mmio
, OBJECT(d
), &lasi_82596_mem_ops
, d
,
116 "lasi_82596-mmio", PA_GET_MACADDR
+ 4);
118 i82596_common_init(dev
, s
, &net_lasi_82596_info
);
121 SysBusI82596State
*lasi_82596_init(MemoryRegion
*addr_space
,
122 hwaddr hpa
, qemu_irq lan_irq
)
125 SysBusI82596State
*s
;
126 static const MACAddr HP_MAC
= {
127 .a
= { 0x08, 0x00, 0x09, 0xef, 0x34, 0xf6 } };
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 */
137 /* LASI 82596 ports in main memory. */
138 memory_region_add_subregion(addr_space
, hpa
, &s
->state
.mmio
);
142 static void lasi_82596_reset(DeviceState
*dev
)
144 SysBusI82596State
*d
= SYSBUS_I82596(dev
);
146 i82596_h_reset(&d
->state
);
149 static void lasi_82596_instance_init(Object
*obj
)
151 SysBusI82596State
*d
= SYSBUS_I82596(obj
);
152 I82596State
*s
= &d
->state
;
154 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
155 "bootindex", "/ethernet-phy@0",
159 static Property lasi_82596_properties
[] = {
160 DEFINE_NIC_PROPERTIES(SysBusI82596State
, state
.conf
),
161 DEFINE_PROP_END_OF_LIST(),
164 static void lasi_82596_class_init(ObjectClass
*klass
, void *data
)
166 DeviceClass
*dc
= DEVICE_CLASS(klass
);
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
);
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
,
185 static void lasi_82596_register_types(void)
187 type_register_static(&lasi_82596_info
);
190 type_init(lasi_82596_register_types
)