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