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 "hw/qdev-properties.h"
28 #include "migration/vmstate.h"
29 #include "sysemu/dma.h"
31 #include "qemu/module.h"
32 #include "net/checksum.h"
36 #include <zlib.h> /* for crc32 */
38 #define IMX_MAX_DESC 1024
40 static const char *imx_default_reg_name(IMXFECState
*s
, uint32_t index
)
43 snprintf(tmp
, sizeof(tmp
), "index %d", index
);
47 static const char *imx_fec_reg_name(IMXFECState
*s
, uint32_t index
)
54 case ENET_MIIGSK_CFGR
:
59 return imx_default_reg_name(s
, index
);
63 static const char *imx_enet_reg_name(IMXFECState
*s
, uint32_t index
)
121 return imx_default_reg_name(s
, index
);
125 static const char *imx_eth_reg_name(IMXFECState
*s
, uint32_t index
)
172 return imx_fec_reg_name(s
, index
);
174 return imx_enet_reg_name(s
, index
);
180 * Versions of this device with more than one TX descriptor save the
181 * 2nd and 3rd descriptors in a subsection, to maintain migration
182 * compatibility with previous versions of the device that only
183 * supported a single descriptor.
185 static bool imx_eth_is_multi_tx_ring(void *opaque
)
187 IMXFECState
*s
= IMX_FEC(opaque
);
189 return s
->tx_ring_num
> 1;
192 static const VMStateDescription vmstate_imx_eth_txdescs
= {
193 .name
= "imx.fec/txdescs",
195 .minimum_version_id
= 1,
196 .needed
= imx_eth_is_multi_tx_ring
,
197 .fields
= (const VMStateField
[]) {
198 VMSTATE_UINT32(tx_descriptor
[1], IMXFECState
),
199 VMSTATE_UINT32(tx_descriptor
[2], IMXFECState
),
200 VMSTATE_END_OF_LIST()
204 static const VMStateDescription vmstate_imx_eth
= {
205 .name
= TYPE_IMX_FEC
,
207 .minimum_version_id
= 2,
208 .fields
= (const VMStateField
[]) {
209 VMSTATE_UINT32_ARRAY(regs
, IMXFECState
, ENET_MAX
),
210 VMSTATE_UINT32(rx_descriptor
, IMXFECState
),
211 VMSTATE_UINT32(tx_descriptor
[0], IMXFECState
),
212 VMSTATE_UINT32(phy_status
, IMXFECState
),
213 VMSTATE_UINT32(phy_control
, IMXFECState
),
214 VMSTATE_UINT32(phy_advertise
, IMXFECState
),
215 VMSTATE_UINT32(phy_int
, IMXFECState
),
216 VMSTATE_UINT32(phy_int_mask
, IMXFECState
),
217 VMSTATE_END_OF_LIST()
219 .subsections
= (const VMStateDescription
* const []) {
220 &vmstate_imx_eth_txdescs
,
225 #define PHY_INT_ENERGYON (1 << 7)
226 #define PHY_INT_AUTONEG_COMPLETE (1 << 6)
227 #define PHY_INT_FAULT (1 << 5)
228 #define PHY_INT_DOWN (1 << 4)
229 #define PHY_INT_AUTONEG_LP (1 << 3)
230 #define PHY_INT_PARFAULT (1 << 2)
231 #define PHY_INT_AUTONEG_PAGE (1 << 1)
233 static void imx_eth_update(IMXFECState
*s
);
236 * The MII phy could raise a GPIO to the processor which in turn
237 * could be handled as an interrpt by the OS.
238 * For now we don't handle any GPIO/interrupt line, so the OS will
239 * have to poll for the PHY status.
241 static void imx_phy_update_irq(IMXFECState
*s
)
246 static void imx_phy_update_link(IMXFECState
*s
)
248 /* Autonegotiation status mirrors link status. */
249 if (qemu_get_queue(s
->nic
)->link_down
) {
250 trace_imx_phy_update_link("down");
251 s
->phy_status
&= ~0x0024;
252 s
->phy_int
|= PHY_INT_DOWN
;
254 trace_imx_phy_update_link("up");
255 s
->phy_status
|= 0x0024;
256 s
->phy_int
|= PHY_INT_ENERGYON
;
257 s
->phy_int
|= PHY_INT_AUTONEG_COMPLETE
;
259 imx_phy_update_irq(s
);
262 static void imx_eth_set_link(NetClientState
*nc
)
264 imx_phy_update_link(IMX_FEC(qemu_get_nic_opaque(nc
)));
267 static void imx_phy_reset(IMXFECState
*s
)
269 trace_imx_phy_reset();
271 s
->phy_status
= 0x7809;
272 s
->phy_control
= 0x3000;
273 s
->phy_advertise
= 0x01e1;
276 imx_phy_update_link(s
);
279 static uint32_t imx_phy_read(IMXFECState
*s
, int reg
)
282 uint32_t phy
= reg
/ 32;
284 if (!s
->phy_connected
) {
288 if (phy
!= s
->phy_num
) {
289 if (s
->phy_consumer
&& phy
== s
->phy_consumer
->phy_num
) {
292 trace_imx_phy_read_num(phy
, s
->phy_num
);
300 case 0: /* Basic Control */
301 val
= s
->phy_control
;
303 case 1: /* Basic Status */
312 case 4: /* Auto-neg advertisement */
313 val
= s
->phy_advertise
;
315 case 5: /* Auto-neg Link Partner Ability */
318 case 6: /* Auto-neg Expansion */
321 case 29: /* Interrupt source. */
324 imx_phy_update_irq(s
);
326 case 30: /* Interrupt mask */
327 val
= s
->phy_int_mask
;
333 qemu_log_mask(LOG_UNIMP
, "[%s.phy]%s: reg %d not implemented\n",
334 TYPE_IMX_FEC
, __func__
, reg
);
338 qemu_log_mask(LOG_GUEST_ERROR
, "[%s.phy]%s: Bad address at offset %d\n",
339 TYPE_IMX_FEC
, __func__
, reg
);
344 trace_imx_phy_read(val
, phy
, reg
);
349 static void imx_phy_write(IMXFECState
*s
, int reg
, uint32_t val
)
351 uint32_t phy
= reg
/ 32;
353 if (!s
->phy_connected
) {
357 if (phy
!= s
->phy_num
) {
358 if (s
->phy_consumer
&& phy
== s
->phy_consumer
->phy_num
) {
361 trace_imx_phy_write_num(phy
, s
->phy_num
);
368 trace_imx_phy_write(val
, phy
, reg
);
371 case 0: /* Basic Control */
375 s
->phy_control
= val
& 0x7980;
376 /* Complete autonegotiation immediately. */
378 s
->phy_status
|= 0x0020;
382 case 4: /* Auto-neg advertisement */
383 s
->phy_advertise
= (val
& 0x2d7f) | 0x80;
385 case 30: /* Interrupt mask */
386 s
->phy_int_mask
= val
& 0xff;
387 imx_phy_update_irq(s
);
393 qemu_log_mask(LOG_UNIMP
, "[%s.phy)%s: reg %d not implemented\n",
394 TYPE_IMX_FEC
, __func__
, reg
);
397 qemu_log_mask(LOG_GUEST_ERROR
, "[%s.phy]%s: Bad address at offset %d\n",
398 TYPE_IMX_FEC
, __func__
, reg
);
403 static void imx_fec_read_bd(IMXFECBufDesc
*bd
, dma_addr_t addr
)
405 dma_memory_read(&address_space_memory
, addr
, bd
, sizeof(*bd
),
406 MEMTXATTRS_UNSPECIFIED
);
408 trace_imx_fec_read_bd(addr
, bd
->flags
, bd
->length
, bd
->data
);
411 static void imx_fec_write_bd(IMXFECBufDesc
*bd
, dma_addr_t addr
)
413 dma_memory_write(&address_space_memory
, addr
, bd
, sizeof(*bd
),
414 MEMTXATTRS_UNSPECIFIED
);
417 static void imx_enet_read_bd(IMXENETBufDesc
*bd
, dma_addr_t addr
)
419 dma_memory_read(&address_space_memory
, addr
, bd
, sizeof(*bd
),
420 MEMTXATTRS_UNSPECIFIED
);
422 trace_imx_enet_read_bd(addr
, bd
->flags
, bd
->length
, bd
->data
,
423 bd
->option
, bd
->status
);
426 static void imx_enet_write_bd(IMXENETBufDesc
*bd
, dma_addr_t addr
)
428 dma_memory_write(&address_space_memory
, addr
, bd
, sizeof(*bd
),
429 MEMTXATTRS_UNSPECIFIED
);
432 static void imx_eth_update(IMXFECState
*s
)
435 * Previous versions of qemu had the ENET_INT_MAC and ENET_INT_TS_TIMER
436 * interrupts swapped. This worked with older versions of Linux (4.14
437 * and older) since Linux associated both interrupt lines with Ethernet
438 * MAC interrupts. Specifically,
439 * - Linux 4.15 and later have separate interrupt handlers for the MAC and
440 * timer interrupts. Those versions of Linux fail with versions of QEMU
441 * with swapped interrupt assignments.
442 * - In linux 4.14, both interrupt lines were registered with the Ethernet
443 * MAC interrupt handler. As a result, all versions of qemu happen to
444 * work, though that is accidental.
445 * - In Linux 4.9 and older, the timer interrupt was registered directly
446 * with the Ethernet MAC interrupt handler. The MAC interrupt was
447 * redirected to a GPIO interrupt to work around erratum ERR006687.
448 * This was implemented using the SOC's IOMUX block. In qemu, this GPIO
449 * interrupt never fired since IOMUX is currently not supported in qemu.
450 * Linux instead received MAC interrupts on the timer interrupt.
451 * As a result, qemu versions with the swapped interrupt assignment work,
452 * albeit accidentally, but qemu versions with the correct interrupt
455 * To ensure that all versions of Linux work, generate ENET_INT_MAC
456 * interrupts on both interrupt lines. This should be changed if and when
457 * qemu supports IOMUX.
459 if (s
->regs
[ENET_EIR
] & s
->regs
[ENET_EIMR
] &
460 (ENET_INT_MAC
| ENET_INT_TS_TIMER
)) {
461 qemu_set_irq(s
->irq
[1], 1);
463 qemu_set_irq(s
->irq
[1], 0);
466 if (s
->regs
[ENET_EIR
] & s
->regs
[ENET_EIMR
] & ENET_INT_MAC
) {
467 qemu_set_irq(s
->irq
[0], 1);
469 qemu_set_irq(s
->irq
[0], 0);
473 static void imx_fec_do_tx(IMXFECState
*s
)
475 int frame_size
= 0, descnt
= 0;
476 uint8_t *ptr
= s
->frame
;
477 uint32_t addr
= s
->tx_descriptor
[0];
479 while (descnt
++ < IMX_MAX_DESC
) {
483 imx_fec_read_bd(&bd
, addr
);
484 if ((bd
.flags
& ENET_BD_R
) == 0) {
486 /* Run out of descriptors to transmit. */
487 trace_imx_eth_tx_bd_busy();
492 if (frame_size
+ len
> ENET_MAX_FRAME_SIZE
) {
493 len
= ENET_MAX_FRAME_SIZE
- frame_size
;
494 s
->regs
[ENET_EIR
] |= ENET_INT_BABT
;
496 dma_memory_read(&address_space_memory
, bd
.data
, ptr
, len
,
497 MEMTXATTRS_UNSPECIFIED
);
500 if (bd
.flags
& ENET_BD_L
) {
501 /* Last buffer in frame. */
502 qemu_send_packet(qemu_get_queue(s
->nic
), s
->frame
, frame_size
);
505 s
->regs
[ENET_EIR
] |= ENET_INT_TXF
;
507 s
->regs
[ENET_EIR
] |= ENET_INT_TXB
;
508 bd
.flags
&= ~ENET_BD_R
;
509 /* Write back the modified descriptor. */
510 imx_fec_write_bd(&bd
, addr
);
511 /* Advance to the next descriptor. */
512 if ((bd
.flags
& ENET_BD_W
) != 0) {
513 addr
= s
->regs
[ENET_TDSR
];
519 s
->tx_descriptor
[0] = addr
;
524 static void imx_enet_do_tx(IMXFECState
*s
, uint32_t index
)
526 int frame_size
= 0, descnt
= 0;
528 uint8_t *ptr
= s
->frame
;
529 uint32_t addr
, int_txb
, int_txf
, tdsr
;
535 int_txb
= ENET_INT_TXB
;
536 int_txf
= ENET_INT_TXF
;
541 int_txb
= ENET_INT_TXB1
;
542 int_txf
= ENET_INT_TXF1
;
547 int_txb
= ENET_INT_TXB2
;
548 int_txf
= ENET_INT_TXF2
;
552 qemu_log_mask(LOG_GUEST_ERROR
,
553 "%s: bogus value for index %x\n",
559 addr
= s
->tx_descriptor
[ring
];
561 while (descnt
++ < IMX_MAX_DESC
) {
565 imx_enet_read_bd(&bd
, addr
);
566 if ((bd
.flags
& ENET_BD_R
) == 0) {
567 /* Run out of descriptors to transmit. */
569 trace_imx_eth_tx_bd_busy();
574 if (frame_size
+ len
> ENET_MAX_FRAME_SIZE
) {
575 len
= ENET_MAX_FRAME_SIZE
- frame_size
;
576 s
->regs
[ENET_EIR
] |= ENET_INT_BABT
;
578 dma_memory_read(&address_space_memory
, bd
.data
, ptr
, len
,
579 MEMTXATTRS_UNSPECIFIED
);
582 if (bd
.flags
& ENET_BD_L
) {
585 if (bd
.option
& ENET_BD_PINS
) {
586 csum
|= (CSUM_TCP
| CSUM_UDP
);
588 if (bd
.option
& ENET_BD_IINS
) {
592 net_checksum_calculate(s
->frame
, frame_size
, csum
);
595 /* Last buffer in frame. */
597 qemu_send_packet(qemu_get_queue(s
->nic
), s
->frame
, frame_size
);
601 if (bd
.option
& ENET_BD_TX_INT
) {
602 s
->regs
[ENET_EIR
] |= int_txf
;
604 /* Indicate that we've updated the last buffer descriptor. */
605 bd
.last_buffer
= ENET_BD_BDU
;
607 if (bd
.option
& ENET_BD_TX_INT
) {
608 s
->regs
[ENET_EIR
] |= int_txb
;
610 bd
.flags
&= ~ENET_BD_R
;
611 /* Write back the modified descriptor. */
612 imx_enet_write_bd(&bd
, addr
);
613 /* Advance to the next descriptor. */
614 if ((bd
.flags
& ENET_BD_W
) != 0) {
615 addr
= s
->regs
[tdsr
];
621 s
->tx_descriptor
[ring
] = addr
;
626 static void imx_eth_do_tx(IMXFECState
*s
, uint32_t index
)
628 if (!s
->is_fec
&& (s
->regs
[ENET_ECR
] & ENET_ECR_EN1588
)) {
629 imx_enet_do_tx(s
, index
);
635 static void imx_eth_enable_rx(IMXFECState
*s
, bool flush
)
639 imx_fec_read_bd(&bd
, s
->rx_descriptor
);
641 s
->regs
[ENET_RDAR
] = (bd
.flags
& ENET_BD_E
) ? ENET_RDAR_RDAR
: 0;
643 if (!s
->regs
[ENET_RDAR
]) {
644 trace_imx_eth_rx_bd_full();
646 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
650 static void imx_eth_reset(DeviceState
*d
)
652 IMXFECState
*s
= IMX_FEC(d
);
654 /* Reset the Device */
655 memset(s
->regs
, 0, sizeof(s
->regs
));
656 s
->regs
[ENET_ECR
] = 0xf0000000;
657 s
->regs
[ENET_MIBC
] = 0xc0000000;
658 s
->regs
[ENET_RCR
] = 0x05ee0001;
659 s
->regs
[ENET_OPD
] = 0x00010000;
661 s
->regs
[ENET_PALR
] = (s
->conf
.macaddr
.a
[0] << 24)
662 | (s
->conf
.macaddr
.a
[1] << 16)
663 | (s
->conf
.macaddr
.a
[2] << 8)
664 | s
->conf
.macaddr
.a
[3];
665 s
->regs
[ENET_PAUR
] = (s
->conf
.macaddr
.a
[4] << 24)
666 | (s
->conf
.macaddr
.a
[5] << 16)
670 s
->regs
[ENET_FRBR
] = 0x00000600;
671 s
->regs
[ENET_FRSR
] = 0x00000500;
672 s
->regs
[ENET_MIIGSK_ENR
] = 0x00000006;
674 s
->regs
[ENET_RAEM
] = 0x00000004;
675 s
->regs
[ENET_RAFL
] = 0x00000004;
676 s
->regs
[ENET_TAEM
] = 0x00000004;
677 s
->regs
[ENET_TAFL
] = 0x00000008;
678 s
->regs
[ENET_TIPG
] = 0x0000000c;
679 s
->regs
[ENET_FTRL
] = 0x000007ff;
680 s
->regs
[ENET_ATPER
] = 0x3b9aca00;
683 s
->rx_descriptor
= 0;
684 memset(s
->tx_descriptor
, 0, sizeof(s
->tx_descriptor
));
686 /* We also reset the PHY */
690 static uint32_t imx_default_read(IMXFECState
*s
, uint32_t index
)
692 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
693 PRIx32
"\n", TYPE_IMX_FEC
, __func__
, index
* 4);
697 static uint32_t imx_fec_read(IMXFECState
*s
, uint32_t index
)
702 case ENET_MIIGSK_CFGR
:
703 case ENET_MIIGSK_ENR
:
704 return s
->regs
[index
];
706 return imx_default_read(s
, index
);
710 static uint32_t imx_enet_read(IMXFECState
*s
, uint32_t index
)
740 return s
->regs
[index
];
742 return imx_default_read(s
, index
);
746 static uint64_t imx_eth_read(void *opaque
, hwaddr offset
, unsigned size
)
749 IMXFECState
*s
= IMX_FEC(opaque
);
750 uint32_t index
= offset
>> 2;
774 value
= s
->regs
[index
];
778 value
= imx_fec_read(s
, index
);
780 value
= imx_enet_read(s
, index
);
785 trace_imx_eth_read(index
, imx_eth_reg_name(s
, index
), value
);
790 static void imx_default_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
792 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad address at offset 0x%"
793 PRIx32
"\n", TYPE_IMX_FEC
, __func__
, index
* 4);
797 static void imx_fec_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
801 /* FRBR is read only */
802 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Register FRBR is read only\n",
803 TYPE_IMX_FEC
, __func__
);
806 s
->regs
[index
] = (value
& 0x000003fc) | 0x00000400;
808 case ENET_MIIGSK_CFGR
:
809 s
->regs
[index
] = value
& 0x00000053;
811 case ENET_MIIGSK_ENR
:
812 s
->regs
[index
] = (value
& 0x00000002) ? 0x00000006 : 0;
815 imx_default_write(s
, index
, value
);
820 static void imx_enet_write(IMXFECState
*s
, uint32_t index
, uint32_t value
)
830 s
->regs
[index
] = value
& 0x000001ff;
833 s
->regs
[index
] = value
& 0x0000001f;
836 s
->regs
[index
] = value
& 0x00003fff;
839 s
->regs
[index
] = value
& 0x00000019;
842 s
->regs
[index
] = value
& 0x000000C7;
845 s
->regs
[index
] = value
& 0x00002a9d;
850 s
->regs
[index
] = value
;
853 /* ATSTMP is read only */
854 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Register ATSTMP is read only\n",
855 TYPE_IMX_FEC
, __func__
);
858 s
->regs
[index
] = value
& 0x7fffffff;
861 s
->regs
[index
] = value
& 0x00007f7f;
864 /* implement clear timer flag */
865 s
->regs
[index
] &= ~(value
& 0x0000000f); /* all bits W1C */
871 s
->regs
[index
] &= ~(value
& 0x00000080); /* W1C bits */
872 s
->regs
[index
] &= ~0x0000007d; /* writable fields */
873 s
->regs
[index
] |= (value
& 0x0000007d);
879 s
->regs
[index
] = value
;
882 imx_default_write(s
, index
, value
);
887 static void imx_eth_write(void *opaque
, hwaddr offset
, uint64_t value
,
890 IMXFECState
*s
= IMX_FEC(opaque
);
891 const bool single_tx_ring
= !imx_eth_is_multi_tx_ring(s
);
892 uint32_t index
= offset
>> 2;
894 trace_imx_eth_write(index
, imx_eth_reg_name(s
, index
), value
);
898 s
->regs
[index
] &= ~value
;
901 s
->regs
[index
] = value
;
904 if (s
->regs
[ENET_ECR
] & ENET_ECR_ETHEREN
) {
905 if (!s
->regs
[index
]) {
906 imx_eth_enable_rx(s
, true);
914 if (unlikely(single_tx_ring
)) {
915 qemu_log_mask(LOG_GUEST_ERROR
,
916 "[%s]%s: trying to access TDAR2 or TDAR1\n",
917 TYPE_IMX_FEC
, __func__
);
922 if (s
->regs
[ENET_ECR
] & ENET_ECR_ETHEREN
) {
923 s
->regs
[index
] = ENET_TDAR_TDAR
;
924 imx_eth_do_tx(s
, index
);
929 if (value
& ENET_ECR_RESET
) {
930 return imx_eth_reset(DEVICE(s
));
932 s
->regs
[index
] = value
;
933 if ((s
->regs
[index
] & ENET_ECR_ETHEREN
) == 0) {
934 s
->regs
[ENET_RDAR
] = 0;
935 s
->rx_descriptor
= s
->regs
[ENET_RDSR
];
936 s
->regs
[ENET_TDAR
] = 0;
937 s
->regs
[ENET_TDAR1
] = 0;
938 s
->regs
[ENET_TDAR2
] = 0;
939 s
->tx_descriptor
[0] = s
->regs
[ENET_TDSR
];
940 s
->tx_descriptor
[1] = s
->regs
[ENET_TDSR1
];
941 s
->tx_descriptor
[2] = s
->regs
[ENET_TDSR2
];
945 s
->regs
[index
] = value
;
946 if (extract32(value
, 29, 1)) {
947 /* This is a read operation */
948 s
->regs
[ENET_MMFR
] = deposit32(s
->regs
[ENET_MMFR
], 0, 16,
953 /* This is a write operation */
954 imx_phy_write(s
, extract32(value
, 18, 10), extract32(value
, 0, 16));
956 /* raise the interrupt as the PHY operation is done */
957 s
->regs
[ENET_EIR
] |= ENET_INT_MII
;
960 s
->regs
[index
] = value
& 0xfe;
963 /* TODO: Implement MIB. */
964 s
->regs
[index
] = (value
& 0x80000000) ? 0xc0000000 : 0;
967 s
->regs
[index
] = value
& 0x07ff003f;
968 /* TODO: Implement LOOP mode. */
971 /* We transmit immediately, so raise GRA immediately. */
972 s
->regs
[index
] = value
;
974 s
->regs
[ENET_EIR
] |= ENET_INT_GRA
;
978 s
->regs
[index
] = value
;
979 s
->conf
.macaddr
.a
[0] = value
>> 24;
980 s
->conf
.macaddr
.a
[1] = value
>> 16;
981 s
->conf
.macaddr
.a
[2] = value
>> 8;
982 s
->conf
.macaddr
.a
[3] = value
;
985 s
->regs
[index
] = (value
| 0x0000ffff) & 0xffff8808;
986 s
->conf
.macaddr
.a
[4] = value
>> 24;
987 s
->conf
.macaddr
.a
[5] = value
>> 16;
990 s
->regs
[index
] = (value
& 0x0000ffff) | 0x00010000;
996 /* TODO: implement MAC hash filtering. */
1000 s
->regs
[index
] = value
& 0x3;
1002 s
->regs
[index
] = value
& 0x13f;
1007 s
->regs
[index
] = value
& ~3;
1009 s
->regs
[index
] = value
& ~7;
1011 s
->rx_descriptor
= s
->regs
[index
];
1015 s
->regs
[index
] = value
& ~3;
1017 s
->regs
[index
] = value
& ~7;
1019 s
->tx_descriptor
[0] = s
->regs
[index
];
1022 if (unlikely(single_tx_ring
)) {
1023 qemu_log_mask(LOG_GUEST_ERROR
,
1024 "[%s]%s: trying to access TDSR1\n",
1025 TYPE_IMX_FEC
, __func__
);
1029 s
->regs
[index
] = value
& ~7;
1030 s
->tx_descriptor
[1] = s
->regs
[index
];
1033 if (unlikely(single_tx_ring
)) {
1034 qemu_log_mask(LOG_GUEST_ERROR
,
1035 "[%s]%s: trying to access TDSR2\n",
1036 TYPE_IMX_FEC
, __func__
);
1040 s
->regs
[index
] = value
& ~7;
1041 s
->tx_descriptor
[2] = s
->regs
[index
];
1044 s
->regs
[index
] = value
& 0x00003ff0;
1048 imx_fec_write(s
, index
, value
);
1050 imx_enet_write(s
, index
, value
);
1058 static bool imx_eth_can_receive(NetClientState
*nc
)
1060 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1062 return !!s
->regs
[ENET_RDAR
];
1065 static ssize_t
imx_fec_receive(NetClientState
*nc
, const uint8_t *buf
,
1068 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1075 unsigned int buf_len
;
1078 trace_imx_fec_receive(size
);
1080 if (!s
->regs
[ENET_RDAR
]) {
1081 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Unexpected packet\n",
1082 TYPE_IMX_FEC
, __func__
);
1086 crc
= cpu_to_be32(crc32(~0, buf
, size
));
1087 /* Increase size by 4, loop below reads the last 4 bytes from crc_ptr. */
1089 crc_ptr
= (uint8_t *) &crc
;
1091 /* Huge frames are truncated. */
1092 if (size
> ENET_MAX_FRAME_SIZE
) {
1093 size
= ENET_MAX_FRAME_SIZE
;
1094 flags
|= ENET_BD_TR
| ENET_BD_LG
;
1097 /* Frames larger than the user limit just set error flags. */
1098 if (size
> (s
->regs
[ENET_RCR
] >> 16)) {
1099 flags
|= ENET_BD_LG
;
1102 addr
= s
->rx_descriptor
;
1104 imx_fec_read_bd(&bd
, addr
);
1105 if ((bd
.flags
& ENET_BD_E
) == 0) {
1106 /* No descriptors available. Bail out. */
1108 * FIXME: This is wrong. We should probably either
1109 * save the remainder for when more RX buffers are
1110 * available, or flag an error.
1112 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Lost end of frame\n",
1113 TYPE_IMX_FEC
, __func__
);
1116 buf_len
= (size
<= s
->regs
[ENET_MRBR
]) ? size
: s
->regs
[ENET_MRBR
];
1117 bd
.length
= buf_len
;
1120 trace_imx_fec_receive_len(addr
, bd
.length
);
1122 /* The last 4 bytes are the CRC. */
1124 buf_len
+= size
- 4;
1127 dma_memory_write(&address_space_memory
, buf_addr
, buf
, buf_len
,
1128 MEMTXATTRS_UNSPECIFIED
);
1131 dma_memory_write(&address_space_memory
, buf_addr
+ buf_len
,
1132 crc_ptr
, 4 - size
, MEMTXATTRS_UNSPECIFIED
);
1133 crc_ptr
+= 4 - size
;
1135 bd
.flags
&= ~ENET_BD_E
;
1137 /* Last buffer in frame. */
1138 bd
.flags
|= flags
| ENET_BD_L
;
1140 trace_imx_fec_receive_last(bd
.flags
);
1142 s
->regs
[ENET_EIR
] |= ENET_INT_RXF
;
1144 s
->regs
[ENET_EIR
] |= ENET_INT_RXB
;
1146 imx_fec_write_bd(&bd
, addr
);
1147 /* Advance to the next descriptor. */
1148 if ((bd
.flags
& ENET_BD_W
) != 0) {
1149 addr
= s
->regs
[ENET_RDSR
];
1154 s
->rx_descriptor
= addr
;
1155 imx_eth_enable_rx(s
, false);
1160 static ssize_t
imx_enet_receive(NetClientState
*nc
, const uint8_t *buf
,
1163 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1170 unsigned int buf_len
;
1172 bool shift16
= s
->regs
[ENET_RACC
] & ENET_RACC_SHIFT16
;
1174 trace_imx_enet_receive(size
);
1176 if (!s
->regs
[ENET_RDAR
]) {
1177 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Unexpected packet\n",
1178 TYPE_IMX_FEC
, __func__
);
1182 crc
= cpu_to_be32(crc32(~0, buf
, size
));
1183 /* Increase size by 4, loop below reads the last 4 bytes from crc_ptr. */
1185 crc_ptr
= (uint8_t *) &crc
;
1191 /* Huge frames are truncated. */
1192 if (size
> s
->regs
[ENET_FTRL
]) {
1193 size
= s
->regs
[ENET_FTRL
];
1194 flags
|= ENET_BD_TR
| ENET_BD_LG
;
1197 /* Frames larger than the user limit just set error flags. */
1198 if (size
> (s
->regs
[ENET_RCR
] >> 16)) {
1199 flags
|= ENET_BD_LG
;
1202 addr
= s
->rx_descriptor
;
1204 imx_enet_read_bd(&bd
, addr
);
1205 if ((bd
.flags
& ENET_BD_E
) == 0) {
1206 /* No descriptors available. Bail out. */
1208 * FIXME: This is wrong. We should probably either
1209 * save the remainder for when more RX buffers are
1210 * available, or flag an error.
1212 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Lost end of frame\n",
1213 TYPE_IMX_FEC
, __func__
);
1216 buf_len
= MIN(size
, s
->regs
[ENET_MRBR
]);
1217 bd
.length
= buf_len
;
1220 trace_imx_enet_receive_len(addr
, bd
.length
);
1222 /* The last 4 bytes are the CRC. */
1224 buf_len
+= size
- 4;
1230 * If SHIFT16 bit of ENETx_RACC register is set we need to
1231 * align the payload to 4-byte boundary.
1233 const uint8_t zeros
[2] = { 0 };
1235 dma_memory_write(&address_space_memory
, buf_addr
, zeros
,
1236 sizeof(zeros
), MEMTXATTRS_UNSPECIFIED
);
1238 buf_addr
+= sizeof(zeros
);
1239 buf_len
-= sizeof(zeros
);
1241 /* We only do this once per Ethernet frame */
1245 dma_memory_write(&address_space_memory
, buf_addr
, buf
, buf_len
,
1246 MEMTXATTRS_UNSPECIFIED
);
1249 dma_memory_write(&address_space_memory
, buf_addr
+ buf_len
,
1250 crc_ptr
, 4 - size
, MEMTXATTRS_UNSPECIFIED
);
1251 crc_ptr
+= 4 - size
;
1253 bd
.flags
&= ~ENET_BD_E
;
1255 /* Last buffer in frame. */
1256 bd
.flags
|= flags
| ENET_BD_L
;
1258 trace_imx_enet_receive_last(bd
.flags
);
1260 /* Indicate that we've updated the last buffer descriptor. */
1261 bd
.last_buffer
= ENET_BD_BDU
;
1262 if (bd
.option
& ENET_BD_RX_INT
) {
1263 s
->regs
[ENET_EIR
] |= ENET_INT_RXF
;
1266 if (bd
.option
& ENET_BD_RX_INT
) {
1267 s
->regs
[ENET_EIR
] |= ENET_INT_RXB
;
1270 imx_enet_write_bd(&bd
, addr
);
1271 /* Advance to the next descriptor. */
1272 if ((bd
.flags
& ENET_BD_W
) != 0) {
1273 addr
= s
->regs
[ENET_RDSR
];
1278 s
->rx_descriptor
= addr
;
1279 imx_eth_enable_rx(s
, false);
1284 static ssize_t
imx_eth_receive(NetClientState
*nc
, const uint8_t *buf
,
1287 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1289 if (!s
->is_fec
&& (s
->regs
[ENET_ECR
] & ENET_ECR_EN1588
)) {
1290 return imx_enet_receive(nc
, buf
, len
);
1292 return imx_fec_receive(nc
, buf
, len
);
1296 static const MemoryRegionOps imx_eth_ops
= {
1297 .read
= imx_eth_read
,
1298 .write
= imx_eth_write
,
1299 .valid
.min_access_size
= 4,
1300 .valid
.max_access_size
= 4,
1301 .endianness
= DEVICE_NATIVE_ENDIAN
,
1304 static void imx_eth_cleanup(NetClientState
*nc
)
1306 IMXFECState
*s
= IMX_FEC(qemu_get_nic_opaque(nc
));
1311 static NetClientInfo imx_eth_net_info
= {
1312 .type
= NET_CLIENT_DRIVER_NIC
,
1313 .size
= sizeof(NICState
),
1314 .can_receive
= imx_eth_can_receive
,
1315 .receive
= imx_eth_receive
,
1316 .cleanup
= imx_eth_cleanup
,
1317 .link_status_changed
= imx_eth_set_link
,
1321 static void imx_eth_realize(DeviceState
*dev
, Error
**errp
)
1323 IMXFECState
*s
= IMX_FEC(dev
);
1324 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1326 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &imx_eth_ops
, s
,
1327 TYPE_IMX_FEC
, FSL_IMX25_FEC_SIZE
);
1328 sysbus_init_mmio(sbd
, &s
->iomem
);
1329 sysbus_init_irq(sbd
, &s
->irq
[0]);
1330 sysbus_init_irq(sbd
, &s
->irq
[1]);
1332 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
1334 s
->nic
= qemu_new_nic(&imx_eth_net_info
, &s
->conf
,
1335 object_get_typename(OBJECT(dev
)),
1336 dev
->id
, &dev
->mem_reentrancy_guard
, s
);
1338 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
1341 static Property imx_eth_properties
[] = {
1342 DEFINE_NIC_PROPERTIES(IMXFECState
, conf
),
1343 DEFINE_PROP_UINT32("tx-ring-num", IMXFECState
, tx_ring_num
, 1),
1344 DEFINE_PROP_UINT32("phy-num", IMXFECState
, phy_num
, 0),
1345 DEFINE_PROP_BOOL("phy-connected", IMXFECState
, phy_connected
, true),
1346 DEFINE_PROP_LINK("phy-consumer", IMXFECState
, phy_consumer
, TYPE_IMX_FEC
,
1348 DEFINE_PROP_END_OF_LIST(),
1351 static void imx_eth_class_init(ObjectClass
*klass
, void *data
)
1353 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1355 dc
->vmsd
= &vmstate_imx_eth
;
1356 device_class_set_legacy_reset(dc
, imx_eth_reset
);
1357 device_class_set_props(dc
, imx_eth_properties
);
1358 dc
->realize
= imx_eth_realize
;
1359 dc
->desc
= "i.MX FEC/ENET Ethernet Controller";
1362 static void imx_fec_init(Object
*obj
)
1364 IMXFECState
*s
= IMX_FEC(obj
);
1369 static void imx_enet_init(Object
*obj
)
1371 IMXFECState
*s
= IMX_FEC(obj
);
1376 static const TypeInfo imx_fec_info
= {
1377 .name
= TYPE_IMX_FEC
,
1378 .parent
= TYPE_SYS_BUS_DEVICE
,
1379 .instance_size
= sizeof(IMXFECState
),
1380 .instance_init
= imx_fec_init
,
1381 .class_init
= imx_eth_class_init
,
1384 static const TypeInfo imx_enet_info
= {
1385 .name
= TYPE_IMX_ENET
,
1386 .parent
= TYPE_IMX_FEC
,
1387 .instance_init
= imx_enet_init
,
1390 static void imx_eth_register_types(void)
1392 type_register_static(&imx_fec_info
);
1393 type_register_static(&imx_enet_info
);
1396 type_init(imx_eth_register_types
)