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