sdhci-acpi.c 12 KB

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