2 * Copyright (c) 1998, 1999, 2003 Scott Mitchell
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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
31 * Copyright (c) 1997 by Werner Koch (dd9jn)
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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
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.
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
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)
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?)
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.
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
90 * Author email: <scott@uk.freebsd.org>
91 * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
95 #include <sys/param.h>
96 #include <sys/cdefs.h>
97 #include <sys/errno.h>
98 #include <sys/kernel.h>
100 #include <sys/socket.h>
101 #include <sys/sockio.h>
102 #include <sys/systm.h>
104 #include <sys/sysctl.h>
106 #include <sys/module.h>
109 #include <machine/bus.h>
110 #include <machine/resource.h>
111 #include <sys/rman.h>
113 #include <net/ethernet.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>
120 #include <net/if_types.h>
122 #include <dev/xe/if_xereg.h>
123 #include <dev/xe/if_xevar.h>
126 * MII command structure
128 struct xe_mii_frame {
129 u_int8_t mii_stdelim;
131 u_int8_t mii_phyaddr;
132 u_int8_t mii_regaddr;
133 u_int8_t mii_turnaround;
138 * Media autonegotiation progress constants
140 #define XE_AUTONEG_NONE 0 /* No autonegotiation in progress */
141 #define XE_AUTONEG_WAITING 1 /* Waiting for transmitter to go idle */
142 #define XE_AUTONEG_STARTED 2 /* Waiting for autonegotiation to complete */
143 #define XE_AUTONEG_100TX 3 /* Trying to force 100baseTX link */
144 #define XE_AUTONEG_FAIL 4 /* Autonegotiation failed */
147 * Prototypes start here
149 static void xe_init (void *xscp);
150 static void xe_start (struct ifnet *ifp);
151 static int xe_ioctl (struct ifnet *ifp, u_long command, caddr_t data);
152 static void xe_watchdog (struct ifnet *ifp);
153 static int xe_media_change (struct ifnet *ifp);
154 static void xe_media_status (struct ifnet *ifp, struct ifmediareq *mrp);
155 static timeout_t xe_setmedia;
156 static void xe_reset (struct xe_softc *scp);
157 static void xe_stop (struct xe_softc *scp);
158 static void xe_enable_intr (struct xe_softc *scp);
159 static void xe_disable_intr (struct xe_softc *scp);
160 static void xe_set_multicast (struct xe_softc *scp);
161 static void xe_set_addr (struct xe_softc *scp, u_int8_t* addr, unsigned idx);
162 static void xe_mchash (struct xe_softc *scp, const uint8_t *addr);
163 static int xe_pio_write_packet (struct xe_softc *scp, struct mbuf *mbp);
168 static void xe_mii_sync (struct xe_softc *scp);
169 static int xe_mii_init (struct xe_softc *scp);
170 static void xe_mii_send (struct xe_softc *scp, u_int32_t bits, int cnt);
171 static int xe_mii_readreg (struct xe_softc *scp, struct xe_mii_frame *frame);
172 static int xe_mii_writereg (struct xe_softc *scp, struct xe_mii_frame *frame);
173 static u_int16_t xe_phy_readreg (struct xe_softc *scp, u_int16_t reg);
174 static void xe_phy_writereg (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
177 * Debugging functions
179 static void xe_mii_dump (struct xe_softc *scp);
181 static void xe_reg_dump (struct xe_softc *scp);
185 * Debug logging levels - set with hw.xe.debug sysctl
187 * 1 = More hardware details, probe/attach progress
188 * 2 = Most function calls, ioctls and media selection progress
189 * 3 = Everything - interrupts, packets in/out and multicast address setup
195 SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "if_xe parameters");
198 SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW,
199 &xe_debug, 0, "if_xe debug level");
201 #define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
202 #define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
203 #define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
205 #define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
208 #define DEVPRINTF(level, arg)
209 #define DPRINTF(level, arg)
210 #define XE_MII_DUMP(scp)
212 #define XE_REG_DUMP(scp)
221 xe_attach (device_t dev)
223 struct xe_softc *scp = device_get_softc(dev);
225 DEVPRINTF(2, (dev, "attach\n"));
227 /* Initialise stuff... */
229 scp->ifp = if_alloc(IFT_ETHER);
230 if (scp->ifp == NULL)
232 scp->ifm = &scp->ifmedia;
233 scp->autoneg_status = XE_AUTONEG_NONE;
235 /* Initialise the ifnet structure */
236 scp->ifp->if_softc = scp;
237 if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
238 scp->ifp->if_timer = 0;
239 scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
241 scp->ifp->if_linkmib = &scp->mibdata;
242 scp->ifp->if_linkmiblen = sizeof scp->mibdata;
243 scp->ifp->if_start = xe_start;
244 scp->ifp->if_ioctl = xe_ioctl;
245 scp->ifp->if_watchdog = xe_watchdog;
246 scp->ifp->if_init = xe_init;
247 scp->ifp->if_baudrate = 100000000;
248 scp->ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
250 /* Initialise the ifmedia structure */
251 ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
252 callout_handle_init(&scp->chand);
254 /* Add supported media types */
256 ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
257 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
258 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
260 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
262 ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
263 ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
265 /* Default is to autoselect best supported media type */
266 ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
268 /* Get the hardware into a known state */
271 /* Get hardware version numbers */
273 scp->version = XE_INB(XE_BOV);
275 scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
277 scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
279 /* Print some useful information */
280 device_printf(dev, "version 0x%02x/0x%02x%s%s\n", scp->version,
281 scp->srev, scp->mohawk ? ", 100Mbps capable" : "",
282 scp->modem ? ", with modem" : "");
284 XE_SELECT_PAGE(0x10);
285 DEVPRINTF(1, (dev, "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
288 XE_INW(XE_VendorID)));
291 XE_SELECT_PAGE(0x45);
292 DEVPRINTF(1, (dev, "CE2 version = 0x%02x\n", XE_INB(XE_REV)));
295 /* Attach the interface */
296 ether_ifattach(scp->ifp, scp->enaddr);
304 * Complete hardware intitialisation and enable output. Exits without doing
305 * anything if there's no address assigned to the card, or if media selection
306 * is in progress (the latter implies we've already run this function).
309 xe_init(void *xscp) {
310 struct xe_softc *scp = xscp;
314 if (scp->autoneg_status != XE_AUTONEG_NONE) return;
316 DEVPRINTF(2, (scp->dev, "init\n"));
320 /* Reset transmitter flags */
323 scp->tx_timeouts = 0;
325 scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
326 scp->ifp->if_timer = 0;
328 /* Soft reset the card */
330 XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
337 * set GP1 and GP2 as outputs (bits 2 & 3)
338 * set GP1 low to power on the ML6692 (bit 0)
339 * set GP2 high to power on the 10Mhz chip (bit 1)
342 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP2_OUT);
345 /* Shut off interrupts */
346 xe_disable_intr(scp);
348 /* Wait for everything to wake up */
353 scp->phy_ok = xe_mii_init(scp);
355 /* Disable 'source insertion' (not sure what that means) */
356 XE_SELECT_PAGE(0x42);
357 XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
359 /* Set 8K/24K Tx/Rx buffer split */
360 if (scp->srev != 1) {
362 XE_OUTW(XE_RBS, 0x2000);
365 /* Enable early transmit mode on Mohawk/Dingo */
367 XE_SELECT_PAGE(0x03);
368 XE_OUTW(XE_TPT, scp->tx_thres);
369 XE_SELECT_PAGE(0x01);
370 XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
373 /* Put MAC address in first 'individual address' register */
374 XE_SELECT_PAGE(0x50);
375 for (i = 0; i < 6; i++)
376 XE_OUTB(0x08 + i, IF_LLADDR(scp->ifp)[scp->mohawk ? 5 - i : i]);
378 /* Set up multicast addresses */
379 xe_set_multicast(scp);
381 /* Fix the receive data offset -- reset can leave it off-by-one */
383 XE_OUTW(XE_DO, 0x2000);
385 /* Set interrupt masks */
387 XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR | XE_IMR0_RX_PACKET);
389 /* Set MAC interrupt masks */
390 XE_SELECT_PAGE(0x40);
392 ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR
393 | XE_RX0M_ALIGN_ERROR | XE_RX0M_LONG_PACKET));
395 ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN
396 | XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
398 /* Clear MAC status registers */
399 XE_SELECT_PAGE(0x40);
400 XE_OUTB(XE_RST0, 0x00);
401 XE_OUTB(XE_TXST0, 0x00);
403 /* Enable receiver and put MAC online */
404 XE_SELECT_PAGE(0x40);
405 XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
407 /* Set up IMR, enable interrupts */
410 /* Start media selection */
414 scp->ifp->if_drv_flags |= IFF_DRV_RUNNING;
415 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
422 * Start output on interface. Should be called at splimp() priority. Check
423 * that the output is idle (ie, IFF_DRV_OACTIVE is not set) before calling this
424 * function. If media selection is in progress we set IFF_DRV_OACTIVE ourselves
425 * and return immediately.
428 xe_start(struct ifnet *ifp) {
429 struct xe_softc *scp = ifp->if_softc;
432 if (scp->autoneg_status != XE_AUTONEG_NONE) {
433 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
437 DEVPRINTF(3, (scp->dev, "start\n"));
440 * Loop while there are packets to be sent, and space to send them.
443 /* Suck a packet off the send queue */
444 IF_DEQUEUE(&ifp->if_snd, mbp);
448 * We are using the !OACTIVE flag to indicate to the outside world that
449 * we can accept an additional packet rather than that the transmitter
450 * is _actually_ active. Indeed, the transmitter may be active, but if
451 * we haven't filled all the buffers with data then we still want to
454 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
458 if (xe_pio_write_packet(scp, mbp) != 0) {
459 /* Push the packet back onto the queue */
460 IF_PREPEND(&ifp->if_snd, mbp);
461 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
465 /* Tap off here if there is a bpf listener */
468 /* In case we don't hear from the card again... */
478 * Process an ioctl request. Adapted from the ed driver.
481 xe_ioctl (register struct ifnet *ifp, u_long command, caddr_t data) {
482 struct xe_softc *scp;
493 DEVPRINTF(2, (scp->dev, "ioctl: SIOCSIFFLAGS: 0x%04x\n", ifp->if_flags));
495 * If the interface is marked up and stopped, then start it. If it is
496 * marked down and running, then stop it.
498 if (ifp->if_flags & IFF_UP) {
499 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
505 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
508 /* FALL THROUGH (handle changes to PROMISC/ALLMULTI flags) */
512 DEVPRINTF(2, (scp->dev, "ioctl: SIOC{ADD,DEL}MULTI\n"));
514 * Multicast list has (maybe) changed; set the hardware filters
517 xe_set_multicast(scp);
523 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ifmedia_ioctl\n"));
525 * Someone wants to get/set media options.
527 error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
531 DEVPRINTF(3, (scp->dev, "ioctl: bounce to ether_ioctl\n"));
532 error = ether_ioctl(ifp, command, data);
542 * Card interrupt handler.
544 * This function is probably more complicated than it needs to be, as it
545 * attempts to deal with the case where multiple packets get sent between
546 * interrupts. This is especially annoying when working out the collision
547 * stats. Not sure whether this case ever really happens or not (maybe on a
548 * slow/heavily loaded machine?) so it's probably best to leave this like it
551 * Note that the crappy PIO used to get packets on and off the card means that
552 * you will spend a lot of time in this routine -- I can get my P150 to spend
553 * 90% of its time servicing interrupts if I really hammer the network. Could
554 * fix this, but then you'd start dropping/losing packets. The moral of this
555 * story? If you want good network performance _and_ some cycles left over to
556 * get your work done, don't buy a Xircom card. Or convince them to tell me
557 * how to do memory-mapped I/O :)
562 struct xe_softc *scp = (struct xe_softc *) xscp;
564 u_int8_t psr, isr, esr, rsr, rst0, txst0, txst1, coll;
568 /* Disable interrupts */
572 /* Cache current register page */
575 /* Read ISR to see what caused this interrupt */
576 while ((isr = XE_INB(XE_ISR)) != 0) {
578 /* 0xff might mean the card is no longer around */
580 DEVPRINTF(3, (scp->dev, "intr: interrupt received for missing card?\n"));
584 /* Read other status registers */
585 XE_SELECT_PAGE(0x40);
586 rst0 = XE_INB(XE_RST0);
588 txst0 = XE_INB(XE_TXST0);
589 txst1 = XE_INB(XE_TXST1);
590 coll = txst1 & XE_TXST1_RETRY_COUNT;
591 XE_OUTB(XE_TXST0, 0);
592 XE_OUTB(XE_TXST1, 0);
595 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 if (isr & XE_ISR_TX_PACKET) {
600 /* Update packet count, accounting for rollover */
601 tpr = XE_INB(XE_TPR);
602 sent = -scp->tx_tpr + tpr;
604 /* Update statistics if we actually sent anything */
607 scp->tx_queued -= sent;
608 ifp->if_opackets += sent;
609 ifp->if_collisions += coll;
612 * According to the Xircom manual, Dingo will sometimes manage to
613 * transmit a packet with triggering an interrupt. If this happens,
614 * we have sent > 1 and the collision count only reflects collisions
615 * on the last packet sent (the one that triggered the interrupt).
616 * Collision stats might therefore be a bit low, but there doesn't
617 * seem to be anything we can do about that.
624 scp->mibdata.dot3StatsSingleCollisionFrames++;
625 scp->mibdata.dot3StatsCollFrequencies[0]++;
628 scp->mibdata.dot3StatsMultipleCollisionFrames++;
629 scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
633 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
636 /* Handle most MAC interrupts */
637 if (isr & XE_ISR_MAC_INTR) {
640 /* Carrier sense lost -- only in 10Mbit HDX mode */
641 if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
642 /* XXX - Need to update media status here */
643 device_printf(scp->dev, "no carrier\n");
645 scp->mibdata.dot3StatsCarrierSenseErrors++;
648 /* Excessive collisions -- try sending again */
649 if (txst0 & XE_TXST0_16_COLLISIONS) {
650 ifp->if_collisions += 16;
652 scp->mibdata.dot3StatsExcessiveCollisions++;
653 scp->mibdata.dot3StatsMultipleCollisionFrames++;
654 scp->mibdata.dot3StatsCollFrequencies[15]++;
655 XE_OUTB(XE_CR, XE_CR_RESTART_TX);
657 /* Transmit underrun -- increase early transmit threshold */
658 if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
659 DEVPRINTF(1, (scp->dev, "transmit underrun"));
660 if (scp->tx_thres < ETHER_MAX_LEN) {
661 if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
662 scp->tx_thres = ETHER_MAX_LEN;
663 DPRINTF(1, (": increasing transmit threshold to %u", scp->tx_thres));
665 XE_OUTW(XE_TPT, scp->tx_thres);
670 scp->mibdata.dot3StatsInternalMacTransmitErrors++;
672 /* Late collision -- just complain about it */
673 if (txst0 & XE_TXST0_LATE_COLLISION) {
674 device_printf(scp->dev, "late collision\n");
676 scp->mibdata.dot3StatsLateCollisions++;
678 /* SQE test failure -- just complain about it */
679 if (txst0 & XE_TXST0_SQE_FAIL) {
680 device_printf(scp->dev, "SQE test failure\n");
682 scp->mibdata.dot3StatsSQETestErrors++;
684 /* Packet too long -- what happens to these */
685 if (rst0 & XE_RST0_LONG_PACKET) {
686 device_printf(scp->dev, "received giant packet\n");
688 scp->mibdata.dot3StatsFrameTooLongs++;
690 /* CRC error -- packet dropped */
691 if (rst0 & XE_RST0_CRC_ERROR) {
692 device_printf(scp->dev, "CRC error\n");
694 scp->mibdata.dot3StatsFCSErrors++;
698 /* Handle received packet(s) */
699 while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
700 rsr = XE_INB(XE_RSR);
702 DEVPRINTF(3, (scp->dev, "intr: ESR=0x%02x, RSR=0x%02x\n", esr, rsr));
704 /* Make sure packet is a good one */
705 if (rsr & XE_RSR_RX_OK) {
706 struct ether_header *ehp;
710 len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
712 DEVPRINTF(3, (scp->dev, "intr: receive length = %d\n", len));
720 * Allocate mbuf to hold received packet. If the mbuf header isn't
721 * big enough, we attach an mbuf cluster to hold the packet. Note the
722 * +=2 to align the packet data on a 32-bit boundary, and the +3 to
723 * allow for the possibility of reading one more byte than the actual
724 * packet length (we always read 16-bit words).
725 * XXX - Surely there's a better way to do this alignment?
727 MGETHDR(mbp, M_DONTWAIT, MT_DATA);
733 if (len + 3 > MHLEN) {
734 MCLGET(mbp, M_DONTWAIT);
735 if ((mbp->m_flags & M_EXT) == 0) {
743 ehp = mtod(mbp, struct ether_header *);
746 * Now get the packet in PIO mode, including the Ethernet header but
747 * omitting the trailing CRC.
751 * Work around a bug in CE2 cards. There seems to be a problem with
752 * duplicated and extraneous bytes in the receive buffer, but without
753 * any real documentation for the CE2 it's hard to tell for sure.
754 * XXX - Needs testing on CE2 hardware
756 if (scp->srev == 0) {
760 rhs = XE_INW(XE_RHSA);
763 rhs += 3; /* Skip control info */
768 if (rhs + len > 0x8000) {
771 for (i = 0; i < len; i++, rhs++) {
772 ((char *)ehp)[i] = XE_INB(XE_EDP);
780 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
781 (u_int16_t *) ehp, (len + 1) >> 1);
784 bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP,
785 (u_int16_t *) ehp, (len + 1) >> 1);
787 /* Deliver packet to upper layers */
788 mbp->m_pkthdr.rcvif = ifp;
789 mbp->m_pkthdr.len = mbp->m_len = len;
790 (*ifp->if_input)(ifp, mbp);
794 /* Packet alignment error -- drop packet */
795 else if (rsr & XE_RSR_ALIGN_ERROR) {
796 device_printf(scp->dev, "alignment error\n");
797 scp->mibdata.dot3StatsAlignmentErrors++;
801 /* Skip to next packet, if there is one */
802 XE_OUTW(XE_DO, 0x8000);
805 /* Clear receiver overruns now we have some free buffer space */
806 if (rst0 & XE_RST0_RX_OVERRUN) {
807 DEVPRINTF(1, (scp->dev, "receive overrun\n"));
809 scp->mibdata.dot3StatsInternalMacReceiveErrors++;
810 XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
814 /* Restore saved page */
817 /* Re-enable interrupts */
818 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
825 * Device timeout/watchdog routine. Called automatically if we queue a packet
826 * for transmission but don't get an interrupt within a specified timeout
827 * (usually 5 seconds). When this happens we assume the worst and reset the
831 xe_watchdog(struct ifnet *ifp) {
832 struct xe_softc *scp = ifp->if_softc;
834 device_printf(scp->dev, "watchdog timeout: resetting card\n");
836 ifp->if_oerrors += scp->tx_queued;
844 * Change media selection.
847 xe_media_change(struct ifnet *ifp) {
848 struct xe_softc *scp = ifp->if_softc;
850 DEVPRINTF(2, (scp->dev, "media_change\n"));
852 if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER)
856 * Some card/media combos aren't always possible -- filter those out here.
858 if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
859 IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok)
869 * Return current media selection.
872 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp) {
873 struct xe_softc *scp = ifp->if_softc;
875 DEVPRINTF(3, (scp->dev, "media_status\n"));
877 /* XXX - This is clearly wrong. Will fix once I have CE2 working */
878 mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
879 mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
886 * Select active media.
888 static void xe_setmedia(void *xscp) {
889 struct xe_softc *scp = xscp;
890 u_int16_t bmcr, bmsr, anar, lpar;
892 DEVPRINTF(2, (scp->dev, "setmedia\n"));
894 /* Cancel any pending timeout */
895 untimeout(xe_setmedia, scp, scp->chand);
896 xe_disable_intr(scp);
899 scp->media = IFM_ETHER;
900 switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
902 case IFM_AUTO: /* Autoselect media */
903 scp->media = IFM_ETHER|IFM_AUTO;
906 * Autoselection is really awful. It goes something like this:
908 * Wait until the transmitter goes idle (2sec timeout).
910 * IF a 100Mbit PHY exists
911 * Start NWAY autonegotiation (3.5sec timeout)
913 * Select 100baseTX or 10baseT, whichever was detected
916 * IF a 100Mbit PHY exists
917 * Try to force a 100baseTX link (3sec timeout)
926 * IF nothing selected so far
927 * IF a 100Mbit PHY exists
930 * Select 10baseT or 10base2, whichever is connected
934 switch (scp->autoneg_status) {
936 case XE_AUTONEG_NONE:
937 DEVPRINTF(2, (scp->dev, "Waiting for idle transmitter\n"));
938 scp->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
939 scp->autoneg_status = XE_AUTONEG_WAITING;
942 case XE_AUTONEG_WAITING:
943 if (scp->tx_queued != 0) {
944 scp->chand = timeout(xe_setmedia, scp, hz/2);
948 DEVPRINTF(2, (scp->dev, "Starting autonegotiation\n"));
949 bmcr = xe_phy_readreg(scp, PHY_BMCR);
950 bmcr &= ~(PHY_BMCR_AUTONEGENBL);
951 xe_phy_writereg(scp, PHY_BMCR, bmcr);
952 anar = xe_phy_readreg(scp, PHY_ANAR);
953 anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
954 anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
955 xe_phy_writereg(scp, PHY_ANAR, anar);
956 bmcr |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
957 xe_phy_writereg(scp, PHY_BMCR, bmcr);
958 scp->autoneg_status = XE_AUTONEG_STARTED;
959 scp->chand = timeout(xe_setmedia, scp, hz * 7/2);
963 scp->autoneg_status = XE_AUTONEG_FAIL;
967 case XE_AUTONEG_STARTED:
968 bmsr = xe_phy_readreg(scp, PHY_BMSR);
969 lpar = xe_phy_readreg(scp, PHY_LPAR);
970 if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
971 DEVPRINTF(2, (scp->dev, "Autonegotiation complete!\n"));
973 * XXX - Shouldn't have to do this, but (on my hub at least) the
974 * XXX - transmitter won't work after a successful autoneg. So we see
975 * XXX - what the negotiation result was and force that mode. I'm
976 * XXX - sure there is an easy fix for this.
978 if (lpar & PHY_LPAR_100BTXHALF) {
979 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
982 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
983 scp->media = IFM_ETHER|IFM_100_TX;
984 scp->autoneg_status = XE_AUTONEG_NONE;
988 * XXX - Bit of a hack going on in here.
989 * XXX - This is derived from Ken Hughes patch to the Linux driver
990 * XXX - to make it work with 10Mbit _autonegotiated_ links on CE3B
991 * XXX - cards. What's a CE3B and how's it differ from a plain CE3?
992 * XXX - these are the things we need to find out.
994 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
997 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
998 XE_SELECT_PAGE(0x42);
999 XE_OUTB(XE_SWC1, 0x80);
1000 scp->media = IFM_ETHER|IFM_10_T;
1001 scp->autoneg_status = XE_AUTONEG_NONE;
1003 /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/ /* Disable PHY? */
1004 /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
1008 DEVPRINTF(2, (scp->dev, "Autonegotiation failed; trying 100baseTX\n"));
1011 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1012 scp->autoneg_status = XE_AUTONEG_100TX;
1013 scp->chand = timeout(xe_setmedia, scp, hz * 3);
1017 scp->autoneg_status = XE_AUTONEG_FAIL;
1022 case XE_AUTONEG_100TX:
1023 (void)xe_phy_readreg(scp, PHY_BMSR);
1024 bmsr = xe_phy_readreg(scp, PHY_BMSR);
1025 if (bmsr & PHY_BMSR_LINKSTAT) {
1026 DEVPRINTF(2, (scp->dev, "Got 100baseTX link!\n"));
1029 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1030 scp->media = IFM_ETHER|IFM_100_TX;
1031 scp->autoneg_status = XE_AUTONEG_NONE;
1034 DEVPRINTF(2, (scp->dev, "Autonegotiation failed; disabling PHY\n"));
1036 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1038 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY? */
1039 scp->autoneg_status = XE_AUTONEG_FAIL;
1045 * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
1046 * either autonegotiation failed, or never got started to begin with. In
1047 * either case, select a suitable 10Mbit media and hope it works. We
1048 * don't need to reset the card again, since it will have been done
1049 * already by the big switch above.
1051 if (scp->autoneg_status == XE_AUTONEG_FAIL) {
1052 DEVPRINTF(2, (scp->dev, "Selecting 10baseX\n"));
1054 XE_SELECT_PAGE(0x42);
1055 XE_OUTB(XE_SWC1, 0x80);
1056 scp->media = IFM_ETHER|IFM_10_T;
1057 scp->autoneg_status = XE_AUTONEG_NONE;
1061 XE_OUTB(XE_GPR0, 4);
1063 XE_SELECT_PAGE(0x42);
1064 XE_OUTB(XE_SWC1, (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? 0x80 : 0xc0);
1065 scp->media = IFM_ETHER|((XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
1066 scp->autoneg_status = XE_AUTONEG_NONE;
1073 * If a specific media has been requested, we just reset the card and
1074 * select it (one small exception -- if 100baseTX is requested by there is
1075 * no PHY, we fall back to 10baseT operation).
1077 case IFM_100_TX: /* Force 100baseTX */
1079 DEVPRINTF(2, (scp->dev, "Selecting 100baseTX\n"));
1080 XE_SELECT_PAGE(0x42);
1081 XE_OUTB(XE_SWC1, 0);
1082 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
1084 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
1085 scp->media |= IFM_100_TX;
1090 case IFM_10_T: /* Force 10baseT */
1091 DEVPRINTF(2, (scp->dev, "Selecting 10baseT\n"));
1093 xe_phy_writereg(scp, PHY_BMCR, 0x0000);
1095 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08); /* Disable PHY */
1097 XE_SELECT_PAGE(0x42);
1098 XE_OUTB(XE_SWC1, 0x80);
1099 scp->media |= IFM_10_T;
1103 DEVPRINTF(2, (scp->dev, "Selecting 10base2\n"));
1104 XE_SELECT_PAGE(0x42);
1105 XE_OUTB(XE_SWC1, 0xc0);
1106 scp->media |= IFM_10_2;
1112 * Finally, the LEDs are set to match whatever media was chosen and the
1113 * transmitter is unblocked.
1115 DEVPRINTF(2, (scp->dev, "Setting LEDs\n"));
1117 switch (IFM_SUBTYPE(scp->media)) {
1120 XE_OUTB(XE_LED, 0x3b);
1122 XE_OUTB(0x0b, 0x04); /* 100Mbit LED */
1126 XE_OUTB(XE_LED, 0x3a);
1130 /* Restart output? */
1131 xe_enable_intr(scp);
1132 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1138 * Hard reset (power cycle) the card.
1141 xe_reset(struct xe_softc *scp) {
1144 DEVPRINTF(2, (scp->dev, "reset\n"));
1150 XE_OUTB(XE_GPR1, 0);
1153 /* Power up again */
1155 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
1157 XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN|XE_GPR1_AIC);
1167 * Take interface offline. This is done by powering down the device, which I
1168 * assume means just shutting down the transceiver and Ethernet logic. This
1169 * requires a _hard_ reset to recover from, as we need to power up again.
1172 xe_stop(struct xe_softc *scp) {
1175 DEVPRINTF(2, (scp->dev, "stop\n"));
1180 * Shut off interrupts.
1182 xe_disable_intr(scp);
1188 XE_OUTB(XE_GPR1, 0);
1192 * set GP1 and GP2 as outputs (bits 2 & 3)
1193 * set GP1 high to power on the ML6692 (bit 0)
1194 * set GP2 low to power on the 10Mhz chip (bit 1)
1197 XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP1_OUT);
1201 * ~IFF_DRV_RUNNING == interface down.
1203 scp->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1204 scp->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1205 scp->ifp->if_timer = 0;
1212 * Enable interrupts from the card.
1215 xe_enable_intr(struct xe_softc *scp) {
1217 DEVPRINTF(2, (scp->dev, "enable_intr\n"));
1220 XE_OUTB(XE_CR, XE_CR_ENABLE_INTR); /* Enable interrupts */
1221 if (scp->modem && !scp->dingo) { /* This bit is just magic */
1222 if (!(XE_INB(0x10) & 0x01)) {
1223 XE_OUTB(0x10, 0x11); /* Unmask master int enable bit */
1230 * Disable interrupts from the card.
1233 xe_disable_intr(struct xe_softc *scp) {
1235 DEVPRINTF(2, (scp->dev, "disable_intr\n"));
1238 XE_OUTB(XE_CR, 0); /* Disable interrupts */
1239 if (scp->modem && !scp->dingo) { /* More magic */
1240 XE_OUTB(0x10, 0x10); /* Mask the master int enable bit */
1246 * Set up multicast filter and promiscuous modes.
1249 xe_set_multicast(struct xe_softc *scp) {
1251 struct ifmultiaddr *maddr;
1254 DEVPRINTF(2, (scp->dev, "set_multicast\n"));
1257 XE_SELECT_PAGE(0x42);
1259 /* Handle PROMISC flag */
1260 if (ifp->if_flags & IFF_PROMISC) {
1261 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
1265 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
1267 /* Handle ALLMULTI flag */
1268 if (ifp->if_flags & IFF_ALLMULTI) {
1269 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
1273 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
1275 /* Iterate over multicast address list */
1278 TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
1279 if (maddr->ifma_addr->sa_family != AF_LINK)
1285 /* First 9 use Individual Addresses for exact matching */
1286 xe_set_addr(scp, LLADDR((struct sockaddr_dl *)maddr->ifma_addr), count);
1289 /* Use hash filter on Mohawk and Dingo */
1290 xe_mchash(scp, LLADDR((struct sockaddr_dl *)maddr->ifma_addr));
1292 /* Nowhere else to put them on CE2 */
1295 IF_ADDR_UNLOCK(ifp);
1297 DEVPRINTF(2, (scp->dev, "set_multicast: count = %u\n", count));
1299 /* Now do some cleanup and enable multicast handling as needed */
1301 /* Disable all multicast handling */
1302 XE_SELECT_PAGE(0x42);
1303 XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~(XE_SWC1_IA_ENABLE|XE_SWC1_ALLMULTI));
1305 XE_SELECT_PAGE(0x02);
1306 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1309 else if (count < 10) {
1310 /* Full in any unused Individual Addresses with our MAC address */
1311 for (i = count + 1; i < 10; i++)
1312 xe_set_addr(scp, IF_LLADDR(scp->ifp), i);
1313 /* Enable Individual Address matching only */
1314 XE_SELECT_PAGE(0x42);
1315 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1317 XE_SELECT_PAGE(0x02);
1318 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1323 /* Check whether hash table is full */
1324 XE_SELECT_PAGE(0x58);
1325 for (i = 0x08; i < 0x10; i++)
1326 if (XE_INB(i) != 0xff)
1329 /* Hash table full - enable promiscuous multicast matching */
1330 XE_SELECT_PAGE(0x42);
1331 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1332 XE_SELECT_PAGE(0x02);
1333 XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
1336 /* Enable hash table and Individual Address matching */
1337 XE_SELECT_PAGE(0x42);
1338 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
1339 XE_SELECT_PAGE(0x02);
1340 XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
1344 /* Enable promiscuous multicast matching */
1345 XE_SELECT_PAGE(0x42);
1346 XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
1355 * Copy the Ethernet multicast address in addr to the on-chip registers for
1356 * Individual Address idx. Assumes that addr is really a multicast address
1357 * and that idx > 0 (slot 0 is always used for the card MAC address).
1360 xe_set_addr(struct xe_softc *scp, u_int8_t* addr, unsigned idx) {
1365 * Individual Addresses are stored in registers 8-F of pages 0x50-0x57. IA1
1366 * therefore starts at register 0xE on page 0x50. The expressions below
1367 * compute the starting page and register for any IA index > 0.
1370 page = 0x50 + idx%4 + idx/4*3;
1371 reg = 0x0e - 2 * (idx%4);
1373 DEVPRINTF(3, (scp->dev, "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n",
1377 * Copy the IA bytes. Note that the byte order is reversed for Mohawk and
1378 * Dingo wrt. CE2 hardware.
1380 XE_SELECT_PAGE(page);
1381 for (i = 0; i < 6; i++) {
1383 DPRINTF(3, (":%02x", addr[i]));
1386 DEVPRINTF(3, (scp->dev, "set_addr: %02x", addr[0]));
1388 XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
1389 if (++reg == 0x10) {
1391 XE_SELECT_PAGE(++page);
1399 * Set the appropriate bit in the multicast hash table for the supplied
1400 * Ethernet multicast address addr. Assumes that addr is really a multicast
1404 xe_mchash(struct xe_softc* scp, const uint8_t *addr) {
1408 hash = ether_crc32_le(addr, ETHER_ADDR_LEN) & 0x3F;
1410 /* Top 3 bits of hash give register - 8, bottom 3 give bit within register */
1411 byte = hash >> 3 | 0x08;
1412 bit = 0x01 << (hash & 0x07);
1414 DEVPRINTF(3, (scp->dev, "set_hash: hash = 0x%02x, byte = 0x%02x, bit = 0x%02x\n", hash, byte, bit));
1416 XE_SELECT_PAGE(0x58);
1417 XE_OUTB(byte, XE_INB(byte) | bit);
1422 * Write an outgoing packet to the card using programmed I/O.
1425 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp) {
1427 unsigned char wantbyte;
1429 u_int8_t savebyte[2];
1431 /* Get total packet length */
1432 if (mbp->m_flags & M_PKTHDR)
1433 len = mbp->m_pkthdr.len;
1435 struct mbuf* mbp2 = mbp;
1436 for (len = 0; mbp2 != NULL; len += mbp2->m_len, mbp2 = mbp2->m_next);
1439 DEVPRINTF(3, (scp->dev, "pio_write_packet: len = %u\n", len));
1441 /* Packets < minimum length may need to be padded out */
1443 if (len < scp->tx_min) {
1444 pad = scp->tx_min - len;
1448 /* Check transmit buffer space */
1450 XE_OUTW(XE_TRS, len+2); /* Only effective on rev. 1 CE2 cards */
1451 if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
1454 /* Send packet length to card */
1455 XE_OUTW(XE_EDP, len);
1458 * Write packet to card using PIO (code stolen from the ed driver)
1461 while (mbp != NULL) {
1464 data = mtod(mbp, caddr_t);
1465 if (wantbyte) { /* Finish the last word */
1466 savebyte[1] = *data;
1467 XE_OUTW(XE_EDP, *(u_short *)savebyte);
1472 if (len > 1) { /* Output contiguous words */
1473 bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
1478 if (len == 1) { /* Save last byte, if necessary */
1479 savebyte[0] = *data;
1487 * Send last byte of odd-length packets
1490 XE_OUTB(XE_EDP, savebyte[0]);
1493 * Can just tell CE3 cards to send; short packets will be padded out with
1494 * random cruft automatically. For CE2, manually pad the packet with
1495 * garbage; it will be sent when the required number or bytes have been
1496 * delivered to the card.
1499 XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX | XE_CR_ENABLE_INTR);
1502 XE_OUTB(XE_EDP, 0xaa);
1505 XE_OUTW(XE_EDP, 0xdead);
1515 /**************************************************************
1517 * M I I F U N C T I O N S *
1519 **************************************************************/
1522 * Alternative MII/PHY handling code adapted from the xl driver. It doesn't
1523 * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
1524 * XXX - this stuff shouldn't be here. It should all be abstracted off to
1525 * XXX - some kind of common MII-handling code, shared by all drivers. But
1526 * XXX - that's a whole other mission.
1528 #define XE_MII_SET(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
1529 #define XE_MII_CLR(x) XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
1533 * Sync the PHYs by setting data bit and strobing the clock 32 times.
1536 xe_mii_sync(struct xe_softc *scp) {
1540 XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
1542 for (i = 0; i < 32; i++) {
1543 XE_MII_SET(XE_MII_CLK);
1545 XE_MII_CLR(XE_MII_CLK);
1552 * Look for a MII-compliant PHY. If we find one, reset it.
1555 xe_mii_init(struct xe_softc *scp) {
1558 status = xe_phy_readreg(scp, PHY_BMSR);
1559 if ((status & 0xff00) != 0x7800) {
1560 DEVPRINTF(2, (scp->dev, "no PHY found, %0x\n", status));
1564 DEVPRINTF(2, (scp->dev, "PHY OK!\n"));
1567 xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
1569 while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
1577 * Clock a series of bits through the MII.
1580 xe_mii_send(struct xe_softc *scp, u_int32_t bits, int cnt) {
1584 XE_MII_CLR(XE_MII_CLK);
1586 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
1588 XE_MII_SET(XE_MII_WRD);
1590 XE_MII_CLR(XE_MII_WRD);
1593 XE_MII_CLR(XE_MII_CLK);
1595 XE_MII_SET(XE_MII_CLK);
1601 * Read an PHY register through the MII.
1604 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1610 * Set up frame for RX.
1612 frame->mii_stdelim = XE_MII_STARTDELIM;
1613 frame->mii_opcode = XE_MII_READOP;
1614 frame->mii_turnaround = 0;
1615 frame->mii_data = 0;
1618 XE_OUTB(XE_GPR2, 0);
1621 * Turn on data xmit.
1623 XE_MII_SET(XE_MII_DIR);
1628 * Send command/address info.
1630 xe_mii_send(scp, frame->mii_stdelim, 2);
1631 xe_mii_send(scp, frame->mii_opcode, 2);
1632 xe_mii_send(scp, frame->mii_phyaddr, 5);
1633 xe_mii_send(scp, frame->mii_regaddr, 5);
1636 XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
1638 XE_MII_SET(XE_MII_CLK);
1641 /* Turn off xmit. */
1642 XE_MII_CLR(XE_MII_DIR);
1645 XE_MII_CLR(XE_MII_CLK);
1647 ack = XE_INB(XE_GPR2) & XE_MII_RDD;
1648 XE_MII_SET(XE_MII_CLK);
1652 * Now try reading data bits. If the ack failed, we still
1653 * need to clock through 16 cycles to keep the PHY(s) in sync.
1656 for(i = 0; i < 16; i++) {
1657 XE_MII_CLR(XE_MII_CLK);
1659 XE_MII_SET(XE_MII_CLK);
1665 for (i = 0x8000; i; i >>= 1) {
1666 XE_MII_CLR(XE_MII_CLK);
1669 if (XE_INB(XE_GPR2) & XE_MII_RDD)
1670 frame->mii_data |= i;
1673 XE_MII_SET(XE_MII_CLK);
1679 XE_MII_CLR(XE_MII_CLK);
1681 XE_MII_SET(XE_MII_CLK);
1693 * Write to a PHY register through the MII.
1696 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame) {
1702 * Set up frame for TX.
1704 frame->mii_stdelim = XE_MII_STARTDELIM;
1705 frame->mii_opcode = XE_MII_WRITEOP;
1706 frame->mii_turnaround = XE_MII_TURNAROUND;
1711 * Turn on data output.
1713 XE_MII_SET(XE_MII_DIR);
1717 xe_mii_send(scp, frame->mii_stdelim, 2);
1718 xe_mii_send(scp, frame->mii_opcode, 2);
1719 xe_mii_send(scp, frame->mii_phyaddr, 5);
1720 xe_mii_send(scp, frame->mii_regaddr, 5);
1721 xe_mii_send(scp, frame->mii_turnaround, 2);
1722 xe_mii_send(scp, frame->mii_data, 16);
1725 XE_MII_SET(XE_MII_CLK);
1727 XE_MII_CLR(XE_MII_CLK);
1733 XE_MII_CLR(XE_MII_DIR);
1742 * Read a register from the PHY.
1745 xe_phy_readreg(struct xe_softc *scp, u_int16_t reg) {
1746 struct xe_mii_frame frame;
1748 bzero((char *)&frame, sizeof(frame));
1750 frame.mii_phyaddr = 0;
1751 frame.mii_regaddr = reg;
1752 xe_mii_readreg(scp, &frame);
1754 return(frame.mii_data);
1759 * Write to a PHY register.
1762 xe_phy_writereg(struct xe_softc *scp, u_int16_t reg, u_int16_t data) {
1763 struct xe_mii_frame frame;
1765 bzero((char *)&frame, sizeof(frame));
1767 frame.mii_phyaddr = 0;
1768 frame.mii_regaddr = reg;
1769 frame.mii_data = data;
1770 xe_mii_writereg(scp, &frame);
1777 * A bit of debugging code.
1780 xe_mii_dump(struct xe_softc *scp) {
1785 device_printf(scp->dev, "MII registers: ");
1786 for (i = 0; i < 2; i++) {
1787 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1789 for (i = 4; i < 7; i++) {
1790 printf(" %d:%04x", i, xe_phy_readreg(scp, i));
1799 xe_reg_dump(struct xe_softc *scp) {
1804 device_printf(scp->dev, "Common registers: ");
1805 for (i = 0; i < 8; i++) {
1806 printf(" %2.2x", XE_INB(i));
1810 for (page = 0; page <= 8; page++) {
1811 device_printf(scp->dev, "Register page %2.2x: ", page);
1812 XE_SELECT_PAGE(page);
1813 for (i = 8; i < 16; i++) {
1814 printf(" %2.2x", XE_INB(i));
1819 for (page = 0x10; page < 0x5f; page++) {
1820 if ((page >= 0x11 && page <= 0x3f) ||
1822 (page >= 0x43 && page <= 0x4f) ||
1825 device_printf(scp->dev, "Register page %2.2x: ", page);
1826 XE_SELECT_PAGE(page);
1827 for (i = 8; i < 16; i++) {
1828 printf(" %2.2x", XE_INB(i));
1838 xe_activate(device_t dev)
1840 struct xe_softc *sc = device_get_softc(dev);
1843 DEVPRINTF(2, (dev, "activate\n"));
1846 sc->port_rid = 0; /* 0 is managed by pccard */
1847 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1848 &sc->port_rid, 0, ~0, 16, RF_ACTIVE);
1850 else if (sc->dingo) {
1852 * Find a 16 byte aligned ioport for the card.
1854 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
1855 sc->port_rid = 1; /* 0 is managed by pccard */
1858 sc->port_res = bus_alloc_resource(dev,
1859 SYS_RES_IOPORT, &sc->port_rid, start, 0x3ff, 16,
1861 if (sc->port_res == 0)
1862 break; /* we failed */
1863 if ((rman_get_start(sc->port_res) & 0xf) == 0)
1865 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1867 start = (rman_get_start(sc->port_res) + 15) & ~0xf;
1869 DEVPRINTF(1, (dev, "RealPort port 0x%0lx, size 0x%0lx\n",
1870 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1871 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1875 * Find contiguous I/O port for the Ethernet function on CEM2 and
1876 * CEM3 cards. We allocate window 0 wherever pccard has decided
1877 * it should be, then find an available window adjacent to it for
1878 * the second function. Not sure that both windows are actually
1881 DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
1882 sc->ce2_port_rid = 0; /* 0 is managed by pccard */
1883 sc->ce2_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1884 &sc->ce2_port_rid, 0, ~0,
1886 if (!sc->ce2_port_res) {
1887 DEVPRINTF(1, (dev, "Cannot allocate I/O port for modem\n"));
1893 start = bus_get_resource_start(dev, SYS_RES_IOPORT,
1895 for (i = 0; i < 2; i++) {
1896 start += (i == 0 ? 8 : -24);
1897 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
1898 &sc->port_rid, start, start + 15, 16, RF_ACTIVE);
1899 if (sc->port_res == 0)
1900 continue; /* Failed, try again if possible */
1901 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
1902 sc->port_rid) == start)
1903 break; /* Success! */
1905 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1909 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0lx, size 0x%0lx\n",
1910 bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
1911 bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
1914 if (!sc->port_res) {
1915 DEVPRINTF(1, (dev, "Cannot allocate ioport\n"));
1921 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
1924 DEVPRINTF(1, (dev, "Cannot allocate irq\n"));
1928 if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, NULL,
1929 xe_intr, sc, &sc->intrhand)) != 0) {
1934 sc->bst = rman_get_bustag(sc->port_res);
1935 sc->bsh = rman_get_bushandle(sc->port_res);
1940 xe_deactivate(device_t dev)
1942 struct xe_softc *sc = device_get_softc(dev);
1944 DEVPRINTF(2, (dev, "deactivate\n"));
1945 xe_disable_intr(sc);
1948 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
1951 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
1954 if (sc->ce2_port_res)
1955 bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
1957 sc->ce2_port_res = 0;
1959 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,