]>
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 | #include <linux/mmc/sdhci.h> |
44 | ||
45 | #include "sdhci.h" | |
46 | ||
47 | enum { | |
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 | ||
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 | 104 | static const struct sdhci_ops sdhci_acpi_ops_dflt = { |
1771059c | 105 | .set_clock = sdhci_set_clock, |
c4e05037 | 106 | .enable_dma = sdhci_acpi_enable_dma, |
2317f56c | 107 | .set_bus_width = sdhci_set_bus_width, |
03231f9b | 108 | .reset = sdhci_reset, |
c4e05037 AH |
109 | }; |
110 | ||
b04fa064 | 111 | static const struct sdhci_ops sdhci_acpi_ops_int = { |
1771059c | 112 | .set_clock = sdhci_set_clock, |
b04fa064 | 113 | .enable_dma = sdhci_acpi_enable_dma, |
2317f56c | 114 | .set_bus_width = sdhci_set_bus_width, |
03231f9b | 115 | .reset = sdhci_reset, |
b04fa064 AH |
116 | .hw_reset = sdhci_acpi_int_hw_reset, |
117 | }; | |
118 | ||
119 | static const struct sdhci_acpi_chip sdhci_acpi_chip_int = { | |
120 | .ops = &sdhci_acpi_ops_int, | |
121 | }; | |
122 | ||
07a58883 | 123 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = { |
b04fa064 AH |
124 | .chip = &sdhci_acpi_chip_int, |
125 | .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | MMC_CAP_HW_RESET, | |
07a58883 AH |
126 | .caps2 = MMC_CAP2_HC_ERASE_SZ, |
127 | .flags = SDHCI_ACPI_RUNTIME_PM, | |
128 | }; | |
129 | ||
e5571397 | 130 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = { |
c6748017 | 131 | .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION, |
e5571397 AH |
132 | .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON, |
133 | .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD, | |
134 | .flags = SDHCI_ACPI_RUNTIME_PM, | |
135 | .pm_caps = MMC_PM_KEEP_POWER, | |
136 | }; | |
137 | ||
07a58883 | 138 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = { |
4fd4409c AH |
139 | .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL | |
140 | SDHCI_ACPI_RUNTIME_PM, | |
a61abe6e | 141 | .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON, |
07a58883 AH |
142 | }; |
143 | ||
144 | struct sdhci_acpi_uid_slot { | |
145 | const char *hid; | |
146 | const char *uid; | |
147 | const struct sdhci_acpi_slot *slot; | |
148 | }; | |
149 | ||
150 | static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = { | |
151 | { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc }, | |
152 | { "80860F14" , "3" , &sdhci_acpi_slot_int_sd }, | |
aad95dc4 | 153 | { "80860F16" , NULL, &sdhci_acpi_slot_int_sd }, |
07a58883 AH |
154 | { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio }, |
155 | { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio }, | |
07c001c1 | 156 | { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio }, |
07a58883 AH |
157 | { "PNP0D40" }, |
158 | { }, | |
159 | }; | |
160 | ||
c4e05037 | 161 | static const struct acpi_device_id sdhci_acpi_ids[] = { |
07a58883 | 162 | { "80860F14" }, |
aad95dc4 | 163 | { "80860F16" }, |
07a58883 AH |
164 | { "INT33BB" }, |
165 | { "INT33C6" }, | |
07c001c1 | 166 | { "INT3436" }, |
07a58883 | 167 | { "PNP0D40" }, |
c4e05037 AH |
168 | { }, |
169 | }; | |
170 | MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids); | |
171 | ||
07a58883 AH |
172 | static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid, |
173 | const char *uid) | |
c4e05037 | 174 | { |
07a58883 AH |
175 | const struct sdhci_acpi_uid_slot *u; |
176 | ||
177 | for (u = sdhci_acpi_uids; u->hid; u++) { | |
178 | if (strcmp(u->hid, hid)) | |
179 | continue; | |
180 | if (!u->uid) | |
181 | return u->slot; | |
182 | if (uid && !strcmp(u->uid, uid)) | |
183 | return u->slot; | |
184 | } | |
c4e05037 AH |
185 | return NULL; |
186 | } | |
187 | ||
07a58883 AH |
188 | static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle, |
189 | const char *hid) | |
190 | { | |
191 | const struct sdhci_acpi_slot *slot; | |
192 | struct acpi_device_info *info; | |
193 | const char *uid = NULL; | |
194 | acpi_status status; | |
195 | ||
196 | status = acpi_get_object_info(handle, &info); | |
197 | if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID)) | |
198 | uid = info->unique_id.string; | |
199 | ||
200 | slot = sdhci_acpi_get_slot_by_ids(hid, uid); | |
201 | ||
202 | kfree(info); | |
203 | return slot; | |
204 | } | |
205 | ||
4e608e4e | 206 | static int sdhci_acpi_probe(struct platform_device *pdev) |
c4e05037 AH |
207 | { |
208 | struct device *dev = &pdev->dev; | |
209 | acpi_handle handle = ACPI_HANDLE(dev); | |
210 | struct acpi_device *device; | |
211 | struct sdhci_acpi_host *c; | |
212 | struct sdhci_host *host; | |
213 | struct resource *iomem; | |
214 | resource_size_t len; | |
215 | const char *hid; | |
87875655 | 216 | int err; |
c4e05037 AH |
217 | |
218 | if (acpi_bus_get_device(handle, &device)) | |
219 | return -ENODEV; | |
220 | ||
221 | if (acpi_bus_get_status(device) || !device->status.present) | |
222 | return -ENODEV; | |
223 | ||
224 | hid = acpi_device_hid(device); | |
225 | ||
226 | iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
227 | if (!iomem) | |
228 | return -ENOMEM; | |
229 | ||
230 | len = resource_size(iomem); | |
231 | if (len < 0x100) | |
232 | dev_err(dev, "Invalid iomem size!\n"); | |
233 | ||
234 | if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev))) | |
235 | return -ENOMEM; | |
236 | ||
237 | host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host)); | |
238 | if (IS_ERR(host)) | |
239 | return PTR_ERR(host); | |
240 | ||
241 | c = sdhci_priv(host); | |
242 | c->host = host; | |
07a58883 | 243 | c->slot = sdhci_acpi_get_slot(handle, hid); |
c4e05037 AH |
244 | c->pdev = pdev; |
245 | c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM); | |
246 | ||
247 | platform_set_drvdata(pdev, c); | |
248 | ||
249 | host->hw_name = "ACPI"; | |
250 | host->ops = &sdhci_acpi_ops_dflt; | |
251 | host->irq = platform_get_irq(pdev, 0); | |
252 | ||
253 | host->ioaddr = devm_ioremap_nocache(dev, iomem->start, | |
254 | resource_size(iomem)); | |
255 | if (host->ioaddr == NULL) { | |
256 | err = -ENOMEM; | |
257 | goto err_free; | |
258 | } | |
259 | ||
260 | if (!dev->dma_mask) { | |
261 | u64 dma_mask; | |
262 | ||
263 | if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) { | |
264 | /* 64-bit DMA is not supported at present */ | |
265 | dma_mask = DMA_BIT_MASK(32); | |
266 | } else { | |
267 | dma_mask = DMA_BIT_MASK(32); | |
268 | } | |
269 | ||
07f4450c RK |
270 | err = dma_coerce_mask_and_coherent(dev, dma_mask); |
271 | if (err) | |
272 | goto err_free; | |
c4e05037 AH |
273 | } |
274 | ||
275 | if (c->slot) { | |
276 | if (c->slot->chip) { | |
277 | host->ops = c->slot->chip->ops; | |
278 | host->quirks |= c->slot->chip->quirks; | |
279 | host->quirks2 |= c->slot->chip->quirks2; | |
280 | host->mmc->caps |= c->slot->chip->caps; | |
281 | host->mmc->caps2 |= c->slot->chip->caps2; | |
282 | host->mmc->pm_caps |= c->slot->chip->pm_caps; | |
283 | } | |
284 | host->quirks |= c->slot->quirks; | |
285 | host->quirks2 |= c->slot->quirks2; | |
286 | host->mmc->caps |= c->slot->caps; | |
287 | host->mmc->caps2 |= c->slot->caps2; | |
288 | host->mmc->pm_caps |= c->slot->pm_caps; | |
289 | } | |
290 | ||
0d3e3350 AH |
291 | host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP; |
292 | ||
a61abe6e | 293 | if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) { |
4fd4409c AH |
294 | bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL); |
295 | ||
296 | if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0)) { | |
297 | dev_warn(dev, "failed to setup card detect gpio\n"); | |
a61abe6e | 298 | c->use_runtime_pm = false; |
4fd4409c | 299 | } |
a61abe6e AH |
300 | } |
301 | ||
4fd4409c AH |
302 | err = sdhci_add_host(host); |
303 | if (err) | |
304 | goto err_free; | |
305 | ||
c4e05037 | 306 | if (c->use_runtime_pm) { |
1d1ff458 | 307 | pm_runtime_set_active(dev); |
c4e05037 AH |
308 | pm_suspend_ignore_children(dev, 1); |
309 | pm_runtime_set_autosuspend_delay(dev, 50); | |
310 | pm_runtime_use_autosuspend(dev); | |
311 | pm_runtime_enable(dev); | |
312 | } | |
313 | ||
314 | return 0; | |
315 | ||
316 | err_free: | |
c4e05037 AH |
317 | sdhci_free_host(c->host); |
318 | return err; | |
319 | } | |
320 | ||
4e608e4e | 321 | static int sdhci_acpi_remove(struct platform_device *pdev) |
c4e05037 AH |
322 | { |
323 | struct sdhci_acpi_host *c = platform_get_drvdata(pdev); | |
324 | struct device *dev = &pdev->dev; | |
325 | int dead; | |
326 | ||
327 | if (c->use_runtime_pm) { | |
328 | pm_runtime_get_sync(dev); | |
329 | pm_runtime_disable(dev); | |
330 | pm_runtime_put_noidle(dev); | |
331 | } | |
332 | ||
333 | dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0); | |
334 | sdhci_remove_host(c->host, dead); | |
c4e05037 AH |
335 | sdhci_free_host(c->host); |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
340 | #ifdef CONFIG_PM_SLEEP | |
341 | ||
342 | static int sdhci_acpi_suspend(struct device *dev) | |
343 | { | |
344 | struct sdhci_acpi_host *c = dev_get_drvdata(dev); | |
345 | ||
346 | return sdhci_suspend_host(c->host); | |
347 | } | |
348 | ||
349 | static int sdhci_acpi_resume(struct device *dev) | |
350 | { | |
351 | struct sdhci_acpi_host *c = dev_get_drvdata(dev); | |
352 | ||
353 | return sdhci_resume_host(c->host); | |
354 | } | |
355 | ||
356 | #else | |
357 | ||
358 | #define sdhci_acpi_suspend NULL | |
359 | #define sdhci_acpi_resume NULL | |
360 | ||
361 | #endif | |
362 | ||
363 | #ifdef CONFIG_PM_RUNTIME | |
364 | ||
365 | static int sdhci_acpi_runtime_suspend(struct device *dev) | |
366 | { | |
367 | struct sdhci_acpi_host *c = dev_get_drvdata(dev); | |
368 | ||
369 | return sdhci_runtime_suspend_host(c->host); | |
370 | } | |
371 | ||
372 | static int sdhci_acpi_runtime_resume(struct device *dev) | |
373 | { | |
374 | struct sdhci_acpi_host *c = dev_get_drvdata(dev); | |
375 | ||
376 | return sdhci_runtime_resume_host(c->host); | |
377 | } | |
378 | ||
379 | static int sdhci_acpi_runtime_idle(struct device *dev) | |
380 | { | |
381 | return 0; | |
382 | } | |
383 | ||
384 | #else | |
385 | ||
386 | #define sdhci_acpi_runtime_suspend NULL | |
387 | #define sdhci_acpi_runtime_resume NULL | |
388 | #define sdhci_acpi_runtime_idle NULL | |
389 | ||
390 | #endif | |
391 | ||
392 | static const struct dev_pm_ops sdhci_acpi_pm_ops = { | |
393 | .suspend = sdhci_acpi_suspend, | |
394 | .resume = sdhci_acpi_resume, | |
395 | .runtime_suspend = sdhci_acpi_runtime_suspend, | |
396 | .runtime_resume = sdhci_acpi_runtime_resume, | |
397 | .runtime_idle = sdhci_acpi_runtime_idle, | |
398 | }; | |
399 | ||
400 | static struct platform_driver sdhci_acpi_driver = { | |
401 | .driver = { | |
402 | .name = "sdhci-acpi", | |
403 | .owner = THIS_MODULE, | |
404 | .acpi_match_table = sdhci_acpi_ids, | |
405 | .pm = &sdhci_acpi_pm_ops, | |
406 | }, | |
407 | .probe = sdhci_acpi_probe, | |
4e608e4e | 408 | .remove = sdhci_acpi_remove, |
c4e05037 AH |
409 | }; |
410 | ||
411 | module_platform_driver(sdhci_acpi_driver); | |
412 | ||
413 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver"); | |
414 | MODULE_AUTHOR("Adrian Hunter"); | |
415 | MODULE_LICENSE("GPL v2"); |