sdhci-acpi.c 12 KB

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