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.
49 #error "The en device requires the old pci compatibility shims"
54 #include <sys/param.h>
55 #include <sys/kernel.h>
56 #include <sys/systm.h>
57 #if defined(__FreeBSD__)
58 #include <sys/eventhandler.h>
60 #include <sys/malloc.h>
61 #include <sys/socket.h>
63 #include <machine/clock.h> /* for DELAY */
67 #include <pci/pcivar.h>
68 #include <pci/pcireg.h>
70 #include <dev/en/midwayreg.h>
71 #include <dev/en/midwayvar.h>
78 static void en_pci_attach __P((pcici_t, int));
79 static const char *en_pci_probe __P((pcici_t, pcidi_t));
80 static void en_pci_shutdown __P((void *, int));
87 /* bus independent stuff */
88 struct en_softc esc; /* includes "device" structure */
91 void *sc_ih; /* interrupt handle */
92 pci_chipset_tag_t en_pc; /* for PCI calls */
93 pcici_t en_confid; /* config id */
96 #if !defined(MIDWAY_ENIONLY)
97 static void eni_get_macaddr __P((struct en_pci_softc *));
99 #if !defined(MIDWAY_ADPONLY)
100 static void adp_get_macaddr __P((struct en_pci_softc *));
104 * pointers to softcs (we alloc)
107 static struct en_pci_softc *enpcis[NEN] = {0};
108 extern struct cfdriver en_cd;
111 * autoconfig structures
114 static u_long en_pci_count;
116 static struct pci_device endevice = {
124 COMPAT_PCI_DRIVER (en, endevice);
127 * local defines (PCI specific stuff)
131 * address of config base memory address register in PCI config space
132 * (this is card specific)
135 #define PCI_CBMA 0x10
138 * tonga (pci bridge). ENI cards only!
141 #define EN_TONGA 0x60 /* PCI config addr of tonga reg */
143 #define TONGA_SWAP_DMA 0x80 /* endian swap control */
144 #define TONGA_SWAP_BYTE 0x40
145 #define TONGA_SWAP_WORD 0x20
148 * adaptec pci bridge. ADP cards only!
151 #define ADP_PCIREG 0x050040 /* PCI control register */
153 #define ADP_PCIREG_RESET 0x1 /* reset card */
154 #define ADP_PCIREG_IENABLE 0x2 /* interrupt enable */
155 #define ADP_PCIREG_SWAP_WORD 0x4 /* swap byte on slave access */
156 #define ADP_PCIREG_SWAP_DMA 0x8 /* swap byte on DMA */
158 #define PCI_VENDOR_EFFICIENTNETS 0x111a /* Efficent Networks */
159 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PF 0x0000 /* ENI-155P ATM */
160 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PA 0x0002 /* ENI-155P ATM */
161 #define PCI_VENDOR_ADP 0x9004 /* adaptec */
162 #define PCI_PRODUCT_ADP_AIC5900 0x5900
163 #define PCI_PRODUCT_ADP_AIC5905 0x5905
164 #define PCI_VENDOR(x) ((x) & 0xFFFF)
165 #define PCI_CHIPID(x) (((x) >> 16) & 0xFFFF)
167 #if !defined(MIDWAY_ENIONLY)
169 static void adp_busreset __P((void *));
172 * bus specific reset function [ADP only!]
175 static void adp_busreset(v)
180 struct en_softc *sc = (struct en_softc *) v;
183 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, ADP_PCIREG_RESET);
184 DELAY(1000); /* let it reset */
185 dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
186 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
187 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA|ADP_PCIREG_IENABLE));
188 dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
189 if ((dummy & (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA)) !=
190 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA))
191 printf("adp_busreset: Adaptec ATM did NOT reset!\n");
195 /***********************************************************************/
201 static const char *en_pci_probe(config_id, device_id)
207 #if !defined(MIDWAY_ADPONLY)
208 if (PCI_VENDOR(device_id) == PCI_VENDOR_EFFICIENTNETS &&
209 (PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PF ||
210 PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PA))
211 return "Efficient Networks ENI-155p";
214 #if !defined(MIDWAY_ENIONLY)
215 if (PCI_VENDOR(device_id) == PCI_VENDOR_ADP &&
216 (PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5900 ||
217 PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5905))
218 return "Adaptec 155 ATM";
224 static void en_pci_attach(config_id, unit)
231 struct en_pci_softc *scp;
237 printf("en%d: not configured; kernel is built for only %d device%s.\n",
238 unit, NEN, NEN == 1 ? "" : "s");
242 scp = (struct en_pci_softc *) malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT);
245 bzero(scp, sizeof(*scp)); /* zero */
248 retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &sc->en_base, &pa);
251 free((caddr_t) scp, M_DEVBUF);
254 enpcis[unit] = scp; /* lock it in */
255 en_cd.cd_devs[unit] = sc; /* fake a cfdriver structure */
256 en_cd.cd_ndevs = NEN;
257 snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname), "en%d", unit);
258 sc->enif.if_unit = unit;
259 sc->enif.if_name = "en";
260 scp->en_confid = config_id;
263 * figure out if we are an adaptec card or not.
264 * XXX: why do we have to re-read PC_ID_REG when en_pci_probe already
268 device_id = pci_conf_read(config_id, PCI_ID_REG);
269 sc->is_adaptec = (PCI_VENDOR(device_id) == PCI_VENDOR_ADP) ? 1 : 0;
272 * Add shutdown hook so that DMA is disabled prior to reboot. Not
273 * doing so could allow DMA to corrupt kernel memory during the
274 * reboot before the driver initializes.
276 EVENTHANDLER_REGISTER(shutdown_post_sync, en_pci_shutdown, scp,
277 SHUTDOWN_PRI_DEFAULT);
279 if (!pci_map_int(config_id, en_intr, (void *) sc, &net_imask)) {
280 printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
283 sc->ipl = 1; /* XXX */
289 #if !defined(MIDWAY_ENIONLY)
290 if (sc->is_adaptec) {
291 adp_get_macaddr(scp);
292 sc->en_busreset = adp_busreset;
297 #if !defined(MIDWAY_ADPONLY)
298 if (!sc->is_adaptec) {
299 eni_get_macaddr(scp);
300 sc->en_busreset = NULL;
301 pci_conf_write(config_id, EN_TONGA, (TONGA_SWAP_DMA|TONGA_SWAP_WORD));
306 * done PCI specific stuff
318 struct en_pci_softc *psc = (struct en_pci_softc *)sc;
324 #if !defined(MIDWAY_ENIONLY)
326 #if defined(sparc) || defined(__FreeBSD__)
327 #define bus_space_read_1(t, h, o) \
328 ((void)t, (*(volatile u_int8_t *)((h) + (o))))
333 struct en_pci_softc *scp;
335 struct en_softc * sc = (struct en_softc *)scp;
338 for (lcv = 0; lcv < sizeof(sc->macaddr); lcv++)
339 sc->macaddr[lcv] = bus_space_read_1(sc->en_memt, sc->en_base,
340 MID_ADPMACOFF + lcv);
343 #endif /* MIDWAY_ENIONLY */
345 #if !defined(MIDWAY_ADPONLY)
348 * Read station (MAC) address from serial EEPROM.
349 * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
351 #define EN_PROM_MAGIC 0x0c
352 #define EN_PROM_DATA 0x02
353 #define EN_PROM_CLK 0x01
358 struct en_pci_softc *scp;
360 struct en_softc * sc = (struct en_softc *)scp;
361 pcici_t id = scp->en_confid;
362 int i, j, address, status;
363 u_int32_t data, t_data;
366 t_data = pci_conf_read(id, EN_TONGA) & 0xffffff00;
368 data = EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
369 pci_conf_write(id, EN_TONGA, data);
371 for (i = 0; i < sizeof(sc->macaddr); i ++){
372 /* start operation */
373 data |= EN_PROM_DATA ;
374 pci_conf_write(id, EN_TONGA, data);
375 data |= EN_PROM_CLK ;
376 pci_conf_write(id, EN_TONGA, data);
377 data &= ~EN_PROM_DATA ;
378 pci_conf_write(id, EN_TONGA, data);
379 data &= ~EN_PROM_CLK ;
380 pci_conf_write(id, EN_TONGA, data);
381 /* send address with serial line */
382 address = ((i + EN_ESI) << 1) + 1;
383 for ( j = 7 ; j >= 0 ; j --){
384 data = (address >> j) & 1 ? data | EN_PROM_DATA :
385 data & ~EN_PROM_DATA;
386 pci_conf_write(id, EN_TONGA, data);
387 data |= EN_PROM_CLK ;
388 pci_conf_write(id, EN_TONGA, data);
389 data &= ~EN_PROM_CLK ;
390 pci_conf_write(id, EN_TONGA, data);
393 data |= EN_PROM_DATA ;
394 pci_conf_write(id, EN_TONGA, data);
395 data |= EN_PROM_CLK ;
396 pci_conf_write(id, EN_TONGA, data);
397 data = pci_conf_read(id, EN_TONGA);
398 status = data & EN_PROM_DATA;
399 data &= ~EN_PROM_CLK ;
400 pci_conf_write(id, EN_TONGA, data);
401 data |= EN_PROM_DATA ;
402 pci_conf_write(id, EN_TONGA, data);
406 for ( j = 7 ; j >= 0 ; j --){
408 data |= EN_PROM_DATA ;
409 pci_conf_write(id, EN_TONGA, data);
410 data |= EN_PROM_CLK ;
411 pci_conf_write(id, EN_TONGA, data);
412 data = pci_conf_read(id, EN_TONGA);
413 if(data & EN_PROM_DATA) tmp |= 1;
414 data &= ~EN_PROM_CLK ;
415 pci_conf_write(id, EN_TONGA, data);
416 data |= EN_PROM_DATA ;
417 pci_conf_write(id, EN_TONGA, data);
420 data |= EN_PROM_DATA ;
421 pci_conf_write(id, EN_TONGA, data);
422 data |= EN_PROM_CLK ;
423 pci_conf_write(id, EN_TONGA, data);
424 data = pci_conf_read(id, EN_TONGA);
425 status = data & EN_PROM_DATA;
426 data &= ~EN_PROM_CLK ;
427 pci_conf_write(id, EN_TONGA, data);
428 data |= EN_PROM_DATA ;
429 pci_conf_write(id, EN_TONGA, data);
431 sc->macaddr[i] = tmp;
434 data &= ~EN_PROM_DATA;
435 pci_conf_write(id, EN_TONGA, data);
437 pci_conf_write(id, EN_TONGA, data);
438 data |= EN_PROM_DATA;
439 pci_conf_write(id, EN_TONGA, data);
440 pci_conf_write(id, EN_TONGA, t_data);
443 #endif /* !MIDWAY_ADPONLY */