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