]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ep/if_ep_pccard.c
Merge lld trunk r351319, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / sys / dev / ep / if_ep_pccard.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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 Herb Peyerl.
18  * 4. The name of Herb Peyerl may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /*
34  * Pccard support for 3C589 by:
35  *              HAMADA Naoki
36  *              nao@tom-yam.or.jp
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/module.h>
47 #include <sys/bus.h>
48
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51
52 #include <net/ethernet.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_media.h>
56
57 #include <dev/ep/if_epreg.h>
58 #include <dev/ep/if_epvar.h>
59
60 #include <dev/pccard/pccardvar.h>
61 #include <dev/pccard/pccard_cis.h>
62
63 #include "pccarddevs.h"
64
65 struct ep_pccard_product
66 {
67         struct pccard_product prod;
68         int chipset;
69 };
70
71 #define EP_CHIP_589     1       /* Classic 3c5x9 chipset */
72 #define EP_CHIP_574     2       /* Roadrunner */
73 #define EP_CHIP_C1      3       /* 3c1 */
74
75 static const struct ep_pccard_product ep_pccard_products[] = {
76         { PCMCIA_CARD(3COM, 3C1),               EP_CHIP_C1 },
77         { PCMCIA_CARD(3COM, 3C562),             EP_CHIP_589 },
78         { PCMCIA_CARD(3COM, 3C589),             EP_CHIP_589 },
79         { PCMCIA_CARD(3COM, 3CXEM556),          EP_CHIP_589 },
80         { PCMCIA_CARD(3COM, 3CXEM556INT),       EP_CHIP_589 },
81         { PCMCIA_CARD(3COM, 3C574),             EP_CHIP_574 },
82         { PCMCIA_CARD(3COM, 3CCFEM556BI),       EP_CHIP_574 },
83         { { NULL } }
84 };
85
86 static const struct ep_pccard_product *
87 ep_pccard_lookup(device_t dev)
88 {
89         return ((const struct ep_pccard_product *)pccard_product_lookup(dev,
90             (const struct pccard_product *)ep_pccard_products,
91             sizeof(ep_pccard_products[0]), NULL));
92 }
93
94 static int
95 ep_pccard_probe(device_t dev)
96 {
97         const struct ep_pccard_product *pp;
98         int             error;
99         uint32_t        fcn = PCCARD_FUNCTION_UNSPEC;
100
101         /* Make sure we're a network function */
102         error = pccard_get_function(dev, &fcn);
103         if (error != 0)
104                 return (error);
105         if (fcn != PCCARD_FUNCTION_NETWORK)
106                 return (ENXIO);
107
108         /* Check to see if we know about this card */
109         if ((pp = ep_pccard_lookup(dev)) == NULL)
110                 return EIO;
111         if (pp->prod.pp_name != NULL)
112                 device_set_desc(dev, pp->prod.pp_name);
113
114         return 0;
115 }
116
117 static int
118 ep_pccard_mac(const struct pccard_tuple *tuple, void *argp)
119 {
120         uint8_t *enaddr = argp;
121         int i;
122
123         /* Code 0x88 is 3com's special cis node contianing the MAC */
124         if (tuple->code != 0x88)
125                 return (0);
126
127         /* Make sure this is a sane node */
128         if (tuple->length < ETHER_ADDR_LEN)
129                 return (0);
130
131         /* Copy the MAC ADDR and return success */
132         for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
133                 enaddr[i] = pccard_tuple_read_1(tuple, i + 1);
134                 enaddr[i + 1] = pccard_tuple_read_1(tuple, i);
135         }
136         return (1);
137 }
138
139 static int
140 ep_pccard_attach(device_t dev)
141 {
142         struct ep_softc *sc = device_get_softc(dev);
143         uint16_t result;
144         int error = 0;
145         const struct ep_pccard_product *pp;
146
147         if ((pp = ep_pccard_lookup(dev)) == NULL)
148                 panic("ep_pccard_attach: can't find product in attach.");
149
150         if (pp->chipset == EP_CHIP_574) {
151                 sc->epb.mii_trans = 1;
152                 sc->epb.cmd_off = 2;
153         } else {
154                 sc->epb.mii_trans = 0;
155                 sc->epb.cmd_off = 0;
156         }
157         if ((error = ep_alloc(dev))) {
158                 device_printf(dev, "ep_alloc() failed! (%d)\n", error);
159                 goto bad;
160         }
161
162         if (pp->chipset == EP_CHIP_C1)
163                 sc->stat |= F_HAS_TX_PLL;
164         
165         /* ROM size = 0, ROM base = 0 */
166         /* For now, ignore AUTO SELECT feature of 3C589B and later. */
167         error = ep_get_e(sc, EEPROM_ADDR_CFG, &result);
168         CSR_WRITE_2(sc, EP_W0_ADDRESS_CFG, result & 0xc000);
169
170         /* 
171          * Fake IRQ must be 3 for 3C589 and 3C589B.  3C589D and newer
172          * ignore this value.  3C589C is unknown, as are the other
173          * cards supported by this driver, but it appears to never hurt
174          * and always helps.
175          */
176         SET_IRQ(sc, 3);
177         CSR_WRITE_2(sc, EP_W0_PRODUCT_ID, sc->epb.prod_id);
178
179         if (sc->epb.mii_trans) {
180                 /*
181                  * turn on the MII transciever
182                  */
183                 GO_WINDOW(sc, 3);
184                 CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040);
185                 DELAY(1000);
186                 CSR_WRITE_2(sc, EP_W3_OPTIONS, 0xc040);
187                 CSR_WRITE_2(sc, EP_COMMAND, RX_RESET);
188                 CSR_WRITE_2(sc, EP_COMMAND, TX_RESET);
189                 EP_BUSY_WAIT(sc);
190                 DELAY(1000);
191                 CSR_WRITE_2(sc, EP_W3_OPTIONS, 0x8040);
192         } else
193                 ep_get_media(sc);
194
195         /*
196          * The 3C562 (a-c revisions) stores the MAC in the CIS in a
197          * way that's unique to 3com.  If we have one of these cards,
198          * scan the CIS for that MAC address, and use it if we find
199          * it.  The NetBSD driver says that the ROADRUNNER chips also
200          * do this, which may be true, but none of the cards that I
201          * have include this TUPLE.  Always prefer the MAC addr in the
202          * CIS tuple to the one returned by the card, as it appears that
203          * only those cards that need it have this special tuple.
204          */
205         if (pccard_cis_scan(dev, ep_pccard_mac, sc->eaddr))
206                 sc->stat |= F_ENADDR_SKIP;
207         if ((error = ep_attach(sc))) {
208                 device_printf(dev, "ep_attach() failed! (%d)\n", error);
209                 goto bad;
210         }
211         if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
212             NULL, ep_intr, sc, &sc->ep_intrhand))) {
213                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
214                 goto bad;
215         }
216         return (0);
217 bad:
218         ep_free(dev);
219         return (error);
220 }
221
222 static device_method_t ep_pccard_methods[] = {
223         /* Device interface */
224         DEVMETHOD(device_probe, ep_pccard_probe),
225         DEVMETHOD(device_attach, ep_pccard_attach),
226         DEVMETHOD(device_detach, ep_detach),
227
228         DEVMETHOD_END
229 };
230
231 static driver_t ep_pccard_driver = {
232         "ep",
233         ep_pccard_methods,
234         sizeof(struct ep_softc),
235 };
236
237 extern devclass_t ep_devclass;
238
239 DRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);
240 PCCARD_PNP_INFO(ep_pccard_products);