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