if_en_pci.c revision 116294
1/*	$NetBSD: if_en_pci.c,v 1.1 1996/06/22 02:00:31 chuck Exp $	*/
2/*
3 *
4 * Copyright (c) 1996 Charles D. Cranor and Washington University.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
21 *
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.
32 *
33 */
34
35/*
36 * i f _ e n _ p c i . c
37 *
38 * author: Chuck Cranor <chuck@ccrc.wustl.edu>
39 * started: spring, 1996.
40 *
41 * FreeBSD PCI glue for the eni155p card.
42 * thanks to Matt Thomas for figuring out FreeBSD vs NetBSD vs etc.. diffs.
43 */
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/sys/dev/en/if_en_pci.c 116294 2003-06-13 12:08:09Z harti $");
47
48#include <sys/param.h>
49#include <sys/kernel.h>
50#include <sys/systm.h>
51#include <sys/socket.h>
52#include <sys/sysctl.h>
53
54#include <sys/bus.h>
55#include <machine/bus.h>
56#include <sys/rman.h>
57#include <machine/resource.h>
58
59#include <vm/uma.h>
60
61#include <net/if.h>
62#include <net/if_atm.h>
63#include <net/if_media.h>
64
65#include <pci/pcivar.h>
66#include <pci/pcireg.h>
67
68#include <dev/utopia/utopia.h>
69#include <dev/en/midwayreg.h>
70#include <dev/en/midwayvar.h>
71
72MODULE_DEPEND(en, pci, 1, 1, 1);
73MODULE_DEPEND(en, atm, 1, 1, 1);
74MODULE_DEPEND(en, utopia, 1, 1, 1);
75
76/*
77 * local structures
78 */
79struct en_pci_softc {
80	/* bus independent stuff */
81	struct en_softc esc;	/* includes "device" structure */
82
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 */
87};
88
89static  void eni_get_macaddr(device_t, struct en_pci_softc *);
90static  void adp_get_macaddr(struct en_pci_softc *);
91
92/*
93 * address of config base memory address register in PCI config space
94 * (this is card specific)
95 */
96#define PCI_CBMA        0x10
97
98/*
99 * tonga (pci bridge).   ENI cards only!
100 */
101#define EN_TONGA        0x60            /* PCI config addr of tonga reg */
102
103#define TONGA_SWAP_DMA  0x80            /* endian swap control */
104#define TONGA_SWAP_BYTE 0x40
105#define TONGA_SWAP_WORD 0x20
106#define TONGA_READ_MULT	0x00
107#define TONGA_READ_MEM	0x04
108#define TONGA_READ_IVAN	0x08
109#define TONGA_READ_KEN	0x0C
110
111/*
112 * adaptec pci bridge.   ADP cards only!
113 */
114#define ADP_PCIREG      0x050040        /* PCI control register */
115
116#define ADP_PCIREG_RESET        0x1     /* reset card */
117#define ADP_PCIREG_IENABLE	0x2	/* interrupt enable */
118#define ADP_PCIREG_SWAP_WORD	0x4	/* swap byte on slave access */
119#define ADP_PCIREG_SWAP_DMA	0x8	/* swap byte on DMA */
120
121#define PCI_VENDOR_EFFICIENTNETS 0x111a			/* Efficent Networks */
122#define PCI_PRODUCT_EFFICIENTNETS_ENI155PF 0x0000	/* ENI-155P ATM */
123#define PCI_PRODUCT_EFFICIENTNETS_ENI155PA 0x0002	/* ENI-155P ATM */
124#define PCI_VENDOR_ADP 0x9004				/* adaptec */
125#define PCI_PRODUCT_ADP_AIC5900 0x5900
126#define PCI_PRODUCT_ADP_AIC5905 0x5905
127#define PCI_VENDOR(x)		((x) & 0xFFFF)
128#define PCI_CHIPID(x)		(((x) >> 16) & 0xFFFF)
129
130/*
131 * bus specific reset function [ADP only!]
132 */
133static void
134adp_busreset(void *v)
135{
136	struct en_softc *sc = (struct en_softc *)v;
137	uint32_t dummy;
138
139	bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
140	    ADP_PCIREG_RESET);
141	DELAY(1000);  			/* let it reset */
142	dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
143	bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
144	    (ADP_PCIREG_SWAP_DMA | ADP_PCIREG_IENABLE));
145	dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
146	if ((dummy & (ADP_PCIREG_SWAP_WORD | ADP_PCIREG_SWAP_DMA)) !=
147	    ADP_PCIREG_SWAP_DMA)
148		if_printf(&sc->ifatm.ifnet, "adp_busreset: Adaptec ATM did "
149		    "NOT reset!\n");
150}
151
152/***********************************************************************/
153
154/*
155 * autoconfig stuff
156 */
157static int
158en_pci_probe(device_t dev)
159{
160	switch (pci_get_vendor(dev)) {
161
162	  case PCI_VENDOR_EFFICIENTNETS:
163		switch (pci_get_device(dev)) {
164
165		    case PCI_PRODUCT_EFFICIENTNETS_ENI155PF:
166		    case PCI_PRODUCT_EFFICIENTNETS_ENI155PA:
167			device_set_desc(dev, "Efficient Networks ENI-155p");
168			return (0);
169		}
170		break;
171
172	  case PCI_VENDOR_ADP:
173		switch (pci_get_device(dev)) {
174
175		  case PCI_PRODUCT_ADP_AIC5900:
176		  case PCI_PRODUCT_ADP_AIC5905:
177			device_set_desc(dev, "Adaptec 155 ATM");
178			return (0);
179		}
180		break;
181	}
182	return (ENXIO);
183}
184
185static int
186en_pci_attach(device_t dev)
187{
188	struct en_softc *sc;
189	struct en_pci_softc *scp;
190	u_long val;
191	int rid, unit, error = 0;
192
193	sc = device_get_softc(dev);
194	scp = (struct en_pci_softc *)sc;
195
196	unit = device_get_unit(dev);
197	sc->ifatm.ifnet.if_unit = unit;
198	sc->ifatm.ifnet.if_name = "en";
199
200	/*
201	 * Enable bus mastering.
202	 */
203	val = pci_read_config(dev, PCIR_COMMAND, 2);
204	val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
205	pci_write_config(dev, PCIR_COMMAND, val, 2);
206
207	/*
208	 * Map control/status registers.
209	 */
210	rid = PCI_CBMA;
211	scp->res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
212	    0, ~0, 1, RF_ACTIVE);
213	if (scp->res == NULL) {
214		device_printf(dev, "could not map memory\n");
215		error = ENXIO;
216		goto fail;
217	}
218
219	sc->dev = dev;
220	sc->en_memt = rman_get_bustag(scp->res);
221	sc->en_base = rman_get_bushandle(scp->res);
222
223	/*
224	 * Allocate our interrupt.
225	 */
226	rid = 0;
227	scp->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
228	    RF_SHAREABLE | RF_ACTIVE);
229	if (scp->irq == NULL) {
230		device_printf(dev, "could not map interrupt\n");
231		bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
232		error = ENXIO;
233		goto fail;
234	}
235
236	sc->ipl = 1; /* XXX (required to enable interrupt on midway) */
237
238	/* figure out if we are an adaptec card or not */
239	sc->is_adaptec = (pci_get_vendor(dev) == PCI_VENDOR_ADP) ? 1 : 0;
240
241	/*
242	 * set up pci bridge
243	 */
244	if (sc->is_adaptec) {
245		adp_get_macaddr(scp);
246		sc->en_busreset = adp_busreset;
247		adp_busreset(sc);
248	} else {
249		eni_get_macaddr(dev, scp);
250		sc->en_busreset = NULL;
251		pci_write_config(dev, EN_TONGA, TONGA_SWAP_DMA | TONGA_READ_IVAN, 4);
252	}
253
254	/*
255	 * Common attach stuff
256	 */
257	if ((error = en_attach(sc)) != 0) {
258		device_printf(dev, "attach failed\n");
259		bus_teardown_intr(dev, scp->irq, scp->ih);
260		bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
261		bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
262		goto fail;
263	}
264
265	/*
266	 * Do the interrupt SETUP last just before returning
267	 */
268	error = bus_setup_intr(dev, scp->irq, INTR_TYPE_NET,
269	    en_intr, sc, &scp->ih);
270	if (error) {
271		en_reset(sc);
272		atm_ifdetach(&sc->ifatm.ifnet);
273		device_printf(dev, "could not setup irq\n");
274		bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
275		bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
276		en_destroy(sc);
277		goto fail;
278	}
279
280	return (0);
281
282    fail:
283	return (error);
284}
285
286/*
287 * Detach the adapter
288 */
289static int
290en_pci_detach(device_t dev)
291{
292	struct en_softc *sc = device_get_softc(dev);
293	struct en_pci_softc *scp = (struct en_pci_softc *)sc;
294
295	/*
296	 * Stop DMA and drop transmit queue.
297	 */
298	if ((sc->ifatm.ifnet.if_flags & IFF_RUNNING)) {
299		if_printf(&sc->ifatm.ifnet, "still running\n");
300		sc->ifatm.ifnet.if_flags &= ~IFF_RUNNING;
301	}
302
303	/*
304	 * Close down routes etc.
305	 */
306	en_reset(sc);
307	atm_ifdetach(&sc->ifatm.ifnet);
308
309	/*
310	 * Deallocate resources.
311	 */
312	bus_teardown_intr(dev, scp->irq, scp->ih);
313	bus_release_resource(dev, SYS_RES_IRQ, 0, scp->irq);
314	bus_release_resource(dev, SYS_RES_MEMORY, PCI_CBMA, scp->res);
315
316	/*
317	 * Free all the driver internal resources
318	 */
319	en_destroy(sc);
320
321	return (0);
322}
323
324static int
325en_pci_shutdown(device_t dev)
326{
327	struct en_pci_softc *psc = device_get_softc(dev);
328
329	en_reset(&psc->esc);
330	DELAY(10);		/* is this necessary? */
331
332	return (0);
333}
334
335/*
336 * Get the MAC address from an Adaptec board. No idea how to get
337 * serial number or other stuff, because I have no documentation for that
338 * card.
339 */
340static void
341adp_get_macaddr(struct en_pci_softc *scp)
342{
343	struct en_softc * sc = (struct en_softc *)scp;
344	int lcv;
345
346	for (lcv = 0; lcv < sizeof(sc->ifatm.mib.esi); lcv++)
347		sc->ifatm.mib.esi[lcv] = bus_space_read_1(sc->en_memt,
348		    sc->en_base, MID_ADPMACOFF + lcv);
349}
350
351/*
352 * Read station (MAC) address from serial EEPROM.
353 * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
354 */
355#define EN_PROM_MAGIC	0x0c
356#define EN_PROM_DATA	0x02
357#define EN_PROM_CLK	0x01
358#define EN_ESI		64
359#define EN_SERIAL	112
360
361/*
362 * Read a byte from the given address in the EEPROM
363 */
364static uint8_t
365eni_get_byte(device_t dev, uint32_t *data, u_int address)
366{
367	int j;
368	uint8_t tmp;
369
370	address = (address << 1) + 1;
371
372	/* start operation */
373	*data |= EN_PROM_DATA ;
374	pci_write_config(dev, EN_TONGA, *data, 4);
375	*data |= EN_PROM_CLK ;
376	pci_write_config(dev, EN_TONGA, *data, 4);
377	*data &= ~EN_PROM_DATA ;
378	pci_write_config(dev, EN_TONGA, *data, 4);
379	*data &= ~EN_PROM_CLK ;
380	pci_write_config(dev, EN_TONGA, *data, 4);
381	/* send address with serial line */
382	for ( j = 7 ; j >= 0 ; j --) {
383		*data = ((address >> j) & 1) ? (*data | EN_PROM_DATA) :
384		    (*data & ~EN_PROM_DATA);
385		pci_write_config(dev, EN_TONGA, *data, 4);
386		*data |= EN_PROM_CLK ;
387		pci_write_config(dev, EN_TONGA, *data, 4);
388		*data &= ~EN_PROM_CLK ;
389		pci_write_config(dev, EN_TONGA, *data, 4);
390	}
391	/* get ack */
392	*data |= EN_PROM_DATA ;
393	pci_write_config(dev, EN_TONGA, *data, 4);
394	*data |= EN_PROM_CLK ;
395	pci_write_config(dev, EN_TONGA, *data, 4);
396	*data = pci_read_config(dev, EN_TONGA, 4);
397	*data &= ~EN_PROM_CLK ;
398	pci_write_config(dev, EN_TONGA, *data, 4);
399	*data |= EN_PROM_DATA ;
400	pci_write_config(dev, EN_TONGA, *data, 4);
401
402	tmp = 0;
403
404	for ( j = 7 ; j >= 0 ; j --) {
405		tmp <<= 1;
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 = pci_read_config(dev, EN_TONGA, 4);
411		if(*data & EN_PROM_DATA) tmp |= 1;
412		*data &= ~EN_PROM_CLK ;
413		pci_write_config(dev, EN_TONGA, *data, 4);
414		*data |= EN_PROM_DATA ;
415		pci_write_config(dev, EN_TONGA, *data, 4);
416	}
417	/* get ack */
418	*data |= EN_PROM_DATA ;
419	pci_write_config(dev, EN_TONGA, *data, 4);
420	*data |= EN_PROM_CLK ;
421	pci_write_config(dev, EN_TONGA, *data, 4);
422	*data = pci_read_config(dev, EN_TONGA, 4);
423	*data &= ~EN_PROM_CLK ;
424	pci_write_config(dev, EN_TONGA, *data, 4);
425	*data |= EN_PROM_DATA ;
426	pci_write_config(dev, EN_TONGA, *data, 4);
427
428	return (tmp);
429}
430
431/*
432 * Get MAC address and other stuff from the EEPROM
433 */
434static void
435eni_get_macaddr(device_t dev, struct en_pci_softc *scp)
436{
437	struct en_softc * sc = (struct en_softc *)scp;
438	int i;
439	uint32_t data, t_data;
440
441	t_data = pci_read_config(dev, EN_TONGA, 4) & 0xffffff00;
442
443	data =  EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
444	pci_write_config(dev, EN_TONGA, data, 4);
445
446	for (i = 0; i < sizeof(sc->ifatm.mib.esi); i ++)
447		sc->ifatm.mib.esi[i] = eni_get_byte(dev, &data, i + EN_ESI);
448
449	sc->ifatm.mib.serial = 0;
450	for (i = 0; i < 4; i++) {
451		sc->ifatm.mib.serial <<= 8;
452		sc->ifatm.mib.serial |= eni_get_byte(dev, &data, i + EN_SERIAL);
453	}
454	/* stop operation */
455	data &=  ~EN_PROM_DATA;
456	pci_write_config(dev, EN_TONGA, data, 4);
457	data |=  EN_PROM_CLK;
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);
462}
463
464/*
465 * Driver infrastructure
466 */
467static device_method_t en_methods[] = {
468	/* Device interface */
469	DEVMETHOD(device_probe,		en_pci_probe),
470	DEVMETHOD(device_attach,	en_pci_attach),
471	DEVMETHOD(device_detach,	en_pci_detach),
472	DEVMETHOD(device_shutdown,	en_pci_shutdown),
473
474	{ 0, 0 }
475};
476
477static driver_t en_driver = {
478	"en",
479	en_methods,
480	sizeof(struct en_pci_softc),
481};
482
483static devclass_t en_devclass;
484
485DRIVER_MODULE(en, pci, en_driver, en_devclass, 0, 0);
486