PageRenderTime 46ms CodeModel.GetById 17ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/mmc/core/sdio.c

https://bitbucket.org/ndreys/linux-sunxi
C | 1000 lines | 626 code | 176 blank | 198 comment | 179 complexity | 5aae0dca7fbb4f5dbcab6dab387d473d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
   1/*
   2 *  linux/drivers/mmc/sdio.c
   3 *
   4 *  Copyright 2006-2007 Pierre Ossman
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License as published by
   8 * the Free Software Foundation; either version 2 of the License, or (at
   9 * your option) any later version.
  10 */
  11
  12#include <linux/err.h>
  13#include <linux/pm_runtime.h>
  14
  15#include <linux/mmc/host.h>
  16#include <linux/mmc/card.h>
  17#include <linux/mmc/sdio.h>
  18#include <linux/mmc/sdio_func.h>
  19#include <linux/mmc/sdio_ids.h>
  20
  21#include "core.h"
  22#include "bus.h"
  23#include "sd.h"
  24#include "sdio_bus.h"
  25#include "mmc_ops.h"
  26#include "sd_ops.h"
  27#include "sdio_ops.h"
  28#include "sdio_cis.h"
  29
  30#ifdef CONFIG_MMC_EMBEDDED_SDIO
  31#include <linux/mmc/sdio_ids.h>
  32#endif
  33
  34static int sdio_read_fbr(struct sdio_func *func)
  35{
  36	int ret;
  37	unsigned char data;
  38
  39	if (mmc_card_nonstd_func_interface(func->card)) {
  40		func->class = SDIO_CLASS_NONE;
  41		return 0;
  42	}
  43
  44	ret = mmc_io_rw_direct(func->card, 0, 0,
  45		SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
  46	if (ret)
  47		goto out;
  48
  49	data &= 0x0f;
  50
  51	if (data == 0x0f) {
  52		ret = mmc_io_rw_direct(func->card, 0, 0,
  53			SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
  54		if (ret)
  55			goto out;
  56	}
  57
  58	func->class = data;
  59
  60out:
  61	return ret;
  62}
  63
  64static int sdio_init_func(struct mmc_card *card, unsigned int fn)
  65{
  66	int ret;
  67	struct sdio_func *func;
  68
  69	BUG_ON(fn > SDIO_MAX_FUNCS);
  70
  71	func = sdio_alloc_func(card);
  72	if (IS_ERR(func))
  73		return PTR_ERR(func);
  74
  75	func->num = fn;
  76
  77	if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
  78		ret = sdio_read_fbr(func);
  79		if (ret)
  80			goto fail;
  81
  82		ret = sdio_read_func_cis(func);
  83		if (ret)
  84			goto fail;
  85	} else {
  86		func->vendor = func->card->cis.vendor;
  87		func->device = func->card->cis.device;
  88		func->max_blksize = func->card->cis.blksize;
  89	}
  90
  91	card->sdio_func[fn - 1] = func;
  92
  93	return 0;
  94
  95fail:
  96	/*
  97	 * It is okay to remove the function here even though we hold
  98	 * the host lock as we haven't registered the device yet.
  99	 */
 100	sdio_remove_func(func);
 101	return ret;
 102}
 103
 104static int sdio_read_cccr(struct mmc_card *card)
 105{
 106	int ret;
 107	int cccr_vsn;
 108	unsigned char data;
 109
 110	memset(&card->cccr, 0, sizeof(struct sdio_cccr));
 111
 112	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
 113	if (ret)
 114		goto out;
 115
 116	cccr_vsn = data & 0x0f;
 117
 118	if (cccr_vsn > SDIO_CCCR_REV_1_20) {
 119		printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
 120			mmc_hostname(card->host), cccr_vsn);
 121		return -EINVAL;
 122	}
 123
 124	card->cccr.sdio_vsn = (data & 0xf0) >> 4;
 125
 126	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
 127	if (ret)
 128		goto out;
 129
 130	if (data & SDIO_CCCR_CAP_SMB)
 131		card->cccr.multi_block = 1;
 132	if (data & SDIO_CCCR_CAP_LSC)
 133		card->cccr.low_speed = 1;
 134	if (data & SDIO_CCCR_CAP_4BLS)
 135		card->cccr.wide_bus = 1;
 136
 137	if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
 138		ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
 139		if (ret)
 140			goto out;
 141
 142		if (data & SDIO_POWER_SMPC)
 143			card->cccr.high_power = 1;
 144	}
 145
 146	if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
 147		ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
 148		if (ret)
 149			goto out;
 150
 151		if (data & SDIO_SPEED_SHS)
 152			card->cccr.high_speed = 1;
 153	}
 154
 155out:
 156	return ret;
 157}
 158
 159static int sdio_enable_wide(struct mmc_card *card)
 160{
 161	int ret;
 162	u8 ctrl;
 163
 164	if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
 165		return 0;
 166
 167	if (card->cccr.low_speed && !card->cccr.wide_bus)
 168		return 0;
 169
 170	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
 171	if (ret)
 172		return ret;
 173
 174	ctrl |= SDIO_BUS_WIDTH_4BIT;
 175
 176	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
 177	if (ret)
 178		return ret;
 179
 180	return 1;
 181}
 182
 183/*
 184 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
 185 * of the card. This may be required on certain setups of boards,
 186 * controllers and embedded sdio device which do not need the card's
 187 * pull-up. As a result, card detection is disabled and power is saved.
 188 */
 189static int sdio_disable_cd(struct mmc_card *card)
 190{
 191	int ret;
 192	u8 ctrl;
 193
 194	if (!mmc_card_disable_cd(card))
 195		return 0;
 196
 197	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
 198	if (ret)
 199		return ret;
 200
 201	ctrl |= SDIO_BUS_CD_DISABLE;
 202
 203	return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
 204}
 205
 206/*
 207 * Devices that remain active during a system suspend are
 208 * put back into 1-bit mode.
 209 */
 210static int sdio_disable_wide(struct mmc_card *card)
 211{
 212	int ret;
 213	u8 ctrl;
 214
 215	if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
 216		return 0;
 217
 218	if (card->cccr.low_speed && !card->cccr.wide_bus)
 219		return 0;
 220
 221	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
 222	if (ret)
 223		return ret;
 224
 225	if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
 226		return 0;
 227
 228	ctrl &= ~SDIO_BUS_WIDTH_4BIT;
 229	ctrl |= SDIO_BUS_ASYNC_INT;
 230
 231	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
 232	if (ret)
 233		return ret;
 234
 235	mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
 236
 237	return 0;
 238}
 239
 240
 241static int sdio_enable_4bit_bus(struct mmc_card *card)
 242{
 243	int err;
 244
 245	if (card->type == MMC_TYPE_SDIO)
 246		return sdio_enable_wide(card);
 247
 248	if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
 249		(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
 250		err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
 251		if (err)
 252			return err;
 253	} else
 254		return 0;
 255
 256	err = sdio_enable_wide(card);
 257	if (err <= 0)
 258		mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
 259
 260	return err;
 261}
 262
 263
 264/*
 265 * Test if the card supports high-speed mode and, if so, switch to it.
 266 */
 267static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
 268{
 269	int ret;
 270	u8 speed;
 271
 272	if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
 273		return 0;
 274
 275	if (!card->cccr.high_speed)
 276		return 0;
 277
 278	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
 279	if (ret)
 280		return ret;
 281
 282	if (enable)
 283		speed |= SDIO_SPEED_EHS;
 284	else
 285		speed &= ~SDIO_SPEED_EHS;
 286
 287	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
 288	if (ret)
 289		return ret;
 290
 291	return 1;
 292}
 293
 294/*
 295 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
 296 */
 297static int sdio_enable_hs(struct mmc_card *card)
 298{
 299	int ret;
 300
 301	ret = mmc_sdio_switch_hs(card, true);
 302	if (ret <= 0 || card->type == MMC_TYPE_SDIO)
 303		return ret;
 304
 305	ret = mmc_sd_switch_hs(card);
 306	if (ret <= 0)
 307		mmc_sdio_switch_hs(card, false);
 308
 309	return ret;
 310}
 311
 312static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
 313{
 314	unsigned max_dtr;
 315
 316	if (mmc_card_highspeed(card)) {
 317		/*
 318		 * The SDIO specification doesn't mention how
 319		 * the CIS transfer speed register relates to
 320		 * high-speed, but it seems that 50 MHz is
 321		 * mandatory.
 322		 */
 323		max_dtr = 50000000;
 324	} else {
 325		max_dtr = card->cis.max_dtr;
 326	}
 327
 328	if (card->type == MMC_TYPE_SD_COMBO)
 329		max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
 330
 331	return max_dtr;
 332}
 333
 334/*
 335 * Handle the detection and initialisation of a card.
 336 *
 337 * In the case of a resume, "oldcard" will contain the card
 338 * we're trying to reinitialise.
 339 */
 340static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
 341			      struct mmc_card *oldcard, int powered_resume)
 342{
 343	struct mmc_card *card;
 344	int err;
 345
 346	BUG_ON(!host);
 347	WARN_ON(!host->claimed);
 348
 349	/*
 350	 * Inform the card of the voltage
 351	 */
 352	if (!powered_resume) {
 353		err = mmc_send_io_op_cond(host, host->ocr, &ocr);
 354		if (err)
 355			goto err;
 356	}
 357
 358	/*
 359	 * For SPI, enable CRC as appropriate.
 360	 */
 361	if (mmc_host_is_spi(host)) {
 362		err = mmc_spi_set_crc(host, use_spi_crc);
 363		if (err)
 364			goto err;
 365	}
 366
 367	/*
 368	 * Allocate card structure.
 369	 */
 370	card = mmc_alloc_card(host, NULL);
 371	if (IS_ERR(card)) {
 372		err = PTR_ERR(card);
 373		goto err;
 374	}
 375
 376	if ((ocr & R4_MEMORY_PRESENT) &&
 377	    mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
 378		card->type = MMC_TYPE_SD_COMBO;
 379
 380		if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
 381		    memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
 382			mmc_remove_card(card);
 383			return -ENOENT;
 384		}
 385	} else {
 386		card->type = MMC_TYPE_SDIO;
 387
 388		if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
 389			mmc_remove_card(card);
 390			return -ENOENT;
 391		}
 392	}
 393
 394	/*
 395	 * Call the optional HC's init_card function to handle quirks.
 396	 */
 397	if (host->ops->init_card)
 398		host->ops->init_card(host, card);
 399
 400	/*
 401	 * For native busses:  set card RCA and quit open drain mode.
 402	 */
 403	if (!powered_resume && !mmc_host_is_spi(host)) {
 404		err = mmc_send_relative_addr(host, &card->rca);
 405		if (err)
 406			goto remove;
 407
 408		/*
 409		 * Update oldcard with the new RCA received from the SDIO
 410		 * device -- we're doing this so that it's updated in the
 411		 * "card" struct when oldcard overwrites that later.
 412		 */
 413		if (oldcard)
 414			oldcard->rca = card->rca;
 415
 416		mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
 417	}
 418
 419	/*
 420	 * Read CSD, before selecting the card
 421	 */
 422	if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
 423		err = mmc_sd_get_csd(host, card);
 424		if (err)
 425			return err;
 426
 427		mmc_decode_cid(card);
 428	}
 429
 430	/*
 431	 * Select card, as all following commands rely on that.
 432	 */
 433	if (!powered_resume && !mmc_host_is_spi(host)) {
 434		err = mmc_select_card(card);
 435		if (err)
 436			goto remove;
 437	}
 438
 439	if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
 440		/*
 441		 * This is non-standard SDIO device, meaning it doesn't
 442		 * have any CIA (Common I/O area) registers present.
 443		 * It's host's responsibility to fill cccr and cis
 444		 * structures in init_card().
 445		 */
 446		mmc_set_clock(host, card->cis.max_dtr);
 447
 448		if (card->cccr.high_speed) {
 449			mmc_card_set_highspeed(card);
 450			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
 451		}
 452
 453		goto finish;
 454	}
 455
 456#ifdef CONFIG_MMC_EMBEDDED_SDIO
 457	if (host->embedded_sdio_data.cccr)
 458		memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
 459	else {
 460#endif
 461		/*
 462		 * Read the common registers.
 463		 */
 464		err = sdio_read_cccr(card);
 465		if (err)
 466			goto remove;
 467#ifdef CONFIG_MMC_EMBEDDED_SDIO
 468	}
 469#endif
 470
 471#ifdef CONFIG_MMC_EMBEDDED_SDIO
 472	if (host->embedded_sdio_data.cis)
 473		memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
 474	else {
 475#endif
 476		/*
 477		 * Read the common CIS tuples.
 478		 */
 479		err = sdio_read_common_cis(card);
 480		if (err)
 481			goto remove;
 482#ifdef CONFIG_MMC_EMBEDDED_SDIO
 483	}
 484#endif
 485
 486	if (oldcard) {
 487		int same = (card->cis.vendor == oldcard->cis.vendor &&
 488			    card->cis.device == oldcard->cis.device);
 489		mmc_remove_card(card);
 490		if (!same)
 491			return -ENOENT;
 492
 493		card = oldcard;
 494	}
 495	mmc_fixup_device(card, NULL);
 496
 497	if (card->type == MMC_TYPE_SD_COMBO) {
 498		err = mmc_sd_setup_card(host, card, oldcard != NULL);
 499		/* handle as SDIO-only card if memory init failed */
 500		if (err) {
 501			mmc_go_idle(host);
 502			if (mmc_host_is_spi(host))
 503				/* should not fail, as it worked previously */
 504				mmc_spi_set_crc(host, use_spi_crc);
 505			card->type = MMC_TYPE_SDIO;
 506		} else
 507			card->dev.type = &sd_type;
 508	}
 509
 510	/*
 511	 * If needed, disconnect card detection pull-up resistor.
 512	 */
 513	err = sdio_disable_cd(card);
 514	if (err)
 515		goto remove;
 516
 517	/*
 518	 * Switch to high-speed (if supported).
 519	 */
 520	err = sdio_enable_hs(card);
 521	if (err > 0)
 522		mmc_sd_go_highspeed(card);
 523	else if (err)
 524		goto remove;
 525
 526	/*
 527	 * Change to the card's maximum speed.
 528	 */
 529	mmc_set_clock(host, mmc_sdio_get_max_clock(card));
 530
 531	/*
 532	 * Switch to wider bus (if supported).
 533	 */
 534	err = sdio_enable_4bit_bus(card);
 535	if (err > 0)
 536		mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
 537	else if (err)
 538		goto remove;
 539
 540finish:
 541	if (!oldcard)
 542		host->card = card;
 543	return 0;
 544
 545remove:
 546	if (!oldcard)
 547		mmc_remove_card(card);
 548
 549err:
 550	return err;
 551}
 552
 553/*
 554 * Host is being removed. Free up the current card.
 555 */
 556static void mmc_sdio_remove(struct mmc_host *host)
 557{
 558	int i;
 559
 560	BUG_ON(!host);
 561	BUG_ON(!host->card);
 562
 563	for (i = 0;i < host->card->sdio_funcs;i++) {
 564		if (host->card->sdio_func[i]) {
 565			sdio_remove_func(host->card->sdio_func[i]);
 566			host->card->sdio_func[i] = NULL;
 567		}
 568	}
 569
 570	mmc_remove_card(host->card);
 571	host->card = NULL;
 572}
 573
 574/*
 575 * Card detection callback from host.
 576 */
 577static void mmc_sdio_detect(struct mmc_host *host)
 578{
 579	int err;
 580
 581	BUG_ON(!host);
 582	BUG_ON(!host->card);
 583
 584	/* Make sure card is powered before detecting it */
 585	if (host->caps & MMC_CAP_POWER_OFF_CARD) {
 586		err = pm_runtime_get_sync(&host->card->dev);
 587		if (err < 0)
 588			goto out;
 589	}
 590
 591	mmc_claim_host(host);
 592
 593	/*
 594	 * Just check if our card has been removed.
 595	 */
 596	err = mmc_select_card(host->card);
 597
 598	mmc_release_host(host);
 599
 600	/*
 601	 * Tell PM core it's OK to power off the card now.
 602	 *
 603	 * The _sync variant is used in order to ensure that the card
 604	 * is left powered off in case an error occurred, and the card
 605	 * is going to be removed.
 606	 *
 607	 * Since there is no specific reason to believe a new user
 608	 * is about to show up at this point, the _sync variant is
 609	 * desirable anyway.
 610	 */
 611	if (host->caps & MMC_CAP_POWER_OFF_CARD)
 612		pm_runtime_put_sync(&host->card->dev);
 613
 614out:
 615	if (err) {
 616		mmc_sdio_remove(host);
 617
 618		mmc_claim_host(host);
 619		mmc_detach_bus(host);
 620		mmc_power_off(host);
 621		mmc_release_host(host);
 622	}
 623}
 624
 625/*
 626 * SDIO suspend.  We need to suspend all functions separately.
 627 * Therefore all registered functions must have drivers with suspend
 628 * and resume methods.  Failing that we simply remove the whole card.
 629 */
 630static int mmc_sdio_suspend(struct mmc_host *host)
 631{
 632	int i, err = 0;
 633
 634	for (i = 0; i < host->card->sdio_funcs; i++) {
 635		struct sdio_func *func = host->card->sdio_func[i];
 636		if (func && sdio_func_present(func) && func->dev.driver) {
 637			const struct dev_pm_ops *pmops = func->dev.driver->pm;
 638			if (!pmops || !pmops->suspend || !pmops->resume) {
 639				/* force removal of entire card in that case */
 640				err = -ENOSYS;
 641			} else
 642				err = pmops->suspend(&func->dev);
 643			if (err)
 644				break;
 645		}
 646	}
 647	while (err && --i >= 0) {
 648		struct sdio_func *func = host->card->sdio_func[i];
 649		if (func && sdio_func_present(func) && func->dev.driver) {
 650			const struct dev_pm_ops *pmops = func->dev.driver->pm;
 651			pmops->resume(&func->dev);
 652		}
 653	}
 654
 655	if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
 656		mmc_claim_host(host);
 657		sdio_disable_wide(host->card);
 658		mmc_release_host(host);
 659	}
 660
 661	return err;
 662}
 663
 664static int mmc_sdio_resume(struct mmc_host *host)
 665{
 666	int i, err = 0;
 667
 668	BUG_ON(!host);
 669	BUG_ON(!host->card);
 670
 671	/* Basic card reinitialization. */
 672	mmc_claim_host(host);
 673
 674	/* No need to reinitialize powered-resumed nonremovable cards */
 675	if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
 676		err = mmc_sdio_init_card(host, host->ocr, host->card,
 677					mmc_card_keep_power(host));
 678	else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
 679		/* We may have switched to 1-bit mode during suspend */
 680		err = sdio_enable_4bit_bus(host->card);
 681		if (err > 0) {
 682			mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
 683			err = 0;
 684		}
 685	}
 686
 687	if (!err && host->sdio_irqs)
 688		wake_up_process(host->sdio_irq_thread);
 689	mmc_release_host(host);
 690
 691	/*
 692	 * If the card looked to be the same as before suspending, then
 693	 * we proceed to resume all card functions.  If one of them returns
 694	 * an error then we simply return that error to the core and the
 695	 * card will be redetected as new.  It is the responsibility of
 696	 * the function driver to perform further tests with the extra
 697	 * knowledge it has of the card to confirm the card is indeed the
 698	 * same as before suspending (same MAC address for network cards,
 699	 * etc.) and return an error otherwise.
 700	 */
 701	for (i = 0; !err && i < host->card->sdio_funcs; i++) {
 702		struct sdio_func *func = host->card->sdio_func[i];
 703		if (func && sdio_func_present(func) && func->dev.driver) {
 704			const struct dev_pm_ops *pmops = func->dev.driver->pm;
 705			err = pmops->resume(&func->dev);
 706		}
 707	}
 708
 709	return err;
 710}
 711
 712static int mmc_sdio_power_restore(struct mmc_host *host)
 713{
 714	int ret;
 715	u32 ocr;
 716
 717	BUG_ON(!host);
 718	BUG_ON(!host->card);
 719
 720	mmc_claim_host(host);
 721
 722	/*
 723	 * Reset the card by performing the same steps that are taken by
 724	 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
 725	 *
 726	 * sdio_reset() is technically not needed. Having just powered up the
 727	 * hardware, it should already be in reset state. However, some
 728	 * platforms (such as SD8686 on OLPC) do not instantly cut power,
 729	 * meaning that a reset is required when restoring power soon after
 730	 * powering off. It is harmless in other cases.
 731	 *
 732	 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
 733	 * is not necessary for non-removable cards. However, it is required
 734	 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
 735	 * harmless in other situations.
 736	 *
 737	 * With these steps taken, mmc_select_voltage() is also required to
 738	 * restore the correct voltage setting of the card.
 739	 */
 740	sdio_reset(host);
 741	mmc_go_idle(host);
 742	mmc_send_if_cond(host, host->ocr_avail);
 743
 744	ret = mmc_send_io_op_cond(host, 0, &ocr);
 745	if (ret)
 746		goto out;
 747
 748	if (host->ocr_avail_sdio)
 749		host->ocr_avail = host->ocr_avail_sdio;
 750
 751	host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
 752	if (!host->ocr) {
 753		ret = -EINVAL;
 754		goto out;
 755	}
 756
 757	ret = mmc_sdio_init_card(host, host->ocr, host->card,
 758				mmc_card_keep_power(host));
 759	if (!ret && host->sdio_irqs)
 760		mmc_signal_sdio_irq(host);
 761
 762out:
 763	mmc_release_host(host);
 764
 765	return ret;
 766}
 767
 768static const struct mmc_bus_ops mmc_sdio_ops = {
 769	.remove = mmc_sdio_remove,
 770	.detect = mmc_sdio_detect,
 771	.suspend = mmc_sdio_suspend,
 772	.resume = mmc_sdio_resume,
 773	.power_restore = mmc_sdio_power_restore,
 774};
 775
 776
 777/*
 778 * Starting point for SDIO card init.
 779 */
 780int mmc_attach_sdio(struct mmc_host *host)
 781{
 782	int err, i, funcs;
 783	u32 ocr;
 784	struct mmc_card *card;
 785
 786	BUG_ON(!host);
 787	WARN_ON(!host->claimed);
 788
 789	err = mmc_send_io_op_cond(host, 0, &ocr);
 790	if (err)
 791		return err;
 792
 793	mmc_attach_bus(host, &mmc_sdio_ops);
 794	if (host->ocr_avail_sdio)
 795		host->ocr_avail = host->ocr_avail_sdio;
 796
 797	/*
 798	 * Sanity check the voltages that the card claims to
 799	 * support.
 800	 */
 801	if (ocr & 0x7F) {
 802		printk(KERN_WARNING "%s: card claims to support voltages "
 803		       "below the defined range. These will be ignored.\n",
 804		       mmc_hostname(host));
 805		ocr &= ~0x7F;
 806	}
 807
 808	host->ocr = mmc_select_voltage(host, ocr);
 809
 810	/*
 811	 * Can we support the voltage(s) of the card(s)?
 812	 */
 813	if (!host->ocr) {
 814		err = -EINVAL;
 815		goto err;
 816	}
 817
 818	/*
 819	 * Detect and init the card.
 820	 */
 821	err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
 822	if (err)
 823		goto err;
 824	card = host->card;
 825
 826	/*
 827	 * Enable runtime PM only if supported by host+card+board
 828	 */
 829	if (host->caps & MMC_CAP_POWER_OFF_CARD) {
 830		/*
 831		 * Let runtime PM core know our card is active
 832		 */
 833		err = pm_runtime_set_active(&card->dev);
 834		if (err)
 835			goto remove;
 836
 837		/*
 838		 * Enable runtime PM for this card
 839		 */
 840		pm_runtime_enable(&card->dev);
 841	}
 842
 843	/*
 844	 * The number of functions on the card is encoded inside
 845	 * the ocr.
 846	 */
 847	funcs = (ocr & 0x70000000) >> 28;
 848	card->sdio_funcs = 0;
 849
 850#ifdef CONFIG_MMC_EMBEDDED_SDIO
 851	if (host->embedded_sdio_data.funcs)
 852		card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
 853#endif
 854
 855	/*
 856	 * Initialize (but don't add) all present functions.
 857	 */
 858	for (i = 0; i < funcs; i++, card->sdio_funcs++) {
 859#ifdef CONFIG_MMC_EMBEDDED_SDIO
 860		if (host->embedded_sdio_data.funcs) {
 861			struct sdio_func *tmp;
 862
 863			tmp = sdio_alloc_func(host->card);
 864			if (IS_ERR(tmp))
 865				goto remove;
 866			tmp->num = (i + 1);
 867			card->sdio_func[i] = tmp;
 868			tmp->class = host->embedded_sdio_data.funcs[i].f_class;
 869			tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
 870			tmp->vendor = card->cis.vendor;
 871			tmp->device = card->cis.device;
 872		} else {
 873#endif
 874			err = sdio_init_func(host->card, i + 1);
 875			if (err)
 876				goto remove;
 877#ifdef CONFIG_MMC_EMBEDDED_SDIO
 878		}
 879#endif
 880		/*
 881		 * Enable Runtime PM for this func (if supported)
 882		 */
 883		if (host->caps & MMC_CAP_POWER_OFF_CARD)
 884			pm_runtime_enable(&card->sdio_func[i]->dev);
 885	}
 886
 887	/*
 888	 * First add the card to the driver model...
 889	 */
 890	mmc_release_host(host);
 891	err = mmc_add_card(host->card);
 892	if (err)
 893		goto remove_added;
 894
 895	/*
 896	 * ...then the SDIO functions.
 897	 */
 898	for (i = 0;i < funcs;i++) {
 899		err = sdio_add_func(host->card->sdio_func[i]);
 900		if (err)
 901			goto remove_added;
 902	}
 903
 904	mmc_claim_host(host);
 905	return 0;
 906
 907
 908remove_added:
 909	/* Remove without lock if the device has been added. */
 910	mmc_sdio_remove(host);
 911	mmc_claim_host(host);
 912remove:
 913	/* And with lock if it hasn't been added. */
 914	mmc_release_host(host);
 915	if (host->card)
 916		mmc_sdio_remove(host);
 917	mmc_claim_host(host);
 918err:
 919	mmc_detach_bus(host);
 920
 921	printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
 922		mmc_hostname(host), err);
 923
 924	return err;
 925}
 926
 927int sdio_reset_comm(struct mmc_card *card)
 928{
 929	struct mmc_host *host = card->host;
 930	u32 ocr;
 931	int err;
 932
 933	printk("%s():\n", __func__);
 934	mmc_claim_host(host);
 935
 936	mmc_go_idle(host);
 937
 938	mmc_set_clock(host, host->f_min);
 939
 940	err = mmc_send_io_op_cond(host, 0, &ocr);
 941	if (err)
 942		goto err;
 943
 944	host->ocr = mmc_select_voltage(host, ocr);
 945	if (!host->ocr) {
 946		err = -EINVAL;
 947		goto err;
 948	}
 949
 950	err = mmc_send_io_op_cond(host, host->ocr, &ocr);
 951	if (err)
 952		goto err;
 953
 954	if (mmc_host_is_spi(host)) {
 955		err = mmc_spi_set_crc(host, use_spi_crc);
 956		if (err)
 957			goto err;
 958	}
 959
 960	if (!mmc_host_is_spi(host)) {
 961		err = mmc_send_relative_addr(host, &card->rca);
 962		if (err)
 963			goto err;
 964		mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
 965	}
 966	if (!mmc_host_is_spi(host)) {
 967		err = mmc_select_card(card);
 968		if (err)
 969			goto err;
 970	}
 971
 972	/*
 973	 * Switch to high-speed (if supported).
 974	 */
 975	err = sdio_enable_hs(card);
 976	if (err > 0)
 977		mmc_sd_go_highspeed(card);
 978	else if (err)
 979		goto err;
 980
 981	/*
 982	 * Change to the card's maximum speed.
 983	 */
 984	mmc_set_clock(host, mmc_sdio_get_max_clock(card));
 985
 986	err = sdio_enable_4bit_bus(card);
 987	if (err > 0)
 988		mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
 989	else if (err)
 990		goto err;
 991
 992	mmc_release_host(host);
 993	return 0;
 994err:
 995	printk("%s: Error resetting SDIO communications (%d)\n",
 996	       mmc_hostname(host), err);
 997	mmc_release_host(host);
 998	return err;
 999}
1000EXPORT_SYMBOL(sdio_reset_comm);