2 * SMSC 91C111 Ethernet interface emulation
4 * Copyright (c) 2005 CodeSourcery, LLC.
5 * Written by Paul Brook
7 * This code is licensed under the GPL
16 /* Number of 2k memory pages available. */
34 /* Bitmask of allocated packets. */
37 int tx_fifo
[NUM_PACKETS
];
39 int rx_fifo
[NUM_PACKETS
];
41 int tx_fifo_done
[NUM_PACKETS
];
42 /* Packet buffer memory. */
43 uint8_t data
[NUM_PACKETS
][2048];
49 static const VMStateDescription vmstate_smc91c111
= {
52 .minimum_version_id
= 1,
53 .fields
= (VMStateField
[]) {
54 VMSTATE_UINT16(tcr
, smc91c111_state
),
55 VMSTATE_UINT16(rcr
, smc91c111_state
),
56 VMSTATE_UINT16(cr
, smc91c111_state
),
57 VMSTATE_UINT16(ctr
, smc91c111_state
),
58 VMSTATE_UINT16(gpr
, smc91c111_state
),
59 VMSTATE_UINT16(ptr
, smc91c111_state
),
60 VMSTATE_UINT16(ercv
, smc91c111_state
),
61 VMSTATE_INT32(bank
, smc91c111_state
),
62 VMSTATE_INT32(packet_num
, smc91c111_state
),
63 VMSTATE_INT32(tx_alloc
, smc91c111_state
),
64 VMSTATE_INT32(allocated
, smc91c111_state
),
65 VMSTATE_INT32(tx_fifo_len
, smc91c111_state
),
66 VMSTATE_INT32_ARRAY(tx_fifo
, smc91c111_state
, NUM_PACKETS
),
67 VMSTATE_INT32(rx_fifo_len
, smc91c111_state
),
68 VMSTATE_INT32_ARRAY(rx_fifo
, smc91c111_state
, NUM_PACKETS
),
69 VMSTATE_INT32(tx_fifo_done_len
, smc91c111_state
),
70 VMSTATE_INT32_ARRAY(tx_fifo_done
, smc91c111_state
, NUM_PACKETS
),
71 VMSTATE_BUFFER_UNSAFE(data
, smc91c111_state
, 0, NUM_PACKETS
* 2048),
72 VMSTATE_UINT8(int_level
, smc91c111_state
),
73 VMSTATE_UINT8(int_mask
, smc91c111_state
),
78 #define RCR_SOFT_RST 0x8000
79 #define RCR_STRIP_CRC 0x0200
80 #define RCR_RXEN 0x0100
82 #define TCR_EPH_LOOP 0x2000
83 #define TCR_NOCRC 0x0100
84 #define TCR_PAD_EN 0x0080
85 #define TCR_FORCOL 0x0004
86 #define TCR_LOOP 0x0002
87 #define TCR_TXEN 0x0001
92 #define INT_RX_OVRN 0x10
93 #define INT_ALLOC 0x08
94 #define INT_TX_EMPTY 0x04
98 #define CTR_AUTO_RELEASE 0x0800
99 #define CTR_RELOAD 0x0002
100 #define CTR_STORE 0x0001
102 #define RS_ALGNERR 0x8000
103 #define RS_BRODCAST 0x4000
104 #define RS_BADCRC 0x2000
105 #define RS_ODDFRAME 0x1000
106 #define RS_TOOLONG 0x0800
107 #define RS_TOOSHORT 0x0400
108 #define RS_MULTICAST 0x0001
110 /* Update interrupt status. */
111 static void smc91c111_update(smc91c111_state
*s
)
115 if (s
->tx_fifo_len
== 0)
116 s
->int_level
|= INT_TX_EMPTY
;
117 if (s
->tx_fifo_done_len
!= 0)
118 s
->int_level
|= INT_TX
;
119 level
= (s
->int_level
& s
->int_mask
) != 0;
120 qemu_set_irq(s
->irq
, level
);
123 /* Try to allocate a packet. Returns 0x80 on failure. */
124 static int smc91c111_allocate_packet(smc91c111_state
*s
)
127 if (s
->allocated
== (1 << NUM_PACKETS
) - 1) {
131 for (i
= 0; i
< NUM_PACKETS
; i
++) {
132 if ((s
->allocated
& (1 << i
)) == 0)
135 s
->allocated
|= 1 << i
;
140 /* Process a pending TX allocate. */
141 static void smc91c111_tx_alloc(smc91c111_state
*s
)
143 s
->tx_alloc
= smc91c111_allocate_packet(s
);
144 if (s
->tx_alloc
== 0x80)
146 s
->int_level
|= INT_ALLOC
;
150 /* Remove and item from the RX FIFO. */
151 static void smc91c111_pop_rx_fifo(smc91c111_state
*s
)
156 if (s
->rx_fifo_len
) {
157 for (i
= 0; i
< s
->rx_fifo_len
; i
++)
158 s
->rx_fifo
[i
] = s
->rx_fifo
[i
+ 1];
159 s
->int_level
|= INT_RCV
;
161 s
->int_level
&= ~INT_RCV
;
166 /* Remove an item from the TX completion FIFO. */
167 static void smc91c111_pop_tx_fifo_done(smc91c111_state
*s
)
171 if (s
->tx_fifo_done_len
== 0)
173 s
->tx_fifo_done_len
--;
174 for (i
= 0; i
< s
->tx_fifo_done_len
; i
++)
175 s
->tx_fifo_done
[i
] = s
->tx_fifo_done
[i
+ 1];
178 /* Release the memory allocated to a packet. */
179 static void smc91c111_release_packet(smc91c111_state
*s
, int packet
)
181 s
->allocated
&= ~(1 << packet
);
182 if (s
->tx_alloc
== 0x80)
183 smc91c111_tx_alloc(s
);
186 /* Flush the TX FIFO. */
187 static void smc91c111_do_tx(smc91c111_state
*s
)
195 if ((s
->tcr
& TCR_TXEN
) == 0)
197 if (s
->tx_fifo_len
== 0)
199 for (i
= 0; i
< s
->tx_fifo_len
; i
++) {
200 packetnum
= s
->tx_fifo
[i
];
201 p
= &s
->data
[packetnum
][0];
202 /* Set status word. */
206 len
|= ((int)*(p
++)) << 8;
208 control
= p
[len
+ 1];
211 /* ??? This overwrites the data following the buffer.
212 Don't know what real hardware does. */
213 if (len
< 64 && (s
->tcr
& TCR_PAD_EN
)) {
214 memset(p
+ len
, 0, 64 - len
);
221 /* The card is supposed to append the CRC to the frame.
222 However none of the other network traffic has the CRC
223 appended. Suspect this is low level ethernet detail we
224 don't need to worry about. */
225 add_crc
= (control
& 0x10) || (s
->tcr
& TCR_NOCRC
) == 0;
229 crc
= crc32(~0, p
, len
);
230 memcpy(p
+ len
, &crc
, 4);
235 if (s
->ctr
& CTR_AUTO_RELEASE
)
237 smc91c111_release_packet(s
, packetnum
);
238 else if (s
->tx_fifo_done_len
< NUM_PACKETS
)
239 s
->tx_fifo_done
[s
->tx_fifo_done_len
++] = packetnum
;
240 qemu_send_packet(&s
->nic
->nc
, p
, len
);
246 /* Add a packet to the TX FIFO. */
247 static void smc91c111_queue_tx(smc91c111_state
*s
, int packet
)
249 if (s
->tx_fifo_len
== NUM_PACKETS
)
251 s
->tx_fifo
[s
->tx_fifo_len
++] = packet
;
255 static void smc91c111_reset(DeviceState
*dev
)
257 smc91c111_state
*s
= FROM_SYSBUS(smc91c111_state
, sysbus_from_qdev(dev
));
260 s
->tx_fifo_done_len
= 0;
271 s
->int_level
= INT_TX_EMPTY
;
276 #define SET_LOW(name, val) s->name = (s->name & 0xff00) | val
277 #define SET_HIGH(name, val) s->name = (s->name & 0xff) | (val << 8)
279 static void smc91c111_writeb(void *opaque
, target_phys_addr_t offset
,
282 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
284 offset
= offset
& 0xf;
298 SET_HIGH(tcr
, value
);
304 SET_HIGH(rcr
, value
);
305 if (s
->rcr
& RCR_SOFT_RST
)
306 smc91c111_reset(&s
->busdev
.qdev
);
308 case 10: case 11: /* RPCR */
311 case 12: case 13: /* Reserved */
324 case 2: case 3: /* BASE */
325 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
326 /* Not implemented. */
328 case 10: /* Genral Purpose */
332 SET_HIGH(gpr
, value
);
334 case 12: /* Control */
336 fprintf(stderr
, "smc91c111:EEPROM store not implemented\n");
338 fprintf(stderr
, "smc91c111:EEPROM reload not implemented\n");
343 SET_HIGH(ctr
, value
);
350 case 0: /* MMU Command */
351 switch (value
>> 5) {
354 case 1: /* Allocate for TX. */
356 s
->int_level
&= ~INT_ALLOC
;
358 smc91c111_tx_alloc(s
);
360 case 2: /* Reset MMU. */
363 s
->tx_fifo_done_len
= 0;
367 case 3: /* Remove from RX FIFO. */
368 smc91c111_pop_rx_fifo(s
);
370 case 4: /* Remove from RX FIFO and release. */
371 if (s
->rx_fifo_len
> 0) {
372 smc91c111_release_packet(s
, s
->rx_fifo
[0]);
374 smc91c111_pop_rx_fifo(s
);
376 case 5: /* Release. */
377 smc91c111_release_packet(s
, s
->packet_num
);
379 case 6: /* Add to TX FIFO. */
380 smc91c111_queue_tx(s
, s
->packet_num
);
382 case 7: /* Reset TX FIFO. */
384 s
->tx_fifo_done_len
= 0;
391 case 2: /* Packet Number Register */
392 s
->packet_num
= value
;
394 case 3: case 4: case 5:
395 /* Should be readonly, but linux writes to them anyway. Ignore. */
397 case 6: /* Pointer */
401 SET_HIGH(ptr
, value
);
403 case 8: case 9: case 10: case 11: /* Data */
413 if (s
->ptr
& 0x4000) {
414 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x7ff);
418 s
->data
[n
][p
] = value
;
421 case 12: /* Interrupt ACK. */
422 s
->int_level
&= ~(value
& 0xd6);
424 smc91c111_pop_tx_fifo_done(s
);
427 case 13: /* Interrupt mask. */
436 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
437 /* Multicast table. */
438 /* Not implemented. */
440 case 8: case 9: /* Management Interface. */
441 /* Not implemented. */
443 case 12: /* Early receive. */
444 s
->ercv
= value
& 0x1f;
451 hw_error("smc91c111_write: Bad reg %d:%x\n", s
->bank
, (int)offset
);
454 static uint32_t smc91c111_readb(void *opaque
, target_phys_addr_t offset
)
456 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
458 offset
= offset
& 0xf;
468 return s
->tcr
& 0xff;
471 case 2: /* EPH Status */
476 return s
->rcr
& 0xff;
479 case 6: /* Counter */
481 /* Not implemented. */
483 case 8: /* Memory size. */
485 case 9: /* Free memory available. */
490 for (i
= 0; i
< NUM_PACKETS
; i
++) {
491 if (s
->allocated
& (1 << i
))
496 case 10: case 11: /* RPCR */
497 /* Not implemented. */
499 case 12: case 13: /* Reserved */
510 case 2: case 3: /* BASE */
511 /* Not implemented. */
513 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
514 return s
->conf
.macaddr
.a
[offset
- 4];
515 case 10: /* General Purpose */
516 return s
->gpr
& 0xff;
519 case 12: /* Control */
520 return s
->ctr
& 0xff;
528 case 0: case 1: /* MMUCR Busy bit. */
530 case 2: /* Packet Number. */
531 return s
->packet_num
;
532 case 3: /* Allocation Result. */
534 case 4: /* TX FIFO */
535 if (s
->tx_fifo_done_len
== 0)
538 return s
->tx_fifo_done
[0];
539 case 5: /* RX FIFO */
540 if (s
->rx_fifo_len
== 0)
543 return s
->rx_fifo
[0];
544 case 6: /* Pointer */
545 return s
->ptr
& 0xff;
547 return (s
->ptr
>> 8) & 0xf7;
548 case 8: case 9: case 10: case 11: /* Data */
558 if (s
->ptr
& 0x4000) {
559 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x07ff);
563 return s
->data
[n
][p
];
565 case 12: /* Interrupt status. */
567 case 13: /* Interrupt mask. */
574 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
575 /* Multicast table. */
576 /* Not implemented. */
578 case 8: /* Management Interface. */
579 /* Not implemented. */
583 case 10: /* Revision. */
594 hw_error("smc91c111_read: Bad reg %d:%x\n", s
->bank
, (int)offset
);
598 static void smc91c111_writew(void *opaque
, target_phys_addr_t offset
,
601 smc91c111_writeb(opaque
, offset
, value
& 0xff);
602 smc91c111_writeb(opaque
, offset
+ 1, value
>> 8);
605 static void smc91c111_writel(void *opaque
, target_phys_addr_t offset
,
608 /* 32-bit writes to offset 0xc only actually write to the bank select
609 register (offset 0xe) */
611 smc91c111_writew(opaque
, offset
, value
& 0xffff);
612 smc91c111_writew(opaque
, offset
+ 2, value
>> 16);
615 static uint32_t smc91c111_readw(void *opaque
, target_phys_addr_t offset
)
618 val
= smc91c111_readb(opaque
, offset
);
619 val
|= smc91c111_readb(opaque
, offset
+ 1) << 8;
623 static uint32_t smc91c111_readl(void *opaque
, target_phys_addr_t offset
)
626 val
= smc91c111_readw(opaque
, offset
);
627 val
|= smc91c111_readw(opaque
, offset
+ 2) << 16;
631 static int smc91c111_can_receive(NetClientState
*nc
)
633 smc91c111_state
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
635 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
637 if (s
->allocated
== (1 << NUM_PACKETS
) - 1)
642 static ssize_t
smc91c111_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
644 smc91c111_state
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
651 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
653 /* Short packets are padded with zeros. Receiving a packet
654 < 64 bytes long is considered an error condition. */
658 packetsize
= (size
& ~1);
660 crc
= (s
->rcr
& RCR_STRIP_CRC
) == 0;
663 /* TODO: Flag overrun and receive errors. */
664 if (packetsize
> 2048)
666 packetnum
= smc91c111_allocate_packet(s
);
667 if (packetnum
== 0x80)
669 s
->rx_fifo
[s
->rx_fifo_len
++] = packetnum
;
671 p
= &s
->data
[packetnum
][0];
672 /* ??? Multicast packets? */
675 status
|= RS_TOOLONG
;
677 status
|= RS_ODDFRAME
;
678 *(p
++) = status
& 0xff;
679 *(p
++) = status
>> 8;
680 *(p
++) = packetsize
& 0xff;
681 *(p
++) = packetsize
>> 8;
682 memcpy(p
, buf
, size
& ~1);
684 /* Pad short packets. */
689 *(p
++) = buf
[size
- 1];
695 /* It's not clear if the CRC should go before or after the last byte in
696 odd sized packets. Linux disables the CRC, so that's no help.
697 The pictures in the documentation show the CRC aligned on a 16-bit
698 boundary before the last odd byte, so that's what we do. */
700 crc
= crc32(~0, buf
, size
);
701 *(p
++) = crc
& 0xff; crc
>>= 8;
702 *(p
++) = crc
& 0xff; crc
>>= 8;
703 *(p
++) = crc
& 0xff; crc
>>= 8;
707 *(p
++) = buf
[size
- 1];
713 /* TODO: Raise early RX interrupt? */
714 s
->int_level
|= INT_RCV
;
720 static const MemoryRegionOps smc91c111_mem_ops
= {
721 /* The special case for 32 bit writes to 0xc means we can't just
722 * set .impl.min/max_access_size to 1, unfortunately
725 .read
= { smc91c111_readb
, smc91c111_readw
, smc91c111_readl
, },
726 .write
= { smc91c111_writeb
, smc91c111_writew
, smc91c111_writel
, },
728 .endianness
= DEVICE_NATIVE_ENDIAN
,
731 static void smc91c111_cleanup(NetClientState
*nc
)
733 smc91c111_state
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
738 static NetClientInfo net_smc91c111_info
= {
739 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
740 .size
= sizeof(NICState
),
741 .can_receive
= smc91c111_can_receive
,
742 .receive
= smc91c111_receive
,
743 .cleanup
= smc91c111_cleanup
,
746 static int smc91c111_init1(SysBusDevice
*dev
)
748 smc91c111_state
*s
= FROM_SYSBUS(smc91c111_state
, dev
);
749 memory_region_init_io(&s
->mmio
, &smc91c111_mem_ops
, s
,
750 "smc91c111-mmio", 16);
751 sysbus_init_mmio(dev
, &s
->mmio
);
752 sysbus_init_irq(dev
, &s
->irq
);
753 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
754 s
->nic
= qemu_new_nic(&net_smc91c111_info
, &s
->conf
,
755 object_get_typename(OBJECT(dev
)), dev
->qdev
.id
, s
);
756 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
757 /* ??? Save/restore. */
761 static Property smc91c111_properties
[] = {
762 DEFINE_NIC_PROPERTIES(smc91c111_state
, conf
),
763 DEFINE_PROP_END_OF_LIST(),
766 static void smc91c111_class_init(ObjectClass
*klass
, void *data
)
768 DeviceClass
*dc
= DEVICE_CLASS(klass
);
769 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
771 k
->init
= smc91c111_init1
;
772 dc
->reset
= smc91c111_reset
;
773 dc
->vmsd
= &vmstate_smc91c111
;
774 dc
->props
= smc91c111_properties
;
777 static TypeInfo smc91c111_info
= {
779 .parent
= TYPE_SYS_BUS_DEVICE
,
780 .instance_size
= sizeof(smc91c111_state
),
781 .class_init
= smc91c111_class_init
,
784 static void smc91c111_register_types(void)
786 type_register_static(&smc91c111_info
);
789 /* Legacy helper function. Should go away when machine config files are
791 void smc91c111_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
796 qemu_check_nic_model(nd
, "smc91c111");
797 dev
= qdev_create(NULL
, "smc91c111");
798 qdev_set_nic_properties(dev
, nd
);
799 qdev_init_nofail(dev
);
800 s
= sysbus_from_qdev(dev
);
801 sysbus_mmio_map(s
, 0, base
);
802 sysbus_connect_irq(s
, 0, irq
);
805 type_init(smc91c111_register_types
)