sdhci-acpi.c 12 KB

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