]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/en/if_en_pci.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / en / if_en_pci.c
1 /*      $NetBSD: if_en_pci.c,v 1.1 1996/06/22 02:00:31 chuck Exp $      */
2 /*-
3  * Copyright (c) 1996 Charles D. Cranor and Washington University.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Charles D. Cranor and
17  *      Washington University.
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.
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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
38  * i f _ e n _ p c i . c  
39  *
40  * author: Chuck Cranor <chuck@ccrc.wustl.edu>
41  * started: spring, 1996.
42  *
43  * FreeBSD PCI glue for the eni155p card.
44  * thanks to Matt Thomas for figuring out FreeBSD vs NetBSD vs etc.. diffs.
45  */
46
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/sysctl.h>
53 #include <sys/condvar.h>
54
55 #include <sys/bus.h>
56 #include <machine/bus.h>
57 #include <sys/rman.h>
58 #include <machine/resource.h>
59
60 #include <vm/uma.h>
61
62 #include <net/if.h>
63 #include <net/if_atm.h>
64 #include <net/if_media.h>
65 #include <net/if_types.h>
66
67 #include <dev/pci/pcivar.h>
68 #include <dev/pci/pcireg.h>
69
70 #include <dev/utopia/utopia.h>
71 #include <dev/en/midwayreg.h>
72 #include <dev/en/midwayvar.h>
73
74 MODULE_DEPEND(en, pci, 1, 1, 1);
75 MODULE_DEPEND(en, atm, 1, 1, 1);
76 MODULE_DEPEND(en, utopia, 1, 1, 1);
77
78 /*
79  * local structures
80  */
81 struct en_pci_softc {
82         /* bus independent stuff */
83         struct en_softc esc;    /* includes "device" structure */
84
85         /* freebsd newbus glue */
86         struct resource *res;   /* resource descriptor for registers */
87         struct resource *irq;   /* resource descriptor for interrupt */
88         void *ih;               /* interrupt handle */
89 };
90
91 static  void eni_get_macaddr(device_t, struct en_pci_softc *);
92 static  void adp_get_macaddr(struct en_pci_softc *);
93
94 /* 
95  * address of config base memory address register in PCI config space
96  * (this is card specific)
97  */
98 #define PCI_CBMA        PCIR_BAR(0)
99
100 /*
101  * tonga (pci bridge).   ENI cards only!
102  */
103 #define EN_TONGA        0x60            /* PCI config addr of tonga reg */
104
105 #define TONGA_SWAP_DMA  0x80            /* endian swap control */
106 #define TONGA_SWAP_BYTE 0x40
107 #define TONGA_SWAP_WORD 0x20
108 #define TONGA_READ_MULT 0x00
109 #define TONGA_READ_MEM  0x04
110 #define TONGA_READ_IVAN 0x08
111 #define TONGA_READ_KEN  0x0C
112
113 /*
114  * adaptec pci bridge.   ADP cards only!
115  */
116 #define ADP_PCIREG      0x050040        /* PCI control register */
117
118 #define ADP_PCIREG_RESET        0x1     /* reset card */
119 #define ADP_PCIREG_IENABLE      0x2     /* interrupt enable */
120 #define ADP_PCIREG_SWAP_WORD    0x4     /* swap byte on slave access */
121 #define ADP_PCIREG_SWAP_DMA     0x8     /* swap byte on DMA */
122
123 #define PCI_VENDOR_EFFICIENTNETS 0x111a                 /* Efficent Networks */
124 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PF 0x0000       /* ENI-155P ATM */
125 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PA 0x0002       /* ENI-155P ATM */
126 #define PCI_VENDOR_ADP 0x9004                           /* adaptec */
127 #define PCI_PRODUCT_ADP_AIC5900 0x5900
128 #define PCI_PRODUCT_ADP_AIC5905 0x5905
129 #define PCI_VENDOR(x)           ((x) & 0xFFFF)
130 #define PCI_CHIPID(x)           (((x) >> 16) & 0xFFFF)
131
132 /*
133  * bus specific reset function [ADP only!]
134  */
135 static void
136 adp_busreset(void *v)
137 {
138         struct en_softc *sc = (struct en_softc *)v;
139         uint32_t dummy;
140
141         bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
142             ADP_PCIREG_RESET);
143         DELAY(1000);                    /* let it reset */
144         dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
145         bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, 
146             (ADP_PCIREG_SWAP_DMA | ADP_PCIREG_IENABLE));
147         dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
148         if ((dummy & (ADP_PCIREG_SWAP_WORD | ADP_PCIREG_SWAP_DMA)) !=
149             ADP_PCIREG_SWAP_DMA)
150                 device_printf(sc->dev, "%s: Adaptec ATM did NOT reset!\n",
151                     __func__);
152 }
153
154 /***********************************************************************/
155
156 /*
157  * autoconfig stuff
158  */
159 static int
160 en_pci_probe(device_t dev)
161 {
162
163         switch (pci_get_vendor(dev)) {
164
165           case PCI_VENDOR_EFFICIENTNETS:
166                 switch (pci_get_device(dev)) {
167
168                     case PCI_PRODUCT_EFFICIENTNETS_ENI155PF:
169                     case PCI_PRODUCT_EFFICIENTNETS_ENI155PA:
170                         device_set_desc(dev, "Efficient Networks ENI-155p");
171                         return (BUS_PROBE_DEFAULT);
172                 }
173                 break;
174
175           case PCI_VENDOR_ADP:
176                 switch (pci_get_device(dev)) {
177
178                   case PCI_PRODUCT_ADP_AIC5900:
179                   case PCI_PRODUCT_ADP_AIC5905:
180                         device_set_desc(dev, "Adaptec 155 ATM");
181                         return (BUS_PROBE_DEFAULT);
182                 }
183                 break;
184         }
185         return (ENXIO);
186 }
187
188 static int
189 en_pci_attach(device_t dev)
190 {
191         struct en_softc *sc;
192         struct en_pci_softc *scp;
193         int rid, error = 0;
194
195         sc = device_get_softc(dev);
196         scp = (struct en_pci_softc *)sc;
197         sc->dev = dev;  
198         sc->ifp = if_alloc(IFT_ATM);
199         if (sc->ifp == NULL) {
200                 device_printf(dev, "can not if_alloc()\n");
201                 error = ENOSPC;
202                 goto fail;
203         }
204
205         if_initname(sc->ifp, device_get_name(dev),
206             device_get_unit(dev));
207
208         /*
209          * Enable bus mastering.
210          */
211         pci_enable_busmaster(dev);
212
213         /*
214          * Map control/status registers.
215          */
216         rid = PCI_CBMA;
217         scp->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
218             RF_ACTIVE);
219         if (scp->res == NULL) {
220                 device_printf(dev, "could not map memory\n");
221                 if_free(sc->ifp);
222                 error = ENXIO;
223                 goto fail;
224         }
225
226         sc->en_memt = rman_get_bustag(scp->res);
227         sc->en_base = rman_get_bushandle(scp->res);
228
229         /*
230          * Allocate our interrupt.
231          */
232         rid = 0;
233         scp->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
234             RF_SHAREABLE | RF_ACTIVE);
235         if (scp->irq == NULL) {
236                 device_printf(dev, "could not map interrupt\n");
237                 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
238                 if_free(sc->ifp);
239                 error = ENXIO;
240                 goto fail;
241         }
242
243         sc->ipl = 1; /* XXX (required to enable interrupt on midway) */
244
245         /* figure out if we are an adaptec card or not */
246         sc->is_adaptec = (pci_get_vendor(dev) == PCI_VENDOR_ADP) ? 1 : 0;
247
248         /*
249          * set up pci bridge
250          */
251         if (sc->is_adaptec) {
252                 adp_get_macaddr(scp);
253                 sc->en_busreset = adp_busreset;
254                 adp_busreset(sc);
255         } else {
256                 eni_get_macaddr(dev, scp);
257                 sc->en_busreset = NULL;
258                 pci_write_config(dev, EN_TONGA, TONGA_SWAP_DMA | TONGA_READ_IVAN, 4);
259         }
260
261         /*
262          * Common attach stuff
263          */
264         if ((error = en_attach(sc)) != 0) {
265                 device_printf(dev, "attach failed\n");
266                 bus_teardown_intr(dev, scp->irq, scp->ih);
267                 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
268                 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
269                 if_free(sc->ifp);
270                 goto fail;
271         }
272
273         /*
274          * Do the interrupt SETUP last just before returning
275          */
276         error = bus_setup_intr(dev, scp->irq, INTR_TYPE_NET,
277             NULL, en_intr, sc, &scp->ih);
278         if (error) {
279                 en_reset(sc);
280                 atm_ifdetach(sc->ifp);
281                 device_printf(dev, "could not setup irq\n");
282                 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
283                 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
284                 en_destroy(sc);
285                 if_free(sc->ifp);
286                 goto fail;
287         }
288
289         return (0);
290
291     fail:
292         return (error);
293 }
294
295 /*
296  * Detach the adapter
297  */
298 static int
299 en_pci_detach(device_t dev)
300 {
301         struct en_softc *sc = device_get_softc(dev);
302         struct en_pci_softc *scp = (struct en_pci_softc *)sc;
303
304         /*
305          * Stop DMA and drop transmit queue.
306          */
307         if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
308                 device_printf(sc->dev, "still running\n");
309                 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
310         }
311
312         /*
313          * Close down routes etc.
314          */
315         en_reset(sc);
316         atm_ifdetach(sc->ifp);
317
318         /*
319          * Deallocate resources.
320          */
321         bus_teardown_intr(dev, scp->irq, scp->ih);
322         bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
323         bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
324
325         /*
326          * Free all the driver internal resources
327          */
328         en_destroy(sc);
329         if_free(sc->ifp);
330
331         return (0);
332 }
333
334 static int
335 en_pci_shutdown(device_t dev)
336 {
337         struct en_pci_softc *psc = device_get_softc(dev);
338
339         en_reset(&psc->esc);
340         DELAY(10);              /* is this necessary? */
341
342         return (0);
343 }
344
345 /*
346  * Get the MAC address from an Adaptec board. No idea how to get
347  * serial number or other stuff, because I have no documentation for that
348  * card.
349  */
350 static void 
351 adp_get_macaddr(struct en_pci_softc *scp)
352 {
353         struct en_softc * sc = (struct en_softc *)scp;
354         int lcv;
355
356         for (lcv = 0; lcv < sizeof(IFP2IFATM(sc->ifp)->mib.esi); lcv++)
357                 IFP2IFATM(sc->ifp)->mib.esi[lcv] = bus_space_read_1(sc->en_memt,
358                     sc->en_base, MID_ADPMACOFF + lcv);
359 }
360
361 /*
362  * Read station (MAC) address from serial EEPROM.
363  * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
364  */
365 #define EN_PROM_MAGIC   0x0c
366 #define EN_PROM_DATA    0x02
367 #define EN_PROM_CLK     0x01
368 #define EN_ESI          64
369 #define EN_SERIAL       112
370
371 /*
372  * Read a byte from the given address in the EEPROM
373  */
374 static uint8_t
375 eni_get_byte(device_t dev, uint32_t *data, u_int address)
376 {
377         int j;
378         uint8_t tmp;
379
380         address = (address << 1) + 1;
381
382         /* start operation */
383         *data |= EN_PROM_DATA ;
384         pci_write_config(dev, EN_TONGA, *data, 4);
385         *data |= EN_PROM_CLK ;
386         pci_write_config(dev, EN_TONGA, *data, 4);
387         *data &= ~EN_PROM_DATA ;
388         pci_write_config(dev, EN_TONGA, *data, 4);
389         *data &= ~EN_PROM_CLK ;
390         pci_write_config(dev, EN_TONGA, *data, 4);
391         /* send address with serial line */
392         for ( j = 7 ; j >= 0 ; j --) {
393                 *data = ((address >> j) & 1) ? (*data | EN_PROM_DATA) :
394                     (*data & ~EN_PROM_DATA);
395                 pci_write_config(dev, EN_TONGA, *data, 4);
396                 *data |= EN_PROM_CLK ;
397                 pci_write_config(dev, EN_TONGA, *data, 4);
398                 *data &= ~EN_PROM_CLK ;
399                 pci_write_config(dev, EN_TONGA, *data, 4);
400         }
401         /* get ack */
402         *data |= EN_PROM_DATA ;
403         pci_write_config(dev, EN_TONGA, *data, 4);
404         *data |= EN_PROM_CLK ;
405         pci_write_config(dev, EN_TONGA, *data, 4);
406         *data = pci_read_config(dev, EN_TONGA, 4);
407         *data &= ~EN_PROM_CLK ;
408         pci_write_config(dev, EN_TONGA, *data, 4);
409         *data |= EN_PROM_DATA ;
410         pci_write_config(dev, EN_TONGA, *data, 4);
411
412         tmp = 0;
413
414         for ( j = 7 ; j >= 0 ; j --) {
415                 tmp <<= 1;
416                 *data |= EN_PROM_DATA ;
417                 pci_write_config(dev, EN_TONGA, *data, 4);
418                 *data |= EN_PROM_CLK ;
419                 pci_write_config(dev, EN_TONGA, *data, 4);
420                 *data = pci_read_config(dev, EN_TONGA, 4);
421                 if(*data & EN_PROM_DATA) tmp |= 1;
422                 *data &= ~EN_PROM_CLK ;
423                 pci_write_config(dev, EN_TONGA, *data, 4);
424                 *data |= EN_PROM_DATA ;
425                 pci_write_config(dev, EN_TONGA, *data, 4);
426         }
427         /* get ack */
428         *data |= EN_PROM_DATA ;
429         pci_write_config(dev, EN_TONGA, *data, 4);
430         *data |= EN_PROM_CLK ;
431         pci_write_config(dev, EN_TONGA, *data, 4);
432         *data = pci_read_config(dev, EN_TONGA, 4);
433         *data &= ~EN_PROM_CLK ;
434         pci_write_config(dev, EN_TONGA, *data, 4);
435         *data |= EN_PROM_DATA ;
436         pci_write_config(dev, EN_TONGA, *data, 4);
437
438         return (tmp);
439 }
440
441 /*
442  * Get MAC address and other stuff from the EEPROM
443  */
444 static void 
445 eni_get_macaddr(device_t dev, struct en_pci_softc *scp)
446 {
447         struct en_softc * sc = (struct en_softc *)scp;
448         int i;
449         uint32_t data, t_data;
450
451         t_data = pci_read_config(dev, EN_TONGA, 4) & 0xffffff00;
452
453         data =  EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
454         pci_write_config(dev, EN_TONGA, data, 4);
455
456         for (i = 0; i < sizeof(IFP2IFATM(sc->ifp)->mib.esi); i ++)
457                 IFP2IFATM(sc->ifp)->mib.esi[i] = eni_get_byte(dev, &data, i + EN_ESI);
458
459         IFP2IFATM(sc->ifp)->mib.serial = 0;
460         for (i = 0; i < 4; i++) {
461                 IFP2IFATM(sc->ifp)->mib.serial <<= 8;
462                 IFP2IFATM(sc->ifp)->mib.serial |= eni_get_byte(dev, &data, i + EN_SERIAL);
463         }
464         /* stop operation */
465         data &=  ~EN_PROM_DATA;
466         pci_write_config(dev, EN_TONGA, data, 4);
467         data |=  EN_PROM_CLK;
468         pci_write_config(dev, EN_TONGA, data, 4);
469         data |=  EN_PROM_DATA;
470         pci_write_config(dev, EN_TONGA, data, 4);
471         pci_write_config(dev, EN_TONGA, t_data, 4);
472 }
473
474 /*
475  * Driver infrastructure
476  */
477 static device_method_t en_methods[] = {
478         /* Device interface */
479         DEVMETHOD(device_probe,         en_pci_probe),
480         DEVMETHOD(device_attach,        en_pci_attach),
481         DEVMETHOD(device_detach,        en_pci_detach),
482         DEVMETHOD(device_shutdown,      en_pci_shutdown),
483
484         { 0, 0 }
485 };
486
487 static driver_t en_driver = {
488         "en",
489         en_methods,
490         sizeof(struct en_pci_softc),
491 };
492
493 static devclass_t en_devclass;
494
495 DRIVER_MODULE(en, pci, en_driver, en_devclass, en_modevent, 0);