host.c 18 KB

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