host.c 11 KB

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