sdhci-acpi.c 10 KB

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