]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/mmc/host/sdhci-acpi.c
mmc: sdhci-acpi: Set SDHCI_QUIRK2_STOP_WITH_TC for Intel host controllers
[mirror_ubuntu-artful-kernel.git] / drivers / mmc / host / sdhci-acpi.c
CommitLineData
c4e05037
AH
1/*
2 * Secure Digital Host Controller Interface ACPI driver.
3 *
4 * Copyright (c) 2012, Intel Corporation.
5 *
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.
9 *
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
13 * more details.
14 *
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.
18 *
19 */
20
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>
27#include <linux/io.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>
36#include <linux/pm.h>
37#include <linux/pm_runtime.h>
b04fa064 38#include <linux/delay.h>
c4e05037
AH
39
40#include <linux/mmc/host.h>
41#include <linux/mmc/pm.h>
4fd4409c 42#include <linux/mmc/slot-gpio.h>
c4e05037
AH
43#include <linux/mmc/sdhci.h>
44
45#include "sdhci.h"
46
47enum {
4fd4409c
AH
48 SDHCI_ACPI_SD_CD = BIT(0),
49 SDHCI_ACPI_RUNTIME_PM = BIT(1),
50 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
c4e05037
AH
51};
52
53struct sdhci_acpi_chip {
54 const struct sdhci_ops *ops;
55 unsigned int quirks;
56 unsigned int quirks2;
57 unsigned long caps;
58 unsigned int caps2;
59 mmc_pm_flag_t pm_caps;
60};
61
62struct sdhci_acpi_slot {
63 const struct sdhci_acpi_chip *chip;
64 unsigned int quirks;
65 unsigned int quirks2;
66 unsigned long caps;
67 unsigned int caps2;
68 mmc_pm_flag_t pm_caps;
69 unsigned int flags;
578b36b6
GY
70 int (*probe_slot)(struct platform_device *);
71 int (*remove_slot)(struct platform_device *);
c4e05037
AH
72};
73
74struct sdhci_acpi_host {
75 struct sdhci_host *host;
76 const struct sdhci_acpi_slot *slot;
77 struct platform_device *pdev;
78 bool use_runtime_pm;
79};
80
81static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
82{
83 return c->slot && (c->slot->flags & flag);
84}
85
86static int sdhci_acpi_enable_dma(struct sdhci_host *host)
87{
88 return 0;
89}
90
b04fa064
AH
91static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
92{
93 u8 reg;
94
95 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
96 reg |= 0x10;
97 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
98 /* For eMMC, minimum is 1us but give it 9us for good measure */
99 udelay(9);
100 reg &= ~0x10;
101 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
102 /* For eMMC, minimum is 200us but give it 300us for good measure */
103 usleep_range(300, 1000);
104}
105
c4e05037 106static const struct sdhci_ops sdhci_acpi_ops_dflt = {
1771059c 107 .set_clock = sdhci_set_clock,
c4e05037 108 .enable_dma = sdhci_acpi_enable_dma,
2317f56c 109 .set_bus_width = sdhci_set_bus_width,
03231f9b 110 .reset = sdhci_reset,
96d7b78c 111 .set_uhs_signaling = sdhci_set_uhs_signaling,
c4e05037
AH
112};
113
b04fa064 114static const struct sdhci_ops sdhci_acpi_ops_int = {
1771059c 115 .set_clock = sdhci_set_clock,
b04fa064 116 .enable_dma = sdhci_acpi_enable_dma,
2317f56c 117 .set_bus_width = sdhci_set_bus_width,
03231f9b 118 .reset = sdhci_reset,
96d7b78c 119 .set_uhs_signaling = sdhci_set_uhs_signaling,
b04fa064
AH
120 .hw_reset = sdhci_acpi_int_hw_reset,
121};
122
123static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
124 .ops = &sdhci_acpi_ops_int,
125};
126
578b36b6
GY
127static int sdhci_acpi_emmc_probe_slot(struct platform_device *pdev)
128{
129 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
130 struct sdhci_host *host;
131
132 if (!c || !c->host)
133 return 0;
134
135 host = c->host;
136
137 /* Platform specific code during emmc proble slot goes here */
138
139 return 0;
140}
141
142static int sdhci_acpi_sdio_probe_slot(struct platform_device *pdev)
143{
144 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
145 struct sdhci_host *host;
146
147 if (!c || !c->host)
148 return 0;
149
150 host = c->host;
151
152 /* Platform specific code during emmc proble slot goes here */
153
154 return 0;
155}
156
157static int sdhci_acpi_sd_probe_slot(struct platform_device *pdev)
158{
159 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
160 struct sdhci_host *host;
161
162 if (!c || !c->host || !c->slot)
163 return 0;
164
165 host = c->host;
166
167 /* Platform specific code during emmc proble slot goes here */
168
169 return 0;
170}
171
07a58883 172static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
b04fa064 173 .chip = &sdhci_acpi_chip_int,
f25c3372
MP
174 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
175 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR,
07a58883
AH
176 .caps2 = MMC_CAP2_HC_ERASE_SZ,
177 .flags = SDHCI_ACPI_RUNTIME_PM,
934e31b9 178 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | SDHCI_QUIRK2_STOP_WITH_TC,
578b36b6 179 .probe_slot = sdhci_acpi_emmc_probe_slot,
07a58883
AH
180};
181
e5571397 182static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
c6748017 183 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
e5571397
AH
184 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
185 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
186 .flags = SDHCI_ACPI_RUNTIME_PM,
187 .pm_caps = MMC_PM_KEEP_POWER,
578b36b6 188 .probe_slot = sdhci_acpi_sdio_probe_slot,
e5571397
AH
189};
190
07a58883 191static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
4fd4409c
AH
192 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
193 SDHCI_ACPI_RUNTIME_PM,
934e31b9
AH
194 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
195 SDHCI_QUIRK2_STOP_WITH_TC,
578b36b6 196 .probe_slot = sdhci_acpi_sd_probe_slot,
07a58883
AH
197};
198
199struct sdhci_acpi_uid_slot {
200 const char *hid;
201 const char *uid;
202 const struct sdhci_acpi_slot *slot;
203};
204
205static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
206 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
207 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
aad95dc4 208 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
07a58883
AH
209 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
210 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
07c001c1 211 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
07a58883
AH
212 { "PNP0D40" },
213 { },
214};
215
c4e05037 216static const struct acpi_device_id sdhci_acpi_ids[] = {
07a58883 217 { "80860F14" },
aad95dc4 218 { "80860F16" },
07a58883
AH
219 { "INT33BB" },
220 { "INT33C6" },
07c001c1 221 { "INT3436" },
07a58883 222 { "PNP0D40" },
c4e05037
AH
223 { },
224};
225MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
226
07a58883
AH
227static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
228 const char *uid)
c4e05037 229{
07a58883
AH
230 const struct sdhci_acpi_uid_slot *u;
231
232 for (u = sdhci_acpi_uids; u->hid; u++) {
233 if (strcmp(u->hid, hid))
234 continue;
235 if (!u->uid)
236 return u->slot;
237 if (uid && !strcmp(u->uid, uid))
238 return u->slot;
239 }
c4e05037
AH
240 return NULL;
241}
242
07a58883
AH
243static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
244 const char *hid)
245{
246 const struct sdhci_acpi_slot *slot;
247 struct acpi_device_info *info;
248 const char *uid = NULL;
249 acpi_status status;
250
251 status = acpi_get_object_info(handle, &info);
252 if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
253 uid = info->unique_id.string;
254
255 slot = sdhci_acpi_get_slot_by_ids(hid, uid);
256
257 kfree(info);
258 return slot;
259}
260
4e608e4e 261static int sdhci_acpi_probe(struct platform_device *pdev)
c4e05037
AH
262{
263 struct device *dev = &pdev->dev;
264 acpi_handle handle = ACPI_HANDLE(dev);
265 struct acpi_device *device;
266 struct sdhci_acpi_host *c;
267 struct sdhci_host *host;
268 struct resource *iomem;
269 resource_size_t len;
270 const char *hid;
87875655 271 int err;
c4e05037
AH
272
273 if (acpi_bus_get_device(handle, &device))
274 return -ENODEV;
275
276 if (acpi_bus_get_status(device) || !device->status.present)
277 return -ENODEV;
278
279 hid = acpi_device_hid(device);
280
281 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
282 if (!iomem)
283 return -ENOMEM;
284
285 len = resource_size(iomem);
286 if (len < 0x100)
287 dev_err(dev, "Invalid iomem size!\n");
288
289 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
290 return -ENOMEM;
291
292 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
293 if (IS_ERR(host))
294 return PTR_ERR(host);
295
296 c = sdhci_priv(host);
297 c->host = host;
07a58883 298 c->slot = sdhci_acpi_get_slot(handle, hid);
c4e05037
AH
299 c->pdev = pdev;
300 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
301
302 platform_set_drvdata(pdev, c);
303
304 host->hw_name = "ACPI";
305 host->ops = &sdhci_acpi_ops_dflt;
306 host->irq = platform_get_irq(pdev, 0);
307
308 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
309 resource_size(iomem));
310 if (host->ioaddr == NULL) {
311 err = -ENOMEM;
312 goto err_free;
313 }
314
315 if (!dev->dma_mask) {
316 u64 dma_mask;
317
318 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
319 /* 64-bit DMA is not supported at present */
320 dma_mask = DMA_BIT_MASK(32);
321 } else {
322 dma_mask = DMA_BIT_MASK(32);
323 }
324
07f4450c
RK
325 err = dma_coerce_mask_and_coherent(dev, dma_mask);
326 if (err)
327 goto err_free;
c4e05037
AH
328 }
329
330 if (c->slot) {
578b36b6
GY
331 if (c->slot->probe_slot) {
332 err = c->slot->probe_slot(pdev);
333 if (err)
334 goto err_free;
335 }
c4e05037
AH
336 if (c->slot->chip) {
337 host->ops = c->slot->chip->ops;
338 host->quirks |= c->slot->chip->quirks;
339 host->quirks2 |= c->slot->chip->quirks2;
340 host->mmc->caps |= c->slot->chip->caps;
341 host->mmc->caps2 |= c->slot->chip->caps2;
342 host->mmc->pm_caps |= c->slot->chip->pm_caps;
343 }
344 host->quirks |= c->slot->quirks;
345 host->quirks2 |= c->slot->quirks2;
346 host->mmc->caps |= c->slot->caps;
347 host->mmc->caps2 |= c->slot->caps2;
348 host->mmc->pm_caps |= c->slot->pm_caps;
349 }
350
0d3e3350
AH
351 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
352
a61abe6e 353 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
4fd4409c
AH
354 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
355
89168b48 356 if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL)) {
4fd4409c 357 dev_warn(dev, "failed to setup card detect gpio\n");
a61abe6e 358 c->use_runtime_pm = false;
4fd4409c 359 }
a61abe6e
AH
360 }
361
4fd4409c
AH
362 err = sdhci_add_host(host);
363 if (err)
364 goto err_free;
365
c4e05037 366 if (c->use_runtime_pm) {
1d1ff458 367 pm_runtime_set_active(dev);
c4e05037
AH
368 pm_suspend_ignore_children(dev, 1);
369 pm_runtime_set_autosuspend_delay(dev, 50);
370 pm_runtime_use_autosuspend(dev);
371 pm_runtime_enable(dev);
372 }
373
374 return 0;
375
376err_free:
c4e05037
AH
377 sdhci_free_host(c->host);
378 return err;
379}
380
4e608e4e 381static int sdhci_acpi_remove(struct platform_device *pdev)
c4e05037
AH
382{
383 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
384 struct device *dev = &pdev->dev;
385 int dead;
386
387 if (c->use_runtime_pm) {
388 pm_runtime_get_sync(dev);
389 pm_runtime_disable(dev);
390 pm_runtime_put_noidle(dev);
391 }
392
578b36b6
GY
393 if (c->slot && c->slot->remove_slot)
394 c->slot->remove_slot(pdev);
395
c4e05037
AH
396 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
397 sdhci_remove_host(c->host, dead);
c4e05037
AH
398 sdhci_free_host(c->host);
399
400 return 0;
401}
402
403#ifdef CONFIG_PM_SLEEP
404
405static int sdhci_acpi_suspend(struct device *dev)
406{
407 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
408
409 return sdhci_suspend_host(c->host);
410}
411
412static int sdhci_acpi_resume(struct device *dev)
413{
414 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
415
416 return sdhci_resume_host(c->host);
417}
418
419#else
420
421#define sdhci_acpi_suspend NULL
422#define sdhci_acpi_resume NULL
423
424#endif
425
426#ifdef CONFIG_PM_RUNTIME
427
428static int sdhci_acpi_runtime_suspend(struct device *dev)
429{
430 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
431
432 return sdhci_runtime_suspend_host(c->host);
433}
434
435static int sdhci_acpi_runtime_resume(struct device *dev)
436{
437 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
438
439 return sdhci_runtime_resume_host(c->host);
440}
441
442static int sdhci_acpi_runtime_idle(struct device *dev)
443{
444 return 0;
445}
446
c4e05037
AH
447#endif
448
449static const struct dev_pm_ops sdhci_acpi_pm_ops = {
450 .suspend = sdhci_acpi_suspend,
451 .resume = sdhci_acpi_resume,
1d75f74b
PG
452 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
453 sdhci_acpi_runtime_resume, sdhci_acpi_runtime_idle)
c4e05037
AH
454};
455
456static struct platform_driver sdhci_acpi_driver = {
457 .driver = {
458 .name = "sdhci-acpi",
459 .owner = THIS_MODULE,
460 .acpi_match_table = sdhci_acpi_ids,
461 .pm = &sdhci_acpi_pm_ops,
462 },
463 .probe = sdhci_acpi_probe,
4e608e4e 464 .remove = sdhci_acpi_remove,
c4e05037
AH
465};
466
467module_platform_driver(sdhci_acpi_driver);
468
469MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
470MODULE_AUTHOR("Adrian Hunter");
471MODULE_LICENSE("GPL v2");