1 | /* |
---|
2 | * MC9323MXL Ethernet driver |
---|
3 | * |
---|
4 | * Copyright (c) 2004 by Cogent Computer Systems |
---|
5 | * Written by Jay Monkman <jtm@lopingdog.com> |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.org/license/LICENSE. |
---|
10 | */ |
---|
11 | |
---|
12 | #define __INSIDE_RTEMS_BSD_TCPIP_STACK__ |
---|
13 | |
---|
14 | #include <rtems.h> |
---|
15 | #include <rtems/rtems_bsdnet.h> |
---|
16 | #include <mc9328mxl.h> |
---|
17 | #include "lan91c11x.h" |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <string.h> |
---|
21 | |
---|
22 | #include <errno.h> |
---|
23 | #include <rtems/error.h> |
---|
24 | #include <rtems/bspIo.h> |
---|
25 | #include <assert.h> |
---|
26 | |
---|
27 | #include <sys/param.h> |
---|
28 | #include <sys/mbuf.h> |
---|
29 | #include <sys/socket.h> |
---|
30 | #include <sys/sockio.h> |
---|
31 | |
---|
32 | #include <net/if.h> |
---|
33 | |
---|
34 | #include <netinet/in.h> |
---|
35 | #include <netinet/if_ether.h> |
---|
36 | |
---|
37 | #include <bsp/irq.h> |
---|
38 | |
---|
39 | /* RTEMS event used by interrupt handler to start receive daemon. */ |
---|
40 | #define START_RECEIVE_EVENT RTEMS_EVENT_1 |
---|
41 | |
---|
42 | /* RTEMS event used to start transmit daemon. */ |
---|
43 | #define START_TRANSMIT_EVENT RTEMS_EVENT_2 |
---|
44 | |
---|
45 | static void enet_isr(void *); |
---|
46 | static void enet_isr_on(void); |
---|
47 | |
---|
48 | typedef struct { |
---|
49 | unsigned long rx_packets; /* total packets received */ |
---|
50 | unsigned long tx_packets; /* total packets transmitted */ |
---|
51 | unsigned long rx_bytes; /* total bytes received */ |
---|
52 | unsigned long tx_bytes; /* total bytes transmitted */ |
---|
53 | unsigned long interrupts; /* total number of interrupts */ |
---|
54 | unsigned long rx_interrupts; /* total number of rx interrupts */ |
---|
55 | unsigned long tx_interrupts; /* total number of tx interrupts */ |
---|
56 | unsigned long txerr_interrupts; /* total number of tx error interrupts */ |
---|
57 | |
---|
58 | } eth_stats_t; |
---|
59 | |
---|
60 | /* |
---|
61 | * Hardware-specific storage |
---|
62 | */ |
---|
63 | typedef struct |
---|
64 | { |
---|
65 | /* |
---|
66 | * Connection to networking code |
---|
67 | * This entry *must* be the first in the sonic_softc structure. |
---|
68 | */ |
---|
69 | struct arpcom arpcom; |
---|
70 | |
---|
71 | int accept_bcast; |
---|
72 | |
---|
73 | /* Tasks waiting for interrupts */ |
---|
74 | rtems_id rx_task; |
---|
75 | rtems_id tx_task; |
---|
76 | |
---|
77 | eth_stats_t stats; |
---|
78 | |
---|
79 | } mc9328mxl_enet_softc_t; |
---|
80 | |
---|
81 | static mc9328mxl_enet_softc_t softc; |
---|
82 | |
---|
83 | |
---|
84 | /* function prototypes */ |
---|
85 | int rtems_mc9328mxl_enet_attach(struct rtems_bsdnet_ifconfig *config, |
---|
86 | void *chip); |
---|
87 | void mc9328mxl_enet_init(void *arg); |
---|
88 | void mc9328mxl_enet_init_hw(mc9328mxl_enet_softc_t *sc); |
---|
89 | void mc9328mxl_enet_start(struct ifnet *ifp); |
---|
90 | void mc9328mxl_enet_stop (mc9328mxl_enet_softc_t *sc); |
---|
91 | void mc9328mxl_enet_tx_task (void *arg); |
---|
92 | void mc9328mxl_enet_sendpacket (struct ifnet *ifp, struct mbuf *m); |
---|
93 | void mc9328mxl_enet_rx_task(void *arg); |
---|
94 | void mc9328mxl_enet_stats(mc9328mxl_enet_softc_t *sc); |
---|
95 | static int mc9328mxl_enet_ioctl(struct ifnet *ifp, |
---|
96 | ioctl_command_t command, caddr_t data); |
---|
97 | |
---|
98 | |
---|
99 | int rtems_mc9328mxl_enet_attach ( |
---|
100 | struct rtems_bsdnet_ifconfig *config, |
---|
101 | void *chip /* only one ethernet, so no chip number */ |
---|
102 | ) |
---|
103 | { |
---|
104 | struct ifnet *ifp; |
---|
105 | int mtu; |
---|
106 | int unitnumber; |
---|
107 | char *unitname; |
---|
108 | int tmp; |
---|
109 | |
---|
110 | /* |
---|
111 | * Parse driver name |
---|
112 | */ |
---|
113 | unitnumber = rtems_bsdnet_parse_driver_name(config, &unitname); |
---|
114 | if (unitnumber < 0) { |
---|
115 | return 0; |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | * Is driver free? |
---|
120 | */ |
---|
121 | if (unitnumber != 0) { |
---|
122 | printf ("Bad MC9328MXL unit number.\n"); |
---|
123 | return 0; |
---|
124 | } |
---|
125 | |
---|
126 | ifp = &softc.arpcom.ac_if; |
---|
127 | if (ifp->if_softc != NULL) { |
---|
128 | printf ("Driver already in use.\n"); |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | /* zero out the control structure */ |
---|
133 | memset( &softc, 0, sizeof(softc) ); |
---|
134 | |
---|
135 | |
---|
136 | /* set the MAC address */ |
---|
137 | tmp = lan91c11x_read_reg(LAN91C11X_IA0); |
---|
138 | softc.arpcom.ac_enaddr[0] = tmp & 0xff; |
---|
139 | softc.arpcom.ac_enaddr[1] = (tmp >> 8) & 0xff; |
---|
140 | |
---|
141 | tmp = lan91c11x_read_reg(LAN91C11X_IA2); |
---|
142 | softc.arpcom.ac_enaddr[2] = tmp & 0xff; |
---|
143 | softc.arpcom.ac_enaddr[3] = (tmp >> 8) & 0xff; |
---|
144 | |
---|
145 | tmp = lan91c11x_read_reg(LAN91C11X_IA4); |
---|
146 | softc.arpcom.ac_enaddr[4] = tmp & 0xff; |
---|
147 | softc.arpcom.ac_enaddr[5] = (tmp >> 8) & 0xff; |
---|
148 | |
---|
149 | if (config->mtu) { |
---|
150 | mtu = config->mtu; |
---|
151 | } else { |
---|
152 | mtu = ETHERMTU; |
---|
153 | } |
---|
154 | |
---|
155 | softc.accept_bcast = !config->ignore_broadcast; |
---|
156 | |
---|
157 | /* |
---|
158 | * Set up network interface values |
---|
159 | */ |
---|
160 | ifp->if_softc = &softc; |
---|
161 | ifp->if_unit = unitnumber; |
---|
162 | ifp->if_name = unitname; |
---|
163 | ifp->if_mtu = mtu; |
---|
164 | ifp->if_init = mc9328mxl_enet_init; |
---|
165 | ifp->if_ioctl = mc9328mxl_enet_ioctl; |
---|
166 | ifp->if_start = mc9328mxl_enet_start; |
---|
167 | ifp->if_output = ether_output; |
---|
168 | ifp->if_flags = IFF_BROADCAST; |
---|
169 | if (ifp->if_snd.ifq_maxlen == 0) { |
---|
170 | ifp->if_snd.ifq_maxlen = ifqmaxlen; |
---|
171 | } |
---|
172 | |
---|
173 | /* Attach the interface */ |
---|
174 | if_attach (ifp); |
---|
175 | ether_ifattach (ifp); |
---|
176 | return 1; |
---|
177 | } |
---|
178 | |
---|
179 | void mc9328mxl_enet_init(void *arg) |
---|
180 | { |
---|
181 | mc9328mxl_enet_softc_t *sc = arg; |
---|
182 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
183 | |
---|
184 | /* |
---|
185 | *This is for stuff that only gets done once (mc9328mxl_enet_init() |
---|
186 | * gets called multiple times |
---|
187 | */ |
---|
188 | if (sc->tx_task == 0) |
---|
189 | { |
---|
190 | /* Set up ENET hardware */ |
---|
191 | mc9328mxl_enet_init_hw(sc); |
---|
192 | |
---|
193 | /* Start driver tasks */ |
---|
194 | sc->rx_task = rtems_bsdnet_newproc("ENrx", |
---|
195 | 4096, |
---|
196 | mc9328mxl_enet_rx_task, |
---|
197 | sc); |
---|
198 | sc->tx_task = rtems_bsdnet_newproc("ENtx", |
---|
199 | 4096, |
---|
200 | mc9328mxl_enet_tx_task, |
---|
201 | sc); |
---|
202 | } /* if tx_task */ |
---|
203 | |
---|
204 | |
---|
205 | /* Configure for promiscuous if needed */ |
---|
206 | if (ifp->if_flags & IFF_PROMISC) { |
---|
207 | lan91c11x_write_reg(LAN91C11X_RCR, |
---|
208 | (lan91c11x_read_reg(LAN91C11X_RCR) | |
---|
209 | LAN91C11X_RCR_PRMS)); |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | /* |
---|
214 | * Tell the world that we're running. |
---|
215 | */ |
---|
216 | ifp->if_flags |= IFF_RUNNING; |
---|
217 | |
---|
218 | /* Enable TX/RX */ |
---|
219 | lan91c11x_write_reg(LAN91C11X_TCR, |
---|
220 | (lan91c11x_read_reg(LAN91C11X_TCR) | |
---|
221 | LAN91C11X_TCR_TXENA)); |
---|
222 | |
---|
223 | lan91c11x_write_reg(LAN91C11X_RCR, |
---|
224 | (lan91c11x_read_reg(LAN91C11X_RCR) | |
---|
225 | LAN91C11X_RCR_RXEN)); |
---|
226 | |
---|
227 | |
---|
228 | } /* mc9328mxl_enet_init() */ |
---|
229 | |
---|
230 | void mc9328mxl_enet_init_hw(mc9328mxl_enet_softc_t *sc) |
---|
231 | { |
---|
232 | uint16_t stat; |
---|
233 | uint16_t my = 0; |
---|
234 | rtems_status_code status = RTEMS_SUCCESSFUL; |
---|
235 | |
---|
236 | lan91c11x_write_reg(LAN91C11X_RCR, LAN91C11X_RCR_RST); |
---|
237 | lan91c11x_write_reg(LAN91C11X_RCR, 0); |
---|
238 | rtems_task_wake_after(1); |
---|
239 | |
---|
240 | /* Reset the PHY */ |
---|
241 | lan91c11x_write_phy_reg(PHY_CTRL, PHY_CTRL_RST); |
---|
242 | while(lan91c11x_read_phy_reg(PHY_CTRL) & PHY_CTRL_RST) { |
---|
243 | rtems_task_wake_after(1); |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | stat = lan91c11x_read_phy_reg(PHY_STAT); |
---|
248 | |
---|
249 | if(stat & PHY_STAT_CAPT4) { |
---|
250 | my |= PHY_ADV_T4; |
---|
251 | } |
---|
252 | /* 100Mbs doesn't work, so we won't advertise it */ |
---|
253 | |
---|
254 | if(stat & PHY_STAT_CAPTXF) { |
---|
255 | my |= PHY_ADV_TXFDX; |
---|
256 | } |
---|
257 | if(stat & PHY_STAT_CAPTXH) { |
---|
258 | my |= PHY_ADV_TXHDX; |
---|
259 | } |
---|
260 | |
---|
261 | if(stat & PHY_STAT_CAPTF) { |
---|
262 | my |= PHY_ADV_10FDX; |
---|
263 | } |
---|
264 | |
---|
265 | if(stat & PHY_STAT_CAPTH) { |
---|
266 | my |= PHY_ADV_10HDX; |
---|
267 | } |
---|
268 | |
---|
269 | my |= PHY_ADV_CSMA; |
---|
270 | |
---|
271 | lan91c11x_write_phy_reg(PHY_AD, my); |
---|
272 | |
---|
273 | |
---|
274 | /* Enable Autonegotiation */ |
---|
275 | #if 0 |
---|
276 | lan91c11x_write_phy_reg(PHY_CTRL, |
---|
277 | (PHY_CTRL_ANEGEN | PHY_CTRL_ANEGRST)); |
---|
278 | #endif |
---|
279 | |
---|
280 | /* Enable full duplex, let MAC take care |
---|
281 | * of padding and CRC. |
---|
282 | */ |
---|
283 | lan91c11x_write_reg(LAN91C11X_TCR, |
---|
284 | (LAN91C11X_TCR_PADEN | |
---|
285 | LAN91C11X_TCR_SWFDUP)); |
---|
286 | |
---|
287 | /* Disable promisc, don'tstrip CRC */ |
---|
288 | lan91c11x_write_reg(LAN91C11X_RCR, 0); |
---|
289 | |
---|
290 | /* Enable auto-negotiation, LEDA is link, LEDB is traffic */ |
---|
291 | lan91c11x_write_reg(LAN91C11X_RPCR, |
---|
292 | (LAN91C11X_RPCR_ANEG | |
---|
293 | LAN91C11X_RPCR_LS2B)); |
---|
294 | |
---|
295 | /* Don't add wait states, enable PHY power */ |
---|
296 | lan91c11x_write_reg(LAN91C11X_CONFIG, |
---|
297 | (LAN91C11X_CONFIG_NOWAIT | |
---|
298 | LAN91C11X_CONFIG_PWR)); |
---|
299 | |
---|
300 | /* Disable error interrupts, enable auto release */ |
---|
301 | lan91c11x_write_reg(LAN91C11X_CTRL, LAN91C11X_CTRL_AUTO); |
---|
302 | |
---|
303 | /* Reset MMU */ |
---|
304 | lan91c11x_write_reg(LAN91C11X_MMUCMD, |
---|
305 | LAN91C11X_MMUCMD_RESETMMU ); |
---|
306 | |
---|
307 | |
---|
308 | rtems_task_wake_after(100); |
---|
309 | /* Enable Autonegotiation */ |
---|
310 | lan91c11x_write_phy_reg(PHY_CTRL, 0x3000); |
---|
311 | rtems_task_wake_after(100); |
---|
312 | |
---|
313 | /* Enable Interrupts for RX */ |
---|
314 | lan91c11x_write_reg(LAN91C11X_INT, LAN91C11X_INT_RXMASK); |
---|
315 | |
---|
316 | /* Enable interrupts on GPIO Port A3 */ |
---|
317 | /* Make pin 3 an input */ |
---|
318 | MC9328MXL_GPIOA_DDIR &= ~bit(3); |
---|
319 | |
---|
320 | /* Use GPIO function for pin 3 */ |
---|
321 | MC9328MXL_GPIOA_GIUS |= bit(3); |
---|
322 | |
---|
323 | /* Set for active high, level triggered interupt */ |
---|
324 | MC9328MXL_GPIOA_ICR1 = ((MC9328MXL_GPIOA_ICR1 & ~(3 << 6)) | |
---|
325 | (2 << 6)); |
---|
326 | |
---|
327 | /* Enable GPIO port A3 interrupt */ |
---|
328 | MC9328MXL_GPIOA_IMR |= bit(3); |
---|
329 | |
---|
330 | /* Install the interrupt handler */ |
---|
331 | status = rtems_interrupt_handler_install( |
---|
332 | BSP_INT_GPIO_PORTA, |
---|
333 | "Network", |
---|
334 | RTEMS_INTERRUPT_UNIQUE, |
---|
335 | enet_isr, |
---|
336 | (void *)BSP_INT_GPIO_PORTA |
---|
337 | ); |
---|
338 | assert(status == RTEMS_SUCCESSFUL); |
---|
339 | enet_isr_on(); |
---|
340 | |
---|
341 | } /* mc9328mxl_enet_init_hw() */ |
---|
342 | |
---|
343 | void mc9328mxl_enet_start(struct ifnet *ifp) |
---|
344 | { |
---|
345 | mc9328mxl_enet_softc_t *sc = ifp->if_softc; |
---|
346 | |
---|
347 | rtems_bsdnet_event_send(sc->tx_task, START_TRANSMIT_EVENT); |
---|
348 | ifp->if_flags |= IFF_OACTIVE; |
---|
349 | } |
---|
350 | |
---|
351 | void mc9328mxl_enet_stop (mc9328mxl_enet_softc_t *sc) |
---|
352 | { |
---|
353 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
354 | |
---|
355 | ifp->if_flags &= ~IFF_RUNNING; |
---|
356 | |
---|
357 | |
---|
358 | /* Stop the transmitter and receiver. */ |
---|
359 | lan91c11x_write_reg(LAN91C11X_TCR, |
---|
360 | (lan91c11x_read_reg(LAN91C11X_TCR) & |
---|
361 | ~LAN91C11X_TCR_TXENA)); |
---|
362 | |
---|
363 | lan91c11x_write_reg(LAN91C11X_RCR, |
---|
364 | (lan91c11x_read_reg(LAN91C11X_RCR) & |
---|
365 | ~LAN91C11X_RCR_RXEN)); |
---|
366 | |
---|
367 | } |
---|
368 | |
---|
369 | /* |
---|
370 | * Driver transmit daemon |
---|
371 | */ |
---|
372 | void mc9328mxl_enet_tx_task(void *arg) |
---|
373 | { |
---|
374 | mc9328mxl_enet_softc_t *sc = (mc9328mxl_enet_softc_t *)arg; |
---|
375 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
376 | struct mbuf *m; |
---|
377 | rtems_event_set events; |
---|
378 | |
---|
379 | for (;;) |
---|
380 | { |
---|
381 | rtems_bsdnet_event_receive( |
---|
382 | START_TRANSMIT_EVENT, |
---|
383 | RTEMS_EVENT_ANY | RTEMS_WAIT, |
---|
384 | RTEMS_NO_TIMEOUT, |
---|
385 | &events); |
---|
386 | |
---|
387 | /* Send packets till queue is empty */ |
---|
388 | for (;;) |
---|
389 | { |
---|
390 | /* Get the next mbuf chain to transmit. */ |
---|
391 | IF_DEQUEUE(&ifp->if_snd, m); |
---|
392 | if (!m) { |
---|
393 | break; |
---|
394 | } |
---|
395 | mc9328mxl_enet_sendpacket (ifp, m); |
---|
396 | softc.stats.tx_packets++; |
---|
397 | |
---|
398 | } |
---|
399 | ifp->if_flags &= ~IFF_OACTIVE; |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | /* Send packet */ |
---|
404 | void mc9328mxl_enet_sendpacket (struct ifnet *ifp, struct mbuf *m) |
---|
405 | { |
---|
406 | struct mbuf *l = NULL; |
---|
407 | int size = 0; |
---|
408 | int tmp; |
---|
409 | int i; |
---|
410 | int start; |
---|
411 | uint16_t d; |
---|
412 | |
---|
413 | /* How big is the packet ? */ |
---|
414 | l = m; |
---|
415 | do { |
---|
416 | size += l->m_len; |
---|
417 | l = l->m_next; |
---|
418 | } while (l != NULL); |
---|
419 | |
---|
420 | /* Allocate a TX buffer */ |
---|
421 | lan91c11x_write_reg(LAN91C11X_MMUCMD, |
---|
422 | (LAN91C11X_MMUCMD_ALLOCTX | |
---|
423 | (size >> 8))); |
---|
424 | |
---|
425 | /* Wait for the allocation */ |
---|
426 | while ((lan91c11x_read_reg(LAN91C11X_INT) & LAN91C11X_INT_ALLOC) == 0) { |
---|
427 | continue; |
---|
428 | } |
---|
429 | |
---|
430 | tmp = lan91c11x_read_reg(LAN91C11X_PNR); |
---|
431 | lan91c11x_write_reg(LAN91C11X_PNR, ((tmp >> 8) & 0xff)); |
---|
432 | |
---|
433 | /* Set the data pointer for auto increment */ |
---|
434 | lan91c11x_write_reg(LAN91C11X_PTR, LAN91C11X_PTR_AUTOINC); |
---|
435 | |
---|
436 | /* A delay is needed between pointer and data access ?!? */ |
---|
437 | for (i = 0; i < 10; i++) { |
---|
438 | continue; |
---|
439 | } |
---|
440 | |
---|
441 | /* Write status word */ |
---|
442 | lan91c11x_write_reg(LAN91C11X_DATA, 0); |
---|
443 | |
---|
444 | /* Write byte count */ |
---|
445 | if (size & 1) { |
---|
446 | size++; |
---|
447 | } |
---|
448 | lan91c11x_write_reg(LAN91C11X_DATA, size + 6); |
---|
449 | |
---|
450 | lan91c11x_lock(); |
---|
451 | |
---|
452 | /* Copy the mbuf */ |
---|
453 | l = m; |
---|
454 | start = 0; |
---|
455 | d = 0; |
---|
456 | while (l != NULL) |
---|
457 | { |
---|
458 | uint8_t *data; |
---|
459 | |
---|
460 | data = mtod(l, uint8_t *); |
---|
461 | |
---|
462 | for (i = start; i < l->m_len; i++) { |
---|
463 | if ((i & 1) == 0) { |
---|
464 | d = data[i] << 8; |
---|
465 | } else { |
---|
466 | d = d | data[i]; |
---|
467 | lan91c11x_write_reg_fast(LAN91C11X_DATA, htons(d)); |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | /* If everything is 2 byte aligned, i will be even */ |
---|
472 | start = (i & 1); |
---|
473 | |
---|
474 | l = l->m_next; |
---|
475 | } |
---|
476 | |
---|
477 | /* write control byte */ |
---|
478 | if (i & 1) { |
---|
479 | lan91c11x_write_reg_fast(LAN91C11X_DATA, |
---|
480 | htons(LAN91C11X_PKT_CTRL_ODD | d)); |
---|
481 | } else { |
---|
482 | lan91c11x_write_reg_fast(LAN91C11X_DATA, 0); |
---|
483 | } |
---|
484 | |
---|
485 | lan91c11x_unlock(); |
---|
486 | |
---|
487 | /* Enable TX interrupts */ |
---|
488 | lan91c11x_write_reg(LAN91C11X_INT, |
---|
489 | (lan91c11x_read_reg(LAN91C11X_INT) | |
---|
490 | LAN91C11X_INT_TXMASK | |
---|
491 | LAN91C11X_INT_TXEMASK)); |
---|
492 | |
---|
493 | /* Enqueue it */ |
---|
494 | lan91c11x_write_reg(LAN91C11X_MMUCMD, |
---|
495 | LAN91C11X_MMUCMD_ENQUEUE); |
---|
496 | |
---|
497 | /* free the mbuf chain we just copied */ |
---|
498 | m_freem(m); |
---|
499 | |
---|
500 | } /* mc9328mxl_enet_sendpacket () */ |
---|
501 | |
---|
502 | |
---|
503 | /* reader task */ |
---|
504 | void mc9328mxl_enet_rx_task(void *arg) |
---|
505 | { |
---|
506 | mc9328mxl_enet_softc_t *sc = (mc9328mxl_enet_softc_t *)arg; |
---|
507 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
508 | struct mbuf *m; |
---|
509 | struct ether_header *eh; |
---|
510 | rtems_event_set events; |
---|
511 | int pktlen; |
---|
512 | uint16_t rsw; |
---|
513 | uint16_t bc; |
---|
514 | uint16_t cbyte; |
---|
515 | int i; |
---|
516 | uint16_t int_reg; |
---|
517 | |
---|
518 | /* Input packet handling loop */ |
---|
519 | while (1) { |
---|
520 | rtems_bsdnet_event_receive( |
---|
521 | START_RECEIVE_EVENT, |
---|
522 | RTEMS_EVENT_ANY | RTEMS_WAIT, |
---|
523 | RTEMS_NO_TIMEOUT, |
---|
524 | &events); |
---|
525 | |
---|
526 | /* Configure for reads from RX data area */ |
---|
527 | lan91c11x_write_reg(LAN91C11X_PTR, |
---|
528 | (LAN91C11X_PTR_AUTOINC | |
---|
529 | LAN91C11X_PTR_RCV | |
---|
530 | LAN91C11X_PTR_READ)); |
---|
531 | |
---|
532 | /* read the receive status word */ |
---|
533 | rsw = lan91c11x_read_reg(LAN91C11X_DATA); |
---|
534 | /* TBD: Need to check rsw here */ |
---|
535 | |
---|
536 | /* read the byte count */ |
---|
537 | bc = lan91c11x_read_reg(LAN91C11X_DATA); |
---|
538 | pktlen = (bc & 0x7ff) - 6; |
---|
539 | |
---|
540 | /* get an mbuf for this packet */ |
---|
541 | MGETHDR(m, M_WAIT, MT_DATA); |
---|
542 | |
---|
543 | /* now get a cluster pointed to by the mbuf */ |
---|
544 | /* since an mbuf by itself is too small */ |
---|
545 | MCLGET(m, M_WAIT); |
---|
546 | |
---|
547 | lan91c11x_lock(); |
---|
548 | |
---|
549 | /* Copy the received packet into an mbuf */ |
---|
550 | for (i = 0; i < (pktlen / 2); i++) { |
---|
551 | ((uint16_t*)m->m_ext.ext_buf)[i] = |
---|
552 | lan91c11x_read_reg_fast(LAN91C11X_DATA); |
---|
553 | } |
---|
554 | |
---|
555 | cbyte = lan91c11x_read_reg_fast(LAN91C11X_DATA); |
---|
556 | if (cbyte & LAN91C11X_PKT_CTRL_ODD) { |
---|
557 | ((uint16_t*)m->m_ext.ext_buf)[i] = cbyte; |
---|
558 | pktlen++; |
---|
559 | } |
---|
560 | lan91c11x_unlock(); |
---|
561 | |
---|
562 | /* Release the packets memory */ |
---|
563 | lan91c11x_write_reg(LAN91C11X_MMUCMD, |
---|
564 | LAN91C11X_MMUCMD_REMTOP); |
---|
565 | |
---|
566 | /* set the receiving interface */ |
---|
567 | m->m_pkthdr.rcvif = ifp; |
---|
568 | m->m_nextpkt = 0; |
---|
569 | |
---|
570 | /* set the length of the mbuf */ |
---|
571 | m->m_len = pktlen - (sizeof(struct ether_header)); |
---|
572 | m->m_pkthdr.len = m->m_len; |
---|
573 | |
---|
574 | /* strip off the ethernet header from the mbuf */ |
---|
575 | /* but save the pointer to it */ |
---|
576 | eh = mtod (m, struct ether_header *); |
---|
577 | m->m_data += sizeof(struct ether_header); |
---|
578 | |
---|
579 | |
---|
580 | softc.stats.rx_packets++; |
---|
581 | |
---|
582 | /* give all this stuff to the stack */ |
---|
583 | ether_input(ifp, eh, m); |
---|
584 | |
---|
585 | /* renable RX interrupts */ |
---|
586 | int_reg = lan91c11x_read_reg(LAN91C11X_INT); |
---|
587 | int_reg |= LAN91C11X_INT_RXMASK; |
---|
588 | lan91c11x_write_reg(LAN91C11X_INT, int_reg); |
---|
589 | |
---|
590 | } |
---|
591 | } /* mc9328mxl_enet_rx_task */ |
---|
592 | |
---|
593 | |
---|
594 | /* Show interface statistics */ |
---|
595 | void mc9328mxl_enet_stats (mc9328mxl_enet_softc_t *sc) |
---|
596 | { |
---|
597 | printf (" Total Interrupts:%-8lu", sc->stats.interrupts); |
---|
598 | printf (" Rx Interrupts:%-8lu", sc->stats.rx_interrupts); |
---|
599 | printf (" Tx Interrupts:%-8lu\n", sc->stats.tx_interrupts); |
---|
600 | printf (" Tx Error Interrupts:%-8lu\n", sc->stats.txerr_interrupts); |
---|
601 | printf (" Rx Packets:%-8lu", sc->stats.rx_packets); |
---|
602 | printf (" Tx Packets:%-8lu\n", sc->stats.tx_packets); |
---|
603 | } |
---|
604 | |
---|
605 | |
---|
606 | /* Enables mc9328mxl_enet interrupts. */ |
---|
607 | static void enet_isr_on(void) |
---|
608 | { |
---|
609 | /* Enable interrupts */ |
---|
610 | MC9328MXL_AITC_INTENNUM = MC9328MXL_INT_GPIO_PORTA; |
---|
611 | |
---|
612 | return; |
---|
613 | } |
---|
614 | |
---|
615 | /* Driver ioctl handler */ |
---|
616 | static int |
---|
617 | mc9328mxl_enet_ioctl (struct ifnet *ifp, ioctl_command_t command, caddr_t data) |
---|
618 | { |
---|
619 | mc9328mxl_enet_softc_t *sc = ifp->if_softc; |
---|
620 | int error = 0; |
---|
621 | |
---|
622 | switch (command) { |
---|
623 | case SIOCGIFADDR: |
---|
624 | case SIOCSIFADDR: |
---|
625 | ether_ioctl (ifp, command, data); |
---|
626 | break; |
---|
627 | |
---|
628 | case SIOCSIFFLAGS: |
---|
629 | switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) |
---|
630 | { |
---|
631 | case IFF_RUNNING: |
---|
632 | mc9328mxl_enet_stop (sc); |
---|
633 | break; |
---|
634 | |
---|
635 | case IFF_UP: |
---|
636 | mc9328mxl_enet_init (sc); |
---|
637 | break; |
---|
638 | |
---|
639 | case IFF_UP | IFF_RUNNING: |
---|
640 | mc9328mxl_enet_stop (sc); |
---|
641 | mc9328mxl_enet_init (sc); |
---|
642 | break; |
---|
643 | |
---|
644 | default: |
---|
645 | break; |
---|
646 | } /* switch (if_flags) */ |
---|
647 | break; |
---|
648 | |
---|
649 | case SIO_RTEMS_SHOW_STATS: |
---|
650 | mc9328mxl_enet_stats (sc); |
---|
651 | break; |
---|
652 | |
---|
653 | /* |
---|
654 | * FIXME: All sorts of multicast commands need to be added here! |
---|
655 | */ |
---|
656 | default: |
---|
657 | error = EINVAL; |
---|
658 | break; |
---|
659 | } /* switch (command) */ |
---|
660 | return error; |
---|
661 | } |
---|
662 | |
---|
663 | /* interrupt handler */ |
---|
664 | static void enet_isr(void * unused) |
---|
665 | { |
---|
666 | uint16_t int_reg; |
---|
667 | |
---|
668 | softc.stats.interrupts++; |
---|
669 | /* get the ISR status and determine RX or TX */ |
---|
670 | int_reg = lan91c11x_read_reg(LAN91C11X_INT); |
---|
671 | |
---|
672 | /* Handle RX interrupts */ |
---|
673 | if ((int_reg & LAN91C11X_INT_RX) && (int_reg & LAN91C11X_INT_RXMASK)) { |
---|
674 | softc.stats.rx_interrupts++; |
---|
675 | |
---|
676 | /* Disable the interrupt */ |
---|
677 | int_reg &= ~LAN91C11X_INT_RXMASK; |
---|
678 | |
---|
679 | rtems_bsdnet_event_send (softc.rx_task, START_RECEIVE_EVENT); |
---|
680 | } |
---|
681 | |
---|
682 | /* Handle TX Empty interrupts */ |
---|
683 | if ((int_reg & LAN91C11X_INT_TXE) && (int_reg & LAN91C11X_INT_TXEMASK)) { |
---|
684 | softc.stats.tx_interrupts++; |
---|
685 | |
---|
686 | /* Disable the interrupt */ |
---|
687 | int_reg &= ~LAN91C11X_INT_TXEMASK; |
---|
688 | |
---|
689 | /* Acknowledge the interrupt */ |
---|
690 | int_reg |= LAN91C11X_INT_TXE; |
---|
691 | |
---|
692 | rtems_bsdnet_event_send(softc.tx_task, START_TRANSMIT_EVENT); |
---|
693 | |
---|
694 | } |
---|
695 | |
---|
696 | /* Handle interrupts for transmit errors */ |
---|
697 | if ((int_reg & LAN91C11X_INT_TX) && (int_reg & LAN91C11X_INT_TXMASK)) { |
---|
698 | softc.stats.txerr_interrupts++; |
---|
699 | printk("Caught TX interrupt - error on transmission\n"); |
---|
700 | } |
---|
701 | |
---|
702 | /* Update the interrupt register on the 91c11x */ |
---|
703 | lan91c11x_write_reg(LAN91C11X_INT, int_reg); |
---|
704 | |
---|
705 | /* clear GPIO Int. status */ |
---|
706 | MC9328MXL_GPIOA_ISR |= bit(3); |
---|
707 | } |
---|
708 | |
---|