root / hw / stellaris_enet.c @ ba5e5090
History | View | Annotate | Download (12.5 kB)
1 | eea589cc | pbrook | /*
|
---|---|---|---|
2 | eea589cc | pbrook | * Luminary Micro Stellaris Ethernet Controller
|
3 | eea589cc | pbrook | *
|
4 | eea589cc | pbrook | * Copyright (c) 2007 CodeSourcery.
|
5 | eea589cc | pbrook | * Written by Paul Brook
|
6 | eea589cc | pbrook | *
|
7 | eea589cc | pbrook | * This code is licenced under the GPL.
|
8 | eea589cc | pbrook | */
|
9 | a5580466 | Paul Brook | #include "sysbus.h" |
10 | eea589cc | pbrook | #include "net.h" |
11 | eea589cc | pbrook | #include <zlib.h> |
12 | eea589cc | pbrook | |
13 | eea589cc | pbrook | //#define DEBUG_STELLARIS_ENET 1
|
14 | eea589cc | pbrook | |
15 | eea589cc | pbrook | #ifdef DEBUG_STELLARIS_ENET
|
16 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
17 | 001faf32 | Blue Swirl | do { printf("stellaris_enet: " fmt , ## __VA_ARGS__); } while (0) |
18 | 001faf32 | Blue Swirl | #define BADF(fmt, ...) \
|
19 | 001faf32 | Blue Swirl | do { fprintf(stderr, "stellaris_enet: error: " fmt , ## __VA_ARGS__); exit(1);} while (0) |
20 | eea589cc | pbrook | #else
|
21 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) do {} while(0) |
22 | 001faf32 | Blue Swirl | #define BADF(fmt, ...) \
|
23 | 001faf32 | Blue Swirl | do { fprintf(stderr, "stellaris_enet: error: " fmt , ## __VA_ARGS__);} while (0) |
24 | eea589cc | pbrook | #endif
|
25 | eea589cc | pbrook | |
26 | eea589cc | pbrook | #define SE_INT_RX 0x01 |
27 | eea589cc | pbrook | #define SE_INT_TXER 0x02 |
28 | eea589cc | pbrook | #define SE_INT_TXEMP 0x04 |
29 | eea589cc | pbrook | #define SE_INT_FOV 0x08 |
30 | eea589cc | pbrook | #define SE_INT_RXER 0x10 |
31 | eea589cc | pbrook | #define SE_INT_MD 0x20 |
32 | eea589cc | pbrook | #define SE_INT_PHY 0x40 |
33 | eea589cc | pbrook | |
34 | eea589cc | pbrook | #define SE_RCTL_RXEN 0x01 |
35 | eea589cc | pbrook | #define SE_RCTL_AMUL 0x02 |
36 | eea589cc | pbrook | #define SE_RCTL_PRMS 0x04 |
37 | eea589cc | pbrook | #define SE_RCTL_BADCRC 0x08 |
38 | eea589cc | pbrook | #define SE_RCTL_RSTFIFO 0x10 |
39 | eea589cc | pbrook | |
40 | eea589cc | pbrook | #define SE_TCTL_TXEN 0x01 |
41 | eea589cc | pbrook | #define SE_TCTL_PADEN 0x02 |
42 | eea589cc | pbrook | #define SE_TCTL_CRC 0x04 |
43 | eea589cc | pbrook | #define SE_TCTL_DUPLEX 0x08 |
44 | eea589cc | pbrook | |
45 | eea589cc | pbrook | typedef struct { |
46 | a5580466 | Paul Brook | SysBusDevice busdev; |
47 | eea589cc | pbrook | uint32_t ris; |
48 | eea589cc | pbrook | uint32_t im; |
49 | eea589cc | pbrook | uint32_t rctl; |
50 | eea589cc | pbrook | uint32_t tctl; |
51 | eea589cc | pbrook | uint32_t thr; |
52 | eea589cc | pbrook | uint32_t mctl; |
53 | eea589cc | pbrook | uint32_t mdv; |
54 | eea589cc | pbrook | uint32_t mtxd; |
55 | eea589cc | pbrook | uint32_t mrxd; |
56 | eea589cc | pbrook | uint32_t np; |
57 | eea589cc | pbrook | int tx_frame_len;
|
58 | eea589cc | pbrook | int tx_fifo_len;
|
59 | eea589cc | pbrook | uint8_t tx_fifo[2048];
|
60 | eea589cc | pbrook | /* Real hardware has a 2k fifo, which works out to be at most 31 packets.
|
61 | eea589cc | pbrook | We implement a full 31 packet fifo. */
|
62 | eea589cc | pbrook | struct {
|
63 | eea589cc | pbrook | uint8_t data[2048];
|
64 | eea589cc | pbrook | int len;
|
65 | eea589cc | pbrook | } rx[31];
|
66 | eea589cc | pbrook | uint8_t *rx_fifo; |
67 | eea589cc | pbrook | int rx_fifo_len;
|
68 | eea589cc | pbrook | int next_packet;
|
69 | 8c9b63b9 | Mark McLoughlin | NICState *nic; |
70 | 540f006a | Gerd Hoffmann | NICConf conf; |
71 | eea589cc | pbrook | qemu_irq irq; |
72 | b946a153 | aliguori | int mmio_index;
|
73 | eea589cc | pbrook | } stellaris_enet_state; |
74 | eea589cc | pbrook | |
75 | eea589cc | pbrook | static void stellaris_enet_update(stellaris_enet_state *s) |
76 | eea589cc | pbrook | { |
77 | eea589cc | pbrook | qemu_set_irq(s->irq, (s->ris & s->im) != 0);
|
78 | eea589cc | pbrook | } |
79 | eea589cc | pbrook | |
80 | eea589cc | pbrook | /* TODO: Implement MAC address filtering. */
|
81 | 8c9b63b9 | Mark McLoughlin | static ssize_t stellaris_enet_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
82 | eea589cc | pbrook | { |
83 | 8c9b63b9 | Mark McLoughlin | stellaris_enet_state *s = DO_UPCAST(NICState, nc, nc)->opaque; |
84 | eea589cc | pbrook | int n;
|
85 | eea589cc | pbrook | uint8_t *p; |
86 | eea589cc | pbrook | uint32_t crc; |
87 | eea589cc | pbrook | |
88 | eea589cc | pbrook | if ((s->rctl & SE_RCTL_RXEN) == 0) |
89 | 4f1c942b | Mark McLoughlin | return -1; |
90 | eea589cc | pbrook | if (s->np >= 31) { |
91 | eea589cc | pbrook | DPRINTF("Packet dropped\n");
|
92 | 4f1c942b | Mark McLoughlin | return -1; |
93 | eea589cc | pbrook | } |
94 | eea589cc | pbrook | |
95 | eea589cc | pbrook | DPRINTF("Received packet len=%d\n", size);
|
96 | eea589cc | pbrook | n = s->next_packet + s->np; |
97 | eea589cc | pbrook | if (n >= 31) |
98 | eea589cc | pbrook | n -= 31;
|
99 | eea589cc | pbrook | s->np++; |
100 | eea589cc | pbrook | |
101 | eea589cc | pbrook | s->rx[n].len = size + 6;
|
102 | eea589cc | pbrook | p = s->rx[n].data; |
103 | eea589cc | pbrook | *(p++) = (size + 6);
|
104 | eea589cc | pbrook | *(p++) = (size + 6) >> 8; |
105 | eea589cc | pbrook | memcpy (p, buf, size); |
106 | eea589cc | pbrook | p += size; |
107 | eea589cc | pbrook | crc = crc32(~0, buf, size);
|
108 | eea589cc | pbrook | *(p++) = crc; |
109 | eea589cc | pbrook | *(p++) = crc >> 8;
|
110 | eea589cc | pbrook | *(p++) = crc >> 16;
|
111 | eea589cc | pbrook | *(p++) = crc >> 24;
|
112 | eea589cc | pbrook | /* Clear the remaining bytes in the last word. */
|
113 | eea589cc | pbrook | if ((size & 3) != 2) { |
114 | eea589cc | pbrook | memset(p, 0, (6 - size) & 3); |
115 | eea589cc | pbrook | } |
116 | eea589cc | pbrook | |
117 | eea589cc | pbrook | s->ris |= SE_INT_RX; |
118 | eea589cc | pbrook | stellaris_enet_update(s); |
119 | 4f1c942b | Mark McLoughlin | |
120 | 4f1c942b | Mark McLoughlin | return size;
|
121 | eea589cc | pbrook | } |
122 | eea589cc | pbrook | |
123 | 8c9b63b9 | Mark McLoughlin | static int stellaris_enet_can_receive(VLANClientState *nc) |
124 | eea589cc | pbrook | { |
125 | 8c9b63b9 | Mark McLoughlin | stellaris_enet_state *s = DO_UPCAST(NICState, nc, nc)->opaque; |
126 | eea589cc | pbrook | |
127 | eea589cc | pbrook | if ((s->rctl & SE_RCTL_RXEN) == 0) |
128 | eea589cc | pbrook | return 1; |
129 | eea589cc | pbrook | |
130 | eea589cc | pbrook | return (s->np < 31); |
131 | eea589cc | pbrook | } |
132 | eea589cc | pbrook | |
133 | c227f099 | Anthony Liguori | static uint32_t stellaris_enet_read(void *opaque, target_phys_addr_t offset) |
134 | eea589cc | pbrook | { |
135 | 57b452a8 | Paul Brook | stellaris_enet_state *s = (stellaris_enet_state *)opaque; |
136 | eea589cc | pbrook | uint32_t val; |
137 | eea589cc | pbrook | |
138 | eea589cc | pbrook | switch (offset) {
|
139 | eea589cc | pbrook | case 0x00: /* RIS */ |
140 | eea589cc | pbrook | DPRINTF("IRQ status %02x\n", s->ris);
|
141 | eea589cc | pbrook | return s->ris;
|
142 | eea589cc | pbrook | case 0x04: /* IM */ |
143 | eea589cc | pbrook | return s->im;
|
144 | eea589cc | pbrook | case 0x08: /* RCTL */ |
145 | eea589cc | pbrook | return s->rctl;
|
146 | eea589cc | pbrook | case 0x0c: /* TCTL */ |
147 | eea589cc | pbrook | return s->tctl;
|
148 | eea589cc | pbrook | case 0x10: /* DATA */ |
149 | eea589cc | pbrook | if (s->rx_fifo_len == 0) { |
150 | eea589cc | pbrook | if (s->np == 0) { |
151 | eea589cc | pbrook | BADF("RX underflow\n");
|
152 | eea589cc | pbrook | return 0; |
153 | eea589cc | pbrook | } |
154 | eea589cc | pbrook | s->rx_fifo_len = s->rx[s->next_packet].len; |
155 | eea589cc | pbrook | s->rx_fifo = s->rx[s->next_packet].data; |
156 | eea589cc | pbrook | DPRINTF("RX FIFO start packet len=%d\n", s->rx_fifo_len);
|
157 | eea589cc | pbrook | } |
158 | eea589cc | pbrook | val = s->rx_fifo[0] | (s->rx_fifo[1] << 8) | (s->rx_fifo[2] << 16) |
159 | eea589cc | pbrook | | (s->rx_fifo[3] << 24); |
160 | eea589cc | pbrook | s->rx_fifo += 4;
|
161 | eea589cc | pbrook | s->rx_fifo_len -= 4;
|
162 | eea589cc | pbrook | if (s->rx_fifo_len <= 0) { |
163 | eea589cc | pbrook | s->rx_fifo_len = 0;
|
164 | eea589cc | pbrook | s->next_packet++; |
165 | eea589cc | pbrook | if (s->next_packet >= 31) |
166 | eea589cc | pbrook | s->next_packet = 0;
|
167 | eea589cc | pbrook | s->np--; |
168 | eea589cc | pbrook | DPRINTF("RX done np=%d\n", s->np);
|
169 | eea589cc | pbrook | } |
170 | eea589cc | pbrook | return val;
|
171 | eea589cc | pbrook | case 0x14: /* IA0 */ |
172 | 540f006a | Gerd Hoffmann | return s->conf.macaddr.a[0] | (s->conf.macaddr.a[1] << 8) |
173 | 540f006a | Gerd Hoffmann | | (s->conf.macaddr.a[2] << 16) | (s->conf.macaddr.a[3] << 24); |
174 | eea589cc | pbrook | case 0x18: /* IA1 */ |
175 | 540f006a | Gerd Hoffmann | return s->conf.macaddr.a[4] | (s->conf.macaddr.a[5] << 8); |
176 | eea589cc | pbrook | case 0x1c: /* THR */ |
177 | eea589cc | pbrook | return s->thr;
|
178 | eea589cc | pbrook | case 0x20: /* MCTL */ |
179 | eea589cc | pbrook | return s->mctl;
|
180 | eea589cc | pbrook | case 0x24: /* MDV */ |
181 | eea589cc | pbrook | return s->mdv;
|
182 | eea589cc | pbrook | case 0x28: /* MADD */ |
183 | eea589cc | pbrook | return 0; |
184 | eea589cc | pbrook | case 0x2c: /* MTXD */ |
185 | eea589cc | pbrook | return s->mtxd;
|
186 | eea589cc | pbrook | case 0x30: /* MRXD */ |
187 | eea589cc | pbrook | return s->mrxd;
|
188 | eea589cc | pbrook | case 0x34: /* NP */ |
189 | eea589cc | pbrook | return s->np;
|
190 | eea589cc | pbrook | case 0x38: /* TR */ |
191 | eea589cc | pbrook | return 0; |
192 | eea589cc | pbrook | case 0x3c: /* Undocuented: Timestamp? */ |
193 | eea589cc | pbrook | return 0; |
194 | eea589cc | pbrook | default:
|
195 | 2ac71179 | Paul Brook | hw_error("stellaris_enet_read: Bad offset %x\n", (int)offset); |
196 | eea589cc | pbrook | return 0; |
197 | eea589cc | pbrook | } |
198 | eea589cc | pbrook | } |
199 | eea589cc | pbrook | |
200 | c227f099 | Anthony Liguori | static void stellaris_enet_write(void *opaque, target_phys_addr_t offset, |
201 | eea589cc | pbrook | uint32_t value) |
202 | eea589cc | pbrook | { |
203 | eea589cc | pbrook | stellaris_enet_state *s = (stellaris_enet_state *)opaque; |
204 | eea589cc | pbrook | |
205 | eea589cc | pbrook | switch (offset) {
|
206 | eea589cc | pbrook | case 0x00: /* IACK */ |
207 | eea589cc | pbrook | s->ris &= ~value; |
208 | eea589cc | pbrook | DPRINTF("IRQ ack %02x/%02x\n", value, s->ris);
|
209 | eea589cc | pbrook | stellaris_enet_update(s); |
210 | eea589cc | pbrook | /* Clearing TXER also resets the TX fifo. */
|
211 | eea589cc | pbrook | if (value & SE_INT_TXER)
|
212 | eea589cc | pbrook | s->tx_frame_len = -1;
|
213 | eea589cc | pbrook | break;
|
214 | eea589cc | pbrook | case 0x04: /* IM */ |
215 | eea589cc | pbrook | DPRINTF("IRQ mask %02x/%02x\n", value, s->ris);
|
216 | eea589cc | pbrook | s->im = value; |
217 | eea589cc | pbrook | stellaris_enet_update(s); |
218 | eea589cc | pbrook | break;
|
219 | eea589cc | pbrook | case 0x08: /* RCTL */ |
220 | eea589cc | pbrook | s->rctl = value; |
221 | eea589cc | pbrook | if (value & SE_RCTL_RSTFIFO) {
|
222 | eea589cc | pbrook | s->rx_fifo_len = 0;
|
223 | eea589cc | pbrook | s->np = 0;
|
224 | eea589cc | pbrook | stellaris_enet_update(s); |
225 | eea589cc | pbrook | } |
226 | eea589cc | pbrook | break;
|
227 | eea589cc | pbrook | case 0x0c: /* TCTL */ |
228 | eea589cc | pbrook | s->tctl = value; |
229 | eea589cc | pbrook | break;
|
230 | eea589cc | pbrook | case 0x10: /* DATA */ |
231 | eea589cc | pbrook | if (s->tx_frame_len == -1) { |
232 | eea589cc | pbrook | s->tx_frame_len = value & 0xffff;
|
233 | eea589cc | pbrook | if (s->tx_frame_len > 2032) { |
234 | eea589cc | pbrook | DPRINTF("TX frame too long (%d)\n", s->tx_frame_len);
|
235 | eea589cc | pbrook | s->tx_frame_len = 0;
|
236 | eea589cc | pbrook | s->ris |= SE_INT_TXER; |
237 | eea589cc | pbrook | stellaris_enet_update(s); |
238 | eea589cc | pbrook | } else {
|
239 | eea589cc | pbrook | DPRINTF("Start TX frame len=%d\n", s->tx_frame_len);
|
240 | eea589cc | pbrook | /* The value written does not include the ethernet header. */
|
241 | eea589cc | pbrook | s->tx_frame_len += 14;
|
242 | eea589cc | pbrook | if ((s->tctl & SE_TCTL_CRC) == 0) |
243 | eea589cc | pbrook | s->tx_frame_len += 4;
|
244 | eea589cc | pbrook | s->tx_fifo_len = 0;
|
245 | eea589cc | pbrook | s->tx_fifo[s->tx_fifo_len++] = value >> 16;
|
246 | eea589cc | pbrook | s->tx_fifo[s->tx_fifo_len++] = value >> 24;
|
247 | eea589cc | pbrook | } |
248 | eea589cc | pbrook | } else {
|
249 | eea589cc | pbrook | s->tx_fifo[s->tx_fifo_len++] = value; |
250 | eea589cc | pbrook | s->tx_fifo[s->tx_fifo_len++] = value >> 8;
|
251 | eea589cc | pbrook | s->tx_fifo[s->tx_fifo_len++] = value >> 16;
|
252 | eea589cc | pbrook | s->tx_fifo[s->tx_fifo_len++] = value >> 24;
|
253 | eea589cc | pbrook | if (s->tx_fifo_len >= s->tx_frame_len) {
|
254 | eea589cc | pbrook | /* We don't implement explicit CRC, so just chop it off. */
|
255 | eea589cc | pbrook | if ((s->tctl & SE_TCTL_CRC) == 0) |
256 | eea589cc | pbrook | s->tx_frame_len -= 4;
|
257 | eea589cc | pbrook | if ((s->tctl & SE_TCTL_PADEN) && s->tx_frame_len < 60) { |
258 | eea589cc | pbrook | memset(&s->tx_fifo[s->tx_frame_len], 0, 60 - s->tx_frame_len); |
259 | eea589cc | pbrook | s->tx_fifo_len = 60;
|
260 | eea589cc | pbrook | } |
261 | 8c9b63b9 | Mark McLoughlin | qemu_send_packet(&s->nic->nc, s->tx_fifo, s->tx_frame_len); |
262 | eea589cc | pbrook | s->tx_frame_len = -1;
|
263 | eea589cc | pbrook | s->ris |= SE_INT_TXEMP; |
264 | eea589cc | pbrook | stellaris_enet_update(s); |
265 | eea589cc | pbrook | DPRINTF("Done TX\n");
|
266 | eea589cc | pbrook | } |
267 | eea589cc | pbrook | } |
268 | eea589cc | pbrook | break;
|
269 | eea589cc | pbrook | case 0x14: /* IA0 */ |
270 | 540f006a | Gerd Hoffmann | s->conf.macaddr.a[0] = value;
|
271 | 540f006a | Gerd Hoffmann | s->conf.macaddr.a[1] = value >> 8; |
272 | 540f006a | Gerd Hoffmann | s->conf.macaddr.a[2] = value >> 16; |
273 | 540f006a | Gerd Hoffmann | s->conf.macaddr.a[3] = value >> 24; |
274 | eea589cc | pbrook | break;
|
275 | eea589cc | pbrook | case 0x18: /* IA1 */ |
276 | 540f006a | Gerd Hoffmann | s->conf.macaddr.a[4] = value;
|
277 | 540f006a | Gerd Hoffmann | s->conf.macaddr.a[5] = value >> 8; |
278 | eea589cc | pbrook | break;
|
279 | eea589cc | pbrook | case 0x1c: /* THR */ |
280 | eea589cc | pbrook | s->thr = value; |
281 | eea589cc | pbrook | break;
|
282 | eea589cc | pbrook | case 0x20: /* MCTL */ |
283 | eea589cc | pbrook | s->mctl = value; |
284 | eea589cc | pbrook | break;
|
285 | eea589cc | pbrook | case 0x24: /* MDV */ |
286 | eea589cc | pbrook | s->mdv = value; |
287 | eea589cc | pbrook | break;
|
288 | eea589cc | pbrook | case 0x28: /* MADD */ |
289 | eea589cc | pbrook | /* ignored. */
|
290 | eea589cc | pbrook | break;
|
291 | eea589cc | pbrook | case 0x2c: /* MTXD */ |
292 | eea589cc | pbrook | s->mtxd = value & 0xff;
|
293 | eea589cc | pbrook | break;
|
294 | eea589cc | pbrook | case 0x30: /* MRXD */ |
295 | eea589cc | pbrook | case 0x34: /* NP */ |
296 | eea589cc | pbrook | case 0x38: /* TR */ |
297 | eea589cc | pbrook | /* Ignored. */
|
298 | eea589cc | pbrook | case 0x3c: /* Undocuented: Timestamp? */ |
299 | eea589cc | pbrook | /* Ignored. */
|
300 | eea589cc | pbrook | break;
|
301 | eea589cc | pbrook | default:
|
302 | 2ac71179 | Paul Brook | hw_error("stellaris_enet_write: Bad offset %x\n", (int)offset); |
303 | eea589cc | pbrook | } |
304 | eea589cc | pbrook | } |
305 | eea589cc | pbrook | |
306 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const stellaris_enet_readfn[] = { |
307 | eea589cc | pbrook | stellaris_enet_read, |
308 | eea589cc | pbrook | stellaris_enet_read, |
309 | eea589cc | pbrook | stellaris_enet_read |
310 | eea589cc | pbrook | }; |
311 | eea589cc | pbrook | |
312 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const stellaris_enet_writefn[] = { |
313 | eea589cc | pbrook | stellaris_enet_write, |
314 | eea589cc | pbrook | stellaris_enet_write, |
315 | eea589cc | pbrook | stellaris_enet_write |
316 | eea589cc | pbrook | }; |
317 | eea589cc | pbrook | static void stellaris_enet_reset(stellaris_enet_state *s) |
318 | eea589cc | pbrook | { |
319 | eea589cc | pbrook | s->mdv = 0x80;
|
320 | eea589cc | pbrook | s->rctl = SE_RCTL_BADCRC; |
321 | eea589cc | pbrook | s->im = SE_INT_PHY | SE_INT_MD | SE_INT_RXER | SE_INT_FOV | SE_INT_TXEMP |
322 | eea589cc | pbrook | | SE_INT_TXER | SE_INT_RX; |
323 | eea589cc | pbrook | s->thr = 0x3f;
|
324 | eea589cc | pbrook | s->tx_frame_len = -1;
|
325 | eea589cc | pbrook | } |
326 | eea589cc | pbrook | |
327 | 23e39294 | pbrook | static void stellaris_enet_save(QEMUFile *f, void *opaque) |
328 | 23e39294 | pbrook | { |
329 | 23e39294 | pbrook | stellaris_enet_state *s = (stellaris_enet_state *)opaque; |
330 | 23e39294 | pbrook | int i;
|
331 | 23e39294 | pbrook | |
332 | 23e39294 | pbrook | qemu_put_be32(f, s->ris); |
333 | 23e39294 | pbrook | qemu_put_be32(f, s->im); |
334 | 23e39294 | pbrook | qemu_put_be32(f, s->rctl); |
335 | 23e39294 | pbrook | qemu_put_be32(f, s->tctl); |
336 | 23e39294 | pbrook | qemu_put_be32(f, s->thr); |
337 | 23e39294 | pbrook | qemu_put_be32(f, s->mctl); |
338 | 23e39294 | pbrook | qemu_put_be32(f, s->mdv); |
339 | 23e39294 | pbrook | qemu_put_be32(f, s->mtxd); |
340 | 23e39294 | pbrook | qemu_put_be32(f, s->mrxd); |
341 | 23e39294 | pbrook | qemu_put_be32(f, s->np); |
342 | 23e39294 | pbrook | qemu_put_be32(f, s->tx_frame_len); |
343 | 23e39294 | pbrook | qemu_put_be32(f, s->tx_fifo_len); |
344 | 23e39294 | pbrook | qemu_put_buffer(f, s->tx_fifo, sizeof(s->tx_fifo));
|
345 | 23e39294 | pbrook | for (i = 0; i < 31; i++) { |
346 | 23e39294 | pbrook | qemu_put_be32(f, s->rx[i].len); |
347 | 23e39294 | pbrook | qemu_put_buffer(f, s->rx[i].data, sizeof(s->rx[i].data));
|
348 | 23e39294 | pbrook | |
349 | 23e39294 | pbrook | } |
350 | 23e39294 | pbrook | qemu_put_be32(f, s->next_packet); |
351 | 23e39294 | pbrook | qemu_put_be32(f, s->rx_fifo - s->rx[s->next_packet].data); |
352 | 23e39294 | pbrook | qemu_put_be32(f, s->rx_fifo_len); |
353 | 23e39294 | pbrook | } |
354 | 23e39294 | pbrook | |
355 | 23e39294 | pbrook | static int stellaris_enet_load(QEMUFile *f, void *opaque, int version_id) |
356 | 23e39294 | pbrook | { |
357 | 23e39294 | pbrook | stellaris_enet_state *s = (stellaris_enet_state *)opaque; |
358 | 23e39294 | pbrook | int i;
|
359 | 23e39294 | pbrook | |
360 | 23e39294 | pbrook | if (version_id != 1) |
361 | 23e39294 | pbrook | return -EINVAL;
|
362 | 23e39294 | pbrook | |
363 | 23e39294 | pbrook | s->ris = qemu_get_be32(f); |
364 | 23e39294 | pbrook | s->im = qemu_get_be32(f); |
365 | 23e39294 | pbrook | s->rctl = qemu_get_be32(f); |
366 | 23e39294 | pbrook | s->tctl = qemu_get_be32(f); |
367 | 23e39294 | pbrook | s->thr = qemu_get_be32(f); |
368 | 23e39294 | pbrook | s->mctl = qemu_get_be32(f); |
369 | 23e39294 | pbrook | s->mdv = qemu_get_be32(f); |
370 | 23e39294 | pbrook | s->mtxd = qemu_get_be32(f); |
371 | 23e39294 | pbrook | s->mrxd = qemu_get_be32(f); |
372 | 23e39294 | pbrook | s->np = qemu_get_be32(f); |
373 | 23e39294 | pbrook | s->tx_frame_len = qemu_get_be32(f); |
374 | 23e39294 | pbrook | s->tx_fifo_len = qemu_get_be32(f); |
375 | 23e39294 | pbrook | qemu_get_buffer(f, s->tx_fifo, sizeof(s->tx_fifo));
|
376 | 23e39294 | pbrook | for (i = 0; i < 31; i++) { |
377 | 23e39294 | pbrook | s->rx[i].len = qemu_get_be32(f); |
378 | 23e39294 | pbrook | qemu_get_buffer(f, s->rx[i].data, sizeof(s->rx[i].data));
|
379 | 23e39294 | pbrook | |
380 | 23e39294 | pbrook | } |
381 | 23e39294 | pbrook | s->next_packet = qemu_get_be32(f); |
382 | 23e39294 | pbrook | s->rx_fifo = s->rx[s->next_packet].data + qemu_get_be32(f); |
383 | 23e39294 | pbrook | s->rx_fifo_len = qemu_get_be32(f); |
384 | 23e39294 | pbrook | |
385 | 23e39294 | pbrook | return 0; |
386 | 23e39294 | pbrook | } |
387 | 23e39294 | pbrook | |
388 | 8c9b63b9 | Mark McLoughlin | static void stellaris_enet_cleanup(VLANClientState *nc) |
389 | b946a153 | aliguori | { |
390 | 8c9b63b9 | Mark McLoughlin | stellaris_enet_state *s = DO_UPCAST(NICState, nc, nc)->opaque; |
391 | b946a153 | aliguori | |
392 | b946a153 | aliguori | unregister_savevm("stellaris_enet", s);
|
393 | b946a153 | aliguori | |
394 | b946a153 | aliguori | cpu_unregister_io_memory(s->mmio_index); |
395 | b946a153 | aliguori | |
396 | b946a153 | aliguori | qemu_free(s); |
397 | b946a153 | aliguori | } |
398 | b946a153 | aliguori | |
399 | 8c9b63b9 | Mark McLoughlin | static NetClientInfo net_stellaris_enet_info = {
|
400 | 8c9b63b9 | Mark McLoughlin | .type = NET_CLIENT_TYPE_NIC, |
401 | 8c9b63b9 | Mark McLoughlin | .size = sizeof(NICState),
|
402 | 8c9b63b9 | Mark McLoughlin | .can_receive = stellaris_enet_can_receive, |
403 | 8c9b63b9 | Mark McLoughlin | .receive = stellaris_enet_receive, |
404 | 8c9b63b9 | Mark McLoughlin | .cleanup = stellaris_enet_cleanup, |
405 | 8c9b63b9 | Mark McLoughlin | }; |
406 | 8c9b63b9 | Mark McLoughlin | |
407 | 81a322d4 | Gerd Hoffmann | static int stellaris_enet_init(SysBusDevice *dev) |
408 | eea589cc | pbrook | { |
409 | a5580466 | Paul Brook | stellaris_enet_state *s = FROM_SYSBUS(stellaris_enet_state, dev); |
410 | eea589cc | pbrook | |
411 | 1eed09cb | Avi Kivity | s->mmio_index = cpu_register_io_memory(stellaris_enet_readfn, |
412 | b946a153 | aliguori | stellaris_enet_writefn, s); |
413 | a5580466 | Paul Brook | sysbus_init_mmio(dev, 0x1000, s->mmio_index);
|
414 | a5580466 | Paul Brook | sysbus_init_irq(dev, &s->irq); |
415 | 540f006a | Gerd Hoffmann | qemu_macaddr_default_if_unset(&s->conf.macaddr); |
416 | a5580466 | Paul Brook | |
417 | 8c9b63b9 | Mark McLoughlin | s->nic = qemu_new_nic(&net_stellaris_enet_info, &s->conf, |
418 | 8c9b63b9 | Mark McLoughlin | dev->qdev.info->name, dev->qdev.id, s); |
419 | 8c9b63b9 | Mark McLoughlin | qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a); |
420 | eea589cc | pbrook | |
421 | eea589cc | pbrook | stellaris_enet_reset(s); |
422 | 23e39294 | pbrook | register_savevm("stellaris_enet", -1, 1, |
423 | 23e39294 | pbrook | stellaris_enet_save, stellaris_enet_load, s); |
424 | 81a322d4 | Gerd Hoffmann | return 0; |
425 | eea589cc | pbrook | } |
426 | a5580466 | Paul Brook | |
427 | 540f006a | Gerd Hoffmann | static SysBusDeviceInfo stellaris_enet_info = {
|
428 | 540f006a | Gerd Hoffmann | .init = stellaris_enet_init, |
429 | 540f006a | Gerd Hoffmann | .qdev.name = "stellaris_enet",
|
430 | 540f006a | Gerd Hoffmann | .qdev.size = sizeof(stellaris_enet_state),
|
431 | 540f006a | Gerd Hoffmann | .qdev.props = (Property[]) { |
432 | 540f006a | Gerd Hoffmann | DEFINE_NIC_PROPERTIES(stellaris_enet_state, conf), |
433 | 540f006a | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
434 | 540f006a | Gerd Hoffmann | } |
435 | 540f006a | Gerd Hoffmann | }; |
436 | 540f006a | Gerd Hoffmann | |
437 | a5580466 | Paul Brook | static void stellaris_enet_register_devices(void) |
438 | a5580466 | Paul Brook | { |
439 | 540f006a | Gerd Hoffmann | sysbus_register_withprop(&stellaris_enet_info); |
440 | a5580466 | Paul Brook | } |
441 | a5580466 | Paul Brook | |
442 | a5580466 | Paul Brook | device_init(stellaris_enet_register_devices) |