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