2 * Secure Digital Host Controller Interface ACPI driver.
4 * Copyright (c) 2012, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/init.h>
22 #include <linux/export.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/platform_device.h>
26 #include <linux/ioport.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/compiler.h>
30 #include <linux/stddef.h>
31 #include <linux/bitops.h>
32 #include <linux/types.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/acpi.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/delay.h>
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/pm.h>
42 #include <linux/mmc/slot-gpio.h>
43 #include <linux/mmc/sdhci.h>
48 SDHCI_ACPI_SD_CD
= BIT(0),
49 SDHCI_ACPI_RUNTIME_PM
= BIT(1),
50 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL
= BIT(2),
53 struct sdhci_acpi_chip
{
54 const struct sdhci_ops
*ops
;
59 mmc_pm_flag_t pm_caps
;
62 struct sdhci_acpi_slot
{
63 const struct sdhci_acpi_chip
*chip
;
68 mmc_pm_flag_t pm_caps
;
72 struct sdhci_acpi_host
{
73 struct sdhci_host
*host
;
74 const struct sdhci_acpi_slot
*slot
;
75 struct platform_device
*pdev
;
79 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host
*c
, unsigned int flag
)
81 return c
->slot
&& (c
->slot
->flags
& flag
);
84 static int sdhci_acpi_enable_dma(struct sdhci_host
*host
)
89 static void sdhci_acpi_int_hw_reset(struct sdhci_host
*host
)
93 reg
= sdhci_readb(host
, SDHCI_POWER_CONTROL
);
95 sdhci_writeb(host
, reg
, SDHCI_POWER_CONTROL
);
96 /* For eMMC, minimum is 1us but give it 9us for good measure */
99 sdhci_writeb(host
, reg
, SDHCI_POWER_CONTROL
);
100 /* For eMMC, minimum is 200us but give it 300us for good measure */
101 usleep_range(300, 1000);
104 static const struct sdhci_ops sdhci_acpi_ops_dflt
= {
105 .set_clock
= sdhci_set_clock
,
106 .enable_dma
= sdhci_acpi_enable_dma
,
107 .set_bus_width
= sdhci_set_bus_width
,
108 .reset
= sdhci_reset
,
109 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
112 static const struct sdhci_ops sdhci_acpi_ops_int
= {
113 .set_clock
= sdhci_set_clock
,
114 .enable_dma
= sdhci_acpi_enable_dma
,
115 .set_bus_width
= sdhci_set_bus_width
,
116 .reset
= sdhci_reset
,
117 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
118 .hw_reset
= sdhci_acpi_int_hw_reset
,
121 static const struct sdhci_acpi_chip sdhci_acpi_chip_int
= {
122 .ops
= &sdhci_acpi_ops_int
,
125 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc
= {
126 .chip
= &sdhci_acpi_chip_int
,
127 .caps
= MMC_CAP_8_BIT_DATA
| MMC_CAP_NONREMOVABLE
| MMC_CAP_HW_RESET
,
128 .caps2
= MMC_CAP2_HC_ERASE_SZ
,
129 .flags
= SDHCI_ACPI_RUNTIME_PM
,
132 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio
= {
133 .quirks
= SDHCI_QUIRK_BROKEN_CARD_DETECTION
,
134 .quirks2
= SDHCI_QUIRK2_HOST_OFF_CARD_ON
,
135 .caps
= MMC_CAP_NONREMOVABLE
| MMC_CAP_POWER_OFF_CARD
,
136 .flags
= SDHCI_ACPI_RUNTIME_PM
,
137 .pm_caps
= MMC_PM_KEEP_POWER
,
140 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd
= {
141 .flags
= SDHCI_ACPI_SD_CD
| SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL
|
142 SDHCI_ACPI_RUNTIME_PM
,
143 .quirks2
= SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON
,
146 struct sdhci_acpi_uid_slot
{
149 const struct sdhci_acpi_slot
*slot
;
152 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids
[] = {
153 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc
},
154 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd
},
155 { "80860F16" , NULL
, &sdhci_acpi_slot_int_sd
},
156 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio
},
157 { "INT33C6" , NULL
, &sdhci_acpi_slot_int_sdio
},
158 { "INT3436" , NULL
, &sdhci_acpi_slot_int_sdio
},
163 static const struct acpi_device_id sdhci_acpi_ids
[] = {
172 MODULE_DEVICE_TABLE(acpi
, sdhci_acpi_ids
);
174 static const struct sdhci_acpi_slot
*sdhci_acpi_get_slot_by_ids(const char *hid
,
177 const struct sdhci_acpi_uid_slot
*u
;
179 for (u
= sdhci_acpi_uids
; u
->hid
; u
++) {
180 if (strcmp(u
->hid
, hid
))
184 if (uid
&& !strcmp(u
->uid
, uid
))
190 static const struct sdhci_acpi_slot
*sdhci_acpi_get_slot(acpi_handle handle
,
193 const struct sdhci_acpi_slot
*slot
;
194 struct acpi_device_info
*info
;
195 const char *uid
= NULL
;
198 status
= acpi_get_object_info(handle
, &info
);
199 if (!ACPI_FAILURE(status
) && (info
->valid
& ACPI_VALID_UID
))
200 uid
= info
->unique_id
.string
;
202 slot
= sdhci_acpi_get_slot_by_ids(hid
, uid
);
208 static int sdhci_acpi_probe(struct platform_device
*pdev
)
210 struct device
*dev
= &pdev
->dev
;
211 acpi_handle handle
= ACPI_HANDLE(dev
);
212 struct acpi_device
*device
;
213 struct sdhci_acpi_host
*c
;
214 struct sdhci_host
*host
;
215 struct resource
*iomem
;
220 if (acpi_bus_get_device(handle
, &device
))
223 if (acpi_bus_get_status(device
) || !device
->status
.present
)
226 hid
= acpi_device_hid(device
);
228 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
232 len
= resource_size(iomem
);
234 dev_err(dev
, "Invalid iomem size!\n");
236 if (!devm_request_mem_region(dev
, iomem
->start
, len
, dev_name(dev
)))
239 host
= sdhci_alloc_host(dev
, sizeof(struct sdhci_acpi_host
));
241 return PTR_ERR(host
);
243 c
= sdhci_priv(host
);
245 c
->slot
= sdhci_acpi_get_slot(handle
, hid
);
247 c
->use_runtime_pm
= sdhci_acpi_flag(c
, SDHCI_ACPI_RUNTIME_PM
);
249 platform_set_drvdata(pdev
, c
);
251 host
->hw_name
= "ACPI";
252 host
->ops
= &sdhci_acpi_ops_dflt
;
253 host
->irq
= platform_get_irq(pdev
, 0);
255 host
->ioaddr
= devm_ioremap_nocache(dev
, iomem
->start
,
256 resource_size(iomem
));
257 if (host
->ioaddr
== NULL
) {
262 if (!dev
->dma_mask
) {
265 if (sdhci_readl(host
, SDHCI_CAPABILITIES
) & SDHCI_CAN_64BIT
) {
266 /* 64-bit DMA is not supported at present */
267 dma_mask
= DMA_BIT_MASK(32);
269 dma_mask
= DMA_BIT_MASK(32);
272 err
= dma_coerce_mask_and_coherent(dev
, dma_mask
);
279 host
->ops
= c
->slot
->chip
->ops
;
280 host
->quirks
|= c
->slot
->chip
->quirks
;
281 host
->quirks2
|= c
->slot
->chip
->quirks2
;
282 host
->mmc
->caps
|= c
->slot
->chip
->caps
;
283 host
->mmc
->caps2
|= c
->slot
->chip
->caps2
;
284 host
->mmc
->pm_caps
|= c
->slot
->chip
->pm_caps
;
286 host
->quirks
|= c
->slot
->quirks
;
287 host
->quirks2
|= c
->slot
->quirks2
;
288 host
->mmc
->caps
|= c
->slot
->caps
;
289 host
->mmc
->caps2
|= c
->slot
->caps2
;
290 host
->mmc
->pm_caps
|= c
->slot
->pm_caps
;
293 host
->mmc
->caps2
|= MMC_CAP2_NO_PRESCAN_POWERUP
;
295 if (sdhci_acpi_flag(c
, SDHCI_ACPI_SD_CD
)) {
296 bool v
= sdhci_acpi_flag(c
, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL
);
298 if (mmc_gpiod_request_cd(host
->mmc
, NULL
, 0, v
, 0)) {
299 dev_warn(dev
, "failed to setup card detect gpio\n");
300 c
->use_runtime_pm
= false;
304 err
= sdhci_add_host(host
);
308 if (c
->use_runtime_pm
) {
309 pm_runtime_set_active(dev
);
310 pm_suspend_ignore_children(dev
, 1);
311 pm_runtime_set_autosuspend_delay(dev
, 50);
312 pm_runtime_use_autosuspend(dev
);
313 pm_runtime_enable(dev
);
319 sdhci_free_host(c
->host
);
323 static int sdhci_acpi_remove(struct platform_device
*pdev
)
325 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
326 struct device
*dev
= &pdev
->dev
;
329 if (c
->use_runtime_pm
) {
330 pm_runtime_get_sync(dev
);
331 pm_runtime_disable(dev
);
332 pm_runtime_put_noidle(dev
);
335 dead
= (sdhci_readl(c
->host
, SDHCI_INT_STATUS
) == ~0);
336 sdhci_remove_host(c
->host
, dead
);
337 sdhci_free_host(c
->host
);
342 #ifdef CONFIG_PM_SLEEP
344 static int sdhci_acpi_suspend(struct device
*dev
)
346 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
348 return sdhci_suspend_host(c
->host
);
351 static int sdhci_acpi_resume(struct device
*dev
)
353 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
355 return sdhci_resume_host(c
->host
);
360 #define sdhci_acpi_suspend NULL
361 #define sdhci_acpi_resume NULL
365 #ifdef CONFIG_PM_RUNTIME
367 static int sdhci_acpi_runtime_suspend(struct device
*dev
)
369 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
371 return sdhci_runtime_suspend_host(c
->host
);
374 static int sdhci_acpi_runtime_resume(struct device
*dev
)
376 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
378 return sdhci_runtime_resume_host(c
->host
);
381 static int sdhci_acpi_runtime_idle(struct device
*dev
)
388 #define sdhci_acpi_runtime_suspend NULL
389 #define sdhci_acpi_runtime_resume NULL
390 #define sdhci_acpi_runtime_idle NULL
394 static const struct dev_pm_ops sdhci_acpi_pm_ops
= {
395 .suspend
= sdhci_acpi_suspend
,
396 .resume
= sdhci_acpi_resume
,
397 .runtime_suspend
= sdhci_acpi_runtime_suspend
,
398 .runtime_resume
= sdhci_acpi_runtime_resume
,
399 .runtime_idle
= sdhci_acpi_runtime_idle
,
402 static struct platform_driver sdhci_acpi_driver
= {
404 .name
= "sdhci-acpi",
405 .owner
= THIS_MODULE
,
406 .acpi_match_table
= sdhci_acpi_ids
,
407 .pm
= &sdhci_acpi_pm_ops
,
409 .probe
= sdhci_acpi_probe
,
410 .remove
= sdhci_acpi_remove
,
413 module_platform_driver(sdhci_acpi_driver
);
415 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
416 MODULE_AUTHOR("Adrian Hunter");
417 MODULE_LICENSE("GPL v2");