blob: b9dee28ee7d03a7ca1588037beedcb585880ccb9 [file] [log] [blame]
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001/*
2 * linux/drivers/mmc/sdio.c
3 *
4 * Copyright 2006-2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12#include <linux/err.h>
13
14#include <linux/mmc/host.h>
15#include <linux/mmc/card.h>
Pierre Ossman35c66c12007-06-11 20:25:43 +020016#include <linux/mmc/sdio.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020017#include <linux/mmc/sdio_func.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020018
19#include "core.h"
20#include "bus.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020021#include "sdio_bus.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020022#include "mmc_ops.h"
23#include "sd_ops.h"
24#include "sdio_ops.h"
Nicolas Pitreb7261262007-06-16 02:04:16 -040025#include "sdio_cis.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020026
Pierre Ossman05970072007-06-11 21:01:00 +020027static int sdio_read_fbr(struct sdio_func *func)
28{
29 int ret;
30 unsigned char data;
31
32 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010033 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020034 if (ret)
35 goto out;
36
37 data &= 0x0f;
38
39 if (data == 0x0f) {
40 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010041 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020042 if (ret)
43 goto out;
44 }
45
46 func->class = data;
47
48out:
49 return ret;
50}
51
Pierre Ossmane29a7d72007-05-26 13:48:18 +020052static int sdio_init_func(struct mmc_card *card, unsigned int fn)
53{
Pierre Ossman05970072007-06-11 21:01:00 +020054 int ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020055 struct sdio_func *func;
56
57 BUG_ON(fn > SDIO_MAX_FUNCS);
58
59 func = sdio_alloc_func(card);
60 if (IS_ERR(func))
61 return PTR_ERR(func);
62
63 func->num = fn;
64
Pierre Ossman05970072007-06-11 21:01:00 +020065 ret = sdio_read_fbr(func);
66 if (ret)
67 goto fail;
68
Pierre Ossman1a632f82007-07-30 15:15:30 +020069 ret = sdio_read_func_cis(func);
Nicolas Pitreb7261262007-06-16 02:04:16 -040070 if (ret)
71 goto fail;
72
Pierre Ossmane29a7d72007-05-26 13:48:18 +020073 card->sdio_func[fn - 1] = func;
74
75 return 0;
Pierre Ossman05970072007-06-11 21:01:00 +020076
77fail:
78 /*
79 * It is okay to remove the function here even though we hold
80 * the host lock as we haven't registered the device yet.
81 */
82 sdio_remove_func(func);
83 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020084}
85
Pierre Ossman35c66c12007-06-11 20:25:43 +020086static int sdio_read_cccr(struct mmc_card *card)
87{
88 int ret;
89 int cccr_vsn;
90 unsigned char data;
91
92 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
93
94 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
95 if (ret)
96 goto out;
97
98 cccr_vsn = data & 0x0f;
99
100 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
101 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
102 mmc_hostname(card->host), cccr_vsn);
103 return -EINVAL;
104 }
105
106 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
107
108 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
109 if (ret)
110 goto out;
111
112 if (data & SDIO_CCCR_CAP_SMB)
113 card->cccr.multi_block = 1;
114 if (data & SDIO_CCCR_CAP_LSC)
115 card->cccr.low_speed = 1;
116 if (data & SDIO_CCCR_CAP_4BLS)
117 card->cccr.wide_bus = 1;
118
119 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
120 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
121 if (ret)
122 goto out;
123
124 if (data & SDIO_POWER_SMPC)
125 card->cccr.high_power = 1;
126 }
127
128 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
129 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
130 if (ret)
131 goto out;
132
133 if (data & SDIO_SPEED_SHS)
134 card->cccr.high_speed = 1;
135 }
136
137out:
138 return ret;
139}
140
Pierre Ossman4ff64712007-07-30 18:23:53 +0200141static int sdio_enable_wide(struct mmc_card *card)
142{
143 int ret;
144 u8 ctrl;
145
146 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
147 return 0;
148
149 if (card->cccr.low_speed && !card->cccr.wide_bus)
150 return 0;
151
152 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
153 if (ret)
154 return ret;
155
156 ctrl |= SDIO_BUS_WIDTH_4BIT;
157
158 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
159 if (ret)
160 return ret;
161
162 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
163
164 return 0;
165}
166
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200167/*
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700168 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
169 * of the card. This may be required on certain setups of boards,
170 * controllers and embedded sdio device which do not need the card's
171 * pull-up. As a result, card detection is disabled and power is saved.
172 */
173static int sdio_disable_cd(struct mmc_card *card)
174{
175 int ret;
176 u8 ctrl;
177
178 if (!card->cccr.disable_cd)
179 return 0;
180
181 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
182 if (ret)
183 return ret;
184
185 ctrl |= SDIO_BUS_CD_DISABLE;
186
187 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
188}
189
190/*
Daniel Drake6b5eda32010-03-05 13:43:34 -0800191 * Devices that remain active during a system suspend are
192 * put back into 1-bit mode.
193 */
194static int sdio_disable_wide(struct mmc_card *card)
195{
196 int ret;
197 u8 ctrl;
198
199 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
200 return 0;
201
202 if (card->cccr.low_speed && !card->cccr.wide_bus)
203 return 0;
204
205 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
206 if (ret)
207 return ret;
208
209 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
210 return 0;
211
212 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
213 ctrl |= SDIO_BUS_ASYNC_INT;
214
215 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
216 if (ret)
217 return ret;
218
219 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
220
221 return 0;
222}
223
224/*
Pierre Ossmand16f5772008-08-31 17:22:46 +0200225 * Test if the card supports high-speed mode and, if so, switch to it.
226 */
227static int sdio_enable_hs(struct mmc_card *card)
228{
229 int ret;
230 u8 speed;
231
232 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
233 return 0;
234
235 if (!card->cccr.high_speed)
236 return 0;
237
238 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
239 if (ret)
240 return ret;
241
242 speed |= SDIO_SPEED_EHS;
243
244 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
245 if (ret)
246 return ret;
247
248 mmc_card_set_highspeed(card);
249 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
250
251 return 0;
252}
253
254/*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700255 * Handle the detection and initialisation of a card.
256 *
257 * In the case of a resume, "oldcard" will contain the card
258 * we're trying to reinitialise.
259 */
260static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
Chris Ball3bca4cf2010-03-05 13:43:33 -0800261 struct mmc_card *oldcard, int powered_resume)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700262{
263 struct mmc_card *card;
264 int err;
265
266 BUG_ON(!host);
267 WARN_ON(!host->claimed);
268
269 /*
270 * Inform the card of the voltage
271 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800272 if (!powered_resume) {
273 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
274 if (err)
275 goto err;
276 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700277
278 /*
279 * For SPI, enable CRC as appropriate.
280 */
281 if (mmc_host_is_spi(host)) {
282 err = mmc_spi_set_crc(host, use_spi_crc);
283 if (err)
284 goto err;
285 }
286
287 /*
288 * Allocate card structure.
289 */
290 card = mmc_alloc_card(host, NULL);
291 if (IS_ERR(card)) {
292 err = PTR_ERR(card);
293 goto err;
294 }
295
296 card->type = MMC_TYPE_SDIO;
297
298 /*
Daniel Mack3fcb0272010-04-01 10:03:25 +0200299 * Call the optional HC's init_card function to handle quirks.
300 */
301 if (host->ops->init_card)
302 host->ops->init_card(host, card);
303
304 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700305 * For native busses: set card RCA and quit open drain mode.
306 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800307 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700308 err = mmc_send_relative_addr(host, &card->rca);
309 if (err)
310 goto remove;
311
312 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
313 }
314
315 /*
316 * Select card, as all following commands rely on that.
317 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800318 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700319 err = mmc_select_card(card);
320 if (err)
321 goto remove;
322 }
323
324 /*
325 * Read the common registers.
326 */
327 err = sdio_read_cccr(card);
328 if (err)
329 goto remove;
330
331 /*
332 * Read the common CIS tuples.
333 */
334 err = sdio_read_common_cis(card);
335 if (err)
336 goto remove;
337
338 if (oldcard) {
339 int same = (card->cis.vendor == oldcard->cis.vendor &&
340 card->cis.device == oldcard->cis.device);
341 mmc_remove_card(card);
342 if (!same) {
343 err = -ENOENT;
344 goto err;
345 }
346 card = oldcard;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700347 return 0;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700348 }
349
350 /*
351 * Switch to high-speed (if supported).
352 */
353 err = sdio_enable_hs(card);
354 if (err)
355 goto remove;
356
357 /*
358 * Change to the card's maximum speed.
359 */
360 if (mmc_card_highspeed(card)) {
361 /*
362 * The SDIO specification doesn't mention how
363 * the CIS transfer speed register relates to
364 * high-speed, but it seems that 50 MHz is
365 * mandatory.
366 */
367 mmc_set_clock(host, 50000000);
368 } else {
369 mmc_set_clock(host, card->cis.max_dtr);
370 }
371
372 /*
373 * Switch to wider bus (if supported).
374 */
375 err = sdio_enable_wide(card);
376 if (err)
377 goto remove;
378
379 if (!oldcard)
380 host->card = card;
381 return 0;
382
383remove:
384 if (!oldcard)
385 mmc_remove_card(card);
386
387err:
388 return err;
389}
390
391/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200392 * Host is being removed. Free up the current card.
393 */
394static void mmc_sdio_remove(struct mmc_host *host)
395{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200396 int i;
397
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200398 BUG_ON(!host);
399 BUG_ON(!host->card);
400
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200401 for (i = 0;i < host->card->sdio_funcs;i++) {
402 if (host->card->sdio_func[i]) {
403 sdio_remove_func(host->card->sdio_func[i]);
404 host->card->sdio_func[i] = NULL;
405 }
406 }
407
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200408 mmc_remove_card(host->card);
409 host->card = NULL;
410}
411
412/*
413 * Card detection callback from host.
414 */
415static void mmc_sdio_detect(struct mmc_host *host)
416{
417 int err;
418
419 BUG_ON(!host);
420 BUG_ON(!host->card);
421
422 mmc_claim_host(host);
423
424 /*
425 * Just check if our card has been removed.
426 */
427 err = mmc_select_card(host->card);
428
429 mmc_release_host(host);
430
431 if (err) {
432 mmc_sdio_remove(host);
433
434 mmc_claim_host(host);
435 mmc_detach_bus(host);
436 mmc_release_host(host);
437 }
438}
439
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700440/*
441 * SDIO suspend. We need to suspend all functions separately.
442 * Therefore all registered functions must have drivers with suspend
443 * and resume methods. Failing that we simply remove the whole card.
444 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700445static int mmc_sdio_suspend(struct mmc_host *host)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700446{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700447 int i, err = 0;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700448
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700449 for (i = 0; i < host->card->sdio_funcs; i++) {
450 struct sdio_func *func = host->card->sdio_func[i];
451 if (func && sdio_func_present(func) && func->dev.driver) {
452 const struct dev_pm_ops *pmops = func->dev.driver->pm;
453 if (!pmops || !pmops->suspend || !pmops->resume) {
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700454 /* force removal of entire card in that case */
455 err = -ENOSYS;
456 } else
457 err = pmops->suspend(&func->dev);
458 if (err)
459 break;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700460 }
461 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700462 while (err && --i >= 0) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700463 struct sdio_func *func = host->card->sdio_func[i];
464 if (func && sdio_func_present(func) && func->dev.driver) {
465 const struct dev_pm_ops *pmops = func->dev.driver->pm;
466 pmops->resume(&func->dev);
467 }
468 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700469
Daniel Drake6b5eda32010-03-05 13:43:34 -0800470 if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
471 mmc_claim_host(host);
472 sdio_disable_wide(host->card);
473 mmc_release_host(host);
474 }
475
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700476 return err;
477}
478
479static int mmc_sdio_resume(struct mmc_host *host)
480{
481 int i, err;
482
483 BUG_ON(!host);
484 BUG_ON(!host->card);
485
486 /* Basic card reinitialization. */
487 mmc_claim_host(host);
Chris Ball3bca4cf2010-03-05 13:43:33 -0800488 err = mmc_sdio_init_card(host, host->ocr, host->card,
489 (host->pm_flags & MMC_PM_KEEP_POWER));
Daniel Drake6b5eda32010-03-05 13:43:34 -0800490 if (!err)
491 /* We may have switched to 1-bit mode during suspend. */
492 err = sdio_enable_wide(host->card);
Nicolas Pitre40216842010-03-05 13:43:34 -0800493 if (!err && host->sdio_irqs)
494 mmc_signal_sdio_irq(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700495 mmc_release_host(host);
496
497 /*
498 * If the card looked to be the same as before suspending, then
499 * we proceed to resume all card functions. If one of them returns
500 * an error then we simply return that error to the core and the
501 * card will be redetected as new. It is the responsibility of
502 * the function driver to perform further tests with the extra
503 * knowledge it has of the card to confirm the card is indeed the
504 * same as before suspending (same MAC address for network cards,
505 * etc.) and return an error otherwise.
506 */
507 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
508 struct sdio_func *func = host->card->sdio_func[i];
509 if (func && sdio_func_present(func) && func->dev.driver) {
510 const struct dev_pm_ops *pmops = func->dev.driver->pm;
511 err = pmops->resume(&func->dev);
512 }
513 }
514
515 return err;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700516}
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200517
518static const struct mmc_bus_ops mmc_sdio_ops = {
519 .remove = mmc_sdio_remove,
520 .detect = mmc_sdio_detect,
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700521 .suspend = mmc_sdio_suspend,
522 .resume = mmc_sdio_resume,
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200523};
524
525
526/*
527 * Starting point for SDIO card init.
528 */
529int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
530{
531 int err;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200532 int i, funcs;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200533 struct mmc_card *card;
534
535 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200536 WARN_ON(!host->claimed);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200537
538 mmc_attach_bus(host, &mmc_sdio_ops);
539
540 /*
541 * Sanity check the voltages that the card claims to
542 * support.
543 */
544 if (ocr & 0x7F) {
545 printk(KERN_WARNING "%s: card claims to support voltages "
546 "below the defined range. These will be ignored.\n",
547 mmc_hostname(host));
548 ocr &= ~0x7F;
549 }
550
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200551 host->ocr = mmc_select_voltage(host, ocr);
552
553 /*
554 * Can we support the voltage(s) of the card(s)?
555 */
556 if (!host->ocr) {
557 err = -EINVAL;
558 goto err;
559 }
560
561 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700562 * Detect and init the card.
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200563 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800564 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200565 if (err)
566 goto err;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700567 card = host->card;
David Brownellaf517152007-08-08 09:11:32 -0700568
569 /*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200570 * The number of functions on the card is encoded inside
571 * the ocr.
572 */
Matt Fleminge8812792009-12-17 15:27:18 -0800573 funcs = (ocr & 0x70000000) >> 28;
574 card->sdio_funcs = 0;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200575
576 /*
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700577 * If needed, disconnect card detection pull-up resistor.
578 */
579 err = sdio_disable_cd(card);
580 if (err)
581 goto remove;
582
583 /*
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200584 * Initialize (but don't add) all present functions.
585 */
Matt Fleminge8812792009-12-17 15:27:18 -0800586 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200587 err = sdio_init_func(host->card, i + 1);
588 if (err)
589 goto remove;
590 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200591
592 mmc_release_host(host);
593
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200594 /*
595 * First add the card to the driver model...
596 */
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200597 err = mmc_add_card(host->card);
598 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200599 goto remove_added;
600
601 /*
602 * ...then the SDIO functions.
603 */
604 for (i = 0;i < funcs;i++) {
605 err = sdio_add_func(host->card->sdio_func[i]);
606 if (err)
607 goto remove_added;
608 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200609
610 return 0;
611
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200612
613remove_added:
614 /* Remove without lock if the device has been added. */
615 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200616 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200617remove:
618 /* And with lock if it hasn't been added. */
619 if (host->card)
620 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200621err:
622 mmc_detach_bus(host);
623 mmc_release_host(host);
624
625 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
626 mmc_hostname(host), err);
627
628 return err;
629}
630