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