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