2 * Luminary Micro Stellaris Ethernet Controller
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
13 //#define DEBUG_STELLARIS_ENET 1
15 #ifdef DEBUG_STELLARIS_ENET
16 #define DPRINTF(fmt, ...) \
17 do { printf("stellaris_enet: " fmt , ## __VA_ARGS__); } while (0)
18 #define BADF(fmt, ...) \
19 do { fprintf(stderr, "stellaris_enet: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
21 #define DPRINTF(fmt, ...) do {} while(0)
22 #define BADF(fmt, ...) \
23 do { fprintf(stderr, "stellaris_enet: error: " fmt , ## __VA_ARGS__);} while (0)
26 #define SE_INT_RX 0x01
27 #define SE_INT_TXER 0x02
28 #define SE_INT_TXEMP 0x04
29 #define SE_INT_FOV 0x08
30 #define SE_INT_RXER 0x10
31 #define SE_INT_MD 0x20
32 #define SE_INT_PHY 0x40
34 #define SE_RCTL_RXEN 0x01
35 #define SE_RCTL_AMUL 0x02
36 #define SE_RCTL_PRMS 0x04
37 #define SE_RCTL_BADCRC 0x08
38 #define SE_RCTL_RSTFIFO 0x10
40 #define SE_TCTL_TXEN 0x01
41 #define SE_TCTL_PADEN 0x02
42 #define SE_TCTL_CRC 0x04
43 #define SE_TCTL_DUPLEX 0x08
59 uint8_t tx_fifo
[2048];
60 /* Real hardware has a 2k fifo, which works out to be at most 31 packets.
61 We implement a full 31 packet fifo. */
73 } stellaris_enet_state
;
75 static void stellaris_enet_update(stellaris_enet_state
*s
)
77 qemu_set_irq(s
->irq
, (s
->ris
& s
->im
) != 0);
80 /* TODO: Implement MAC address filtering. */
81 static ssize_t
stellaris_enet_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
83 stellaris_enet_state
*s
= vc
->opaque
;
88 if ((s
->rctl
& SE_RCTL_RXEN
) == 0)
91 DPRINTF("Packet dropped\n");
95 DPRINTF("Received packet len=%d\n", size
);
96 n
= s
->next_packet
+ s
->np
;
101 s
->rx
[n
].len
= size
+ 6;
104 *(p
++) = (size
+ 6) >> 8;
105 memcpy (p
, buf
, size
);
107 crc
= crc32(~0, buf
, size
);
112 /* Clear the remaining bytes in the last word. */
113 if ((size
& 3) != 2) {
114 memset(p
, 0, (6 - size
) & 3);
118 stellaris_enet_update(s
);
123 static int stellaris_enet_can_receive(VLANClientState
*vc
)
125 stellaris_enet_state
*s
= vc
->opaque
;
127 if ((s
->rctl
& SE_RCTL_RXEN
) == 0)
133 static uint32_t stellaris_enet_read(void *opaque
, target_phys_addr_t offset
)
135 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
140 DPRINTF("IRQ status %02x\n", s
->ris
);
144 case 0x08: /* RCTL */
146 case 0x0c: /* TCTL */
148 case 0x10: /* DATA */
149 if (s
->rx_fifo_len
== 0) {
151 BADF("RX underflow\n");
154 s
->rx_fifo_len
= s
->rx
[s
->next_packet
].len
;
155 s
->rx_fifo
= s
->rx
[s
->next_packet
].data
;
156 DPRINTF("RX FIFO start packet len=%d\n", s
->rx_fifo_len
);
158 val
= s
->rx_fifo
[0] | (s
->rx_fifo
[1] << 8) | (s
->rx_fifo
[2] << 16)
159 | (s
->rx_fifo
[3] << 24);
162 if (s
->rx_fifo_len
<= 0) {
165 if (s
->next_packet
>= 31)
168 DPRINTF("RX done np=%d\n", s
->np
);
172 return s
->macaddr
[0] | (s
->macaddr
[1] << 8)
173 | (s
->macaddr
[2] << 16) | (s
->macaddr
[3] << 24);
175 return s
->macaddr
[4] | (s
->macaddr
[5] << 8);
178 case 0x20: /* MCTL */
182 case 0x28: /* MADD */
184 case 0x2c: /* MTXD */
186 case 0x30: /* MRXD */
192 case 0x3c: /* Undocuented: Timestamp? */
195 hw_error("stellaris_enet_read: Bad offset %x\n", (int)offset
);
200 static void stellaris_enet_write(void *opaque
, target_phys_addr_t offset
,
203 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
206 case 0x00: /* IACK */
208 DPRINTF("IRQ ack %02x/%02x\n", value
, s
->ris
);
209 stellaris_enet_update(s
);
210 /* Clearing TXER also resets the TX fifo. */
211 if (value
& SE_INT_TXER
)
212 s
->tx_frame_len
= -1;
215 DPRINTF("IRQ mask %02x/%02x\n", value
, s
->ris
);
217 stellaris_enet_update(s
);
219 case 0x08: /* RCTL */
221 if (value
& SE_RCTL_RSTFIFO
) {
224 stellaris_enet_update(s
);
227 case 0x0c: /* TCTL */
230 case 0x10: /* DATA */
231 if (s
->tx_frame_len
== -1) {
232 s
->tx_frame_len
= value
& 0xffff;
233 if (s
->tx_frame_len
> 2032) {
234 DPRINTF("TX frame too long (%d)\n", s
->tx_frame_len
);
236 s
->ris
|= SE_INT_TXER
;
237 stellaris_enet_update(s
);
239 DPRINTF("Start TX frame len=%d\n", s
->tx_frame_len
);
240 /* The value written does not include the ethernet header. */
241 s
->tx_frame_len
+= 14;
242 if ((s
->tctl
& SE_TCTL_CRC
) == 0)
243 s
->tx_frame_len
+= 4;
245 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 16;
246 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 24;
249 s
->tx_fifo
[s
->tx_fifo_len
++] = value
;
250 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 8;
251 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 16;
252 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 24;
253 if (s
->tx_fifo_len
>= s
->tx_frame_len
) {
254 /* We don't implement explicit CRC, so just chop it off. */
255 if ((s
->tctl
& SE_TCTL_CRC
) == 0)
256 s
->tx_frame_len
-= 4;
257 if ((s
->tctl
& SE_TCTL_PADEN
) && s
->tx_frame_len
< 60) {
258 memset(&s
->tx_fifo
[s
->tx_frame_len
], 0, 60 - s
->tx_frame_len
);
261 qemu_send_packet(s
->vc
, s
->tx_fifo
, s
->tx_frame_len
);
262 s
->tx_frame_len
= -1;
263 s
->ris
|= SE_INT_TXEMP
;
264 stellaris_enet_update(s
);
265 DPRINTF("Done TX\n");
270 s
->macaddr
[0] = value
;
271 s
->macaddr
[1] = value
>> 8;
272 s
->macaddr
[2] = value
>> 16;
273 s
->macaddr
[3] = value
>> 24;
276 s
->macaddr
[4] = value
;
277 s
->macaddr
[5] = value
>> 8;
282 case 0x20: /* MCTL */
288 case 0x28: /* MADD */
291 case 0x2c: /* MTXD */
292 s
->mtxd
= value
& 0xff;
294 case 0x30: /* MRXD */
298 case 0x3c: /* Undocuented: Timestamp? */
302 hw_error("stellaris_enet_write: Bad offset %x\n", (int)offset
);
306 static CPUReadMemoryFunc
* const stellaris_enet_readfn
[] = {
312 static CPUWriteMemoryFunc
* const stellaris_enet_writefn
[] = {
313 stellaris_enet_write
,
314 stellaris_enet_write
,
317 static void stellaris_enet_reset(stellaris_enet_state
*s
)
320 s
->rctl
= SE_RCTL_BADCRC
;
321 s
->im
= SE_INT_PHY
| SE_INT_MD
| SE_INT_RXER
| SE_INT_FOV
| SE_INT_TXEMP
322 | SE_INT_TXER
| SE_INT_RX
;
324 s
->tx_frame_len
= -1;
327 static void stellaris_enet_save(QEMUFile
*f
, void *opaque
)
329 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
332 qemu_put_be32(f
, s
->ris
);
333 qemu_put_be32(f
, s
->im
);
334 qemu_put_be32(f
, s
->rctl
);
335 qemu_put_be32(f
, s
->tctl
);
336 qemu_put_be32(f
, s
->thr
);
337 qemu_put_be32(f
, s
->mctl
);
338 qemu_put_be32(f
, s
->mdv
);
339 qemu_put_be32(f
, s
->mtxd
);
340 qemu_put_be32(f
, s
->mrxd
);
341 qemu_put_be32(f
, s
->np
);
342 qemu_put_be32(f
, s
->tx_frame_len
);
343 qemu_put_be32(f
, s
->tx_fifo_len
);
344 qemu_put_buffer(f
, s
->tx_fifo
, sizeof(s
->tx_fifo
));
345 for (i
= 0; i
< 31; i
++) {
346 qemu_put_be32(f
, s
->rx
[i
].len
);
347 qemu_put_buffer(f
, s
->rx
[i
].data
, sizeof(s
->rx
[i
].data
));
350 qemu_put_be32(f
, s
->next_packet
);
351 qemu_put_be32(f
, s
->rx_fifo
- s
->rx
[s
->next_packet
].data
);
352 qemu_put_be32(f
, s
->rx_fifo_len
);
355 static int stellaris_enet_load(QEMUFile
*f
, void *opaque
, int version_id
)
357 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
363 s
->ris
= qemu_get_be32(f
);
364 s
->im
= qemu_get_be32(f
);
365 s
->rctl
= qemu_get_be32(f
);
366 s
->tctl
= qemu_get_be32(f
);
367 s
->thr
= qemu_get_be32(f
);
368 s
->mctl
= qemu_get_be32(f
);
369 s
->mdv
= qemu_get_be32(f
);
370 s
->mtxd
= qemu_get_be32(f
);
371 s
->mrxd
= qemu_get_be32(f
);
372 s
->np
= qemu_get_be32(f
);
373 s
->tx_frame_len
= qemu_get_be32(f
);
374 s
->tx_fifo_len
= qemu_get_be32(f
);
375 qemu_get_buffer(f
, s
->tx_fifo
, sizeof(s
->tx_fifo
));
376 for (i
= 0; i
< 31; i
++) {
377 s
->rx
[i
].len
= qemu_get_be32(f
);
378 qemu_get_buffer(f
, s
->rx
[i
].data
, sizeof(s
->rx
[i
].data
));
381 s
->next_packet
= qemu_get_be32(f
);
382 s
->rx_fifo
= s
->rx
[s
->next_packet
].data
+ qemu_get_be32(f
);
383 s
->rx_fifo_len
= qemu_get_be32(f
);
388 static void stellaris_enet_cleanup(VLANClientState
*vc
)
390 stellaris_enet_state
*s
= vc
->opaque
;
392 unregister_savevm("stellaris_enet", s
);
394 cpu_unregister_io_memory(s
->mmio_index
);
399 static int stellaris_enet_init(SysBusDevice
*dev
)
401 stellaris_enet_state
*s
= FROM_SYSBUS(stellaris_enet_state
, dev
);
403 s
->mmio_index
= cpu_register_io_memory(stellaris_enet_readfn
,
404 stellaris_enet_writefn
, s
);
405 sysbus_init_mmio(dev
, 0x1000, s
->mmio_index
);
406 sysbus_init_irq(dev
, &s
->irq
);
407 qdev_get_macaddr(&dev
->qdev
, s
->macaddr
);
409 s
->vc
= qdev_get_vlan_client(&dev
->qdev
,
410 stellaris_enet_can_receive
,
411 stellaris_enet_receive
, NULL
,
412 stellaris_enet_cleanup
, s
);
413 qemu_format_nic_info_str(s
->vc
, s
->macaddr
);
415 stellaris_enet_reset(s
);
416 register_savevm("stellaris_enet", -1, 1,
417 stellaris_enet_save
, stellaris_enet_load
, s
);
421 static void stellaris_enet_register_devices(void)
423 sysbus_register_dev("stellaris_enet", sizeof(stellaris_enet_state
),
424 stellaris_enet_init
);
427 device_init(stellaris_enet_register_devices
)