kernel: Remove some more unused variables in the network drivers.
[dragonfly.git] / sys / dev / netif / en_pci / if_en_pci.c
1 /*      $NetBSD: if_en_pci.c,v 1.1 1996/06/22 02:00:31 chuck Exp $      */
2
3 /*
4  *
5  * Copyright (c) 1996 Charles D. Cranor and Washington University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
22  *
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.
33  *
34  * $FreeBSD: src/sys/pci/if_en_pci.c,v 1.12 1999/08/21 22:10:49 msmith Exp $
35  */
36
37 /*
38  *
39  * i f _ e n _ p c i . c  
40  *
41  * author: Chuck Cranor <chuck@ccrc.wustl.edu>
42  * started: spring, 1996.
43  *
44  * FreeBSD PCI glue for the eni155p card.
45  * thanks to Matt Thomas for figuring out FreeBSD vs NetBSD vs etc.. diffs.
46  */
47
48 #include "use_en.h"
49
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #if defined(__DragonFly__) || defined(__FreeBSD__)
54 #include <sys/eventhandler.h>
55 #endif
56 #include <sys/malloc.h>
57 #include <sys/socket.h>
58
59 #include <machine/clock.h>              /* for DELAY */
60
61 #include <net/if.h>
62
63 #include <bus/pci/pcidevs.h>
64 #include <bus/pci/pcivar.h>
65 #include <bus/pci/pcireg.h>
66
67 #include <dev/atm/en/midwayreg.h>
68 #include <dev/atm/en/midwayvar.h>
69
70
71 /*
72  * prototypes
73  */
74
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);
78
79 /*
80  * local structures
81  */
82
83 struct en_pci_softc {
84   /* bus independent stuff */
85   struct en_softc esc;          /* includes "device" structure */
86
87   /* PCI bus glue */
88   void *sc_ih;                  /* interrupt handle */
89   pci_chipset_tag_t en_pc;      /* for PCI calls */
90   pcici_t en_confid;            /* config id */
91 };
92
93 #if !defined(MIDWAY_ENIONLY)
94 static  void eni_get_macaddr (struct en_pci_softc *);
95 #endif
96 #if !defined(MIDWAY_ADPONLY)
97 static  void adp_get_macaddr (struct en_pci_softc *);
98 #endif
99
100 /*
101  * pointers to softcs (we alloc)
102  */
103
104 static struct en_pci_softc *enpcis[NEN] = {0};
105 extern struct cfdriver en_cd;
106
107 /*
108  * autoconfig structures
109  */
110
111 static u_long en_pci_count;
112
113 static struct pci_device endevice = {
114         "en",
115         en_pci_probe,
116         en_pci_attach,
117         &en_pci_count,
118         NULL,
119 };  
120
121 COMPAT_PCI_DRIVER (if_en, endevice);
122
123 /*
124  * local defines (PCI specific stuff)
125  */
126
127 /* 
128  * address of config base memory address register in PCI config space
129  * (this is card specific)
130  */
131         
132 #define PCI_CBMA        0x10
133
134 /*
135  * tonga (pci bridge).   ENI cards only!
136  */
137
138 #define EN_TONGA        0x60            /* PCI config addr of tonga reg */
139
140 #define TONGA_SWAP_DMA  0x80            /* endian swap control */
141 #define TONGA_SWAP_BYTE 0x40
142 #define TONGA_SWAP_WORD 0x20
143
144 /*
145  * adaptec pci bridge.   ADP cards only!
146  */
147
148 #define ADP_PCIREG      0x050040        /* PCI control register */
149
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 */
154
155 #define PCI_VENDOR(x)           ((x) & 0xFFFF)
156 #define PCI_CHIPID(x)           (((x) >> 16) & 0xFFFF)
157
158 #if !defined(MIDWAY_ENIONLY)
159
160 static void adp_busreset (void *);
161
162 /*
163  * bus specific reset function [ADP only!]
164  */
165
166 static void
167 adp_busreset(void *v)
168 {
169   struct en_softc *sc = (struct en_softc *) v;
170   u_int32_t dummy;
171
172   bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, ADP_PCIREG_RESET);
173   DELAY(1000);  /* let it reset */
174   dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
175   bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, 
176                 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA|ADP_PCIREG_IENABLE));
177   dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
178   if ((dummy & (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA)) !=
179                 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA))
180     kprintf("adp_busreset: Adaptec ATM did NOT reset!\n");
181 }
182 #endif
183
184 /***********************************************************************/
185
186 /*
187  * autoconfig stuff
188  */
189
190 static const char *
191 en_pci_probe(pcici_t config_id, pcidi_t device_id)
192 {
193 #if !defined(MIDWAY_ADPONLY)
194   if (PCI_VENDOR(device_id) == PCI_VENDOR_EFFICIENTNETS && 
195       (PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PF ||
196        PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PA))
197     return "Efficient Networks ENI-155p";
198 #endif
199
200 #if !defined(MIDWAY_ENIONLY)
201   if (PCI_VENDOR(device_id) == PCI_VENDOR_ADP && 
202       (PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5900 ||
203        PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5905))
204     return "Adaptec 155 ATM";
205 #endif
206
207   return 0;
208 }
209
210 static void
211 en_pci_attach(pcici_t config_id, int unit)
212 {
213   struct en_softc *sc;
214   struct en_pci_softc *scp;
215   pcidi_t device_id;
216   int retval;
217   vm_offset_t pa;
218
219   if (unit >= NEN) {
220     kprintf("en%d: not configured; kernel is built for only %d device%s.\n",
221         unit, NEN, NEN == 1 ? "" : "s");
222     return;
223   }
224
225   scp = kmalloc(sizeof(*scp), M_DEVBUF, M_WAITOK | M_ZERO);
226   sc = &scp->esc;
227
228   retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &sc->en_base, &pa);
229
230   if (!retval) {
231     kfree((caddr_t) scp, M_DEVBUF);
232     return;
233   }
234   enpcis[unit] = scp;                   /* lock it in */
235   en_cd.cd_devs[unit] = sc;             /* fake a cfdriver structure */
236   en_cd.cd_ndevs = NEN;
237   if_initname(&(sc->enif), "en", unit);
238   ksnprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname), "%s",
239       sc->enif.if_xname);
240   scp->en_confid = config_id;
241
242   /*
243    * figure out if we are an adaptec card or not.
244    * XXX: why do we have to re-read PC_ID_REG when en_pci_probe already
245    * had that info?
246    */
247
248   device_id = pci_conf_read(config_id, PCI_ID_REG);
249   sc->is_adaptec = (PCI_VENDOR(device_id) == PCI_VENDOR_ADP) ? 1 : 0;
250   
251   /*
252    * Add shutdown hook so that DMA is disabled prior to reboot. Not
253    * doing so could allow DMA to corrupt kernel memory during the
254    * reboot before the driver initializes.
255    */
256   EVENTHANDLER_REGISTER(shutdown_post_sync, en_pci_shutdown, scp,
257                         SHUTDOWN_PRI_DRIVER);
258
259   if (!pci_map_int(config_id, en_intr, (void *) sc)) {
260     kprintf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
261     return;
262   }
263   sc->ipl = 1; /* XXX */
264
265   /*
266    * set up pci bridge
267    */
268
269 #if !defined(MIDWAY_ENIONLY)
270   if (sc->is_adaptec) {
271     adp_get_macaddr(scp);
272     sc->en_busreset = adp_busreset;
273     adp_busreset(sc);
274   }
275 #endif
276
277 #if !defined(MIDWAY_ADPONLY)
278   if (!sc->is_adaptec) {
279     eni_get_macaddr(scp);
280     sc->en_busreset = NULL;
281     pci_conf_write(config_id, EN_TONGA, (TONGA_SWAP_DMA|TONGA_SWAP_WORD));
282   }
283 #endif
284
285   /*
286    * done PCI specific stuff
287    */
288
289   en_attach(sc);
290
291 }
292
293 static void
294 en_pci_shutdown(void *sc, int howto)
295 {
296     struct en_pci_softc *psc = (struct en_pci_softc *)sc;
297     
298     en_reset(&psc->esc);
299     DELAY(10);
300 }
301
302 #if !defined(MIDWAY_ENIONLY)
303
304 #if defined(__DragonFly__) || defined(sparc) || defined(__FreeBSD__)
305 #define bus_space_read_1(t, h, o) \
306                 ((void)t, (*(volatile u_int8_t *)((h) + (o))))
307 #endif
308
309 static void 
310 adp_get_macaddr(struct en_pci_softc *scp)
311 {
312   struct en_softc * sc = (struct en_softc *)scp;
313   int lcv;
314
315   for (lcv = 0; lcv < sizeof(sc->macaddr); lcv++)
316     sc->macaddr[lcv] = bus_space_read_1(sc->en_memt, sc->en_base,
317                                         MID_ADPMACOFF + lcv);
318 }
319
320 #endif /* MIDWAY_ENIONLY */
321
322 #if !defined(MIDWAY_ADPONLY)
323
324 /*
325  * Read station (MAC) address from serial EEPROM.
326  * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
327  */
328 #define EN_PROM_MAGIC  0x0c
329 #define EN_PROM_DATA   0x02
330 #define EN_PROM_CLK    0x01
331 #define EN_ESI         64
332
333 static void 
334 eni_get_macaddr(struct en_pci_softc *scp)
335 {
336   struct en_softc * sc = (struct en_softc *)scp;
337   pcici_t id = scp->en_confid;
338   int i, j, address;
339   u_int32_t data, t_data;
340   u_int8_t tmp;
341   
342   t_data = pci_conf_read(id, EN_TONGA) & 0xffffff00;
343
344   data =  EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
345   pci_conf_write(id, EN_TONGA, data);
346
347   for (i = 0; i < sizeof(sc->macaddr); i ++){
348     /* start operation */
349     data |= EN_PROM_DATA ;
350     pci_conf_write(id, EN_TONGA, data);
351     data |= EN_PROM_CLK ;
352     pci_conf_write(id, EN_TONGA, data);
353     data &= ~EN_PROM_DATA ;
354     pci_conf_write(id, EN_TONGA, data);
355     data &= ~EN_PROM_CLK ;
356     pci_conf_write(id, EN_TONGA, data);
357     /* send address with serial line */
358     address = ((i + EN_ESI) << 1) + 1;
359     for ( j = 7 ; j >= 0 ; j --){
360       data = (address >> j) & 1 ? data | EN_PROM_DATA :
361       data & ~EN_PROM_DATA;
362       pci_conf_write(id, EN_TONGA, data);
363       data |= EN_PROM_CLK ;
364       pci_conf_write(id, EN_TONGA, data);
365       data &= ~EN_PROM_CLK ;
366       pci_conf_write(id, EN_TONGA, data);
367     }
368     /* get ack */
369     data |= EN_PROM_DATA ;
370     pci_conf_write(id, EN_TONGA, data);
371     data |= EN_PROM_CLK ;
372     pci_conf_write(id, EN_TONGA, data);
373     data = pci_conf_read(id, EN_TONGA);
374     data &= ~EN_PROM_CLK ;
375     pci_conf_write(id, EN_TONGA, data);
376     data |= EN_PROM_DATA ;
377     pci_conf_write(id, EN_TONGA, data);
378
379     tmp = 0;
380
381     for ( j = 7 ; j >= 0 ; j --){
382       tmp <<= 1;
383       data |= EN_PROM_DATA ;
384       pci_conf_write(id, EN_TONGA, data);
385       data |= EN_PROM_CLK ;
386       pci_conf_write(id, EN_TONGA, data);
387       data = pci_conf_read(id, EN_TONGA);
388       if(data & EN_PROM_DATA) tmp |= 1;
389       data &= ~EN_PROM_CLK ;
390       pci_conf_write(id, EN_TONGA, data);
391       data |= EN_PROM_DATA ;
392       pci_conf_write(id, EN_TONGA, data);
393     }
394     /* get ack */
395     data |= EN_PROM_DATA ;
396     pci_conf_write(id, EN_TONGA, data);
397     data |= EN_PROM_CLK ;
398     pci_conf_write(id, EN_TONGA, data);
399     data = pci_conf_read(id, EN_TONGA);
400     data &= ~EN_PROM_CLK ;
401     pci_conf_write(id, EN_TONGA, data);
402     data |= EN_PROM_DATA ;
403     pci_conf_write(id, EN_TONGA, data);
404
405     sc->macaddr[i] = tmp;
406   }
407   /* stop operation */
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 |=  EN_PROM_DATA;
413   pci_conf_write(id, EN_TONGA, data);
414   pci_conf_write(id, EN_TONGA, t_data);
415 }
416
417 #endif /* !MIDWAY_ADPONLY */