sdhci-acpi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/delay.h>
  38. #include <linux/mmc/host.h>
  39. #include <linux/mmc/pm.h>
  40. #include <linux/mmc/slot-gpio.h>
  41. #include <linux/mmc/sdhci.h>
  42. #include "sdhci.h"
  43. enum {
  44. SDHCI_ACPI_SD_CD = BIT(0),
  45. SDHCI_ACPI_RUNTIME_PM = BIT(1),
  46. SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
  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. .set_bus_width = sdhci_set_bus_width,
  95. };
  96. static const struct sdhci_ops sdhci_acpi_ops_int = {
  97. .enable_dma = sdhci_acpi_enable_dma,
  98. .set_bus_width = sdhci_set_bus_width,
  99. .hw_reset = sdhci_acpi_int_hw_reset,
  100. };
  101. static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
  102. .ops = &sdhci_acpi_ops_int,
  103. };
  104. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
  105. .chip = &sdhci_acpi_chip_int,
  106. .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | MMC_CAP_HW_RESET,
  107. .caps2 = MMC_CAP2_HC_ERASE_SZ,
  108. .flags = SDHCI_ACPI_RUNTIME_PM,
  109. };
  110. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
  111. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  112. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  113. .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
  114. .flags = SDHCI_ACPI_RUNTIME_PM,
  115. .pm_caps = MMC_PM_KEEP_POWER,
  116. };
  117. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
  118. .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
  119. SDHCI_ACPI_RUNTIME_PM,
  120. .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
  121. };
  122. struct sdhci_acpi_uid_slot {
  123. const char *hid;
  124. const char *uid;
  125. const struct sdhci_acpi_slot *slot;
  126. };
  127. static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
  128. { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
  129. { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
  130. { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
  131. { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
  132. { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
  133. { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
  134. { "PNP0D40" },
  135. { },
  136. };
  137. static const struct acpi_device_id sdhci_acpi_ids[] = {
  138. { "80860F14" },
  139. { "80860F16" },
  140. { "INT33BB" },
  141. { "INT33C6" },
  142. { "INT3436" },
  143. { "PNP0D40" },
  144. { },
  145. };
  146. MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
  147. static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
  148. const char *uid)
  149. {
  150. const struct sdhci_acpi_uid_slot *u;
  151. for (u = sdhci_acpi_uids; u->hid; u++) {
  152. if (strcmp(u->hid, hid))
  153. continue;
  154. if (!u->uid)
  155. return u->slot;
  156. if (uid && !strcmp(u->uid, uid))
  157. return u->slot;
  158. }
  159. return NULL;
  160. }
  161. static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
  162. const char *hid)
  163. {
  164. const struct sdhci_acpi_slot *slot;
  165. struct acpi_device_info *info;
  166. const char *uid = NULL;
  167. acpi_status status;
  168. status = acpi_get_object_info(handle, &info);
  169. if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
  170. uid = info->unique_id.string;
  171. slot = sdhci_acpi_get_slot_by_ids(hid, uid);
  172. kfree(info);
  173. return slot;
  174. }
  175. static int sdhci_acpi_probe(struct platform_device *pdev)
  176. {
  177. struct device *dev = &pdev->dev;
  178. acpi_handle handle = ACPI_HANDLE(dev);
  179. struct acpi_device *device;
  180. struct sdhci_acpi_host *c;
  181. struct sdhci_host *host;
  182. struct resource *iomem;
  183. resource_size_t len;
  184. const char *hid;
  185. int err;
  186. if (acpi_bus_get_device(handle, &device))
  187. return -ENODEV;
  188. if (acpi_bus_get_status(device) || !device->status.present)
  189. return -ENODEV;
  190. hid = acpi_device_hid(device);
  191. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  192. if (!iomem)
  193. return -ENOMEM;
  194. len = resource_size(iomem);
  195. if (len < 0x100)
  196. dev_err(dev, "Invalid iomem size!\n");
  197. if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
  198. return -ENOMEM;
  199. host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
  200. if (IS_ERR(host))
  201. return PTR_ERR(host);
  202. c = sdhci_priv(host);
  203. c->host = host;
  204. c->slot = sdhci_acpi_get_slot(handle, hid);
  205. c->pdev = pdev;
  206. c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
  207. platform_set_drvdata(pdev, c);
  208. host->hw_name = "ACPI";
  209. host->ops = &sdhci_acpi_ops_dflt;
  210. host->irq = platform_get_irq(pdev, 0);
  211. host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
  212. resource_size(iomem));
  213. if (host->ioaddr == NULL) {
  214. err = -ENOMEM;
  215. goto err_free;
  216. }
  217. if (!dev->dma_mask) {
  218. u64 dma_mask;
  219. if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
  220. /* 64-bit DMA is not supported at present */
  221. dma_mask = DMA_BIT_MASK(32);
  222. } else {
  223. dma_mask = DMA_BIT_MASK(32);
  224. }
  225. err = dma_coerce_mask_and_coherent(dev, dma_mask);
  226. if (err)
  227. goto err_free;
  228. }
  229. if (c->slot) {
  230. if (c->slot->chip) {
  231. host->ops = c->slot->chip->ops;
  232. host->quirks |= c->slot->chip->quirks;
  233. host->quirks2 |= c->slot->chip->quirks2;
  234. host->mmc->caps |= c->slot->chip->caps;
  235. host->mmc->caps2 |= c->slot->chip->caps2;
  236. host->mmc->pm_caps |= c->slot->chip->pm_caps;
  237. }
  238. host->quirks |= c->slot->quirks;
  239. host->quirks2 |= c->slot->quirks2;
  240. host->mmc->caps |= c->slot->caps;
  241. host->mmc->caps2 |= c->slot->caps2;
  242. host->mmc->pm_caps |= c->slot->pm_caps;
  243. }
  244. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  245. if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
  246. bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
  247. if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0)) {
  248. dev_warn(dev, "failed to setup card detect gpio\n");
  249. c->use_runtime_pm = false;
  250. }
  251. }
  252. err = sdhci_add_host(host);
  253. if (err)
  254. goto err_free;
  255. if (c->use_runtime_pm) {
  256. pm_runtime_set_active(dev);
  257. pm_suspend_ignore_children(dev, 1);
  258. pm_runtime_set_autosuspend_delay(dev, 50);
  259. pm_runtime_use_autosuspend(dev);
  260. pm_runtime_enable(dev);
  261. }
  262. return 0;
  263. err_free:
  264. sdhci_free_host(c->host);
  265. return err;
  266. }
  267. static int sdhci_acpi_remove(struct platform_device *pdev)
  268. {
  269. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  270. struct device *dev = &pdev->dev;
  271. int dead;
  272. if (c->use_runtime_pm) {
  273. pm_runtime_get_sync(dev);
  274. pm_runtime_disable(dev);
  275. pm_runtime_put_noidle(dev);
  276. }
  277. dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
  278. sdhci_remove_host(c->host, dead);
  279. sdhci_free_host(c->host);
  280. return 0;
  281. }
  282. #ifdef CONFIG_PM_SLEEP
  283. static int sdhci_acpi_suspend(struct device *dev)
  284. {
  285. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  286. return sdhci_suspend_host(c->host);
  287. }
  288. static int sdhci_acpi_resume(struct device *dev)
  289. {
  290. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  291. return sdhci_resume_host(c->host);
  292. }
  293. #else
  294. #define sdhci_acpi_suspend NULL
  295. #define sdhci_acpi_resume NULL
  296. #endif
  297. #ifdef CONFIG_PM_RUNTIME
  298. static int sdhci_acpi_runtime_suspend(struct device *dev)
  299. {
  300. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  301. return sdhci_runtime_suspend_host(c->host);
  302. }
  303. static int sdhci_acpi_runtime_resume(struct device *dev)
  304. {
  305. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  306. return sdhci_runtime_resume_host(c->host);
  307. }
  308. static int sdhci_acpi_runtime_idle(struct device *dev)
  309. {
  310. return 0;
  311. }
  312. #else
  313. #define sdhci_acpi_runtime_suspend NULL
  314. #define sdhci_acpi_runtime_resume NULL
  315. #define sdhci_acpi_runtime_idle NULL
  316. #endif
  317. static const struct dev_pm_ops sdhci_acpi_pm_ops = {
  318. .suspend = sdhci_acpi_suspend,
  319. .resume = sdhci_acpi_resume,
  320. .runtime_suspend = sdhci_acpi_runtime_suspend,
  321. .runtime_resume = sdhci_acpi_runtime_resume,
  322. .runtime_idle = sdhci_acpi_runtime_idle,
  323. };
  324. static struct platform_driver sdhci_acpi_driver = {
  325. .driver = {
  326. .name = "sdhci-acpi",
  327. .owner = THIS_MODULE,
  328. .acpi_match_table = sdhci_acpi_ids,
  329. .pm = &sdhci_acpi_pm_ops,
  330. },
  331. .probe = sdhci_acpi_probe,
  332. .remove = sdhci_acpi_remove,
  333. };
  334. module_platform_driver(sdhci_acpi_driver);
  335. MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
  336. MODULE_AUTHOR("Adrian Hunter");
  337. MODULE_LICENSE("GPL v2");