sdhci_acpi.c revision 318198
1/*-
2 * Copyright (c) 2017 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: stable/10/sys/dev/sdhci/sdhci_acpi.c 318198 2017-05-11 21:01:02Z marius $");
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/bus.h>
32#include <sys/kernel.h>
33#include <sys/lock.h>
34#include <sys/module.h>
35#include <sys/mutex.h>
36#include <sys/resource.h>
37#include <sys/rman.h>
38#include <sys/sysctl.h>
39#include <sys/taskqueue.h>
40
41#include <machine/bus.h>
42#include <machine/resource.h>
43
44#include <contrib/dev/acpica/include/acpi.h>
45#include <dev/acpica/acpivar.h>
46
47#include <dev/mmc/bridge.h>
48
49#include <dev/sdhci/sdhci.h>
50
51#include "mmcbr_if.h"
52#include "sdhci_if.h"
53
54static const struct sdhci_acpi_device {
55	const char*	hid;
56	int		uid;
57	const char	*desc;
58	u_int		quirks;
59} sdhci_acpi_devices[] = {
60	{ "80860F14",	1,	"Intel Bay Trail eMMC 4.5 Controller",
61	    SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
62	    SDHCI_QUIRK_INTEL_POWER_UP_RESET |
63	    SDHCI_QUIRK_WAIT_WHILE_BUSY },
64	{ "80860F14",	3,	"Intel Bay Trail SDXC Controller",
65	    SDHCI_QUIRK_WAIT_WHILE_BUSY },
66	{ "80860F16",	0,	"Intel Bay Trail SDXC Controller",
67	    SDHCI_QUIRK_WAIT_WHILE_BUSY },
68	{ NULL, 0, NULL, 0}
69};
70
71static char *sdhci_ids[] = {
72	"80860F14",
73	"80860F16",
74	NULL
75};
76
77struct sdhci_acpi_softc {
78	u_int		quirks;		/* Chip specific quirks */
79	struct resource *irq_res;	/* IRQ resource */
80	void		*intrhand;	/* Interrupt handle */
81
82	struct sdhci_slot slot;
83	struct resource	*mem_res;	/* Memory resource */
84};
85
86static void sdhci_acpi_intr(void *arg);
87static int sdhci_acpi_detach(device_t dev);
88
89static uint8_t
90sdhci_acpi_read_1(device_t dev, struct sdhci_slot *slot __unused,
91    bus_size_t off)
92{
93	struct sdhci_acpi_softc *sc = device_get_softc(dev);
94
95	bus_barrier(sc->mem_res, 0, 0xFF,
96	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
97	return bus_read_1(sc->mem_res, off);
98}
99
100static void
101sdhci_acpi_write_1(device_t dev, struct sdhci_slot *slot __unused,
102    bus_size_t off, uint8_t val)
103{
104	struct sdhci_acpi_softc *sc = device_get_softc(dev);
105
106	bus_barrier(sc->mem_res, 0, 0xFF,
107	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
108	bus_write_1(sc->mem_res, off, val);
109}
110
111static uint16_t
112sdhci_acpi_read_2(device_t dev, struct sdhci_slot *slot __unused,
113    bus_size_t off)
114{
115	struct sdhci_acpi_softc *sc = device_get_softc(dev);
116
117	bus_barrier(sc->mem_res, 0, 0xFF,
118	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
119	return bus_read_2(sc->mem_res, off);
120}
121
122static void
123sdhci_acpi_write_2(device_t dev, struct sdhci_slot *slot __unused,
124    bus_size_t off, uint16_t val)
125{
126	struct sdhci_acpi_softc *sc = device_get_softc(dev);
127
128	bus_barrier(sc->mem_res, 0, 0xFF,
129	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
130	bus_write_2(sc->mem_res, off, val);
131}
132
133static uint32_t
134sdhci_acpi_read_4(device_t dev, struct sdhci_slot *slot __unused,
135    bus_size_t off)
136{
137	struct sdhci_acpi_softc *sc = device_get_softc(dev);
138
139	bus_barrier(sc->mem_res, 0, 0xFF,
140	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
141	return bus_read_4(sc->mem_res, off);
142}
143
144static void
145sdhci_acpi_write_4(device_t dev, struct sdhci_slot *slot __unused,
146    bus_size_t off, uint32_t val)
147{
148	struct sdhci_acpi_softc *sc = device_get_softc(dev);
149
150	bus_barrier(sc->mem_res, 0, 0xFF,
151	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
152	bus_write_4(sc->mem_res, off, val);
153}
154
155static void
156sdhci_acpi_read_multi_4(device_t dev, struct sdhci_slot *slot __unused,
157    bus_size_t off, uint32_t *data, bus_size_t count)
158{
159	struct sdhci_acpi_softc *sc = device_get_softc(dev);
160
161	bus_read_multi_stream_4(sc->mem_res, off, data, count);
162}
163
164static void
165sdhci_acpi_write_multi_4(device_t dev, struct sdhci_slot *slot __unused,
166    bus_size_t off, uint32_t *data, bus_size_t count)
167{
168	struct sdhci_acpi_softc *sc = device_get_softc(dev);
169
170	bus_write_multi_stream_4(sc->mem_res, off, data, count);
171}
172
173static const struct sdhci_acpi_device *
174sdhci_acpi_find_device(device_t dev)
175{
176	const char *hid;
177	int i, uid;
178	ACPI_HANDLE handle;
179	ACPI_STATUS status;
180
181	hid = ACPI_ID_PROBE(device_get_parent(dev), dev, sdhci_ids);
182	if (hid == NULL)
183		return (NULL);
184
185	handle = acpi_get_handle(dev);
186	status = acpi_GetInteger(handle, "_UID", &uid);
187	if (ACPI_FAILURE(status))
188		uid = 0;
189
190	for (i = 0; sdhci_acpi_devices[i].hid != NULL; i++) {
191		if (strcmp(sdhci_acpi_devices[i].hid, hid) != 0)
192			continue;
193		if ((sdhci_acpi_devices[i].uid != 0) &&
194		    (sdhci_acpi_devices[i].uid != uid))
195			continue;
196		return (&sdhci_acpi_devices[i]);
197	}
198
199	return (NULL);
200}
201
202static int
203sdhci_acpi_probe(device_t dev)
204{
205	const struct sdhci_acpi_device *acpi_dev;
206
207	acpi_dev = sdhci_acpi_find_device(dev);
208	if (acpi_dev == NULL)
209		return (ENXIO);
210
211	device_set_desc(dev, acpi_dev->desc);
212
213	return (BUS_PROBE_DEFAULT);
214}
215
216static int
217sdhci_acpi_attach(device_t dev)
218{
219	struct sdhci_acpi_softc *sc = device_get_softc(dev);
220	int rid, err;
221	const struct sdhci_acpi_device *acpi_dev;
222
223	acpi_dev = sdhci_acpi_find_device(dev);
224	if (acpi_dev == NULL)
225		return (ENXIO);
226
227	sc->quirks = acpi_dev->quirks;
228
229	/* Allocate IRQ. */
230	rid = 0;
231	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
232		RF_ACTIVE);
233	if (sc->irq_res == NULL) {
234		device_printf(dev, "can't allocate IRQ\n");
235		return (ENOMEM);
236	}
237
238	rid = 0;
239	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
240	    &rid, RF_ACTIVE);
241	if (sc->mem_res == NULL) {
242		device_printf(dev, "can't allocate memory resource for slot\n");
243		sdhci_acpi_detach(dev);
244		return (ENOMEM);
245	}
246
247	sc->slot.quirks = sc->quirks;
248
249	err = sdhci_init_slot(dev, &sc->slot, 0);
250	if (err) {
251		device_printf(dev, "failed to init slot\n");
252		sdhci_acpi_detach(dev);
253		return (err);
254	}
255
256	/* Activate the interrupt */
257	err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
258	    NULL, sdhci_acpi_intr, sc, &sc->intrhand);
259	if (err) {
260		device_printf(dev, "can't setup IRQ\n");
261		sdhci_acpi_detach(dev);
262		return (err);
263	}
264
265	/* Process cards detection. */
266	sdhci_start_slot(&sc->slot);
267
268	return (0);
269}
270
271static int
272sdhci_acpi_detach(device_t dev)
273{
274	struct sdhci_acpi_softc *sc = device_get_softc(dev);
275
276	if (sc->intrhand)
277		bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
278	if (sc->irq_res)
279		bus_release_resource(dev, SYS_RES_IRQ,
280		    rman_get_rid(sc->irq_res), sc->irq_res);
281
282	if (sc->mem_res) {
283		sdhci_cleanup_slot(&sc->slot);
284		bus_release_resource(dev, SYS_RES_MEMORY,
285		    rman_get_rid(sc->mem_res), sc->mem_res);
286	}
287
288	return (0);
289}
290
291static int
292sdhci_acpi_shutdown(device_t dev)
293{
294
295	return (0);
296}
297
298static int
299sdhci_acpi_suspend(device_t dev)
300{
301	struct sdhci_acpi_softc *sc = device_get_softc(dev);
302	int err;
303
304	err = bus_generic_suspend(dev);
305	if (err)
306		return (err);
307	sdhci_generic_suspend(&sc->slot);
308	return (0);
309}
310
311static int
312sdhci_acpi_resume(device_t dev)
313{
314	struct sdhci_acpi_softc *sc = device_get_softc(dev);
315	int err;
316
317	sdhci_generic_resume(&sc->slot);
318	err = bus_generic_resume(dev);
319	if (err)
320		return (err);
321	return (0);
322}
323
324static void
325sdhci_acpi_intr(void *arg)
326{
327	struct sdhci_acpi_softc *sc = (struct sdhci_acpi_softc *)arg;
328
329	sdhci_generic_intr(&sc->slot);
330}
331
332static device_method_t sdhci_methods[] = {
333	/* device_if */
334	DEVMETHOD(device_probe, sdhci_acpi_probe),
335	DEVMETHOD(device_attach, sdhci_acpi_attach),
336	DEVMETHOD(device_detach, sdhci_acpi_detach),
337	DEVMETHOD(device_shutdown, sdhci_acpi_shutdown),
338	DEVMETHOD(device_suspend, sdhci_acpi_suspend),
339	DEVMETHOD(device_resume, sdhci_acpi_resume),
340
341	/* Bus interface */
342	DEVMETHOD(bus_read_ivar,	sdhci_generic_read_ivar),
343	DEVMETHOD(bus_write_ivar,	sdhci_generic_write_ivar),
344
345	/* mmcbr_if */
346	DEVMETHOD(mmcbr_update_ios,	sdhci_generic_update_ios),
347	DEVMETHOD(mmcbr_request,	sdhci_generic_request),
348	DEVMETHOD(mmcbr_get_ro,		sdhci_generic_get_ro),
349	DEVMETHOD(mmcbr_acquire_host,   sdhci_generic_acquire_host),
350	DEVMETHOD(mmcbr_release_host,   sdhci_generic_release_host),
351
352	/* SDHCI registers accessors */
353	DEVMETHOD(sdhci_read_1,		sdhci_acpi_read_1),
354	DEVMETHOD(sdhci_read_2,		sdhci_acpi_read_2),
355	DEVMETHOD(sdhci_read_4,		sdhci_acpi_read_4),
356	DEVMETHOD(sdhci_read_multi_4,	sdhci_acpi_read_multi_4),
357	DEVMETHOD(sdhci_write_1,	sdhci_acpi_write_1),
358	DEVMETHOD(sdhci_write_2,	sdhci_acpi_write_2),
359	DEVMETHOD(sdhci_write_4,	sdhci_acpi_write_4),
360	DEVMETHOD(sdhci_write_multi_4,	sdhci_acpi_write_multi_4),
361
362	DEVMETHOD_END
363};
364
365static driver_t sdhci_acpi_driver = {
366	"sdhci_acpi",
367	sdhci_methods,
368	sizeof(struct sdhci_acpi_softc),
369};
370static devclass_t sdhci_acpi_devclass;
371
372DRIVER_MODULE(sdhci_acpi, acpi, sdhci_acpi_driver, sdhci_acpi_devclass, NULL,
373    NULL);
374MODULE_DEPEND(sdhci_acpi, sdhci, 1, 1, 1);
375MMC_DECLARE_BRIDGE(sdhci_acpi);
376