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