if_en_pci.c revision 25603
1260684Skaiw/*	$NetBSD: if_en_pci.c,v 1.1 1996/06/22 02:00:31 chuck Exp $	*/
2260684Skaiw
3260684Skaiw/*
4260684Skaiw *
5260684Skaiw * Copyright (c) 1996 Charles D. Cranor and Washington University.
6260684Skaiw * All rights reserved.
7260684Skaiw *
8260684Skaiw * Redistribution and use in source and binary forms, with or without
9260684Skaiw * modification, are permitted provided that the following conditions
10260684Skaiw * are met:
11260684Skaiw * 1. Redistributions of source code must retain the above copyright
12260684Skaiw *    notice, this list of conditions and the following disclaimer.
13260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright
14260684Skaiw *    notice, this list of conditions and the following disclaimer in the
15260684Skaiw *    documentation and/or other materials provided with the distribution.
16260684Skaiw * 3. All advertising materials mentioning features or use of this software
17260684Skaiw *    must display the following acknowledgement:
18260684Skaiw *      This product includes software developed by Charles D. Cranor and
19260684Skaiw *	Washington University.
20260684Skaiw * 4. The name of the author may not be used to endorse or promote products
21260684Skaiw *    derived from this software without specific prior written permission.
22260684Skaiw *
23260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24260684Skaiw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25260684Skaiw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26260684Skaiw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27260684Skaiw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28260684Skaiw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29260684Skaiw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30260684Skaiw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31260684Skaiw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32260684Skaiw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33260684Skaiw */
34260684Skaiw
35260684Skaiw/*
36260684Skaiw *
37260684Skaiw * i f _ e n _ p c i . c
38260684Skaiw *
39260684Skaiw * author: Chuck Cranor <chuck@ccrc.wustl.edu>
40260684Skaiw * started: spring, 1996.
41260684Skaiw *
42260684Skaiw * FreeBSD PCI glue for the eni155p card.
43260684Skaiw * thanks to Matt Thomas for figuring out FreeBSD vs NetBSD vs etc.. diffs.
44260684Skaiw */
45260684Skaiw
46260684Skaiw#include "en.h"
47260684Skaiw#include "pci.h"
48260684Skaiw#if (NEN > 0) && (NPCI > 0)
49260684Skaiw
50260684Skaiw#include <sys/param.h>
51260684Skaiw#include <sys/types.h>
52260684Skaiw#include <sys/kernel.h>
53260684Skaiw#include <sys/systm.h>
54260684Skaiw#ifndef SHUTDOWN_PRE_SYNC
55260684Skaiw/*
56260684Skaiw * device shutdown mechanism has been changed since 2.2-ALPHA.
57260684Skaiw * if SHUTDOWN_PRE_SYNC is defined in "sys/systm.h", use new one.
58260684Skaiw * otherwise, use old one.
59260684Skaiw *	new: 2.2-ALPHA, 2.2-BETA, 2.2-GAMME, 2.2-RELEASE, 3.0
60260684Skaiw *	old: 2.1.5, 2.1.6, 2.2-SNAP
61260684Skaiw *			-- kjc
62260684Skaiw */
63260684Skaiw#include <sys/devconf.h>
64260684Skaiw#endif
65260684Skaiw#include <sys/mbuf.h>
66260684Skaiw#include <sys/socket.h>
67260684Skaiw#include <sys/socketvar.h>
68260684Skaiw
69260684Skaiw#include <machine/cpufunc.h>		/* for rdtsc proto for clock.h below */
70260684Skaiw#include <machine/clock.h>		/* for DELAY */
71260684Skaiw
72260684Skaiw#include <net/if.h>
73260684Skaiw
74260684Skaiw#include <pci/pcivar.h>
75260684Skaiw#include <pci/pcireg.h>
76260684Skaiw
77260684Skaiw#include <dev/en/midwayreg.h>
78260684Skaiw#include <dev/en/midwayvar.h>
79260684Skaiw
80260684Skaiw
81260684Skaiw/*
82260684Skaiw * prototypes
83260684Skaiw */
84260684Skaiw
85260684Skaiwstatic	void en_pci_attach __P((pcici_t, int));
86260684Skaiwstatic	char *en_pci_probe __P((pcici_t, pcidi_t));
87260684Skaiw#ifdef SHUTDOWN_PRE_SYNC
88260684Skaiwstatic void en_pci_shutdown __P((int, void *));
89260684Skaiw#else
90260684Skaiwstatic	int en_pci_shutdown __P((struct kern_devconf *, int));
91260684Skaiw#endif
92260684Skaiw
93260684Skaiw/*
94260684Skaiw * local structures
95260684Skaiw */
96260684Skaiw
97260684Skaiwstruct en_pci_softc {
98260684Skaiw  /* bus independent stuff */
99260684Skaiw  struct en_softc esc;		/* includes "device" structure */
100260684Skaiw
101260684Skaiw  /* PCI bus glue */
102260684Skaiw  void *sc_ih;			/* interrupt handle */
103260684Skaiw  pci_chipset_tag_t en_pc;	/* for PCI calls */
104260684Skaiw
105260684Skaiw};
106260684Skaiw
107260684Skaiw/*
108260684Skaiw * pointers to softcs (we alloc)
109260684Skaiw */
110260684Skaiw
111260684Skaiwstatic struct en_pci_softc *enpcis[NEN] = {0};
112260684Skaiwextern struct cfdriver en_cd;
113260684Skaiw
114260684Skaiw/*
115260684Skaiw * autoconfig structures
116260684Skaiw */
117260684Skaiw
118260684Skaiwstatic u_long en_pci_count;
119260684Skaiw
120260684Skaiwstatic struct pci_device endevice = {
121260684Skaiw	"en",
122260684Skaiw	en_pci_probe,
123260684Skaiw	en_pci_attach,
124260684Skaiw	&en_pci_count,
125260684Skaiw#ifdef SHUTDOWN_PRE_SYNC
126260684Skaiw	NULL,
127260684Skaiw#else
128260684Skaiw	en_pci_shutdown,
129260684Skaiw#endif
130260684Skaiw};
131260684Skaiw
132260684SkaiwDATA_SET (pcidevice_set, endevice);
133260684Skaiw
134260684Skaiw/*
135260684Skaiw * local defines (PCI specific stuff)
136260684Skaiw */
137260684Skaiw
138260684Skaiw/*
139260684Skaiw * address of config base memory address register in PCI config space
140260684Skaiw * (this is card specific)
141260684Skaiw */
142260684Skaiw
143260684Skaiw#define PCI_CBMA        0x10
144260684Skaiw
145260684Skaiw/*
146260684Skaiw * tonga (pci bridge).   ENI cards only!
147260684Skaiw */
148260684Skaiw
149260684Skaiw#define EN_TONGA        0x60            /* PCI config addr of tonga reg */
150260684Skaiw
151260684Skaiw#define TONGA_SWAP_DMA  0x80            /* endian swap control */
152#define TONGA_SWAP_BYTE 0x40
153#define TONGA_SWAP_WORD 0x20
154
155/*
156 * adaptec pci bridge.   ADP cards only!
157 */
158
159#define ADP_PCIREG      0x050040        /* PCI control register */
160
161#define ADP_PCIREG_RESET        0x1     /* reset card */
162#define ADP_PCIREG_IENABLE	0x2	/* interrupt enable */
163#define ADP_PCIREG_SWAP_WORD	0x4	/* swap byte on slave access */
164#define ADP_PCIREG_SWAP_DMA	0x8	/* swap byte on DMA */
165
166#define PCI_VENDOR_EFFICIENTNETS 0x111a			/* Efficent Networks */
167#define PCI_PRODUCT_EFFICIENTNETS_ENI155PF 0x0000	/* ENI-155P ATM */
168#define PCI_PRODUCT_EFFICIENTNETS_ENI155PA 0x0002	/* ENI-155P ATM */
169#define PCI_VENDOR_ADP 0x9004				/* adaptec */
170#define PCI_PRODUCT_ADP_AIC5900 0x5900
171#define PCI_PRODUCT_ADP_AIC5905 0x5905
172#define PCI_VENDOR(x)		((x) & 0xFFFF)
173#define PCI_CHIPID(x)		(((x) >> 16) & 0xFFFF)
174
175#if !defined(MIDWAY_ENIONLY)
176
177static void adp_busreset __P((void *));
178
179/*
180 * bus specific reset function [ADP only!]
181 */
182
183static void adp_busreset(v)
184
185void *v;
186
187{
188  struct en_softc *sc = (struct en_softc *) v;
189  u_int32_t dummy;
190
191  bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, ADP_PCIREG_RESET);
192  DELAY(1000);  /* let it reset */
193  dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
194  bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
195		(ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA|ADP_PCIREG_IENABLE));
196  dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
197  if ((dummy & (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA)) !=
198		(ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA))
199    printf("adp_busreset: Adaptec ATM did NOT reset!\n");
200}
201#endif
202
203/***********************************************************************/
204
205/*
206 * autoconfig stuff
207 */
208
209static char *en_pci_probe(config_id, device_id)
210
211pcici_t config_id;
212pcidi_t device_id;
213
214{
215#if !defined(MIDWAY_ADPONLY)
216  if (PCI_VENDOR(device_id) == PCI_VENDOR_EFFICIENTNETS &&
217      (PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PF ||
218       PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PA))
219    return "Efficient Networks ENI-155p";
220#endif
221
222#if !defined(MIDWAY_ENIONLY)
223  if (PCI_VENDOR(device_id) == PCI_VENDOR_ADP &&
224      (PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5900 ||
225       PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5905))
226    return "Adaptec 155 ATM";
227#endif
228
229  return 0;
230}
231
232static void en_pci_attach(config_id, unit)
233
234pcici_t config_id;
235int unit;
236
237{
238  struct en_softc *sc;
239  struct en_pci_softc *scp;
240  pcidi_t device_id;
241  int retval;
242  vm_offset_t pa;
243
244  if (unit >= NEN) {
245    printf("en%d: not configured; kernel is built for only %d device%s.\n",
246	unit, NEN, NEN == 1 ? "" : "s");
247    return;
248  }
249
250  scp = (struct en_pci_softc *) malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT);
251  if (scp == NULL)
252    return;
253  bzero(scp, sizeof(*scp));		/* zero */
254  sc = &scp->esc;
255
256  retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &sc->en_base, &pa);
257
258  if (!retval) {
259    free((caddr_t) scp, M_DEVBUF);
260    return;
261  }
262  enpcis[unit] = scp;			/* lock it in */
263  en_cd.cd_devs[unit] = sc;		/* fake a cfdriver structure */
264  en_cd.cd_ndevs = NEN;
265  sprintf(sc->sc_dev.dv_xname, "en%d", unit);
266  sc->enif.if_unit = unit;
267  sc->enif.if_name = "en";
268
269  /*
270   * figure out if we are an adaptec card or not.
271   * XXX: why do we have to re-read PC_ID_REG when en_pci_probe already
272   * had that info?
273   */
274
275  device_id = pci_conf_read(config_id, PCI_ID_REG);
276  sc->is_adaptec = (PCI_VENDOR(device_id) == PCI_VENDOR_ADP) ? 1 : 0;
277
278#ifdef SHUTDOWN_PRE_SYNC
279  /*
280   * Add shutdown hook so that DMA is disabled prior to reboot. Not
281   * doing so could allow DMA to corrupt kernel memory during the
282   * reboot before the driver initializes.
283   */
284  at_shutdown(en_pci_shutdown, scp, SHUTDOWN_POST_SYNC);
285#endif
286
287  if (!pci_map_int(config_id, en_intr, (void *) sc, &net_imask)) {
288    printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
289    return;
290  }
291  sc->ipl = 1; /* XXX */
292
293  /*
294   * set up pci bridge
295   */
296
297#if !defined(MIDWAY_ENIONLY)
298  if (sc->is_adaptec) {
299    sc->en_busreset = adp_busreset;
300    adp_busreset(sc);
301  }
302#endif
303
304#if !defined(MIDWAY_ADPONLY)
305  if (!sc->is_adaptec) {
306    sc->en_busreset = NULL;
307    pci_conf_write(config_id, EN_TONGA, (TONGA_SWAP_DMA|TONGA_SWAP_WORD));
308  }
309#endif
310
311  /*
312   * done PCI specific stuff
313   */
314
315  en_attach(sc);
316
317}
318
319#ifdef SHUTDOWN_PRE_SYNC
320static void
321en_pci_shutdown(
322	int howto,
323	void *sc)
324{
325    struct en_pci_softc *psc = (struct en_pci_softc *)sc;
326
327    en_reset(&psc->esc);
328    DELAY(10);
329}
330#else  /* !SHUTDOWN_PRE_SYNC */
331static int
332en_pci_shutdown(kdc, force)
333
334struct kern_devconf *kdc;
335int force;
336
337{
338  if (kdc->kdc_unit < NEN) {
339    struct en_pci_softc *psc = enpcis[kdc->kdc_unit];
340    if (psc)			/* can it be null? */
341      en_reset(&psc->esc);
342    DELAY(10);
343  }
344  dev_detach(kdc);
345  return(0);
346}
347#endif /* !SHUTDOWN_PRE_SYNC */
348#endif /* NEN > 0 && NPCI > 0 */
349