root / hw / net / lan9118.c @ f487b677
History | View | Annotate | Download (39.1 kB)
1 | 2a424990 | Paul Brook | /*
|
---|---|---|---|
2 | 2a424990 | Paul Brook | * SMSC LAN9118 Ethernet interface emulation
|
3 | 2a424990 | Paul Brook | *
|
4 | 2a424990 | Paul Brook | * Copyright (c) 2009 CodeSourcery, LLC.
|
5 | 2a424990 | Paul Brook | * Written by Paul Brook
|
6 | 2a424990 | Paul Brook | *
|
7 | 8e31bf38 | Matthew Fernandez | * This code is licensed under the GNU GPL v2
|
8 | 6b620ca3 | Paolo Bonzini | *
|
9 | 6b620ca3 | Paolo Bonzini | * Contributions after 2012-01-13 are licensed under the terms of the
|
10 | 6b620ca3 | Paolo Bonzini | * GNU GPL, version 2 or (at your option) any later version.
|
11 | 2a424990 | Paul Brook | */
|
12 | 2a424990 | Paul Brook | |
13 | 83c9f4ca | Paolo Bonzini | #include "hw/sysbus.h" |
14 | 1422e32d | Paolo Bonzini | #include "net/net.h" |
15 | bd2be150 | Peter Maydell | #include "hw/devices.h" |
16 | 9c17d615 | Paolo Bonzini | #include "sysemu/sysemu.h" |
17 | 83c9f4ca | Paolo Bonzini | #include "hw/ptimer.h" |
18 | 2a424990 | Paul Brook | /* For crc32 */
|
19 | 2a424990 | Paul Brook | #include <zlib.h> |
20 | 2a424990 | Paul Brook | |
21 | 2a424990 | Paul Brook | //#define DEBUG_LAN9118
|
22 | 2a424990 | Paul Brook | |
23 | 2a424990 | Paul Brook | #ifdef DEBUG_LAN9118
|
24 | 2a424990 | Paul Brook | #define DPRINTF(fmt, ...) \
|
25 | 2a424990 | Paul Brook | do { printf("lan9118: " fmt , ## __VA_ARGS__); } while (0) |
26 | 2a424990 | Paul Brook | #define BADF(fmt, ...) \
|
27 | 2a424990 | Paul Brook | do { hw_error("lan9118: error: " fmt , ## __VA_ARGS__);} while (0) |
28 | 2a424990 | Paul Brook | #else
|
29 | 2a424990 | Paul Brook | #define DPRINTF(fmt, ...) do {} while(0) |
30 | 2a424990 | Paul Brook | #define BADF(fmt, ...) \
|
31 | 2a424990 | Paul Brook | do { fprintf(stderr, "lan9118: error: " fmt , ## __VA_ARGS__);} while (0) |
32 | 2a424990 | Paul Brook | #endif
|
33 | 2a424990 | Paul Brook | |
34 | 2a424990 | Paul Brook | #define CSR_ID_REV 0x50 |
35 | 2a424990 | Paul Brook | #define CSR_IRQ_CFG 0x54 |
36 | 2a424990 | Paul Brook | #define CSR_INT_STS 0x58 |
37 | 2a424990 | Paul Brook | #define CSR_INT_EN 0x5c |
38 | 2a424990 | Paul Brook | #define CSR_BYTE_TEST 0x64 |
39 | 2a424990 | Paul Brook | #define CSR_FIFO_INT 0x68 |
40 | 2a424990 | Paul Brook | #define CSR_RX_CFG 0x6c |
41 | 2a424990 | Paul Brook | #define CSR_TX_CFG 0x70 |
42 | 2a424990 | Paul Brook | #define CSR_HW_CFG 0x74 |
43 | 2a424990 | Paul Brook | #define CSR_RX_DP_CTRL 0x78 |
44 | 2a424990 | Paul Brook | #define CSR_RX_FIFO_INF 0x7c |
45 | 2a424990 | Paul Brook | #define CSR_TX_FIFO_INF 0x80 |
46 | 2a424990 | Paul Brook | #define CSR_PMT_CTRL 0x84 |
47 | 2a424990 | Paul Brook | #define CSR_GPIO_CFG 0x88 |
48 | 209bf965 | Paul Brook | #define CSR_GPT_CFG 0x8c |
49 | 209bf965 | Paul Brook | #define CSR_GPT_CNT 0x90 |
50 | 2a424990 | Paul Brook | #define CSR_WORD_SWAP 0x98 |
51 | 2a424990 | Paul Brook | #define CSR_FREE_RUN 0x9c |
52 | 2a424990 | Paul Brook | #define CSR_RX_DROP 0xa0 |
53 | 2a424990 | Paul Brook | #define CSR_MAC_CSR_CMD 0xa4 |
54 | 2a424990 | Paul Brook | #define CSR_MAC_CSR_DATA 0xa8 |
55 | 2a424990 | Paul Brook | #define CSR_AFC_CFG 0xac |
56 | 2a424990 | Paul Brook | #define CSR_E2P_CMD 0xb0 |
57 | 2a424990 | Paul Brook | #define CSR_E2P_DATA 0xb4 |
58 | 2a424990 | Paul Brook | |
59 | 2a424990 | Paul Brook | /* IRQ_CFG */
|
60 | 209bf965 | Paul Brook | #define IRQ_INT 0x00001000 |
61 | 2a424990 | Paul Brook | #define IRQ_EN 0x00000100 |
62 | 2a424990 | Paul Brook | #define IRQ_POL 0x00000010 |
63 | 2a424990 | Paul Brook | #define IRQ_TYPE 0x00000001 |
64 | 2a424990 | Paul Brook | |
65 | 2a424990 | Paul Brook | /* INT_STS/INT_EN */
|
66 | 2a424990 | Paul Brook | #define SW_INT 0x80000000 |
67 | 2a424990 | Paul Brook | #define TXSTOP_INT 0x02000000 |
68 | 2a424990 | Paul Brook | #define RXSTOP_INT 0x01000000 |
69 | 2a424990 | Paul Brook | #define RXDFH_INT 0x00800000 |
70 | 2a424990 | Paul Brook | #define TX_IOC_INT 0x00200000 |
71 | 2a424990 | Paul Brook | #define RXD_INT 0x00100000 |
72 | 2a424990 | Paul Brook | #define GPT_INT 0x00080000 |
73 | 2a424990 | Paul Brook | #define PHY_INT 0x00040000 |
74 | 2a424990 | Paul Brook | #define PME_INT 0x00020000 |
75 | 2a424990 | Paul Brook | #define TXSO_INT 0x00010000 |
76 | 2a424990 | Paul Brook | #define RWT_INT 0x00008000 |
77 | 2a424990 | Paul Brook | #define RXE_INT 0x00004000 |
78 | 2a424990 | Paul Brook | #define TXE_INT 0x00002000 |
79 | 2a424990 | Paul Brook | #define TDFU_INT 0x00000800 |
80 | 2a424990 | Paul Brook | #define TDFO_INT 0x00000400 |
81 | 2a424990 | Paul Brook | #define TDFA_INT 0x00000200 |
82 | 2a424990 | Paul Brook | #define TSFF_INT 0x00000100 |
83 | 2a424990 | Paul Brook | #define TSFL_INT 0x00000080 |
84 | 2a424990 | Paul Brook | #define RXDF_INT 0x00000040 |
85 | 2a424990 | Paul Brook | #define RDFL_INT 0x00000020 |
86 | 2a424990 | Paul Brook | #define RSFF_INT 0x00000010 |
87 | 2a424990 | Paul Brook | #define RSFL_INT 0x00000008 |
88 | 2a424990 | Paul Brook | #define GPIO2_INT 0x00000004 |
89 | 2a424990 | Paul Brook | #define GPIO1_INT 0x00000002 |
90 | 2a424990 | Paul Brook | #define GPIO0_INT 0x00000001 |
91 | 2a424990 | Paul Brook | #define RESERVED_INT 0x7c001000 |
92 | 2a424990 | Paul Brook | |
93 | 2a424990 | Paul Brook | #define MAC_CR 1 |
94 | 2a424990 | Paul Brook | #define MAC_ADDRH 2 |
95 | 2a424990 | Paul Brook | #define MAC_ADDRL 3 |
96 | 2a424990 | Paul Brook | #define MAC_HASHH 4 |
97 | 2a424990 | Paul Brook | #define MAC_HASHL 5 |
98 | 2a424990 | Paul Brook | #define MAC_MII_ACC 6 |
99 | 2a424990 | Paul Brook | #define MAC_MII_DATA 7 |
100 | 2a424990 | Paul Brook | #define MAC_FLOW 8 |
101 | 2a424990 | Paul Brook | #define MAC_VLAN1 9 /* TODO */ |
102 | 2a424990 | Paul Brook | #define MAC_VLAN2 10 /* TODO */ |
103 | 2a424990 | Paul Brook | #define MAC_WUFF 11 /* TODO */ |
104 | 2a424990 | Paul Brook | #define MAC_WUCSR 12 /* TODO */ |
105 | 2a424990 | Paul Brook | |
106 | 2a424990 | Paul Brook | #define MAC_CR_RXALL 0x80000000 |
107 | 2a424990 | Paul Brook | #define MAC_CR_RCVOWN 0x00800000 |
108 | 2a424990 | Paul Brook | #define MAC_CR_LOOPBK 0x00200000 |
109 | 2a424990 | Paul Brook | #define MAC_CR_FDPX 0x00100000 |
110 | 2a424990 | Paul Brook | #define MAC_CR_MCPAS 0x00080000 |
111 | 2a424990 | Paul Brook | #define MAC_CR_PRMS 0x00040000 |
112 | 2a424990 | Paul Brook | #define MAC_CR_INVFILT 0x00020000 |
113 | 2a424990 | Paul Brook | #define MAC_CR_PASSBAD 0x00010000 |
114 | 2a424990 | Paul Brook | #define MAC_CR_HO 0x00008000 |
115 | 2a424990 | Paul Brook | #define MAC_CR_HPFILT 0x00002000 |
116 | 2a424990 | Paul Brook | #define MAC_CR_LCOLL 0x00001000 |
117 | 2a424990 | Paul Brook | #define MAC_CR_BCAST 0x00000800 |
118 | 2a424990 | Paul Brook | #define MAC_CR_DISRTY 0x00000400 |
119 | 2a424990 | Paul Brook | #define MAC_CR_PADSTR 0x00000100 |
120 | 2a424990 | Paul Brook | #define MAC_CR_BOLMT 0x000000c0 |
121 | 2a424990 | Paul Brook | #define MAC_CR_DFCHK 0x00000020 |
122 | 2a424990 | Paul Brook | #define MAC_CR_TXEN 0x00000008 |
123 | 2a424990 | Paul Brook | #define MAC_CR_RXEN 0x00000004 |
124 | 2a424990 | Paul Brook | #define MAC_CR_RESERVED 0x7f404213 |
125 | 2a424990 | Paul Brook | |
126 | 209bf965 | Paul Brook | #define PHY_INT_ENERGYON 0x80 |
127 | 209bf965 | Paul Brook | #define PHY_INT_AUTONEG_COMPLETE 0x40 |
128 | 209bf965 | Paul Brook | #define PHY_INT_FAULT 0x20 |
129 | 209bf965 | Paul Brook | #define PHY_INT_DOWN 0x10 |
130 | 209bf965 | Paul Brook | #define PHY_INT_AUTONEG_LP 0x08 |
131 | 209bf965 | Paul Brook | #define PHY_INT_PARFAULT 0x04 |
132 | 209bf965 | Paul Brook | #define PHY_INT_AUTONEG_PAGE 0x02 |
133 | 209bf965 | Paul Brook | |
134 | 209bf965 | Paul Brook | #define GPT_TIMER_EN 0x20000000 |
135 | 209bf965 | Paul Brook | |
136 | 2a424990 | Paul Brook | enum tx_state {
|
137 | 2a424990 | Paul Brook | TX_IDLE, |
138 | 2a424990 | Paul Brook | TX_B, |
139 | 2a424990 | Paul Brook | TX_DATA |
140 | 2a424990 | Paul Brook | }; |
141 | 2a424990 | Paul Brook | |
142 | 2a424990 | Paul Brook | typedef struct { |
143 | b09da0c3 | Peter Maydell | /* state is a tx_state but we can't put enums in VMStateDescriptions. */
|
144 | b09da0c3 | Peter Maydell | uint32_t state; |
145 | 2a424990 | Paul Brook | uint32_t cmd_a; |
146 | 2a424990 | Paul Brook | uint32_t cmd_b; |
147 | b09da0c3 | Peter Maydell | int32_t buffer_size; |
148 | b09da0c3 | Peter Maydell | int32_t offset; |
149 | b09da0c3 | Peter Maydell | int32_t pad; |
150 | b09da0c3 | Peter Maydell | int32_t fifo_used; |
151 | b09da0c3 | Peter Maydell | int32_t len; |
152 | 2a424990 | Paul Brook | uint8_t data[2048];
|
153 | 2a424990 | Paul Brook | } LAN9118Packet; |
154 | 2a424990 | Paul Brook | |
155 | b09da0c3 | Peter Maydell | static const VMStateDescription vmstate_lan9118_packet = { |
156 | b09da0c3 | Peter Maydell | .name = "lan9118_packet",
|
157 | b09da0c3 | Peter Maydell | .version_id = 1,
|
158 | b09da0c3 | Peter Maydell | .minimum_version_id = 1,
|
159 | b09da0c3 | Peter Maydell | .fields = (VMStateField[]) { |
160 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(state, LAN9118Packet), |
161 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(cmd_a, LAN9118Packet), |
162 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(cmd_b, LAN9118Packet), |
163 | b09da0c3 | Peter Maydell | VMSTATE_INT32(buffer_size, LAN9118Packet), |
164 | b09da0c3 | Peter Maydell | VMSTATE_INT32(offset, LAN9118Packet), |
165 | b09da0c3 | Peter Maydell | VMSTATE_INT32(pad, LAN9118Packet), |
166 | b09da0c3 | Peter Maydell | VMSTATE_INT32(fifo_used, LAN9118Packet), |
167 | b09da0c3 | Peter Maydell | VMSTATE_INT32(len, LAN9118Packet), |
168 | b09da0c3 | Peter Maydell | VMSTATE_UINT8_ARRAY(data, LAN9118Packet, 2048),
|
169 | b09da0c3 | Peter Maydell | VMSTATE_END_OF_LIST() |
170 | b09da0c3 | Peter Maydell | } |
171 | b09da0c3 | Peter Maydell | }; |
172 | b09da0c3 | Peter Maydell | |
173 | 2a424990 | Paul Brook | typedef struct { |
174 | 2a424990 | Paul Brook | SysBusDevice busdev; |
175 | 83b9f88c | Mark McLoughlin | NICState *nic; |
176 | 2a424990 | Paul Brook | NICConf conf; |
177 | 2a424990 | Paul Brook | qemu_irq irq; |
178 | f0cdd7a9 | Peter Maydell | MemoryRegion mmio; |
179 | 209bf965 | Paul Brook | ptimer_state *timer; |
180 | 2a424990 | Paul Brook | |
181 | 2a424990 | Paul Brook | uint32_t irq_cfg; |
182 | 2a424990 | Paul Brook | uint32_t int_sts; |
183 | 2a424990 | Paul Brook | uint32_t int_en; |
184 | 2a424990 | Paul Brook | uint32_t fifo_int; |
185 | 2a424990 | Paul Brook | uint32_t rx_cfg; |
186 | 2a424990 | Paul Brook | uint32_t tx_cfg; |
187 | 2a424990 | Paul Brook | uint32_t hw_cfg; |
188 | 2a424990 | Paul Brook | uint32_t pmt_ctrl; |
189 | 2a424990 | Paul Brook | uint32_t gpio_cfg; |
190 | 209bf965 | Paul Brook | uint32_t gpt_cfg; |
191 | 2a424990 | Paul Brook | uint32_t word_swap; |
192 | 2a424990 | Paul Brook | uint32_t free_timer_start; |
193 | 2a424990 | Paul Brook | uint32_t mac_cmd; |
194 | 2a424990 | Paul Brook | uint32_t mac_data; |
195 | 2a424990 | Paul Brook | uint32_t afc_cfg; |
196 | 2a424990 | Paul Brook | uint32_t e2p_cmd; |
197 | 2a424990 | Paul Brook | uint32_t e2p_data; |
198 | 2a424990 | Paul Brook | |
199 | 2a424990 | Paul Brook | uint32_t mac_cr; |
200 | 2a424990 | Paul Brook | uint32_t mac_hashh; |
201 | 2a424990 | Paul Brook | uint32_t mac_hashl; |
202 | 2a424990 | Paul Brook | uint32_t mac_mii_acc; |
203 | 2a424990 | Paul Brook | uint32_t mac_mii_data; |
204 | 2a424990 | Paul Brook | uint32_t mac_flow; |
205 | 2a424990 | Paul Brook | |
206 | 2a424990 | Paul Brook | uint32_t phy_status; |
207 | 2a424990 | Paul Brook | uint32_t phy_control; |
208 | 2a424990 | Paul Brook | uint32_t phy_advertise; |
209 | 209bf965 | Paul Brook | uint32_t phy_int; |
210 | 209bf965 | Paul Brook | uint32_t phy_int_mask; |
211 | 2a424990 | Paul Brook | |
212 | b09da0c3 | Peter Maydell | int32_t eeprom_writable; |
213 | c46a3ea0 | Blue Swirl | uint8_t eeprom[128];
|
214 | 2a424990 | Paul Brook | |
215 | b09da0c3 | Peter Maydell | int32_t tx_fifo_size; |
216 | 2a424990 | Paul Brook | LAN9118Packet *txp; |
217 | 2a424990 | Paul Brook | LAN9118Packet tx_packet; |
218 | 2a424990 | Paul Brook | |
219 | b09da0c3 | Peter Maydell | int32_t tx_status_fifo_used; |
220 | b09da0c3 | Peter Maydell | int32_t tx_status_fifo_head; |
221 | 2a424990 | Paul Brook | uint32_t tx_status_fifo[512];
|
222 | 2a424990 | Paul Brook | |
223 | b09da0c3 | Peter Maydell | int32_t rx_status_fifo_size; |
224 | b09da0c3 | Peter Maydell | int32_t rx_status_fifo_used; |
225 | b09da0c3 | Peter Maydell | int32_t rx_status_fifo_head; |
226 | 2a424990 | Paul Brook | uint32_t rx_status_fifo[896];
|
227 | b09da0c3 | Peter Maydell | int32_t rx_fifo_size; |
228 | b09da0c3 | Peter Maydell | int32_t rx_fifo_used; |
229 | b09da0c3 | Peter Maydell | int32_t rx_fifo_head; |
230 | 2a424990 | Paul Brook | uint32_t rx_fifo[3360];
|
231 | b09da0c3 | Peter Maydell | int32_t rx_packet_size_head; |
232 | b09da0c3 | Peter Maydell | int32_t rx_packet_size_tail; |
233 | b09da0c3 | Peter Maydell | int32_t rx_packet_size[1024];
|
234 | 2a424990 | Paul Brook | |
235 | b09da0c3 | Peter Maydell | int32_t rxp_offset; |
236 | b09da0c3 | Peter Maydell | int32_t rxp_size; |
237 | b09da0c3 | Peter Maydell | int32_t rxp_pad; |
238 | 1248f8d4 | Evgeny Voevodin | |
239 | 1248f8d4 | Evgeny Voevodin | uint32_t write_word_prev_offset; |
240 | 1248f8d4 | Evgeny Voevodin | uint32_t write_word_n; |
241 | 1248f8d4 | Evgeny Voevodin | uint16_t write_word_l; |
242 | 1248f8d4 | Evgeny Voevodin | uint16_t write_word_h; |
243 | 1248f8d4 | Evgeny Voevodin | uint32_t read_word_prev_offset; |
244 | 1248f8d4 | Evgeny Voevodin | uint32_t read_word_n; |
245 | 1248f8d4 | Evgeny Voevodin | uint32_t read_long; |
246 | 1248f8d4 | Evgeny Voevodin | |
247 | 1248f8d4 | Evgeny Voevodin | uint32_t mode_16bit; |
248 | 2a424990 | Paul Brook | } lan9118_state; |
249 | 2a424990 | Paul Brook | |
250 | b09da0c3 | Peter Maydell | static const VMStateDescription vmstate_lan9118 = { |
251 | b09da0c3 | Peter Maydell | .name = "lan9118",
|
252 | 1248f8d4 | Evgeny Voevodin | .version_id = 2,
|
253 | b09da0c3 | Peter Maydell | .minimum_version_id = 1,
|
254 | b09da0c3 | Peter Maydell | .fields = (VMStateField[]) { |
255 | b09da0c3 | Peter Maydell | VMSTATE_PTIMER(timer, lan9118_state), |
256 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(irq_cfg, lan9118_state), |
257 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(int_sts, lan9118_state), |
258 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(int_en, lan9118_state), |
259 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(fifo_int, lan9118_state), |
260 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(rx_cfg, lan9118_state), |
261 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(tx_cfg, lan9118_state), |
262 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(hw_cfg, lan9118_state), |
263 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(pmt_ctrl, lan9118_state), |
264 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(gpio_cfg, lan9118_state), |
265 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(gpt_cfg, lan9118_state), |
266 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(word_swap, lan9118_state), |
267 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(free_timer_start, lan9118_state), |
268 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(mac_cmd, lan9118_state), |
269 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(mac_data, lan9118_state), |
270 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(afc_cfg, lan9118_state), |
271 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(e2p_cmd, lan9118_state), |
272 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(e2p_data, lan9118_state), |
273 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(mac_cr, lan9118_state), |
274 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(mac_hashh, lan9118_state), |
275 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(mac_hashl, lan9118_state), |
276 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(mac_mii_acc, lan9118_state), |
277 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(mac_mii_data, lan9118_state), |
278 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(mac_flow, lan9118_state), |
279 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(phy_status, lan9118_state), |
280 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(phy_control, lan9118_state), |
281 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(phy_advertise, lan9118_state), |
282 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(phy_int, lan9118_state), |
283 | b09da0c3 | Peter Maydell | VMSTATE_UINT32(phy_int_mask, lan9118_state), |
284 | b09da0c3 | Peter Maydell | VMSTATE_INT32(eeprom_writable, lan9118_state), |
285 | b09da0c3 | Peter Maydell | VMSTATE_UINT8_ARRAY(eeprom, lan9118_state, 128),
|
286 | b09da0c3 | Peter Maydell | VMSTATE_INT32(tx_fifo_size, lan9118_state), |
287 | b09da0c3 | Peter Maydell | /* txp always points at tx_packet so need not be saved */
|
288 | b09da0c3 | Peter Maydell | VMSTATE_STRUCT(tx_packet, lan9118_state, 0,
|
289 | b09da0c3 | Peter Maydell | vmstate_lan9118_packet, LAN9118Packet), |
290 | b09da0c3 | Peter Maydell | VMSTATE_INT32(tx_status_fifo_used, lan9118_state), |
291 | b09da0c3 | Peter Maydell | VMSTATE_INT32(tx_status_fifo_head, lan9118_state), |
292 | b09da0c3 | Peter Maydell | VMSTATE_UINT32_ARRAY(tx_status_fifo, lan9118_state, 512),
|
293 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rx_status_fifo_size, lan9118_state), |
294 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rx_status_fifo_used, lan9118_state), |
295 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rx_status_fifo_head, lan9118_state), |
296 | b09da0c3 | Peter Maydell | VMSTATE_UINT32_ARRAY(rx_status_fifo, lan9118_state, 896),
|
297 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rx_fifo_size, lan9118_state), |
298 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rx_fifo_used, lan9118_state), |
299 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rx_fifo_head, lan9118_state), |
300 | b09da0c3 | Peter Maydell | VMSTATE_UINT32_ARRAY(rx_fifo, lan9118_state, 3360),
|
301 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rx_packet_size_head, lan9118_state), |
302 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rx_packet_size_tail, lan9118_state), |
303 | b09da0c3 | Peter Maydell | VMSTATE_INT32_ARRAY(rx_packet_size, lan9118_state, 1024),
|
304 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rxp_offset, lan9118_state), |
305 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rxp_size, lan9118_state), |
306 | b09da0c3 | Peter Maydell | VMSTATE_INT32(rxp_pad, lan9118_state), |
307 | 1248f8d4 | Evgeny Voevodin | VMSTATE_UINT32_V(write_word_prev_offset, lan9118_state, 2),
|
308 | 1248f8d4 | Evgeny Voevodin | VMSTATE_UINT32_V(write_word_n, lan9118_state, 2),
|
309 | 1248f8d4 | Evgeny Voevodin | VMSTATE_UINT16_V(write_word_l, lan9118_state, 2),
|
310 | 1248f8d4 | Evgeny Voevodin | VMSTATE_UINT16_V(write_word_h, lan9118_state, 2),
|
311 | 1248f8d4 | Evgeny Voevodin | VMSTATE_UINT32_V(read_word_prev_offset, lan9118_state, 2),
|
312 | 1248f8d4 | Evgeny Voevodin | VMSTATE_UINT32_V(read_word_n, lan9118_state, 2),
|
313 | 1248f8d4 | Evgeny Voevodin | VMSTATE_UINT32_V(read_long, lan9118_state, 2),
|
314 | 1248f8d4 | Evgeny Voevodin | VMSTATE_UINT32_V(mode_16bit, lan9118_state, 2),
|
315 | b09da0c3 | Peter Maydell | VMSTATE_END_OF_LIST() |
316 | b09da0c3 | Peter Maydell | } |
317 | b09da0c3 | Peter Maydell | }; |
318 | b09da0c3 | Peter Maydell | |
319 | 2a424990 | Paul Brook | static void lan9118_update(lan9118_state *s) |
320 | 2a424990 | Paul Brook | { |
321 | 2a424990 | Paul Brook | int level;
|
322 | 2a424990 | Paul Brook | |
323 | 2a424990 | Paul Brook | /* TODO: Implement FIFO level IRQs. */
|
324 | 2a424990 | Paul Brook | level = (s->int_sts & s->int_en) != 0;
|
325 | 209bf965 | Paul Brook | if (level) {
|
326 | 209bf965 | Paul Brook | s->irq_cfg |= IRQ_INT; |
327 | 209bf965 | Paul Brook | } else {
|
328 | 209bf965 | Paul Brook | s->irq_cfg &= ~IRQ_INT; |
329 | 209bf965 | Paul Brook | } |
330 | 2a424990 | Paul Brook | if ((s->irq_cfg & IRQ_EN) == 0) { |
331 | 2a424990 | Paul Brook | level = 0;
|
332 | 2a424990 | Paul Brook | } |
333 | eb47d7c5 | Peter Maydell | if ((s->irq_cfg & (IRQ_TYPE | IRQ_POL)) != (IRQ_TYPE | IRQ_POL)) {
|
334 | eb47d7c5 | Peter Maydell | /* Interrupt is active low unless we're configured as
|
335 | eb47d7c5 | Peter Maydell | * active-high polarity, push-pull type.
|
336 | eb47d7c5 | Peter Maydell | */
|
337 | eb47d7c5 | Peter Maydell | level = !level; |
338 | eb47d7c5 | Peter Maydell | } |
339 | 2a424990 | Paul Brook | qemu_set_irq(s->irq, level); |
340 | 2a424990 | Paul Brook | } |
341 | 2a424990 | Paul Brook | |
342 | 2a424990 | Paul Brook | static void lan9118_mac_changed(lan9118_state *s) |
343 | 2a424990 | Paul Brook | { |
344 | b356f76d | Jason Wang | qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a); |
345 | 2a424990 | Paul Brook | } |
346 | 2a424990 | Paul Brook | |
347 | 2a424990 | Paul Brook | static void lan9118_reload_eeprom(lan9118_state *s) |
348 | 2a424990 | Paul Brook | { |
349 | 2a424990 | Paul Brook | int i;
|
350 | 2a424990 | Paul Brook | if (s->eeprom[0] != 0xa5) { |
351 | 2a424990 | Paul Brook | s->e2p_cmd &= ~0x10;
|
352 | 2a424990 | Paul Brook | DPRINTF("MACADDR load failed\n");
|
353 | 2a424990 | Paul Brook | return;
|
354 | 2a424990 | Paul Brook | } |
355 | 2a424990 | Paul Brook | for (i = 0; i < 6; i++) { |
356 | 2a424990 | Paul Brook | s->conf.macaddr.a[i] = s->eeprom[i + 1];
|
357 | 2a424990 | Paul Brook | } |
358 | 2a424990 | Paul Brook | s->e2p_cmd |= 0x10;
|
359 | 2a424990 | Paul Brook | DPRINTF("MACADDR loaded from eeprom\n");
|
360 | 2a424990 | Paul Brook | lan9118_mac_changed(s); |
361 | 2a424990 | Paul Brook | } |
362 | 2a424990 | Paul Brook | |
363 | 209bf965 | Paul Brook | static void phy_update_irq(lan9118_state *s) |
364 | 209bf965 | Paul Brook | { |
365 | 209bf965 | Paul Brook | if (s->phy_int & s->phy_int_mask) {
|
366 | 209bf965 | Paul Brook | s->int_sts |= PHY_INT; |
367 | 209bf965 | Paul Brook | } else {
|
368 | 209bf965 | Paul Brook | s->int_sts &= ~PHY_INT; |
369 | 209bf965 | Paul Brook | } |
370 | 209bf965 | Paul Brook | lan9118_update(s); |
371 | 209bf965 | Paul Brook | } |
372 | 209bf965 | Paul Brook | |
373 | 2a424990 | Paul Brook | static void phy_update_link(lan9118_state *s) |
374 | 2a424990 | Paul Brook | { |
375 | 2a424990 | Paul Brook | /* Autonegotiation status mirrors link status. */
|
376 | b356f76d | Jason Wang | if (qemu_get_queue(s->nic)->link_down) {
|
377 | 2a424990 | Paul Brook | s->phy_status &= ~0x0024;
|
378 | 209bf965 | Paul Brook | s->phy_int |= PHY_INT_DOWN; |
379 | 2a424990 | Paul Brook | } else {
|
380 | 2a424990 | Paul Brook | s->phy_status |= 0x0024;
|
381 | 209bf965 | Paul Brook | s->phy_int |= PHY_INT_ENERGYON; |
382 | 209bf965 | Paul Brook | s->phy_int |= PHY_INT_AUTONEG_COMPLETE; |
383 | 2a424990 | Paul Brook | } |
384 | 209bf965 | Paul Brook | phy_update_irq(s); |
385 | 2a424990 | Paul Brook | } |
386 | 2a424990 | Paul Brook | |
387 | 4e68f7a0 | Stefan Hajnoczi | static void lan9118_set_link(NetClientState *nc) |
388 | 2a424990 | Paul Brook | { |
389 | cc1f0f45 | Jason Wang | phy_update_link(qemu_get_nic_opaque(nc)); |
390 | 2a424990 | Paul Brook | } |
391 | 2a424990 | Paul Brook | |
392 | 2a424990 | Paul Brook | static void phy_reset(lan9118_state *s) |
393 | 2a424990 | Paul Brook | { |
394 | 209bf965 | Paul Brook | s->phy_status = 0x7809;
|
395 | 2a424990 | Paul Brook | s->phy_control = 0x3000;
|
396 | 2a424990 | Paul Brook | s->phy_advertise = 0x01e1;
|
397 | 209bf965 | Paul Brook | s->phy_int_mask = 0;
|
398 | 209bf965 | Paul Brook | s->phy_int = 0;
|
399 | 2a424990 | Paul Brook | phy_update_link(s); |
400 | 2a424990 | Paul Brook | } |
401 | 2a424990 | Paul Brook | |
402 | 2a424990 | Paul Brook | static void lan9118_reset(DeviceState *d) |
403 | 2a424990 | Paul Brook | { |
404 | 1356b98d | Andreas Färber | lan9118_state *s = FROM_SYSBUS(lan9118_state, SYS_BUS_DEVICE(d)); |
405 | eb47d7c5 | Peter Maydell | s->irq_cfg &= (IRQ_TYPE | IRQ_POL); |
406 | 2a424990 | Paul Brook | s->int_sts = 0;
|
407 | 2a424990 | Paul Brook | s->int_en = 0;
|
408 | 2a424990 | Paul Brook | s->fifo_int = 0x48000000;
|
409 | 2a424990 | Paul Brook | s->rx_cfg = 0;
|
410 | 2a424990 | Paul Brook | s->tx_cfg = 0;
|
411 | 1248f8d4 | Evgeny Voevodin | s->hw_cfg = s->mode_16bit ? 0x00050000 : 0x00050004; |
412 | 2a424990 | Paul Brook | s->pmt_ctrl &= 0x45;
|
413 | 2a424990 | Paul Brook | s->gpio_cfg = 0;
|
414 | 2a424990 | Paul Brook | s->txp->fifo_used = 0;
|
415 | 2a424990 | Paul Brook | s->txp->state = TX_IDLE; |
416 | 2a424990 | Paul Brook | s->txp->cmd_a = 0xffffffffu;
|
417 | 2a424990 | Paul Brook | s->txp->cmd_b = 0xffffffffu;
|
418 | 2a424990 | Paul Brook | s->txp->len = 0;
|
419 | 2a424990 | Paul Brook | s->txp->fifo_used = 0;
|
420 | 2a424990 | Paul Brook | s->tx_fifo_size = 4608;
|
421 | 2a424990 | Paul Brook | s->tx_status_fifo_used = 0;
|
422 | 2a424990 | Paul Brook | s->rx_status_fifo_size = 704;
|
423 | 2a424990 | Paul Brook | s->rx_fifo_size = 2640;
|
424 | 2a424990 | Paul Brook | s->rx_fifo_used = 0;
|
425 | 2a424990 | Paul Brook | s->rx_status_fifo_size = 176;
|
426 | 2a424990 | Paul Brook | s->rx_status_fifo_used = 0;
|
427 | 2a424990 | Paul Brook | s->rxp_offset = 0;
|
428 | 2a424990 | Paul Brook | s->rxp_size = 0;
|
429 | 2a424990 | Paul Brook | s->rxp_pad = 0;
|
430 | 2a424990 | Paul Brook | s->rx_packet_size_tail = s->rx_packet_size_head; |
431 | 2a424990 | Paul Brook | s->rx_packet_size[s->rx_packet_size_head] = 0;
|
432 | 2a424990 | Paul Brook | s->mac_cmd = 0;
|
433 | 2a424990 | Paul Brook | s->mac_data = 0;
|
434 | 2a424990 | Paul Brook | s->afc_cfg = 0;
|
435 | 2a424990 | Paul Brook | s->e2p_cmd = 0;
|
436 | 2a424990 | Paul Brook | s->e2p_data = 0;
|
437 | 74475455 | Paolo Bonzini | s->free_timer_start = qemu_get_clock_ns(vm_clock) / 40;
|
438 | 2a424990 | Paul Brook | |
439 | 209bf965 | Paul Brook | ptimer_stop(s->timer); |
440 | 209bf965 | Paul Brook | ptimer_set_count(s->timer, 0xffff);
|
441 | 209bf965 | Paul Brook | s->gpt_cfg = 0xffff;
|
442 | 209bf965 | Paul Brook | |
443 | 2a424990 | Paul Brook | s->mac_cr = MAC_CR_PRMS; |
444 | 2a424990 | Paul Brook | s->mac_hashh = 0;
|
445 | 2a424990 | Paul Brook | s->mac_hashl = 0;
|
446 | 2a424990 | Paul Brook | s->mac_mii_acc = 0;
|
447 | 2a424990 | Paul Brook | s->mac_mii_data = 0;
|
448 | 2a424990 | Paul Brook | s->mac_flow = 0;
|
449 | 2a424990 | Paul Brook | |
450 | 1248f8d4 | Evgeny Voevodin | s->read_word_n = 0;
|
451 | 1248f8d4 | Evgeny Voevodin | s->write_word_n = 0;
|
452 | 1248f8d4 | Evgeny Voevodin | |
453 | 2a424990 | Paul Brook | phy_reset(s); |
454 | 2a424990 | Paul Brook | |
455 | 2a424990 | Paul Brook | s->eeprom_writable = 0;
|
456 | 2a424990 | Paul Brook | lan9118_reload_eeprom(s); |
457 | 2a424990 | Paul Brook | } |
458 | 2a424990 | Paul Brook | |
459 | 4e68f7a0 | Stefan Hajnoczi | static int lan9118_can_receive(NetClientState *nc) |
460 | 2a424990 | Paul Brook | { |
461 | 2a424990 | Paul Brook | return 1; |
462 | 2a424990 | Paul Brook | } |
463 | 2a424990 | Paul Brook | |
464 | 2a424990 | Paul Brook | static void rx_fifo_push(lan9118_state *s, uint32_t val) |
465 | 2a424990 | Paul Brook | { |
466 | 2a424990 | Paul Brook | int fifo_pos;
|
467 | 2a424990 | Paul Brook | fifo_pos = s->rx_fifo_head + s->rx_fifo_used; |
468 | 2a424990 | Paul Brook | if (fifo_pos >= s->rx_fifo_size)
|
469 | 2a424990 | Paul Brook | fifo_pos -= s->rx_fifo_size; |
470 | 2a424990 | Paul Brook | s->rx_fifo[fifo_pos] = val; |
471 | 2a424990 | Paul Brook | s->rx_fifo_used++; |
472 | 2a424990 | Paul Brook | } |
473 | 2a424990 | Paul Brook | |
474 | 2a424990 | Paul Brook | /* Return nonzero if the packet is accepted by the filter. */
|
475 | 2a424990 | Paul Brook | static int lan9118_filter(lan9118_state *s, const uint8_t *addr) |
476 | 2a424990 | Paul Brook | { |
477 | 2a424990 | Paul Brook | int multicast;
|
478 | 2a424990 | Paul Brook | uint32_t hash; |
479 | 2a424990 | Paul Brook | |
480 | 2a424990 | Paul Brook | if (s->mac_cr & MAC_CR_PRMS) {
|
481 | 2a424990 | Paul Brook | return 1; |
482 | 2a424990 | Paul Brook | } |
483 | 2a424990 | Paul Brook | if (addr[0] == 0xff && addr[1] == 0xff && addr[2] == 0xff && |
484 | 2a424990 | Paul Brook | addr[3] == 0xff && addr[4] == 0xff && addr[5] == 0xff) { |
485 | 2a424990 | Paul Brook | return (s->mac_cr & MAC_CR_BCAST) == 0; |
486 | 2a424990 | Paul Brook | } |
487 | 2a424990 | Paul Brook | |
488 | 2a424990 | Paul Brook | multicast = addr[0] & 1; |
489 | 2a424990 | Paul Brook | if (multicast &&s->mac_cr & MAC_CR_MCPAS) {
|
490 | 2a424990 | Paul Brook | return 1; |
491 | 2a424990 | Paul Brook | } |
492 | 2a424990 | Paul Brook | if (multicast ? (s->mac_cr & MAC_CR_HPFILT) == 0 |
493 | 2a424990 | Paul Brook | : (s->mac_cr & MAC_CR_HO) == 0) {
|
494 | 2a424990 | Paul Brook | /* Exact matching. */
|
495 | 2a424990 | Paul Brook | hash = memcmp(addr, s->conf.macaddr.a, 6);
|
496 | 2a424990 | Paul Brook | if (s->mac_cr & MAC_CR_INVFILT) {
|
497 | 2a424990 | Paul Brook | return hash != 0; |
498 | 2a424990 | Paul Brook | } else {
|
499 | 2a424990 | Paul Brook | return hash == 0; |
500 | 2a424990 | Paul Brook | } |
501 | 2a424990 | Paul Brook | } else {
|
502 | 2a424990 | Paul Brook | /* Hash matching */
|
503 | 449bc90e | Aurelien Jarno | hash = compute_mcast_idx(addr); |
504 | 2a424990 | Paul Brook | if (hash & 0x20) { |
505 | 2a424990 | Paul Brook | return (s->mac_hashh >> (hash & 0x1f)) & 1; |
506 | 2a424990 | Paul Brook | } else {
|
507 | 2a424990 | Paul Brook | return (s->mac_hashl >> (hash & 0x1f)) & 1; |
508 | 2a424990 | Paul Brook | } |
509 | 2a424990 | Paul Brook | } |
510 | 2a424990 | Paul Brook | } |
511 | 2a424990 | Paul Brook | |
512 | 4e68f7a0 | Stefan Hajnoczi | static ssize_t lan9118_receive(NetClientState *nc, const uint8_t *buf, |
513 | 2a424990 | Paul Brook | size_t size) |
514 | 2a424990 | Paul Brook | { |
515 | cc1f0f45 | Jason Wang | lan9118_state *s = qemu_get_nic_opaque(nc); |
516 | 2a424990 | Paul Brook | int fifo_len;
|
517 | 2a424990 | Paul Brook | int offset;
|
518 | 2a424990 | Paul Brook | int src_pos;
|
519 | 2a424990 | Paul Brook | int n;
|
520 | 2a424990 | Paul Brook | int filter;
|
521 | 2a424990 | Paul Brook | uint32_t val; |
522 | 2a424990 | Paul Brook | uint32_t crc; |
523 | 2a424990 | Paul Brook | uint32_t status; |
524 | 2a424990 | Paul Brook | |
525 | 2a424990 | Paul Brook | if ((s->mac_cr & MAC_CR_RXEN) == 0) { |
526 | 2a424990 | Paul Brook | return -1; |
527 | 2a424990 | Paul Brook | } |
528 | 2a424990 | Paul Brook | |
529 | 2a424990 | Paul Brook | if (size >= 2048 || size < 14) { |
530 | 2a424990 | Paul Brook | return -1; |
531 | 2a424990 | Paul Brook | } |
532 | 2a424990 | Paul Brook | |
533 | 2a424990 | Paul Brook | /* TODO: Implement FIFO overflow notification. */
|
534 | 2a424990 | Paul Brook | if (s->rx_status_fifo_used == s->rx_status_fifo_size) {
|
535 | 2a424990 | Paul Brook | return -1; |
536 | 2a424990 | Paul Brook | } |
537 | 2a424990 | Paul Brook | |
538 | 2a424990 | Paul Brook | filter = lan9118_filter(s, buf); |
539 | 2a424990 | Paul Brook | if (!filter && (s->mac_cr & MAC_CR_RXALL) == 0) { |
540 | 2a424990 | Paul Brook | return size;
|
541 | 2a424990 | Paul Brook | } |
542 | 2a424990 | Paul Brook | |
543 | 2a424990 | Paul Brook | offset = (s->rx_cfg >> 8) & 0x1f; |
544 | 2a424990 | Paul Brook | n = offset & 3;
|
545 | 2a424990 | Paul Brook | fifo_len = (size + n + 3) >> 2; |
546 | 2a424990 | Paul Brook | /* Add a word for the CRC. */
|
547 | 2a424990 | Paul Brook | fifo_len++; |
548 | 2a424990 | Paul Brook | if (s->rx_fifo_size - s->rx_fifo_used < fifo_len) {
|
549 | 2a424990 | Paul Brook | return -1; |
550 | 2a424990 | Paul Brook | } |
551 | 2a424990 | Paul Brook | |
552 | 2a424990 | Paul Brook | DPRINTF("Got packet len:%d fifo:%d filter:%s\n",
|
553 | 2a424990 | Paul Brook | (int)size, fifo_len, filter ? "pass" : "fail"); |
554 | 2a424990 | Paul Brook | val = 0;
|
555 | 2a424990 | Paul Brook | crc = bswap32(crc32(~0, buf, size));
|
556 | 2a424990 | Paul Brook | for (src_pos = 0; src_pos < size; src_pos++) { |
557 | 2a424990 | Paul Brook | val = (val >> 8) | ((uint32_t)buf[src_pos] << 24); |
558 | 2a424990 | Paul Brook | n++; |
559 | 2a424990 | Paul Brook | if (n == 4) { |
560 | 2a424990 | Paul Brook | n = 0;
|
561 | 2a424990 | Paul Brook | rx_fifo_push(s, val); |
562 | 2a424990 | Paul Brook | val = 0;
|
563 | 2a424990 | Paul Brook | } |
564 | 2a424990 | Paul Brook | } |
565 | 2a424990 | Paul Brook | if (n) {
|
566 | 2a424990 | Paul Brook | val >>= ((4 - n) * 8); |
567 | 2a424990 | Paul Brook | val |= crc << (n * 8);
|
568 | 2a424990 | Paul Brook | rx_fifo_push(s, val); |
569 | 2a424990 | Paul Brook | val = crc >> ((4 - n) * 8); |
570 | 2a424990 | Paul Brook | rx_fifo_push(s, val); |
571 | 2a424990 | Paul Brook | } else {
|
572 | 2a424990 | Paul Brook | rx_fifo_push(s, crc); |
573 | 2a424990 | Paul Brook | } |
574 | 2a424990 | Paul Brook | n = s->rx_status_fifo_head + s->rx_status_fifo_used; |
575 | 2a424990 | Paul Brook | if (n >= s->rx_status_fifo_size) {
|
576 | 2a424990 | Paul Brook | n -= s->rx_status_fifo_size; |
577 | 2a424990 | Paul Brook | } |
578 | 2a424990 | Paul Brook | s->rx_packet_size[s->rx_packet_size_tail] = fifo_len; |
579 | 2a424990 | Paul Brook | s->rx_packet_size_tail = (s->rx_packet_size_tail + 1023) & 1023; |
580 | 2a424990 | Paul Brook | s->rx_status_fifo_used++; |
581 | 2a424990 | Paul Brook | |
582 | 2a424990 | Paul Brook | status = (size + 4) << 16; |
583 | 2a424990 | Paul Brook | if (buf[0] == 0xff && buf[1] == 0xff && buf[2] == 0xff && |
584 | 2a424990 | Paul Brook | buf[3] == 0xff && buf[4] == 0xff && buf[5] == 0xff) { |
585 | 2a424990 | Paul Brook | status |= 0x00002000;
|
586 | 2a424990 | Paul Brook | } else if (buf[0] & 1) { |
587 | 2a424990 | Paul Brook | status |= 0x00000400;
|
588 | 2a424990 | Paul Brook | } |
589 | 2a424990 | Paul Brook | if (!filter) {
|
590 | 2a424990 | Paul Brook | status |= 0x40000000;
|
591 | 2a424990 | Paul Brook | } |
592 | 2a424990 | Paul Brook | s->rx_status_fifo[n] = status; |
593 | 2a424990 | Paul Brook | |
594 | 2a424990 | Paul Brook | if (s->rx_status_fifo_used > (s->fifo_int & 0xff)) { |
595 | 2a424990 | Paul Brook | s->int_sts |= RSFL_INT; |
596 | 2a424990 | Paul Brook | } |
597 | 2a424990 | Paul Brook | lan9118_update(s); |
598 | 2a424990 | Paul Brook | |
599 | 2a424990 | Paul Brook | return size;
|
600 | 2a424990 | Paul Brook | } |
601 | 2a424990 | Paul Brook | |
602 | 2a424990 | Paul Brook | static uint32_t rx_fifo_pop(lan9118_state *s)
|
603 | 2a424990 | Paul Brook | { |
604 | 2a424990 | Paul Brook | int n;
|
605 | 2a424990 | Paul Brook | uint32_t val; |
606 | 2a424990 | Paul Brook | |
607 | 2a424990 | Paul Brook | if (s->rxp_size == 0 && s->rxp_pad == 0) { |
608 | 2a424990 | Paul Brook | s->rxp_size = s->rx_packet_size[s->rx_packet_size_head]; |
609 | 2a424990 | Paul Brook | s->rx_packet_size[s->rx_packet_size_head] = 0;
|
610 | 2a424990 | Paul Brook | if (s->rxp_size != 0) { |
611 | 2a424990 | Paul Brook | s->rx_packet_size_head = (s->rx_packet_size_head + 1023) & 1023; |
612 | 2a424990 | Paul Brook | s->rxp_offset = (s->rx_cfg >> 10) & 7; |
613 | 2a424990 | Paul Brook | n = s->rxp_offset + s->rxp_size; |
614 | 2a424990 | Paul Brook | switch (s->rx_cfg >> 30) { |
615 | 2a424990 | Paul Brook | case 1: |
616 | 2a424990 | Paul Brook | n = (-n) & 3;
|
617 | 2a424990 | Paul Brook | break;
|
618 | 2a424990 | Paul Brook | case 2: |
619 | 2a424990 | Paul Brook | n = (-n) & 7;
|
620 | 2a424990 | Paul Brook | break;
|
621 | 2a424990 | Paul Brook | default:
|
622 | 2a424990 | Paul Brook | n = 0;
|
623 | 2a424990 | Paul Brook | break;
|
624 | 2a424990 | Paul Brook | } |
625 | 2a424990 | Paul Brook | s->rxp_pad = n; |
626 | 2a424990 | Paul Brook | DPRINTF("Pop packet size:%d offset:%d pad: %d\n",
|
627 | 2a424990 | Paul Brook | s->rxp_size, s->rxp_offset, s->rxp_pad); |
628 | 2a424990 | Paul Brook | } |
629 | 2a424990 | Paul Brook | } |
630 | 2a424990 | Paul Brook | if (s->rxp_offset > 0) { |
631 | 2a424990 | Paul Brook | s->rxp_offset--; |
632 | 2a424990 | Paul Brook | val = 0;
|
633 | 2a424990 | Paul Brook | } else if (s->rxp_size > 0) { |
634 | 2a424990 | Paul Brook | s->rxp_size--; |
635 | 2a424990 | Paul Brook | val = s->rx_fifo[s->rx_fifo_head++]; |
636 | 2a424990 | Paul Brook | if (s->rx_fifo_head >= s->rx_fifo_size) {
|
637 | 2a424990 | Paul Brook | s->rx_fifo_head -= s->rx_fifo_size; |
638 | 2a424990 | Paul Brook | } |
639 | 2a424990 | Paul Brook | s->rx_fifo_used--; |
640 | 2a424990 | Paul Brook | } else if (s->rxp_pad > 0) { |
641 | 2a424990 | Paul Brook | s->rxp_pad--; |
642 | 2a424990 | Paul Brook | val = 0;
|
643 | 2a424990 | Paul Brook | } else {
|
644 | 2a424990 | Paul Brook | DPRINTF("RX underflow\n");
|
645 | 2a424990 | Paul Brook | s->int_sts |= RXE_INT; |
646 | 2a424990 | Paul Brook | val = 0;
|
647 | 2a424990 | Paul Brook | } |
648 | 2a424990 | Paul Brook | lan9118_update(s); |
649 | 2a424990 | Paul Brook | return val;
|
650 | 2a424990 | Paul Brook | } |
651 | 2a424990 | Paul Brook | |
652 | 2a424990 | Paul Brook | static void do_tx_packet(lan9118_state *s) |
653 | 2a424990 | Paul Brook | { |
654 | 2a424990 | Paul Brook | int n;
|
655 | 2a424990 | Paul Brook | uint32_t status; |
656 | 2a424990 | Paul Brook | |
657 | 2a424990 | Paul Brook | /* FIXME: Honor TX disable, and allow queueing of packets. */
|
658 | 2a424990 | Paul Brook | if (s->phy_control & 0x4000) { |
659 | 2a424990 | Paul Brook | /* This assumes the receive routine doesn't touch the VLANClient. */
|
660 | b356f76d | Jason Wang | lan9118_receive(qemu_get_queue(s->nic), s->txp->data, s->txp->len); |
661 | 2a424990 | Paul Brook | } else {
|
662 | b356f76d | Jason Wang | qemu_send_packet(qemu_get_queue(s->nic), s->txp->data, s->txp->len); |
663 | 2a424990 | Paul Brook | } |
664 | 2a424990 | Paul Brook | s->txp->fifo_used = 0;
|
665 | 2a424990 | Paul Brook | |
666 | 2a424990 | Paul Brook | if (s->tx_status_fifo_used == 512) { |
667 | 2a424990 | Paul Brook | /* Status FIFO full */
|
668 | 2a424990 | Paul Brook | return;
|
669 | 2a424990 | Paul Brook | } |
670 | 2a424990 | Paul Brook | /* Add entry to status FIFO. */
|
671 | 2a424990 | Paul Brook | status = s->txp->cmd_b & 0xffff0000u;
|
672 | 2a424990 | Paul Brook | DPRINTF("Sent packet tag:%04x len %d\n", status >> 16, s->txp->len); |
673 | 2a424990 | Paul Brook | n = (s->tx_status_fifo_head + s->tx_status_fifo_used) & 511;
|
674 | 2a424990 | Paul Brook | s->tx_status_fifo[n] = status; |
675 | 2a424990 | Paul Brook | s->tx_status_fifo_used++; |
676 | 2a424990 | Paul Brook | if (s->tx_status_fifo_used == 512) { |
677 | 2a424990 | Paul Brook | s->int_sts |= TSFF_INT; |
678 | 2a424990 | Paul Brook | /* TODO: Stop transmission. */
|
679 | 2a424990 | Paul Brook | } |
680 | 2a424990 | Paul Brook | } |
681 | 2a424990 | Paul Brook | |
682 | 2a424990 | Paul Brook | static uint32_t rx_status_fifo_pop(lan9118_state *s)
|
683 | 2a424990 | Paul Brook | { |
684 | 2a424990 | Paul Brook | uint32_t val; |
685 | 2a424990 | Paul Brook | |
686 | 2a424990 | Paul Brook | val = s->rx_status_fifo[s->rx_status_fifo_head]; |
687 | 2a424990 | Paul Brook | if (s->rx_status_fifo_used != 0) { |
688 | 2a424990 | Paul Brook | s->rx_status_fifo_used--; |
689 | 2a424990 | Paul Brook | s->rx_status_fifo_head++; |
690 | 2a424990 | Paul Brook | if (s->rx_status_fifo_head >= s->rx_status_fifo_size) {
|
691 | 2a424990 | Paul Brook | s->rx_status_fifo_head -= s->rx_status_fifo_size; |
692 | 2a424990 | Paul Brook | } |
693 | 2a424990 | Paul Brook | /* ??? What value should be returned when the FIFO is empty? */
|
694 | 2a424990 | Paul Brook | DPRINTF("RX status pop 0x%08x\n", val);
|
695 | 2a424990 | Paul Brook | } |
696 | 2a424990 | Paul Brook | return val;
|
697 | 2a424990 | Paul Brook | } |
698 | 2a424990 | Paul Brook | |
699 | 2a424990 | Paul Brook | static uint32_t tx_status_fifo_pop(lan9118_state *s)
|
700 | 2a424990 | Paul Brook | { |
701 | 2a424990 | Paul Brook | uint32_t val; |
702 | 2a424990 | Paul Brook | |
703 | 2a424990 | Paul Brook | val = s->tx_status_fifo[s->tx_status_fifo_head]; |
704 | 2a424990 | Paul Brook | if (s->tx_status_fifo_used != 0) { |
705 | 2a424990 | Paul Brook | s->tx_status_fifo_used--; |
706 | 2a424990 | Paul Brook | s->tx_status_fifo_head = (s->tx_status_fifo_head + 1) & 511; |
707 | 2a424990 | Paul Brook | /* ??? What value should be returned when the FIFO is empty? */
|
708 | 2a424990 | Paul Brook | } |
709 | 2a424990 | Paul Brook | return val;
|
710 | 2a424990 | Paul Brook | } |
711 | 2a424990 | Paul Brook | |
712 | 2a424990 | Paul Brook | static void tx_fifo_push(lan9118_state *s, uint32_t val) |
713 | 2a424990 | Paul Brook | { |
714 | 2a424990 | Paul Brook | int n;
|
715 | 2a424990 | Paul Brook | |
716 | 2a424990 | Paul Brook | if (s->txp->fifo_used == s->tx_fifo_size) {
|
717 | 2a424990 | Paul Brook | s->int_sts |= TDFO_INT; |
718 | 2a424990 | Paul Brook | return;
|
719 | 2a424990 | Paul Brook | } |
720 | 2a424990 | Paul Brook | switch (s->txp->state) {
|
721 | 2a424990 | Paul Brook | case TX_IDLE:
|
722 | 2a424990 | Paul Brook | s->txp->cmd_a = val & 0x831f37ff;
|
723 | 2a424990 | Paul Brook | s->txp->fifo_used++; |
724 | 2a424990 | Paul Brook | s->txp->state = TX_B; |
725 | 2a424990 | Paul Brook | break;
|
726 | 2a424990 | Paul Brook | case TX_B:
|
727 | 2a424990 | Paul Brook | if (s->txp->cmd_a & 0x2000) { |
728 | 2a424990 | Paul Brook | /* First segment */
|
729 | 2a424990 | Paul Brook | s->txp->cmd_b = val; |
730 | 2a424990 | Paul Brook | s->txp->fifo_used++; |
731 | 2a424990 | Paul Brook | s->txp->buffer_size = s->txp->cmd_a & 0x7ff;
|
732 | 2a424990 | Paul Brook | s->txp->offset = (s->txp->cmd_a >> 16) & 0x1f; |
733 | 2a424990 | Paul Brook | /* End alignment does not include command words. */
|
734 | 2a424990 | Paul Brook | n = (s->txp->buffer_size + s->txp->offset + 3) >> 2; |
735 | 2a424990 | Paul Brook | switch ((n >> 24) & 3) { |
736 | 2a424990 | Paul Brook | case 1: |
737 | 2a424990 | Paul Brook | n = (-n) & 3;
|
738 | 2a424990 | Paul Brook | break;
|
739 | 2a424990 | Paul Brook | case 2: |
740 | 2a424990 | Paul Brook | n = (-n) & 7;
|
741 | 2a424990 | Paul Brook | break;
|
742 | 2a424990 | Paul Brook | default:
|
743 | 2a424990 | Paul Brook | n = 0;
|
744 | 2a424990 | Paul Brook | } |
745 | 2a424990 | Paul Brook | s->txp->pad = n; |
746 | 2a424990 | Paul Brook | s->txp->len = 0;
|
747 | 2a424990 | Paul Brook | } |
748 | 2a424990 | Paul Brook | DPRINTF("Block len:%d offset:%d pad:%d cmd %08x\n",
|
749 | 2a424990 | Paul Brook | s->txp->buffer_size, s->txp->offset, s->txp->pad, |
750 | 2a424990 | Paul Brook | s->txp->cmd_a); |
751 | 2a424990 | Paul Brook | s->txp->state = TX_DATA; |
752 | 2a424990 | Paul Brook | break;
|
753 | 2a424990 | Paul Brook | case TX_DATA:
|
754 | 2a424990 | Paul Brook | if (s->txp->offset >= 4) { |
755 | 2a424990 | Paul Brook | s->txp->offset -= 4;
|
756 | 2a424990 | Paul Brook | break;
|
757 | 2a424990 | Paul Brook | } |
758 | 2a424990 | Paul Brook | if (s->txp->buffer_size <= 0 && s->txp->pad != 0) { |
759 | 2a424990 | Paul Brook | s->txp->pad--; |
760 | 2a424990 | Paul Brook | } else {
|
761 | 2a424990 | Paul Brook | n = 4;
|
762 | 2a424990 | Paul Brook | while (s->txp->offset) {
|
763 | 2a424990 | Paul Brook | val >>= 8;
|
764 | 2a424990 | Paul Brook | n--; |
765 | 2a424990 | Paul Brook | s->txp->offset--; |
766 | 2a424990 | Paul Brook | } |
767 | 2a424990 | Paul Brook | /* Documentation is somewhat unclear on the ordering of bytes
|
768 | 2a424990 | Paul Brook | in FIFO words. Empirical results show it to be little-endian.
|
769 | 2a424990 | Paul Brook | */
|
770 | 2a424990 | Paul Brook | /* TODO: FIFO overflow checking. */
|
771 | 2a424990 | Paul Brook | while (n--) {
|
772 | 2a424990 | Paul Brook | s->txp->data[s->txp->len] = val & 0xff;
|
773 | 2a424990 | Paul Brook | s->txp->len++; |
774 | 2a424990 | Paul Brook | val >>= 8;
|
775 | 2a424990 | Paul Brook | s->txp->buffer_size--; |
776 | 2a424990 | Paul Brook | } |
777 | 2a424990 | Paul Brook | s->txp->fifo_used++; |
778 | 2a424990 | Paul Brook | } |
779 | 2a424990 | Paul Brook | if (s->txp->buffer_size <= 0 && s->txp->pad == 0) { |
780 | 2a424990 | Paul Brook | if (s->txp->cmd_a & 0x1000) { |
781 | 2a424990 | Paul Brook | do_tx_packet(s); |
782 | 2a424990 | Paul Brook | } |
783 | 2a424990 | Paul Brook | if (s->txp->cmd_a & 0x80000000) { |
784 | 2a424990 | Paul Brook | s->int_sts |= TX_IOC_INT; |
785 | 2a424990 | Paul Brook | } |
786 | 2a424990 | Paul Brook | s->txp->state = TX_IDLE; |
787 | 2a424990 | Paul Brook | } |
788 | 2a424990 | Paul Brook | break;
|
789 | 2a424990 | Paul Brook | } |
790 | 2a424990 | Paul Brook | } |
791 | 2a424990 | Paul Brook | |
792 | 2a424990 | Paul Brook | static uint32_t do_phy_read(lan9118_state *s, int reg) |
793 | 2a424990 | Paul Brook | { |
794 | 209bf965 | Paul Brook | uint32_t val; |
795 | 209bf965 | Paul Brook | |
796 | 2a424990 | Paul Brook | switch (reg) {
|
797 | 2a424990 | Paul Brook | case 0: /* Basic Control */ |
798 | 2a424990 | Paul Brook | return s->phy_control;
|
799 | 2a424990 | Paul Brook | case 1: /* Basic Status */ |
800 | 2a424990 | Paul Brook | return s->phy_status;
|
801 | 2a424990 | Paul Brook | case 2: /* ID1 */ |
802 | 2a424990 | Paul Brook | return 0x0007; |
803 | 2a424990 | Paul Brook | case 3: /* ID2 */ |
804 | 2a424990 | Paul Brook | return 0xc0d1; |
805 | 66a0a2cb | Dong Xu Wang | case 4: /* Auto-neg advertisement */ |
806 | 2a424990 | Paul Brook | return s->phy_advertise;
|
807 | 2a424990 | Paul Brook | case 5: /* Auto-neg Link Partner Ability */ |
808 | 2a424990 | Paul Brook | return 0x0f71; |
809 | 2a424990 | Paul Brook | case 6: /* Auto-neg Expansion */ |
810 | 2a424990 | Paul Brook | return 1; |
811 | 2a424990 | Paul Brook | /* TODO 17, 18, 27, 29, 30, 31 */
|
812 | 209bf965 | Paul Brook | case 29: /* Interrupt source. */ |
813 | 209bf965 | Paul Brook | val = s->phy_int; |
814 | 209bf965 | Paul Brook | s->phy_int = 0;
|
815 | 209bf965 | Paul Brook | phy_update_irq(s); |
816 | 209bf965 | Paul Brook | return val;
|
817 | 209bf965 | Paul Brook | case 30: /* Interrupt mask */ |
818 | 209bf965 | Paul Brook | return s->phy_int_mask;
|
819 | 2a424990 | Paul Brook | default:
|
820 | 2a424990 | Paul Brook | BADF("PHY read reg %d\n", reg);
|
821 | 2a424990 | Paul Brook | return 0; |
822 | 2a424990 | Paul Brook | } |
823 | 2a424990 | Paul Brook | } |
824 | 2a424990 | Paul Brook | |
825 | 2a424990 | Paul Brook | static void do_phy_write(lan9118_state *s, int reg, uint32_t val) |
826 | 2a424990 | Paul Brook | { |
827 | 2a424990 | Paul Brook | switch (reg) {
|
828 | 2a424990 | Paul Brook | case 0: /* Basic Control */ |
829 | 2a424990 | Paul Brook | if (val & 0x8000) { |
830 | 2a424990 | Paul Brook | phy_reset(s); |
831 | 2a424990 | Paul Brook | break;
|
832 | 2a424990 | Paul Brook | } |
833 | 2a424990 | Paul Brook | s->phy_control = val & 0x7980;
|
834 | 4b71051e | Stefan Weil | /* Complete autonegotiation immediately. */
|
835 | 2a424990 | Paul Brook | if (val & 0x1000) { |
836 | 2a424990 | Paul Brook | s->phy_status |= 0x0020;
|
837 | 2a424990 | Paul Brook | } |
838 | 2a424990 | Paul Brook | break;
|
839 | 66a0a2cb | Dong Xu Wang | case 4: /* Auto-neg advertisement */ |
840 | 2a424990 | Paul Brook | s->phy_advertise = (val & 0x2d7f) | 0x80; |
841 | 2a424990 | Paul Brook | break;
|
842 | 209bf965 | Paul Brook | /* TODO 17, 18, 27, 31 */
|
843 | 209bf965 | Paul Brook | case 30: /* Interrupt mask */ |
844 | 209bf965 | Paul Brook | s->phy_int_mask = val & 0xff;
|
845 | 209bf965 | Paul Brook | phy_update_irq(s); |
846 | 209bf965 | Paul Brook | break;
|
847 | 2a424990 | Paul Brook | default:
|
848 | 2a424990 | Paul Brook | BADF("PHY write reg %d = 0x%04x\n", reg, val);
|
849 | 2a424990 | Paul Brook | } |
850 | 2a424990 | Paul Brook | } |
851 | 2a424990 | Paul Brook | |
852 | 2a424990 | Paul Brook | static void do_mac_write(lan9118_state *s, int reg, uint32_t val) |
853 | 2a424990 | Paul Brook | { |
854 | 2a424990 | Paul Brook | switch (reg) {
|
855 | 2a424990 | Paul Brook | case MAC_CR:
|
856 | 2a424990 | Paul Brook | if ((s->mac_cr & MAC_CR_RXEN) != 0 && (val & MAC_CR_RXEN) == 0) { |
857 | 2a424990 | Paul Brook | s->int_sts |= RXSTOP_INT; |
858 | 2a424990 | Paul Brook | } |
859 | 2a424990 | Paul Brook | s->mac_cr = val & ~MAC_CR_RESERVED; |
860 | 2a424990 | Paul Brook | DPRINTF("MAC_CR: %08x\n", val);
|
861 | 2a424990 | Paul Brook | break;
|
862 | 2a424990 | Paul Brook | case MAC_ADDRH:
|
863 | 2a424990 | Paul Brook | s->conf.macaddr.a[4] = val & 0xff; |
864 | 2a424990 | Paul Brook | s->conf.macaddr.a[5] = (val >> 8) & 0xff; |
865 | 2a424990 | Paul Brook | lan9118_mac_changed(s); |
866 | 2a424990 | Paul Brook | break;
|
867 | 2a424990 | Paul Brook | case MAC_ADDRL:
|
868 | 2a424990 | Paul Brook | s->conf.macaddr.a[0] = val & 0xff; |
869 | 2a424990 | Paul Brook | s->conf.macaddr.a[1] = (val >> 8) & 0xff; |
870 | 2a424990 | Paul Brook | s->conf.macaddr.a[2] = (val >> 16) & 0xff; |
871 | 2a424990 | Paul Brook | s->conf.macaddr.a[3] = (val >> 24) & 0xff; |
872 | 2a424990 | Paul Brook | lan9118_mac_changed(s); |
873 | 2a424990 | Paul Brook | break;
|
874 | 2a424990 | Paul Brook | case MAC_HASHH:
|
875 | 2a424990 | Paul Brook | s->mac_hashh = val; |
876 | 2a424990 | Paul Brook | break;
|
877 | 2a424990 | Paul Brook | case MAC_HASHL:
|
878 | 2a424990 | Paul Brook | s->mac_hashl = val; |
879 | 2a424990 | Paul Brook | break;
|
880 | 2a424990 | Paul Brook | case MAC_MII_ACC:
|
881 | 2a424990 | Paul Brook | s->mac_mii_acc = val & 0xffc2;
|
882 | 2a424990 | Paul Brook | if (val & 2) { |
883 | 2a424990 | Paul Brook | DPRINTF("PHY write %d = 0x%04x\n",
|
884 | 2a424990 | Paul Brook | (val >> 6) & 0x1f, s->mac_mii_data); |
885 | 2a424990 | Paul Brook | do_phy_write(s, (val >> 6) & 0x1f, s->mac_mii_data); |
886 | 2a424990 | Paul Brook | } else {
|
887 | 2a424990 | Paul Brook | s->mac_mii_data = do_phy_read(s, (val >> 6) & 0x1f); |
888 | 2a424990 | Paul Brook | DPRINTF("PHY read %d = 0x%04x\n",
|
889 | 2a424990 | Paul Brook | (val >> 6) & 0x1f, s->mac_mii_data); |
890 | 2a424990 | Paul Brook | } |
891 | 2a424990 | Paul Brook | break;
|
892 | 2a424990 | Paul Brook | case MAC_MII_DATA:
|
893 | 2a424990 | Paul Brook | s->mac_mii_data = val & 0xffff;
|
894 | 2a424990 | Paul Brook | break;
|
895 | 2a424990 | Paul Brook | case MAC_FLOW:
|
896 | 2a424990 | Paul Brook | s->mac_flow = val & 0xffff0000;
|
897 | 2a424990 | Paul Brook | break;
|
898 | a0313c00 | Atsushi Nemoto | case MAC_VLAN1:
|
899 | a0313c00 | Atsushi Nemoto | /* Writing to this register changes a condition for
|
900 | a0313c00 | Atsushi Nemoto | * FrameTooLong bit in rx_status. Since we do not set
|
901 | a0313c00 | Atsushi Nemoto | * FrameTooLong anyway, just ignore write to this.
|
902 | a0313c00 | Atsushi Nemoto | */
|
903 | a0313c00 | Atsushi Nemoto | break;
|
904 | 2a424990 | Paul Brook | default:
|
905 | 2a424990 | Paul Brook | hw_error("lan9118: Unimplemented MAC register write: %d = 0x%x\n",
|
906 | 2a424990 | Paul Brook | s->mac_cmd & 0xf, val);
|
907 | 2a424990 | Paul Brook | } |
908 | 2a424990 | Paul Brook | } |
909 | 2a424990 | Paul Brook | |
910 | 2a424990 | Paul Brook | static uint32_t do_mac_read(lan9118_state *s, int reg) |
911 | 2a424990 | Paul Brook | { |
912 | 2a424990 | Paul Brook | switch (reg) {
|
913 | 2a424990 | Paul Brook | case MAC_CR:
|
914 | 2a424990 | Paul Brook | return s->mac_cr;
|
915 | 2a424990 | Paul Brook | case MAC_ADDRH:
|
916 | 2a424990 | Paul Brook | return s->conf.macaddr.a[4] | (s->conf.macaddr.a[5] << 8); |
917 | 2a424990 | Paul Brook | case MAC_ADDRL:
|
918 | 2a424990 | Paul Brook | return s->conf.macaddr.a[0] | (s->conf.macaddr.a[1] << 8) |
919 | 2a424990 | Paul Brook | | (s->conf.macaddr.a[2] << 16) | (s->conf.macaddr.a[3] << 24); |
920 | 2a424990 | Paul Brook | case MAC_HASHH:
|
921 | 2a424990 | Paul Brook | return s->mac_hashh;
|
922 | 2a424990 | Paul Brook | break;
|
923 | 2a424990 | Paul Brook | case MAC_HASHL:
|
924 | 2a424990 | Paul Brook | return s->mac_hashl;
|
925 | 2a424990 | Paul Brook | break;
|
926 | 2a424990 | Paul Brook | case MAC_MII_ACC:
|
927 | 2a424990 | Paul Brook | return s->mac_mii_acc;
|
928 | 2a424990 | Paul Brook | case MAC_MII_DATA:
|
929 | 2a424990 | Paul Brook | return s->mac_mii_data;
|
930 | 2a424990 | Paul Brook | case MAC_FLOW:
|
931 | 2a424990 | Paul Brook | return s->mac_flow;
|
932 | 2a424990 | Paul Brook | default:
|
933 | 2a424990 | Paul Brook | hw_error("lan9118: Unimplemented MAC register read: %d\n",
|
934 | 2a424990 | Paul Brook | s->mac_cmd & 0xf);
|
935 | 2a424990 | Paul Brook | } |
936 | 2a424990 | Paul Brook | } |
937 | 2a424990 | Paul Brook | |
938 | 2a424990 | Paul Brook | static void lan9118_eeprom_cmd(lan9118_state *s, int cmd, int addr) |
939 | 2a424990 | Paul Brook | { |
940 | 2a424990 | Paul Brook | s->e2p_cmd = (s->e2p_cmd & 0x10) | (cmd << 28) | addr; |
941 | 2a424990 | Paul Brook | switch (cmd) {
|
942 | 2a424990 | Paul Brook | case 0: |
943 | 2a424990 | Paul Brook | s->e2p_data = s->eeprom[addr]; |
944 | 2a424990 | Paul Brook | DPRINTF("EEPROM Read %d = 0x%02x\n", addr, s->e2p_data);
|
945 | 2a424990 | Paul Brook | break;
|
946 | 2a424990 | Paul Brook | case 1: |
947 | 2a424990 | Paul Brook | s->eeprom_writable = 0;
|
948 | 2a424990 | Paul Brook | DPRINTF("EEPROM Write Disable\n");
|
949 | 2a424990 | Paul Brook | break;
|
950 | 2a424990 | Paul Brook | case 2: /* EWEN */ |
951 | 2a424990 | Paul Brook | s->eeprom_writable = 1;
|
952 | 2a424990 | Paul Brook | DPRINTF("EEPROM Write Enable\n");
|
953 | 2a424990 | Paul Brook | break;
|
954 | 2a424990 | Paul Brook | case 3: /* WRITE */ |
955 | 2a424990 | Paul Brook | if (s->eeprom_writable) {
|
956 | 2a424990 | Paul Brook | s->eeprom[addr] &= s->e2p_data; |
957 | 2a424990 | Paul Brook | DPRINTF("EEPROM Write %d = 0x%02x\n", addr, s->e2p_data);
|
958 | 2a424990 | Paul Brook | } else {
|
959 | 2a424990 | Paul Brook | DPRINTF("EEPROM Write %d (ignored)\n", addr);
|
960 | 2a424990 | Paul Brook | } |
961 | 2a424990 | Paul Brook | break;
|
962 | 2a424990 | Paul Brook | case 4: /* WRAL */ |
963 | 2a424990 | Paul Brook | if (s->eeprom_writable) {
|
964 | 2a424990 | Paul Brook | for (addr = 0; addr < 128; addr++) { |
965 | 2a424990 | Paul Brook | s->eeprom[addr] &= s->e2p_data; |
966 | 2a424990 | Paul Brook | } |
967 | 2a424990 | Paul Brook | DPRINTF("EEPROM Write All 0x%02x\n", s->e2p_data);
|
968 | 2a424990 | Paul Brook | } else {
|
969 | 2a424990 | Paul Brook | DPRINTF("EEPROM Write All (ignored)\n");
|
970 | 2a424990 | Paul Brook | } |
971 | 0e3b800e | Peter Maydell | break;
|
972 | 2a424990 | Paul Brook | case 5: /* ERASE */ |
973 | 2a424990 | Paul Brook | if (s->eeprom_writable) {
|
974 | 2a424990 | Paul Brook | s->eeprom[addr] = 0xff;
|
975 | 2a424990 | Paul Brook | DPRINTF("EEPROM Erase %d\n", addr);
|
976 | 2a424990 | Paul Brook | } else {
|
977 | 2a424990 | Paul Brook | DPRINTF("EEPROM Erase %d (ignored)\n", addr);
|
978 | 2a424990 | Paul Brook | } |
979 | 2a424990 | Paul Brook | break;
|
980 | 2a424990 | Paul Brook | case 6: /* ERAL */ |
981 | 2a424990 | Paul Brook | if (s->eeprom_writable) {
|
982 | 2a424990 | Paul Brook | memset(s->eeprom, 0xff, 128); |
983 | 2a424990 | Paul Brook | DPRINTF("EEPROM Erase All\n");
|
984 | 2a424990 | Paul Brook | } else {
|
985 | 2a424990 | Paul Brook | DPRINTF("EEPROM Erase All (ignored)\n");
|
986 | 2a424990 | Paul Brook | } |
987 | 2a424990 | Paul Brook | break;
|
988 | 2a424990 | Paul Brook | case 7: /* RELOAD */ |
989 | 2a424990 | Paul Brook | lan9118_reload_eeprom(s); |
990 | 2a424990 | Paul Brook | break;
|
991 | 2a424990 | Paul Brook | } |
992 | 2a424990 | Paul Brook | } |
993 | 2a424990 | Paul Brook | |
994 | 209bf965 | Paul Brook | static void lan9118_tick(void *opaque) |
995 | 209bf965 | Paul Brook | { |
996 | 209bf965 | Paul Brook | lan9118_state *s = (lan9118_state *)opaque; |
997 | 209bf965 | Paul Brook | if (s->int_en & GPT_INT) {
|
998 | 209bf965 | Paul Brook | s->int_sts |= GPT_INT; |
999 | 209bf965 | Paul Brook | } |
1000 | 209bf965 | Paul Brook | lan9118_update(s); |
1001 | 209bf965 | Paul Brook | } |
1002 | 209bf965 | Paul Brook | |
1003 | a8170e5e | Avi Kivity | static void lan9118_writel(void *opaque, hwaddr offset, |
1004 | f0cdd7a9 | Peter Maydell | uint64_t val, unsigned size)
|
1005 | 2a424990 | Paul Brook | { |
1006 | 2a424990 | Paul Brook | lan9118_state *s = (lan9118_state *)opaque; |
1007 | 2a424990 | Paul Brook | offset &= 0xff;
|
1008 | 1248f8d4 | Evgeny Voevodin | |
1009 | 2a424990 | Paul Brook | //DPRINTF("Write reg 0x%02x = 0x%08x\n", (int)offset, val);
|
1010 | 2a424990 | Paul Brook | if (offset >= 0x20 && offset < 0x40) { |
1011 | 2a424990 | Paul Brook | /* TX FIFO */
|
1012 | 2a424990 | Paul Brook | tx_fifo_push(s, val); |
1013 | 2a424990 | Paul Brook | return;
|
1014 | 2a424990 | Paul Brook | } |
1015 | 2a424990 | Paul Brook | switch (offset) {
|
1016 | 2a424990 | Paul Brook | case CSR_IRQ_CFG:
|
1017 | 2a424990 | Paul Brook | /* TODO: Implement interrupt deassertion intervals. */
|
1018 | eb47d7c5 | Peter Maydell | val &= (IRQ_EN | IRQ_POL | IRQ_TYPE); |
1019 | eb47d7c5 | Peter Maydell | s->irq_cfg = (s->irq_cfg & IRQ_INT) | val; |
1020 | 2a424990 | Paul Brook | break;
|
1021 | 2a424990 | Paul Brook | case CSR_INT_STS:
|
1022 | 2a424990 | Paul Brook | s->int_sts &= ~val; |
1023 | 2a424990 | Paul Brook | break;
|
1024 | 2a424990 | Paul Brook | case CSR_INT_EN:
|
1025 | 2a424990 | Paul Brook | s->int_en = val & ~RESERVED_INT; |
1026 | 2a424990 | Paul Brook | s->int_sts |= val & SW_INT; |
1027 | 2a424990 | Paul Brook | break;
|
1028 | 2a424990 | Paul Brook | case CSR_FIFO_INT:
|
1029 | 2a424990 | Paul Brook | DPRINTF("FIFO INT levels %08x\n", val);
|
1030 | 2a424990 | Paul Brook | s->fifo_int = val; |
1031 | 2a424990 | Paul Brook | break;
|
1032 | 2a424990 | Paul Brook | case CSR_RX_CFG:
|
1033 | 2a424990 | Paul Brook | if (val & 0x8000) { |
1034 | 2a424990 | Paul Brook | /* RX_DUMP */
|
1035 | 2a424990 | Paul Brook | s->rx_fifo_used = 0;
|
1036 | 2a424990 | Paul Brook | s->rx_status_fifo_used = 0;
|
1037 | 2a424990 | Paul Brook | s->rx_packet_size_tail = s->rx_packet_size_head; |
1038 | 2a424990 | Paul Brook | s->rx_packet_size[s->rx_packet_size_head] = 0;
|
1039 | 2a424990 | Paul Brook | } |
1040 | 2a424990 | Paul Brook | s->rx_cfg = val & 0xcfff1ff0;
|
1041 | 2a424990 | Paul Brook | break;
|
1042 | 2a424990 | Paul Brook | case CSR_TX_CFG:
|
1043 | 2a424990 | Paul Brook | if (val & 0x8000) { |
1044 | 2a424990 | Paul Brook | s->tx_status_fifo_used = 0;
|
1045 | 2a424990 | Paul Brook | } |
1046 | 2a424990 | Paul Brook | if (val & 0x4000) { |
1047 | 2a424990 | Paul Brook | s->txp->state = TX_IDLE; |
1048 | 2a424990 | Paul Brook | s->txp->fifo_used = 0;
|
1049 | 2a424990 | Paul Brook | s->txp->cmd_a = 0xffffffff;
|
1050 | 2a424990 | Paul Brook | } |
1051 | 2a424990 | Paul Brook | s->tx_cfg = val & 6;
|
1052 | 2a424990 | Paul Brook | break;
|
1053 | 2a424990 | Paul Brook | case CSR_HW_CFG:
|
1054 | 2a424990 | Paul Brook | if (val & 1) { |
1055 | 2a424990 | Paul Brook | /* SRST */
|
1056 | 2a424990 | Paul Brook | lan9118_reset(&s->busdev.qdev); |
1057 | 2a424990 | Paul Brook | } else {
|
1058 | 1248f8d4 | Evgeny Voevodin | s->hw_cfg = (val & 0x003f300) | (s->hw_cfg & 0x4); |
1059 | 2a424990 | Paul Brook | } |
1060 | 2a424990 | Paul Brook | break;
|
1061 | 2a424990 | Paul Brook | case CSR_RX_DP_CTRL:
|
1062 | 2a424990 | Paul Brook | if (val & 0x80000000) { |
1063 | 2a424990 | Paul Brook | /* Skip forward to next packet. */
|
1064 | 2a424990 | Paul Brook | s->rxp_pad = 0;
|
1065 | 2a424990 | Paul Brook | s->rxp_offset = 0;
|
1066 | 2a424990 | Paul Brook | if (s->rxp_size == 0) { |
1067 | 2a424990 | Paul Brook | /* Pop a word to start the next packet. */
|
1068 | 2a424990 | Paul Brook | rx_fifo_pop(s); |
1069 | 2a424990 | Paul Brook | s->rxp_pad = 0;
|
1070 | 2a424990 | Paul Brook | s->rxp_offset = 0;
|
1071 | 2a424990 | Paul Brook | } |
1072 | 2a424990 | Paul Brook | s->rx_fifo_head += s->rxp_size; |
1073 | 2a424990 | Paul Brook | if (s->rx_fifo_head >= s->rx_fifo_size) {
|
1074 | 2a424990 | Paul Brook | s->rx_fifo_head -= s->rx_fifo_size; |
1075 | 2a424990 | Paul Brook | } |
1076 | 2a424990 | Paul Brook | } |
1077 | 2a424990 | Paul Brook | break;
|
1078 | 2a424990 | Paul Brook | case CSR_PMT_CTRL:
|
1079 | 2a424990 | Paul Brook | if (val & 0x400) { |
1080 | 2a424990 | Paul Brook | phy_reset(s); |
1081 | 2a424990 | Paul Brook | } |
1082 | 2a424990 | Paul Brook | s->pmt_ctrl &= ~0x34e;
|
1083 | 2a424990 | Paul Brook | s->pmt_ctrl |= (val & 0x34e);
|
1084 | 2a424990 | Paul Brook | break;
|
1085 | 2a424990 | Paul Brook | case CSR_GPIO_CFG:
|
1086 | 2a424990 | Paul Brook | /* Probably just enabling LEDs. */
|
1087 | 2a424990 | Paul Brook | s->gpio_cfg = val & 0x7777071f;
|
1088 | 2a424990 | Paul Brook | break;
|
1089 | 209bf965 | Paul Brook | case CSR_GPT_CFG:
|
1090 | 209bf965 | Paul Brook | if ((s->gpt_cfg ^ val) & GPT_TIMER_EN) {
|
1091 | 209bf965 | Paul Brook | if (val & GPT_TIMER_EN) {
|
1092 | 209bf965 | Paul Brook | ptimer_set_count(s->timer, val & 0xffff);
|
1093 | 209bf965 | Paul Brook | ptimer_run(s->timer, 0);
|
1094 | 209bf965 | Paul Brook | } else {
|
1095 | 209bf965 | Paul Brook | ptimer_stop(s->timer); |
1096 | 209bf965 | Paul Brook | ptimer_set_count(s->timer, 0xffff);
|
1097 | 209bf965 | Paul Brook | } |
1098 | 209bf965 | Paul Brook | } |
1099 | 209bf965 | Paul Brook | s->gpt_cfg = val & (GPT_TIMER_EN | 0xffff);
|
1100 | 209bf965 | Paul Brook | break;
|
1101 | 2a424990 | Paul Brook | case CSR_WORD_SWAP:
|
1102 | 2a424990 | Paul Brook | /* Ignored because we're in 32-bit mode. */
|
1103 | 2a424990 | Paul Brook | s->word_swap = val; |
1104 | 2a424990 | Paul Brook | break;
|
1105 | 2a424990 | Paul Brook | case CSR_MAC_CSR_CMD:
|
1106 | 2a424990 | Paul Brook | s->mac_cmd = val & 0x4000000f;
|
1107 | 2a424990 | Paul Brook | if (val & 0x80000000) { |
1108 | 2a424990 | Paul Brook | if (val & 0x40000000) { |
1109 | 2a424990 | Paul Brook | s->mac_data = do_mac_read(s, val & 0xf);
|
1110 | 2a424990 | Paul Brook | DPRINTF("MAC read %d = 0x%08x\n", val & 0xf, s->mac_data); |
1111 | 2a424990 | Paul Brook | } else {
|
1112 | 2a424990 | Paul Brook | DPRINTF("MAC write %d = 0x%08x\n", val & 0xf, s->mac_data); |
1113 | 2a424990 | Paul Brook | do_mac_write(s, val & 0xf, s->mac_data);
|
1114 | 2a424990 | Paul Brook | } |
1115 | 2a424990 | Paul Brook | } |
1116 | 2a424990 | Paul Brook | break;
|
1117 | 2a424990 | Paul Brook | case CSR_MAC_CSR_DATA:
|
1118 | 2a424990 | Paul Brook | s->mac_data = val; |
1119 | 2a424990 | Paul Brook | break;
|
1120 | 2a424990 | Paul Brook | case CSR_AFC_CFG:
|
1121 | 2a424990 | Paul Brook | s->afc_cfg = val & 0x00ffffff;
|
1122 | 2a424990 | Paul Brook | break;
|
1123 | 2a424990 | Paul Brook | case CSR_E2P_CMD:
|
1124 | c46a3ea0 | Blue Swirl | lan9118_eeprom_cmd(s, (val >> 28) & 7, val & 0x7f); |
1125 | 2a424990 | Paul Brook | break;
|
1126 | 2a424990 | Paul Brook | case CSR_E2P_DATA:
|
1127 | 2a424990 | Paul Brook | s->e2p_data = val & 0xff;
|
1128 | 2a424990 | Paul Brook | break;
|
1129 | 2a424990 | Paul Brook | |
1130 | 2a424990 | Paul Brook | default:
|
1131 | f0cdd7a9 | Peter Maydell | hw_error("lan9118_write: Bad reg 0x%x = %x\n", (int)offset, (int)val); |
1132 | 2a424990 | Paul Brook | break;
|
1133 | 2a424990 | Paul Brook | } |
1134 | 2a424990 | Paul Brook | lan9118_update(s); |
1135 | 2a424990 | Paul Brook | } |
1136 | 2a424990 | Paul Brook | |
1137 | a8170e5e | Avi Kivity | static void lan9118_writew(void *opaque, hwaddr offset, |
1138 | 1248f8d4 | Evgeny Voevodin | uint32_t val) |
1139 | 1248f8d4 | Evgeny Voevodin | { |
1140 | 1248f8d4 | Evgeny Voevodin | lan9118_state *s = (lan9118_state *)opaque; |
1141 | 1248f8d4 | Evgeny Voevodin | offset &= 0xff;
|
1142 | 1248f8d4 | Evgeny Voevodin | |
1143 | 1248f8d4 | Evgeny Voevodin | if (s->write_word_prev_offset != (offset & ~0x3)) { |
1144 | 1248f8d4 | Evgeny Voevodin | /* New offset, reset word counter */
|
1145 | 1248f8d4 | Evgeny Voevodin | s->write_word_n = 0;
|
1146 | 1248f8d4 | Evgeny Voevodin | s->write_word_prev_offset = offset & ~0x3;
|
1147 | 1248f8d4 | Evgeny Voevodin | } |
1148 | 1248f8d4 | Evgeny Voevodin | |
1149 | 1248f8d4 | Evgeny Voevodin | if (offset & 0x2) { |
1150 | 1248f8d4 | Evgeny Voevodin | s->write_word_h = val; |
1151 | 1248f8d4 | Evgeny Voevodin | } else {
|
1152 | 1248f8d4 | Evgeny Voevodin | s->write_word_l = val; |
1153 | 1248f8d4 | Evgeny Voevodin | } |
1154 | 1248f8d4 | Evgeny Voevodin | |
1155 | 1248f8d4 | Evgeny Voevodin | //DPRINTF("Writew reg 0x%02x = 0x%08x\n", (int)offset, val);
|
1156 | 1248f8d4 | Evgeny Voevodin | s->write_word_n++; |
1157 | 1248f8d4 | Evgeny Voevodin | if (s->write_word_n == 2) { |
1158 | 1248f8d4 | Evgeny Voevodin | s->write_word_n = 0;
|
1159 | 1248f8d4 | Evgeny Voevodin | lan9118_writel(s, offset & ~3, s->write_word_l +
|
1160 | 1248f8d4 | Evgeny Voevodin | (s->write_word_h << 16), 4); |
1161 | 1248f8d4 | Evgeny Voevodin | } |
1162 | 1248f8d4 | Evgeny Voevodin | } |
1163 | 1248f8d4 | Evgeny Voevodin | |
1164 | a8170e5e | Avi Kivity | static void lan9118_16bit_mode_write(void *opaque, hwaddr offset, |
1165 | 1248f8d4 | Evgeny Voevodin | uint64_t val, unsigned size)
|
1166 | 1248f8d4 | Evgeny Voevodin | { |
1167 | 1248f8d4 | Evgeny Voevodin | switch (size) {
|
1168 | 1248f8d4 | Evgeny Voevodin | case 2: |
1169 | 0ed8b6f6 | Blue Swirl | lan9118_writew(opaque, offset, (uint32_t)val); |
1170 | 0ed8b6f6 | Blue Swirl | return;
|
1171 | 1248f8d4 | Evgeny Voevodin | case 4: |
1172 | 0ed8b6f6 | Blue Swirl | lan9118_writel(opaque, offset, val, size); |
1173 | 0ed8b6f6 | Blue Swirl | return;
|
1174 | 1248f8d4 | Evgeny Voevodin | } |
1175 | 1248f8d4 | Evgeny Voevodin | |
1176 | 1248f8d4 | Evgeny Voevodin | hw_error("lan9118_write: Bad size 0x%x\n", size);
|
1177 | 1248f8d4 | Evgeny Voevodin | } |
1178 | 1248f8d4 | Evgeny Voevodin | |
1179 | a8170e5e | Avi Kivity | static uint64_t lan9118_readl(void *opaque, hwaddr offset, |
1180 | f0cdd7a9 | Peter Maydell | unsigned size)
|
1181 | 2a424990 | Paul Brook | { |
1182 | 2a424990 | Paul Brook | lan9118_state *s = (lan9118_state *)opaque; |
1183 | 2a424990 | Paul Brook | |
1184 | 2a424990 | Paul Brook | //DPRINTF("Read reg 0x%02x\n", (int)offset);
|
1185 | 2a424990 | Paul Brook | if (offset < 0x20) { |
1186 | 2a424990 | Paul Brook | /* RX FIFO */
|
1187 | 2a424990 | Paul Brook | return rx_fifo_pop(s);
|
1188 | 2a424990 | Paul Brook | } |
1189 | 2a424990 | Paul Brook | switch (offset) {
|
1190 | 2a424990 | Paul Brook | case 0x40: |
1191 | 2a424990 | Paul Brook | return rx_status_fifo_pop(s);
|
1192 | 2a424990 | Paul Brook | case 0x44: |
1193 | 2a424990 | Paul Brook | return s->rx_status_fifo[s->tx_status_fifo_head];
|
1194 | 2a424990 | Paul Brook | case 0x48: |
1195 | 2a424990 | Paul Brook | return tx_status_fifo_pop(s);
|
1196 | 2a424990 | Paul Brook | case 0x4c: |
1197 | 2a424990 | Paul Brook | return s->tx_status_fifo[s->tx_status_fifo_head];
|
1198 | 2a424990 | Paul Brook | case CSR_ID_REV:
|
1199 | 2a424990 | Paul Brook | return 0x01180001; |
1200 | 2a424990 | Paul Brook | case CSR_IRQ_CFG:
|
1201 | 2a424990 | Paul Brook | return s->irq_cfg;
|
1202 | 2a424990 | Paul Brook | case CSR_INT_STS:
|
1203 | 2a424990 | Paul Brook | return s->int_sts;
|
1204 | 2a424990 | Paul Brook | case CSR_INT_EN:
|
1205 | 2a424990 | Paul Brook | return s->int_en;
|
1206 | 2a424990 | Paul Brook | case CSR_BYTE_TEST:
|
1207 | 2a424990 | Paul Brook | return 0x87654321; |
1208 | 2a424990 | Paul Brook | case CSR_FIFO_INT:
|
1209 | 2a424990 | Paul Brook | return s->fifo_int;
|
1210 | 2a424990 | Paul Brook | case CSR_RX_CFG:
|
1211 | 2a424990 | Paul Brook | return s->rx_cfg;
|
1212 | 2a424990 | Paul Brook | case CSR_TX_CFG:
|
1213 | 2a424990 | Paul Brook | return s->tx_cfg;
|
1214 | 2a424990 | Paul Brook | case CSR_HW_CFG:
|
1215 | 1248f8d4 | Evgeny Voevodin | return s->hw_cfg;
|
1216 | 2a424990 | Paul Brook | case CSR_RX_DP_CTRL:
|
1217 | 2a424990 | Paul Brook | return 0; |
1218 | 2a424990 | Paul Brook | case CSR_RX_FIFO_INF:
|
1219 | 2a424990 | Paul Brook | return (s->rx_status_fifo_used << 16) | (s->rx_fifo_used << 2); |
1220 | 2a424990 | Paul Brook | case CSR_TX_FIFO_INF:
|
1221 | 2a424990 | Paul Brook | return (s->tx_status_fifo_used << 16) |
1222 | 2a424990 | Paul Brook | | (s->tx_fifo_size - s->txp->fifo_used); |
1223 | 2a424990 | Paul Brook | case CSR_PMT_CTRL:
|
1224 | 2a424990 | Paul Brook | return s->pmt_ctrl;
|
1225 | 2a424990 | Paul Brook | case CSR_GPIO_CFG:
|
1226 | 2a424990 | Paul Brook | return s->gpio_cfg;
|
1227 | 209bf965 | Paul Brook | case CSR_GPT_CFG:
|
1228 | 209bf965 | Paul Brook | return s->gpt_cfg;
|
1229 | 209bf965 | Paul Brook | case CSR_GPT_CNT:
|
1230 | 209bf965 | Paul Brook | return ptimer_get_count(s->timer);
|
1231 | 2a424990 | Paul Brook | case CSR_WORD_SWAP:
|
1232 | 2a424990 | Paul Brook | return s->word_swap;
|
1233 | 2a424990 | Paul Brook | case CSR_FREE_RUN:
|
1234 | 74475455 | Paolo Bonzini | return (qemu_get_clock_ns(vm_clock) / 40) - s->free_timer_start; |
1235 | 2a424990 | Paul Brook | case CSR_RX_DROP:
|
1236 | 2a424990 | Paul Brook | /* TODO: Implement dropped frames counter. */
|
1237 | 2a424990 | Paul Brook | return 0; |
1238 | 2a424990 | Paul Brook | case CSR_MAC_CSR_CMD:
|
1239 | 2a424990 | Paul Brook | return s->mac_cmd;
|
1240 | 2a424990 | Paul Brook | case CSR_MAC_CSR_DATA:
|
1241 | 2a424990 | Paul Brook | return s->mac_data;
|
1242 | 2a424990 | Paul Brook | case CSR_AFC_CFG:
|
1243 | 2a424990 | Paul Brook | return s->afc_cfg;
|
1244 | 2a424990 | Paul Brook | case CSR_E2P_CMD:
|
1245 | 2a424990 | Paul Brook | return s->e2p_cmd;
|
1246 | 2a424990 | Paul Brook | case CSR_E2P_DATA:
|
1247 | 2a424990 | Paul Brook | return s->e2p_data;
|
1248 | 2a424990 | Paul Brook | } |
1249 | 2a424990 | Paul Brook | hw_error("lan9118_read: Bad reg 0x%x\n", (int)offset); |
1250 | 2a424990 | Paul Brook | return 0; |
1251 | 2a424990 | Paul Brook | } |
1252 | 2a424990 | Paul Brook | |
1253 | a8170e5e | Avi Kivity | static uint32_t lan9118_readw(void *opaque, hwaddr offset) |
1254 | 1248f8d4 | Evgeny Voevodin | { |
1255 | 1248f8d4 | Evgeny Voevodin | lan9118_state *s = (lan9118_state *)opaque; |
1256 | 1248f8d4 | Evgeny Voevodin | uint32_t val; |
1257 | 1248f8d4 | Evgeny Voevodin | |
1258 | 1248f8d4 | Evgeny Voevodin | if (s->read_word_prev_offset != (offset & ~0x3)) { |
1259 | 1248f8d4 | Evgeny Voevodin | /* New offset, reset word counter */
|
1260 | 1248f8d4 | Evgeny Voevodin | s->read_word_n = 0;
|
1261 | 1248f8d4 | Evgeny Voevodin | s->read_word_prev_offset = offset & ~0x3;
|
1262 | 1248f8d4 | Evgeny Voevodin | } |
1263 | 1248f8d4 | Evgeny Voevodin | |
1264 | 1248f8d4 | Evgeny Voevodin | s->read_word_n++; |
1265 | 1248f8d4 | Evgeny Voevodin | if (s->read_word_n == 1) { |
1266 | 1248f8d4 | Evgeny Voevodin | s->read_long = lan9118_readl(s, offset & ~3, 4); |
1267 | 1248f8d4 | Evgeny Voevodin | } else {
|
1268 | 1248f8d4 | Evgeny Voevodin | s->read_word_n = 0;
|
1269 | 1248f8d4 | Evgeny Voevodin | } |
1270 | 1248f8d4 | Evgeny Voevodin | |
1271 | 1248f8d4 | Evgeny Voevodin | if (offset & 2) { |
1272 | 1248f8d4 | Evgeny Voevodin | val = s->read_long >> 16;
|
1273 | 1248f8d4 | Evgeny Voevodin | } else {
|
1274 | 1248f8d4 | Evgeny Voevodin | val = s->read_long & 0xFFFF;
|
1275 | 1248f8d4 | Evgeny Voevodin | } |
1276 | 1248f8d4 | Evgeny Voevodin | |
1277 | 1248f8d4 | Evgeny Voevodin | //DPRINTF("Readw reg 0x%02x, val 0x%x\n", (int)offset, val);
|
1278 | 1248f8d4 | Evgeny Voevodin | return val;
|
1279 | 1248f8d4 | Evgeny Voevodin | } |
1280 | 1248f8d4 | Evgeny Voevodin | |
1281 | a8170e5e | Avi Kivity | static uint64_t lan9118_16bit_mode_read(void *opaque, hwaddr offset, |
1282 | 1248f8d4 | Evgeny Voevodin | unsigned size)
|
1283 | 1248f8d4 | Evgeny Voevodin | { |
1284 | 1248f8d4 | Evgeny Voevodin | switch (size) {
|
1285 | 1248f8d4 | Evgeny Voevodin | case 2: |
1286 | 1248f8d4 | Evgeny Voevodin | return lan9118_readw(opaque, offset);
|
1287 | 1248f8d4 | Evgeny Voevodin | case 4: |
1288 | 1248f8d4 | Evgeny Voevodin | return lan9118_readl(opaque, offset, size);
|
1289 | 1248f8d4 | Evgeny Voevodin | } |
1290 | 1248f8d4 | Evgeny Voevodin | |
1291 | 1248f8d4 | Evgeny Voevodin | hw_error("lan9118_read: Bad size 0x%x\n", size);
|
1292 | 1248f8d4 | Evgeny Voevodin | return 0; |
1293 | 1248f8d4 | Evgeny Voevodin | } |
1294 | 1248f8d4 | Evgeny Voevodin | |
1295 | f0cdd7a9 | Peter Maydell | static const MemoryRegionOps lan9118_mem_ops = { |
1296 | f0cdd7a9 | Peter Maydell | .read = lan9118_readl, |
1297 | f0cdd7a9 | Peter Maydell | .write = lan9118_writel, |
1298 | f0cdd7a9 | Peter Maydell | .endianness = DEVICE_NATIVE_ENDIAN, |
1299 | 2a424990 | Paul Brook | }; |
1300 | 2a424990 | Paul Brook | |
1301 | 1248f8d4 | Evgeny Voevodin | static const MemoryRegionOps lan9118_16bit_mem_ops = { |
1302 | 1248f8d4 | Evgeny Voevodin | .read = lan9118_16bit_mode_read, |
1303 | 1248f8d4 | Evgeny Voevodin | .write = lan9118_16bit_mode_write, |
1304 | 1248f8d4 | Evgeny Voevodin | .endianness = DEVICE_NATIVE_ENDIAN, |
1305 | 1248f8d4 | Evgeny Voevodin | }; |
1306 | 1248f8d4 | Evgeny Voevodin | |
1307 | 4e68f7a0 | Stefan Hajnoczi | static void lan9118_cleanup(NetClientState *nc) |
1308 | 2a424990 | Paul Brook | { |
1309 | cc1f0f45 | Jason Wang | lan9118_state *s = qemu_get_nic_opaque(nc); |
1310 | 2a424990 | Paul Brook | |
1311 | 83b9f88c | Mark McLoughlin | s->nic = NULL;
|
1312 | 2a424990 | Paul Brook | } |
1313 | 2a424990 | Paul Brook | |
1314 | 83b9f88c | Mark McLoughlin | static NetClientInfo net_lan9118_info = {
|
1315 | 2be64a68 | Laszlo Ersek | .type = NET_CLIENT_OPTIONS_KIND_NIC, |
1316 | 83b9f88c | Mark McLoughlin | .size = sizeof(NICState),
|
1317 | 83b9f88c | Mark McLoughlin | .can_receive = lan9118_can_receive, |
1318 | 83b9f88c | Mark McLoughlin | .receive = lan9118_receive, |
1319 | 83b9f88c | Mark McLoughlin | .cleanup = lan9118_cleanup, |
1320 | 83b9f88c | Mark McLoughlin | .link_status_changed = lan9118_set_link, |
1321 | 83b9f88c | Mark McLoughlin | }; |
1322 | 83b9f88c | Mark McLoughlin | |
1323 | 2a424990 | Paul Brook | static int lan9118_init1(SysBusDevice *dev) |
1324 | 2a424990 | Paul Brook | { |
1325 | 2a424990 | Paul Brook | lan9118_state *s = FROM_SYSBUS(lan9118_state, dev); |
1326 | 209bf965 | Paul Brook | QEMUBH *bh; |
1327 | 2a424990 | Paul Brook | int i;
|
1328 | 1248f8d4 | Evgeny Voevodin | const MemoryRegionOps *mem_ops =
|
1329 | 1248f8d4 | Evgeny Voevodin | s->mode_16bit ? &lan9118_16bit_mem_ops : &lan9118_mem_ops; |
1330 | 2a424990 | Paul Brook | |
1331 | 1248f8d4 | Evgeny Voevodin | memory_region_init_io(&s->mmio, mem_ops, s, "lan9118-mmio", 0x100); |
1332 | 750ecd44 | Avi Kivity | sysbus_init_mmio(dev, &s->mmio); |
1333 | 2a424990 | Paul Brook | sysbus_init_irq(dev, &s->irq); |
1334 | 2a424990 | Paul Brook | qemu_macaddr_default_if_unset(&s->conf.macaddr); |
1335 | 2a424990 | Paul Brook | |
1336 | 83b9f88c | Mark McLoughlin | s->nic = qemu_new_nic(&net_lan9118_info, &s->conf, |
1337 | f79f2bfc | Anthony Liguori | object_get_typename(OBJECT(dev)), dev->qdev.id, s); |
1338 | b356f76d | Jason Wang | qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a); |
1339 | 2a424990 | Paul Brook | s->eeprom[0] = 0xa5; |
1340 | 2a424990 | Paul Brook | for (i = 0; i < 6; i++) { |
1341 | 2a424990 | Paul Brook | s->eeprom[i + 1] = s->conf.macaddr.a[i];
|
1342 | 2a424990 | Paul Brook | } |
1343 | 2a424990 | Paul Brook | s->pmt_ctrl = 1;
|
1344 | 2a424990 | Paul Brook | s->txp = &s->tx_packet; |
1345 | 2a424990 | Paul Brook | |
1346 | 209bf965 | Paul Brook | bh = qemu_bh_new(lan9118_tick, s); |
1347 | 209bf965 | Paul Brook | s->timer = ptimer_init(bh); |
1348 | 209bf965 | Paul Brook | ptimer_set_freq(s->timer, 10000);
|
1349 | 209bf965 | Paul Brook | ptimer_set_limit(s->timer, 0xffff, 1); |
1350 | 209bf965 | Paul Brook | |
1351 | 2a424990 | Paul Brook | return 0; |
1352 | 2a424990 | Paul Brook | } |
1353 | 2a424990 | Paul Brook | |
1354 | 999e12bb | Anthony Liguori | static Property lan9118_properties[] = {
|
1355 | 999e12bb | Anthony Liguori | DEFINE_NIC_PROPERTIES(lan9118_state, conf), |
1356 | 1248f8d4 | Evgeny Voevodin | DEFINE_PROP_UINT32("mode_16bit", lan9118_state, mode_16bit, 0), |
1357 | 999e12bb | Anthony Liguori | DEFINE_PROP_END_OF_LIST(), |
1358 | 999e12bb | Anthony Liguori | }; |
1359 | 999e12bb | Anthony Liguori | |
1360 | 999e12bb | Anthony Liguori | static void lan9118_class_init(ObjectClass *klass, void *data) |
1361 | 999e12bb | Anthony Liguori | { |
1362 | 39bffca2 | Anthony Liguori | DeviceClass *dc = DEVICE_CLASS(klass); |
1363 | 999e12bb | Anthony Liguori | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
1364 | 999e12bb | Anthony Liguori | |
1365 | 999e12bb | Anthony Liguori | k->init = lan9118_init1; |
1366 | 39bffca2 | Anthony Liguori | dc->reset = lan9118_reset; |
1367 | 39bffca2 | Anthony Liguori | dc->props = lan9118_properties; |
1368 | 39bffca2 | Anthony Liguori | dc->vmsd = &vmstate_lan9118; |
1369 | 999e12bb | Anthony Liguori | } |
1370 | 999e12bb | Anthony Liguori | |
1371 | 8c43a6f0 | Andreas Färber | static const TypeInfo lan9118_info = { |
1372 | 39bffca2 | Anthony Liguori | .name = "lan9118",
|
1373 | 39bffca2 | Anthony Liguori | .parent = TYPE_SYS_BUS_DEVICE, |
1374 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(lan9118_state),
|
1375 | 39bffca2 | Anthony Liguori | .class_init = lan9118_class_init, |
1376 | 2a424990 | Paul Brook | }; |
1377 | 2a424990 | Paul Brook | |
1378 | 83f7d43a | Andreas Färber | static void lan9118_register_types(void) |
1379 | 2a424990 | Paul Brook | { |
1380 | 39bffca2 | Anthony Liguori | type_register_static(&lan9118_info); |
1381 | 2a424990 | Paul Brook | } |
1382 | 2a424990 | Paul Brook | |
1383 | 2a424990 | Paul Brook | /* Legacy helper function. Should go away when machine config files are
|
1384 | 2a424990 | Paul Brook | implemented. */
|
1385 | 2a424990 | Paul Brook | void lan9118_init(NICInfo *nd, uint32_t base, qemu_irq irq)
|
1386 | 2a424990 | Paul Brook | { |
1387 | 2a424990 | Paul Brook | DeviceState *dev; |
1388 | 2a424990 | Paul Brook | SysBusDevice *s; |
1389 | 2a424990 | Paul Brook | |
1390 | 2a424990 | Paul Brook | qemu_check_nic_model(nd, "lan9118");
|
1391 | 2a424990 | Paul Brook | dev = qdev_create(NULL, "lan9118"); |
1392 | 2a424990 | Paul Brook | qdev_set_nic_properties(dev, nd); |
1393 | 2a424990 | Paul Brook | qdev_init_nofail(dev); |
1394 | 1356b98d | Andreas Färber | s = SYS_BUS_DEVICE(dev); |
1395 | 2a424990 | Paul Brook | sysbus_mmio_map(s, 0, base);
|
1396 | 2a424990 | Paul Brook | sysbus_connect_irq(s, 0, irq);
|
1397 | 2a424990 | Paul Brook | } |
1398 | 2a424990 | Paul Brook | |
1399 | 83f7d43a | Andreas Färber | type_init(lan9118_register_types) |