2 * QEMU model of Xilinx AXI-Ethernet.
4 * Copyright (c) 2011 Edgar E. Iglesias.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-char.h"
29 #include "net/checksum.h"
31 #include "xilinx_axidma.h"
35 /* Advertisement control register. */
36 #define ADVERTISE_10HALF 0x0020 /* Try for 10mbps half-duplex */
37 #define ADVERTISE_10FULL 0x0040 /* Try for 10mbps full-duplex */
38 #define ADVERTISE_100HALF 0x0080 /* Try for 100mbps half-duplex */
39 #define ADVERTISE_100FULL 0x0100 /* Try for 100mbps full-duplex */
46 unsigned int (*read
)(struct PHY
*phy
, unsigned int req
);
47 void (*write
)(struct PHY
*phy
, unsigned int req
,
51 static unsigned int tdk_read(struct PHY
*phy
, unsigned int req
)
64 /* Speeds and modes. */
65 r
|= (1 << 13) | (1 << 14);
66 r
|= (1 << 11) | (1 << 12);
67 r
|= (1 << 5); /* Autoneg complete. */
68 r
|= (1 << 3); /* Autoneg able. */
69 r
|= (1 << 2); /* link. */
70 r
|= (1 << 1); /* link. */
73 /* Link partner ability.
74 We are kind; always agree with whatever best mode
75 the guest advertises. */
76 r
= 1 << 14; /* Success. */
77 /* Copy advertised modes. */
78 r
|= phy
->regs
[4] & (15 << 5);
79 /* Autoneg support. */
83 /* Marvel PHY on many xilinx boards. */
84 r
= 0x8000; /* 1000Mb */
88 /* Diagnostics reg. */
96 /* Are we advertising 100 half or 100 duplex ? */
97 speed_100
= !!(phy
->regs
[4] & ADVERTISE_100HALF
);
98 speed_100
|= !!(phy
->regs
[4] & ADVERTISE_100FULL
);
100 /* Are we advertising 10 duplex or 100 duplex ? */
101 duplex
= !!(phy
->regs
[4] & ADVERTISE_100FULL
);
102 duplex
|= !!(phy
->regs
[4] & ADVERTISE_10FULL
);
103 r
= (speed_100
<< 10) | (duplex
<< 11);
108 r
= phy
->regs
[regnum
];
111 DPHY(qemu_log("\n%s %x = reg[%d]\n", __func__
, r
, regnum
));
116 tdk_write(struct PHY
*phy
, unsigned int req
, unsigned int data
)
121 DPHY(qemu_log("%s reg[%d] = %x\n", __func__
, regnum
, data
));
124 phy
->regs
[regnum
] = data
;
130 tdk_init(struct PHY
*phy
)
132 phy
->regs
[0] = 0x3100;
134 phy
->regs
[2] = 0x0300;
135 phy
->regs
[3] = 0xe400;
136 /* Autonegotiation advertisement reg. */
137 phy
->regs
[4] = 0x01E1;
140 phy
->read
= tdk_read
;
141 phy
->write
= tdk_write
;
167 struct PHY
*devs
[32];
171 mdio_attach(struct MDIOBus
*bus
, struct PHY
*phy
, unsigned int addr
)
173 bus
->devs
[addr
& 0x1f] = phy
;
176 #ifdef USE_THIS_DEAD_CODE
178 mdio_detach(struct MDIOBus
*bus
, struct PHY
*phy
, unsigned int addr
)
180 bus
->devs
[addr
& 0x1f] = NULL
;
184 static uint16_t mdio_read_req(struct MDIOBus
*bus
, unsigned int addr
,
190 phy
= bus
->devs
[addr
];
191 if (phy
&& phy
->read
) {
192 data
= phy
->read(phy
, reg
);
196 DPHY(qemu_log("%s addr=%d reg=%d data=%x\n", __func__
, addr
, reg
, data
));
200 static void mdio_write_req(struct MDIOBus
*bus
, unsigned int addr
,
201 unsigned int reg
, uint16_t data
)
205 DPHY(qemu_log("%s addr=%d reg=%d data=%x\n", __func__
, addr
, reg
, data
));
206 phy
= bus
->devs
[addr
];
207 if (phy
&& phy
->write
) {
208 phy
->write(phy
, reg
, data
);
214 #define R_RAF (0x000 / 4)
216 RAF_MCAST_REJ
= (1 << 1),
217 RAF_BCAST_REJ
= (1 << 2),
218 RAF_EMCF_EN
= (1 << 12),
219 RAF_NEWFUNC_EN
= (1 << 11)
222 #define R_IS (0x00C / 4)
224 IS_HARD_ACCESS_COMPLETE
= 1,
225 IS_AUTONEG
= (1 << 1),
226 IS_RX_COMPLETE
= (1 << 2),
227 IS_RX_REJECT
= (1 << 3),
228 IS_TX_COMPLETE
= (1 << 5),
229 IS_RX_DCM_LOCK
= (1 << 6),
230 IS_MGM_RDY
= (1 << 7),
231 IS_PHY_RST_DONE
= (1 << 8),
234 #define R_IP (0x010 / 4)
235 #define R_IE (0x014 / 4)
236 #define R_UAWL (0x020 / 4)
237 #define R_UAWU (0x024 / 4)
238 #define R_PPST (0x030 / 4)
240 PPST_LINKSTATUS
= (1 << 0),
241 PPST_PHY_LINKSTATUS
= (1 << 7),
244 #define R_STATS_RX_BYTESL (0x200 / 4)
245 #define R_STATS_RX_BYTESH (0x204 / 4)
246 #define R_STATS_TX_BYTESL (0x208 / 4)
247 #define R_STATS_TX_BYTESH (0x20C / 4)
248 #define R_STATS_RXL (0x290 / 4)
249 #define R_STATS_RXH (0x294 / 4)
250 #define R_STATS_RX_BCASTL (0x2a0 / 4)
251 #define R_STATS_RX_BCASTH (0x2a4 / 4)
252 #define R_STATS_RX_MCASTL (0x2a8 / 4)
253 #define R_STATS_RX_MCASTH (0x2ac / 4)
255 #define R_RCW0 (0x400 / 4)
256 #define R_RCW1 (0x404 / 4)
258 RCW1_VLAN
= (1 << 27),
260 RCW1_FCS
= (1 << 29),
261 RCW1_JUM
= (1 << 30),
262 RCW1_RST
= (1 << 31),
265 #define R_TC (0x408 / 4)
274 #define R_EMMC (0x410 / 4)
276 EMMC_LINKSPEED_10MB
= (0 << 30),
277 EMMC_LINKSPEED_100MB
= (1 << 30),
278 EMMC_LINKSPEED_1000MB
= (2 << 30),
281 #define R_PHYC (0x414 / 4)
283 #define R_MC (0x500 / 4)
284 #define MC_EN (1 << 6)
286 #define R_MCR (0x504 / 4)
287 #define R_MWD (0x508 / 4)
288 #define R_MRD (0x50c / 4)
289 #define R_MIS (0x600 / 4)
290 #define R_MIP (0x620 / 4)
291 #define R_MIE (0x640 / 4)
292 #define R_MIC (0x640 / 4)
294 #define R_UAW0 (0x700 / 4)
295 #define R_UAW1 (0x704 / 4)
296 #define R_FMI (0x708 / 4)
297 #define R_AF0 (0x710 / 4)
298 #define R_AF1 (0x714 / 4)
299 #define R_MAX (0x34 / 4)
301 /* Indirect registers. */
303 struct MDIOBus mdio_bus
;
309 struct XilinxAXIEnet
{
343 /* Receive configuration words. */
345 /* Transmit config. */
350 /* Unicast Address Word. */
352 /* Unicast address filter used with extended mcast. */
356 uint32_t regs
[R_MAX
];
358 /* Multicast filter addrs. */
359 uint32_t maddr
[4][2];
360 /* 32K x 1 lookup filter. */
361 uint32_t ext_mtable
[1024];
367 static void axienet_rx_reset(struct XilinxAXIEnet
*s
)
369 s
->rcw
[1] = RCW1_JUM
| RCW1_FCS
| RCW1_RX
| RCW1_VLAN
;
372 static void axienet_tx_reset(struct XilinxAXIEnet
*s
)
374 s
->tc
= TC_JUM
| TC_TX
| TC_VLAN
;
377 static inline int axienet_rx_resetting(struct XilinxAXIEnet
*s
)
379 return s
->rcw
[1] & RCW1_RST
;
382 static inline int axienet_rx_enabled(struct XilinxAXIEnet
*s
)
384 return s
->rcw
[1] & RCW1_RX
;
387 static inline int axienet_extmcf_enabled(struct XilinxAXIEnet
*s
)
389 return !!(s
->regs
[R_RAF
] & RAF_EMCF_EN
);
392 static inline int axienet_newfunc_enabled(struct XilinxAXIEnet
*s
)
394 return !!(s
->regs
[R_RAF
] & RAF_NEWFUNC_EN
);
397 static void axienet_reset(struct XilinxAXIEnet
*s
)
402 s
->regs
[R_PPST
] = PPST_LINKSTATUS
| PPST_PHY_LINKSTATUS
;
403 s
->regs
[R_IS
] = IS_AUTONEG
| IS_RX_DCM_LOCK
| IS_MGM_RDY
| IS_PHY_RST_DONE
;
405 s
->emmc
= EMMC_LINKSPEED_100MB
;
408 static void enet_update_irq(struct XilinxAXIEnet
*s
)
410 s
->regs
[R_IP
] = s
->regs
[R_IS
] & s
->regs
[R_IE
];
411 qemu_set_irq(s
->irq
, !!s
->regs
[R_IP
]);
414 static uint32_t enet_readl(void *opaque
, target_phys_addr_t addr
)
416 struct XilinxAXIEnet
*s
= opaque
;
423 r
= s
->rcw
[addr
& 1];
439 r
= s
->mii
.regs
[addr
& 3] | (1 << 7); /* Always ready. */
442 case R_STATS_RX_BYTESL
:
443 case R_STATS_RX_BYTESH
:
444 r
= s
->stats
.rx_bytes
>> (32 * (addr
& 1));
447 case R_STATS_TX_BYTESL
:
448 case R_STATS_TX_BYTESH
:
449 r
= s
->stats
.tx_bytes
>> (32 * (addr
& 1));
454 r
= s
->stats
.rx
>> (32 * (addr
& 1));
456 case R_STATS_RX_BCASTL
:
457 case R_STATS_RX_BCASTH
:
458 r
= s
->stats
.rx_bcast
>> (32 * (addr
& 1));
460 case R_STATS_RX_MCASTL
:
461 case R_STATS_RX_MCASTH
:
462 r
= s
->stats
.rx_mcast
>> (32 * (addr
& 1));
468 r
= s
->mii
.regs
[addr
& 3];
473 r
= s
->uaw
[addr
& 1];
478 r
= s
->ext_uaw
[addr
& 1];
487 r
= s
->maddr
[s
->fmi
& 3][addr
& 1];
490 case 0x8000 ... 0x83ff:
491 r
= s
->ext_mtable
[addr
- 0x8000];
495 if (addr
< ARRAY_SIZE(s
->regs
)) {
498 DENET(qemu_log("%s addr=" TARGET_FMT_plx
" v=%x\n",
499 __func__
, addr
* 4, r
));
506 enet_writel(void *opaque
, target_phys_addr_t addr
, uint32_t value
)
508 struct XilinxAXIEnet
*s
= opaque
;
509 struct TEMAC
*t
= &s
->TEMAC
;
515 s
->rcw
[addr
& 1] = value
;
516 if ((addr
& 1) && value
& RCW1_RST
) {
523 if (value
& TC_RST
) {
537 value
&= ((1 < 7) - 1);
539 /* Enable the MII. */
541 unsigned int miiclkdiv
= value
& ((1 << 6) - 1);
543 qemu_log("AXIENET: MDIO enabled but MDIOCLK is zero!\n");
550 unsigned int phyaddr
= (value
>> 24) & 0x1f;
551 unsigned int regaddr
= (value
>> 16) & 0x1f;
552 unsigned int op
= (value
>> 14) & 3;
553 unsigned int initiate
= (value
>> 11) & 1;
557 mdio_write_req(&t
->mdio_bus
, phyaddr
, regaddr
, s
->mii
.mwd
);
558 } else if (op
== 2) {
559 s
->mii
.mrd
= mdio_read_req(&t
->mdio_bus
, phyaddr
, regaddr
);
561 qemu_log("AXIENET: invalid MDIOBus OP=%d\n", op
);
570 s
->mii
.regs
[addr
& 3] = value
;
576 s
->uaw
[addr
& 1] = value
;
581 s
->ext_uaw
[addr
& 1] = value
;
590 s
->maddr
[s
->fmi
& 3][addr
& 1] = value
;
593 case 0x8000 ... 0x83ff:
594 s
->ext_mtable
[addr
- 0x8000] = value
;
598 DENET(qemu_log("%s addr=" TARGET_FMT_plx
" v=%x\n",
599 __func__
, addr
* 4, value
));
600 if (addr
< ARRAY_SIZE(s
->regs
)) {
601 s
->regs
[addr
] = value
;
608 static CPUReadMemoryFunc
* const enet_read
[] = {
614 static CPUWriteMemoryFunc
* const enet_write
[] = {
620 static int eth_can_rx(VLANClientState
*nc
)
622 struct XilinxAXIEnet
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
625 return !axienet_rx_resetting(s
) && axienet_rx_enabled(s
);
628 static int enet_match_addr(const uint8_t *buf
, uint32_t f0
, uint32_t f1
)
632 if (memcmp(buf
, &f0
, 4)) {
636 if (buf
[4] != (f1
& 0xff) || buf
[5] != ((f1
>> 8) & 0xff)) {
643 static ssize_t
eth_rx(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
645 struct XilinxAXIEnet
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
646 static const unsigned char sa_bcast
[6] = {0xff, 0xff, 0xff,
648 static const unsigned char sa_ipmcast
[3] = {0x01, 0x00, 0x52};
649 uint32_t app
[6] = {0};
650 int promisc
= s
->fmi
& (1 << 31);
651 int unicast
, broadcast
, multicast
, ip_multicast
= 0;
657 DENET(qemu_log("%s: %zd bytes\n", __func__
, size
));
659 unicast
= ~buf
[0] & 0x1;
660 broadcast
= memcmp(buf
, sa_bcast
, 6) == 0;
661 multicast
= !unicast
&& !broadcast
;
662 if (multicast
&& (memcmp(sa_ipmcast
, buf
, sizeof sa_ipmcast
) == 0)) {
666 /* Jumbo or vlan sizes ? */
667 if (!(s
->rcw
[1] & RCW1_JUM
)) {
668 if (size
> 1518 && size
<= 1522 && !(s
->rcw
[1] & RCW1_VLAN
)) {
673 /* Basic Address filters. If you want to use the extended filters
674 you'll generally have to place the ethernet mac into promiscuous mode
675 to avoid the basic filtering from dropping most frames. */
678 if (!enet_match_addr(buf
, s
->uaw
[0], s
->uaw
[1])) {
684 if (s
->regs
[R_RAF
] & RAF_BCAST_REJ
) {
691 if (s
->regs
[R_RAF
] & RAF_MCAST_REJ
) {
695 for (i
= 0; i
< 4; i
++) {
696 if (enet_match_addr(buf
, s
->maddr
[i
][0], s
->maddr
[i
][1])) {
709 /* Extended mcast filtering enabled? */
710 if (axienet_newfunc_enabled(s
) && axienet_extmcf_enabled(s
)) {
712 if (!enet_match_addr(buf
, s
->ext_uaw
[0], s
->ext_uaw
[1])) {
718 if (s
->regs
[R_RAF
] & RAF_BCAST_REJ
) {
725 if (!memcmp(buf
, sa_ipmcast
, 3)) {
729 idx
= (buf
[4] & 0x7f) << 8;
732 bit
= 1 << (idx
& 0x1f);
735 if (!(s
->ext_mtable
[idx
] & bit
)) {
743 s
->regs
[R_IS
] |= IS_RX_REJECT
;
748 if (size
> (s
->c_rxmem
- 4)) {
749 size
= s
->c_rxmem
- 4;
752 memcpy(s
->rxmem
, buf
, size
);
753 memset(s
->rxmem
+ size
, 0, 4); /* Clear the FCS. */
755 if (s
->rcw
[1] & RCW1_FCS
) {
756 size
+= 4; /* fcs is inband. */
760 csum32
= net_checksum_add(size
- 14, (uint8_t *)s
->rxmem
+ 14);
762 csum32
= (csum32
& 0xffff) + (csum32
>> 16);
763 /* And twice to get rid of possible carries. */
764 csum16
= (csum32
& 0xffff) + (csum32
>> 16);
766 app
[4] = size
& 0xffff;
768 s
->stats
.rx_bytes
+= size
;
772 app
[2] |= 1 | (ip_multicast
<< 1);
773 } else if (broadcast
) {
781 xlx_dma_push_to_dma(s
->dmach
, (void *)s
->rxmem
, size
, app
);
783 s
->regs
[R_IS
] |= IS_RX_COMPLETE
;
788 static void eth_cleanup(VLANClientState
*nc
)
791 struct XilinxAXIEnet
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
797 axienet_stream_push(void *opaque
, uint8_t *buf
, size_t size
, uint32_t *hdr
)
799 struct XilinxAXIEnet
*s
= opaque
;
802 if (!(s
->tc
& TC_TX
)) {
806 /* Jumbo or vlan sizes ? */
807 if (!(s
->tc
& TC_JUM
)) {
808 if (size
> 1518 && size
<= 1522 && !(s
->tc
& TC_VLAN
)) {
814 unsigned int start_off
= hdr
[1] >> 16;
815 unsigned int write_off
= hdr
[1] & 0xffff;
819 tmp_csum
= net_checksum_add(size
- start_off
,
820 (uint8_t *)buf
+ start_off
);
821 /* Accumulate the seed. */
822 tmp_csum
+= hdr
[2] & 0xffff;
824 /* Fold the 32bit partial checksum. */
825 csum
= net_checksum_finish(tmp_csum
);
828 buf
[write_off
] = csum
>> 8;
829 buf
[write_off
+ 1] = csum
& 0xff;
832 qemu_send_packet(&s
->nic
->nc
, buf
, size
);
834 s
->stats
.tx_bytes
+= size
;
835 s
->regs
[R_IS
] |= IS_TX_COMPLETE
;
839 static NetClientInfo net_xilinx_enet_info
= {
840 .type
= NET_CLIENT_TYPE_NIC
,
841 .size
= sizeof(NICState
),
842 .can_receive
= eth_can_rx
,
844 .cleanup
= eth_cleanup
,
847 static int xilinx_enet_init(SysBusDevice
*dev
)
849 struct XilinxAXIEnet
*s
= FROM_SYSBUS(typeof(*s
), dev
);
852 sysbus_init_irq(dev
, &s
->irq
);
855 hw_error("Unconnected Xilinx Ethernet MAC.\n");
858 xlx_dma_connect_client(s
->dmach
, s
, axienet_stream_push
);
860 enet_regs
= cpu_register_io_memory(enet_read
, enet_write
, s
,
861 DEVICE_LITTLE_ENDIAN
);
862 sysbus_init_mmio(dev
, 0x40000, enet_regs
);
864 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
865 s
->nic
= qemu_new_nic(&net_xilinx_enet_info
, &s
->conf
,
866 dev
->qdev
.info
->name
, dev
->qdev
.id
, s
);
867 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
869 tdk_init(&s
->TEMAC
.phy
);
870 mdio_attach(&s
->TEMAC
.mdio_bus
, &s
->TEMAC
.phy
, s
->c_phyaddr
);
874 s
->rxmem
= qemu_malloc(s
->c_rxmem
);
880 static SysBusDeviceInfo xilinx_enet_info
= {
881 .init
= xilinx_enet_init
,
882 .qdev
.name
= "xilinx,axienet",
883 .qdev
.size
= sizeof(struct XilinxAXIEnet
),
884 .qdev
.props
= (Property
[]) {
885 DEFINE_PROP_UINT32("phyaddr", struct XilinxAXIEnet
, c_phyaddr
, 7),
886 DEFINE_PROP_UINT32("c_rxmem", struct XilinxAXIEnet
, c_rxmem
, 0x1000),
887 DEFINE_PROP_UINT32("c_txmem", struct XilinxAXIEnet
, c_txmem
, 0x1000),
888 DEFINE_PROP_PTR("dmach", struct XilinxAXIEnet
, dmach
),
889 DEFINE_NIC_PROPERTIES(struct XilinxAXIEnet
, conf
),
890 DEFINE_PROP_END_OF_LIST(),
893 static void xilinx_enet_register(void)
895 sysbus_register_withprop(&xilinx_enet_info
);
898 device_init(xilinx_enet_register
)