2 * Luminary Micro Stellaris Ethernet Controller
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed 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
45 #define TYPE_STELLARIS_ENET "stellaris_enet"
46 #define STELLARIS_ENET(obj) \
47 OBJECT_CHECK(stellaris_enet_state, (obj), TYPE_STELLARIS_ENET)
50 SysBusDevice parent_obj
;
64 uint8_t tx_fifo
[2048];
65 /* Real hardware has a 2k fifo, which works out to be at most 31 packets.
66 We implement a full 31 packet fifo. */
78 } stellaris_enet_state
;
80 static void stellaris_enet_update(stellaris_enet_state
*s
)
82 qemu_set_irq(s
->irq
, (s
->ris
& s
->im
) != 0);
85 /* TODO: Implement MAC address filtering. */
86 static ssize_t
stellaris_enet_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
88 stellaris_enet_state
*s
= qemu_get_nic_opaque(nc
);
93 if ((s
->rctl
& SE_RCTL_RXEN
) == 0)
96 DPRINTF("Packet dropped\n");
100 DPRINTF("Received packet len=%d\n", size
);
101 n
= s
->next_packet
+ s
->np
;
106 s
->rx
[n
].len
= size
+ 6;
109 *(p
++) = (size
+ 6) >> 8;
110 memcpy (p
, buf
, size
);
112 crc
= crc32(~0, buf
, size
);
117 /* Clear the remaining bytes in the last word. */
118 if ((size
& 3) != 2) {
119 memset(p
, 0, (6 - size
) & 3);
123 stellaris_enet_update(s
);
128 static int stellaris_enet_can_receive(NetClientState
*nc
)
130 stellaris_enet_state
*s
= qemu_get_nic_opaque(nc
);
132 if ((s
->rctl
& SE_RCTL_RXEN
) == 0)
138 static uint64_t stellaris_enet_read(void *opaque
, hwaddr offset
,
141 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
146 DPRINTF("IRQ status %02x\n", s
->ris
);
150 case 0x08: /* RCTL */
152 case 0x0c: /* TCTL */
154 case 0x10: /* DATA */
155 if (s
->rx_fifo_len
== 0) {
157 BADF("RX underflow\n");
160 s
->rx_fifo_len
= s
->rx
[s
->next_packet
].len
;
161 s
->rx_fifo
= s
->rx
[s
->next_packet
].data
;
162 DPRINTF("RX FIFO start packet len=%d\n", s
->rx_fifo_len
);
164 val
= s
->rx_fifo
[0] | (s
->rx_fifo
[1] << 8) | (s
->rx_fifo
[2] << 16)
165 | (s
->rx_fifo
[3] << 24);
168 if (s
->rx_fifo_len
<= 0) {
171 if (s
->next_packet
>= 31)
174 DPRINTF("RX done np=%d\n", s
->np
);
178 return s
->conf
.macaddr
.a
[0] | (s
->conf
.macaddr
.a
[1] << 8)
179 | (s
->conf
.macaddr
.a
[2] << 16)
180 | ((uint32_t)s
->conf
.macaddr
.a
[3] << 24);
182 return s
->conf
.macaddr
.a
[4] | (s
->conf
.macaddr
.a
[5] << 8);
185 case 0x20: /* MCTL */
189 case 0x28: /* MADD */
191 case 0x2c: /* MTXD */
193 case 0x30: /* MRXD */
199 case 0x3c: /* Undocuented: Timestamp? */
202 hw_error("stellaris_enet_read: Bad offset %x\n", (int)offset
);
207 static void stellaris_enet_write(void *opaque
, hwaddr offset
,
208 uint64_t value
, unsigned size
)
210 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
213 case 0x00: /* IACK */
215 DPRINTF("IRQ ack %02x/%02x\n", value
, s
->ris
);
216 stellaris_enet_update(s
);
217 /* Clearing TXER also resets the TX fifo. */
218 if (value
& SE_INT_TXER
)
219 s
->tx_frame_len
= -1;
222 DPRINTF("IRQ mask %02x/%02x\n", value
, s
->ris
);
224 stellaris_enet_update(s
);
226 case 0x08: /* RCTL */
228 if (value
& SE_RCTL_RSTFIFO
) {
231 stellaris_enet_update(s
);
234 case 0x0c: /* TCTL */
237 case 0x10: /* DATA */
238 if (s
->tx_frame_len
== -1) {
239 s
->tx_frame_len
= value
& 0xffff;
240 if (s
->tx_frame_len
> 2032) {
241 DPRINTF("TX frame too long (%d)\n", s
->tx_frame_len
);
243 s
->ris
|= SE_INT_TXER
;
244 stellaris_enet_update(s
);
246 DPRINTF("Start TX frame len=%d\n", s
->tx_frame_len
);
247 /* The value written does not include the ethernet header. */
248 s
->tx_frame_len
+= 14;
249 if ((s
->tctl
& SE_TCTL_CRC
) == 0)
250 s
->tx_frame_len
+= 4;
252 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 16;
253 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 24;
256 s
->tx_fifo
[s
->tx_fifo_len
++] = value
;
257 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 8;
258 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 16;
259 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 24;
260 if (s
->tx_fifo_len
>= s
->tx_frame_len
) {
261 /* We don't implement explicit CRC, so just chop it off. */
262 if ((s
->tctl
& SE_TCTL_CRC
) == 0)
263 s
->tx_frame_len
-= 4;
264 if ((s
->tctl
& SE_TCTL_PADEN
) && s
->tx_frame_len
< 60) {
265 memset(&s
->tx_fifo
[s
->tx_frame_len
], 0, 60 - s
->tx_frame_len
);
268 qemu_send_packet(qemu_get_queue(s
->nic
), s
->tx_fifo
,
270 s
->tx_frame_len
= -1;
271 s
->ris
|= SE_INT_TXEMP
;
272 stellaris_enet_update(s
);
273 DPRINTF("Done TX\n");
278 s
->conf
.macaddr
.a
[0] = value
;
279 s
->conf
.macaddr
.a
[1] = value
>> 8;
280 s
->conf
.macaddr
.a
[2] = value
>> 16;
281 s
->conf
.macaddr
.a
[3] = value
>> 24;
284 s
->conf
.macaddr
.a
[4] = value
;
285 s
->conf
.macaddr
.a
[5] = value
>> 8;
290 case 0x20: /* MCTL */
296 case 0x28: /* MADD */
299 case 0x2c: /* MTXD */
300 s
->mtxd
= value
& 0xff;
302 case 0x30: /* MRXD */
306 case 0x3c: /* Undocuented: Timestamp? */
310 hw_error("stellaris_enet_write: Bad offset %x\n", (int)offset
);
314 static const MemoryRegionOps stellaris_enet_ops
= {
315 .read
= stellaris_enet_read
,
316 .write
= stellaris_enet_write
,
317 .endianness
= DEVICE_NATIVE_ENDIAN
,
320 static void stellaris_enet_reset(stellaris_enet_state
*s
)
323 s
->rctl
= SE_RCTL_BADCRC
;
324 s
->im
= SE_INT_PHY
| SE_INT_MD
| SE_INT_RXER
| SE_INT_FOV
| SE_INT_TXEMP
325 | SE_INT_TXER
| SE_INT_RX
;
327 s
->tx_frame_len
= -1;
330 static void stellaris_enet_save(QEMUFile
*f
, void *opaque
)
332 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
335 qemu_put_be32(f
, s
->ris
);
336 qemu_put_be32(f
, s
->im
);
337 qemu_put_be32(f
, s
->rctl
);
338 qemu_put_be32(f
, s
->tctl
);
339 qemu_put_be32(f
, s
->thr
);
340 qemu_put_be32(f
, s
->mctl
);
341 qemu_put_be32(f
, s
->mdv
);
342 qemu_put_be32(f
, s
->mtxd
);
343 qemu_put_be32(f
, s
->mrxd
);
344 qemu_put_be32(f
, s
->np
);
345 qemu_put_be32(f
, s
->tx_frame_len
);
346 qemu_put_be32(f
, s
->tx_fifo_len
);
347 qemu_put_buffer(f
, s
->tx_fifo
, sizeof(s
->tx_fifo
));
348 for (i
= 0; i
< 31; i
++) {
349 qemu_put_be32(f
, s
->rx
[i
].len
);
350 qemu_put_buffer(f
, s
->rx
[i
].data
, sizeof(s
->rx
[i
].data
));
353 qemu_put_be32(f
, s
->next_packet
);
354 qemu_put_be32(f
, s
->rx_fifo
- s
->rx
[s
->next_packet
].data
);
355 qemu_put_be32(f
, s
->rx_fifo_len
);
358 static int stellaris_enet_load(QEMUFile
*f
, void *opaque
, int version_id
)
360 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
366 s
->ris
= qemu_get_be32(f
);
367 s
->im
= qemu_get_be32(f
);
368 s
->rctl
= qemu_get_be32(f
);
369 s
->tctl
= qemu_get_be32(f
);
370 s
->thr
= qemu_get_be32(f
);
371 s
->mctl
= qemu_get_be32(f
);
372 s
->mdv
= qemu_get_be32(f
);
373 s
->mtxd
= qemu_get_be32(f
);
374 s
->mrxd
= qemu_get_be32(f
);
375 s
->np
= qemu_get_be32(f
);
376 s
->tx_frame_len
= qemu_get_be32(f
);
377 s
->tx_fifo_len
= qemu_get_be32(f
);
378 qemu_get_buffer(f
, s
->tx_fifo
, sizeof(s
->tx_fifo
));
379 for (i
= 0; i
< 31; i
++) {
380 s
->rx
[i
].len
= qemu_get_be32(f
);
381 qemu_get_buffer(f
, s
->rx
[i
].data
, sizeof(s
->rx
[i
].data
));
384 s
->next_packet
= qemu_get_be32(f
);
385 s
->rx_fifo
= s
->rx
[s
->next_packet
].data
+ qemu_get_be32(f
);
386 s
->rx_fifo_len
= qemu_get_be32(f
);
391 static void stellaris_enet_cleanup(NetClientState
*nc
)
393 stellaris_enet_state
*s
= qemu_get_nic_opaque(nc
);
398 static NetClientInfo net_stellaris_enet_info
= {
399 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
400 .size
= sizeof(NICState
),
401 .can_receive
= stellaris_enet_can_receive
,
402 .receive
= stellaris_enet_receive
,
403 .cleanup
= stellaris_enet_cleanup
,
406 static int stellaris_enet_init(SysBusDevice
*sbd
)
408 DeviceState
*dev
= DEVICE(sbd
);
409 stellaris_enet_state
*s
= STELLARIS_ENET(dev
);
411 memory_region_init_io(&s
->mmio
, OBJECT(s
), &stellaris_enet_ops
, s
,
412 "stellaris_enet", 0x1000);
413 sysbus_init_mmio(sbd
, &s
->mmio
);
414 sysbus_init_irq(sbd
, &s
->irq
);
415 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
417 s
->nic
= qemu_new_nic(&net_stellaris_enet_info
, &s
->conf
,
418 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
419 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
421 stellaris_enet_reset(s
);
422 register_savevm(dev
, "stellaris_enet", -1, 1,
423 stellaris_enet_save
, stellaris_enet_load
, s
);
427 static void stellaris_enet_unrealize(DeviceState
*dev
, Error
**errp
)
429 stellaris_enet_state
*s
= STELLARIS_ENET(dev
);
431 unregister_savevm(DEVICE(s
), "stellaris_enet", s
);
433 memory_region_destroy(&s
->mmio
);
436 static Property stellaris_enet_properties
[] = {
437 DEFINE_NIC_PROPERTIES(stellaris_enet_state
, conf
),
438 DEFINE_PROP_END_OF_LIST(),
441 static void stellaris_enet_class_init(ObjectClass
*klass
, void *data
)
443 DeviceClass
*dc
= DEVICE_CLASS(klass
);
444 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
446 k
->init
= stellaris_enet_init
;
447 dc
->unrealize
= stellaris_enet_unrealize
;
448 dc
->props
= stellaris_enet_properties
;
451 static const TypeInfo stellaris_enet_info
= {
452 .name
= TYPE_STELLARIS_ENET
,
453 .parent
= TYPE_SYS_BUS_DEVICE
,
454 .instance_size
= sizeof(stellaris_enet_state
),
455 .class_init
= stellaris_enet_class_init
,
458 static void stellaris_enet_register_types(void)
460 type_register_static(&stellaris_enet_info
);
463 type_init(stellaris_enet_register_types
)