sdhci-acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. int (*probe_slot)(struct platform_device *, const char *, const char *);
  65. int (*remove_slot)(struct platform_device *);
  66. };
  67. struct sdhci_acpi_host {
  68. struct sdhci_host *host;
  69. const struct sdhci_acpi_slot *slot;
  70. struct platform_device *pdev;
  71. bool use_runtime_pm;
  72. };
  73. static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
  74. {
  75. return c->slot && (c->slot->flags & flag);
  76. }
  77. static int sdhci_acpi_enable_dma(struct sdhci_host *host)
  78. {
  79. return 0;
  80. }
  81. static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
  82. {
  83. u8 reg;
  84. reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
  85. reg |= 0x10;
  86. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  87. /* For eMMC, minimum is 1us but give it 9us for good measure */
  88. udelay(9);
  89. reg &= ~0x10;
  90. sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
  91. /* For eMMC, minimum is 200us but give it 300us for good measure */
  92. usleep_range(300, 1000);
  93. }
  94. static const struct sdhci_ops sdhci_acpi_ops_dflt = {
  95. .set_clock = sdhci_set_clock,
  96. .enable_dma = sdhci_acpi_enable_dma,
  97. .set_bus_width = sdhci_set_bus_width,
  98. .reset = sdhci_reset,
  99. .set_uhs_signaling = sdhci_set_uhs_signaling,
  100. };
  101. static const struct sdhci_ops sdhci_acpi_ops_int = {
  102. .set_clock = sdhci_set_clock,
  103. .enable_dma = sdhci_acpi_enable_dma,
  104. .set_bus_width = sdhci_set_bus_width,
  105. .reset = sdhci_reset,
  106. .set_uhs_signaling = sdhci_set_uhs_signaling,
  107. .hw_reset = sdhci_acpi_int_hw_reset,
  108. };
  109. static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
  110. .ops = &sdhci_acpi_ops_int,
  111. };
  112. static int sdhci_acpi_emmc_probe_slot(struct platform_device *pdev,
  113. const char *hid, const char *uid)
  114. {
  115. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  116. struct sdhci_host *host;
  117. if (!c || !c->host)
  118. return 0;
  119. host = c->host;
  120. /* Platform specific code during emmc proble slot goes here */
  121. if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
  122. sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
  123. sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
  124. host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
  125. return 0;
  126. }
  127. static int sdhci_acpi_sdio_probe_slot(struct platform_device *pdev,
  128. const char *hid, const char *uid)
  129. {
  130. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  131. struct sdhci_host *host;
  132. if (!c || !c->host)
  133. return 0;
  134. host = c->host;
  135. /* Platform specific code during emmc proble slot goes here */
  136. return 0;
  137. }
  138. static int sdhci_acpi_sd_probe_slot(struct platform_device *pdev,
  139. const char *hid, const char *uid)
  140. {
  141. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  142. struct sdhci_host *host;
  143. if (!c || !c->host || !c->slot)
  144. return 0;
  145. host = c->host;
  146. /* Platform specific code during emmc proble slot goes here */
  147. return 0;
  148. }
  149. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
  150. .chip = &sdhci_acpi_chip_int,
  151. .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
  152. MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR,
  153. .caps2 = MMC_CAP2_HC_ERASE_SZ,
  154. .flags = SDHCI_ACPI_RUNTIME_PM,
  155. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | SDHCI_QUIRK2_STOP_WITH_TC,
  156. .probe_slot = sdhci_acpi_emmc_probe_slot,
  157. };
  158. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
  159. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
  160. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  161. .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
  162. .flags = SDHCI_ACPI_RUNTIME_PM,
  163. .pm_caps = MMC_PM_KEEP_POWER,
  164. .probe_slot = sdhci_acpi_sdio_probe_slot,
  165. };
  166. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
  167. .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
  168. SDHCI_ACPI_RUNTIME_PM,
  169. .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
  170. SDHCI_QUIRK2_STOP_WITH_TC,
  171. .probe_slot = sdhci_acpi_sd_probe_slot,
  172. };
  173. struct sdhci_acpi_uid_slot {
  174. const char *hid;
  175. const char *uid;
  176. const struct sdhci_acpi_slot *slot;
  177. };
  178. static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
  179. { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
  180. { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
  181. { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
  182. { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
  183. { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd },
  184. { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
  185. { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
  186. { "PNP0D40" },
  187. { },
  188. };
  189. static const struct acpi_device_id sdhci_acpi_ids[] = {
  190. { "80860F14" },
  191. { "80860F16" },
  192. { "INT33BB" },
  193. { "INT33C6" },
  194. { "INT3436" },
  195. { "PNP0D40" },
  196. { },
  197. };
  198. MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
  199. static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
  200. const char *uid)
  201. {
  202. const struct sdhci_acpi_uid_slot *u;
  203. for (u = sdhci_acpi_uids; u->hid; u++) {
  204. if (strcmp(u->hid, hid))
  205. continue;
  206. if (!u->uid)
  207. return u->slot;
  208. if (uid && !strcmp(u->uid, uid))
  209. return u->slot;
  210. }
  211. return NULL;
  212. }
  213. static int sdhci_acpi_probe(struct platform_device *pdev)
  214. {
  215. struct device *dev = &pdev->dev;
  216. acpi_handle handle = ACPI_HANDLE(dev);
  217. struct acpi_device *device;
  218. struct sdhci_acpi_host *c;
  219. struct sdhci_host *host;
  220. struct resource *iomem;
  221. resource_size_t len;
  222. const char *hid;
  223. const char *uid;
  224. int err;
  225. if (acpi_bus_get_device(handle, &device))
  226. return -ENODEV;
  227. if (acpi_bus_get_status(device) || !device->status.present)
  228. return -ENODEV;
  229. hid = acpi_device_hid(device);
  230. uid = device->pnp.unique_id;
  231. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  232. if (!iomem)
  233. return -ENOMEM;
  234. len = resource_size(iomem);
  235. if (len < 0x100)
  236. dev_err(dev, "Invalid iomem size!\n");
  237. if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
  238. return -ENOMEM;
  239. host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
  240. if (IS_ERR(host))
  241. return PTR_ERR(host);
  242. c = sdhci_priv(host);
  243. c->host = host;
  244. c->slot = sdhci_acpi_get_slot(hid, uid);
  245. c->pdev = pdev;
  246. c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
  247. platform_set_drvdata(pdev, c);
  248. host->hw_name = "ACPI";
  249. host->ops = &sdhci_acpi_ops_dflt;
  250. host->irq = platform_get_irq(pdev, 0);
  251. host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
  252. resource_size(iomem));
  253. if (host->ioaddr == NULL) {
  254. err = -ENOMEM;
  255. goto err_free;
  256. }
  257. if (!dev->dma_mask) {
  258. u64 dma_mask;
  259. if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
  260. /* 64-bit DMA is not supported at present */
  261. dma_mask = DMA_BIT_MASK(32);
  262. } else {
  263. dma_mask = DMA_BIT_MASK(32);
  264. }
  265. err = dma_coerce_mask_and_coherent(dev, dma_mask);
  266. if (err)
  267. goto err_free;
  268. }
  269. if (c->slot) {
  270. if (c->slot->probe_slot) {
  271. err = c->slot->probe_slot(pdev, hid, uid);
  272. if (err)
  273. goto err_free;
  274. }
  275. if (c->slot->chip) {
  276. host->ops = c->slot->chip->ops;
  277. host->quirks |= c->slot->chip->quirks;
  278. host->quirks2 |= c->slot->chip->quirks2;
  279. host->mmc->caps |= c->slot->chip->caps;
  280. host->mmc->caps2 |= c->slot->chip->caps2;
  281. host->mmc->pm_caps |= c->slot->chip->pm_caps;
  282. }
  283. host->quirks |= c->slot->quirks;
  284. host->quirks2 |= c->slot->quirks2;
  285. host->mmc->caps |= c->slot->caps;
  286. host->mmc->caps2 |= c->slot->caps2;
  287. host->mmc->pm_caps |= c->slot->pm_caps;
  288. }
  289. host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
  290. if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
  291. bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
  292. if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL)) {
  293. dev_warn(dev, "failed to setup card detect gpio\n");
  294. c->use_runtime_pm = false;
  295. }
  296. }
  297. err = sdhci_add_host(host);
  298. if (err)
  299. goto err_free;
  300. if (c->use_runtime_pm) {
  301. pm_runtime_set_active(dev);
  302. pm_suspend_ignore_children(dev, 1);
  303. pm_runtime_set_autosuspend_delay(dev, 50);
  304. pm_runtime_use_autosuspend(dev);
  305. pm_runtime_enable(dev);
  306. }
  307. return 0;
  308. err_free:
  309. sdhci_free_host(c->host);
  310. return err;
  311. }
  312. static int sdhci_acpi_remove(struct platform_device *pdev)
  313. {
  314. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  315. struct device *dev = &pdev->dev;
  316. int dead;
  317. if (c->use_runtime_pm) {
  318. pm_runtime_get_sync(dev);
  319. pm_runtime_disable(dev);
  320. pm_runtime_put_noidle(dev);
  321. }
  322. if (c->slot && c->slot->remove_slot)
  323. c->slot->remove_slot(pdev);
  324. dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
  325. sdhci_remove_host(c->host, dead);
  326. sdhci_free_host(c->host);
  327. return 0;
  328. }
  329. #ifdef CONFIG_PM_SLEEP
  330. static int sdhci_acpi_suspend(struct device *dev)
  331. {
  332. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  333. return sdhci_suspend_host(c->host);
  334. }
  335. static int sdhci_acpi_resume(struct device *dev)
  336. {
  337. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  338. return sdhci_resume_host(c->host);
  339. }
  340. #else
  341. #define sdhci_acpi_suspend NULL
  342. #define sdhci_acpi_resume NULL
  343. #endif
  344. #ifdef CONFIG_PM_RUNTIME
  345. static int sdhci_acpi_runtime_suspend(struct device *dev)
  346. {
  347. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  348. return sdhci_runtime_suspend_host(c->host);
  349. }
  350. static int sdhci_acpi_runtime_resume(struct device *dev)
  351. {
  352. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  353. return sdhci_runtime_resume_host(c->host);
  354. }
  355. static int sdhci_acpi_runtime_idle(struct device *dev)
  356. {
  357. return 0;
  358. }
  359. #endif
  360. static const struct dev_pm_ops sdhci_acpi_pm_ops = {
  361. .suspend = sdhci_acpi_suspend,
  362. .resume = sdhci_acpi_resume,
  363. SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
  364. sdhci_acpi_runtime_resume, sdhci_acpi_runtime_idle)
  365. };
  366. static struct platform_driver sdhci_acpi_driver = {
  367. .driver = {
  368. .name = "sdhci-acpi",
  369. .owner = THIS_MODULE,
  370. .acpi_match_table = sdhci_acpi_ids,
  371. .pm = &sdhci_acpi_pm_ops,
  372. },
  373. .probe = sdhci_acpi_probe,
  374. .remove = sdhci_acpi_remove,
  375. };
  376. module_platform_driver(sdhci_acpi_driver);
  377. MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
  378. MODULE_AUTHOR("Adrian Hunter");
  379. MODULE_LICENSE("GPL v2");