host.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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/suspend.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/card.h>
  26. #include <linux/mmc/slot-gpio.h>
  27. #include "core.h"
  28. #include "host.h"
  29. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  30. static void mmc_host_classdev_release(struct device *dev)
  31. {
  32. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  33. mutex_destroy(&host->slot.lock);
  34. kfree(host);
  35. }
  36. static struct class mmc_host_class = {
  37. .name = "mmc_host",
  38. .dev_release = mmc_host_classdev_release,
  39. };
  40. int mmc_register_host_class(void)
  41. {
  42. return class_register(&mmc_host_class);
  43. }
  44. void mmc_unregister_host_class(void)
  45. {
  46. class_unregister(&mmc_host_class);
  47. }
  48. static DEFINE_IDR(mmc_host_idr);
  49. static DEFINE_SPINLOCK(mmc_host_lock);
  50. #ifdef CONFIG_MMC_CLKGATE
  51. static ssize_t clkgate_delay_show(struct device *dev,
  52. struct device_attribute *attr, char *buf)
  53. {
  54. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  55. return snprintf(buf, PAGE_SIZE, "%lu\n", host->clkgate_delay);
  56. }
  57. static ssize_t clkgate_delay_store(struct device *dev,
  58. struct device_attribute *attr, const char *buf, size_t count)
  59. {
  60. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  61. unsigned long flags, value;
  62. if (kstrtoul(buf, 0, &value))
  63. return -EINVAL;
  64. spin_lock_irqsave(&host->clk_lock, flags);
  65. host->clkgate_delay = value;
  66. spin_unlock_irqrestore(&host->clk_lock, flags);
  67. return count;
  68. }
  69. /*
  70. * Enabling clock gating will make the core call out to the host
  71. * once up and once down when it performs a request or card operation
  72. * intermingled in any fashion. The driver will see this through
  73. * set_ios() operations with ios.clock field set to 0 to gate (disable)
  74. * the block clock, and to the old frequency to enable it again.
  75. */
  76. static void mmc_host_clk_gate_delayed(struct mmc_host *host)
  77. {
  78. unsigned long tick_ns;
  79. unsigned long freq = host->ios.clock;
  80. unsigned long flags;
  81. if (!freq) {
  82. pr_debug("%s: frequency set to 0 in disable function, "
  83. "this means the clock is already disabled.\n",
  84. mmc_hostname(host));
  85. return;
  86. }
  87. /*
  88. * New requests may have appeared while we were scheduling,
  89. * then there is no reason to delay the check before
  90. * clk_disable().
  91. */
  92. spin_lock_irqsave(&host->clk_lock, flags);
  93. /*
  94. * Delay n bus cycles (at least 8 from MMC spec) before attempting
  95. * to disable the MCI block clock. The reference count may have
  96. * gone up again after this delay due to rescheduling!
  97. */
  98. if (!host->clk_requests) {
  99. spin_unlock_irqrestore(&host->clk_lock, flags);
  100. tick_ns = DIV_ROUND_UP(1000000000, freq);
  101. ndelay(host->clk_delay * tick_ns);
  102. } else {
  103. /* New users appeared while waiting for this work */
  104. spin_unlock_irqrestore(&host->clk_lock, flags);
  105. return;
  106. }
  107. mutex_lock(&host->clk_gate_mutex);
  108. spin_lock_irqsave(&host->clk_lock, flags);
  109. if (!host->clk_requests) {
  110. spin_unlock_irqrestore(&host->clk_lock, flags);
  111. /* This will set host->ios.clock to 0 */
  112. mmc_gate_clock(host);
  113. spin_lock_irqsave(&host->clk_lock, flags);
  114. pr_debug("%s: gated MCI clock\n", mmc_hostname(host));
  115. }
  116. spin_unlock_irqrestore(&host->clk_lock, flags);
  117. mutex_unlock(&host->clk_gate_mutex);
  118. }
  119. /*
  120. * Internal work. Work to disable the clock at some later point.
  121. */
  122. static void mmc_host_clk_gate_work(struct work_struct *work)
  123. {
  124. struct mmc_host *host = container_of(work, struct mmc_host,
  125. clk_gate_work.work);
  126. mmc_host_clk_gate_delayed(host);
  127. }
  128. /**
  129. * mmc_host_clk_hold - ungate hardware MCI clocks
  130. * @host: host to ungate.
  131. *
  132. * Makes sure the host ios.clock is restored to a non-zero value
  133. * past this call. Increase clock reference count and ungate clock
  134. * if we're the first user.
  135. */
  136. void mmc_host_clk_hold(struct mmc_host *host)
  137. {
  138. unsigned long flags;
  139. /* cancel any clock gating work scheduled by mmc_host_clk_release() */
  140. cancel_delayed_work_sync(&host->clk_gate_work);
  141. mutex_lock(&host->clk_gate_mutex);
  142. spin_lock_irqsave(&host->clk_lock, flags);
  143. if (host->clk_gated) {
  144. spin_unlock_irqrestore(&host->clk_lock, flags);
  145. mmc_ungate_clock(host);
  146. spin_lock_irqsave(&host->clk_lock, flags);
  147. pr_debug("%s: ungated MCI clock\n", mmc_hostname(host));
  148. }
  149. host->clk_requests++;
  150. spin_unlock_irqrestore(&host->clk_lock, flags);
  151. mutex_unlock(&host->clk_gate_mutex);
  152. }
  153. /**
  154. * mmc_host_may_gate_card - check if this card may be gated
  155. * @card: card to check.
  156. */
  157. static bool mmc_host_may_gate_card(struct mmc_card *card)
  158. {
  159. /* If there is no card we may gate it */
  160. if (!card)
  161. return true;
  162. /*
  163. * Don't gate SDIO cards! These need to be clocked at all times
  164. * since they may be independent systems generating interrupts
  165. * and other events. The clock requests counter from the core will
  166. * go down to zero since the core does not need it, but we will not
  167. * gate the clock, because there is somebody out there that may still
  168. * be using it.
  169. */
  170. return !(card->quirks & MMC_QUIRK_BROKEN_CLK_GATING);
  171. }
  172. /**
  173. * mmc_host_clk_release - gate off hardware MCI clocks
  174. * @host: host to gate.
  175. *
  176. * Calls the host driver with ios.clock set to zero as often as possible
  177. * in order to gate off hardware MCI clocks. Decrease clock reference
  178. * count and schedule disabling of clock.
  179. */
  180. void mmc_host_clk_release(struct mmc_host *host)
  181. {
  182. unsigned long flags;
  183. spin_lock_irqsave(&host->clk_lock, flags);
  184. host->clk_requests--;
  185. if (mmc_host_may_gate_card(host->card) &&
  186. !host->clk_requests)
  187. schedule_delayed_work(&host->clk_gate_work,
  188. msecs_to_jiffies(host->clkgate_delay));
  189. spin_unlock_irqrestore(&host->clk_lock, flags);
  190. }
  191. /**
  192. * mmc_host_clk_rate - get current clock frequency setting
  193. * @host: host to get the clock frequency for.
  194. *
  195. * Returns current clock frequency regardless of gating.
  196. */
  197. unsigned int mmc_host_clk_rate(struct mmc_host *host)
  198. {
  199. unsigned long freq;
  200. unsigned long flags;
  201. spin_lock_irqsave(&host->clk_lock, flags);
  202. if (host->clk_gated)
  203. freq = host->clk_old;
  204. else
  205. freq = host->ios.clock;
  206. spin_unlock_irqrestore(&host->clk_lock, flags);
  207. return freq;
  208. }
  209. /**
  210. * mmc_host_clk_init - set up clock gating code
  211. * @host: host with potential clock to control
  212. */
  213. static inline void mmc_host_clk_init(struct mmc_host *host)
  214. {
  215. host->clk_requests = 0;
  216. /* Hold MCI clock for 8 cycles by default */
  217. host->clk_delay = 8;
  218. /*
  219. * Default clock gating delay is 0ms to avoid wasting power.
  220. * This value can be tuned by writing into sysfs entry.
  221. */
  222. host->clkgate_delay = 0;
  223. host->clk_gated = false;
  224. INIT_DELAYED_WORK(&host->clk_gate_work, mmc_host_clk_gate_work);
  225. spin_lock_init(&host->clk_lock);
  226. mutex_init(&host->clk_gate_mutex);
  227. }
  228. /**
  229. * mmc_host_clk_exit - shut down clock gating code
  230. * @host: host with potential clock to control
  231. */
  232. static inline void mmc_host_clk_exit(struct mmc_host *host)
  233. {
  234. /*
  235. * Wait for any outstanding gate and then make sure we're
  236. * ungated before exiting.
  237. */
  238. if (cancel_delayed_work_sync(&host->clk_gate_work))
  239. mmc_host_clk_gate_delayed(host);
  240. if (host->clk_gated)
  241. mmc_host_clk_hold(host);
  242. /* There should be only one user now */
  243. WARN_ON(host->clk_requests > 1);
  244. }
  245. static inline void mmc_host_clk_sysfs_init(struct mmc_host *host)
  246. {
  247. host->clkgate_delay_attr.show = clkgate_delay_show;
  248. host->clkgate_delay_attr.store = clkgate_delay_store;
  249. sysfs_attr_init(&host->clkgate_delay_attr.attr);
  250. host->clkgate_delay_attr.attr.name = "clkgate_delay";
  251. host->clkgate_delay_attr.attr.mode = S_IRUGO | S_IWUSR;
  252. if (device_create_file(&host->class_dev, &host->clkgate_delay_attr))
  253. pr_err("%s: Failed to create clkgate_delay sysfs entry\n",
  254. mmc_hostname(host));
  255. }
  256. #else
  257. static inline void mmc_host_clk_init(struct mmc_host *host)
  258. {
  259. }
  260. static inline void mmc_host_clk_exit(struct mmc_host *host)
  261. {
  262. }
  263. static inline void mmc_host_clk_sysfs_init(struct mmc_host *host)
  264. {
  265. }
  266. #endif
  267. /**
  268. * mmc_of_parse() - parse host's device-tree node
  269. * @host: host whose node should be parsed.
  270. *
  271. * To keep the rest of the MMC subsystem unaware of whether DT has been
  272. * used to to instantiate and configure this host instance or not, we
  273. * parse the properties and set respective generic mmc-host flags and
  274. * parameters.
  275. */
  276. int mmc_of_parse(struct mmc_host *host)
  277. {
  278. struct device_node *np;
  279. u32 bus_width;
  280. bool explicit_inv_wp, gpio_inv_wp = false;
  281. enum of_gpio_flags flags;
  282. int len, ret, gpio;
  283. if (!host->parent || !host->parent->of_node)
  284. return 0;
  285. np = host->parent->of_node;
  286. /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
  287. if (of_property_read_u32(np, "bus-width", &bus_width) < 0) {
  288. dev_dbg(host->parent,
  289. "\"bus-width\" property is missing, assuming 1 bit.\n");
  290. bus_width = 1;
  291. }
  292. switch (bus_width) {
  293. case 8:
  294. host->caps |= MMC_CAP_8_BIT_DATA;
  295. /* Hosts capable of 8-bit transfers can also do 4 bits */
  296. case 4:
  297. host->caps |= MMC_CAP_4_BIT_DATA;
  298. break;
  299. case 1:
  300. break;
  301. default:
  302. dev_err(host->parent,
  303. "Invalid \"bus-width\" value %ud!\n", bus_width);
  304. return -EINVAL;
  305. }
  306. /* f_max is obtained from the optional "max-frequency" property */
  307. of_property_read_u32(np, "max-frequency", &host->f_max);
  308. /*
  309. * Configure CD and WP pins. They are both by default active low to
  310. * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
  311. * mmc-gpio helpers are used to attach, configure and use them. If
  312. * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
  313. * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
  314. * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
  315. * is set. If the "non-removable" property is found, the
  316. * MMC_CAP_NONREMOVABLE capability is set and no card-detection
  317. * configuration is performed.
  318. */
  319. /* Parse Card Detection */
  320. if (of_find_property(np, "non-removable", &len)) {
  321. host->caps |= MMC_CAP_NONREMOVABLE;
  322. } else {
  323. bool explicit_inv_cd, gpio_inv_cd = false;
  324. explicit_inv_cd = of_property_read_bool(np, "cd-inverted");
  325. if (of_find_property(np, "broken-cd", &len))
  326. host->caps |= MMC_CAP_NEEDS_POLL;
  327. gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
  328. if (gpio == -EPROBE_DEFER)
  329. return gpio;
  330. if (gpio_is_valid(gpio)) {
  331. if (!(flags & OF_GPIO_ACTIVE_LOW))
  332. gpio_inv_cd = true;
  333. ret = mmc_gpio_request_cd(host, gpio, 0);
  334. if (ret < 0) {
  335. dev_err(host->parent,
  336. "Failed to request CD GPIO #%d: %d!\n",
  337. gpio, ret);
  338. return ret;
  339. } else {
  340. dev_info(host->parent, "Got CD GPIO #%d.\n",
  341. gpio);
  342. }
  343. }
  344. if (explicit_inv_cd ^ gpio_inv_cd)
  345. host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  346. }
  347. /* Parse Write Protection */
  348. explicit_inv_wp = of_property_read_bool(np, "wp-inverted");
  349. gpio = of_get_named_gpio_flags(np, "wp-gpios", 0, &flags);
  350. if (gpio == -EPROBE_DEFER) {
  351. ret = -EPROBE_DEFER;
  352. goto out;
  353. }
  354. if (gpio_is_valid(gpio)) {
  355. if (!(flags & OF_GPIO_ACTIVE_LOW))
  356. gpio_inv_wp = true;
  357. ret = mmc_gpio_request_ro(host, gpio);
  358. if (ret < 0) {
  359. dev_err(host->parent,
  360. "Failed to request WP GPIO: %d!\n", ret);
  361. goto out;
  362. } else {
  363. dev_info(host->parent, "Got WP GPIO #%d.\n",
  364. gpio);
  365. }
  366. }
  367. if (explicit_inv_wp ^ gpio_inv_wp)
  368. host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  369. if (of_find_property(np, "cap-sd-highspeed", &len))
  370. host->caps |= MMC_CAP_SD_HIGHSPEED;
  371. if (of_find_property(np, "cap-mmc-highspeed", &len))
  372. host->caps |= MMC_CAP_MMC_HIGHSPEED;
  373. if (of_find_property(np, "cap-power-off-card", &len))
  374. host->caps |= MMC_CAP_POWER_OFF_CARD;
  375. if (of_find_property(np, "cap-sdio-irq", &len))
  376. host->caps |= MMC_CAP_SDIO_IRQ;
  377. if (of_find_property(np, "full-pwr-cycle", &len))
  378. host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
  379. if (of_find_property(np, "keep-power-in-suspend", &len))
  380. host->pm_caps |= MMC_PM_KEEP_POWER;
  381. if (of_find_property(np, "enable-sdio-wakeup", &len))
  382. host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  383. return 0;
  384. out:
  385. mmc_gpio_free_cd(host);
  386. return ret;
  387. }
  388. EXPORT_SYMBOL(mmc_of_parse);
  389. /**
  390. * mmc_alloc_host - initialise the per-host structure.
  391. * @extra: sizeof private data structure
  392. * @dev: pointer to host device model structure
  393. *
  394. * Initialise the per-host structure.
  395. */
  396. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  397. {
  398. int err;
  399. struct mmc_host *host;
  400. host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  401. if (!host)
  402. return NULL;
  403. /* scanning will be enabled when we're ready */
  404. host->rescan_disable = 1;
  405. idr_preload(GFP_KERNEL);
  406. spin_lock(&mmc_host_lock);
  407. err = idr_alloc(&mmc_host_idr, host, 0, 0, GFP_NOWAIT);
  408. if (err >= 0)
  409. host->index = err;
  410. spin_unlock(&mmc_host_lock);
  411. idr_preload_end();
  412. if (err < 0)
  413. goto free;
  414. dev_set_name(&host->class_dev, "mmc%d", host->index);
  415. host->parent = dev;
  416. host->class_dev.parent = dev;
  417. host->class_dev.class = &mmc_host_class;
  418. device_initialize(&host->class_dev);
  419. mmc_host_clk_init(host);
  420. mutex_init(&host->slot.lock);
  421. host->slot.cd_irq = -EINVAL;
  422. spin_lock_init(&host->lock);
  423. init_waitqueue_head(&host->wq);
  424. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  425. #ifdef CONFIG_PM
  426. host->pm_notify.notifier_call = mmc_pm_notify;
  427. #endif
  428. /*
  429. * By default, hosts do not support SGIO or large requests.
  430. * They have to set these according to their abilities.
  431. */
  432. host->max_segs = 1;
  433. host->max_seg_size = PAGE_CACHE_SIZE;
  434. host->max_req_size = PAGE_CACHE_SIZE;
  435. host->max_blk_size = 512;
  436. host->max_blk_count = PAGE_CACHE_SIZE / 512;
  437. return host;
  438. free:
  439. kfree(host);
  440. return NULL;
  441. }
  442. EXPORT_SYMBOL(mmc_alloc_host);
  443. /**
  444. * mmc_add_host - initialise host hardware
  445. * @host: mmc host
  446. *
  447. * Register the host with the driver model. The host must be
  448. * prepared to start servicing requests before this function
  449. * completes.
  450. */
  451. int mmc_add_host(struct mmc_host *host)
  452. {
  453. int err;
  454. WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
  455. !host->ops->enable_sdio_irq);
  456. err = device_add(&host->class_dev);
  457. if (err)
  458. return err;
  459. led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
  460. #ifdef CONFIG_DEBUG_FS
  461. mmc_add_host_debugfs(host);
  462. #endif
  463. mmc_host_clk_sysfs_init(host);
  464. mmc_start_host(host);
  465. register_pm_notifier(&host->pm_notify);
  466. return 0;
  467. }
  468. EXPORT_SYMBOL(mmc_add_host);
  469. /**
  470. * mmc_remove_host - remove host hardware
  471. * @host: mmc host
  472. *
  473. * Unregister and remove all cards associated with this host,
  474. * and power down the MMC bus. No new requests will be issued
  475. * after this function has returned.
  476. */
  477. void mmc_remove_host(struct mmc_host *host)
  478. {
  479. unregister_pm_notifier(&host->pm_notify);
  480. mmc_stop_host(host);
  481. #ifdef CONFIG_DEBUG_FS
  482. mmc_remove_host_debugfs(host);
  483. #endif
  484. device_del(&host->class_dev);
  485. led_trigger_unregister_simple(host->led);
  486. mmc_host_clk_exit(host);
  487. }
  488. EXPORT_SYMBOL(mmc_remove_host);
  489. /**
  490. * mmc_free_host - free the host structure
  491. * @host: mmc host
  492. *
  493. * Free the host once all references to it have been dropped.
  494. */
  495. void mmc_free_host(struct mmc_host *host)
  496. {
  497. spin_lock(&mmc_host_lock);
  498. idr_remove(&mmc_host_idr, host->index);
  499. spin_unlock(&mmc_host_lock);
  500. put_device(&host->class_dev);
  501. }
  502. EXPORT_SYMBOL(mmc_free_host);