xref: /openbmc/linux/drivers/mmc/core/sdio.c (revision 59b4412f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  linux/drivers/mmc/sdio.c
4  *
5  *  Copyright 2006-2007 Pierre Ossman
6  */
7 
8 #include <linux/err.h>
9 #include <linux/pm_runtime.h>
10 
11 #include <linux/mmc/host.h>
12 #include <linux/mmc/card.h>
13 #include <linux/mmc/mmc.h>
14 #include <linux/mmc/sdio.h>
15 #include <linux/mmc/sdio_func.h>
16 #include <linux/mmc/sdio_ids.h>
17 
18 #include "core.h"
19 #include "card.h"
20 #include "host.h"
21 #include "bus.h"
22 #include "quirks.h"
23 #include "sd.h"
24 #include "sdio_bus.h"
25 #include "mmc_ops.h"
26 #include "sd_ops.h"
27 #include "sdio_ops.h"
28 #include "sdio_cis.h"
29 
30 MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
31 MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
32 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
33 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
34 
35 static struct attribute *sdio_std_attrs[] = {
36 	&dev_attr_vendor.attr,
37 	&dev_attr_device.attr,
38 	&dev_attr_ocr.attr,
39 	&dev_attr_rca.attr,
40 	NULL,
41 };
42 ATTRIBUTE_GROUPS(sdio_std);
43 
44 static struct device_type sdio_type = {
45 	.groups = sdio_std_groups,
46 };
47 
48 static int sdio_read_fbr(struct sdio_func *func)
49 {
50 	int ret;
51 	unsigned char data;
52 
53 	if (mmc_card_nonstd_func_interface(func->card)) {
54 		func->class = SDIO_CLASS_NONE;
55 		return 0;
56 	}
57 
58 	ret = mmc_io_rw_direct(func->card, 0, 0,
59 		SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
60 	if (ret)
61 		goto out;
62 
63 	data &= 0x0f;
64 
65 	if (data == 0x0f) {
66 		ret = mmc_io_rw_direct(func->card, 0, 0,
67 			SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
68 		if (ret)
69 			goto out;
70 	}
71 
72 	func->class = data;
73 
74 out:
75 	return ret;
76 }
77 
78 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
79 {
80 	int ret;
81 	struct sdio_func *func;
82 
83 	if (WARN_ON(fn > SDIO_MAX_FUNCS))
84 		return -EINVAL;
85 
86 	func = sdio_alloc_func(card);
87 	if (IS_ERR(func))
88 		return PTR_ERR(func);
89 
90 	func->num = fn;
91 
92 	if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
93 		ret = sdio_read_fbr(func);
94 		if (ret)
95 			goto fail;
96 
97 		ret = sdio_read_func_cis(func);
98 		if (ret)
99 			goto fail;
100 	} else {
101 		func->vendor = func->card->cis.vendor;
102 		func->device = func->card->cis.device;
103 		func->max_blksize = func->card->cis.blksize;
104 	}
105 
106 	card->sdio_func[fn - 1] = func;
107 
108 	return 0;
109 
110 fail:
111 	/*
112 	 * It is okay to remove the function here even though we hold
113 	 * the host lock as we haven't registered the device yet.
114 	 */
115 	sdio_remove_func(func);
116 	return ret;
117 }
118 
119 static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
120 {
121 	int ret;
122 	int cccr_vsn;
123 	int uhs = ocr & R4_18V_PRESENT;
124 	unsigned char data;
125 	unsigned char speed;
126 
127 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
128 	if (ret)
129 		goto out;
130 
131 	cccr_vsn = data & 0x0f;
132 
133 	if (cccr_vsn > SDIO_CCCR_REV_3_00) {
134 		pr_err("%s: unrecognised CCCR structure version %d\n",
135 			mmc_hostname(card->host), cccr_vsn);
136 		return -EINVAL;
137 	}
138 
139 	card->cccr.sdio_vsn = (data & 0xf0) >> 4;
140 
141 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
142 	if (ret)
143 		goto out;
144 
145 	if (data & SDIO_CCCR_CAP_SMB)
146 		card->cccr.multi_block = 1;
147 	if (data & SDIO_CCCR_CAP_LSC)
148 		card->cccr.low_speed = 1;
149 	if (data & SDIO_CCCR_CAP_4BLS)
150 		card->cccr.wide_bus = 1;
151 
152 	if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
153 		ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
154 		if (ret)
155 			goto out;
156 
157 		if (data & SDIO_POWER_SMPC)
158 			card->cccr.high_power = 1;
159 	}
160 
161 	if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
162 		ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
163 		if (ret)
164 			goto out;
165 
166 		card->scr.sda_spec3 = 0;
167 		card->sw_caps.sd3_bus_mode = 0;
168 		card->sw_caps.sd3_drv_type = 0;
169 		if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
170 			card->scr.sda_spec3 = 1;
171 			ret = mmc_io_rw_direct(card, 0, 0,
172 				SDIO_CCCR_UHS, 0, &data);
173 			if (ret)
174 				goto out;
175 
176 			if (mmc_host_uhs(card->host)) {
177 				if (data & SDIO_UHS_DDR50)
178 					card->sw_caps.sd3_bus_mode
179 						|= SD_MODE_UHS_DDR50;
180 
181 				if (data & SDIO_UHS_SDR50)
182 					card->sw_caps.sd3_bus_mode
183 						|= SD_MODE_UHS_SDR50;
184 
185 				if (data & SDIO_UHS_SDR104)
186 					card->sw_caps.sd3_bus_mode
187 						|= SD_MODE_UHS_SDR104;
188 			}
189 
190 			ret = mmc_io_rw_direct(card, 0, 0,
191 				SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
192 			if (ret)
193 				goto out;
194 
195 			if (data & SDIO_DRIVE_SDTA)
196 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
197 			if (data & SDIO_DRIVE_SDTC)
198 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
199 			if (data & SDIO_DRIVE_SDTD)
200 				card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
201 		}
202 
203 		/* if no uhs mode ensure we check for high speed */
204 		if (!card->sw_caps.sd3_bus_mode) {
205 			if (speed & SDIO_SPEED_SHS) {
206 				card->cccr.high_speed = 1;
207 				card->sw_caps.hs_max_dtr = 50000000;
208 			} else {
209 				card->cccr.high_speed = 0;
210 				card->sw_caps.hs_max_dtr = 25000000;
211 			}
212 		}
213 	}
214 
215 out:
216 	return ret;
217 }
218 
219 static int sdio_enable_wide(struct mmc_card *card)
220 {
221 	int ret;
222 	u8 ctrl;
223 
224 	if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
225 		return 0;
226 
227 	if (card->cccr.low_speed && !card->cccr.wide_bus)
228 		return 0;
229 
230 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
231 	if (ret)
232 		return ret;
233 
234 	if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
235 		pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
236 			mmc_hostname(card->host), ctrl);
237 
238 	/* set as 4-bit bus width */
239 	ctrl &= ~SDIO_BUS_WIDTH_MASK;
240 	ctrl |= SDIO_BUS_WIDTH_4BIT;
241 
242 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
243 	if (ret)
244 		return ret;
245 
246 	return 1;
247 }
248 
249 /*
250  * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
251  * of the card. This may be required on certain setups of boards,
252  * controllers and embedded sdio device which do not need the card's
253  * pull-up. As a result, card detection is disabled and power is saved.
254  */
255 static int sdio_disable_cd(struct mmc_card *card)
256 {
257 	int ret;
258 	u8 ctrl;
259 
260 	if (!mmc_card_disable_cd(card))
261 		return 0;
262 
263 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
264 	if (ret)
265 		return ret;
266 
267 	ctrl |= SDIO_BUS_CD_DISABLE;
268 
269 	return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
270 }
271 
272 /*
273  * Devices that remain active during a system suspend are
274  * put back into 1-bit mode.
275  */
276 static int sdio_disable_wide(struct mmc_card *card)
277 {
278 	int ret;
279 	u8 ctrl;
280 
281 	if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
282 		return 0;
283 
284 	if (card->cccr.low_speed && !card->cccr.wide_bus)
285 		return 0;
286 
287 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
288 	if (ret)
289 		return ret;
290 
291 	if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
292 		return 0;
293 
294 	ctrl &= ~SDIO_BUS_WIDTH_4BIT;
295 	ctrl |= SDIO_BUS_ASYNC_INT;
296 
297 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
298 	if (ret)
299 		return ret;
300 
301 	mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
302 
303 	return 0;
304 }
305 
306 
307 static int sdio_enable_4bit_bus(struct mmc_card *card)
308 {
309 	int err;
310 
311 	if (card->type == MMC_TYPE_SDIO)
312 		err = sdio_enable_wide(card);
313 	else if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
314 		 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
315 		err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
316 		if (err)
317 			return err;
318 		err = sdio_enable_wide(card);
319 		if (err <= 0)
320 			mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
321 	} else
322 		return 0;
323 
324 	if (err > 0) {
325 		mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
326 		err = 0;
327 	}
328 
329 	return err;
330 }
331 
332 
333 /*
334  * Test if the card supports high-speed mode and, if so, switch to it.
335  */
336 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
337 {
338 	int ret;
339 	u8 speed;
340 
341 	if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
342 		return 0;
343 
344 	if (!card->cccr.high_speed)
345 		return 0;
346 
347 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
348 	if (ret)
349 		return ret;
350 
351 	if (enable)
352 		speed |= SDIO_SPEED_EHS;
353 	else
354 		speed &= ~SDIO_SPEED_EHS;
355 
356 	ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
357 	if (ret)
358 		return ret;
359 
360 	return 1;
361 }
362 
363 /*
364  * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
365  */
366 static int sdio_enable_hs(struct mmc_card *card)
367 {
368 	int ret;
369 
370 	ret = mmc_sdio_switch_hs(card, true);
371 	if (ret <= 0 || card->type == MMC_TYPE_SDIO)
372 		return ret;
373 
374 	ret = mmc_sd_switch_hs(card);
375 	if (ret <= 0)
376 		mmc_sdio_switch_hs(card, false);
377 
378 	return ret;
379 }
380 
381 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
382 {
383 	unsigned max_dtr;
384 
385 	if (mmc_card_hs(card)) {
386 		/*
387 		 * The SDIO specification doesn't mention how
388 		 * the CIS transfer speed register relates to
389 		 * high-speed, but it seems that 50 MHz is
390 		 * mandatory.
391 		 */
392 		max_dtr = 50000000;
393 	} else {
394 		max_dtr = card->cis.max_dtr;
395 	}
396 
397 	if (card->type == MMC_TYPE_SD_COMBO)
398 		max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
399 
400 	return max_dtr;
401 }
402 
403 static unsigned char host_drive_to_sdio_drive(int host_strength)
404 {
405 	switch (host_strength) {
406 	case MMC_SET_DRIVER_TYPE_A:
407 		return SDIO_DTSx_SET_TYPE_A;
408 	case MMC_SET_DRIVER_TYPE_B:
409 		return SDIO_DTSx_SET_TYPE_B;
410 	case MMC_SET_DRIVER_TYPE_C:
411 		return SDIO_DTSx_SET_TYPE_C;
412 	case MMC_SET_DRIVER_TYPE_D:
413 		return SDIO_DTSx_SET_TYPE_D;
414 	default:
415 		return SDIO_DTSx_SET_TYPE_B;
416 	}
417 }
418 
419 static void sdio_select_driver_type(struct mmc_card *card)
420 {
421 	int card_drv_type, drive_strength, drv_type;
422 	unsigned char card_strength;
423 	int err;
424 
425 	card->drive_strength = 0;
426 
427 	card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
428 
429 	drive_strength = mmc_select_drive_strength(card,
430 						   card->sw_caps.uhs_max_dtr,
431 						   card_drv_type, &drv_type);
432 
433 	if (drive_strength) {
434 		/* if error just use default for drive strength B */
435 		err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
436 				       &card_strength);
437 		if (err)
438 			return;
439 
440 		card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
441 		card_strength |= host_drive_to_sdio_drive(drive_strength);
442 
443 		/* if error default to drive strength B */
444 		err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
445 				       card_strength, NULL);
446 		if (err)
447 			return;
448 		card->drive_strength = drive_strength;
449 	}
450 
451 	if (drv_type)
452 		mmc_set_driver_type(card->host, drv_type);
453 }
454 
455 
456 static int sdio_set_bus_speed_mode(struct mmc_card *card)
457 {
458 	unsigned int bus_speed, timing;
459 	int err;
460 	unsigned char speed;
461 	unsigned int max_rate;
462 
463 	/*
464 	 * If the host doesn't support any of the UHS-I modes, fallback on
465 	 * default speed.
466 	 */
467 	if (!mmc_host_uhs(card->host))
468 		return 0;
469 
470 	bus_speed = SDIO_SPEED_SDR12;
471 	timing = MMC_TIMING_UHS_SDR12;
472 	if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
473 	    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
474 			bus_speed = SDIO_SPEED_SDR104;
475 			timing = MMC_TIMING_UHS_SDR104;
476 			card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
477 			card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
478 	} else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
479 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
480 			bus_speed = SDIO_SPEED_DDR50;
481 			timing = MMC_TIMING_UHS_DDR50;
482 			card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
483 			card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
484 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
485 		    MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
486 		    SD_MODE_UHS_SDR50)) {
487 			bus_speed = SDIO_SPEED_SDR50;
488 			timing = MMC_TIMING_UHS_SDR50;
489 			card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
490 			card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
491 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
492 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
493 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
494 			bus_speed = SDIO_SPEED_SDR25;
495 			timing = MMC_TIMING_UHS_SDR25;
496 			card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
497 			card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
498 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
499 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
500 		    MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
501 		    SD_MODE_UHS_SDR12)) {
502 			bus_speed = SDIO_SPEED_SDR12;
503 			timing = MMC_TIMING_UHS_SDR12;
504 			card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
505 			card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
506 	}
507 
508 	err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
509 	if (err)
510 		return err;
511 
512 	speed &= ~SDIO_SPEED_BSS_MASK;
513 	speed |= bus_speed;
514 	err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
515 	if (err)
516 		return err;
517 
518 	max_rate = min_not_zero(card->quirk_max_rate,
519 				card->sw_caps.uhs_max_dtr);
520 
521 	if (bus_speed) {
522 		mmc_set_timing(card->host, timing);
523 		mmc_set_clock(card->host, max_rate);
524 	}
525 
526 	return 0;
527 }
528 
529 /*
530  * UHS-I specific initialization procedure
531  */
532 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
533 {
534 	int err;
535 
536 	if (!card->scr.sda_spec3)
537 		return 0;
538 
539 	/* Switch to wider bus */
540 	err = sdio_enable_4bit_bus(card);
541 	if (err)
542 		goto out;
543 
544 	/* Set the driver strength for the card */
545 	sdio_select_driver_type(card);
546 
547 	/* Set bus speed mode of the card */
548 	err = sdio_set_bus_speed_mode(card);
549 	if (err)
550 		goto out;
551 
552 	/*
553 	 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
554 	 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
555 	 */
556 	if (!mmc_host_is_spi(card->host) &&
557 	    ((card->host->ios.timing == MMC_TIMING_UHS_SDR50) ||
558 	      (card->host->ios.timing == MMC_TIMING_UHS_SDR104)))
559 		err = mmc_execute_tuning(card);
560 out:
561 	return err;
562 }
563 
564 static int mmc_sdio_pre_init(struct mmc_host *host, u32 ocr,
565 			     struct mmc_card *card)
566 {
567 	if (card)
568 		mmc_remove_card(card);
569 
570 	/*
571 	 * Reset the card by performing the same steps that are taken by
572 	 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
573 	 *
574 	 * sdio_reset() is technically not needed. Having just powered up the
575 	 * hardware, it should already be in reset state. However, some
576 	 * platforms (such as SD8686 on OLPC) do not instantly cut power,
577 	 * meaning that a reset is required when restoring power soon after
578 	 * powering off. It is harmless in other cases.
579 	 *
580 	 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
581 	 * is not necessary for non-removable cards. However, it is required
582 	 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
583 	 * harmless in other situations.
584 	 *
585 	 */
586 
587 	sdio_reset(host);
588 	mmc_go_idle(host);
589 	mmc_send_if_cond(host, ocr);
590 	return mmc_send_io_op_cond(host, 0, NULL);
591 }
592 
593 /*
594  * Handle the detection and initialisation of a card.
595  *
596  * In the case of a resume, "oldcard" will contain the card
597  * we're trying to reinitialise.
598  */
599 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
600 			      struct mmc_card *oldcard)
601 {
602 	struct mmc_card *card;
603 	int err;
604 	int retries = 10;
605 	u32 rocr = 0;
606 	u32 ocr_card = ocr;
607 
608 	WARN_ON(!host->claimed);
609 
610 	/* to query card if 1.8V signalling is supported */
611 	if (mmc_host_uhs(host))
612 		ocr |= R4_18V_PRESENT;
613 
614 try_again:
615 	if (!retries) {
616 		pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
617 		ocr &= ~R4_18V_PRESENT;
618 	}
619 
620 	/*
621 	 * Inform the card of the voltage
622 	 */
623 	err = mmc_send_io_op_cond(host, ocr, &rocr);
624 	if (err)
625 		return err;
626 
627 	/*
628 	 * For SPI, enable CRC as appropriate.
629 	 */
630 	if (mmc_host_is_spi(host)) {
631 		err = mmc_spi_set_crc(host, use_spi_crc);
632 		if (err)
633 			return err;
634 	}
635 
636 	/*
637 	 * Allocate card structure.
638 	 */
639 	card = mmc_alloc_card(host, &sdio_type);
640 	if (IS_ERR(card))
641 		return PTR_ERR(card);
642 
643 	if ((rocr & R4_MEMORY_PRESENT) &&
644 	    mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
645 		card->type = MMC_TYPE_SD_COMBO;
646 
647 		if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
648 		    memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
649 			err = -ENOENT;
650 			goto mismatch;
651 		}
652 	} else {
653 		card->type = MMC_TYPE_SDIO;
654 
655 		if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
656 			err = -ENOENT;
657 			goto mismatch;
658 		}
659 	}
660 
661 	/*
662 	 * Call the optional HC's init_card function to handle quirks.
663 	 */
664 	if (host->ops->init_card)
665 		host->ops->init_card(host, card);
666 
667 	/*
668 	 * If the host and card support UHS-I mode request the card
669 	 * to switch to 1.8V signaling level.  No 1.8v signalling if
670 	 * UHS mode is not enabled to maintain compatibility and some
671 	 * systems that claim 1.8v signalling in fact do not support
672 	 * it. Per SDIO spec v3, section 3.1.2, if the voltage is already
673 	 * 1.8v, the card sets S18A to 0 in the R4 response. So it will
674 	 * fails to check rocr & R4_18V_PRESENT,  but we still need to
675 	 * try to init uhs card. sdio_read_cccr will take over this task
676 	 * to make sure which speed mode should work.
677 	 */
678 	if (rocr & ocr & R4_18V_PRESENT) {
679 		err = mmc_set_uhs_voltage(host, ocr_card);
680 		if (err == -EAGAIN) {
681 			mmc_sdio_pre_init(host, ocr_card, card);
682 			retries--;
683 			goto try_again;
684 		} else if (err) {
685 			ocr &= ~R4_18V_PRESENT;
686 		}
687 	}
688 
689 	/*
690 	 * For native busses:  set card RCA and quit open drain mode.
691 	 */
692 	if (!mmc_host_is_spi(host)) {
693 		err = mmc_send_relative_addr(host, &card->rca);
694 		if (err)
695 			goto remove;
696 
697 		/*
698 		 * Update oldcard with the new RCA received from the SDIO
699 		 * device -- we're doing this so that it's updated in the
700 		 * "card" struct when oldcard overwrites that later.
701 		 */
702 		if (oldcard)
703 			oldcard->rca = card->rca;
704 	}
705 
706 	/*
707 	 * Read CSD, before selecting the card
708 	 */
709 	if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
710 		err = mmc_sd_get_csd(host, card);
711 		if (err)
712 			goto remove;
713 
714 		mmc_decode_cid(card);
715 	}
716 
717 	/*
718 	 * Select card, as all following commands rely on that.
719 	 */
720 	if (!mmc_host_is_spi(host)) {
721 		err = mmc_select_card(card);
722 		if (err)
723 			goto remove;
724 	}
725 
726 	if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
727 		/*
728 		 * This is non-standard SDIO device, meaning it doesn't
729 		 * have any CIA (Common I/O area) registers present.
730 		 * It's host's responsibility to fill cccr and cis
731 		 * structures in init_card().
732 		 */
733 		mmc_set_clock(host, card->cis.max_dtr);
734 
735 		if (card->cccr.high_speed) {
736 			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
737 		}
738 
739 		if (oldcard)
740 			mmc_remove_card(card);
741 		else
742 			host->card = card;
743 
744 		return 0;
745 	}
746 
747 	/*
748 	 * Read the common registers. Note that we should try to
749 	 * validate whether UHS would work or not.
750 	 */
751 	err = sdio_read_cccr(card, ocr);
752 	if (err) {
753 		mmc_sdio_pre_init(host, ocr_card, card);
754 		if (ocr & R4_18V_PRESENT) {
755 			/* Retry init sequence, but without R4_18V_PRESENT. */
756 			retries = 0;
757 			goto try_again;
758 		}
759 		return err;
760 	}
761 
762 	/*
763 	 * Read the common CIS tuples.
764 	 */
765 	err = sdio_read_common_cis(card);
766 	if (err)
767 		goto remove;
768 
769 	if (oldcard) {
770 		if (card->cis.vendor == oldcard->cis.vendor &&
771 		    card->cis.device == oldcard->cis.device) {
772 			mmc_remove_card(card);
773 			card = oldcard;
774 		} else {
775 			err = -ENOENT;
776 			goto mismatch;
777 		}
778 	}
779 	card->ocr = ocr_card;
780 	mmc_fixup_device(card, sdio_fixup_methods);
781 
782 	if (card->type == MMC_TYPE_SD_COMBO) {
783 		err = mmc_sd_setup_card(host, card, oldcard != NULL);
784 		/* handle as SDIO-only card if memory init failed */
785 		if (err) {
786 			mmc_go_idle(host);
787 			if (mmc_host_is_spi(host))
788 				/* should not fail, as it worked previously */
789 				mmc_spi_set_crc(host, use_spi_crc);
790 			card->type = MMC_TYPE_SDIO;
791 		} else
792 			card->dev.type = &sd_type;
793 	}
794 
795 	/*
796 	 * If needed, disconnect card detection pull-up resistor.
797 	 */
798 	err = sdio_disable_cd(card);
799 	if (err)
800 		goto remove;
801 
802 	/* Initialization sequence for UHS-I cards */
803 	/* Only if card supports 1.8v and UHS signaling */
804 	if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
805 		err = mmc_sdio_init_uhs_card(card);
806 		if (err)
807 			goto remove;
808 	} else {
809 		/*
810 		 * Switch to high-speed (if supported).
811 		 */
812 		err = sdio_enable_hs(card);
813 		if (err > 0)
814 			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
815 		else if (err)
816 			goto remove;
817 
818 		/*
819 		 * Change to the card's maximum speed.
820 		 */
821 		mmc_set_clock(host, mmc_sdio_get_max_clock(card));
822 
823 		/*
824 		 * Switch to wider bus (if supported).
825 		 */
826 		err = sdio_enable_4bit_bus(card);
827 		if (err)
828 			goto remove;
829 	}
830 
831 	if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
832 	    host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
833 		pr_err("%s: Host failed to negotiate down from 3.3V\n",
834 			mmc_hostname(host));
835 		err = -EINVAL;
836 		goto remove;
837 	}
838 
839 	host->card = card;
840 	return 0;
841 
842 mismatch:
843 	pr_debug("%s: Perhaps the card was replaced\n", mmc_hostname(host));
844 remove:
845 	if (oldcard != card)
846 		mmc_remove_card(card);
847 	return err;
848 }
849 
850 static int mmc_sdio_reinit_card(struct mmc_host *host)
851 {
852 	int ret;
853 
854 	ret = mmc_sdio_pre_init(host, host->card->ocr, NULL);
855 	if (ret)
856 		return ret;
857 
858 	return mmc_sdio_init_card(host, host->card->ocr, host->card);
859 }
860 
861 /*
862  * Host is being removed. Free up the current card.
863  */
864 static void mmc_sdio_remove(struct mmc_host *host)
865 {
866 	int i;
867 
868 	for (i = 0;i < host->card->sdio_funcs;i++) {
869 		if (host->card->sdio_func[i]) {
870 			sdio_remove_func(host->card->sdio_func[i]);
871 			host->card->sdio_func[i] = NULL;
872 		}
873 	}
874 
875 	mmc_remove_card(host->card);
876 	host->card = NULL;
877 }
878 
879 /*
880  * Card detection - card is alive.
881  */
882 static int mmc_sdio_alive(struct mmc_host *host)
883 {
884 	return mmc_select_card(host->card);
885 }
886 
887 /*
888  * Card detection callback from host.
889  */
890 static void mmc_sdio_detect(struct mmc_host *host)
891 {
892 	int err;
893 
894 	/* Make sure card is powered before detecting it */
895 	if (host->caps & MMC_CAP_POWER_OFF_CARD) {
896 		err = pm_runtime_get_sync(&host->card->dev);
897 		if (err < 0) {
898 			pm_runtime_put_noidle(&host->card->dev);
899 			goto out;
900 		}
901 	}
902 
903 	mmc_claim_host(host);
904 
905 	/*
906 	 * Just check if our card has been removed.
907 	 */
908 	err = _mmc_detect_card_removed(host);
909 
910 	mmc_release_host(host);
911 
912 	/*
913 	 * Tell PM core it's OK to power off the card now.
914 	 *
915 	 * The _sync variant is used in order to ensure that the card
916 	 * is left powered off in case an error occurred, and the card
917 	 * is going to be removed.
918 	 *
919 	 * Since there is no specific reason to believe a new user
920 	 * is about to show up at this point, the _sync variant is
921 	 * desirable anyway.
922 	 */
923 	if (host->caps & MMC_CAP_POWER_OFF_CARD)
924 		pm_runtime_put_sync(&host->card->dev);
925 
926 out:
927 	if (err) {
928 		mmc_sdio_remove(host);
929 
930 		mmc_claim_host(host);
931 		mmc_detach_bus(host);
932 		mmc_power_off(host);
933 		mmc_release_host(host);
934 	}
935 }
936 
937 /*
938  * SDIO pre_suspend.  We need to suspend all functions separately.
939  * Therefore all registered functions must have drivers with suspend
940  * and resume methods.  Failing that we simply remove the whole card.
941  */
942 static int mmc_sdio_pre_suspend(struct mmc_host *host)
943 {
944 	int i, err = 0;
945 
946 	for (i = 0; i < host->card->sdio_funcs; i++) {
947 		struct sdio_func *func = host->card->sdio_func[i];
948 		if (func && sdio_func_present(func) && func->dev.driver) {
949 			const struct dev_pm_ops *pmops = func->dev.driver->pm;
950 			if (!pmops || !pmops->suspend || !pmops->resume) {
951 				/* force removal of entire card in that case */
952 				err = -ENOSYS;
953 				break;
954 			}
955 		}
956 	}
957 
958 	return err;
959 }
960 
961 /*
962  * SDIO suspend.  Suspend all functions separately.
963  */
964 static int mmc_sdio_suspend(struct mmc_host *host)
965 {
966 	WARN_ON(host->sdio_irqs && !mmc_card_keep_power(host));
967 
968 	/* Prevent processing of SDIO IRQs in suspended state. */
969 	mmc_card_set_suspended(host->card);
970 	cancel_delayed_work_sync(&host->sdio_irq_work);
971 
972 	mmc_claim_host(host);
973 
974 	if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
975 		sdio_disable_wide(host->card);
976 
977 	if (!mmc_card_keep_power(host)) {
978 		mmc_power_off(host);
979 	} else if (host->retune_period) {
980 		mmc_retune_timer_stop(host);
981 		mmc_retune_needed(host);
982 	}
983 
984 	mmc_release_host(host);
985 
986 	return 0;
987 }
988 
989 static int mmc_sdio_resume(struct mmc_host *host)
990 {
991 	int err = 0;
992 
993 	/* Basic card reinitialization. */
994 	mmc_claim_host(host);
995 
996 	/*
997 	 * Restore power and reinitialize the card when needed. Note that a
998 	 * removable card is checked from a detect work later on in the resume
999 	 * process.
1000 	 */
1001 	if (!mmc_card_keep_power(host)) {
1002 		mmc_power_up(host, host->card->ocr);
1003 		/*
1004 		 * Tell runtime PM core we just powered up the card,
1005 		 * since it still believes the card is powered off.
1006 		 * Note that currently runtime PM is only enabled
1007 		 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1008 		 */
1009 		if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1010 			pm_runtime_disable(&host->card->dev);
1011 			pm_runtime_set_active(&host->card->dev);
1012 			pm_runtime_enable(&host->card->dev);
1013 		}
1014 		err = mmc_sdio_reinit_card(host);
1015 	} else if (mmc_card_wake_sdio_irq(host)) {
1016 		/* We may have switched to 1-bit mode during suspend */
1017 		err = sdio_enable_4bit_bus(host->card);
1018 	}
1019 
1020 	if (err)
1021 		goto out;
1022 
1023 	/* Allow SDIO IRQs to be processed again. */
1024 	mmc_card_clr_suspended(host->card);
1025 
1026 	if (host->sdio_irqs) {
1027 		if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
1028 			wake_up_process(host->sdio_irq_thread);
1029 		else if (host->caps & MMC_CAP_SDIO_IRQ)
1030 			queue_delayed_work(system_wq, &host->sdio_irq_work, 0);
1031 	}
1032 
1033 out:
1034 	mmc_release_host(host);
1035 
1036 	host->pm_flags &= ~MMC_PM_KEEP_POWER;
1037 	return err;
1038 }
1039 
1040 static int mmc_sdio_runtime_suspend(struct mmc_host *host)
1041 {
1042 	/* No references to the card, cut the power to it. */
1043 	mmc_claim_host(host);
1044 	mmc_power_off(host);
1045 	mmc_release_host(host);
1046 
1047 	return 0;
1048 }
1049 
1050 static int mmc_sdio_runtime_resume(struct mmc_host *host)
1051 {
1052 	int ret;
1053 
1054 	/* Restore power and re-initialize. */
1055 	mmc_claim_host(host);
1056 	mmc_power_up(host, host->card->ocr);
1057 	ret = mmc_sdio_reinit_card(host);
1058 	mmc_release_host(host);
1059 
1060 	return ret;
1061 }
1062 
1063 /*
1064  * SDIO HW reset
1065  *
1066  * Returns 0 if the HW reset was executed synchronously, returns 1 if the HW
1067  * reset was asynchronously scheduled, else a negative error code.
1068  */
1069 static int mmc_sdio_hw_reset(struct mmc_host *host)
1070 {
1071 	struct mmc_card *card = host->card;
1072 
1073 	/*
1074 	 * In case the card is shared among multiple func drivers, reset the
1075 	 * card through a rescan work. In this way it will be removed and
1076 	 * re-detected, thus all func drivers becomes informed about it.
1077 	 */
1078 	if (atomic_read(&card->sdio_funcs_probed) > 1) {
1079 		if (mmc_card_removed(card))
1080 			return 1;
1081 		host->rescan_entered = 0;
1082 		mmc_card_set_removed(card);
1083 		_mmc_detect_change(host, 0, false);
1084 		return 1;
1085 	}
1086 
1087 	/*
1088 	 * A single func driver has been probed, then let's skip the heavy
1089 	 * hotplug dance above and execute the reset immediately.
1090 	 */
1091 	mmc_power_cycle(host, card->ocr);
1092 	return mmc_sdio_reinit_card(host);
1093 }
1094 
1095 static int mmc_sdio_sw_reset(struct mmc_host *host)
1096 {
1097 	mmc_set_clock(host, host->f_init);
1098 	sdio_reset(host);
1099 	mmc_go_idle(host);
1100 
1101 	mmc_set_initial_state(host);
1102 	mmc_set_initial_signal_voltage(host);
1103 
1104 	return mmc_sdio_reinit_card(host);
1105 }
1106 
1107 static const struct mmc_bus_ops mmc_sdio_ops = {
1108 	.remove = mmc_sdio_remove,
1109 	.detect = mmc_sdio_detect,
1110 	.pre_suspend = mmc_sdio_pre_suspend,
1111 	.suspend = mmc_sdio_suspend,
1112 	.resume = mmc_sdio_resume,
1113 	.runtime_suspend = mmc_sdio_runtime_suspend,
1114 	.runtime_resume = mmc_sdio_runtime_resume,
1115 	.alive = mmc_sdio_alive,
1116 	.hw_reset = mmc_sdio_hw_reset,
1117 	.sw_reset = mmc_sdio_sw_reset,
1118 };
1119 
1120 
1121 /*
1122  * Starting point for SDIO card init.
1123  */
1124 int mmc_attach_sdio(struct mmc_host *host)
1125 {
1126 	int err, i, funcs;
1127 	u32 ocr, rocr;
1128 	struct mmc_card *card;
1129 
1130 	WARN_ON(!host->claimed);
1131 
1132 	err = mmc_send_io_op_cond(host, 0, &ocr);
1133 	if (err)
1134 		return err;
1135 
1136 	mmc_attach_bus(host, &mmc_sdio_ops);
1137 	if (host->ocr_avail_sdio)
1138 		host->ocr_avail = host->ocr_avail_sdio;
1139 
1140 
1141 	rocr = mmc_select_voltage(host, ocr);
1142 
1143 	/*
1144 	 * Can we support the voltage(s) of the card(s)?
1145 	 */
1146 	if (!rocr) {
1147 		err = -EINVAL;
1148 		goto err;
1149 	}
1150 
1151 	/*
1152 	 * Detect and init the card.
1153 	 */
1154 	err = mmc_sdio_init_card(host, rocr, NULL);
1155 	if (err)
1156 		goto err;
1157 
1158 	card = host->card;
1159 
1160 	/*
1161 	 * Enable runtime PM only if supported by host+card+board
1162 	 */
1163 	if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1164 		/*
1165 		 * Do not allow runtime suspend until after SDIO function
1166 		 * devices are added.
1167 		 */
1168 		pm_runtime_get_noresume(&card->dev);
1169 
1170 		/*
1171 		 * Let runtime PM core know our card is active
1172 		 */
1173 		err = pm_runtime_set_active(&card->dev);
1174 		if (err)
1175 			goto remove;
1176 
1177 		/*
1178 		 * Enable runtime PM for this card
1179 		 */
1180 		pm_runtime_enable(&card->dev);
1181 	}
1182 
1183 	/*
1184 	 * The number of functions on the card is encoded inside
1185 	 * the ocr.
1186 	 */
1187 	funcs = (ocr & 0x70000000) >> 28;
1188 	card->sdio_funcs = 0;
1189 
1190 	/*
1191 	 * Initialize (but don't add) all present functions.
1192 	 */
1193 	for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1194 		err = sdio_init_func(host->card, i + 1);
1195 		if (err)
1196 			goto remove;
1197 
1198 		/*
1199 		 * Enable Runtime PM for this func (if supported)
1200 		 */
1201 		if (host->caps & MMC_CAP_POWER_OFF_CARD)
1202 			pm_runtime_enable(&card->sdio_func[i]->dev);
1203 	}
1204 
1205 	/*
1206 	 * First add the card to the driver model...
1207 	 */
1208 	mmc_release_host(host);
1209 	err = mmc_add_card(host->card);
1210 	if (err)
1211 		goto remove_added;
1212 
1213 	/*
1214 	 * ...then the SDIO functions.
1215 	 */
1216 	for (i = 0;i < funcs;i++) {
1217 		err = sdio_add_func(host->card->sdio_func[i]);
1218 		if (err)
1219 			goto remove_added;
1220 	}
1221 
1222 	if (host->caps & MMC_CAP_POWER_OFF_CARD)
1223 		pm_runtime_put(&card->dev);
1224 
1225 	mmc_claim_host(host);
1226 	return 0;
1227 
1228 
1229 remove:
1230 	mmc_release_host(host);
1231 remove_added:
1232 	/*
1233 	 * The devices are being deleted so it is not necessary to disable
1234 	 * runtime PM. Similarly we also don't pm_runtime_put() the SDIO card
1235 	 * because it needs to be active to remove any function devices that
1236 	 * were probed, and after that it gets deleted.
1237 	 */
1238 	mmc_sdio_remove(host);
1239 	mmc_claim_host(host);
1240 err:
1241 	mmc_detach_bus(host);
1242 
1243 	pr_err("%s: error %d whilst initialising SDIO card\n",
1244 		mmc_hostname(host), err);
1245 
1246 	return err;
1247 }
1248 
1249