Line data Source code
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>
16 : #include <linux/mmc/sdio.h>
17 : #include <linux/mmc/sdio_func.h>
18 :
19 : #include "core.h"
20 : #include "bus.h"
21 : #include "sdio_bus.h"
22 : #include "mmc_ops.h"
23 : #include "sd_ops.h"
24 : #include "sdio_ops.h"
25 : #include "sdio_cis.h"
26 :
27 : static int sdio_read_fbr(struct sdio_func *func)
28 : {
29 0 : int ret;
30 0 : unsigned char data;
31 :
32 0 : ret = mmc_io_rw_direct(func->card, 0, 0,
33 : SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
34 0 : if (ret)
35 0 : goto out;
36 :
37 0 : data &= 0x0f;
38 :
39 0 : if (data == 0x0f) {
40 0 : ret = mmc_io_rw_direct(func->card, 0, 0,
41 : SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
42 0 : if (ret)
43 0 : goto out;
44 : }
45 :
46 0 : func->class = data;
47 0 :
48 : out:
49 0 : return ret;
50 : }
51 :
52 : static int sdio_init_func(struct mmc_card *card, unsigned int fn)
53 : {
54 0 : int ret;
55 0 : struct sdio_func *func;
56 0 :
57 0 : BUG_ON(fn > SDIO_MAX_FUNCS);
58 0 :
59 0 : func = sdio_alloc_func(card);
60 0 : if (IS_ERR(func))
61 0 : return PTR_ERR(func);
62 :
63 0 : func->num = fn;
64 :
65 0 : ret = sdio_read_fbr(func);
66 0 : if (ret)
67 0 : goto fail;
68 :
69 0 : ret = sdio_read_func_cis(func);
70 0 : if (ret)
71 0 : goto fail;
72 :
73 0 : card->sdio_func[fn - 1] = func;
74 :
75 0 : return 0;
76 0 :
77 : fail:
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 0 : sdio_remove_func(func);
83 0 : return ret;
84 : }
85 :
86 : static int sdio_read_cccr(struct mmc_card *card)
87 : {
88 3 : int ret;
89 3 : int cccr_vsn;
90 3 : unsigned char data;
91 3 :
92 3 : memset(&card->cccr, 0, sizeof(struct sdio_cccr));
93 :
94 9 : ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
95 6 : if (ret)
96 3 : goto out;
97 :
98 3 : cccr_vsn = data & 0x0f;
99 :
100 6 : if (cccr_vsn > SDIO_CCCR_REV_1_20) {
101 9 : printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
102 : mmc_hostname(card->host), cccr_vsn);
103 3 : return -EINVAL;
104 : }
105 :
106 3 : card->cccr.sdio_vsn = (data & 0xf0) >> 4;
107 :
108 9 : ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
109 6 : if (ret)
110 3 : goto out;
111 :
112 6 : if (data & SDIO_CCCR_CAP_SMB)
113 3 : card->cccr.multi_block = 1;
114 6 : if (data & SDIO_CCCR_CAP_LSC)
115 3 : card->cccr.low_speed = 1;
116 6 : if (data & SDIO_CCCR_CAP_4BLS)
117 3 : card->cccr.wide_bus = 1;
118 :
119 6 : if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
120 9 : ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
121 6 : if (ret)
122 3 : goto out;
123 :
124 6 : if (data & SDIO_POWER_SMPC)
125 3 : card->cccr.high_power = 1;
126 : }
127 :
128 12 : if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
129 18 : ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
130 6 : if (ret)
131 3 : goto out;
132 :
133 6 : if (data & SDIO_SPEED_SHS)
134 3 : card->cccr.high_speed = 1;
135 : }
136 :
137 : out:
138 12 : return ret;
139 : }
140 :
141 9 : static int sdio_enable_wide(struct mmc_card *card)
142 : {
143 2 : int ret;
144 2 : u8 ctrl;
145 :
146 4 : if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
147 2 : return 0;
148 :
149 4 : if (card->cccr.low_speed && !card->cccr.wide_bus)
150 2 : return 0;
151 :
152 6 : ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
153 4 : if (ret)
154 2 : return ret;
155 :
156 2 : ctrl |= SDIO_BUS_WIDTH_4BIT;
157 :
158 6 : ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
159 4 : if (ret)
160 2 : return ret;
161 :
162 4 : mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
163 :
164 2 : return 0;
165 : }
166 :
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 : */
173 : static int sdio_disable_cd(struct mmc_card *card)
174 : {
175 0 : int ret;
176 0 : u8 ctrl;
177 0 :
178 0 : if (!card->cccr.disable_cd)
179 0 : return 0;
180 :
181 0 : ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
182 0 : if (ret)
183 0 : return ret;
184 :
185 0 : ctrl |= SDIO_BUS_CD_DISABLE;
186 :
187 0 : return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
188 : }
189 :
190 : /*
191 : * Test if the card supports high-speed mode and, if so, switch to it.
192 : */
193 : static int sdio_enable_hs(struct mmc_card *card)
194 : {
195 1 : int ret;
196 1 : u8 speed;
197 :
198 2 : if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
199 1 : return 0;
200 :
201 2 : if (!card->cccr.high_speed)
202 1 : return 0;
203 :
204 3 : ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
205 2 : if (ret)
206 1 : return ret;
207 :
208 1 : speed |= SDIO_SPEED_EHS;
209 :
210 3 : ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
211 2 : if (ret)
212 1 : return ret;
213 :
214 1 : mmc_card_set_highspeed(card);
215 2 : mmc_set_timing(card->host, MMC_TIMING_SD_HS);
216 :
217 1 : return 0;
218 : }
219 :
220 : /*
221 : * Handle the detection and initialisation of a card.
222 : *
223 : * In the case of a resume, "oldcard" will contain the card
224 : * we're trying to reinitialise.
225 : */
226 : static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
227 : struct mmc_card *oldcard)
228 1 : {
229 1 : struct mmc_card *card;
230 1 : int err;
231 1 :
232 7 : BUG_ON(!host);
233 9 : WARN_ON(!host->claimed);
234 1 :
235 1 : /*
236 : * Inform the card of the voltage
237 : */
238 3 : err = mmc_send_io_op_cond(host, host->ocr, &ocr);
239 2 : if (err)
240 1 : goto err;
241 :
242 : /*
243 : * For SPI, enable CRC as appropriate.
244 : */
245 2 : if (mmc_host_is_spi(host)) {
246 2 : err = mmc_spi_set_crc(host, use_spi_crc);
247 2 : if (err)
248 1 : goto err;
249 : }
250 :
251 : /*
252 : * Allocate card structure.
253 : */
254 6 : card = mmc_alloc_card(host, NULL);
255 4 : if (IS_ERR(card)) {
256 3 : err = PTR_ERR(card);
257 1 : goto err;
258 : }
259 :
260 1 : card->type = MMC_TYPE_SDIO;
261 :
262 : /*
263 : * For native busses: set card RCA and quit open drain mode.
264 : */
265 2 : if (!mmc_host_is_spi(host)) {
266 2 : err = mmc_send_relative_addr(host, &card->rca);
267 2 : if (err)
268 1 : goto remove;
269 :
270 : /*
271 : * Update oldcard with the new RCA received from the SDIO
272 : * device -- we're doing this so that it's updated in the
273 : * "card" struct when oldcard overwrites that later.
274 : */
275 2 : if (oldcard)
276 1 : oldcard->rca = card->rca;
277 :
278 2 : mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
279 : }
280 :
281 : /*
282 : * Select card, as all following commands rely on that.
283 : */
284 4 : if (!mmc_host_is_spi(host)) {
285 4 : err = mmc_select_card(card);
286 2 : if (err)
287 1 : goto remove;
288 : }
289 :
290 : /*
291 : * Read the common registers.
292 : */
293 18 : err = sdio_read_cccr(card);
294 2 : if (err)
295 1 : goto remove;
296 :
297 : /*
298 : * Read the common CIS tuples.
299 : */
300 2 : err = sdio_read_common_cis(card);
301 2 : if (err)
302 1 : goto remove;
303 :
304 2 : if (oldcard) {
305 1 : int same = (card->cis.vendor == oldcard->cis.vendor &&
306 : card->cis.device == oldcard->cis.device);
307 4 : mmc_remove_card(card);
308 2 : if (!same) {
309 1 : err = -ENOENT;
310 1 : goto err;
311 : }
312 1 : card = oldcard;
313 1 : return 0;
314 : }
315 :
316 : /*
317 : * Switch to high-speed (if supported).
318 : */
319 5 : err = sdio_enable_hs(card);
320 2 : if (err)
321 1 : goto remove;
322 :
323 : /*
324 : * Change to the card's maximum speed.
325 : */
326 2 : if (mmc_card_highspeed(card)) {
327 : /*
328 : * The SDIO specification doesn't mention how
329 : * the CIS transfer speed register relates to
330 : * high-speed, but it seems that 50 MHz is
331 : * mandatory.
332 : */
333 2 : mmc_set_clock(host, 50000000);
334 : } else {
335 2 : mmc_set_clock(host, card->cis.max_dtr);
336 : }
337 :
338 : /*
339 : * Switch to wider bus (if supported).
340 : */
341 10 : err = sdio_enable_wide(card);
342 2 : if (err)
343 1 : goto remove;
344 :
345 2 : if (!oldcard)
346 1 : host->card = card;
347 1 : return 0;
348 6 :
349 : remove:
350 12 : if (!oldcard)
351 24 : mmc_remove_card(card);
352 :
353 : err:
354 11 : return err;
355 : }
356 7 :
357 : /*
358 : * Host is being removed. Free up the current card.
359 : */
360 : static void mmc_sdio_remove(struct mmc_host *host)
361 : {
362 2 : int i;
363 2 :
364 14 : BUG_ON(!host);
365 14 : BUG_ON(!host->card);
366 :
367 12 : for (i = 0;i < host->card->sdio_funcs;i++) {
368 10 : if (host->card->sdio_func[i]) {
369 6 : sdio_remove_func(host->card->sdio_func[i]);
370 2 : host->card->sdio_func[i] = NULL;
371 : }
372 : }
373 :
374 8 : mmc_remove_card(host->card);
375 2 : host->card = NULL;
376 2 : }
377 :
378 : /*
379 : * Card detection callback from host.
380 : */
381 : static void mmc_sdio_detect(struct mmc_host *host)
382 : {
383 1 : int err;
384 1 :
385 7 : BUG_ON(!host);
386 7 : BUG_ON(!host->card);
387 :
388 2 : mmc_claim_host(host);
389 :
390 : /*
391 : * Just check if our card has been removed.
392 : */
393 2 : err = mmc_select_card(host->card);
394 :
395 2 : mmc_release_host(host);
396 :
397 2 : if (err) {
398 2 : mmc_sdio_remove(host);
399 :
400 2 : mmc_claim_host(host);
401 2 : mmc_detach_bus(host);
402 2 : mmc_release_host(host);
403 : }
404 2 : }
405 :
406 : /*
407 : * SDIO suspend. We need to suspend all functions separately.
408 : * Therefore all registered functions must have drivers with suspend
409 : * and resume methods. Failing that we simply remove the whole card.
410 : */
411 : static int mmc_sdio_suspend(struct mmc_host *host)
412 : {
413 2 : int i, err = 0;
414 1 :
415 6 : for (i = 0; i < host->card->sdio_funcs; i++) {
416 4 : struct sdio_func *func = host->card->sdio_func[i];
417 9 : if (func && sdio_func_present(func) && func->dev.driver) {
418 2 : const struct dev_pm_ops *pmops = func->dev.driver->pm;
419 8 : if (!pmops || !pmops->suspend || !pmops->resume) {
420 : /* force removal of entire card in that case */
421 2 : err = -ENOSYS;
422 : } else
423 7 : err = pmops->suspend(&func->dev);
424 2 : if (err)
425 1 : break;
426 : }
427 : }
428 6 : while (err && --i >= 0) {
429 2 : struct sdio_func *func = host->card->sdio_func[i];
430 8 : if (func && sdio_func_present(func) && func->dev.driver) {
431 1 : const struct dev_pm_ops *pmops = func->dev.driver->pm;
432 7 : pmops->resume(&func->dev);
433 1 : }
434 : }
435 :
436 1 : return err;
437 1 : }
438 1 :
439 : static int mmc_sdio_resume(struct mmc_host *host)
440 : {
441 1 : int i, err;
442 1 :
443 7 : BUG_ON(!host);
444 8 : BUG_ON(!host->card);
445 1 :
446 1 : /* Basic card reinitialization. */
447 2 : mmc_claim_host(host);
448 12 : err = mmc_sdio_init_card(host, host->ocr, host->card);
449 2 : mmc_release_host(host);
450 :
451 : /*
452 : * If the card looked to be the same as before suspending, then
453 : * we proceed to resume all card functions. If one of them returns
454 : * an error then we simply return that error to the core and the
455 : * card will be redetected as new. It is the responsibility of
456 : * the function driver to perform further tests with the extra
457 : * knowledge it has of the card to confirm the card is indeed the
458 : * same as before suspending (same MAC address for network cards,
459 : * etc.) and return an error otherwise.
460 : */
461 7 : for (i = 0; !err && i < host->card->sdio_funcs; i++) {
462 3 : struct sdio_func *func = host->card->sdio_func[i];
463 8 : if (func && sdio_func_present(func) && func->dev.driver) {
464 1 : const struct dev_pm_ops *pmops = func->dev.driver->pm;
465 7 : err = pmops->resume(&func->dev);
466 : }
467 : }
468 :
469 1 : return err;
470 : }
471 :
472 1 : static const struct mmc_bus_ops mmc_sdio_ops = {
473 : .remove = mmc_sdio_remove,
474 : .detect = mmc_sdio_detect,
475 : .suspend = mmc_sdio_suspend,
476 : .resume = mmc_sdio_resume,
477 : };
478 :
479 :
480 : /*
481 : * Starting point for SDIO card init.
482 : */
483 : int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
484 : {
485 0 : int err;
486 0 : int i, funcs;
487 0 : struct mmc_card *card;
488 0 :
489 0 : BUG_ON(!host);
490 0 : WARN_ON(!host->claimed);
491 0 :
492 0 : mmc_attach_bus(host, &mmc_sdio_ops);
493 0 :
494 : /*
495 : * Sanity check the voltages that the card claims to
496 : * support.
497 : */
498 0 : if (ocr & 0x7F) {
499 0 : printk(KERN_WARNING "%s: card claims to support voltages "
500 : "below the defined range. These will be ignored.\n",
501 : mmc_hostname(host));
502 0 : ocr &= ~0x7F;
503 : }
504 :
505 0 : host->ocr = mmc_select_voltage(host, ocr);
506 :
507 : /*
508 : * Can we support the voltage(s) of the card(s)?
509 : */
510 0 : if (!host->ocr) {
511 0 : err = -EINVAL;
512 0 : goto err;
513 : }
514 :
515 : /*
516 : * Detect and init the card.
517 : */
518 0 : err = mmc_sdio_init_card(host, host->ocr, NULL);
519 0 : if (err)
520 0 : goto err;
521 0 : card = host->card;
522 :
523 : /*
524 : * The number of functions on the card is encoded inside
525 : * the ocr.
526 : */
527 0 : funcs = (ocr & 0x70000000) >> 28;
528 0 : card->sdio_funcs = 0;
529 :
530 : /*
531 : * If needed, disconnect card detection pull-up resistor.
532 : */
533 0 : err = sdio_disable_cd(card);
534 0 : if (err)
535 0 : goto remove;
536 :
537 : /*
538 : * Initialize (but don't add) all present functions.
539 : */
540 0 : for (i = 0; i < funcs; i++, card->sdio_funcs++) {
541 0 : err = sdio_init_func(host->card, i + 1);
542 0 : if (err)
543 0 : goto remove;
544 : }
545 :
546 0 : mmc_release_host(host);
547 :
548 : /*
549 : * First add the card to the driver model...
550 : */
551 0 : err = mmc_add_card(host->card);
552 0 : if (err)
553 0 : goto remove_added;
554 :
555 : /*
556 : * ...then the SDIO functions.
557 : */
558 0 : for (i = 0;i < funcs;i++) {
559 0 : err = sdio_add_func(host->card->sdio_func[i]);
560 0 : if (err)
561 0 : goto remove_added;
562 : }
563 :
564 0 : return 0;
565 0 :
566 :
567 : remove_added:
568 : /* Remove without lock if the device has been added. */
569 0 : mmc_sdio_remove(host);
570 0 : mmc_claim_host(host);
571 : remove:
572 0 : /* And with lock if it hasn't been added. */
573 0 : if (host->card)
574 0 : mmc_sdio_remove(host);
575 : err:
576 0 : mmc_detach_bus(host);
577 0 : mmc_release_host(host);
578 :
579 0 : printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
580 : mmc_hostname(host), err);
581 :
582 0 : return err;
583 : }
584 :
|