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.
34 * $FreeBSD: src/sys/pci/if_en_pci.c,v 1.12 1999/08/21 22:10:49 msmith Exp $
35 * $DragonFly: src/sys/dev/netif/en_pci/if_en_pci.c,v 1.4 2003/08/27 09:38:30 rob Exp $
40 * i f _ e n _ p c i . c
42 * author: Chuck Cranor <chuck@ccrc.wustl.edu>
43 * started: spring, 1996.
45 * FreeBSD PCI glue for the eni155p card.
46 * thanks to Matt Thomas for figuring out FreeBSD vs NetBSD vs etc.. diffs.
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #if defined(__FreeBSD__)
55 #include <sys/eventhandler.h>
57 #include <sys/malloc.h>
58 #include <sys/socket.h>
60 #include <machine/clock.h> /* for DELAY */
64 #include <bus/pci/pcivar.h>
65 #include <bus/pci/pcireg.h>
67 #include <dev/atm/en/midwayreg.h>
68 #include <dev/atm/en/midwayvar.h>
75 static void en_pci_attach (pcici_t, int);
76 static const char *en_pci_probe (pcici_t, pcidi_t);
77 static void en_pci_shutdown (void *, int);
84 /* bus independent stuff */
85 struct en_softc esc; /* includes "device" structure */
88 void *sc_ih; /* interrupt handle */
89 pci_chipset_tag_t en_pc; /* for PCI calls */
90 pcici_t en_confid; /* config id */
93 #if !defined(MIDWAY_ENIONLY)
94 static void eni_get_macaddr (struct en_pci_softc *);
96 #if !defined(MIDWAY_ADPONLY)
97 static void adp_get_macaddr (struct en_pci_softc *);
101 * pointers to softcs (we alloc)
104 static struct en_pci_softc *enpcis[NEN] = {0};
105 extern struct cfdriver en_cd;
108 * autoconfig structures
111 static u_long en_pci_count;
113 static struct pci_device endevice = {
121 COMPAT_PCI_DRIVER (en, endevice);
124 * local defines (PCI specific stuff)
128 * address of config base memory address register in PCI config space
129 * (this is card specific)
132 #define PCI_CBMA 0x10
135 * tonga (pci bridge). ENI cards only!
138 #define EN_TONGA 0x60 /* PCI config addr of tonga reg */
140 #define TONGA_SWAP_DMA 0x80 /* endian swap control */
141 #define TONGA_SWAP_BYTE 0x40
142 #define TONGA_SWAP_WORD 0x20
145 * adaptec pci bridge. ADP cards only!
148 #define ADP_PCIREG 0x050040 /* PCI control register */
150 #define ADP_PCIREG_RESET 0x1 /* reset card */
151 #define ADP_PCIREG_IENABLE 0x2 /* interrupt enable */
152 #define ADP_PCIREG_SWAP_WORD 0x4 /* swap byte on slave access */
153 #define ADP_PCIREG_SWAP_DMA 0x8 /* swap byte on DMA */
155 #define PCI_VENDOR_EFFICIENTNETS 0x111a /* Efficent Networks */
156 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PF 0x0000 /* ENI-155P ATM */
157 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PA 0x0002 /* ENI-155P ATM */
158 #define PCI_VENDOR_ADP 0x9004 /* adaptec */
159 #define PCI_PRODUCT_ADP_AIC5900 0x5900
160 #define PCI_PRODUCT_ADP_AIC5905 0x5905
161 #define PCI_VENDOR(x) ((x) & 0xFFFF)
162 #define PCI_CHIPID(x) (((x) >> 16) & 0xFFFF)
164 #if !defined(MIDWAY_ENIONLY)
166 static void adp_busreset (void *);
169 * bus specific reset function [ADP only!]
172 static void adp_busreset(v)
177 struct en_softc *sc = (struct en_softc *) v;
180 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, ADP_PCIREG_RESET);
181 DELAY(1000); /* let it reset */
182 dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
183 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
184 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA|ADP_PCIREG_IENABLE));
185 dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
186 if ((dummy & (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA)) !=
187 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA))
188 printf("adp_busreset: Adaptec ATM did NOT reset!\n");
192 /***********************************************************************/
198 static const char *en_pci_probe(config_id, device_id)
204 #if !defined(MIDWAY_ADPONLY)
205 if (PCI_VENDOR(device_id) == PCI_VENDOR_EFFICIENTNETS &&
206 (PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PF ||
207 PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PA))
208 return "Efficient Networks ENI-155p";
211 #if !defined(MIDWAY_ENIONLY)
212 if (PCI_VENDOR(device_id) == PCI_VENDOR_ADP &&
213 (PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5900 ||
214 PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5905))
215 return "Adaptec 155 ATM";
221 static void en_pci_attach(config_id, unit)
228 struct en_pci_softc *scp;
234 printf("en%d: not configured; kernel is built for only %d device%s.\n",
235 unit, NEN, NEN == 1 ? "" : "s");
239 scp = (struct en_pci_softc *) malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT);
242 bzero(scp, sizeof(*scp)); /* zero */
245 retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &sc->en_base, &pa);
248 free((caddr_t) scp, M_DEVBUF);
251 enpcis[unit] = scp; /* lock it in */
252 en_cd.cd_devs[unit] = sc; /* fake a cfdriver structure */
253 en_cd.cd_ndevs = NEN;
254 snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname), "en%d", unit);
255 sc->enif.if_unit = unit;
256 sc->enif.if_name = "en";
257 scp->en_confid = config_id;
260 * figure out if we are an adaptec card or not.
261 * XXX: why do we have to re-read PC_ID_REG when en_pci_probe already
265 device_id = pci_conf_read(config_id, PCI_ID_REG);
266 sc->is_adaptec = (PCI_VENDOR(device_id) == PCI_VENDOR_ADP) ? 1 : 0;
269 * Add shutdown hook so that DMA is disabled prior to reboot. Not
270 * doing so could allow DMA to corrupt kernel memory during the
271 * reboot before the driver initializes.
273 EVENTHANDLER_REGISTER(shutdown_post_sync, en_pci_shutdown, scp,
274 SHUTDOWN_PRI_DEFAULT);
276 if (!pci_map_int(config_id, en_intr, (void *) sc, &net_imask)) {
277 printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
280 sc->ipl = 1; /* XXX */
286 #if !defined(MIDWAY_ENIONLY)
287 if (sc->is_adaptec) {
288 adp_get_macaddr(scp);
289 sc->en_busreset = adp_busreset;
294 #if !defined(MIDWAY_ADPONLY)
295 if (!sc->is_adaptec) {
296 eni_get_macaddr(scp);
297 sc->en_busreset = NULL;
298 pci_conf_write(config_id, EN_TONGA, (TONGA_SWAP_DMA|TONGA_SWAP_WORD));
303 * done PCI specific stuff
315 struct en_pci_softc *psc = (struct en_pci_softc *)sc;
321 #if !defined(MIDWAY_ENIONLY)
323 #if defined(sparc) || defined(__FreeBSD__)
324 #define bus_space_read_1(t, h, o) \
325 ((void)t, (*(volatile u_int8_t *)((h) + (o))))
330 struct en_pci_softc *scp;
332 struct en_softc * sc = (struct en_softc *)scp;
335 for (lcv = 0; lcv < sizeof(sc->macaddr); lcv++)
336 sc->macaddr[lcv] = bus_space_read_1(sc->en_memt, sc->en_base,
337 MID_ADPMACOFF + lcv);
340 #endif /* MIDWAY_ENIONLY */
342 #if !defined(MIDWAY_ADPONLY)
345 * Read station (MAC) address from serial EEPROM.
346 * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
348 #define EN_PROM_MAGIC 0x0c
349 #define EN_PROM_DATA 0x02
350 #define EN_PROM_CLK 0x01
355 struct en_pci_softc *scp;
357 struct en_softc * sc = (struct en_softc *)scp;
358 pcici_t id = scp->en_confid;
359 int i, j, address, status;
360 u_int32_t data, t_data;
363 t_data = pci_conf_read(id, EN_TONGA) & 0xffffff00;
365 data = EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
366 pci_conf_write(id, EN_TONGA, data);
368 for (i = 0; i < sizeof(sc->macaddr); i ++){
369 /* start operation */
370 data |= EN_PROM_DATA ;
371 pci_conf_write(id, EN_TONGA, data);
372 data |= EN_PROM_CLK ;
373 pci_conf_write(id, EN_TONGA, data);
374 data &= ~EN_PROM_DATA ;
375 pci_conf_write(id, EN_TONGA, data);
376 data &= ~EN_PROM_CLK ;
377 pci_conf_write(id, EN_TONGA, data);
378 /* send address with serial line */
379 address = ((i + EN_ESI) << 1) + 1;
380 for ( j = 7 ; j >= 0 ; j --){
381 data = (address >> j) & 1 ? data | EN_PROM_DATA :
382 data & ~EN_PROM_DATA;
383 pci_conf_write(id, EN_TONGA, data);
384 data |= EN_PROM_CLK ;
385 pci_conf_write(id, EN_TONGA, data);
386 data &= ~EN_PROM_CLK ;
387 pci_conf_write(id, EN_TONGA, data);
390 data |= EN_PROM_DATA ;
391 pci_conf_write(id, EN_TONGA, data);
392 data |= EN_PROM_CLK ;
393 pci_conf_write(id, EN_TONGA, data);
394 data = pci_conf_read(id, EN_TONGA);
395 status = data & EN_PROM_DATA;
396 data &= ~EN_PROM_CLK ;
397 pci_conf_write(id, EN_TONGA, data);
398 data |= EN_PROM_DATA ;
399 pci_conf_write(id, EN_TONGA, data);
403 for ( j = 7 ; j >= 0 ; j --){
405 data |= EN_PROM_DATA ;
406 pci_conf_write(id, EN_TONGA, data);
407 data |= EN_PROM_CLK ;
408 pci_conf_write(id, EN_TONGA, data);
409 data = pci_conf_read(id, EN_TONGA);
410 if(data & EN_PROM_DATA) tmp |= 1;
411 data &= ~EN_PROM_CLK ;
412 pci_conf_write(id, EN_TONGA, data);
413 data |= EN_PROM_DATA ;
414 pci_conf_write(id, EN_TONGA, data);
417 data |= EN_PROM_DATA ;
418 pci_conf_write(id, EN_TONGA, data);
419 data |= EN_PROM_CLK ;
420 pci_conf_write(id, EN_TONGA, data);
421 data = pci_conf_read(id, EN_TONGA);
422 status = data & EN_PROM_DATA;
423 data &= ~EN_PROM_CLK ;
424 pci_conf_write(id, EN_TONGA, data);
425 data |= EN_PROM_DATA ;
426 pci_conf_write(id, EN_TONGA, data);
428 sc->macaddr[i] = tmp;
431 data &= ~EN_PROM_DATA;
432 pci_conf_write(id, EN_TONGA, data);
434 pci_conf_write(id, EN_TONGA, data);
435 data |= EN_PROM_DATA;
436 pci_conf_write(id, EN_TONGA, data);
437 pci_conf_write(id, EN_TONGA, t_data);
440 #endif /* !MIDWAY_ADPONLY */