sdhci-acpi.c 14 KB

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