host.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * linux/drivers/mmc/core/host.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  5. * Copyright (C) 2007-2008 Pierre Ossman
  6. * Copyright (C) 2010 Linus Walleij
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * MMC host class device management
  13. */
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/idr.h>
  17. #include <linux/of.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/export.h>
  21. #include <linux/leds.h>
  22. #include <linux/slab.h>
  23. #include <linux/mmc/host.h>
  24. #include <linux/mmc/card.h>
  25. #include <linux/mmc/slot-gpio.h>
  26. #include "core.h"
  27. #include "crypto.h"
  28. #include "host.h"
  29. #include "slot-gpio.h"
  30. #include "pwrseq.h"
  31. #include "sdio_ops.h"
  32. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  33. static DEFINE_IDA(mmc_host_ida);
  34. static void mmc_host_classdev_release(struct device *dev)
  35. {
  36. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  37. ida_simple_remove(&mmc_host_ida, host->index);
  38. kfree(host);
  39. }
  40. static struct class mmc_host_class = {
  41. .name = "mmc_host",
  42. .dev_release = mmc_host_classdev_release,
  43. };
  44. int mmc_register_host_class(void)
  45. {
  46. return class_register(&mmc_host_class);
  47. }
  48. void mmc_unregister_host_class(void)
  49. {
  50. class_unregister(&mmc_host_class);
  51. }
  52. void mmc_retune_enable(struct mmc_host *host)
  53. {
  54. host->can_retune = 1;
  55. if (host->retune_period)
  56. mod_timer(&host->retune_timer,
  57. jiffies + host->retune_period * HZ);
  58. }
  59. /*
  60. * Pause re-tuning for a small set of operations. The pause begins after the
  61. * next command and after first doing re-tuning.
  62. */
  63. void mmc_retune_pause(struct mmc_host *host)
  64. {
  65. if (!host->retune_paused) {
  66. host->retune_paused = 1;
  67. mmc_retune_needed(host);
  68. mmc_retune_hold(host);
  69. }
  70. }
  71. EXPORT_SYMBOL(mmc_retune_pause);
  72. void mmc_retune_unpause(struct mmc_host *host)
  73. {
  74. if (host->retune_paused) {
  75. host->retune_paused = 0;
  76. mmc_retune_release(host);
  77. }
  78. }
  79. EXPORT_SYMBOL(mmc_retune_unpause);
  80. void mmc_retune_disable(struct mmc_host *host)
  81. {
  82. mmc_retune_unpause(host);
  83. host->can_retune = 0;
  84. del_timer_sync(&host->retune_timer);
  85. host->retune_now = 0;
  86. host->need_retune = 0;
  87. }
  88. void mmc_retune_timer_stop(struct mmc_host *host)
  89. {
  90. del_timer_sync(&host->retune_timer);
  91. }
  92. EXPORT_SYMBOL(mmc_retune_timer_stop);
  93. void mmc_retune_hold(struct mmc_host *host)
  94. {
  95. if (!host->hold_retune)
  96. host->retune_now = 1;
  97. host->hold_retune += 1;
  98. }
  99. void mmc_retune_hold_now(struct mmc_host *host)
  100. {
  101. host->retune_now = 0;
  102. host->hold_retune += 1;
  103. }
  104. void mmc_retune_release(struct mmc_host *host)
  105. {
  106. if (host->hold_retune)
  107. host->hold_retune -= 1;
  108. else
  109. WARN_ON(1);
  110. }
  111. int mmc_retune(struct mmc_host *host)
  112. {
  113. bool return_to_hs400 = false;
  114. int err;
  115. if (host->retune_now)
  116. host->retune_now = 0;
  117. else
  118. return 0;
  119. if (!host->need_retune || host->doing_retune || !host->card)
  120. return 0;
  121. host->need_retune = 0;
  122. host->doing_retune = 1;
  123. if (host->ios.timing == MMC_TIMING_MMC_HS400) {
  124. err = mmc_hs400_to_hs200(host->card);
  125. if (err)
  126. goto out;
  127. return_to_hs400 = true;
  128. if (host->ops->prepare_hs400_tuning)
  129. host->ops->prepare_hs400_tuning(host, &host->ios);
  130. }
  131. err = mmc_execute_tuning(host->card);
  132. if (err)
  133. goto out;
  134. if (return_to_hs400)
  135. err = mmc_hs200_to_hs400(host->card);
  136. out:
  137. host->doing_retune = 0;
  138. return err;
  139. }
  140. static void mmc_retune_timer(unsigned long data)
  141. {
  142. struct mmc_host *host = (struct mmc_host *)data;
  143. mmc_retune_needed(host);
  144. }
  145. /**
  146. * mmc_of_parse() - parse host's device-tree node
  147. * @host: host whose node should be parsed.
  148. *
  149. * To keep the rest of the MMC subsystem unaware of whether DT has been
  150. * used to to instantiate and configure this host instance or not, we
  151. * parse the properties and set respective generic mmc-host flags and
  152. * parameters.
  153. */
  154. int mmc_of_parse(struct mmc_host *host)
  155. {
  156. struct device *dev = host->parent;
  157. u32 bus_width;
  158. int ret;
  159. bool cd_cap_invert, cd_gpio_invert = false;
  160. bool ro_cap_invert, ro_gpio_invert = false;
  161. if (!dev || !dev_fwnode(dev))
  162. return 0;
  163. /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
  164. if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
  165. dev_dbg(host->parent,
  166. "\"bus-width\" property is missing, assuming 1 bit.\n");
  167. bus_width = 1;
  168. }
  169. switch (bus_width) {
  170. case 8:
  171. host->caps |= MMC_CAP_8_BIT_DATA;
  172. /* Hosts capable of 8-bit transfers can also do 4 bits */
  173. case 4:
  174. host->caps |= MMC_CAP_4_BIT_DATA;
  175. break;
  176. case 1:
  177. break;
  178. default:
  179. dev_err(host->parent,
  180. "Invalid \"bus-width\" value %u!\n", bus_width);
  181. return -EINVAL;
  182. }
  183. /* f_max is obtained from the optional "max-frequency" property */
  184. device_property_read_u32(dev, "max-frequency", &host->f_max);
  185. /*
  186. * Configure CD and WP pins. They are both by default active low to
  187. * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
  188. * mmc-gpio helpers are used to attach, configure and use them. If
  189. * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
  190. * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
  191. * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
  192. * is set. If the "non-removable" property is found, the
  193. * MMC_CAP_NONREMOVABLE capability is set and no card-detection
  194. * configuration is performed.
  195. */
  196. /* Parse Card Detection */
  197. if (device_property_read_bool(dev, "non-removable")) {
  198. host->caps |= MMC_CAP_NONREMOVABLE;
  199. } else {
  200. cd_cap_invert = device_property_read_bool(dev, "cd-inverted");
  201. if (device_property_read_bool(dev, "broken-cd"))
  202. host->caps |= MMC_CAP_NEEDS_POLL;
  203. ret = mmc_gpiod_request_cd(host, "cd", 0, true,
  204. 0, &cd_gpio_invert);
  205. if (!ret)
  206. dev_info(host->parent, "Got CD GPIO\n");
  207. else if (ret != -ENOENT && ret != -ENOSYS)
  208. return ret;
  209. /*
  210. * There are two ways to flag that the CD line is inverted:
  211. * through the cd-inverted flag and by the GPIO line itself
  212. * being inverted from the GPIO subsystem. This is a leftover
  213. * from the times when the GPIO subsystem did not make it
  214. * possible to flag a line as inverted.
  215. *
  216. * If the capability on the host AND the GPIO line are
  217. * both inverted, the end result is that the CD line is
  218. * not inverted.
  219. */
  220. if (cd_cap_invert ^ cd_gpio_invert)
  221. host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  222. }
  223. /* Parse Write Protection */
  224. ro_cap_invert = device_property_read_bool(dev, "wp-inverted");
  225. ret = mmc_gpiod_request_ro(host, "wp", 0, false, 0, &ro_gpio_invert);
  226. if (!ret)
  227. dev_info(host->parent, "Got WP GPIO\n");
  228. else if (ret != -ENOENT && ret != -ENOSYS)
  229. return ret;
  230. if (device_property_read_bool(dev, "disable-wp"))
  231. host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
  232. /* See the comment on CD inversion above */
  233. if (ro_cap_invert ^ ro_gpio_invert)
  234. host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  235. if (device_property_read_bool(dev, "cap-sd-highspeed"))
  236. host->caps |= MMC_CAP_SD_HIGHSPEED;
  237. if (device_property_read_bool(dev, "cap-mmc-highspeed"))
  238. host->caps |= MMC_CAP_MMC_HIGHSPEED;
  239. if (device_property_read_bool(dev, "sd-uhs-sdr12"))
  240. host->caps |= MMC_CAP_UHS_SDR12;
  241. if (device_property_read_bool(dev, "sd-uhs-sdr25"))
  242. host->caps |= MMC_CAP_UHS_SDR25;
  243. if (device_property_read_bool(dev, "sd-uhs-sdr50"))
  244. host->caps |= MMC_CAP_UHS_SDR50;
  245. if (device_property_read_bool(dev, "sd-uhs-sdr104"))
  246. host->caps |= MMC_CAP_UHS_SDR104;
  247. if (device_property_read_bool(dev, "sd-uhs-ddr50"))
  248. host->caps |= MMC_CAP_UHS_DDR50;
  249. if (device_property_read_bool(dev, "cap-power-off-card"))
  250. host->caps |= MMC_CAP_POWER_OFF_CARD;
  251. if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
  252. host->caps |= MMC_CAP_HW_RESET;
  253. if (device_property_read_bool(dev, "cap-sdio-irq"))
  254. host->caps |= MMC_CAP_SDIO_IRQ;
  255. if (device_property_read_bool(dev, "full-pwr-cycle"))
  256. host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
  257. if (device_property_read_bool(dev, "keep-power-in-suspend"))
  258. host->pm_caps |= MMC_PM_KEEP_POWER;
  259. if (device_property_read_bool(dev, "wakeup-source") ||
  260. device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
  261. host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  262. if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
  263. host->caps |= MMC_CAP_3_3V_DDR;
  264. if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
  265. host->caps |= MMC_CAP_1_8V_DDR;
  266. if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
  267. host->caps |= MMC_CAP_1_2V_DDR;
  268. if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
  269. host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
  270. if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
  271. host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
  272. if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
  273. host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
  274. if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
  275. host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
  276. if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
  277. host->caps2 |= MMC_CAP2_HS400_ES;
  278. if (device_property_read_bool(dev, "no-sdio"))
  279. host->caps2 |= MMC_CAP2_NO_SDIO;
  280. if (device_property_read_bool(dev, "no-sd"))
  281. host->caps2 |= MMC_CAP2_NO_SD;
  282. if (device_property_read_bool(dev, "no-mmc"))
  283. host->caps2 |= MMC_CAP2_NO_MMC;
  284. host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
  285. if (host->dsr_req && (host->dsr & ~0xffff)) {
  286. dev_err(host->parent,
  287. "device tree specified broken value for DSR: 0x%x, ignoring\n",
  288. host->dsr);
  289. host->dsr_req = 0;
  290. }
  291. return mmc_pwrseq_alloc(host);
  292. }
  293. EXPORT_SYMBOL(mmc_of_parse);
  294. /**
  295. * mmc_alloc_host - initialise the per-host structure.
  296. * @extra: sizeof private data structure
  297. * @dev: pointer to host device model structure
  298. *
  299. * Initialise the per-host structure.
  300. */
  301. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  302. {
  303. int err;
  304. struct mmc_host *host;
  305. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  306. int i;
  307. #endif
  308. host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  309. if (!host)
  310. return NULL;
  311. /* scanning will be enabled when we're ready */
  312. host->rescan_disable = 1;
  313. err = ida_simple_get(&mmc_host_ida, 0, 0, GFP_KERNEL);
  314. if (err < 0) {
  315. kfree(host);
  316. return NULL;
  317. }
  318. host->index = err;
  319. dev_set_name(&host->class_dev, "mmc%d", host->index);
  320. host->parent = dev;
  321. host->class_dev.parent = dev;
  322. host->class_dev.class = &mmc_host_class;
  323. device_initialize(&host->class_dev);
  324. device_enable_async_suspend(&host->class_dev);
  325. if (mmc_gpio_alloc(host)) {
  326. put_device(&host->class_dev);
  327. return NULL;
  328. }
  329. spin_lock_init(&host->lock);
  330. init_waitqueue_head(&host->wq);
  331. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  332. INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
  333. setup_timer(&host->retune_timer, mmc_retune_timer, (unsigned long)host);
  334. /*
  335. * By default, hosts do not support SGIO or large requests.
  336. * They have to set these according to their abilities.
  337. */
  338. host->max_segs = 1;
  339. host->max_seg_size = PAGE_SIZE;
  340. host->max_req_size = PAGE_SIZE;
  341. host->max_blk_size = 512;
  342. host->max_blk_count = PAGE_SIZE / 512;
  343. #ifdef CONFIG_MTK_EMMC_CQ_SUPPORT
  344. for (i = 0; i < EMMC_MAX_QUEUE_DEPTH; i++)
  345. host->areq_que[i] = NULL;
  346. atomic_set(&host->areq_cnt, 0);
  347. host->areq_cur = NULL;
  348. host->done_mrq = NULL;
  349. INIT_LIST_HEAD(&host->cmd_que);
  350. INIT_LIST_HEAD(&host->dat_que);
  351. spin_lock_init(&host->cmd_que_lock);
  352. spin_lock_init(&host->dat_que_lock);
  353. spin_lock_init(&host->que_lock);
  354. init_waitqueue_head(&host->cmp_que);
  355. init_waitqueue_head(&host->cmdq_que);
  356. #endif
  357. return host;
  358. }
  359. EXPORT_SYMBOL(mmc_alloc_host);
  360. /**
  361. * mmc_add_host - initialise host hardware
  362. * @host: mmc host
  363. *
  364. * Register the host with the driver model. The host must be
  365. * prepared to start servicing requests before this function
  366. * completes.
  367. */
  368. int mmc_add_host(struct mmc_host *host)
  369. {
  370. int err;
  371. WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
  372. !host->ops->enable_sdio_irq);
  373. err = device_add(&host->class_dev);
  374. if (err)
  375. return err;
  376. led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
  377. #ifdef CONFIG_DEBUG_FS
  378. mmc_add_host_debugfs(host);
  379. #endif
  380. mmc_start_host(host);
  381. if (!(host->pm_flags & MMC_PM_IGNORE_PM_NOTIFY))
  382. mmc_register_pm_notifier(host);
  383. return 0;
  384. }
  385. EXPORT_SYMBOL(mmc_add_host);
  386. /**
  387. * mmc_remove_host - remove host hardware
  388. * @host: mmc host
  389. *
  390. * Unregister and remove all cards associated with this host,
  391. * and power down the MMC bus. No new requests will be issued
  392. * after this function has returned.
  393. */
  394. void mmc_remove_host(struct mmc_host *host)
  395. {
  396. if (!(host->pm_flags & MMC_PM_IGNORE_PM_NOTIFY))
  397. mmc_unregister_pm_notifier(host);
  398. mmc_stop_host(host);
  399. #ifdef CONFIG_DEBUG_FS
  400. mmc_remove_host_debugfs(host);
  401. #endif
  402. device_del(&host->class_dev);
  403. led_trigger_unregister_simple(host->led);
  404. }
  405. EXPORT_SYMBOL(mmc_remove_host);
  406. /**
  407. * mmc_free_host - free the host structure
  408. * @host: mmc host
  409. *
  410. * Free the host once all references to it have been dropped.
  411. */
  412. void mmc_free_host(struct mmc_host *host)
  413. {
  414. mmc_crypto_free_host(host);
  415. mmc_pwrseq_free(host);
  416. put_device(&host->class_dev);
  417. }
  418. EXPORT_SYMBOL(mmc_free_host);