Merge from vendor branch TEXINFO:
[dragonfly.git] / sys / dev / netif / xe / if_xe_pccard.c
1 /*
2  * Copyright (c) 2002 Takeshi Shibagaki
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  * xe pccard interface driver
27  *
28  * $FreeBSD: src/sys/dev/xe/if_xe_pccard.c,v 1.1 2002/02/20 14:23:58 shiba Exp $
29  * $DragonFly: src/sys/dev/netif/xe/if_xe_pccard.c,v 1.1 2005/07/13 17:31:05 joerg Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36
37 #include <sys/module.h>
38 #include <sys/bus.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/rman.h>
43  
44 #include <net/ethernet.h>
45 #include <net/if.h> 
46 #include <net/if_arp.h>
47 #include <net/if_media.h>
48 #include <net/if_mib.h>
49
50 #include <bus/pccard/pccardvar.h>
51 #include <bus/pccard/pccarddevs.h>
52 #include "card_if.h"
53
54 #include "if_xereg.h"
55 #include "if_xevar.h"
56
57 static const struct pccard_product xe_pccard_products[] = {
58         PCMCIA_CARD(ACCTON, EN2226, 0),
59         PCMCIA_CARD(COMPAQ2, CPQ_10_100, 0),
60         PCMCIA_CARD(INTEL, EEPRO100, 0),
61         PCMCIA_CARD(XIRCOM, CE, 0),
62         PCMCIA_CARD(XIRCOM, CE2, 0),
63         PCMCIA_CARD(XIRCOM, CE3, 0),
64         PCMCIA_CARD(XIRCOM, CEM, 0),
65         PCMCIA_CARD(XIRCOM, CEM28, 0),
66         PCMCIA_CARD(XIRCOM, CEM33, 0),
67         PCMCIA_CARD(XIRCOM, CEM56, 0),
68         PCMCIA_CARD(XIRCOM, REM56, 0),
69         PCMCIA_CARD(XIRCOM, CNW_801, 0),
70         PCMCIA_CARD(XIRCOM, CNW_802, 0),
71         { NULL }
72 };
73
74 struct xe_vendor {
75         uint32_t         vendor_id;
76         const char      *vendor_desc;
77 } xe_vendors[] = {
78         { PCMCIA_VENDOR_XIRCOM,         "Xircom" },
79         { PCMCIA_VENDOR_COMPAQ,         "Compaq" },
80         { PCMCIA_VENDOR_COMPAQ2,        "Compaq" },
81         { PCMCIA_VENDOR_INTEL,          "Intel" },
82         { 0,                            "Unknown" }
83 };
84
85 #define XE_CARD_TYPE_FLAGS_NO           0x0
86 #define XE_CARD_TYPE_FLAGS_CE2          0x1
87 #define XE_CARD_TYPE_FLAGS_MOHAWK       0x2
88 #define XE_CARD_TYPE_FLAGS_DINGO        0x4
89
90 #define XE_PROD_UMASK           0x100f
91 #define XE_PROD_MODEM_UMASK     0x1000
92 #define XE_PROD_SINGLE_ID1      0x1
93 #define XE_PROD_SINGLE_ID2      0x2
94 #define XE_PROD_SINGLE_ID3      0x3
95 #define XE_PROD_MULTI_ID1       0x1001
96 #define XE_PROD_MULTI_ID2       0x1002
97 #define XE_PROD_MULTI_ID3       0x1003
98 #define XE_PROD_MULTI_ID4       0x1004
99 #define XE_PROD_MULTI_ID5       0x1005
100 #define XE_PROD_MULTI_ID6       0x1006
101 #define XE_PROD_MULTI_ID7       0x1007
102
103 struct xe_card_type {
104         uint32_t         prod_type;
105         const char      *card_type_desc;
106         uint32_t         flags;
107 } xe_card_types[] = {
108         { XE_PROD_MULTI_ID1,    "CEM",          XE_CARD_TYPE_FLAGS_NO },
109         { XE_PROD_MULTI_ID2,    "CEM2",         XE_CARD_TYPE_FLAGS_CE2 },
110         { XE_PROD_MULTI_ID3,    "CEM3",         XE_CARD_TYPE_FLAGS_CE2 },
111         { XE_PROD_MULTI_ID4,    "CEM33",        XE_CARD_TYPE_FLAGS_CE2 },
112         { XE_PROD_MULTI_ID5,    "CEM56M",       XE_CARD_TYPE_FLAGS_MOHAWK },
113         { XE_PROD_MULTI_ID6,    "CEM56",        XE_CARD_TYPE_FLAGS_MOHAWK |
114                                                 XE_CARD_TYPE_FLAGS_DINGO },
115         { XE_PROD_MULTI_ID7,    "CEM56",        XE_CARD_TYPE_FLAGS_MOHAWK |
116                                                 XE_CARD_TYPE_FLAGS_DINGO },
117         { XE_PROD_SINGLE_ID1,   "CE",           XE_CARD_TYPE_FLAGS_NO },
118         { XE_PROD_SINGLE_ID2,   "CE2",          XE_CARD_TYPE_FLAGS_CE2 },
119         { XE_PROD_SINGLE_ID3,   "CE3",          XE_CARD_TYPE_FLAGS_MOHAWK },
120         { 0, NULL, -1 }
121 };
122
123 static struct xe_vendor         *xe_vendor_lookup       (uint32_t);
124 static struct xe_card_type      *xe_card_type_lookup    (uint32_t);
125
126 static int      xe_cem56fix     (device_t);
127 static int      xe_pccard_probe (device_t);
128 static int      xe_pccard_match (device_t);
129 static int      xe_pccard_attach(device_t);
130
131 static device_method_t xe_pccard_methods[] = {
132         /* Device interface */
133         DEVMETHOD(device_probe,         pccard_compat_probe),
134         DEVMETHOD(device_attach,        pccard_compat_attach),
135         DEVMETHOD(device_detach,        xe_detach),
136
137         /* Card interface */
138         DEVMETHOD(card_compat_match,    xe_pccard_match),
139         DEVMETHOD(card_compat_probe,    xe_pccard_probe),
140         DEVMETHOD(card_compat_attach,   xe_pccard_attach),
141
142         { 0, 0 }
143 };
144
145 static driver_t xe_pccard_driver = {
146         "xe",
147         xe_pccard_methods,
148         sizeof(struct xe_softc),
149 };
150
151 devclass_t xe_devclass;
152 DRIVER_MODULE(xe, pccard, xe_pccard_driver, xe_devclass, 0, 0);
153
154 /*
155  * Fixing for RealPort cards - they need a little furtling to get the
156  * ethernet working. But this codes don't work well in NEWCARD.
157  */
158 static int
159 xe_cem56fix(device_t dev)
160 {
161         struct xe_softc *sc = device_get_softc(dev);
162         bus_space_tag_t bst;
163         bus_space_handle_t bsh;
164         struct resource *r;
165         int rid;
166         int ioport;
167
168         device_printf(dev, "Realport port 0x%0lx, size 0x%0lx\n",
169             bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
170             bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid));
171
172         rid = 0;
173         r = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0,
174                                ~0, 4 << 10, RF_ACTIVE);
175         if (r == NULL) {
176                 device_printf(dev, "Can't map in attribute memory\n");
177                 return -1;
178         }
179
180         bsh = rman_get_bushandle(r);
181         bst = rman_get_bustag(r);
182
183         CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, rid,
184                            PCCARD_A_MEM_ATTR);
185
186         bus_space_write_1(bst, bsh, DINGO_ECOR, DINGO_ECOR_IRQ_LEVEL |
187                                                 DINGO_ECOR_INT_ENABLE |
188                                                 DINGO_ECOR_IOB_ENABLE |
189                                                 DINGO_ECOR_ETH_ENABLE);
190         ioport = bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid);
191         bus_space_write_1(bst, bsh, DINGO_EBAR0, ioport & 0xff);
192         bus_space_write_1(bst, bsh, DINGO_EBAR1, (ioport >> 8) & 0xff);
193
194         bus_space_write_1(bst, bsh, DINGO_DCOR0, DINGO_DCOR0_SF_INT);
195         bus_space_write_1(bst, bsh, DINGO_DCOR1, DINGO_DCOR1_INT_LEVEL |
196                                                  DINGO_DCOR1_EEDIO);
197         bus_space_write_1(bst, bsh, DINGO_DCOR2, 0x00);
198         bus_space_write_1(bst, bsh, DINGO_DCOR3, 0x00);
199         bus_space_write_1(bst, bsh, DINGO_DCOR4, 0x00);
200
201         bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
202
203         /* success! */
204         return 0;
205 }
206
207 static struct xe_vendor *
208 xe_vendor_lookup(uint32_t vendor_id)
209 {
210         struct xe_vendor *v;
211
212         for (v = xe_vendors; v->vendor_id != 0; ++v) {
213                 if(v->vendor_id == vendor_id)
214                         break;
215         }
216         return v;
217 }
218       
219 static struct xe_card_type *
220 xe_card_type_lookup(uint32_t prod)
221 {
222         struct xe_card_type *ct;
223
224         for (ct = xe_card_types; ct->card_type_desc != NULL; ++ct) {
225                 if(ct->prod_type == (prod & XE_PROD_UMASK))
226                         return ct;
227         }
228         return NULL;
229 }
230
231 /*
232  * PCMCIA probe routine.
233  * Identify the device.  Called from the bus driver when the card is
234  * inserted or otherwise powers up.
235  */
236 static int
237 xe_pccard_probe(device_t dev)
238 {
239         struct xe_softc *scp = device_get_softc(dev);
240         uint32_t vendor, product, prod;
241         uint16_t prodext;
242         uint8_t *ether_addr;
243         const char *cis3_str=NULL;
244         struct xe_vendor *vendor_itm;
245         struct xe_card_type *card_itm;
246         int i;
247
248         /*
249          * PCCARD_CISTPL_MANFID = 0x20
250          */
251         vendor = pccard_get_vendor(dev);
252         vendor_itm = xe_vendor_lookup(vendor);
253         /*
254          * We always have some vendor here, although
255          * vendor description may be "Unknown".
256          */
257         scp->vendor = vendor_itm->vendor_desc;
258
259         product = pccard_get_product(dev);
260         prodext = pccard_get_prodext(dev);
261
262         /*
263          * prod(new) =  rev, media, prod(old)
264          * prod(new) =  (don't care), (care 0x10 bit), (care 0x0f bit)
265          */
266         prod = (product << 8) | prodext;
267         card_itm = xe_card_type_lookup(prod);
268         if (card_itm == NULL)
269                 return ENODEV;
270
271         scp->card_type = card_itm->card_type_desc;
272         if (card_itm->prod_type & XE_PROD_MODEM_UMASK)
273                 scp->modem = 1;
274
275         for (i = 1; i != XE_CARD_TYPE_FLAGS_DINGO; i = i << 1) {
276                 switch(i & card_itm->flags) {
277                 case XE_CARD_TYPE_FLAGS_CE2:
278                         scp->ce2 = 1;
279                         break;
280                 case XE_CARD_TYPE_FLAGS_MOHAWK:
281                         scp->mohawk = 1;
282                         break;
283                 case XE_CARD_TYPE_FLAGS_DINGO:
284                         scp->dingo = 1;
285                         break;
286                 }
287         }
288
289         /*
290          * PCCARD_CISTPL_VERS_1 = 0x15
291          *
292          * Check for certain strange CE2's that look like CE's:
293          * match 3rd version string against "CE2"
294          */
295         cis3_str = pccard_get_cis3_str(dev);
296         if (strcmp(scp->card_type, "CE") == 0)
297                 if (strcmp(cis3_str, "CE2") ==0)
298                         scp->card_type = "CE2";
299
300         /*
301          * PCCARD_CISTPL_FUNCE = 0x22
302          */
303         ether_addr = pccard_get_ether(dev);
304         bcopy(ether_addr, scp->arpcom.ac_enaddr, ETHER_ADDR_LEN);
305
306         /* Reject unsupported cards */
307         if (strcmp(scp->card_type, "CE") == 0 ||
308             strcmp(scp->card_type, "CEM") == 0) {
309                 device_printf(dev, "Sorry, your %s card is not supported :(\n",
310                               scp->card_type);
311                 return ENODEV;
312         }
313
314         /* Success */
315         return 0;
316 }
317
318 static int
319 xe_pccard_attach(device_t dev)
320 {
321         struct xe_softc *scp = device_get_softc(dev);
322         int err;
323
324         if ((err = xe_activate(dev)) != 0)
325                 return err;
326          
327         /* Hack RealPorts into submission */
328         if (scp->dingo && xe_cem56fix(dev) < 0) {
329                 device_printf(dev, "Unable to fix your RealPort\n");
330                 xe_deactivate(dev);
331                 return ENODEV;
332         }
333         return xe_attach(dev);
334 }
335
336 static int
337 xe_pccard_match(device_t dev)
338 {
339         const struct pccard_product *pp;
340
341         if ((pp = pccard_product_lookup(dev, xe_pccard_products,
342              sizeof(xe_pccard_products[0]), NULL)) != NULL) {
343                 if (pp->pp_name != NULL)
344                         device_set_desc(dev, pp->pp_name);
345                 return 0;
346         }
347         return EIO;
348 }