sdhci-acpi.c 10 KB

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