sni_emmc.c revision 1.6
1/*	$NetBSD: sni_emmc.c,v 1.6 2020/03/25 23:20:38 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.6 2020/03/25 23:20:38 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 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	prop_dictionary_t dict = device_properties(self);
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	_Bool disable;
110
111	prop_dictionary_get_bool(dict, "disable", &disable);
112	if (disable) {
113		aprint_naive(": disabled\n");
114		aprint_normal(": disabled\n");
115		return;
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(": unable to map device\n");
120		return;
121	}
122	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
123		aprint_error(": 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
134	aprint_naive("\n");
135	aprint_normal_dev(self, "Socionext eMMC controller\n");
136	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
137
138	sc->sc.sc_dev = self;
139	sc->sc.sc_dmat = faa->faa_dmat;
140	sc->sc.sc_host = sc->sc_hosts;
141	sc->sc_phandle = phandle;
142	sc->sc_iot = faa->faa_bst;
143	sc->sc_ioh = ioh;
144	sc->sc_iob = addr;
145	sc->sc_ios = size;
146
147	config_defer(self, sniemmc_attach_i);
148	return;
149 fail:
150	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
151	return;
152}
153
154static int
155sniemmc_acpi_match(device_t parent, struct cfdata *match, void *aux)
156{
157	static const char * compatible[] = {
158		"SCX0002",
159		NULL
160	};
161	struct acpi_attach_args *aa = aux;
162
163	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
164		return 0;
165	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
166}
167
168static void
169sniemmc_acpi_attach(device_t parent, device_t self, void *aux)
170{
171	struct sniemmc_softc * const sc = device_private(self);
172	struct acpi_attach_args *aa = aux;
173	bus_space_handle_t ioh;
174	struct acpi_resources res;
175	struct acpi_mem *mem;
176	struct acpi_irq *irq;
177	ACPI_STATUS rv;
178
179	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
180	    &res, &acpi_resource_parse_ops_default);
181	if (ACPI_FAILURE(rv))
182		return;
183	mem = acpi_res_mem(&res, 0);
184	irq = acpi_res_irq(&res, 0);
185	if (mem == NULL || irq == NULL || mem->ar_length == 0) {
186		aprint_error(": incomplete resources\n");
187		return;
188	}
189	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
190	    &ioh)) {
191		aprint_error(": couldn't map registers\n");
192		return;
193	}
194	sc->sc_ih = acpi_intr_establish(self,
195	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
196	    IPL_BIO, false, sdhc_intr, &sc->sc, device_xname(self));
197	if (sc->sc_ih == NULL) {
198		aprint_error_dev(self, "couldn't establish interrupt\n");
199		goto fail;
200	}
201
202	aprint_naive("\n");
203	aprint_normal_dev(self, "Socionext eMMC controller\n");
204
205	sc->sc.sc_dev = self;
206	sc->sc.sc_dmat = aa->aa_dmat;
207	sc->sc.sc_host = sc->sc_hosts;
208	sc->sc_iot = aa->aa_memt;
209	sc->sc_ioh = ioh;
210	sc->sc_ios = mem->ar_length;
211
212	config_defer(self, sniemmc_attach_i);
213
214	acpi_resource_cleanup(&res);
215	return;
216 fail:
217	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
218	acpi_resource_cleanup(&res);
219	return;
220}
221
222static void
223sniemmc_attach_i(device_t self)
224{
225	struct sniemmc_softc * const sc = device_private(self);
226	int error;
227
228	sc->sc.sc_flags = 0;
229	sc->sc.sc_flags |= SDHC_FLAG_32BIT_ACCESS;
230	sc->sc.sc_clkbase = 50000;	/* Default to 50MHz */
231
232	aprint_normal_dev(sc->sc.sc_dev, "doing sdhc_host() ...\n");
233#if 0
234	error = sdhc_host_found(&sc->sc, sc->sc_iot, sc->sc_ioh, sc->sc_ios);
235#endif
236	error = 0;
237	if (error) {
238		aprint_error_dev(self, "couldn't intialize host, error=%d\n",				error);
239		goto fail;
240	}
241	return;
242 fail:
243	if (sc->sc_phandle)
244		fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
245	else
246		acpi_intr_disestablish(sc->sc_ih);
247	sc->sc_ih = NULL;
248	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
249	return;
250}
251