]>
Commit | Line | Data |
---|---|---|
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> | |
38 | #include <linux/delay.h> | |
39 | ||
40 | #include <linux/mmc/host.h> | |
41 | #include <linux/mmc/pm.h> | |
42 | #include <linux/mmc/slot-gpio.h> | |
43 | #include <linux/mmc/sdhci.h> | |
44 | ||
45 | #include "sdhci.h" | |
46 | ||
47 | enum { | |
48 | SDHCI_ACPI_SD_CD = BIT(0), | |
49 | SDHCI_ACPI_RUNTIME_PM = BIT(1), | |
50 | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2), | |
51 | }; | |
52 | ||
53 | struct 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 | ||
62 | struct 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; | |
70 | int (*probe_slot)(struct platform_device *); | |
71 | int (*remove_slot)(struct platform_device *); | |
72 | }; | |
73 | ||
74 | struct 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 | ||
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 | { | |
88 | return 0; | |
89 | } | |
90 | ||
91 | static 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 | ||
106 | static const struct sdhci_ops sdhci_acpi_ops_dflt = { | |
107 | .set_clock = sdhci_set_clock, | |
108 | .enable_dma = sdhci_acpi_enable_dma, | |
109 | .set_bus_width = sdhci_set_bus_width, | |
110 | .reset = sdhci_reset, | |
111 | .set_uhs_signaling = sdhci_set_uhs_signaling, | |
112 | }; | |
113 | ||
114 | static const struct sdhci_ops sdhci_acpi_ops_int = { | |
115 | .set_clock = sdhci_set_clock, | |
116 | .enable_dma = sdhci_acpi_enable_dma, | |
117 | .set_bus_width = sdhci_set_bus_width, | |
118 | .reset = sdhci_reset, | |
119 | .set_uhs_signaling = sdhci_set_uhs_signaling, | |
120 | .hw_reset = sdhci_acpi_int_hw_reset, | |
121 | }; | |
122 | ||
123 | static const struct sdhci_acpi_chip sdhci_acpi_chip_int = { | |
124 | .ops = &sdhci_acpi_ops_int, | |
125 | }; | |
126 | ||
127 | static 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 | ||
142 | static 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 | ||
157 | static 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 | ||
172 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = { | |
173 | .chip = &sdhci_acpi_chip_int, | |
174 | .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | | |
175 | MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR, | |
176 | .caps2 = MMC_CAP2_HC_ERASE_SZ, | |
177 | .flags = SDHCI_ACPI_RUNTIME_PM, | |
178 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | SDHCI_QUIRK2_STOP_WITH_TC, | |
179 | .probe_slot = sdhci_acpi_emmc_probe_slot, | |
180 | }; | |
181 | ||
182 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = { | |
183 | .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION, | |
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, | |
188 | .probe_slot = sdhci_acpi_sdio_probe_slot, | |
189 | }; | |
190 | ||
191 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = { | |
192 | .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL | | |
193 | SDHCI_ACPI_RUNTIME_PM, | |
194 | .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON | | |
195 | SDHCI_QUIRK2_STOP_WITH_TC, | |
196 | .probe_slot = sdhci_acpi_sd_probe_slot, | |
197 | }; | |
198 | ||
199 | struct sdhci_acpi_uid_slot { | |
200 | const char *hid; | |
201 | const char *uid; | |
202 | const struct sdhci_acpi_slot *slot; | |
203 | }; | |
204 | ||
205 | static 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 }, | |
208 | { "80860F16" , NULL, &sdhci_acpi_slot_int_sd }, | |
209 | { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio }, | |
210 | { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio }, | |
211 | { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio }, | |
212 | { "PNP0D40" }, | |
213 | { }, | |
214 | }; | |
215 | ||
216 | static const struct acpi_device_id sdhci_acpi_ids[] = { | |
217 | { "80860F14" }, | |
218 | { "80860F16" }, | |
219 | { "INT33BB" }, | |
220 | { "INT33C6" }, | |
221 | { "INT3436" }, | |
222 | { "PNP0D40" }, | |
223 | { }, | |
224 | }; | |
225 | MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids); | |
226 | ||
227 | static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid, | |
228 | const char *uid) | |
229 | { | |
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 | } | |
240 | return NULL; | |
241 | } | |
242 | ||
243 | static 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 | ||
261 | static int sdhci_acpi_probe(struct platform_device *pdev) | |
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; | |
271 | int err; | |
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; | |
298 | c->slot = sdhci_acpi_get_slot(handle, hid); | |
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 | ||
325 | err = dma_coerce_mask_and_coherent(dev, dma_mask); | |
326 | if (err) | |
327 | goto err_free; | |
328 | } | |
329 | ||
330 | if (c->slot) { | |
331 | if (c->slot->probe_slot) { | |
332 | err = c->slot->probe_slot(pdev); | |
333 | if (err) | |
334 | goto err_free; | |
335 | } | |
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 | ||
351 | host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP; | |
352 | ||
353 | if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) { | |
354 | bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL); | |
355 | ||
356 | if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL)) { | |
357 | dev_warn(dev, "failed to setup card detect gpio\n"); | |
358 | c->use_runtime_pm = false; | |
359 | } | |
360 | } | |
361 | ||
362 | err = sdhci_add_host(host); | |
363 | if (err) | |
364 | goto err_free; | |
365 | ||
366 | if (c->use_runtime_pm) { | |
367 | pm_runtime_set_active(dev); | |
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 | ||
376 | err_free: | |
377 | sdhci_free_host(c->host); | |
378 | return err; | |
379 | } | |
380 | ||
381 | static int sdhci_acpi_remove(struct platform_device *pdev) | |
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 | ||
393 | if (c->slot && c->slot->remove_slot) | |
394 | c->slot->remove_slot(pdev); | |
395 | ||
396 | dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0); | |
397 | sdhci_remove_host(c->host, dead); | |
398 | sdhci_free_host(c->host); | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
403 | #ifdef CONFIG_PM_SLEEP | |
404 | ||
405 | static 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 | ||
412 | static 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 | ||
428 | static 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 | ||
435 | static 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 | ||
442 | static int sdhci_acpi_runtime_idle(struct device *dev) | |
443 | { | |
444 | return 0; | |
445 | } | |
446 | ||
447 | #endif | |
448 | ||
449 | static const struct dev_pm_ops sdhci_acpi_pm_ops = { | |
450 | .suspend = sdhci_acpi_suspend, | |
451 | .resume = sdhci_acpi_resume, | |
452 | SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend, | |
453 | sdhci_acpi_runtime_resume, sdhci_acpi_runtime_idle) | |
454 | }; | |
455 | ||
456 | static 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, | |
464 | .remove = sdhci_acpi_remove, | |
465 | }; | |
466 | ||
467 | module_platform_driver(sdhci_acpi_driver); | |
468 | ||
469 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver"); | |
470 | MODULE_AUTHOR("Adrian Hunter"); | |
471 | MODULE_LICENSE("GPL v2"); |