xref: /linux/drivers/mmc/core/sdio.c (revision 1144c1e4ddab9339e262e2702744e8f5c6220824)
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"
234facdde1SUlf Hansson #include "card.h"
245857b29bSUlf Hansson #include "host.h"
255c4e6f13SPierre Ossman #include "bus.h"
2628fc64afSShawn Lin #include "quirks.h"
2771578a1eSMichal Miroslaw #include "sd.h"
28e29a7d73SPierre Ossman #include "sdio_bus.h"
295c4e6f13SPierre Ossman #include "mmc_ops.h"
305c4e6f13SPierre Ossman #include "sd_ops.h"
315c4e6f13SPierre Ossman #include "sdio_ops.h"
32b7261261SNicolas Pitre #include "sdio_cis.h"
335c4e6f13SPierre Ossman 
340597007fSPierre Ossman static int sdio_read_fbr(struct sdio_func *func)
350597007fSPierre Ossman {
360597007fSPierre Ossman 	int ret;
370597007fSPierre Ossman 	unsigned char data;
380597007fSPierre Ossman 
39eab40687SOhad Ben-Cohen 	if (mmc_card_nonstd_func_interface(func->card)) {
40eab40687SOhad Ben-Cohen 		func->class = SDIO_CLASS_NONE;
41eab40687SOhad Ben-Cohen 		return 0;
42eab40687SOhad Ben-Cohen 	}
43eab40687SOhad Ben-Cohen 
440597007fSPierre Ossman 	ret = mmc_io_rw_direct(func->card, 0, 0,
457616ee95SDavid Vrabel 		SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
460597007fSPierre Ossman 	if (ret)
470597007fSPierre Ossman 		goto out;
480597007fSPierre Ossman 
490597007fSPierre Ossman 	data &= 0x0f;
500597007fSPierre Ossman 
510597007fSPierre Ossman 	if (data == 0x0f) {
520597007fSPierre Ossman 		ret = mmc_io_rw_direct(func->card, 0, 0,
537616ee95SDavid Vrabel 			SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
540597007fSPierre Ossman 		if (ret)
550597007fSPierre Ossman 			goto out;
560597007fSPierre Ossman 	}
570597007fSPierre Ossman 
580597007fSPierre Ossman 	func->class = data;
590597007fSPierre Ossman 
600597007fSPierre Ossman out:
610597007fSPierre Ossman 	return ret;
620597007fSPierre Ossman }
630597007fSPierre Ossman 
64e29a7d73SPierre Ossman static int sdio_init_func(struct mmc_card *card, unsigned int fn)
65e29a7d73SPierre Ossman {
660597007fSPierre Ossman 	int ret;
67e29a7d73SPierre Ossman 	struct sdio_func *func;
68e29a7d73SPierre Ossman 
695df0e823SShawn Lin 	if (WARN_ON(fn > SDIO_MAX_FUNCS))
705df0e823SShawn Lin 		return -EINVAL;
71e29a7d73SPierre Ossman 
72e29a7d73SPierre Ossman 	func = sdio_alloc_func(card);
73e29a7d73SPierre Ossman 	if (IS_ERR(func))
74e29a7d73SPierre Ossman 		return PTR_ERR(func);
75e29a7d73SPierre Ossman 
76e29a7d73SPierre Ossman 	func->num = fn;
77e29a7d73SPierre Ossman 
786f51be3dSGrazvydas Ignotas 	if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
790597007fSPierre Ossman 		ret = sdio_read_fbr(func);
800597007fSPierre Ossman 		if (ret)
810597007fSPierre Ossman 			goto fail;
820597007fSPierre Ossman 
831a632f8cSPierre Ossman 		ret = sdio_read_func_cis(func);
84b7261261SNicolas Pitre 		if (ret)
85b7261261SNicolas Pitre 			goto fail;
866f51be3dSGrazvydas Ignotas 	} else {
876f51be3dSGrazvydas Ignotas 		func->vendor = func->card->cis.vendor;
886f51be3dSGrazvydas Ignotas 		func->device = func->card->cis.device;
896f51be3dSGrazvydas Ignotas 		func->max_blksize = func->card->cis.blksize;
906f51be3dSGrazvydas Ignotas 	}
91b7261261SNicolas Pitre 
92e29a7d73SPierre Ossman 	card->sdio_func[fn - 1] = func;
93e29a7d73SPierre Ossman 
94e29a7d73SPierre Ossman 	return 0;
950597007fSPierre Ossman 
960597007fSPierre Ossman fail:
970597007fSPierre Ossman 	/*
980597007fSPierre Ossman 	 * It is okay to remove the function here even though we hold
990597007fSPierre Ossman 	 * the host lock as we haven't registered the device yet.
1000597007fSPierre Ossman 	 */
1010597007fSPierre Ossman 	sdio_remove_func(func);
1020597007fSPierre Ossman 	return ret;
103e29a7d73SPierre Ossman }
104e29a7d73SPierre Ossman 
1052d0d68f5SPhilip Rakity static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
10635c66c19SPierre Ossman {
10735c66c19SPierre Ossman 	int ret;
10835c66c19SPierre Ossman 	int cccr_vsn;
1092d0d68f5SPhilip Rakity 	int uhs = ocr & R4_18V_PRESENT;
11035c66c19SPierre Ossman 	unsigned char data;
111a303c531SPhilip Rakity 	unsigned char speed;
11235c66c19SPierre Ossman 
11335c66c19SPierre Ossman 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
11435c66c19SPierre Ossman 	if (ret)
11535c66c19SPierre Ossman 		goto out;
11635c66c19SPierre Ossman 
11735c66c19SPierre Ossman 	cccr_vsn = data & 0x0f;
11835c66c19SPierre Ossman 
119b4625dabSBing Zhao 	if (cccr_vsn > SDIO_CCCR_REV_3_00) {
120a3c76eb9SGirish K S 		pr_err("%s: unrecognised CCCR structure version %d\n",
12135c66c19SPierre Ossman 			mmc_hostname(card->host), cccr_vsn);
12235c66c19SPierre Ossman 		return -EINVAL;
12335c66c19SPierre Ossman 	}
12435c66c19SPierre Ossman 
12535c66c19SPierre Ossman 	card->cccr.sdio_vsn = (data & 0xf0) >> 4;
12635c66c19SPierre Ossman 
12735c66c19SPierre Ossman 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
12835c66c19SPierre Ossman 	if (ret)
12935c66c19SPierre Ossman 		goto out;
13035c66c19SPierre Ossman 
13135c66c19SPierre Ossman 	if (data & SDIO_CCCR_CAP_SMB)
13235c66c19SPierre Ossman 		card->cccr.multi_block = 1;
13335c66c19SPierre Ossman 	if (data & SDIO_CCCR_CAP_LSC)
13435c66c19SPierre Ossman 		card->cccr.low_speed = 1;
13535c66c19SPierre Ossman 	if (data & SDIO_CCCR_CAP_4BLS)
13635c66c19SPierre Ossman 		card->cccr.wide_bus = 1;
13735c66c19SPierre Ossman 
13835c66c19SPierre Ossman 	if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
13935c66c19SPierre Ossman 		ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
14035c66c19SPierre Ossman 		if (ret)
14135c66c19SPierre Ossman 			goto out;
14235c66c19SPierre Ossman 
14335c66c19SPierre Ossman 		if (data & SDIO_POWER_SMPC)
14435c66c19SPierre Ossman 			card->cccr.high_power = 1;
14535c66c19SPierre Ossman 	}
14635c66c19SPierre Ossman 
14735c66c19SPierre Ossman 	if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
148a303c531SPhilip Rakity 		ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
14935c66c19SPierre Ossman 		if (ret)
15035c66c19SPierre Ossman 			goto out;
15135c66c19SPierre Ossman 
152a303c531SPhilip Rakity 		card->scr.sda_spec3 = 0;
153a303c531SPhilip Rakity 		card->sw_caps.sd3_bus_mode = 0;
154a303c531SPhilip Rakity 		card->sw_caps.sd3_drv_type = 0;
1552d0d68f5SPhilip Rakity 		if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
156a303c531SPhilip Rakity 			card->scr.sda_spec3 = 1;
157a303c531SPhilip Rakity 			ret = mmc_io_rw_direct(card, 0, 0,
158a303c531SPhilip Rakity 				SDIO_CCCR_UHS, 0, &data);
159a303c531SPhilip Rakity 			if (ret)
160a303c531SPhilip Rakity 				goto out;
161a303c531SPhilip Rakity 
16241875e38SSujit Reddy Thumma 			if (mmc_host_uhs(card->host)) {
163a303c531SPhilip Rakity 				if (data & SDIO_UHS_DDR50)
164a303c531SPhilip Rakity 					card->sw_caps.sd3_bus_mode
165a303c531SPhilip Rakity 						|= SD_MODE_UHS_DDR50;
166a303c531SPhilip Rakity 
167a303c531SPhilip Rakity 				if (data & SDIO_UHS_SDR50)
168a303c531SPhilip Rakity 					card->sw_caps.sd3_bus_mode
169a303c531SPhilip Rakity 						|= SD_MODE_UHS_SDR50;
170a303c531SPhilip Rakity 
171a303c531SPhilip Rakity 				if (data & SDIO_UHS_SDR104)
172a303c531SPhilip Rakity 					card->sw_caps.sd3_bus_mode
173a303c531SPhilip Rakity 						|= SD_MODE_UHS_SDR104;
174a303c531SPhilip Rakity 			}
175a303c531SPhilip Rakity 
176a303c531SPhilip Rakity 			ret = mmc_io_rw_direct(card, 0, 0,
177a303c531SPhilip Rakity 				SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
178a303c531SPhilip Rakity 			if (ret)
179a303c531SPhilip Rakity 				goto out;
180a303c531SPhilip Rakity 
181a303c531SPhilip Rakity 			if (data & SDIO_DRIVE_SDTA)
182a303c531SPhilip Rakity 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
183a303c531SPhilip Rakity 			if (data & SDIO_DRIVE_SDTC)
184a303c531SPhilip Rakity 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
185a303c531SPhilip Rakity 			if (data & SDIO_DRIVE_SDTD)
186a303c531SPhilip Rakity 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
187a303c531SPhilip Rakity 		}
188a303c531SPhilip Rakity 
189a303c531SPhilip Rakity 		/* if no uhs mode ensure we check for high speed */
190a303c531SPhilip Rakity 		if (!card->sw_caps.sd3_bus_mode) {
191a303c531SPhilip Rakity 			if (speed & SDIO_SPEED_SHS) {
19235c66c19SPierre Ossman 				card->cccr.high_speed = 1;
193a303c531SPhilip Rakity 				card->sw_caps.hs_max_dtr = 50000000;
194a303c531SPhilip Rakity 			} else {
195a303c531SPhilip Rakity 				card->cccr.high_speed = 0;
196a303c531SPhilip Rakity 				card->sw_caps.hs_max_dtr = 25000000;
197a303c531SPhilip Rakity 			}
198a303c531SPhilip Rakity 		}
19935c66c19SPierre Ossman 	}
20035c66c19SPierre Ossman 
20135c66c19SPierre Ossman out:
20235c66c19SPierre Ossman 	return ret;
20335c66c19SPierre Ossman }
20435c66c19SPierre Ossman 
2054ff6471cSPierre Ossman static int sdio_enable_wide(struct mmc_card *card)
2064ff6471cSPierre Ossman {
2074ff6471cSPierre Ossman 	int ret;
2084ff6471cSPierre Ossman 	u8 ctrl;
2094ff6471cSPierre Ossman 
2104ff6471cSPierre Ossman 	if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
2114ff6471cSPierre Ossman 		return 0;
2124ff6471cSPierre Ossman 
2134ff6471cSPierre Ossman 	if (card->cccr.low_speed && !card->cccr.wide_bus)
2144ff6471cSPierre Ossman 		return 0;
2154ff6471cSPierre Ossman 
2164ff6471cSPierre Ossman 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
2174ff6471cSPierre Ossman 	if (ret)
2184ff6471cSPierre Ossman 		return ret;
2194ff6471cSPierre Ossman 
2202a0fe914SYong Ding 	if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
2216606110dSJoe Perches 		pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
2222a0fe914SYong Ding 			mmc_hostname(card->host), ctrl);
2232a0fe914SYong Ding 
2242a0fe914SYong Ding 	/* set as 4-bit bus width */
2252a0fe914SYong Ding 	ctrl &= ~SDIO_BUS_WIDTH_MASK;
2264ff6471cSPierre Ossman 	ctrl |= SDIO_BUS_WIDTH_4BIT;
2274ff6471cSPierre Ossman 
2284ff6471cSPierre Ossman 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
2294ff6471cSPierre Ossman 	if (ret)
2304ff6471cSPierre Ossman 		return ret;
2314ff6471cSPierre Ossman 
2327310ece8SMichal Miroslaw 	return 1;
2334ff6471cSPierre Ossman }
2344ff6471cSPierre Ossman 
2355c4e6f13SPierre Ossman /*
236006ebd5dSOhad Ben-Cohen  * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
237006ebd5dSOhad Ben-Cohen  * of the card. This may be required on certain setups of boards,
238006ebd5dSOhad Ben-Cohen  * controllers and embedded sdio device which do not need the card's
239006ebd5dSOhad Ben-Cohen  * pull-up. As a result, card detection is disabled and power is saved.
240006ebd5dSOhad Ben-Cohen  */
241006ebd5dSOhad Ben-Cohen static int sdio_disable_cd(struct mmc_card *card)
242006ebd5dSOhad Ben-Cohen {
243006ebd5dSOhad Ben-Cohen 	int ret;
244006ebd5dSOhad Ben-Cohen 	u8 ctrl;
245006ebd5dSOhad Ben-Cohen 
2462059a02dSOhad Ben-Cohen 	if (!mmc_card_disable_cd(card))
247006ebd5dSOhad Ben-Cohen 		return 0;
248006ebd5dSOhad Ben-Cohen 
249006ebd5dSOhad Ben-Cohen 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
250006ebd5dSOhad Ben-Cohen 	if (ret)
251006ebd5dSOhad Ben-Cohen 		return ret;
252006ebd5dSOhad Ben-Cohen 
253006ebd5dSOhad Ben-Cohen 	ctrl |= SDIO_BUS_CD_DISABLE;
254006ebd5dSOhad Ben-Cohen 
255006ebd5dSOhad Ben-Cohen 	return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
256006ebd5dSOhad Ben-Cohen }
257006ebd5dSOhad Ben-Cohen 
258006ebd5dSOhad Ben-Cohen /*
2596b5eda36SDaniel Drake  * Devices that remain active during a system suspend are
2606b5eda36SDaniel Drake  * put back into 1-bit mode.
2616b5eda36SDaniel Drake  */
2626b5eda36SDaniel Drake static int sdio_disable_wide(struct mmc_card *card)
2636b5eda36SDaniel Drake {
2646b5eda36SDaniel Drake 	int ret;
2656b5eda36SDaniel Drake 	u8 ctrl;
2666b5eda36SDaniel Drake 
2676b5eda36SDaniel Drake 	if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
2686b5eda36SDaniel Drake 		return 0;
2696b5eda36SDaniel Drake 
2706b5eda36SDaniel Drake 	if (card->cccr.low_speed && !card->cccr.wide_bus)
2716b5eda36SDaniel Drake 		return 0;
2726b5eda36SDaniel Drake 
2736b5eda36SDaniel Drake 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
2746b5eda36SDaniel Drake 	if (ret)
2756b5eda36SDaniel Drake 		return ret;
2766b5eda36SDaniel Drake 
2776b5eda36SDaniel Drake 	if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
2786b5eda36SDaniel Drake 		return 0;
2796b5eda36SDaniel Drake 
2806b5eda36SDaniel Drake 	ctrl &= ~SDIO_BUS_WIDTH_4BIT;
2816b5eda36SDaniel Drake 	ctrl |= SDIO_BUS_ASYNC_INT;
2826b5eda36SDaniel Drake 
2836b5eda36SDaniel Drake 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
2846b5eda36SDaniel Drake 	if (ret)
2856b5eda36SDaniel Drake 		return ret;
2866b5eda36SDaniel Drake 
2876b5eda36SDaniel Drake 	mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
2886b5eda36SDaniel Drake 
2896b5eda36SDaniel Drake 	return 0;
2906b5eda36SDaniel Drake }
2916b5eda36SDaniel Drake 
2927310ece8SMichal Miroslaw 
2937310ece8SMichal Miroslaw static int sdio_enable_4bit_bus(struct mmc_card *card)
2947310ece8SMichal Miroslaw {
2957310ece8SMichal Miroslaw 	int err;
2967310ece8SMichal Miroslaw 
2977310ece8SMichal Miroslaw 	if (card->type == MMC_TYPE_SDIO)
298303dbedcSNeilBrown 		err = sdio_enable_wide(card);
299303dbedcSNeilBrown 	else if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
3007310ece8SMichal Miroslaw 		 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
3017310ece8SMichal Miroslaw 		err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
3027310ece8SMichal Miroslaw 		if (err)
3037310ece8SMichal Miroslaw 			return err;
3047310ece8SMichal Miroslaw 		err = sdio_enable_wide(card);
3057310ece8SMichal Miroslaw 		if (err <= 0)
3067310ece8SMichal Miroslaw 			mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
307303dbedcSNeilBrown 	} else
308303dbedcSNeilBrown 		return 0;
309303dbedcSNeilBrown 
310303dbedcSNeilBrown 	if (err > 0) {
311303dbedcSNeilBrown 		mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
312303dbedcSNeilBrown 		err = 0;
313303dbedcSNeilBrown 	}
3147310ece8SMichal Miroslaw 
3157310ece8SMichal Miroslaw 	return err;
3167310ece8SMichal Miroslaw }
3177310ece8SMichal Miroslaw 
3187310ece8SMichal Miroslaw 
3196b5eda36SDaniel Drake /*
320d16f5770SPierre Ossman  * Test if the card supports high-speed mode and, if so, switch to it.
321d16f5770SPierre Ossman  */
3227310ece8SMichal Miroslaw static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
323d16f5770SPierre Ossman {
324d16f5770SPierre Ossman 	int ret;
325d16f5770SPierre Ossman 	u8 speed;
326d16f5770SPierre Ossman 
327d16f5770SPierre Ossman 	if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
328d16f5770SPierre Ossman 		return 0;
329d16f5770SPierre Ossman 
330d16f5770SPierre Ossman 	if (!card->cccr.high_speed)
331d16f5770SPierre Ossman 		return 0;
332d16f5770SPierre Ossman 
333d16f5770SPierre Ossman 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
334d16f5770SPierre Ossman 	if (ret)
335d16f5770SPierre Ossman 		return ret;
336d16f5770SPierre Ossman 
3377310ece8SMichal Miroslaw 	if (enable)
338d16f5770SPierre Ossman 		speed |= SDIO_SPEED_EHS;
3397310ece8SMichal Miroslaw 	else
3407310ece8SMichal Miroslaw 		speed &= ~SDIO_SPEED_EHS;
341d16f5770SPierre Ossman 
342d16f5770SPierre Ossman 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
343d16f5770SPierre Ossman 	if (ret)
344d16f5770SPierre Ossman 		return ret;
345d16f5770SPierre Ossman 
34671578a1eSMichal Miroslaw 	return 1;
34771578a1eSMichal Miroslaw }
348d16f5770SPierre Ossman 
3497310ece8SMichal Miroslaw /*
3507310ece8SMichal Miroslaw  * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
3517310ece8SMichal Miroslaw  */
3527310ece8SMichal Miroslaw static int sdio_enable_hs(struct mmc_card *card)
3537310ece8SMichal Miroslaw {
3547310ece8SMichal Miroslaw 	int ret;
3557310ece8SMichal Miroslaw 
3567310ece8SMichal Miroslaw 	ret = mmc_sdio_switch_hs(card, true);
3577310ece8SMichal Miroslaw 	if (ret <= 0 || card->type == MMC_TYPE_SDIO)
3587310ece8SMichal Miroslaw 		return ret;
3597310ece8SMichal Miroslaw 
3607310ece8SMichal Miroslaw 	ret = mmc_sd_switch_hs(card);
3617310ece8SMichal Miroslaw 	if (ret <= 0)
3627310ece8SMichal Miroslaw 		mmc_sdio_switch_hs(card, false);
3637310ece8SMichal Miroslaw 
3647310ece8SMichal Miroslaw 	return ret;
3657310ece8SMichal Miroslaw }
3667310ece8SMichal Miroslaw 
36771578a1eSMichal Miroslaw static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
36871578a1eSMichal Miroslaw {
36971578a1eSMichal Miroslaw 	unsigned max_dtr;
37071578a1eSMichal Miroslaw 
371cdc99179SSeungwon Jeon 	if (mmc_card_hs(card)) {
37271578a1eSMichal Miroslaw 		/*
37371578a1eSMichal Miroslaw 		 * The SDIO specification doesn't mention how
37471578a1eSMichal Miroslaw 		 * the CIS transfer speed register relates to
37571578a1eSMichal Miroslaw 		 * high-speed, but it seems that 50 MHz is
37671578a1eSMichal Miroslaw 		 * mandatory.
37771578a1eSMichal Miroslaw 		 */
37871578a1eSMichal Miroslaw 		max_dtr = 50000000;
37971578a1eSMichal Miroslaw 	} else {
38071578a1eSMichal Miroslaw 		max_dtr = card->cis.max_dtr;
38171578a1eSMichal Miroslaw 	}
38271578a1eSMichal Miroslaw 
3837310ece8SMichal Miroslaw 	if (card->type == MMC_TYPE_SD_COMBO)
3847310ece8SMichal Miroslaw 		max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
3857310ece8SMichal Miroslaw 
38671578a1eSMichal Miroslaw 	return max_dtr;
387d16f5770SPierre Ossman }
388d16f5770SPierre Ossman 
389a303c531SPhilip Rakity static unsigned char host_drive_to_sdio_drive(int host_strength)
390a303c531SPhilip Rakity {
391a303c531SPhilip Rakity 	switch (host_strength) {
392a303c531SPhilip Rakity 	case MMC_SET_DRIVER_TYPE_A:
393a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_A;
394a303c531SPhilip Rakity 	case MMC_SET_DRIVER_TYPE_B:
395a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_B;
396a303c531SPhilip Rakity 	case MMC_SET_DRIVER_TYPE_C:
397a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_C;
398a303c531SPhilip Rakity 	case MMC_SET_DRIVER_TYPE_D:
399a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_D;
400a303c531SPhilip Rakity 	default:
401a303c531SPhilip Rakity 		return SDIO_DTSx_SET_TYPE_B;
402a303c531SPhilip Rakity 	}
403a303c531SPhilip Rakity }
404a303c531SPhilip Rakity 
405a303c531SPhilip Rakity static void sdio_select_driver_type(struct mmc_card *card)
406a303c531SPhilip Rakity {
407fa021cefSAdrian Hunter 	int card_drv_type, drive_strength, drv_type;
408a303c531SPhilip Rakity 	unsigned char card_strength;
409a303c531SPhilip Rakity 	int err;
410a303c531SPhilip Rakity 
4113853a042SAdrian Hunter 	card->drive_strength = 0;
4123853a042SAdrian Hunter 
413fa021cefSAdrian Hunter 	card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
414a303c531SPhilip Rakity 
415e23350b3SAdrian Hunter 	drive_strength = mmc_select_drive_strength(card,
416a303c531SPhilip Rakity 						   card->sw_caps.uhs_max_dtr,
417e23350b3SAdrian Hunter 						   card_drv_type, &drv_type);
418a303c531SPhilip Rakity 
419b4f30a17SAdrian Hunter 	if (drive_strength) {
420a303c531SPhilip Rakity 		/* if error just use default for drive strength B */
421a303c531SPhilip Rakity 		err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
422a303c531SPhilip Rakity 				       &card_strength);
423a303c531SPhilip Rakity 		if (err)
424a303c531SPhilip Rakity 			return;
425a303c531SPhilip Rakity 
426a303c531SPhilip Rakity 		card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
427a303c531SPhilip Rakity 		card_strength |= host_drive_to_sdio_drive(drive_strength);
428a303c531SPhilip Rakity 
429b4f30a17SAdrian Hunter 		/* if error default to drive strength B */
430a303c531SPhilip Rakity 		err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
431a303c531SPhilip Rakity 				       card_strength, NULL);
432b4f30a17SAdrian Hunter 		if (err)
433b4f30a17SAdrian Hunter 			return;
4343853a042SAdrian Hunter 		card->drive_strength = drive_strength;
435b4f30a17SAdrian Hunter 	}
436a303c531SPhilip Rakity 
437b4f30a17SAdrian Hunter 	if (drv_type)
438b4f30a17SAdrian Hunter 		mmc_set_driver_type(card->host, drv_type);
439a303c531SPhilip Rakity }
440a303c531SPhilip Rakity 
441a303c531SPhilip Rakity 
442a303c531SPhilip Rakity static int sdio_set_bus_speed_mode(struct mmc_card *card)
443a303c531SPhilip Rakity {
444a303c531SPhilip Rakity 	unsigned int bus_speed, timing;
445a303c531SPhilip Rakity 	int err;
446a303c531SPhilip Rakity 	unsigned char speed;
447a303c531SPhilip Rakity 
448a303c531SPhilip Rakity 	/*
449a303c531SPhilip Rakity 	 * If the host doesn't support any of the UHS-I modes, fallback on
450a303c531SPhilip Rakity 	 * default speed.
451a303c531SPhilip Rakity 	 */
45241875e38SSujit Reddy Thumma 	if (!mmc_host_uhs(card->host))
453a303c531SPhilip Rakity 		return 0;
454a303c531SPhilip Rakity 
455a303c531SPhilip Rakity 	bus_speed = SDIO_SPEED_SDR12;
456a303c531SPhilip Rakity 	timing = MMC_TIMING_UHS_SDR12;
457a303c531SPhilip Rakity 	if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
458a303c531SPhilip Rakity 	    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
459a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_SDR104;
460a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_SDR104;
461a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
46277e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
463a303c531SPhilip Rakity 	} else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
464a303c531SPhilip Rakity 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
465a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_DDR50;
466a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_DDR50;
467a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
46877e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
469a303c531SPhilip Rakity 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
470a303c531SPhilip Rakity 		    MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
471a303c531SPhilip Rakity 		    SD_MODE_UHS_SDR50)) {
472a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_SDR50;
473a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_SDR50;
474a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
47577e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
476a303c531SPhilip Rakity 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
477a303c531SPhilip Rakity 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
478a303c531SPhilip Rakity 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
479a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_SDR25;
480a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_SDR25;
481a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
48277e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
483a303c531SPhilip Rakity 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
484a303c531SPhilip Rakity 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
485a303c531SPhilip Rakity 		    MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
486a303c531SPhilip Rakity 		    SD_MODE_UHS_SDR12)) {
487a303c531SPhilip Rakity 			bus_speed = SDIO_SPEED_SDR12;
488a303c531SPhilip Rakity 			timing = MMC_TIMING_UHS_SDR12;
489a303c531SPhilip Rakity 			card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
49077e2ff08SSubhash Jadavani 			card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
491a303c531SPhilip Rakity 	}
492a303c531SPhilip Rakity 
493a303c531SPhilip Rakity 	err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
494a303c531SPhilip Rakity 	if (err)
495a303c531SPhilip Rakity 		return err;
496a303c531SPhilip Rakity 
497a303c531SPhilip Rakity 	speed &= ~SDIO_SPEED_BSS_MASK;
498a303c531SPhilip Rakity 	speed |= bus_speed;
499a303c531SPhilip Rakity 	err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
500a303c531SPhilip Rakity 	if (err)
501a303c531SPhilip Rakity 		return err;
502a303c531SPhilip Rakity 
503a303c531SPhilip Rakity 	if (bus_speed) {
504a303c531SPhilip Rakity 		mmc_set_timing(card->host, timing);
505a303c531SPhilip Rakity 		mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
506a303c531SPhilip Rakity 	}
507a303c531SPhilip Rakity 
508a303c531SPhilip Rakity 	return 0;
509a303c531SPhilip Rakity }
510a303c531SPhilip Rakity 
511a303c531SPhilip Rakity /*
512a303c531SPhilip Rakity  * UHS-I specific initialization procedure
513a303c531SPhilip Rakity  */
514a303c531SPhilip Rakity static int mmc_sdio_init_uhs_card(struct mmc_card *card)
515a303c531SPhilip Rakity {
516a303c531SPhilip Rakity 	int err;
517a303c531SPhilip Rakity 
518a303c531SPhilip Rakity 	if (!card->scr.sda_spec3)
519a303c531SPhilip Rakity 		return 0;
520a303c531SPhilip Rakity 
521a303c531SPhilip Rakity 	/*
522a303c531SPhilip Rakity 	 * Switch to wider bus (if supported).
523a303c531SPhilip Rakity 	 */
524303dbedcSNeilBrown 	if (card->host->caps & MMC_CAP_4_BIT_DATA)
525a303c531SPhilip Rakity 		err = sdio_enable_4bit_bus(card);
526a303c531SPhilip Rakity 
527a303c531SPhilip Rakity 	/* Set the driver strength for the card */
528a303c531SPhilip Rakity 	sdio_select_driver_type(card);
529a303c531SPhilip Rakity 
530a303c531SPhilip Rakity 	/* Set bus speed mode of the card */
531a303c531SPhilip Rakity 	err = sdio_set_bus_speed_mode(card);
532a303c531SPhilip Rakity 	if (err)
533a303c531SPhilip Rakity 		goto out;
534a303c531SPhilip Rakity 
535810e08eeSFredrik Soderstedt 	/*
536810e08eeSFredrik Soderstedt 	 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
537810e08eeSFredrik Soderstedt 	 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
538810e08eeSFredrik Soderstedt 	 */
53963e415c6SAdrian Hunter 	if (!mmc_host_is_spi(card->host) &&
540e10c3219SCarlo Caione 	    ((card->host->ios.timing == MMC_TIMING_UHS_SDR50) ||
541e10c3219SCarlo Caione 	      (card->host->ios.timing == MMC_TIMING_UHS_SDR104)))
54263e415c6SAdrian Hunter 		err = mmc_execute_tuning(card);
543a303c531SPhilip Rakity out:
544a303c531SPhilip Rakity 	return err;
545a303c531SPhilip Rakity }
546a303c531SPhilip Rakity 
5470eb51a58SShawn Lin static void mmc_sdio_resend_if_cond(struct mmc_host *host,
5480eb51a58SShawn Lin 				    struct mmc_card *card)
5490eb51a58SShawn Lin {
5500eb51a58SShawn Lin 	sdio_reset(host);
5510eb51a58SShawn Lin 	mmc_go_idle(host);
5520eb51a58SShawn Lin 	mmc_send_if_cond(host, host->ocr_avail);
5530eb51a58SShawn Lin 	mmc_remove_card(card);
5540eb51a58SShawn Lin }
5550eb51a58SShawn Lin 
556d16f5770SPierre Ossman /*
55717d33e14SNicolas Pitre  * Handle the detection and initialisation of a card.
55817d33e14SNicolas Pitre  *
55917d33e14SNicolas Pitre  * In the case of a resume, "oldcard" will contain the card
56017d33e14SNicolas Pitre  * we're trying to reinitialise.
56117d33e14SNicolas Pitre  */
56217d33e14SNicolas Pitre static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
5633bca4cf7SChris Ball 			      struct mmc_card *oldcard, int powered_resume)
56417d33e14SNicolas Pitre {
56517d33e14SNicolas Pitre 	struct mmc_card *card;
56617d33e14SNicolas Pitre 	int err;
5670797e5f1SJohan Rudholm 	int retries = 10;
568db4a0d05SUlf Hansson 	u32 rocr = 0;
56969041150SUlf Hansson 	u32 ocr_card = ocr;
57017d33e14SNicolas Pitre 
57117d33e14SNicolas Pitre 	WARN_ON(!host->claimed);
57217d33e14SNicolas Pitre 
573db4a0d05SUlf Hansson 	/* to query card if 1.8V signalling is supported */
574db4a0d05SUlf Hansson 	if (mmc_host_uhs(host))
575db4a0d05SUlf Hansson 		ocr |= R4_18V_PRESENT;
576db4a0d05SUlf Hansson 
5770797e5f1SJohan Rudholm try_again:
5780797e5f1SJohan Rudholm 	if (!retries) {
5796606110dSJoe Perches 		pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
5800797e5f1SJohan Rudholm 		ocr &= ~R4_18V_PRESENT;
5810797e5f1SJohan Rudholm 	}
5820797e5f1SJohan Rudholm 
58317d33e14SNicolas Pitre 	/*
58417d33e14SNicolas Pitre 	 * Inform the card of the voltage
58517d33e14SNicolas Pitre 	 */
5863bca4cf7SChris Ball 	if (!powered_resume) {
587db4a0d05SUlf Hansson 		err = mmc_send_io_op_cond(host, ocr, &rocr);
58817d33e14SNicolas Pitre 		if (err)
58917d33e14SNicolas Pitre 			goto err;
5903bca4cf7SChris Ball 	}
59117d33e14SNicolas Pitre 
59217d33e14SNicolas Pitre 	/*
59317d33e14SNicolas Pitre 	 * For SPI, enable CRC as appropriate.
59417d33e14SNicolas Pitre 	 */
59517d33e14SNicolas Pitre 	if (mmc_host_is_spi(host)) {
59617d33e14SNicolas Pitre 		err = mmc_spi_set_crc(host, use_spi_crc);
59717d33e14SNicolas Pitre 		if (err)
59817d33e14SNicolas Pitre 			goto err;
59917d33e14SNicolas Pitre 	}
60017d33e14SNicolas Pitre 
60117d33e14SNicolas Pitre 	/*
60217d33e14SNicolas Pitre 	 * Allocate card structure.
60317d33e14SNicolas Pitre 	 */
60417d33e14SNicolas Pitre 	card = mmc_alloc_card(host, NULL);
60517d33e14SNicolas Pitre 	if (IS_ERR(card)) {
60617d33e14SNicolas Pitre 		err = PTR_ERR(card);
60717d33e14SNicolas Pitre 		goto err;
60817d33e14SNicolas Pitre 	}
60917d33e14SNicolas Pitre 
610db4a0d05SUlf Hansson 	if ((rocr & R4_MEMORY_PRESENT) &&
611db4a0d05SUlf Hansson 	    mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
6127310ece8SMichal Miroslaw 		card->type = MMC_TYPE_SD_COMBO;
6137310ece8SMichal Miroslaw 
6147310ece8SMichal Miroslaw 		if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
6157310ece8SMichal Miroslaw 		    memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
6167310ece8SMichal Miroslaw 			mmc_remove_card(card);
6177310ece8SMichal Miroslaw 			return -ENOENT;
6187310ece8SMichal Miroslaw 		}
6197310ece8SMichal Miroslaw 	} else {
62017d33e14SNicolas Pitre 		card->type = MMC_TYPE_SDIO;
62117d33e14SNicolas Pitre 
6227310ece8SMichal Miroslaw 		if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
6237310ece8SMichal Miroslaw 			mmc_remove_card(card);
6247310ece8SMichal Miroslaw 			return -ENOENT;
6257310ece8SMichal Miroslaw 		}
6267310ece8SMichal Miroslaw 	}
6277310ece8SMichal Miroslaw 
62817d33e14SNicolas Pitre 	/*
6293fcb027dSDaniel Mack 	 * Call the optional HC's init_card function to handle quirks.
6303fcb027dSDaniel Mack 	 */
6313fcb027dSDaniel Mack 	if (host->ops->init_card)
6323fcb027dSDaniel Mack 		host->ops->init_card(host, card);
6333fcb027dSDaniel Mack 
6343fcb027dSDaniel Mack 	/*
635a303c531SPhilip Rakity 	 * If the host and card support UHS-I mode request the card
636a303c531SPhilip Rakity 	 * to switch to 1.8V signaling level.  No 1.8v signalling if
6377122bbb0SMasanari Iida 	 * UHS mode is not enabled to maintain compatibility and some
638a303c531SPhilip Rakity 	 * systems that claim 1.8v signalling in fact do not support
6395fc3d80eSShawn Lin 	 * it. Per SDIO spec v3, section 3.1.2, if the voltage is already
6405fc3d80eSShawn Lin 	 * 1.8v, the card sets S18A to 0 in the R4 response. So it will
6415fc3d80eSShawn Lin 	 * fails to check rocr & R4_18V_PRESENT,  but we still need to
6425fc3d80eSShawn Lin 	 * try to init uhs card. sdio_read_cccr will take over this task
6435fc3d80eSShawn Lin 	 * to make sure which speed mode should work.
644a303c531SPhilip Rakity 	 */
645db4a0d05SUlf Hansson 	if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
6462ed573b6SUlf Hansson 		err = mmc_set_uhs_voltage(host, ocr_card);
6470797e5f1SJohan Rudholm 		if (err == -EAGAIN) {
6480eb51a58SShawn Lin 			mmc_sdio_resend_if_cond(host, card);
6490797e5f1SJohan Rudholm 			retries--;
6500797e5f1SJohan Rudholm 			goto try_again;
6510797e5f1SJohan Rudholm 		} else if (err) {
652a303c531SPhilip Rakity 			ocr &= ~R4_18V_PRESENT;
653a303c531SPhilip Rakity 		}
654a303c531SPhilip Rakity 	}
655a303c531SPhilip Rakity 
656a303c531SPhilip Rakity 	/*
65717d33e14SNicolas Pitre 	 * For native busses:  set card RCA and quit open drain mode.
65817d33e14SNicolas Pitre 	 */
6593bca4cf7SChris Ball 	if (!powered_resume && !mmc_host_is_spi(host)) {
66017d33e14SNicolas Pitre 		err = mmc_send_relative_addr(host, &card->rca);
66117d33e14SNicolas Pitre 		if (err)
66217d33e14SNicolas Pitre 			goto remove;
66317d33e14SNicolas Pitre 
6640aab3995SStefan Nilsson XK 		/*
6650aab3995SStefan Nilsson XK 		 * Update oldcard with the new RCA received from the SDIO
6660aab3995SStefan Nilsson XK 		 * device -- we're doing this so that it's updated in the
6670aab3995SStefan Nilsson XK 		 * "card" struct when oldcard overwrites that later.
6680aab3995SStefan Nilsson XK 		 */
6690aab3995SStefan Nilsson XK 		if (oldcard)
6700aab3995SStefan Nilsson XK 			oldcard->rca = card->rca;
67117d33e14SNicolas Pitre 	}
67217d33e14SNicolas Pitre 
67317d33e14SNicolas Pitre 	/*
6747310ece8SMichal Miroslaw 	 * Read CSD, before selecting the card
6757310ece8SMichal Miroslaw 	 */
6767310ece8SMichal Miroslaw 	if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
6777310ece8SMichal Miroslaw 		err = mmc_sd_get_csd(host, card);
6787310ece8SMichal Miroslaw 		if (err)
6797310ece8SMichal Miroslaw 			return err;
6807310ece8SMichal Miroslaw 
6817310ece8SMichal Miroslaw 		mmc_decode_cid(card);
6827310ece8SMichal Miroslaw 	}
6837310ece8SMichal Miroslaw 
6847310ece8SMichal Miroslaw 	/*
68517d33e14SNicolas Pitre 	 * Select card, as all following commands rely on that.
68617d33e14SNicolas Pitre 	 */
6873bca4cf7SChris Ball 	if (!powered_resume && !mmc_host_is_spi(host)) {
68817d33e14SNicolas Pitre 		err = mmc_select_card(card);
68917d33e14SNicolas Pitre 		if (err)
69017d33e14SNicolas Pitre 			goto remove;
69117d33e14SNicolas Pitre 	}
69217d33e14SNicolas Pitre 
6936f51be3dSGrazvydas Ignotas 	if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
6946f51be3dSGrazvydas Ignotas 		/*
6956f51be3dSGrazvydas Ignotas 		 * This is non-standard SDIO device, meaning it doesn't
6966f51be3dSGrazvydas Ignotas 		 * have any CIA (Common I/O area) registers present.
6976f51be3dSGrazvydas Ignotas 		 * It's host's responsibility to fill cccr and cis
6986f51be3dSGrazvydas Ignotas 		 * structures in init_card().
6996f51be3dSGrazvydas Ignotas 		 */
7006f51be3dSGrazvydas Ignotas 		mmc_set_clock(host, card->cis.max_dtr);
7016f51be3dSGrazvydas Ignotas 
7026f51be3dSGrazvydas Ignotas 		if (card->cccr.high_speed) {
7036f51be3dSGrazvydas Ignotas 			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
7046f51be3dSGrazvydas Ignotas 		}
7056f51be3dSGrazvydas Ignotas 
7066f51be3dSGrazvydas Ignotas 		goto finish;
7076f51be3dSGrazvydas Ignotas 	}
7086f51be3dSGrazvydas Ignotas 
70917d33e14SNicolas Pitre 	/*
7105fc3d80eSShawn Lin 	 * Read the common registers. Note that we should try to
7115fc3d80eSShawn Lin 	 * validate whether UHS would work or not.
71217d33e14SNicolas Pitre 	 */
7132d0d68f5SPhilip Rakity 	err = sdio_read_cccr(card, ocr);
7145fc3d80eSShawn Lin 	if (err) {
7155fc3d80eSShawn Lin 		mmc_sdio_resend_if_cond(host, card);
7165fc3d80eSShawn Lin 		if (ocr & R4_18V_PRESENT) {
7175fc3d80eSShawn Lin 			/* Retry init sequence, but without R4_18V_PRESENT. */
7185fc3d80eSShawn Lin 			retries = 0;
7195fc3d80eSShawn Lin 			goto try_again;
7205fc3d80eSShawn Lin 		} else {
72117d33e14SNicolas Pitre 			goto remove;
7225fc3d80eSShawn Lin 		}
7235fc3d80eSShawn Lin 	}
72417d33e14SNicolas Pitre 
72517d33e14SNicolas Pitre 	/*
72617d33e14SNicolas Pitre 	 * Read the common CIS tuples.
72717d33e14SNicolas Pitre 	 */
72817d33e14SNicolas Pitre 	err = sdio_read_common_cis(card);
72917d33e14SNicolas Pitre 	if (err)
73017d33e14SNicolas Pitre 		goto remove;
73117d33e14SNicolas Pitre 
73217d33e14SNicolas Pitre 	if (oldcard) {
73317d33e14SNicolas Pitre 		int same = (card->cis.vendor == oldcard->cis.vendor &&
73417d33e14SNicolas Pitre 			    card->cis.device == oldcard->cis.device);
73517d33e14SNicolas Pitre 		mmc_remove_card(card);
7367310ece8SMichal Miroslaw 		if (!same)
7377310ece8SMichal Miroslaw 			return -ENOENT;
7387310ece8SMichal Miroslaw 
73917d33e14SNicolas Pitre 		card = oldcard;
74017d33e14SNicolas Pitre 	}
74169041150SUlf Hansson 	card->ocr = ocr_card;
742*1144c1e4SShawn Lin 	mmc_fixup_device(card, sdio_fixup_methods);
74317d33e14SNicolas Pitre 
7447310ece8SMichal Miroslaw 	if (card->type == MMC_TYPE_SD_COMBO) {
7457310ece8SMichal Miroslaw 		err = mmc_sd_setup_card(host, card, oldcard != NULL);
7467310ece8SMichal Miroslaw 		/* handle as SDIO-only card if memory init failed */
7477310ece8SMichal Miroslaw 		if (err) {
7487310ece8SMichal Miroslaw 			mmc_go_idle(host);
7497310ece8SMichal Miroslaw 			if (mmc_host_is_spi(host))
7507310ece8SMichal Miroslaw 				/* should not fail, as it worked previously */
7517310ece8SMichal Miroslaw 				mmc_spi_set_crc(host, use_spi_crc);
7527310ece8SMichal Miroslaw 			card->type = MMC_TYPE_SDIO;
7537310ece8SMichal Miroslaw 		} else
7547310ece8SMichal Miroslaw 			card->dev.type = &sd_type;
7557310ece8SMichal Miroslaw 	}
7567310ece8SMichal Miroslaw 
7577310ece8SMichal Miroslaw 	/*
7587310ece8SMichal Miroslaw 	 * If needed, disconnect card detection pull-up resistor.
7597310ece8SMichal Miroslaw 	 */
7607310ece8SMichal Miroslaw 	err = sdio_disable_cd(card);
7617310ece8SMichal Miroslaw 	if (err)
7627310ece8SMichal Miroslaw 		goto remove;
7637310ece8SMichal Miroslaw 
764a303c531SPhilip Rakity 	/* Initialization sequence for UHS-I cards */
765a303c531SPhilip Rakity 	/* Only if card supports 1.8v and UHS signaling */
766a303c531SPhilip Rakity 	if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
767a303c531SPhilip Rakity 		err = mmc_sdio_init_uhs_card(card);
768a303c531SPhilip Rakity 		if (err)
769a303c531SPhilip Rakity 			goto remove;
770a303c531SPhilip Rakity 	} else {
77117d33e14SNicolas Pitre 		/*
77217d33e14SNicolas Pitre 		 * Switch to high-speed (if supported).
77317d33e14SNicolas Pitre 		 */
77417d33e14SNicolas Pitre 		err = sdio_enable_hs(card);
77571578a1eSMichal Miroslaw 		if (err > 0)
776cdc99179SSeungwon Jeon 			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
77771578a1eSMichal Miroslaw 		else if (err)
77817d33e14SNicolas Pitre 			goto remove;
77917d33e14SNicolas Pitre 
78017d33e14SNicolas Pitre 		/*
78117d33e14SNicolas Pitre 		 * Change to the card's maximum speed.
78217d33e14SNicolas Pitre 		 */
78371578a1eSMichal Miroslaw 		mmc_set_clock(host, mmc_sdio_get_max_clock(card));
78417d33e14SNicolas Pitre 
78517d33e14SNicolas Pitre 		/*
78617d33e14SNicolas Pitre 		 * Switch to wider bus (if supported).
78717d33e14SNicolas Pitre 		 */
7887310ece8SMichal Miroslaw 		err = sdio_enable_4bit_bus(card);
789303dbedcSNeilBrown 		if (err)
79017d33e14SNicolas Pitre 			goto remove;
791a303c531SPhilip Rakity 	}
7926f51be3dSGrazvydas Ignotas finish:
79317d33e14SNicolas Pitre 	if (!oldcard)
79417d33e14SNicolas Pitre 		host->card = card;
79517d33e14SNicolas Pitre 	return 0;
79617d33e14SNicolas Pitre 
79717d33e14SNicolas Pitre remove:
79817d33e14SNicolas Pitre 	if (!oldcard)
79917d33e14SNicolas Pitre 		mmc_remove_card(card);
80017d33e14SNicolas Pitre 
80117d33e14SNicolas Pitre err:
80217d33e14SNicolas Pitre 	return err;
80317d33e14SNicolas Pitre }
80417d33e14SNicolas Pitre 
80517d33e14SNicolas Pitre /*
8065c4e6f13SPierre Ossman  * Host is being removed. Free up the current card.
8075c4e6f13SPierre Ossman  */
8085c4e6f13SPierre Ossman static void mmc_sdio_remove(struct mmc_host *host)
8095c4e6f13SPierre Ossman {
810e29a7d73SPierre Ossman 	int i;
811e29a7d73SPierre Ossman 
812e29a7d73SPierre Ossman 	for (i = 0;i < host->card->sdio_funcs;i++) {
813e29a7d73SPierre Ossman 		if (host->card->sdio_func[i]) {
814e29a7d73SPierre Ossman 			sdio_remove_func(host->card->sdio_func[i]);
815e29a7d73SPierre Ossman 			host->card->sdio_func[i] = NULL;
816e29a7d73SPierre Ossman 		}
817e29a7d73SPierre Ossman 	}
818e29a7d73SPierre Ossman 
8195c4e6f13SPierre Ossman 	mmc_remove_card(host->card);
8205c4e6f13SPierre Ossman 	host->card = NULL;
8215c4e6f13SPierre Ossman }
8225c4e6f13SPierre Ossman 
8235c4e6f13SPierre Ossman /*
824d3049504SAdrian Hunter  * Card detection - card is alive.
825d3049504SAdrian Hunter  */
826d3049504SAdrian Hunter static int mmc_sdio_alive(struct mmc_host *host)
827d3049504SAdrian Hunter {
828d3049504SAdrian Hunter 	return mmc_select_card(host->card);
829d3049504SAdrian Hunter }
830d3049504SAdrian Hunter 
831d3049504SAdrian Hunter /*
8325c4e6f13SPierre Ossman  * Card detection callback from host.
8335c4e6f13SPierre Ossman  */
8345c4e6f13SPierre Ossman static void mmc_sdio_detect(struct mmc_host *host)
8355c4e6f13SPierre Ossman {
8365c4e6f13SPierre Ossman 	int err;
8375c4e6f13SPierre Ossman 
83887973ba2SOhad Ben-Cohen 	/* Make sure card is powered before detecting it */
839ed919b01SOhad Ben-Cohen 	if (host->caps & MMC_CAP_POWER_OFF_CARD) {
84087973ba2SOhad Ben-Cohen 		err = pm_runtime_get_sync(&host->card->dev);
8413bffb800SLi Fei 		if (err < 0) {
8423bffb800SLi Fei 			pm_runtime_put_noidle(&host->card->dev);
84387973ba2SOhad Ben-Cohen 			goto out;
844ed919b01SOhad Ben-Cohen 		}
8453bffb800SLi Fei 	}
84687973ba2SOhad Ben-Cohen 
8475c4e6f13SPierre Ossman 	mmc_claim_host(host);
8485c4e6f13SPierre Ossman 
8495c4e6f13SPierre Ossman 	/*
8505c4e6f13SPierre Ossman 	 * Just check if our card has been removed.
8515c4e6f13SPierre Ossman 	 */
852d3049504SAdrian Hunter 	err = _mmc_detect_card_removed(host);
8535c4e6f13SPierre Ossman 
8545c4e6f13SPierre Ossman 	mmc_release_host(host);
8555c4e6f13SPierre Ossman 
8564d0812c3SOhad Ben-Cohen 	/*
8574d0812c3SOhad Ben-Cohen 	 * Tell PM core it's OK to power off the card now.
8584d0812c3SOhad Ben-Cohen 	 *
8594d0812c3SOhad Ben-Cohen 	 * The _sync variant is used in order to ensure that the card
8604d0812c3SOhad Ben-Cohen 	 * is left powered off in case an error occurred, and the card
8614d0812c3SOhad Ben-Cohen 	 * is going to be removed.
8624d0812c3SOhad Ben-Cohen 	 *
8634d0812c3SOhad Ben-Cohen 	 * Since there is no specific reason to believe a new user
8644d0812c3SOhad Ben-Cohen 	 * is about to show up at this point, the _sync variant is
8654d0812c3SOhad Ben-Cohen 	 * desirable anyway.
8664d0812c3SOhad Ben-Cohen 	 */
867ed919b01SOhad Ben-Cohen 	if (host->caps & MMC_CAP_POWER_OFF_CARD)
8684d0812c3SOhad Ben-Cohen 		pm_runtime_put_sync(&host->card->dev);
8694d0812c3SOhad Ben-Cohen 
87087973ba2SOhad Ben-Cohen out:
8715c4e6f13SPierre Ossman 	if (err) {
8725c4e6f13SPierre Ossman 		mmc_sdio_remove(host);
8735c4e6f13SPierre Ossman 
8745c4e6f13SPierre Ossman 		mmc_claim_host(host);
8755c4e6f13SPierre Ossman 		mmc_detach_bus(host);
8767f7e4129SUlf Hansson 		mmc_power_off(host);
8775c4e6f13SPierre Ossman 		mmc_release_host(host);
8785c4e6f13SPierre Ossman 	}
8795c4e6f13SPierre Ossman }
8805c4e6f13SPierre Ossman 
88117d33e14SNicolas Pitre /*
882810caddbSUlf Hansson  * SDIO pre_suspend.  We need to suspend all functions separately.
88317d33e14SNicolas Pitre  * Therefore all registered functions must have drivers with suspend
88417d33e14SNicolas Pitre  * and resume methods.  Failing that we simply remove the whole card.
88517d33e14SNicolas Pitre  */
886810caddbSUlf Hansson static int mmc_sdio_pre_suspend(struct mmc_host *host)
88717d33e14SNicolas Pitre {
88895cdfb72SNicolas Pitre 	int i, err = 0;
88917d33e14SNicolas Pitre 
89017d33e14SNicolas Pitre 	for (i = 0; i < host->card->sdio_funcs; i++) {
89117d33e14SNicolas Pitre 		struct sdio_func *func = host->card->sdio_func[i];
89217d33e14SNicolas Pitre 		if (func && sdio_func_present(func) && func->dev.driver) {
89317d33e14SNicolas Pitre 			const struct dev_pm_ops *pmops = func->dev.driver->pm;
89417d33e14SNicolas Pitre 			if (!pmops || !pmops->suspend || !pmops->resume) {
89595cdfb72SNicolas Pitre 				/* force removal of entire card in that case */
89695cdfb72SNicolas Pitre 				err = -ENOSYS;
897810caddbSUlf Hansson 				break;
898810caddbSUlf Hansson 			}
899810caddbSUlf Hansson 		}
900810caddbSUlf Hansson 	}
901810caddbSUlf Hansson 
902810caddbSUlf Hansson 	return err;
903810caddbSUlf Hansson }
904810caddbSUlf Hansson 
905810caddbSUlf Hansson /*
906810caddbSUlf Hansson  * SDIO suspend.  Suspend all functions separately.
907810caddbSUlf Hansson  */
908810caddbSUlf Hansson static int mmc_sdio_suspend(struct mmc_host *host)
909810caddbSUlf Hansson {
9106b5eda36SDaniel Drake 	mmc_claim_host(host);
9118d1ffc8cSUlf Hansson 
9128d1ffc8cSUlf Hansson 	if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
9136b5eda36SDaniel Drake 		sdio_disable_wide(host->card);
9146b5eda36SDaniel Drake 
91579d5a65aSAdrian Hunter 	if (!mmc_card_keep_power(host)) {
91674590263SUlf Hansson 		mmc_power_off(host);
91779d5a65aSAdrian Hunter 	} else if (host->retune_period) {
91879d5a65aSAdrian Hunter 		mmc_retune_timer_stop(host);
91979d5a65aSAdrian Hunter 		mmc_retune_needed(host);
92079d5a65aSAdrian Hunter 	}
92174590263SUlf Hansson 
9228d1ffc8cSUlf Hansson 	mmc_release_host(host);
9238d1ffc8cSUlf Hansson 
924573185ccSUlf Hansson 	return 0;
92595cdfb72SNicolas Pitre }
92695cdfb72SNicolas Pitre 
92795cdfb72SNicolas Pitre static int mmc_sdio_resume(struct mmc_host *host)
92895cdfb72SNicolas Pitre {
929573185ccSUlf Hansson 	int err = 0;
93095cdfb72SNicolas Pitre 
93195cdfb72SNicolas Pitre 	/* Basic card reinitialization. */
93295cdfb72SNicolas Pitre 	mmc_claim_host(host);
933080bc977SOhad Ben-Cohen 
93474590263SUlf Hansson 	/* Restore power if needed */
93574590263SUlf Hansson 	if (!mmc_card_keep_power(host)) {
93669041150SUlf Hansson 		mmc_power_up(host, host->card->ocr);
93774590263SUlf Hansson 		/*
93874590263SUlf Hansson 		 * Tell runtime PM core we just powered up the card,
93974590263SUlf Hansson 		 * since it still believes the card is powered off.
94074590263SUlf Hansson 		 * Note that currently runtime PM is only enabled
94174590263SUlf Hansson 		 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
94274590263SUlf Hansson 		 */
94374590263SUlf Hansson 		if (host->caps & MMC_CAP_POWER_OFF_CARD) {
94474590263SUlf Hansson 			pm_runtime_disable(&host->card->dev);
94574590263SUlf Hansson 			pm_runtime_set_active(&host->card->dev);
94674590263SUlf Hansson 			pm_runtime_enable(&host->card->dev);
94774590263SUlf Hansson 		}
94874590263SUlf Hansson 	}
94974590263SUlf Hansson 
950080bc977SOhad Ben-Cohen 	/* No need to reinitialize powered-resumed nonremovable cards */
95151aa66a5SSubhash Jadavani 	if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
95251aa66a5SSubhash Jadavani 		sdio_reset(host);
95351aa66a5SSubhash Jadavani 		mmc_go_idle(host);
954006fc51cSNeilBrown 		mmc_send_if_cond(host, host->card->ocr);
955006fc51cSNeilBrown 		err = mmc_send_io_op_cond(host, 0, NULL);
956006fc51cSNeilBrown 		if (!err)
957006fc51cSNeilBrown 			err = mmc_sdio_init_card(host, host->card->ocr,
958006fc51cSNeilBrown 						 host->card,
959a5e9425dSOhad Ben-Cohen 						 mmc_card_keep_power(host));
96051aa66a5SSubhash Jadavani 	} else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
961080bc977SOhad Ben-Cohen 		/* We may have switched to 1-bit mode during suspend */
962080bc977SOhad Ben-Cohen 		err = sdio_enable_4bit_bus(host->card);
963080bc977SOhad Ben-Cohen 	}
964080bc977SOhad Ben-Cohen 
965dea67c4eSFu Zhonghui 	if (!err && host->sdio_irqs) {
9669eadcc05SUlf Hansson 		if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
967bbbc4c4dSNicolas Pitre 			wake_up_process(host->sdio_irq_thread);
9689eadcc05SUlf Hansson 		else if (host->caps & MMC_CAP_SDIO_IRQ)
969dea67c4eSFu Zhonghui 			host->ops->enable_sdio_irq(host, 1);
970dea67c4eSFu Zhonghui 	}
971dea67c4eSFu Zhonghui 
97295cdfb72SNicolas Pitre 	mmc_release_host(host);
97395cdfb72SNicolas Pitre 
97474590263SUlf Hansson 	host->pm_flags &= ~MMC_PM_KEEP_POWER;
97595cdfb72SNicolas Pitre 	return err;
97617d33e14SNicolas Pitre }
9775c4e6f13SPierre Ossman 
978d3fe37b1SOhad Ben-Cohen static int mmc_sdio_power_restore(struct mmc_host *host)
979d3fe37b1SOhad Ben-Cohen {
980d3fe37b1SOhad Ben-Cohen 	int ret;
981d3fe37b1SOhad Ben-Cohen 
982d3fe37b1SOhad Ben-Cohen 	mmc_claim_host(host);
983c6e633adSDaniel Drake 
984c6e633adSDaniel Drake 	/*
985c6e633adSDaniel Drake 	 * Reset the card by performing the same steps that are taken by
986c6e633adSDaniel Drake 	 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
987c6e633adSDaniel Drake 	 *
988c6e633adSDaniel Drake 	 * sdio_reset() is technically not needed. Having just powered up the
989c6e633adSDaniel Drake 	 * hardware, it should already be in reset state. However, some
990c6e633adSDaniel Drake 	 * platforms (such as SD8686 on OLPC) do not instantly cut power,
991c6e633adSDaniel Drake 	 * meaning that a reset is required when restoring power soon after
992c6e633adSDaniel Drake 	 * powering off. It is harmless in other cases.
993c6e633adSDaniel Drake 	 *
994c6e633adSDaniel Drake 	 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
995c6e633adSDaniel Drake 	 * is not necessary for non-removable cards. However, it is required
996c6e633adSDaniel Drake 	 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
997c6e633adSDaniel Drake 	 * harmless in other situations.
998c6e633adSDaniel Drake 	 *
999c6e633adSDaniel Drake 	 */
1000e7747475SUlf Hansson 
1001c6e633adSDaniel Drake 	sdio_reset(host);
1002c6e633adSDaniel Drake 	mmc_go_idle(host);
100359b6c9e0SNeilBrown 	mmc_send_if_cond(host, host->card->ocr);
1004c6e633adSDaniel Drake 
1005ce69d37bSUlf Hansson 	ret = mmc_send_io_op_cond(host, 0, NULL);
1006c6e633adSDaniel Drake 	if (ret)
1007c6e633adSDaniel Drake 		goto out;
1008c6e633adSDaniel Drake 
1009ce69d37bSUlf Hansson 	ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
1010a5e9425dSOhad Ben-Cohen 				mmc_card_keep_power(host));
1011d3fe37b1SOhad Ben-Cohen 	if (!ret && host->sdio_irqs)
1012d3fe37b1SOhad Ben-Cohen 		mmc_signal_sdio_irq(host);
1013c6e633adSDaniel Drake 
1014c6e633adSDaniel Drake out:
1015d3fe37b1SOhad Ben-Cohen 	mmc_release_host(host);
1016d3fe37b1SOhad Ben-Cohen 
1017d3fe37b1SOhad Ben-Cohen 	return ret;
1018d3fe37b1SOhad Ben-Cohen }
1019d3fe37b1SOhad Ben-Cohen 
102012d01d0bSUlf Hansson static int mmc_sdio_runtime_suspend(struct mmc_host *host)
102112d01d0bSUlf Hansson {
102212d01d0bSUlf Hansson 	/* No references to the card, cut the power to it. */
10238d1ffc8cSUlf Hansson 	mmc_claim_host(host);
102412d01d0bSUlf Hansson 	mmc_power_off(host);
10258d1ffc8cSUlf Hansson 	mmc_release_host(host);
10268d1ffc8cSUlf Hansson 
102712d01d0bSUlf Hansson 	return 0;
102812d01d0bSUlf Hansson }
102912d01d0bSUlf Hansson 
103012d01d0bSUlf Hansson static int mmc_sdio_runtime_resume(struct mmc_host *host)
103112d01d0bSUlf Hansson {
10328d1ffc8cSUlf Hansson 	int ret;
10338d1ffc8cSUlf Hansson 
103412d01d0bSUlf Hansson 	/* Restore power and re-initialize. */
10358d1ffc8cSUlf Hansson 	mmc_claim_host(host);
103669041150SUlf Hansson 	mmc_power_up(host, host->card->ocr);
10378d1ffc8cSUlf Hansson 	ret = mmc_sdio_power_restore(host);
10388d1ffc8cSUlf Hansson 	mmc_release_host(host);
10398d1ffc8cSUlf Hansson 
10408d1ffc8cSUlf Hansson 	return ret;
104112d01d0bSUlf Hansson }
104212d01d0bSUlf Hansson 
10431fb654fdSAndreas Fenkart static int mmc_sdio_reset(struct mmc_host *host)
10441fb654fdSAndreas Fenkart {
10451fb654fdSAndreas Fenkart 	mmc_power_cycle(host, host->card->ocr);
10461fb654fdSAndreas Fenkart 	return mmc_sdio_power_restore(host);
10471fb654fdSAndreas Fenkart }
10481fb654fdSAndreas Fenkart 
10495c4e6f13SPierre Ossman static const struct mmc_bus_ops mmc_sdio_ops = {
10505c4e6f13SPierre Ossman 	.remove = mmc_sdio_remove,
10515c4e6f13SPierre Ossman 	.detect = mmc_sdio_detect,
1052810caddbSUlf Hansson 	.pre_suspend = mmc_sdio_pre_suspend,
105317d33e14SNicolas Pitre 	.suspend = mmc_sdio_suspend,
105417d33e14SNicolas Pitre 	.resume = mmc_sdio_resume,
105512d01d0bSUlf Hansson 	.runtime_suspend = mmc_sdio_runtime_suspend,
105612d01d0bSUlf Hansson 	.runtime_resume = mmc_sdio_runtime_resume,
1057d3fe37b1SOhad Ben-Cohen 	.power_restore = mmc_sdio_power_restore,
1058d3049504SAdrian Hunter 	.alive = mmc_sdio_alive,
10591fb654fdSAndreas Fenkart 	.reset = mmc_sdio_reset,
10605c4e6f13SPierre Ossman };
10615c4e6f13SPierre Ossman 
10625c4e6f13SPierre Ossman 
10635c4e6f13SPierre Ossman /*
10645c4e6f13SPierre Ossman  * Starting point for SDIO card init.
10655c4e6f13SPierre Ossman  */
1066807e8e40SAndy Ross int mmc_attach_sdio(struct mmc_host *host)
10675c4e6f13SPierre Ossman {
1068807e8e40SAndy Ross 	int err, i, funcs;
106969041150SUlf Hansson 	u32 ocr, rocr;
10705c4e6f13SPierre Ossman 	struct mmc_card *card;
10715c4e6f13SPierre Ossman 
1072d84075c8SPierre Ossman 	WARN_ON(!host->claimed);
10735c4e6f13SPierre Ossman 
1074807e8e40SAndy Ross 	err = mmc_send_io_op_cond(host, 0, &ocr);
1075807e8e40SAndy Ross 	if (err)
1076807e8e40SAndy Ross 		return err;
1077807e8e40SAndy Ross 
10785c4e6f13SPierre Ossman 	mmc_attach_bus(host, &mmc_sdio_ops);
10798f230f45STakashi Iwai 	if (host->ocr_avail_sdio)
10808f230f45STakashi Iwai 		host->ocr_avail = host->ocr_avail_sdio;
10815c4e6f13SPierre Ossman 
10825c4e6f13SPierre Ossman 
108369041150SUlf Hansson 	rocr = mmc_select_voltage(host, ocr);
10845c4e6f13SPierre Ossman 
10855c4e6f13SPierre Ossman 	/*
10865c4e6f13SPierre Ossman 	 * Can we support the voltage(s) of the card(s)?
10875c4e6f13SPierre Ossman 	 */
108869041150SUlf Hansson 	if (!rocr) {
10895c4e6f13SPierre Ossman 		err = -EINVAL;
10905c4e6f13SPierre Ossman 		goto err;
10915c4e6f13SPierre Ossman 	}
10925c4e6f13SPierre Ossman 
10935c4e6f13SPierre Ossman 	/*
109417d33e14SNicolas Pitre 	 * Detect and init the card.
10955c4e6f13SPierre Ossman 	 */
109669041150SUlf Hansson 	err = mmc_sdio_init_card(host, rocr, NULL, 0);
10975c4e6f13SPierre Ossman 	if (err)
10985c4e6f13SPierre Ossman 		goto err;
1099ec2ed700SUlf Hansson 
110017d33e14SNicolas Pitre 	card = host->card;
1101af517150SDavid Brownell 
1102af517150SDavid Brownell 	/*
1103ed919b01SOhad Ben-Cohen 	 * Enable runtime PM only if supported by host+card+board
1104ed919b01SOhad Ben-Cohen 	 */
1105ed919b01SOhad Ben-Cohen 	if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1106ed919b01SOhad Ben-Cohen 		/*
110781968561SOhad Ben-Cohen 		 * Let runtime PM core know our card is active
110881968561SOhad Ben-Cohen 		 */
110981968561SOhad Ben-Cohen 		err = pm_runtime_set_active(&card->dev);
111081968561SOhad Ben-Cohen 		if (err)
111181968561SOhad Ben-Cohen 			goto remove;
111281968561SOhad Ben-Cohen 
111381968561SOhad Ben-Cohen 		/*
111481968561SOhad Ben-Cohen 		 * Enable runtime PM for this card
111581968561SOhad Ben-Cohen 		 */
111681968561SOhad Ben-Cohen 		pm_runtime_enable(&card->dev);
1117ed919b01SOhad Ben-Cohen 	}
111881968561SOhad Ben-Cohen 
111981968561SOhad Ben-Cohen 	/*
11205c4e6f13SPierre Ossman 	 * The number of functions on the card is encoded inside
11215c4e6f13SPierre Ossman 	 * the ocr.
11225c4e6f13SPierre Ossman 	 */
1123e8812793SMatt Fleming 	funcs = (ocr & 0x70000000) >> 28;
1124e8812793SMatt Fleming 	card->sdio_funcs = 0;
11254ff6471cSPierre Ossman 
11264ff6471cSPierre Ossman 	/*
1127e29a7d73SPierre Ossman 	 * Initialize (but don't add) all present functions.
1128e29a7d73SPierre Ossman 	 */
1129e8812793SMatt Fleming 	for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1130e29a7d73SPierre Ossman 		err = sdio_init_func(host->card, i + 1);
1131e29a7d73SPierre Ossman 		if (err)
1132e29a7d73SPierre Ossman 			goto remove;
113340bba0c1SOhad Ben-Cohen 
113440bba0c1SOhad Ben-Cohen 		/*
1135ed919b01SOhad Ben-Cohen 		 * Enable Runtime PM for this func (if supported)
113640bba0c1SOhad Ben-Cohen 		 */
1137ed919b01SOhad Ben-Cohen 		if (host->caps & MMC_CAP_POWER_OFF_CARD)
113840bba0c1SOhad Ben-Cohen 			pm_runtime_enable(&card->sdio_func[i]->dev);
1139e29a7d73SPierre Ossman 	}
11405c4e6f13SPierre Ossman 
1141e29a7d73SPierre Ossman 	/*
1142e29a7d73SPierre Ossman 	 * First add the card to the driver model...
1143e29a7d73SPierre Ossman 	 */
1144807e8e40SAndy Ross 	mmc_release_host(host);
11455c4e6f13SPierre Ossman 	err = mmc_add_card(host->card);
11465c4e6f13SPierre Ossman 	if (err)
1147e29a7d73SPierre Ossman 		goto remove_added;
1148e29a7d73SPierre Ossman 
1149e29a7d73SPierre Ossman 	/*
1150e29a7d73SPierre Ossman 	 * ...then the SDIO functions.
1151e29a7d73SPierre Ossman 	 */
1152e29a7d73SPierre Ossman 	for (i = 0;i < funcs;i++) {
1153e29a7d73SPierre Ossman 		err = sdio_add_func(host->card->sdio_func[i]);
1154e29a7d73SPierre Ossman 		if (err)
1155e29a7d73SPierre Ossman 			goto remove_added;
1156e29a7d73SPierre Ossman 	}
11575c4e6f13SPierre Ossman 
115834497913SDmitry Shmidt 	mmc_claim_host(host);
11595c4e6f13SPierre Ossman 	return 0;
11605c4e6f13SPierre Ossman 
1161e29a7d73SPierre Ossman 
1162e29a7d73SPierre Ossman remove_added:
1163e29a7d73SPierre Ossman 	/* Remove without lock if the device has been added. */
1164e29a7d73SPierre Ossman 	mmc_sdio_remove(host);
11655c4e6f13SPierre Ossman 	mmc_claim_host(host);
1166e29a7d73SPierre Ossman remove:
1167e29a7d73SPierre Ossman 	/* And with lock if it hasn't been added. */
1168807e8e40SAndy Ross 	mmc_release_host(host);
1169e29a7d73SPierre Ossman 	if (host->card)
1170e29a7d73SPierre Ossman 		mmc_sdio_remove(host);
1171807e8e40SAndy Ross 	mmc_claim_host(host);
11725c4e6f13SPierre Ossman err:
11735c4e6f13SPierre Ossman 	mmc_detach_bus(host);
11745c4e6f13SPierre Ossman 
1175a3c76eb9SGirish K S 	pr_err("%s: error %d whilst initialising SDIO card\n",
11765c4e6f13SPierre Ossman 		mmc_hostname(host), err);
11775c4e6f13SPierre Ossman 
11785c4e6f13SPierre Ossman 	return err;
11795c4e6f13SPierre Ossman }
11805c4e6f13SPierre Ossman 
1181