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>
47 SDHCI_ACPI_SD_CD
= BIT(0),
48 SDHCI_ACPI_RUNTIME_PM
= BIT(1),
49 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL
= BIT(2),
52 struct sdhci_acpi_chip
{
53 const struct sdhci_ops
*ops
;
58 mmc_pm_flag_t pm_caps
;
61 struct sdhci_acpi_slot
{
62 const struct sdhci_acpi_chip
*chip
;
67 mmc_pm_flag_t pm_caps
;
69 int (*probe_slot
)(struct platform_device
*, const char *, const char *);
70 int (*remove_slot
)(struct platform_device
*);
73 struct sdhci_acpi_host
{
74 struct sdhci_host
*host
;
75 const struct sdhci_acpi_slot
*slot
;
76 struct platform_device
*pdev
;
81 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host
*c
, unsigned int flag
)
83 return c
->slot
&& (c
->slot
->flags
& flag
);
86 static int sdhci_acpi_enable_dma(struct sdhci_host
*host
)
88 struct sdhci_acpi_host
*c
= sdhci_priv(host
);
89 struct device
*dev
= &c
->pdev
->dev
;
95 if (host
->flags
& SDHCI_USE_64_BIT_DMA
) {
96 if (host
->quirks2
& SDHCI_QUIRK2_BROKEN_64_BIT_DMA
) {
97 host
->flags
&= ~SDHCI_USE_64_BIT_DMA
;
99 err
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64));
101 dev_warn(dev
, "Failed to set 64-bit DMA mask\n");
106 err
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32));
113 static void sdhci_acpi_int_hw_reset(struct sdhci_host
*host
)
117 reg
= sdhci_readb(host
, SDHCI_POWER_CONTROL
);
119 sdhci_writeb(host
, reg
, SDHCI_POWER_CONTROL
);
120 /* For eMMC, minimum is 1us but give it 9us for good measure */
123 sdhci_writeb(host
, reg
, SDHCI_POWER_CONTROL
);
124 /* For eMMC, minimum is 200us but give it 300us for good measure */
125 usleep_range(300, 1000);
128 static const struct sdhci_ops sdhci_acpi_ops_dflt
= {
129 .set_clock
= sdhci_set_clock
,
130 .enable_dma
= sdhci_acpi_enable_dma
,
131 .set_bus_width
= sdhci_set_bus_width
,
132 .reset
= sdhci_reset
,
133 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
136 static const struct sdhci_ops sdhci_acpi_ops_int
= {
137 .set_clock
= sdhci_set_clock
,
138 .enable_dma
= sdhci_acpi_enable_dma
,
139 .set_bus_width
= sdhci_set_bus_width
,
140 .reset
= sdhci_reset
,
141 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
142 .hw_reset
= sdhci_acpi_int_hw_reset
,
145 static const struct sdhci_acpi_chip sdhci_acpi_chip_int
= {
146 .ops
= &sdhci_acpi_ops_int
,
149 static int bxt_get_cd(struct mmc_host
*mmc
)
151 int gpio_cd
= mmc_gpio_get_cd(mmc
);
152 struct sdhci_host
*host
= mmc_priv(mmc
);
159 pm_runtime_get_sync(mmc
->parent
);
161 spin_lock_irqsave(&host
->lock
, flags
);
163 if (host
->flags
& SDHCI_DEVICE_DEAD
)
166 ret
= !!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
);
168 spin_unlock_irqrestore(&host
->lock
, flags
);
170 pm_runtime_mark_last_busy(mmc
->parent
);
171 pm_runtime_put_autosuspend(mmc
->parent
);
176 static int sdhci_acpi_emmc_probe_slot(struct platform_device
*pdev
,
177 const char *hid
, const char *uid
)
179 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
180 struct sdhci_host
*host
;
187 /* Platform specific code during emmc probe slot goes here */
189 if (hid
&& uid
&& !strcmp(hid
, "80860F14") && !strcmp(uid
, "1") &&
190 sdhci_readl(host
, SDHCI_CAPABILITIES
) == 0x446cc8b2 &&
191 sdhci_readl(host
, SDHCI_CAPABILITIES_1
) == 0x00000807)
192 host
->timeout_clk
= 1000; /* 1000 kHz i.e. 1 MHz */
197 static int sdhci_acpi_sdio_probe_slot(struct platform_device
*pdev
,
198 const char *hid
, const char *uid
)
200 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
201 struct sdhci_host
*host
;
208 /* Platform specific code during sdio probe slot goes here */
213 static int sdhci_acpi_sd_probe_slot(struct platform_device
*pdev
,
214 const char *hid
, const char *uid
)
216 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
217 struct sdhci_host
*host
;
219 if (!c
|| !c
->host
|| !c
->slot
)
224 /* Platform specific code during sd probe slot goes here */
226 if (hid
&& !strcmp(hid
, "80865ACA"))
227 host
->mmc_host_ops
.get_cd
= bxt_get_cd
;
232 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc
= {
233 .chip
= &sdhci_acpi_chip_int
,
234 .caps
= MMC_CAP_8_BIT_DATA
| MMC_CAP_NONREMOVABLE
|
235 MMC_CAP_HW_RESET
| MMC_CAP_1_8V_DDR
|
236 MMC_CAP_BUS_WIDTH_TEST
| MMC_CAP_WAIT_WHILE_BUSY
,
237 .caps2
= MMC_CAP2_HC_ERASE_SZ
,
238 .flags
= SDHCI_ACPI_RUNTIME_PM
,
239 .quirks
= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
,
240 .quirks2
= SDHCI_QUIRK2_PRESET_VALUE_BROKEN
|
241 SDHCI_QUIRK2_STOP_WITH_TC
|
242 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400
,
243 .probe_slot
= sdhci_acpi_emmc_probe_slot
,
246 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio
= {
247 .quirks
= SDHCI_QUIRK_BROKEN_CARD_DETECTION
|
248 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
,
249 .quirks2
= SDHCI_QUIRK2_HOST_OFF_CARD_ON
,
250 .caps
= MMC_CAP_NONREMOVABLE
| MMC_CAP_POWER_OFF_CARD
|
251 MMC_CAP_BUS_WIDTH_TEST
| MMC_CAP_WAIT_WHILE_BUSY
,
252 .flags
= SDHCI_ACPI_RUNTIME_PM
,
253 .pm_caps
= MMC_PM_KEEP_POWER
,
254 .probe_slot
= sdhci_acpi_sdio_probe_slot
,
257 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd
= {
258 .flags
= SDHCI_ACPI_SD_CD
| SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL
|
259 SDHCI_ACPI_RUNTIME_PM
,
260 .quirks
= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
,
261 .quirks2
= SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON
|
262 SDHCI_QUIRK2_STOP_WITH_TC
,
263 .caps
= MMC_CAP_BUS_WIDTH_TEST
| MMC_CAP_WAIT_WHILE_BUSY
,
264 .probe_slot
= sdhci_acpi_sd_probe_slot
,
267 struct sdhci_acpi_uid_slot
{
270 const struct sdhci_acpi_slot
*slot
;
273 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids
[] = {
274 { "80865ACA", NULL
, &sdhci_acpi_slot_int_sd
},
275 { "80865ACC", NULL
, &sdhci_acpi_slot_int_emmc
},
276 { "80865AD0", NULL
, &sdhci_acpi_slot_int_sdio
},
277 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc
},
278 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd
},
279 { "80860F16" , NULL
, &sdhci_acpi_slot_int_sd
},
280 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio
},
281 { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd
},
282 { "INT33C6" , NULL
, &sdhci_acpi_slot_int_sdio
},
283 { "INT3436" , NULL
, &sdhci_acpi_slot_int_sdio
},
284 { "INT344D" , NULL
, &sdhci_acpi_slot_int_sdio
},
285 { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd
},
290 static const struct acpi_device_id sdhci_acpi_ids
[] = {
303 MODULE_DEVICE_TABLE(acpi
, sdhci_acpi_ids
);
305 static const struct sdhci_acpi_slot
*sdhci_acpi_get_slot(const char *hid
,
308 const struct sdhci_acpi_uid_slot
*u
;
310 for (u
= sdhci_acpi_uids
; u
->hid
; u
++) {
311 if (strcmp(u
->hid
, hid
))
315 if (uid
&& !strcmp(u
->uid
, uid
))
321 static int sdhci_acpi_probe(struct platform_device
*pdev
)
323 struct device
*dev
= &pdev
->dev
;
324 acpi_handle handle
= ACPI_HANDLE(dev
);
325 struct acpi_device
*device
;
326 struct sdhci_acpi_host
*c
;
327 struct sdhci_host
*host
;
328 struct resource
*iomem
;
334 if (acpi_bus_get_device(handle
, &device
))
337 if (acpi_bus_get_status(device
) || !device
->status
.present
)
340 hid
= acpi_device_hid(device
);
341 uid
= device
->pnp
.unique_id
;
343 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
347 len
= resource_size(iomem
);
349 dev_err(dev
, "Invalid iomem size!\n");
351 if (!devm_request_mem_region(dev
, iomem
->start
, len
, dev_name(dev
)))
354 host
= sdhci_alloc_host(dev
, sizeof(struct sdhci_acpi_host
));
356 return PTR_ERR(host
);
358 c
= sdhci_priv(host
);
360 c
->slot
= sdhci_acpi_get_slot(hid
, uid
);
362 c
->use_runtime_pm
= sdhci_acpi_flag(c
, SDHCI_ACPI_RUNTIME_PM
);
364 platform_set_drvdata(pdev
, c
);
366 host
->hw_name
= "ACPI";
367 host
->ops
= &sdhci_acpi_ops_dflt
;
368 host
->irq
= platform_get_irq(pdev
, 0);
370 host
->ioaddr
= devm_ioremap_nocache(dev
, iomem
->start
,
371 resource_size(iomem
));
372 if (host
->ioaddr
== NULL
) {
378 if (c
->slot
->probe_slot
) {
379 err
= c
->slot
->probe_slot(pdev
, hid
, uid
);
384 host
->ops
= c
->slot
->chip
->ops
;
385 host
->quirks
|= c
->slot
->chip
->quirks
;
386 host
->quirks2
|= c
->slot
->chip
->quirks2
;
387 host
->mmc
->caps
|= c
->slot
->chip
->caps
;
388 host
->mmc
->caps2
|= c
->slot
->chip
->caps2
;
389 host
->mmc
->pm_caps
|= c
->slot
->chip
->pm_caps
;
391 host
->quirks
|= c
->slot
->quirks
;
392 host
->quirks2
|= c
->slot
->quirks2
;
393 host
->mmc
->caps
|= c
->slot
->caps
;
394 host
->mmc
->caps2
|= c
->slot
->caps2
;
395 host
->mmc
->pm_caps
|= c
->slot
->pm_caps
;
398 host
->mmc
->caps2
|= MMC_CAP2_NO_PRESCAN_POWERUP
;
400 if (sdhci_acpi_flag(c
, SDHCI_ACPI_SD_CD
)) {
401 bool v
= sdhci_acpi_flag(c
, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL
);
403 if (mmc_gpiod_request_cd(host
->mmc
, NULL
, 0, v
, 0, NULL
)) {
404 dev_warn(dev
, "failed to setup card detect gpio\n");
405 c
->use_runtime_pm
= false;
409 err
= sdhci_add_host(host
);
413 if (c
->use_runtime_pm
) {
414 pm_runtime_set_active(dev
);
415 pm_suspend_ignore_children(dev
, 1);
416 pm_runtime_set_autosuspend_delay(dev
, 50);
417 pm_runtime_use_autosuspend(dev
);
418 pm_runtime_enable(dev
);
424 sdhci_free_host(c
->host
);
428 static int sdhci_acpi_remove(struct platform_device
*pdev
)
430 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
431 struct device
*dev
= &pdev
->dev
;
434 if (c
->use_runtime_pm
) {
435 pm_runtime_get_sync(dev
);
436 pm_runtime_disable(dev
);
437 pm_runtime_put_noidle(dev
);
440 if (c
->slot
&& c
->slot
->remove_slot
)
441 c
->slot
->remove_slot(pdev
);
443 dead
= (sdhci_readl(c
->host
, SDHCI_INT_STATUS
) == ~0);
444 sdhci_remove_host(c
->host
, dead
);
445 sdhci_free_host(c
->host
);
450 #ifdef CONFIG_PM_SLEEP
452 static int sdhci_acpi_suspend(struct device
*dev
)
454 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
456 return sdhci_suspend_host(c
->host
);
459 static int sdhci_acpi_resume(struct device
*dev
)
461 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
463 return sdhci_resume_host(c
->host
);
468 #define sdhci_acpi_suspend NULL
469 #define sdhci_acpi_resume NULL
475 static int sdhci_acpi_runtime_suspend(struct device
*dev
)
477 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
479 return sdhci_runtime_suspend_host(c
->host
);
482 static int sdhci_acpi_runtime_resume(struct device
*dev
)
484 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
486 return sdhci_runtime_resume_host(c
->host
);
491 static const struct dev_pm_ops sdhci_acpi_pm_ops
= {
492 .suspend
= sdhci_acpi_suspend
,
493 .resume
= sdhci_acpi_resume
,
494 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend
,
495 sdhci_acpi_runtime_resume
, NULL
)
498 static struct platform_driver sdhci_acpi_driver
= {
500 .name
= "sdhci-acpi",
501 .acpi_match_table
= sdhci_acpi_ids
,
502 .pm
= &sdhci_acpi_pm_ops
,
504 .probe
= sdhci_acpi_probe
,
505 .remove
= sdhci_acpi_remove
,
508 module_platform_driver(sdhci_acpi_driver
);
510 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
511 MODULE_AUTHOR("Adrian Hunter");
512 MODULE_LICENSE("GPL v2");