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"
25 #include "hw/net/imx_fec.h"
26 #include "sysemu/dma.h"
28 #include "net/checksum.h"
35 #define DEBUG_IMX_FEC 0
38 #define FEC_PRINTF(fmt, args...) \
40 if (DEBUG_IMX_FEC) { \
41 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_FEC, \
47 #define DEBUG_IMX_PHY 0
50 #define PHY_PRINTF(fmt, args...) \
52 if (DEBUG_IMX_PHY) { \
53 fprintf(stderr, "[%s.phy]%s: " fmt , TYPE_IMX_FEC, \
58 #define IMX_MAX_DESC 1024
60 static const char *imx_default_reg_name(IMXFECState
*s
, uint32_t index
)
63 sprintf(tmp
, "index %d", index
);
67 static const char *imx_fec_reg_name(IMXFECState
*s
, uint32_t index
)
74 case ENET_MIIGSK_CFGR
:
79 return imx_default_reg_name(s
, index
);
83 static const char *imx_enet_reg_name(IMXFECState
*s
, uint32_t index
)
141 return imx_default_reg_name(s
, index
);
145 static const char *imx_eth_reg_name(IMXFECState
*s
, uint32_t index
)
192 return imx_fec_reg_name(s
, index
);
194 return imx_enet_reg_name(s
, index
);
200 * Versions of this device with more than one TX descriptor save the
201 * 2nd and 3rd descriptors in a subsection, to maintain migration
202 * compatibility with previous versions of the device that only
203 * supported a single descriptor.
205 static bool imx_eth_is_multi_tx_ring(void *opaque
)
207 IMXFECState
*s
= IMX_FEC(opaque
);
209 return s
->tx_ring_num
> 1;
212 static const VMStateDescription vmstate_imx_eth_txdescs
= {
213 .name
= "imx.fec/txdescs",
215 .minimum_version_id
= 1,
216 .needed
= imx_eth_is_multi_tx_ring
,
217 .fields
= (VMStateField
[]) {
218 VMSTATE_UINT32(tx_descriptor
[1], IMXFECState
),
219 VMSTATE_UINT32(tx_descriptor
[2], IMXFECState
),
220 VMSTATE_END_OF_LIST()
224 static const VMStateDescription vmstate_imx_eth
= {
225 .name
= TYPE_IMX_FEC
,
227 .minimum_version_id
= 2,
228 .fields
= (VMStateField
[]) {
229 VMSTATE_UINT32_ARRAY(regs
, IMXFECState
, ENET_MAX
),
230 VMSTATE_UINT32(rx_descriptor
, IMXFECState
),
231 VMSTATE_UINT32(tx_descriptor
[0], IMXFECState
),
232 VMSTATE_UINT32(phy_status
, IMXFECState
),
233 VMSTATE_UINT32(phy_control
, IMXFECState
),
234 VMSTATE_UINT32(phy_advertise
, IMXFECState
),
235 VMSTATE_UINT32(phy_int
, IMXFECState
),
236 VMSTATE_UINT32(phy_int_mask
, IMXFECState
),
237 VMSTATE_END_OF_LIST()
239 .subsections
= (const VMStateDescription
* []) {
240 &vmstate_imx_eth_txdescs
,
245 #define PHY_INT_ENERGYON (1 << 7)
246 #define PHY_INT_AUTONEG_COMPLETE (1 << 6)
247 #define PHY_INT_FAULT (1 << 5)
248 #define PHY_INT_DOWN (1 << 4)
249 #define PHY_INT_AUTONEG_LP (1 << 3)
250 #define PHY_INT_PARFAULT (1 << 2)
251 #define PHY_INT_AUTONEG_PAGE (1 << 1)
253 static void imx_eth_update(IMXFECState
*s
);
256 * The MII phy could raise a GPIO to the processor which in turn
257 * could be handled as an interrpt by the OS.
258 * For now we don't handle any GPIO/interrupt line, so the OS will
259 * have to poll for the PHY status.
261 static void phy_update_irq(IMXFECState
*s
)
266 static void phy_update_link(IMXFECState
*s
)
268 /* Autonegotiation status mirrors link status. */
269 if (qemu_get_queue(s
->nic
)->link_down
) {
270 PHY_PRINTF("link is down\n");
271 s
->phy_status
&= ~0x0024;
272 s
->phy_int
|= PHY_INT_DOWN
;
274 PHY_PRINTF("link is up\n");
275 s
->phy_status
|= 0x0024;
276 s
->phy_int
|= PHY_INT_ENERGYON
;
277 s
->phy_int
|= PHY_INT_AUTONEG_COMPLETE
;
282 static void imx_eth_set_link(NetClientState
*nc
)
284 phy_update_link(IMX_FEC(qemu_get_nic_opaque(nc
)));
287 static void phy_reset(IMXFECState
*s
)
289 s
->phy_status
= 0x7809;
290 s
->phy_control
= 0x3000;
291 s
->phy_advertise
= 0x01e1;
297 static uint32_t do_phy_read(IMXFECState
*s
, int reg
)
302 /* we only advertise one phy */
307 case 0: /* Basic Control */
308 val
= s
->phy_control
;
310 case 1: /* Basic Status */
319 case 4: /* Auto-neg advertisement */
320 val
= s
->phy_advertise
;
322 case 5: /* Auto-neg Link Partner Ability */
325 case 6: /* Auto-neg Expansion */
328 case 29: /* Interrupt source. */
333 case 30: /* Interrupt mask */
334 val
= s
->phy_int_mask
;
340 qemu_log_mask(LOG_UNIMP
, "[%s.phy]%s: reg %d not implemented\n",
341 TYPE_IMX_FEC
, __func__
, reg
);
345 qemu_log_mask(LOG_GUEST_ERROR
, "[%s.phy]%s: Bad address at offset %d\n",
346 TYPE_IMX_FEC
, __func__
, reg
);
351 PHY_PRINTF("read 0x%04x @ %d\n", val
, reg
);
356 static void do_phy_write(IMXFECState
*s
, int reg
, uint32_t val
)
358 PHY_PRINTF("write 0x%04x @ %d\n", val
, reg
);
361 /* we only advertise one phy */
366 case 0: /* Basic Control */
370 s
->phy_control
= val
& 0x7980;
371 /* Complete autonegotiation immediately. */
373 s
->phy_status
|= 0x0020;
377 case 4: /* Auto-neg advertisement */
378 s
->phy_advertise
= (val
& 0x2d7f) | 0x80;
380 case 30: /* Interrupt mask */
381 s
->phy_int_mask
= val
& 0xff;
388 qemu_log_mask(LOG_UNIMP
, "[%s.phy)%s: reg %d not implemented\n",
389 TYPE_IMX_FEC
, __func__
, reg
);
392 qemu_log_mask(LOG_GUEST_ERROR
, "[%s.phy]%s: Bad address at offset %d\n",
393 TYPE_IMX_FEC
, __func__
, reg
);
398 static void imx_fec_read_bd(IMXFECBufDesc
*bd
, dma_addr_t addr
)
400 dma_memory_read(&address_space_memory
, addr
, bd
, sizeof(*bd
));
403 static void imx_fec_write_bd(IMXFECBufDesc
*bd
, dma_addr_t addr
)
405 dma_memory_write(&address_space_memory
, addr
, bd
, sizeof(*bd
));
408 static void imx_enet_read_bd(IMXENETBufDesc
*bd
, dma_addr_t addr
)
410 dma_memory_read(&address_space_memory
, addr
, bd
, sizeof(*bd
));
413 static void imx_enet_write_bd(IMXENETBufDesc
*bd
, dma_addr_t addr
)
415 dma_memory_write(&address_space_memory
, addr
, bd
, sizeof(*bd
));
418 static void imx_eth_update(IMXFECState
*s
)
420 if (s
->regs
[ENET_EIR
] & s
->regs
[ENET_EIMR
] & ENET_INT_TS_TIMER
) {
421 qemu_set_irq(s
->irq
[1], 1);
423 qemu_set_irq(s
->irq
[1], 0);
426 if (s
->regs
[ENET_EIR
] & s
->regs
[ENET_EIMR
] & ENET_INT_MAC
) {
427 qemu_set_irq(s
->irq
[0], 1);
429 qemu_set_irq(s
->irq
[0], 0);
433 static void imx_fec_do_tx(IMXFECState
*s
)
435 int frame_size
= 0, descnt
= 0;
436 uint8_t *ptr
= s
->frame
;
437 uint32_t addr
= s
->tx_descriptor
[0];
439 while (descnt
++ < IMX_MAX_DESC
) {
443 imx_fec_read_bd(&bd
, addr
);
444 FEC_PRINTF("tx_bd %x flags %04x len %d data %08x\n",
445 addr
, bd
.flags
, bd
.length
, bd
.data
);
446 if ((bd
.flags
& ENET_BD_R
) == 0) {
447 /* Run out of descriptors to transmit. */
448 FEC_PRINTF("tx_bd ran out of descriptors to transmit\n");
452 if (frame_size
+ len
> ENET_MAX_FRAME_SIZE
) {
453 len
= ENET_MAX_FRAME_SIZE
- frame_size
;
454 s
->regs
[ENET_EIR
] |= ENET_INT_BABT
;
456 dma_memory_read(&address_space_memory
, bd
.data
, ptr
, len
);
459 if (bd
.flags
& ENET_BD_L
) {
460 /* Last buffer in frame. */
461 qemu_send_packet(qemu_get_queue(s
->nic
), s
->frame
, frame_size
);
464 s
->regs
[ENET_EIR
] |= ENET_INT_TXF
;
466 s
->regs
[ENET_EIR
] |= ENET_INT_TXB
;
467 bd
.flags
&= ~ENET_BD_R
;
468 /* Write back the modified descriptor. */
469 imx_fec_write_bd(&bd
, addr
);
470 /* Advance to the next descriptor. */
471 if ((bd
.flags
& ENET_BD_W
) != 0) {
472 addr
= s
->regs
[ENET_TDSR
];
478 s
->tx_descriptor
[0] = addr
;
483 static void imx_enet_do_tx(IMXFECState
*s
, uint32_t index
)
485 int frame_size
= 0, descnt
= 0;
487 uint8_t *ptr
= s
->frame
;
488 uint32_t addr
, int_txb
, int_txf
, tdsr
;
494 int_txb
= ENET_INT_TXB
;
495 int_txf
= ENET_INT_TXF
;
500 int_txb
= ENET_INT_TXB1
;
501 int_txf
= ENET_INT_TXF1
;
506 int_txb
= ENET_INT_TXB2
;
507 int_txf
= ENET_INT_TXF2
;
511 qemu_log_mask(LOG_GUEST_ERROR
,
512 "%s: bogus value for index %x\n",
518 addr
= s
->tx_descriptor
[ring
];
520 while (descnt
++ < IMX_MAX_DESC
) {
524 imx_enet_read_bd(&bd
, addr
);
525 FEC_PRINTF("tx_bd %x flags %04x len %d data %08x option %04x "
526 "status %04x\n", addr
, bd
.flags
, bd
.length
, bd
.data
,
527 bd
.option
, bd
.status
);
528 if ((bd
.flags
& ENET_BD_R
) == 0) {
529 /* Run out of descriptors to transmit. */
533 if (frame_size
+ len
> ENET_MAX_FRAME_SIZE
) {
534 len
= ENET_MAX_FRAME_SIZE
- frame_size
;
535 s
->regs
[ENET_EIR
] |= ENET_INT_BABT
;
537 dma_memory_read(&address_space_memory
, bd
.data
, ptr
, len
);
540 if (bd
.flags
& ENET_BD_L
) {
541 if (bd
.option
& ENET_BD_PINS
) {
542 struct ip_header
*ip_hd
= PKT_GET_IP_HDR(s
->frame
);
543 if (IP_HEADER_VERSION(ip_hd
) == 4) {
544 net_checksum_calculate(s
->frame
, frame_size
);
547 if (bd
.option
& ENET_BD_IINS
) {
548 struct ip_header
*ip_hd
= PKT_GET_IP_HDR(s
->frame
);
549 /* We compute checksum only for IPv4 frames */
550 if (IP_HEADER_VERSION(ip_hd
) == 4) {
553 csum
= net_raw_checksum((uint8_t *)ip_hd
, sizeof(*ip_hd
));
554 ip_hd
->ip_sum
= cpu_to_be16(csum
);
557 /* Last buffer in frame. */
559 qemu_send_packet(qemu_get_queue(s
->nic
), s
->frame
, frame_size
);
563 if (bd
.option
& ENET_BD_TX_INT
) {
564 s
->regs
[ENET_EIR
] |= int_txf
;
567 if (bd
.option
& ENET_BD_TX_INT
) {
568 s
->regs
[ENET_EIR
] |= int_txb
;
570 bd
.flags
&= ~ENET_BD_R
;
571 /* Write back the modified descriptor. */
572 imx_enet_write_bd(&bd
, addr
);
573 /* Advance to the next descriptor. */
574 if ((bd
.flags
& ENET_BD_W
) != 0) {
575 addr
= s
->regs
[tdsr
];
581 s
->tx_descriptor
[ring
] = addr
;
586 static void imx_eth_do_tx(IMXFECState
*s
, uint32_t index
)
588 if (!s
->is_fec
&& (s
->regs
[ENET_ECR
] & ENET_ECR_EN1588
)) {
589 imx_enet_do_tx(s
, index
);
595 static void imx_eth_enable_rx(IMXFECState
*s
, bool flush
)
600 imx_fec_read_bd(&bd
, s
->rx_descriptor
);
602 rx_ring_full
= !(bd
.flags
& ENET_BD_E
);
605 FEC_PRINTF("RX buffer full\n");
607 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
610 s
->regs
[ENET_RDAR
] = rx_ring_full
? 0 : ENET_RDAR_RDAR
;
613 static void imx_eth_reset(DeviceState
*d
)
615 IMXFECState
*s
= IMX_FEC(d
);
617 /* Reset the Device */
618 memset(s
->regs
, 0, sizeof(s
->regs
));
619 s
->regs
[ENET_ECR
] = 0xf0000000;
620 s
->regs
[ENET_MIBC
] = 0xc0000000;
621 s
->regs
[ENET_RCR
] = 0x05ee0001;
622 s
->regs
[ENET_OPD
] = 0x00010000;
624 s
->regs
[ENET_PALR
] = (s
->conf
.macaddr
.a
[0] << 24)
625 | (s
->conf
.macaddr
.a
[1] << 16)
626 | (s
->conf
.macaddr
.a
[2] << 8)
627 | s
->conf
.macaddr
.a
[3];
628 s
->regs
[ENET_PAUR
] = (s
->conf
.macaddr
.a
[4] << 24)
629 | (s
->conf
.macaddr
.a
[5] << 16)
633 s
->regs
[ENET_FRBR
] = 0x00000600;
634 s
->regs
[ENET_FRSR
] = 0x00000500;
635 s
->regs
[ENET_MIIGSK_ENR
] = 0x00000006;
637 s
->regs
[ENET_RAEM
] = 0x00000004;
638 s
->regs
[ENET_RAFL
] = 0x00000004;
639 s
->regs
[ENET_TAEM
] = 0x00000004;
640 s
->regs
[ENET_TAFL
] = 0x00000008;
641 s
->regs
[ENET_TIPG
] = 0x0000000c;
642 s
->regs
[ENET_FTRL
] = 0x000007ff;
643 s
->regs
[ENET_ATPER
] = 0x3b9aca00;
646 s
->rx_descriptor
= 0;
647 memset(s
->tx_descriptor
, 0, sizeof(s
->tx_descriptor
));
649 /* We also reset the PHY */
653 static uint32_t imx_default_read(IMXFECState
*s
, uint32_t index
)
655 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
656 PRIx32
"\n", TYPE_IMX_FEC
, __func__
, index
* 4);
660 static uint32_t imx_fec_read(IMXFECState
*s
, uint32_t index
)
665 case ENET_MIIGSK_CFGR
:
666 case ENET_MIIGSK_ENR
:
667 return s
->regs
[index
];
669 return imx_default_read(s
, index
);
673 static uint32_t imx_enet_read(IMXFECState
*s
, uint32_t index
)
703 return s
->regs
[index
];
705 return imx_default_read(s
, index
);
709 static uint64_t imx_eth_read(void *opaque
, hwaddr offset
, unsigned size
)
712 IMXFECState
*s
= IMX_FEC(opaque
);
713 uint32_t index
= offset
>> 2;
737 value
= s
->regs
[index
];
741 value
= imx_fec_read(s
, index
);
743 value
= imx_enet_read(s
, index
);
748 FEC_PRINTF("reg[%s] => 0x%" PRIx32
"\n", imx_eth_reg_name(s
, index
),
754 static void imx_default_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
756 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad address at offset 0x%"
757 PRIx32
"\n", TYPE_IMX_FEC
, __func__
, index
* 4);
761 static void imx_fec_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
765 /* FRBR is read only */
766 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Register FRBR is read only\n",
767 TYPE_IMX_FEC
, __func__
);
770 s
->regs
[index
] = (value
& 0x000003fc) | 0x00000400;
772 case ENET_MIIGSK_CFGR
:
773 s
->regs
[index
] = value
& 0x00000053;
775 case ENET_MIIGSK_ENR
:
776 s
->regs
[index
] = (value
& 0x00000002) ? 0x00000006 : 0;
779 imx_default_write(s
, index
, value
);
784 static void imx_enet_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
794 s
->regs
[index
] = value
& 0x000001ff;
797 s
->regs
[index
] = value
& 0x0000001f;
800 s
->regs
[index
] = value
& 0x00003fff;
803 s
->regs
[index
] = value
& 0x00000019;
806 s
->regs
[index
] = value
& 0x000000C7;
809 s
->regs
[index
] = value
& 0x00002a9d;
814 s
->regs
[index
] = value
;
817 /* ATSTMP is read only */
818 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Register ATSTMP is read only\n",
819 TYPE_IMX_FEC
, __func__
);
822 s
->regs
[index
] = value
& 0x7fffffff;
825 s
->regs
[index
] = value
& 0x00007f7f;
828 /* implement clear timer flag */
829 value
= value
& 0x0000000f;
835 value
= value
& 0x000000fd;
841 s
->regs
[index
] = value
;
844 imx_default_write(s
, index
, value
);
849 static void imx_eth_write(void *opaque
, hwaddr offset
, uint64_t value
,
852 IMXFECState
*s
= IMX_FEC(opaque
);
853 const bool single_tx_ring
= !imx_eth_is_multi_tx_ring(s
);
854 uint32_t index
= offset
>> 2;
856 FEC_PRINTF("reg[%s] <= 0x%" PRIx32
"\n", imx_eth_reg_name(s
, index
),
861 s
->regs
[index
] &= ~value
;
864 s
->regs
[index
] = value
;
867 if (s
->regs
[ENET_ECR
] & ENET_ECR_ETHEREN
) {
868 if (!s
->regs
[index
]) {
869 s
->regs
[index
] = ENET_RDAR_RDAR
;
870 imx_eth_enable_rx(s
, true);
876 case ENET_TDAR1
: /* FALLTHROUGH */
877 case ENET_TDAR2
: /* FALLTHROUGH */
878 if (unlikely(single_tx_ring
)) {
879 qemu_log_mask(LOG_GUEST_ERROR
,
880 "[%s]%s: trying to access TDAR2 or TDAR1\n",
881 TYPE_IMX_FEC
, __func__
);
884 case ENET_TDAR
: /* FALLTHROUGH */
885 if (s
->regs
[ENET_ECR
] & ENET_ECR_ETHEREN
) {
886 s
->regs
[index
] = ENET_TDAR_TDAR
;
887 imx_eth_do_tx(s
, index
);
892 if (value
& ENET_ECR_RESET
) {
893 return imx_eth_reset(DEVICE(s
));
895 s
->regs
[index
] = value
;
896 if ((s
->regs
[index
] & ENET_ECR_ETHEREN
) == 0) {
897 s
->regs
[ENET_RDAR
] = 0;
898 s
->rx_descriptor
= s
->regs
[ENET_RDSR
];
899 s
->regs
[ENET_TDAR
] = 0;
900 s
->regs
[ENET_TDAR1
] = 0;
901 s
->regs
[ENET_TDAR2
] = 0;
902 s
->tx_descriptor
[0] = s
->regs
[ENET_TDSR
];
903 s
->tx_descriptor
[1] = s
->regs
[ENET_TDSR1
];
904 s
->tx_descriptor
[2] = s
->regs
[ENET_TDSR2
];
908 s
->regs
[index
] = value
;
909 if (extract32(value
, 29, 1)) {
910 /* This is a read operation */
911 s
->regs
[ENET_MMFR
] = deposit32(s
->regs
[ENET_MMFR
], 0, 16,
916 /* This a write operation */
917 do_phy_write(s
, extract32(value
, 18, 10), extract32(value
, 0, 16));
919 /* raise the interrupt as the PHY operation is done */
920 s
->regs
[ENET_EIR
] |= ENET_INT_MII
;
923 s
->regs
[index
] = value
& 0xfe;
926 /* TODO: Implement MIB. */
927 s
->regs
[index
] = (value
& 0x80000000) ? 0xc0000000 : 0;
930 s
->regs
[index
] = value
& 0x07ff003f;
931 /* TODO: Implement LOOP mode. */
934 /* We transmit immediately, so raise GRA immediately. */
935 s
->regs
[index
] = value
;
937 s
->regs
[ENET_EIR
] |= ENET_INT_GRA
;
941 s
->regs
[index
] = value
;
942 s
->conf
.macaddr
.a
[0] = value
>> 24;
943 s
->conf
.macaddr
.a
[1] = value
>> 16;
944 s
->conf
.macaddr
.a
[2] = value
>> 8;
945 s
->conf
.macaddr
.a
[3] = value
;
948 s
->regs
[index
] = (value
| 0x0000ffff) & 0xffff8808;
949 s
->conf
.macaddr
.a
[4] = value
>> 24;
950 s
->conf
.macaddr
.a
[5] = value
>> 16;
953 s
->regs
[index
] = (value
& 0x0000ffff) | 0x00010000;
959 /* TODO: implement MAC hash filtering. */
963 s
->regs
[index
] = value
& 0x3;
965 s
->regs
[index
] = value
& 0x13f;
970 s
->regs
[index
] = value
& ~3;
972 s
->regs
[index
] = value
& ~7;
974 s
->rx_descriptor
= s
->regs
[index
];
978 s
->regs
[index
] = value
& ~3;
980 s
->regs
[index
] = value
& ~7;
982 s
->tx_descriptor
[0] = s
->regs
[index
];
985 if (unlikely(single_tx_ring
)) {
986 qemu_log_mask(LOG_GUEST_ERROR
,
987 "[%s]%s: trying to access TDSR1\n",
988 TYPE_IMX_FEC
, __func__
);
992 s
->regs
[index
] = value
& ~7;
993 s
->tx_descriptor
[1] = s
->regs
[index
];
996 if (unlikely(single_tx_ring
)) {
997 qemu_log_mask(LOG_GUEST_ERROR
,
998 "[%s]%s: trying to access TDSR2\n",
999 TYPE_IMX_FEC
, __func__
);
1003 s
->regs
[index
] = value
& ~7;
1004 s
->tx_descriptor
[2] = s
->regs
[index
];
1007 s
->regs
[index
] = value
& 0x00003ff0;
1011 imx_fec_write(s
, index
, value
);
1013 imx_enet_write(s
, index
, value
);
1021 static int imx_eth_can_receive(NetClientState
*nc
)
1023 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1027 return !!s
->regs
[ENET_RDAR
];
1030 static ssize_t
imx_fec_receive(NetClientState
*nc
, const uint8_t *buf
,
1033 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1040 unsigned int buf_len
;
1043 FEC_PRINTF("len %d\n", (int)size
);
1045 if (!s
->regs
[ENET_RDAR
]) {
1046 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Unexpected packet\n",
1047 TYPE_IMX_FEC
, __func__
);
1051 /* 4 bytes for the CRC. */
1053 crc
= cpu_to_be32(crc32(~0, buf
, size
));
1054 crc_ptr
= (uint8_t *) &crc
;
1056 /* Huge frames are truncated. */
1057 if (size
> ENET_MAX_FRAME_SIZE
) {
1058 size
= ENET_MAX_FRAME_SIZE
;
1059 flags
|= ENET_BD_TR
| ENET_BD_LG
;
1062 /* Frames larger than the user limit just set error flags. */
1063 if (size
> (s
->regs
[ENET_RCR
] >> 16)) {
1064 flags
|= ENET_BD_LG
;
1067 addr
= s
->rx_descriptor
;
1069 imx_fec_read_bd(&bd
, addr
);
1070 if ((bd
.flags
& ENET_BD_E
) == 0) {
1071 /* No descriptors available. Bail out. */
1073 * FIXME: This is wrong. We should probably either
1074 * save the remainder for when more RX buffers are
1075 * available, or flag an error.
1077 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Lost end of frame\n",
1078 TYPE_IMX_FEC
, __func__
);
1081 buf_len
= (size
<= s
->regs
[ENET_MRBR
]) ? size
: s
->regs
[ENET_MRBR
];
1082 bd
.length
= buf_len
;
1085 FEC_PRINTF("rx_bd 0x%x length %d\n", addr
, bd
.length
);
1087 /* The last 4 bytes are the CRC. */
1089 buf_len
+= size
- 4;
1092 dma_memory_write(&address_space_memory
, buf_addr
, buf
, buf_len
);
1095 dma_memory_write(&address_space_memory
, buf_addr
+ buf_len
,
1097 crc_ptr
+= 4 - size
;
1099 bd
.flags
&= ~ENET_BD_E
;
1101 /* Last buffer in frame. */
1102 bd
.flags
|= flags
| ENET_BD_L
;
1103 FEC_PRINTF("rx frame flags %04x\n", bd
.flags
);
1104 s
->regs
[ENET_EIR
] |= ENET_INT_RXF
;
1106 s
->regs
[ENET_EIR
] |= ENET_INT_RXB
;
1108 imx_fec_write_bd(&bd
, addr
);
1109 /* Advance to the next descriptor. */
1110 if ((bd
.flags
& ENET_BD_W
) != 0) {
1111 addr
= s
->regs
[ENET_RDSR
];
1116 s
->rx_descriptor
= addr
;
1117 imx_eth_enable_rx(s
, false);
1122 static ssize_t
imx_enet_receive(NetClientState
*nc
, const uint8_t *buf
,
1125 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1132 unsigned int buf_len
;
1134 bool shift16
= s
->regs
[ENET_RACC
] & ENET_RACC_SHIFT16
;
1136 FEC_PRINTF("len %d\n", (int)size
);
1138 if (!s
->regs
[ENET_RDAR
]) {
1139 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Unexpected packet\n",
1140 TYPE_IMX_FEC
, __func__
);
1144 /* 4 bytes for the CRC. */
1146 crc
= cpu_to_be32(crc32(~0, buf
, size
));
1147 crc_ptr
= (uint8_t *) &crc
;
1153 /* Huge frames are truncated. */
1154 if (size
> s
->regs
[ENET_FTRL
]) {
1155 size
= s
->regs
[ENET_FTRL
];
1156 flags
|= ENET_BD_TR
| ENET_BD_LG
;
1159 /* Frames larger than the user limit just set error flags. */
1160 if (size
> (s
->regs
[ENET_RCR
] >> 16)) {
1161 flags
|= ENET_BD_LG
;
1164 addr
= s
->rx_descriptor
;
1166 imx_enet_read_bd(&bd
, addr
);
1167 if ((bd
.flags
& ENET_BD_E
) == 0) {
1168 /* No descriptors available. Bail out. */
1170 * FIXME: This is wrong. We should probably either
1171 * save the remainder for when more RX buffers are
1172 * available, or flag an error.
1174 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Lost end of frame\n",
1175 TYPE_IMX_FEC
, __func__
);
1178 buf_len
= MIN(size
, s
->regs
[ENET_MRBR
]);
1179 bd
.length
= buf_len
;
1182 FEC_PRINTF("rx_bd 0x%x length %d\n", addr
, bd
.length
);
1184 /* The last 4 bytes are the CRC. */
1186 buf_len
+= size
- 4;
1192 * If SHIFT16 bit of ENETx_RACC register is set we need to
1193 * align the payload to 4-byte boundary.
1195 const uint8_t zeros
[2] = { 0 };
1197 dma_memory_write(&address_space_memory
, buf_addr
,
1198 zeros
, sizeof(zeros
));
1200 buf_addr
+= sizeof(zeros
);
1201 buf_len
-= sizeof(zeros
);
1203 /* We only do this once per Ethernet frame */
1207 dma_memory_write(&address_space_memory
, buf_addr
, buf
, buf_len
);
1210 dma_memory_write(&address_space_memory
, buf_addr
+ buf_len
,
1212 crc_ptr
+= 4 - size
;
1214 bd
.flags
&= ~ENET_BD_E
;
1216 /* Last buffer in frame. */
1217 bd
.flags
|= flags
| ENET_BD_L
;
1218 FEC_PRINTF("rx frame flags %04x\n", bd
.flags
);
1219 if (bd
.option
& ENET_BD_RX_INT
) {
1220 s
->regs
[ENET_EIR
] |= ENET_INT_RXF
;
1223 if (bd
.option
& ENET_BD_RX_INT
) {
1224 s
->regs
[ENET_EIR
] |= ENET_INT_RXB
;
1227 imx_enet_write_bd(&bd
, addr
);
1228 /* Advance to the next descriptor. */
1229 if ((bd
.flags
& ENET_BD_W
) != 0) {
1230 addr
= s
->regs
[ENET_RDSR
];
1235 s
->rx_descriptor
= addr
;
1236 imx_eth_enable_rx(s
, false);
1241 static ssize_t
imx_eth_receive(NetClientState
*nc
, const uint8_t *buf
,
1244 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1246 if (!s
->is_fec
&& (s
->regs
[ENET_ECR
] & ENET_ECR_EN1588
)) {
1247 return imx_enet_receive(nc
, buf
, len
);
1249 return imx_fec_receive(nc
, buf
, len
);
1253 static const MemoryRegionOps imx_eth_ops
= {
1254 .read
= imx_eth_read
,
1255 .write
= imx_eth_write
,
1256 .valid
.min_access_size
= 4,
1257 .valid
.max_access_size
= 4,
1258 .endianness
= DEVICE_NATIVE_ENDIAN
,
1261 static void imx_eth_cleanup(NetClientState
*nc
)
1263 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1268 static NetClientInfo imx_eth_net_info
= {
1269 .type
= NET_CLIENT_DRIVER_NIC
,
1270 .size
= sizeof(NICState
),
1271 .can_receive
= imx_eth_can_receive
,
1272 .receive
= imx_eth_receive
,
1273 .cleanup
= imx_eth_cleanup
,
1274 .link_status_changed
= imx_eth_set_link
,
1278 static void imx_eth_realize(DeviceState
*dev
, Error
**errp
)
1280 IMXFECState
*s
= IMX_FEC(dev
);
1281 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1283 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx_eth_ops
, s
,
1284 TYPE_IMX_FEC
, FSL_IMX25_FEC_SIZE
);
1285 sysbus_init_mmio(sbd
, &s
->iomem
);
1286 sysbus_init_irq(sbd
, &s
->irq
[0]);
1287 sysbus_init_irq(sbd
, &s
->irq
[1]);
1289 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
1291 s
->nic
= qemu_new_nic(&imx_eth_net_info
, &s
->conf
,
1292 object_get_typename(OBJECT(dev
)),
1293 DEVICE(dev
)->id
, s
);
1295 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
1298 static Property imx_eth_properties
[] = {
1299 DEFINE_NIC_PROPERTIES(IMXFECState
, conf
),
1300 DEFINE_PROP_UINT32("tx-ring-num", IMXFECState
, tx_ring_num
, 1),
1301 DEFINE_PROP_END_OF_LIST(),
1304 static void imx_eth_class_init(ObjectClass
*klass
, void *data
)
1306 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1308 dc
->vmsd
= &vmstate_imx_eth
;
1309 dc
->reset
= imx_eth_reset
;
1310 dc
->props
= imx_eth_properties
;
1311 dc
->realize
= imx_eth_realize
;
1312 dc
->desc
= "i.MX FEC/ENET Ethernet Controller";
1315 static void imx_fec_init(Object
*obj
)
1317 IMXFECState
*s
= IMX_FEC(obj
);
1322 static void imx_enet_init(Object
*obj
)
1324 IMXFECState
*s
= IMX_FEC(obj
);
1329 static const TypeInfo imx_fec_info
= {
1330 .name
= TYPE_IMX_FEC
,
1331 .parent
= TYPE_SYS_BUS_DEVICE
,
1332 .instance_size
= sizeof(IMXFECState
),
1333 .instance_init
= imx_fec_init
,
1334 .class_init
= imx_eth_class_init
,
1337 static const TypeInfo imx_enet_info
= {
1338 .name
= TYPE_IMX_ENET
,
1339 .parent
= TYPE_IMX_FEC
,
1340 .instance_init
= imx_enet_init
,
1343 static void imx_eth_register_types(void)
1345 type_register_static(&imx_fec_info
);
1346 type_register_static(&imx_enet_info
);
1349 type_init(imx_eth_register_types
)