Statistics
| Branch: | Revision:

root / hw / lan9118.c @ e927d487

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