1 /* $NetBSD: if_xi.c,v 1.68 2009/11/13 01:14:35 dyoung Exp $ */
2 /* OpenBSD: if_xe.c,v 1.9 1999/09/16 11:28:42 niklas Exp */
5 * Copyright (c) 2004 Charles M. Hannum. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
23 * Copyright (c) 1999 Niklas Hallqvist, Brandon Creighton, Job de Haas
24 * All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by Niklas Hallqvist,
37 * Brandon Creighton and Job de Haas.
38 * 4. The name of the author may not be used to endorse or promote products
39 * derived from this software without specific prior written permission
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
42 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
45 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
50 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 * A driver for Xircom CreditCard PCMCIA Ethernet adapters.
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: if_xi.c,v 1.68 2009/11/13 01:14:35 dyoung Exp $");
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/device.h>
67 #include <sys/ioctl.h>
69 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/kernel.h>
75 #include <net/if_dl.h>
76 #include <net/if_media.h>
77 #include <net/if_types.h>
78 #include <net/if_ether.h>
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip.h>
85 #include <netinet/if_inarp.h>
89 #include <netipx/ipx.h>
90 #include <netipx/ipx_if.h>
96 #include <net/bpfdesc.h>
100 * Maximum number of bytes to read per interrupt. Linux recommends
101 * somewhere between 2000-22000.
102 * XXX This is currently a hard maximum.
104 #define MAX_BYTES_INTR 12000
106 #include <dev/mii/mii.h>
107 #include <dev/mii/miivar.h>
109 #include <dev/pcmcia/pcmciareg.h>
110 #include <dev/pcmcia/pcmciavar.h>
111 #include <dev/pcmcia/pcmciadevs.h>
113 #include <dev/pcmcia/if_xireg.h>
114 #include <dev/pcmcia/if_xivar.h>
117 #define INLINE inline
120 #endif /* __GNUC__ */
123 #define XIDEBUG_VALUE 0
126 #define DPRINTF(cat, x) if (xidebug & (cat)) printf x
128 #define XID_CONFIG 0x01
130 #define XID_INTR 0x04
131 #define XID_FIFO 0x08
132 #define XID_MCAST 0x10
135 int xidebug
= XIDEBUG_VALUE
;
140 #define DPRINTF(cat, x) (void)0
145 STATIC
int xi_enable(struct xi_softc
*);
146 STATIC
void xi_disable(struct xi_softc
*);
147 STATIC
void xi_cycle_power(struct xi_softc
*);
148 STATIC
int xi_ether_ioctl(struct ifnet
*, u_long cmd
, void *);
149 STATIC
void xi_full_reset(struct xi_softc
*);
150 STATIC
void xi_init(struct xi_softc
*);
151 STATIC
int xi_ioctl(struct ifnet
*, u_long
, void *);
152 STATIC
int xi_mdi_read(device_t
, int, int);
153 STATIC
void xi_mdi_write(device_t
, int, int, int);
154 STATIC
int xi_mediachange(struct ifnet
*);
155 STATIC u_int16_t
xi_get(struct xi_softc
*);
156 STATIC
void xi_reset(struct xi_softc
*);
157 STATIC
void xi_set_address(struct xi_softc
*);
158 STATIC
void xi_start(struct ifnet
*);
159 STATIC
void xi_statchg(device_t
);
160 STATIC
void xi_stop(struct xi_softc
*);
161 STATIC
void xi_watchdog(struct ifnet
*);
164 xi_attach(struct xi_softc
*sc
, u_int8_t
*myea
)
166 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
170 * Configuration as advised by DINGO documentation.
171 * Dingo has some extra configuration registers in the CCR space.
173 if (sc
->sc_chipset
>= XI_CHIPSET_DINGO
) {
174 struct pcmcia_mem_handle pcmh
;
176 bus_size_t ccr_offset
;
178 /* get access to the DINGO CCR space */
179 if (pcmcia_mem_alloc(psc
->sc_pf
, PCMCIA_CCR_SIZE_DINGO
,
181 DPRINTF(XID_CONFIG
, ("xi: bad mem alloc\n"));
184 if (pcmcia_mem_map(psc
->sc_pf
, PCMCIA_MEM_ATTR
,
185 psc
->sc_pf
->ccr_base
, PCMCIA_CCR_SIZE_DINGO
,
186 &pcmh
, &ccr_offset
, &ccr_window
)) {
187 DPRINTF(XID_CONFIG
, ("xi: bad mem map\n"));
188 pcmcia_mem_free(psc
->sc_pf
, &pcmh
);
192 /* enable the second function - usually modem */
193 bus_space_write_1(pcmh
.memt
, pcmh
.memh
,
194 ccr_offset
+ PCMCIA_CCR_DCOR0
, PCMCIA_CCR_DCOR0_SFINT
);
195 bus_space_write_1(pcmh
.memt
, pcmh
.memh
,
196 ccr_offset
+ PCMCIA_CCR_DCOR1
,
197 PCMCIA_CCR_DCOR1_FORCE_LEVIREQ
| PCMCIA_CCR_DCOR1_D6
);
198 bus_space_write_1(pcmh
.memt
, pcmh
.memh
,
199 ccr_offset
+ PCMCIA_CCR_DCOR2
, 0);
200 bus_space_write_1(pcmh
.memt
, pcmh
.memh
,
201 ccr_offset
+ PCMCIA_CCR_DCOR3
, 0);
202 bus_space_write_1(pcmh
.memt
, pcmh
.memh
,
203 ccr_offset
+ PCMCIA_CCR_DCOR4
, 0);
205 /* We don't need them anymore and can free them (I think). */
206 pcmcia_mem_unmap(psc
->sc_pf
, ccr_window
);
207 pcmcia_mem_free(psc
->sc_pf
, &pcmh
);
211 /* Reset and initialize the card. */
214 printf("%s: MAC address %s\n", device_xname(sc
->sc_dev
), ether_sprintf(myea
));
216 ifp
= &sc
->sc_ethercom
.ec_if
;
217 /* Initialize the ifnet structure. */
218 strlcpy(ifp
->if_xname
, device_xname(sc
->sc_dev
), IFNAMSIZ
);
220 ifp
->if_start
= xi_start
;
221 ifp
->if_ioctl
= xi_ioctl
;
222 ifp
->if_watchdog
= xi_watchdog
;
224 IFF_BROADCAST
| IFF_NOTRAILERS
| IFF_SIMPLEX
| IFF_MULTICAST
;
225 IFQ_SET_READY(&ifp
->if_snd
);
227 /* 802.1q capability */
228 sc
->sc_ethercom
.ec_capabilities
|= ETHERCAP_VLAN_MTU
;
230 /* Attach the interface. */
232 ether_ifattach(ifp
, myea
);
235 * Initialize our media structures and probe the MII.
237 sc
->sc_mii
.mii_ifp
= ifp
;
238 sc
->sc_mii
.mii_readreg
= xi_mdi_read
;
239 sc
->sc_mii
.mii_writereg
= xi_mdi_write
;
240 sc
->sc_mii
.mii_statchg
= xi_statchg
;
241 sc
->sc_ethercom
.ec_mii
= &sc
->sc_mii
;
242 ifmedia_init(&sc
->sc_mii
.mii_media
, 0, xi_mediachange
,
244 DPRINTF(XID_MII
| XID_CONFIG
,
245 ("xi: bmsr %x\n", xi_mdi_read(sc
->sc_dev
, 0, 1)));
247 mii_attach(sc
->sc_dev
, &sc
->sc_mii
, 0xffffffff, MII_PHY_ANY
,
249 if (LIST_FIRST(&sc
->sc_mii
.mii_phys
) == NULL
)
250 ifmedia_add(&sc
->sc_mii
.mii_media
, IFM_ETHER
| IFM_AUTO
, 0,
252 ifmedia_set(&sc
->sc_mii
.mii_media
, IFM_ETHER
| IFM_AUTO
);
255 rnd_attach_source(&sc
->sc_rnd_source
, device_xname(sc
->sc_dev
), RND_TYPE_NET
, 0);
260 xi_detach(device_t self
, int flags
)
262 struct xi_softc
*sc
= device_private(self
);
263 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
265 DPRINTF(XID_CONFIG
, ("xi_detach()\n"));
270 rnd_detach_source(&sc
->sc_rnd_source
);
273 mii_detach(&sc
->sc_mii
, MII_PHY_ANY
, MII_OFFSET_ANY
);
274 ifmedia_delete_instance(&sc
->sc_mii
.mii_media
, IFM_INST_ANY
);
284 struct xi_softc
*sc
= arg
;
285 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
286 u_int8_t esr
, rsr
, isr
, rx_status
;
287 u_int16_t tx_status
, recvcount
= 0, tempint
;
289 DPRINTF(XID_CONFIG
, ("xi_intr()\n"));
291 if (sc
->sc_enabled
== 0 || !device_is_active(sc
->sc_dev
))
294 ifp
->if_timer
= 0; /* turn watchdog timer off */
297 if (sc
->sc_chipset
>= XI_CHIPSET_MOHAWK
) {
298 /* Disable interrupt (Linux does it). */
299 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, CR
, 0);
302 esr
= bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, ESR
);
303 isr
= bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, ISR0
);
304 rsr
= bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, RSR
);
306 /* Check to see if card has been ejected. */
309 printf("%s: interrupt for dead card\n",
310 device_xname(sc
->sc_dev
));
314 DPRINTF(XID_INTR
, ("xi: isr=%02x\n", isr
));
318 bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, RXST0
);
319 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, RXST0
, ~rx_status
& 0xff);
321 bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, TXST0
);
323 bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, TXST1
) << 8;
324 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, TXST0
, 0);
325 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, TXST1
, 0);
326 DPRINTF(XID_INTR
, ("xi: rx_status=%02x tx_status=%04x\n", rx_status
,
330 while (esr
& FULL_PKT_RCV
) {
331 if (!(rsr
& RSR_RX_OK
))
334 /* Compare bytes read this interrupt to hard maximum. */
335 if (recvcount
> MAX_BYTES_INTR
) {
337 ("xi: too many bytes this interrupt\n"));
340 bus_space_write_2(sc
->sc_bst
, sc
->sc_bsh
, DO0
,
343 tempint
= xi_get(sc
); /* XXX doesn't check the error! */
344 recvcount
+= tempint
;
345 ifp
->if_ibytes
+= tempint
;
346 esr
= bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, ESR
);
347 rsr
= bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, RSR
);
350 /* Packet too long? */
351 if (rsr
& RSR_TOO_LONG
) {
353 DPRINTF(XID_INTR
, ("xi: packet too long\n"));
357 if (rsr
& RSR_CRCERR
) {
359 DPRINTF(XID_INTR
, ("xi: CRC error detected\n"));
362 /* Alignment error? */
363 if (rsr
& RSR_ALIGNERR
) {
365 DPRINTF(XID_INTR
, ("xi: alignment error detected\n"));
368 /* Check for rx overrun. */
369 if (rx_status
& RX_OVERRUN
) {
371 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, CR
, CLR_RX_OVERRUN
);
372 DPRINTF(XID_INTR
, ("xi: overrun cleared\n"));
375 /* Try to start more packets transmitting. */
376 if (IFQ_IS_EMPTY(&ifp
->if_snd
) == 0)
379 /* Detected excessive collisions? */
380 if ((tx_status
& EXCESSIVE_COLL
) && ifp
->if_opackets
> 0) {
381 DPRINTF(XID_INTR
, ("xi: excessive collisions\n"));
382 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, CR
, RESTART_TX
);
386 if ((tx_status
& TX_ABORT
) && ifp
->if_opackets
> 0)
389 /* have handled the interrupt */
391 rnd_add_uint32(&sc
->sc_rnd_source
, tx_status
);
395 /* Reenable interrupts. */
397 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, CR
, ENABLE_INT
);
403 * Pull a packet from the card into an mbuf chain.
406 xi_get(struct xi_softc
*sc
)
408 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
409 struct mbuf
*top
, **mp
, *m
;
410 u_int16_t pktlen
, len
, recvcount
= 0;
413 DPRINTF(XID_CONFIG
, ("xi_get()\n"));
417 bus_space_read_2(sc
->sc_bst
, sc
->sc_bsh
, RBC0
) & RBC_COUNT_MASK
;
419 DPRINTF(XID_CONFIG
, ("xi_get: pktlen=%d\n", pktlen
));
423 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
424 * when MPE is set. It is not known why.
429 /* XXX should this be incremented now ? */
432 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
435 m
->m_pkthdr
.rcvif
= ifp
;
436 m
->m_pkthdr
.len
= pktlen
;
443 MGET(m
, M_DONTWAIT
, MT_DATA
);
450 if (pktlen
>= MINCLSIZE
) {
451 MCLGET(m
, M_DONTWAIT
);
452 if (!(m
->m_flags
& M_EXT
)) {
460 char *newdata
= (char *)ALIGN(m
->m_data
+
461 sizeof(struct ether_header
)) -
462 sizeof(struct ether_header
);
463 len
-= newdata
- m
->m_data
;
466 len
= min(pktlen
, len
);
467 data
= mtod(m
, u_int8_t
*);
470 bus_space_read_multi_2(sc
->sc_bst
, sc
->sc_bsh
, EDP
,
471 (u_int16_t
*)data
, len
>>1);
473 *data
= bus_space_read_1(sc
->sc_bst
, sc
->sc_bsh
, EDP
);
480 /* Skip Rx packet. */
481 bus_space_write_2(sc
->sc_bst
, sc
->sc_bsh
, DO0
, DO_SKIP_RX_PKT
);
486 /* Trim the CRC off the end of the packet. */
487 m_adj(top
, -ETHER_CRC_LEN
);
493 bpf_mtap(ifp
->if_bpf
, top
);
496 (*ifp
->if_input
)(ifp
, top
);
501 * Serial management for the MII.
502 * The DELAY's below stem from the fact that the maximum frequency
503 * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
504 * go much faster than that.
507 /* Let the MII serial management be idle for one period. */
508 static INLINE
void xi_mdi_idle(struct xi_softc
*);
510 xi_mdi_idle(struct xi_softc
*sc
)
512 bus_space_tag_t bst
= sc
->sc_bst
;
513 bus_space_handle_t bsh
= sc
->sc_bsh
;
515 /* Drive MDC low... */
516 bus_space_write_1(bst
, bsh
, GP2
, MDC_LOW
);
519 /* and high again. */
520 bus_space_write_1(bst
, bsh
, GP2
, MDC_HIGH
);
524 /* Pulse out one bit of data. */
525 static INLINE
void xi_mdi_pulse(struct xi_softc
*, int);
527 xi_mdi_pulse(struct xi_softc
*sc
, int data
)
529 bus_space_tag_t bst
= sc
->sc_bst
;
530 bus_space_handle_t bsh
= sc
->sc_bsh
;
531 u_int8_t bit
= data
? MDIO_HIGH
: MDIO_LOW
;
533 /* First latch the data bit MDIO with clock bit MDC low...*/
534 bus_space_write_1(bst
, bsh
, GP2
, bit
| MDC_LOW
);
537 /* then raise the clock again, preserving the data bit. */
538 bus_space_write_1(bst
, bsh
, GP2
, bit
| MDC_HIGH
);
542 /* Probe one bit of data. */
543 static INLINE
int xi_mdi_probe(struct xi_softc
*sc
);
545 xi_mdi_probe(struct xi_softc
*sc
)
547 bus_space_tag_t bst
= sc
->sc_bst
;
548 bus_space_handle_t bsh
= sc
->sc_bsh
;
551 /* Pull clock bit MDCK low... */
552 bus_space_write_1(bst
, bsh
, GP2
, MDC_LOW
);
555 /* Read data and drive clock high again. */
556 x
= bus_space_read_1(bst
, bsh
, GP2
);
557 bus_space_write_1(bst
, bsh
, GP2
, MDC_HIGH
);
563 /* Pulse out a sequence of data bits. */
564 static INLINE
void xi_mdi_pulse_bits(struct xi_softc
*, u_int32_t
, int);
566 xi_mdi_pulse_bits(struct xi_softc
*sc
, u_int32_t data
, int len
)
570 for (mask
= 1 << (len
- 1); mask
; mask
>>= 1)
571 xi_mdi_pulse(sc
, data
& mask
);
574 /* Read a PHY register. */
576 xi_mdi_read(device_t self
, int phy
, int reg
)
578 struct xi_softc
*sc
= device_private(self
);
584 for (i
= 0; i
< 32; i
++) /* Synchronize. */
586 xi_mdi_pulse_bits(sc
, 0x06, 4); /* Start + Read opcode */
587 xi_mdi_pulse_bits(sc
, phy
, 5); /* PHY address */
588 xi_mdi_pulse_bits(sc
, reg
, 5); /* PHY register */
589 xi_mdi_idle(sc
); /* Turn around. */
590 xi_mdi_probe(sc
); /* Drop initial zero bit. */
592 for (mask
= 1 << 15; mask
; mask
>>= 1) {
593 if (xi_mdi_probe(sc
))
599 ("xi_mdi_read: phy %d reg %d -> %x\n", phy
, reg
, data
));
604 /* Write a PHY register. */
606 xi_mdi_write(device_t self
, int phy
, int reg
, int value
)
608 struct xi_softc
*sc
= device_private(self
);
612 for (i
= 0; i
< 32; i
++) /* Synchronize. */
614 xi_mdi_pulse_bits(sc
, 0x05, 4); /* Start + Write opcode */
615 xi_mdi_pulse_bits(sc
, phy
, 5); /* PHY address */
616 xi_mdi_pulse_bits(sc
, reg
, 5); /* PHY register */
617 xi_mdi_pulse_bits(sc
, 0x02, 2); /* Turn around. */
618 xi_mdi_pulse_bits(sc
, value
, 16); /* Write the data */
619 xi_mdi_idle(sc
); /* Idle away. */
622 ("xi_mdi_write: phy %d reg %d val %x\n", phy
, reg
, value
));
626 xi_statchg(device_t self
)
628 /* XXX Update ifp->if_baudrate */
632 * Change media according to request.
635 xi_mediachange(struct ifnet
*ifp
)
639 DPRINTF(XID_CONFIG
, ("xi_mediachange()\n"));
641 if (ifp
->if_flags
& IFF_UP
) {
643 xi_init(ifp
->if_softc
);
650 xi_reset(struct xi_softc
*sc
)
654 DPRINTF(XID_CONFIG
, ("xi_reset()\n"));
663 xi_watchdog(struct ifnet
*ifp
)
665 struct xi_softc
*sc
= ifp
->if_softc
;
667 printf("%s: device timeout\n", device_xname(sc
->sc_dev
));
674 xi_stop(register struct xi_softc
*sc
)
676 bus_space_tag_t bst
= sc
->sc_bst
;
677 bus_space_handle_t bsh
= sc
->sc_bsh
;
679 DPRINTF(XID_CONFIG
, ("xi_stop()\n"));
682 bus_space_write_1(bst
, bsh
, CMD0
, DISABLE_RX
);
684 /* Disable interrupts. */
686 bus_space_write_1(bst
, bsh
, CR
, 0);
689 bus_space_write_1(bst
, bsh
, IMR0
, 0);
691 /* Cancel watchdog timer. */
692 sc
->sc_ethercom
.ec_if
.if_timer
= 0;
696 xi_enable(struct xi_softc
*sc
)
700 if (!sc
->sc_enabled
) {
701 error
= (*sc
->sc_enable
)(sc
);
711 xi_disable(struct xi_softc
*sc
)
714 if (sc
->sc_enabled
) {
716 (*sc
->sc_disable
)(sc
);
721 xi_init(struct xi_softc
*sc
)
723 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
724 bus_space_tag_t bst
= sc
->sc_bst
;
725 bus_space_handle_t bsh
= sc
->sc_bsh
;
727 DPRINTF(XID_CONFIG
, ("xi_init()\n"));
729 /* Setup the ethernet interrupt mask. */
731 bus_space_write_1(bst
, bsh
, IMR0
,
732 ISR_TX_OFLOW
| ISR_PKT_TX
| ISR_MAC_INT
| /* ISR_RX_EARLY | */
733 ISR_RX_FULL
| ISR_RX_PKT_REJ
| ISR_FORCED_INT
);
734 if (sc
->sc_chipset
< XI_CHIPSET_DINGO
) {
735 /* XXX What is this? Not for Dingo at least. */
736 /* Unmask TX underrun detection */
737 bus_space_write_1(bst
, bsh
, IMR1
, 1);
740 /* Enable interrupts. */
742 bus_space_write_1(bst
, bsh
, CR
, ENABLE_INT
);
747 bus_space_write_1(bst
, bsh
, CMD0
, ENABLE_RX
| ONLINE
);
751 /* Set current media. */
752 mii_mediachg(&sc
->sc_mii
);
754 ifp
->if_flags
|= IFF_RUNNING
;
755 ifp
->if_flags
&= ~IFF_OACTIVE
;
761 * Start outputting on the interface.
762 * Always called as splnet().
765 xi_start(struct ifnet
*ifp
)
767 struct xi_softc
*sc
= ifp
->if_softc
;
768 bus_space_tag_t bst
= sc
->sc_bst
;
769 bus_space_handle_t bsh
= sc
->sc_bsh
;
770 unsigned int s
, len
, pad
= 0;
774 DPRINTF(XID_CONFIG
, ("xi_start()\n"));
776 /* Don't transmit if interface is busy or not running. */
777 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_OACTIVE
)) != IFF_RUNNING
) {
778 DPRINTF(XID_CONFIG
, ("xi: interface busy or not running\n"));
782 /* Peek at the next packet. */
783 IFQ_POLL(&ifp
->if_snd
, m0
);
787 /* We need to use m->m_pkthdr.len, so require the header. */
788 if (!(m0
->m_flags
& M_PKTHDR
))
789 panic("xi_start: no header mbuf");
791 len
= m0
->m_pkthdr
.len
;
794 /* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
795 if (len
< ETHER_MIN_LEN
- ETHER_CRC_LEN
)
796 pad
= ETHER_MIN_LEN
- ETHER_CRC_LEN
- len
;
803 bus_space_write_2(bst
, bsh
, TRS
, (u_int16_t
)len
+ pad
+ 2);
804 space
= bus_space_read_2(bst
, bsh
, TSO
) & 0x7fff;
805 if (len
+ pad
+ 2 > space
) {
807 ("xi: not enough space in output FIFO (%d > %d)\n",
808 len
+ pad
+ 2, space
));
812 IFQ_DEQUEUE(&ifp
->if_snd
, m0
);
816 bpf_mtap(ifp
->if_bpf
, m0
);
820 * Do the output at splhigh() so that an interrupt from another device
821 * won't cause a FIFO underrun.
825 bus_space_write_2(bst
, bsh
, EDP
, (u_int16_t
)len
+ pad
);
828 bus_space_write_multi_2(bst
, bsh
, EDP
,
829 mtod(m
, u_int16_t
*), m
->m_len
>>1);
831 DPRINTF(XID_CONFIG
, ("xi: XXX odd!\n"));
832 bus_space_write_1(bst
, bsh
, EDP
,
833 *(mtod(m
, u_int8_t
*) + m
->m_len
- 1));
838 DPRINTF(XID_CONFIG
, ("xi: len=%d pad=%d total=%d\n", len
, pad
, len
+pad
+4));
839 if (sc
->sc_chipset
>= XI_CHIPSET_MOHAWK
)
840 bus_space_write_1(bst
, bsh
, CR
, TX_PKT
| ENABLE_INT
);
842 for (; pad
> 1; pad
-= 2)
843 bus_space_write_2(bst
, bsh
, EDP
, 0);
845 bus_space_write_1(bst
, bsh
, EDP
, 0);
855 xi_ether_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
857 struct ifaddr
*ifa
= (struct ifaddr
*)data
;
858 struct xi_softc
*sc
= ifp
->if_softc
;
861 DPRINTF(XID_CONFIG
, ("xi_ether_ioctl()\n"));
865 if ((error
= xi_enable(sc
)) != 0)
868 ifp
->if_flags
|= IFF_UP
;
871 switch (ifa
->ifa_addr
->sa_family
) {
874 arp_ifinit(ifp
, ifa
);
892 xi_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
894 struct xi_softc
*sc
= ifp
->if_softc
;
897 DPRINTF(XID_CONFIG
, ("xi_ioctl()\n"));
903 error
= xi_ether_ioctl(ifp
, cmd
, data
);
907 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
909 /* XXX re-use ether_ioctl() */
910 switch (ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) {
913 * If interface is marked down and it is running,
917 ifp
->if_flags
&= ~IFF_RUNNING
;
922 * If interface is marked up and it is stopped,
925 if ((error
= xi_enable(sc
)) != 0)
929 case IFF_UP
|IFF_RUNNING
:
931 * Reset the interface to pick up changes in any
932 * other flags that affect hardware registers.
943 if (sc
->sc_enabled
== 0) {
950 if ((error
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
952 * Multicast list has changed; set the hardware
953 * filter accordingly.
955 if (ifp
->if_flags
& IFF_RUNNING
)
962 error
= ether_ioctl(ifp
, cmd
, data
);
971 xi_set_address(struct xi_softc
*sc
)
973 bus_space_tag_t bst
= sc
->sc_bst
;
974 bus_space_handle_t bsh
= sc
->sc_bsh
;
975 struct ethercom
*ether
= &sc
->sc_ethercom
;
976 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
977 struct ether_multistep step
;
978 struct ether_multi
*enm
;
982 const u_int8_t
*enaddr
;
983 u_int8_t indaddr
[64];
985 DPRINTF(XID_CONFIG
, ("xi_set_address()\n"));
987 enaddr
= (const u_int8_t
*)CLLADDR(ifp
->if_sadl
);
988 if (sc
->sc_chipset
>= XI_CHIPSET_MOHAWK
)
989 for (i
= 0; i
< 6; i
++)
990 indaddr
[i
] = enaddr
[5 - i
];
992 for (i
= 0; i
< 6; i
++)
993 indaddr
[i
] = enaddr
[i
];
996 if (ether
->ec_multicnt
> 9) {
997 ifp
->if_flags
|= IFF_ALLMULTI
;
1001 ETHER_FIRST_MULTI(step
, ether
, enm
);
1002 for (; enm
; num
++) {
1003 if (memcmp(enm
->enm_addrlo
, enm
->enm_addrhi
,
1004 sizeof(enm
->enm_addrlo
)) != 0) {
1006 * The multicast address is really a range;
1007 * it's easier just to accept all multicasts.
1008 * XXX should we be setting IFF_ALLMULTI here?
1010 ifp
->if_flags
|= IFF_ALLMULTI
;
1013 if (sc
->sc_chipset
>= XI_CHIPSET_MOHAWK
)
1014 for (i
= 0; i
< 6; i
++)
1015 indaddr
[num
* 6 + i
] = enm
->enm_addrlo
[5 - i
];
1017 for (i
= 0; i
< 6; i
++)
1018 indaddr
[num
* 6 + i
] = enm
->enm_addrlo
[i
];
1019 ETHER_NEXT_MULTI(step
, enm
);
1021 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1025 memset(&indaddr
[num
* 6], 0xff, 6 * (10 - num
));
1027 for (page
= 0; page
< 8; page
++) {
1029 if (xidebug
& XID_MCAST
) {
1030 printf("page %d before:", page
);
1031 for (i
= 0; i
< 8; i
++)
1032 printf(" %02x", indaddr
[page
* 8 + i
]);
1037 PAGE(sc
, 0x50 + page
);
1038 bus_space_write_region_1(bst
, bsh
, IA
, &indaddr
[page
* 8],
1042 * Without this delay, the address registers on my CE2 get
1043 * trashed the first and I have to cycle it. I have no idea
1044 * why. - mycroft, 2004/08/09
1049 if (xidebug
& XID_MCAST
) {
1050 bus_space_read_region_1(bst
, bsh
, IA
,
1051 &indaddr
[page
* 8], page
== 7 ? 4 : 8);
1052 printf("page %d after: ", page
);
1053 for (i
= 0; i
< 8; i
++)
1054 printf(" %02x", indaddr
[page
* 8 + i
]);
1062 if (ifp
->if_flags
& IFF_PROMISC
)
1064 if (ifp
->if_flags
& (IFF_ALLMULTI
|IFF_PROMISC
))
1065 x
|= SWC1_MCAST_PROM
;
1066 if (!LIST_FIRST(&sc
->sc_mii
.mii_phys
))
1067 x
|= SWC1_AUTO_MEDIA
;
1068 bus_space_write_1(sc
->sc_bst
, sc
->sc_bsh
, SWC1
, x
);
1072 xi_cycle_power(struct xi_softc
*sc
)
1074 bus_space_tag_t bst
= sc
->sc_bst
;
1075 bus_space_handle_t bsh
= sc
->sc_bsh
;
1077 DPRINTF(XID_CONFIG
, ("xi_cycle_power()\n"));
1081 bus_space_write_1(bst
, bsh
, GP1
, 0);
1082 tsleep(&xi_cycle_power
, PWAIT
, "xipwr1", hz
* 40 / 1000);
1083 if (sc
->sc_chipset
>= XI_CHIPSET_MOHAWK
)
1084 bus_space_write_1(bst
, bsh
, GP1
, POWER_UP
);
1086 /* XXX What is bit 2 (aka AIC)? */
1087 bus_space_write_1(bst
, bsh
, GP1
, POWER_UP
| 4);
1088 tsleep(&xi_cycle_power
, PWAIT
, "xipwr2", hz
* 20 / 1000);
1092 xi_full_reset(struct xi_softc
*sc
)
1094 bus_space_tag_t bst
= sc
->sc_bst
;
1095 bus_space_handle_t bsh
= sc
->sc_bsh
;
1098 DPRINTF(XID_CONFIG
, ("xi_full_reset()\n"));
1100 /* Do an as extensive reset as possible on all functions. */
1102 bus_space_write_1(bst
, bsh
, CR
, SOFT_RESET
);
1103 tsleep(&xi_full_reset
, PWAIT
, "xirst1", hz
* 20 / 1000);
1104 bus_space_write_1(bst
, bsh
, CR
, 0);
1105 tsleep(&xi_full_reset
, PWAIT
, "xirst2", hz
* 20 / 1000);
1107 if (sc
->sc_chipset
>= XI_CHIPSET_MOHAWK
) {
1109 * Drive GP1 low to power up ML6692 and GP2 high to power up
1110 * the 10MHz chip. XXX What chip is that? The phy?
1112 bus_space_write_1(bst
, bsh
, GP0
, GP1_OUT
| GP2_OUT
| GP2_WR
);
1114 tsleep(&xi_full_reset
, PWAIT
, "xirst3", hz
* 500 / 1000);
1116 /* Get revision information. XXX Symbolic constants. */
1117 sc
->sc_rev
= bus_space_read_1(bst
, bsh
, BV
) &
1118 ((sc
->sc_chipset
>= XI_CHIPSET_MOHAWK
) ? 0x70 : 0x30) >> 4;
1119 DPRINTF(XID_CONFIG
, ("xi: rev=%02x\n", sc
->sc_rev
));
1121 /* Media selection. XXX Maybe manual overriding too? */
1122 if (sc
->sc_chipset
< XI_CHIPSET_MOHAWK
) {
1124 * XXX I have no idea what this really does, it is from the
1127 bus_space_write_1(bst
, bsh
, GP0
, GP1_OUT
);
1129 tsleep(&xi_full_reset
, PWAIT
, "xirst4", hz
* 40 / 1000);
1132 * Disable source insertion.
1133 * XXX Dingo does not have this bit, but Linux does it unconditionally.
1135 if (sc
->sc_chipset
< XI_CHIPSET_DINGO
) {
1137 bus_space_write_1(bst
, bsh
, SWC0
, 0x20);
1140 /* Set the local memory dividing line. */
1141 if (sc
->sc_rev
!= 1) {
1143 /* XXX Symbolic constant preferrable. */
1144 bus_space_write_2(bst
, bsh
, RBS0
, 0x2000);
1148 * Apparently the receive byte pointer can be bad after a reset, so
1149 * we hardwire it correctly.
1152 bus_space_write_2(bst
, bsh
, DO0
, DO_CHG_OFFSET
);
1154 /* Setup ethernet MAC registers. XXX Symbolic constants. */
1156 bus_space_write_1(bst
, bsh
, RX0MSK
,
1157 PKT_TOO_LONG
| CRC_ERR
| RX_OVERRUN
| RX_ABORT
| RX_OK
);
1158 bus_space_write_1(bst
, bsh
, TX0MSK
,
1159 CARRIER_LOST
| EXCESSIVE_COLL
| TX_UNDERRUN
| LATE_COLLISION
|
1160 SQE
| TX_ABORT
| TX_OK
);
1161 if (sc
->sc_chipset
< XI_CHIPSET_DINGO
)
1162 /* XXX From Linux, dunno what 0xb0 means. */
1163 bus_space_write_1(bst
, bsh
, TX1MSK
, 0xb0);
1164 bus_space_write_1(bst
, bsh
, RXST0
, 0);
1165 bus_space_write_1(bst
, bsh
, TXST0
, 0);
1166 bus_space_write_1(bst
, bsh
, TXST1
, 0);
1170 /* Enable MII function if available. */
1172 if (LIST_FIRST(&sc
->sc_mii
.mii_phys
))
1174 bus_space_write_1(bst
, bsh
, MSR
, x
);
1175 tsleep(&xi_full_reset
, PWAIT
, "xirst5", hz
* 20 / 1000);
1177 /* Configure the LED registers. */
1178 /* XXX This is not good for 10base2. */
1179 bus_space_write_1(bst
, bsh
, LED
,
1180 (LED_TX_ACT
<< LED1_SHIFT
) | (LED_10MB_LINK
<< LED0_SHIFT
));
1181 if (sc
->sc_chipset
>= XI_CHIPSET_DINGO
)
1182 bus_space_write_1(bst
, bsh
, LED3
, LED_100MB_LINK
<< LED3_SHIFT
);
1185 * The Linux driver says this:
1186 * We should switch back to page 0 to avoid a bug in revision 0
1187 * where regs with offset below 8 can't be read after an access
1188 * to the MAC registers.