sdhci-acpi.c 14 KB

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