1 /* $NetBSD: if_en_pci.c,v 1.1 1996/06/22 02:00:31 chuck Exp $ */
5 * Copyright (c) 1996 Charles D. Cranor and Washington University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/socket.h>
54 #include <machine/bus.h>
56 #include <machine/resource.h>
60 #include <pci/pcivar.h>
61 #include <pci/pcireg.h>
63 #include <dev/en/midwayreg.h>
64 #include <dev/en/midwayvar.h>
70 static int en_pci_probe(device_t);
71 static int en_pci_attach(device_t);
72 static int en_pci_detach(device_t);
73 static int en_pci_shutdown(device_t);
80 /* bus independent stuff */
81 struct en_softc esc; /* includes "device" structure */
83 /* freebsd newbus glue */
84 struct resource *res; /* resource descriptor for registers */
85 struct resource *irq; /* resource descriptor for interrupt */
86 void *ih; /* interrupt handle */
89 #if !defined(MIDWAY_ENIONLY)
90 static void eni_get_macaddr(device_t, struct en_pci_softc *);
92 #if !defined(MIDWAY_ADPONLY)
93 static void adp_get_macaddr(struct en_pci_softc *);
97 * local defines (PCI specific stuff)
101 * address of config base memory address register in PCI config space
102 * (this is card specific)
105 #define PCI_CBMA 0x10
108 * tonga (pci bridge). ENI cards only!
111 #define EN_TONGA 0x60 /* PCI config addr of tonga reg */
113 #define TONGA_SWAP_DMA 0x80 /* endian swap control */
114 #define TONGA_SWAP_BYTE 0x40
115 #define TONGA_SWAP_WORD 0x20
118 * adaptec pci bridge. ADP cards only!
121 #define ADP_PCIREG 0x050040 /* PCI control register */
123 #define ADP_PCIREG_RESET 0x1 /* reset card */
124 #define ADP_PCIREG_IENABLE 0x2 /* interrupt enable */
125 #define ADP_PCIREG_SWAP_WORD 0x4 /* swap byte on slave access */
126 #define ADP_PCIREG_SWAP_DMA 0x8 /* swap byte on DMA */
128 #define PCI_VENDOR_EFFICIENTNETS 0x111a /* Efficent Networks */
129 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PF 0x0000 /* ENI-155P ATM */
130 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PA 0x0002 /* ENI-155P ATM */
131 #define PCI_VENDOR_ADP 0x9004 /* adaptec */
132 #define PCI_PRODUCT_ADP_AIC5900 0x5900
133 #define PCI_PRODUCT_ADP_AIC5905 0x5905
134 #define PCI_VENDOR(x) ((x) & 0xFFFF)
135 #define PCI_CHIPID(x) (((x) >> 16) & 0xFFFF)
137 #if !defined(MIDWAY_ENIONLY)
139 static void adp_busreset(void *);
142 * bus specific reset function [ADP only!]
145 static void adp_busreset(v)
150 struct en_softc *sc = (struct en_softc *) v;
153 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, ADP_PCIREG_RESET);
154 DELAY(1000); /* let it reset */
155 dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
156 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
157 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA|ADP_PCIREG_IENABLE));
158 dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
159 if ((dummy & (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA)) !=
160 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA))
161 printf("adp_busreset: Adaptec ATM did NOT reset!\n");
165 /***********************************************************************/
172 en_pci_probe(device_t dev)
174 switch (pci_get_vendor(dev)) {
175 #if !defined(MIDWAY_ADPONLY)
176 case PCI_VENDOR_EFFICIENTNETS:
177 switch (pci_get_device(dev)) {
178 case PCI_PRODUCT_EFFICIENTNETS_ENI155PF:
179 case PCI_PRODUCT_EFFICIENTNETS_ENI155PA:
180 device_set_desc(dev, "Efficient Networks ENI-155p");
185 #if !defined(MIDWAY_ENIONLY)
187 switch (pci_get_device(dev)) {
188 case PCI_PRODUCT_ADP_AIC5900:
189 case PCI_PRODUCT_ADP_AIC5905:
190 device_set_desc(dev, "Adaptec 155 ATM");
200 en_pci_attach(device_t dev)
203 struct en_pci_softc *scp;
205 int rid, s, unit, error = 0;
207 sc = device_get_softc(dev);
208 scp = (struct en_pci_softc *)sc;
209 bzero(scp, sizeof(*scp)); /* zero */
214 * Enable bus mastering.
216 val = pci_read_config(dev, PCIR_COMMAND, 2);
217 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
218 pci_write_config(dev, PCIR_COMMAND, val, 2);
221 * Map control/status registers.
224 scp->res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
225 0, ~0, 1, RF_ACTIVE);
227 device_printf(dev, "could not map memory\n");
232 sc->en_memt = rman_get_bustag(scp->res);
233 sc->en_base = rman_get_bushandle(scp->res);
236 * Allocate our interrupt.
239 scp->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
240 RF_SHAREABLE | RF_ACTIVE);
241 if (scp->irq == NULL) {
242 device_printf(dev, "could not map interrupt\n");
243 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
248 error = bus_setup_intr(dev, scp->irq, INTR_TYPE_NET,
249 en_intr, sc, &scp->ih);
251 device_printf(dev, "could not setup irq\n");
252 bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
253 bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
256 sc->ipl = 1; /* XXX (required to enable interrupt on midway) */
258 unit = device_get_unit(dev);
259 snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname), "en%d", unit);
260 sc->enif.if_unit = unit;
261 sc->enif.if_name = "en";
263 /* figure out if we are an adaptec card or not */
264 sc->is_adaptec = (pci_get_vendor(dev) == PCI_VENDOR_ADP) ? 1 : 0;
269 #if !defined(MIDWAY_ENIONLY)
270 if (sc->is_adaptec) {
271 adp_get_macaddr(scp);
272 sc->en_busreset = adp_busreset;
277 #if !defined(MIDWAY_ADPONLY)
278 if (!sc->is_adaptec) {
279 eni_get_macaddr(dev, scp);
280 sc->en_busreset = NULL;
281 pci_write_config(dev, EN_TONGA, (TONGA_SWAP_DMA|TONGA_SWAP_WORD), 4);
286 * done PCI specific stuff
301 en_pci_detach(device_t dev)
303 struct en_softc *sc = device_get_softc(dev);
304 struct en_pci_softc *scp = (struct en_pci_softc *)sc;
310 * Close down routes etc.
312 if_detach(&sc->enif);
315 * Stop DMA and drop transmit queue.
320 * Deallocate resources.
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);
328 * Free all the driver internal resources
338 en_pci_shutdown(device_t dev)
340 struct en_pci_softc *psc = (struct en_pci_softc *)device_get_softc(dev);
343 DELAY(10); /* is this necessary? */
347 #if !defined(MIDWAY_ENIONLY)
350 #define bus_space_read_1(t, h, o) \
351 ((void)t, (*(volatile u_int8_t *)((h) + (o))))
356 struct en_pci_softc *scp;
358 struct en_softc * sc = (struct en_softc *)scp;
361 for (lcv = 0; lcv < sizeof(sc->macaddr); lcv++)
362 sc->macaddr[lcv] = bus_space_read_1(sc->en_memt, sc->en_base,
363 MID_ADPMACOFF + lcv);
366 #endif /* MIDWAY_ENIONLY */
368 #if !defined(MIDWAY_ADPONLY)
371 * Read station (MAC) address from serial EEPROM.
372 * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
374 #define EN_PROM_MAGIC 0x0c
375 #define EN_PROM_DATA 0x02
376 #define EN_PROM_CLK 0x01
380 eni_get_macaddr(device_t dev, struct en_pci_softc *scp)
382 struct en_softc * sc = (struct en_softc *)scp;
383 int i, j, address, status;
384 u_int32_t data, t_data;
387 t_data = pci_read_config(dev, EN_TONGA, 4) & 0xffffff00;
389 data = EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
390 pci_write_config(dev, EN_TONGA, data, 4);
392 for (i = 0; i < sizeof(sc->macaddr); i ++){
393 /* start operation */
394 data |= EN_PROM_DATA ;
395 pci_write_config(dev, EN_TONGA, data, 4);
396 data |= EN_PROM_CLK ;
397 pci_write_config(dev, EN_TONGA, data, 4);
398 data &= ~EN_PROM_DATA ;
399 pci_write_config(dev, EN_TONGA, data, 4);
400 data &= ~EN_PROM_CLK ;
401 pci_write_config(dev, EN_TONGA, data, 4);
402 /* send address with serial line */
403 address = ((i + EN_ESI) << 1) + 1;
404 for ( j = 7 ; j >= 0 ; j --){
405 data = (address >> j) & 1 ? data | EN_PROM_DATA :
406 data & ~EN_PROM_DATA;
407 pci_write_config(dev, EN_TONGA, data, 4);
408 data |= EN_PROM_CLK ;
409 pci_write_config(dev, EN_TONGA, data, 4);
410 data &= ~EN_PROM_CLK ;
411 pci_write_config(dev, EN_TONGA, data, 4);
414 data |= EN_PROM_DATA ;
415 pci_write_config(dev, EN_TONGA, data, 4);
416 data |= EN_PROM_CLK ;
417 pci_write_config(dev, EN_TONGA, data, 4);
418 data = pci_read_config(dev, EN_TONGA, 4);
419 status = data & EN_PROM_DATA;
420 data &= ~EN_PROM_CLK ;
421 pci_write_config(dev, EN_TONGA, data, 4);
422 data |= EN_PROM_DATA ;
423 pci_write_config(dev, EN_TONGA, data, 4);
427 for ( j = 7 ; j >= 0 ; j --){
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 if(data & EN_PROM_DATA) tmp |= 1;
435 data &= ~EN_PROM_CLK ;
436 pci_write_config(dev, EN_TONGA, data, 4);
437 data |= EN_PROM_DATA ;
438 pci_write_config(dev, EN_TONGA, data, 4);
441 data |= EN_PROM_DATA ;
442 pci_write_config(dev, EN_TONGA, data, 4);
443 data |= EN_PROM_CLK ;
444 pci_write_config(dev, EN_TONGA, data, 4);
445 data = pci_read_config(dev, EN_TONGA, 4);
446 status = data & EN_PROM_DATA;
447 data &= ~EN_PROM_CLK ;
448 pci_write_config(dev, EN_TONGA, data, 4);
449 data |= EN_PROM_DATA ;
450 pci_write_config(dev, EN_TONGA, data, 4);
452 sc->macaddr[i] = tmp;
455 data &= ~EN_PROM_DATA;
456 pci_write_config(dev, EN_TONGA, data, 4);
458 pci_write_config(dev, EN_TONGA, data, 4);
459 data |= EN_PROM_DATA;
460 pci_write_config(dev, EN_TONGA, data, 4);
461 pci_write_config(dev, EN_TONGA, t_data, 4);
464 #endif /* !MIDWAY_ADPONLY */
466 static device_method_t en_methods[] = {
467 /* Device interface */
468 DEVMETHOD(device_probe, en_pci_probe),
469 DEVMETHOD(device_attach, en_pci_attach),
470 DEVMETHOD(device_detach, en_pci_detach),
471 DEVMETHOD(device_shutdown, en_pci_shutdown),
476 static driver_t en_driver = {
479 sizeof(struct en_pci_softc),
482 static devclass_t en_devclass;
484 DRIVER_MODULE(if_en, pci, en_driver, en_devclass, 0, 0);