sni_emmc.c revision 1.10
1/*	$NetBSD: sni_emmc.c,v 1.10 2021/12/21 06:00:45 nisimura Exp $	*/
2
3/*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Socionext SC2A11 SynQuacer eMMC driver
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: sni_emmc.c,v 1.10 2021/12/21 06:00:45 nisimura Exp $");
38
39#include <sys/param.h>
40#include <sys/bus.h>
41#include <sys/intr.h>
42#include <sys/device.h>
43#include <sys/errno.h>
44#include <sys/kernel.h>
45#include <sys/systm.h>
46
47#include <machine/endian.h>
48
49#include <dev/sdmmc/sdhcreg.h>
50#include <dev/sdmmc/sdhcvar.h>
51#include <dev/sdmmc/sdmmcvar.h>
52
53#include <dev/fdt/fdtvar.h>
54#include <dev/acpi/acpireg.h>
55#include <dev/acpi/acpivar.h>
56#include <dev/acpi/acpi_intr.h>
57
58static int sniemmc_fdt_match(device_t, struct cfdata *, void *);
59static void sniemmc_fdt_attach(device_t, device_t, void *);
60static int sniemmc_acpi_match(device_t, struct cfdata *, void *);
61static void sniemmc_acpi_attach(device_t, device_t, void *);
62
63struct sniemmc_softc {
64	struct sdhc_softc	sc;
65	bus_space_tag_t		sc_iot;
66	bus_space_handle_t	sc_ioh;
67	bus_addr_t		sc_iob;
68	bus_size_t		sc_ios;
69	void			*sc_ih;
70	struct sdhc_host	*sc_hosts[1];
71	bus_dmamap_t		sc_dmamap;
72	bus_dma_segment_t	sc_segs[1];
73	kcondvar_t		sc_cv;
74	int			sc_phandle;
75};
76
77CFATTACH_DECL_NEW(sniemmc_fdt, sizeof(struct sniemmc_softc),
78    sniemmc_fdt_match, sniemmc_fdt_attach, NULL, NULL);
79
80CFATTACH_DECL_NEW(sniemmc_acpi, sizeof(struct sniemmc_softc),
81    sniemmc_acpi_match, sniemmc_acpi_attach, NULL, NULL);
82
83static void sniemmc_attach_i(device_t);
84
85static const struct device_compatible_entry compat_data[] = {
86	{ .compat = "socionext,synquacer-sdhci" },
87	{ .compat = "fujitsu,mb86s70-sdhci-3.0" },
88	DEVICE_COMPAT_EOL
89};
90static const struct device_compatible_entry compatible[] = {
91	{ .compat = "SCX0002" },
92	DEVICE_COMPAT_EOL
93};
94
95static int
96sniemmc_fdt_match(device_t parent, struct cfdata *match, void *aux)
97{
98	struct fdt_attach_args * const faa = aux;
99
100	return of_compatible_match(faa->faa_phandle, compat_data);
101}
102
103static void
104sniemmc_fdt_attach(device_t parent, device_t self, void *aux)
105{
106	struct sniemmc_softc * const sc = device_private(self);
107	struct fdt_attach_args * const faa = aux;
108	const int phandle = faa->faa_phandle;
109	bus_space_handle_t ioh;
110	bus_addr_t addr;
111	bus_size_t size;
112	char intrstr[128];
113
114	aprint_naive("\n");
115	aprint_normal_dev(self, "Socionext eMMC controller\n");
116
117	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
118	    || bus_space_map(faa->faa_bst, addr, size, 0, &ioh) != 0) {
119		aprint_error_dev(self, "unable to map device\n");
120		return;
121	}
122	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
123		aprint_error_dev(self, "failed to decode interrupt\n");
124		goto fail;
125	}
126	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SDMMC, 0,
127	    sdhc_intr, &sc->sc);
128	if (sc->sc_ih == NULL) {
129		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
130		    intrstr);
131		goto fail;
132	}
133	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
134
135	sc->sc.sc_dev = self;
136	sc->sc.sc_dmat = faa->faa_dmat;
137	sc->sc.sc_host = sc->sc_hosts;
138	sc->sc_iot = faa->faa_bst;
139	sc->sc_ioh = ioh;
140	sc->sc_iob = addr;
141	sc->sc_ios = size;
142	sc->sc_phandle = phandle;
143
144	config_defer(self, sniemmc_attach_i);
145	return;
146 fail:
147	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
148	return;
149}
150
151static int
152sniemmc_acpi_match(device_t parent, struct cfdata *match, void *aux)
153{
154	struct acpi_attach_args *aa = aux;
155
156	return acpi_compatible_match(aa, compatible);
157}
158
159static void
160sniemmc_acpi_attach(device_t parent, device_t self, void *aux)
161{
162	struct sniemmc_softc * const sc = device_private(self);
163	struct acpi_attach_args *aa = aux;
164	ACPI_HANDLE handle = aa->aa_node->ad_handle;
165	bus_space_handle_t ioh;
166	struct acpi_resources res;
167	struct acpi_mem *mem;
168	struct acpi_irq *irq;
169	ACPI_STATUS rv;
170
171	aprint_naive("\n");
172	aprint_normal(": Socionext eMMC controller\n");
173
174	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
175	    &res, &acpi_resource_parse_ops_default);
176	if (ACPI_FAILURE(rv))
177		return;
178	mem = acpi_res_mem(&res, 0);
179	irq = acpi_res_irq(&res, 0);
180	if (mem == NULL || irq == NULL || mem->ar_length == 0) {
181		aprint_error_dev(self, "incomplete resources\n");
182		return;
183	}
184	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
185	    &ioh)) {
186		aprint_error_dev(self, "couldn't map registers\n");
187		return;
188	}
189	sc->sc_ih = acpi_intr_establish(self, (uint64_t)handle,
190	    IPL_BIO, false, sdhc_intr, &sc->sc, device_xname(self));
191	if (sc->sc_ih == NULL) {
192		aprint_error_dev(self, "couldn't establish interrupt\n");
193		goto fail;
194	}
195
196	sc->sc.sc_dev = self;
197	sc->sc.sc_dmat = aa->aa_dmat;
198	sc->sc.sc_host = sc->sc_hosts;
199	sc->sc_iot = aa->aa_memt;
200	sc->sc_ioh = ioh;
201	sc->sc_ios = mem->ar_length;
202	sc->sc_phandle = 0;
203
204	config_defer(self, sniemmc_attach_i);
205
206	acpi_resource_cleanup(&res);
207	return;
208 fail:
209	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
210	acpi_resource_cleanup(&res);
211	return;
212}
213
214static void
215sniemmc_attach_i(device_t self)
216{
217	struct sniemmc_softc * const sc = device_private(self);
218	int error;
219
220	sc->sc.sc_flags = 0;
221	sc->sc.sc_flags |= SDHC_FLAG_32BIT_ACCESS;
222	sc->sc.sc_clkbase = 50000;	/* Default to 50MHz */
223
224	aprint_normal_dev(sc->sc.sc_dev, "doing sdhc_host() ...\n");
225#if 0
226	error = sdhc_host_found(&sc->sc, sc->sc_iot, sc->sc_ioh, sc->sc_ios);
227#endif
228	error = 0;
229	if (error) {
230		aprint_error_dev(self, "couldn't initialize host, error=%d\n",
231		    error);
232		goto fail;
233	}
234	return;
235 fail:
236	if (sc->sc_phandle)
237		fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
238	else
239		acpi_intr_disestablish(sc->sc_ih);
240	sc->sc_ih = NULL;
241	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
242	return;
243}
244