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