2 * ColdFire Fast Ethernet Controller emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
9 #include "qemu/osdep.h"
13 #include "qemu/module.h"
14 #include "hw/m68k/mcf.h"
15 #include "hw/m68k/mcf_fec.h"
16 #include "hw/net/mii.h"
17 #include "hw/qdev-properties.h"
18 #include "hw/sysbus.h"
25 #define DPRINTF(fmt, ...) \
26 do { printf("mcf_fec: " fmt , ## __VA_ARGS__); } while (0)
28 #define DPRINTF(fmt, ...) do {} while(0)
31 #define FEC_MAX_DESC 1024
32 #define FEC_MAX_FRAME_SIZE 2032
33 #define FEC_MIB_SIZE 64
36 SysBusDevice parent_obj
;
39 qemu_irq irq
[FEC_NUM_IRQ
];
46 uint32_t rx_descriptor
;
47 uint32_t tx_descriptor
;
58 uint32_t mib
[FEC_MIB_SIZE
];
61 #define FEC_INT_HB 0x80000000
62 #define FEC_INT_BABR 0x40000000
63 #define FEC_INT_BABT 0x20000000
64 #define FEC_INT_GRA 0x10000000
65 #define FEC_INT_TXF 0x08000000
66 #define FEC_INT_TXB 0x04000000
67 #define FEC_INT_RXF 0x02000000
68 #define FEC_INT_RXB 0x01000000
69 #define FEC_INT_MII 0x00800000
70 #define FEC_INT_EB 0x00400000
71 #define FEC_INT_LC 0x00200000
72 #define FEC_INT_RL 0x00100000
73 #define FEC_INT_UN 0x00080000
78 /* Map interrupt flags onto IRQ lines. */
79 static const uint32_t mcf_fec_irq_map
[FEC_NUM_IRQ
] = {
95 /* Buffer Descriptor. */
102 #define FEC_BD_R 0x8000
103 #define FEC_BD_E 0x8000
104 #define FEC_BD_O1 0x4000
105 #define FEC_BD_W 0x2000
106 #define FEC_BD_O2 0x1000
107 #define FEC_BD_L 0x0800
108 #define FEC_BD_TC 0x0400
109 #define FEC_BD_ABC 0x0200
110 #define FEC_BD_M 0x0100
111 #define FEC_BD_BC 0x0080
112 #define FEC_BD_MC 0x0040
113 #define FEC_BD_LG 0x0020
114 #define FEC_BD_NO 0x0010
115 #define FEC_BD_CR 0x0004
116 #define FEC_BD_OV 0x0002
117 #define FEC_BD_TR 0x0001
119 #define MIB_RMON_T_DROP 0
120 #define MIB_RMON_T_PACKETS 1
121 #define MIB_RMON_T_BC_PKT 2
122 #define MIB_RMON_T_MC_PKT 3
123 #define MIB_RMON_T_CRC_ALIGN 4
124 #define MIB_RMON_T_UNDERSIZE 5
125 #define MIB_RMON_T_OVERSIZE 6
126 #define MIB_RMON_T_FRAG 7
127 #define MIB_RMON_T_JAB 8
128 #define MIB_RMON_T_COL 9
129 #define MIB_RMON_T_P64 10
130 #define MIB_RMON_T_P65TO127 11
131 #define MIB_RMON_T_P128TO255 12
132 #define MIB_RMON_T_P256TO511 13
133 #define MIB_RMON_T_P512TO1023 14
134 #define MIB_RMON_T_P1024TO2047 15
135 #define MIB_RMON_T_P_GTE2048 16
136 #define MIB_RMON_T_OCTETS 17
137 #define MIB_IEEE_T_DROP 18
138 #define MIB_IEEE_T_FRAME_OK 19
139 #define MIB_IEEE_T_1COL 20
140 #define MIB_IEEE_T_MCOL 21
141 #define MIB_IEEE_T_DEF 22
142 #define MIB_IEEE_T_LCOL 23
143 #define MIB_IEEE_T_EXCOL 24
144 #define MIB_IEEE_T_MACERR 25
145 #define MIB_IEEE_T_CSERR 26
146 #define MIB_IEEE_T_SQE 27
147 #define MIB_IEEE_T_FDXFC 28
148 #define MIB_IEEE_T_OCTETS_OK 29
150 #define MIB_RMON_R_DROP 32
151 #define MIB_RMON_R_PACKETS 33
152 #define MIB_RMON_R_BC_PKT 34
153 #define MIB_RMON_R_MC_PKT 35
154 #define MIB_RMON_R_CRC_ALIGN 36
155 #define MIB_RMON_R_UNDERSIZE 37
156 #define MIB_RMON_R_OVERSIZE 38
157 #define MIB_RMON_R_FRAG 39
158 #define MIB_RMON_R_JAB 40
159 #define MIB_RMON_R_RESVD_0 41
160 #define MIB_RMON_R_P64 42
161 #define MIB_RMON_R_P65TO127 43
162 #define MIB_RMON_R_P128TO255 44
163 #define MIB_RMON_R_P256TO511 45
164 #define MIB_RMON_R_P512TO1023 46
165 #define MIB_RMON_R_P1024TO2047 47
166 #define MIB_RMON_R_P_GTE2048 48
167 #define MIB_RMON_R_OCTETS 49
168 #define MIB_IEEE_R_DROP 50
169 #define MIB_IEEE_R_FRAME_OK 51
170 #define MIB_IEEE_R_CRC 52
171 #define MIB_IEEE_R_ALIGN 53
172 #define MIB_IEEE_R_MACERR 54
173 #define MIB_IEEE_R_FDXFC 55
174 #define MIB_IEEE_R_OCTETS_OK 56
176 static void mcf_fec_read_bd(mcf_fec_bd
*bd
, uint32_t addr
)
178 cpu_physical_memory_read(addr
, bd
, sizeof(*bd
));
179 be16_to_cpus(&bd
->flags
);
180 be16_to_cpus(&bd
->length
);
181 be32_to_cpus(&bd
->data
);
184 static void mcf_fec_write_bd(mcf_fec_bd
*bd
, uint32_t addr
)
187 tmp
.flags
= cpu_to_be16(bd
->flags
);
188 tmp
.length
= cpu_to_be16(bd
->length
);
189 tmp
.data
= cpu_to_be32(bd
->data
);
190 cpu_physical_memory_write(addr
, &tmp
, sizeof(tmp
));
193 static void mcf_fec_update(mcf_fec_state
*s
)
200 active
= s
->eir
& s
->eimr
;
201 changed
= active
^s
->irq_state
;
202 for (i
= 0; i
< FEC_NUM_IRQ
; i
++) {
203 mask
= mcf_fec_irq_map
[i
];
204 if (changed
& mask
) {
205 DPRINTF("IRQ %d = %d\n", i
, (active
& mask
) != 0);
206 qemu_set_irq(s
->irq
[i
], (active
& mask
) != 0);
209 s
->irq_state
= active
;
212 static void mcf_fec_tx_stats(mcf_fec_state
*s
, int size
)
214 s
->mib
[MIB_RMON_T_PACKETS
]++;
215 s
->mib
[MIB_RMON_T_OCTETS
] += size
;
217 s
->mib
[MIB_RMON_T_FRAG
]++;
218 } else if (size
== 64) {
219 s
->mib
[MIB_RMON_T_P64
]++;
220 } else if (size
< 128) {
221 s
->mib
[MIB_RMON_T_P65TO127
]++;
222 } else if (size
< 256) {
223 s
->mib
[MIB_RMON_T_P128TO255
]++;
224 } else if (size
< 512) {
225 s
->mib
[MIB_RMON_T_P256TO511
]++;
226 } else if (size
< 1024) {
227 s
->mib
[MIB_RMON_T_P512TO1023
]++;
228 } else if (size
< 2048) {
229 s
->mib
[MIB_RMON_T_P1024TO2047
]++;
231 s
->mib
[MIB_RMON_T_P_GTE2048
]++;
233 s
->mib
[MIB_IEEE_T_FRAME_OK
]++;
234 s
->mib
[MIB_IEEE_T_OCTETS_OK
] += size
;
237 static void mcf_fec_do_tx(mcf_fec_state
*s
)
243 uint8_t frame
[FEC_MAX_FRAME_SIZE
];
249 addr
= s
->tx_descriptor
;
250 while (descnt
++ < FEC_MAX_DESC
) {
251 mcf_fec_read_bd(&bd
, addr
);
252 DPRINTF("tx_bd %x flags %04x len %d data %08x\n",
253 addr
, bd
.flags
, bd
.length
, bd
.data
);
254 if ((bd
.flags
& FEC_BD_R
) == 0) {
255 /* Run out of descriptors to transmit. */
259 if (frame_size
+ len
> FEC_MAX_FRAME_SIZE
) {
260 len
= FEC_MAX_FRAME_SIZE
- frame_size
;
261 s
->eir
|= FEC_INT_BABT
;
263 cpu_physical_memory_read(bd
.data
, ptr
, len
);
266 if (bd
.flags
& FEC_BD_L
) {
267 /* Last buffer in frame. */
268 DPRINTF("Sending packet\n");
269 qemu_send_packet(qemu_get_queue(s
->nic
), frame
, frame_size
);
270 mcf_fec_tx_stats(s
, frame_size
);
273 s
->eir
|= FEC_INT_TXF
;
275 s
->eir
|= FEC_INT_TXB
;
276 bd
.flags
&= ~FEC_BD_R
;
277 /* Write back the modified descriptor. */
278 mcf_fec_write_bd(&bd
, addr
);
279 /* Advance to the next descriptor. */
280 if ((bd
.flags
& FEC_BD_W
) != 0) {
286 s
->tx_descriptor
= addr
;
289 static void mcf_fec_enable_rx(mcf_fec_state
*s
)
291 NetClientState
*nc
= qemu_get_queue(s
->nic
);
294 mcf_fec_read_bd(&bd
, s
->rx_descriptor
);
295 s
->rx_enabled
= ((bd
.flags
& FEC_BD_E
) != 0);
297 qemu_flush_queued_packets(nc
);
301 static void mcf_fec_reset(DeviceState
*dev
)
303 mcf_fec_state
*s
= MCF_FEC_NET(dev
);
316 #define MMFR_WRITE_OP (1 << 28)
317 #define MMFR_READ_OP (2 << 28)
318 #define MMFR_PHYADDR(v) (((v) >> 23) & 0x1f)
319 #define MMFR_REGNUM(v) (((v) >> 18) & 0x1f)
321 static uint64_t mcf_fec_read_mdio(mcf_fec_state
*s
)
325 if (s
->mmfr
& MMFR_WRITE_OP
)
327 if (MMFR_PHYADDR(s
->mmfr
) != 1)
328 return s
->mmfr
|= 0xffff;
330 switch (MMFR_REGNUM(s
->mmfr
)) {
332 v
= MII_BMCR_SPEED
| MII_BMCR_AUTOEN
| MII_BMCR_FD
;
335 v
= MII_BMSR_100TX_FD
| MII_BMSR_100TX_HD
| MII_BMSR_10T_FD
|
336 MII_BMSR_10T_HD
| MII_BMSR_MFPS
| MII_BMSR_AN_COMP
|
337 MII_BMSR_AUTONEG
| MII_BMSR_LINK_ST
;
346 v
= MII_ANAR_TXFD
| MII_ANAR_TX
| MII_ANAR_10FD
|
347 MII_ANAR_10
| MII_ANAR_CSMACD
;
350 v
= MII_ANLPAR_ACK
| MII_ANLPAR_TXFD
| MII_ANLPAR_TX
|
351 MII_ANLPAR_10FD
| MII_ANLPAR_10
| MII_ANLPAR_CSMACD
;
357 s
->mmfr
= (s
->mmfr
& ~0xffff) | v
;
361 static uint64_t mcf_fec_read(void *opaque
, hwaddr addr
,
364 mcf_fec_state
*s
= (mcf_fec_state
*)opaque
;
365 switch (addr
& 0x3ff) {
366 case 0x004: return s
->eir
;
367 case 0x008: return s
->eimr
;
368 case 0x010: return s
->rx_enabled
? (1 << 24) : 0; /* RDAR */
369 case 0x014: return 0; /* TDAR */
370 case 0x024: return s
->ecr
;
371 case 0x040: return mcf_fec_read_mdio(s
);
372 case 0x044: return s
->mscr
;
373 case 0x064: return 0; /* MIBC */
374 case 0x084: return s
->rcr
;
375 case 0x0c4: return s
->tcr
;
376 case 0x0e4: /* PALR */
377 return (s
->conf
.macaddr
.a
[0] << 24) | (s
->conf
.macaddr
.a
[1] << 16)
378 | (s
->conf
.macaddr
.a
[2] << 8) | s
->conf
.macaddr
.a
[3];
380 case 0x0e8: /* PAUR */
381 return (s
->conf
.macaddr
.a
[4] << 24) | (s
->conf
.macaddr
.a
[5] << 16) | 0x8808;
382 case 0x0ec: return 0x10000; /* OPD */
383 case 0x118: return 0;
384 case 0x11c: return 0;
385 case 0x120: return 0;
386 case 0x124: return 0;
387 case 0x144: return s
->tfwr
;
388 case 0x14c: return 0x600;
389 case 0x150: return s
->rfsr
;
390 case 0x180: return s
->erdsr
;
391 case 0x184: return s
->etdsr
;
392 case 0x188: return s
->emrbr
;
393 case 0x200 ... 0x2e0: return s
->mib
[(addr
& 0x1ff) / 4];
395 hw_error("mcf_fec_read: Bad address 0x%x\n", (int)addr
);
400 static void mcf_fec_write(void *opaque
, hwaddr addr
,
401 uint64_t value
, unsigned size
)
403 mcf_fec_state
*s
= (mcf_fec_state
*)opaque
;
404 switch (addr
& 0x3ff) {
411 case 0x010: /* RDAR */
412 if ((s
->ecr
& FEC_EN
) && !s
->rx_enabled
) {
413 DPRINTF("RX enable\n");
414 mcf_fec_enable_rx(s
);
417 case 0x014: /* TDAR */
418 if (s
->ecr
& FEC_EN
) {
424 if (value
& FEC_RESET
) {
426 mcf_fec_reset(opaque
);
428 if ((s
->ecr
& FEC_EN
) == 0) {
434 s
->eir
|= FEC_INT_MII
;
437 s
->mscr
= value
& 0xfe;
440 /* TODO: Implement MIB. */
443 s
->rcr
= value
& 0x07ff003f;
444 /* TODO: Implement LOOP mode. */
446 case 0x0c4: /* TCR */
447 /* We transmit immediately, so raise GRA immediately. */
450 s
->eir
|= FEC_INT_GRA
;
452 case 0x0e4: /* PALR */
453 s
->conf
.macaddr
.a
[0] = value
>> 24;
454 s
->conf
.macaddr
.a
[1] = value
>> 16;
455 s
->conf
.macaddr
.a
[2] = value
>> 8;
456 s
->conf
.macaddr
.a
[3] = value
;
458 case 0x0e8: /* PAUR */
459 s
->conf
.macaddr
.a
[4] = value
>> 24;
460 s
->conf
.macaddr
.a
[5] = value
>> 16;
469 /* TODO: implement MAC hash filtering. */
475 /* FRBR writes ignored. */
478 s
->rfsr
= (value
& 0x3fc) | 0x400;
481 s
->erdsr
= value
& ~3;
482 s
->rx_descriptor
= s
->erdsr
;
485 s
->etdsr
= value
& ~3;
486 s
->tx_descriptor
= s
->etdsr
;
489 s
->emrbr
= value
> 0 ? value
& 0x7F0 : 0x7F0;
491 case 0x200 ... 0x2e0:
492 s
->mib
[(addr
& 0x1ff) / 4] = value
;
495 hw_error("mcf_fec_write Bad address 0x%x\n", (int)addr
);
500 static void mcf_fec_rx_stats(mcf_fec_state
*s
, int size
)
502 s
->mib
[MIB_RMON_R_PACKETS
]++;
503 s
->mib
[MIB_RMON_R_OCTETS
] += size
;
505 s
->mib
[MIB_RMON_R_FRAG
]++;
506 } else if (size
== 64) {
507 s
->mib
[MIB_RMON_R_P64
]++;
508 } else if (size
< 128) {
509 s
->mib
[MIB_RMON_R_P65TO127
]++;
510 } else if (size
< 256) {
511 s
->mib
[MIB_RMON_R_P128TO255
]++;
512 } else if (size
< 512) {
513 s
->mib
[MIB_RMON_R_P256TO511
]++;
514 } else if (size
< 1024) {
515 s
->mib
[MIB_RMON_R_P512TO1023
]++;
516 } else if (size
< 2048) {
517 s
->mib
[MIB_RMON_R_P1024TO2047
]++;
519 s
->mib
[MIB_RMON_R_P_GTE2048
]++;
521 s
->mib
[MIB_IEEE_R_FRAME_OK
]++;
522 s
->mib
[MIB_IEEE_R_OCTETS_OK
] += size
;
525 static int mcf_fec_have_receive_space(mcf_fec_state
*s
, size_t want
)
530 /* Walk descriptor list to determine if we have enough buffer */
531 addr
= s
->rx_descriptor
;
533 mcf_fec_read_bd(&bd
, addr
);
534 if ((bd
.flags
& FEC_BD_E
) == 0) {
537 if (want
< s
->emrbr
) {
541 /* Advance to the next descriptor. */
542 if ((bd
.flags
& FEC_BD_W
) != 0) {
551 static ssize_t
mcf_fec_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
553 mcf_fec_state
*s
= qemu_get_nic_opaque(nc
);
560 unsigned int buf_len
;
563 DPRINTF("do_rx len %d\n", size
);
564 if (!s
->rx_enabled
) {
567 /* 4 bytes for the CRC. */
569 crc
= cpu_to_be32(crc32(~0, buf
, size
));
570 crc_ptr
= (uint8_t *)&crc
;
571 /* Huge frames are truncted. */
572 if (size
> FEC_MAX_FRAME_SIZE
) {
573 size
= FEC_MAX_FRAME_SIZE
;
574 flags
|= FEC_BD_TR
| FEC_BD_LG
;
576 /* Frames larger than the user limit just set error flags. */
577 if (size
> (s
->rcr
>> 16)) {
580 /* Check if we have enough space in current descriptors */
581 if (!mcf_fec_have_receive_space(s
, size
)) {
584 addr
= s
->rx_descriptor
;
587 mcf_fec_read_bd(&bd
, addr
);
588 buf_len
= (size
<= s
->emrbr
) ? size
: s
->emrbr
;
591 DPRINTF("rx_bd %x length %d\n", addr
, bd
.length
);
592 /* The last 4 bytes are the CRC. */
596 cpu_physical_memory_write(buf_addr
, buf
, buf_len
);
599 cpu_physical_memory_write(buf_addr
+ buf_len
, crc_ptr
, 4 - size
);
602 bd
.flags
&= ~FEC_BD_E
;
604 /* Last buffer in frame. */
605 bd
.flags
|= flags
| FEC_BD_L
;
606 DPRINTF("rx frame flags %04x\n", bd
.flags
);
607 s
->eir
|= FEC_INT_RXF
;
609 s
->eir
|= FEC_INT_RXB
;
611 mcf_fec_write_bd(&bd
, addr
);
612 /* Advance to the next descriptor. */
613 if ((bd
.flags
& FEC_BD_W
) != 0) {
619 s
->rx_descriptor
= addr
;
620 mcf_fec_rx_stats(s
, retsize
);
621 mcf_fec_enable_rx(s
);
626 static const MemoryRegionOps mcf_fec_ops
= {
627 .read
= mcf_fec_read
,
628 .write
= mcf_fec_write
,
629 .endianness
= DEVICE_NATIVE_ENDIAN
,
632 static NetClientInfo net_mcf_fec_info
= {
633 .type
= NET_CLIENT_DRIVER_NIC
,
634 .size
= sizeof(NICState
),
635 .receive
= mcf_fec_receive
,
638 static void mcf_fec_realize(DeviceState
*dev
, Error
**errp
)
640 mcf_fec_state
*s
= MCF_FEC_NET(dev
);
642 s
->nic
= qemu_new_nic(&net_mcf_fec_info
, &s
->conf
,
643 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
644 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
647 static void mcf_fec_instance_init(Object
*obj
)
649 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
650 mcf_fec_state
*s
= MCF_FEC_NET(obj
);
653 memory_region_init_io(&s
->iomem
, obj
, &mcf_fec_ops
, s
, "fec", 0x400);
654 sysbus_init_mmio(sbd
, &s
->iomem
);
655 for (i
= 0; i
< FEC_NUM_IRQ
; i
++) {
656 sysbus_init_irq(sbd
, &s
->irq
[i
]);
660 static Property mcf_fec_properties
[] = {
661 DEFINE_NIC_PROPERTIES(mcf_fec_state
, conf
),
662 DEFINE_PROP_END_OF_LIST(),
665 static void mcf_fec_class_init(ObjectClass
*oc
, void *data
)
667 DeviceClass
*dc
= DEVICE_CLASS(oc
);
669 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
670 dc
->realize
= mcf_fec_realize
;
671 dc
->desc
= "MCF Fast Ethernet Controller network device";
672 dc
->reset
= mcf_fec_reset
;
673 dc
->props
= mcf_fec_properties
;
676 static const TypeInfo mcf_fec_info
= {
677 .name
= TYPE_MCF_FEC_NET
,
678 .parent
= TYPE_SYS_BUS_DEVICE
,
679 .instance_size
= sizeof(mcf_fec_state
),
680 .instance_init
= mcf_fec_instance_init
,
681 .class_init
= mcf_fec_class_init
,
684 static void mcf_fec_register_types(void)
686 type_register_static(&mcf_fec_info
);
689 type_init(mcf_fec_register_types
)