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