• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *, const char *, const char *);
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 
sdhci_acpi_flag(struct sdhci_acpi_host * c,unsigned int flag)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 
sdhci_acpi_enable_dma(struct sdhci_host * host)86 static int sdhci_acpi_enable_dma(struct sdhci_host *host)
87 {
88 	return 0;
89 }
90 
sdhci_acpi_int_hw_reset(struct sdhci_host * host)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 
sdhci_acpi_emmc_probe_slot(struct platform_device * pdev,const char * hid,const char * uid)127 static int sdhci_acpi_emmc_probe_slot(struct platform_device *pdev,
128 				      const char *hid, const char *uid)
129 {
130 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
131 	struct sdhci_host *host;
132 
133 	if (!c || !c->host)
134 		return 0;
135 
136 	host = c->host;
137 
138 	/* Platform specific code during emmc proble slot goes here */
139 
140 	if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
141 	    sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
142 	    sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
143 		host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
144 
145 	return 0;
146 }
147 
sdhci_acpi_sdio_probe_slot(struct platform_device * pdev,const char * hid,const char * uid)148 static int sdhci_acpi_sdio_probe_slot(struct platform_device *pdev,
149 				      const char *hid, const char *uid)
150 {
151 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
152 	struct sdhci_host *host;
153 
154 	if (!c || !c->host)
155 		return 0;
156 
157 	host = c->host;
158 
159 	/* Platform specific code during emmc proble slot goes here */
160 
161 	return 0;
162 }
163 
sdhci_acpi_sd_probe_slot(struct platform_device * pdev,const char * hid,const char * uid)164 static int sdhci_acpi_sd_probe_slot(struct platform_device *pdev,
165 				    const char *hid, const char *uid)
166 {
167 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
168 	struct sdhci_host *host;
169 
170 	if (!c || !c->host || !c->slot)
171 		return 0;
172 
173 	host = c->host;
174 
175 	/* Platform specific code during emmc proble slot goes here */
176 
177 	return 0;
178 }
179 
180 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
181 	.chip    = &sdhci_acpi_chip_int,
182 	.caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
183 		   MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
184 		   MMC_CAP_WAIT_WHILE_BUSY,
185 	.caps2   = MMC_CAP2_HC_ERASE_SZ,
186 	.flags   = SDHCI_ACPI_RUNTIME_PM,
187 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | SDHCI_QUIRK2_STOP_WITH_TC,
188 	.probe_slot	= sdhci_acpi_emmc_probe_slot,
189 };
190 
191 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
192 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
193 	.quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
194 	.caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
195 		   MMC_CAP_WAIT_WHILE_BUSY,
196 	.flags   = SDHCI_ACPI_RUNTIME_PM,
197 	.pm_caps = MMC_PM_KEEP_POWER,
198 	.probe_slot	= sdhci_acpi_sdio_probe_slot,
199 };
200 
201 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
202 	.flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
203 		   SDHCI_ACPI_RUNTIME_PM,
204 	.quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
205 		   SDHCI_QUIRK2_STOP_WITH_TC,
206 	.caps    = MMC_CAP_WAIT_WHILE_BUSY,
207 	.probe_slot	= sdhci_acpi_sd_probe_slot,
208 };
209 
210 struct sdhci_acpi_uid_slot {
211 	const char *hid;
212 	const char *uid;
213 	const struct sdhci_acpi_slot *slot;
214 };
215 
216 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
217 	{ "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
218 	{ "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
219 	{ "80860F16" , NULL, &sdhci_acpi_slot_int_sd   },
220 	{ "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
221 	{ "INT33BB"  , "3" , &sdhci_acpi_slot_int_sd },
222 	{ "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
223 	{ "INT3436"  , NULL, &sdhci_acpi_slot_int_sdio },
224 	{ "PNP0D40"  },
225 	{ },
226 };
227 
228 static const struct acpi_device_id sdhci_acpi_ids[] = {
229 	{ "80860F14" },
230 	{ "80860F16" },
231 	{ "INT33BB"  },
232 	{ "INT33C6"  },
233 	{ "INT3436"  },
234 	{ "PNP0D40"  },
235 	{ },
236 };
237 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
238 
sdhci_acpi_get_slot(const char * hid,const char * uid)239 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
240 							 const char *uid)
241 {
242 	const struct sdhci_acpi_uid_slot *u;
243 
244 	for (u = sdhci_acpi_uids; u->hid; u++) {
245 		if (strcmp(u->hid, hid))
246 			continue;
247 		if (!u->uid)
248 			return u->slot;
249 		if (uid && !strcmp(u->uid, uid))
250 			return u->slot;
251 	}
252 	return NULL;
253 }
254 
sdhci_acpi_probe(struct platform_device * pdev)255 static int sdhci_acpi_probe(struct platform_device *pdev)
256 {
257 	struct device *dev = &pdev->dev;
258 	acpi_handle handle = ACPI_HANDLE(dev);
259 	struct acpi_device *device;
260 	struct sdhci_acpi_host *c;
261 	struct sdhci_host *host;
262 	struct resource *iomem;
263 	resource_size_t len;
264 	const char *hid;
265 	const char *uid;
266 	int err;
267 
268 	if (acpi_bus_get_device(handle, &device))
269 		return -ENODEV;
270 
271 	if (acpi_bus_get_status(device) || !device->status.present)
272 		return -ENODEV;
273 
274 	hid = acpi_device_hid(device);
275 	uid = device->pnp.unique_id;
276 
277 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
278 	if (!iomem)
279 		return -ENOMEM;
280 
281 	len = resource_size(iomem);
282 	if (len < 0x100)
283 		dev_err(dev, "Invalid iomem size!\n");
284 
285 	if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
286 		return -ENOMEM;
287 
288 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
289 	if (IS_ERR(host))
290 		return PTR_ERR(host);
291 
292 	c = sdhci_priv(host);
293 	c->host = host;
294 	c->slot = sdhci_acpi_get_slot(hid, uid);
295 	c->pdev = pdev;
296 	c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
297 
298 	platform_set_drvdata(pdev, c);
299 
300 	host->hw_name	= "ACPI";
301 	host->ops	= &sdhci_acpi_ops_dflt;
302 	host->irq	= platform_get_irq(pdev, 0);
303 
304 	host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
305 					    resource_size(iomem));
306 	if (host->ioaddr == NULL) {
307 		err = -ENOMEM;
308 		goto err_free;
309 	}
310 
311 	if (!dev->dma_mask) {
312 		u64 dma_mask;
313 
314 		if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
315 			/* 64-bit DMA is not supported at present */
316 			dma_mask = DMA_BIT_MASK(32);
317 		} else {
318 			dma_mask = DMA_BIT_MASK(32);
319 		}
320 
321 		err = dma_coerce_mask_and_coherent(dev, dma_mask);
322 		if (err)
323 			goto err_free;
324 	}
325 
326 	if (c->slot) {
327 		if (c->slot->probe_slot) {
328 			err = c->slot->probe_slot(pdev, hid, uid);
329 			if (err)
330 				goto err_free;
331 		}
332 		if (c->slot->chip) {
333 			host->ops            = c->slot->chip->ops;
334 			host->quirks        |= c->slot->chip->quirks;
335 			host->quirks2       |= c->slot->chip->quirks2;
336 			host->mmc->caps     |= c->slot->chip->caps;
337 			host->mmc->caps2    |= c->slot->chip->caps2;
338 			host->mmc->pm_caps  |= c->slot->chip->pm_caps;
339 		}
340 		host->quirks        |= c->slot->quirks;
341 		host->quirks2       |= c->slot->quirks2;
342 		host->mmc->caps     |= c->slot->caps;
343 		host->mmc->caps2    |= c->slot->caps2;
344 		host->mmc->pm_caps  |= c->slot->pm_caps;
345 	}
346 
347 	host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
348 
349 	if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
350 		bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
351 
352 		if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL)) {
353 			dev_warn(dev, "failed to setup card detect gpio\n");
354 			c->use_runtime_pm = false;
355 		}
356 	}
357 
358 	err = sdhci_add_host(host);
359 	if (err)
360 		goto err_free;
361 
362 	if (c->use_runtime_pm) {
363 		pm_runtime_set_active(dev);
364 		pm_suspend_ignore_children(dev, 1);
365 		pm_runtime_set_autosuspend_delay(dev, 50);
366 		pm_runtime_use_autosuspend(dev);
367 		pm_runtime_enable(dev);
368 	}
369 
370 	return 0;
371 
372 err_free:
373 	sdhci_free_host(c->host);
374 	return err;
375 }
376 
sdhci_acpi_remove(struct platform_device * pdev)377 static int sdhci_acpi_remove(struct platform_device *pdev)
378 {
379 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
380 	struct device *dev = &pdev->dev;
381 	int dead;
382 
383 	if (c->use_runtime_pm) {
384 		pm_runtime_get_sync(dev);
385 		pm_runtime_disable(dev);
386 		pm_runtime_put_noidle(dev);
387 	}
388 
389 	if (c->slot && c->slot->remove_slot)
390 		c->slot->remove_slot(pdev);
391 
392 	dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
393 	sdhci_remove_host(c->host, dead);
394 	sdhci_free_host(c->host);
395 
396 	return 0;
397 }
398 
399 #ifdef CONFIG_PM_SLEEP
400 
sdhci_acpi_suspend(struct device * dev)401 static int sdhci_acpi_suspend(struct device *dev)
402 {
403 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
404 
405 	return sdhci_suspend_host(c->host);
406 }
407 
sdhci_acpi_resume(struct device * dev)408 static int sdhci_acpi_resume(struct device *dev)
409 {
410 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
411 
412 	return sdhci_resume_host(c->host);
413 }
414 
415 #else
416 
417 #define sdhci_acpi_suspend	NULL
418 #define sdhci_acpi_resume	NULL
419 
420 #endif
421 
422 #ifdef CONFIG_PM_RUNTIME
423 
sdhci_acpi_runtime_suspend(struct device * dev)424 static int sdhci_acpi_runtime_suspend(struct device *dev)
425 {
426 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
427 
428 	return sdhci_runtime_suspend_host(c->host);
429 }
430 
sdhci_acpi_runtime_resume(struct device * dev)431 static int sdhci_acpi_runtime_resume(struct device *dev)
432 {
433 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
434 
435 	return sdhci_runtime_resume_host(c->host);
436 }
437 
sdhci_acpi_runtime_idle(struct device * dev)438 static int sdhci_acpi_runtime_idle(struct device *dev)
439 {
440 	return 0;
441 }
442 
443 #endif
444 
445 static const struct dev_pm_ops sdhci_acpi_pm_ops = {
446 	.suspend		= sdhci_acpi_suspend,
447 	.resume			= sdhci_acpi_resume,
448 	SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
449 			sdhci_acpi_runtime_resume, sdhci_acpi_runtime_idle)
450 };
451 
452 static struct platform_driver sdhci_acpi_driver = {
453 	.driver = {
454 		.name			= "sdhci-acpi",
455 		.owner			= THIS_MODULE,
456 		.acpi_match_table	= sdhci_acpi_ids,
457 		.pm			= &sdhci_acpi_pm_ops,
458 	},
459 	.probe	= sdhci_acpi_probe,
460 	.remove	= sdhci_acpi_remove,
461 };
462 
463 module_platform_driver(sdhci_acpi_driver);
464 
465 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
466 MODULE_AUTHOR("Adrian Hunter");
467 MODULE_LICENSE("GPL v2");
468