2 * SMSC 91C111 Ethernet interface emulation
4 * Copyright (c) 2005 CodeSourcery, LLC.
5 * Written by Paul Brook
7 * This code is licenced 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 #define RCR_SOFT_RST 0x8000
50 #define RCR_STRIP_CRC 0x0200
51 #define RCR_RXEN 0x0100
53 #define TCR_EPH_LOOP 0x2000
54 #define TCR_NOCRC 0x0100
55 #define TCR_PAD_EN 0x0080
56 #define TCR_FORCOL 0x0004
57 #define TCR_LOOP 0x0002
58 #define TCR_TXEN 0x0001
63 #define INT_RX_OVRN 0x10
64 #define INT_ALLOC 0x08
65 #define INT_TX_EMPTY 0x04
69 #define CTR_AUTO_RELEASE 0x0800
70 #define CTR_RELOAD 0x0002
71 #define CTR_STORE 0x0001
73 #define RS_ALGNERR 0x8000
74 #define RS_BRODCAST 0x4000
75 #define RS_BADCRC 0x2000
76 #define RS_ODDFRAME 0x1000
77 #define RS_TOOLONG 0x0800
78 #define RS_TOOSHORT 0x0400
79 #define RS_MULTICAST 0x0001
81 /* Update interrupt status. */
82 static void smc91c111_update(smc91c111_state
*s
)
86 if (s
->tx_fifo_len
== 0)
87 s
->int_level
|= INT_TX_EMPTY
;
88 if (s
->tx_fifo_done_len
!= 0)
89 s
->int_level
|= INT_TX
;
90 level
= (s
->int_level
& s
->int_mask
) != 0;
91 qemu_set_irq(s
->irq
, level
);
94 /* Try to allocate a packet. Returns 0x80 on failure. */
95 static int smc91c111_allocate_packet(smc91c111_state
*s
)
98 if (s
->allocated
== (1 << NUM_PACKETS
) - 1) {
102 for (i
= 0; i
< NUM_PACKETS
; i
++) {
103 if ((s
->allocated
& (1 << i
)) == 0)
106 s
->allocated
|= 1 << i
;
111 /* Process a pending TX allocate. */
112 static void smc91c111_tx_alloc(smc91c111_state
*s
)
114 s
->tx_alloc
= smc91c111_allocate_packet(s
);
115 if (s
->tx_alloc
== 0x80)
117 s
->int_level
|= INT_ALLOC
;
121 /* Remove and item from the RX FIFO. */
122 static void smc91c111_pop_rx_fifo(smc91c111_state
*s
)
127 if (s
->rx_fifo_len
) {
128 for (i
= 0; i
< s
->rx_fifo_len
; i
++)
129 s
->rx_fifo
[i
] = s
->rx_fifo
[i
+ 1];
130 s
->int_level
|= INT_RCV
;
132 s
->int_level
&= ~INT_RCV
;
137 /* Remove an item from the TX completion FIFO. */
138 static void smc91c111_pop_tx_fifo_done(smc91c111_state
*s
)
142 if (s
->tx_fifo_done_len
== 0)
144 s
->tx_fifo_done_len
--;
145 for (i
= 0; i
< s
->tx_fifo_done_len
; i
++)
146 s
->tx_fifo_done
[i
] = s
->tx_fifo_done
[i
+ 1];
149 /* Release the memory allocated to a packet. */
150 static void smc91c111_release_packet(smc91c111_state
*s
, int packet
)
152 s
->allocated
&= ~(1 << packet
);
153 if (s
->tx_alloc
== 0x80)
154 smc91c111_tx_alloc(s
);
157 /* Flush the TX FIFO. */
158 static void smc91c111_do_tx(smc91c111_state
*s
)
166 if ((s
->tcr
& TCR_TXEN
) == 0)
168 if (s
->tx_fifo_len
== 0)
170 for (i
= 0; i
< s
->tx_fifo_len
; i
++) {
171 packetnum
= s
->tx_fifo
[i
];
172 p
= &s
->data
[packetnum
][0];
173 /* Set status word. */
177 len
|= ((int)*(p
++)) << 8;
179 control
= p
[len
+ 1];
182 /* ??? This overwrites the data following the buffer.
183 Don't know what real hardware does. */
184 if (len
< 64 && (s
->tcr
& TCR_PAD_EN
)) {
185 memset(p
+ len
, 0, 64 - len
);
192 /* The card is supposed to append the CRC to the frame.
193 However none of the other network traffic has the CRC
194 appended. Suspect this is low level ethernet detail we
195 don't need to worry about. */
196 add_crc
= (control
& 0x10) || (s
->tcr
& TCR_NOCRC
) == 0;
200 crc
= crc32(~0, p
, len
);
201 memcpy(p
+ len
, &crc
, 4);
206 if (s
->ctr
& CTR_AUTO_RELEASE
)
208 smc91c111_release_packet(s
, packetnum
);
209 else if (s
->tx_fifo_done_len
< NUM_PACKETS
)
210 s
->tx_fifo_done
[s
->tx_fifo_done_len
++] = packetnum
;
211 qemu_send_packet(&s
->nic
->nc
, p
, len
);
217 /* Add a packet to the TX FIFO. */
218 static void smc91c111_queue_tx(smc91c111_state
*s
, int packet
)
220 if (s
->tx_fifo_len
== NUM_PACKETS
)
222 s
->tx_fifo
[s
->tx_fifo_len
++] = packet
;
226 static void smc91c111_reset(smc91c111_state
*s
)
230 s
->tx_fifo_done_len
= 0;
241 s
->int_level
= INT_TX_EMPTY
;
246 #define SET_LOW(name, val) s->name = (s->name & 0xff00) | val
247 #define SET_HIGH(name, val) s->name = (s->name & 0xff) | (val << 8)
249 static void smc91c111_writeb(void *opaque
, target_phys_addr_t offset
,
252 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
254 offset
= offset
& 0xf;
268 SET_HIGH(tcr
, value
);
274 SET_HIGH(rcr
, value
);
275 if (s
->rcr
& RCR_SOFT_RST
)
278 case 10: case 11: /* RPCR */
281 case 12: case 13: /* Reserved */
294 case 2: case 3: /* BASE */
295 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
296 /* Not implemented. */
298 case 10: /* Genral Purpose */
302 SET_HIGH(gpr
, value
);
304 case 12: /* Control */
306 fprintf(stderr
, "smc91c111:EEPROM store not implemented\n");
308 fprintf(stderr
, "smc91c111:EEPROM reload not implemented\n");
313 SET_HIGH(ctr
, value
);
320 case 0: /* MMU Command */
321 switch (value
>> 5) {
324 case 1: /* Allocate for TX. */
326 s
->int_level
&= ~INT_ALLOC
;
328 smc91c111_tx_alloc(s
);
330 case 2: /* Reset MMU. */
333 s
->tx_fifo_done_len
= 0;
337 case 3: /* Remove from RX FIFO. */
338 smc91c111_pop_rx_fifo(s
);
340 case 4: /* Remove from RX FIFO and release. */
341 if (s
->rx_fifo_len
> 0) {
342 smc91c111_release_packet(s
, s
->rx_fifo
[0]);
344 smc91c111_pop_rx_fifo(s
);
346 case 5: /* Release. */
347 smc91c111_release_packet(s
, s
->packet_num
);
349 case 6: /* Add to TX FIFO. */
350 smc91c111_queue_tx(s
, s
->packet_num
);
352 case 7: /* Reset TX FIFO. */
354 s
->tx_fifo_done_len
= 0;
361 case 2: /* Packet Number Register */
362 s
->packet_num
= value
;
364 case 3: case 4: case 5:
365 /* Should be readonly, but linux writes to them anyway. Ignore. */
367 case 6: /* Pointer */
371 SET_HIGH(ptr
, value
);
373 case 8: case 9: case 10: case 11: /* Data */
383 if (s
->ptr
& 0x4000) {
384 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x7ff);
388 s
->data
[n
][p
] = value
;
391 case 12: /* Interrupt ACK. */
392 s
->int_level
&= ~(value
& 0xd6);
394 smc91c111_pop_tx_fifo_done(s
);
397 case 13: /* Interrupt mask. */
406 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
407 /* Multicast table. */
408 /* Not implemented. */
410 case 8: case 9: /* Management Interface. */
411 /* Not implemented. */
413 case 12: /* Early receive. */
414 s
->ercv
= value
& 0x1f;
421 hw_error("smc91c111_write: Bad reg %d:%x\n", s
->bank
, (int)offset
);
424 static uint32_t smc91c111_readb(void *opaque
, target_phys_addr_t offset
)
426 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
428 offset
= offset
& 0xf;
438 return s
->tcr
& 0xff;
441 case 2: /* EPH Status */
446 return s
->rcr
& 0xff;
449 case 6: /* Counter */
451 /* Not implemented. */
453 case 8: /* Memory size. */
455 case 9: /* Free memory available. */
460 for (i
= 0; i
< NUM_PACKETS
; i
++) {
461 if (s
->allocated
& (1 << i
))
466 case 10: case 11: /* RPCR */
467 /* Not implemented. */
469 case 12: case 13: /* Reserved */
480 case 2: case 3: /* BASE */
481 /* Not implemented. */
483 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
484 return s
->conf
.macaddr
.a
[offset
- 4];
485 case 10: /* General Purpose */
486 return s
->gpr
& 0xff;
489 case 12: /* Control */
490 return s
->ctr
& 0xff;
498 case 0: case 1: /* MMUCR Busy bit. */
500 case 2: /* Packet Number. */
501 return s
->packet_num
;
502 case 3: /* Allocation Result. */
504 case 4: /* TX FIFO */
505 if (s
->tx_fifo_done_len
== 0)
508 return s
->tx_fifo_done
[0];
509 case 5: /* RX FIFO */
510 if (s
->rx_fifo_len
== 0)
513 return s
->rx_fifo
[0];
514 case 6: /* Pointer */
515 return s
->ptr
& 0xff;
517 return (s
->ptr
>> 8) & 0xf7;
518 case 8: case 9: case 10: case 11: /* Data */
528 if (s
->ptr
& 0x4000) {
529 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x07ff);
533 return s
->data
[n
][p
];
535 case 12: /* Interrupt status. */
537 case 13: /* Interrupt mask. */
544 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
545 /* Multicast table. */
546 /* Not implemented. */
548 case 8: /* Management Interface. */
549 /* Not implemented. */
553 case 10: /* Revision. */
564 hw_error("smc91c111_read: Bad reg %d:%x\n", s
->bank
, (int)offset
);
568 static void smc91c111_writew(void *opaque
, target_phys_addr_t offset
,
571 smc91c111_writeb(opaque
, offset
, value
& 0xff);
572 smc91c111_writeb(opaque
, offset
+ 1, value
>> 8);
575 static void smc91c111_writel(void *opaque
, target_phys_addr_t offset
,
578 /* 32-bit writes to offset 0xc only actually write to the bank select
579 register (offset 0xe) */
581 smc91c111_writew(opaque
, offset
, value
& 0xffff);
582 smc91c111_writew(opaque
, offset
+ 2, value
>> 16);
585 static uint32_t smc91c111_readw(void *opaque
, target_phys_addr_t offset
)
588 val
= smc91c111_readb(opaque
, offset
);
589 val
|= smc91c111_readb(opaque
, offset
+ 1) << 8;
593 static uint32_t smc91c111_readl(void *opaque
, target_phys_addr_t offset
)
596 val
= smc91c111_readw(opaque
, offset
);
597 val
|= smc91c111_readw(opaque
, offset
+ 2) << 16;
601 static int smc91c111_can_receive(VLANClientState
*nc
)
603 smc91c111_state
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
605 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
607 if (s
->allocated
== (1 << NUM_PACKETS
) - 1)
612 static ssize_t
smc91c111_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
614 smc91c111_state
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
621 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
623 /* Short packets are padded with zeros. Receiving a packet
624 < 64 bytes long is considered an error condition. */
628 packetsize
= (size
& ~1);
630 crc
= (s
->rcr
& RCR_STRIP_CRC
) == 0;
633 /* TODO: Flag overrun and receive errors. */
634 if (packetsize
> 2048)
636 packetnum
= smc91c111_allocate_packet(s
);
637 if (packetnum
== 0x80)
639 s
->rx_fifo
[s
->rx_fifo_len
++] = packetnum
;
641 p
= &s
->data
[packetnum
][0];
642 /* ??? Multicast packets? */
645 status
|= RS_TOOLONG
;
647 status
|= RS_ODDFRAME
;
648 *(p
++) = status
& 0xff;
649 *(p
++) = status
>> 8;
650 *(p
++) = packetsize
& 0xff;
651 *(p
++) = packetsize
>> 8;
652 memcpy(p
, buf
, size
& ~1);
654 /* Pad short packets. */
659 *(p
++) = buf
[size
- 1];
665 /* It's not clear if the CRC should go before or after the last byte in
666 odd sized packets. Linux disables the CRC, so that's no help.
667 The pictures in the documentation show the CRC aligned on a 16-bit
668 boundary before the last odd byte, so that's what we do. */
670 crc
= crc32(~0, buf
, size
);
671 *(p
++) = crc
& 0xff; crc
>>= 8;
672 *(p
++) = crc
& 0xff; crc
>>= 8;
673 *(p
++) = crc
& 0xff; crc
>>= 8;
677 *(p
++) = buf
[size
- 1];
683 /* TODO: Raise early RX interrupt? */
684 s
->int_level
|= INT_RCV
;
690 static CPUReadMemoryFunc
* const smc91c111_readfn
[] = {
696 static CPUWriteMemoryFunc
* const smc91c111_writefn
[] = {
702 static void smc91c111_cleanup(VLANClientState
*nc
)
704 smc91c111_state
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
709 static NetClientInfo net_smc91c111_info
= {
710 .type
= NET_CLIENT_TYPE_NIC
,
711 .size
= sizeof(NICState
),
712 .can_receive
= smc91c111_can_receive
,
713 .receive
= smc91c111_receive
,
714 .cleanup
= smc91c111_cleanup
,
717 static int smc91c111_init1(SysBusDevice
*dev
)
719 smc91c111_state
*s
= FROM_SYSBUS(smc91c111_state
, dev
);
721 s
->mmio_index
= cpu_register_io_memory(smc91c111_readfn
,
722 smc91c111_writefn
, s
);
723 sysbus_init_mmio(dev
, 16, s
->mmio_index
);
724 sysbus_init_irq(dev
, &s
->irq
);
725 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
729 s
->nic
= qemu_new_nic(&net_smc91c111_info
, &s
->conf
,
730 dev
->qdev
.info
->name
, dev
->qdev
.id
, s
);
731 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
732 /* ??? Save/restore. */
736 static SysBusDeviceInfo smc91c111_info
= {
737 .init
= smc91c111_init1
,
738 .qdev
.name
= "smc91c111",
739 .qdev
.size
= sizeof(smc91c111_state
),
740 .qdev
.props
= (Property
[]) {
741 DEFINE_NIC_PROPERTIES(smc91c111_state
, conf
),
742 DEFINE_PROP_END_OF_LIST(),
746 static void smc91c111_register_devices(void)
748 sysbus_register_withprop(&smc91c111_info
);
751 /* Legacy helper function. Should go away when machine config files are
753 void smc91c111_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
758 qemu_check_nic_model(nd
, "smc91c111");
759 dev
= qdev_create(NULL
, "smc91c111");
760 qdev_set_nic_properties(dev
, nd
);
761 qdev_init_nofail(dev
);
762 s
= sysbus_from_qdev(dev
);
763 sysbus_mmio_map(s
, 0, base
);
764 sysbus_connect_irq(s
, 0, irq
);
767 device_init(smc91c111_register_devices
)