xref: /netbsd-src/sys/dev/acpi/eqos_acpi.c (revision 865c57e0098351fba0d2d2a97b33e7e0270e62c6)
1 /* $NetBSD: eqos_acpi.c,v 1.1 2022/01/03 17:19:41 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2022 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: eqos_acpi.c,v 1.1 2022/01/03 17:19:41 jmcneill Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/cpu.h>
37 #include <sys/device.h>
38 #include <sys/kmem.h>
39 
40 #include <sys/rndsource.h>
41 
42 #include <net/if.h>
43 #include <net/if_dl.h>
44 #include <net/if_ether.h>
45 #include <net/if_media.h>
46 
47 #include <dev/mii/miivar.h>
48 
49 #include <dev/ic/dwc_eqos_var.h>
50 
51 #include <dev/acpi/acpireg.h>
52 #include <dev/acpi/acpivar.h>
53 #include <dev/acpi/acpi_intr.h>
54 
55 #ifdef NET_MPSAFE
56 #define	EQOS_INTR_MPSAFE	true
57 #else
58 #define	EQOS_INTR_MPSAFE	false
59 #endif
60 
61 #define	CSR_RATE_RGMII		125000000
62 
63 static const struct device_compatible_entry compat_data[] = {
64 	{ .compat = "snps,dwmac-4.20a" },	/* DT link */
65 	DEVICE_COMPAT_EOL
66 };
67 
68 static int	eqos_acpi_match(device_t, cfdata_t, void *);
69 static void	eqos_acpi_attach(device_t, device_t, void *);
70 
71 static void	eqos_acpi_init_props(struct eqos_softc *, ACPI_HANDLE);
72 
73 CFATTACH_DECL_NEW(eqos_acpi, sizeof(struct eqos_softc),
74     eqos_acpi_match, eqos_acpi_attach, NULL, NULL);
75 
76 static int
77 eqos_acpi_match(device_t parent, cfdata_t cf, void *aux)
78 {
79 	struct acpi_attach_args *aa = aux;
80 
81 	return acpi_compatible_match(aa, compat_data);
82 }
83 
84 static void
85 eqos_acpi_attach(device_t parent, device_t self, void *aux)
86 {
87 	struct eqos_softc * const sc = device_private(self);
88 	struct acpi_attach_args *aa = aux;
89 	ACPI_HANDLE handle = aa->aa_node->ad_handle;
90 	struct acpi_resources res;
91 	struct acpi_mem *mem;
92 	struct acpi_irq *irq;
93 	ACPI_STATUS rv;
94 	int error;
95 	void *ih;
96 
97 	sc->sc_dev = self;
98 
99 	rv = acpi_resource_parse(sc->sc_dev, handle, "_CRS",
100 	    &res, &acpi_resource_parse_ops_default);
101 	if (ACPI_FAILURE(rv))
102 		return;
103 
104 	mem = acpi_res_mem(&res, 0);
105 	if (mem == NULL) {
106 		aprint_error_dev(self, "couldn't find mem resource\n");
107 		goto done;
108 	}
109 
110 	irq = acpi_res_irq(&res, 0);
111 	if (irq == NULL) {
112 		aprint_error_dev(self, "couldn't find irq resource\n");
113 		goto done;
114 	}
115 
116 	sc->sc_bst = aa->aa_memt;
117 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length,
118 	    0, &sc->sc_bsh);
119 	if (error != 0) {
120 		aprint_error_dev(self, "couldn't map registers\n");
121 		goto done;
122 	}
123 	sc->sc_dmat = BUS_DMA_TAG_VALID(aa->aa_dmat64) ?
124 	    aa->aa_dmat64 : aa->aa_dmat;
125 	sc->sc_phy_id = MII_PHY_ANY;
126 	sc->sc_csr_clock = CSR_RATE_RGMII;
127 
128 	eqos_acpi_init_props(sc, handle);
129 
130 	aprint_normal("%s", device_xname(self));
131 	if (eqos_attach(sc) != 0)
132 		goto done;
133 
134         ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)handle, IPL_NET,
135 	    EQOS_INTR_MPSAFE, eqos_intr, sc, device_xname(self));
136 	if (ih == NULL) {
137 		aprint_error_dev(self, "couldn't establish interrupt\n");
138 		goto done;
139 	}
140 
141 done:
142 	acpi_resource_cleanup(&res);
143 }
144 
145 static void
146 eqos_acpi_init_props(struct eqos_softc *sc, ACPI_HANDLE handle)
147 {
148 	prop_dictionary_t prop = device_properties(sc->sc_dev);
149 	ACPI_STATUS rv;
150 	ACPI_INTEGER ival;
151 #if notyet
152 	ACPI_HANDLE axi;
153 	char *sval;
154 #endif
155 
156 	/* Defaults */
157 	prop_dictionary_set_uint(prop, "snps,wr_osr_lmt", 4);
158 	prop_dictionary_set_uint(prop, "snps,rd_osr_lmt", 8);
159 
160 	/* Get settings from DSD properties */
161 	rv = acpi_dsd_integer(handle, "snps,mixed-burst", &ival);
162 	if (ACPI_SUCCESS(rv) && ival) {
163 		prop_dictionary_set_bool(prop, "snps,mixed-burst", true);
164 	}
165 	rv = acpi_dsd_integer(handle, "snps,tso", &ival);
166 	if (ACPI_SUCCESS(rv) && ival) {
167 		prop_dictionary_set_bool(prop, "snps,tso", true);
168 	}
169 
170 #if notyet
171 	rv = acpi_dsd_string(handle, "snps,axi-config", &sval);
172 	if (ACPI_SUCCESS(rv)) {
173 		rv = AcpiGetHandle(handle, sval, &axi);
174 		if (ACPI_SUCCESS(rv)) {
175 			rv = acpi_dsd_integer(axi, "snps,wr_osr_lmt", &ival);
176 			if (ACPI_SUCCESS(rv) && ival > 0) {
177 				prop_dictionary_set_uint(prop,
178 				    "snps,wr_osr_lmt", ival);
179 			}
180 			rv = acpi_dsd_integer(axi, "snps,rd_osr_lmt", &ival);
181 			if (ACPI_SUCCESS(rv) && ival > 0) {
182 				prop_dictionary_set_uint(prop,
183 				    "snps,rd_osr_lmt", ival);
184 			}
185 		}
186 		kmem_strfree(sval);
187 	}
188 #endif
189 }
190