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