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