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