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
14 /* Number of 2k memory pages available. */
31 /* Bitmask of allocated packets. */
34 int tx_fifo
[NUM_PACKETS
];
36 int rx_fifo
[NUM_PACKETS
];
38 int tx_fifo_done
[NUM_PACKETS
];
39 /* Packet buffer memory. */
40 uint8_t data
[NUM_PACKETS
][2048];
46 #define RCR_SOFT_RST 0x8000
47 #define RCR_STRIP_CRC 0x0200
48 #define RCR_RXEN 0x0100
50 #define TCR_EPH_LOOP 0x2000
51 #define TCR_NOCRC 0x0100
52 #define TCR_PAD_EN 0x0080
53 #define TCR_FORCOL 0x0004
54 #define TCR_LOOP 0x0002
55 #define TCR_TXEN 0x0001
60 #define INT_RX_OVRN 0x10
61 #define INT_ALLOC 0x08
62 #define INT_TX_EMPTY 0x04
66 #define CTR_AUTO_RELEASE 0x0800
67 #define CTR_RELOAD 0x0002
68 #define CTR_STORE 0x0001
70 #define RS_ALGNERR 0x8000
71 #define RS_BRODCAST 0x4000
72 #define RS_BADCRC 0x2000
73 #define RS_ODDFRAME 0x1000
74 #define RS_TOOLONG 0x0800
75 #define RS_TOOSHORT 0x0400
76 #define RS_MULTICAST 0x0001
78 /* Update interrupt status. */
79 static void smc91c111_update(smc91c111_state
*s
)
83 if (s
->tx_fifo_len
== 0)
84 s
->int_level
|= INT_TX_EMPTY
;
85 if (s
->tx_fifo_done_len
!= 0)
86 s
->int_level
|= INT_TX
;
87 level
= (s
->int_level
& s
->int_mask
) != 0;
88 qemu_set_irq(s
->irq
, level
);
91 /* Try to allocate a packet. Returns 0x80 on failure. */
92 static int smc91c111_allocate_packet(smc91c111_state
*s
)
95 if (s
->allocated
== (1 << NUM_PACKETS
) - 1) {
99 for (i
= 0; i
< NUM_PACKETS
; i
++) {
100 if ((s
->allocated
& (1 << i
)) == 0)
103 s
->allocated
|= 1 << i
;
108 /* Process a pending TX allocate. */
109 static void smc91c111_tx_alloc(smc91c111_state
*s
)
111 s
->tx_alloc
= smc91c111_allocate_packet(s
);
112 if (s
->tx_alloc
== 0x80)
114 s
->int_level
|= INT_ALLOC
;
118 /* Remove and item from the RX FIFO. */
119 static void smc91c111_pop_rx_fifo(smc91c111_state
*s
)
124 if (s
->rx_fifo_len
) {
125 for (i
= 0; i
< s
->rx_fifo_len
; i
++)
126 s
->rx_fifo
[i
] = s
->rx_fifo
[i
+ 1];
127 s
->int_level
|= INT_RCV
;
129 s
->int_level
&= ~INT_RCV
;
134 /* Remove an item from the TX completion FIFO. */
135 static void smc91c111_pop_tx_fifo_done(smc91c111_state
*s
)
139 if (s
->tx_fifo_done_len
== 0)
141 s
->tx_fifo_done_len
--;
142 for (i
= 0; i
< s
->tx_fifo_done_len
; i
++)
143 s
->tx_fifo_done
[i
] = s
->tx_fifo_done
[i
+ 1];
146 /* Release the memory allocated to a packet. */
147 static void smc91c111_release_packet(smc91c111_state
*s
, int packet
)
149 s
->allocated
&= ~(1 << packet
);
150 if (s
->tx_alloc
== 0x80)
151 smc91c111_tx_alloc(s
);
154 /* Flush the TX FIFO. */
155 static void smc91c111_do_tx(smc91c111_state
*s
)
164 if ((s
->tcr
& TCR_TXEN
) == 0)
166 if (s
->tx_fifo_len
== 0)
168 for (i
= 0; i
< s
->tx_fifo_len
; i
++) {
169 packetnum
= s
->tx_fifo
[i
];
170 p
= &s
->data
[packetnum
][0];
171 /* Set status word. */
175 len
|= ((int)*(p
++)) << 8;
177 control
= p
[len
+ 1];
180 /* ??? This overwrites the data following the buffer.
181 Don't know what real hardware does. */
182 if (len
< 64 && (s
->tcr
& TCR_PAD_EN
)) {
183 memset(p
+ len
, 0, 64 - len
);
187 /* The card is supposed to append the CRC to the frame. However
188 none of the other network traffic has the CRC appended.
189 Suspect this is low level ethernet detail we don't need to worry
191 add_crc
= (control
& 0x10) || (s
->tcr
& TCR_NOCRC
) == 0;
195 crc
= crc32(~0, p
, len
);
196 memcpy(p
+ len
, &crc
, 4);
202 if (s
->ctr
& CTR_AUTO_RELEASE
)
204 smc91c111_release_packet(s
, packetnum
);
205 else if (s
->tx_fifo_done_len
< NUM_PACKETS
)
206 s
->tx_fifo_done
[s
->tx_fifo_done_len
++] = packetnum
;
207 qemu_send_packet(s
->vc
, p
, len
);
213 /* Add a packet to the TX FIFO. */
214 static void smc91c111_queue_tx(smc91c111_state
*s
, int packet
)
216 if (s
->tx_fifo_len
== NUM_PACKETS
)
218 s
->tx_fifo
[s
->tx_fifo_len
++] = packet
;
222 static void smc91c111_reset(smc91c111_state
*s
)
226 s
->tx_fifo_done_len
= 0;
237 s
->int_level
= INT_TX_EMPTY
;
242 #define SET_LOW(name, val) s->name = (s->name & 0xff00) | val
243 #define SET_HIGH(name, val) s->name = (s->name & 0xff) | (val << 8)
245 static void smc91c111_writeb(void *opaque
, target_phys_addr_t offset
,
248 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
264 SET_HIGH(tcr
, value
);
270 SET_HIGH(rcr
, value
);
271 if (s
->rcr
& RCR_SOFT_RST
)
274 case 10: case 11: /* RPCR */
288 case 2: case 3: /* BASE */
289 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
290 /* Not implemented. */
292 case 10: /* Genral Purpose */
296 SET_HIGH(gpr
, value
);
298 case 12: /* Control */
300 fprintf(stderr
, "smc91c111:EEPROM store not implemented\n");
302 fprintf(stderr
, "smc91c111:EEPROM reload not implemented\n");
307 SET_HIGH(ctr
, value
);
314 case 0: /* MMU Command */
315 switch (value
>> 5) {
318 case 1: /* Allocate for TX. */
320 s
->int_level
&= ~INT_ALLOC
;
322 smc91c111_tx_alloc(s
);
324 case 2: /* Reset MMU. */
327 s
->tx_fifo_done_len
= 0;
331 case 3: /* Remove from RX FIFO. */
332 smc91c111_pop_rx_fifo(s
);
334 case 4: /* Remove from RX FIFO and release. */
335 if (s
->rx_fifo_len
> 0) {
336 smc91c111_release_packet(s
, s
->rx_fifo
[0]);
338 smc91c111_pop_rx_fifo(s
);
340 case 5: /* Release. */
341 smc91c111_release_packet(s
, s
->packet_num
);
343 case 6: /* Add to TX FIFO. */
344 smc91c111_queue_tx(s
, s
->packet_num
);
346 case 7: /* Reset TX FIFO. */
348 s
->tx_fifo_done_len
= 0;
355 case 2: /* Packet Number Register */
356 s
->packet_num
= value
;
358 case 3: case 4: case 5:
359 /* Should be readonly, but linux writes to them anyway. Ignore. */
361 case 6: /* Pointer */
365 SET_HIGH(ptr
, value
);
367 case 8: case 9: case 10: case 11: /* Data */
377 if (s
->ptr
& 0x4000) {
378 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x7ff);
382 s
->data
[n
][p
] = value
;
385 case 12: /* Interrupt ACK. */
386 s
->int_level
&= ~(value
& 0xd6);
388 smc91c111_pop_tx_fifo_done(s
);
391 case 13: /* Interrupt mask. */
400 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
401 /* Multicast table. */
402 /* Not implemented. */
404 case 8: case 9: /* Management Interface. */
405 /* Not implemented. */
407 case 12: /* Early receive. */
408 s
->ercv
= value
& 0x1f;
415 cpu_abort (cpu_single_env
, "smc91c111_write: Bad reg %d:%x\n",
419 static uint32_t smc91c111_readb(void *opaque
, target_phys_addr_t offset
)
421 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 cpu_abort (cpu_single_env
, "smc91c111_read: Bad reg %d:%x\n",
562 static void smc91c111_writew(void *opaque
, target_phys_addr_t offset
,
565 smc91c111_writeb(opaque
, offset
, value
& 0xff);
566 smc91c111_writeb(opaque
, offset
+ 1, value
>> 8);
569 static void smc91c111_writel(void *opaque
, target_phys_addr_t offset
,
572 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
573 /* 32-bit writes to offset 0xc only actually write to the bank select
574 register (offset 0xe) */
575 if (offset
!= s
->base
+ 0xc)
576 smc91c111_writew(opaque
, offset
, value
& 0xffff);
577 smc91c111_writew(opaque
, offset
+ 2, value
>> 16);
580 static uint32_t smc91c111_readw(void *opaque
, target_phys_addr_t offset
)
583 val
= smc91c111_readb(opaque
, offset
);
584 val
|= smc91c111_readb(opaque
, offset
+ 1) << 8;
588 static uint32_t smc91c111_readl(void *opaque
, target_phys_addr_t offset
)
591 val
= smc91c111_readw(opaque
, offset
);
592 val
|= smc91c111_readw(opaque
, offset
+ 2) << 16;
596 static int smc91c111_can_receive(void *opaque
)
598 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
600 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
602 if (s
->allocated
== (1 << NUM_PACKETS
) - 1)
607 static void smc91c111_receive(void *opaque
, const uint8_t *buf
, int size
)
609 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
616 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
618 /* Short packets are padded with zeros. Receiving a packet
619 < 64 bytes long is considered an error condition. */
623 packetsize
= (size
& ~1);
625 crc
= (s
->rcr
& RCR_STRIP_CRC
) == 0;
628 /* TODO: Flag overrun and receive errors. */
629 if (packetsize
> 2048)
631 packetnum
= smc91c111_allocate_packet(s
);
632 if (packetnum
== 0x80)
634 s
->rx_fifo
[s
->rx_fifo_len
++] = packetnum
;
636 p
= &s
->data
[packetnum
][0];
637 /* ??? Multicast packets? */
640 status
|= RS_TOOLONG
;
642 status
|= RS_ODDFRAME
;
643 *(p
++) = status
& 0xff;
644 *(p
++) = status
>> 8;
645 *(p
++) = packetsize
& 0xff;
646 *(p
++) = packetsize
>> 8;
647 memcpy(p
, buf
, size
& ~1);
649 /* Pad short packets. */
654 *(p
++) = buf
[size
- 1];
660 /* It's not clear if the CRC should go before or after the last byte in
661 odd sized packets. Linux disables the CRC, so that's no help.
662 The pictures in the documentation show the CRC aligned on a 16-bit
663 boundary before the last odd byte, so that's what we do. */
665 crc
= crc32(~0, buf
, size
);
666 *(p
++) = crc
& 0xff; crc
>>= 8;
667 *(p
++) = crc
& 0xff; crc
>>= 8;
668 *(p
++) = crc
& 0xff; crc
>>= 8;
669 *(p
++) = crc
& 0xff; crc
>>= 8;
672 *(p
++) = buf
[size
- 1];
678 /* TODO: Raise early RX interrupt? */
679 s
->int_level
|= INT_RCV
;
683 static CPUReadMemoryFunc
*smc91c111_readfn
[] = {
689 static CPUWriteMemoryFunc
*smc91c111_writefn
[] = {
695 void smc91c111_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
700 s
= (smc91c111_state
*)qemu_mallocz(sizeof(smc91c111_state
));
701 iomemtype
= cpu_register_io_memory(0, smc91c111_readfn
,
702 smc91c111_writefn
, s
);
703 cpu_register_physical_memory(base
, 16, iomemtype
);
706 memcpy(s
->macaddr
, nd
->macaddr
, 6);
710 s
->vc
= qemu_new_vlan_client(nd
->vlan
, smc91c111_receive
,
711 smc91c111_can_receive
, s
);
712 /* ??? Save/restore. */