2 * Luminary Micro Stellaris Ethernet Controller
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
14 //#define DEBUG_STELLARIS_ENET 1
16 #ifdef DEBUG_STELLARIS_ENET
17 #define DPRINTF(fmt, args...) \
18 do { printf("stellaris_enet: " fmt , ##args); } while (0)
19 #define BADF(fmt, args...) \
20 do { fprintf(stderr, "stellaris_enet: error: " fmt , ##args); exit(1);} while (0)
22 #define DPRINTF(fmt, args...) do {} while(0)
23 #define BADF(fmt, args...) \
24 do { fprintf(stderr, "stellaris_enet: error: " fmt , ##args);} while (0)
27 #define SE_INT_RX 0x01
28 #define SE_INT_TXER 0x02
29 #define SE_INT_TXEMP 0x04
30 #define SE_INT_FOV 0x08
31 #define SE_INT_RXER 0x10
32 #define SE_INT_MD 0x20
33 #define SE_INT_PHY 0x40
35 #define SE_RCTL_RXEN 0x01
36 #define SE_RCTL_AMUL 0x02
37 #define SE_RCTL_PRMS 0x04
38 #define SE_RCTL_BADCRC 0x08
39 #define SE_RCTL_RSTFIFO 0x10
41 #define SE_TCTL_TXEN 0x01
42 #define SE_TCTL_PADEN 0x02
43 #define SE_TCTL_CRC 0x04
44 #define SE_TCTL_DUPLEX 0x08
60 uint8_t tx_fifo
[2048];
61 /* Real hardware has a 2k fifo, which works out to be at most 31 packets.
62 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 void stellaris_enet_receive(void *opaque
, const uint8_t *buf
, int size
)
83 stellaris_enet_state
*s
= (stellaris_enet_state
*)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
);
121 static int stellaris_enet_can_receive(void *opaque
)
123 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
125 if ((s
->rctl
& SE_RCTL_RXEN
) == 0)
131 static uint32_t stellaris_enet_read(void *opaque
, target_phys_addr_t offset
)
133 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
139 DPRINTF("IRQ status %02x\n", s
->ris
);
143 case 0x08: /* RCTL */
145 case 0x0c: /* TCTL */
147 case 0x10: /* DATA */
148 if (s
->rx_fifo_len
== 0) {
150 BADF("RX underflow\n");
153 s
->rx_fifo_len
= s
->rx
[s
->next_packet
].len
;
154 s
->rx_fifo
= s
->rx
[s
->next_packet
].data
;
155 DPRINTF("RX FIFO start packet len=%d\n", s
->rx_fifo_len
);
157 val
= s
->rx_fifo
[0] | (s
->rx_fifo
[1] << 8) | (s
->rx_fifo
[2] << 16)
158 | (s
->rx_fifo
[3] << 24);
161 if (s
->rx_fifo_len
<= 0) {
164 if (s
->next_packet
>= 31)
167 DPRINTF("RX done np=%d\n", s
->np
);
171 return s
->macaddr
[0] | (s
->macaddr
[1] << 8)
172 | (s
->macaddr
[2] << 16) | (s
->macaddr
[3] << 24);
174 return s
->macaddr
[4] | (s
->macaddr
[5] << 8);
177 case 0x20: /* MCTL */
181 case 0x28: /* MADD */
183 case 0x2c: /* MTXD */
185 case 0x30: /* MRXD */
191 case 0x3c: /* Undocuented: Timestamp? */
194 cpu_abort (cpu_single_env
, "stellaris_enet_read: Bad offset %x\n",
200 static void stellaris_enet_write(void *opaque
, target_phys_addr_t offset
,
203 stellaris_enet_state
*s
= (stellaris_enet_state
*)opaque
;
207 case 0x00: /* IACK */
209 DPRINTF("IRQ ack %02x/%02x\n", value
, s
->ris
);
210 stellaris_enet_update(s
);
211 /* Clearing TXER also resets the TX fifo. */
212 if (value
& SE_INT_TXER
)
213 s
->tx_frame_len
= -1;
216 DPRINTF("IRQ mask %02x/%02x\n", value
, s
->ris
);
218 stellaris_enet_update(s
);
220 case 0x08: /* RCTL */
222 if (value
& SE_RCTL_RSTFIFO
) {
225 stellaris_enet_update(s
);
228 case 0x0c: /* TCTL */
231 case 0x10: /* DATA */
232 if (s
->tx_frame_len
== -1) {
233 s
->tx_frame_len
= value
& 0xffff;
234 if (s
->tx_frame_len
> 2032) {
235 DPRINTF("TX frame too long (%d)\n", s
->tx_frame_len
);
237 s
->ris
|= SE_INT_TXER
;
238 stellaris_enet_update(s
);
240 DPRINTF("Start TX frame len=%d\n", s
->tx_frame_len
);
241 /* The value written does not include the ethernet header. */
242 s
->tx_frame_len
+= 14;
243 if ((s
->tctl
& SE_TCTL_CRC
) == 0)
244 s
->tx_frame_len
+= 4;
246 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 16;
247 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 24;
250 s
->tx_fifo
[s
->tx_fifo_len
++] = value
;
251 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 8;
252 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 16;
253 s
->tx_fifo
[s
->tx_fifo_len
++] = value
>> 24;
254 if (s
->tx_fifo_len
>= s
->tx_frame_len
) {
255 /* We don't implement explicit CRC, so just chop it off. */
256 if ((s
->tctl
& SE_TCTL_CRC
) == 0)
257 s
->tx_frame_len
-= 4;
258 if ((s
->tctl
& SE_TCTL_PADEN
) && s
->tx_frame_len
< 60) {
259 memset(&s
->tx_fifo
[s
->tx_frame_len
], 0, 60 - s
->tx_frame_len
);
262 qemu_send_packet(s
->vc
, s
->tx_fifo
, s
->tx_frame_len
);
263 s
->tx_frame_len
= -1;
264 s
->ris
|= SE_INT_TXEMP
;
265 stellaris_enet_update(s
);
266 DPRINTF("Done TX\n");
271 s
->macaddr
[0] = value
;
272 s
->macaddr
[1] = value
>> 8;
273 s
->macaddr
[2] = value
>> 16;
274 s
->macaddr
[3] = value
>> 24;
277 s
->macaddr
[4] = value
;
278 s
->macaddr
[5] = value
>> 8;
283 case 0x20: /* MCTL */
289 case 0x28: /* MADD */
292 case 0x2c: /* MTXD */
293 s
->mtxd
= value
& 0xff;
295 case 0x30: /* MRXD */
299 case 0x3c: /* Undocuented: Timestamp? */
303 cpu_abort (cpu_single_env
, "stellaris_enet_write: Bad offset %x\n",
308 static CPUReadMemoryFunc
*stellaris_enet_readfn
[] = {
314 static CPUWriteMemoryFunc
*stellaris_enet_writefn
[] = {
315 stellaris_enet_write
,
316 stellaris_enet_write
,
319 static void stellaris_enet_reset(stellaris_enet_state
*s
)
322 s
->rctl
= SE_RCTL_BADCRC
;
323 s
->im
= SE_INT_PHY
| SE_INT_MD
| SE_INT_RXER
| SE_INT_FOV
| SE_INT_TXEMP
324 | SE_INT_TXER
| SE_INT_RX
;
326 s
->tx_frame_len
= -1;
329 void stellaris_enet_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
331 stellaris_enet_state
*s
;
334 s
= (stellaris_enet_state
*)qemu_mallocz(sizeof(stellaris_enet_state
));
335 iomemtype
= cpu_register_io_memory(0, stellaris_enet_readfn
,
336 stellaris_enet_writefn
, s
);
337 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
340 memcpy(s
->macaddr
, nd
->macaddr
, 6);
343 s
->vc
= qemu_new_vlan_client(nd
->vlan
, stellaris_enet_receive
,
344 stellaris_enet_can_receive
, s
);
346 stellaris_enet_reset(s
);