host.c 12.4 KB
Newer Older
1 2 3 4
/*
 *  linux/drivers/mmc/core/host.c
 *
 *  Copyright (C) 2003 Russell King, All Rights Reserved.
P
Pierre Ossman 已提交
5
 *  Copyright (C) 2007-2008 Pierre Ossman
6
 *  Copyright (C) 2010 Linus Walleij
7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  MMC host class device management
 */

#include <linux/device.h>
#include <linux/err.h>
#include <linux/idr.h>
18 19
#include <linux/of.h>
#include <linux/of_gpio.h>
20
#include <linux/pagemap.h>
21
#include <linux/export.h>
P
Pierre Ossman 已提交
22
#include <linux/leds.h>
23
#include <linux/slab.h>
24 25

#include <linux/mmc/host.h>
26
#include <linux/mmc/card.h>
27
#include <linux/mmc/slot-gpio.h>
28 29 30

#include "core.h"
#include "host.h"
31
#include "slot-gpio.h"
32
#include "pwrseq.h"
33
#include "sdio_ops.h"
34 35 36

#define cls_dev_to_mmc_host(d)	container_of(d, struct mmc_host, class_dev)

37
static DEFINE_IDA(mmc_host_ida);
38

39 40 41
static void mmc_host_classdev_release(struct device *dev)
{
	struct mmc_host *host = cls_dev_to_mmc_host(dev);
42
	ida_simple_remove(&mmc_host_ida, host->index);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	kfree(host);
}

static struct class mmc_host_class = {
	.name		= "mmc_host",
	.dev_release	= mmc_host_classdev_release,
};

int mmc_register_host_class(void)
{
	return class_register(&mmc_host_class);
}

void mmc_unregister_host_class(void)
{
	class_unregister(&mmc_host_class);
}

61 62 63 64 65 66 67 68
void mmc_retune_enable(struct mmc_host *host)
{
	host->can_retune = 1;
	if (host->retune_period)
		mod_timer(&host->retune_timer,
			  jiffies + host->retune_period * HZ);
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/*
 * Pause re-tuning for a small set of operations.  The pause begins after the
 * next command and after first doing re-tuning.
 */
void mmc_retune_pause(struct mmc_host *host)
{
	if (!host->retune_paused) {
		host->retune_paused = 1;
		mmc_retune_needed(host);
		mmc_retune_hold(host);
	}
}
EXPORT_SYMBOL(mmc_retune_pause);

void mmc_retune_unpause(struct mmc_host *host)
{
	if (host->retune_paused) {
		host->retune_paused = 0;
		mmc_retune_release(host);
	}
}
EXPORT_SYMBOL(mmc_retune_unpause);

92 93
void mmc_retune_disable(struct mmc_host *host)
{
94
	mmc_retune_unpause(host);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	host->can_retune = 0;
	del_timer_sync(&host->retune_timer);
	host->retune_now = 0;
	host->need_retune = 0;
}

void mmc_retune_timer_stop(struct mmc_host *host)
{
	del_timer_sync(&host->retune_timer);
}
EXPORT_SYMBOL(mmc_retune_timer_stop);

void mmc_retune_hold(struct mmc_host *host)
{
	if (!host->hold_retune)
		host->retune_now = 1;
	host->hold_retune += 1;
}

void mmc_retune_release(struct mmc_host *host)
{
	if (host->hold_retune)
		host->hold_retune -= 1;
	else
		WARN_ON(1);
}
121
EXPORT_SYMBOL(mmc_retune_release);
122 123 124

int mmc_retune(struct mmc_host *host)
{
125
	bool return_to_hs400 = false;
126 127 128 129 130 131 132 133 134 135 136 137 138 139
	int err;

	if (host->retune_now)
		host->retune_now = 0;
	else
		return 0;

	if (!host->need_retune || host->doing_retune || !host->card)
		return 0;

	host->need_retune = 0;

	host->doing_retune = 1;

140 141 142 143 144 145 146 147 148 149 150
	if (host->ios.timing == MMC_TIMING_MMC_HS400) {
		err = mmc_hs400_to_hs200(host->card);
		if (err)
			goto out;

		return_to_hs400 = true;

		if (host->ops->prepare_hs400_tuning)
			host->ops->prepare_hs400_tuning(host, &host->ios);
	}

151
	err = mmc_execute_tuning(host->card);
152 153
	if (err)
		goto out;
154

155 156 157
	if (return_to_hs400)
		err = mmc_hs200_to_hs400(host->card);
out:
158 159 160 161 162
	host->doing_retune = 0;

	return err;
}

163
static void mmc_retune_timer(struct timer_list *t)
164
{
165
	struct mmc_host *host = from_timer(host, t, retune_timer);
166 167 168 169

	mmc_retune_needed(host);
}

170 171 172 173 174 175 176 177 178
/**
 *	mmc_of_parse() - parse host's device-tree node
 *	@host: host whose node should be parsed.
 *
 * To keep the rest of the MMC subsystem unaware of whether DT has been
 * used to to instantiate and configure this host instance or not, we
 * parse the properties and set respective generic mmc-host flags and
 * parameters.
 */
179
int mmc_of_parse(struct mmc_host *host)
180
{
181
	struct device *dev = host->parent;
182
	u32 bus_width, drv_type;
183
	int ret;
184 185
	bool cd_cap_invert, cd_gpio_invert = false;
	bool ro_cap_invert, ro_gpio_invert = false;
186

187
	if (!dev || !dev_fwnode(dev))
188
		return 0;
189 190

	/* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
191
	if (device_property_read_u32(dev, "bus-width", &bus_width) < 0) {
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
		dev_dbg(host->parent,
			"\"bus-width\" property is missing, assuming 1 bit.\n");
		bus_width = 1;
	}

	switch (bus_width) {
	case 8:
		host->caps |= MMC_CAP_8_BIT_DATA;
		/* Hosts capable of 8-bit transfers can also do 4 bits */
	case 4:
		host->caps |= MMC_CAP_4_BIT_DATA;
		break;
	case 1:
		break;
	default:
		dev_err(host->parent,
208
			"Invalid \"bus-width\" value %u!\n", bus_width);
209
		return -EINVAL;
210 211 212
	}

	/* f_max is obtained from the optional "max-frequency" property */
213
	device_property_read_u32(dev, "max-frequency", &host->f_max);
214 215 216 217 218 219 220 221 222 223 224 225 226 227

	/*
	 * Configure CD and WP pins. They are both by default active low to
	 * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
	 * mmc-gpio helpers are used to attach, configure and use them. If
	 * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
	 * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
	 * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
	 * is set. If the "non-removable" property is found, the
	 * MMC_CAP_NONREMOVABLE capability is set and no card-detection
	 * configuration is performed.
	 */

	/* Parse Card Detection */
228
	if (device_property_read_bool(dev, "non-removable")) {
229 230
		host->caps |= MMC_CAP_NONREMOVABLE;
	} else {
231
		cd_cap_invert = device_property_read_bool(dev, "cd-inverted");
232

233
		if (device_property_read_bool(dev, "broken-cd"))
234 235
			host->caps |= MMC_CAP_NEEDS_POLL;

236
		ret = mmc_gpiod_request_cd(host, "cd", 0, true,
237
					   0, &cd_gpio_invert);
238
		if (!ret)
239
			dev_info(host->parent, "Got CD GPIO\n");
240
		else if (ret != -ENOENT && ret != -ENOSYS)
241
			return ret;
242 243 244 245 246 247 248 249 250 251 252 253

		/*
		 * There are two ways to flag that the CD line is inverted:
		 * through the cd-inverted flag and by the GPIO line itself
		 * being inverted from the GPIO subsystem. This is a leftover
		 * from the times when the GPIO subsystem did not make it
		 * possible to flag a line as inverted.
		 *
		 * If the capability on the host AND the GPIO line are
		 * both inverted, the end result is that the CD line is
		 * not inverted.
		 */
254
		if (cd_cap_invert ^ cd_gpio_invert)
255
			host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
256 257 258
	}

	/* Parse Write Protection */
259
	ro_cap_invert = device_property_read_bool(dev, "wp-inverted");
260

261
	ret = mmc_gpiod_request_ro(host, "wp", 0, false, 0, &ro_gpio_invert);
262
	if (!ret)
263
		dev_info(host->parent, "Got WP GPIO\n");
264
	else if (ret != -ENOENT && ret != -ENOSYS)
265
		return ret;
266

267
	if (device_property_read_bool(dev, "disable-wp"))
268 269
		host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;

270
	/* See the comment on CD inversion above */
271
	if (ro_cap_invert ^ ro_gpio_invert)
272 273
		host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;

274
	if (device_property_read_bool(dev, "cap-sd-highspeed"))
275
		host->caps |= MMC_CAP_SD_HIGHSPEED;
276
	if (device_property_read_bool(dev, "cap-mmc-highspeed"))
277
		host->caps |= MMC_CAP_MMC_HIGHSPEED;
278
	if (device_property_read_bool(dev, "sd-uhs-sdr12"))
279
		host->caps |= MMC_CAP_UHS_SDR12;
280
	if (device_property_read_bool(dev, "sd-uhs-sdr25"))
281
		host->caps |= MMC_CAP_UHS_SDR25;
282
	if (device_property_read_bool(dev, "sd-uhs-sdr50"))
283
		host->caps |= MMC_CAP_UHS_SDR50;
284
	if (device_property_read_bool(dev, "sd-uhs-sdr104"))
285
		host->caps |= MMC_CAP_UHS_SDR104;
286
	if (device_property_read_bool(dev, "sd-uhs-ddr50"))
287
		host->caps |= MMC_CAP_UHS_DDR50;
288
	if (device_property_read_bool(dev, "cap-power-off-card"))
289
		host->caps |= MMC_CAP_POWER_OFF_CARD;
290
	if (device_property_read_bool(dev, "cap-mmc-hw-reset"))
291
		host->caps |= MMC_CAP_HW_RESET;
292
	if (device_property_read_bool(dev, "cap-sdio-irq"))
293
		host->caps |= MMC_CAP_SDIO_IRQ;
294
	if (device_property_read_bool(dev, "full-pwr-cycle"))
295
		host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
296
	if (device_property_read_bool(dev, "keep-power-in-suspend"))
297
		host->pm_caps |= MMC_PM_KEEP_POWER;
298 299
	if (device_property_read_bool(dev, "wakeup-source") ||
	    device_property_read_bool(dev, "enable-sdio-wakeup")) /* legacy */
300
		host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
301
	if (device_property_read_bool(dev, "mmc-ddr-3_3v"))
302
		host->caps |= MMC_CAP_3_3V_DDR;
303
	if (device_property_read_bool(dev, "mmc-ddr-1_8v"))
304
		host->caps |= MMC_CAP_1_8V_DDR;
305
	if (device_property_read_bool(dev, "mmc-ddr-1_2v"))
306
		host->caps |= MMC_CAP_1_2V_DDR;
307
	if (device_property_read_bool(dev, "mmc-hs200-1_8v"))
308
		host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
309
	if (device_property_read_bool(dev, "mmc-hs200-1_2v"))
310
		host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
311
	if (device_property_read_bool(dev, "mmc-hs400-1_8v"))
312
		host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
313
	if (device_property_read_bool(dev, "mmc-hs400-1_2v"))
314
		host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
315
	if (device_property_read_bool(dev, "mmc-hs400-enhanced-strobe"))
316
		host->caps2 |= MMC_CAP2_HS400_ES;
317
	if (device_property_read_bool(dev, "no-sdio"))
318
		host->caps2 |= MMC_CAP2_NO_SDIO;
319
	if (device_property_read_bool(dev, "no-sd"))
320
		host->caps2 |= MMC_CAP2_NO_SD;
321
	if (device_property_read_bool(dev, "no-mmc"))
322
		host->caps2 |= MMC_CAP2_NO_MMC;
323

324 325 326 327 328 329 330 331 332
	/* Must be after "non-removable" check */
	if (device_property_read_u32(dev, "fixed-emmc-driver-type", &drv_type) == 0) {
		if (host->caps & MMC_CAP_NONREMOVABLE)
			host->fixed_drv_type = drv_type;
		else
			dev_err(host->parent,
				"can't use fixed driver type, media is removable\n");
	}

333
	host->dsr_req = !device_property_read_u32(dev, "dsr", &host->dsr);
334 335 336 337 338 339 340
	if (host->dsr_req && (host->dsr & ~0xffff)) {
		dev_err(host->parent,
			"device tree specified broken value for DSR: 0x%x, ignoring\n",
			host->dsr);
		host->dsr_req = 0;
	}

341
	return mmc_pwrseq_alloc(host);
342 343 344 345
}

EXPORT_SYMBOL(mmc_of_parse);

346 347 348 349 350 351 352 353 354
/**
 *	mmc_alloc_host - initialise the per-host structure.
 *	@extra: sizeof private data structure
 *	@dev: pointer to host device model structure
 *
 *	Initialise the per-host structure.
 */
struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
{
P
Pierre Ossman 已提交
355
	int err;
356 357
	struct mmc_host *host;

358
	host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
359 360 361
	if (!host)
		return NULL;

362 363
	/* scanning will be enabled when we're ready */
	host->rescan_disable = 1;
364

365 366
	err = ida_simple_get(&mmc_host_ida, 0, 0, GFP_KERNEL);
	if (err < 0) {
367 368 369 370
		kfree(host);
		return NULL;
	}

371
	host->index = err;
P
Pierre Ossman 已提交
372

373
	dev_set_name(&host->class_dev, "mmc%d", host->index);
P
Pierre Ossman 已提交
374

375 376 377 378
	host->parent = dev;
	host->class_dev.parent = dev;
	host->class_dev.class = &mmc_host_class;
	device_initialize(&host->class_dev);
379
	device_enable_async_suspend(&host->class_dev);
380

381 382
	if (mmc_gpio_alloc(host)) {
		put_device(&host->class_dev);
383 384
		ida_simple_remove(&mmc_host_ida, host->index);
		kfree(host);
385 386
		return NULL;
	}
387

388 389 390
	spin_lock_init(&host->lock);
	init_waitqueue_head(&host->wq);
	INIT_DELAYED_WORK(&host->detect, mmc_rescan);
391
	INIT_DELAYED_WORK(&host->sdio_irq_work, sdio_irq_work);
392
	timer_setup(&host->retune_timer, mmc_retune_timer, 0);
393 394 395 396 397

	/*
	 * By default, hosts do not support SGIO or large requests.
	 * They have to set these according to their abilities.
	 */
398
	host->max_segs = 1;
399
	host->max_seg_size = PAGE_SIZE;
400

401
	host->max_req_size = PAGE_SIZE;
402
	host->max_blk_size = 512;
403
	host->max_blk_count = PAGE_SIZE / 512;
404

405 406
	host->fixed_drv_type = -EINVAL;

407 408 409 410 411 412 413 414
	return host;
}

EXPORT_SYMBOL(mmc_alloc_host);

/**
 *	mmc_add_host - initialise host hardware
 *	@host: mmc host
P
Pierre Ossman 已提交
415 416 417 418
 *
 *	Register the host with the driver model. The host must be
 *	prepared to start servicing requests before this function
 *	completes.
419 420 421 422 423
 */
int mmc_add_host(struct mmc_host *host)
{
	int err;

424 425 426
	WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
		!host->ops->enable_sdio_irq);

427 428 429 430
	err = device_add(&host->class_dev);
	if (err)
		return err;

431 432
	led_trigger_register_simple(dev_name(&host->class_dev), &host->led);

433 434 435 436
#ifdef CONFIG_DEBUG_FS
	mmc_add_host_debugfs(host);
#endif

437
	mmc_start_host(host);
438
	mmc_register_pm_notifier(host);
439 440 441 442 443 444 445 446 447 448 449

	return 0;
}

EXPORT_SYMBOL(mmc_add_host);

/**
 *	mmc_remove_host - remove host hardware
 *	@host: mmc host
 *
 *	Unregister and remove all cards associated with this host,
P
Pierre Ossman 已提交
450 451
 *	and power down the MMC bus. No new requests will be issued
 *	after this function has returned.
452 453 454
 */
void mmc_remove_host(struct mmc_host *host)
{
455
	mmc_unregister_pm_notifier(host);
456 457
	mmc_stop_host(host);

458 459 460 461
#ifdef CONFIG_DEBUG_FS
	mmc_remove_host_debugfs(host);
#endif

462 463
	device_del(&host->class_dev);

464
	led_trigger_unregister_simple(host->led);
465 466 467 468 469 470 471 472 473 474 475 476
}

EXPORT_SYMBOL(mmc_remove_host);

/**
 *	mmc_free_host - free the host structure
 *	@host: mmc host
 *
 *	Free the host once all references to it have been dropped.
 */
void mmc_free_host(struct mmc_host *host)
{
477
	mmc_pwrseq_free(host);
478 479 480 481
	put_device(&host->class_dev);
}

EXPORT_SYMBOL(mmc_free_host);