host.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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/pagemap.h>
  18. #include <linux/export.h>
  19. #include <linux/leds.h>
  20. #include <linux/slab.h>
  21. #include <linux/suspend.h>
  22. #include <linux/mmc/host.h>
  23. #include <linux/mmc/card.h>
  24. #include "core.h"
  25. #include "host.h"
  26. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  27. static void mmc_host_classdev_release(struct device *dev)
  28. {
  29. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  30. kfree(host);
  31. }
  32. static struct class mmc_host_class = {
  33. .name = "mmc_host",
  34. .dev_release = mmc_host_classdev_release,
  35. };
  36. int mmc_register_host_class(void)
  37. {
  38. return class_register(&mmc_host_class);
  39. }
  40. void mmc_unregister_host_class(void)
  41. {
  42. class_unregister(&mmc_host_class);
  43. }
  44. static DEFINE_IDR(mmc_host_idr);
  45. static DEFINE_SPINLOCK(mmc_host_lock);
  46. #ifdef CONFIG_MMC_CLKGATE
  47. /*
  48. * Enabling clock gating will make the core call out to the host
  49. * once up and once down when it performs a request or card operation
  50. * intermingled in any fashion. The driver will see this through
  51. * set_ios() operations with ios.clock field set to 0 to gate (disable)
  52. * the block clock, and to the old frequency to enable it again.
  53. */
  54. static void mmc_host_clk_gate_delayed(struct mmc_host *host)
  55. {
  56. unsigned long tick_ns;
  57. unsigned long freq = host->ios.clock;
  58. unsigned long flags;
  59. if (!freq) {
  60. pr_debug("%s: frequency set to 0 in disable function, "
  61. "this means the clock is already disabled.\n",
  62. mmc_hostname(host));
  63. return;
  64. }
  65. /*
  66. * New requests may have appeared while we were scheduling,
  67. * then there is no reason to delay the check before
  68. * clk_disable().
  69. */
  70. spin_lock_irqsave(&host->clk_lock, flags);
  71. /*
  72. * Delay n bus cycles (at least 8 from MMC spec) before attempting
  73. * to disable the MCI block clock. The reference count may have
  74. * gone up again after this delay due to rescheduling!
  75. */
  76. if (!host->clk_requests) {
  77. spin_unlock_irqrestore(&host->clk_lock, flags);
  78. tick_ns = DIV_ROUND_UP(1000000000, freq);
  79. ndelay(host->clk_delay * tick_ns);
  80. } else {
  81. /* New users appeared while waiting for this work */
  82. spin_unlock_irqrestore(&host->clk_lock, flags);
  83. return;
  84. }
  85. mutex_lock(&host->clk_gate_mutex);
  86. spin_lock_irqsave(&host->clk_lock, flags);
  87. if (!host->clk_requests) {
  88. spin_unlock_irqrestore(&host->clk_lock, flags);
  89. /* This will set host->ios.clock to 0 */
  90. mmc_gate_clock(host);
  91. spin_lock_irqsave(&host->clk_lock, flags);
  92. pr_debug("%s: gated MCI clock\n", mmc_hostname(host));
  93. }
  94. spin_unlock_irqrestore(&host->clk_lock, flags);
  95. mutex_unlock(&host->clk_gate_mutex);
  96. }
  97. /*
  98. * Internal work. Work to disable the clock at some later point.
  99. */
  100. static void mmc_host_clk_gate_work(struct work_struct *work)
  101. {
  102. struct mmc_host *host = container_of(work, struct mmc_host,
  103. clk_gate_work);
  104. mmc_host_clk_gate_delayed(host);
  105. }
  106. /**
  107. * mmc_host_clk_hold - ungate hardware MCI clocks
  108. * @host: host to ungate.
  109. *
  110. * Makes sure the host ios.clock is restored to a non-zero value
  111. * past this call. Increase clock reference count and ungate clock
  112. * if we're the first user.
  113. */
  114. void mmc_host_clk_hold(struct mmc_host *host)
  115. {
  116. unsigned long flags;
  117. mutex_lock(&host->clk_gate_mutex);
  118. spin_lock_irqsave(&host->clk_lock, flags);
  119. if (host->clk_gated) {
  120. spin_unlock_irqrestore(&host->clk_lock, flags);
  121. mmc_ungate_clock(host);
  122. spin_lock_irqsave(&host->clk_lock, flags);
  123. pr_debug("%s: ungated MCI clock\n", mmc_hostname(host));
  124. }
  125. host->clk_requests++;
  126. spin_unlock_irqrestore(&host->clk_lock, flags);
  127. mutex_unlock(&host->clk_gate_mutex);
  128. }
  129. /**
  130. * mmc_host_may_gate_card - check if this card may be gated
  131. * @card: card to check.
  132. */
  133. static bool mmc_host_may_gate_card(struct mmc_card *card)
  134. {
  135. /* If there is no card we may gate it */
  136. if (!card)
  137. return true;
  138. /*
  139. * Don't gate SDIO cards! These need to be clocked at all times
  140. * since they may be independent systems generating interrupts
  141. * and other events. The clock requests counter from the core will
  142. * go down to zero since the core does not need it, but we will not
  143. * gate the clock, because there is somebody out there that may still
  144. * be using it.
  145. */
  146. return !(card->quirks & MMC_QUIRK_BROKEN_CLK_GATING);
  147. }
  148. /**
  149. * mmc_host_clk_release - gate off hardware MCI clocks
  150. * @host: host to gate.
  151. *
  152. * Calls the host driver with ios.clock set to zero as often as possible
  153. * in order to gate off hardware MCI clocks. Decrease clock reference
  154. * count and schedule disabling of clock.
  155. */
  156. void mmc_host_clk_release(struct mmc_host *host)
  157. {
  158. unsigned long flags;
  159. spin_lock_irqsave(&host->clk_lock, flags);
  160. host->clk_requests--;
  161. if (mmc_host_may_gate_card(host->card) &&
  162. !host->clk_requests)
  163. queue_work(system_nrt_wq, &host->clk_gate_work);
  164. spin_unlock_irqrestore(&host->clk_lock, flags);
  165. }
  166. /**
  167. * mmc_host_clk_rate - get current clock frequency setting
  168. * @host: host to get the clock frequency for.
  169. *
  170. * Returns current clock frequency regardless of gating.
  171. */
  172. unsigned int mmc_host_clk_rate(struct mmc_host *host)
  173. {
  174. unsigned long freq;
  175. unsigned long flags;
  176. spin_lock_irqsave(&host->clk_lock, flags);
  177. if (host->clk_gated)
  178. freq = host->clk_old;
  179. else
  180. freq = host->ios.clock;
  181. spin_unlock_irqrestore(&host->clk_lock, flags);
  182. return freq;
  183. }
  184. /**
  185. * mmc_host_clk_init - set up clock gating code
  186. * @host: host with potential clock to control
  187. */
  188. static inline void mmc_host_clk_init(struct mmc_host *host)
  189. {
  190. host->clk_requests = 0;
  191. /* Hold MCI clock for 8 cycles by default */
  192. host->clk_delay = 8;
  193. host->clk_gated = false;
  194. INIT_WORK(&host->clk_gate_work, mmc_host_clk_gate_work);
  195. spin_lock_init(&host->clk_lock);
  196. mutex_init(&host->clk_gate_mutex);
  197. }
  198. /**
  199. * mmc_host_clk_exit - shut down clock gating code
  200. * @host: host with potential clock to control
  201. */
  202. static inline void mmc_host_clk_exit(struct mmc_host *host)
  203. {
  204. /*
  205. * Wait for any outstanding gate and then make sure we're
  206. * ungated before exiting.
  207. */
  208. if (cancel_work_sync(&host->clk_gate_work))
  209. mmc_host_clk_gate_delayed(host);
  210. if (host->clk_gated)
  211. mmc_host_clk_hold(host);
  212. /* There should be only one user now */
  213. WARN_ON(host->clk_requests > 1);
  214. }
  215. #else
  216. static inline void mmc_host_clk_init(struct mmc_host *host)
  217. {
  218. }
  219. static inline void mmc_host_clk_exit(struct mmc_host *host)
  220. {
  221. }
  222. #endif
  223. /**
  224. * mmc_alloc_host - initialise the per-host structure.
  225. * @extra: sizeof private data structure
  226. * @dev: pointer to host device model structure
  227. *
  228. * Initialise the per-host structure.
  229. */
  230. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  231. {
  232. int err;
  233. struct mmc_host *host;
  234. if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
  235. return NULL;
  236. host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  237. if (!host)
  238. return NULL;
  239. spin_lock(&mmc_host_lock);
  240. err = idr_get_new(&mmc_host_idr, host, &host->index);
  241. spin_unlock(&mmc_host_lock);
  242. if (err)
  243. goto free;
  244. dev_set_name(&host->class_dev, "mmc%d", host->index);
  245. host->parent = dev;
  246. host->class_dev.parent = dev;
  247. host->class_dev.class = &mmc_host_class;
  248. device_initialize(&host->class_dev);
  249. mmc_host_clk_init(host);
  250. spin_lock_init(&host->lock);
  251. init_waitqueue_head(&host->wq);
  252. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  253. INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
  254. #ifdef CONFIG_PM
  255. host->pm_notify.notifier_call = mmc_pm_notify;
  256. #endif
  257. /*
  258. * By default, hosts do not support SGIO or large requests.
  259. * They have to set these according to their abilities.
  260. */
  261. host->max_segs = 1;
  262. host->max_seg_size = PAGE_CACHE_SIZE;
  263. host->max_req_size = PAGE_CACHE_SIZE;
  264. host->max_blk_size = 512;
  265. host->max_blk_count = PAGE_CACHE_SIZE / 512;
  266. /*
  267. * Enable runtime power management by default. This flag was added due
  268. * to runtime power management causing disruption for some users, but
  269. * the power on/off code has been improved since then.
  270. *
  271. * We'll enable this flag by default as an experiment, and if no
  272. * problems are reported, we will follow up later and remove the flag
  273. * altogether.
  274. */
  275. host->caps = MMC_CAP_POWER_OFF_CARD;
  276. return host;
  277. free:
  278. kfree(host);
  279. return NULL;
  280. }
  281. EXPORT_SYMBOL(mmc_alloc_host);
  282. /**
  283. * mmc_add_host - initialise host hardware
  284. * @host: mmc host
  285. *
  286. * Register the host with the driver model. The host must be
  287. * prepared to start servicing requests before this function
  288. * completes.
  289. */
  290. int mmc_add_host(struct mmc_host *host)
  291. {
  292. int err;
  293. WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
  294. !host->ops->enable_sdio_irq);
  295. err = device_add(&host->class_dev);
  296. if (err)
  297. return err;
  298. led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
  299. #ifdef CONFIG_DEBUG_FS
  300. mmc_add_host_debugfs(host);
  301. #endif
  302. mmc_start_host(host);
  303. register_pm_notifier(&host->pm_notify);
  304. return 0;
  305. }
  306. EXPORT_SYMBOL(mmc_add_host);
  307. /**
  308. * mmc_remove_host - remove host hardware
  309. * @host: mmc host
  310. *
  311. * Unregister and remove all cards associated with this host,
  312. * and power down the MMC bus. No new requests will be issued
  313. * after this function has returned.
  314. */
  315. void mmc_remove_host(struct mmc_host *host)
  316. {
  317. unregister_pm_notifier(&host->pm_notify);
  318. mmc_stop_host(host);
  319. #ifdef CONFIG_DEBUG_FS
  320. mmc_remove_host_debugfs(host);
  321. #endif
  322. device_del(&host->class_dev);
  323. led_trigger_unregister_simple(host->led);
  324. mmc_host_clk_exit(host);
  325. }
  326. EXPORT_SYMBOL(mmc_remove_host);
  327. /**
  328. * mmc_free_host - free the host structure
  329. * @host: mmc host
  330. *
  331. * Free the host once all references to it have been dropped.
  332. */
  333. void mmc_free_host(struct mmc_host *host)
  334. {
  335. spin_lock(&mmc_host_lock);
  336. idr_remove(&mmc_host_idr, host->index);
  337. spin_unlock(&mmc_host_lock);
  338. put_device(&host->class_dev);
  339. }
  340. EXPORT_SYMBOL(mmc_free_host);