2 * Faraday FTGMAC100 Gigabit Ethernet
4 * Copyright (C) 2016-2017, IBM Corporation.
6 * Based on Coldfire Fast Ethernet Controller emulation.
8 * Copyright (c) 2007 CodeSourcery.
10 * This code is licensed under the GPL version 2 or later. See the
11 * COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "hw/net/ftgmac100.h"
16 #include "sysemu/dma.h"
18 #include "net/checksum.h"
20 #include "hw/net/mii.h"
28 #define FTGMAC100_ISR 0x00
29 #define FTGMAC100_IER 0x04
30 #define FTGMAC100_MAC_MADR 0x08
31 #define FTGMAC100_MAC_LADR 0x0c
32 #define FTGMAC100_MATH0 0x10
33 #define FTGMAC100_MATH1 0x14
34 #define FTGMAC100_NPTXPD 0x18
35 #define FTGMAC100_RXPD 0x1C
36 #define FTGMAC100_NPTXR_BADR 0x20
37 #define FTGMAC100_RXR_BADR 0x24
38 #define FTGMAC100_HPTXPD 0x28
39 #define FTGMAC100_HPTXR_BADR 0x2c
40 #define FTGMAC100_ITC 0x30
41 #define FTGMAC100_APTC 0x34
42 #define FTGMAC100_DBLAC 0x38
43 #define FTGMAC100_REVR 0x40
44 #define FTGMAC100_FEAR1 0x44
45 #define FTGMAC100_RBSR 0x4c
46 #define FTGMAC100_TPAFCR 0x48
48 #define FTGMAC100_MACCR 0x50
49 #define FTGMAC100_MACSR 0x54
50 #define FTGMAC100_PHYCR 0x60
51 #define FTGMAC100_PHYDATA 0x64
52 #define FTGMAC100_FCR 0x68
55 * Interrupt status register & interrupt enable register
57 #define FTGMAC100_INT_RPKT_BUF (1 << 0)
58 #define FTGMAC100_INT_RPKT_FIFO (1 << 1)
59 #define FTGMAC100_INT_NO_RXBUF (1 << 2)
60 #define FTGMAC100_INT_RPKT_LOST (1 << 3)
61 #define FTGMAC100_INT_XPKT_ETH (1 << 4)
62 #define FTGMAC100_INT_XPKT_FIFO (1 << 5)
63 #define FTGMAC100_INT_NO_NPTXBUF (1 << 6)
64 #define FTGMAC100_INT_XPKT_LOST (1 << 7)
65 #define FTGMAC100_INT_AHB_ERR (1 << 8)
66 #define FTGMAC100_INT_PHYSTS_CHG (1 << 9)
67 #define FTGMAC100_INT_NO_HPTXBUF (1 << 10)
70 * Automatic polling timer control register
72 #define FTGMAC100_APTC_RXPOLL_CNT(x) ((x) & 0xf)
73 #define FTGMAC100_APTC_RXPOLL_TIME_SEL (1 << 4)
74 #define FTGMAC100_APTC_TXPOLL_CNT(x) (((x) >> 8) & 0xf)
75 #define FTGMAC100_APTC_TXPOLL_TIME_SEL (1 << 12)
78 * PHY control register
80 #define FTGMAC100_PHYCR_MIIRD (1 << 26)
81 #define FTGMAC100_PHYCR_MIIWR (1 << 27)
83 #define FTGMAC100_PHYCR_DEV(x) (((x) >> 16) & 0x1f)
84 #define FTGMAC100_PHYCR_REG(x) (((x) >> 21) & 0x1f)
89 #define FTGMAC100_PHYDATA_MIIWDATA(x) ((x) & 0xffff)
90 #define FTGMAC100_PHYDATA_MIIRDATA(x) (((x) >> 16) & 0xffff)
95 #define FTGMAC100_REVR_NEW_MDIO_INTERFACE (1 << 31)
98 * MAC control register
100 #define FTGMAC100_MACCR_TXDMA_EN (1 << 0)
101 #define FTGMAC100_MACCR_RXDMA_EN (1 << 1)
102 #define FTGMAC100_MACCR_TXMAC_EN (1 << 2)
103 #define FTGMAC100_MACCR_RXMAC_EN (1 << 3)
104 #define FTGMAC100_MACCR_RM_VLAN (1 << 4)
105 #define FTGMAC100_MACCR_HPTXR_EN (1 << 5)
106 #define FTGMAC100_MACCR_LOOP_EN (1 << 6)
107 #define FTGMAC100_MACCR_ENRX_IN_HALFTX (1 << 7)
108 #define FTGMAC100_MACCR_FULLDUP (1 << 8)
109 #define FTGMAC100_MACCR_GIGA_MODE (1 << 9)
110 #define FTGMAC100_MACCR_CRC_APD (1 << 10) /* not needed */
111 #define FTGMAC100_MACCR_RX_RUNT (1 << 12)
112 #define FTGMAC100_MACCR_JUMBO_LF (1 << 13)
113 #define FTGMAC100_MACCR_RX_ALL (1 << 14)
114 #define FTGMAC100_MACCR_HT_MULTI_EN (1 << 15)
115 #define FTGMAC100_MACCR_RX_MULTIPKT (1 << 16)
116 #define FTGMAC100_MACCR_RX_BROADPKT (1 << 17)
117 #define FTGMAC100_MACCR_DISCARD_CRCERR (1 << 18)
118 #define FTGMAC100_MACCR_FAST_MODE (1 << 19)
119 #define FTGMAC100_MACCR_SW_RST (1 << 31)
122 * Transmit descriptor
124 #define FTGMAC100_TXDES0_TXBUF_SIZE(x) ((x) & 0x3fff)
125 #define FTGMAC100_TXDES0_EDOTR (1 << 15)
126 #define FTGMAC100_TXDES0_CRC_ERR (1 << 19)
127 #define FTGMAC100_TXDES0_LTS (1 << 28)
128 #define FTGMAC100_TXDES0_FTS (1 << 29)
129 #define FTGMAC100_TXDES0_EDOTR_ASPEED (1 << 30)
130 #define FTGMAC100_TXDES0_TXDMA_OWN (1 << 31)
132 #define FTGMAC100_TXDES1_VLANTAG_CI(x) ((x) & 0xffff)
133 #define FTGMAC100_TXDES1_INS_VLANTAG (1 << 16)
134 #define FTGMAC100_TXDES1_TCP_CHKSUM (1 << 17)
135 #define FTGMAC100_TXDES1_UDP_CHKSUM (1 << 18)
136 #define FTGMAC100_TXDES1_IP_CHKSUM (1 << 19)
137 #define FTGMAC100_TXDES1_LLC (1 << 22)
138 #define FTGMAC100_TXDES1_TX2FIC (1 << 30)
139 #define FTGMAC100_TXDES1_TXIC (1 << 31)
144 #define FTGMAC100_RXDES0_VDBC 0x3fff
145 #define FTGMAC100_RXDES0_EDORR (1 << 15)
146 #define FTGMAC100_RXDES0_MULTICAST (1 << 16)
147 #define FTGMAC100_RXDES0_BROADCAST (1 << 17)
148 #define FTGMAC100_RXDES0_RX_ERR (1 << 18)
149 #define FTGMAC100_RXDES0_CRC_ERR (1 << 19)
150 #define FTGMAC100_RXDES0_FTL (1 << 20)
151 #define FTGMAC100_RXDES0_RUNT (1 << 21)
152 #define FTGMAC100_RXDES0_RX_ODD_NB (1 << 22)
153 #define FTGMAC100_RXDES0_FIFO_FULL (1 << 23)
154 #define FTGMAC100_RXDES0_PAUSE_OPCODE (1 << 24)
155 #define FTGMAC100_RXDES0_PAUSE_FRAME (1 << 25)
156 #define FTGMAC100_RXDES0_LRS (1 << 28)
157 #define FTGMAC100_RXDES0_FRS (1 << 29)
158 #define FTGMAC100_RXDES0_EDORR_ASPEED (1 << 30)
159 #define FTGMAC100_RXDES0_RXPKT_RDY (1 << 31)
161 #define FTGMAC100_RXDES1_VLANTAG_CI 0xffff
162 #define FTGMAC100_RXDES1_PROT_MASK (0x3 << 20)
163 #define FTGMAC100_RXDES1_PROT_NONIP (0x0 << 20)
164 #define FTGMAC100_RXDES1_PROT_IP (0x1 << 20)
165 #define FTGMAC100_RXDES1_PROT_TCPIP (0x2 << 20)
166 #define FTGMAC100_RXDES1_PROT_UDPIP (0x3 << 20)
167 #define FTGMAC100_RXDES1_LLC (1 << 22)
168 #define FTGMAC100_RXDES1_DF (1 << 23)
169 #define FTGMAC100_RXDES1_VLANTAG_AVAIL (1 << 24)
170 #define FTGMAC100_RXDES1_TCP_CHKSUM_ERR (1 << 25)
171 #define FTGMAC100_RXDES1_UDP_CHKSUM_ERR (1 << 26)
172 #define FTGMAC100_RXDES1_IP_CHKSUM_ERR (1 << 27)
175 * Receive and transmit Buffer Descriptor
180 uint32_t des2
; /* not used by HW */
185 * Specific RTL8211E MII Registers
187 #define RTL8211E_MII_PHYCR 16 /* PHY Specific Control */
188 #define RTL8211E_MII_PHYSR 17 /* PHY Specific Status */
189 #define RTL8211E_MII_INER 18 /* Interrupt Enable */
190 #define RTL8211E_MII_INSR 19 /* Interrupt Status */
191 #define RTL8211E_MII_RXERC 24 /* Receive Error Counter */
192 #define RTL8211E_MII_LDPSR 27 /* Link Down Power Saving */
193 #define RTL8211E_MII_EPAGSR 30 /* Extension Page Select */
194 #define RTL8211E_MII_PAGSEL 31 /* Page Select */
197 * RTL8211E Interrupt Status
199 #define PHY_INT_AUTONEG_ERROR (1 << 15)
200 #define PHY_INT_PAGE_RECV (1 << 12)
201 #define PHY_INT_AUTONEG_COMPLETE (1 << 11)
202 #define PHY_INT_LINK_STATUS (1 << 10)
203 #define PHY_INT_ERROR (1 << 9)
204 #define PHY_INT_DOWN (1 << 8)
205 #define PHY_INT_JABBER (1 << 0)
208 * Max frame size for the receiving buffer
210 #define FTGMAC100_MAX_FRAME_SIZE 9220
212 /* Limits depending on the type of the frame
214 * 9216 for Jumbo frames (+ 4 for VLAN)
215 * 1518 for other frames (+ 4 for VLAN)
217 static int ftgmac100_max_frame_size(FTGMAC100State
*s
, uint16_t proto
)
219 int max
= (s
->maccr
& FTGMAC100_MACCR_JUMBO_LF
? 9216 : 1518);
221 return max
+ (proto
== ETH_P_VLAN
? 4 : 0);
224 static void ftgmac100_update_irq(FTGMAC100State
*s
)
226 qemu_set_irq(s
->irq
, s
->isr
& s
->ier
);
230 * The MII phy could raise a GPIO to the processor which in turn
231 * could be handled as an interrpt by the OS.
232 * For now we don't handle any GPIO/interrupt line, so the OS will
233 * have to poll for the PHY status.
235 static void phy_update_irq(FTGMAC100State
*s
)
237 ftgmac100_update_irq(s
);
240 static void phy_update_link(FTGMAC100State
*s
)
242 /* Autonegotiation status mirrors link status. */
243 if (qemu_get_queue(s
->nic
)->link_down
) {
244 s
->phy_status
&= ~(MII_BMSR_LINK_ST
| MII_BMSR_AN_COMP
);
245 s
->phy_int
|= PHY_INT_DOWN
;
247 s
->phy_status
|= (MII_BMSR_LINK_ST
| MII_BMSR_AN_COMP
);
248 s
->phy_int
|= PHY_INT_AUTONEG_COMPLETE
;
253 static void ftgmac100_set_link(NetClientState
*nc
)
255 phy_update_link(FTGMAC100(qemu_get_nic_opaque(nc
)));
258 static void phy_reset(FTGMAC100State
*s
)
260 s
->phy_status
= (MII_BMSR_100TX_FD
| MII_BMSR_100TX_HD
| MII_BMSR_10T_FD
|
261 MII_BMSR_10T_HD
| MII_BMSR_EXTSTAT
| MII_BMSR_MFPS
|
262 MII_BMSR_AN_COMP
| MII_BMSR_AUTONEG
| MII_BMSR_LINK_ST
|
264 s
->phy_control
= (MII_BMCR_AUTOEN
| MII_BMCR_FD
| MII_BMCR_SPEED1000
);
265 s
->phy_advertise
= (MII_ANAR_PAUSE_ASYM
| MII_ANAR_PAUSE
| MII_ANAR_TXFD
|
266 MII_ANAR_TX
| MII_ANAR_10FD
| MII_ANAR_10
|
272 static uint32_t do_phy_read(FTGMAC100State
*s
, int reg
)
277 case MII_BMCR
: /* Basic Control */
278 val
= s
->phy_control
;
280 case MII_BMSR
: /* Basic Status */
283 case MII_PHYID1
: /* ID1 */
284 val
= RTL8211E_PHYID1
;
286 case MII_PHYID2
: /* ID2 */
287 val
= RTL8211E_PHYID2
;
289 case MII_ANAR
: /* Auto-neg advertisement */
290 val
= s
->phy_advertise
;
292 case MII_ANLPAR
: /* Auto-neg Link Partner Ability */
293 val
= (MII_ANLPAR_ACK
| MII_ANLPAR_PAUSE
| MII_ANLPAR_TXFD
|
294 MII_ANLPAR_TX
| MII_ANLPAR_10FD
| MII_ANLPAR_10
|
297 case MII_ANER
: /* Auto-neg Expansion */
300 case MII_CTRL1000
: /* 1000BASE-T control */
301 val
= (MII_CTRL1000_HALF
| MII_CTRL1000_FULL
);
303 case MII_STAT1000
: /* 1000BASE-T status */
304 val
= MII_STAT1000_FULL
;
306 case RTL8211E_MII_INSR
: /* Interrupt status. */
311 case RTL8211E_MII_INER
: /* Interrupt enable */
312 val
= s
->phy_int_mask
;
314 case RTL8211E_MII_PHYCR
:
315 case RTL8211E_MII_PHYSR
:
316 case RTL8211E_MII_RXERC
:
317 case RTL8211E_MII_LDPSR
:
318 case RTL8211E_MII_EPAGSR
:
319 case RTL8211E_MII_PAGSEL
:
320 qemu_log_mask(LOG_UNIMP
, "%s: reg %d not implemented\n",
325 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad address at offset %d\n",
334 #define MII_BMCR_MASK (MII_BMCR_LOOPBACK | MII_BMCR_SPEED100 | \
335 MII_BMCR_SPEED | MII_BMCR_AUTOEN | MII_BMCR_PDOWN | \
336 MII_BMCR_FD | MII_BMCR_CTST)
337 #define MII_ANAR_MASK 0x2d7f
339 static void do_phy_write(FTGMAC100State
*s
, int reg
, uint32_t val
)
342 case MII_BMCR
: /* Basic Control */
343 if (val
& MII_BMCR_RESET
) {
346 s
->phy_control
= val
& MII_BMCR_MASK
;
347 /* Complete autonegotiation immediately. */
348 if (val
& MII_BMCR_AUTOEN
) {
349 s
->phy_status
|= MII_BMSR_AN_COMP
;
353 case MII_ANAR
: /* Auto-neg advertisement */
354 s
->phy_advertise
= (val
& MII_ANAR_MASK
) | MII_ANAR_TX
;
356 case RTL8211E_MII_INER
: /* Interrupt enable */
357 s
->phy_int_mask
= val
& 0xff;
360 case RTL8211E_MII_PHYCR
:
361 case RTL8211E_MII_PHYSR
:
362 case RTL8211E_MII_RXERC
:
363 case RTL8211E_MII_LDPSR
:
364 case RTL8211E_MII_EPAGSR
:
365 case RTL8211E_MII_PAGSEL
:
366 qemu_log_mask(LOG_UNIMP
, "%s: reg %d not implemented\n",
370 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad address at offset %d\n",
376 static int ftgmac100_read_bd(FTGMAC100Desc
*bd
, dma_addr_t addr
)
378 if (dma_memory_read(&address_space_memory
, addr
, bd
, sizeof(*bd
))) {
379 qemu_log_mask(LOG_GUEST_ERROR
, "%s: failed to read descriptor @ 0x%"
380 HWADDR_PRIx
"\n", __func__
, addr
);
383 bd
->des0
= le32_to_cpu(bd
->des0
);
384 bd
->des1
= le32_to_cpu(bd
->des1
);
385 bd
->des2
= le32_to_cpu(bd
->des2
);
386 bd
->des3
= le32_to_cpu(bd
->des3
);
390 static int ftgmac100_write_bd(FTGMAC100Desc
*bd
, dma_addr_t addr
)
394 lebd
.des0
= cpu_to_le32(bd
->des0
);
395 lebd
.des1
= cpu_to_le32(bd
->des1
);
396 lebd
.des2
= cpu_to_le32(bd
->des2
);
397 lebd
.des3
= cpu_to_le32(bd
->des3
);
398 if (dma_memory_write(&address_space_memory
, addr
, &lebd
, sizeof(lebd
))) {
399 qemu_log_mask(LOG_GUEST_ERROR
, "%s: failed to write descriptor @ 0x%"
400 HWADDR_PRIx
"\n", __func__
, addr
);
406 static void ftgmac100_do_tx(FTGMAC100State
*s
, uint32_t tx_ring
,
407 uint32_t tx_descriptor
)
410 uint8_t *ptr
= s
->frame
;
411 uint32_t addr
= tx_descriptor
;
418 if (ftgmac100_read_bd(&bd
, addr
) ||
419 ((bd
.des0
& FTGMAC100_TXDES0_TXDMA_OWN
) == 0)) {
420 /* Run out of descriptors to transmit. */
421 s
->isr
|= FTGMAC100_INT_NO_NPTXBUF
;
425 /* record transmit flags as they are valid only on the first
427 if (bd
.des0
& FTGMAC100_TXDES0_FTS
) {
431 len
= FTGMAC100_TXDES0_TXBUF_SIZE(bd
.des0
);
432 if (frame_size
+ len
> sizeof(s
->frame
)) {
433 qemu_log_mask(LOG_GUEST_ERROR
, "%s: frame too big : %d bytes\n",
435 s
->isr
|= FTGMAC100_INT_XPKT_LOST
;
436 len
= sizeof(s
->frame
) - frame_size
;
439 if (dma_memory_read(&address_space_memory
, bd
.des3
, ptr
, len
)) {
440 qemu_log_mask(LOG_GUEST_ERROR
, "%s: failed to read packet @ 0x%x\n",
442 s
->isr
|= FTGMAC100_INT_NO_NPTXBUF
;
447 if (bd
.des0
& FTGMAC100_TXDES0_FTS
&&
448 bd
.des1
& FTGMAC100_TXDES1_INS_VLANTAG
&&
449 be16_to_cpu(PKT_GET_ETH_HDR(ptr
)->h_proto
) != ETH_P_VLAN
) {
450 if (frame_size
+ len
+ 4 > sizeof(s
->frame
)) {
451 qemu_log_mask(LOG_GUEST_ERROR
, "%s: frame too big : %d bytes\n",
453 s
->isr
|= FTGMAC100_INT_XPKT_LOST
;
454 len
= sizeof(s
->frame
) - frame_size
- 4;
456 memmove(ptr
+ 16, ptr
+ 12, len
- 12);
457 stw_be_p(ptr
+ 12, ETH_P_VLAN
);
458 stw_be_p(ptr
+ 14, bd
.des1
);
464 if (bd
.des0
& FTGMAC100_TXDES0_LTS
) {
465 if (flags
& FTGMAC100_TXDES1_IP_CHKSUM
) {
466 net_checksum_calculate(s
->frame
, frame_size
);
468 /* Last buffer in frame. */
469 qemu_send_packet(qemu_get_queue(s
->nic
), s
->frame
, frame_size
);
472 if (flags
& FTGMAC100_TXDES1_TXIC
) {
473 s
->isr
|= FTGMAC100_INT_XPKT_ETH
;
477 if (flags
& FTGMAC100_TXDES1_TX2FIC
) {
478 s
->isr
|= FTGMAC100_INT_XPKT_FIFO
;
480 bd
.des0
&= ~FTGMAC100_TXDES0_TXDMA_OWN
;
482 /* Write back the modified descriptor. */
483 ftgmac100_write_bd(&bd
, addr
);
484 /* Advance to the next descriptor. */
485 if (bd
.des0
& s
->txdes0_edotr
) {
488 addr
+= sizeof(FTGMAC100Desc
);
492 s
->tx_descriptor
= addr
;
494 ftgmac100_update_irq(s
);
497 static int ftgmac100_can_receive(NetClientState
*nc
)
499 FTGMAC100State
*s
= FTGMAC100(qemu_get_nic_opaque(nc
));
502 if ((s
->maccr
& (FTGMAC100_MACCR_RXDMA_EN
| FTGMAC100_MACCR_RXMAC_EN
))
503 != (FTGMAC100_MACCR_RXDMA_EN
| FTGMAC100_MACCR_RXMAC_EN
)) {
507 if (ftgmac100_read_bd(&bd
, s
->rx_descriptor
)) {
510 return !(bd
.des0
& FTGMAC100_RXDES0_RXPKT_RDY
);
514 * This is purely informative. The HW can poll the RW (and RX) ring
515 * buffers for available descriptors but we don't need to trigger a
516 * timer for that in qemu.
518 static uint32_t ftgmac100_rxpoll(FTGMAC100State
*s
)
522 * Speed TIME_SEL=0 TIME_SEL=1
524 * 10 51.2 ms 819.2 ms
525 * 100 5.12 ms 81.92 ms
526 * 1000 1.024 ms 16.384 ms
528 static const int div
[] = { 20, 200, 1000 };
530 uint32_t cnt
= 1024 * FTGMAC100_APTC_RXPOLL_CNT(s
->aptcr
);
531 uint32_t speed
= (s
->maccr
& FTGMAC100_MACCR_FAST_MODE
) ? 1 : 0;
533 if (s
->aptcr
& FTGMAC100_APTC_RXPOLL_TIME_SEL
) {
537 if (s
->maccr
& FTGMAC100_MACCR_GIGA_MODE
) {
541 return cnt
/ div
[speed
];
544 static void ftgmac100_reset(DeviceState
*d
)
546 FTGMAC100State
*s
= FTGMAC100(d
);
548 /* Reset the FTGMAC100 */
554 s
->rx_descriptor
= 0;
556 s
->tx_descriptor
= 0;
561 s
->dblac
= 0x00022f00;
575 static uint64_t ftgmac100_read(void *opaque
, hwaddr addr
, unsigned size
)
577 FTGMAC100State
*s
= FTGMAC100(opaque
);
579 switch (addr
& 0xff) {
584 case FTGMAC100_MAC_MADR
:
585 return (s
->conf
.macaddr
.a
[0] << 8) | s
->conf
.macaddr
.a
[1];
586 case FTGMAC100_MAC_LADR
:
587 return ((uint32_t) s
->conf
.macaddr
.a
[2] << 24) |
588 (s
->conf
.macaddr
.a
[3] << 16) | (s
->conf
.macaddr
.a
[4] << 8) |
589 s
->conf
.macaddr
.a
[5];
590 case FTGMAC100_MATH0
:
592 case FTGMAC100_MATH1
:
596 case FTGMAC100_DBLAC
:
600 case FTGMAC100_FEAR1
:
602 case FTGMAC100_TPAFCR
:
606 case FTGMAC100_MACCR
:
608 case FTGMAC100_PHYCR
:
610 case FTGMAC100_PHYDATA
:
613 /* We might want to support these one day */
614 case FTGMAC100_HPTXPD
: /* High Priority Transmit Poll Demand */
615 case FTGMAC100_HPTXR_BADR
: /* High Priority Transmit Ring Base Address */
616 case FTGMAC100_MACSR
: /* MAC Status Register (MACSR) */
617 qemu_log_mask(LOG_UNIMP
, "%s: read to unimplemented register 0x%"
618 HWADDR_PRIx
"\n", __func__
, addr
);
621 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad address at offset 0x%"
622 HWADDR_PRIx
"\n", __func__
, addr
);
627 static void ftgmac100_write(void *opaque
, hwaddr addr
,
628 uint64_t value
, unsigned size
)
630 FTGMAC100State
*s
= FTGMAC100(opaque
);
633 switch (addr
& 0xff) {
634 case FTGMAC100_ISR
: /* Interrupt status */
637 case FTGMAC100_IER
: /* Interrupt control */
640 case FTGMAC100_MAC_MADR
: /* MAC */
641 s
->conf
.macaddr
.a
[0] = value
>> 8;
642 s
->conf
.macaddr
.a
[1] = value
;
644 case FTGMAC100_MAC_LADR
:
645 s
->conf
.macaddr
.a
[2] = value
>> 24;
646 s
->conf
.macaddr
.a
[3] = value
>> 16;
647 s
->conf
.macaddr
.a
[4] = value
>> 8;
648 s
->conf
.macaddr
.a
[5] = value
;
650 case FTGMAC100_MATH0
: /* Multicast Address Hash Table 0 */
653 case FTGMAC100_MATH1
: /* Multicast Address Hash Table 1 */
656 case FTGMAC100_ITC
: /* TODO: Interrupt Timer Control */
659 case FTGMAC100_RXR_BADR
: /* Ring buffer address */
661 s
->rx_descriptor
= s
->rx_ring
;
664 case FTGMAC100_RBSR
: /* DMA buffer size */
668 case FTGMAC100_NPTXR_BADR
: /* Transmit buffer address */
670 s
->tx_descriptor
= s
->tx_ring
;
673 case FTGMAC100_NPTXPD
: /* Trigger transmit */
674 if ((s
->maccr
& (FTGMAC100_MACCR_TXDMA_EN
| FTGMAC100_MACCR_TXMAC_EN
))
675 == (FTGMAC100_MACCR_TXDMA_EN
| FTGMAC100_MACCR_TXMAC_EN
)) {
676 /* TODO: high priority tx ring */
677 ftgmac100_do_tx(s
, s
->tx_ring
, s
->tx_descriptor
);
679 if (ftgmac100_can_receive(qemu_get_queue(s
->nic
))) {
680 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
684 case FTGMAC100_RXPD
: /* Receive Poll Demand Register */
685 if (ftgmac100_can_receive(qemu_get_queue(s
->nic
))) {
686 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
690 case FTGMAC100_APTC
: /* Automatic polling */
693 if (FTGMAC100_APTC_RXPOLL_CNT(s
->aptcr
)) {
697 if (FTGMAC100_APTC_TXPOLL_CNT(s
->aptcr
)) {
698 qemu_log_mask(LOG_UNIMP
, "%s: no transmit polling\n", __func__
);
702 case FTGMAC100_MACCR
: /* MAC Device control */
704 if (value
& FTGMAC100_MACCR_SW_RST
) {
705 ftgmac100_reset(DEVICE(s
));
708 if (ftgmac100_can_receive(qemu_get_queue(s
->nic
))) {
709 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
713 case FTGMAC100_PHYCR
: /* PHY Device control */
714 reg
= FTGMAC100_PHYCR_REG(value
);
716 if (value
& FTGMAC100_PHYCR_MIIWR
) {
717 do_phy_write(s
, reg
, s
->phydata
& 0xffff);
718 s
->phycr
&= ~FTGMAC100_PHYCR_MIIWR
;
720 s
->phydata
= do_phy_read(s
, reg
) << 16;
721 s
->phycr
&= ~FTGMAC100_PHYCR_MIIRD
;
724 case FTGMAC100_PHYDATA
:
725 s
->phydata
= value
& 0xffff;
727 case FTGMAC100_DBLAC
: /* DMA Burst Length and Arbitration Control */
730 case FTGMAC100_REVR
: /* Feature Register */
731 /* TODO: Only Old MDIO interface is supported */
732 s
->revr
= value
& ~FTGMAC100_REVR_NEW_MDIO_INTERFACE
;
734 case FTGMAC100_FEAR1
: /* Feature Register 1 */
737 case FTGMAC100_TPAFCR
: /* Transmit Priority Arbitration and FIFO Control */
740 case FTGMAC100_FCR
: /* Flow Control */
744 case FTGMAC100_HPTXPD
: /* High Priority Transmit Poll Demand */
745 case FTGMAC100_HPTXR_BADR
: /* High Priority Transmit Ring Base Address */
746 case FTGMAC100_MACSR
: /* MAC Status Register (MACSR) */
747 qemu_log_mask(LOG_UNIMP
, "%s: write to unimplemented register 0x%"
748 HWADDR_PRIx
"\n", __func__
, addr
);
751 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad address at offset 0x%"
752 HWADDR_PRIx
"\n", __func__
, addr
);
756 ftgmac100_update_irq(s
);
759 static int ftgmac100_filter(FTGMAC100State
*s
, const uint8_t *buf
, size_t len
)
763 if (s
->maccr
& FTGMAC100_MACCR_RX_ALL
) {
767 switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf
))) {
769 if (!(s
->maccr
& FTGMAC100_MACCR_RX_BROADPKT
)) {
774 if (!(s
->maccr
& FTGMAC100_MACCR_RX_MULTIPKT
)) {
775 if (!(s
->maccr
& FTGMAC100_MACCR_HT_MULTI_EN
)) {
779 mcast_idx
= net_crc32_le(buf
, ETH_ALEN
);
780 mcast_idx
= (~(mcast_idx
>> 2)) & 0x3f;
781 if (!(s
->math
[mcast_idx
/ 32] & (1 << (mcast_idx
% 32)))) {
787 if (memcmp(s
->conf
.macaddr
.a
, buf
, 6)) {
796 static ssize_t
ftgmac100_receive(NetClientState
*nc
, const uint8_t *buf
,
799 FTGMAC100State
*s
= FTGMAC100(qemu_get_nic_opaque(nc
));
808 uint32_t first
= FTGMAC100_RXDES0_FRS
;
809 uint16_t proto
= be16_to_cpu(PKT_GET_ETH_HDR(buf
)->h_proto
);
810 int max_frame_size
= ftgmac100_max_frame_size(s
, proto
);
812 if ((s
->maccr
& (FTGMAC100_MACCR_RXDMA_EN
| FTGMAC100_MACCR_RXMAC_EN
))
813 != (FTGMAC100_MACCR_RXDMA_EN
| FTGMAC100_MACCR_RXMAC_EN
)) {
817 /* TODO : Pad to minimum Ethernet frame length */
818 /* handle small packets. */
820 qemu_log_mask(LOG_GUEST_ERROR
, "%s: dropped frame of %zd bytes\n",
825 if (!ftgmac100_filter(s
, buf
, size
)) {
829 /* 4 bytes for the CRC. */
831 crc
= cpu_to_be32(crc32(~0, buf
, size
));
832 crc_ptr
= (uint8_t *) &crc
;
834 /* Huge frames are truncated. */
835 if (size
> max_frame_size
) {
836 qemu_log_mask(LOG_GUEST_ERROR
, "%s: frame too big : %zd bytes\n",
838 size
= max_frame_size
;
839 flags
|= FTGMAC100_RXDES0_FTL
;
842 switch (get_eth_packet_type(PKT_GET_ETH_HDR(buf
))) {
844 flags
|= FTGMAC100_RXDES0_BROADCAST
;
847 flags
|= FTGMAC100_RXDES0_MULTICAST
;
853 addr
= s
->rx_descriptor
;
855 if (!ftgmac100_can_receive(nc
)) {
856 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Unexpected packet\n", __func__
);
860 if (ftgmac100_read_bd(&bd
, addr
) ||
861 (bd
.des0
& FTGMAC100_RXDES0_RXPKT_RDY
)) {
862 /* No descriptors available. Bail out. */
863 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Lost end of frame\n",
865 s
->isr
|= FTGMAC100_INT_NO_RXBUF
;
868 buf_len
= (size
<= s
->rbsr
) ? size
: s
->rbsr
;
869 bd
.des0
|= buf_len
& 0x3fff;
872 /* The last 4 bytes are the CRC. */
877 if (first
&& proto
== ETH_P_VLAN
&& buf_len
>= 18) {
878 bd
.des1
= lduw_be_p(buf
+ 14) | FTGMAC100_RXDES1_VLANTAG_AVAIL
;
880 if (s
->maccr
& FTGMAC100_MACCR_RM_VLAN
) {
881 dma_memory_write(&address_space_memory
, buf_addr
, buf
, 12);
882 dma_memory_write(&address_space_memory
, buf_addr
+ 12, buf
+ 16,
885 dma_memory_write(&address_space_memory
, buf_addr
, buf
, buf_len
);
889 dma_memory_write(&address_space_memory
, buf_addr
, buf
, buf_len
);
893 dma_memory_write(&address_space_memory
, buf_addr
+ buf_len
,
898 bd
.des0
|= first
| FTGMAC100_RXDES0_RXPKT_RDY
;
901 /* Last buffer in frame. */
902 bd
.des0
|= flags
| FTGMAC100_RXDES0_LRS
;
903 s
->isr
|= FTGMAC100_INT_RPKT_BUF
;
905 s
->isr
|= FTGMAC100_INT_RPKT_FIFO
;
907 ftgmac100_write_bd(&bd
, addr
);
908 if (bd
.des0
& s
->rxdes0_edorr
) {
911 addr
+= sizeof(FTGMAC100Desc
);
914 s
->rx_descriptor
= addr
;
916 ftgmac100_update_irq(s
);
920 static const MemoryRegionOps ftgmac100_ops
= {
921 .read
= ftgmac100_read
,
922 .write
= ftgmac100_write
,
923 .valid
.min_access_size
= 4,
924 .valid
.max_access_size
= 4,
925 .endianness
= DEVICE_LITTLE_ENDIAN
,
928 static void ftgmac100_cleanup(NetClientState
*nc
)
930 FTGMAC100State
*s
= FTGMAC100(qemu_get_nic_opaque(nc
));
935 static NetClientInfo net_ftgmac100_info
= {
936 .type
= NET_CLIENT_DRIVER_NIC
,
937 .size
= sizeof(NICState
),
938 .can_receive
= ftgmac100_can_receive
,
939 .receive
= ftgmac100_receive
,
940 .cleanup
= ftgmac100_cleanup
,
941 .link_status_changed
= ftgmac100_set_link
,
944 static void ftgmac100_realize(DeviceState
*dev
, Error
**errp
)
946 FTGMAC100State
*s
= FTGMAC100(dev
);
947 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
950 s
->txdes0_edotr
= FTGMAC100_TXDES0_EDOTR_ASPEED
;
951 s
->rxdes0_edorr
= FTGMAC100_RXDES0_EDORR_ASPEED
;
953 s
->txdes0_edotr
= FTGMAC100_TXDES0_EDOTR
;
954 s
->rxdes0_edorr
= FTGMAC100_RXDES0_EDORR
;
957 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &ftgmac100_ops
, s
,
958 TYPE_FTGMAC100
, 0x2000);
959 sysbus_init_mmio(sbd
, &s
->iomem
);
960 sysbus_init_irq(sbd
, &s
->irq
);
961 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
963 s
->conf
.peers
.ncs
[0] = nd_table
[0].netdev
;
965 s
->nic
= qemu_new_nic(&net_ftgmac100_info
, &s
->conf
,
966 object_get_typename(OBJECT(dev
)), DEVICE(dev
)->id
,
968 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
971 static const VMStateDescription vmstate_ftgmac100
= {
972 .name
= TYPE_FTGMAC100
,
974 .minimum_version_id
= 1,
975 .fields
= (VMStateField
[]) {
976 VMSTATE_UINT32(irq_state
, FTGMAC100State
),
977 VMSTATE_UINT32(isr
, FTGMAC100State
),
978 VMSTATE_UINT32(ier
, FTGMAC100State
),
979 VMSTATE_UINT32(rx_enabled
, FTGMAC100State
),
980 VMSTATE_UINT32(rx_ring
, FTGMAC100State
),
981 VMSTATE_UINT32(rbsr
, FTGMAC100State
),
982 VMSTATE_UINT32(tx_ring
, FTGMAC100State
),
983 VMSTATE_UINT32(rx_descriptor
, FTGMAC100State
),
984 VMSTATE_UINT32(tx_descriptor
, FTGMAC100State
),
985 VMSTATE_UINT32_ARRAY(math
, FTGMAC100State
, 2),
986 VMSTATE_UINT32(itc
, FTGMAC100State
),
987 VMSTATE_UINT32(aptcr
, FTGMAC100State
),
988 VMSTATE_UINT32(dblac
, FTGMAC100State
),
989 VMSTATE_UINT32(revr
, FTGMAC100State
),
990 VMSTATE_UINT32(fear1
, FTGMAC100State
),
991 VMSTATE_UINT32(tpafcr
, FTGMAC100State
),
992 VMSTATE_UINT32(maccr
, FTGMAC100State
),
993 VMSTATE_UINT32(phycr
, FTGMAC100State
),
994 VMSTATE_UINT32(phydata
, FTGMAC100State
),
995 VMSTATE_UINT32(fcr
, FTGMAC100State
),
996 VMSTATE_UINT32(phy_status
, FTGMAC100State
),
997 VMSTATE_UINT32(phy_control
, FTGMAC100State
),
998 VMSTATE_UINT32(phy_advertise
, FTGMAC100State
),
999 VMSTATE_UINT32(phy_int
, FTGMAC100State
),
1000 VMSTATE_UINT32(phy_int_mask
, FTGMAC100State
),
1001 VMSTATE_UINT32(txdes0_edotr
, FTGMAC100State
),
1002 VMSTATE_UINT32(rxdes0_edorr
, FTGMAC100State
),
1003 VMSTATE_END_OF_LIST()
1007 static Property ftgmac100_properties
[] = {
1008 DEFINE_PROP_BOOL("aspeed", FTGMAC100State
, aspeed
, false),
1009 DEFINE_NIC_PROPERTIES(FTGMAC100State
, conf
),
1010 DEFINE_PROP_END_OF_LIST(),
1013 static void ftgmac100_class_init(ObjectClass
*klass
, void *data
)
1015 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1017 dc
->vmsd
= &vmstate_ftgmac100
;
1018 dc
->reset
= ftgmac100_reset
;
1019 dc
->props
= ftgmac100_properties
;
1020 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1021 dc
->realize
= ftgmac100_realize
;
1022 dc
->desc
= "Faraday FTGMAC100 Gigabit Ethernet emulation";
1025 static const TypeInfo ftgmac100_info
= {
1026 .name
= TYPE_FTGMAC100
,
1027 .parent
= TYPE_SYS_BUS_DEVICE
,
1028 .instance_size
= sizeof(FTGMAC100State
),
1029 .class_init
= ftgmac100_class_init
,
1032 static void ftgmac100_register_types(void)
1034 type_register_static(&ftgmac100_info
);
1037 type_init(ftgmac100_register_types
)