blob: 3483c089baa7eb840e8a88b36c9d96283d63575e [file] [log] [blame]
Adrian Hunterc4e05032012-11-23 21:17:34 +01001/*
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>
Adrian Hunterb04fa062013-06-13 11:50:27 +030038#include <linux/delay.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010039
40#include <linux/mmc/host.h>
41#include <linux/mmc/pm.h>
Adrian Hunter4fd44092014-03-10 15:02:42 +020042#include <linux/mmc/slot-gpio.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010043#include <linux/mmc/sdhci.h>
44
45#include "sdhci.h"
46
47enum {
Adrian Hunter4fd44092014-03-10 15:02:42 +020048 SDHCI_ACPI_SD_CD = BIT(0),
49 SDHCI_ACPI_RUNTIME_PM = BIT(1),
50 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
Adrian Hunterc4e05032012-11-23 21:17:34 +010051};
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;
Gao, Yunpeng578b36b2014-09-01 11:35:40 +080070 int (*probe_slot)(struct platform_device *);
71 int (*remove_slot)(struct platform_device *);
Adrian Hunterc4e05032012-11-23 21:17:34 +010072};
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
Adrian Hunterb04fa062013-06-13 11:50:27 +030091static 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
Adrian Hunterc4e05032012-11-23 21:17:34 +0100106static const struct sdhci_ops sdhci_acpi_ops_dflt = {
Russell King17710592014-04-25 12:58:55 +0100107 .set_clock = sdhci_set_clock,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100108 .enable_dma = sdhci_acpi_enable_dma,
Russell King2317f562014-04-25 12:57:07 +0100109 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100110 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100111 .set_uhs_signaling = sdhci_set_uhs_signaling,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100112};
113
Adrian Hunterb04fa062013-06-13 11:50:27 +0300114static const struct sdhci_ops sdhci_acpi_ops_int = {
Russell King17710592014-04-25 12:58:55 +0100115 .set_clock = sdhci_set_clock,
Adrian Hunterb04fa062013-06-13 11:50:27 +0300116 .enable_dma = sdhci_acpi_enable_dma,
Russell King2317f562014-04-25 12:57:07 +0100117 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100118 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100119 .set_uhs_signaling = sdhci_set_uhs_signaling,
Adrian Hunterb04fa062013-06-13 11:50:27 +0300120 .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
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800127static 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
Adrian Hunter07a58882013-04-26 11:27:22 +0300172static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
Adrian Hunterb04fa062013-06-13 11:50:27 +0300173 .chip = &sdhci_acpi_chip_int,
Maurice Petallof25c3372014-07-08 19:11:01 +0800174 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
175 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR,
Adrian Hunter07a58882013-04-26 11:27:22 +0300176 .caps2 = MMC_CAP2_HC_ERASE_SZ,
177 .flags = SDHCI_ACPI_RUNTIME_PM,
Maurice Petallod61b5942014-07-08 19:11:00 +0800178 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800179 .probe_slot = sdhci_acpi_emmc_probe_slot,
Adrian Hunter07a58882013-04-26 11:27:22 +0300180};
181
Adrian Huntere5571392012-12-10 21:18:48 +0100182static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
Adrian Hunterc6748012014-04-03 14:58:39 +0300183 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
Adrian Huntere5571392012-12-10 21:18:48 +0100184 .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,
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800188 .probe_slot = sdhci_acpi_sdio_probe_slot,
Adrian Huntere5571392012-12-10 21:18:48 +0100189};
190
Adrian Hunter07a58882013-04-26 11:27:22 +0300191static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
Adrian Hunter4fd44092014-03-10 15:02:42 +0200192 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
193 SDHCI_ACPI_RUNTIME_PM,
Adrian Huntera61abe62013-05-06 12:17:33 +0300194 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800195 .probe_slot = sdhci_acpi_sd_probe_slot,
Adrian Hunter07a58882013-04-26 11:27:22 +0300196};
197
198struct sdhci_acpi_uid_slot {
199 const char *hid;
200 const char *uid;
201 const struct sdhci_acpi_slot *slot;
202};
203
204static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
205 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
206 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
Adrian Hunteraad95dc2014-03-10 15:02:43 +0200207 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
Adrian Hunter07a58882013-04-26 11:27:22 +0300208 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
209 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
Mika Westerberg07c001c2013-11-12 12:01:33 +0200210 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
Adrian Hunter07a58882013-04-26 11:27:22 +0300211 { "PNP0D40" },
212 { },
213};
214
Adrian Hunterc4e05032012-11-23 21:17:34 +0100215static const struct acpi_device_id sdhci_acpi_ids[] = {
Adrian Hunter07a58882013-04-26 11:27:22 +0300216 { "80860F14" },
Adrian Hunteraad95dc2014-03-10 15:02:43 +0200217 { "80860F16" },
Adrian Hunter07a58882013-04-26 11:27:22 +0300218 { "INT33BB" },
219 { "INT33C6" },
Mika Westerberg07c001c2013-11-12 12:01:33 +0200220 { "INT3436" },
Adrian Hunter07a58882013-04-26 11:27:22 +0300221 { "PNP0D40" },
Adrian Hunterc4e05032012-11-23 21:17:34 +0100222 { },
223};
224MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
225
Adrian Hunter07a58882013-04-26 11:27:22 +0300226static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
227 const char *uid)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100228{
Adrian Hunter07a58882013-04-26 11:27:22 +0300229 const struct sdhci_acpi_uid_slot *u;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100230
Adrian Hunter07a58882013-04-26 11:27:22 +0300231 for (u = sdhci_acpi_uids; u->hid; u++) {
232 if (strcmp(u->hid, hid))
233 continue;
234 if (!u->uid)
235 return u->slot;
236 if (uid && !strcmp(u->uid, uid))
237 return u->slot;
238 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100239 return NULL;
240}
241
Adrian Hunter07a58882013-04-26 11:27:22 +0300242static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
243 const char *hid)
244{
245 const struct sdhci_acpi_slot *slot;
246 struct acpi_device_info *info;
247 const char *uid = NULL;
248 acpi_status status;
249
250 status = acpi_get_object_info(handle, &info);
251 if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
252 uid = info->unique_id.string;
253
254 slot = sdhci_acpi_get_slot_by_ids(hid, uid);
255
256 kfree(info);
257 return slot;
258}
259
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800260static int sdhci_acpi_probe(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100261{
262 struct device *dev = &pdev->dev;
263 acpi_handle handle = ACPI_HANDLE(dev);
264 struct acpi_device *device;
265 struct sdhci_acpi_host *c;
266 struct sdhci_host *host;
267 struct resource *iomem;
268 resource_size_t len;
269 const char *hid;
Mika Westerberg87875652014-01-08 12:40:55 +0200270 int err;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100271
272 if (acpi_bus_get_device(handle, &device))
273 return -ENODEV;
274
275 if (acpi_bus_get_status(device) || !device->status.present)
276 return -ENODEV;
277
278 hid = acpi_device_hid(device);
279
280 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
281 if (!iomem)
282 return -ENOMEM;
283
284 len = resource_size(iomem);
285 if (len < 0x100)
286 dev_err(dev, "Invalid iomem size!\n");
287
288 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
289 return -ENOMEM;
290
291 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
292 if (IS_ERR(host))
293 return PTR_ERR(host);
294
295 c = sdhci_priv(host);
296 c->host = host;
Adrian Hunter07a58882013-04-26 11:27:22 +0300297 c->slot = sdhci_acpi_get_slot(handle, hid);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100298 c->pdev = pdev;
299 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
300
301 platform_set_drvdata(pdev, c);
302
303 host->hw_name = "ACPI";
304 host->ops = &sdhci_acpi_ops_dflt;
305 host->irq = platform_get_irq(pdev, 0);
306
307 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
308 resource_size(iomem));
309 if (host->ioaddr == NULL) {
310 err = -ENOMEM;
311 goto err_free;
312 }
313
314 if (!dev->dma_mask) {
315 u64 dma_mask;
316
317 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
318 /* 64-bit DMA is not supported at present */
319 dma_mask = DMA_BIT_MASK(32);
320 } else {
321 dma_mask = DMA_BIT_MASK(32);
322 }
323
Russell King07f44502013-06-27 14:06:28 +0100324 err = dma_coerce_mask_and_coherent(dev, dma_mask);
325 if (err)
326 goto err_free;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100327 }
328
329 if (c->slot) {
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800330 if (c->slot->probe_slot) {
331 err = c->slot->probe_slot(pdev);
332 if (err)
333 goto err_free;
334 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100335 if (c->slot->chip) {
336 host->ops = c->slot->chip->ops;
337 host->quirks |= c->slot->chip->quirks;
338 host->quirks2 |= c->slot->chip->quirks2;
339 host->mmc->caps |= c->slot->chip->caps;
340 host->mmc->caps2 |= c->slot->chip->caps2;
341 host->mmc->pm_caps |= c->slot->chip->pm_caps;
342 }
343 host->quirks |= c->slot->quirks;
344 host->quirks2 |= c->slot->quirks2;
345 host->mmc->caps |= c->slot->caps;
346 host->mmc->caps2 |= c->slot->caps2;
347 host->mmc->pm_caps |= c->slot->pm_caps;
348 }
349
Adrian Hunter0d3e3352013-04-04 16:41:06 +0300350 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
351
Adrian Hunter4fd44092014-03-10 15:02:42 +0200352 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
353 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
354
355 if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0)) {
356 dev_warn(dev, "failed to setup card detect gpio\n");
357 c->use_runtime_pm = false;
358 }
359 }
360
Adrian Hunterc4e05032012-11-23 21:17:34 +0100361 err = sdhci_add_host(host);
362 if (err)
363 goto err_free;
364
365 if (c->use_runtime_pm) {
Adrian Hunter1d1ff452013-04-26 11:27:21 +0300366 pm_runtime_set_active(dev);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100367 pm_suspend_ignore_children(dev, 1);
368 pm_runtime_set_autosuspend_delay(dev, 50);
369 pm_runtime_use_autosuspend(dev);
370 pm_runtime_enable(dev);
371 }
372
373 return 0;
374
375err_free:
Adrian Hunterc4e05032012-11-23 21:17:34 +0100376 sdhci_free_host(c->host);
377 return err;
378}
379
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800380static int sdhci_acpi_remove(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100381{
382 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
383 struct device *dev = &pdev->dev;
384 int dead;
385
386 if (c->use_runtime_pm) {
387 pm_runtime_get_sync(dev);
388 pm_runtime_disable(dev);
389 pm_runtime_put_noidle(dev);
390 }
391
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800392 if (c->slot && c->slot->remove_slot)
393 c->slot->remove_slot(pdev);
394
Adrian Hunterc4e05032012-11-23 21:17:34 +0100395 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
396 sdhci_remove_host(c->host, dead);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100397 sdhci_free_host(c->host);
398
399 return 0;
400}
401
402#ifdef CONFIG_PM_SLEEP
403
404static int sdhci_acpi_suspend(struct device *dev)
405{
406 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
407
408 return sdhci_suspend_host(c->host);
409}
410
411static int sdhci_acpi_resume(struct device *dev)
412{
413 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
414
415 return sdhci_resume_host(c->host);
416}
417
418#else
419
420#define sdhci_acpi_suspend NULL
421#define sdhci_acpi_resume NULL
422
423#endif
424
425#ifdef CONFIG_PM_RUNTIME
426
427static int sdhci_acpi_runtime_suspend(struct device *dev)
428{
429 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
430
431 return sdhci_runtime_suspend_host(c->host);
432}
433
434static int sdhci_acpi_runtime_resume(struct device *dev)
435{
436 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
437
438 return sdhci_runtime_resume_host(c->host);
439}
440
441static int sdhci_acpi_runtime_idle(struct device *dev)
442{
443 return 0;
444}
445
Adrian Hunterc4e05032012-11-23 21:17:34 +0100446#endif
447
448static const struct dev_pm_ops sdhci_acpi_pm_ops = {
449 .suspend = sdhci_acpi_suspend,
450 .resume = sdhci_acpi_resume,
Peter Griffin1d75f742014-08-12 17:14:29 +0100451 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
452 sdhci_acpi_runtime_resume, sdhci_acpi_runtime_idle)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100453};
454
455static struct platform_driver sdhci_acpi_driver = {
456 .driver = {
457 .name = "sdhci-acpi",
458 .owner = THIS_MODULE,
459 .acpi_match_table = sdhci_acpi_ids,
460 .pm = &sdhci_acpi_pm_ops,
461 },
462 .probe = sdhci_acpi_probe,
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800463 .remove = sdhci_acpi_remove,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100464};
465
466module_platform_driver(sdhci_acpi_driver);
467
468MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
469MODULE_AUTHOR("Adrian Hunter");
470MODULE_LICENSE("GPL v2");