xref: /linux/drivers/mmc/core/sdio.c (revision 59b6c9e0ad9f2578ff8b2ef8c718bcb8b895fefb)
15c4e6f13SPierre Ossman /*
25c4e6f13SPierre Ossman  *  linux/drivers/mmc/sdio.c
35c4e6f13SPierre Ossman  *
45c4e6f13SPierre Ossman  *  Copyright 2006-2007 Pierre Ossman
55c4e6f13SPierre Ossman  *
65c4e6f13SPierre Ossman  * This program is free software; you can redistribute it and/or modify
75c4e6f13SPierre Ossman  * it under the terms of the GNU General Public License as published by
85c4e6f13SPierre Ossman  * the Free Software Foundation; either version 2 of the License, or (at
95c4e6f13SPierre Ossman  * your option) any later version.
105c4e6f13SPierre Ossman  */
115c4e6f13SPierre Ossman 
125c4e6f13SPierre Ossman #include <linux/err.h>
1381968561SOhad Ben-Cohen #include <linux/pm_runtime.h>
145c4e6f13SPierre Ossman 
155c4e6f13SPierre Ossman #include <linux/mmc/host.h>
165c4e6f13SPierre Ossman #include <linux/mmc/card.h>
17a4924c71SGirish K S #include <linux/mmc/mmc.h>
1835c66c19SPierre Ossman #include <linux/mmc/sdio.h>
19e29a7d73SPierre Ossman #include <linux/mmc/sdio_func.h>
20eab40687SOhad Ben-Cohen #include <linux/mmc/sdio_ids.h>
215c4e6f13SPierre Ossman 
225c4e6f13SPierre Ossman #include "core.h"
235c4e6f13SPierre Ossman #include "bus.h"
2471578a1eSMichal Miroslaw #include "sd.h"
25e29a7d73SPierre Ossman #include "sdio_bus.h"
265c4e6f13SPierre Ossman #include "mmc_ops.h"
275c4e6f13SPierre Ossman #include "sd_ops.h"
285c4e6f13SPierre Ossman #include "sdio_ops.h"
29b7261261SNicolas Pitre #include "sdio_cis.h"
305c4e6f13SPierre Ossman 
310597007fSPierre Ossman static int sdio_read_fbr(struct sdio_func *func)
320597007fSPierre Ossman {
330597007fSPierre Ossman 	int ret;
340597007fSPierre Ossman 	unsigned char data;
350597007fSPierre Ossman 
36eab40687SOhad Ben-Cohen 	if (mmc_card_nonstd_func_interface(func->card)) {
37eab40687SOhad Ben-Cohen 		func->class = SDIO_CLASS_NONE;
38eab40687SOhad Ben-Cohen 		return 0;
39eab40687SOhad Ben-Cohen 	}
40eab40687SOhad Ben-Cohen 
410597007fSPierre Ossman 	ret = mmc_io_rw_direct(func->card, 0, 0,
427616ee95SDavid Vrabel 		SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
430597007fSPierre Ossman 	if (ret)
440597007fSPierre Ossman 		goto out;
450597007fSPierre Ossman 
460597007fSPierre Ossman 	data &= 0x0f;
470597007fSPierre Ossman 
480597007fSPierre Ossman 	if (data == 0x0f) {
490597007fSPierre Ossman 		ret = mmc_io_rw_direct(func->card, 0, 0,
507616ee95SDavid Vrabel 			SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
510597007fSPierre Ossman 		if (ret)
520597007fSPierre Ossman 			goto out;
530597007fSPierre Ossman 	}
540597007fSPierre Ossman 
550597007fSPierre Ossman 	func->class = data;
560597007fSPierre Ossman 
570597007fSPierre Ossman out:
580597007fSPierre Ossman 	return ret;
590597007fSPierre Ossman }
600597007fSPierre Ossman 
61e29a7d73SPierre Ossman static int sdio_init_func(struct mmc_card *card, unsigned int fn)
62e29a7d73SPierre Ossman {
630597007fSPierre Ossman 	int ret;
64e29a7d73SPierre Ossman 	struct sdio_func *func;
65e29a7d73SPierre Ossman 
66e29a7d73SPierre Ossman 	BUG_ON(fn > SDIO_MAX_FUNCS);
67e29a7d73SPierre Ossman 
68e29a7d73SPierre Ossman 	func = sdio_alloc_func(card);
69e29a7d73SPierre Ossman 	if (IS_ERR(func))
70e29a7d73SPierre Ossman 		return PTR_ERR(func);
71e29a7d73SPierre Ossman 
72e29a7d73SPierre Ossman 	func->num = fn;
73e29a7d73SPierre Ossman 
746f51be3dSGrazvydas Ignotas 	if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
750597007fSPierre Ossman 		ret = sdio_read_fbr(func);
760597007fSPierre Ossman 		if (ret)
770597007fSPierre Ossman 			goto fail;
780597007fSPierre Ossman 
791a632f8cSPierre Ossman 		ret = sdio_read_func_cis(func);
80b7261261SNicolas Pitre 		if (ret)
81b7261261SNicolas Pitre 			goto fail;
826f51be3dSGrazvydas Ignotas 	} else {
836f51be3dSGrazvydas Ignotas 		func->vendor = func->card->cis.vendor;
846f51be3dSGrazvydas Ignotas 		func->device = func->card->cis.device;
856f51be3dSGrazvydas Ignotas 		func->max_blksize = func->card->cis.blksize;
866f51be3dSGrazvydas Ignotas 	}
87b7261261SNicolas Pitre 
88e29a7d73SPierre Ossman 	card->sdio_func[fn - 1] = func;
89e29a7d73SPierre Ossman 
90e29a7d73SPierre Ossman 	return 0;
910597007fSPierre Ossman 
920597007fSPierre Ossman fail:
930597007fSPierre Ossman 	/*
940597007fSPierre Ossman 	 * It is okay to remove the function here even though we hold
950597007fSPierre Ossman 	 * the host lock as we haven't registered the device yet.
960597007fSPierre Ossman 	 */
970597007fSPierre Ossman 	sdio_remove_func(func);
980597007fSPierre Ossman 	return ret;
99e29a7d73SPierre Ossman }
100e29a7d73SPierre Ossman 
1012d0d68f5SPhilip Rakity static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
10235c66c19SPierre Ossman {
10335c66c19SPierre Ossman 	int ret;
10435c66c19SPierre Ossman 	int cccr_vsn;
1052d0d68f5SPhilip Rakity 	int uhs = ocr & R4_18V_PRESENT;
10635c66c19SPierre Ossman 	unsigned char data;
107a303c531SPhilip Rakity 	unsigned char speed;
10835c66c19SPierre Ossman 
10935c66c19SPierre Ossman 	memset(&card->cccr, 0, sizeof(struct sdio_cccr));
11035c66c19SPierre Ossman 
11135c66c19SPierre Ossman 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
11235c66c19SPierre Ossman 	if (ret)
11335c66c19SPierre Ossman 		goto out;
11435c66c19SPierre Ossman 
11535c66c19SPierre Ossman 	cccr_vsn = data & 0x0f;
11635c66c19SPierre Ossman 
117b4625dabSBing Zhao 	if (cccr_vsn > SDIO_CCCR_REV_3_00) {
118a3c76eb9SGirish K S 		pr_err("%s: unrecognised CCCR structure version %d\n",
11935c66c19SPierre Ossman 			mmc_hostname(card->host), cccr_vsn);
12035c66c19SPierre Ossman 		return -EINVAL;
12135c66c19SPierre Ossman 	}
12235c66c19SPierre Ossman 
12335c66c19SPierre Ossman 	card->cccr.sdio_vsn = (data & 0xf0) >> 4;
12435c66c19SPierre Ossman 
12535c66c19SPierre Ossman 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
12635c66c19SPierre Ossman 	if (ret)
12735c66c19SPierre Ossman 		goto out;
12835c66c19SPierre Ossman 
12935c66c19SPierre Ossman 	if (data & SDIO_CCCR_CAP_SMB)
13035c66c19SPierre Ossman 		card->cccr.multi_block = 1;
13135c66c19SPierre Ossman 	if (data & SDIO_CCCR_CAP_LSC)
13235c66c19SPierre Ossman 		card->cccr.low_speed = 1;
13335c66c19SPierre Ossman 	if (data & SDIO_CCCR_CAP_4BLS)
13435c66c19SPierre Ossman 		card->cccr.wide_bus = 1;
13535c66c19SPierre Ossman 
13635c66c19SPierre Ossman 	if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
13735c66c19SPierre Ossman 		ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
13835c66c19SPierre Ossman 		if (ret)
13935c66c19SPierre Ossman 			goto out;
14035c66c19SPierre Ossman 
14135c66c19SPierre Ossman 		if (data & SDIO_POWER_SMPC)
14235c66c19SPierre Ossman 			card->cccr.high_power = 1;
14335c66c19SPierre Ossman 	}
14435c66c19SPierre Ossman 
14535c66c19SPierre Ossman 	if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
146a303c531SPhilip Rakity 		ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
14735c66c19SPierre Ossman 		if (ret)
14835c66c19SPierre Ossman 			goto out;
14935c66c19SPierre Ossman 
150a303c531SPhilip Rakity 		card->scr.sda_spec3 = 0;
151a303c531SPhilip Rakity 		card->sw_caps.sd3_bus_mode = 0;
152a303c531SPhilip Rakity 		card->sw_caps.sd3_drv_type = 0;
1532d0d68f5SPhilip Rakity 		if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
154a303c531SPhilip Rakity 			card->scr.sda_spec3 = 1;
155a303c531SPhilip Rakity 			ret = mmc_io_rw_direct(card, 0, 0,
156a303c531SPhilip Rakity 				SDIO_CCCR_UHS, 0, &data);
157a303c531SPhilip Rakity 			if (ret)
158a303c531SPhilip Rakity 				goto out;
159a303c531SPhilip Rakity 
16041875e38SSujit Reddy Thumma 			if (mmc_host_uhs(card->host)) {
161a303c531SPhilip Rakity 				if (data & SDIO_UHS_DDR50)
162a303c531SPhilip Rakity 					card->sw_caps.sd3_bus_mode
163a303c531SPhilip Rakity 						|= SD_MODE_UHS_DDR50;
164a303c531SPhilip Rakity 
165a303c531SPhilip Rakity 				if (data & SDIO_UHS_SDR50)
166a303c531SPhilip Rakity 					card->sw_caps.sd3_bus_mode
167a303c531SPhilip Rakity 						|= SD_MODE_UHS_SDR50;
168a303c531SPhilip Rakity 
169a303c531SPhilip Rakity 				if (data & SDIO_UHS_SDR104)
170a303c531SPhilip Rakity 					card->sw_caps.sd3_bus_mode
171a303c531SPhilip Rakity 						|= SD_MODE_UHS_SDR104;
172a303c531SPhilip Rakity 			}
173a303c531SPhilip Rakity 
174a303c531SPhilip Rakity 			ret = mmc_io_rw_direct(card, 0, 0,
175a303c531SPhilip Rakity 				SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
176a303c531SPhilip Rakity 			if (ret)
177a303c531SPhilip Rakity 				goto out;
178a303c531SPhilip Rakity 
179a303c531SPhilip Rakity 			if (data & SDIO_DRIVE_SDTA)
180a303c531SPhilip Rakity 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
181a303c531SPhilip Rakity 			if (data & SDIO_DRIVE_SDTC)
182a303c531SPhilip Rakity 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
183a303c531SPhilip Rakity 			if (data & SDIO_DRIVE_SDTD)
184a303c531SPhilip Rakity 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
185a303c531SPhilip Rakity 		}
186a303c531SPhilip Rakity 
187a303c531SPhilip Rakity 		/* if no uhs mode ensure we check for high speed */
188a303c531SPhilip Rakity 		if (!card->sw_caps.sd3_bus_mode) {
189a303c531SPhilip Rakity 			if (speed & SDIO_SPEED_SHS) {
19035c66c19SPierre Ossman 				card->cccr.high_speed = 1;
191a303c531SPhilip Rakity 				card->sw_caps.hs_max_dtr = 50000000;
192a303c531SPhilip Rakity 			} else {
193a303c531SPhilip Rakity 				card->cccr.high_speed = 0;
194a303c531SPhilip Rakity 				card->sw_caps.hs_max_dtr = 25000000;
195a303c531SPhilip Rakity 			}
196a303c531SPhilip Rakity 		}
19735c66c19SPierre Ossman 	}
19835c66c19SPierre Ossman 
19935c66c19SPierre Ossman out:
20035c66c19SPierre Ossman 	return ret;
20135c66c19SPierre Ossman }
20235c66c19SPierre Ossman 
2034ff6471cSPierre Ossman static int sdio_enable_wide(struct mmc_card *card)
2044ff6471cSPierre Ossman {
2054ff6471cSPierre Ossman 	int ret;
2064ff6471cSPierre Ossman 	u8 ctrl;
2074ff6471cSPierre Ossman 
2084ff6471cSPierre Ossman 	if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
2094ff6471cSPierre Ossman 		return 0;
2104ff6471cSPierre Ossman 
2114ff6471cSPierre Ossman 	if (card->cccr.low_speed && !card->cccr.wide_bus)
2124ff6471cSPierre Ossman 		return 0;
2134ff6471cSPierre Ossman 
2144ff6471cSPierre Ossman 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
2154ff6471cSPierre Ossman 	if (ret)
2164ff6471cSPierre Ossman 		return ret;
2174ff6471cSPierre Ossman 
2182a0fe914SYong Ding 	if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
2196606110dSJoe Perches 		pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
2202a0fe914SYong Ding 			mmc_hostname(card->host), ctrl);
2212a0fe914SYong Ding 
2222a0fe914SYong Ding 	/* set as 4-bit bus width */
2232a0fe914SYong Ding 	ctrl &= ~SDIO_BUS_WIDTH_MASK;
2244ff6471cSPierre Ossman 	ctrl |= SDIO_BUS_WIDTH_4BIT;
2254ff6471cSPierre Ossman 
2264ff6471cSPierre Ossman 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
2274ff6471cSPierre Ossman 	if (ret)
2284ff6471cSPierre Ossman 		return ret;
2294ff6471cSPierre Ossman 
2307310ece8SMichal Miroslaw 	return 1;
2314ff6471cSPierre Ossman }
2324ff6471cSPierre Ossman 
2335c4e6f13SPierre Ossman /*
234006ebd5dSOhad Ben-Cohen  * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
235006ebd5dSOhad Ben-Cohen  * of the card. This may be required on certain setups of boards,
236006ebd5dSOhad Ben-Cohen  * controllers and embedded sdio device which do not need the card's
237006ebd5dSOhad Ben-Cohen  * pull-up. As a result, card detection is disabled and power is saved.
238006ebd5dSOhad Ben-Cohen  */
239006ebd5dSOhad Ben-Cohen static int sdio_disable_cd(struct mmc_card *card)
240006ebd5dSOhad Ben-Cohen {
241006ebd5dSOhad Ben-Cohen 	int ret;
242006ebd5dSOhad Ben-Cohen 	u8 ctrl;
243006ebd5dSOhad Ben-Cohen 
2442059a02dSOhad Ben-Cohen 	if (!mmc_card_disable_cd(card))
245006ebd5dSOhad Ben-Cohen 		return 0;
246006ebd5dSOhad Ben-Cohen 
247006ebd5dSOhad Ben-Cohen 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
248006ebd5dSOhad Ben-Cohen 	if (ret)
249006ebd5dSOhad Ben-Cohen 		return ret;
250006ebd5dSOhad Ben-Cohen 
251006ebd5dSOhad Ben-Cohen 	ctrl |= SDIO_BUS_CD_DISABLE;
252006ebd5dSOhad Ben-Cohen 
253006ebd5dSOhad Ben-Cohen 	return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
254006ebd5dSOhad Ben-Cohen }
255006ebd5dSOhad Ben-Cohen 
256006ebd5dSOhad Ben-Cohen /*
2576b5eda36SDaniel Drake  * Devices that remain active during a system suspend are
2586b5eda36SDaniel Drake  * put back into 1-bit mode.
2596b5eda36SDaniel Drake  */
2606b5eda36SDaniel Drake static int sdio_disable_wide(struct mmc_card *card)
2616b5eda36SDaniel Drake {
2626b5eda36SDaniel Drake 	int ret;
2636b5eda36SDaniel Drake 	u8 ctrl;
2646b5eda36SDaniel Drake 
2656b5eda36SDaniel Drake 	if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
2666b5eda36SDaniel Drake 		return 0;
2676b5eda36SDaniel Drake 
2686b5eda36SDaniel Drake 	if (card->cccr.low_speed && !card->cccr.wide_bus)
2696b5eda36SDaniel Drake 		return 0;
2706b5eda36SDaniel Drake 
2716b5eda36SDaniel Drake 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
2726b5eda36SDaniel Drake 	if (ret)
2736b5eda36SDaniel Drake 		return ret;
2746b5eda36SDaniel Drake 
2756b5eda36SDaniel Drake 	if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
2766b5eda36SDaniel Drake 		return 0;
2776b5eda36SDaniel Drake 
2786b5eda36SDaniel Drake 	ctrl &= ~SDIO_BUS_WIDTH_4BIT;
2796b5eda36SDaniel Drake 	ctrl |= SDIO_BUS_ASYNC_INT;
2806b5eda36SDaniel Drake 
2816b5eda36SDaniel Drake 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
2826b5eda36SDaniel Drake 	if (ret)
2836b5eda36SDaniel Drake 		return ret;
2846b5eda36SDaniel Drake 
2856b5eda36SDaniel Drake 	mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
2866b5eda36SDaniel Drake 
2876b5eda36SDaniel Drake 	return 0;
2886b5eda36SDaniel Drake }
2896b5eda36SDaniel Drake 
2907310ece8SMichal Miroslaw 
2917310ece8SMichal Miroslaw static int sdio_enable_4bit_bus(struct mmc_card *card)
2927310ece8SMichal Miroslaw {
2937310ece8SMichal Miroslaw 	int err;
2947310ece8SMichal Miroslaw 
2957310ece8SMichal Miroslaw 	if (card->type == MMC_TYPE_SDIO)
2967310ece8SMichal Miroslaw 		return sdio_enable_wide(card);
2977310ece8SMichal Miroslaw 
2987310ece8SMichal Miroslaw 	if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
2997310ece8SMichal Miroslaw 		(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
3007310ece8SMichal Miroslaw 		err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
3017310ece8SMichal Miroslaw 		if (err)
3027310ece8SMichal Miroslaw 			return err;
3037310ece8SMichal Miroslaw 	} else
3047310ece8SMichal Miroslaw 		return 0;
3057310ece8SMichal Miroslaw 
3067310ece8SMichal Miroslaw 	err = sdio_enable_wide(card);
3077310ece8SMichal Miroslaw 	if (err <= 0)
3087310ece8SMichal Miroslaw 		mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
3097310ece8SMichal Miroslaw 
3107310ece8SMichal Miroslaw 	return err;
3117310ece8SMichal Miroslaw }
3127310ece8SMichal Miroslaw 
3137310ece8SMichal Miroslaw 
3146b5eda36SDaniel Drake /*
315d16f5770SPierre Ossman  * Test if the card supports high-speed mode and, if so, switch to it.
316d16f5770SPierre Ossman  */
3177310ece8SMichal Miroslaw static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
318d16f5770SPierre Ossman {
319d16f5770SPierre Ossman 	int ret;
320d16f5770SPierre Ossman 	u8 speed;
321d16f5770SPierre Ossman 
322d16f5770SPierre Ossman 	if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
323d16f5770SPierre Ossman 		return 0;
324d16f5770SPierre Ossman 
325d16f5770SPierre Ossman 	if (!card->cccr.high_speed)
326d16f5770SPierre Ossman 		return 0;
327d16f5770SPierre Ossman 
328d16f5770SPierre Ossman 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
329d16f5770SPierre Ossman 	if (ret)
330d16f5770SPierre Ossman 		return ret;
331d16f5770SPierre Ossman 
3327310ece8SMichal Miroslaw 	if (enable)
333d16f5770SPierre Ossman 		speed |= SDIO_SPEED_EHS;
3347310ece8SMichal Miroslaw 	else
3357310ece8SMichal Miroslaw 		speed &= ~SDIO_SPEED_EHS;
336d16f5770SPierre Ossman 
337d16f5770SPierre Ossman 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
338d16f5770SPierre Ossman 	if (ret)
339d16f5770SPierre Ossman 		return ret;
340d16f5770SPierre Ossman 
34171578a1eSMichal Miroslaw 	return 1;
34271578a1eSMichal Miroslaw }
343d16f5770SPierre Ossman 
3447310ece8SMichal Miroslaw /*
3457310ece8SMichal Miroslaw  * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
3467310ece8SMichal Miroslaw  */
3477310ece8SMichal Miroslaw static int sdio_enable_hs(struct mmc_card *card)
3487310ece8SMichal Miroslaw {
3497310ece8SMichal Miroslaw 	int ret;
3507310ece8SMichal Miroslaw 
3517310ece8SMichal Miroslaw 	ret = mmc_sdio_switch_hs(card, true);
3527310ece8SMichal Miroslaw 	if (ret <= 0 || card->type == MMC_TYPE_SDIO)
3537310ece8SMichal Miroslaw 		return ret;
3547310ece8SMichal Miroslaw 
3557310ece8SMichal Miroslaw 	ret = mmc_sd_switch_hs(card);
3567310ece8SMichal Miroslaw 	if (ret <= 0)
3577310ece8SMichal Miroslaw 		mmc_sdio_switch_hs(card, false);
3587310ece8SMichal Miroslaw 
3597310ece8SMichal Miroslaw 	return ret;
3607310ece8SMichal Miroslaw }
3617310ece8SMichal Miroslaw 
36271578a1eSMichal Miroslaw static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
36371578a1eSMichal Miroslaw {
36471578a1eSMichal Miroslaw 	unsigned max_dtr;
36571578a1eSMichal Miroslaw 
366cdc99179SSeungwon Jeon 	if (mmc_card_hs(card)) {
36771578a1eSMichal Miroslaw 		/*
36871578a1eSMichal Miroslaw 		 * The SDIO specification doesn't mention how
36971578a1eSMichal Miroslaw 		 * the CIS transfer speed register relates to
37071578a1eSMichal Miroslaw 		 * high-speed, but it seems that 50 MHz is
37171578a1eSMichal Miroslaw 		 * mandatory.
37271578a1eSMichal Miroslaw 		 */
37371578a1eSMichal Miroslaw 		max_dtr = 50000000;
37471578a1eSMichal Miroslaw 	} else {
37571578a1eSMichal Miroslaw 		max_dtr = card->cis.max_dtr;
37671578a1eSMichal Miroslaw 	}
37771578a1eSMichal Miroslaw 
3787310ece8SMichal Miroslaw 	if (card->type == MMC_TYPE_SD_COMBO)
3797310ece8SMichal Miroslaw 		max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
3807310ece8SMichal Miroslaw 
38171578a1eSMichal Miroslaw 	return max_dtr;
382d16f5770SPierre Ossman }
383d16f5770SPierre Ossman 
384a303c531SPhilip Rakity static unsigned char host_drive_to_sdio_drive(int host_strength)
385a303c531SPhilip Rakity {
386a303c531SPhilip Rakity 	switch (host_strength) {
387a303c531SPhilip Rakity 	case MMC_SET_DRIVER_TYPE_A:
388a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_A;
389a303c531SPhilip Rakity 	case MMC_SET_DRIVER_TYPE_B:
390a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_B;
391a303c531SPhilip Rakity 	case MMC_SET_DRIVER_TYPE_C:
392a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_C;
393a303c531SPhilip Rakity 	case MMC_SET_DRIVER_TYPE_D:
394a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_D;
395a303c531SPhilip Rakity 	default:
396a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_B;
397a303c531SPhilip Rakity 	}
398a303c531SPhilip Rakity }
399a303c531SPhilip Rakity 
400a303c531SPhilip Rakity static void sdio_select_driver_type(struct mmc_card *card)
401a303c531SPhilip Rakity {
402a303c531SPhilip Rakity 	int host_drv_type = SD_DRIVER_TYPE_B;
403a303c531SPhilip Rakity 	int card_drv_type = SD_DRIVER_TYPE_B;
404a303c531SPhilip Rakity 	int drive_strength;
405a303c531SPhilip Rakity 	unsigned char card_strength;
406a303c531SPhilip Rakity 	int err;
407a303c531SPhilip Rakity 
408a303c531SPhilip Rakity 	/*
409a303c531SPhilip Rakity 	 * If the host doesn't support any of the Driver Types A,C or D,
410a303c531SPhilip Rakity 	 * or there is no board specific handler then default Driver
411a303c531SPhilip Rakity 	 * Type B is used.
412a303c531SPhilip Rakity 	 */
413a303c531SPhilip Rakity 	if (!(card->host->caps &
414a303c531SPhilip Rakity 		(MMC_CAP_DRIVER_TYPE_A |
415a303c531SPhilip Rakity 		 MMC_CAP_DRIVER_TYPE_C |
416a303c531SPhilip Rakity 		 MMC_CAP_DRIVER_TYPE_D)))
417a303c531SPhilip Rakity 		return;
418a303c531SPhilip Rakity 
419a303c531SPhilip Rakity 	if (!card->host->ops->select_drive_strength)
420a303c531SPhilip Rakity 		return;
421a303c531SPhilip Rakity 
422a303c531SPhilip Rakity 	if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
423a303c531SPhilip Rakity 		host_drv_type |= SD_DRIVER_TYPE_A;
424a303c531SPhilip Rakity 
425a303c531SPhilip Rakity 	if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
426a303c531SPhilip Rakity 		host_drv_type |= SD_DRIVER_TYPE_C;
427a303c531SPhilip Rakity 
428a303c531SPhilip Rakity 	if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
429a303c531SPhilip Rakity 		host_drv_type |= SD_DRIVER_TYPE_D;
430a303c531SPhilip Rakity 
431a303c531SPhilip Rakity 	if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
432a303c531SPhilip Rakity 		card_drv_type |= SD_DRIVER_TYPE_A;
433a303c531SPhilip Rakity 
434a303c531SPhilip Rakity 	if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
435a303c531SPhilip Rakity 		card_drv_type |= SD_DRIVER_TYPE_C;
436a303c531SPhilip Rakity 
437a303c531SPhilip Rakity 	if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
438a303c531SPhilip Rakity 		card_drv_type |= SD_DRIVER_TYPE_D;
439a303c531SPhilip Rakity 
440a303c531SPhilip Rakity 	/*
441a303c531SPhilip Rakity 	 * The drive strength that the hardware can support
442a303c531SPhilip Rakity 	 * depends on the board design.  Pass the appropriate
443a303c531SPhilip Rakity 	 * information and let the hardware specific code
444a303c531SPhilip Rakity 	 * return what is possible given the options
445a303c531SPhilip Rakity 	 */
446a303c531SPhilip Rakity 	drive_strength = card->host->ops->select_drive_strength(
447a303c531SPhilip Rakity 		card->sw_caps.uhs_max_dtr,
448a303c531SPhilip Rakity 		host_drv_type, card_drv_type);
449a303c531SPhilip Rakity 
450a303c531SPhilip Rakity 	/* if error just use default for drive strength B */
451a303c531SPhilip Rakity 	err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
452a303c531SPhilip Rakity 		&card_strength);
453a303c531SPhilip Rakity 	if (err)
454a303c531SPhilip Rakity 		return;
455a303c531SPhilip Rakity 
456a303c531SPhilip Rakity 	card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
457a303c531SPhilip Rakity 	card_strength |= host_drive_to_sdio_drive(drive_strength);
458a303c531SPhilip Rakity 
459a303c531SPhilip Rakity 	err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
460a303c531SPhilip Rakity 		card_strength, NULL);
461a303c531SPhilip Rakity 
462a303c531SPhilip Rakity 	/* if error default to drive strength B */
463a303c531SPhilip Rakity 	if (!err)
464a303c531SPhilip Rakity 		mmc_set_driver_type(card->host, drive_strength);
465a303c531SPhilip Rakity }
466a303c531SPhilip Rakity 
467a303c531SPhilip Rakity 
468a303c531SPhilip Rakity static int sdio_set_bus_speed_mode(struct mmc_card *card)
469a303c531SPhilip Rakity {
470a303c531SPhilip Rakity 	unsigned int bus_speed, timing;
471a303c531SPhilip Rakity 	int err;
472a303c531SPhilip Rakity 	unsigned char speed;
473a303c531SPhilip Rakity 
474a303c531SPhilip Rakity 	/*
475a303c531SPhilip Rakity 	 * If the host doesn't support any of the UHS-I modes, fallback on
476a303c531SPhilip Rakity 	 * default speed.
477a303c531SPhilip Rakity 	 */
47841875e38SSujit Reddy Thumma 	if (!mmc_host_uhs(card->host))
479a303c531SPhilip Rakity 		return 0;
480a303c531SPhilip Rakity 
481a303c531SPhilip Rakity 	bus_speed = SDIO_SPEED_SDR12;
482a303c531SPhilip Rakity 	timing = MMC_TIMING_UHS_SDR12;
483a303c531SPhilip Rakity 	if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
484a303c531SPhilip Rakity 	    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
485a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_SDR104;
486a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_SDR104;
487a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
48877e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
489a303c531SPhilip Rakity 	} else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
490a303c531SPhilip Rakity 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
491a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_DDR50;
492a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_DDR50;
493a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
49477e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
495a303c531SPhilip Rakity 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
496a303c531SPhilip Rakity 		    MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
497a303c531SPhilip Rakity 		    SD_MODE_UHS_SDR50)) {
498a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_SDR50;
499a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_SDR50;
500a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
50177e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
502a303c531SPhilip Rakity 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
503a303c531SPhilip Rakity 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
504a303c531SPhilip Rakity 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
505a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_SDR25;
506a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_SDR25;
507a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
50877e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
509a303c531SPhilip Rakity 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
510a303c531SPhilip Rakity 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
511a303c531SPhilip Rakity 		    MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
512a303c531SPhilip Rakity 		    SD_MODE_UHS_SDR12)) {
513a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_SDR12;
514a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_SDR12;
515a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
51677e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
517a303c531SPhilip Rakity 	}
518a303c531SPhilip Rakity 
519a303c531SPhilip Rakity 	err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
520a303c531SPhilip Rakity 	if (err)
521a303c531SPhilip Rakity 		return err;
522a303c531SPhilip Rakity 
523a303c531SPhilip Rakity 	speed &= ~SDIO_SPEED_BSS_MASK;
524a303c531SPhilip Rakity 	speed |= bus_speed;
525a303c531SPhilip Rakity 	err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
526a303c531SPhilip Rakity 	if (err)
527a303c531SPhilip Rakity 		return err;
528a303c531SPhilip Rakity 
529a303c531SPhilip Rakity 	if (bus_speed) {
530a303c531SPhilip Rakity 		mmc_set_timing(card->host, timing);
531a303c531SPhilip Rakity 		mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
532a303c531SPhilip Rakity 	}
533a303c531SPhilip Rakity 
534a303c531SPhilip Rakity 	return 0;
535a303c531SPhilip Rakity }
536a303c531SPhilip Rakity 
537a303c531SPhilip Rakity /*
538a303c531SPhilip Rakity  * UHS-I specific initialization procedure
539a303c531SPhilip Rakity  */
540a303c531SPhilip Rakity static int mmc_sdio_init_uhs_card(struct mmc_card *card)
541a303c531SPhilip Rakity {
542a303c531SPhilip Rakity 	int err;
543a303c531SPhilip Rakity 
544a303c531SPhilip Rakity 	if (!card->scr.sda_spec3)
545a303c531SPhilip Rakity 		return 0;
546a303c531SPhilip Rakity 
547a303c531SPhilip Rakity 	/*
548a303c531SPhilip Rakity 	 * Switch to wider bus (if supported).
549a303c531SPhilip Rakity 	 */
550a303c531SPhilip Rakity 	if (card->host->caps & MMC_CAP_4_BIT_DATA) {
551a303c531SPhilip Rakity 		err = sdio_enable_4bit_bus(card);
552a303c531SPhilip Rakity 		if (err > 0) {
553a303c531SPhilip Rakity 			mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
554a303c531SPhilip Rakity 			err = 0;
555a303c531SPhilip Rakity 		}
556a303c531SPhilip Rakity 	}
557a303c531SPhilip Rakity 
558a303c531SPhilip Rakity 	/* Set the driver strength for the card */
559a303c531SPhilip Rakity 	sdio_select_driver_type(card);
560a303c531SPhilip Rakity 
561a303c531SPhilip Rakity 	/* Set bus speed mode of the card */
562a303c531SPhilip Rakity 	err = sdio_set_bus_speed_mode(card);
563a303c531SPhilip Rakity 	if (err)
564a303c531SPhilip Rakity 		goto out;
565a303c531SPhilip Rakity 
566810e08eeSFredrik Soderstedt 	/*
567810e08eeSFredrik Soderstedt 	 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
568810e08eeSFredrik Soderstedt 	 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
569810e08eeSFredrik Soderstedt 	 */
570810e08eeSFredrik Soderstedt 	if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning &&
571810e08eeSFredrik Soderstedt 			((card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR50) ||
572810e08eeSFredrik Soderstedt 			 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104))) {
573810e08eeSFredrik Soderstedt 		mmc_host_clk_hold(card->host);
574a4924c71SGirish K S 		err = card->host->ops->execute_tuning(card->host,
575a4924c71SGirish K S 						      MMC_SEND_TUNING_BLOCK);
576810e08eeSFredrik Soderstedt 		mmc_host_clk_release(card->host);
577810e08eeSFredrik Soderstedt 	}
578a303c531SPhilip Rakity 
579a303c531SPhilip Rakity out:
580a303c531SPhilip Rakity 
581a303c531SPhilip Rakity 	return err;
582a303c531SPhilip Rakity }
583a303c531SPhilip Rakity 
584d16f5770SPierre Ossman /*
58517d33e14SNicolas Pitre  * Handle the detection and initialisation of a card.
58617d33e14SNicolas Pitre  *
58717d33e14SNicolas Pitre  * In the case of a resume, "oldcard" will contain the card
58817d33e14SNicolas Pitre  * we're trying to reinitialise.
58917d33e14SNicolas Pitre  */
59017d33e14SNicolas Pitre static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
5913bca4cf7SChris Ball 			      struct mmc_card *oldcard, int powered_resume)
59217d33e14SNicolas Pitre {
59317d33e14SNicolas Pitre 	struct mmc_card *card;
59417d33e14SNicolas Pitre 	int err;
5950797e5f1SJohan Rudholm 	int retries = 10;
596db4a0d05SUlf Hansson 	u32 rocr = 0;
59769041150SUlf Hansson 	u32 ocr_card = ocr;
59817d33e14SNicolas Pitre 
59917d33e14SNicolas Pitre 	BUG_ON(!host);
60017d33e14SNicolas Pitre 	WARN_ON(!host->claimed);
60117d33e14SNicolas Pitre 
602db4a0d05SUlf Hansson 	/* to query card if 1.8V signalling is supported */
603db4a0d05SUlf Hansson 	if (mmc_host_uhs(host))
604db4a0d05SUlf Hansson 		ocr |= R4_18V_PRESENT;
605db4a0d05SUlf Hansson 
6060797e5f1SJohan Rudholm try_again:
6070797e5f1SJohan Rudholm 	if (!retries) {
6086606110dSJoe Perches 		pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
6090797e5f1SJohan Rudholm 		ocr &= ~R4_18V_PRESENT;
6100797e5f1SJohan Rudholm 	}
6110797e5f1SJohan Rudholm 
61217d33e14SNicolas Pitre 	/*
61317d33e14SNicolas Pitre 	 * Inform the card of the voltage
61417d33e14SNicolas Pitre 	 */
6153bca4cf7SChris Ball 	if (!powered_resume) {
616db4a0d05SUlf Hansson 		err = mmc_send_io_op_cond(host, ocr, &rocr);
61717d33e14SNicolas Pitre 		if (err)
61817d33e14SNicolas Pitre 			goto err;
6193bca4cf7SChris Ball 	}
62017d33e14SNicolas Pitre 
62117d33e14SNicolas Pitre 	/*
62217d33e14SNicolas Pitre 	 * For SPI, enable CRC as appropriate.
62317d33e14SNicolas Pitre 	 */
62417d33e14SNicolas Pitre 	if (mmc_host_is_spi(host)) {
62517d33e14SNicolas Pitre 		err = mmc_spi_set_crc(host, use_spi_crc);
62617d33e14SNicolas Pitre 		if (err)
62717d33e14SNicolas Pitre 			goto err;
62817d33e14SNicolas Pitre 	}
62917d33e14SNicolas Pitre 
63017d33e14SNicolas Pitre 	/*
63117d33e14SNicolas Pitre 	 * Allocate card structure.
63217d33e14SNicolas Pitre 	 */
63317d33e14SNicolas Pitre 	card = mmc_alloc_card(host, NULL);
63417d33e14SNicolas Pitre 	if (IS_ERR(card)) {
63517d33e14SNicolas Pitre 		err = PTR_ERR(card);
63617d33e14SNicolas Pitre 		goto err;
63717d33e14SNicolas Pitre 	}
63817d33e14SNicolas Pitre 
639db4a0d05SUlf Hansson 	if ((rocr & R4_MEMORY_PRESENT) &&
640db4a0d05SUlf Hansson 	    mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
6417310ece8SMichal Miroslaw 		card->type = MMC_TYPE_SD_COMBO;
6427310ece8SMichal Miroslaw 
6437310ece8SMichal Miroslaw 		if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
6447310ece8SMichal Miroslaw 		    memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
6457310ece8SMichal Miroslaw 			mmc_remove_card(card);
6467310ece8SMichal Miroslaw 			return -ENOENT;
6477310ece8SMichal Miroslaw 		}
6487310ece8SMichal Miroslaw 	} else {
64917d33e14SNicolas Pitre 		card->type = MMC_TYPE_SDIO;
65017d33e14SNicolas Pitre 
6517310ece8SMichal Miroslaw 		if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
6527310ece8SMichal Miroslaw 			mmc_remove_card(card);
6537310ece8SMichal Miroslaw 			return -ENOENT;
6547310ece8SMichal Miroslaw 		}
6557310ece8SMichal Miroslaw 	}
6567310ece8SMichal Miroslaw 
65717d33e14SNicolas Pitre 	/*
6583fcb027dSDaniel Mack 	 * Call the optional HC's init_card function to handle quirks.
6593fcb027dSDaniel Mack 	 */
6603fcb027dSDaniel Mack 	if (host->ops->init_card)
6613fcb027dSDaniel Mack 		host->ops->init_card(host, card);
6623fcb027dSDaniel Mack 
6633fcb027dSDaniel Mack 	/*
664a303c531SPhilip Rakity 	 * If the host and card support UHS-I mode request the card
665a303c531SPhilip Rakity 	 * to switch to 1.8V signaling level.  No 1.8v signalling if
6667122bbb0SMasanari Iida 	 * UHS mode is not enabled to maintain compatibility and some
667a303c531SPhilip Rakity 	 * systems that claim 1.8v signalling in fact do not support
668a303c531SPhilip Rakity 	 * it.
669a303c531SPhilip Rakity 	 */
670db4a0d05SUlf Hansson 	if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
6710f791fdaSUlf Hansson 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
672db4a0d05SUlf Hansson 					ocr);
6730797e5f1SJohan Rudholm 		if (err == -EAGAIN) {
6740797e5f1SJohan Rudholm 			sdio_reset(host);
6750797e5f1SJohan Rudholm 			mmc_go_idle(host);
6760797e5f1SJohan Rudholm 			mmc_send_if_cond(host, host->ocr_avail);
6770797e5f1SJohan Rudholm 			mmc_remove_card(card);
6780797e5f1SJohan Rudholm 			retries--;
6790797e5f1SJohan Rudholm 			goto try_again;
6800797e5f1SJohan Rudholm 		} else if (err) {
681a303c531SPhilip Rakity 			ocr &= ~R4_18V_PRESENT;
682a303c531SPhilip Rakity 		}
683a303c531SPhilip Rakity 		err = 0;
684a303c531SPhilip Rakity 	} else {
685a303c531SPhilip Rakity 		ocr &= ~R4_18V_PRESENT;
686a303c531SPhilip Rakity 	}
687a303c531SPhilip Rakity 
688a303c531SPhilip Rakity 	/*
68917d33e14SNicolas Pitre 	 * For native busses:  set card RCA and quit open drain mode.
69017d33e14SNicolas Pitre 	 */
6913bca4cf7SChris Ball 	if (!powered_resume && !mmc_host_is_spi(host)) {
69217d33e14SNicolas Pitre 		err = mmc_send_relative_addr(host, &card->rca);
69317d33e14SNicolas Pitre 		if (err)
69417d33e14SNicolas Pitre 			goto remove;
69517d33e14SNicolas Pitre 
6960aab3995SStefan Nilsson XK 		/*
6970aab3995SStefan Nilsson XK 		 * Update oldcard with the new RCA received from the SDIO
6980aab3995SStefan Nilsson XK 		 * device -- we're doing this so that it's updated in the
6990aab3995SStefan Nilsson XK 		 * "card" struct when oldcard overwrites that later.
7000aab3995SStefan Nilsson XK 		 */
7010aab3995SStefan Nilsson XK 		if (oldcard)
7020aab3995SStefan Nilsson XK 			oldcard->rca = card->rca;
70317d33e14SNicolas Pitre 	}
70417d33e14SNicolas Pitre 
70517d33e14SNicolas Pitre 	/*
7067310ece8SMichal Miroslaw 	 * Read CSD, before selecting the card
7077310ece8SMichal Miroslaw 	 */
7087310ece8SMichal Miroslaw 	if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
7097310ece8SMichal Miroslaw 		err = mmc_sd_get_csd(host, card);
7107310ece8SMichal Miroslaw 		if (err)
7117310ece8SMichal Miroslaw 			return err;
7127310ece8SMichal Miroslaw 
7137310ece8SMichal Miroslaw 		mmc_decode_cid(card);
7147310ece8SMichal Miroslaw 	}
7157310ece8SMichal Miroslaw 
7167310ece8SMichal Miroslaw 	/*
71717d33e14SNicolas Pitre 	 * Select card, as all following commands rely on that.
71817d33e14SNicolas Pitre 	 */
7193bca4cf7SChris Ball 	if (!powered_resume && !mmc_host_is_spi(host)) {
72017d33e14SNicolas Pitre 		err = mmc_select_card(card);
72117d33e14SNicolas Pitre 		if (err)
72217d33e14SNicolas Pitre 			goto remove;
72317d33e14SNicolas Pitre 	}
72417d33e14SNicolas Pitre 
7256f51be3dSGrazvydas Ignotas 	if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
7266f51be3dSGrazvydas Ignotas 		/*
7276f51be3dSGrazvydas Ignotas 		 * This is non-standard SDIO device, meaning it doesn't
7286f51be3dSGrazvydas Ignotas 		 * have any CIA (Common I/O area) registers present.
7296f51be3dSGrazvydas Ignotas 		 * It's host's responsibility to fill cccr and cis
7306f51be3dSGrazvydas Ignotas 		 * structures in init_card().
7316f51be3dSGrazvydas Ignotas 		 */
7326f51be3dSGrazvydas Ignotas 		mmc_set_clock(host, card->cis.max_dtr);
7336f51be3dSGrazvydas Ignotas 
7346f51be3dSGrazvydas Ignotas 		if (card->cccr.high_speed) {
7356f51be3dSGrazvydas Ignotas 			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
7366f51be3dSGrazvydas Ignotas 		}
7376f51be3dSGrazvydas Ignotas 
7386f51be3dSGrazvydas Ignotas 		goto finish;
7396f51be3dSGrazvydas Ignotas 	}
7406f51be3dSGrazvydas Ignotas 
74117d33e14SNicolas Pitre 	/*
74217d33e14SNicolas Pitre 	 * Read the common registers.
74317d33e14SNicolas Pitre 	 */
7442d0d68f5SPhilip Rakity 	err = sdio_read_cccr(card, ocr);
74517d33e14SNicolas Pitre 	if (err)
74617d33e14SNicolas Pitre 		goto remove;
74717d33e14SNicolas Pitre 
74817d33e14SNicolas Pitre 	/*
74917d33e14SNicolas Pitre 	 * Read the common CIS tuples.
75017d33e14SNicolas Pitre 	 */
75117d33e14SNicolas Pitre 	err = sdio_read_common_cis(card);
75217d33e14SNicolas Pitre 	if (err)
75317d33e14SNicolas Pitre 		goto remove;
75417d33e14SNicolas Pitre 
75517d33e14SNicolas Pitre 	if (oldcard) {
75617d33e14SNicolas Pitre 		int same = (card->cis.vendor == oldcard->cis.vendor &&
75717d33e14SNicolas Pitre 			    card->cis.device == oldcard->cis.device);
75817d33e14SNicolas Pitre 		mmc_remove_card(card);
7597310ece8SMichal Miroslaw 		if (!same)
7607310ece8SMichal Miroslaw 			return -ENOENT;
7617310ece8SMichal Miroslaw 
76217d33e14SNicolas Pitre 		card = oldcard;
76317d33e14SNicolas Pitre 	}
76469041150SUlf Hansson 	card->ocr = ocr_card;
76532780cd1SAndrei Warkentin 	mmc_fixup_device(card, NULL);
76617d33e14SNicolas Pitre 
7677310ece8SMichal Miroslaw 	if (card->type == MMC_TYPE_SD_COMBO) {
7687310ece8SMichal Miroslaw 		err = mmc_sd_setup_card(host, card, oldcard != NULL);
7697310ece8SMichal Miroslaw 		/* handle as SDIO-only card if memory init failed */
7707310ece8SMichal Miroslaw 		if (err) {
7717310ece8SMichal Miroslaw 			mmc_go_idle(host);
7727310ece8SMichal Miroslaw 			if (mmc_host_is_spi(host))
7737310ece8SMichal Miroslaw 				/* should not fail, as it worked previously */
7747310ece8SMichal Miroslaw 				mmc_spi_set_crc(host, use_spi_crc);
7757310ece8SMichal Miroslaw 			card->type = MMC_TYPE_SDIO;
7767310ece8SMichal Miroslaw 		} else
7777310ece8SMichal Miroslaw 			card->dev.type = &sd_type;
7787310ece8SMichal Miroslaw 	}
7797310ece8SMichal Miroslaw 
7807310ece8SMichal Miroslaw 	/*
7817310ece8SMichal Miroslaw 	 * If needed, disconnect card detection pull-up resistor.
7827310ece8SMichal Miroslaw 	 */
7837310ece8SMichal Miroslaw 	err = sdio_disable_cd(card);
7847310ece8SMichal Miroslaw 	if (err)
7857310ece8SMichal Miroslaw 		goto remove;
7867310ece8SMichal Miroslaw 
787a303c531SPhilip Rakity 	/* Initialization sequence for UHS-I cards */
788a303c531SPhilip Rakity 	/* Only if card supports 1.8v and UHS signaling */
789a303c531SPhilip Rakity 	if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
790a303c531SPhilip Rakity 		err = mmc_sdio_init_uhs_card(card);
791a303c531SPhilip Rakity 		if (err)
792a303c531SPhilip Rakity 			goto remove;
793a303c531SPhilip Rakity 	} else {
79417d33e14SNicolas Pitre 		/*
79517d33e14SNicolas Pitre 		 * Switch to high-speed (if supported).
79617d33e14SNicolas Pitre 		 */
79717d33e14SNicolas Pitre 		err = sdio_enable_hs(card);
79871578a1eSMichal Miroslaw 		if (err > 0)
799cdc99179SSeungwon Jeon 			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
80071578a1eSMichal Miroslaw 		else if (err)
80117d33e14SNicolas Pitre 			goto remove;
80217d33e14SNicolas Pitre 
80317d33e14SNicolas Pitre 		/*
80417d33e14SNicolas Pitre 		 * Change to the card's maximum speed.
80517d33e14SNicolas Pitre 		 */
80671578a1eSMichal Miroslaw 		mmc_set_clock(host, mmc_sdio_get_max_clock(card));
80717d33e14SNicolas Pitre 
80817d33e14SNicolas Pitre 		/*
80917d33e14SNicolas Pitre 		 * Switch to wider bus (if supported).
81017d33e14SNicolas Pitre 		 */
8117310ece8SMichal Miroslaw 		err = sdio_enable_4bit_bus(card);
8127310ece8SMichal Miroslaw 		if (err > 0)
8137310ece8SMichal Miroslaw 			mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
8147310ece8SMichal Miroslaw 		else if (err)
81517d33e14SNicolas Pitre 			goto remove;
816a303c531SPhilip Rakity 	}
8176f51be3dSGrazvydas Ignotas finish:
81817d33e14SNicolas Pitre 	if (!oldcard)
81917d33e14SNicolas Pitre 		host->card = card;
82017d33e14SNicolas Pitre 	return 0;
82117d33e14SNicolas Pitre 
82217d33e14SNicolas Pitre remove:
82317d33e14SNicolas Pitre 	if (!oldcard)
82417d33e14SNicolas Pitre 		mmc_remove_card(card);
82517d33e14SNicolas Pitre 
82617d33e14SNicolas Pitre err:
82717d33e14SNicolas Pitre 	return err;
82817d33e14SNicolas Pitre }
82917d33e14SNicolas Pitre 
83017d33e14SNicolas Pitre /*
8315c4e6f13SPierre Ossman  * Host is being removed. Free up the current card.
8325c4e6f13SPierre Ossman  */
8335c4e6f13SPierre Ossman static void mmc_sdio_remove(struct mmc_host *host)
8345c4e6f13SPierre Ossman {
835e29a7d73SPierre Ossman 	int i;
836e29a7d73SPierre Ossman 
8375c4e6f13SPierre Ossman 	BUG_ON(!host);
8385c4e6f13SPierre Ossman 	BUG_ON(!host->card);
8395c4e6f13SPierre Ossman 
840e29a7d73SPierre Ossman 	for (i = 0;i < host->card->sdio_funcs;i++) {
841e29a7d73SPierre Ossman 		if (host->card->sdio_func[i]) {
842e29a7d73SPierre Ossman 			sdio_remove_func(host->card->sdio_func[i]);
843e29a7d73SPierre Ossman 			host->card->sdio_func[i] = NULL;
844e29a7d73SPierre Ossman 		}
845e29a7d73SPierre Ossman 	}
846e29a7d73SPierre Ossman 
8475c4e6f13SPierre Ossman 	mmc_remove_card(host->card);
8485c4e6f13SPierre Ossman 	host->card = NULL;
8495c4e6f13SPierre Ossman }
8505c4e6f13SPierre Ossman 
8515c4e6f13SPierre Ossman /*
852d3049504SAdrian Hunter  * Card detection - card is alive.
853d3049504SAdrian Hunter  */
854d3049504SAdrian Hunter static int mmc_sdio_alive(struct mmc_host *host)
855d3049504SAdrian Hunter {
856d3049504SAdrian Hunter 	return mmc_select_card(host->card);
857d3049504SAdrian Hunter }
858d3049504SAdrian Hunter 
859d3049504SAdrian Hunter /*
8605c4e6f13SPierre Ossman  * Card detection callback from host.
8615c4e6f13SPierre Ossman  */
8625c4e6f13SPierre Ossman static void mmc_sdio_detect(struct mmc_host *host)
8635c4e6f13SPierre Ossman {
8645c4e6f13SPierre Ossman 	int err;
8655c4e6f13SPierre Ossman 
8665c4e6f13SPierre Ossman 	BUG_ON(!host);
8675c4e6f13SPierre Ossman 	BUG_ON(!host->card);
8685c4e6f13SPierre Ossman 
86987973ba2SOhad Ben-Cohen 	/* Make sure card is powered before detecting it */
870ed919b01SOhad Ben-Cohen 	if (host->caps & MMC_CAP_POWER_OFF_CARD) {
87187973ba2SOhad Ben-Cohen 		err = pm_runtime_get_sync(&host->card->dev);
8723bffb800SLi Fei 		if (err < 0) {
8733bffb800SLi Fei 			pm_runtime_put_noidle(&host->card->dev);
87487973ba2SOhad Ben-Cohen 			goto out;
875ed919b01SOhad Ben-Cohen 		}
8763bffb800SLi Fei 	}
87787973ba2SOhad Ben-Cohen 
8785c4e6f13SPierre Ossman 	mmc_claim_host(host);
8795c4e6f13SPierre Ossman 
8805c4e6f13SPierre Ossman 	/*
8815c4e6f13SPierre Ossman 	 * Just check if our card has been removed.
8825c4e6f13SPierre Ossman 	 */
883d3049504SAdrian Hunter 	err = _mmc_detect_card_removed(host);
8845c4e6f13SPierre Ossman 
8855c4e6f13SPierre Ossman 	mmc_release_host(host);
8865c4e6f13SPierre Ossman 
8874d0812c3SOhad Ben-Cohen 	/*
8884d0812c3SOhad Ben-Cohen 	 * Tell PM core it's OK to power off the card now.
8894d0812c3SOhad Ben-Cohen 	 *
8904d0812c3SOhad Ben-Cohen 	 * The _sync variant is used in order to ensure that the card
8914d0812c3SOhad Ben-Cohen 	 * is left powered off in case an error occurred, and the card
8924d0812c3SOhad Ben-Cohen 	 * is going to be removed.
8934d0812c3SOhad Ben-Cohen 	 *
8944d0812c3SOhad Ben-Cohen 	 * Since there is no specific reason to believe a new user
8954d0812c3SOhad Ben-Cohen 	 * is about to show up at this point, the _sync variant is
8964d0812c3SOhad Ben-Cohen 	 * desirable anyway.
8974d0812c3SOhad Ben-Cohen 	 */
898ed919b01SOhad Ben-Cohen 	if (host->caps & MMC_CAP_POWER_OFF_CARD)
8994d0812c3SOhad Ben-Cohen 		pm_runtime_put_sync(&host->card->dev);
9004d0812c3SOhad Ben-Cohen 
90187973ba2SOhad Ben-Cohen out:
9025c4e6f13SPierre Ossman 	if (err) {
9035c4e6f13SPierre Ossman 		mmc_sdio_remove(host);
9045c4e6f13SPierre Ossman 
9055c4e6f13SPierre Ossman 		mmc_claim_host(host);
9065c4e6f13SPierre Ossman 		mmc_detach_bus(host);
9077f7e4129SUlf Hansson 		mmc_power_off(host);
9085c4e6f13SPierre Ossman 		mmc_release_host(host);
9095c4e6f13SPierre Ossman 	}
9105c4e6f13SPierre Ossman }
9115c4e6f13SPierre Ossman 
91217d33e14SNicolas Pitre /*
913810caddbSUlf Hansson  * SDIO pre_suspend.  We need to suspend all functions separately.
91417d33e14SNicolas Pitre  * Therefore all registered functions must have drivers with suspend
91517d33e14SNicolas Pitre  * and resume methods.  Failing that we simply remove the whole card.
91617d33e14SNicolas Pitre  */
917810caddbSUlf Hansson static int mmc_sdio_pre_suspend(struct mmc_host *host)
91817d33e14SNicolas Pitre {
91995cdfb72SNicolas Pitre 	int i, err = 0;
92017d33e14SNicolas Pitre 
92117d33e14SNicolas Pitre 	for (i = 0; i < host->card->sdio_funcs; i++) {
92217d33e14SNicolas Pitre 		struct sdio_func *func = host->card->sdio_func[i];
92317d33e14SNicolas Pitre 		if (func && sdio_func_present(func) && func->dev.driver) {
92417d33e14SNicolas Pitre 			const struct dev_pm_ops *pmops = func->dev.driver->pm;
92517d33e14SNicolas Pitre 			if (!pmops || !pmops->suspend || !pmops->resume) {
92695cdfb72SNicolas Pitre 				/* force removal of entire card in that case */
92795cdfb72SNicolas Pitre 				err = -ENOSYS;
928810caddbSUlf Hansson 				break;
929810caddbSUlf Hansson 			}
930810caddbSUlf Hansson 		}
931810caddbSUlf Hansson 	}
932810caddbSUlf Hansson 
933810caddbSUlf Hansson 	return err;
934810caddbSUlf Hansson }
935810caddbSUlf Hansson 
936810caddbSUlf Hansson /*
937810caddbSUlf Hansson  * SDIO suspend.  Suspend all functions separately.
938810caddbSUlf Hansson  */
939810caddbSUlf Hansson static int mmc_sdio_suspend(struct mmc_host *host)
940810caddbSUlf Hansson {
941573185ccSUlf Hansson 	if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
9426b5eda36SDaniel Drake 		mmc_claim_host(host);
9436b5eda36SDaniel Drake 		sdio_disable_wide(host->card);
9446b5eda36SDaniel Drake 		mmc_release_host(host);
9456b5eda36SDaniel Drake 	}
9466b5eda36SDaniel Drake 
947573185ccSUlf Hansson 	if (!mmc_card_keep_power(host))
94874590263SUlf Hansson 		mmc_power_off(host);
94974590263SUlf Hansson 
950573185ccSUlf Hansson 	return 0;
95195cdfb72SNicolas Pitre }
95295cdfb72SNicolas Pitre 
95395cdfb72SNicolas Pitre static int mmc_sdio_resume(struct mmc_host *host)
95495cdfb72SNicolas Pitre {
955573185ccSUlf Hansson 	int err = 0;
95695cdfb72SNicolas Pitre 
95795cdfb72SNicolas Pitre 	BUG_ON(!host);
95895cdfb72SNicolas Pitre 	BUG_ON(!host->card);
95995cdfb72SNicolas Pitre 
96095cdfb72SNicolas Pitre 	/* Basic card reinitialization. */
96195cdfb72SNicolas Pitre 	mmc_claim_host(host);
962080bc977SOhad Ben-Cohen 
96374590263SUlf Hansson 	/* Restore power if needed */
96474590263SUlf Hansson 	if (!mmc_card_keep_power(host)) {
96569041150SUlf Hansson 		mmc_power_up(host, host->card->ocr);
96674590263SUlf Hansson 		/*
96774590263SUlf Hansson 		 * Tell runtime PM core we just powered up the card,
96874590263SUlf Hansson 		 * since it still believes the card is powered off.
96974590263SUlf Hansson 		 * Note that currently runtime PM is only enabled
97074590263SUlf Hansson 		 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
97174590263SUlf Hansson 		 */
97274590263SUlf Hansson 		if (host->caps & MMC_CAP_POWER_OFF_CARD) {
97374590263SUlf Hansson 			pm_runtime_disable(&host->card->dev);
97474590263SUlf Hansson 			pm_runtime_set_active(&host->card->dev);
97574590263SUlf Hansson 			pm_runtime_enable(&host->card->dev);
97674590263SUlf Hansson 		}
97774590263SUlf Hansson 	}
97874590263SUlf Hansson 
979080bc977SOhad Ben-Cohen 	/* No need to reinitialize powered-resumed nonremovable cards */
98051aa66a5SSubhash Jadavani 	if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
98151aa66a5SSubhash Jadavani 		sdio_reset(host);
98251aa66a5SSubhash Jadavani 		mmc_go_idle(host);
98369041150SUlf Hansson 		err = mmc_sdio_init_card(host, host->card->ocr, host->card,
984a5e9425dSOhad Ben-Cohen 					mmc_card_keep_power(host));
98551aa66a5SSubhash Jadavani 	} else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
986080bc977SOhad Ben-Cohen 		/* We may have switched to 1-bit mode during suspend */
987080bc977SOhad Ben-Cohen 		err = sdio_enable_4bit_bus(host->card);
988080bc977SOhad Ben-Cohen 		if (err > 0) {
989080bc977SOhad Ben-Cohen 			mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
990080bc977SOhad Ben-Cohen 			err = 0;
991080bc977SOhad Ben-Cohen 		}
992080bc977SOhad Ben-Cohen 	}
993080bc977SOhad Ben-Cohen 
994dea67c4eSFu Zhonghui 	if (!err && host->sdio_irqs) {
995dea67c4eSFu Zhonghui 		if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
996bbbc4c4dSNicolas Pitre 			wake_up_process(host->sdio_irq_thread);
997dea67c4eSFu Zhonghui 		} else if (host->caps & MMC_CAP_SDIO_IRQ) {
998dea67c4eSFu Zhonghui 			mmc_host_clk_hold(host);
999dea67c4eSFu Zhonghui 			host->ops->enable_sdio_irq(host, 1);
1000dea67c4eSFu Zhonghui 			mmc_host_clk_release(host);
1001dea67c4eSFu Zhonghui 		}
1002dea67c4eSFu Zhonghui 	}
1003dea67c4eSFu Zhonghui 
100495cdfb72SNicolas Pitre 	mmc_release_host(host);
100595cdfb72SNicolas Pitre 
100674590263SUlf Hansson 	host->pm_flags &= ~MMC_PM_KEEP_POWER;
100795cdfb72SNicolas Pitre 	return err;
100817d33e14SNicolas Pitre }
10095c4e6f13SPierre Ossman 
1010d3fe37b1SOhad Ben-Cohen static int mmc_sdio_power_restore(struct mmc_host *host)
1011d3fe37b1SOhad Ben-Cohen {
1012d3fe37b1SOhad Ben-Cohen 	int ret;
1013d3fe37b1SOhad Ben-Cohen 
1014d3fe37b1SOhad Ben-Cohen 	BUG_ON(!host);
1015d3fe37b1SOhad Ben-Cohen 	BUG_ON(!host->card);
1016d3fe37b1SOhad Ben-Cohen 
1017d3fe37b1SOhad Ben-Cohen 	mmc_claim_host(host);
1018c6e633adSDaniel Drake 
1019c6e633adSDaniel Drake 	/*
1020c6e633adSDaniel Drake 	 * Reset the card by performing the same steps that are taken by
1021c6e633adSDaniel Drake 	 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1022c6e633adSDaniel Drake 	 *
1023c6e633adSDaniel Drake 	 * sdio_reset() is technically not needed. Having just powered up the
1024c6e633adSDaniel Drake 	 * hardware, it should already be in reset state. However, some
1025c6e633adSDaniel Drake 	 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1026c6e633adSDaniel Drake 	 * meaning that a reset is required when restoring power soon after
1027c6e633adSDaniel Drake 	 * powering off. It is harmless in other cases.
1028c6e633adSDaniel Drake 	 *
1029c6e633adSDaniel Drake 	 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1030c6e633adSDaniel Drake 	 * is not necessary for non-removable cards. However, it is required
1031c6e633adSDaniel Drake 	 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1032c6e633adSDaniel Drake 	 * harmless in other situations.
1033c6e633adSDaniel Drake 	 *
1034c6e633adSDaniel Drake 	 */
1035e7747475SUlf Hansson 
1036c6e633adSDaniel Drake 	sdio_reset(host);
1037c6e633adSDaniel Drake 	mmc_go_idle(host);
1038*59b6c9e0SNeilBrown 	mmc_send_if_cond(host, host->card->ocr);
1039c6e633adSDaniel Drake 
1040ce69d37bSUlf Hansson 	ret = mmc_send_io_op_cond(host, 0, NULL);
1041c6e633adSDaniel Drake 	if (ret)
1042c6e633adSDaniel Drake 		goto out;
1043c6e633adSDaniel Drake 
1044ce69d37bSUlf Hansson 	ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
1045a5e9425dSOhad Ben-Cohen 				mmc_card_keep_power(host));
1046d3fe37b1SOhad Ben-Cohen 	if (!ret && host->sdio_irqs)
1047d3fe37b1SOhad Ben-Cohen 		mmc_signal_sdio_irq(host);
1048c6e633adSDaniel Drake 
1049c6e633adSDaniel Drake out:
1050d3fe37b1SOhad Ben-Cohen 	mmc_release_host(host);
1051d3fe37b1SOhad Ben-Cohen 
1052d3fe37b1SOhad Ben-Cohen 	return ret;
1053d3fe37b1SOhad Ben-Cohen }
1054d3fe37b1SOhad Ben-Cohen 
105512d01d0bSUlf Hansson static int mmc_sdio_runtime_suspend(struct mmc_host *host)
105612d01d0bSUlf Hansson {
105712d01d0bSUlf Hansson 	/* No references to the card, cut the power to it. */
105812d01d0bSUlf Hansson 	mmc_power_off(host);
105912d01d0bSUlf Hansson 	return 0;
106012d01d0bSUlf Hansson }
106112d01d0bSUlf Hansson 
106212d01d0bSUlf Hansson static int mmc_sdio_runtime_resume(struct mmc_host *host)
106312d01d0bSUlf Hansson {
106412d01d0bSUlf Hansson 	/* Restore power and re-initialize. */
106569041150SUlf Hansson 	mmc_power_up(host, host->card->ocr);
106612d01d0bSUlf Hansson 	return mmc_sdio_power_restore(host);
106712d01d0bSUlf Hansson }
106812d01d0bSUlf Hansson 
10695c4e6f13SPierre Ossman static const struct mmc_bus_ops mmc_sdio_ops = {
10705c4e6f13SPierre Ossman 	.remove = mmc_sdio_remove,
10715c4e6f13SPierre Ossman 	.detect = mmc_sdio_detect,
1072810caddbSUlf Hansson 	.pre_suspend = mmc_sdio_pre_suspend,
107317d33e14SNicolas Pitre 	.suspend = mmc_sdio_suspend,
107417d33e14SNicolas Pitre 	.resume = mmc_sdio_resume,
107512d01d0bSUlf Hansson 	.runtime_suspend = mmc_sdio_runtime_suspend,
107612d01d0bSUlf Hansson 	.runtime_resume = mmc_sdio_runtime_resume,
1077d3fe37b1SOhad Ben-Cohen 	.power_restore = mmc_sdio_power_restore,
1078d3049504SAdrian Hunter 	.alive = mmc_sdio_alive,
10795c4e6f13SPierre Ossman };
10805c4e6f13SPierre Ossman 
10815c4e6f13SPierre Ossman 
10825c4e6f13SPierre Ossman /*
10835c4e6f13SPierre Ossman  * Starting point for SDIO card init.
10845c4e6f13SPierre Ossman  */
1085807e8e40SAndy Ross int mmc_attach_sdio(struct mmc_host *host)
10865c4e6f13SPierre Ossman {
1087807e8e40SAndy Ross 	int err, i, funcs;
108869041150SUlf Hansson 	u32 ocr, rocr;
10895c4e6f13SPierre Ossman 	struct mmc_card *card;
10905c4e6f13SPierre Ossman 
10915c4e6f13SPierre Ossman 	BUG_ON(!host);
1092d84075c8SPierre Ossman 	WARN_ON(!host->claimed);
10935c4e6f13SPierre Ossman 
1094807e8e40SAndy Ross 	err = mmc_send_io_op_cond(host, 0, &ocr);
1095807e8e40SAndy Ross 	if (err)
1096807e8e40SAndy Ross 		return err;
1097807e8e40SAndy Ross 
10985c4e6f13SPierre Ossman 	mmc_attach_bus(host, &mmc_sdio_ops);
10998f230f45STakashi Iwai 	if (host->ocr_avail_sdio)
11008f230f45STakashi Iwai 		host->ocr_avail = host->ocr_avail_sdio;
11015c4e6f13SPierre Ossman 
11025c4e6f13SPierre Ossman 
110369041150SUlf Hansson 	rocr = mmc_select_voltage(host, ocr);
11045c4e6f13SPierre Ossman 
11055c4e6f13SPierre Ossman 	/*
11065c4e6f13SPierre Ossman 	 * Can we support the voltage(s) of the card(s)?
11075c4e6f13SPierre Ossman 	 */
110869041150SUlf Hansson 	if (!rocr) {
11095c4e6f13SPierre Ossman 		err = -EINVAL;
11105c4e6f13SPierre Ossman 		goto err;
11115c4e6f13SPierre Ossman 	}
11125c4e6f13SPierre Ossman 
11135c4e6f13SPierre Ossman 	/*
111417d33e14SNicolas Pitre 	 * Detect and init the card.
11155c4e6f13SPierre Ossman 	 */
111669041150SUlf Hansson 	err = mmc_sdio_init_card(host, rocr, NULL, 0);
11175c4e6f13SPierre Ossman 	if (err)
11185c4e6f13SPierre Ossman 		goto err;
1119ec2ed700SUlf Hansson 
112017d33e14SNicolas Pitre 	card = host->card;
1121af517150SDavid Brownell 
1122af517150SDavid Brownell 	/*
1123ed919b01SOhad Ben-Cohen 	 * Enable runtime PM only if supported by host+card+board
1124ed919b01SOhad Ben-Cohen 	 */
1125ed919b01SOhad Ben-Cohen 	if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1126ed919b01SOhad Ben-Cohen 		/*
112781968561SOhad Ben-Cohen 		 * Let runtime PM core know our card is active
112881968561SOhad Ben-Cohen 		 */
112981968561SOhad Ben-Cohen 		err = pm_runtime_set_active(&card->dev);
113081968561SOhad Ben-Cohen 		if (err)
113181968561SOhad Ben-Cohen 			goto remove;
113281968561SOhad Ben-Cohen 
113381968561SOhad Ben-Cohen 		/*
113481968561SOhad Ben-Cohen 		 * Enable runtime PM for this card
113581968561SOhad Ben-Cohen 		 */
113681968561SOhad Ben-Cohen 		pm_runtime_enable(&card->dev);
1137ed919b01SOhad Ben-Cohen 	}
113881968561SOhad Ben-Cohen 
113981968561SOhad Ben-Cohen 	/*
11405c4e6f13SPierre Ossman 	 * The number of functions on the card is encoded inside
11415c4e6f13SPierre Ossman 	 * the ocr.
11425c4e6f13SPierre Ossman 	 */
1143e8812793SMatt Fleming 	funcs = (ocr & 0x70000000) >> 28;
1144e8812793SMatt Fleming 	card->sdio_funcs = 0;
11454ff6471cSPierre Ossman 
11464ff6471cSPierre Ossman 	/*
1147e29a7d73SPierre Ossman 	 * Initialize (but don't add) all present functions.
1148e29a7d73SPierre Ossman 	 */
1149e8812793SMatt Fleming 	for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1150e29a7d73SPierre Ossman 		err = sdio_init_func(host->card, i + 1);
1151e29a7d73SPierre Ossman 		if (err)
1152e29a7d73SPierre Ossman 			goto remove;
115340bba0c1SOhad Ben-Cohen 
115440bba0c1SOhad Ben-Cohen 		/*
1155ed919b01SOhad Ben-Cohen 		 * Enable Runtime PM for this func (if supported)
115640bba0c1SOhad Ben-Cohen 		 */
1157ed919b01SOhad Ben-Cohen 		if (host->caps & MMC_CAP_POWER_OFF_CARD)
115840bba0c1SOhad Ben-Cohen 			pm_runtime_enable(&card->sdio_func[i]->dev);
1159e29a7d73SPierre Ossman 	}
11605c4e6f13SPierre Ossman 
1161e29a7d73SPierre Ossman 	/*
1162e29a7d73SPierre Ossman 	 * First add the card to the driver model...
1163e29a7d73SPierre Ossman 	 */
1164807e8e40SAndy Ross 	mmc_release_host(host);
11655c4e6f13SPierre Ossman 	err = mmc_add_card(host->card);
11665c4e6f13SPierre Ossman 	if (err)
1167e29a7d73SPierre Ossman 		goto remove_added;
1168e29a7d73SPierre Ossman 
1169e29a7d73SPierre Ossman 	/*
1170e29a7d73SPierre Ossman 	 * ...then the SDIO functions.
1171e29a7d73SPierre Ossman 	 */
1172e29a7d73SPierre Ossman 	for (i = 0;i < funcs;i++) {
1173e29a7d73SPierre Ossman 		err = sdio_add_func(host->card->sdio_func[i]);
1174e29a7d73SPierre Ossman 		if (err)
1175e29a7d73SPierre Ossman 			goto remove_added;
1176e29a7d73SPierre Ossman 	}
11775c4e6f13SPierre Ossman 
117834497913SDmitry Shmidt 	mmc_claim_host(host);
11795c4e6f13SPierre Ossman 	return 0;
11805c4e6f13SPierre Ossman 
1181e29a7d73SPierre Ossman 
1182e29a7d73SPierre Ossman remove_added:
1183e29a7d73SPierre Ossman 	/* Remove without lock if the device has been added. */
1184e29a7d73SPierre Ossman 	mmc_sdio_remove(host);
11855c4e6f13SPierre Ossman 	mmc_claim_host(host);
1186e29a7d73SPierre Ossman remove:
1187e29a7d73SPierre Ossman 	/* And with lock if it hasn't been added. */
1188807e8e40SAndy Ross 	mmc_release_host(host);
1189e29a7d73SPierre Ossman 	if (host->card)
1190e29a7d73SPierre Ossman 		mmc_sdio_remove(host);
1191807e8e40SAndy Ross 	mmc_claim_host(host);
11925c4e6f13SPierre Ossman err:
11935c4e6f13SPierre Ossman 	mmc_detach_bus(host);
11945c4e6f13SPierre Ossman 
1195a3c76eb9SGirish K S 	pr_err("%s: error %d whilst initialising SDIO card\n",
11965c4e6f13SPierre Ossman 		mmc_hostname(host), err);
11975c4e6f13SPierre Ossman 
11985c4e6f13SPierre Ossman 	return err;
11995c4e6f13SPierre Ossman }
12005c4e6f13SPierre Ossman 
1201