FreeBSD/Linux Kernel Cross Reference
sys/dev/xe/if_xe.c
1 /*-
2 * Copyright (c) 1998, 1999, 2003 Scott Mitchell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26 /*
27 * Portions of this software were derived from Werner Koch's xirc2ps driver
28 * for Linux under the terms of the following license (from v1.30 of the
29 * xirc2ps driver):
30 *
31 * Copyright (c) 1997 by Werner Koch (dd9jn)
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, and the entire permission notice in its entirety,
38 * including the disclaimer of warranties.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. The name of the author may not be used to endorse or promote
43 * products derived from this software without specific prior
44 * written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
47 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
49 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
50 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
52 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD: releng/5.3/sys/dev/xe/if_xe.c 133703 2004-08-14 00:15:26Z rwatson $");
61
62 /*
63 * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters. The
64 * following cards are currently known to work with the driver:
65 * Xircom CreditCard 10/100 (CE3)
66 * Xircom CreditCard Ethernet + Modem 28 (CEM28)
67 * Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
68 * Xircom RealPort Ethernet 10
69 * Xircom RealPort Ethernet 10/100
70 * Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
71 * Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
72 * Compaq Netelligent 10/100 PC Card (CPQ-10/100)
73 *
74 * Some other cards *should* work, but support for them is either broken or in
75 * an unknown state at the moment. I'm always interested in hearing from
76 * people who own any of these cards:
77 * Xircom CreditCard 10Base-T (PS-CE2-10)
78 * Xircom CreditCard Ethernet + ModemII (CEM2)
79 * Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
80 *
81 * Thanks to all who assisted with the development and testing of the driver,
82 * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
83 * Nelson, Mike Kephart, Bill Rainey and Douglas Rand. Apologies if I've left
84 * out anyone who deserves a mention here.
85 *
86 * Special thanks to Ade Lovett for both hosting the mailing list and doing
87 * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
88 * the web pages.
89 *
90 * Author email: <scott@uk.freebsd.org>
91 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
92 */
93
94
95 #include <sys/param.h>
96 #include <sys/cdefs.h>
97 #include <sys/errno.h>
98 #include <sys/kernel.h>
99 #include <sys/mbuf.h>
100 #include <sys/socket.h>
101 #include <sys/sockio.h>
102 #include <sys/systm.h>
103 #include <sys/uio.h>
104 #include <sys/sysctl.h>
105
106 #include <sys/module.h>
107 #include <sys/bus.h>
108
109 #include <machine/bus.h>
110 #include <machine/resource.h>
111 #include <sys/rman.h>
112
113 #include <net/ethernet.h>
114 #include <net/if.h>
115 #include <net/if_arp.h>
116 #include <net/if_dl.h>
117 #include <net/if_media.h>
118 #include <net/if_mib.h>
119 #include <net/bpf.h>
120
121 #include <dev/xe/if_xereg.h>
122 #include <dev/xe/if_xevar.h>
123
124 /*
125 * MII command structure
126 */
127 struct xe_mii_frame {
128 u_int8_t mii_stdelim;
129 u_int8_t mii_opcode;
130 u_int8_t mii_phyaddr;
131 u_int8_t mii_regaddr;
132 u_int8_t mii_turnaround;
133 u_int16_t mii_data;
134 };
135
136 /*
137 * Media autonegotiation progress constants
138 */
139 #define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
140 #define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
141 #define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
142 #define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
143 #define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
144
145 /*
146 * Prototypes start here
147 */
148 static void xe_init (void *xscp);
149 static void xe_start (struct ifnet *ifp);
150 static int xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data);
151 static void xe_watchdog (struct ifnet *ifp);
152 static int xe_media_change (struct ifnet *ifp);
153 static void xe_media_status (struct ifnet *ifp, struct ifmediareq *mrp);
154 static timeout_t xe_setmedia;
155 static void xe_reset (struct xe_softc *scp);
156 static void xe_stop (struct xe_softc *scp);
157 static void xe_enable_intr (struct xe_softc *scp);
158 static void xe_disable_intr (struct xe_softc *scp);
159 static void xe_set_multicast (struct xe_softc *scp);
160 static void xe_set_addr (struct xe_softc *scp, u_int8_t* addr, unsigned idx);
161 static void xe_mchash (struct xe_softc *scp, const uint8_t *addr);
162 static int xe_pio_write_packet (struct xe_softc *scp, struct mbuf *mbp);
163
164 /*
165 * MII functions
166 */
167 static void xe_mii_sync (struct xe_softc *scp);
168 static int xe_mii_init (struct xe_softc *scp);
169 static void xe_mii_send (struct xe_softc *scp, u_int32_t bits, int cnt);
170 static int xe_mii_readreg (struct xe_softc *scp, struct xe_mii_frame *frame);
171 static int xe_mii_writereg (struct xe_softc *scp, struct xe_mii_frame *frame);
172 static u_int16_t xe_phy_readreg (struct xe_softc *scp, u_int16_t reg);
173 static void xe_phy_writereg (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
174
175 /*
176 * Debugging functions
177 */
178 static void xe_mii_dump (struct xe_softc *scp);
179 #if 0
180 static void xe_reg_dump (struct xe_softc *scp);
181 #endif
182
183 /*
184 * Debug logging levels - set with hw.xe.debug sysctl
185 * 0 = None
186 * 1 = More hardware details, probe/attach progress
187 * 2 = Most function calls, ioctls and media selection progress
188 * 3 = Everything - interrupts, packets in/out and multicast address setup
189 */
190 #define XE_DEBUG
191 #ifdef XE_DEBUG
192
193 /* sysctl vars */
194 SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "if_xe parameters");
195
196 int xe_debug = 0;
197 SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW,
198 &xe_debug, 0, "if_xe debug level");
199
200 #define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
201 #define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
202 #define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
203 #if 0
204 #define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
205 #endif
206 #else
207 #define DEVPRINTF(level, arg)
208 #define DPRINTF(level, arg)
209 #define XE_MII_DUMP(scp)
210 #if 0
211 #define XE_REG_DUMP(scp)
212 #endif
213 #endif
214
215
216 /*
217 * Attach a device.
218 */
219 int
220 xe_attach (device_t dev)
221 {
222 struct xe_softc *scp = device_get_softc(dev);
223
224 DEVPRINTF(2, (dev, "attach\n"));
225
226 /* Initialise stuff... */
227 scp->dev = dev;
228 scp->ifp = &scp->arpcom.ac_if;
229 scp->ifm = &scp->ifmedia;
230 scp->autoneg_status = XE_AUTONEG_NONE;
231
232 /* Initialise the ifnet structure */
233 scp->ifp->if_softc = scp;
234 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
235 scp->ifp->if_timer = 0;
236 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
237 IFF_NEEDSGIANT);
238 scp->ifp->if_linkmib = &scp->mibdata;
239 scp->ifp->if_linkmiblen = sizeof scp->mibdata;
240 scp->ifp->if_start = xe_start;
241 scp->ifp->if_ioctl = xe_ioctl;
242 scp->ifp->if_watchdog = xe_watchdog;
243 scp->ifp->if_init = xe_init;
244 scp->ifp->if_baudrate = 100000000;
245 scp->ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
246
247 /* Initialise the ifmedia structure */
248 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
249 callout_handle_init(&scp->chand);
250
251 /* Add supported media types */
252 if (scp->mohawk) {
253 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
254 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
255 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
256 }
257 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
258 if (scp->ce2)
259 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
260 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
261
262 /* Default is to autoselect best supported media type */
263 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
264
265 /* Get the hardware into a known state */
266 xe_reset(scp);
267
268 /* Get hardware version numbers */
269 XE_SELECT_PAGE(4);
270 scp->version = XE_INB(XE_BOV);
271 if (scp->mohawk)
272 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
273 else
274 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
275
276 /* Print some useful information */
277 device_printf(dev, "%s %s, version 0x%02x/0x%02x%s%s\n",
278 scp->vendor,
279 scp->card_type,
280 scp->version,
281 scp->srev,
282 scp->mohawk ? ", 100Mbps capable" : "",
283 scp->modem ? ", with modem" : "");
284 if (scp->mohawk) {
285 XE_SELECT_PAGE(0x10);
286 DEVPRINTF(1, (dev, "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
287 XE_INW(XE_DINGOID),
288 XE_INW(XE_RevID),
289 XE_INW(XE_VendorID)));
290 }
291 if (scp->ce2) {
292 XE_SELECT_PAGE(0x45);
293 DEVPRINTF(1, (dev, "CE2 version = 0x%#02x\n", XE_INB(XE_REV)));
294 }
295
296 /* Attach the interface */
297 ether_ifattach(scp->ifp, scp->arpcom.ac_enaddr);
298
299 /* Done */
300 return 0;
301 }
302
303
304 /*
305 * Complete hardware intitialisation and enable output. Exits without doing
306 * anything if there's no address assigned to the card, or if media selection
307 * is in progress (the latter implies we've already run this function).
308 */
309 static void
310 xe_init(void *xscp) {
311 struct xe_softc *scp = xscp;
312 unsigned i;
313 int s;
314
315 if (scp->autoneg_status != XE_AUTONEG_NONE) return;
316
317 DEVPRINTF(2, (scp->dev, "init\n"));
318
319 s = splimp();
320
321 /* Reset transmitter flags */
322 scp->tx_queued = 0;
323 scp->tx_tpr = 0;
324 scp->tx_timeouts = 0;
325 scp->tx_thres = 64;
326 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
327 scp->ifp->if_timer = 0;
328
329 /* Soft reset the card */
330 XE_SELECT_PAGE(0);
331 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
332 DELAY(40000);
333 XE_OUTB(XE_CR, 0);
334 DELAY(40000);
335
336 if (scp->mohawk) {
337 /*
338 * set GP1 and GP2 as outputs (bits 2 & 3)
339 * set GP1 low to power on the ML6692 (bit 0)
340 * set GP2 high to power on the 10Mhz chip (bit 1)
341 */
342 XE_SELECT_PAGE(4);
343 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP2_OUT);
344 }
345
346 /* Shut off interrupts */
347 xe_disable_intr(scp);
348
349 /* Wait for everything to wake up */
350 DELAY(500000);
351
352 /* Check for PHY */
353 if (scp->mohawk)
354 scp->phy_ok = xe_mii_init(scp);
355
356 /* Disable 'source insertion' (not sure what that means) */
357 XE_SELECT_PAGE(0x42);
358 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
359
360 /* Set 8K/24K Tx/Rx buffer split */
361 if (scp->srev != 1) {
362 XE_SELECT_PAGE(2);
363 XE_OUTW(XE_RBS, 0x2000);
364 }
365
366 /* Enable early transmit mode on Mohawk/Dingo */
367 if (scp->mohawk) {
368 XE_SELECT_PAGE(0x03);
369 XE_OUTW(XE_TPT, scp->tx_thres);
370 XE_SELECT_PAGE(0x01);
371 XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
372 }
373
374 /* Put MAC address in first 'individual address' register */
375 XE_SELECT_PAGE(0x50);
376 for (i = 0; i < 6; i++)
377 XE_OUTB(0x08 + i, scp->arpcom.ac_enaddr[scp->mohawk ? 5 - i : i]);
378
379 /* Set up multicast addresses */
380 xe_set_multicast(scp);
381
382 /* Fix the receive data offset -- reset can leave it off-by-one */
383 XE_SELECT_PAGE(0);
384 XE_OUTW(XE_DO, 0x2000);
385
386 /* Set interrupt masks */
387 XE_SELECT_PAGE(1);
388 XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR | XE_IMR0_RX_PACKET);
389
390 /* Set MAC interrupt masks */
391 XE_SELECT_PAGE(0x40);
392 XE_OUTB(XE_RX0Msk,
393 ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR
394 | XE_RX0M_ALIGN_ERROR | XE_RX0M_LONG_PACKET));
395 XE_OUTB(XE_TX0Msk,
396 ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN
397 | XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
398
399 /* Clear MAC status registers */
400 XE_SELECT_PAGE(0x40);
401 XE_OUTB(XE_RST0, 0x00);
402 XE_OUTB(XE_TXST0, 0x00);
403
404 /* Enable receiver and put MAC online */
405 XE_SELECT_PAGE(0x40);
406 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
407
408 /* Set up IMR, enable interrupts */
409 xe_enable_intr(scp);
410
411 /* Start media selection */
412 xe_setmedia(scp);
413
414 /* Enable output */
415 scp->ifp->if_flags |= IFF_RUNNING;
416 scp->ifp->if_flags &= ~IFF_OACTIVE;
417
418 (void)splx(s);
419 }
420
421
422 /*
423 * Start output on interface. Should be called at splimp() priority. Check
424 * that the output is idle (ie, IFF_OACTIVE is not set) before calling this
425 * function. If media selection is in progress we set IFF_OACTIVE ourselves
426 * and return immediately.
427 */
428 static void
429 xe_start(struct ifnet *ifp) {
430 struct xe_softc *scp = ifp->if_softc;
431 struct mbuf *mbp;
432
433 if (scp->autoneg_status != XE_AUTONEG_NONE) {
434 ifp->if_flags |= IFF_OACTIVE;
435 return;
436 }
437
438 DEVPRINTF(3, (scp->dev, "start\n"));
439
440 /*
441 * Loop while there are packets to be sent, and space to send them.
442 */
443 while (1) {
444 /* Suck a packet off the send queue */
445 IF_DEQUEUE(&ifp->if_snd, mbp);
446
447 if (mbp == NULL) {
448 /*
449 * We are using the !OACTIVE flag to indicate to the outside world that
450 * we can accept an additional packet rather than that the transmitter
451 * is _actually_ active. Indeed, the transmitter may be active, but if
452 * we haven't filled all the buffers with data then we still want to
453 * accept more.
454 */
455 ifp->if_flags &= ~IFF_OACTIVE;
456 return;
457 }
458
459 if (xe_pio_write_packet(scp, mbp) != 0) {
460 /* Push the packet back onto the queue */
461 IF_PREPEND(&ifp->if_snd, mbp);
462 ifp->if_flags |= IFF_OACTIVE;
463 return;
464 }
465
466 /* Tap off here if there is a bpf listener */
467 BPF_MTAP(ifp, mbp);
468
469 /* In case we don't hear from the card again... */
470 ifp->if_timer = 5;
471 scp->tx_queued++;
472
473 m_freem(mbp);
474 }
475 }
476
477
478 /*
479 * Process an ioctl request. Adapted from the ed driver.
480 */
481 static int
482 xe_ioctl (register struct ifnet *ifp, u_long command, caddr_t data) {
483 struct xe_softc *scp;
484 int s, error;
485
486 scp = ifp->if_softc;
487 error = 0;
488
489 s = splimp();
490
491 switch (command) {
492
493 case SIOCSIFFLAGS:
494 DEVPRINTF(2, (scp->dev, "ioctl: SIOCSIFFLAGS: 0x%04x\n", ifp->if_flags));
495 /*
496 * If the interface is marked up and stopped, then start it. If it is
497 * marked down and running, then stop it.
498 */
499 if (ifp->if_flags & IFF_UP) {
500 if (!(ifp->if_flags & IFF_RUNNING)) {
501 xe_reset(scp);
502 xe_init(scp);
503 }
504 }
505 else {
506 if (ifp->if_flags & IFF_RUNNING)
507 xe_stop(scp);
508 }
509 /* FALL THROUGH (handle changes to PROMISC/ALLMULTI flags) */
510
511 case SIOCADDMULTI:
512 case SIOCDELMULTI:
513 DEVPRINTF(2, (scp->dev, "ioctl: SIOC{ADD,DEL}MULTI\n"));
514 /*
515 * Multicast list has (maybe) changed; set the hardware filters
516 * accordingly.
517 */
518 xe_set_multicast(scp);
519 error = 0;
520 break;
521
522 case SIOCSIFMEDIA:
523 case SIOCGIFMEDIA:
524 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ifmedia_ioctl\n"));
525 /*
526 * Someone wants to get/set media options.
527 */
528 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
529 break;
530
531 default:
532 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ether_ioctl\n"));
533 error = ether_ioctl(ifp, command, data);
534 }
535
536 (void)splx(s);
537
538 return error;
539 }
540
541
542 /*
543 * Card interrupt handler.
544 *
545 * This function is probably more complicated than it needs to be, as it
546 * attempts to deal with the case where multiple packets get sent between
547 * interrupts. This is especially annoying when working out the collision
548 * stats. Not sure whether this case ever really happens or not (maybe on a
549 * slow/heavily loaded machine?) so it's probably best to leave this like it
550 * is.
551 *
552 * Note that the crappy PIO used to get packets on and off the card means that
553 * you will spend a lot of time in this routine -- I can get my P150 to spend
554 * 90% of its time servicing interrupts if I really hammer the network. Could
555 * fix this, but then you'd start dropping/losing packets. The moral of this
556 * story? If you want good network performance _and_ some cycles left over to
557 * get your work done, don't buy a Xircom card. Or convince them to tell me
558 * how to do memory-mapped I/O :)
559 */
560 static void
561 xe_intr(void *xscp)
562 {
563 struct xe_softc *scp = (struct xe_softc *) xscp;
564 struct ifnet *ifp;
565 u_int8_t psr, isr, esr, rsr, rst0, txst0, txst1, coll;
566
567 ifp = &scp->arpcom.ac_if;
568
569 /* Disable interrupts */
570 if (scp->mohawk)
571 XE_OUTB(XE_CR, 0);
572
573 /* Cache current register page */
574 psr = XE_INB(XE_PR);
575
576 /* Read ISR to see what caused this interrupt */
577 while ((isr = XE_INB(XE_ISR)) != 0) {
578
579 /* 0xff might mean the card is no longer around */
580 if (isr == 0xff) {
581 DEVPRINTF(3, (scp->dev, "intr: interrupt received for missing card?\n"));
582 break;
583 }
584
585 /* Read other status registers */
586 XE_SELECT_PAGE(0x40);
587 rst0 = XE_INB(XE_RST0);
588 XE_OUTB(XE_RST0, 0);
589 txst0 = XE_INB(XE_TXST0);
590 txst1 = XE_INB(XE_TXST1);
591 coll = txst1 & XE_TXST1_RETRY_COUNT;
592 XE_OUTB(XE_TXST0, 0);
593 XE_OUTB(XE_TXST1, 0);
594 XE_SELECT_PAGE(0);
595
596 DEVPRINTF(3, (scp->dev, "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x, COLL=0x%01x\n", isr, rst0, txst1, txst0, coll));
597
598 if (isr & XE_ISR_TX_PACKET) {
599 u_int8_t tpr, sent;
600
601 /* Update packet count, accounting for rollover */
602 tpr = XE_INB(XE_TPR);
603 sent = -scp->tx_tpr + tpr;
604
605 /* Update statistics if we actually sent anything */
606 if (sent > 0) {
607 scp->tx_tpr = tpr;
608 scp->tx_queued -= sent;
609 ifp->if_opackets += sent;
610 ifp->if_collisions += coll;
611
612 /*
613 * According to the Xircom manual, Dingo will sometimes manage to
614 * transmit a packet with triggering an interrupt. If this happens,
615 * we have sent > 1 and the collision count only reflects collisions
616 * on the last packet sent (the one that triggered the interrupt).
617 * Collision stats might therefore be a bit low, but there doesn't
618 * seem to be anything we can do about that.
619 */
620
621 switch (coll) {
622 case 0:
623 break;
624 case 1:
625 scp->mibdata.dot3StatsSingleCollisionFrames++;
626 scp->mibdata.dot3StatsCollFrequencies[0]++;
627 break;
628 default:
629 scp->mibdata.dot3StatsMultipleCollisionFrames++;
630 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
631 }
632 }
633 ifp->if_timer = 0;
634 ifp->if_flags &= ~IFF_OACTIVE;
635 }
636
637 /* Handle most MAC interrupts */
638 if (isr & XE_ISR_MAC_INTR) {
639
640 #if 0
641 /* Carrier sense lost -- only in 10Mbit HDX mode */
642 if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
643 /* XXX - Need to update media status here */
644 device_printf(scp->dev, "no carrier\n");
645 ifp->if_oerrors++;
646 scp->mibdata.dot3StatsCarrierSenseErrors++;
647 }
648 #endif
649 /* Excessive collisions -- try sending again */
650 if (txst0 & XE_TXST0_16_COLLISIONS) {
651 ifp->if_collisions += 16;
652 ifp->if_oerrors++;
653 scp->mibdata.dot3StatsExcessiveCollisions++;
654 scp->mibdata.dot3StatsMultipleCollisionFrames++;
655 scp->mibdata.dot3StatsCollFrequencies[15]++;
656 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
657 }
658 /* Transmit underrun -- increase early transmit threshold */
659 if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
660 DEVPRINTF(1, (scp->dev, "transmit underrun"));
661 if (scp->tx_thres < ETHER_MAX_LEN) {
662 if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
663 scp->tx_thres = ETHER_MAX_LEN;
664 DPRINTF(1, (": increasing transmit threshold to %u", scp->tx_thres));
665 XE_SELECT_PAGE(0x3);
666 XE_OUTW(XE_TPT, scp->tx_thres);
667 XE_SELECT_PAGE(0x0);
668 }
669 DPRINTF(1, ("\n"));
670 ifp->if_oerrors++;
671 scp->mibdata.dot3StatsInternalMacTransmitErrors++;
672 }
673 /* Late collision -- just complain about it */
674 if (txst0 & XE_TXST0_LATE_COLLISION) {
675 device_printf(scp->dev, "late collision\n");
676 ifp->if_oerrors++;
677 scp->mibdata.dot3StatsLateCollisions++;
678 }
679 /* SQE test failure -- just complain about it */
680 if (txst0 & XE_TXST0_SQE_FAIL) {
681 device_printf(scp->dev, "SQE test failure\n");
682 ifp->if_oerrors++;
683 scp->mibdata.dot3StatsSQETestErrors++;
684 }
685 /* Packet too long -- what happens to these */
686 if (rst0 & XE_RST0_LONG_PACKET) {
687 device_printf(scp->dev, "received giant packet\n");
688 ifp->if_ierrors++;
689 scp->mibdata.dot3StatsFrameTooLongs++;
690 }
691 /* CRC error -- packet dropped */
692 if (rst0 & XE_RST0_CRC_ERROR) {
693 device_printf(scp->dev, "CRC error\n");
694 ifp->if_ierrors++;
695 scp->mibdata.dot3StatsFCSErrors++;
696 }
697 }
698
699 /* Handle received packet(s) */
700 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
701 rsr = XE_INB(XE_RSR);
702
703 DEVPRINTF(3, (scp->dev, "intr: ESR=0x%02x, RSR=0x%02x\n", esr, rsr));
704
705 /* Make sure packet is a good one */
706 if (rsr & XE_RSR_RX_OK) {
707 struct ether_header *ehp;
708 struct mbuf *mbp;
709 u_int16_t len;
710
711 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
712
713 DEVPRINTF(3, (scp->dev, "intr: receive length = %d\n", len));
714
715 if (len == 0) {
716 ifp->if_iqdrops++;
717 continue;
718 }
719
720 /*
721 * Allocate mbuf to hold received packet. If the mbuf header isn't
722 * big enough, we attach an mbuf cluster to hold the packet. Note the
723 * +=2 to align the packet data on a 32-bit boundary, and the +3 to
724 * allow for the possibility of reading one more byte than the actual
725 * packet length (we always read 16-bit words).
726 * XXX - Surely there's a better way to do this alignment?
727 */
728 MGETHDR(mbp, M_DONTWAIT, MT_DATA);
729 if (mbp == NULL) {
730 ifp->if_iqdrops++;
731 continue;
732 }
733
734 if (len + 3 > MHLEN) {
735 MCLGET(mbp, M_DONTWAIT);
736 if ((mbp->m_flags & M_EXT) == 0) {
737 m_freem(mbp);
738 ifp->if_iqdrops++;
739 continue;
740 }
741 }
742
743 mbp->m_data += 2;
744 ehp = mtod(mbp, struct ether_header *);
745
746 /*
747 * Now get the packet in PIO mode, including the Ethernet header but
748 * omitting the trailing CRC.
749 */
750
751 /*
752 * Work around a bug in CE2 cards. There seems to be a problem with
753 * duplicated and extraneous bytes in the receive buffer, but without
754 * any real documentation for the CE2 it's hard to tell for sure.
755 * XXX - Needs testing on CE2 hardware
756 */
757 if (scp->srev == 0) {
758 u_short rhs;
759
760 XE_SELECT_PAGE(5);
761 rhs = XE_INW(XE_RHSA);
762 XE_SELECT_PAGE(0);
763
764 rhs += 3; /* Skip control info */
765
766 if (rhs >= 0x8000)
767 rhs = 0;
768
769 if (rhs + len > 0x8000) {
770 int i;
771
772 for (i = 0; i < len; i++, rhs++) {
773 ((char *)ehp)[i] = XE_INB(XE_EDP);
774 if (rhs == 0x8000) {
775 rhs = 0;
776 i--;
777 }
778 }
779 }
780 else
781 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
782 (u_int16_t *) ehp, (len + 1) >> 1);
783 }
784 else
785 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
786 (u_int16_t *) ehp, (len + 1) >> 1);
787
788 /* Deliver packet to upper layers */
789 mbp->m_pkthdr.rcvif = ifp;
790 mbp->m_pkthdr.len = mbp->m_len = len;
791 (*ifp->if_input)(ifp, mbp);
792 ifp->if_ipackets++;
793 }
794
795 /* Packet alignment error -- drop packet */
796 else if (rsr & XE_RSR_ALIGN_ERROR) {
797 device_printf(scp->dev, "alignment error\n");
798 scp->mibdata.dot3StatsAlignmentErrors++;
799 ifp->if_ierrors++;
800 }
801
802 /* Skip to next packet, if there is one */
803 XE_OUTW(XE_DO, 0x8000);
804 }
805
806 /* Clear receiver overruns now we have some free buffer space */
807 if (rst0 & XE_RST0_RX_OVERRUN) {
808 DEVPRINTF(1, (scp->dev, "receive overrun\n"));
809 ifp->if_ierrors++;
810 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
811 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
812 }
813 }
814
815 /* Restore saved page */
816 XE_SELECT_PAGE(psr);
817
818 /* Re-enable interrupts */
819 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
820
821 return;
822 }
823
824
825 /*
826 * Device timeout/watchdog routine. Called automatically if we queue a packet
827 * for transmission but don't get an interrupt within a specified timeout
828 * (usually 5 seconds). When this happens we assume the worst and reset the
829 * card.
830 */
831 static void
832 xe_watchdog(struct ifnet *ifp) {
833 struct xe_softc *scp = ifp->if_softc;
834
835 device_printf(scp->dev, "watchdog timeout: resetting card\n");
836 scp->tx_timeouts++;
837 ifp->if_oerrors += scp->tx_queued;
838 xe_stop(scp);
839 xe_reset(scp);
840 xe_init(scp);
841 }
842
843
844 /*
845 * Change media selection.
846 */
847 static int
848 xe_media_change(struct ifnet *ifp) {
849 struct xe_softc *scp = ifp->if_softc;
850
851 DEVPRINTF(2, (scp->dev, "media_change\n"));
852
853 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER)
854 return(EINVAL);
855
856 /*
857 * Some card/media combos aren't always possible -- filter those out here.
858 */
859 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
860 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok)
861 return (EINVAL);
862
863 xe_setmedia(scp);
864
865 return 0;
866 }
867
868
869 /*
870 * Return current media selection.
871 */
872 static void
873 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp) {
874 struct xe_softc *scp = ifp->if_softc;
875
876 DEVPRINTF(3, (scp->dev, "media_status\n"));
877
878 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
879 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
880 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
881
882 return;
883 }
884
885
886 /*
887 * Select active media.
888 */
889 static void xe_setmedia(void *xscp) {
890 struct xe_softc *scp = xscp;
891 u_int16_t bmcr, bmsr, anar, lpar;
892
893 DEVPRINTF(2, (scp->dev, "setmedia\n"));
894
895 /* Cancel any pending timeout */
896 untimeout(xe_setmedia, scp, scp->chand);
897 xe_disable_intr(scp);
898
899 /* Select media */
900 scp->media = IFM_ETHER;
901 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
902
903 case IFM_AUTO: /* Autoselect media */
904 scp->media = IFM_ETHER|IFM_AUTO;
905
906 /*
907 * Autoselection is really awful. It goes something like this:
908 *
909 * Wait until the transmitter goes idle (2sec timeout).
910 * Reset card
911 * IF a 100Mbit PHY exists
912 * Start NWAY autonegotiation (3.5sec timeout)
913 * IF that succeeds
914 * Select 100baseTX or 10baseT, whichever was detected
915 * ELSE
916 * Reset card
917 * IF a 100Mbit PHY exists
918 * Try to force a 100baseTX link (3sec timeout)
919 * IF that succeeds
920 * Select 100baseTX
921 * ELSE
922 * Disable the PHY
923 * ENDIF
924 * ENDIF
925 * ENDIF
926 * ENDIF
927 * IF nothing selected so far
928 * IF a 100Mbit PHY exists
929 * Select 10baseT
930 * ELSE
931 * Select 10baseT or 10base2, whichever is connected
932 * ENDIF
933 * ENDIF
934 */
935 switch (scp->autoneg_status) {
936
937 case XE_AUTONEG_NONE:
938 DEVPRINTF(2, (scp->dev, "Waiting for idle transmitter\n"));
939 scp->arpcom.ac_if.if_flags |= IFF_OACTIVE;
940 scp->autoneg_status = XE_AUTONEG_WAITING;
941 /* FALL THROUGH */
942
943 case XE_AUTONEG_WAITING:
944 if (scp->tx_queued != 0) {
945 scp->chand = timeout(xe_setmedia, scp, hz/2);
946 return;
947 }
948 if (scp->phy_ok) {
949 DEVPRINTF(2, (scp->dev, "Starting autonegotiation\n"));
950 bmcr = xe_phy_readreg(scp, PHY_BMCR);
951 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
952 xe_phy_writereg(scp, PHY_BMCR, bmcr);
953 anar = xe_phy_readreg(scp, PHY_ANAR);
954 anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
955 anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
956 xe_phy_writereg(scp, PHY_ANAR, anar);
957 bmcr |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
958 xe_phy_writereg(scp, PHY_BMCR, bmcr);
959 scp->autoneg_status = XE_AUTONEG_STARTED;
960 scp->chand = timeout(xe_setmedia, scp, hz * 7/2);
961 return;
962 }
963 else {
964 scp->autoneg_status = XE_AUTONEG_FAIL;
965 }
966 break;
967
968 case XE_AUTONEG_STARTED:
969 bmsr = xe_phy_readreg(scp, PHY_BMSR);
970 lpar = xe_phy_readreg(scp, PHY_LPAR);
971 if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
972 DEVPRINTF(2, (scp->dev, "Autonegotiation complete!\n"));
973 /*
974 * XXX - Shouldn't have to do this, but (on my hub at least) the
975 * XXX - transmitter won't work after a successful autoneg. So we see
976 * XXX - what the negotiation result was and force that mode. I'm
977 * XXX - sure there is an easy fix for this.
978 */
979 if (lpar & PHY_LPAR_100BTXHALF) {
980 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
981 XE_MII_DUMP(scp);
982 XE_SELECT_PAGE(2);
983 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
984 scp->media = IFM_ETHER|IFM_100_TX;
985 scp->autoneg_status = XE_AUTONEG_NONE;
986 }
987 else {
988 /*
989 * XXX - Bit of a hack going on in here.
990 * XXX - This is derived from Ken Hughes patch to the Linux driver
991 * XXX - to make it work with 10Mbit _autonegotiated_ links on CE3B
992 * XXX - cards. What's a CE3B and how's it differ from a plain CE3?
993 * XXX - these are the things we need to find out.
994 */
995 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
996 XE_SELECT_PAGE(2);
997 /* BEGIN HACK */
998 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
999 XE_SELECT_PAGE(0x42);
1000 XE_OUTB(XE_SWC1, 0x80);
1001 scp->media = IFM_ETHER|IFM_10_T;
1002 scp->autoneg_status = XE_AUTONEG_NONE;
1003 /* END HACK */
1004 /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/ /* Disable PHY? */
1005 /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
1006 }
1007 }
1008 else {
1009 DEVPRINTF(2, (scp->dev, "Autonegotiation failed; trying 100baseTX\n"));
1010 XE_MII_DUMP(scp);
1011 if (scp->phy_ok) {
1012 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1013 scp->autoneg_status = XE_AUTONEG_100TX;
1014 scp->chand = timeout(xe_setmedia, scp, hz * 3);
1015 return;
1016 }
1017 else {
1018 scp->autoneg_status = XE_AUTONEG_FAIL;
1019 }
1020 }
1021 break;
1022
1023 case XE_AUTONEG_100TX:
1024 (void)xe_phy_readreg(scp, PHY_BMSR);
1025 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1026 if (bmsr & PHY_BMSR_LINKSTAT) {
1027 DEVPRINTF(2, (scp->dev, "Got 100baseTX link!\n"));
1028 XE_MII_DUMP(scp);
1029 XE_SELECT_PAGE(2);
1030 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1031 scp->media = IFM_ETHER|IFM_100_TX;
1032 scp->autoneg_status = XE_AUTONEG_NONE;
1033 }
1034 else {
1035 DEVPRINTF(2, (scp->dev, "Autonegotiation failed; disabling PHY\n"));
1036 XE_MII_DUMP(scp);
1037 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1038 XE_SELECT_PAGE(2);
1039 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY? */
1040 scp->autoneg_status = XE_AUTONEG_FAIL;
1041 }
1042 break;
1043 }
1044
1045 /*
1046 * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
1047 * either autonegotiation failed, or never got started to begin with. In
1048 * either case, select a suitable 10Mbit media and hope it works. We
1049 * don't need to reset the card again, since it will have been done
1050 * already by the big switch above.
1051 */
1052 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1053 DEVPRINTF(2, (scp->dev, "Selecting 10baseX\n"));
1054 if (scp->mohawk) {
1055 XE_SELECT_PAGE(0x42);
1056 XE_OUTB(XE_SWC1, 0x80);
1057 scp->media = IFM_ETHER|IFM_10_T;
1058 scp->autoneg_status = XE_AUTONEG_NONE;
1059 }
1060 else {
1061 XE_SELECT_PAGE(4);
1062 XE_OUTB(XE_GPR0, 4);
1063 DELAY(50000);
1064 XE_SELECT_PAGE(0x42);
1065 XE_OUTB(XE_SWC1, (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? 0x80 : 0xc0);
1066 scp->media = IFM_ETHER|((XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1067 scp->autoneg_status = XE_AUTONEG_NONE;
1068 }
1069 }
1070 break;
1071
1072
1073 /*
1074 * If a specific media has been requested, we just reset the card and
1075 * select it (one small exception -- if 100baseTX is requested by there is
1076 * no PHY, we fall back to 10baseT operation).
1077 */
1078 case IFM_100_TX: /* Force 100baseTX */
1079 if (scp->phy_ok) {
1080 DEVPRINTF(2, (scp->dev, "Selecting 100baseTX\n"));
1081 XE_SELECT_PAGE(0x42);
1082 XE_OUTB(XE_SWC1, 0);
1083 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1084 XE_SELECT_PAGE(2);
1085 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1086 scp->media |= IFM_100_TX;
1087 break;
1088 }
1089 /* FALLTHROUGH */
1090
1091 case IFM_10_T: /* Force 10baseT */
1092 DEVPRINTF(2, (scp->dev, "Selecting 10baseT\n"));
1093 if (scp->phy_ok) {
1094 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1095 XE_SELECT_PAGE(2);
1096 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY */
1097 }
1098 XE_SELECT_PAGE(0x42);
1099 XE_OUTB(XE_SWC1, 0x80);
1100 scp->media |= IFM_10_T;
1101 break;
1102
1103 case IFM_10_2:
1104 DEVPRINTF(2, (scp->dev, "Selecting 10base2\n"));
1105 XE_SELECT_PAGE(0x42);
1106 XE_OUTB(XE_SWC1, 0xc0);
1107 scp->media |= IFM_10_2;
1108 break;
1109 }
1110
1111
1112 /*
1113 * Finally, the LEDs are set to match whatever media was chosen and the
1114 * transmitter is unblocked.
1115 */
1116 DEVPRINTF(2, (scp->dev, "Setting LEDs\n"));
1117 XE_SELECT_PAGE(2);
1118 switch (IFM_SUBTYPE(scp->media)) {
1119 case IFM_100_TX:
1120 case IFM_10_T:
1121 XE_OUTB(XE_LED, 0x3b);
1122 if (scp->dingo)
1123 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1124 break;
1125
1126 case IFM_10_2:
1127 XE_OUTB(XE_LED, 0x3a);
1128 break;
1129 }
1130
1131 /* Restart output? */
1132 xe_enable_intr(scp);
1133 scp->ifp->if_flags &= ~IFF_OACTIVE;
1134 xe_start(scp->ifp);
1135 }
1136
1137
1138 /*
1139 * Hard reset (power cycle) the card.
1140 */
1141 static void
1142 xe_reset(struct xe_softc *scp) {
1143 int s;
1144
1145 DEVPRINTF(2, (scp->dev, "reset\n"));
1146
1147 s = splimp();
1148
1149 /* Power down */
1150 XE_SELECT_PAGE(4);
1151 XE_OUTB(XE_GPR1, 0);
1152 DELAY(40000);
1153
1154 /* Power up again */
1155 if (scp->mohawk)
1156 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1157 else
1158 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN|XE_GPR1_AIC);
1159
1160 DELAY(40000);
1161 XE_SELECT_PAGE(0);
1162
1163 (void)splx(s);
1164 }
1165
1166
1167 /*
1168 * Take interface offline. This is done by powering down the device, which I
1169 * assume means just shutting down the transceiver and Ethernet logic. This
1170 * requires a _hard_ reset to recover from, as we need to power up again.
1171 */
1172 static void
1173 xe_stop(struct xe_softc *scp) {
1174 int s;
1175
1176 DEVPRINTF(2, (scp->dev, "stop\n"));
1177
1178 s = splimp();
1179
1180 /*
1181 * Shut off interrupts.
1182 */
1183 xe_disable_intr(scp);
1184
1185 /*
1186 * Power down.
1187 */
1188 XE_SELECT_PAGE(4);
1189 XE_OUTB(XE_GPR1, 0);
1190 XE_SELECT_PAGE(0);
1191 if (scp->mohawk) {
1192 /*
1193 * set GP1 and GP2 as outputs (bits 2 & 3)
1194 * set GP1 high to power on the ML6692 (bit 0)
1195 * set GP2 low to power on the 10Mhz chip (bit 1)
1196 */
1197 XE_SELECT_PAGE(4);
1198 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP1_OUT);
1199 }
1200
1201 /*
1202 * ~IFF_RUNNING == interface down.
1203 */
1204 scp->ifp->if_flags &= ~IFF_RUNNING;
1205 scp->ifp->if_flags &= ~IFF_OACTIVE;
1206 scp->ifp->if_timer = 0;
1207
1208 (void)splx(s);
1209 }
1210
1211
1212 /*
1213 * Enable interrupts from the card.
1214 */
1215 static void
1216 xe_enable_intr(struct xe_softc *scp) {
1217
1218 DEVPRINTF(2, (scp->dev, "enable_intr\n"));
1219
1220 XE_SELECT_PAGE(0);
1221 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1222 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1223 if (!(XE_INB(0x10) & 0x01)) {
1224 XE_OUTB(0x10, 0x11); /* Unmask master int enable bit */
1225 }
1226 }
1227 }
1228
1229
1230 /*
1231 * Disable interrupts from the card.
1232 */
1233 static void
1234 xe_disable_intr(struct xe_softc *scp) {
1235
1236 DEVPRINTF(2, (scp->dev, "disable_intr\n"));
1237
1238 XE_SELECT_PAGE(0);
1239 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1240 if (scp->modem && !scp->dingo) { /* More magic */
1241 XE_OUTB(0x10, 0x10); /* Mask the master int enable bit */
1242 }
1243 }
1244
1245
1246 /*
1247 * Set up multicast filter and promiscuous modes.
1248 */
1249 static void
1250 xe_set_multicast(struct xe_softc *scp) {
1251 struct ifnet *ifp;
1252 struct ifmultiaddr *maddr;
1253 unsigned count, i;
1254
1255 DEVPRINTF(2, (scp->dev, "set_multicast\n"));
1256
1257 ifp = &scp->arpcom.ac_if;
1258 XE_SELECT_PAGE(0x42);
1259
1260 /* Handle PROMISC flag */
1261 if (ifp->if_flags & IFF_PROMISC) {
1262 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1263 return;
1264 }
1265 else
1266 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1267
1268 /* Handle ALLMULTI flag */
1269 if (ifp->if_flags & IFF_ALLMULTI) {
1270 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1271 return;
1272 }
1273 else
1274 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1275
1276 /* Iterate over multicast address list */
1277 count = 0;
1278 #if __FreeBSD_version < 500000
1279 LIST_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
1280 #else
1281 TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
1282 #endif
1283 if (maddr->ifma_addr->sa_family != AF_LINK)
1284 continue;
1285
1286 count++;
1287
1288 if (count < 10)
1289 /* First 9 use Individual Addresses for exact matching */
1290 xe_set_addr(scp, LLADDR((struct sockaddr_dl *)maddr->ifma_addr), count);
1291 else
1292 if (scp->mohawk)
1293 /* Use hash filter on Mohawk and Dingo */
1294 xe_mchash(scp, LLADDR((struct sockaddr_dl *)maddr->ifma_addr));
1295 else
1296 /* Nowhere else to put them on CE2 */
1297 break;
1298 }
1299
1300 DEVPRINTF(2, (scp->dev, "set_multicast: count = %u\n", count));
1301
1302 /* Now do some cleanup and enable multicast handling as needed */
1303 if (count == 0) {
1304 /* Disable all multicast handling */
1305 XE_SELECT_PAGE(0x42);
1306 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~(XE_SWC1_IA_ENABLE|XE_SWC1_ALLMULTI));
1307 if (scp->mohawk) {
1308 XE_SELECT_PAGE(0x02);
1309 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1310 }
1311 }
1312 else if (count < 10) {
1313 /* Full in any unused Individual Addresses with our MAC address */
1314 for (i = count + 1; i < 10; i++)
1315 xe_set_addr(scp, (u_int8_t *)(&scp->arpcom.ac_enaddr), i);
1316 /* Enable Individual Address matching only */
1317 XE_SELECT_PAGE(0x42);
1318 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1319 if (scp->mohawk) {
1320 XE_SELECT_PAGE(0x02);
1321 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1322 }
1323 }
1324 else {
1325 if (scp->mohawk) {
1326 /* Check whether hash table is full */
1327 XE_SELECT_PAGE(0x58);
1328 for (i = 0x08; i < 0x10; i++)
1329 if (XE_INB(i) != 0xff)
1330 break;
1331 if (i == 0x10) {
1332 /* Hash table full - enable promiscuous multicast matching */
1333 XE_SELECT_PAGE(0x42);
1334 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1335 XE_SELECT_PAGE(0x02);
1336 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1337 }
1338 else {
1339 /* Enable hash table and Individual Address matching */
1340 XE_SELECT_PAGE(0x42);
1341 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1342 XE_SELECT_PAGE(0x02);
1343 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1344 }
1345 }
1346 else {
1347 /* Enable promiscuous multicast matching */
1348 XE_SELECT_PAGE(0x42);
1349 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1350 }
1351 }
1352
1353 XE_SELECT_PAGE(0);
1354 }
1355
1356
1357 /*
1358 * Copy the Ethernet multicast address in addr to the on-chip registers for
1359 * Individual Address idx. Assumes that addr is really a multicast address
1360 * and that idx > 0 (slot 0 is always used for the card MAC address).
1361 */
1362 static void
1363 xe_set_addr(struct xe_softc *scp, u_int8_t* addr, unsigned idx) {
1364 u_int8_t page, reg;
1365 unsigned i;
1366
1367 /*
1368 * Individual Addresses are stored in registers 8-F of pages 0x50-0x57. IA1
1369 * therefore starts at register 0xE on page 0x50. The expressions below
1370 * compute the starting page and register for any IA index > 0.
1371 */
1372 --idx;
1373 page = 0x50 + idx%4 + idx/4*3;
1374 reg = 0x0e - 2 * (idx%4);
1375
1376 DEVPRINTF(3, (scp->dev, "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n",
1377 idx+1, page, reg));
1378
1379 /*
1380 * Copy the IA bytes. Note that the byte order is reversed for Mohawk and
1381 * Dingo wrt. CE2 hardware.
1382 */
1383 XE_SELECT_PAGE(page);
1384 for (i = 0; i < 6; i++) {
1385 if (i > 0) {
1386 DPRINTF(3, (":%02x", addr[i]));
1387 }
1388 else {
1389 DEVPRINTF(3, (scp->dev, "set_addr: %02x", addr[0]));
1390 }
1391 XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1392 if (++reg == 0x10) {
1393 reg = 0x08;
1394 XE_SELECT_PAGE(++page);
1395 }
1396 }
1397 DPRINTF(3, ("\n"));
1398 }
1399
1400
1401 /*
1402 * Set the appropriate bit in the multicast hash table for the supplied
1403 * Ethernet multicast address addr. Assumes that addr is really a multicast
1404 * address.
1405 */
1406 static void
1407 xe_mchash(struct xe_softc* scp, const uint8_t *addr) {
1408 int bit;
1409 uint8_t byte, hash;
1410
1411 hash = ether_crc32_le(addr, ETHER_ADDR_LEN) & 0x3F;
1412
1413 /* Top 3 bits of hash give register - 8, bottom 3 give bit within register */
1414 byte = hash >> 3 | 0x08;
1415 bit = 0x01 << (hash & 0x07);
1416
1417 DEVPRINTF(3, (scp->dev, "set_hash: hash = 0x%02x, byte = 0x%02x, bit = 0x%02x\n", hash, byte, bit));
1418
1419 XE_SELECT_PAGE(0x58);
1420 XE_OUTB(byte, XE_INB(byte) | bit);
1421 }
1422
1423
1424 /*
1425 * Write an outgoing packet to the card using programmed I/O.
1426 */
1427 static int
1428 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp) {
1429 unsigned len, pad;
1430 unsigned char wantbyte;
1431 u_int8_t *data;
1432 u_int8_t savebyte[2];
1433
1434 /* Get total packet length */
1435 if (mbp->m_flags & M_PKTHDR)
1436 len = mbp->m_pkthdr.len;
1437 else {
1438 struct mbuf* mbp2 = mbp;
1439 for (len = 0; mbp2 != NULL; len += mbp2->m_len, mbp2 = mbp2->m_next);
1440 }
1441
1442 DEVPRINTF(3, (scp->dev, "pio_write_packet: len = %u\n", len));
1443
1444 /* Packets < minimum length may need to be padded out */
1445 pad = 0;
1446 if (len < scp->tx_min) {
1447 pad = scp->tx_min - len;
1448 len = scp->tx_min;
1449 }
1450
1451 /* Check transmit buffer space */
1452 XE_SELECT_PAGE(0);
1453 XE_OUTW(XE_TRS, len+2); /* Only effective on rev. 1 CE2 cards */
1454 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1455 return 1;
1456
1457 /* Send packet length to card */
1458 XE_OUTW(XE_EDP, len);
1459
1460 /*
1461 * Write packet to card using PIO (code stolen from the ed driver)
1462 */
1463 wantbyte = 0;
1464 while (mbp != NULL) {
1465 len = mbp->m_len;
1466 if (len > 0) {
1467 data = mtod(mbp, caddr_t);
1468 if (wantbyte) { /* Finish the last word */
1469 savebyte[1] = *data;
1470 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1471 data++;
1472 len--;
1473 wantbyte = 0;
1474 }
1475 if (len > 1) { /* Output contiguous words */
1476 bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
1477 len >> 1);
1478 data += len & ~1;
1479 len &= 1;
1480 }
1481 if (len == 1) { /* Save last byte, if necessary */
1482 savebyte[0] = *data;
1483 wantbyte = 1;
1484 }
1485 }
1486 mbp = mbp->m_next;
1487 }
1488
1489 /*
1490 * Send last byte of odd-length packets
1491 */
1492 if (wantbyte)
1493 XE_OUTB(XE_EDP, savebyte[0]);
1494
1495 /*
1496 * Can just tell CE3 cards to send; short packets will be padded out with
1497 * random cruft automatically. For CE2, manually pad the packet with
1498 * garbage; it will be sent when the required number or bytes have been
1499 * delivered to the card.
1500 */
1501 if (scp->mohawk)
1502 XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX | XE_CR_ENABLE_INTR);
1503 else if (pad > 0) {
1504 if (pad & 0x01)
1505 XE_OUTB(XE_EDP, 0xaa);
1506 pad >>= 1;
1507 while (pad > 0) {
1508 XE_OUTW(XE_EDP, 0xdead);
1509 pad--;
1510 }
1511 }
1512
1513 return 0;
1514 }
1515
1516
1517
1518 /**************************************************************
1519 * *
1520 * M I I F U N C T I O N S *
1521 * *
1522 **************************************************************/
1523
1524 /*
1525 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1526 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1527 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1528 * XXX - some kind of common MII-handling code, shared by all drivers. But
1529 * XXX - that's a whole other mission.
1530 */
1531 #define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1532 #define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1533
1534
1535 /*
1536 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1537 */
1538 static void
1539 xe_mii_sync(struct xe_softc *scp) {
1540 register int i;
1541
1542 XE_SELECT_PAGE(2);
1543 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1544
1545 for (i = 0; i < 32; i++) {
1546 XE_MII_SET(XE_MII_CLK);
1547 DELAY(1);
1548 XE_MII_CLR(XE_MII_CLK);
1549 DELAY(1);
1550 }
1551 }
1552
1553
1554 /*
1555 * Look for a MII-compliant PHY. If we find one, reset it.
1556 */
1557 static int
1558 xe_mii_init(struct xe_softc *scp) {
1559 u_int16_t status;
1560
1561 status = xe_phy_readreg(scp, PHY_BMSR);
1562 if ((status & 0xff00) != 0x7800) {
1563 DEVPRINTF(2, (scp->dev, "no PHY found, %0x\n", status));
1564 return 0;
1565 }
1566 else {
1567 DEVPRINTF(2, (scp->dev, "PHY OK!\n"));
1568
1569 /* Reset the PHY */
1570 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1571 DELAY(500);
1572 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
1573 XE_MII_DUMP(scp);
1574 return 1;
1575 }
1576 }
1577
1578
1579 /*
1580 * Clock a series of bits through the MII.
1581 */
1582 static void
1583 xe_mii_send(struct xe_softc *scp, u_int32_t bits, int cnt) {
1584 int i;
1585
1586 XE_SELECT_PAGE(2);
1587 XE_MII_CLR(XE_MII_CLK);
1588
1589 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1590 if (bits & i) {
1591 XE_MII_SET(XE_MII_WRD);
1592 } else {
1593 XE_MII_CLR(XE_MII_WRD);
1594 }
1595 DELAY(1);
1596 XE_MII_CLR(XE_MII_CLK);
1597 DELAY(1);
1598 XE_MII_SET(XE_MII_CLK);
1599 }
1600 }
1601
1602
1603 /*
1604 * Read an PHY register through the MII.
1605 */
1606 static int
1607 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1608 int i, ack, s;
1609
1610 s = splimp();
1611
1612 /*
1613 * Set up frame for RX.
1614 */
1615 frame->mii_stdelim = XE_MII_STARTDELIM;
1616 frame->mii_opcode = XE_MII_READOP;
1617 frame->mii_turnaround = 0;
1618 frame->mii_data = 0;
1619
1620 XE_SELECT_PAGE(2);
1621 XE_OUTB(XE_GPR2, 0);
1622
1623 /*
1624 * Turn on data xmit.
1625 */
1626 XE_MII_SET(XE_MII_DIR);
1627
1628 xe_mii_sync(scp);
1629
1630 /*
1631 * Send command/address info.
1632 */
1633 xe_mii_send(scp, frame->mii_stdelim, 2);
1634 xe_mii_send(scp, frame->mii_opcode, 2);
1635 xe_mii_send(scp, frame->mii_phyaddr, 5);
1636 xe_mii_send(scp, frame->mii_regaddr, 5);
1637
1638 /* Idle bit */
1639 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1640 DELAY(1);
1641 XE_MII_SET(XE_MII_CLK);
1642 DELAY(1);
1643
1644 /* Turn off xmit. */
1645 XE_MII_CLR(XE_MII_DIR);
1646
1647 /* Check for ack */
1648 XE_MII_CLR(XE_MII_CLK);
1649 DELAY(1);
1650 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1651 XE_MII_SET(XE_MII_CLK);
1652 DELAY(1);
1653
1654 /*
1655 * Now try reading data bits. If the ack failed, we still
1656 * need to clock through 16 cycles to keep the PHY(s) in sync.
1657 */
1658 if (ack) {
1659 for(i = 0; i < 16; i++) {
1660 XE_MII_CLR(XE_MII_CLK);
1661 DELAY(1);
1662 XE_MII_SET(XE_MII_CLK);
1663 DELAY(1);
1664 }
1665 goto fail;
1666 }
1667
1668 for (i = 0x8000; i; i >>= 1) {
1669 XE_MII_CLR(XE_MII_CLK);
1670 DELAY(1);
1671 if (!ack) {
1672 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1673 frame->mii_data |= i;
1674 DELAY(1);
1675 }
1676 XE_MII_SET(XE_MII_CLK);
1677 DELAY(1);
1678 }
1679
1680 fail:
1681
1682 XE_MII_CLR(XE_MII_CLK);
1683 DELAY(1);
1684 XE_MII_SET(XE_MII_CLK);
1685 DELAY(1);
1686
1687 splx(s);
1688
1689 if (ack)
1690 return(1);
1691 return(0);
1692 }
1693
1694
1695 /*
1696 * Write to a PHY register through the MII.
1697 */
1698 static int
1699 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1700 int s;
1701
1702 s = splimp();
1703
1704 /*
1705 * Set up frame for TX.
1706 */
1707 frame->mii_stdelim = XE_MII_STARTDELIM;
1708 frame->mii_opcode = XE_MII_WRITEOP;
1709 frame->mii_turnaround = XE_MII_TURNAROUND;
1710
1711 XE_SELECT_PAGE(2);
1712
1713 /*
1714 * Turn on data output.
1715 */
1716 XE_MII_SET(XE_MII_DIR);
1717
1718 xe_mii_sync(scp);
1719
1720 xe_mii_send(scp, frame->mii_stdelim, 2);
1721 xe_mii_send(scp, frame->mii_opcode, 2);
1722 xe_mii_send(scp, frame->mii_phyaddr, 5);
1723 xe_mii_send(scp, frame->mii_regaddr, 5);
1724 xe_mii_send(scp, frame->mii_turnaround, 2);
1725 xe_mii_send(scp, frame->mii_data, 16);
1726
1727 /* Idle bit. */
1728 XE_MII_SET(XE_MII_CLK);
1729 DELAY(1);
1730 XE_MII_CLR(XE_MII_CLK);
1731 DELAY(1);
1732
1733 /*
1734 * Turn off xmit.
1735 */
1736 XE_MII_CLR(XE_MII_DIR);
1737
1738 splx(s);
1739
1740 return(0);
1741 }
1742
1743
1744 /*
1745 * Read a register from the PHY.
1746 */
1747 static u_int16_t
1748 xe_phy_readreg(struct xe_softc *scp, u_int16_t reg) {
1749 struct xe_mii_frame frame;
1750
1751 bzero((char *)&frame, sizeof(frame));
1752
1753 frame.mii_phyaddr = 0;
1754 frame.mii_regaddr = reg;
1755 xe_mii_readreg(scp, &frame);
1756
1757 return(frame.mii_data);
1758 }
1759
1760
1761 /*
1762 * Write to a PHY register.
1763 */
1764 static void
1765 xe_phy_writereg(struct xe_softc *scp, u_int16_t reg, u_int16_t data) {
1766 struct xe_mii_frame frame;
1767
1768 bzero((char *)&frame, sizeof(frame));
1769
1770 frame.mii_phyaddr = 0;
1771 frame.mii_regaddr = reg;
1772 frame.mii_data = data;
1773 xe_mii_writereg(scp, &frame);
1774
1775 return;
1776 }
1777
1778
1779 /*
1780 * A bit of debugging code.
1781 */
1782 static void
1783 xe_mii_dump(struct xe_softc *scp) {
1784 int i, s;
1785
1786 s = splimp();
1787
1788 device_printf(scp->dev, "MII registers: ");
1789 for (i = 0; i < 2; i++) {
1790 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1791 }
1792 for (i = 4; i < 7; i++) {
1793 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1794 }
1795 printf("\n");
1796
1797 (void)splx(s);
1798 }
1799
1800 #if 0
1801 void
1802 xe_reg_dump(struct xe_softc *scp) {
1803 int page, i, s;
1804
1805 s = splimp();
1806
1807 device_printf(scp->dev, "Common registers: ");
1808 for (i = 0; i < 8; i++) {
1809 printf(" %2.2x", XE_INB(i));
1810 }
1811 printf("\n");
1812
1813 for (page = 0; page <= 8; page++) {
1814 device_printf(scp->dev, "Register page %2.2x: ", page);
1815 XE_SELECT_PAGE(page);
1816 for (i = 8; i < 16; i++) {
1817 printf(" %2.2x", XE_INB(i));
1818 }
1819 printf("\n");
1820 }
1821
1822 for (page = 0x10; page < 0x5f; page++) {
1823 if ((page >= 0x11 && page <= 0x3f) ||
1824 (page == 0x41) ||
1825 (page >= 0x43 && page <= 0x4f) ||
1826 (page >= 0x59))
1827 continue;
1828 device_printf(scp->dev, "Register page %2.2x: ", page);
1829 XE_SELECT_PAGE(page);
1830 for (i = 8; i < 16; i++) {
1831 printf(" %2.2x", XE_INB(i));
1832 }
1833 printf("\n");
1834 }
1835
1836 (void)splx(s);
1837 }
1838 #endif
1839
1840 int
1841 xe_activate(device_t dev)
1842 {
1843 struct xe_softc *sc = device_get_softc(dev);
1844 int start, err, i;
1845
1846 DEVPRINTF(2, (dev, "activate\n"));
1847
1848 if (!sc->modem) {
1849 sc->port_rid = 0; /* 0 is managed by pccard */
1850 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1851 &sc->port_rid, 0, ~0, 16, RF_ACTIVE);
1852 }
1853 else if (sc->dingo) {
1854 /*
1855 * Find a 16 byte aligned ioport for the card.
1856 */
1857 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1858 sc->port_rid = 1; /* 0 is managed by pccard */
1859 start = 0x100;
1860 do {
1861 sc->port_res = bus_alloc_resource(dev,
1862 SYS_RES_IOPORT, &sc->port_rid, start, 0x3ff, 16,
1863 RF_ACTIVE);
1864 if (sc->port_res == 0)
1865 break; /* we failed */
1866 if ((rman_get_start(sc->port_res) & 0xf) == 0)
1867 break; /* good */
1868 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1869 sc->port_res);
1870 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1871 } while (1);
1872 DEVPRINTF(1, (dev, "RealPort port 0x%0lx, size 0x%0lx\n",
1873 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1874 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1875 }
1876 else if (sc->ce2) {
1877 /*
1878 * Find contiguous I/O port for the Ethernet function on CEM2 and
1879 * CEM3 cards. We allocate window 0 wherever pccard has decided
1880 * it should be, then find an available window adjacent to it for
1881 * the second function. Not sure that both windows are actually
1882 * needed.
1883 */
1884 DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
1885 sc->ce2_port_rid = 0; /* 0 is managed by pccard */
1886 sc->ce2_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1887 &sc->ce2_port_rid, 0, ~0,
1888 8, RF_ACTIVE);
1889 if (!sc->ce2_port_res) {
1890 DEVPRINTF(1, (dev, "Cannot allocate I/O port for modem\n"));
1891 return ENOMEM;
1892 }
1893
1894 sc->port_rid = 1;
1895 start = bus_get_resource_start(dev, SYS_RES_IOPORT,
1896 sc->ce2_port_rid);
1897 for (i = 0; i < 2; i++) {
1898 start += (i == 0 ? 8 : -24);
1899 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1900 &sc->port_rid, start,
1901 start + 18, 18, RF_ACTIVE);
1902 if (sc->port_res == 0)
1903 continue; /* Failed, try again if possible */
1904 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
1905 sc->port_rid) == start)
1906 break; /* Success! */
1907
1908 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1909 sc->port_res);
1910 sc->port_res = 0;
1911 }
1912 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0lx, size 0x%0lx\n",
1913 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1914 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1915 }
1916
1917 if (!sc->port_res) {
1918 DEVPRINTF(1, (dev, "Cannot allocate ioport\n"));
1919 return ENOMEM;
1920 }
1921
1922 sc->irq_rid = 0;
1923 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
1924 RF_ACTIVE);
1925 if (!sc->irq_res) {
1926 DEVPRINTF(1, (dev, "Cannot allocate irq\n"));
1927 xe_deactivate(dev);
1928 return ENOMEM;
1929 }
1930 if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, xe_intr, sc,
1931 &sc->intrhand)) != 0) {
1932 xe_deactivate(dev);
1933 return err;
1934 }
1935
1936 sc->bst = rman_get_bustag(sc->port_res);
1937 sc->bsh = rman_get_bushandle(sc->port_res);
1938 return (0);
1939 }
1940
1941 void
1942 xe_deactivate(device_t dev)
1943 {
1944 struct xe_softc *sc = device_get_softc(dev);
1945
1946 DEVPRINTF(2, (dev, "deactivate\n"));
1947 xe_disable_intr(sc);
1948
1949 if (sc->intrhand)
1950 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
1951 sc->intrhand = 0;
1952 if (sc->port_res)
1953 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1954 sc->port_res);
1955 sc->port_res = 0;
1956 if (sc->ce2_port_res)
1957 bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
1958 sc->ce2_port_res);
1959 sc->ce2_port_res = 0;
1960 if (sc->irq_res)
1961 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1962 sc->irq_res);
1963 sc->irq_res = 0;
1964 return;
1965 }
Cache object: c09118da58fc9256b1487f31d763972a
|