1 /* $NetBSD: dwcmmc_acpi.c,v 1.3 2024/02/09 16:53:15 skrll 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 <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: dwcmmc_acpi.c,v 1.3 2024/02/09 16:53:15 skrll Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/device.h>
36 
37 #include <dev/ic/dwc_mmc_var.h>
38 #include <dev/ic/dwc_mmc_reg.h>
39 #include <dev/sdmmc/sdmmcchip.h>
40 
41 #include <dev/acpi/acpireg.h>
42 #include <dev/acpi/acpivar.h>
43 #include <dev/acpi/acpi_intr.h>
44 
45 /*
46  * FIXME: Remove separate compat_data arrays once acpi_compatible_lookup
47  *          grows support for DT link devices.
48  */
49 static const struct device_compatible_entry rockchip_compat_data[] = {
50           { .compat = "rockchip,rk3288-dw-mshc" },
51           DEVICE_COMPAT_EOL
52 };
53 static const struct device_compatible_entry compat_data[] = {
54           { .compat = "snps,dw-mshc" },
55           DEVICE_COMPAT_EOL
56 };
57 
58 static int          dwcmmc_acpi_match(device_t, cfdata_t, void *);
59 static void         dwcmmc_acpi_attach(device_t, device_t, void *);
60 
61 static int          dwcmmc_acpi_init_props(struct dwc_mmc_softc *, ACPI_HANDLE);
62 
63 CFATTACH_DECL_NEW(dwcmmc_acpi, sizeof(struct dwc_mmc_softc),
64     dwcmmc_acpi_match, dwcmmc_acpi_attach, NULL, NULL);
65 
66 static int
dwcmmc_acpi_match(device_t parent,cfdata_t cf,void * aux)67 dwcmmc_acpi_match(device_t parent, cfdata_t cf, void *aux)
68 {
69           struct acpi_attach_args *aa = aux;
70           int match;
71 
72           match = acpi_compatible_match(aa, rockchip_compat_data);
73           if (!match) {
74                     match = acpi_compatible_match(aa, compat_data);
75           }
76 
77           return match;
78 }
79 
80 static void
dwcmmc_acpi_attach(device_t parent,device_t self,void * aux)81 dwcmmc_acpi_attach(device_t parent, device_t self, void *aux)
82 {
83           struct dwc_mmc_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           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 
101           sc->sc_flags = DWC_MMC_F_USE_HOLD_REG | DWC_MMC_F_DMA;
102 
103           if (acpi_compatible_match(aa, rockchip_compat_data)) {
104                     sc->sc_ciu_div = 2;
105                     sc->sc_intr_cardmask = DWC_MMC_INT_SDIO_INT(8);
106           }
107 
108           mem = acpi_res_mem(&res, 0);
109           if (mem == NULL) {
110                     aprint_error_dev(self, "couldn't find mem resource\n");
111                     goto done;
112           }
113 
114           irq = acpi_res_irq(&res, 0);
115           if (irq == NULL) {
116                     aprint_error_dev(self, "couldn't find irq resource\n");
117                     goto done;
118           }
119 
120           sc->sc_bst = aa->aa_memt;
121           error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length,
122               0, &sc->sc_bsh);
123           if (error != 0) {
124                     aprint_error_dev(self, "couldn't map registers\n");
125                     goto done;
126           }
127           sc->sc_dmat = aa->aa_dmat;
128 
129           if (dwcmmc_acpi_init_props(sc, handle) != 0) {
130                     goto done;
131           }
132 
133           if (dwc_mmc_init(sc) != 0) {
134                     goto done;
135           }
136 
137         ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)handle, IPL_NET,
138               false, dwc_mmc_intr, sc, device_xname(self));
139           if (ih == NULL) {
140                     aprint_error_dev(self, "couldn't establish interrupt\n");
141                     goto done;
142           }
143 
144 done:
145           acpi_resource_cleanup(&res);
146 }
147 
148 static int
dwcmmc_acpi_init_props(struct dwc_mmc_softc * sc,ACPI_HANDLE handle)149 dwcmmc_acpi_init_props(struct dwc_mmc_softc *sc, ACPI_HANDLE handle)
150 {
151           ACPI_INTEGER ival;
152           bool bval;
153 
154           /* Defaults */
155           sc->sc_fifo_depth = 0;
156           sc->sc_clock_freq = UINT_MAX;
157           sc->sc_bus_width = 4;
158 
159           /* Get settings from DSD properties */
160           if (ACPI_SUCCESS(acpi_dsd_integer(handle, "fifo-depth", &ival))) {
161                     sc->sc_fifo_depth = ival;
162           }
163           if (ACPI_SUCCESS(acpi_dsd_integer(handle, "max-frequency", &ival))) {
164                     sc->sc_clock_freq = ival;
165           }
166           if (ACPI_SUCCESS(acpi_dsd_integer(handle, "bus-width", &ival))) {
167                     sc->sc_bus_width = ival;
168           }
169           bval = false;
170           acpi_dsd_bool(handle, "non-removable", &bval);
171           if (bval) {
172                     sc->sc_flags |= DWC_MMC_F_NON_REMOVABLE;
173           }
174           bval = false;
175           acpi_dsd_bool(handle, "broken-cd", &bval);
176           if (bval) {
177                     sc->sc_flags |= DWC_MMC_F_BROKEN_CD;
178           }
179 
180           if (sc->sc_clock_freq == 0 || sc->sc_clock_freq == UINT_MAX) {
181                     aprint_error_dev(sc->sc_dev, "clock frequency not specified\n");
182                     return ENXIO;
183           }
184 
185           return 0;
186 }
187