2 * i.MX Fast Ethernet Controller emulation.
4 * Copyright (c) 2013 Jean-Christophe Dubois. <jcd@tribudubois.net>
6 * Based on Coldfire Fast Ethernet Controller emulation.
8 * Copyright (c) 2007 CodeSourcery.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
26 #include "hw/net/imx_fec.h"
27 #include "sysemu/dma.h"
29 #include "qemu/module.h"
30 #include "net/checksum.h"
37 #define DEBUG_IMX_FEC 0
40 #define FEC_PRINTF(fmt, args...) \
42 if (DEBUG_IMX_FEC) { \
43 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_FEC, \
49 #define DEBUG_IMX_PHY 0
52 #define PHY_PRINTF(fmt, args...) \
54 if (DEBUG_IMX_PHY) { \
55 fprintf(stderr, "[%s.phy]%s: " fmt , TYPE_IMX_FEC, \
60 #define IMX_MAX_DESC 1024
62 static const char *imx_default_reg_name(IMXFECState
*s
, uint32_t index
)
65 sprintf(tmp
, "index %d", index
);
69 static const char *imx_fec_reg_name(IMXFECState
*s
, uint32_t index
)
76 case ENET_MIIGSK_CFGR
:
81 return imx_default_reg_name(s
, index
);
85 static const char *imx_enet_reg_name(IMXFECState
*s
, uint32_t index
)
143 return imx_default_reg_name(s
, index
);
147 static const char *imx_eth_reg_name(IMXFECState
*s
, uint32_t index
)
194 return imx_fec_reg_name(s
, index
);
196 return imx_enet_reg_name(s
, index
);
202 * Versions of this device with more than one TX descriptor save the
203 * 2nd and 3rd descriptors in a subsection, to maintain migration
204 * compatibility with previous versions of the device that only
205 * supported a single descriptor.
207 static bool imx_eth_is_multi_tx_ring(void *opaque
)
209 IMXFECState
*s
= IMX_FEC(opaque
);
211 return s
->tx_ring_num
> 1;
214 static const VMStateDescription vmstate_imx_eth_txdescs
= {
215 .name
= "imx.fec/txdescs",
217 .minimum_version_id
= 1,
218 .needed
= imx_eth_is_multi_tx_ring
,
219 .fields
= (VMStateField
[]) {
220 VMSTATE_UINT32(tx_descriptor
[1], IMXFECState
),
221 VMSTATE_UINT32(tx_descriptor
[2], IMXFECState
),
222 VMSTATE_END_OF_LIST()
226 static const VMStateDescription vmstate_imx_eth
= {
227 .name
= TYPE_IMX_FEC
,
229 .minimum_version_id
= 2,
230 .fields
= (VMStateField
[]) {
231 VMSTATE_UINT32_ARRAY(regs
, IMXFECState
, ENET_MAX
),
232 VMSTATE_UINT32(rx_descriptor
, IMXFECState
),
233 VMSTATE_UINT32(tx_descriptor
[0], IMXFECState
),
234 VMSTATE_UINT32(phy_status
, IMXFECState
),
235 VMSTATE_UINT32(phy_control
, IMXFECState
),
236 VMSTATE_UINT32(phy_advertise
, IMXFECState
),
237 VMSTATE_UINT32(phy_int
, IMXFECState
),
238 VMSTATE_UINT32(phy_int_mask
, IMXFECState
),
239 VMSTATE_END_OF_LIST()
241 .subsections
= (const VMStateDescription
* []) {
242 &vmstate_imx_eth_txdescs
,
247 #define PHY_INT_ENERGYON (1 << 7)
248 #define PHY_INT_AUTONEG_COMPLETE (1 << 6)
249 #define PHY_INT_FAULT (1 << 5)
250 #define PHY_INT_DOWN (1 << 4)
251 #define PHY_INT_AUTONEG_LP (1 << 3)
252 #define PHY_INT_PARFAULT (1 << 2)
253 #define PHY_INT_AUTONEG_PAGE (1 << 1)
255 static void imx_eth_update(IMXFECState
*s
);
258 * The MII phy could raise a GPIO to the processor which in turn
259 * could be handled as an interrpt by the OS.
260 * For now we don't handle any GPIO/interrupt line, so the OS will
261 * have to poll for the PHY status.
263 static void phy_update_irq(IMXFECState
*s
)
268 static void phy_update_link(IMXFECState
*s
)
270 /* Autonegotiation status mirrors link status. */
271 if (qemu_get_queue(s
->nic
)->link_down
) {
272 PHY_PRINTF("link is down\n");
273 s
->phy_status
&= ~0x0024;
274 s
->phy_int
|= PHY_INT_DOWN
;
276 PHY_PRINTF("link is up\n");
277 s
->phy_status
|= 0x0024;
278 s
->phy_int
|= PHY_INT_ENERGYON
;
279 s
->phy_int
|= PHY_INT_AUTONEG_COMPLETE
;
284 static void imx_eth_set_link(NetClientState
*nc
)
286 phy_update_link(IMX_FEC(qemu_get_nic_opaque(nc
)));
289 static void phy_reset(IMXFECState
*s
)
291 s
->phy_status
= 0x7809;
292 s
->phy_control
= 0x3000;
293 s
->phy_advertise
= 0x01e1;
299 static uint32_t do_phy_read(IMXFECState
*s
, int reg
)
304 /* we only advertise one phy */
309 case 0: /* Basic Control */
310 val
= s
->phy_control
;
312 case 1: /* Basic Status */
321 case 4: /* Auto-neg advertisement */
322 val
= s
->phy_advertise
;
324 case 5: /* Auto-neg Link Partner Ability */
327 case 6: /* Auto-neg Expansion */
330 case 29: /* Interrupt source. */
335 case 30: /* Interrupt mask */
336 val
= s
->phy_int_mask
;
342 qemu_log_mask(LOG_UNIMP
, "[%s.phy]%s: reg %d not implemented\n",
343 TYPE_IMX_FEC
, __func__
, reg
);
347 qemu_log_mask(LOG_GUEST_ERROR
, "[%s.phy]%s: Bad address at offset %d\n",
348 TYPE_IMX_FEC
, __func__
, reg
);
353 PHY_PRINTF("read 0x%04x @ %d\n", val
, reg
);
358 static void do_phy_write(IMXFECState
*s
, int reg
, uint32_t val
)
360 PHY_PRINTF("write 0x%04x @ %d\n", val
, reg
);
363 /* we only advertise one phy */
368 case 0: /* Basic Control */
372 s
->phy_control
= val
& 0x7980;
373 /* Complete autonegotiation immediately. */
375 s
->phy_status
|= 0x0020;
379 case 4: /* Auto-neg advertisement */
380 s
->phy_advertise
= (val
& 0x2d7f) | 0x80;
382 case 30: /* Interrupt mask */
383 s
->phy_int_mask
= val
& 0xff;
390 qemu_log_mask(LOG_UNIMP
, "[%s.phy)%s: reg %d not implemented\n",
391 TYPE_IMX_FEC
, __func__
, reg
);
394 qemu_log_mask(LOG_GUEST_ERROR
, "[%s.phy]%s: Bad address at offset %d\n",
395 TYPE_IMX_FEC
, __func__
, reg
);
400 static void imx_fec_read_bd(IMXFECBufDesc
*bd
, dma_addr_t addr
)
402 dma_memory_read(&address_space_memory
, addr
, bd
, sizeof(*bd
));
405 static void imx_fec_write_bd(IMXFECBufDesc
*bd
, dma_addr_t addr
)
407 dma_memory_write(&address_space_memory
, addr
, bd
, sizeof(*bd
));
410 static void imx_enet_read_bd(IMXENETBufDesc
*bd
, dma_addr_t addr
)
412 dma_memory_read(&address_space_memory
, addr
, bd
, sizeof(*bd
));
415 static void imx_enet_write_bd(IMXENETBufDesc
*bd
, dma_addr_t addr
)
417 dma_memory_write(&address_space_memory
, addr
, bd
, sizeof(*bd
));
420 static void imx_eth_update(IMXFECState
*s
)
423 * Previous versions of qemu had the ENET_INT_MAC and ENET_INT_TS_TIMER
424 * interrupts swapped. This worked with older versions of Linux (4.14
425 * and older) since Linux associated both interrupt lines with Ethernet
426 * MAC interrupts. Specifically,
427 * - Linux 4.15 and later have separate interrupt handlers for the MAC and
428 * timer interrupts. Those versions of Linux fail with versions of QEMU
429 * with swapped interrupt assignments.
430 * - In linux 4.14, both interrupt lines were registered with the Ethernet
431 * MAC interrupt handler. As a result, all versions of qemu happen to
432 * work, though that is accidental.
433 * - In Linux 4.9 and older, the timer interrupt was registered directly
434 * with the Ethernet MAC interrupt handler. The MAC interrupt was
435 * redirected to a GPIO interrupt to work around erratum ERR006687.
436 * This was implemented using the SOC's IOMUX block. In qemu, this GPIO
437 * interrupt never fired since IOMUX is currently not supported in qemu.
438 * Linux instead received MAC interrupts on the timer interrupt.
439 * As a result, qemu versions with the swapped interrupt assignment work,
440 * albeit accidentally, but qemu versions with the correct interrupt
443 * To ensure that all versions of Linux work, generate ENET_INT_MAC
444 * interrrupts on both interrupt lines. This should be changed if and when
445 * qemu supports IOMUX.
447 if (s
->regs
[ENET_EIR
] & s
->regs
[ENET_EIMR
] &
448 (ENET_INT_MAC
| ENET_INT_TS_TIMER
)) {
449 qemu_set_irq(s
->irq
[1], 1);
451 qemu_set_irq(s
->irq
[1], 0);
454 if (s
->regs
[ENET_EIR
] & s
->regs
[ENET_EIMR
] & ENET_INT_MAC
) {
455 qemu_set_irq(s
->irq
[0], 1);
457 qemu_set_irq(s
->irq
[0], 0);
461 static void imx_fec_do_tx(IMXFECState
*s
)
463 int frame_size
= 0, descnt
= 0;
464 uint8_t *ptr
= s
->frame
;
465 uint32_t addr
= s
->tx_descriptor
[0];
467 while (descnt
++ < IMX_MAX_DESC
) {
471 imx_fec_read_bd(&bd
, addr
);
472 FEC_PRINTF("tx_bd %x flags %04x len %d data %08x\n",
473 addr
, bd
.flags
, bd
.length
, bd
.data
);
474 if ((bd
.flags
& ENET_BD_R
) == 0) {
475 /* Run out of descriptors to transmit. */
476 FEC_PRINTF("tx_bd ran out of descriptors to transmit\n");
480 if (frame_size
+ len
> ENET_MAX_FRAME_SIZE
) {
481 len
= ENET_MAX_FRAME_SIZE
- frame_size
;
482 s
->regs
[ENET_EIR
] |= ENET_INT_BABT
;
484 dma_memory_read(&address_space_memory
, bd
.data
, ptr
, len
);
487 if (bd
.flags
& ENET_BD_L
) {
488 /* Last buffer in frame. */
489 qemu_send_packet(qemu_get_queue(s
->nic
), s
->frame
, frame_size
);
492 s
->regs
[ENET_EIR
] |= ENET_INT_TXF
;
494 s
->regs
[ENET_EIR
] |= ENET_INT_TXB
;
495 bd
.flags
&= ~ENET_BD_R
;
496 /* Write back the modified descriptor. */
497 imx_fec_write_bd(&bd
, addr
);
498 /* Advance to the next descriptor. */
499 if ((bd
.flags
& ENET_BD_W
) != 0) {
500 addr
= s
->regs
[ENET_TDSR
];
506 s
->tx_descriptor
[0] = addr
;
511 static void imx_enet_do_tx(IMXFECState
*s
, uint32_t index
)
513 int frame_size
= 0, descnt
= 0;
515 uint8_t *ptr
= s
->frame
;
516 uint32_t addr
, int_txb
, int_txf
, tdsr
;
522 int_txb
= ENET_INT_TXB
;
523 int_txf
= ENET_INT_TXF
;
528 int_txb
= ENET_INT_TXB1
;
529 int_txf
= ENET_INT_TXF1
;
534 int_txb
= ENET_INT_TXB2
;
535 int_txf
= ENET_INT_TXF2
;
539 qemu_log_mask(LOG_GUEST_ERROR
,
540 "%s: bogus value for index %x\n",
546 addr
= s
->tx_descriptor
[ring
];
548 while (descnt
++ < IMX_MAX_DESC
) {
552 imx_enet_read_bd(&bd
, addr
);
553 FEC_PRINTF("tx_bd %x flags %04x len %d data %08x option %04x "
554 "status %04x\n", addr
, bd
.flags
, bd
.length
, bd
.data
,
555 bd
.option
, bd
.status
);
556 if ((bd
.flags
& ENET_BD_R
) == 0) {
557 /* Run out of descriptors to transmit. */
561 if (frame_size
+ len
> ENET_MAX_FRAME_SIZE
) {
562 len
= ENET_MAX_FRAME_SIZE
- frame_size
;
563 s
->regs
[ENET_EIR
] |= ENET_INT_BABT
;
565 dma_memory_read(&address_space_memory
, bd
.data
, ptr
, len
);
568 if (bd
.flags
& ENET_BD_L
) {
569 if (bd
.option
& ENET_BD_PINS
) {
570 struct ip_header
*ip_hd
= PKT_GET_IP_HDR(s
->frame
);
571 if (IP_HEADER_VERSION(ip_hd
) == 4) {
572 net_checksum_calculate(s
->frame
, frame_size
);
575 if (bd
.option
& ENET_BD_IINS
) {
576 struct ip_header
*ip_hd
= PKT_GET_IP_HDR(s
->frame
);
577 /* We compute checksum only for IPv4 frames */
578 if (IP_HEADER_VERSION(ip_hd
) == 4) {
581 csum
= net_raw_checksum((uint8_t *)ip_hd
, sizeof(*ip_hd
));
582 ip_hd
->ip_sum
= cpu_to_be16(csum
);
585 /* Last buffer in frame. */
587 qemu_send_packet(qemu_get_queue(s
->nic
), s
->frame
, frame_size
);
591 if (bd
.option
& ENET_BD_TX_INT
) {
592 s
->regs
[ENET_EIR
] |= int_txf
;
595 if (bd
.option
& ENET_BD_TX_INT
) {
596 s
->regs
[ENET_EIR
] |= int_txb
;
598 bd
.flags
&= ~ENET_BD_R
;
599 /* Write back the modified descriptor. */
600 imx_enet_write_bd(&bd
, addr
);
601 /* Advance to the next descriptor. */
602 if ((bd
.flags
& ENET_BD_W
) != 0) {
603 addr
= s
->regs
[tdsr
];
609 s
->tx_descriptor
[ring
] = addr
;
614 static void imx_eth_do_tx(IMXFECState
*s
, uint32_t index
)
616 if (!s
->is_fec
&& (s
->regs
[ENET_ECR
] & ENET_ECR_EN1588
)) {
617 imx_enet_do_tx(s
, index
);
623 static void imx_eth_enable_rx(IMXFECState
*s
, bool flush
)
627 imx_fec_read_bd(&bd
, s
->rx_descriptor
);
629 s
->regs
[ENET_RDAR
] = (bd
.flags
& ENET_BD_E
) ? ENET_RDAR_RDAR
: 0;
631 if (!s
->regs
[ENET_RDAR
]) {
632 FEC_PRINTF("RX buffer full\n");
634 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
638 static void imx_eth_reset(DeviceState
*d
)
640 IMXFECState
*s
= IMX_FEC(d
);
642 /* Reset the Device */
643 memset(s
->regs
, 0, sizeof(s
->regs
));
644 s
->regs
[ENET_ECR
] = 0xf0000000;
645 s
->regs
[ENET_MIBC
] = 0xc0000000;
646 s
->regs
[ENET_RCR
] = 0x05ee0001;
647 s
->regs
[ENET_OPD
] = 0x00010000;
649 s
->regs
[ENET_PALR
] = (s
->conf
.macaddr
.a
[0] << 24)
650 | (s
->conf
.macaddr
.a
[1] << 16)
651 | (s
->conf
.macaddr
.a
[2] << 8)
652 | s
->conf
.macaddr
.a
[3];
653 s
->regs
[ENET_PAUR
] = (s
->conf
.macaddr
.a
[4] << 24)
654 | (s
->conf
.macaddr
.a
[5] << 16)
658 s
->regs
[ENET_FRBR
] = 0x00000600;
659 s
->regs
[ENET_FRSR
] = 0x00000500;
660 s
->regs
[ENET_MIIGSK_ENR
] = 0x00000006;
662 s
->regs
[ENET_RAEM
] = 0x00000004;
663 s
->regs
[ENET_RAFL
] = 0x00000004;
664 s
->regs
[ENET_TAEM
] = 0x00000004;
665 s
->regs
[ENET_TAFL
] = 0x00000008;
666 s
->regs
[ENET_TIPG
] = 0x0000000c;
667 s
->regs
[ENET_FTRL
] = 0x000007ff;
668 s
->regs
[ENET_ATPER
] = 0x3b9aca00;
671 s
->rx_descriptor
= 0;
672 memset(s
->tx_descriptor
, 0, sizeof(s
->tx_descriptor
));
674 /* We also reset the PHY */
678 static uint32_t imx_default_read(IMXFECState
*s
, uint32_t index
)
680 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
681 PRIx32
"\n", TYPE_IMX_FEC
, __func__
, index
* 4);
685 static uint32_t imx_fec_read(IMXFECState
*s
, uint32_t index
)
690 case ENET_MIIGSK_CFGR
:
691 case ENET_MIIGSK_ENR
:
692 return s
->regs
[index
];
694 return imx_default_read(s
, index
);
698 static uint32_t imx_enet_read(IMXFECState
*s
, uint32_t index
)
728 return s
->regs
[index
];
730 return imx_default_read(s
, index
);
734 static uint64_t imx_eth_read(void *opaque
, hwaddr offset
, unsigned size
)
737 IMXFECState
*s
= IMX_FEC(opaque
);
738 uint32_t index
= offset
>> 2;
762 value
= s
->regs
[index
];
766 value
= imx_fec_read(s
, index
);
768 value
= imx_enet_read(s
, index
);
773 FEC_PRINTF("reg[%s] => 0x%" PRIx32
"\n", imx_eth_reg_name(s
, index
),
779 static void imx_default_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
781 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad address at offset 0x%"
782 PRIx32
"\n", TYPE_IMX_FEC
, __func__
, index
* 4);
786 static void imx_fec_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
790 /* FRBR is read only */
791 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Register FRBR is read only\n",
792 TYPE_IMX_FEC
, __func__
);
795 s
->regs
[index
] = (value
& 0x000003fc) | 0x00000400;
797 case ENET_MIIGSK_CFGR
:
798 s
->regs
[index
] = value
& 0x00000053;
800 case ENET_MIIGSK_ENR
:
801 s
->regs
[index
] = (value
& 0x00000002) ? 0x00000006 : 0;
804 imx_default_write(s
, index
, value
);
809 static void imx_enet_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
819 s
->regs
[index
] = value
& 0x000001ff;
822 s
->regs
[index
] = value
& 0x0000001f;
825 s
->regs
[index
] = value
& 0x00003fff;
828 s
->regs
[index
] = value
& 0x00000019;
831 s
->regs
[index
] = value
& 0x000000C7;
834 s
->regs
[index
] = value
& 0x00002a9d;
839 s
->regs
[index
] = value
;
842 /* ATSTMP is read only */
843 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Register ATSTMP is read only\n",
844 TYPE_IMX_FEC
, __func__
);
847 s
->regs
[index
] = value
& 0x7fffffff;
850 s
->regs
[index
] = value
& 0x00007f7f;
853 /* implement clear timer flag */
854 value
= value
& 0x0000000f;
860 value
= value
& 0x000000fd;
866 s
->regs
[index
] = value
;
869 imx_default_write(s
, index
, value
);
874 static void imx_eth_write(void *opaque
, hwaddr offset
, uint64_t value
,
877 IMXFECState
*s
= IMX_FEC(opaque
);
878 const bool single_tx_ring
= !imx_eth_is_multi_tx_ring(s
);
879 uint32_t index
= offset
>> 2;
881 FEC_PRINTF("reg[%s] <= 0x%" PRIx32
"\n", imx_eth_reg_name(s
, index
),
886 s
->regs
[index
] &= ~value
;
889 s
->regs
[index
] = value
;
892 if (s
->regs
[ENET_ECR
] & ENET_ECR_ETHEREN
) {
893 if (!s
->regs
[index
]) {
894 imx_eth_enable_rx(s
, true);
900 case ENET_TDAR1
: /* FALLTHROUGH */
901 case ENET_TDAR2
: /* FALLTHROUGH */
902 if (unlikely(single_tx_ring
)) {
903 qemu_log_mask(LOG_GUEST_ERROR
,
904 "[%s]%s: trying to access TDAR2 or TDAR1\n",
905 TYPE_IMX_FEC
, __func__
);
908 case ENET_TDAR
: /* FALLTHROUGH */
909 if (s
->regs
[ENET_ECR
] & ENET_ECR_ETHEREN
) {
910 s
->regs
[index
] = ENET_TDAR_TDAR
;
911 imx_eth_do_tx(s
, index
);
916 if (value
& ENET_ECR_RESET
) {
917 return imx_eth_reset(DEVICE(s
));
919 s
->regs
[index
] = value
;
920 if ((s
->regs
[index
] & ENET_ECR_ETHEREN
) == 0) {
921 s
->regs
[ENET_RDAR
] = 0;
922 s
->rx_descriptor
= s
->regs
[ENET_RDSR
];
923 s
->regs
[ENET_TDAR
] = 0;
924 s
->regs
[ENET_TDAR1
] = 0;
925 s
->regs
[ENET_TDAR2
] = 0;
926 s
->tx_descriptor
[0] = s
->regs
[ENET_TDSR
];
927 s
->tx_descriptor
[1] = s
->regs
[ENET_TDSR1
];
928 s
->tx_descriptor
[2] = s
->regs
[ENET_TDSR2
];
932 s
->regs
[index
] = value
;
933 if (extract32(value
, 29, 1)) {
934 /* This is a read operation */
935 s
->regs
[ENET_MMFR
] = deposit32(s
->regs
[ENET_MMFR
], 0, 16,
940 /* This a write operation */
941 do_phy_write(s
, extract32(value
, 18, 10), extract32(value
, 0, 16));
943 /* raise the interrupt as the PHY operation is done */
944 s
->regs
[ENET_EIR
] |= ENET_INT_MII
;
947 s
->regs
[index
] = value
& 0xfe;
950 /* TODO: Implement MIB. */
951 s
->regs
[index
] = (value
& 0x80000000) ? 0xc0000000 : 0;
954 s
->regs
[index
] = value
& 0x07ff003f;
955 /* TODO: Implement LOOP mode. */
958 /* We transmit immediately, so raise GRA immediately. */
959 s
->regs
[index
] = value
;
961 s
->regs
[ENET_EIR
] |= ENET_INT_GRA
;
965 s
->regs
[index
] = value
;
966 s
->conf
.macaddr
.a
[0] = value
>> 24;
967 s
->conf
.macaddr
.a
[1] = value
>> 16;
968 s
->conf
.macaddr
.a
[2] = value
>> 8;
969 s
->conf
.macaddr
.a
[3] = value
;
972 s
->regs
[index
] = (value
| 0x0000ffff) & 0xffff8808;
973 s
->conf
.macaddr
.a
[4] = value
>> 24;
974 s
->conf
.macaddr
.a
[5] = value
>> 16;
977 s
->regs
[index
] = (value
& 0x0000ffff) | 0x00010000;
983 /* TODO: implement MAC hash filtering. */
987 s
->regs
[index
] = value
& 0x3;
989 s
->regs
[index
] = value
& 0x13f;
994 s
->regs
[index
] = value
& ~3;
996 s
->regs
[index
] = value
& ~7;
998 s
->rx_descriptor
= s
->regs
[index
];
1002 s
->regs
[index
] = value
& ~3;
1004 s
->regs
[index
] = value
& ~7;
1006 s
->tx_descriptor
[0] = s
->regs
[index
];
1009 if (unlikely(single_tx_ring
)) {
1010 qemu_log_mask(LOG_GUEST_ERROR
,
1011 "[%s]%s: trying to access TDSR1\n",
1012 TYPE_IMX_FEC
, __func__
);
1016 s
->regs
[index
] = value
& ~7;
1017 s
->tx_descriptor
[1] = s
->regs
[index
];
1020 if (unlikely(single_tx_ring
)) {
1021 qemu_log_mask(LOG_GUEST_ERROR
,
1022 "[%s]%s: trying to access TDSR2\n",
1023 TYPE_IMX_FEC
, __func__
);
1027 s
->regs
[index
] = value
& ~7;
1028 s
->tx_descriptor
[2] = s
->regs
[index
];
1031 s
->regs
[index
] = value
& 0x00003ff0;
1035 imx_fec_write(s
, index
, value
);
1037 imx_enet_write(s
, index
, value
);
1045 static int imx_eth_can_receive(NetClientState
*nc
)
1047 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1051 return !!s
->regs
[ENET_RDAR
];
1054 static ssize_t
imx_fec_receive(NetClientState
*nc
, const uint8_t *buf
,
1057 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1064 unsigned int buf_len
;
1067 FEC_PRINTF("len %d\n", (int)size
);
1069 if (!s
->regs
[ENET_RDAR
]) {
1070 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Unexpected packet\n",
1071 TYPE_IMX_FEC
, __func__
);
1075 /* 4 bytes for the CRC. */
1077 crc
= cpu_to_be32(crc32(~0, buf
, size
));
1078 crc_ptr
= (uint8_t *) &crc
;
1080 /* Huge frames are truncated. */
1081 if (size
> ENET_MAX_FRAME_SIZE
) {
1082 size
= ENET_MAX_FRAME_SIZE
;
1083 flags
|= ENET_BD_TR
| ENET_BD_LG
;
1086 /* Frames larger than the user limit just set error flags. */
1087 if (size
> (s
->regs
[ENET_RCR
] >> 16)) {
1088 flags
|= ENET_BD_LG
;
1091 addr
= s
->rx_descriptor
;
1093 imx_fec_read_bd(&bd
, addr
);
1094 if ((bd
.flags
& ENET_BD_E
) == 0) {
1095 /* No descriptors available. Bail out. */
1097 * FIXME: This is wrong. We should probably either
1098 * save the remainder for when more RX buffers are
1099 * available, or flag an error.
1101 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Lost end of frame\n",
1102 TYPE_IMX_FEC
, __func__
);
1105 buf_len
= (size
<= s
->regs
[ENET_MRBR
]) ? size
: s
->regs
[ENET_MRBR
];
1106 bd
.length
= buf_len
;
1109 FEC_PRINTF("rx_bd 0x%x length %d\n", addr
, bd
.length
);
1111 /* The last 4 bytes are the CRC. */
1113 buf_len
+= size
- 4;
1116 dma_memory_write(&address_space_memory
, buf_addr
, buf
, buf_len
);
1119 dma_memory_write(&address_space_memory
, buf_addr
+ buf_len
,
1121 crc_ptr
+= 4 - size
;
1123 bd
.flags
&= ~ENET_BD_E
;
1125 /* Last buffer in frame. */
1126 bd
.flags
|= flags
| ENET_BD_L
;
1127 FEC_PRINTF("rx frame flags %04x\n", bd
.flags
);
1128 s
->regs
[ENET_EIR
] |= ENET_INT_RXF
;
1130 s
->regs
[ENET_EIR
] |= ENET_INT_RXB
;
1132 imx_fec_write_bd(&bd
, addr
);
1133 /* Advance to the next descriptor. */
1134 if ((bd
.flags
& ENET_BD_W
) != 0) {
1135 addr
= s
->regs
[ENET_RDSR
];
1140 s
->rx_descriptor
= addr
;
1141 imx_eth_enable_rx(s
, false);
1146 static ssize_t
imx_enet_receive(NetClientState
*nc
, const uint8_t *buf
,
1149 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1156 unsigned int buf_len
;
1158 bool shift16
= s
->regs
[ENET_RACC
] & ENET_RACC_SHIFT16
;
1160 FEC_PRINTF("len %d\n", (int)size
);
1162 if (!s
->regs
[ENET_RDAR
]) {
1163 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Unexpected packet\n",
1164 TYPE_IMX_FEC
, __func__
);
1168 /* 4 bytes for the CRC. */
1170 crc
= cpu_to_be32(crc32(~0, buf
, size
));
1171 crc_ptr
= (uint8_t *) &crc
;
1177 /* Huge frames are truncated. */
1178 if (size
> s
->regs
[ENET_FTRL
]) {
1179 size
= s
->regs
[ENET_FTRL
];
1180 flags
|= ENET_BD_TR
| ENET_BD_LG
;
1183 /* Frames larger than the user limit just set error flags. */
1184 if (size
> (s
->regs
[ENET_RCR
] >> 16)) {
1185 flags
|= ENET_BD_LG
;
1188 addr
= s
->rx_descriptor
;
1190 imx_enet_read_bd(&bd
, addr
);
1191 if ((bd
.flags
& ENET_BD_E
) == 0) {
1192 /* No descriptors available. Bail out. */
1194 * FIXME: This is wrong. We should probably either
1195 * save the remainder for when more RX buffers are
1196 * available, or flag an error.
1198 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Lost end of frame\n",
1199 TYPE_IMX_FEC
, __func__
);
1202 buf_len
= MIN(size
, s
->regs
[ENET_MRBR
]);
1203 bd
.length
= buf_len
;
1206 FEC_PRINTF("rx_bd 0x%x length %d\n", addr
, bd
.length
);
1208 /* The last 4 bytes are the CRC. */
1210 buf_len
+= size
- 4;
1216 * If SHIFT16 bit of ENETx_RACC register is set we need to
1217 * align the payload to 4-byte boundary.
1219 const uint8_t zeros
[2] = { 0 };
1221 dma_memory_write(&address_space_memory
, buf_addr
,
1222 zeros
, sizeof(zeros
));
1224 buf_addr
+= sizeof(zeros
);
1225 buf_len
-= sizeof(zeros
);
1227 /* We only do this once per Ethernet frame */
1231 dma_memory_write(&address_space_memory
, buf_addr
, buf
, buf_len
);
1234 dma_memory_write(&address_space_memory
, buf_addr
+ buf_len
,
1236 crc_ptr
+= 4 - size
;
1238 bd
.flags
&= ~ENET_BD_E
;
1240 /* Last buffer in frame. */
1241 bd
.flags
|= flags
| ENET_BD_L
;
1242 FEC_PRINTF("rx frame flags %04x\n", bd
.flags
);
1243 if (bd
.option
& ENET_BD_RX_INT
) {
1244 s
->regs
[ENET_EIR
] |= ENET_INT_RXF
;
1247 if (bd
.option
& ENET_BD_RX_INT
) {
1248 s
->regs
[ENET_EIR
] |= ENET_INT_RXB
;
1251 imx_enet_write_bd(&bd
, addr
);
1252 /* Advance to the next descriptor. */
1253 if ((bd
.flags
& ENET_BD_W
) != 0) {
1254 addr
= s
->regs
[ENET_RDSR
];
1259 s
->rx_descriptor
= addr
;
1260 imx_eth_enable_rx(s
, false);
1265 static ssize_t
imx_eth_receive(NetClientState
*nc
, const uint8_t *buf
,
1268 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1270 if (!s
->is_fec
&& (s
->regs
[ENET_ECR
] & ENET_ECR_EN1588
)) {
1271 return imx_enet_receive(nc
, buf
, len
);
1273 return imx_fec_receive(nc
, buf
, len
);
1277 static const MemoryRegionOps imx_eth_ops
= {
1278 .read
= imx_eth_read
,
1279 .write
= imx_eth_write
,
1280 .valid
.min_access_size
= 4,
1281 .valid
.max_access_size
= 4,
1282 .endianness
= DEVICE_NATIVE_ENDIAN
,
1285 static void imx_eth_cleanup(NetClientState
*nc
)
1287 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1292 static NetClientInfo imx_eth_net_info
= {
1293 .type
= NET_CLIENT_DRIVER_NIC
,
1294 .size
= sizeof(NICState
),
1295 .can_receive
= imx_eth_can_receive
,
1296 .receive
= imx_eth_receive
,
1297 .cleanup
= imx_eth_cleanup
,
1298 .link_status_changed
= imx_eth_set_link
,
1302 static void imx_eth_realize(DeviceState
*dev
, Error
**errp
)
1304 IMXFECState
*s
= IMX_FEC(dev
);
1305 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1307 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx_eth_ops
, s
,
1308 TYPE_IMX_FEC
, FSL_IMX25_FEC_SIZE
);
1309 sysbus_init_mmio(sbd
, &s
->iomem
);
1310 sysbus_init_irq(sbd
, &s
->irq
[0]);
1311 sysbus_init_irq(sbd
, &s
->irq
[1]);
1313 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
1315 s
->nic
= qemu_new_nic(&imx_eth_net_info
, &s
->conf
,
1316 object_get_typename(OBJECT(dev
)),
1317 DEVICE(dev
)->id
, s
);
1319 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
1322 static Property imx_eth_properties
[] = {
1323 DEFINE_NIC_PROPERTIES(IMXFECState
, conf
),
1324 DEFINE_PROP_UINT32("tx-ring-num", IMXFECState
, tx_ring_num
, 1),
1325 DEFINE_PROP_END_OF_LIST(),
1328 static void imx_eth_class_init(ObjectClass
*klass
, void *data
)
1330 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1332 dc
->vmsd
= &vmstate_imx_eth
;
1333 dc
->reset
= imx_eth_reset
;
1334 dc
->props
= imx_eth_properties
;
1335 dc
->realize
= imx_eth_realize
;
1336 dc
->desc
= "i.MX FEC/ENET Ethernet Controller";
1339 static void imx_fec_init(Object
*obj
)
1341 IMXFECState
*s
= IMX_FEC(obj
);
1346 static void imx_enet_init(Object
*obj
)
1348 IMXFECState
*s
= IMX_FEC(obj
);
1353 static const TypeInfo imx_fec_info
= {
1354 .name
= TYPE_IMX_FEC
,
1355 .parent
= TYPE_SYS_BUS_DEVICE
,
1356 .instance_size
= sizeof(IMXFECState
),
1357 .instance_init
= imx_fec_init
,
1358 .class_init
= imx_eth_class_init
,
1361 static const TypeInfo imx_enet_info
= {
1362 .name
= TYPE_IMX_ENET
,
1363 .parent
= TYPE_IMX_FEC
,
1364 .instance_init
= imx_enet_init
,
1367 static void imx_eth_register_types(void)
1369 type_register_static(&imx_fec_info
);
1370 type_register_static(&imx_enet_info
);
1373 type_init(imx_eth_register_types
)