1 /* $NetBSD: if_en_pci.c,v 1.1 1996/06/22 02:00:31 chuck Exp $ */
3 * Copyright (c) 1996 Charles D. Cranor and Washington University.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * i f _ e n _ p c i . c
40 * author: Chuck Cranor <chuck@ccrc.wustl.edu>
41 * started: spring, 1996.
43 * FreeBSD PCI glue for the eni155p card.
44 * thanks to Matt Thomas for figuring out FreeBSD vs NetBSD vs etc.. diffs.
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
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>
59 #include <machine/bus.h>
61 #include <machine/resource.h>
66 #include <net/if_atm.h>
67 #include <net/if_media.h>
68 #include <net/if_types.h>
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pcireg.h>
73 #include <dev/utopia/utopia.h>
74 #include <dev/en/midwayreg.h>
75 #include <dev/en/midwayvar.h>
77 MODULE_DEPEND(en, pci, 1, 1, 1);
78 MODULE_DEPEND(en, atm, 1, 1, 1);
79 MODULE_DEPEND(en, utopia, 1, 1, 1);
85 /* bus independent stuff */
86 struct en_softc esc; /* includes "device" structure */
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 */
94 static void eni_get_macaddr(device_t, struct en_pci_softc *);
95 static void adp_get_macaddr(struct en_pci_softc *);
98 * address of config base memory address register in PCI config space
99 * (this is card specific)
101 #define PCI_CBMA PCIR_BAR(0)
104 * tonga (pci bridge). ENI cards only!
106 #define EN_TONGA 0x60 /* PCI config addr of tonga reg */
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
117 * adaptec pci bridge. ADP cards only!
119 #define ADP_PCIREG 0x050040 /* PCI control register */
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 */
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)
136 * bus specific reset function [ADP only!]
139 adp_busreset(void *v)
141 struct en_softc *sc = (struct en_softc *)v;
144 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
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)) !=
153 device_printf(sc->dev, "%s: Adaptec ATM did NOT reset!\n",
157 /***********************************************************************/
163 en_pci_probe(device_t dev)
166 switch (pci_get_vendor(dev)) {
168 case PCI_VENDOR_EFFICIENTNETS:
169 switch (pci_get_device(dev)) {
171 case PCI_PRODUCT_EFFICIENTNETS_ENI155PF:
172 case PCI_PRODUCT_EFFICIENTNETS_ENI155PA:
173 device_set_desc(dev, "Efficient Networks ENI-155p");
174 return (BUS_PROBE_DEFAULT);
179 switch (pci_get_device(dev)) {
181 case PCI_PRODUCT_ADP_AIC5900:
182 case PCI_PRODUCT_ADP_AIC5905:
183 device_set_desc(dev, "Adaptec 155 ATM");
184 return (BUS_PROBE_DEFAULT);
192 en_pci_attach(device_t dev)
195 struct en_pci_softc *scp;
198 sc = device_get_softc(dev);
199 scp = (struct en_pci_softc *)sc;
201 sc->ifp = if_alloc(IFT_ATM);
202 if (sc->ifp == NULL) {
203 device_printf(dev, "can not if_alloc()\n");
208 if_initname(sc->ifp, device_get_name(dev),
209 device_get_unit(dev));
212 * Enable bus mastering.
214 pci_enable_busmaster(dev);
217 * Map control/status registers.
220 scp->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
222 if (scp->res == NULL) {
223 device_printf(dev, "could not map memory\n");
229 sc->en_memt = rman_get_bustag(scp->res);
230 sc->en_base = rman_get_bushandle(scp->res);
233 * Allocate our interrupt.
236 scp->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
237 RF_SHAREABLE | RF_ACTIVE);
238 if (scp->irq == NULL) {
239 device_printf(dev, "could not map interrupt\n");
240 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
246 sc->ipl = 1; /* XXX (required to enable interrupt on midway) */
248 /* figure out if we are an adaptec card or not */
249 sc->is_adaptec = (pci_get_vendor(dev) == PCI_VENDOR_ADP) ? 1 : 0;
254 if (sc->is_adaptec) {
255 adp_get_macaddr(scp);
256 sc->en_busreset = adp_busreset;
259 eni_get_macaddr(dev, scp);
260 sc->en_busreset = NULL;
261 pci_write_config(dev, EN_TONGA, TONGA_SWAP_DMA | TONGA_READ_IVAN, 4);
265 * Common attach stuff
267 if ((error = en_attach(sc)) != 0) {
268 device_printf(dev, "attach failed\n");
269 bus_teardown_intr(dev, scp->irq, scp->ih);
270 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
271 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
277 * Do the interrupt SETUP last just before returning
279 error = bus_setup_intr(dev, scp->irq, INTR_TYPE_NET,
280 NULL, en_intr, sc, &scp->ih);
283 atm_ifdetach(sc->ifp);
284 device_printf(dev, "could not setup irq\n");
285 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
286 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
302 en_pci_detach(device_t dev)
304 struct en_softc *sc = device_get_softc(dev);
305 struct en_pci_softc *scp = (struct en_pci_softc *)sc;
308 * Stop DMA and drop transmit queue.
310 if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
311 device_printf(sc->dev, "still running\n");
312 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
316 * Close down routes etc.
319 atm_ifdetach(sc->ifp);
322 * Deallocate resources.
324 bus_teardown_intr(dev, scp->irq, scp->ih);
325 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
326 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
329 * Free all the driver internal resources
338 en_pci_shutdown(device_t dev)
340 struct en_pci_softc *psc = device_get_softc(dev);
343 DELAY(10); /* is this necessary? */
349 * Get the MAC address from an Adaptec board. No idea how to get
350 * serial number or other stuff, because I have no documentation for that
354 adp_get_macaddr(struct en_pci_softc *scp)
356 struct en_softc * sc = (struct en_softc *)scp;
359 for (lcv = 0; lcv < sizeof(IFP2IFATM(sc->ifp)->mib.esi); lcv++)
360 IFP2IFATM(sc->ifp)->mib.esi[lcv] = bus_space_read_1(sc->en_memt,
361 sc->en_base, MID_ADPMACOFF + lcv);
365 * Read station (MAC) address from serial EEPROM.
366 * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
368 #define EN_PROM_MAGIC 0x0c
369 #define EN_PROM_DATA 0x02
370 #define EN_PROM_CLK 0x01
372 #define EN_SERIAL 112
375 * Read a byte from the given address in the EEPROM
378 eni_get_byte(device_t dev, uint32_t *data, u_int address)
383 address = (address << 1) + 1;
385 /* start operation */
386 *data |= EN_PROM_DATA ;
387 pci_write_config(dev, EN_TONGA, *data, 4);
388 *data |= EN_PROM_CLK ;
389 pci_write_config(dev, EN_TONGA, *data, 4);
390 *data &= ~EN_PROM_DATA ;
391 pci_write_config(dev, EN_TONGA, *data, 4);
392 *data &= ~EN_PROM_CLK ;
393 pci_write_config(dev, EN_TONGA, *data, 4);
394 /* send address with serial line */
395 for ( j = 7 ; j >= 0 ; j --) {
396 *data = ((address >> j) & 1) ? (*data | EN_PROM_DATA) :
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 &= ~EN_PROM_CLK ;
402 pci_write_config(dev, EN_TONGA, *data, 4);
405 *data |= EN_PROM_DATA ;
406 pci_write_config(dev, EN_TONGA, *data, 4);
407 *data |= EN_PROM_CLK ;
408 pci_write_config(dev, EN_TONGA, *data, 4);
409 *data = pci_read_config(dev, EN_TONGA, 4);
410 *data &= ~EN_PROM_CLK ;
411 pci_write_config(dev, EN_TONGA, *data, 4);
412 *data |= EN_PROM_DATA ;
413 pci_write_config(dev, EN_TONGA, *data, 4);
417 for ( j = 7 ; j >= 0 ; j --) {
419 *data |= EN_PROM_DATA ;
420 pci_write_config(dev, EN_TONGA, *data, 4);
421 *data |= EN_PROM_CLK ;
422 pci_write_config(dev, EN_TONGA, *data, 4);
423 *data = pci_read_config(dev, EN_TONGA, 4);
424 if(*data & EN_PROM_DATA) tmp |= 1;
425 *data &= ~EN_PROM_CLK ;
426 pci_write_config(dev, EN_TONGA, *data, 4);
427 *data |= EN_PROM_DATA ;
428 pci_write_config(dev, EN_TONGA, *data, 4);
431 *data |= EN_PROM_DATA ;
432 pci_write_config(dev, EN_TONGA, *data, 4);
433 *data |= EN_PROM_CLK ;
434 pci_write_config(dev, EN_TONGA, *data, 4);
435 *data = pci_read_config(dev, EN_TONGA, 4);
436 *data &= ~EN_PROM_CLK ;
437 pci_write_config(dev, EN_TONGA, *data, 4);
438 *data |= EN_PROM_DATA ;
439 pci_write_config(dev, EN_TONGA, *data, 4);
445 * Get MAC address and other stuff from the EEPROM
448 eni_get_macaddr(device_t dev, struct en_pci_softc *scp)
450 struct en_softc * sc = (struct en_softc *)scp;
452 uint32_t data, t_data;
454 t_data = pci_read_config(dev, EN_TONGA, 4) & 0xffffff00;
456 data = EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
457 pci_write_config(dev, EN_TONGA, data, 4);
459 for (i = 0; i < sizeof(IFP2IFATM(sc->ifp)->mib.esi); i ++)
460 IFP2IFATM(sc->ifp)->mib.esi[i] = eni_get_byte(dev, &data, i + EN_ESI);
462 IFP2IFATM(sc->ifp)->mib.serial = 0;
463 for (i = 0; i < 4; i++) {
464 IFP2IFATM(sc->ifp)->mib.serial <<= 8;
465 IFP2IFATM(sc->ifp)->mib.serial |= eni_get_byte(dev, &data, i + EN_SERIAL);
468 data &= ~EN_PROM_DATA;
469 pci_write_config(dev, EN_TONGA, data, 4);
471 pci_write_config(dev, EN_TONGA, data, 4);
472 data |= EN_PROM_DATA;
473 pci_write_config(dev, EN_TONGA, data, 4);
474 pci_write_config(dev, EN_TONGA, t_data, 4);
478 * Driver infrastructure
480 static device_method_t en_methods[] = {
481 /* Device interface */
482 DEVMETHOD(device_probe, en_pci_probe),
483 DEVMETHOD(device_attach, en_pci_attach),
484 DEVMETHOD(device_detach, en_pci_detach),
485 DEVMETHOD(device_shutdown, en_pci_shutdown),
490 static driver_t en_driver = {
493 sizeof(struct en_pci_softc),
496 static devclass_t en_devclass;
498 DRIVER_MODULE(en, pci, en_driver, en_devclass, en_modevent, 0);