2 * Emulation of Allwinner EMAC Fast Ethernet controller and
3 * Realtek RTL8201CP PHY
5 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
7 * This model is based on reverse-engineering of Linux kernel driver.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include "qemu/osdep.h"
20 #include "hw/sysbus.h"
22 #include "qemu/fifo8.h"
23 #include "hw/net/allwinner_emac.h"
27 static uint8_t padding
[60];
29 static void mii_set_link(RTL8201CPState
*mii
, bool link_ok
)
32 mii
->bmsr
|= MII_BMSR_LINK_ST
| MII_BMSR_AN_COMP
;
33 mii
->anlpar
|= MII_ANAR_TXFD
| MII_ANAR_10FD
| MII_ANAR_10
|
36 mii
->bmsr
&= ~(MII_BMSR_LINK_ST
| MII_BMSR_AN_COMP
);
37 mii
->anlpar
= MII_ANAR_TX
;
41 static void mii_reset(RTL8201CPState
*mii
, bool link_ok
)
43 mii
->bmcr
= MII_BMCR_FD
| MII_BMCR_AUTOEN
| MII_BMCR_SPEED
;
44 mii
->bmsr
= MII_BMSR_100TX_FD
| MII_BMSR_100TX_HD
| MII_BMSR_10T_FD
|
45 MII_BMSR_10T_HD
| MII_BMSR_MFPS
| MII_BMSR_AUTONEG
;
46 mii
->anar
= MII_ANAR_TXFD
| MII_ANAR_TX
| MII_ANAR_10FD
| MII_ANAR_10
|
48 mii
->anlpar
= MII_ANAR_TX
;
50 mii_set_link(mii
, link_ok
);
53 static uint16_t RTL8201CP_mdio_read(AwEmacState
*s
, uint8_t addr
, uint8_t reg
)
55 RTL8201CPState
*mii
= &s
->mii
;
56 uint16_t ret
= 0xffff;
58 if (addr
== s
->phy_addr
) {
65 return RTL8201CP_PHYID1
;
67 return RTL8201CP_PHYID2
;
78 qemu_log_mask(LOG_UNIMP
,
79 "allwinner_emac: read from unimpl. mii reg 0x%x\n",
83 qemu_log_mask(LOG_GUEST_ERROR
,
84 "allwinner_emac: read from invalid mii reg 0x%x\n",
92 static void RTL8201CP_mdio_write(AwEmacState
*s
, uint8_t addr
, uint8_t reg
,
95 RTL8201CPState
*mii
= &s
->mii
;
98 if (addr
== s
->phy_addr
) {
101 if (value
& MII_BMCR_RESET
) {
102 nc
= qemu_get_queue(s
->nic
);
103 mii_reset(mii
, !nc
->link_down
);
116 qemu_log_mask(LOG_GUEST_ERROR
,
117 "allwinner_emac: write to read-only mii reg 0x%x\n",
125 qemu_log_mask(LOG_UNIMP
,
126 "allwinner_emac: write to unimpl. mii reg 0x%x\n",
130 qemu_log_mask(LOG_GUEST_ERROR
,
131 "allwinner_emac: write to invalid mii reg 0x%x\n",
137 static void aw_emac_update_irq(AwEmacState
*s
)
139 qemu_set_irq(s
->irq
, (s
->int_sta
& s
->int_ctl
) != 0);
142 static void aw_emac_tx_reset(AwEmacState
*s
, int chan
)
144 fifo8_reset(&s
->tx_fifo
[chan
]);
145 s
->tx_length
[chan
] = 0;
148 static void aw_emac_rx_reset(AwEmacState
*s
)
150 fifo8_reset(&s
->rx_fifo
);
151 s
->rx_num_packets
= 0;
152 s
->rx_packet_size
= 0;
153 s
->rx_packet_pos
= 0;
156 static void fifo8_push_word(Fifo8
*fifo
, uint32_t val
)
158 fifo8_push(fifo
, val
);
159 fifo8_push(fifo
, val
>> 8);
160 fifo8_push(fifo
, val
>> 16);
161 fifo8_push(fifo
, val
>> 24);
164 static uint32_t fifo8_pop_word(Fifo8
*fifo
)
168 ret
= fifo8_pop(fifo
);
169 ret
|= fifo8_pop(fifo
) << 8;
170 ret
|= fifo8_pop(fifo
) << 16;
171 ret
|= fifo8_pop(fifo
) << 24;
176 static int aw_emac_can_receive(NetClientState
*nc
)
178 AwEmacState
*s
= qemu_get_nic_opaque(nc
);
181 * To avoid packet drops, allow reception only when there is space
182 * for a full frame: 1522 + 8 (rx headers) + 2 (padding).
184 return (s
->ctl
& EMAC_CTL_RX_EN
) && (fifo8_num_free(&s
->rx_fifo
) >= 1532);
187 static ssize_t
aw_emac_receive(NetClientState
*nc
, const uint8_t *buf
,
190 AwEmacState
*s
= qemu_get_nic_opaque(nc
);
191 Fifo8
*fifo
= &s
->rx_fifo
;
192 size_t padded_size
, total_size
;
195 padded_size
= size
> 60 ? size
: 60;
196 total_size
= QEMU_ALIGN_UP(RX_HDR_SIZE
+ padded_size
+ CRC_SIZE
, 4);
198 if (!(s
->ctl
& EMAC_CTL_RX_EN
) || (fifo8_num_free(fifo
) < total_size
)) {
202 fifo8_push_word(fifo
, EMAC_UNDOCUMENTED_MAGIC
);
203 fifo8_push_word(fifo
, EMAC_RX_HEADER(padded_size
+ CRC_SIZE
,
204 EMAC_RX_IO_DATA_STATUS_OK
));
205 fifo8_push_all(fifo
, buf
, size
);
206 crc
= crc32(~0, buf
, size
);
208 if (padded_size
!= size
) {
209 fifo8_push_all(fifo
, padding
, padded_size
- size
);
210 crc
= crc32(crc
, padding
, padded_size
- size
);
213 fifo8_push_word(fifo
, crc
);
214 fifo8_push_all(fifo
, padding
, QEMU_ALIGN_UP(padded_size
, 4) - padded_size
);
217 s
->int_sta
|= EMAC_INT_RX
;
218 aw_emac_update_irq(s
);
223 static void aw_emac_reset(DeviceState
*dev
)
225 AwEmacState
*s
= AW_EMAC(dev
);
226 NetClientState
*nc
= qemu_get_queue(s
->nic
);
235 aw_emac_tx_reset(s
, 0);
236 aw_emac_tx_reset(s
, 1);
239 mii_reset(&s
->mii
, !nc
->link_down
);
242 static uint64_t aw_emac_read(void *opaque
, hwaddr offset
, unsigned size
)
244 AwEmacState
*s
= opaque
;
245 Fifo8
*fifo
= &s
->rx_fifo
;
252 case EMAC_TX_MODE_REG
:
254 case EMAC_TX_INS_REG
:
255 return s
->tx_channel
;
256 case EMAC_RX_CTL_REG
:
258 case EMAC_RX_IO_DATA_REG
:
259 if (!s
->rx_num_packets
) {
260 qemu_log_mask(LOG_GUEST_ERROR
,
261 "Read IO data register when no packet available");
265 ret
= fifo8_pop_word(fifo
);
267 switch (s
->rx_packet_pos
) {
268 case 0: /* Word is magic header */
269 s
->rx_packet_pos
+= 4;
271 case 4: /* Word is rx info header */
272 s
->rx_packet_pos
+= 4;
273 s
->rx_packet_size
= QEMU_ALIGN_UP(extract32(ret
, 0, 16), 4);
275 default: /* Word is packet data */
276 s
->rx_packet_pos
+= 4;
277 s
->rx_packet_size
-= 4;
279 if (!s
->rx_packet_size
) {
280 s
->rx_packet_pos
= 0;
282 nc
= qemu_get_queue(s
->nic
);
283 if (aw_emac_can_receive(nc
)) {
284 qemu_flush_queued_packets(nc
);
289 case EMAC_RX_FBC_REG
:
290 return s
->rx_num_packets
;
291 case EMAC_INT_CTL_REG
:
293 case EMAC_INT_STA_REG
:
295 case EMAC_MAC_MRDD_REG
:
296 return RTL8201CP_mdio_read(s
,
297 extract32(s
->phy_target
, PHY_ADDR_SHIFT
, 8),
298 extract32(s
->phy_target
, PHY_REG_SHIFT
, 8));
300 qemu_log_mask(LOG_UNIMP
,
301 "allwinner_emac: read access to unknown register 0x"
302 TARGET_FMT_plx
"\n", offset
);
309 static void aw_emac_write(void *opaque
, hwaddr offset
, uint64_t value
,
312 AwEmacState
*s
= opaque
;
314 NetClientState
*nc
= qemu_get_queue(s
->nic
);
319 if (value
& EMAC_CTL_RESET
) {
320 aw_emac_reset(DEVICE(s
));
321 value
&= ~EMAC_CTL_RESET
;
324 if (aw_emac_can_receive(nc
)) {
325 qemu_flush_queued_packets(nc
);
328 case EMAC_TX_MODE_REG
:
331 case EMAC_TX_CTL0_REG
:
332 case EMAC_TX_CTL1_REG
:
333 chan
= (offset
== EMAC_TX_CTL0_REG
? 0 : 1);
334 if ((value
& 1) && (s
->ctl
& EMAC_CTL_TX_EN
)) {
338 fifo
= &s
->tx_fifo
[chan
];
339 len
= s
->tx_length
[chan
];
341 if (len
> fifo8_num_used(fifo
)) {
342 len
= fifo8_num_used(fifo
);
343 qemu_log_mask(LOG_GUEST_ERROR
,
344 "allwinner_emac: TX length > fifo data length\n");
347 data
= fifo8_pop_buf(fifo
, len
, &ret
);
348 qemu_send_packet(nc
, data
, ret
);
349 aw_emac_tx_reset(s
, chan
);
350 /* Raise TX interrupt */
351 s
->int_sta
|= EMAC_INT_TX_CHAN(chan
);
352 aw_emac_update_irq(s
);
356 case EMAC_TX_INS_REG
:
357 s
->tx_channel
= value
< NUM_TX_FIFOS
? value
: 0;
359 case EMAC_TX_PL0_REG
:
360 case EMAC_TX_PL1_REG
:
361 chan
= (offset
== EMAC_TX_PL0_REG
? 0 : 1);
362 if (value
> TX_FIFO_SIZE
) {
363 qemu_log_mask(LOG_GUEST_ERROR
,
364 "allwinner_emac: invalid TX frame length %d\n",
366 value
= TX_FIFO_SIZE
;
368 s
->tx_length
[chan
] = value
;
370 case EMAC_TX_IO_DATA_REG
:
371 fifo
= &s
->tx_fifo
[s
->tx_channel
];
372 if (fifo8_num_free(fifo
) < 4) {
373 qemu_log_mask(LOG_GUEST_ERROR
,
374 "allwinner_emac: TX data overruns fifo\n");
377 fifo8_push_word(fifo
, value
);
379 case EMAC_RX_CTL_REG
:
382 case EMAC_RX_FBC_REG
:
387 case EMAC_INT_CTL_REG
:
389 aw_emac_update_irq(s
);
391 case EMAC_INT_STA_REG
:
392 s
->int_sta
&= ~value
;
393 aw_emac_update_irq(s
);
395 case EMAC_MAC_MADR_REG
:
396 s
->phy_target
= value
;
398 case EMAC_MAC_MWTD_REG
:
399 RTL8201CP_mdio_write(s
, extract32(s
->phy_target
, PHY_ADDR_SHIFT
, 8),
400 extract32(s
->phy_target
, PHY_REG_SHIFT
, 8), value
);
403 qemu_log_mask(LOG_UNIMP
,
404 "allwinner_emac: write access to unknown register 0x"
405 TARGET_FMT_plx
"\n", offset
);
409 static void aw_emac_set_link(NetClientState
*nc
)
411 AwEmacState
*s
= qemu_get_nic_opaque(nc
);
413 mii_set_link(&s
->mii
, !nc
->link_down
);
416 static const MemoryRegionOps aw_emac_mem_ops
= {
417 .read
= aw_emac_read
,
418 .write
= aw_emac_write
,
419 .endianness
= DEVICE_NATIVE_ENDIAN
,
421 .min_access_size
= 4,
422 .max_access_size
= 4,
426 static NetClientInfo net_aw_emac_info
= {
427 .type
= NET_CLIENT_DRIVER_NIC
,
428 .size
= sizeof(NICState
),
429 .can_receive
= aw_emac_can_receive
,
430 .receive
= aw_emac_receive
,
431 .link_status_changed
= aw_emac_set_link
,
434 static void aw_emac_init(Object
*obj
)
436 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
437 AwEmacState
*s
= AW_EMAC(obj
);
439 memory_region_init_io(&s
->iomem
, OBJECT(s
), &aw_emac_mem_ops
, s
,
441 sysbus_init_mmio(sbd
, &s
->iomem
);
442 sysbus_init_irq(sbd
, &s
->irq
);
445 static void aw_emac_realize(DeviceState
*dev
, Error
**errp
)
447 AwEmacState
*s
= AW_EMAC(dev
);
449 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
450 s
->nic
= qemu_new_nic(&net_aw_emac_info
, &s
->conf
,
451 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
452 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
454 fifo8_create(&s
->rx_fifo
, RX_FIFO_SIZE
);
455 fifo8_create(&s
->tx_fifo
[0], TX_FIFO_SIZE
);
456 fifo8_create(&s
->tx_fifo
[1], TX_FIFO_SIZE
);
459 static Property aw_emac_properties
[] = {
460 DEFINE_NIC_PROPERTIES(AwEmacState
, conf
),
461 DEFINE_PROP_UINT8("phy-addr", AwEmacState
, phy_addr
, 0),
462 DEFINE_PROP_END_OF_LIST(),
465 static const VMStateDescription vmstate_mii
= {
468 .minimum_version_id
= 1,
469 .fields
= (VMStateField
[]) {
470 VMSTATE_UINT16(bmcr
, RTL8201CPState
),
471 VMSTATE_UINT16(bmsr
, RTL8201CPState
),
472 VMSTATE_UINT16(anar
, RTL8201CPState
),
473 VMSTATE_UINT16(anlpar
, RTL8201CPState
),
474 VMSTATE_END_OF_LIST()
478 static int aw_emac_post_load(void *opaque
, int version_id
)
480 AwEmacState
*s
= opaque
;
482 aw_emac_set_link(qemu_get_queue(s
->nic
));
487 static const VMStateDescription vmstate_aw_emac
= {
488 .name
= "allwinner_emac",
490 .minimum_version_id
= 1,
491 .post_load
= aw_emac_post_load
,
492 .fields
= (VMStateField
[]) {
493 VMSTATE_STRUCT(mii
, AwEmacState
, 1, vmstate_mii
, RTL8201CPState
),
494 VMSTATE_UINT32(ctl
, AwEmacState
),
495 VMSTATE_UINT32(tx_mode
, AwEmacState
),
496 VMSTATE_UINT32(rx_ctl
, AwEmacState
),
497 VMSTATE_UINT32(int_ctl
, AwEmacState
),
498 VMSTATE_UINT32(int_sta
, AwEmacState
),
499 VMSTATE_UINT32(phy_target
, AwEmacState
),
500 VMSTATE_FIFO8(rx_fifo
, AwEmacState
),
501 VMSTATE_UINT32(rx_num_packets
, AwEmacState
),
502 VMSTATE_UINT32(rx_packet_size
, AwEmacState
),
503 VMSTATE_UINT32(rx_packet_pos
, AwEmacState
),
504 VMSTATE_STRUCT_ARRAY(tx_fifo
, AwEmacState
, NUM_TX_FIFOS
, 1,
505 vmstate_fifo8
, Fifo8
),
506 VMSTATE_UINT32_ARRAY(tx_length
, AwEmacState
, NUM_TX_FIFOS
),
507 VMSTATE_UINT32(tx_channel
, AwEmacState
),
508 VMSTATE_END_OF_LIST()
512 static void aw_emac_class_init(ObjectClass
*klass
, void *data
)
514 DeviceClass
*dc
= DEVICE_CLASS(klass
);
516 dc
->realize
= aw_emac_realize
;
517 dc
->props
= aw_emac_properties
;
518 dc
->reset
= aw_emac_reset
;
519 dc
->vmsd
= &vmstate_aw_emac
;
522 static const TypeInfo aw_emac_info
= {
523 .name
= TYPE_AW_EMAC
,
524 .parent
= TYPE_SYS_BUS_DEVICE
,
525 .instance_size
= sizeof(AwEmacState
),
526 .instance_init
= aw_emac_init
,
527 .class_init
= aw_emac_class_init
,
530 static void aw_emac_register_types(void)
532 type_register_static(&aw_emac_info
);
535 type_init(aw_emac_register_types
)