Initial import from FreeBSD RELENG_4:
[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 "en.h"
49
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #if 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 <pci/pcivar.h>
64 #include <pci/pcireg.h>
65
66 #include <dev/en/midwayreg.h>
67 #include <dev/en/midwayvar.h>
68
69
70 /*
71  * prototypes
72  */
73
74 static  void en_pci_attach __P((pcici_t, int));
75 static  const char *en_pci_probe __P((pcici_t, pcidi_t));
76 static void en_pci_shutdown __P((void *, int));
77
78 /*
79  * local structures
80  */
81
82 struct en_pci_softc {
83   /* bus independent stuff */
84   struct en_softc esc;          /* includes "device" structure */
85
86   /* PCI bus glue */
87   void *sc_ih;                  /* interrupt handle */
88   pci_chipset_tag_t en_pc;      /* for PCI calls */
89   pcici_t en_confid;            /* config id */
90 };
91
92 #if !defined(MIDWAY_ENIONLY)
93 static  void eni_get_macaddr __P((struct en_pci_softc *));
94 #endif
95 #if !defined(MIDWAY_ADPONLY)
96 static  void adp_get_macaddr __P((struct en_pci_softc *));
97 #endif
98
99 /*
100  * pointers to softcs (we alloc)
101  */
102
103 static struct en_pci_softc *enpcis[NEN] = {0};
104 extern struct cfdriver en_cd;
105
106 /*
107  * autoconfig structures
108  */
109
110 static u_long en_pci_count;
111
112 static struct pci_device endevice = {
113         "en",
114         en_pci_probe,
115         en_pci_attach,
116         &en_pci_count,
117         NULL,
118 };  
119
120 COMPAT_PCI_DRIVER (en, endevice);
121
122 /*
123  * local defines (PCI specific stuff)
124  */
125
126 /* 
127  * address of config base memory address register in PCI config space
128  * (this is card specific)
129  */
130         
131 #define PCI_CBMA        0x10
132
133 /*
134  * tonga (pci bridge).   ENI cards only!
135  */
136
137 #define EN_TONGA        0x60            /* PCI config addr of tonga reg */
138
139 #define TONGA_SWAP_DMA  0x80            /* endian swap control */
140 #define TONGA_SWAP_BYTE 0x40
141 #define TONGA_SWAP_WORD 0x20
142
143 /*
144  * adaptec pci bridge.   ADP cards only!
145  */
146
147 #define ADP_PCIREG      0x050040        /* PCI control register */
148
149 #define ADP_PCIREG_RESET        0x1     /* reset card */
150 #define ADP_PCIREG_IENABLE      0x2     /* interrupt enable */
151 #define ADP_PCIREG_SWAP_WORD    0x4     /* swap byte on slave access */
152 #define ADP_PCIREG_SWAP_DMA     0x8     /* swap byte on DMA */
153
154 #define PCI_VENDOR_EFFICIENTNETS 0x111a                 /* Efficent Networks */
155 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PF 0x0000       /* ENI-155P ATM */
156 #define PCI_PRODUCT_EFFICIENTNETS_ENI155PA 0x0002       /* ENI-155P ATM */
157 #define PCI_VENDOR_ADP 0x9004                           /* adaptec */
158 #define PCI_PRODUCT_ADP_AIC5900 0x5900
159 #define PCI_PRODUCT_ADP_AIC5905 0x5905
160 #define PCI_VENDOR(x)           ((x) & 0xFFFF)
161 #define PCI_CHIPID(x)           (((x) >> 16) & 0xFFFF)
162
163 #if !defined(MIDWAY_ENIONLY)
164
165 static void adp_busreset __P((void *));
166
167 /*
168  * bus specific reset function [ADP only!]
169  */
170
171 static void adp_busreset(v)
172
173 void *v;
174
175 {
176   struct en_softc *sc = (struct en_softc *) v;
177   u_int32_t dummy;
178
179   bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, ADP_PCIREG_RESET);
180   DELAY(1000);  /* let it reset */
181   dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
182   bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, 
183                 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA|ADP_PCIREG_IENABLE));
184   dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
185   if ((dummy & (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA)) !=
186                 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA))
187     printf("adp_busreset: Adaptec ATM did NOT reset!\n");
188 }
189 #endif
190
191 /***********************************************************************/
192
193 /*
194  * autoconfig stuff
195  */
196
197 static const char *en_pci_probe(config_id, device_id)
198
199 pcici_t config_id;
200 pcidi_t device_id;
201
202 {
203 #if !defined(MIDWAY_ADPONLY)
204   if (PCI_VENDOR(device_id) == PCI_VENDOR_EFFICIENTNETS && 
205       (PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PF ||
206        PCI_CHIPID(device_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PA))
207     return "Efficient Networks ENI-155p";
208 #endif
209
210 #if !defined(MIDWAY_ENIONLY)
211   if (PCI_VENDOR(device_id) == PCI_VENDOR_ADP && 
212       (PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5900 ||
213        PCI_CHIPID(device_id) == PCI_PRODUCT_ADP_AIC5905))
214     return "Adaptec 155 ATM";
215 #endif
216
217   return 0;
218 }
219
220 static void en_pci_attach(config_id, unit)
221
222 pcici_t config_id;
223 int unit;
224
225 {
226   struct en_softc *sc;
227   struct en_pci_softc *scp;
228   pcidi_t device_id;
229   int retval;
230   vm_offset_t pa;
231
232   if (unit >= NEN) {
233     printf("en%d: not configured; kernel is built for only %d device%s.\n",
234         unit, NEN, NEN == 1 ? "" : "s");
235     return;
236   }
237
238   scp = (struct en_pci_softc *) malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT);
239   if (scp == NULL)
240     return;
241   bzero(scp, sizeof(*scp));             /* zero */
242   sc = &scp->esc;
243
244   retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &sc->en_base, &pa);
245
246   if (!retval) {
247     free((caddr_t) scp, M_DEVBUF);
248     return;
249   }
250   enpcis[unit] = scp;                   /* lock it in */
251   en_cd.cd_devs[unit] = sc;             /* fake a cfdriver structure */
252   en_cd.cd_ndevs = NEN;
253   snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname), "en%d", unit);
254   sc->enif.if_unit = unit;
255   sc->enif.if_name = "en";
256   scp->en_confid = config_id;
257
258   /*
259    * figure out if we are an adaptec card or not.
260    * XXX: why do we have to re-read PC_ID_REG when en_pci_probe already
261    * had that info?
262    */
263
264   device_id = pci_conf_read(config_id, PCI_ID_REG);
265   sc->is_adaptec = (PCI_VENDOR(device_id) == PCI_VENDOR_ADP) ? 1 : 0;
266   
267   /*
268    * Add shutdown hook so that DMA is disabled prior to reboot. Not
269    * doing so could allow DMA to corrupt kernel memory during the
270    * reboot before the driver initializes.
271    */
272   EVENTHANDLER_REGISTER(shutdown_post_sync, en_pci_shutdown, scp,
273                         SHUTDOWN_PRI_DEFAULT);
274
275   if (!pci_map_int(config_id, en_intr, (void *) sc, &net_imask)) {
276     printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
277     return;
278   }
279   sc->ipl = 1; /* XXX */
280
281   /*
282    * set up pci bridge
283    */
284
285 #if !defined(MIDWAY_ENIONLY)
286   if (sc->is_adaptec) {
287     adp_get_macaddr(scp);
288     sc->en_busreset = adp_busreset;
289     adp_busreset(sc);
290   }
291 #endif
292
293 #if !defined(MIDWAY_ADPONLY)
294   if (!sc->is_adaptec) {
295     eni_get_macaddr(scp);
296     sc->en_busreset = NULL;
297     pci_conf_write(config_id, EN_TONGA, (TONGA_SWAP_DMA|TONGA_SWAP_WORD));
298   }
299 #endif
300
301   /*
302    * done PCI specific stuff
303    */
304
305   en_attach(sc);
306
307 }
308
309 static void
310 en_pci_shutdown(
311         void *sc,
312         int howto)
313 {
314     struct en_pci_softc *psc = (struct en_pci_softc *)sc;
315     
316     en_reset(&psc->esc);
317     DELAY(10);
318 }
319
320 #if !defined(MIDWAY_ENIONLY)
321
322 #if defined(sparc) || defined(__FreeBSD__)
323 #define bus_space_read_1(t, h, o) \
324                 ((void)t, (*(volatile u_int8_t *)((h) + (o))))
325 #endif
326
327 static void 
328 adp_get_macaddr(scp)
329      struct en_pci_softc *scp;
330 {
331   struct en_softc * sc = (struct en_softc *)scp;
332   int lcv;
333
334   for (lcv = 0; lcv < sizeof(sc->macaddr); lcv++)
335     sc->macaddr[lcv] = bus_space_read_1(sc->en_memt, sc->en_base,
336                                         MID_ADPMACOFF + lcv);
337 }
338
339 #endif /* MIDWAY_ENIONLY */
340
341 #if !defined(MIDWAY_ADPONLY)
342
343 /*
344  * Read station (MAC) address from serial EEPROM.
345  * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
346  */
347 #define EN_PROM_MAGIC  0x0c
348 #define EN_PROM_DATA   0x02
349 #define EN_PROM_CLK    0x01
350 #define EN_ESI         64
351
352 static void 
353 eni_get_macaddr(scp)
354      struct en_pci_softc *scp;
355 {
356   struct en_softc * sc = (struct en_softc *)scp;
357   pcici_t id = scp->en_confid;
358   int i, j, address, status;
359   u_int32_t data, t_data;
360   u_int8_t tmp;
361   
362   t_data = pci_conf_read(id, EN_TONGA) & 0xffffff00;
363
364   data =  EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
365   pci_conf_write(id, EN_TONGA, data);
366
367   for (i = 0; i < sizeof(sc->macaddr); i ++){
368     /* start operation */
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 &= ~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     /* send address with serial line */
378     address = ((i + EN_ESI) << 1) + 1;
379     for ( j = 7 ; j >= 0 ; j --){
380       data = (address >> j) & 1 ? data | EN_PROM_DATA :
381       data & ~EN_PROM_DATA;
382       pci_conf_write(id, EN_TONGA, data);
383       data |= EN_PROM_CLK ;
384       pci_conf_write(id, EN_TONGA, data);
385       data &= ~EN_PROM_CLK ;
386       pci_conf_write(id, EN_TONGA, data);
387     }
388     /* get ack */
389     data |= EN_PROM_DATA ;
390     pci_conf_write(id, EN_TONGA, data);
391     data |= EN_PROM_CLK ;
392     pci_conf_write(id, EN_TONGA, data);
393     data = pci_conf_read(id, EN_TONGA);
394     status = data & EN_PROM_DATA;
395     data &= ~EN_PROM_CLK ;
396     pci_conf_write(id, EN_TONGA, data);
397     data |= EN_PROM_DATA ;
398     pci_conf_write(id, EN_TONGA, data);
399
400     tmp = 0;
401
402     for ( j = 7 ; j >= 0 ; j --){
403       tmp <<= 1;
404       data |= EN_PROM_DATA ;
405       pci_conf_write(id, EN_TONGA, data);
406       data |= EN_PROM_CLK ;
407       pci_conf_write(id, EN_TONGA, data);
408       data = pci_conf_read(id, EN_TONGA);
409       if(data & EN_PROM_DATA) tmp |= 1;
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     }
415     /* get ack */
416     data |= EN_PROM_DATA ;
417     pci_conf_write(id, EN_TONGA, data);
418     data |= EN_PROM_CLK ;
419     pci_conf_write(id, EN_TONGA, data);
420     data = pci_conf_read(id, EN_TONGA);
421     status = data & EN_PROM_DATA;
422     data &= ~EN_PROM_CLK ;
423     pci_conf_write(id, EN_TONGA, data);
424     data |= EN_PROM_DATA ;
425     pci_conf_write(id, EN_TONGA, data);
426
427     sc->macaddr[i] = tmp;
428   }
429   /* stop operation */
430   data &=  ~EN_PROM_DATA;
431   pci_conf_write(id, EN_TONGA, data);
432   data |=  EN_PROM_CLK;
433   pci_conf_write(id, EN_TONGA, data);
434   data |=  EN_PROM_DATA;
435   pci_conf_write(id, EN_TONGA, data);
436   pci_conf_write(id, EN_TONGA, t_data);
437 }
438
439 #endif /* !MIDWAY_ADPONLY */