2 * i.MX Fast Ethernet Controller emulation.
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);
199 static const VMStateDescription vmstate_imx_eth = {
200 .name = TYPE_IMX_FEC,
202 .minimum_version_id = 2,
203 .fields = (VMStateField[]) {
204 VMSTATE_UINT32_ARRAY(regs, IMXFECState, ENET_MAX),
205 VMSTATE_UINT32(rx_descriptor, IMXFECState),
206 VMSTATE_UINT32(tx_descriptor, IMXFECState),
208 VMSTATE_UINT32(phy_status, IMXFECState),
209 VMSTATE_UINT32(phy_control, IMXFECState),
210 VMSTATE_UINT32(phy_advertise, IMXFECState),
211 VMSTATE_UINT32(phy_int, IMXFECState),
212 VMSTATE_UINT32(phy_int_mask, IMXFECState),
213 VMSTATE_END_OF_LIST()
217 #define PHY_INT_ENERGYON (1 << 7)
218 #define PHY_INT_AUTONEG_COMPLETE (1 << 6)
219 #define PHY_INT_FAULT (1 << 5)
220 #define PHY_INT_DOWN (1 << 4)
221 #define PHY_INT_AUTONEG_LP (1 << 3)
222 #define PHY_INT_PARFAULT (1 << 2)
223 #define PHY_INT_AUTONEG_PAGE (1 << 1)
225 static void imx_eth_update(IMXFECState *s);
228 * The MII phy could raise a GPIO to the processor which in turn
229 * could be handled as an interrpt by the OS.
230 * For now we don't handle any GPIO/interrupt line, so the OS will
231 * have to poll for the PHY status.
233 static void phy_update_irq(IMXFECState *s)
238 static void phy_update_link(IMXFECState *s)
240 /* Autonegotiation status mirrors link status. */
241 if (qemu_get_queue(s->nic)->link_down) {
242 PHY_PRINTF("link is down\n");
243 s->phy_status &= ~0x0024;
244 s->phy_int |= PHY_INT_DOWN;
246 PHY_PRINTF("link is up\n");
247 s->phy_status |= 0x0024;
248 s->phy_int |= PHY_INT_ENERGYON;
249 s->phy_int |= PHY_INT_AUTONEG_COMPLETE;
254 static void imx_eth_set_link(NetClientState *nc)
256 phy_update_link(IMX_FEC(qemu_get_nic_opaque(nc)));
259 static void phy_reset(IMXFECState *s)
261 s->phy_status = 0x7809;
262 s->phy_control = 0x3000;
263 s->phy_advertise = 0x01e1;
269 static uint32_t do_phy_read(IMXFECState *s, int reg)
274 /* we only advertise one phy */
279 case 0: /* Basic Control */
280 val = s->phy_control;
282 case 1: /* Basic Status */
291 case 4: /* Auto-neg advertisement */
292 val = s->phy_advertise;
294 case 5: /* Auto-neg Link Partner Ability */
297 case 6: /* Auto-neg Expansion */
300 case 29: /* Interrupt source. */
305 case 30: /* Interrupt mask */
306 val = s->phy_int_mask;
312 qemu_log_mask(LOG_UNIMP, "[%s.phy]%s: reg %d not implemented\n",
313 TYPE_IMX_FEC, __func__, reg);
317 qemu_log_mask(LOG_GUEST_ERROR, "[%s.phy]%s: Bad address at offset %d\n",
318 TYPE_IMX_FEC, __func__, reg);
323 PHY_PRINTF("read 0x%04x @ %d\n", val, reg);
328 static void do_phy_write(IMXFECState *s, int reg, uint32_t val)
330 PHY_PRINTF("write 0x%04x @ %d\n", val, reg);
333 /* we only advertise one phy */
338 case 0: /* Basic Control */
342 s->phy_control = val & 0x7980;
343 /* Complete autonegotiation immediately. */
345 s->phy_status |= 0x0020;
349 case 4: /* Auto-neg advertisement */
350 s->phy_advertise = (val & 0x2d7f) | 0x80;
352 case 30: /* Interrupt mask */
353 s->phy_int_mask = val & 0xff;
360 qemu_log_mask(LOG_UNIMP, "[%s.phy)%s: reg %d not implemented\n",
361 TYPE_IMX_FEC, __func__, reg);
364 qemu_log_mask(LOG_GUEST_ERROR, "[%s.phy]%s: Bad address at offset %d\n",
365 TYPE_IMX_FEC, __func__, reg);
370 static void imx_fec_read_bd(IMXFECBufDesc *bd, dma_addr_t addr)
372 dma_memory_read(&address_space_memory, addr, bd, sizeof(*bd));
375 static void imx_fec_write_bd(IMXFECBufDesc *bd, dma_addr_t addr)
377 dma_memory_write(&address_space_memory, addr, bd, sizeof(*bd));
380 static void imx_enet_read_bd(IMXENETBufDesc *bd, dma_addr_t addr)
382 dma_memory_read(&address_space_memory, addr, bd, sizeof(*bd));
385 static void imx_enet_write_bd(IMXENETBufDesc *bd, dma_addr_t addr)
387 dma_memory_write(&address_space_memory, addr, bd, sizeof(*bd));
390 static void imx_eth_update(IMXFECState *s)
392 if (s->regs[ENET_EIR] & s->regs[ENET_EIMR] & ENET_INT_TS_TIMER) {
393 qemu_set_irq(s->irq[1], 1);
395 qemu_set_irq(s->irq[1], 0);
398 if (s->regs[ENET_EIR] & s->regs[ENET_EIMR] & ENET_INT_MAC) {
399 qemu_set_irq(s->irq[0], 1);
401 qemu_set_irq(s->irq[0], 0);
405 static void imx_fec_do_tx(IMXFECState *s)
407 int frame_size = 0, descnt = 0;
408 uint8_t frame[ENET_MAX_FRAME_SIZE];
409 uint8_t *ptr = frame;
410 uint32_t addr = s->tx_descriptor;
412 while (descnt++ < IMX_MAX_DESC) {
416 imx_fec_read_bd(&bd, addr);
417 FEC_PRINTF("tx_bd %x flags %04x len %d data %08x\n",
418 addr, bd.flags, bd.length, bd.data);
419 if ((bd.flags & ENET_BD_R) == 0) {
420 /* Run out of descriptors to transmit. */
421 FEC_PRINTF("tx_bd ran out of descriptors to transmit\n");
425 if (frame_size + len > ENET_MAX_FRAME_SIZE) {
426 len = ENET_MAX_FRAME_SIZE - frame_size;
427 s->regs[ENET_EIR] |= ENET_INT_BABT;
429 dma_memory_read(&address_space_memory, bd.data, ptr, len);
432 if (bd.flags & ENET_BD_L) {
433 /* Last buffer in frame. */
434 qemu_send_packet(qemu_get_queue(s->nic), frame, frame_size);
437 s->regs[ENET_EIR] |= ENET_INT_TXF;
439 s->regs[ENET_EIR] |= ENET_INT_TXB;
440 bd.flags &= ~ENET_BD_R;
441 /* Write back the modified descriptor. */
442 imx_fec_write_bd(&bd, addr);
443 /* Advance to the next descriptor. */
444 if ((bd.flags & ENET_BD_W) != 0) {
445 addr = s->regs[ENET_TDSR];
451 s->tx_descriptor = addr;
456 static void imx_enet_do_tx(IMXFECState *s)
458 int frame_size = 0, descnt = 0;
459 uint8_t frame[ENET_MAX_FRAME_SIZE];
460 uint8_t *ptr = frame;
461 uint32_t addr = s->tx_descriptor;
463 while (descnt++ < IMX_MAX_DESC) {
467 imx_enet_read_bd(&bd, addr);
468 FEC_PRINTF("tx_bd %x flags %04x len %d data %08x option %04x "
469 "status %04x\n", addr, bd.flags, bd.length, bd.data,
470 bd.option, bd.status);
471 if ((bd.flags & ENET_BD_R) == 0) {
472 /* Run out of descriptors to transmit. */
476 if (frame_size + len > ENET_MAX_FRAME_SIZE) {
477 len = ENET_MAX_FRAME_SIZE - frame_size;
478 s->regs[ENET_EIR] |= ENET_INT_BABT;
480 dma_memory_read(&address_space_memory, bd.data, ptr, len);
483 if (bd.flags & ENET_BD_L) {
484 if (bd.option & ENET_BD_PINS) {
485 struct ip_header *ip_hd = PKT_GET_IP_HDR(frame);
486 if (IP_HEADER_VERSION(ip_hd) == 4) {
487 net_checksum_calculate(frame, frame_size);
490 if (bd.option & ENET_BD_IINS) {
491 struct ip_header *ip_hd = PKT_GET_IP_HDR(frame);
492 /* We compute checksum only for IPv4 frames */
493 if (IP_HEADER_VERSION(ip_hd) == 4) {
496 csum = net_raw_checksum((uint8_t *)ip_hd, sizeof(*ip_hd));
497 ip_hd->ip_sum = cpu_to_be16(csum);
500 /* Last buffer in frame. */
501 qemu_send_packet(qemu_get_queue(s->nic), frame, len);
504 if (bd.option & ENET_BD_TX_INT) {
505 s->regs[ENET_EIR] |= ENET_INT_TXF;
508 if (bd.option & ENET_BD_TX_INT) {
509 s->regs[ENET_EIR] |= ENET_INT_TXB;
511 bd.flags &= ~ENET_BD_R;
512 /* Write back the modified descriptor. */
513 imx_enet_write_bd(&bd, addr);
514 /* Advance to the next descriptor. */
515 if ((bd.flags & ENET_BD_W) != 0) {
516 addr = s->regs[ENET_TDSR];
522 s->tx_descriptor = addr;
527 static void imx_eth_do_tx(IMXFECState *s)
529 if (!s->is_fec && (s->regs[ENET_ECR] & ENET_ECR_EN1588)) {
536 static void imx_eth_enable_rx(IMXFECState *s)
541 imx_fec_read_bd(&bd, s->rx_descriptor);
543 tmp = ((bd.flags & ENET_BD_E) != 0);
546 FEC_PRINTF("RX buffer full\n");
547 } else if (!s->regs[ENET_RDAR]) {
548 qemu_flush_queued_packets(qemu_get_queue(s->nic));
551 s->regs[ENET_RDAR] = tmp ? ENET_RDAR_RDAR : 0;
554 static void imx_eth_reset(DeviceState *d)
556 IMXFECState *s = IMX_FEC(d);
558 /* Reset the Device */
559 memset(s->regs, 0, sizeof(s->regs));
560 s->regs[ENET_ECR] = 0xf0000000;
561 s->regs[ENET_MIBC] = 0xc0000000;
562 s->regs[ENET_RCR] = 0x05ee0001;
563 s->regs[ENET_OPD] = 0x00010000;
565 s->regs[ENET_PALR] = (s->conf.macaddr.a[0] << 24)
566 | (s->conf.macaddr.a[1] << 16)
567 | (s->conf.macaddr.a[2] << 8)
568 | s->conf.macaddr.a[3];
569 s->regs[ENET_PAUR] = (s->conf.macaddr.a[4] << 24)
570 | (s->conf.macaddr.a[5] << 16)
574 s->regs[ENET_FRBR] = 0x00000600;
575 s->regs[ENET_FRSR] = 0x00000500;
576 s->regs[ENET_MIIGSK_ENR] = 0x00000006;
578 s->regs[ENET_RAEM] = 0x00000004;
579 s->regs[ENET_RAFL] = 0x00000004;
580 s->regs[ENET_TAEM] = 0x00000004;
581 s->regs[ENET_TAFL] = 0x00000008;
582 s->regs[ENET_TIPG] = 0x0000000c;
583 s->regs[ENET_FTRL] = 0x000007ff;
584 s->regs[ENET_ATPER] = 0x3b9aca00;
587 s->rx_descriptor = 0;
588 s->tx_descriptor = 0;
590 /* We also reset the PHY */
594 static uint32_t imx_default_read(IMXFECState *s, uint32_t index)
596 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
597 PRIx32 "\n", TYPE_IMX_FEC, __func__, index * 4);
601 static uint32_t imx_fec_read(IMXFECState *s, uint32_t index)
606 case ENET_MIIGSK_CFGR:
607 case ENET_MIIGSK_ENR:
608 return s->regs[index];
610 return imx_default_read(s, index);
614 static uint32_t imx_enet_read(IMXFECState *s, uint32_t index)
644 return s->regs[index];
646 return imx_default_read(s, index);
650 static uint64_t imx_eth_read(void *opaque, hwaddr offset, unsigned size)
653 IMXFECState *s = IMX_FEC(opaque);
654 uint32_t index = offset >> 2;
678 value = s->regs[index];
682 value = imx_fec_read(s, index);
684 value = imx_enet_read(s, index);
689 FEC_PRINTF("reg[%s] => 0x%" PRIx32 "\n", imx_eth_reg_name(s, index),
695 static void imx_default_write(IMXFECState *s, uint32_t index, uint32_t value)
697 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad address at offset 0x%"
698 PRIx32 "\n", TYPE_IMX_FEC, __func__, index * 4);
702 static void imx_fec_write(IMXFECState *s, uint32_t index, uint32_t value)
706 /* FRBR is read only */
707 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Register FRBR is read only\n",
708 TYPE_IMX_FEC, __func__);
711 s->regs[index] = (value & 0x000003fc) | 0x00000400;
713 case ENET_MIIGSK_CFGR:
714 s->regs[index] = value & 0x00000053;
716 case ENET_MIIGSK_ENR:
717 s->regs[index] = (value & 0x00000002) ? 0x00000006 : 0;
720 imx_default_write(s, index, value);
725 static void imx_enet_write(IMXFECState *s, uint32_t index, uint32_t value)
735 s->regs[index] = value & 0x000001ff;
738 s->regs[index] = value & 0x0000001f;
741 s->regs[index] = value & 0x00003fff;
744 s->regs[index] = value & 0x00000019;
747 s->regs[index] = value & 0x000000C7;
750 s->regs[index] = value & 0x00002a9d;
755 s->regs[index] = value;
758 /* ATSTMP is read only */
759 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Register ATSTMP is read only\n",
760 TYPE_IMX_FEC, __func__);
763 s->regs[index] = value & 0x7fffffff;
766 s->regs[index] = value & 0x00007f7f;
769 /* implement clear timer flag */
770 value = value & 0x0000000f;
776 value = value & 0x000000fd;
782 s->regs[index] = value;
785 imx_default_write(s, index, value);
790 static void imx_eth_write(void *opaque, hwaddr offset, uint64_t value,
793 IMXFECState *s = IMX_FEC(opaque);
794 uint32_t index = offset >> 2;
796 FEC_PRINTF("reg[%s] <= 0x%" PRIx32 "\n", imx_eth_reg_name(s, index),
801 s->regs[index] &= ~value;
804 s->regs[index] = value;
807 if (s->regs[ENET_ECR] & ENET_ECR_ETHEREN) {
808 if (!s->regs[index]) {
809 s->regs[index] = ENET_RDAR_RDAR;
810 imx_eth_enable_rx(s);
817 if (s->regs[ENET_ECR] & ENET_ECR_ETHEREN) {
818 s->regs[index] = ENET_TDAR_TDAR;
824 if (value & ENET_ECR_RESET) {
825 return imx_eth_reset(DEVICE(s));
827 s->regs[index] = value;
828 if ((s->regs[index] & ENET_ECR_ETHEREN) == 0) {
829 s->regs[ENET_RDAR] = 0;
830 s->rx_descriptor = s->regs[ENET_RDSR];
831 s->regs[ENET_TDAR] = 0;
832 s->tx_descriptor = s->regs[ENET_TDSR];
836 s->regs[index] = value;
837 if (extract32(value, 29, 1)) {
838 /* This is a read operation */
839 s->regs[ENET_MMFR] = deposit32(s->regs[ENET_MMFR], 0, 16,
844 /* This a write operation */
845 do_phy_write(s, extract32(value, 18, 10), extract32(value, 0, 16));
847 /* raise the interrupt as the PHY operation is done */
848 s->regs[ENET_EIR] |= ENET_INT_MII;
851 s->regs[index] = value & 0xfe;
854 /* TODO: Implement MIB. */
855 s->regs[index] = (value & 0x80000000) ? 0xc0000000 : 0;
858 s->regs[index] = value & 0x07ff003f;
859 /* TODO: Implement LOOP mode. */
862 /* We transmit immediately, so raise GRA immediately. */
863 s->regs[index] = value;
865 s->regs[ENET_EIR] |= ENET_INT_GRA;
869 s->regs[index] = value;
870 s->conf.macaddr.a[0] = value >> 24;
871 s->conf.macaddr.a[1] = value >> 16;
872 s->conf.macaddr.a[2] = value >> 8;
873 s->conf.macaddr.a[3] = value;
876 s->regs[index] = (value | 0x0000ffff) & 0xffff8808;
877 s->conf.macaddr.a[4] = value >> 24;
878 s->conf.macaddr.a[5] = value >> 16;
881 s->regs[index] = (value & 0x0000ffff) | 0x00010000;
887 /* TODO: implement MAC hash filtering. */
891 s->regs[index] = value & 0x3;
893 s->regs[index] = value & 0x13f;
898 s->regs[index] = value & ~3;
900 s->regs[index] = value & ~7;
902 s->rx_descriptor = s->regs[index];
906 s->regs[index] = value & ~3;
908 s->regs[index] = value & ~7;
910 s->tx_descriptor = s->regs[index];
913 s->regs[index] = value & 0x00003ff0;
917 imx_fec_write(s, index, value);
919 imx_enet_write(s, index, value);
927 static int imx_eth_can_receive(NetClientState *nc)
929 IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
933 return s->regs[ENET_RDAR] ? 1 : 0;
936 static ssize_t imx_fec_receive(NetClientState *nc, const uint8_t *buf,
939 IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
946 unsigned int buf_len;
949 FEC_PRINTF("len %d\n", (int)size);
951 if (!s->regs[ENET_RDAR]) {
952 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Unexpected packet\n",
953 TYPE_IMX_FEC, __func__);
957 /* 4 bytes for the CRC. */
959 crc = cpu_to_be32(crc32(~0, buf, size));
960 crc_ptr = (uint8_t *) &crc;
962 /* Huge frames are truncated. */
963 if (size > ENET_MAX_FRAME_SIZE) {
964 size = ENET_MAX_FRAME_SIZE;
965 flags |= ENET_BD_TR | ENET_BD_LG;
968 /* Frames larger than the user limit just set error flags. */
969 if (size > (s->regs[ENET_RCR] >> 16)) {
973 addr = s->rx_descriptor;
975 imx_fec_read_bd(&bd, addr);
976 if ((bd.flags & ENET_BD_E) == 0) {
977 /* No descriptors available. Bail out. */
979 * FIXME: This is wrong. We should probably either
980 * save the remainder for when more RX buffers are
981 * available, or flag an error.
983 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Lost end of frame\n",
984 TYPE_IMX_FEC, __func__);
987 buf_len = (size <= s->regs[ENET_MRBR]) ? size : s->regs[ENET_MRBR];
991 FEC_PRINTF("rx_bd 0x%x length %d\n", addr, bd.length);
993 /* The last 4 bytes are the CRC. */
998 dma_memory_write(&address_space_memory, buf_addr, buf, buf_len);
1001 dma_memory_write(&address_space_memory, buf_addr + buf_len,
1003 crc_ptr += 4 - size;
1005 bd.flags &= ~ENET_BD_E;
1007 /* Last buffer in frame. */
1008 bd.flags |= flags | ENET_BD_L;
1009 FEC_PRINTF("rx frame flags %04x\n", bd.flags);
1010 s->regs[ENET_EIR] |= ENET_INT_RXF;
1012 s->regs[ENET_EIR] |= ENET_INT_RXB;
1014 imx_fec_write_bd(&bd, addr);
1015 /* Advance to the next descriptor. */
1016 if ((bd.flags & ENET_BD_W) != 0) {
1017 addr = s->regs[ENET_RDSR];
1022 s->rx_descriptor = addr;
1023 imx_eth_enable_rx(s);
1028 static ssize_t imx_enet_receive(NetClientState *nc, const uint8_t *buf,
1031 IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
1038 unsigned int buf_len;
1041 FEC_PRINTF("len %d\n", (int)size);
1043 if (!s->regs[ENET_RDAR]) {
1044 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Unexpected packet\n",
1045 TYPE_IMX_FEC, __func__);
1049 /* 4 bytes for the CRC. */
1051 crc = cpu_to_be32(crc32(~0, buf, size));
1052 crc_ptr = (uint8_t *) &crc;
1054 /* Huge frames are truncted. */
1055 if (size > ENET_MAX_FRAME_SIZE) {
1056 size = ENET_MAX_FRAME_SIZE;
1057 flags |= ENET_BD_TR | ENET_BD_LG;
1060 /* Frames larger than the user limit just set error flags. */
1061 if (size > (s->regs[ENET_RCR] >> 16)) {
1062 flags |= ENET_BD_LG;
1065 addr = s->rx_descriptor;
1067 imx_enet_read_bd(&bd, addr);
1068 if ((bd.flags & ENET_BD_E) == 0) {
1069 /* No descriptors available. Bail out. */
1071 * FIXME: This is wrong. We should probably either
1072 * save the remainder for when more RX buffers are
1073 * available, or flag an error.
1075 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Lost end of frame\n",
1076 TYPE_IMX_FEC, __func__);
1079 buf_len = (size <= s->regs[ENET_MRBR]) ? size : s->regs[ENET_MRBR];
1080 bd.length = buf_len;
1083 FEC_PRINTF("rx_bd 0x%x length %d\n", addr, bd.length);
1085 /* The last 4 bytes are the CRC. */
1087 buf_len += size - 4;
1090 dma_memory_write(&address_space_memory, buf_addr, buf, buf_len);
1093 dma_memory_write(&address_space_memory, buf_addr + buf_len,
1095 crc_ptr += 4 - size;
1097 bd.flags &= ~ENET_BD_E;
1099 /* Last buffer in frame. */
1100 bd.flags |= flags | ENET_BD_L;
1101 FEC_PRINTF("rx frame flags %04x\n", bd.flags);
1102 if (bd.option & ENET_BD_RX_INT) {
1103 s->regs[ENET_EIR] |= ENET_INT_RXF;
1106 if (bd.option & ENET_BD_RX_INT) {
1107 s->regs[ENET_EIR] |= ENET_INT_RXB;
1110 imx_enet_write_bd(&bd, addr);
1111 /* Advance to the next descriptor. */
1112 if ((bd.flags & ENET_BD_W) != 0) {
1113 addr = s->regs[ENET_RDSR];
1118 s->rx_descriptor = addr;
1119 imx_eth_enable_rx(s);
1124 static ssize_t imx_eth_receive(NetClientState *nc, const uint8_t *buf,
1127 IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
1129 if (!s->is_fec && (s->regs[ENET_ECR] & ENET_ECR_EN1588)) {
1130 return imx_enet_receive(nc, buf, len);
1132 return imx_fec_receive(nc, buf, len);
1136 static const MemoryRegionOps imx_eth_ops = {
1137 .read = imx_eth_read,
1138 .write = imx_eth_write,
1139 .valid.min_access_size = 4,
1140 .valid.max_access_size = 4,
1141 .endianness = DEVICE_NATIVE_ENDIAN,
1144 static void imx_eth_cleanup(NetClientState *nc)
1146 IMXFECState *s = IMX_FEC(qemu_get_nic_opaque(nc));
1151 static NetClientInfo imx_eth_net_info = {
1152 .type = NET_CLIENT_DRIVER_NIC,
1153 .size = sizeof(NICState),
1154 .can_receive = imx_eth_can_receive,
1155 .receive = imx_eth_receive,
1156 .cleanup = imx_eth_cleanup,
1157 .link_status_changed = imx_eth_set_link,
1161 static void imx_eth_realize(DeviceState *dev, Error **errp)
1163 IMXFECState *s = IMX_FEC(dev);
1164 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1166 memory_region_init_io(&s->iomem, OBJECT(dev), &imx_eth_ops, s,
1167 TYPE_IMX_FEC, 0x400);
1168 sysbus_init_mmio(sbd, &s->iomem);
1169 sysbus_init_irq(sbd, &s->irq[0]);
1170 sysbus_init_irq(sbd, &s->irq[1]);
1172 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1174 s->conf.peers.ncs[0] = nd_table[0].netdev;
1176 s->nic = qemu_new_nic(&imx_eth_net_info, &s->conf,
1177 object_get_typename(OBJECT(dev)),
1178 DEVICE(dev)->id, s);
1180 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
1183 static Property imx_eth_properties[] = {
1184 DEFINE_NIC_PROPERTIES(IMXFECState, conf),
1185 DEFINE_PROP_END_OF_LIST(),
1188 static void imx_eth_class_init(ObjectClass *klass, void *data)
1190 DeviceClass *dc = DEVICE_CLASS(klass);
1192 dc->vmsd = &vmstate_imx_eth;
1193 dc->reset = imx_eth_reset;
1194 dc->props = imx_eth_properties;
1195 dc->realize = imx_eth_realize;
1196 dc->desc = "i.MX FEC/ENET Ethernet Controller";
1199 static void imx_fec_init(Object *obj)
1201 IMXFECState *s = IMX_FEC(obj);
1206 static void imx_enet_init(Object *obj)
1208 IMXFECState *s = IMX_FEC(obj);
1213 static const TypeInfo imx_fec_info = {
1214 .name = TYPE_IMX_FEC,
1215 .parent = TYPE_SYS_BUS_DEVICE,
1216 .instance_size = sizeof(IMXFECState),
1217 .instance_init = imx_fec_init,
1218 .class_init = imx_eth_class_init,
1221 static const TypeInfo imx_enet_info = {
1222 .name = TYPE_IMX_ENET,
1223 .parent = TYPE_IMX_FEC,
1224 .instance_init = imx_enet_init,
1227 static void imx_eth_register_types(void)
1229 type_register_static(&imx_fec_info);
1230 type_register_static(&imx_enet_info);
1233 type_init(imx_eth_register_types)