xref: /netbsd-src/sys/dev/acpi/genet_acpi.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /* $NetBSD: genet_acpi.c,v 1.5 2021/05/03 10:28:26 rin 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.5 2021/05/03 10:28:26 rin 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 <sys/rndsource.h>
40 
41 #include <net/if.h>
42 #include <net/if_dl.h>
43 #include <net/if_ether.h>
44 #include <net/if_media.h>
45 
46 #include <dev/mii/miivar.h>
47 
48 #include <dev/ic/bcmgenetvar.h>
49 
50 #include <dev/acpi/acpireg.h>
51 #include <dev/acpi/acpivar.h>
52 #include <dev/acpi/acpi_intr.h>
53 
54 #ifdef NET_MPSAFE
55 #define	GENET_INTR_MPSAFE	true
56 #else
57 #define	GENET_INTR_MPSAFE	false
58 #endif
59 
60 static const struct device_compatible_entry compat_data[] = {
61 	{ .compat = "BCM6E4E" },	/* Broadcom GENET v5 */
62 	DEVICE_COMPAT_EOL
63 };
64 
65 static int	genet_acpi_match(device_t, cfdata_t, void *);
66 static void	genet_acpi_attach(device_t, device_t, void *);
67 
68 CFATTACH_DECL_NEW(genet_acpi, sizeof(struct genet_softc),
69     genet_acpi_match, genet_acpi_attach, NULL, NULL);
70 
71 static int
72 genet_acpi_match(device_t parent, cfdata_t cf, void *aux)
73 {
74 	struct acpi_attach_args *aa = aux;
75 
76 	return acpi_compatible_match(aa, compat_data);
77 }
78 
79 static void
80 genet_acpi_attach(device_t parent, device_t self, void *aux)
81 {
82 	struct genet_softc * const sc = device_private(self);
83 	struct acpi_attach_args *aa = aux;
84 	ACPI_HANDLE handle = aa->aa_node->ad_handle;
85 	struct acpi_resources res;
86 	struct acpi_mem *mem;
87 	struct acpi_irq *irq;
88 	char *phy_mode;
89 	ACPI_STATUS rv;
90 	int error;
91 	void *ih;
92 
93 	sc->sc_dev = self;
94 
95 	rv = acpi_resource_parse(sc->sc_dev, handle, "_CRS",
96 	    &res, &acpi_resource_parse_ops_default);
97 	if (ACPI_FAILURE(rv))
98 		return;
99 
100 	mem = acpi_res_mem(&res, 0);
101 	if (mem == NULL) {
102 		aprint_error_dev(self, "couldn't find mem resource\n");
103 		goto done;
104 	}
105 
106 	irq = acpi_res_irq(&res, 0);
107 	if (irq == NULL) {
108 		aprint_error_dev(self, "couldn't find irq resource\n");
109 		goto done;
110 	}
111 
112 	sc->sc_bst = aa->aa_memt;
113 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length,
114 	    0, &sc->sc_bsh);
115 	if (error != 0) {
116 		aprint_error_dev(self, "couldn't map registers\n");
117 		goto done;
118 	}
119 	sc->sc_dmat = BUS_DMA_TAG_VALID(aa->aa_dmat64) ?
120 	    aa->aa_dmat64 : aa->aa_dmat;
121 	sc->sc_phy_id = MII_PHY_ANY;
122 
123 	rv = acpi_dsd_string(handle, "phy-mode", &phy_mode);
124 	if (ACPI_FAILURE(rv)) {
125 		aprint_error_dev(self, "missing 'phy-mode' property\n");
126 		goto done;
127 	}
128 
129 	if (strcmp(phy_mode, "rgmii-rxid") == 0)
130 		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_RXID;
131 	else if (strcmp(phy_mode, "rgmii-txid") == 0)
132 		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_TXID;
133 	else if (strcmp(phy_mode, "rgmii-id") == 0)
134 		sc->sc_phy_mode = GENET_PHY_MODE_RGMII_ID;
135 	else if (strcmp(phy_mode, "rgmii") == 0)
136 		sc->sc_phy_mode = GENET_PHY_MODE_RGMII;
137 	else {
138 		aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
139 		goto done;
140 	}
141 
142 	aprint_normal("%s", device_xname(self));
143 	if (genet_attach(sc) != 0)
144 		goto done;
145 
146         ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)handle, IPL_NET,
147 	    GENET_INTR_MPSAFE, genet_intr, sc, device_xname(self));
148 	if (ih == NULL) {
149 		aprint_error_dev(self, "couldn't establish interrupt\n");
150 		goto done;
151 	}
152 
153 done:
154 	acpi_resource_cleanup(&res);
155 }
156