genet_acpi.c revision 1.2
1/* $NetBSD: genet_acpi.c,v 1.2 2020/05/25 19:49:28 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
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
29#include "opt_net_mpsafe.h"
30
31#include <sys/cdefs.h>
32__KERNEL_RCSID(0, "$NetBSD: genet_acpi.c,v 1.2 2020/05/25 19:49:28 jmcneill Exp $");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/cpu.h>
37#include <sys/device.h>
38
39#include <net/if.h>
40#include <net/if_dl.h>
41#include <net/if_ether.h>
42#include <net/if_media.h>
43
44#include <dev/mii/miivar.h>
45
46#include <dev/ic/bcmgenetvar.h>
47
48#include <dev/acpi/acpireg.h>
49#include <dev/acpi/acpivar.h>
50#include <dev/acpi/acpi_intr.h>
51
52#ifdef NET_MPSAFE
53#define	GENET_INTR_MPSAFE	true
54#else
55#define	GENET_INTR_MPSAFE	false
56#endif
57
58static const char * const compatible[] = {
59	"BCM6E4E",	/* Broadcom GENET v5 */
60	NULL
61};
62
63static int	genet_acpi_match(device_t, cfdata_t, void *);
64static void	genet_acpi_attach(device_t, device_t, void *);
65
66CFATTACH_DECL_NEW(genet_acpi, sizeof(struct genet_softc),
67    genet_acpi_match, genet_acpi_attach, NULL, NULL);
68
69static int
70genet_acpi_match(device_t parent, cfdata_t cf, void *aux)
71{
72	struct acpi_attach_args *aa = aux;
73
74	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
75		return 0;
76
77	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
78}
79
80static void
81genet_acpi_attach(device_t parent, device_t self, void *aux)
82{
83	struct genet_softc * const sc = device_private(self);
84	struct acpi_attach_args *aa = aux;
85	ACPI_HANDLE handle = aa->aa_node->ad_handle;
86	struct acpi_resources res;
87	struct acpi_mem *mem;
88	struct acpi_irq *irq;
89	char *phy_mode;
90	ACPI_STATUS rv;
91	int error;
92	void *ih;
93
94	sc->sc_dev = self;
95
96	rv = acpi_resource_parse(sc->sc_dev, handle, "_CRS",
97	    &res, &acpi_resource_parse_ops_default);
98	if (ACPI_FAILURE(rv))
99		return;
100
101	mem = acpi_res_mem(&res, 0);
102	if (mem == NULL) {
103		aprint_error_dev(self, "couldn't find mem resource\n");
104		goto done;
105	}
106
107	irq = acpi_res_irq(&res, 0);
108	if (irq == NULL) {
109		aprint_error_dev(self, "couldn't find irq resource\n");
110		goto done;
111	}
112
113	sc->sc_bst = aa->aa_memt;
114	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length,
115	    0, &sc->sc_bsh);
116	if (error != 0) {
117		aprint_error_dev(self, "couldn't map registers\n");
118		goto done;
119	}
120	sc->sc_dmat = BUS_DMA_TAG_VALID(aa->aa_dmat64) ?
121	    aa->aa_dmat64 : aa->aa_dmat;
122	sc->sc_phy_id = MII_PHY_ANY;
123
124	rv = acpi_dsd_string(handle, "phy-mode", &phy_mode);
125	if (ACPI_FAILURE(rv)) {
126		aprint_error_dev(self, "missing 'phy-mode' property\n");
127		goto done;
128	}
129
130	if (strcmp(phy_mode, "rgmii-rxid") == 0)
131		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
132	else if (strcmp(phy_mode, "rgmii-txid") == 0)
133		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
134	else if (strcmp(phy_mode, "rgmii-id") == 0)
135		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
136	else if (strcmp(phy_mode, "rgmii") == 0)
137		sc->sc_phy_mode = GENET_PHY_MODE_RGMII;
138	else {
139		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
140		goto done;
141	}
142
143	aprint_normal("%s", device_xname(self));
144	if (genet_attach(sc) != 0)
145		goto done;
146
147        ih = acpi_intr_establish(self, (uint64_t)handle, IPL_NET,
148	    GENET_INTR_MPSAFE, genet_intr, sc, device_xname(self));
149	if (ih == NULL) {
150		aprint_error_dev(self, "couldn't establish interrupt\n");
151		goto done;
152	}
153
154done:
155	acpi_resource_cleanup(&res);
156}
157