2 * SMSC LAN9118 Ethernet interface emulation
4 * Copyright (c) 2009 CodeSourcery, LLC.
5 * Written by Paul Brook
7 * This code is licensed under the GNU GPL v2
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
13 #include "qemu/osdep.h"
14 #include "hw/sysbus.h"
15 #include "migration/vmstate.h"
19 #include "hw/net/lan9118.h"
20 #include "hw/ptimer.h"
21 #include "hw/qdev-properties.h"
22 #include "qapi/error.h"
24 #include "qemu/module.h"
27 #include "qom/object.h"
29 //#define DEBUG_LAN9118
32 #define DPRINTF(fmt, ...) \
33 do { printf("lan9118: " fmt , ## __VA_ARGS__); } while (0)
35 #define DPRINTF(fmt, ...) do {} while(0)
38 /* The tx and rx fifo ports are a range of aliased 32-bit registers */
39 #define RX_DATA_FIFO_PORT_FIRST 0x00
40 #define RX_DATA_FIFO_PORT_LAST 0x1f
41 #define TX_DATA_FIFO_PORT_FIRST 0x20
42 #define TX_DATA_FIFO_PORT_LAST 0x3f
44 #define RX_STATUS_FIFO_PORT 0x40
45 #define RX_STATUS_FIFO_PEEK 0x44
46 #define TX_STATUS_FIFO_PORT 0x48
47 #define TX_STATUS_FIFO_PEEK 0x4c
49 #define CSR_ID_REV 0x50
50 #define CSR_IRQ_CFG 0x54
51 #define CSR_INT_STS 0x58
52 #define CSR_INT_EN 0x5c
53 #define CSR_BYTE_TEST 0x64
54 #define CSR_FIFO_INT 0x68
55 #define CSR_RX_CFG 0x6c
56 #define CSR_TX_CFG 0x70
57 #define CSR_HW_CFG 0x74
58 #define CSR_RX_DP_CTRL 0x78
59 #define CSR_RX_FIFO_INF 0x7c
60 #define CSR_TX_FIFO_INF 0x80
61 #define CSR_PMT_CTRL 0x84
62 #define CSR_GPIO_CFG 0x88
63 #define CSR_GPT_CFG 0x8c
64 #define CSR_GPT_CNT 0x90
65 #define CSR_WORD_SWAP 0x98
66 #define CSR_FREE_RUN 0x9c
67 #define CSR_RX_DROP 0xa0
68 #define CSR_MAC_CSR_CMD 0xa4
69 #define CSR_MAC_CSR_DATA 0xa8
70 #define CSR_AFC_CFG 0xac
71 #define CSR_E2P_CMD 0xb0
72 #define CSR_E2P_DATA 0xb4
74 #define E2P_CMD_MAC_ADDR_LOADED 0x100
77 #define IRQ_INT 0x00001000
78 #define IRQ_EN 0x00000100
79 #define IRQ_POL 0x00000010
80 #define IRQ_TYPE 0x00000001
83 #define SW_INT 0x80000000
84 #define TXSTOP_INT 0x02000000
85 #define RXSTOP_INT 0x01000000
86 #define RXDFH_INT 0x00800000
87 #define TX_IOC_INT 0x00200000
88 #define RXD_INT 0x00100000
89 #define GPT_INT 0x00080000
90 #define PHY_INT 0x00040000
91 #define PME_INT 0x00020000
92 #define TXSO_INT 0x00010000
93 #define RWT_INT 0x00008000
94 #define RXE_INT 0x00004000
95 #define TXE_INT 0x00002000
96 #define TDFU_INT 0x00000800
97 #define TDFO_INT 0x00000400
98 #define TDFA_INT 0x00000200
99 #define TSFF_INT 0x00000100
100 #define TSFL_INT 0x00000080
101 #define RXDF_INT 0x00000040
102 #define RDFL_INT 0x00000020
103 #define RSFF_INT 0x00000010
104 #define RSFL_INT 0x00000008
105 #define GPIO2_INT 0x00000004
106 #define GPIO1_INT 0x00000002
107 #define GPIO0_INT 0x00000001
108 #define RESERVED_INT 0x7c001000
115 #define MAC_MII_ACC 6
116 #define MAC_MII_DATA 7
118 #define MAC_VLAN1 9 /* TODO */
119 #define MAC_VLAN2 10 /* TODO */
120 #define MAC_WUFF 11 /* TODO */
121 #define MAC_WUCSR 12 /* TODO */
123 #define MAC_CR_RXALL 0x80000000
124 #define MAC_CR_RCVOWN 0x00800000
125 #define MAC_CR_LOOPBK 0x00200000
126 #define MAC_CR_FDPX 0x00100000
127 #define MAC_CR_MCPAS 0x00080000
128 #define MAC_CR_PRMS 0x00040000
129 #define MAC_CR_INVFILT 0x00020000
130 #define MAC_CR_PASSBAD 0x00010000
131 #define MAC_CR_HO 0x00008000
132 #define MAC_CR_HPFILT 0x00002000
133 #define MAC_CR_LCOLL 0x00001000
134 #define MAC_CR_BCAST 0x00000800
135 #define MAC_CR_DISRTY 0x00000400
136 #define MAC_CR_PADSTR 0x00000100
137 #define MAC_CR_BOLMT 0x000000c0
138 #define MAC_CR_DFCHK 0x00000020
139 #define MAC_CR_TXEN 0x00000008
140 #define MAC_CR_RXEN 0x00000004
141 #define MAC_CR_RESERVED 0x7f404213
143 #define PHY_INT_ENERGYON 0x80
144 #define PHY_INT_AUTONEG_COMPLETE 0x40
145 #define PHY_INT_FAULT 0x20
146 #define PHY_INT_DOWN 0x10
147 #define PHY_INT_AUTONEG_LP 0x08
148 #define PHY_INT_PARFAULT 0x04
149 #define PHY_INT_AUTONEG_PAGE 0x02
151 #define GPT_TIMER_EN 0x20000000
160 /* state is a tx_state but we can't put enums in VMStateDescriptions. */
172 static const VMStateDescription vmstate_lan9118_packet
= {
173 .name
= "lan9118_packet",
175 .minimum_version_id
= 1,
176 .fields
= (VMStateField
[]) {
177 VMSTATE_UINT32(state
, LAN9118Packet
),
178 VMSTATE_UINT32(cmd_a
, LAN9118Packet
),
179 VMSTATE_UINT32(cmd_b
, LAN9118Packet
),
180 VMSTATE_INT32(buffer_size
, LAN9118Packet
),
181 VMSTATE_INT32(offset
, LAN9118Packet
),
182 VMSTATE_INT32(pad
, LAN9118Packet
),
183 VMSTATE_INT32(fifo_used
, LAN9118Packet
),
184 VMSTATE_INT32(len
, LAN9118Packet
),
185 VMSTATE_UINT8_ARRAY(data
, LAN9118Packet
, 2048),
186 VMSTATE_END_OF_LIST()
190 OBJECT_DECLARE_SIMPLE_TYPE(lan9118_state
, LAN9118
)
192 struct lan9118_state
{
193 SysBusDevice parent_obj
;
212 uint32_t free_timer_start
;
222 uint32_t mac_mii_acc
;
223 uint32_t mac_mii_data
;
227 uint32_t phy_control
;
228 uint32_t phy_advertise
;
230 uint32_t phy_int_mask
;
232 int32_t eeprom_writable
;
235 int32_t tx_fifo_size
;
237 LAN9118Packet tx_packet
;
239 int32_t tx_status_fifo_used
;
240 int32_t tx_status_fifo_head
;
241 uint32_t tx_status_fifo
[512];
243 int32_t rx_status_fifo_size
;
244 int32_t rx_status_fifo_used
;
245 int32_t rx_status_fifo_head
;
246 uint32_t rx_status_fifo
[896];
247 int32_t rx_fifo_size
;
248 int32_t rx_fifo_used
;
249 int32_t rx_fifo_head
;
250 uint32_t rx_fifo
[3360];
251 int32_t rx_packet_size_head
;
252 int32_t rx_packet_size_tail
;
253 int32_t rx_packet_size
[1024];
259 uint32_t write_word_prev_offset
;
260 uint32_t write_word_n
;
261 uint16_t write_word_l
;
262 uint16_t write_word_h
;
263 uint32_t read_word_prev_offset
;
264 uint32_t read_word_n
;
270 static const VMStateDescription vmstate_lan9118
= {
273 .minimum_version_id
= 1,
274 .fields
= (VMStateField
[]) {
275 VMSTATE_PTIMER(timer
, lan9118_state
),
276 VMSTATE_UINT32(irq_cfg
, lan9118_state
),
277 VMSTATE_UINT32(int_sts
, lan9118_state
),
278 VMSTATE_UINT32(int_en
, lan9118_state
),
279 VMSTATE_UINT32(fifo_int
, lan9118_state
),
280 VMSTATE_UINT32(rx_cfg
, lan9118_state
),
281 VMSTATE_UINT32(tx_cfg
, lan9118_state
),
282 VMSTATE_UINT32(hw_cfg
, lan9118_state
),
283 VMSTATE_UINT32(pmt_ctrl
, lan9118_state
),
284 VMSTATE_UINT32(gpio_cfg
, lan9118_state
),
285 VMSTATE_UINT32(gpt_cfg
, lan9118_state
),
286 VMSTATE_UINT32(word_swap
, lan9118_state
),
287 VMSTATE_UINT32(free_timer_start
, lan9118_state
),
288 VMSTATE_UINT32(mac_cmd
, lan9118_state
),
289 VMSTATE_UINT32(mac_data
, lan9118_state
),
290 VMSTATE_UINT32(afc_cfg
, lan9118_state
),
291 VMSTATE_UINT32(e2p_cmd
, lan9118_state
),
292 VMSTATE_UINT32(e2p_data
, lan9118_state
),
293 VMSTATE_UINT32(mac_cr
, lan9118_state
),
294 VMSTATE_UINT32(mac_hashh
, lan9118_state
),
295 VMSTATE_UINT32(mac_hashl
, lan9118_state
),
296 VMSTATE_UINT32(mac_mii_acc
, lan9118_state
),
297 VMSTATE_UINT32(mac_mii_data
, lan9118_state
),
298 VMSTATE_UINT32(mac_flow
, lan9118_state
),
299 VMSTATE_UINT32(phy_status
, lan9118_state
),
300 VMSTATE_UINT32(phy_control
, lan9118_state
),
301 VMSTATE_UINT32(phy_advertise
, lan9118_state
),
302 VMSTATE_UINT32(phy_int
, lan9118_state
),
303 VMSTATE_UINT32(phy_int_mask
, lan9118_state
),
304 VMSTATE_INT32(eeprom_writable
, lan9118_state
),
305 VMSTATE_UINT8_ARRAY(eeprom
, lan9118_state
, 128),
306 VMSTATE_INT32(tx_fifo_size
, lan9118_state
),
307 /* txp always points at tx_packet so need not be saved */
308 VMSTATE_STRUCT(tx_packet
, lan9118_state
, 0,
309 vmstate_lan9118_packet
, LAN9118Packet
),
310 VMSTATE_INT32(tx_status_fifo_used
, lan9118_state
),
311 VMSTATE_INT32(tx_status_fifo_head
, lan9118_state
),
312 VMSTATE_UINT32_ARRAY(tx_status_fifo
, lan9118_state
, 512),
313 VMSTATE_INT32(rx_status_fifo_size
, lan9118_state
),
314 VMSTATE_INT32(rx_status_fifo_used
, lan9118_state
),
315 VMSTATE_INT32(rx_status_fifo_head
, lan9118_state
),
316 VMSTATE_UINT32_ARRAY(rx_status_fifo
, lan9118_state
, 896),
317 VMSTATE_INT32(rx_fifo_size
, lan9118_state
),
318 VMSTATE_INT32(rx_fifo_used
, lan9118_state
),
319 VMSTATE_INT32(rx_fifo_head
, lan9118_state
),
320 VMSTATE_UINT32_ARRAY(rx_fifo
, lan9118_state
, 3360),
321 VMSTATE_INT32(rx_packet_size_head
, lan9118_state
),
322 VMSTATE_INT32(rx_packet_size_tail
, lan9118_state
),
323 VMSTATE_INT32_ARRAY(rx_packet_size
, lan9118_state
, 1024),
324 VMSTATE_INT32(rxp_offset
, lan9118_state
),
325 VMSTATE_INT32(rxp_size
, lan9118_state
),
326 VMSTATE_INT32(rxp_pad
, lan9118_state
),
327 VMSTATE_UINT32_V(write_word_prev_offset
, lan9118_state
, 2),
328 VMSTATE_UINT32_V(write_word_n
, lan9118_state
, 2),
329 VMSTATE_UINT16_V(write_word_l
, lan9118_state
, 2),
330 VMSTATE_UINT16_V(write_word_h
, lan9118_state
, 2),
331 VMSTATE_UINT32_V(read_word_prev_offset
, lan9118_state
, 2),
332 VMSTATE_UINT32_V(read_word_n
, lan9118_state
, 2),
333 VMSTATE_UINT32_V(read_long
, lan9118_state
, 2),
334 VMSTATE_UINT32_V(mode_16bit
, lan9118_state
, 2),
335 VMSTATE_END_OF_LIST()
339 static void lan9118_update(lan9118_state
*s
)
343 /* TODO: Implement FIFO level IRQs. */
344 level
= (s
->int_sts
& s
->int_en
) != 0;
346 s
->irq_cfg
|= IRQ_INT
;
348 s
->irq_cfg
&= ~IRQ_INT
;
350 if ((s
->irq_cfg
& IRQ_EN
) == 0) {
353 if ((s
->irq_cfg
& (IRQ_TYPE
| IRQ_POL
)) != (IRQ_TYPE
| IRQ_POL
)) {
354 /* Interrupt is active low unless we're configured as
355 * active-high polarity, push-pull type.
359 qemu_set_irq(s
->irq
, level
);
362 static void lan9118_mac_changed(lan9118_state
*s
)
364 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
367 static void lan9118_reload_eeprom(lan9118_state
*s
)
370 if (s
->eeprom
[0] != 0xa5) {
371 s
->e2p_cmd
&= ~E2P_CMD_MAC_ADDR_LOADED
;
372 DPRINTF("MACADDR load failed\n");
375 for (i
= 0; i
< 6; i
++) {
376 s
->conf
.macaddr
.a
[i
] = s
->eeprom
[i
+ 1];
378 s
->e2p_cmd
|= E2P_CMD_MAC_ADDR_LOADED
;
379 DPRINTF("MACADDR loaded from eeprom\n");
380 lan9118_mac_changed(s
);
383 static void phy_update_irq(lan9118_state
*s
)
385 if (s
->phy_int
& s
->phy_int_mask
) {
386 s
->int_sts
|= PHY_INT
;
388 s
->int_sts
&= ~PHY_INT
;
393 static void phy_update_link(lan9118_state
*s
)
395 /* Autonegotiation status mirrors link status. */
396 if (qemu_get_queue(s
->nic
)->link_down
) {
397 s
->phy_status
&= ~0x0024;
398 s
->phy_int
|= PHY_INT_DOWN
;
400 s
->phy_status
|= 0x0024;
401 s
->phy_int
|= PHY_INT_ENERGYON
;
402 s
->phy_int
|= PHY_INT_AUTONEG_COMPLETE
;
407 static void lan9118_set_link(NetClientState
*nc
)
409 phy_update_link(qemu_get_nic_opaque(nc
));
412 static void phy_reset(lan9118_state
*s
)
414 s
->phy_status
= 0x7809;
415 s
->phy_control
= 0x3000;
416 s
->phy_advertise
= 0x01e1;
422 static void lan9118_reset(DeviceState
*d
)
424 lan9118_state
*s
= LAN9118(d
);
426 s
->irq_cfg
&= (IRQ_TYPE
| IRQ_POL
);
429 s
->fifo_int
= 0x48000000;
432 s
->hw_cfg
= s
->mode_16bit
? 0x00050000 : 0x00050004;
435 s
->txp
->fifo_used
= 0;
436 s
->txp
->state
= TX_IDLE
;
437 s
->txp
->cmd_a
= 0xffffffffu
;
438 s
->txp
->cmd_b
= 0xffffffffu
;
440 s
->txp
->fifo_used
= 0;
441 s
->tx_fifo_size
= 4608;
442 s
->tx_status_fifo_used
= 0;
443 s
->rx_status_fifo_size
= 704;
444 s
->rx_fifo_size
= 2640;
446 s
->rx_status_fifo_size
= 176;
447 s
->rx_status_fifo_used
= 0;
451 s
->rx_packet_size_tail
= s
->rx_packet_size_head
;
452 s
->rx_packet_size
[s
->rx_packet_size_head
] = 0;
458 s
->free_timer_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) / 40;
460 ptimer_transaction_begin(s
->timer
);
461 ptimer_stop(s
->timer
);
462 ptimer_set_count(s
->timer
, 0xffff);
463 ptimer_transaction_commit(s
->timer
);
466 s
->mac_cr
= MAC_CR_PRMS
;
478 s
->eeprom_writable
= 0;
479 lan9118_reload_eeprom(s
);
482 static void rx_fifo_push(lan9118_state
*s
, uint32_t val
)
485 fifo_pos
= s
->rx_fifo_head
+ s
->rx_fifo_used
;
486 if (fifo_pos
>= s
->rx_fifo_size
)
487 fifo_pos
-= s
->rx_fifo_size
;
488 s
->rx_fifo
[fifo_pos
] = val
;
492 /* Return nonzero if the packet is accepted by the filter. */
493 static int lan9118_filter(lan9118_state
*s
, const uint8_t *addr
)
498 if (s
->mac_cr
& MAC_CR_PRMS
) {
501 if (addr
[0] == 0xff && addr
[1] == 0xff && addr
[2] == 0xff &&
502 addr
[3] == 0xff && addr
[4] == 0xff && addr
[5] == 0xff) {
503 return (s
->mac_cr
& MAC_CR_BCAST
) == 0;
506 multicast
= addr
[0] & 1;
507 if (multicast
&&s
->mac_cr
& MAC_CR_MCPAS
) {
510 if (multicast
? (s
->mac_cr
& MAC_CR_HPFILT
) == 0
511 : (s
->mac_cr
& MAC_CR_HO
) == 0) {
512 /* Exact matching. */
513 hash
= memcmp(addr
, s
->conf
.macaddr
.a
, 6);
514 if (s
->mac_cr
& MAC_CR_INVFILT
) {
521 hash
= net_crc32(addr
, ETH_ALEN
) >> 26;
523 return (s
->mac_hashh
>> (hash
& 0x1f)) & 1;
525 return (s
->mac_hashl
>> (hash
& 0x1f)) & 1;
530 static ssize_t
lan9118_receive(NetClientState
*nc
, const uint8_t *buf
,
533 lan9118_state
*s
= qemu_get_nic_opaque(nc
);
543 if ((s
->mac_cr
& MAC_CR_RXEN
) == 0) {
547 if (size
>= 2048 || size
< 14) {
551 /* TODO: Implement FIFO overflow notification. */
552 if (s
->rx_status_fifo_used
== s
->rx_status_fifo_size
) {
556 filter
= lan9118_filter(s
, buf
);
557 if (!filter
&& (s
->mac_cr
& MAC_CR_RXALL
) == 0) {
561 offset
= (s
->rx_cfg
>> 8) & 0x1f;
563 fifo_len
= (size
+ n
+ 3) >> 2;
564 /* Add a word for the CRC. */
566 if (s
->rx_fifo_size
- s
->rx_fifo_used
< fifo_len
) {
570 DPRINTF("Got packet len:%d fifo:%d filter:%s\n",
571 (int)size
, fifo_len
, filter
? "pass" : "fail");
573 crc
= bswap32(crc32(~0, buf
, size
));
574 for (src_pos
= 0; src_pos
< size
; src_pos
++) {
575 val
= (val
>> 8) | ((uint32_t)buf
[src_pos
] << 24);
579 rx_fifo_push(s
, val
);
584 val
>>= ((4 - n
) * 8);
585 val
|= crc
<< (n
* 8);
586 rx_fifo_push(s
, val
);
587 val
= crc
>> ((4 - n
) * 8);
588 rx_fifo_push(s
, val
);
590 rx_fifo_push(s
, crc
);
592 n
= s
->rx_status_fifo_head
+ s
->rx_status_fifo_used
;
593 if (n
>= s
->rx_status_fifo_size
) {
594 n
-= s
->rx_status_fifo_size
;
596 s
->rx_packet_size
[s
->rx_packet_size_tail
] = fifo_len
;
597 s
->rx_packet_size_tail
= (s
->rx_packet_size_tail
+ 1023) & 1023;
598 s
->rx_status_fifo_used
++;
600 status
= (size
+ 4) << 16;
601 if (buf
[0] == 0xff && buf
[1] == 0xff && buf
[2] == 0xff &&
602 buf
[3] == 0xff && buf
[4] == 0xff && buf
[5] == 0xff) {
603 status
|= 0x00002000;
604 } else if (buf
[0] & 1) {
605 status
|= 0x00000400;
608 status
|= 0x40000000;
610 s
->rx_status_fifo
[n
] = status
;
612 if (s
->rx_status_fifo_used
> (s
->fifo_int
& 0xff)) {
613 s
->int_sts
|= RSFL_INT
;
620 static uint32_t rx_fifo_pop(lan9118_state
*s
)
625 if (s
->rxp_size
== 0 && s
->rxp_pad
== 0) {
626 s
->rxp_size
= s
->rx_packet_size
[s
->rx_packet_size_head
];
627 s
->rx_packet_size
[s
->rx_packet_size_head
] = 0;
628 if (s
->rxp_size
!= 0) {
629 s
->rx_packet_size_head
= (s
->rx_packet_size_head
+ 1023) & 1023;
630 s
->rxp_offset
= (s
->rx_cfg
>> 10) & 7;
631 n
= s
->rxp_offset
+ s
->rxp_size
;
632 switch (s
->rx_cfg
>> 30) {
644 DPRINTF("Pop packet size:%d offset:%d pad: %d\n",
645 s
->rxp_size
, s
->rxp_offset
, s
->rxp_pad
);
648 if (s
->rxp_offset
> 0) {
651 } else if (s
->rxp_size
> 0) {
653 val
= s
->rx_fifo
[s
->rx_fifo_head
++];
654 if (s
->rx_fifo_head
>= s
->rx_fifo_size
) {
655 s
->rx_fifo_head
-= s
->rx_fifo_size
;
658 } else if (s
->rxp_pad
> 0) {
662 DPRINTF("RX underflow\n");
663 s
->int_sts
|= RXE_INT
;
670 static void do_tx_packet(lan9118_state
*s
)
675 /* FIXME: Honor TX disable, and allow queueing of packets. */
676 if (s
->phy_control
& 0x4000) {
677 /* This assumes the receive routine doesn't touch the VLANClient. */
678 qemu_receive_packet(qemu_get_queue(s
->nic
), s
->txp
->data
, s
->txp
->len
);
680 qemu_send_packet(qemu_get_queue(s
->nic
), s
->txp
->data
, s
->txp
->len
);
682 s
->txp
->fifo_used
= 0;
684 if (s
->tx_status_fifo_used
== 512) {
685 /* Status FIFO full */
688 /* Add entry to status FIFO. */
689 status
= s
->txp
->cmd_b
& 0xffff0000u
;
690 DPRINTF("Sent packet tag:%04x len %d\n", status
>> 16, s
->txp
->len
);
691 n
= (s
->tx_status_fifo_head
+ s
->tx_status_fifo_used
) & 511;
692 s
->tx_status_fifo
[n
] = status
;
693 s
->tx_status_fifo_used
++;
696 * Generate TSFL interrupt if TX FIFO level exceeds the level
697 * specified in the FIFO_INT TX Status Level field.
699 if (s
->tx_status_fifo_used
> ((s
->fifo_int
>> 16) & 0xff)) {
700 s
->int_sts
|= TSFL_INT
;
702 if (s
->tx_status_fifo_used
== 512) {
703 s
->int_sts
|= TSFF_INT
;
704 /* TODO: Stop transmission. */
708 static uint32_t rx_status_fifo_pop(lan9118_state
*s
)
712 val
= s
->rx_status_fifo
[s
->rx_status_fifo_head
];
713 if (s
->rx_status_fifo_used
!= 0) {
714 s
->rx_status_fifo_used
--;
715 s
->rx_status_fifo_head
++;
716 if (s
->rx_status_fifo_head
>= s
->rx_status_fifo_size
) {
717 s
->rx_status_fifo_head
-= s
->rx_status_fifo_size
;
719 /* ??? What value should be returned when the FIFO is empty? */
720 DPRINTF("RX status pop 0x%08x\n", val
);
725 static uint32_t tx_status_fifo_pop(lan9118_state
*s
)
729 val
= s
->tx_status_fifo
[s
->tx_status_fifo_head
];
730 if (s
->tx_status_fifo_used
!= 0) {
731 s
->tx_status_fifo_used
--;
732 s
->tx_status_fifo_head
= (s
->tx_status_fifo_head
+ 1) & 511;
733 /* ??? What value should be returned when the FIFO is empty? */
738 static void tx_fifo_push(lan9118_state
*s
, uint32_t val
)
742 if (s
->txp
->fifo_used
== s
->tx_fifo_size
) {
743 s
->int_sts
|= TDFO_INT
;
746 switch (s
->txp
->state
) {
748 s
->txp
->cmd_a
= val
& 0x831f37ff;
750 s
->txp
->state
= TX_B
;
751 s
->txp
->buffer_size
= extract32(s
->txp
->cmd_a
, 0, 11);
752 s
->txp
->offset
= extract32(s
->txp
->cmd_a
, 16, 5);
755 if (s
->txp
->cmd_a
& 0x2000) {
759 /* End alignment does not include command words. */
760 n
= (s
->txp
->buffer_size
+ s
->txp
->offset
+ 3) >> 2;
761 switch ((n
>> 24) & 3) {
774 DPRINTF("Block len:%d offset:%d pad:%d cmd %08x\n",
775 s
->txp
->buffer_size
, s
->txp
->offset
, s
->txp
->pad
,
777 s
->txp
->state
= TX_DATA
;
780 if (s
->txp
->offset
>= 4) {
784 if (s
->txp
->buffer_size
<= 0 && s
->txp
->pad
!= 0) {
787 n
= MIN(4, s
->txp
->buffer_size
+ s
->txp
->offset
);
788 while (s
->txp
->offset
) {
793 /* Documentation is somewhat unclear on the ordering of bytes
794 in FIFO words. Empirical results show it to be little-endian.
796 /* TODO: FIFO overflow checking. */
798 s
->txp
->data
[s
->txp
->len
] = val
& 0xff;
801 s
->txp
->buffer_size
--;
805 if (s
->txp
->buffer_size
<= 0 && s
->txp
->pad
== 0) {
806 if (s
->txp
->cmd_a
& 0x1000) {
809 if (s
->txp
->cmd_a
& 0x80000000) {
810 s
->int_sts
|= TX_IOC_INT
;
812 s
->txp
->state
= TX_IDLE
;
818 static uint32_t do_phy_read(lan9118_state
*s
, int reg
)
823 case 0: /* Basic Control */
824 return s
->phy_control
;
825 case 1: /* Basic Status */
826 return s
->phy_status
;
831 case 4: /* Auto-neg advertisement */
832 return s
->phy_advertise
;
833 case 5: /* Auto-neg Link Partner Ability */
835 case 6: /* Auto-neg Expansion */
837 /* TODO 17, 18, 27, 29, 30, 31 */
838 case 29: /* Interrupt source. */
843 case 30: /* Interrupt mask */
844 return s
->phy_int_mask
;
846 qemu_log_mask(LOG_GUEST_ERROR
,
847 "do_phy_read: PHY read reg %d\n", reg
);
852 static void do_phy_write(lan9118_state
*s
, int reg
, uint32_t val
)
855 case 0: /* Basic Control */
860 s
->phy_control
= val
& 0x7980;
861 /* Complete autonegotiation immediately. */
863 s
->phy_status
|= 0x0020;
866 case 4: /* Auto-neg advertisement */
867 s
->phy_advertise
= (val
& 0x2d7f) | 0x80;
869 /* TODO 17, 18, 27, 31 */
870 case 30: /* Interrupt mask */
871 s
->phy_int_mask
= val
& 0xff;
875 qemu_log_mask(LOG_GUEST_ERROR
,
876 "do_phy_write: PHY write reg %d = 0x%04x\n", reg
, val
);
880 static void do_mac_write(lan9118_state
*s
, int reg
, uint32_t val
)
884 if ((s
->mac_cr
& MAC_CR_RXEN
) != 0 && (val
& MAC_CR_RXEN
) == 0) {
885 s
->int_sts
|= RXSTOP_INT
;
887 s
->mac_cr
= val
& ~MAC_CR_RESERVED
;
888 DPRINTF("MAC_CR: %08x\n", val
);
891 s
->conf
.macaddr
.a
[4] = val
& 0xff;
892 s
->conf
.macaddr
.a
[5] = (val
>> 8) & 0xff;
893 lan9118_mac_changed(s
);
896 s
->conf
.macaddr
.a
[0] = val
& 0xff;
897 s
->conf
.macaddr
.a
[1] = (val
>> 8) & 0xff;
898 s
->conf
.macaddr
.a
[2] = (val
>> 16) & 0xff;
899 s
->conf
.macaddr
.a
[3] = (val
>> 24) & 0xff;
900 lan9118_mac_changed(s
);
909 s
->mac_mii_acc
= val
& 0xffc2;
911 DPRINTF("PHY write %d = 0x%04x\n",
912 (val
>> 6) & 0x1f, s
->mac_mii_data
);
913 do_phy_write(s
, (val
>> 6) & 0x1f, s
->mac_mii_data
);
915 s
->mac_mii_data
= do_phy_read(s
, (val
>> 6) & 0x1f);
916 DPRINTF("PHY read %d = 0x%04x\n",
917 (val
>> 6) & 0x1f, s
->mac_mii_data
);
921 s
->mac_mii_data
= val
& 0xffff;
924 s
->mac_flow
= val
& 0xffff0000;
927 /* Writing to this register changes a condition for
928 * FrameTooLong bit in rx_status. Since we do not set
929 * FrameTooLong anyway, just ignore write to this.
933 qemu_log_mask(LOG_GUEST_ERROR
,
934 "lan9118: Unimplemented MAC register write: %d = 0x%x\n",
935 s
->mac_cmd
& 0xf, val
);
939 static uint32_t do_mac_read(lan9118_state
*s
, int reg
)
945 return s
->conf
.macaddr
.a
[4] | (s
->conf
.macaddr
.a
[5] << 8);
947 return s
->conf
.macaddr
.a
[0] | (s
->conf
.macaddr
.a
[1] << 8)
948 | (s
->conf
.macaddr
.a
[2] << 16) | (s
->conf
.macaddr
.a
[3] << 24);
954 return s
->mac_mii_acc
;
956 return s
->mac_mii_data
;
960 qemu_log_mask(LOG_GUEST_ERROR
,
961 "lan9118: Unimplemented MAC register read: %d\n",
967 static void lan9118_eeprom_cmd(lan9118_state
*s
, int cmd
, int addr
)
969 s
->e2p_cmd
= (s
->e2p_cmd
& E2P_CMD_MAC_ADDR_LOADED
) | (cmd
<< 28) | addr
;
972 s
->e2p_data
= s
->eeprom
[addr
];
973 DPRINTF("EEPROM Read %d = 0x%02x\n", addr
, s
->e2p_data
);
976 s
->eeprom_writable
= 0;
977 DPRINTF("EEPROM Write Disable\n");
980 s
->eeprom_writable
= 1;
981 DPRINTF("EEPROM Write Enable\n");
984 if (s
->eeprom_writable
) {
985 s
->eeprom
[addr
] &= s
->e2p_data
;
986 DPRINTF("EEPROM Write %d = 0x%02x\n", addr
, s
->e2p_data
);
988 DPRINTF("EEPROM Write %d (ignored)\n", addr
);
992 if (s
->eeprom_writable
) {
993 for (addr
= 0; addr
< 128; addr
++) {
994 s
->eeprom
[addr
] &= s
->e2p_data
;
996 DPRINTF("EEPROM Write All 0x%02x\n", s
->e2p_data
);
998 DPRINTF("EEPROM Write All (ignored)\n");
1002 if (s
->eeprom_writable
) {
1003 s
->eeprom
[addr
] = 0xff;
1004 DPRINTF("EEPROM Erase %d\n", addr
);
1006 DPRINTF("EEPROM Erase %d (ignored)\n", addr
);
1010 if (s
->eeprom_writable
) {
1011 memset(s
->eeprom
, 0xff, 128);
1012 DPRINTF("EEPROM Erase All\n");
1014 DPRINTF("EEPROM Erase All (ignored)\n");
1017 case 7: /* RELOAD */
1018 lan9118_reload_eeprom(s
);
1023 static void lan9118_tick(void *opaque
)
1025 lan9118_state
*s
= (lan9118_state
*)opaque
;
1026 if (s
->int_en
& GPT_INT
) {
1027 s
->int_sts
|= GPT_INT
;
1032 static void lan9118_writel(void *opaque
, hwaddr offset
,
1033 uint64_t val
, unsigned size
)
1035 lan9118_state
*s
= (lan9118_state
*)opaque
;
1038 //DPRINTF("Write reg 0x%02x = 0x%08x\n", (int)offset, val);
1039 if (offset
>= TX_DATA_FIFO_PORT_FIRST
&&
1040 offset
<= TX_DATA_FIFO_PORT_LAST
) {
1042 tx_fifo_push(s
, val
);
1047 /* TODO: Implement interrupt deassertion intervals. */
1048 val
&= (IRQ_EN
| IRQ_POL
| IRQ_TYPE
);
1049 s
->irq_cfg
= (s
->irq_cfg
& IRQ_INT
) | val
;
1055 s
->int_en
= val
& ~RESERVED_INT
;
1056 s
->int_sts
|= val
& SW_INT
;
1059 DPRINTF("FIFO INT levels %08x\n", val
);
1065 s
->rx_fifo_used
= 0;
1066 s
->rx_status_fifo_used
= 0;
1067 s
->rx_packet_size_tail
= s
->rx_packet_size_head
;
1068 s
->rx_packet_size
[s
->rx_packet_size_head
] = 0;
1070 s
->rx_cfg
= val
& 0xcfff1ff0;
1074 s
->tx_status_fifo_used
= 0;
1077 s
->txp
->state
= TX_IDLE
;
1078 s
->txp
->fifo_used
= 0;
1079 s
->txp
->cmd_a
= 0xffffffff;
1081 s
->tx_cfg
= val
& 6;
1086 lan9118_reset(DEVICE(s
));
1088 s
->hw_cfg
= (val
& 0x003f300) | (s
->hw_cfg
& 0x4);
1091 case CSR_RX_DP_CTRL
:
1092 if (val
& 0x80000000) {
1093 /* Skip forward to next packet. */
1096 if (s
->rxp_size
== 0) {
1097 /* Pop a word to start the next packet. */
1102 s
->rx_fifo_head
+= s
->rxp_size
;
1103 if (s
->rx_fifo_head
>= s
->rx_fifo_size
) {
1104 s
->rx_fifo_head
-= s
->rx_fifo_size
;
1112 s
->pmt_ctrl
&= ~0x34e;
1113 s
->pmt_ctrl
|= (val
& 0x34e);
1116 /* Probably just enabling LEDs. */
1117 s
->gpio_cfg
= val
& 0x7777071f;
1120 if ((s
->gpt_cfg
^ val
) & GPT_TIMER_EN
) {
1121 ptimer_transaction_begin(s
->timer
);
1122 if (val
& GPT_TIMER_EN
) {
1123 ptimer_set_count(s
->timer
, val
& 0xffff);
1124 ptimer_run(s
->timer
, 0);
1126 ptimer_stop(s
->timer
);
1127 ptimer_set_count(s
->timer
, 0xffff);
1129 ptimer_transaction_commit(s
->timer
);
1131 s
->gpt_cfg
= val
& (GPT_TIMER_EN
| 0xffff);
1134 /* Ignored because we're in 32-bit mode. */
1137 case CSR_MAC_CSR_CMD
:
1138 s
->mac_cmd
= val
& 0x4000000f;
1139 if (val
& 0x80000000) {
1140 if (val
& 0x40000000) {
1141 s
->mac_data
= do_mac_read(s
, val
& 0xf);
1142 DPRINTF("MAC read %d = 0x%08x\n", val
& 0xf, s
->mac_data
);
1144 DPRINTF("MAC write %d = 0x%08x\n", val
& 0xf, s
->mac_data
);
1145 do_mac_write(s
, val
& 0xf, s
->mac_data
);
1149 case CSR_MAC_CSR_DATA
:
1153 s
->afc_cfg
= val
& 0x00ffffff;
1156 lan9118_eeprom_cmd(s
, (val
>> 28) & 7, val
& 0x7f);
1159 s
->e2p_data
= val
& 0xff;
1163 qemu_log_mask(LOG_GUEST_ERROR
, "lan9118_write: Bad reg 0x%x = %x\n",
1164 (int)offset
, (int)val
);
1170 static void lan9118_writew(void *opaque
, hwaddr offset
,
1173 lan9118_state
*s
= (lan9118_state
*)opaque
;
1176 if (s
->write_word_prev_offset
!= (offset
& ~0x3)) {
1177 /* New offset, reset word counter */
1178 s
->write_word_n
= 0;
1179 s
->write_word_prev_offset
= offset
& ~0x3;
1183 s
->write_word_h
= val
;
1185 s
->write_word_l
= val
;
1188 //DPRINTF("Writew reg 0x%02x = 0x%08x\n", (int)offset, val);
1190 if (s
->write_word_n
== 2) {
1191 s
->write_word_n
= 0;
1192 lan9118_writel(s
, offset
& ~3, s
->write_word_l
+
1193 (s
->write_word_h
<< 16), 4);
1197 static void lan9118_16bit_mode_write(void *opaque
, hwaddr offset
,
1198 uint64_t val
, unsigned size
)
1202 lan9118_writew(opaque
, offset
, (uint32_t)val
);
1205 lan9118_writel(opaque
, offset
, val
, size
);
1209 qemu_log_mask(LOG_GUEST_ERROR
,
1210 "lan9118_16bit_mode_write: Bad size 0x%x\n", size
);
1213 static uint64_t lan9118_readl(void *opaque
, hwaddr offset
,
1216 lan9118_state
*s
= (lan9118_state
*)opaque
;
1218 //DPRINTF("Read reg 0x%02x\n", (int)offset);
1219 if (offset
<= RX_DATA_FIFO_PORT_LAST
) {
1221 return rx_fifo_pop(s
);
1224 case RX_STATUS_FIFO_PORT
:
1225 return rx_status_fifo_pop(s
);
1226 case RX_STATUS_FIFO_PEEK
:
1227 return s
->rx_status_fifo
[s
->rx_status_fifo_head
];
1228 case TX_STATUS_FIFO_PORT
:
1229 return tx_status_fifo_pop(s
);
1230 case TX_STATUS_FIFO_PEEK
:
1231 return s
->tx_status_fifo
[s
->tx_status_fifo_head
];
1250 case CSR_RX_DP_CTRL
:
1252 case CSR_RX_FIFO_INF
:
1253 return (s
->rx_status_fifo_used
<< 16) | (s
->rx_fifo_used
<< 2);
1254 case CSR_TX_FIFO_INF
:
1255 return (s
->tx_status_fifo_used
<< 16)
1256 | (s
->tx_fifo_size
- s
->txp
->fifo_used
);
1264 return ptimer_get_count(s
->timer
);
1266 return s
->word_swap
;
1268 return (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) / 40) - s
->free_timer_start
;
1270 /* TODO: Implement dropped frames counter. */
1272 case CSR_MAC_CSR_CMD
:
1274 case CSR_MAC_CSR_DATA
:
1283 qemu_log_mask(LOG_GUEST_ERROR
, "lan9118_read: Bad reg 0x%x\n", (int)offset
);
1287 static uint32_t lan9118_readw(void *opaque
, hwaddr offset
)
1289 lan9118_state
*s
= (lan9118_state
*)opaque
;
1292 if (s
->read_word_prev_offset
!= (offset
& ~0x3)) {
1293 /* New offset, reset word counter */
1295 s
->read_word_prev_offset
= offset
& ~0x3;
1299 if (s
->read_word_n
== 1) {
1300 s
->read_long
= lan9118_readl(s
, offset
& ~3, 4);
1306 val
= s
->read_long
>> 16;
1308 val
= s
->read_long
& 0xFFFF;
1311 //DPRINTF("Readw reg 0x%02x, val 0x%x\n", (int)offset, val);
1315 static uint64_t lan9118_16bit_mode_read(void *opaque
, hwaddr offset
,
1320 return lan9118_readw(opaque
, offset
);
1322 return lan9118_readl(opaque
, offset
, size
);
1325 qemu_log_mask(LOG_GUEST_ERROR
,
1326 "lan9118_16bit_mode_read: Bad size 0x%x\n", size
);
1330 static const MemoryRegionOps lan9118_mem_ops
= {
1331 .read
= lan9118_readl
,
1332 .write
= lan9118_writel
,
1333 .endianness
= DEVICE_NATIVE_ENDIAN
,
1336 static const MemoryRegionOps lan9118_16bit_mem_ops
= {
1337 .read
= lan9118_16bit_mode_read
,
1338 .write
= lan9118_16bit_mode_write
,
1339 .endianness
= DEVICE_NATIVE_ENDIAN
,
1342 static NetClientInfo net_lan9118_info
= {
1343 .type
= NET_CLIENT_DRIVER_NIC
,
1344 .size
= sizeof(NICState
),
1345 .receive
= lan9118_receive
,
1346 .link_status_changed
= lan9118_set_link
,
1349 static void lan9118_realize(DeviceState
*dev
, Error
**errp
)
1351 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1352 lan9118_state
*s
= LAN9118(dev
);
1354 const MemoryRegionOps
*mem_ops
=
1355 s
->mode_16bit
? &lan9118_16bit_mem_ops
: &lan9118_mem_ops
;
1357 memory_region_init_io(&s
->mmio
, OBJECT(dev
), mem_ops
, s
,
1358 "lan9118-mmio", 0x100);
1359 sysbus_init_mmio(sbd
, &s
->mmio
);
1360 sysbus_init_irq(sbd
, &s
->irq
);
1361 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
1363 s
->nic
= qemu_new_nic(&net_lan9118_info
, &s
->conf
,
1364 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
1365 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
1366 s
->eeprom
[0] = 0xa5;
1367 for (i
= 0; i
< 6; i
++) {
1368 s
->eeprom
[i
+ 1] = s
->conf
.macaddr
.a
[i
];
1371 s
->txp
= &s
->tx_packet
;
1373 s
->timer
= ptimer_init(lan9118_tick
, s
, PTIMER_POLICY_LEGACY
);
1374 ptimer_transaction_begin(s
->timer
);
1375 ptimer_set_freq(s
->timer
, 10000);
1376 ptimer_set_limit(s
->timer
, 0xffff, 1);
1377 ptimer_transaction_commit(s
->timer
);
1380 static Property lan9118_properties
[] = {
1381 DEFINE_NIC_PROPERTIES(lan9118_state
, conf
),
1382 DEFINE_PROP_UINT32("mode_16bit", lan9118_state
, mode_16bit
, 0),
1383 DEFINE_PROP_END_OF_LIST(),
1386 static void lan9118_class_init(ObjectClass
*klass
, void *data
)
1388 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1390 dc
->reset
= lan9118_reset
;
1391 device_class_set_props(dc
, lan9118_properties
);
1392 dc
->vmsd
= &vmstate_lan9118
;
1393 dc
->realize
= lan9118_realize
;
1396 static const TypeInfo lan9118_info
= {
1397 .name
= TYPE_LAN9118
,
1398 .parent
= TYPE_SYS_BUS_DEVICE
,
1399 .instance_size
= sizeof(lan9118_state
),
1400 .class_init
= lan9118_class_init
,
1403 static void lan9118_register_types(void)
1405 type_register_static(&lan9118_info
);
1408 /* Legacy helper function. Should go away when machine config files are
1410 void lan9118_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
1415 qemu_check_nic_model(nd
, "lan9118");
1416 dev
= qdev_new(TYPE_LAN9118
);
1417 qdev_set_nic_properties(dev
, nd
);
1418 s
= SYS_BUS_DEVICE(dev
);
1419 sysbus_realize_and_unref(s
, &error_fatal
);
1420 sysbus_mmio_map(s
, 0, base
);
1421 sysbus_connect_irq(s
, 0, irq
);
1424 type_init(lan9118_register_types
)