host.c 16 KB

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