sni_emmc.c revision 1.8
1/*	$NetBSD: sni_emmc.c,v 1.8 2021/01/27 03:10:19 thorpej 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.8 2021/01/27 03:10:19 thorpej 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};
90
91static int
92sniemmc_fdt_match(device_t parent, struct cfdata *match, void *aux)
93{
94	struct fdt_attach_args * const faa = aux;
95
96	return of_compatible_match(faa->faa_phandle, compat_data);
97}
98
99static void
100sniemmc_fdt_attach(device_t parent, device_t self, void *aux)
101{
102	struct sniemmc_softc * const sc = device_private(self);
103	struct fdt_attach_args * const faa = aux;
104	const int phandle = faa->faa_phandle;
105	bus_space_handle_t ioh;
106	bus_addr_t addr;
107	bus_size_t size;
108	char intrstr[128];
109
110	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
111	    || bus_space_map(faa->faa_bst, addr, size, 0, &ioh) != 0) {
112		aprint_error(": unable to map device\n");
113		return;
114	}
115	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
116		aprint_error(": failed to decode interrupt\n");
117		goto fail;
118	}
119	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SDMMC, 0,
120	    sdhc_intr, &sc->sc);
121	if (sc->sc_ih == NULL) {
122		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
123		    intrstr);
124		goto fail;
125	}
126
127	aprint_naive("\n");
128	aprint_normal_dev(self, "Socionext eMMC controller\n");
129	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
130
131	sc->sc.sc_dev = self;
132	sc->sc.sc_dmat = faa->faa_dmat;
133	sc->sc.sc_host = sc->sc_hosts;
134	sc->sc_phandle = phandle;
135	sc->sc_iot = faa->faa_bst;
136	sc->sc_ioh = ioh;
137	sc->sc_iob = addr;
138	sc->sc_ios = size;
139
140	config_defer(self, sniemmc_attach_i);
141	return;
142 fail:
143	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
144	return;
145}
146
147static int
148sniemmc_acpi_match(device_t parent, struct cfdata *match, void *aux)
149{
150	static const char * compatible[] = {
151		"SCX0002",
152		NULL
153	};
154	struct acpi_attach_args *aa = aux;
155
156	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
157		return 0;
158	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
159}
160
161static void
162sniemmc_acpi_attach(device_t parent, device_t self, void *aux)
163{
164	struct sniemmc_softc * const sc = device_private(self);
165	struct acpi_attach_args *aa = aux;
166	bus_space_handle_t ioh;
167	struct acpi_resources res;
168	struct acpi_mem *mem;
169	struct acpi_irq *irq;
170	ACPI_STATUS rv;
171
172	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
173	    &res, &acpi_resource_parse_ops_default);
174	if (ACPI_FAILURE(rv))
175		return;
176	mem = acpi_res_mem(&res, 0);
177	irq = acpi_res_irq(&res, 0);
178	if (mem == NULL || irq == NULL || mem->ar_length == 0) {
179		aprint_error(": incomplete resources\n");
180		return;
181	}
182	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
183	    &ioh)) {
184		aprint_error(": couldn't map registers\n");
185		return;
186	}
187	sc->sc_ih = acpi_intr_establish(self,
188	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
189	    IPL_BIO, false, sdhc_intr, &sc->sc, device_xname(self));
190	if (sc->sc_ih == NULL) {
191		aprint_error_dev(self, "couldn't establish interrupt\n");
192		goto fail;
193	}
194
195	aprint_naive("\n");
196	aprint_normal_dev(self, "Socionext eMMC controller\n");
197
198	sc->sc.sc_dev = self;
199	sc->sc.sc_dmat = aa->aa_dmat;
200	sc->sc.sc_host = sc->sc_hosts;
201	sc->sc_iot = aa->aa_memt;
202	sc->sc_ioh = ioh;
203	sc->sc_ios = mem->ar_length;
204
205	config_defer(self, sniemmc_attach_i);
206
207	acpi_resource_cleanup(&res);
208	return;
209 fail:
210	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
211	acpi_resource_cleanup(&res);
212	return;
213}
214
215static void
216sniemmc_attach_i(device_t self)
217{
218	struct sniemmc_softc * const sc = device_private(self);
219	int error;
220
221	sc->sc.sc_flags = 0;
222	sc->sc.sc_flags |= SDHC_FLAG_32BIT_ACCESS;
223	sc->sc.sc_clkbase = 50000;	/* Default to 50MHz */
224
225	aprint_normal_dev(sc->sc.sc_dev, "doing sdhc_host() ...\n");
226#if 0
227	error = sdhc_host_found(&sc->sc, sc->sc_iot, sc->sc_ioh, sc->sc_ios);
228#endif
229	error = 0;
230	if (error) {
231		aprint_error_dev(self, "couldn't intialize host, error=%d\n",				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