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
;
80 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host
*c
, unsigned int flag
)
82 return c
->slot
&& (c
->slot
->flags
& flag
);
85 static void sdhci_acpi_int_hw_reset(struct sdhci_host
*host
)
89 reg
= sdhci_readb(host
, SDHCI_POWER_CONTROL
);
91 sdhci_writeb(host
, reg
, SDHCI_POWER_CONTROL
);
92 /* For eMMC, minimum is 1us but give it 9us for good measure */
95 sdhci_writeb(host
, reg
, SDHCI_POWER_CONTROL
);
96 /* For eMMC, minimum is 200us but give it 300us for good measure */
97 usleep_range(300, 1000);
100 static const struct sdhci_ops sdhci_acpi_ops_dflt
= {
101 .set_clock
= sdhci_set_clock
,
102 .set_bus_width
= sdhci_set_bus_width
,
103 .reset
= sdhci_reset
,
104 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
107 static const struct sdhci_ops sdhci_acpi_ops_int
= {
108 .set_clock
= sdhci_set_clock
,
109 .set_bus_width
= sdhci_set_bus_width
,
110 .reset
= sdhci_reset
,
111 .set_uhs_signaling
= sdhci_set_uhs_signaling
,
112 .hw_reset
= sdhci_acpi_int_hw_reset
,
115 static const struct sdhci_acpi_chip sdhci_acpi_chip_int
= {
116 .ops
= &sdhci_acpi_ops_int
,
119 static int bxt_get_cd(struct mmc_host
*mmc
)
121 int gpio_cd
= mmc_gpio_get_cd(mmc
);
122 struct sdhci_host
*host
= mmc_priv(mmc
);
129 pm_runtime_get_sync(mmc
->parent
);
131 spin_lock_irqsave(&host
->lock
, flags
);
133 if (host
->flags
& SDHCI_DEVICE_DEAD
)
136 ret
= !!(sdhci_readl(host
, SDHCI_PRESENT_STATE
) & SDHCI_CARD_PRESENT
);
138 spin_unlock_irqrestore(&host
->lock
, flags
);
140 pm_runtime_mark_last_busy(mmc
->parent
);
141 pm_runtime_put_autosuspend(mmc
->parent
);
146 static int sdhci_acpi_emmc_probe_slot(struct platform_device
*pdev
,
147 const char *hid
, const char *uid
)
149 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
150 struct sdhci_host
*host
;
157 /* Platform specific code during emmc probe slot goes here */
159 if (hid
&& uid
&& !strcmp(hid
, "80860F14") && !strcmp(uid
, "1") &&
160 sdhci_readl(host
, SDHCI_CAPABILITIES
) == 0x446cc8b2 &&
161 sdhci_readl(host
, SDHCI_CAPABILITIES_1
) == 0x00000807)
162 host
->timeout_clk
= 1000; /* 1000 kHz i.e. 1 MHz */
167 static int sdhci_acpi_sdio_probe_slot(struct platform_device
*pdev
,
168 const char *hid
, const char *uid
)
170 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
171 struct sdhci_host
*host
;
178 /* Platform specific code during sdio probe slot goes here */
183 static int sdhci_acpi_sd_probe_slot(struct platform_device
*pdev
,
184 const char *hid
, const char *uid
)
186 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
187 struct sdhci_host
*host
;
189 if (!c
|| !c
->host
|| !c
->slot
)
194 /* Platform specific code during sd probe slot goes here */
196 if (hid
&& !strcmp(hid
, "80865ACA"))
197 host
->mmc_host_ops
.get_cd
= bxt_get_cd
;
202 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc
= {
203 .chip
= &sdhci_acpi_chip_int
,
204 .caps
= MMC_CAP_8_BIT_DATA
| MMC_CAP_NONREMOVABLE
|
205 MMC_CAP_HW_RESET
| MMC_CAP_1_8V_DDR
|
206 MMC_CAP_BUS_WIDTH_TEST
| MMC_CAP_WAIT_WHILE_BUSY
,
207 .caps2
= MMC_CAP2_HC_ERASE_SZ
,
208 .flags
= SDHCI_ACPI_RUNTIME_PM
,
209 .quirks
= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
,
210 .quirks2
= SDHCI_QUIRK2_PRESET_VALUE_BROKEN
|
211 SDHCI_QUIRK2_STOP_WITH_TC
|
212 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400
,
213 .probe_slot
= sdhci_acpi_emmc_probe_slot
,
216 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio
= {
217 .quirks
= SDHCI_QUIRK_BROKEN_CARD_DETECTION
|
218 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
,
219 .quirks2
= SDHCI_QUIRK2_HOST_OFF_CARD_ON
,
220 .caps
= MMC_CAP_NONREMOVABLE
| MMC_CAP_POWER_OFF_CARD
|
221 MMC_CAP_BUS_WIDTH_TEST
| MMC_CAP_WAIT_WHILE_BUSY
,
222 .flags
= SDHCI_ACPI_RUNTIME_PM
,
223 .pm_caps
= MMC_PM_KEEP_POWER
,
224 .probe_slot
= sdhci_acpi_sdio_probe_slot
,
227 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd
= {
228 .flags
= SDHCI_ACPI_SD_CD
| SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL
|
229 SDHCI_ACPI_RUNTIME_PM
,
230 .quirks
= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
,
231 .quirks2
= SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON
|
232 SDHCI_QUIRK2_STOP_WITH_TC
,
233 .caps
= MMC_CAP_BUS_WIDTH_TEST
| MMC_CAP_WAIT_WHILE_BUSY
,
234 .probe_slot
= sdhci_acpi_sd_probe_slot
,
237 static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v
= {
238 .quirks
= SDHCI_QUIRK_BROKEN_CARD_DETECTION
,
239 .quirks2
= SDHCI_QUIRK2_NO_1_8_V
,
240 .caps
= MMC_CAP_NONREMOVABLE
,
243 static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd
= {
244 .quirks
= SDHCI_QUIRK_BROKEN_CARD_DETECTION
,
245 .caps
= MMC_CAP_NONREMOVABLE
,
248 struct sdhci_acpi_uid_slot
{
251 const struct sdhci_acpi_slot
*slot
;
254 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids
[] = {
255 { "80865ACA", NULL
, &sdhci_acpi_slot_int_sd
},
256 { "80865ACC", NULL
, &sdhci_acpi_slot_int_emmc
},
257 { "80865AD0", NULL
, &sdhci_acpi_slot_int_sdio
},
258 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc
},
259 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd
},
260 { "80860F16" , NULL
, &sdhci_acpi_slot_int_sd
},
261 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio
},
262 { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd
},
263 { "INT33C6" , NULL
, &sdhci_acpi_slot_int_sdio
},
264 { "INT3436" , NULL
, &sdhci_acpi_slot_int_sdio
},
265 { "INT344D" , NULL
, &sdhci_acpi_slot_int_sdio
},
266 { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd
},
268 { "QCOM8051", NULL
, &sdhci_acpi_slot_qcom_sd_3v
},
269 { "QCOM8052", NULL
, &sdhci_acpi_slot_qcom_sd
},
273 static const struct acpi_device_id sdhci_acpi_ids
[] = {
288 MODULE_DEVICE_TABLE(acpi
, sdhci_acpi_ids
);
290 static const struct sdhci_acpi_slot
*sdhci_acpi_get_slot(const char *hid
,
293 const struct sdhci_acpi_uid_slot
*u
;
295 for (u
= sdhci_acpi_uids
; u
->hid
; u
++) {
296 if (strcmp(u
->hid
, hid
))
300 if (uid
&& !strcmp(u
->uid
, uid
))
306 static int sdhci_acpi_probe(struct platform_device
*pdev
)
308 struct device
*dev
= &pdev
->dev
;
309 acpi_handle handle
= ACPI_HANDLE(dev
);
310 struct acpi_device
*device
;
311 struct sdhci_acpi_host
*c
;
312 struct sdhci_host
*host
;
313 struct resource
*iomem
;
319 if (acpi_bus_get_device(handle
, &device
))
322 if (acpi_bus_get_status(device
) || !device
->status
.present
)
325 hid
= acpi_device_hid(device
);
326 uid
= device
->pnp
.unique_id
;
328 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
332 len
= resource_size(iomem
);
334 dev_err(dev
, "Invalid iomem size!\n");
336 if (!devm_request_mem_region(dev
, iomem
->start
, len
, dev_name(dev
)))
339 host
= sdhci_alloc_host(dev
, sizeof(struct sdhci_acpi_host
));
341 return PTR_ERR(host
);
343 c
= sdhci_priv(host
);
345 c
->slot
= sdhci_acpi_get_slot(hid
, uid
);
347 c
->use_runtime_pm
= sdhci_acpi_flag(c
, SDHCI_ACPI_RUNTIME_PM
);
349 platform_set_drvdata(pdev
, c
);
351 host
->hw_name
= "ACPI";
352 host
->ops
= &sdhci_acpi_ops_dflt
;
353 host
->irq
= platform_get_irq(pdev
, 0);
355 host
->ioaddr
= devm_ioremap_nocache(dev
, iomem
->start
,
356 resource_size(iomem
));
357 if (host
->ioaddr
== NULL
) {
363 if (c
->slot
->probe_slot
) {
364 err
= c
->slot
->probe_slot(pdev
, hid
, uid
);
369 host
->ops
= c
->slot
->chip
->ops
;
370 host
->quirks
|= c
->slot
->chip
->quirks
;
371 host
->quirks2
|= c
->slot
->chip
->quirks2
;
372 host
->mmc
->caps
|= c
->slot
->chip
->caps
;
373 host
->mmc
->caps2
|= c
->slot
->chip
->caps2
;
374 host
->mmc
->pm_caps
|= c
->slot
->chip
->pm_caps
;
376 host
->quirks
|= c
->slot
->quirks
;
377 host
->quirks2
|= c
->slot
->quirks2
;
378 host
->mmc
->caps
|= c
->slot
->caps
;
379 host
->mmc
->caps2
|= c
->slot
->caps2
;
380 host
->mmc
->pm_caps
|= c
->slot
->pm_caps
;
383 host
->mmc
->caps2
|= MMC_CAP2_NO_PRESCAN_POWERUP
;
385 if (sdhci_acpi_flag(c
, SDHCI_ACPI_SD_CD
)) {
386 bool v
= sdhci_acpi_flag(c
, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL
);
388 if (mmc_gpiod_request_cd(host
->mmc
, NULL
, 0, v
, 0, NULL
)) {
389 dev_warn(dev
, "failed to setup card detect gpio\n");
390 c
->use_runtime_pm
= false;
394 err
= sdhci_add_host(host
);
398 if (c
->use_runtime_pm
) {
399 pm_runtime_set_active(dev
);
400 pm_suspend_ignore_children(dev
, 1);
401 pm_runtime_set_autosuspend_delay(dev
, 50);
402 pm_runtime_use_autosuspend(dev
);
403 pm_runtime_enable(dev
);
406 device_enable_async_suspend(dev
);
411 sdhci_free_host(c
->host
);
415 static int sdhci_acpi_remove(struct platform_device
*pdev
)
417 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
418 struct device
*dev
= &pdev
->dev
;
421 if (c
->use_runtime_pm
) {
422 pm_runtime_get_sync(dev
);
423 pm_runtime_disable(dev
);
424 pm_runtime_put_noidle(dev
);
427 if (c
->slot
&& c
->slot
->remove_slot
)
428 c
->slot
->remove_slot(pdev
);
430 dead
= (sdhci_readl(c
->host
, SDHCI_INT_STATUS
) == ~0);
431 sdhci_remove_host(c
->host
, dead
);
432 sdhci_free_host(c
->host
);
437 #ifdef CONFIG_PM_SLEEP
439 static int sdhci_acpi_suspend(struct device
*dev
)
441 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
443 return sdhci_suspend_host(c
->host
);
446 static int sdhci_acpi_resume(struct device
*dev
)
448 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
450 return sdhci_resume_host(c
->host
);
455 #define sdhci_acpi_suspend NULL
456 #define sdhci_acpi_resume NULL
462 static int sdhci_acpi_runtime_suspend(struct device
*dev
)
464 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
466 return sdhci_runtime_suspend_host(c
->host
);
469 static int sdhci_acpi_runtime_resume(struct device
*dev
)
471 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
473 return sdhci_runtime_resume_host(c
->host
);
478 static const struct dev_pm_ops sdhci_acpi_pm_ops
= {
479 .suspend
= sdhci_acpi_suspend
,
480 .resume
= sdhci_acpi_resume
,
481 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend
,
482 sdhci_acpi_runtime_resume
, NULL
)
485 static struct platform_driver sdhci_acpi_driver
= {
487 .name
= "sdhci-acpi",
488 .acpi_match_table
= sdhci_acpi_ids
,
489 .pm
= &sdhci_acpi_pm_ops
,
491 .probe
= sdhci_acpi_probe
,
492 .remove
= sdhci_acpi_remove
,
495 module_platform_driver(sdhci_acpi_driver
);
497 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
498 MODULE_AUTHOR("Adrian Hunter");
499 MODULE_LICENSE("GPL v2");