sni_emmc.c revision 1.7
1/*	$NetBSD: sni_emmc.c,v 1.7 2020/05/31 23:55:18 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.7 2020/05/31 23:55:18 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 int
86sniemmc_fdt_match(device_t parent, struct cfdata *match, void *aux)
87{
88	static const char * compatible[] = {
89		"socionext,synquacer-sdhci",
90		"fujitsu,mb86s70-sdhci-3.0",
91		NULL
92	};
93	struct fdt_attach_args * const faa = aux;
94
95	return of_match_compatible(faa->faa_phandle, compatible);
96}
97
98static void
99sniemmc_fdt_attach(device_t parent, device_t self, void *aux)
100{
101	struct sniemmc_softc * const sc = device_private(self);
102	struct fdt_attach_args * const faa = aux;
103	const int phandle = faa->faa_phandle;
104	bus_space_handle_t ioh;
105	bus_addr_t addr;
106	bus_size_t size;
107	char intrstr[128];
108
109	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
110	    || bus_space_map(faa->faa_bst, addr, size, 0, &ioh) != 0) {
111		aprint_error(": unable to map device\n");
112		return;
113	}
114	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
115		aprint_error(": failed to decode interrupt\n");
116		goto fail;
117	}
118	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SDMMC, 0,
119	    sdhc_intr, &sc->sc);
120	if (sc->sc_ih == NULL) {
121		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
122		    intrstr);
123		goto fail;
124	}
125
126	aprint_naive("\n");
127	aprint_normal_dev(self, "Socionext eMMC controller\n");
128	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
129
130	sc->sc.sc_dev = self;
131	sc->sc.sc_dmat = faa->faa_dmat;
132	sc->sc.sc_host = sc->sc_hosts;
133	sc->sc_phandle = phandle;
134	sc->sc_iot = faa->faa_bst;
135	sc->sc_ioh = ioh;
136	sc->sc_iob = addr;
137	sc->sc_ios = size;
138
139	config_defer(self, sniemmc_attach_i);
140	return;
141 fail:
142	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
143	return;
144}
145
146static int
147sniemmc_acpi_match(device_t parent, struct cfdata *match, void *aux)
148{
149	static const char * compatible[] = {
150		"SCX0002",
151		NULL
152	};
153	struct acpi_attach_args *aa = aux;
154
155	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
156		return 0;
157	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
158}
159
160static void
161sniemmc_acpi_attach(device_t parent, device_t self, void *aux)
162{
163	struct sniemmc_softc * const sc = device_private(self);
164	struct acpi_attach_args *aa = aux;
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	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
172	    &res, &acpi_resource_parse_ops_default);
173	if (ACPI_FAILURE(rv))
174		return;
175	mem = acpi_res_mem(&res, 0);
176	irq = acpi_res_irq(&res, 0);
177	if (mem == NULL || irq == NULL || mem->ar_length == 0) {
178		aprint_error(": incomplete resources\n");
179		return;
180	}
181	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
182	    &ioh)) {
183		aprint_error(": couldn't map registers\n");
184		return;
185	}
186	sc->sc_ih = acpi_intr_establish(self,
187	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
188	    IPL_BIO, false, sdhc_intr, &sc->sc, device_xname(self));
189	if (sc->sc_ih == NULL) {
190		aprint_error_dev(self, "couldn't establish interrupt\n");
191		goto fail;
192	}
193
194	aprint_naive("\n");
195	aprint_normal_dev(self, "Socionext eMMC controller\n");
196
197	sc->sc.sc_dev = self;
198	sc->sc.sc_dmat = aa->aa_dmat;
199	sc->sc.sc_host = sc->sc_hosts;
200	sc->sc_iot = aa->aa_memt;
201	sc->sc_ioh = ioh;
202	sc->sc_ios = mem->ar_length;
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 intialize host, error=%d\n",				error);
231		goto fail;
232	}
233	return;
234 fail:
235	if (sc->sc_phandle)
236		fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
237	else
238		acpi_intr_disestablish(sc->sc_ih);
239	sc->sc_ih = NULL;
240	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
241	return;
242}
243