]> Git Repo - linux.git/blame - drivers/mmc/core/sdio.c
ARM: MXC: mxcmmc: Teach the driver SDIO operations
[linux.git] / drivers / mmc / core / sdio.c
CommitLineData
5c4e6f13
PO
1/*
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>
35c66c19 16#include <linux/mmc/sdio.h>
e29a7d73 17#include <linux/mmc/sdio_func.h>
5c4e6f13
PO
18
19#include "core.h"
20#include "bus.h"
e29a7d73 21#include "sdio_bus.h"
5c4e6f13
PO
22#include "mmc_ops.h"
23#include "sd_ops.h"
24#include "sdio_ops.h"
b7261261 25#include "sdio_cis.h"
5c4e6f13 26
0597007f
PO
27static 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,
7616ee95 33 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
0597007f
PO
34 if (ret)
35 goto out;
36
37 data &= 0x0f;
38
39 if (data == 0x0f) {
40 ret = mmc_io_rw_direct(func->card, 0, 0,
7616ee95 41 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
0597007f
PO
42 if (ret)
43 goto out;
44 }
45
46 func->class = data;
47
48out:
49 return ret;
50}
51
e29a7d73
PO
52static int sdio_init_func(struct mmc_card *card, unsigned int fn)
53{
0597007f 54 int ret;
e29a7d73
PO
55 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
0597007f
PO
65 ret = sdio_read_fbr(func);
66 if (ret)
67 goto fail;
68
1a632f8c 69 ret = sdio_read_func_cis(func);
b7261261
NP
70 if (ret)
71 goto fail;
72
e29a7d73
PO
73 card->sdio_func[fn - 1] = func;
74
75 return 0;
0597007f
PO
76
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;
e29a7d73
PO
84}
85
35c66c19
PO
86static 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
4ff6471c
PO
141static 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
006ebd5d
OBC
167/*
168 * 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
6b5eda36
DD
190/*
191 * 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
d16f5770
PO
224/*
225 * 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
17d33e14
NP
254/*
255 * 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,
3bca4cf7 261 struct mmc_card *oldcard, int powered_resume)
17d33e14
NP
262{
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 */
3bca4cf7
CB
272 if (!powered_resume) {
273 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
274 if (err)
275 goto err;
276 }
17d33e14
NP
277
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 /*
299 * For native busses: set card RCA and quit open drain mode.
300 */
3bca4cf7 301 if (!powered_resume && !mmc_host_is_spi(host)) {
17d33e14
NP
302 err = mmc_send_relative_addr(host, &card->rca);
303 if (err)
304 goto remove;
305
306 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
307 }
308
309 /*
310 * Select card, as all following commands rely on that.
311 */
3bca4cf7 312 if (!powered_resume && !mmc_host_is_spi(host)) {
17d33e14
NP
313 err = mmc_select_card(card);
314 if (err)
315 goto remove;
316 }
317
318 /*
319 * Read the common registers.
320 */
321 err = sdio_read_cccr(card);
322 if (err)
323 goto remove;
324
325 /*
326 * Read the common CIS tuples.
327 */
328 err = sdio_read_common_cis(card);
329 if (err)
330 goto remove;
331
332 if (oldcard) {
333 int same = (card->cis.vendor == oldcard->cis.vendor &&
334 card->cis.device == oldcard->cis.device);
335 mmc_remove_card(card);
336 if (!same) {
337 err = -ENOENT;
338 goto err;
339 }
340 card = oldcard;
95cdfb72 341 return 0;
17d33e14
NP
342 }
343
344 /*
345 * Switch to high-speed (if supported).
346 */
347 err = sdio_enable_hs(card);
348 if (err)
349 goto remove;
350
351 /*
352 * Change to the card's maximum speed.
353 */
354 if (mmc_card_highspeed(card)) {
355 /*
356 * The SDIO specification doesn't mention how
357 * the CIS transfer speed register relates to
358 * high-speed, but it seems that 50 MHz is
359 * mandatory.
360 */
361 mmc_set_clock(host, 50000000);
362 } else {
363 mmc_set_clock(host, card->cis.max_dtr);
364 }
365
366 /*
367 * Switch to wider bus (if supported).
368 */
369 err = sdio_enable_wide(card);
370 if (err)
371 goto remove;
372
373 if (!oldcard)
374 host->card = card;
375 return 0;
376
377remove:
378 if (!oldcard)
379 mmc_remove_card(card);
380
381err:
382 return err;
383}
384
5c4e6f13
PO
385/*
386 * Host is being removed. Free up the current card.
387 */
388static void mmc_sdio_remove(struct mmc_host *host)
389{
e29a7d73
PO
390 int i;
391
5c4e6f13
PO
392 BUG_ON(!host);
393 BUG_ON(!host->card);
394
e29a7d73
PO
395 for (i = 0;i < host->card->sdio_funcs;i++) {
396 if (host->card->sdio_func[i]) {
397 sdio_remove_func(host->card->sdio_func[i]);
398 host->card->sdio_func[i] = NULL;
399 }
400 }
401
5c4e6f13
PO
402 mmc_remove_card(host->card);
403 host->card = NULL;
404}
405
406/*
407 * Card detection callback from host.
408 */
409static void mmc_sdio_detect(struct mmc_host *host)
410{
411 int err;
412
413 BUG_ON(!host);
414 BUG_ON(!host->card);
415
416 mmc_claim_host(host);
417
418 /*
419 * Just check if our card has been removed.
420 */
421 err = mmc_select_card(host->card);
422
423 mmc_release_host(host);
424
425 if (err) {
426 mmc_sdio_remove(host);
427
428 mmc_claim_host(host);
429 mmc_detach_bus(host);
430 mmc_release_host(host);
431 }
432}
433
17d33e14
NP
434/*
435 * SDIO suspend. We need to suspend all functions separately.
436 * Therefore all registered functions must have drivers with suspend
437 * and resume methods. Failing that we simply remove the whole card.
438 */
95cdfb72 439static int mmc_sdio_suspend(struct mmc_host *host)
17d33e14 440{
95cdfb72 441 int i, err = 0;
17d33e14 442
17d33e14
NP
443 for (i = 0; i < host->card->sdio_funcs; i++) {
444 struct sdio_func *func = host->card->sdio_func[i];
445 if (func && sdio_func_present(func) && func->dev.driver) {
446 const struct dev_pm_ops *pmops = func->dev.driver->pm;
447 if (!pmops || !pmops->suspend || !pmops->resume) {
95cdfb72
NP
448 /* force removal of entire card in that case */
449 err = -ENOSYS;
450 } else
451 err = pmops->suspend(&func->dev);
452 if (err)
453 break;
17d33e14
NP
454 }
455 }
95cdfb72 456 while (err && --i >= 0) {
17d33e14
NP
457 struct sdio_func *func = host->card->sdio_func[i];
458 if (func && sdio_func_present(func) && func->dev.driver) {
459 const struct dev_pm_ops *pmops = func->dev.driver->pm;
95cdfb72 460 pmops->resume(&func->dev);
17d33e14
NP
461 }
462 }
95cdfb72 463
6b5eda36
DD
464 if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
465 mmc_claim_host(host);
466 sdio_disable_wide(host->card);
467 mmc_release_host(host);
468 }
469
95cdfb72 470 return err;
17d33e14
NP
471}
472
95cdfb72 473static int mmc_sdio_resume(struct mmc_host *host)
17d33e14
NP
474{
475 int i, err;
476
477 BUG_ON(!host);
478 BUG_ON(!host->card);
479
95cdfb72 480 /* Basic card reinitialization. */
17d33e14 481 mmc_claim_host(host);
3bca4cf7
CB
482 err = mmc_sdio_init_card(host, host->ocr, host->card,
483 (host->pm_flags & MMC_PM_KEEP_POWER));
6b5eda36
DD
484 if (!err)
485 /* We may have switched to 1-bit mode during suspend. */
486 err = sdio_enable_wide(host->card);
40216842
NP
487 if (!err && host->sdio_irqs)
488 mmc_signal_sdio_irq(host);
17d33e14 489 mmc_release_host(host);
17d33e14 490
95cdfb72
NP
491 /*
492 * If the card looked to be the same as before suspending, then
493 * we proceed to resume all card functions. If one of them returns
494 * an error then we simply return that error to the core and the
495 * card will be redetected as new. It is the responsibility of
496 * the function driver to perform further tests with the extra
497 * knowledge it has of the card to confirm the card is indeed the
498 * same as before suspending (same MAC address for network cards,
499 * etc.) and return an error otherwise.
500 */
501 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
17d33e14
NP
502 struct sdio_func *func = host->card->sdio_func[i];
503 if (func && sdio_func_present(func) && func->dev.driver) {
504 const struct dev_pm_ops *pmops = func->dev.driver->pm;
95cdfb72 505 err = pmops->resume(&func->dev);
17d33e14
NP
506 }
507 }
95cdfb72
NP
508
509 return err;
17d33e14 510}
5c4e6f13
PO
511
512static const struct mmc_bus_ops mmc_sdio_ops = {
513 .remove = mmc_sdio_remove,
514 .detect = mmc_sdio_detect,
17d33e14
NP
515 .suspend = mmc_sdio_suspend,
516 .resume = mmc_sdio_resume,
5c4e6f13
PO
517};
518
519
520/*
521 * Starting point for SDIO card init.
522 */
523int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
524{
525 int err;
e29a7d73 526 int i, funcs;
5c4e6f13
PO
527 struct mmc_card *card;
528
529 BUG_ON(!host);
d84075c8 530 WARN_ON(!host->claimed);
5c4e6f13
PO
531
532 mmc_attach_bus(host, &mmc_sdio_ops);
533
534 /*
535 * Sanity check the voltages that the card claims to
536 * support.
537 */
538 if (ocr & 0x7F) {
539 printk(KERN_WARNING "%s: card claims to support voltages "
540 "below the defined range. These will be ignored.\n",
541 mmc_hostname(host));
542 ocr &= ~0x7F;
543 }
544
5c4e6f13
PO
545 host->ocr = mmc_select_voltage(host, ocr);
546
547 /*
548 * Can we support the voltage(s) of the card(s)?
549 */
550 if (!host->ocr) {
551 err = -EINVAL;
552 goto err;
553 }
554
555 /*
17d33e14 556 * Detect and init the card.
5c4e6f13 557 */
3bca4cf7 558 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
5c4e6f13
PO
559 if (err)
560 goto err;
17d33e14 561 card = host->card;
af517150 562
5c4e6f13
PO
563 /*
564 * The number of functions on the card is encoded inside
565 * the ocr.
566 */
e8812793
MF
567 funcs = (ocr & 0x70000000) >> 28;
568 card->sdio_funcs = 0;
4ff6471c 569
006ebd5d
OBC
570 /*
571 * If needed, disconnect card detection pull-up resistor.
572 */
573 err = sdio_disable_cd(card);
574 if (err)
575 goto remove;
576
e29a7d73
PO
577 /*
578 * Initialize (but don't add) all present functions.
579 */
e8812793 580 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
e29a7d73
PO
581 err = sdio_init_func(host->card, i + 1);
582 if (err)
583 goto remove;
584 }
5c4e6f13
PO
585
586 mmc_release_host(host);
587
e29a7d73
PO
588 /*
589 * First add the card to the driver model...
590 */
5c4e6f13
PO
591 err = mmc_add_card(host->card);
592 if (err)
e29a7d73
PO
593 goto remove_added;
594
595 /*
596 * ...then the SDIO functions.
597 */
598 for (i = 0;i < funcs;i++) {
599 err = sdio_add_func(host->card->sdio_func[i]);
600 if (err)
601 goto remove_added;
602 }
5c4e6f13
PO
603
604 return 0;
605
e29a7d73
PO
606
607remove_added:
608 /* Remove without lock if the device has been added. */
609 mmc_sdio_remove(host);
5c4e6f13 610 mmc_claim_host(host);
e29a7d73
PO
611remove:
612 /* And with lock if it hasn't been added. */
613 if (host->card)
614 mmc_sdio_remove(host);
5c4e6f13
PO
615err:
616 mmc_detach_bus(host);
617 mmc_release_host(host);
618
619 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
620 mmc_hostname(host), err);
621
622 return err;
623}
624
This page took 0.389142 seconds and 4 git commands to generate.