host.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/mmc/core/host.c
  4. *
  5. * Copyright (C) 2003 Russell King, All Rights Reserved.
  6. * Copyright (C) 2007-2008 Pierre Ossman
  7. * Copyright (C) 2010 Linus Walleij
  8. *
  9. * MMC host class device management
  10. */
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/idr.h>
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/pm_wakeup.h>
  18. #include <linux/export.h>
  19. #include <linux/leds.h>
  20. #include <linux/slab.h>
  21. #include <linux/mmc/host.h>
  22. #include <linux/mmc/card.h>
  23. #include <linux/mmc/slot-gpio.h>
  24. #include "core.h"
  25. #include "crypto.h"
  26. #include "host.h"
  27. #include "slot-gpio.h"
  28. #include "pwrseq.h"
  29. #include "sdio_ops.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. #ifdef CONFIG_PM_SLEEP
  33. static int mmc_host_class_prepare(struct device *dev)
  34. {
  35. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  36. /*
  37. * It's safe to access the bus_ops pointer, as both userspace and the
  38. * workqueue for detecting cards are frozen at this point.
  39. */
  40. if (!host->bus_ops)
  41. return 0;
  42. /* Validate conditions for system suspend. */
  43. if (host->bus_ops->pre_suspend)
  44. return host->bus_ops->pre_suspend(host);
  45. return 0;
  46. }
  47. static void mmc_host_class_complete(struct device *dev)
  48. {
  49. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  50. _mmc_detect_change(host, 0, false);
  51. }
  52. static const struct dev_pm_ops mmc_host_class_dev_pm_ops = {
  53. .prepare = mmc_host_class_prepare,
  54. .complete = mmc_host_class_complete,
  55. };
  56. #define MMC_HOST_CLASS_DEV_PM_OPS (&mmc_host_class_dev_pm_ops)
  57. #else
  58. #define MMC_HOST_CLASS_DEV_PM_OPS NULL
  59. #endif
  60. static void mmc_host_classdev_release(struct device *dev)
  61. {
  62. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  63. wakeup_source_unregister(host->ws);
  64. if (of_alias_get_id(host->parent->of_node, "mmc") < 0)
  65. ida_simple_remove(&mmc_host_ida, host->index);
  66. kfree(host);
  67. }
  68. static int mmc_host_classdev_shutdown(struct device *dev)
  69. {
  70. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  71. __mmc_stop_host(host);
  72. return 0;
  73. }
  74. static struct class mmc_host_class = {
  75. .name = "mmc_host",
  76. .dev_release = mmc_host_classdev_release,
  77. .shutdown_pre = mmc_host_classdev_shutdown,
  78. .pm = MMC_HOST_CLASS_DEV_PM_OPS,
  79. };
  80. int mmc_register_host_class(void)
  81. {
  82. return class_register(&mmc_host_class);
  83. }
  84. void mmc_unregister_host_class(void)
  85. {
  86. class_unregister(&mmc_host_class);
  87. }
  88. void mmc_retune_enable(struct mmc_host *host)
  89. {
  90. host->can_retune = 1;
  91. if (host->retune_period)
  92. mod_timer(&host->retune_timer,
  93. jiffies + host->retune_period * HZ);
  94. }
  95. /*
  96. * Pause re-tuning for a small set of operations. The pause begins after the
  97. * next command and after first doing re-tuning.
  98. */
  99. void mmc_retune_pause(struct mmc_host *host)
  100. {
  101. if (!host->retune_paused) {
  102. host->retune_paused = 1;
  103. mmc_retune_needed(host);
  104. mmc_retune_hold(host);
  105. }
  106. }
  107. EXPORT_SYMBOL(mmc_retune_pause);
  108. void mmc_retune_unpause(struct mmc_host *host)
  109. {
  110. if (host->retune_paused) {
  111. host->retune_paused = 0;
  112. mmc_retune_release(host);
  113. }
  114. }
  115. EXPORT_SYMBOL(mmc_retune_unpause);
  116. void mmc_retune_disable(struct mmc_host *host)
  117. {
  118. mmc_retune_unpause(host);
  119. host->can_retune = 0;
  120. del_timer_sync(&host->retune_timer);
  121. host->retune_now = 0;
  122. host->need_retune = 0;
  123. }
  124. void mmc_retune_timer_stop(struct mmc_host *host)
  125. {
  126. del_timer_sync(&host->retune_timer);
  127. }
  128. EXPORT_SYMBOL(mmc_retune_timer_stop);
  129. void mmc_retune_hold(struct mmc_host *host)
  130. {
  131. if (!host->hold_retune)
  132. host->retune_now = 1;
  133. host->hold_retune += 1;
  134. }
  135. void mmc_retune_release(struct mmc_host *host)
  136. {
  137. if (host->hold_retune)
  138. host->hold_retune -= 1;
  139. else
  140. WARN_ON(1);
  141. }
  142. EXPORT_SYMBOL(mmc_retune_release);
  143. int mmc_retune(struct mmc_host *host)
  144. {
  145. bool return_to_hs400 = false;
  146. int err;
  147. if (host->retune_now)
  148. host->retune_now = 0;
  149. else
  150. return 0;
  151. if (!host->need_retune || host->doing_retune || !host->card)
  152. return 0;
  153. host->need_retune = 0;
  154. host->doing_retune = 1;
  155. if (host->ios.timing == MMC_TIMING_MMC_HS400) {
  156. err = mmc_hs400_to_hs200(host->card);
  157. if (err)
  158. goto out;
  159. return_to_hs400 = true;
  160. }
  161. err = mmc_execute_tuning(host->card);
  162. if (err)
  163. goto out;
  164. if (return_to_hs400)
  165. err = mmc_hs200_to_hs400(host->card);
  166. out:
  167. host->doing_retune = 0;
  168. return err;
  169. }
  170. static void mmc_retune_timer(struct timer_list *t)
  171. {
  172. struct mmc_host *host = from_timer(host, t, retune_timer);
  173. mmc_retune_needed(host);
  174. }
  175. /**
  176. * mmc_of_parse() - parse host's device-tree node
  177. * @host: host whose node should be parsed.
  178. *
  179. * To keep the rest of the MMC subsystem unaware of whether DT has been
  180. * used to to instantiate and configure this host instance or not, we
  181. * parse the properties and set respective generic mmc-host flags and
  182. * parameters.
  183. */
  184. int mmc_of_parse(struct mmc_host *host)
  185. {
  186. struct device *dev = host->parent;
  187. u32 bus_width, drv_type, cd_debounce_delay_ms;
  188. int ret;
  189. if (!dev || !dev_fwnode(dev))
  190. return 0;
  191. /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
  192. if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
  193. dev_dbg(host->parent,
  194. "\"bus-width\" property is missing, assuming 1 bit.\n");
  195. bus_width = 1;
  196. }
  197. switch (bus_width) {
  198. case 8:
  199. host->caps |= MMC_CAP_8_BIT_DATA;
  200. fallthrough; /* Hosts capable of 8-bit can also do 4 bits */
  201. case 4:
  202. host->caps |= MMC_CAP_4_BIT_DATA;
  203. break;
  204. case 1:
  205. break;
  206. default:
  207. dev_err(host->parent,
  208. "Invalid \"bus-width\" value %u!\n", bus_width);
  209. return -EINVAL;
  210. }
  211. /* f_max is obtained from the optional "max-frequency" property */
  212. device_property_read_u32(dev, "max-frequency", &host->f_max);
  213. /*
  214. * Configure CD and WP pins. They are both by default active low to
  215. * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
  216. * mmc-gpio helpers are used to attach, configure and use them. If
  217. * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
  218. * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
  219. * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
  220. * is set. If the "non-removable" property is found, the
  221. * MMC_CAP_NONREMOVABLE capability is set and no card-detection
  222. * configuration is performed.
  223. */
  224. /* Parse Card Detection */
  225. if (device_property_read_bool(dev, "non-removable")) {
  226. host->caps |= MMC_CAP_NONREMOVABLE;
  227. } else {
  228. if (device_property_read_bool(dev, "cd-inverted"))
  229. host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  230. if (device_property_read_u32(dev, "cd-debounce-delay-ms",
  231. &cd_debounce_delay_ms))
  232. cd_debounce_delay_ms = 200;
  233. if (device_property_read_bool(dev, "broken-cd"))
  234. host->caps |= MMC_CAP_NEEDS_POLL;
  235. ret = mmc_gpiod_request_cd(host, "cd", 0, false,
  236. cd_debounce_delay_ms * 1000);
  237. if (!ret)
  238. dev_info(host->parent, "Got CD GPIO\n");
  239. else if (ret != -ENOENT && ret != -ENOSYS)
  240. return ret;
  241. }
  242. /* Parse Write Protection */
  243. if (device_property_read_bool(dev, "wp-inverted"))
  244. host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  245. ret = mmc_gpiod_request_ro(host, "wp", 0, 0);
  246. if (!ret)
  247. dev_info(host->parent, "Got WP GPIO\n");
  248. else if (ret != -ENOENT && ret != -ENOSYS)
  249. return ret;
  250. if (device_property_read_bool(dev, "disable-wp"))
  251. host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
  252. if (device_property_read_bool(dev, "cap-sd-highspeed"))
  253. host->caps |= MMC_CAP_SD_HIGHSPEED;
  254. if (device_property_read_bool(dev, "cap-mmc-highspeed"))
  255. host->caps |= MMC_CAP_MMC_HIGHSPEED;
  256. if (device_property_read_bool(dev, "sd-uhs-sdr12"))
  257. host->caps |= MMC_CAP_UHS_SDR12;
  258. if (device_property_read_bool(dev, "sd-uhs-sdr25"))
  259. host->caps |= MMC_CAP_UHS_SDR25;
  260. if (device_property_read_bool(dev, "sd-uhs-sdr50"))
  261. host->caps |= MMC_CAP_UHS_SDR50;
  262. if (device_property_read_bool(dev, "sd-uhs-sdr104"))
  263. host->caps |= MMC_CAP_UHS_SDR104;
  264. if (device_property_read_bool(dev, "sd-uhs-ddr50"))
  265. host->caps |= MMC_CAP_UHS_DDR50;
  266. if (device_property_read_bool(dev, "cap-power-off-card"))
  267. host->caps |= MMC_CAP_POWER_OFF_CARD;
  268. if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
  269. host->caps |= MMC_CAP_HW_RESET;
  270. if (device_property_read_bool(dev, "cap-sdio-irq"))
  271. host->caps |= MMC_CAP_SDIO_IRQ;
  272. if (device_property_read_bool(dev, "full-pwr-cycle"))
  273. host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
  274. if (device_property_read_bool(dev, "full-pwr-cycle-in-suspend"))
  275. host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND;
  276. if (device_property_read_bool(dev, "keep-power-in-suspend"))
  277. host->pm_caps |= MMC_PM_KEEP_POWER;
  278. if (device_property_read_bool(dev, "wakeup-source") ||
  279. device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
  280. host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  281. if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
  282. host->caps |= MMC_CAP_3_3V_DDR;
  283. if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
  284. host->caps |= MMC_CAP_1_8V_DDR;
  285. if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
  286. host->caps |= MMC_CAP_1_2V_DDR;
  287. if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
  288. host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
  289. if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
  290. host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
  291. if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
  292. host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
  293. if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
  294. host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
  295. if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
  296. host->caps2 |= MMC_CAP2_HS400_ES;
  297. if (device_property_read_bool(dev, "no-sdio"))
  298. host->caps2 |= MMC_CAP2_NO_SDIO;
  299. if (device_property_read_bool(dev, "no-sd"))
  300. host->caps2 |= MMC_CAP2_NO_SD;
  301. if (device_property_read_bool(dev, "no-mmc"))
  302. host->caps2 |= MMC_CAP2_NO_MMC;
  303. /* Must be after "non-removable" check */
  304. if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
  305. if (host->caps & MMC_CAP_NONREMOVABLE)
  306. host->fixed_drv_type = drv_type;
  307. else
  308. dev_err(host->parent,
  309. "can't use fixed driver type, media is removable\n");
  310. }
  311. host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
  312. if (host->dsr_req && (host->dsr & ~0xffff)) {
  313. dev_err(host->parent,
  314. "device tree specified broken value for DSR: 0x%x, ignoring\n",
  315. host->dsr);
  316. host->dsr_req = 0;
  317. }
  318. device_property_read_u32(dev, "post-power-on-delay-ms",
  319. &host->ios.power_delay_ms);
  320. return mmc_pwrseq_alloc(host);
  321. }
  322. EXPORT_SYMBOL(mmc_of_parse);
  323. /**
  324. * mmc_of_parse_voltage - return mask of supported voltages
  325. * @np: The device node need to be parsed.
  326. * @mask: mask of voltages available for MMC/SD/SDIO
  327. *
  328. * Parse the "voltage-ranges" DT property, returning zero if it is not
  329. * found, negative errno if the voltage-range specification is invalid,
  330. * or one if the voltage-range is specified and successfully parsed.
  331. */
  332. int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
  333. {
  334. const u32 *voltage_ranges;
  335. int num_ranges, i;
  336. voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
  337. if (!voltage_ranges) {
  338. pr_debug("%pOF: voltage-ranges unspecified\n", np);
  339. return 0;
  340. }
  341. num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
  342. if (!num_ranges) {
  343. pr_err("%pOF: voltage-ranges empty\n", np);
  344. return -EINVAL;
  345. }
  346. for (i = 0; i < num_ranges; i++) {
  347. const int j = i * 2;
  348. u32 ocr_mask;
  349. ocr_mask = mmc_vddrange_to_ocrmask(
  350. be32_to_cpu(voltage_ranges[j]),
  351. be32_to_cpu(voltage_ranges[j + 1]));
  352. if (!ocr_mask) {
  353. pr_err("%pOF: voltage-range #%d is invalid\n",
  354. np, i);
  355. return -EINVAL;
  356. }
  357. *mask |= ocr_mask;
  358. }
  359. return 1;
  360. }
  361. EXPORT_SYMBOL(mmc_of_parse_voltage);
  362. /**
  363. * mmc_first_nonreserved_index() - get the first index that is not reserved
  364. */
  365. static int mmc_first_nonreserved_index(void)
  366. {
  367. int max;
  368. max = of_alias_get_highest_id("mmc");
  369. if (max < 0)
  370. return 0;
  371. return max + 1;
  372. }
  373. /**
  374. * mmc_alloc_host - initialise the per-host structure.
  375. * @extra: sizeof private data structure
  376. * @dev: pointer to host device model structure
  377. *
  378. * Initialise the per-host structure.
  379. */
  380. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  381. {
  382. int index;
  383. struct mmc_host *host;
  384. int alias_id, min_idx, max_idx;
  385. host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  386. if (!host)
  387. return NULL;
  388. /* scanning will be enabled when we're ready */
  389. host->rescan_disable = 1;
  390. alias_id = of_alias_get_id(dev->of_node, "mmc");
  391. if (alias_id >= 0) {
  392. index = alias_id;
  393. } else {
  394. min_idx = mmc_first_nonreserved_index();
  395. max_idx = 0;
  396. index = ida_simple_get(&mmc_host_ida, min_idx, max_idx, GFP_KERNEL);
  397. if (index < 0) {
  398. kfree(host);
  399. return NULL;
  400. }
  401. }
  402. host->index = index;
  403. dev_set_name(&host->class_dev, "mmc%d", host->index);
  404. host->ws = wakeup_source_register(NULL, dev_name(&host->class_dev));
  405. host->parent = dev;
  406. host->class_dev.parent = dev;
  407. host->class_dev.class = &mmc_host_class;
  408. device_initialize(&host->class_dev);
  409. device_enable_async_suspend(&host->class_dev);
  410. if (mmc_gpio_alloc(host)) {
  411. put_device(&host->class_dev);
  412. return NULL;
  413. }
  414. spin_lock_init(&host->lock);
  415. init_waitqueue_head(&host->wq);
  416. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  417. INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
  418. timer_setup(&host->retune_timer, mmc_retune_timer, 0);
  419. /*
  420. * By default, hosts do not support SGIO or large requests.
  421. * They have to set these according to their abilities.
  422. */
  423. host->max_segs = 1;
  424. host->max_seg_size = PAGE_SIZE;
  425. host->max_req_size = PAGE_SIZE;
  426. host->max_blk_size = 512;
  427. host->max_blk_count = PAGE_SIZE / 512;
  428. host->fixed_drv_type = -EINVAL;
  429. host->ios.power_delay_ms = 10;
  430. host->ios.power_mode = MMC_POWER_UNDEFINED;
  431. return host;
  432. }
  433. EXPORT_SYMBOL(mmc_alloc_host);
  434. static int mmc_validate_host_caps(struct mmc_host *host)
  435. {
  436. if (host->caps & MMC_CAP_SDIO_IRQ && !host->ops->enable_sdio_irq) {
  437. dev_warn(host->parent, "missing ->enable_sdio_irq() ops\n");
  438. return -EINVAL;
  439. }
  440. return 0;
  441. }
  442. /**
  443. * mmc_add_host - initialise host hardware
  444. * @host: mmc host
  445. *
  446. * Register the host with the driver model. The host must be
  447. * prepared to start servicing requests before this function
  448. * completes.
  449. */
  450. int mmc_add_host(struct mmc_host *host)
  451. {
  452. int err;
  453. err = mmc_validate_host_caps(host);
  454. if (err)
  455. return err;
  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_start_host(host);
  464. return 0;
  465. }
  466. EXPORT_SYMBOL(mmc_add_host);
  467. /**
  468. * mmc_remove_host - remove host hardware
  469. * @host: mmc host
  470. *
  471. * Unregister and remove all cards associated with this host,
  472. * and power down the MMC bus. No new requests will be issued
  473. * after this function has returned.
  474. */
  475. void mmc_remove_host(struct mmc_host *host)
  476. {
  477. mmc_stop_host(host);
  478. #ifdef CONFIG_DEBUG_FS
  479. mmc_remove_host_debugfs(host);
  480. #endif
  481. device_del(&host->class_dev);
  482. led_trigger_unregister_simple(host->led);
  483. }
  484. EXPORT_SYMBOL(mmc_remove_host);
  485. /**
  486. * mmc_free_host - free the host structure
  487. * @host: mmc host
  488. *
  489. * Free the host once all references to it have been dropped.
  490. */
  491. void mmc_free_host(struct mmc_host *host)
  492. {
  493. mmc_pwrseq_free(host);
  494. put_device(&host->class_dev);
  495. }
  496. EXPORT_SYMBOL(mmc_free_host);