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