2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Matthew R. Green
5 * Copyright (c) 2007 Marius Strobl <marius@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * from: NetBSD: if_hme_pci.c,v 1.14 2004/03/17 08:58:23 martin Exp
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * PCI front-end device driver for the HME ethernet device.
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
52 #include <machine/bus.h>
53 #if defined(__powerpc__)
54 #include <dev/ofw/openfirm.h>
55 #include <machine/ofw_machdep.h>
57 #include <machine/resource.h>
61 #include <net/ethernet.h>
63 #include <net/if_arp.h>
64 #include <net/if_dl.h>
65 #include <net/if_media.h>
67 #include <dev/mii/mii.h>
68 #include <dev/mii/miivar.h>
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pcireg.h>
73 #include <dev/hme/if_hmereg.h>
74 #include <dev/hme/if_hmevar.h>
76 #include "miibus_if.h"
78 struct hme_pci_softc {
79 struct hme_softc hsc_hme; /* HME device */
80 struct resource *hsc_sres;
81 struct resource *hsc_ires;
85 static int hme_pci_probe(device_t);
86 static int hme_pci_attach(device_t);
87 static int hme_pci_detach(device_t);
88 static int hme_pci_suspend(device_t);
89 static int hme_pci_resume(device_t);
91 static device_method_t hme_pci_methods[] = {
92 /* Device interface */
93 DEVMETHOD(device_probe, hme_pci_probe),
94 DEVMETHOD(device_attach, hme_pci_attach),
95 DEVMETHOD(device_detach, hme_pci_detach),
96 DEVMETHOD(device_suspend, hme_pci_suspend),
97 DEVMETHOD(device_resume, hme_pci_resume),
98 /* Can just use the suspend method here. */
99 DEVMETHOD(device_shutdown, hme_pci_suspend),
102 DEVMETHOD(miibus_readreg, hme_mii_readreg),
103 DEVMETHOD(miibus_writereg, hme_mii_writereg),
104 DEVMETHOD(miibus_statchg, hme_mii_statchg),
109 static driver_t hme_pci_driver = {
112 sizeof(struct hme_pci_softc)
115 DRIVER_MODULE(hme, pci, hme_pci_driver, hme_devclass, 0, 0);
116 MODULE_DEPEND(hme, pci, 1, 1, 1);
117 MODULE_DEPEND(hme, ether, 1, 1, 1);
119 #define PCI_VENDOR_SUN 0x108e
120 #define PCI_PRODUCT_SUN_EBUS 0x1000
121 #define PCI_PRODUCT_SUN_HMENETWORK 0x1001
124 hme_pci_probe(device_t dev)
127 if (pci_get_vendor(dev) == PCI_VENDOR_SUN &&
128 pci_get_device(dev) == PCI_PRODUCT_SUN_HMENETWORK) {
129 device_set_desc(dev, "Sun HME 10/100 Ethernet");
130 return (BUS_PROBE_DEFAULT);
136 hme_pci_attach(device_t dev)
138 struct hme_pci_softc *hsc;
139 struct hme_softc *sc;
140 bus_space_tag_t memt;
141 bus_space_handle_t memh;
143 #if !defined(__powerpc__)
144 device_t *children, ebus_dev;
145 struct resource *ebus_rres;
149 pci_enable_busmaster(dev);
151 * Some Sun HMEs do have their intpin register bogusly set to 0,
152 * although it should be 1. Correct that.
154 if (pci_get_intpin(dev) == 0)
155 pci_set_intpin(dev, 1);
157 hsc = device_get_softc(dev);
160 sc->sc_flags |= HME_PCI;
161 mtx_init(&sc->sc_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
165 * Map five register banks:
167 * bank 0: HME SEB registers: +0x0000
168 * bank 1: HME ETX registers: +0x2000
169 * bank 2: HME ERX registers: +0x4000
170 * bank 3: HME MAC registers: +0x6000
171 * bank 4: HME MIF registers: +0x7000
175 hsc->hsc_sres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
177 if (hsc->hsc_sres == NULL) {
178 device_printf(dev, "could not map device registers\n");
183 hsc->hsc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
184 &i, RF_SHAREABLE | RF_ACTIVE);
185 if (hsc->hsc_ires == NULL) {
186 device_printf(dev, "could not allocate interrupt\n");
190 memt = rman_get_bustag(hsc->hsc_sres);
191 memh = rman_get_bushandle(hsc->hsc_sres);
192 sc->sc_sebt = sc->sc_etxt = sc->sc_erxt = sc->sc_mact = sc->sc_mift =
194 bus_space_subregion(memt, memh, 0x0000, 0x1000, &sc->sc_sebh);
195 bus_space_subregion(memt, memh, 0x2000, 0x1000, &sc->sc_etxh);
196 bus_space_subregion(memt, memh, 0x4000, 0x1000, &sc->sc_erxh);
197 bus_space_subregion(memt, memh, 0x6000, 0x1000, &sc->sc_mach);
198 bus_space_subregion(memt, memh, 0x7000, 0x1000, &sc->sc_mifh);
200 #if defined(__powerpc__)
201 OF_getetheraddr(dev, sc->sc_enaddr);
204 * Dig out VPD (vital product data) and read NA (network address).
206 * The PCI HME is a PCIO chip, which is composed of two functions:
207 * function 0: PCI-EBus2 bridge, and
208 * function 1: HappyMeal Ethernet controller.
210 * The VPD of HME resides in the Boot PROM (PCI FCode) attached
211 * to the EBus bridge and can't be accessed via the PCI capability
213 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
214 * chapter 2 describes the data structure.
216 * We don't have a MI EBus driver since no EBus device exists
217 * (besides the FCode PROM) on add-on HME boards. The ``no driver
218 * attached'' message for function 0 therefore is what is expected.
221 #define PCI_ROMHDR_SIZE 0x1c
222 #define PCI_ROMHDR_SIG 0x00
223 #define PCI_ROMHDR_SIG_MAGIC 0xaa55 /* little endian */
224 #define PCI_ROMHDR_PTR_DATA 0x18
225 #define PCI_ROM_SIZE 0x18
226 #define PCI_ROM_SIG 0x00
227 #define PCI_ROM_SIG_MAGIC 0x52494350 /* "PCIR", endian */
229 #define PCI_ROM_VENDOR 0x04
230 #define PCI_ROM_DEVICE 0x06
231 #define PCI_ROM_PTR_VPD 0x08
232 #define PCI_VPDRES_BYTE0 0x00
233 #define PCI_VPDRES_ISLARGE(x) ((x) & 0x80)
234 #define PCI_VPDRES_LARGE_NAME(x) ((x) & 0x7f)
235 #define PCI_VPDRES_TYPE_VPD 0x10 /* large */
236 #define PCI_VPDRES_LARGE_LEN_LSB 0x01
237 #define PCI_VPDRES_LARGE_LEN_MSB 0x02
238 #define PCI_VPDRES_LARGE_DATA 0x03
239 #define PCI_VPD_SIZE 0x03
240 #define PCI_VPD_KEY0 0x00
241 #define PCI_VPD_KEY1 0x01
242 #define PCI_VPD_LEN 0x02
243 #define PCI_VPD_DATA 0x03
245 #define HME_ROM_READ_N(n, offs) bus_space_read_ ## n (memt, memh, (offs))
246 #define HME_ROM_READ_1(offs) HME_ROM_READ_N(1, (offs))
247 #define HME_ROM_READ_2(offs) HME_ROM_READ_N(2, (offs))
248 #define HME_ROM_READ_4(offs) HME_ROM_READ_N(4, (offs))
250 /* Search accompanying EBus bridge. */
251 slot = pci_get_slot(dev);
252 if (device_get_children(device_get_parent(dev), &children, &i) != 0) {
253 device_printf(dev, "could not get children\n");
258 for (j = 0; j < i; j++) {
259 if (pci_get_class(children[j]) == PCIC_BRIDGE &&
260 pci_get_vendor(children[j]) == PCI_VENDOR_SUN &&
261 pci_get_device(children[j]) == PCI_PRODUCT_SUN_EBUS &&
262 pci_get_slot(children[j]) == slot) {
263 ebus_dev = children[j];
267 if (ebus_dev == NULL) {
268 device_printf(dev, "could not find EBus bridge\n");
273 /* Map EBus bridge PROM registers. */
275 if ((ebus_rres = bus_alloc_resource_any(ebus_dev, SYS_RES_MEMORY,
276 &i, RF_ACTIVE)) == NULL) {
277 device_printf(dev, "could not map PROM registers\n");
281 memt = rman_get_bustag(ebus_rres);
282 memh = rman_get_bushandle(ebus_rres);
284 /* Read PCI Expansion ROM header. */
285 if (HME_ROM_READ_2(PCI_ROMHDR_SIG) != PCI_ROMHDR_SIG_MAGIC ||
286 (i = HME_ROM_READ_2(PCI_ROMHDR_PTR_DATA)) < PCI_ROMHDR_SIZE) {
287 device_printf(dev, "unexpected PCI Expansion ROM header\n");
292 /* Read PCI Expansion ROM data. */
293 if (HME_ROM_READ_4(i + PCI_ROM_SIG) != PCI_ROM_SIG_MAGIC ||
294 HME_ROM_READ_2(i + PCI_ROM_VENDOR) != pci_get_vendor(dev) ||
295 HME_ROM_READ_2(i + PCI_ROM_DEVICE) != pci_get_device(dev) ||
296 (j = HME_ROM_READ_2(i + PCI_ROM_PTR_VPD)) < i + PCI_ROM_SIZE) {
297 device_printf(dev, "unexpected PCI Expansion ROM data\n");
304 * SUNW,hme cards have a single large resource VPD-R tag
305 * containing one NA. SUNW,qfe cards have four large resource
306 * VPD-R tags containing one NA each (all four HME chips share
308 * The VPD used on both cards is not in PCI 2.2 standard format
309 * however. The length in the resource header is in big endian
310 * and the end tag is non-standard (0x79) and followed by an
311 * all-zero "checksum" byte. Sun calls this a "Fresh Choice
314 /* Look at the end tag to determine whether this is a VPD with 4 NAs. */
315 if (HME_ROM_READ_1(j + PCI_VPDRES_LARGE_DATA + PCI_VPD_SIZE +
316 ETHER_ADDR_LEN) != 0x79 &&
317 HME_ROM_READ_1(j + 4 * (PCI_VPDRES_LARGE_DATA + PCI_VPD_SIZE +
318 ETHER_ADDR_LEN)) == 0x79)
319 /* Use the Nth NA for the Nth HME on this SUNW,qfe. */
320 j += slot * (PCI_VPDRES_LARGE_DATA + PCI_VPD_SIZE +
322 if (PCI_VPDRES_ISLARGE(HME_ROM_READ_1(j + PCI_VPDRES_BYTE0)) == 0 ||
323 PCI_VPDRES_LARGE_NAME(HME_ROM_READ_1(j + PCI_VPDRES_BYTE0)) !=
324 PCI_VPDRES_TYPE_VPD ||
325 (HME_ROM_READ_1(j + PCI_VPDRES_LARGE_LEN_LSB) << 8 |
326 HME_ROM_READ_1(j + PCI_VPDRES_LARGE_LEN_MSB)) !=
327 PCI_VPD_SIZE + ETHER_ADDR_LEN ||
328 HME_ROM_READ_1(j + PCI_VPDRES_LARGE_DATA + PCI_VPD_KEY0) !=
330 HME_ROM_READ_1(j + PCI_VPDRES_LARGE_DATA + PCI_VPD_KEY1) !=
332 HME_ROM_READ_1(j + PCI_VPDRES_LARGE_DATA + PCI_VPD_LEN) !=
334 device_printf(dev, "unexpected PCI VPD\n");
338 bus_space_read_region_1(memt, memh, j + PCI_VPDRES_LARGE_DATA +
339 PCI_VPD_DATA, sc->sc_enaddr, ETHER_ADDR_LEN);
342 bus_release_resource(ebus_dev, SYS_RES_MEMORY,
343 rman_get_rid(ebus_rres), ebus_rres);
345 free(children, M_TEMP);
350 sc->sc_burst = 64; /* XXX */
353 * call the main configure
355 if ((error = hme_config(sc)) != 0) {
356 device_printf(dev, "could not be configured\n");
360 if ((error = bus_setup_intr(dev, hsc->hsc_ires, INTR_TYPE_NET |
361 INTR_MPSAFE, NULL, hme_intr, sc, &hsc->hsc_ih)) != 0) {
362 device_printf(dev, "couldn't establish interrupt\n");
369 bus_release_resource(dev, SYS_RES_IRQ,
370 rman_get_rid(hsc->hsc_ires), hsc->hsc_ires);
372 bus_release_resource(dev, SYS_RES_MEMORY,
373 rman_get_rid(hsc->hsc_sres), hsc->hsc_sres);
375 mtx_destroy(&sc->sc_lock);
380 hme_pci_detach(device_t dev)
382 struct hme_pci_softc *hsc;
383 struct hme_softc *sc;
385 hsc = device_get_softc(dev);
387 bus_teardown_intr(dev, hsc->hsc_ires, hsc->hsc_ih);
389 bus_release_resource(dev, SYS_RES_IRQ,
390 rman_get_rid(hsc->hsc_ires), hsc->hsc_ires);
391 bus_release_resource(dev, SYS_RES_MEMORY,
392 rman_get_rid(hsc->hsc_sres), hsc->hsc_sres);
393 mtx_destroy(&sc->sc_lock);
398 hme_pci_suspend(device_t dev)
400 struct hme_pci_softc *hsc;
402 hsc = device_get_softc(dev);
403 hme_suspend(&hsc->hsc_hme);
408 hme_pci_resume(device_t dev)
410 struct hme_pci_softc *hsc;
412 hsc = device_get_softc(dev);
413 hme_resume(&hsc->hsc_hme);