host.c 12 KB

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