[1a3d1f3e] | 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 |
---|
[c499856] | 9 | * http://www.rtems.org/license/LICENSE. |
---|
[1a3d1f3e] | 10 | */ |
---|
[d8d6a08] | 11 | |
---|
| 12 | #define __INSIDE_RTEMS_BSD_TCPIP_STACK__ |
---|
| 13 | |
---|
[1a3d1f3e] | 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> |
---|
[d000bd0] | 24 | #include <rtems/bspIo.h> |
---|
[52e44630] | 25 | #include <assert.h> |
---|
[1a3d1f3e] | 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 | |
---|
[b2d7cf06] | 37 | #include <bsp/irq.h> |
---|
[1a3d1f3e] | 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 | |
---|
[1f4321b] | 45 | static void enet_isr(void *); |
---|
[52e44630] | 46 | static void enet_isr_on(void); |
---|
| 47 | |
---|
[1a3d1f3e] | 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; |
---|
[32b8506] | 70 | |
---|
[1a3d1f3e] | 71 | int accept_bcast; |
---|
[32b8506] | 72 | |
---|
[1a3d1f3e] | 73 | /* Tasks waiting for interrupts */ |
---|
| 74 | rtems_id rx_task; |
---|
| 75 | rtems_id tx_task; |
---|
[32b8506] | 76 | |
---|
[1a3d1f3e] | 77 | eth_stats_t stats; |
---|
| 78 | |
---|
| 79 | } mc9328mxl_enet_softc_t; |
---|
| 80 | |
---|
| 81 | static mc9328mxl_enet_softc_t softc; |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | /* function prototypes */ |
---|
[32b8506] | 85 | int rtems_mc9328mxl_enet_attach(struct rtems_bsdnet_ifconfig *config, |
---|
[1a3d1f3e] | 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); |
---|
[32b8506] | 95 | static int mc9328mxl_enet_ioctl(struct ifnet *ifp, |
---|
[7a5265ff] | 96 | ioctl_command_t command, caddr_t data); |
---|
[1a3d1f3e] | 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; |
---|
[32b8506] | 109 | |
---|
[1a3d1f3e] | 110 | /* |
---|
| 111 | * Parse driver name |
---|
| 112 | */ |
---|
| 113 | unitnumber = rtems_bsdnet_parse_driver_name(config, &unitname); |
---|
| 114 | if (unitnumber < 0) { |
---|
| 115 | return 0; |
---|
| 116 | } |
---|
[32b8506] | 117 | |
---|
[1a3d1f3e] | 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 | } |
---|
[32b8506] | 131 | |
---|
[1a3d1f3e] | 132 | /* zero out the control structure */ |
---|
| 133 | memset( &softc, 0, sizeof(softc) ); |
---|
[32b8506] | 134 | |
---|
| 135 | |
---|
[1a3d1f3e] | 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; |
---|
[32b8506] | 156 | |
---|
[1a3d1f3e] | 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 | } |
---|
[32b8506] | 172 | |
---|
[1a3d1f3e] | 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 | { |
---|
[32b8506] | 181 | mc9328mxl_enet_softc_t *sc = arg; |
---|
[1a3d1f3e] | 182 | struct ifnet *ifp = &sc->arpcom.ac_if; |
---|
[32b8506] | 183 | |
---|
| 184 | /* |
---|
| 185 | *This is for stuff that only gets done once (mc9328mxl_enet_init() |
---|
| 186 | * gets called multiple times |
---|
[1a3d1f3e] | 187 | */ |
---|
| 188 | if (sc->tx_task == 0) |
---|
| 189 | { |
---|
| 190 | /* Set up ENET hardware */ |
---|
| 191 | mc9328mxl_enet_init_hw(sc); |
---|
[32b8506] | 192 | |
---|
[1a3d1f3e] | 193 | /* Start driver tasks */ |
---|
[32b8506] | 194 | sc->rx_task = rtems_bsdnet_newproc("ENrx", |
---|
| 195 | 4096, |
---|
| 196 | mc9328mxl_enet_rx_task, |
---|
[1a3d1f3e] | 197 | sc); |
---|
[32b8506] | 198 | sc->tx_task = rtems_bsdnet_newproc("ENtx", |
---|
| 199 | 4096, |
---|
| 200 | mc9328mxl_enet_tx_task, |
---|
[1a3d1f3e] | 201 | sc); |
---|
| 202 | } /* if tx_task */ |
---|
[32b8506] | 203 | |
---|
[1a3d1f3e] | 204 | |
---|
| 205 | /* Configure for promiscuous if needed */ |
---|
| 206 | if (ifp->if_flags & IFF_PROMISC) { |
---|
[32b8506] | 207 | lan91c11x_write_reg(LAN91C11X_RCR, |
---|
[1a3d1f3e] | 208 | (lan91c11x_read_reg(LAN91C11X_RCR) | |
---|
| 209 | LAN91C11X_RCR_PRMS)); |
---|
| 210 | } |
---|
[32b8506] | 211 | |
---|
| 212 | |
---|
[1a3d1f3e] | 213 | /* |
---|
| 214 | * Tell the world that we're running. |
---|
| 215 | */ |
---|
| 216 | ifp->if_flags |= IFF_RUNNING; |
---|
[32b8506] | 217 | |
---|
[1a3d1f3e] | 218 | /* Enable TX/RX */ |
---|
[32b8506] | 219 | lan91c11x_write_reg(LAN91C11X_TCR, |
---|
[1a3d1f3e] | 220 | (lan91c11x_read_reg(LAN91C11X_TCR) | |
---|
| 221 | LAN91C11X_TCR_TXENA)); |
---|
| 222 | |
---|
[32b8506] | 223 | lan91c11x_write_reg(LAN91C11X_RCR, |
---|
[1a3d1f3e] | 224 | (lan91c11x_read_reg(LAN91C11X_RCR) | |
---|
| 225 | LAN91C11X_RCR_RXEN)); |
---|
[32b8506] | 226 | |
---|
[1a3d1f3e] | 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; |
---|
[52e44630] | 234 | rtems_status_code status = RTEMS_SUCCESSFUL; |
---|
[1a3d1f3e] | 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); |
---|
[32b8506] | 248 | |
---|
[1a3d1f3e] | 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 | } |
---|
[32b8506] | 268 | |
---|
[1a3d1f3e] | 269 | my |= PHY_ADV_CSMA; |
---|
[32b8506] | 270 | |
---|
[1a3d1f3e] | 271 | lan91c11x_write_phy_reg(PHY_AD, my); |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | /* Enable Autonegotiation */ |
---|
| 275 | #if 0 |
---|
[32b8506] | 276 | lan91c11x_write_phy_reg(PHY_CTRL, |
---|
[1a3d1f3e] | 277 | (PHY_CTRL_ANEGEN | PHY_CTRL_ANEGRST)); |
---|
| 278 | #endif |
---|
| 279 | |
---|
| 280 | /* Enable full duplex, let MAC take care |
---|
| 281 | * of padding and CRC. |
---|
| 282 | */ |
---|
[32b8506] | 283 | lan91c11x_write_reg(LAN91C11X_TCR, |
---|
[1a3d1f3e] | 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 */ |
---|
[32b8506] | 291 | lan91c11x_write_reg(LAN91C11X_RPCR, |
---|
[1a3d1f3e] | 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); |
---|
[32b8506] | 302 | |
---|
[1a3d1f3e] | 303 | /* Reset MMU */ |
---|
[32b8506] | 304 | lan91c11x_write_reg(LAN91C11X_MMUCMD, |
---|
[1a3d1f3e] | 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 */ |
---|
[32b8506] | 318 | MC9328MXL_GPIOA_DDIR &= ~bit(3); |
---|
[1a3d1f3e] | 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 */ |
---|
[52e44630] | 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(); |
---|
[32b8506] | 340 | |
---|
[1a3d1f3e] | 341 | } /* mc9328mxl_enet_init_hw() */ |
---|
| 342 | |
---|
| 343 | void mc9328mxl_enet_start(struct ifnet *ifp) |
---|
| 344 | { |
---|
| 345 | mc9328mxl_enet_softc_t *sc = ifp->if_softc; |
---|
[32b8506] | 346 | |
---|
[26e90fb1] | 347 | rtems_bsdnet_event_send(sc->tx_task, START_TRANSMIT_EVENT); |
---|
[1a3d1f3e] | 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; |
---|
[32b8506] | 354 | |
---|
[1a3d1f3e] | 355 | ifp->if_flags &= ~IFF_RUNNING; |
---|
[32b8506] | 356 | |
---|
[1a3d1f3e] | 357 | |
---|
| 358 | /* Stop the transmitter and receiver. */ |
---|
[32b8506] | 359 | lan91c11x_write_reg(LAN91C11X_TCR, |
---|
| 360 | (lan91c11x_read_reg(LAN91C11X_TCR) & |
---|
[1a3d1f3e] | 361 | ~LAN91C11X_TCR_TXENA)); |
---|
| 362 | |
---|
[32b8506] | 363 | lan91c11x_write_reg(LAN91C11X_RCR, |
---|
| 364 | (lan91c11x_read_reg(LAN91C11X_RCR) & |
---|
[1a3d1f3e] | 365 | ~LAN91C11X_RCR_RXEN)); |
---|
[32b8506] | 366 | |
---|
[1a3d1f3e] | 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; |
---|
[32b8506] | 378 | |
---|
[1a3d1f3e] | 379 | for (;;) |
---|
| 380 | { |
---|
| 381 | rtems_bsdnet_event_receive( |
---|
| 382 | START_TRANSMIT_EVENT, |
---|
| 383 | RTEMS_EVENT_ANY | RTEMS_WAIT, |
---|
| 384 | RTEMS_NO_TIMEOUT, |
---|
| 385 | &events); |
---|
[32b8506] | 386 | |
---|
[1a3d1f3e] | 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++; |
---|
[32b8506] | 397 | |
---|
[1a3d1f3e] | 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 */ |
---|
[32b8506] | 421 | lan91c11x_write_reg(LAN91C11X_MMUCMD, |
---|
[1a3d1f3e] | 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 | } |
---|
[32b8506] | 448 | lan91c11x_write_reg(LAN91C11X_DATA, size + 6); |
---|
[1a3d1f3e] | 449 | |
---|
| 450 | lan91c11x_lock(); |
---|
| 451 | |
---|
| 452 | /* Copy the mbuf */ |
---|
[32b8506] | 453 | l = m; |
---|
[1a3d1f3e] | 454 | start = 0; |
---|
| 455 | d = 0; |
---|
| 456 | while (l != NULL) |
---|
| 457 | { |
---|
| 458 | uint8_t *data; |
---|
[32b8506] | 459 | |
---|
[1a3d1f3e] | 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 | } |
---|
[32b8506] | 476 | |
---|
[1a3d1f3e] | 477 | /* write control byte */ |
---|
| 478 | if (i & 1) { |
---|
[32b8506] | 479 | lan91c11x_write_reg_fast(LAN91C11X_DATA, |
---|
[1a3d1f3e] | 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 */ |
---|
[32b8506] | 488 | lan91c11x_write_reg(LAN91C11X_INT, |
---|
[1a3d1f3e] | 489 | (lan91c11x_read_reg(LAN91C11X_INT) | |
---|
| 490 | LAN91C11X_INT_TXMASK | |
---|
| 491 | LAN91C11X_INT_TXEMASK)); |
---|
| 492 | |
---|
| 493 | /* Enqueue it */ |
---|
[32b8506] | 494 | lan91c11x_write_reg(LAN91C11X_MMUCMD, |
---|
[1a3d1f3e] | 495 | LAN91C11X_MMUCMD_ENQUEUE); |
---|
[32b8506] | 496 | |
---|
[1a3d1f3e] | 497 | /* free the mbuf chain we just copied */ |
---|
| 498 | m_freem(m); |
---|
[32b8506] | 499 | |
---|
[1a3d1f3e] | 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 */ |
---|
[32b8506] | 527 | lan91c11x_write_reg(LAN91C11X_PTR, |
---|
[1a3d1f3e] | 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); |
---|
[32b8506] | 542 | |
---|
[1a3d1f3e] | 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(); |
---|
[32b8506] | 548 | |
---|
[1a3d1f3e] | 549 | /* Copy the received packet into an mbuf */ |
---|
| 550 | for (i = 0; i < (pktlen / 2); i++) { |
---|
[32b8506] | 551 | ((uint16_t*)m->m_ext.ext_buf)[i] = |
---|
[1a3d1f3e] | 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 */ |
---|
[32b8506] | 563 | lan91c11x_write_reg(LAN91C11X_MMUCMD, |
---|
[1a3d1f3e] | 564 | LAN91C11X_MMUCMD_REMTOP); |
---|
| 565 | |
---|
| 566 | /* set the receiving interface */ |
---|
| 567 | m->m_pkthdr.rcvif = ifp; |
---|
| 568 | m->m_nextpkt = 0; |
---|
[32b8506] | 569 | |
---|
[1a3d1f3e] | 570 | /* set the length of the mbuf */ |
---|
| 571 | m->m_len = pktlen - (sizeof(struct ether_header)); |
---|
| 572 | m->m_pkthdr.len = m->m_len; |
---|
[32b8506] | 573 | |
---|
[1a3d1f3e] | 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); |
---|
[32b8506] | 578 | |
---|
| 579 | |
---|
[1a3d1f3e] | 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 | } |
---|
[32b8506] | 591 | } /* mc9328mxl_enet_rx_task */ |
---|
[1a3d1f3e] | 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. */ |
---|
[52e44630] | 607 | static void enet_isr_on(void) |
---|
[1a3d1f3e] | 608 | { |
---|
| 609 | /* Enable interrupts */ |
---|
| 610 | MC9328MXL_AITC_INTENNUM = MC9328MXL_INT_GPIO_PORTA; |
---|
[32b8506] | 611 | |
---|
[1a3d1f3e] | 612 | return; |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | /* Driver ioctl handler */ |
---|
| 616 | static int |
---|
[7a5265ff] | 617 | mc9328mxl_enet_ioctl (struct ifnet *ifp, ioctl_command_t command, caddr_t data) |
---|
[1a3d1f3e] | 618 | { |
---|
| 619 | mc9328mxl_enet_softc_t *sc = ifp->if_softc; |
---|
| 620 | int error = 0; |
---|
[32b8506] | 621 | |
---|
[1a3d1f3e] | 622 | switch (command) { |
---|
| 623 | case SIOCGIFADDR: |
---|
| 624 | case SIOCSIFADDR: |
---|
| 625 | ether_ioctl (ifp, command, data); |
---|
| 626 | break; |
---|
[32b8506] | 627 | |
---|
[1a3d1f3e] | 628 | case SIOCSIFFLAGS: |
---|
| 629 | switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) |
---|
| 630 | { |
---|
| 631 | case IFF_RUNNING: |
---|
| 632 | mc9328mxl_enet_stop (sc); |
---|
| 633 | break; |
---|
[32b8506] | 634 | |
---|
[1a3d1f3e] | 635 | case IFF_UP: |
---|
| 636 | mc9328mxl_enet_init (sc); |
---|
| 637 | break; |
---|
[32b8506] | 638 | |
---|
[1a3d1f3e] | 639 | case IFF_UP | IFF_RUNNING: |
---|
| 640 | mc9328mxl_enet_stop (sc); |
---|
| 641 | mc9328mxl_enet_init (sc); |
---|
| 642 | break; |
---|
[32b8506] | 643 | |
---|
[1a3d1f3e] | 644 | default: |
---|
| 645 | break; |
---|
| 646 | } /* switch (if_flags) */ |
---|
| 647 | break; |
---|
[32b8506] | 648 | |
---|
[1a3d1f3e] | 649 | case SIO_RTEMS_SHOW_STATS: |
---|
| 650 | mc9328mxl_enet_stats (sc); |
---|
| 651 | break; |
---|
[32b8506] | 652 | |
---|
[1a3d1f3e] | 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 */ |
---|
[1f4321b] | 664 | static void enet_isr(void * unused) |
---|
[1a3d1f3e] | 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); |
---|
[32b8506] | 671 | |
---|
[1a3d1f3e] | 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; |
---|
[32b8506] | 678 | |
---|
[26e90fb1] | 679 | rtems_bsdnet_event_send (softc.rx_task, START_RECEIVE_EVENT); |
---|
[1a3d1f3e] | 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 | |
---|
[26e90fb1] | 692 | rtems_bsdnet_event_send(softc.tx_task, START_TRANSMIT_EVENT); |
---|
[1a3d1f3e] | 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 | } |
---|
[32b8506] | 701 | |
---|
[1a3d1f3e] | 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 | |
---|