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