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