1 /* $NetBSD: if_en_pci.c,v 1.1 1996/06/22 02:00:31 chuck Exp $ */
4 * Copyright (c) 1996 Charles D. Cranor and Washington University.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * i f _ e n _ p c i . c
41 * author: Chuck Cranor <chuck@ccrc.wustl.edu>
42 * started: spring, 1996.
44 * FreeBSD PCI glue for the eni155p card.
45 * thanks to Matt Thomas for figuring out FreeBSD vs NetBSD vs etc.. diffs.
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
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>
60 #include <machine/bus.h>
62 #include <machine/resource.h>
67 #include <net/if_atm.h>
68 #include <net/if_media.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 0x10
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 if_printf(&sc->ifatm.ifnet, "adp_busreset: Adaptec ATM did "
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");
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");
192 en_pci_attach(device_t dev)
195 struct en_pci_softc *scp;
199 sc = device_get_softc(dev);
200 scp = (struct en_pci_softc *)sc;
202 if_initname(&(sc->ifatm.ifnet), device_get_name(dev),
203 device_get_unit(dev));
206 * Enable bus mastering.
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);
213 * Map control/status registers.
216 scp->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
218 if (scp->res == NULL) {
219 device_printf(dev, "could not map memory\n");
225 sc->en_memt = rman_get_bustag(scp->res);
226 sc->en_base = rman_get_bushandle(scp->res);
229 * Allocate our interrupt.
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);
241 sc->ipl = 1; /* XXX (required to enable interrupt on midway) */
243 /* figure out if we are an adaptec card or not */
244 sc->is_adaptec = (pci_get_vendor(dev) == PCI_VENDOR_ADP) ? 1 : 0;
249 if (sc->is_adaptec) {
250 adp_get_macaddr(scp);
251 sc->en_busreset = adp_busreset;
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);
260 * Common attach stuff
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);
271 * Do the interrupt SETUP last just before returning
273 error = bus_setup_intr(dev, scp->irq, INTR_TYPE_NET,
274 en_intr, sc, &scp->ih);
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);
295 en_pci_detach(device_t dev)
297 struct en_softc *sc = device_get_softc(dev);
298 struct en_pci_softc *scp = (struct en_pci_softc *)sc;
301 * Stop DMA and drop transmit queue.
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;
309 * Close down routes etc.
312 atm_ifdetach(&sc->ifatm.ifnet);
315 * Deallocate resources.
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);
322 * Free all the driver internal resources
330 en_pci_shutdown(device_t dev)
332 struct en_pci_softc *psc = device_get_softc(dev);
335 DELAY(10); /* is this necessary? */
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
346 adp_get_macaddr(struct en_pci_softc *scp)
348 struct en_softc * sc = (struct en_softc *)scp;
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);
357 * Read station (MAC) address from serial EEPROM.
358 * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
360 #define EN_PROM_MAGIC 0x0c
361 #define EN_PROM_DATA 0x02
362 #define EN_PROM_CLK 0x01
364 #define EN_SERIAL 112
367 * Read a byte from the given address in the EEPROM
370 eni_get_byte(device_t dev, uint32_t *data, u_int address)
375 address = (address << 1) + 1;
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);
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);
409 for ( j = 7 ; j >= 0 ; j --) {
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);
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);
437 * Get MAC address and other stuff from the EEPROM
440 eni_get_macaddr(device_t dev, struct en_pci_softc *scp)
442 struct en_softc * sc = (struct en_softc *)scp;
444 uint32_t data, t_data;
446 t_data = pci_read_config(dev, EN_TONGA, 4) & 0xffffff00;
448 data = EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
449 pci_write_config(dev, EN_TONGA, data, 4);
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);
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);
460 data &= ~EN_PROM_DATA;
461 pci_write_config(dev, EN_TONGA, data, 4);
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);
470 * Driver infrastructure
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),
482 static driver_t en_driver = {
485 sizeof(struct en_pci_softc),
488 static devclass_t en_devclass;
490 DRIVER_MODULE(en, pci, en_driver, en_devclass, en_modevent, 0);