2 * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include <sys/param.h>
27 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/module.h>
31 #include <sys/resource.h>
33 #include <sys/taskqueue.h>
37 #include <dev/acpica/acpivar.h>
39 #include <bus/pci/pcivar.h>
41 #include <bus/mmc/bridge.h>
43 #include <dev/disk/sdhci/sdhci.h>
48 ACPI_MODULE_NAME("sdhci_acpi");
50 struct sdhci_acpi_softc
{
51 device_t dev
; /* Controller device */
53 struct resource
*irq_res
; /* IRQ resource */
54 void *intrhand
; /* Interrupt handle */
56 struct sdhci_slot slot
;
57 struct resource
*mem_res
; /* Memory resource */
61 sdhci_acpi_read_1(device_t dev
, struct sdhci_slot
*slot __unused
,
64 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
66 bus_barrier(sc
->mem_res
, 0, 0xFF,
67 BUS_SPACE_BARRIER_READ
| BUS_SPACE_BARRIER_WRITE
);
68 return bus_read_1(sc
->mem_res
, off
);
72 sdhci_acpi_write_1(device_t dev
, struct sdhci_slot
*slot __unused
,
73 bus_size_t off
, uint8_t val
)
75 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
77 bus_barrier(sc
->mem_res
, 0, 0xFF,
78 BUS_SPACE_BARRIER_READ
| BUS_SPACE_BARRIER_WRITE
);
79 bus_write_1(sc
->mem_res
, off
, val
);
83 sdhci_acpi_read_2(device_t dev
, struct sdhci_slot
*slot __unused
,
86 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
88 bus_barrier(sc
->mem_res
, 0, 0xFF,
89 BUS_SPACE_BARRIER_READ
| BUS_SPACE_BARRIER_WRITE
);
90 return bus_read_2(sc
->mem_res
, off
);
94 sdhci_acpi_write_2(device_t dev
, struct sdhci_slot
*slot __unused
,
95 bus_size_t off
, uint16_t val
)
97 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
99 bus_barrier(sc
->mem_res
, 0, 0xFF,
100 BUS_SPACE_BARRIER_READ
| BUS_SPACE_BARRIER_WRITE
);
101 bus_write_2(sc
->mem_res
, off
, val
);
105 sdhci_acpi_read_4(device_t dev
, struct sdhci_slot
*slot __unused
,
108 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
110 bus_barrier(sc
->mem_res
, 0, 0xFF,
111 BUS_SPACE_BARRIER_READ
| BUS_SPACE_BARRIER_WRITE
);
112 return bus_read_4(sc
->mem_res
, off
);
116 sdhci_acpi_write_4(device_t dev
, struct sdhci_slot
*slot __unused
,
117 bus_size_t off
, uint32_t val
)
119 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
121 bus_barrier(sc
->mem_res
, 0, 0xFF,
122 BUS_SPACE_BARRIER_READ
| BUS_SPACE_BARRIER_WRITE
);
123 bus_write_4(sc
->mem_res
, off
, val
);
127 sdhci_acpi_read_multi_4(device_t dev
, struct sdhci_slot
*slot __unused
,
128 bus_size_t off
, uint32_t *data
, bus_size_t count
)
130 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
132 bus_read_multi_stream_4(sc
->mem_res
, off
, data
, count
);
136 sdhci_acpi_write_multi_4(device_t dev
, struct sdhci_slot
*slot __unused
,
137 bus_size_t off
, uint32_t *data
, bus_size_t count
)
139 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
141 bus_write_multi_stream_4(sc
->mem_res
, off
, data
, count
);
144 static void sdhci_acpi_intr(void *arg
);
146 #define INTEL_ATOM_QUIRKS_SDCARD \
147 SDHCI_QUIRK_WHITELIST_ADMA2 | SDHCI_QUIRK_WAIT_WHILE_BUSY
148 #define INTEL_ATOM_QUIRKS_EMMC \
149 INTEL_ATOM_QUIRKS_SDCARD | SDHCI_QUIRK_MMC_DDR52 | \
150 SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 | SDHCI_QUIRK_PRESET_VALUE_BROKEN
156 } sdhci_devices
[] = {
157 /* The Intel Atom integrated controllers work fine with ADMA2. */
158 /* Bay Trail / Braswell */
159 {"80860F14", "1", INTEL_ATOM_QUIRKS_EMMC
},
160 {"80860F14", "3", INTEL_ATOM_QUIRKS_SDCARD
},
161 {"80860F16", NULL
, INTEL_ATOM_QUIRKS_SDCARD
},
164 SDHCI_QUIRK_BROKEN_DMA
| /* APL18 erratum */
165 INTEL_ATOM_QUIRKS_SDCARD
},
167 SDHCI_QUIRK_BROKEN_DMA
| /* APL18 erratum */
168 INTEL_ATOM_QUIRKS_EMMC
},
171 static char *sdhci_ids
[] = {
180 sdhci_acpi_probe(device_t dev
)
182 if (acpi_disabled("sdhci"))
185 if (ACPI_ID_PROBE(device_get_parent(dev
), dev
, sdhci_ids
) == NULL
)
188 device_set_desc(dev
, "SDHCI controller");
193 sdhci_acpi_attach(device_t dev
)
195 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
197 int err
, i
, rid
, quirks
;
199 id
= ACPI_ID_PROBE(device_get_parent(dev
), dev
, sdhci_ids
);
204 sc
->handle
= acpi_get_handle(dev
);
207 for (i
= 0; i
< NELEM(sdhci_devices
); i
++) {
208 if (strcmp(sdhci_devices
[i
].hid
, id
) == 0 &&
209 (sdhci_devices
[i
].uid
== NULL
||
210 acpi_MatchUid(sc
->handle
, sdhci_devices
[i
].uid
))) {
211 quirks
= sdhci_devices
[i
].quirks
;
215 quirks
&= ~sdhci_quirk_clear
;
216 quirks
|= sdhci_quirk_set
;
220 sc
->irq_res
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
, &rid
,
222 if (sc
->irq_res
== NULL
) {
223 device_printf(dev
, "Can't allocate IRQ\n");
228 /* Allocate memory. */
230 sc
->mem_res
= bus_alloc_resource_any(dev
, SYS_RES_MEMORY
, &rid
,
232 if (sc
->mem_res
== NULL
) {
233 device_printf(dev
, "Can't allocate memory for slot %d\n", 0);
238 pci_set_powerstate(dev
, PCI_POWERSTATE_D0
);
240 sc
->slot
.quirks
= quirks
;
241 if (sdhci_init_slot(dev
, &sc
->slot
, 0) != 0) {
242 device_printf(dev
, "sdhci initialization failed\n");
243 pci_set_powerstate(dev
, PCI_POWERSTATE_D3
);
248 device_printf(dev
, "%d slot(s) allocated\n", 1);
249 /* Activate the interrupt */
250 err
= bus_setup_intr(dev
, sc
->irq_res
, INTR_MPSAFE
,
251 sdhci_acpi_intr
, sc
, &sc
->intrhand
, NULL
);
253 device_printf(dev
, "Can't setup IRQ\n");
255 /* Process cards detection. */
256 sdhci_start_slot(&sc
->slot
);
261 if (sc
->irq_res
!= NULL
) {
262 bus_release_resource(dev
, SYS_RES_IRQ
,
263 rman_get_rid(sc
->irq_res
), sc
->irq_res
);
265 if (sc
->mem_res
!= NULL
) {
266 bus_release_resource(dev
, SYS_RES_MEMORY
,
267 rman_get_rid(sc
->mem_res
), sc
->mem_res
);
273 sdhci_acpi_detach(device_t dev
)
275 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
277 bus_teardown_intr(dev
, sc
->irq_res
, sc
->intrhand
);
278 bus_release_resource(dev
, SYS_RES_IRQ
,
279 rman_get_rid(sc
->irq_res
), sc
->irq_res
);
281 sdhci_cleanup_slot(&sc
->slot
);
282 bus_release_resource(dev
, SYS_RES_MEMORY
,
283 rman_get_rid(sc
->mem_res
), sc
->mem_res
);
284 pci_set_powerstate(dev
, PCI_POWERSTATE_D3
);
289 sdhci_acpi_suspend(device_t dev
)
291 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
294 err
= bus_generic_suspend(dev
);
297 sdhci_generic_suspend(&sc
->slot
);
302 sdhci_acpi_resume(device_t dev
)
304 struct sdhci_acpi_softc
*sc
= device_get_softc(dev
);
306 sdhci_generic_resume(&sc
->slot
);
307 return (bus_generic_resume(dev
));
311 sdhci_acpi_intr(void *arg
)
313 struct sdhci_acpi_softc
*sc
= (struct sdhci_acpi_softc
*)arg
;
315 sdhci_generic_intr(&sc
->slot
);
318 static device_method_t sdhci_methods
[] = {
320 DEVMETHOD(device_probe
, sdhci_acpi_probe
),
321 DEVMETHOD(device_attach
, sdhci_acpi_attach
),
322 DEVMETHOD(device_detach
, sdhci_acpi_detach
),
323 DEVMETHOD(device_suspend
, sdhci_acpi_suspend
),
324 DEVMETHOD(device_resume
, sdhci_acpi_resume
),
327 DEVMETHOD(bus_read_ivar
, sdhci_generic_read_ivar
),
328 DEVMETHOD(bus_write_ivar
, sdhci_generic_write_ivar
),
331 DEVMETHOD(mmcbr_update_ios
, sdhci_generic_update_ios
),
332 DEVMETHOD(mmcbr_switch_vccq
, sdhci_generic_switch_vccq
),
333 DEVMETHOD(mmcbr_request
, sdhci_generic_request
),
334 DEVMETHOD(mmcbr_get_ro
, sdhci_generic_get_ro
),
335 DEVMETHOD(mmcbr_acquire_host
, sdhci_generic_acquire_host
),
336 DEVMETHOD(mmcbr_release_host
, sdhci_generic_release_host
),
338 /* SDHCI accessors */
339 DEVMETHOD(sdhci_read_1
, sdhci_acpi_read_1
),
340 DEVMETHOD(sdhci_read_2
, sdhci_acpi_read_2
),
341 DEVMETHOD(sdhci_read_4
, sdhci_acpi_read_4
),
342 DEVMETHOD(sdhci_read_multi_4
, sdhci_acpi_read_multi_4
),
343 DEVMETHOD(sdhci_write_1
, sdhci_acpi_write_1
),
344 DEVMETHOD(sdhci_write_2
, sdhci_acpi_write_2
),
345 DEVMETHOD(sdhci_write_4
, sdhci_acpi_write_4
),
346 DEVMETHOD(sdhci_write_multi_4
, sdhci_acpi_write_multi_4
),
347 DEVMETHOD(sdhci_set_uhs_timing
, sdhci_generic_set_uhs_timing
),
352 static driver_t sdhci_acpi_driver
= {
355 sizeof(struct sdhci_acpi_softc
),
356 .gpri
= KOBJ_GPRI_ACPI
358 static devclass_t sdhci_acpi_devclass
;
360 DRIVER_MODULE(sdhci_acpi
, acpi
, sdhci_acpi_driver
, sdhci_acpi_devclass
, NULL
,
362 MODULE_DEPEND(sdhci_acpi
, sdhci
, 1, 1, 1);