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