2 * SMSC LAN9118 Ethernet interface emulation
4 * Copyright (c) 2009 CodeSourcery, LLC.
5 * Written by Paul Brook
7 * This code is licenced under the GNU GPL v2
16 //#define DEBUG_LAN9118
19 #define DPRINTF(fmt, ...) \
20 do { printf("lan9118: " fmt , ## __VA_ARGS__); } while (0)
21 #define BADF(fmt, ...) \
22 do { hw_error("lan9118: error: " fmt , ## __VA_ARGS__);} while (0)
24 #define DPRINTF(fmt, ...) do {} while(0)
25 #define BADF(fmt, ...) \
26 do { fprintf(stderr, "lan9118: error: " fmt , ## __VA_ARGS__);} while (0)
29 #define CSR_ID_REV 0x50
30 #define CSR_IRQ_CFG 0x54
31 #define CSR_INT_STS 0x58
32 #define CSR_INT_EN 0x5c
33 #define CSR_BYTE_TEST 0x64
34 #define CSR_FIFO_INT 0x68
35 #define CSR_RX_CFG 0x6c
36 #define CSR_TX_CFG 0x70
37 #define CSR_HW_CFG 0x74
38 #define CSR_RX_DP_CTRL 0x78
39 #define CSR_RX_FIFO_INF 0x7c
40 #define CSR_TX_FIFO_INF 0x80
41 #define CSR_PMT_CTRL 0x84
42 #define CSR_GPIO_CFG 0x88
43 #define CSR_GPT_CFG 0x8c /* TODO */
44 #define CSR_GPT_CNT 0x90 /* TODO */
45 #define CSR_WORD_SWAP 0x98
46 #define CSR_FREE_RUN 0x9c
47 #define CSR_RX_DROP 0xa0
48 #define CSR_MAC_CSR_CMD 0xa4
49 #define CSR_MAC_CSR_DATA 0xa8
50 #define CSR_AFC_CFG 0xac
51 #define CSR_E2P_CMD 0xb0
52 #define CSR_E2P_DATA 0xb4
55 #define IRQ_EN 0x00000100
56 #define IRQ_POL 0x00000010
57 #define IRQ_TYPE 0x00000001
60 #define SW_INT 0x80000000
61 #define TXSTOP_INT 0x02000000
62 #define RXSTOP_INT 0x01000000
63 #define RXDFH_INT 0x00800000
64 #define TX_IOC_INT 0x00200000
65 #define RXD_INT 0x00100000
66 #define GPT_INT 0x00080000
67 #define PHY_INT 0x00040000
68 #define PME_INT 0x00020000
69 #define TXSO_INT 0x00010000
70 #define RWT_INT 0x00008000
71 #define RXE_INT 0x00004000
72 #define TXE_INT 0x00002000
73 #define TDFU_INT 0x00000800
74 #define TDFO_INT 0x00000400
75 #define TDFA_INT 0x00000200
76 #define TSFF_INT 0x00000100
77 #define TSFL_INT 0x00000080
78 #define RXDF_INT 0x00000040
79 #define RDFL_INT 0x00000020
80 #define RSFF_INT 0x00000010
81 #define RSFL_INT 0x00000008
82 #define GPIO2_INT 0x00000004
83 #define GPIO1_INT 0x00000002
84 #define GPIO0_INT 0x00000001
85 #define RESERVED_INT 0x7c001000
93 #define MAC_MII_DATA 7
95 #define MAC_VLAN1 9 /* TODO */
96 #define MAC_VLAN2 10 /* TODO */
97 #define MAC_WUFF 11 /* TODO */
98 #define MAC_WUCSR 12 /* TODO */
100 #define MAC_CR_RXALL 0x80000000
101 #define MAC_CR_RCVOWN 0x00800000
102 #define MAC_CR_LOOPBK 0x00200000
103 #define MAC_CR_FDPX 0x00100000
104 #define MAC_CR_MCPAS 0x00080000
105 #define MAC_CR_PRMS 0x00040000
106 #define MAC_CR_INVFILT 0x00020000
107 #define MAC_CR_PASSBAD 0x00010000
108 #define MAC_CR_HO 0x00008000
109 #define MAC_CR_HPFILT 0x00002000
110 #define MAC_CR_LCOLL 0x00001000
111 #define MAC_CR_BCAST 0x00000800
112 #define MAC_CR_DISRTY 0x00000400
113 #define MAC_CR_PADSTR 0x00000100
114 #define MAC_CR_BOLMT 0x000000c0
115 #define MAC_CR_DFCHK 0x00000020
116 #define MAC_CR_TXEN 0x00000008
117 #define MAC_CR_RXEN 0x00000004
118 #define MAC_CR_RESERVED 0x7f404213
155 uint32_t free_timer_start
;
165 uint32_t mac_mii_acc
;
166 uint32_t mac_mii_data
;
170 uint32_t phy_control
;
171 uint32_t phy_advertise
;
178 LAN9118Packet tx_packet
;
180 int tx_status_fifo_used
;
181 int tx_status_fifo_head
;
182 uint32_t tx_status_fifo
[512];
184 int rx_status_fifo_size
;
185 int rx_status_fifo_used
;
186 int rx_status_fifo_head
;
187 uint32_t rx_status_fifo
[896];
191 uint32_t rx_fifo
[3360];
192 int rx_packet_size_head
;
193 int rx_packet_size_tail
;
194 int rx_packet_size
[1024];
201 static void lan9118_update(lan9118_state
*s
)
205 /* TODO: Implement FIFO level IRQs. */
206 level
= (s
->int_sts
& s
->int_en
) != 0;
207 if ((s
->irq_cfg
& IRQ_EN
) == 0) {
210 qemu_set_irq(s
->irq
, level
);
213 static void lan9118_mac_changed(lan9118_state
*s
)
215 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
218 static void lan9118_reload_eeprom(lan9118_state
*s
)
221 if (s
->eeprom
[0] != 0xa5) {
223 DPRINTF("MACADDR load failed\n");
226 for (i
= 0; i
< 6; i
++) {
227 s
->conf
.macaddr
.a
[i
] = s
->eeprom
[i
+ 1];
230 DPRINTF("MACADDR loaded from eeprom\n");
231 lan9118_mac_changed(s
);
234 static void phy_update_link(lan9118_state
*s
)
236 /* Autonegotiation status mirrors link status. */
237 if (s
->nic
->nc
.link_down
) {
238 s
->phy_status
&= ~0x0024;
240 s
->phy_status
|= 0x0024;
244 static void lan9118_set_link(VLANClientState
*nc
)
246 phy_update_link(DO_UPCAST(NICState
, nc
, nc
)->opaque
);
249 static void phy_reset(lan9118_state
*s
)
251 s
->phy_status
= 0x7805;
252 s
->phy_control
= 0x3000;
253 s
->phy_advertise
= 0x01e1;
257 static void lan9118_reset(DeviceState
*d
)
259 lan9118_state
*s
= FROM_SYSBUS(lan9118_state
, sysbus_from_qdev(d
));
261 s
->irq_cfg
&= ~(IRQ_TYPE
| IRQ_POL
);
264 s
->fifo_int
= 0x48000000;
267 s
->hw_cfg
= 0x00050000;
270 s
->txp
->fifo_used
= 0;
271 s
->txp
->state
= TX_IDLE
;
272 s
->txp
->cmd_a
= 0xffffffffu
;
273 s
->txp
->cmd_b
= 0xffffffffu
;
275 s
->txp
->fifo_used
= 0;
276 s
->tx_fifo_size
= 4608;
277 s
->tx_status_fifo_used
= 0;
278 s
->rx_status_fifo_size
= 704;
279 s
->rx_fifo_size
= 2640;
281 s
->rx_status_fifo_size
= 176;
282 s
->rx_status_fifo_used
= 0;
286 s
->rx_packet_size_tail
= s
->rx_packet_size_head
;
287 s
->rx_packet_size
[s
->rx_packet_size_head
] = 0;
293 s
->free_timer_start
= qemu_get_clock(vm_clock
) / 40;
295 s
->mac_cr
= MAC_CR_PRMS
;
304 s
->eeprom_writable
= 0;
305 lan9118_reload_eeprom(s
);
308 static int lan9118_can_receive(VLANClientState
*nc
)
313 static void rx_fifo_push(lan9118_state
*s
, uint32_t val
)
316 fifo_pos
= s
->rx_fifo_head
+ s
->rx_fifo_used
;
317 if (fifo_pos
>= s
->rx_fifo_size
)
318 fifo_pos
-= s
->rx_fifo_size
;
319 s
->rx_fifo
[fifo_pos
] = val
;
323 /* Return nonzero if the packet is accepted by the filter. */
324 static int lan9118_filter(lan9118_state
*s
, const uint8_t *addr
)
329 if (s
->mac_cr
& MAC_CR_PRMS
) {
332 if (addr
[0] == 0xff && addr
[1] == 0xff && addr
[2] == 0xff &&
333 addr
[3] == 0xff && addr
[4] == 0xff && addr
[5] == 0xff) {
334 return (s
->mac_cr
& MAC_CR_BCAST
) == 0;
337 multicast
= addr
[0] & 1;
338 if (multicast
&&s
->mac_cr
& MAC_CR_MCPAS
) {
341 if (multicast
? (s
->mac_cr
& MAC_CR_HPFILT
) == 0
342 : (s
->mac_cr
& MAC_CR_HO
) == 0) {
343 /* Exact matching. */
344 hash
= memcmp(addr
, s
->conf
.macaddr
.a
, 6);
345 if (s
->mac_cr
& MAC_CR_INVFILT
) {
352 hash
= (crc32(~0, addr
, 6) >> 26);
354 return (s
->mac_hashh
>> (hash
& 0x1f)) & 1;
356 return (s
->mac_hashl
>> (hash
& 0x1f)) & 1;
361 static ssize_t
lan9118_receive(VLANClientState
*nc
, const uint8_t *buf
,
364 lan9118_state
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
374 if ((s
->mac_cr
& MAC_CR_RXEN
) == 0) {
378 if (size
>= 2048 || size
< 14) {
382 /* TODO: Implement FIFO overflow notification. */
383 if (s
->rx_status_fifo_used
== s
->rx_status_fifo_size
) {
387 filter
= lan9118_filter(s
, buf
);
388 if (!filter
&& (s
->mac_cr
& MAC_CR_RXALL
) == 0) {
392 offset
= (s
->rx_cfg
>> 8) & 0x1f;
394 fifo_len
= (size
+ n
+ 3) >> 2;
395 /* Add a word for the CRC. */
397 if (s
->rx_fifo_size
- s
->rx_fifo_used
< fifo_len
) {
401 DPRINTF("Got packet len:%d fifo:%d filter:%s\n",
402 (int)size
, fifo_len
, filter
? "pass" : "fail");
404 crc
= bswap32(crc32(~0, buf
, size
));
405 for (src_pos
= 0; src_pos
< size
; src_pos
++) {
406 val
= (val
>> 8) | ((uint32_t)buf
[src_pos
] << 24);
410 rx_fifo_push(s
, val
);
415 val
>>= ((4 - n
) * 8);
416 val
|= crc
<< (n
* 8);
417 rx_fifo_push(s
, val
);
418 val
= crc
>> ((4 - n
) * 8);
419 rx_fifo_push(s
, val
);
421 rx_fifo_push(s
, crc
);
423 n
= s
->rx_status_fifo_head
+ s
->rx_status_fifo_used
;
424 if (n
>= s
->rx_status_fifo_size
) {
425 n
-= s
->rx_status_fifo_size
;
427 s
->rx_packet_size
[s
->rx_packet_size_tail
] = fifo_len
;
428 s
->rx_packet_size_tail
= (s
->rx_packet_size_tail
+ 1023) & 1023;
429 s
->rx_status_fifo_used
++;
431 status
= (size
+ 4) << 16;
432 if (buf
[0] == 0xff && buf
[1] == 0xff && buf
[2] == 0xff &&
433 buf
[3] == 0xff && buf
[4] == 0xff && buf
[5] == 0xff) {
434 status
|= 0x00002000;
435 } else if (buf
[0] & 1) {
436 status
|= 0x00000400;
439 status
|= 0x40000000;
441 s
->rx_status_fifo
[n
] = status
;
443 if (s
->rx_status_fifo_used
> (s
->fifo_int
& 0xff)) {
444 s
->int_sts
|= RSFL_INT
;
451 static uint32_t rx_fifo_pop(lan9118_state
*s
)
456 if (s
->rxp_size
== 0 && s
->rxp_pad
== 0) {
457 s
->rxp_size
= s
->rx_packet_size
[s
->rx_packet_size_head
];
458 s
->rx_packet_size
[s
->rx_packet_size_head
] = 0;
459 if (s
->rxp_size
!= 0) {
460 s
->rx_packet_size_head
= (s
->rx_packet_size_head
+ 1023) & 1023;
461 s
->rxp_offset
= (s
->rx_cfg
>> 10) & 7;
462 n
= s
->rxp_offset
+ s
->rxp_size
;
463 switch (s
->rx_cfg
>> 30) {
475 DPRINTF("Pop packet size:%d offset:%d pad: %d\n",
476 s
->rxp_size
, s
->rxp_offset
, s
->rxp_pad
);
479 if (s
->rxp_offset
> 0) {
482 } else if (s
->rxp_size
> 0) {
484 val
= s
->rx_fifo
[s
->rx_fifo_head
++];
485 if (s
->rx_fifo_head
>= s
->rx_fifo_size
) {
486 s
->rx_fifo_head
-= s
->rx_fifo_size
;
489 } else if (s
->rxp_pad
> 0) {
493 DPRINTF("RX underflow\n");
494 s
->int_sts
|= RXE_INT
;
501 static void do_tx_packet(lan9118_state
*s
)
506 /* FIXME: Honor TX disable, and allow queueing of packets. */
507 if (s
->phy_control
& 0x4000) {
508 /* This assumes the receive routine doesn't touch the VLANClient. */
509 lan9118_receive(&s
->nic
->nc
, s
->txp
->data
, s
->txp
->len
);
511 qemu_send_packet(&s
->nic
->nc
, s
->txp
->data
, s
->txp
->len
);
513 s
->txp
->fifo_used
= 0;
515 if (s
->tx_status_fifo_used
== 512) {
516 /* Status FIFO full */
519 /* Add entry to status FIFO. */
520 status
= s
->txp
->cmd_b
& 0xffff0000u
;
521 DPRINTF("Sent packet tag:%04x len %d\n", status
>> 16, s
->txp
->len
);
522 n
= (s
->tx_status_fifo_head
+ s
->tx_status_fifo_used
) & 511;
523 s
->tx_status_fifo
[n
] = status
;
524 s
->tx_status_fifo_used
++;
525 if (s
->tx_status_fifo_used
== 512) {
526 s
->int_sts
|= TSFF_INT
;
527 /* TODO: Stop transmission. */
531 static uint32_t rx_status_fifo_pop(lan9118_state
*s
)
535 val
= s
->rx_status_fifo
[s
->rx_status_fifo_head
];
536 if (s
->rx_status_fifo_used
!= 0) {
537 s
->rx_status_fifo_used
--;
538 s
->rx_status_fifo_head
++;
539 if (s
->rx_status_fifo_head
>= s
->rx_status_fifo_size
) {
540 s
->rx_status_fifo_head
-= s
->rx_status_fifo_size
;
542 /* ??? What value should be returned when the FIFO is empty? */
543 DPRINTF("RX status pop 0x%08x\n", val
);
548 static uint32_t tx_status_fifo_pop(lan9118_state
*s
)
552 val
= s
->tx_status_fifo
[s
->tx_status_fifo_head
];
553 if (s
->tx_status_fifo_used
!= 0) {
554 s
->tx_status_fifo_used
--;
555 s
->tx_status_fifo_head
= (s
->tx_status_fifo_head
+ 1) & 511;
556 /* ??? What value should be returned when the FIFO is empty? */
561 static void tx_fifo_push(lan9118_state
*s
, uint32_t val
)
565 if (s
->txp
->fifo_used
== s
->tx_fifo_size
) {
566 s
->int_sts
|= TDFO_INT
;
569 switch (s
->txp
->state
) {
571 s
->txp
->cmd_a
= val
& 0x831f37ff;
573 s
->txp
->state
= TX_B
;
576 if (s
->txp
->cmd_a
& 0x2000) {
580 s
->txp
->buffer_size
= s
->txp
->cmd_a
& 0x7ff;
581 s
->txp
->offset
= (s
->txp
->cmd_a
>> 16) & 0x1f;
582 /* End alignment does not include command words. */
583 n
= (s
->txp
->buffer_size
+ s
->txp
->offset
+ 3) >> 2;
584 switch ((n
>> 24) & 3) {
597 DPRINTF("Block len:%d offset:%d pad:%d cmd %08x\n",
598 s
->txp
->buffer_size
, s
->txp
->offset
, s
->txp
->pad
,
600 s
->txp
->state
= TX_DATA
;
603 if (s
->txp
->offset
>= 4) {
607 if (s
->txp
->buffer_size
<= 0 && s
->txp
->pad
!= 0) {
611 while (s
->txp
->offset
) {
616 /* Documentation is somewhat unclear on the ordering of bytes
617 in FIFO words. Empirical results show it to be little-endian.
619 /* TODO: FIFO overflow checking. */
621 s
->txp
->data
[s
->txp
->len
] = val
& 0xff;
624 s
->txp
->buffer_size
--;
628 if (s
->txp
->buffer_size
<= 0 && s
->txp
->pad
== 0) {
629 if (s
->txp
->cmd_a
& 0x1000) {
632 if (s
->txp
->cmd_a
& 0x80000000) {
633 s
->int_sts
|= TX_IOC_INT
;
635 s
->txp
->state
= TX_IDLE
;
641 static uint32_t do_phy_read(lan9118_state
*s
, int reg
)
644 case 0: /* Basic Control */
645 return s
->phy_control
;
646 case 1: /* Basic Status */
647 return s
->phy_status
;
652 case 4: /* Auto-neg advertisment */
653 return s
->phy_advertise
;
654 case 5: /* Auto-neg Link Partner Ability */
656 case 6: /* Auto-neg Expansion */
658 /* TODO 17, 18, 27, 29, 30, 31 */
660 BADF("PHY read reg %d\n", reg
);
665 static void do_phy_write(lan9118_state
*s
, int reg
, uint32_t val
)
668 case 0: /* Basic Control */
673 s
->phy_control
= val
& 0x7980;
674 /* Complete autonegotiation imediately. */
676 s
->phy_status
|= 0x0020;
679 case 4: /* Auto-neg advertisment */
680 s
->phy_advertise
= (val
& 0x2d7f) | 0x80;
682 /* TODO 17, 18, 27, 29, 30, 31 */
684 BADF("PHY write reg %d = 0x%04x\n", reg
, val
);
688 static void do_mac_write(lan9118_state
*s
, int reg
, uint32_t val
)
692 if ((s
->mac_cr
& MAC_CR_RXEN
) != 0 && (val
& MAC_CR_RXEN
) == 0) {
693 s
->int_sts
|= RXSTOP_INT
;
695 s
->mac_cr
= val
& ~MAC_CR_RESERVED
;
696 DPRINTF("MAC_CR: %08x\n", val
);
699 s
->conf
.macaddr
.a
[4] = val
& 0xff;
700 s
->conf
.macaddr
.a
[5] = (val
>> 8) & 0xff;
701 lan9118_mac_changed(s
);
704 s
->conf
.macaddr
.a
[0] = val
& 0xff;
705 s
->conf
.macaddr
.a
[1] = (val
>> 8) & 0xff;
706 s
->conf
.macaddr
.a
[2] = (val
>> 16) & 0xff;
707 s
->conf
.macaddr
.a
[3] = (val
>> 24) & 0xff;
708 lan9118_mac_changed(s
);
717 s
->mac_mii_acc
= val
& 0xffc2;
719 DPRINTF("PHY write %d = 0x%04x\n",
720 (val
>> 6) & 0x1f, s
->mac_mii_data
);
721 do_phy_write(s
, (val
>> 6) & 0x1f, s
->mac_mii_data
);
723 s
->mac_mii_data
= do_phy_read(s
, (val
>> 6) & 0x1f);
724 DPRINTF("PHY read %d = 0x%04x\n",
725 (val
>> 6) & 0x1f, s
->mac_mii_data
);
729 s
->mac_mii_data
= val
& 0xffff;
732 s
->mac_flow
= val
& 0xffff0000;
735 hw_error("lan9118: Unimplemented MAC register write: %d = 0x%x\n",
736 s
->mac_cmd
& 0xf, val
);
740 static uint32_t do_mac_read(lan9118_state
*s
, int reg
)
746 return s
->conf
.macaddr
.a
[4] | (s
->conf
.macaddr
.a
[5] << 8);
748 return s
->conf
.macaddr
.a
[0] | (s
->conf
.macaddr
.a
[1] << 8)
749 | (s
->conf
.macaddr
.a
[2] << 16) | (s
->conf
.macaddr
.a
[3] << 24);
757 return s
->mac_mii_acc
;
759 return s
->mac_mii_data
;
763 hw_error("lan9118: Unimplemented MAC register read: %d\n",
768 static void lan9118_eeprom_cmd(lan9118_state
*s
, int cmd
, int addr
)
770 s
->e2p_cmd
= (s
->e2p_cmd
& 0x10) | (cmd
<< 28) | addr
;
773 s
->e2p_data
= s
->eeprom
[addr
];
774 DPRINTF("EEPROM Read %d = 0x%02x\n", addr
, s
->e2p_data
);
777 s
->eeprom_writable
= 0;
778 DPRINTF("EEPROM Write Disable\n");
781 s
->eeprom_writable
= 1;
782 DPRINTF("EEPROM Write Enable\n");
785 if (s
->eeprom_writable
) {
786 s
->eeprom
[addr
] &= s
->e2p_data
;
787 DPRINTF("EEPROM Write %d = 0x%02x\n", addr
, s
->e2p_data
);
789 DPRINTF("EEPROM Write %d (ignored)\n", addr
);
793 if (s
->eeprom_writable
) {
794 for (addr
= 0; addr
< 128; addr
++) {
795 s
->eeprom
[addr
] &= s
->e2p_data
;
797 DPRINTF("EEPROM Write All 0x%02x\n", s
->e2p_data
);
799 DPRINTF("EEPROM Write All (ignored)\n");
802 if (s
->eeprom_writable
) {
803 s
->eeprom
[addr
] = 0xff;
804 DPRINTF("EEPROM Erase %d\n", addr
);
806 DPRINTF("EEPROM Erase %d (ignored)\n", addr
);
810 if (s
->eeprom_writable
) {
811 memset(s
->eeprom
, 0xff, 128);
812 DPRINTF("EEPROM Erase All\n");
814 DPRINTF("EEPROM Erase All (ignored)\n");
818 lan9118_reload_eeprom(s
);
823 static void lan9118_writel(void *opaque
, target_phys_addr_t offset
,
826 lan9118_state
*s
= (lan9118_state
*)opaque
;
829 //DPRINTF("Write reg 0x%02x = 0x%08x\n", (int)offset, val);
830 if (offset
>= 0x20 && offset
< 0x40) {
832 tx_fifo_push(s
, val
);
837 /* TODO: Implement interrupt deassertion intervals. */
838 s
->irq_cfg
= (val
& IRQ_EN
);
844 s
->int_en
= val
& ~RESERVED_INT
;
845 s
->int_sts
|= val
& SW_INT
;
848 DPRINTF("FIFO INT levels %08x\n", val
);
855 s
->rx_status_fifo_used
= 0;
856 s
->rx_packet_size_tail
= s
->rx_packet_size_head
;
857 s
->rx_packet_size
[s
->rx_packet_size_head
] = 0;
859 s
->rx_cfg
= val
& 0xcfff1ff0;
863 s
->tx_status_fifo_used
= 0;
866 s
->txp
->state
= TX_IDLE
;
867 s
->txp
->fifo_used
= 0;
868 s
->txp
->cmd_a
= 0xffffffff;
875 lan9118_reset(&s
->busdev
.qdev
);
877 s
->hw_cfg
= val
& 0x003f300;
881 if (val
& 0x80000000) {
882 /* Skip forward to next packet. */
885 if (s
->rxp_size
== 0) {
886 /* Pop a word to start the next packet. */
891 s
->rx_fifo_head
+= s
->rxp_size
;
892 if (s
->rx_fifo_head
>= s
->rx_fifo_size
) {
893 s
->rx_fifo_head
-= s
->rx_fifo_size
;
901 s
->pmt_ctrl
&= ~0x34e;
902 s
->pmt_ctrl
|= (val
& 0x34e);
905 /* Probably just enabling LEDs. */
906 s
->gpio_cfg
= val
& 0x7777071f;
909 /* Ignored because we're in 32-bit mode. */
912 case CSR_MAC_CSR_CMD
:
913 s
->mac_cmd
= val
& 0x4000000f;
914 if (val
& 0x80000000) {
915 if (val
& 0x40000000) {
916 s
->mac_data
= do_mac_read(s
, val
& 0xf);
917 DPRINTF("MAC read %d = 0x%08x\n", val
& 0xf, s
->mac_data
);
919 DPRINTF("MAC write %d = 0x%08x\n", val
& 0xf, s
->mac_data
);
920 do_mac_write(s
, val
& 0xf, s
->mac_data
);
924 case CSR_MAC_CSR_DATA
:
928 s
->afc_cfg
= val
& 0x00ffffff;
931 lan9118_eeprom_cmd(s
, (val
>> 28) & 7, val
& 0xff);
934 s
->e2p_data
= val
& 0xff;
938 hw_error("lan9118_write: Bad reg 0x%x = %x\n", (int)offset
, val
);
944 static uint32_t lan9118_readl(void *opaque
, target_phys_addr_t offset
)
946 lan9118_state
*s
= (lan9118_state
*)opaque
;
948 //DPRINTF("Read reg 0x%02x\n", (int)offset);
951 return rx_fifo_pop(s
);
955 return rx_status_fifo_pop(s
);
957 return s
->rx_status_fifo
[s
->tx_status_fifo_head
];
959 return tx_status_fifo_pop(s
);
961 return s
->tx_status_fifo
[s
->tx_status_fifo_head
];
979 return s
->hw_cfg
| 0x4;
982 case CSR_RX_FIFO_INF
:
983 return (s
->rx_status_fifo_used
<< 16) | (s
->rx_fifo_used
<< 2);
984 case CSR_TX_FIFO_INF
:
985 return (s
->tx_status_fifo_used
<< 16)
986 | (s
->tx_fifo_size
- s
->txp
->fifo_used
);
994 return (qemu_get_clock(vm_clock
) / 40) - s
->free_timer_start
;
996 /* TODO: Implement dropped frames counter. */
998 case CSR_MAC_CSR_CMD
:
1000 case CSR_MAC_CSR_DATA
:
1009 hw_error("lan9118_read: Bad reg 0x%x\n", (int)offset
);
1013 static CPUReadMemoryFunc
* const lan9118_readfn
[] = {
1019 static CPUWriteMemoryFunc
* const lan9118_writefn
[] = {
1025 static void lan9118_cleanup(VLANClientState
*nc
)
1027 lan9118_state
*s
= DO_UPCAST(NICState
, nc
, nc
)->opaque
;
1032 static NetClientInfo net_lan9118_info
= {
1033 .type
= NET_CLIENT_TYPE_NIC
,
1034 .size
= sizeof(NICState
),
1035 .can_receive
= lan9118_can_receive
,
1036 .receive
= lan9118_receive
,
1037 .cleanup
= lan9118_cleanup
,
1038 .link_status_changed
= lan9118_set_link
,
1041 static int lan9118_init1(SysBusDevice
*dev
)
1043 lan9118_state
*s
= FROM_SYSBUS(lan9118_state
, dev
);
1046 s
->mmio_index
= cpu_register_io_memory(lan9118_readfn
,
1047 lan9118_writefn
, s
);
1048 sysbus_init_mmio(dev
, 0x100, s
->mmio_index
);
1049 sysbus_init_irq(dev
, &s
->irq
);
1050 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
1052 s
->nic
= qemu_new_nic(&net_lan9118_info
, &s
->conf
,
1053 dev
->qdev
.info
->name
, dev
->qdev
.id
, s
);
1054 qemu_format_nic_info_str(&s
->nic
->nc
, s
->conf
.macaddr
.a
);
1055 s
->eeprom
[0] = 0xa5;
1056 for (i
= 0; i
< 6; i
++) {
1057 s
->eeprom
[i
+ 1] = s
->conf
.macaddr
.a
[i
];
1060 s
->txp
= &s
->tx_packet
;
1062 /* ??? Save/restore. */
1066 static SysBusDeviceInfo lan9118_info
= {
1067 .init
= lan9118_init1
,
1068 .qdev
.name
= "lan9118",
1069 .qdev
.size
= sizeof(lan9118_state
),
1070 .qdev
.reset
= lan9118_reset
,
1071 .qdev
.props
= (Property
[]) {
1072 DEFINE_NIC_PROPERTIES(lan9118_state
, conf
),
1073 DEFINE_PROP_END_OF_LIST(),
1077 static void lan9118_register_devices(void)
1079 sysbus_register_withprop(&lan9118_info
);
1082 /* Legacy helper function. Should go away when machine config files are
1084 void lan9118_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
1089 qemu_check_nic_model(nd
, "lan9118");
1090 dev
= qdev_create(NULL
, "lan9118");
1091 qdev_set_nic_properties(dev
, nd
);
1092 qdev_init_nofail(dev
);
1093 s
= sysbus_from_qdev(dev
);
1094 sysbus_mmio_map(s
, 0, base
);
1095 sysbus_connect_irq(s
, 0, irq
);
1098 device_init(lan9118_register_devices
)