root / hw / mcf_fec.c @ 3d53f5c3
History | View | Annotate | Download (12.3 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | 7e049b8a | pbrook | * ColdFire Fast Ethernet Controller emulation.
|
3 | 7e049b8a | pbrook | *
|
4 | 7e049b8a | pbrook | * Copyright (c) 2007 CodeSourcery.
|
5 | 7e049b8a | pbrook | *
|
6 | 7e049b8a | pbrook | * This code is licenced under the GPL
|
7 | 7e049b8a | pbrook | */
|
8 | 87ecb68b | pbrook | #include "hw.h" |
9 | 87ecb68b | pbrook | #include "net.h" |
10 | 87ecb68b | pbrook | #include "mcf.h" |
11 | 7e049b8a | pbrook | /* For crc32 */
|
12 | 7e049b8a | pbrook | #include <zlib.h> |
13 | 7e049b8a | pbrook | |
14 | 7e049b8a | pbrook | //#define DEBUG_FEC 1
|
15 | 7e049b8a | pbrook | |
16 | 7e049b8a | pbrook | #ifdef DEBUG_FEC
|
17 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
18 | 001faf32 | Blue Swirl | do { printf("mcf_fec: " fmt , ## __VA_ARGS__); } while (0) |
19 | 7e049b8a | pbrook | #else
|
20 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) do {} while(0) |
21 | 7e049b8a | pbrook | #endif
|
22 | 7e049b8a | pbrook | |
23 | 7e049b8a | pbrook | #define FEC_MAX_FRAME_SIZE 2032 |
24 | 7e049b8a | pbrook | |
25 | 7e049b8a | pbrook | typedef struct { |
26 | 7e049b8a | pbrook | qemu_irq *irq; |
27 | b946a153 | aliguori | int mmio_index;
|
28 | 1cc49d95 | Mark McLoughlin | NICState *nic; |
29 | 1cc49d95 | Mark McLoughlin | NICConf conf; |
30 | 7e049b8a | pbrook | uint32_t irq_state; |
31 | 7e049b8a | pbrook | uint32_t eir; |
32 | 7e049b8a | pbrook | uint32_t eimr; |
33 | 7e049b8a | pbrook | int rx_enabled;
|
34 | 7e049b8a | pbrook | uint32_t rx_descriptor; |
35 | 7e049b8a | pbrook | uint32_t tx_descriptor; |
36 | 7e049b8a | pbrook | uint32_t ecr; |
37 | 7e049b8a | pbrook | uint32_t mmfr; |
38 | 7e049b8a | pbrook | uint32_t mscr; |
39 | 7e049b8a | pbrook | uint32_t rcr; |
40 | 7e049b8a | pbrook | uint32_t tcr; |
41 | 7e049b8a | pbrook | uint32_t tfwr; |
42 | 7e049b8a | pbrook | uint32_t rfsr; |
43 | 7e049b8a | pbrook | uint32_t erdsr; |
44 | 7e049b8a | pbrook | uint32_t etdsr; |
45 | 7e049b8a | pbrook | uint32_t emrbr; |
46 | 7e049b8a | pbrook | } mcf_fec_state; |
47 | 7e049b8a | pbrook | |
48 | 7e049b8a | pbrook | #define FEC_INT_HB 0x80000000 |
49 | 7e049b8a | pbrook | #define FEC_INT_BABR 0x40000000 |
50 | 7e049b8a | pbrook | #define FEC_INT_BABT 0x20000000 |
51 | 7e049b8a | pbrook | #define FEC_INT_GRA 0x10000000 |
52 | 7e049b8a | pbrook | #define FEC_INT_TXF 0x08000000 |
53 | 7e049b8a | pbrook | #define FEC_INT_TXB 0x04000000 |
54 | 7e049b8a | pbrook | #define FEC_INT_RXF 0x02000000 |
55 | 7e049b8a | pbrook | #define FEC_INT_RXB 0x01000000 |
56 | 7e049b8a | pbrook | #define FEC_INT_MII 0x00800000 |
57 | 7e049b8a | pbrook | #define FEC_INT_EB 0x00400000 |
58 | 7e049b8a | pbrook | #define FEC_INT_LC 0x00200000 |
59 | 7e049b8a | pbrook | #define FEC_INT_RL 0x00100000 |
60 | 7e049b8a | pbrook | #define FEC_INT_UN 0x00080000 |
61 | 7e049b8a | pbrook | |
62 | 7e049b8a | pbrook | #define FEC_EN 2 |
63 | 7e049b8a | pbrook | #define FEC_RESET 1 |
64 | 7e049b8a | pbrook | |
65 | 7e049b8a | pbrook | /* Map interrupt flags onto IRQ lines. */
|
66 | 7e049b8a | pbrook | #define FEC_NUM_IRQ 13 |
67 | 7e049b8a | pbrook | static const uint32_t mcf_fec_irq_map[FEC_NUM_IRQ] = { |
68 | 7e049b8a | pbrook | FEC_INT_TXF, |
69 | 7e049b8a | pbrook | FEC_INT_TXB, |
70 | 7e049b8a | pbrook | FEC_INT_UN, |
71 | 7e049b8a | pbrook | FEC_INT_RL, |
72 | 7e049b8a | pbrook | FEC_INT_RXF, |
73 | 7e049b8a | pbrook | FEC_INT_RXB, |
74 | 7e049b8a | pbrook | FEC_INT_MII, |
75 | 7e049b8a | pbrook | FEC_INT_LC, |
76 | 7e049b8a | pbrook | FEC_INT_HB, |
77 | 7e049b8a | pbrook | FEC_INT_GRA, |
78 | 7e049b8a | pbrook | FEC_INT_EB, |
79 | 7e049b8a | pbrook | FEC_INT_BABT, |
80 | 7e049b8a | pbrook | FEC_INT_BABR |
81 | 7e049b8a | pbrook | }; |
82 | 7e049b8a | pbrook | |
83 | 7e049b8a | pbrook | /* Buffer Descriptor. */
|
84 | 7e049b8a | pbrook | typedef struct { |
85 | 7e049b8a | pbrook | uint16_t flags; |
86 | 7e049b8a | pbrook | uint16_t length; |
87 | 7e049b8a | pbrook | uint32_t data; |
88 | 7e049b8a | pbrook | } mcf_fec_bd; |
89 | 7e049b8a | pbrook | |
90 | 7e049b8a | pbrook | #define FEC_BD_R 0x8000 |
91 | 7e049b8a | pbrook | #define FEC_BD_E 0x8000 |
92 | 7e049b8a | pbrook | #define FEC_BD_O1 0x4000 |
93 | 7e049b8a | pbrook | #define FEC_BD_W 0x2000 |
94 | 7e049b8a | pbrook | #define FEC_BD_O2 0x1000 |
95 | 7e049b8a | pbrook | #define FEC_BD_L 0x0800 |
96 | 7e049b8a | pbrook | #define FEC_BD_TC 0x0400 |
97 | 7e049b8a | pbrook | #define FEC_BD_ABC 0x0200 |
98 | 7e049b8a | pbrook | #define FEC_BD_M 0x0100 |
99 | 7e049b8a | pbrook | #define FEC_BD_BC 0x0080 |
100 | 7e049b8a | pbrook | #define FEC_BD_MC 0x0040 |
101 | 7e049b8a | pbrook | #define FEC_BD_LG 0x0020 |
102 | 7e049b8a | pbrook | #define FEC_BD_NO 0x0010 |
103 | 7e049b8a | pbrook | #define FEC_BD_CR 0x0004 |
104 | 7e049b8a | pbrook | #define FEC_BD_OV 0x0002 |
105 | 7e049b8a | pbrook | #define FEC_BD_TR 0x0001 |
106 | 7e049b8a | pbrook | |
107 | 7e049b8a | pbrook | static void mcf_fec_read_bd(mcf_fec_bd *bd, uint32_t addr) |
108 | 7e049b8a | pbrook | { |
109 | 7e049b8a | pbrook | cpu_physical_memory_read(addr, (uint8_t *)bd, sizeof(*bd));
|
110 | 7e049b8a | pbrook | be16_to_cpus(&bd->flags); |
111 | 7e049b8a | pbrook | be16_to_cpus(&bd->length); |
112 | 7e049b8a | pbrook | be32_to_cpus(&bd->data); |
113 | 7e049b8a | pbrook | } |
114 | 7e049b8a | pbrook | |
115 | 7e049b8a | pbrook | static void mcf_fec_write_bd(mcf_fec_bd *bd, uint32_t addr) |
116 | 7e049b8a | pbrook | { |
117 | 7e049b8a | pbrook | mcf_fec_bd tmp; |
118 | 7e049b8a | pbrook | tmp.flags = cpu_to_be16(bd->flags); |
119 | 7e049b8a | pbrook | tmp.length = cpu_to_be16(bd->length); |
120 | 7e049b8a | pbrook | tmp.data = cpu_to_be32(bd->data); |
121 | 7e049b8a | pbrook | cpu_physical_memory_write(addr, (uint8_t *)&tmp, sizeof(tmp));
|
122 | 7e049b8a | pbrook | } |
123 | 7e049b8a | pbrook | |
124 | 7e049b8a | pbrook | static void mcf_fec_update(mcf_fec_state *s) |
125 | 7e049b8a | pbrook | { |
126 | 7e049b8a | pbrook | uint32_t active; |
127 | 7e049b8a | pbrook | uint32_t changed; |
128 | 7e049b8a | pbrook | uint32_t mask; |
129 | 7e049b8a | pbrook | int i;
|
130 | 7e049b8a | pbrook | |
131 | 7e049b8a | pbrook | active = s->eir & s->eimr; |
132 | 7e049b8a | pbrook | changed = active ^s->irq_state; |
133 | 7e049b8a | pbrook | for (i = 0; i < FEC_NUM_IRQ; i++) { |
134 | 7e049b8a | pbrook | mask = mcf_fec_irq_map[i]; |
135 | 7e049b8a | pbrook | if (changed & mask) {
|
136 | 7e049b8a | pbrook | DPRINTF("IRQ %d = %d\n", i, (active & mask) != 0); |
137 | 7e049b8a | pbrook | qemu_set_irq(s->irq[i], (active & mask) != 0);
|
138 | 7e049b8a | pbrook | } |
139 | 7e049b8a | pbrook | } |
140 | 7e049b8a | pbrook | s->irq_state = active; |
141 | 7e049b8a | pbrook | } |
142 | 7e049b8a | pbrook | |
143 | 7e049b8a | pbrook | static void mcf_fec_do_tx(mcf_fec_state *s) |
144 | 7e049b8a | pbrook | { |
145 | 7e049b8a | pbrook | uint32_t addr; |
146 | 7e049b8a | pbrook | mcf_fec_bd bd; |
147 | 7e049b8a | pbrook | int frame_size;
|
148 | 7e049b8a | pbrook | int len;
|
149 | 7e049b8a | pbrook | uint8_t frame[FEC_MAX_FRAME_SIZE]; |
150 | 7e049b8a | pbrook | uint8_t *ptr; |
151 | 7e049b8a | pbrook | |
152 | 7e049b8a | pbrook | DPRINTF("do_tx\n");
|
153 | 7e049b8a | pbrook | ptr = frame; |
154 | 7e049b8a | pbrook | frame_size = 0;
|
155 | 7e049b8a | pbrook | addr = s->tx_descriptor; |
156 | 7e049b8a | pbrook | while (1) { |
157 | 7e049b8a | pbrook | mcf_fec_read_bd(&bd, addr); |
158 | 7e049b8a | pbrook | DPRINTF("tx_bd %x flags %04x len %d data %08x\n",
|
159 | 7e049b8a | pbrook | addr, bd.flags, bd.length, bd.data); |
160 | 7e049b8a | pbrook | if ((bd.flags & FEC_BD_R) == 0) { |
161 | 7e049b8a | pbrook | /* Run out of descriptors to transmit. */
|
162 | 7e049b8a | pbrook | break;
|
163 | 7e049b8a | pbrook | } |
164 | 7e049b8a | pbrook | len = bd.length; |
165 | 7e049b8a | pbrook | if (frame_size + len > FEC_MAX_FRAME_SIZE) {
|
166 | 7e049b8a | pbrook | len = FEC_MAX_FRAME_SIZE - frame_size; |
167 | 7e049b8a | pbrook | s->eir |= FEC_INT_BABT; |
168 | 7e049b8a | pbrook | } |
169 | 7e049b8a | pbrook | cpu_physical_memory_read(bd.data, ptr, len); |
170 | 7e049b8a | pbrook | ptr += len; |
171 | 7e049b8a | pbrook | frame_size += len; |
172 | 7e049b8a | pbrook | if (bd.flags & FEC_BD_L) {
|
173 | 7e049b8a | pbrook | /* Last buffer in frame. */
|
174 | 7e049b8a | pbrook | DPRINTF("Sending packet\n");
|
175 | 1cc49d95 | Mark McLoughlin | qemu_send_packet(&s->nic->nc, frame, len); |
176 | 7e049b8a | pbrook | ptr = frame; |
177 | 7e049b8a | pbrook | frame_size = 0;
|
178 | 7e049b8a | pbrook | s->eir |= FEC_INT_TXF; |
179 | 7e049b8a | pbrook | } |
180 | 7e049b8a | pbrook | s->eir |= FEC_INT_TXB; |
181 | 7e049b8a | pbrook | bd.flags &= ~FEC_BD_R; |
182 | 7e049b8a | pbrook | /* Write back the modified descriptor. */
|
183 | 7e049b8a | pbrook | mcf_fec_write_bd(&bd, addr); |
184 | 7e049b8a | pbrook | /* Advance to the next descriptor. */
|
185 | 7e049b8a | pbrook | if ((bd.flags & FEC_BD_W) != 0) { |
186 | 7e049b8a | pbrook | addr = s->etdsr; |
187 | 7e049b8a | pbrook | } else {
|
188 | 7e049b8a | pbrook | addr += 8;
|
189 | 7e049b8a | pbrook | } |
190 | 7e049b8a | pbrook | } |
191 | 7e049b8a | pbrook | s->tx_descriptor = addr; |
192 | 7e049b8a | pbrook | } |
193 | 7e049b8a | pbrook | |
194 | 4fdcd8d4 | pbrook | static void mcf_fec_enable_rx(mcf_fec_state *s) |
195 | 7e049b8a | pbrook | { |
196 | 7e049b8a | pbrook | mcf_fec_bd bd; |
197 | 7e049b8a | pbrook | |
198 | 7e049b8a | pbrook | mcf_fec_read_bd(&bd, s->rx_descriptor); |
199 | 7e049b8a | pbrook | s->rx_enabled = ((bd.flags & FEC_BD_E) != 0);
|
200 | 7e049b8a | pbrook | if (!s->rx_enabled)
|
201 | 7e049b8a | pbrook | DPRINTF("RX buffer full\n");
|
202 | 7e049b8a | pbrook | } |
203 | 7e049b8a | pbrook | |
204 | 7e049b8a | pbrook | static void mcf_fec_reset(mcf_fec_state *s) |
205 | 7e049b8a | pbrook | { |
206 | 7e049b8a | pbrook | s->eir = 0;
|
207 | 7e049b8a | pbrook | s->eimr = 0;
|
208 | 7e049b8a | pbrook | s->rx_enabled = 0;
|
209 | 7e049b8a | pbrook | s->ecr = 0;
|
210 | 7e049b8a | pbrook | s->mscr = 0;
|
211 | 7e049b8a | pbrook | s->rcr = 0x05ee0001;
|
212 | 7e049b8a | pbrook | s->tcr = 0;
|
213 | 7e049b8a | pbrook | s->tfwr = 0;
|
214 | 7e049b8a | pbrook | s->rfsr = 0x500;
|
215 | 7e049b8a | pbrook | } |
216 | 7e049b8a | pbrook | |
217 | c227f099 | Anthony Liguori | static uint32_t mcf_fec_read(void *opaque, target_phys_addr_t addr) |
218 | 7e049b8a | pbrook | { |
219 | 7e049b8a | pbrook | mcf_fec_state *s = (mcf_fec_state *)opaque; |
220 | 7e049b8a | pbrook | switch (addr & 0x3ff) { |
221 | 7e049b8a | pbrook | case 0x004: return s->eir; |
222 | 7e049b8a | pbrook | case 0x008: return s->eimr; |
223 | 7e049b8a | pbrook | case 0x010: return s->rx_enabled ? (1 << 24) : 0; /* RDAR */ |
224 | 7e049b8a | pbrook | case 0x014: return 0; /* TDAR */ |
225 | 7e049b8a | pbrook | case 0x024: return s->ecr; |
226 | 7e049b8a | pbrook | case 0x040: return s->mmfr; |
227 | 7e049b8a | pbrook | case 0x044: return s->mscr; |
228 | 7e049b8a | pbrook | case 0x064: return 0; /* MIBC */ |
229 | 7e049b8a | pbrook | case 0x084: return s->rcr; |
230 | 7e049b8a | pbrook | case 0x0c4: return s->tcr; |
231 | 7e049b8a | pbrook | case 0x0e4: /* PALR */ |
232 | 1cc49d95 | Mark McLoughlin | return (s->conf.macaddr.a[0] << 24) | (s->conf.macaddr.a[1] << 16) |
233 | 1cc49d95 | Mark McLoughlin | | (s->conf.macaddr.a[2] << 8) | s->conf.macaddr.a[3]; |
234 | 7e049b8a | pbrook | break;
|
235 | 7e049b8a | pbrook | case 0x0e8: /* PAUR */ |
236 | 1cc49d95 | Mark McLoughlin | return (s->conf.macaddr.a[4] << 24) | (s->conf.macaddr.a[5] << 16) | 0x8808; |
237 | 7e049b8a | pbrook | case 0x0ec: return 0x10000; /* OPD */ |
238 | 7e049b8a | pbrook | case 0x118: return 0; |
239 | 7e049b8a | pbrook | case 0x11c: return 0; |
240 | 7e049b8a | pbrook | case 0x120: return 0; |
241 | 7e049b8a | pbrook | case 0x124: return 0; |
242 | 7e049b8a | pbrook | case 0x144: return s->tfwr; |
243 | 7e049b8a | pbrook | case 0x14c: return 0x600; |
244 | 7e049b8a | pbrook | case 0x150: return s->rfsr; |
245 | 7e049b8a | pbrook | case 0x180: return s->erdsr; |
246 | 7e049b8a | pbrook | case 0x184: return s->etdsr; |
247 | 7e049b8a | pbrook | case 0x188: return s->emrbr; |
248 | 7e049b8a | pbrook | default:
|
249 | 2ac71179 | Paul Brook | hw_error("mcf_fec_read: Bad address 0x%x\n", (int)addr); |
250 | 7e049b8a | pbrook | return 0; |
251 | 7e049b8a | pbrook | } |
252 | 7e049b8a | pbrook | } |
253 | 7e049b8a | pbrook | |
254 | c227f099 | Anthony Liguori | static void mcf_fec_write(void *opaque, target_phys_addr_t addr, uint32_t value) |
255 | 7e049b8a | pbrook | { |
256 | 7e049b8a | pbrook | mcf_fec_state *s = (mcf_fec_state *)opaque; |
257 | 7e049b8a | pbrook | switch (addr & 0x3ff) { |
258 | 7e049b8a | pbrook | case 0x004: |
259 | 7e049b8a | pbrook | s->eir &= ~value; |
260 | 7e049b8a | pbrook | break;
|
261 | 7e049b8a | pbrook | case 0x008: |
262 | 7e049b8a | pbrook | s->eimr = value; |
263 | 7e049b8a | pbrook | break;
|
264 | 7e049b8a | pbrook | case 0x010: /* RDAR */ |
265 | 7e049b8a | pbrook | if ((s->ecr & FEC_EN) && !s->rx_enabled) {
|
266 | 7e049b8a | pbrook | DPRINTF("RX enable\n");
|
267 | 7e049b8a | pbrook | mcf_fec_enable_rx(s); |
268 | 7e049b8a | pbrook | } |
269 | 7e049b8a | pbrook | break;
|
270 | 7e049b8a | pbrook | case 0x014: /* TDAR */ |
271 | 7e049b8a | pbrook | if (s->ecr & FEC_EN) {
|
272 | 7e049b8a | pbrook | mcf_fec_do_tx(s); |
273 | 7e049b8a | pbrook | } |
274 | 7e049b8a | pbrook | break;
|
275 | 7e049b8a | pbrook | case 0x024: |
276 | 7e049b8a | pbrook | s->ecr = value; |
277 | 7e049b8a | pbrook | if (value & FEC_RESET) {
|
278 | 7e049b8a | pbrook | DPRINTF("Reset\n");
|
279 | 7e049b8a | pbrook | mcf_fec_reset(s); |
280 | 7e049b8a | pbrook | } |
281 | 7e049b8a | pbrook | if ((s->ecr & FEC_EN) == 0) { |
282 | 7e049b8a | pbrook | s->rx_enabled = 0;
|
283 | 7e049b8a | pbrook | } |
284 | 7e049b8a | pbrook | break;
|
285 | 7e049b8a | pbrook | case 0x040: |
286 | 7e049b8a | pbrook | /* TODO: Implement MII. */
|
287 | 7e049b8a | pbrook | s->mmfr = value; |
288 | 7e049b8a | pbrook | break;
|
289 | 7e049b8a | pbrook | case 0x044: |
290 | 7e049b8a | pbrook | s->mscr = value & 0xfe;
|
291 | 7e049b8a | pbrook | break;
|
292 | 7e049b8a | pbrook | case 0x064: |
293 | 7e049b8a | pbrook | /* TODO: Implement MIB. */
|
294 | 7e049b8a | pbrook | break;
|
295 | 7e049b8a | pbrook | case 0x084: |
296 | 7e049b8a | pbrook | s->rcr = value & 0x07ff003f;
|
297 | 7e049b8a | pbrook | /* TODO: Implement LOOP mode. */
|
298 | 7e049b8a | pbrook | break;
|
299 | 7e049b8a | pbrook | case 0x0c4: /* TCR */ |
300 | 7e049b8a | pbrook | /* We transmit immediately, so raise GRA immediately. */
|
301 | 7e049b8a | pbrook | s->tcr = value; |
302 | 7e049b8a | pbrook | if (value & 1) |
303 | 7e049b8a | pbrook | s->eir |= FEC_INT_GRA; |
304 | 7e049b8a | pbrook | break;
|
305 | 7e049b8a | pbrook | case 0x0e4: /* PALR */ |
306 | 1cc49d95 | Mark McLoughlin | s->conf.macaddr.a[0] = value >> 24; |
307 | 1cc49d95 | Mark McLoughlin | s->conf.macaddr.a[1] = value >> 16; |
308 | 1cc49d95 | Mark McLoughlin | s->conf.macaddr.a[2] = value >> 8; |
309 | 1cc49d95 | Mark McLoughlin | s->conf.macaddr.a[3] = value;
|
310 | 7e049b8a | pbrook | break;
|
311 | 7e049b8a | pbrook | case 0x0e8: /* PAUR */ |
312 | 1cc49d95 | Mark McLoughlin | s->conf.macaddr.a[4] = value >> 24; |
313 | 1cc49d95 | Mark McLoughlin | s->conf.macaddr.a[5] = value >> 16; |
314 | 7e049b8a | pbrook | break;
|
315 | 7e049b8a | pbrook | case 0x0ec: |
316 | 7e049b8a | pbrook | /* OPD */
|
317 | 7e049b8a | pbrook | break;
|
318 | 7e049b8a | pbrook | case 0x118: |
319 | 7e049b8a | pbrook | case 0x11c: |
320 | 7e049b8a | pbrook | case 0x120: |
321 | 7e049b8a | pbrook | case 0x124: |
322 | 7e049b8a | pbrook | /* TODO: implement MAC hash filtering. */
|
323 | 7e049b8a | pbrook | break;
|
324 | 7e049b8a | pbrook | case 0x144: |
325 | 7e049b8a | pbrook | s->tfwr = value & 3;
|
326 | 7e049b8a | pbrook | break;
|
327 | 7e049b8a | pbrook | case 0x14c: |
328 | 7e049b8a | pbrook | /* FRBR writes ignored. */
|
329 | 7e049b8a | pbrook | break;
|
330 | 7e049b8a | pbrook | case 0x150: |
331 | 7e049b8a | pbrook | s->rfsr = (value & 0x3fc) | 0x400; |
332 | 7e049b8a | pbrook | break;
|
333 | 7e049b8a | pbrook | case 0x180: |
334 | 7e049b8a | pbrook | s->erdsr = value & ~3;
|
335 | 7e049b8a | pbrook | s->rx_descriptor = s->erdsr; |
336 | 7e049b8a | pbrook | break;
|
337 | 7e049b8a | pbrook | case 0x184: |
338 | 7e049b8a | pbrook | s->etdsr = value & ~3;
|
339 | 7e049b8a | pbrook | s->tx_descriptor = s->etdsr; |
340 | 7e049b8a | pbrook | break;
|
341 | 7e049b8a | pbrook | case 0x188: |
342 | 7e049b8a | pbrook | s->emrbr = value & 0x7f0;
|
343 | 7e049b8a | pbrook | break;
|
344 | 7e049b8a | pbrook | default:
|
345 | 2ac71179 | Paul Brook | hw_error("mcf_fec_write Bad address 0x%x\n", (int)addr); |
346 | 7e049b8a | pbrook | } |
347 | 7e049b8a | pbrook | mcf_fec_update(s); |
348 | 7e049b8a | pbrook | } |
349 | 7e049b8a | pbrook | |
350 | 1cc49d95 | Mark McLoughlin | static int mcf_fec_can_receive(VLANClientState *nc) |
351 | 7e049b8a | pbrook | { |
352 | 1cc49d95 | Mark McLoughlin | mcf_fec_state *s = DO_UPCAST(NICState, nc, nc)->opaque; |
353 | 7e049b8a | pbrook | return s->rx_enabled;
|
354 | 7e049b8a | pbrook | } |
355 | 7e049b8a | pbrook | |
356 | 1cc49d95 | Mark McLoughlin | static ssize_t mcf_fec_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
357 | 7e049b8a | pbrook | { |
358 | 1cc49d95 | Mark McLoughlin | mcf_fec_state *s = DO_UPCAST(NICState, nc, nc)->opaque; |
359 | 7e049b8a | pbrook | mcf_fec_bd bd; |
360 | 7e049b8a | pbrook | uint32_t flags = 0;
|
361 | 7e049b8a | pbrook | uint32_t addr; |
362 | 7e049b8a | pbrook | uint32_t crc; |
363 | 7e049b8a | pbrook | uint32_t buf_addr; |
364 | 7e049b8a | pbrook | uint8_t *crc_ptr; |
365 | 7e049b8a | pbrook | unsigned int buf_len; |
366 | 7e049b8a | pbrook | |
367 | 7e049b8a | pbrook | DPRINTF("do_rx len %d\n", size);
|
368 | 7e049b8a | pbrook | if (!s->rx_enabled) {
|
369 | 7e049b8a | pbrook | fprintf(stderr, "mcf_fec_receive: Unexpected packet\n");
|
370 | 7e049b8a | pbrook | } |
371 | 7e049b8a | pbrook | /* 4 bytes for the CRC. */
|
372 | 7e049b8a | pbrook | size += 4;
|
373 | 7e049b8a | pbrook | crc = cpu_to_be32(crc32(~0, buf, size));
|
374 | 7e049b8a | pbrook | crc_ptr = (uint8_t *)&crc; |
375 | 7e049b8a | pbrook | /* Huge frames are truncted. */
|
376 | 7e049b8a | pbrook | if (size > FEC_MAX_FRAME_SIZE) {
|
377 | 7e049b8a | pbrook | size = FEC_MAX_FRAME_SIZE; |
378 | 7e049b8a | pbrook | flags |= FEC_BD_TR | FEC_BD_LG; |
379 | 7e049b8a | pbrook | } |
380 | 7e049b8a | pbrook | /* Frames larger than the user limit just set error flags. */
|
381 | 7e049b8a | pbrook | if (size > (s->rcr >> 16)) { |
382 | 7e049b8a | pbrook | flags |= FEC_BD_LG; |
383 | 7e049b8a | pbrook | } |
384 | 7e049b8a | pbrook | addr = s->rx_descriptor; |
385 | 7e049b8a | pbrook | while (size > 0) { |
386 | 7e049b8a | pbrook | mcf_fec_read_bd(&bd, addr); |
387 | 7e049b8a | pbrook | if ((bd.flags & FEC_BD_E) == 0) { |
388 | 7e049b8a | pbrook | /* No descriptors available. Bail out. */
|
389 | 7e049b8a | pbrook | /* FIXME: This is wrong. We should probably either save the
|
390 | 7e049b8a | pbrook | remainder for when more RX buffers are available, or
|
391 | 7e049b8a | pbrook | flag an error. */
|
392 | 7e049b8a | pbrook | fprintf(stderr, "mcf_fec: Lost end of frame\n");
|
393 | 7e049b8a | pbrook | break;
|
394 | 7e049b8a | pbrook | } |
395 | 7e049b8a | pbrook | buf_len = (size <= s->emrbr) ? size: s->emrbr; |
396 | 7e049b8a | pbrook | bd.length = buf_len; |
397 | 7e049b8a | pbrook | size -= buf_len; |
398 | 7e049b8a | pbrook | DPRINTF("rx_bd %x length %d\n", addr, bd.length);
|
399 | 7e049b8a | pbrook | /* The last 4 bytes are the CRC. */
|
400 | 7e049b8a | pbrook | if (size < 4) |
401 | 7e049b8a | pbrook | buf_len += size - 4;
|
402 | 7e049b8a | pbrook | buf_addr = bd.data; |
403 | 7e049b8a | pbrook | cpu_physical_memory_write(buf_addr, buf, buf_len); |
404 | 7e049b8a | pbrook | buf += buf_len; |
405 | 7e049b8a | pbrook | if (size < 4) { |
406 | 7e049b8a | pbrook | cpu_physical_memory_write(buf_addr + buf_len, crc_ptr, 4 - size);
|
407 | 7e049b8a | pbrook | crc_ptr += 4 - size;
|
408 | 7e049b8a | pbrook | } |
409 | 7e049b8a | pbrook | bd.flags &= ~FEC_BD_E; |
410 | 7e049b8a | pbrook | if (size == 0) { |
411 | 7e049b8a | pbrook | /* Last buffer in frame. */
|
412 | 7e049b8a | pbrook | bd.flags |= flags | FEC_BD_L; |
413 | 7e049b8a | pbrook | DPRINTF("rx frame flags %04x\n", bd.flags);
|
414 | 7e049b8a | pbrook | s->eir |= FEC_INT_RXF; |
415 | 7e049b8a | pbrook | } else {
|
416 | 7e049b8a | pbrook | s->eir |= FEC_INT_RXB; |
417 | 7e049b8a | pbrook | } |
418 | 7e049b8a | pbrook | mcf_fec_write_bd(&bd, addr); |
419 | 7e049b8a | pbrook | /* Advance to the next descriptor. */
|
420 | 7e049b8a | pbrook | if ((bd.flags & FEC_BD_W) != 0) { |
421 | 7e049b8a | pbrook | addr = s->erdsr; |
422 | 7e049b8a | pbrook | } else {
|
423 | 7e049b8a | pbrook | addr += 8;
|
424 | 7e049b8a | pbrook | } |
425 | 7e049b8a | pbrook | } |
426 | 7e049b8a | pbrook | s->rx_descriptor = addr; |
427 | 7e049b8a | pbrook | mcf_fec_enable_rx(s); |
428 | 7e049b8a | pbrook | mcf_fec_update(s); |
429 | 4f1c942b | Mark McLoughlin | return size;
|
430 | 7e049b8a | pbrook | } |
431 | 7e049b8a | pbrook | |
432 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const mcf_fec_readfn[] = { |
433 | 7e049b8a | pbrook | mcf_fec_read, |
434 | 7e049b8a | pbrook | mcf_fec_read, |
435 | 7e049b8a | pbrook | mcf_fec_read |
436 | 7e049b8a | pbrook | }; |
437 | 7e049b8a | pbrook | |
438 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const mcf_fec_writefn[] = { |
439 | 7e049b8a | pbrook | mcf_fec_write, |
440 | 7e049b8a | pbrook | mcf_fec_write, |
441 | 7e049b8a | pbrook | mcf_fec_write |
442 | 7e049b8a | pbrook | }; |
443 | 7e049b8a | pbrook | |
444 | 1cc49d95 | Mark McLoughlin | static void mcf_fec_cleanup(VLANClientState *nc) |
445 | b946a153 | aliguori | { |
446 | 1cc49d95 | Mark McLoughlin | mcf_fec_state *s = DO_UPCAST(NICState, nc, nc)->opaque; |
447 | b946a153 | aliguori | |
448 | b946a153 | aliguori | cpu_unregister_io_memory(s->mmio_index); |
449 | b946a153 | aliguori | |
450 | b946a153 | aliguori | qemu_free(s); |
451 | b946a153 | aliguori | } |
452 | b946a153 | aliguori | |
453 | 1cc49d95 | Mark McLoughlin | static NetClientInfo net_mcf_fec_info = {
|
454 | 1cc49d95 | Mark McLoughlin | .type = NET_CLIENT_TYPE_NIC, |
455 | 1cc49d95 | Mark McLoughlin | .size = sizeof(NICState),
|
456 | 1cc49d95 | Mark McLoughlin | .can_receive = mcf_fec_can_receive, |
457 | 1cc49d95 | Mark McLoughlin | .receive = mcf_fec_receive, |
458 | 1cc49d95 | Mark McLoughlin | .cleanup = mcf_fec_cleanup, |
459 | 1cc49d95 | Mark McLoughlin | }; |
460 | 1cc49d95 | Mark McLoughlin | |
461 | c227f099 | Anthony Liguori | void mcf_fec_init(NICInfo *nd, target_phys_addr_t base, qemu_irq *irq)
|
462 | 7e049b8a | pbrook | { |
463 | 7e049b8a | pbrook | mcf_fec_state *s; |
464 | 7e049b8a | pbrook | |
465 | 0ae18cee | aliguori | qemu_check_nic_model(nd, "mcf_fec");
|
466 | 0ae18cee | aliguori | |
467 | 7e049b8a | pbrook | s = (mcf_fec_state *)qemu_mallocz(sizeof(mcf_fec_state));
|
468 | 7e049b8a | pbrook | s->irq = irq; |
469 | 1eed09cb | Avi Kivity | s->mmio_index = cpu_register_io_memory(mcf_fec_readfn, |
470 | b946a153 | aliguori | mcf_fec_writefn, s); |
471 | b946a153 | aliguori | cpu_register_physical_memory(base, 0x400, s->mmio_index);
|
472 | 7e049b8a | pbrook | |
473 | 1cc49d95 | Mark McLoughlin | memcpy(s->conf.macaddr.a, nd->macaddr, sizeof(nd->macaddr));
|
474 | 1cc49d95 | Mark McLoughlin | s->conf.vlan = nd->vlan; |
475 | 1cc49d95 | Mark McLoughlin | s->conf.peer = nd->netdev; |
476 | 1cc49d95 | Mark McLoughlin | |
477 | 1cc49d95 | Mark McLoughlin | s->nic = qemu_new_nic(&net_mcf_fec_info, &s->conf, nd->model, nd->name, s); |
478 | 1cc49d95 | Mark McLoughlin | |
479 | 1cc49d95 | Mark McLoughlin | qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a); |
480 | 7e049b8a | pbrook | } |