sdhci-acpi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/interrupt.h>
  34. #include <linux/acpi.h>
  35. #include <linux/pm.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/mmc/host.h>
  38. #include <linux/mmc/pm.h>
  39. #include <linux/mmc/sdhci.h>
  40. #include "sdhci.h"
  41. enum {
  42. SDHCI_ACPI_SD_CD = BIT(0),
  43. SDHCI_ACPI_RUNTIME_PM = BIT(1),
  44. };
  45. struct sdhci_acpi_chip {
  46. const struct sdhci_ops *ops;
  47. unsigned int quirks;
  48. unsigned int quirks2;
  49. unsigned long caps;
  50. unsigned int caps2;
  51. mmc_pm_flag_t pm_caps;
  52. };
  53. struct sdhci_acpi_slot {
  54. const struct sdhci_acpi_chip *chip;
  55. unsigned int quirks;
  56. unsigned int quirks2;
  57. unsigned long caps;
  58. unsigned int caps2;
  59. mmc_pm_flag_t pm_caps;
  60. unsigned int flags;
  61. };
  62. struct sdhci_acpi_host {
  63. struct sdhci_host *host;
  64. const struct sdhci_acpi_slot *slot;
  65. struct platform_device *pdev;
  66. bool use_runtime_pm;
  67. };
  68. static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
  69. {
  70. return c->slot && (c->slot->flags & flag);
  71. }
  72. static int sdhci_acpi_enable_dma(struct sdhci_host *host)
  73. {
  74. return 0;
  75. }
  76. static const struct sdhci_ops sdhci_acpi_ops_dflt = {
  77. .enable_dma = sdhci_acpi_enable_dma,
  78. };
  79. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
  80. .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
  81. .caps2 = MMC_CAP2_HC_ERASE_SZ,
  82. .flags = SDHCI_ACPI_RUNTIME_PM,
  83. };
  84. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
  85. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  86. .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
  87. .flags = SDHCI_ACPI_RUNTIME_PM,
  88. .pm_caps = MMC_PM_KEEP_POWER,
  89. };
  90. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
  91. };
  92. struct sdhci_acpi_uid_slot {
  93. const char *hid;
  94. const char *uid;
  95. const struct sdhci_acpi_slot *slot;
  96. };
  97. static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
  98. { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
  99. { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
  100. { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
  101. { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
  102. { "PNP0D40" },
  103. { },
  104. };
  105. static const struct acpi_device_id sdhci_acpi_ids[] = {
  106. { "80860F14" },
  107. { "INT33BB" },
  108. { "INT33C6" },
  109. { "PNP0D40" },
  110. { },
  111. };
  112. MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
  113. static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
  114. const char *uid)
  115. {
  116. const struct sdhci_acpi_uid_slot *u;
  117. for (u = sdhci_acpi_uids; u->hid; u++) {
  118. if (strcmp(u->hid, hid))
  119. continue;
  120. if (!u->uid)
  121. return u->slot;
  122. if (uid && !strcmp(u->uid, uid))
  123. return u->slot;
  124. }
  125. return NULL;
  126. }
  127. static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
  128. const char *hid)
  129. {
  130. const struct sdhci_acpi_slot *slot;
  131. struct acpi_device_info *info;
  132. const char *uid = NULL;
  133. acpi_status status;
  134. status = acpi_get_object_info(handle, &info);
  135. if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
  136. uid = info->unique_id.string;
  137. slot = sdhci_acpi_get_slot_by_ids(hid, uid);
  138. kfree(info);
  139. return slot;
  140. }
  141. static int sdhci_acpi_probe(struct platform_device *pdev)
  142. {
  143. struct device *dev = &pdev->dev;
  144. acpi_handle handle = ACPI_HANDLE(dev);
  145. struct acpi_device *device;
  146. struct sdhci_acpi_host *c;
  147. struct sdhci_host *host;
  148. struct resource *iomem;
  149. resource_size_t len;
  150. const char *hid;
  151. int err;
  152. if (acpi_bus_get_device(handle, &device))
  153. return -ENODEV;
  154. if (acpi_bus_get_status(device) || !device->status.present)
  155. return -ENODEV;
  156. hid = acpi_device_hid(device);
  157. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. if (!iomem)
  159. return -ENOMEM;
  160. len = resource_size(iomem);
  161. if (len < 0x100)
  162. dev_err(dev, "Invalid iomem size!\n");
  163. if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
  164. return -ENOMEM;
  165. host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
  166. if (IS_ERR(host))
  167. return PTR_ERR(host);
  168. c = sdhci_priv(host);
  169. c->host = host;
  170. c->slot = sdhci_acpi_get_slot(handle, hid);
  171. c->pdev = pdev;
  172. c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
  173. platform_set_drvdata(pdev, c);
  174. host->hw_name = "ACPI";
  175. host->ops = &sdhci_acpi_ops_dflt;
  176. host->irq = platform_get_irq(pdev, 0);
  177. host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
  178. resource_size(iomem));
  179. if (host->ioaddr == NULL) {
  180. err = -ENOMEM;
  181. goto err_free;
  182. }
  183. if (!dev->dma_mask) {
  184. u64 dma_mask;
  185. if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
  186. /* 64-bit DMA is not supported at present */
  187. dma_mask = DMA_BIT_MASK(32);
  188. } else {
  189. dma_mask = DMA_BIT_MASK(32);
  190. }
  191. dev->dma_mask = &dev->coherent_dma_mask;
  192. dev->coherent_dma_mask = dma_mask;
  193. }
  194. if (c->slot) {
  195. if (c->slot->chip) {
  196. host->ops = c->slot->chip->ops;
  197. host->quirks |= c->slot->chip->quirks;
  198. host->quirks2 |= c->slot->chip->quirks2;
  199. host->mmc->caps |= c->slot->chip->caps;
  200. host->mmc->caps2 |= c->slot->chip->caps2;
  201. host->mmc->pm_caps |= c->slot->chip->pm_caps;
  202. }
  203. host->quirks |= c->slot->quirks;
  204. host->quirks2 |= c->slot->quirks2;
  205. host->mmc->caps |= c->slot->caps;
  206. host->mmc->caps2 |= c->slot->caps2;
  207. host->mmc->pm_caps |= c->slot->pm_caps;
  208. }
  209. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  210. err = sdhci_add_host(host);
  211. if (err)
  212. goto err_free;
  213. if (c->use_runtime_pm) {
  214. pm_runtime_set_active(dev);
  215. pm_suspend_ignore_children(dev, 1);
  216. pm_runtime_set_autosuspend_delay(dev, 50);
  217. pm_runtime_use_autosuspend(dev);
  218. pm_runtime_enable(dev);
  219. }
  220. return 0;
  221. err_free:
  222. sdhci_free_host(c->host);
  223. return err;
  224. }
  225. static int sdhci_acpi_remove(struct platform_device *pdev)
  226. {
  227. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  228. struct device *dev = &pdev->dev;
  229. int dead;
  230. if (c->use_runtime_pm) {
  231. pm_runtime_get_sync(dev);
  232. pm_runtime_disable(dev);
  233. pm_runtime_put_noidle(dev);
  234. }
  235. dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
  236. sdhci_remove_host(c->host, dead);
  237. sdhci_free_host(c->host);
  238. return 0;
  239. }
  240. #ifdef CONFIG_PM_SLEEP
  241. static int sdhci_acpi_suspend(struct device *dev)
  242. {
  243. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  244. return sdhci_suspend_host(c->host);
  245. }
  246. static int sdhci_acpi_resume(struct device *dev)
  247. {
  248. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  249. return sdhci_resume_host(c->host);
  250. }
  251. #else
  252. #define sdhci_acpi_suspend NULL
  253. #define sdhci_acpi_resume NULL
  254. #endif
  255. #ifdef CONFIG_PM_RUNTIME
  256. static int sdhci_acpi_runtime_suspend(struct device *dev)
  257. {
  258. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  259. return sdhci_runtime_suspend_host(c->host);
  260. }
  261. static int sdhci_acpi_runtime_resume(struct device *dev)
  262. {
  263. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  264. return sdhci_runtime_resume_host(c->host);
  265. }
  266. static int sdhci_acpi_runtime_idle(struct device *dev)
  267. {
  268. return 0;
  269. }
  270. #else
  271. #define sdhci_acpi_runtime_suspend NULL
  272. #define sdhci_acpi_runtime_resume NULL
  273. #define sdhci_acpi_runtime_idle NULL
  274. #endif
  275. static const struct dev_pm_ops sdhci_acpi_pm_ops = {
  276. .suspend = sdhci_acpi_suspend,
  277. .resume = sdhci_acpi_resume,
  278. .runtime_suspend = sdhci_acpi_runtime_suspend,
  279. .runtime_resume = sdhci_acpi_runtime_resume,
  280. .runtime_idle = sdhci_acpi_runtime_idle,
  281. };
  282. static struct platform_driver sdhci_acpi_driver = {
  283. .driver = {
  284. .name = "sdhci-acpi",
  285. .owner = THIS_MODULE,
  286. .acpi_match_table = sdhci_acpi_ids,
  287. .pm = &sdhci_acpi_pm_ops,
  288. },
  289. .probe = sdhci_acpi_probe,
  290. .remove = sdhci_acpi_remove,
  291. };
  292. module_platform_driver(sdhci_acpi_driver);
  293. MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
  294. MODULE_AUTHOR("Adrian Hunter");
  295. MODULE_LICENSE("GPL v2");