]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/le/if_le_pci.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / dev / le / if_le_pci.c
1 /*      $NetBSD: if_le_pci.c,v 1.43 2005/12/11 12:22:49 christos Exp $  */
2
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the NetBSD
22  *      Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 /*-
41  * Copyright (c) 1992, 1993
42  *      The Regents of the University of California.  All rights reserved.
43  *
44  * This code is derived from software contributed to Berkeley by
45  * Ralph Campbell and Rick Macklem.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *      @(#)if_le.c     8.2 (Berkeley) 11/16/93
72  */
73
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/bus.h>
80 #include <sys/endian.h>
81 #include <sys/kernel.h>
82 #include <sys/lock.h>
83 #include <sys/module.h>
84 #include <sys/mutex.h>
85 #include <sys/resource.h>
86 #include <sys/rman.h>
87 #include <sys/socket.h>
88
89 #include <net/ethernet.h>
90 #include <net/if.h>
91 #include <net/if_media.h>
92
93 #include <machine/bus.h>
94 #include <machine/resource.h>
95
96 #include <dev/pci/pcireg.h>
97 #include <dev/pci/pcivar.h>
98
99 #include <dev/le/lancereg.h>
100 #include <dev/le/lancevar.h>
101 #include <dev/le/am79900var.h>
102
103 #define AMD_VENDOR      0x1022
104 #define AMD_PCNET_PCI   0x2000
105 #define AMD_PCNET_HOME  0x2001
106 #define PCNET_MEMSIZE   (32*1024)
107 #define PCNET_PCI_RDP   0x10
108 #define PCNET_PCI_RAP   0x12
109 #define PCNET_PCI_BDP   0x16
110
111 struct le_pci_softc {
112         struct am79900_softc    sc_am79900;     /* glue to MI code */
113
114         struct resource         *sc_rres;
115
116         struct resource         *sc_ires;
117         void                    *sc_ih;
118
119         bus_dma_tag_t           sc_pdmat;
120         bus_dma_tag_t           sc_dmat;
121         bus_dmamap_t            sc_dmam;
122 };
123
124 static device_probe_t le_pci_probe;
125 static device_attach_t le_pci_attach;
126 static device_detach_t le_pci_detach;
127 static device_resume_t le_pci_resume;
128 static device_suspend_t le_pci_suspend;
129
130 static device_method_t le_pci_methods[] = {
131         /* Device interface */
132         DEVMETHOD(device_probe,         le_pci_probe),
133         DEVMETHOD(device_attach,        le_pci_attach),
134         DEVMETHOD(device_detach,        le_pci_detach),
135         /* We can just use the suspend method here. */
136         DEVMETHOD(device_shutdown,      le_pci_suspend),
137         DEVMETHOD(device_suspend,       le_pci_suspend),
138         DEVMETHOD(device_resume,        le_pci_resume),
139
140         { 0, 0 }
141 };
142
143 DEFINE_CLASS_0(le, le_pci_driver, le_pci_methods, sizeof(struct le_pci_softc));
144 DRIVER_MODULE(le, pci, le_pci_driver, le_devclass, 0, 0);
145 MODULE_DEPEND(le, ether, 1, 1, 1);
146
147 static const int le_home_supmedia[] = {
148         IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, 0)
149 };
150
151 static const int le_pci_supmedia[] = {
152         IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
153         IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, IFM_FDX, 0),
154         IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
155         IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
156         IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0),
157         IFM_MAKEWORD(IFM_ETHER, IFM_10_5, IFM_FDX, 0)
158 };
159
160 static void le_pci_wrbcr(struct lance_softc *, uint16_t, uint16_t);
161 static uint16_t le_pci_rdbcr(struct lance_softc *, uint16_t);
162 static void le_pci_wrcsr(struct lance_softc *, uint16_t, uint16_t);
163 static uint16_t le_pci_rdcsr(struct lance_softc *, uint16_t);
164 static int le_pci_mediachange(struct lance_softc *);
165 static void le_pci_hwreset(struct lance_softc *);
166 static bus_dmamap_callback_t le_pci_dma_callback;
167
168 static void
169 le_pci_wrbcr(struct lance_softc *sc, uint16_t port, uint16_t val)
170 {
171         struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
172
173         bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
174         bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
175         bus_write_2(lesc->sc_rres, PCNET_PCI_BDP, val);
176 }
177
178 static uint16_t
179 le_pci_rdbcr(struct lance_softc *sc, uint16_t port)
180 {
181         struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
182
183         bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
184         bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
185         return (bus_read_2(lesc->sc_rres, PCNET_PCI_BDP));
186 }
187
188 static void
189 le_pci_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
190 {
191         struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
192
193         bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
194         bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
195         bus_write_2(lesc->sc_rres, PCNET_PCI_RDP, val);
196 }
197
198 static uint16_t
199 le_pci_rdcsr(struct lance_softc *sc, uint16_t port)
200 {
201         struct le_pci_softc *lesc = (struct le_pci_softc *)sc;
202
203         bus_write_2(lesc->sc_rres, PCNET_PCI_RAP, port);
204         bus_barrier(lesc->sc_rres, PCNET_PCI_RAP, 2, BUS_SPACE_BARRIER_WRITE);
205         return (bus_read_2(lesc->sc_rres, PCNET_PCI_RDP));
206 }
207
208 static int
209 le_pci_mediachange(struct lance_softc *sc)
210 {
211         struct ifmedia *ifm = &sc->sc_media;
212         uint16_t reg;
213
214         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
215                 return (EINVAL);
216
217         if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
218                 le_pci_wrbcr(sc, LE_BCR49,
219                     (le_pci_rdbcr(sc, LE_BCR49) & ~LE_B49_PHYSEL) | 0x1);
220         else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO)
221                 le_pci_wrbcr(sc, LE_BCR2,
222                     le_pci_rdbcr(sc, LE_BCR2) | LE_B2_ASEL);
223         else {
224                 le_pci_wrbcr(sc, LE_BCR2,
225                     le_pci_rdbcr(sc, LE_BCR2) & ~LE_B2_ASEL);
226
227                 reg = le_pci_rdcsr(sc, LE_CSR15);
228                 reg &= ~LE_C15_PORTSEL(LE_PORTSEL_MASK);
229                 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T)
230                         reg |= LE_C15_PORTSEL(LE_PORTSEL_10T);
231                 else
232                         reg |= LE_C15_PORTSEL(LE_PORTSEL_AUI);
233                 le_pci_wrcsr(sc, LE_CSR15, reg);
234         }
235
236         reg = le_pci_rdbcr(sc, LE_BCR9);
237         if (IFM_OPTIONS(ifm->ifm_media) & IFM_FDX) {
238                 reg |= LE_B9_FDEN;
239                 /*
240                  * Allow FDX on AUI only if explicitly chosen,
241                  * not in autoselect mode.
242                  */
243                 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_5)
244                         reg |= LE_B9_AUIFD;
245                 else
246                         reg &= ~LE_B9_AUIFD;
247         } else
248                 reg &= ~LE_B9_FDEN;
249         le_pci_wrbcr(sc, LE_BCR9, reg);
250
251         return (0);
252 }
253
254 static void
255 le_pci_hwreset(struct lance_softc *sc)
256 {
257
258         /*
259          * Chip is stopped. Set software style to PCnet-PCI (32-bit).
260          * Actually, am79900.c implements ILACC support (hence its
261          * name) but unfortunately VMware does not. As far as this
262          * driver is concerned that should not make a difference
263          * though, as the settings used have the same meaning for
264          * both, ILACC and PCnet-PCI (note that there would be a
265          * difference for the ADD_FCS/NO_FCS bit if used).
266          */
267         le_pci_wrbcr(sc, LE_BCR20, LE_B20_SSTYLE_PCNETPCI2);
268 }
269
270 static void
271 le_pci_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
272 {
273         struct lance_softc *sc = (struct lance_softc *)xsc;
274
275         if (error != 0)
276                 return;
277         KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__));
278         sc->sc_addr = segs[0].ds_addr;
279 }
280
281 static int
282 le_pci_probe(device_t dev)
283 {
284
285         if (pci_get_vendor(dev) != AMD_VENDOR)
286                 return (ENXIO);
287
288         switch (pci_get_device(dev)) {
289         case AMD_PCNET_PCI:
290                 device_set_desc(dev, "AMD PCnet-PCI");
291                 /* Let pcn(4) win. */
292                 return (BUS_PROBE_LOW_PRIORITY);
293         case AMD_PCNET_HOME:
294                 device_set_desc(dev, "AMD PCnet-Home");
295                 /* Let pcn(4) win. */
296                 return (BUS_PROBE_LOW_PRIORITY);
297         default:
298                 return (ENXIO);
299         }
300 }
301
302 static int
303 le_pci_attach(device_t dev)
304 {
305         struct le_pci_softc *lesc;
306         struct lance_softc *sc;
307         int error, i;
308
309         lesc = device_get_softc(dev);
310         sc = &lesc->sc_am79900.lsc;
311
312         LE_LOCK_INIT(sc, device_get_nameunit(dev));
313
314         pci_enable_busmaster(dev);
315
316         i = PCIR_BAR(0);
317         lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
318             &i, RF_ACTIVE);
319         if (lesc->sc_rres == NULL) {
320                 device_printf(dev, "cannot allocate registers\n");
321                 error = ENXIO;
322                 goto fail_mtx;
323         }
324
325         i = 0;
326         if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
327             &i, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
328                 device_printf(dev, "cannot allocate interrupt\n");
329                 error = ENXIO;
330                 goto fail_rres;
331         }
332
333         error = bus_dma_tag_create(
334             bus_get_dma_tag(dev),       /* parent */
335             1, 0,                       /* alignment, boundary */
336             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
337             BUS_SPACE_MAXADDR,          /* highaddr */
338             NULL, NULL,                 /* filter, filterarg */
339             BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
340             0,                          /* nsegments */
341             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
342             0,                          /* flags */
343             NULL, NULL,                 /* lockfunc, lockarg */
344             &lesc->sc_pdmat);
345         if (error != 0) {
346                 device_printf(dev, "cannot allocate parent DMA tag\n");
347                 goto fail_ires;
348         }
349
350         sc->sc_memsize = PCNET_MEMSIZE;
351         /*
352          * For Am79C970A, Am79C971 and Am79C978 the init block must be 2-byte
353          * aligned and the ring descriptors must be 16-byte aligned when using
354          * a 32-bit software style.
355          */
356         error = bus_dma_tag_create(
357             lesc->sc_pdmat,             /* parent */
358             16, 0,                      /* alignment, boundary */
359             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
360             BUS_SPACE_MAXADDR,          /* highaddr */
361             NULL, NULL,                 /* filter, filterarg */
362             sc->sc_memsize,             /* maxsize */
363             1,                          /* nsegments */
364             sc->sc_memsize,             /* maxsegsize */
365             0,                          /* flags */
366             NULL, NULL,                 /* lockfunc, lockarg */
367             &lesc->sc_dmat);
368         if (error != 0) {
369                 device_printf(dev, "cannot allocate buffer DMA tag\n");
370                 goto fail_pdtag;
371         }
372
373         error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem,
374             BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam);
375         if (error != 0) {
376                 device_printf(dev, "cannot allocate DMA buffer memory\n");
377                 goto fail_dtag;
378         }
379
380         sc->sc_addr = 0;
381         error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem,
382             sc->sc_memsize, le_pci_dma_callback, sc, 0);
383         if (error != 0 || sc->sc_addr == 0) {
384                 device_printf(dev, "cannot load DMA buffer map\n");
385                 goto fail_dmem;
386         }
387
388         sc->sc_flags = LE_BSWAP;
389         sc->sc_conf3 = 0;
390
391         sc->sc_mediastatus = NULL;
392         switch (pci_get_device(dev)) {
393         case AMD_PCNET_HOME:
394                 sc->sc_mediachange = le_pci_mediachange;
395                 sc->sc_supmedia = le_home_supmedia;
396                 sc->sc_nsupmedia = sizeof(le_home_supmedia) / sizeof(int);
397                 sc->sc_defaultmedia = le_home_supmedia[0];
398                 break;
399         default:
400                 sc->sc_mediachange = le_pci_mediachange;
401                 sc->sc_supmedia = le_pci_supmedia;
402                 sc->sc_nsupmedia = sizeof(le_pci_supmedia) / sizeof(int);
403                 sc->sc_defaultmedia = le_pci_supmedia[0];
404         }
405
406         /*
407          * Extract the physical MAC address from the ROM.
408          */
409         bus_read_region_1(lesc->sc_rres, 0, sc->sc_enaddr,
410             sizeof(sc->sc_enaddr));
411
412         sc->sc_copytodesc = lance_copytobuf_contig;
413         sc->sc_copyfromdesc = lance_copyfrombuf_contig;
414         sc->sc_copytobuf = lance_copytobuf_contig;
415         sc->sc_copyfrombuf = lance_copyfrombuf_contig;
416         sc->sc_zerobuf = lance_zerobuf_contig;
417
418         sc->sc_rdcsr = le_pci_rdcsr;
419         sc->sc_wrcsr = le_pci_wrcsr;
420         sc->sc_hwreset = le_pci_hwreset;
421         sc->sc_hwinit = NULL;
422         sc->sc_hwintr = NULL;
423         sc->sc_nocarrier = NULL;
424
425         error = am79900_config(&lesc->sc_am79900, device_get_name(dev),
426             device_get_unit(dev));
427         if (error != 0) {
428                 device_printf(dev, "cannot attach Am79900\n");
429                 goto fail_dmap;
430         }
431
432         error = bus_setup_intr(dev, lesc->sc_ires, INTR_TYPE_NET | INTR_MPSAFE,
433             NULL, am79900_intr, sc, &lesc->sc_ih);
434         if (error != 0) {
435                 device_printf(dev, "cannot set up interrupt\n");
436                 goto fail_am79900;
437         }
438
439         return (0);
440
441  fail_am79900:
442         am79900_detach(&lesc->sc_am79900);
443  fail_dmap:
444         bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
445  fail_dmem:
446         bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
447  fail_dtag:
448         bus_dma_tag_destroy(lesc->sc_dmat);
449  fail_pdtag:
450         bus_dma_tag_destroy(lesc->sc_pdmat);
451  fail_ires:
452         bus_release_resource(dev, SYS_RES_IRQ,
453             rman_get_rid(lesc->sc_ires), lesc->sc_ires);
454  fail_rres:
455         bus_release_resource(dev, SYS_RES_IOPORT,
456             rman_get_rid(lesc->sc_rres), lesc->sc_rres);
457  fail_mtx:
458         LE_LOCK_DESTROY(sc);
459         return (error);
460 }
461
462 static int
463 le_pci_detach(device_t dev)
464 {
465         struct le_pci_softc *lesc;
466         struct lance_softc *sc;
467
468         lesc = device_get_softc(dev);
469         sc = &lesc->sc_am79900.lsc;
470
471         bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih);
472         am79900_detach(&lesc->sc_am79900);
473         bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam);
474         bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam);
475         bus_dma_tag_destroy(lesc->sc_dmat);
476         bus_dma_tag_destroy(lesc->sc_pdmat);
477         bus_release_resource(dev, SYS_RES_IRQ,
478             rman_get_rid(lesc->sc_ires), lesc->sc_ires);
479         bus_release_resource(dev, SYS_RES_IOPORT,
480             rman_get_rid(lesc->sc_rres), lesc->sc_rres);
481         LE_LOCK_DESTROY(sc);
482
483         return (0);
484 }
485
486 static int
487 le_pci_suspend(device_t dev)
488 {
489         struct le_pci_softc *lesc;
490
491         lesc = device_get_softc(dev);
492
493         lance_suspend(&lesc->sc_am79900.lsc);
494
495         return (0);
496 }
497
498 static int
499 le_pci_resume(device_t dev)
500 {
501         struct le_pci_softc *lesc;
502
503         lesc = device_get_softc(dev);
504
505         lance_resume(&lesc->sc_am79900.lsc);
506
507         return (0);
508 }