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. */
33 /* Bitmask of allocated packets. */
36 int tx_fifo
[NUM_PACKETS
];
38 int rx_fifo
[NUM_PACKETS
];
40 int tx_fifo_done
[NUM_PACKETS
];
41 /* Packet buffer memory. */
42 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
)
167 if ((s
->tcr
& TCR_TXEN
) == 0)
169 if (s
->tx_fifo_len
== 0)
171 for (i
= 0; i
< s
->tx_fifo_len
; i
++) {
172 packetnum
= s
->tx_fifo
[i
];
173 p
= &s
->data
[packetnum
][0];
174 /* Set status word. */
178 len
|= ((int)*(p
++)) << 8;
180 control
= p
[len
+ 1];
183 /* ??? This overwrites the data following the buffer.
184 Don't know what real hardware does. */
185 if (len
< 64 && (s
->tcr
& TCR_PAD_EN
)) {
186 memset(p
+ len
, 0, 64 - len
);
190 /* The card is supposed to append the CRC to the frame. However
191 none of the other network traffic has the CRC appended.
192 Suspect this is low level ethernet detail we don't need to worry
194 add_crc
= (control
& 0x10) || (s
->tcr
& TCR_NOCRC
) == 0;
198 crc
= crc32(~0, p
, len
);
199 memcpy(p
+ len
, &crc
, 4);
205 if (s
->ctr
& CTR_AUTO_RELEASE
)
207 smc91c111_release_packet(s
, packetnum
);
208 else if (s
->tx_fifo_done_len
< NUM_PACKETS
)
209 s
->tx_fifo_done
[s
->tx_fifo_done_len
++] = packetnum
;
210 qemu_send_packet(s
->vc
, p
, len
);
216 /* Add a packet to the TX FIFO. */
217 static void smc91c111_queue_tx(smc91c111_state
*s
, int packet
)
219 if (s
->tx_fifo_len
== NUM_PACKETS
)
221 s
->tx_fifo
[s
->tx_fifo_len
++] = packet
;
225 static void smc91c111_reset(smc91c111_state
*s
)
229 s
->tx_fifo_done_len
= 0;
240 s
->int_level
= INT_TX_EMPTY
;
245 #define SET_LOW(name, val) s->name = (s->name & 0xff00) | val
246 #define SET_HIGH(name, val) s->name = (s->name & 0xff) | (val << 8)
248 static void smc91c111_writeb(void *opaque
, target_phys_addr_t offset
,
251 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
266 SET_HIGH(tcr
, value
);
272 SET_HIGH(rcr
, value
);
273 if (s
->rcr
& RCR_SOFT_RST
)
276 case 10: case 11: /* RPCR */
290 case 2: case 3: /* BASE */
291 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
292 /* Not implemented. */
294 case 10: /* Genral Purpose */
298 SET_HIGH(gpr
, value
);
300 case 12: /* Control */
302 fprintf(stderr
, "smc91c111:EEPROM store not implemented\n");
304 fprintf(stderr
, "smc91c111:EEPROM reload not implemented\n");
309 SET_HIGH(ctr
, value
);
316 case 0: /* MMU Command */
317 switch (value
>> 5) {
320 case 1: /* Allocate for TX. */
322 s
->int_level
&= ~INT_ALLOC
;
324 smc91c111_tx_alloc(s
);
326 case 2: /* Reset MMU. */
329 s
->tx_fifo_done_len
= 0;
333 case 3: /* Remove from RX FIFO. */
334 smc91c111_pop_rx_fifo(s
);
336 case 4: /* Remove from RX FIFO and release. */
337 if (s
->rx_fifo_len
> 0) {
338 smc91c111_release_packet(s
, s
->rx_fifo
[0]);
340 smc91c111_pop_rx_fifo(s
);
342 case 5: /* Release. */
343 smc91c111_release_packet(s
, s
->packet_num
);
345 case 6: /* Add to TX FIFO. */
346 smc91c111_queue_tx(s
, s
->packet_num
);
348 case 7: /* Reset TX FIFO. */
350 s
->tx_fifo_done_len
= 0;
357 case 2: /* Packet Number Register */
358 s
->packet_num
= value
;
360 case 3: case 4: case 5:
361 /* Should be readonly, but linux writes to them anyway. Ignore. */
363 case 6: /* Pointer */
367 SET_HIGH(ptr
, value
);
369 case 8: case 9: case 10: case 11: /* Data */
379 if (s
->ptr
& 0x4000) {
380 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x7ff);
384 s
->data
[n
][p
] = value
;
387 case 12: /* Interrupt ACK. */
388 s
->int_level
&= ~(value
& 0xd6);
390 smc91c111_pop_tx_fifo_done(s
);
393 case 13: /* Interrupt mask. */
402 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
403 /* Multicast table. */
404 /* Not implemented. */
406 case 8: case 9: /* Management Interface. */
407 /* Not implemented. */
409 case 12: /* Early receive. */
410 s
->ercv
= value
& 0x1f;
417 hw_error("smc91c111_write: Bad reg %d:%x\n", s
->bank
, (int)offset
);
420 static uint32_t smc91c111_readb(void *opaque
, target_phys_addr_t offset
)
422 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
433 return s
->tcr
& 0xff;
436 case 2: /* EPH Status */
441 return s
->rcr
& 0xff;
444 case 6: /* Counter */
446 /* Not implemented. */
448 case 8: /* Memory size. */
450 case 9: /* Free memory available. */
455 for (i
= 0; i
< NUM_PACKETS
; i
++) {
456 if (s
->allocated
& (1 << i
))
461 case 10: case 11: /* RPCR */
462 /* Not implemented. */
473 case 2: case 3: /* BASE */
474 /* Not implemented. */
476 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
477 return s
->macaddr
[offset
- 4];
478 case 10: /* General Purpose */
479 return s
->gpr
& 0xff;
482 case 12: /* Control */
483 return s
->ctr
& 0xff;
491 case 0: case 1: /* MMUCR Busy bit. */
493 case 2: /* Packet Number. */
494 return s
->packet_num
;
495 case 3: /* Allocation Result. */
497 case 4: /* TX FIFO */
498 if (s
->tx_fifo_done_len
== 0)
501 return s
->tx_fifo_done
[0];
502 case 5: /* RX FIFO */
503 if (s
->rx_fifo_len
== 0)
506 return s
->rx_fifo
[0];
507 case 6: /* Pointer */
508 return s
->ptr
& 0xff;
510 return (s
->ptr
>> 8) & 0xf7;
511 case 8: case 9: case 10: case 11: /* Data */
521 if (s
->ptr
& 0x4000) {
522 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x07ff);
526 return s
->data
[n
][p
];
528 case 12: /* Interrupt status. */
530 case 13: /* Interrupt mask. */
537 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
538 /* Multicast table. */
539 /* Not implemented. */
541 case 8: /* Management Interface. */
542 /* Not implemented. */
546 case 10: /* Revision. */
557 hw_error("smc91c111_read: Bad reg %d:%x\n", s
->bank
, (int)offset
);
561 static void smc91c111_writew(void *opaque
, target_phys_addr_t offset
,
564 smc91c111_writeb(opaque
, offset
, value
& 0xff);
565 smc91c111_writeb(opaque
, offset
+ 1, value
>> 8);
568 static void smc91c111_writel(void *opaque
, target_phys_addr_t offset
,
571 /* 32-bit writes to offset 0xc only actually write to the bank select
572 register (offset 0xe) */
574 smc91c111_writew(opaque
, offset
, value
& 0xffff);
575 smc91c111_writew(opaque
, offset
+ 2, value
>> 16);
578 static uint32_t smc91c111_readw(void *opaque
, target_phys_addr_t offset
)
581 val
= smc91c111_readb(opaque
, offset
);
582 val
|= smc91c111_readb(opaque
, offset
+ 1) << 8;
586 static uint32_t smc91c111_readl(void *opaque
, target_phys_addr_t offset
)
589 val
= smc91c111_readw(opaque
, offset
);
590 val
|= smc91c111_readw(opaque
, offset
+ 2) << 16;
594 static int smc91c111_can_receive(void *opaque
)
596 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
598 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
600 if (s
->allocated
== (1 << NUM_PACKETS
) - 1)
605 static void smc91c111_receive(void *opaque
, const uint8_t *buf
, int size
)
607 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
614 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
616 /* Short packets are padded with zeros. Receiving a packet
617 < 64 bytes long is considered an error condition. */
621 packetsize
= (size
& ~1);
623 crc
= (s
->rcr
& RCR_STRIP_CRC
) == 0;
626 /* TODO: Flag overrun and receive errors. */
627 if (packetsize
> 2048)
629 packetnum
= smc91c111_allocate_packet(s
);
630 if (packetnum
== 0x80)
632 s
->rx_fifo
[s
->rx_fifo_len
++] = packetnum
;
634 p
= &s
->data
[packetnum
][0];
635 /* ??? Multicast packets? */
638 status
|= RS_TOOLONG
;
640 status
|= RS_ODDFRAME
;
641 *(p
++) = status
& 0xff;
642 *(p
++) = status
>> 8;
643 *(p
++) = packetsize
& 0xff;
644 *(p
++) = packetsize
>> 8;
645 memcpy(p
, buf
, size
& ~1);
647 /* Pad short packets. */
652 *(p
++) = buf
[size
- 1];
658 /* It's not clear if the CRC should go before or after the last byte in
659 odd sized packets. Linux disables the CRC, so that's no help.
660 The pictures in the documentation show the CRC aligned on a 16-bit
661 boundary before the last odd byte, so that's what we do. */
663 crc
= crc32(~0, buf
, size
);
664 *(p
++) = crc
& 0xff; crc
>>= 8;
665 *(p
++) = crc
& 0xff; crc
>>= 8;
666 *(p
++) = crc
& 0xff; crc
>>= 8;
667 *(p
++) = crc
& 0xff; crc
>>= 8;
670 *(p
++) = buf
[size
- 1];
676 /* TODO: Raise early RX interrupt? */
677 s
->int_level
|= INT_RCV
;
681 static CPUReadMemoryFunc
*smc91c111_readfn
[] = {
687 static CPUWriteMemoryFunc
*smc91c111_writefn
[] = {
693 static void smc91c111_cleanup(VLANClientState
*vc
)
695 smc91c111_state
*s
= vc
->opaque
;
697 cpu_unregister_io_memory(s
->mmio_index
);
701 static void smc91c111_init1(SysBusDevice
*dev
)
703 smc91c111_state
*s
= FROM_SYSBUS(smc91c111_state
, dev
);
705 s
->mmio_index
= cpu_register_io_memory(0, smc91c111_readfn
,
706 smc91c111_writefn
, s
);
707 sysbus_init_mmio(dev
, 16, s
->mmio_index
);
708 sysbus_init_irq(dev
, &s
->irq
);
709 qdev_get_macaddr(&dev
->qdev
, s
->macaddr
);
713 s
->vc
= qdev_get_vlan_client(&dev
->qdev
,
714 smc91c111_receive
, smc91c111_can_receive
,
715 smc91c111_cleanup
, s
);
716 qemu_format_nic_info_str(s
->vc
, s
->macaddr
);
717 /* ??? Save/restore. */
720 static void smc91c111_register_devices(void)
722 sysbus_register_dev("smc91c111", sizeof(smc91c111_state
), smc91c111_init1
);
725 /* Legacy helper function. Should go away when machine config files are
727 void smc91c111_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
732 qemu_check_nic_model(nd
, "smc91c111");
733 dev
= qdev_create(NULL
, "smc91c111");
734 qdev_set_netdev(dev
, nd
);
736 s
= sysbus_from_qdev(dev
);
737 sysbus_mmio_map(s
, 0, base
);
738 sysbus_connect_irq(s
, 0, irq
);
741 device_init(smc91c111_register_devices
)