if_hme_pci.c revision 133599
1/*-
2 * Copyright (c) 2000 Matthew R. Green
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	from: NetBSD: if_hme_pci.c,v 1.7 2001/10/05 17:49:43 thorpej Exp
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/dev/hme/if_hme_pci.c 133599 2004-08-12 20:37:02Z marius $");
33
34/*
35 * PCI front-end device driver for the HME ethernet device.
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/bus.h>
41#include <sys/kernel.h>
42#include <sys/module.h>
43#include <sys/resource.h>
44#include <sys/socket.h>
45
46#include <machine/bus.h>
47#include <dev/ofw/openfirm.h>
48#include <machine/ofw_machdep.h>
49#include <machine/resource.h>
50
51#include <sys/rman.h>
52
53#include <net/ethernet.h>
54#include <net/if.h>
55#include <net/if_arp.h>
56#include <net/if_dl.h>
57#include <net/if_media.h>
58
59#include <dev/mii/mii.h>
60#include <dev/mii/miivar.h>
61
62#include <dev/pci/pcivar.h>
63#include <dev/pci/pcireg.h>
64
65#include <dev/hme/if_hmereg.h>
66#include <dev/hme/if_hmevar.h>
67
68#include "miibus_if.h"
69
70struct hme_pci_softc {
71	struct	hme_softc	hsc_hme;	/* HME device */
72	struct	resource	*hsc_sres;
73	int			hsc_srid;
74	struct	resource	*hsc_ires;
75	int			hsc_irid;
76	bus_space_tag_t		hsc_memt;
77	bus_space_handle_t	hsc_memh;
78	void			*hsc_ih;
79};
80
81static int hme_pci_probe(device_t);
82static int hme_pci_attach(device_t);
83static int hme_pci_detach(device_t);
84static int hme_pci_suspend(device_t);
85static int hme_pci_resume(device_t);
86
87static device_method_t hme_pci_methods[] = {
88	/* Device interface */
89	DEVMETHOD(device_probe,		hme_pci_probe),
90	DEVMETHOD(device_attach,	hme_pci_attach),
91	DEVMETHOD(device_detach,	hme_pci_detach),
92	DEVMETHOD(device_suspend,	hme_pci_suspend),
93	DEVMETHOD(device_resume,	hme_pci_resume),
94	/* Can just use the suspend method here. */
95	DEVMETHOD(device_shutdown,	hme_pci_suspend),
96
97	/* bus interface */
98	DEVMETHOD(bus_print_child,	bus_generic_print_child),
99	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
100
101	/* MII interface */
102	DEVMETHOD(miibus_readreg,	hme_mii_readreg),
103	DEVMETHOD(miibus_writereg,	hme_mii_writereg),
104	DEVMETHOD(miibus_statchg,	hme_mii_statchg),
105
106	{ 0, 0 }
107};
108
109static driver_t hme_pci_driver = {
110	"hme",
111	hme_pci_methods,
112	sizeof(struct hme_pci_softc)
113};
114
115DRIVER_MODULE(hme, pci, hme_pci_driver, hme_devclass, 0, 0);
116MODULE_DEPEND(hme, pci, 1, 1, 1);
117MODULE_DEPEND(hme, ether, 1, 1, 1);
118
119int
120hme_pci_probe(device_t dev)
121{
122
123	if (pci_get_vendor(dev) == 0x108e &&
124	    pci_get_device(dev) ==  0x1001) {
125		device_set_desc(dev, "Sun HME 10/100 Ethernet");
126		return (0);
127	}
128	return (ENXIO);
129}
130
131int
132hme_pci_attach(device_t dev)
133{
134	struct hme_pci_softc *hsc = device_get_softc(dev);
135	struct hme_softc *sc = &hsc->hsc_hme;
136	int error = 0;
137
138	pci_enable_busmaster(dev);
139	/*
140	 * Some Sun HMEs do have their intpin register bogusly set to 0,
141	 * although it should be 1. correct that.
142	 */
143	if (pci_get_intpin(dev) == 0)
144		pci_set_intpin(dev, 1);
145
146	sc->sc_pci = 1;
147	sc->sc_dev = dev;
148
149	/*
150	 * Map five register banks:
151	 *
152	 *	bank 0: HME SEB registers:	+0x0000
153	 *	bank 1: HME ETX registers:	+0x2000
154	 *	bank 2: HME ERX registers:	+0x4000
155	 *	bank 3: HME MAC registers:	+0x6000
156	 *	bank 4: HME MIF registers:	+0x7000
157	 *
158	 */
159	hsc->hsc_srid = PCI_HME_BASEADDR;
160	hsc->hsc_sres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
161	    &hsc->hsc_srid, RF_ACTIVE);
162	if (hsc->hsc_sres == NULL) {
163		device_printf(dev, "could not map device registers\n");
164		return (ENXIO);
165	}
166	hsc->hsc_irid = 0;
167	hsc->hsc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
168	    &hsc->hsc_irid, RF_SHAREABLE | RF_ACTIVE);
169	if (hsc->hsc_ires == NULL) {
170		device_printf(dev, "could not allocate interrupt\n");
171		error = ENXIO;
172		goto fail_sres;
173	}
174	hsc->hsc_memt = rman_get_bustag(hsc->hsc_sres);
175	hsc->hsc_memh = rman_get_bushandle(hsc->hsc_sres);
176	sc->sc_sebt = sc->sc_etxt = sc->sc_erxt = sc->sc_mact = sc->sc_mift =
177	    hsc->hsc_memt;
178	bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x0000, 0x1000,
179	    &sc->sc_sebh);
180	bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x2000, 0x1000,
181	    &sc->sc_etxh);
182	bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x4000, 0x1000,
183	    &sc->sc_erxh);
184	bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x6000, 0x1000,
185	    &sc->sc_mach);
186	bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x7000, 0x1000,
187	    &sc->sc_mifh);
188
189	OF_getetheraddr(dev, sc->sc_arpcom.ac_enaddr);
190
191	sc->sc_burst = 64;	/* XXX */
192
193	/*
194	 * call the main configure
195	 */
196	if ((error = hme_config(sc)) != 0) {
197		device_printf(dev, "could not be configured\n");
198		goto fail_ires;
199	}
200
201	if ((error = bus_setup_intr(dev, hsc->hsc_ires, INTR_TYPE_NET, hme_intr,
202	     sc, &hsc->hsc_ih)) != 0) {
203		device_printf(dev, "couldn't establish interrupt\n");
204		hme_detach(sc);
205		goto fail_ires;
206	}
207	return (0);
208
209fail_ires:
210	bus_release_resource(dev, SYS_RES_IRQ, hsc->hsc_irid, hsc->hsc_ires);
211fail_sres:
212	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_srid, hsc->hsc_sres);
213	return (error);
214}
215
216static int
217hme_pci_detach(device_t dev)
218{
219	struct hme_pci_softc *hsc = device_get_softc(dev);
220	struct hme_softc *sc = &hsc->hsc_hme;
221
222	hme_detach(sc);
223
224	bus_teardown_intr(dev, hsc->hsc_ires, hsc->hsc_ih);
225	bus_release_resource(dev, SYS_RES_IRQ, hsc->hsc_irid, hsc->hsc_ires);
226	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_srid, hsc->hsc_sres);
227	return (0);
228}
229
230static int
231hme_pci_suspend(device_t dev)
232{
233	struct hme_pci_softc *hsc = device_get_softc(dev);
234	struct hme_softc *sc = &hsc->hsc_hme;
235
236	hme_suspend(sc);
237	return (0);
238}
239
240static int
241hme_pci_resume(device_t dev)
242{
243	struct hme_pci_softc *hsc = device_get_softc(dev);
244	struct hme_softc *sc = &hsc->hsc_hme;
245
246	hme_resume(sc);
247	return (0);
248}
249