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