]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/pci/if_en_pci.c
Per conversations in -current, add #error to these drivers when you don't
[FreeBSD/FreeBSD.git] / sys / 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$
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 #ifndef COMPAT_OLDPCI
49 #error "The en device requires the old pci compatibility shims"
50 #endif
51
52 #include "en.h"
53
54 #include <sys/param.h>
55 #include <sys/kernel.h>
56 #include <sys/systm.h>
57 #if defined(__FreeBSD__)
58 #include <sys/eventhandler.h>
59 #endif
60 #include <sys/malloc.h>
61 #include <sys/socket.h>
62
63 #include <machine/clock.h>              /* for DELAY */
64
65 #include <net/if.h>
66
67 #include <pci/pcivar.h>
68 #include <pci/pcireg.h>
69
70 #include <dev/en/midwayreg.h>
71 #include <dev/en/midwayvar.h>
72
73
74 /*
75  * prototypes
76  */
77
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));
81
82 /*
83  * local structures
84  */
85
86 struct en_pci_softc {
87   /* bus independent stuff */
88   struct en_softc esc;          /* includes "device" structure */
89
90   /* PCI bus glue */
91   void *sc_ih;                  /* interrupt handle */
92   pci_chipset_tag_t en_pc;      /* for PCI calls */
93   pcici_t en_confid;            /* config id */
94 };
95
96 #if !defined(MIDWAY_ENIONLY)
97 static  void eni_get_macaddr __P((struct en_pci_softc *));
98 #endif
99 #if !defined(MIDWAY_ADPONLY)
100 static  void adp_get_macaddr __P((struct en_pci_softc *));
101 #endif
102
103 /*
104  * pointers to softcs (we alloc)
105  */
106
107 static struct en_pci_softc *enpcis[NEN] = {0};
108 extern struct cfdriver en_cd;
109
110 /*
111  * autoconfig structures
112  */
113
114 static u_long en_pci_count;
115
116 static struct pci_device endevice = {
117         "en",
118         en_pci_probe,
119         en_pci_attach,
120         &en_pci_count,
121         NULL,
122 };  
123
124 COMPAT_PCI_DRIVER (en, endevice);
125
126 /*
127  * local defines (PCI specific stuff)
128  */
129
130 /* 
131  * address of config base memory address register in PCI config space
132  * (this is card specific)
133  */
134         
135 #define PCI_CBMA        0x10
136
137 /*
138  * tonga (pci bridge).   ENI cards only!
139  */
140
141 #define EN_TONGA        0x60            /* PCI config addr of tonga reg */
142
143 #define TONGA_SWAP_DMA  0x80            /* endian swap control */
144 #define TONGA_SWAP_BYTE 0x40
145 #define TONGA_SWAP_WORD 0x20
146
147 /*
148  * adaptec pci bridge.   ADP cards only!
149  */
150
151 #define ADP_PCIREG      0x050040        /* PCI control register */
152
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 */
157
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)
166
167 #if !defined(MIDWAY_ENIONLY)
168
169 static void adp_busreset __P((void *));
170
171 /*
172  * bus specific reset function [ADP only!]
173  */
174
175 static void adp_busreset(v)
176
177 void *v;
178
179 {
180   struct en_softc *sc = (struct en_softc *) v;
181   u_int32_t dummy;
182
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");
192 }
193 #endif
194
195 /***********************************************************************/
196
197 /*
198  * autoconfig stuff
199  */
200
201 static const char *en_pci_probe(config_id, device_id)
202
203 pcici_t config_id;
204 pcidi_t device_id;
205
206 {
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";
212 #endif
213
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";
219 #endif
220
221   return 0;
222 }
223
224 static void en_pci_attach(config_id, unit)
225
226 pcici_t config_id;
227 int unit;
228
229 {
230   struct en_softc *sc;
231   struct en_pci_softc *scp;
232   pcidi_t device_id;
233   int retval;
234   vm_offset_t pa;
235
236   if (unit >= NEN) {
237     printf("en%d: not configured; kernel is built for only %d device%s.\n",
238         unit, NEN, NEN == 1 ? "" : "s");
239     return;
240   }
241
242   scp = (struct en_pci_softc *) malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT);
243   if (scp == NULL)
244     return;
245   bzero(scp, sizeof(*scp));             /* zero */
246   sc = &scp->esc;
247
248   retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &sc->en_base, &pa);
249
250   if (!retval) {
251     free((caddr_t) scp, M_DEVBUF);
252     return;
253   }
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;
261
262   /*
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
265    * had that info?
266    */
267
268   device_id = pci_conf_read(config_id, PCI_ID_REG);
269   sc->is_adaptec = (PCI_VENDOR(device_id) == PCI_VENDOR_ADP) ? 1 : 0;
270   
271   /*
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.
275    */
276   EVENTHANDLER_REGISTER(shutdown_post_sync, en_pci_shutdown, scp,
277                         SHUTDOWN_PRI_DEFAULT);
278
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);
281     return;
282   }
283   sc->ipl = 1; /* XXX */
284
285   /*
286    * set up pci bridge
287    */
288
289 #if !defined(MIDWAY_ENIONLY)
290   if (sc->is_adaptec) {
291     adp_get_macaddr(scp);
292     sc->en_busreset = adp_busreset;
293     adp_busreset(sc);
294   }
295 #endif
296
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));
302   }
303 #endif
304
305   /*
306    * done PCI specific stuff
307    */
308
309   en_attach(sc);
310
311 }
312
313 static void
314 en_pci_shutdown(
315         void *sc,
316         int howto)
317 {
318     struct en_pci_softc *psc = (struct en_pci_softc *)sc;
319     
320     en_reset(&psc->esc);
321     DELAY(10);
322 }
323
324 #if !defined(MIDWAY_ENIONLY)
325
326 #if defined(sparc) || defined(__FreeBSD__)
327 #define bus_space_read_1(t, h, o) \
328                 ((void)t, (*(volatile u_int8_t *)((h) + (o))))
329 #endif
330
331 static void 
332 adp_get_macaddr(scp)
333      struct en_pci_softc *scp;
334 {
335   struct en_softc * sc = (struct en_softc *)scp;
336   int lcv;
337
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);
341 }
342
343 #endif /* MIDWAY_ENIONLY */
344
345 #if !defined(MIDWAY_ADPONLY)
346
347 /*
348  * Read station (MAC) address from serial EEPROM.
349  * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
350  */
351 #define EN_PROM_MAGIC  0x0c
352 #define EN_PROM_DATA   0x02
353 #define EN_PROM_CLK    0x01
354 #define EN_ESI         64
355
356 static void 
357 eni_get_macaddr(scp)
358      struct en_pci_softc *scp;
359 {
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;
364   u_int8_t tmp;
365   
366   t_data = pci_conf_read(id, EN_TONGA) & 0xffffff00;
367
368   data =  EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
369   pci_conf_write(id, EN_TONGA, data);
370
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);
391     }
392     /* get ack */
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);
403
404     tmp = 0;
405
406     for ( j = 7 ; j >= 0 ; j --){
407       tmp <<= 1;
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);
418     }
419     /* get ack */
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);
430
431     sc->macaddr[i] = tmp;
432   }
433   /* stop operation */
434   data &=  ~EN_PROM_DATA;
435   pci_conf_write(id, EN_TONGA, data);
436   data |=  EN_PROM_CLK;
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);
441 }
442
443 #endif /* !MIDWAY_ADPONLY */