]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/core/sdio.c
62df8e177585b9668629faacf3f5564a6b259990
[linux-2.6-omap-h63xx.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                 func->num * 0x100 + 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                         func->num * 0x100 + 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         ret = sdio_read_fbr(func);
66         if (ret)
67                 goto fail;
68
69         ret = sdio_read_func_cis(func);
70         if (ret)
71                 goto fail;
72
73         card->sdio_func[fn - 1] = func;
74
75         return 0;
76
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         sdio_remove_func(func);
83         return ret;
84 }
85
86 static 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
137 out:
138         return ret;
139 }
140
141 /*
142  * Host is being removed. Free up the current card.
143  */
144 static void mmc_sdio_remove(struct mmc_host *host)
145 {
146         int i;
147
148         BUG_ON(!host);
149         BUG_ON(!host->card);
150
151         for (i = 0;i < host->card->sdio_funcs;i++) {
152                 if (host->card->sdio_func[i]) {
153                         sdio_remove_func(host->card->sdio_func[i]);
154                         host->card->sdio_func[i] = NULL;
155                 }
156         }
157
158         mmc_remove_card(host->card);
159         host->card = NULL;
160 }
161
162 /*
163  * Card detection callback from host.
164  */
165 static void mmc_sdio_detect(struct mmc_host *host)
166 {
167         int err;
168
169         BUG_ON(!host);
170         BUG_ON(!host->card);
171
172         mmc_claim_host(host);
173
174         /*
175          * Just check if our card has been removed.
176          */
177         err = mmc_select_card(host->card);
178
179         mmc_release_host(host);
180
181         if (err) {
182                 mmc_sdio_remove(host);
183
184                 mmc_claim_host(host);
185                 mmc_detach_bus(host);
186                 mmc_release_host(host);
187         }
188 }
189
190
191 static const struct mmc_bus_ops mmc_sdio_ops = {
192         .remove = mmc_sdio_remove,
193         .detect = mmc_sdio_detect,
194 };
195
196
197 /*
198  * Starting point for SDIO card init.
199  */
200 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
201 {
202         int err;
203         int i, funcs;
204         struct mmc_card *card;
205
206         BUG_ON(!host);
207         BUG_ON(!host->claimed);
208
209         mmc_attach_bus(host, &mmc_sdio_ops);
210
211         /*
212          * Sanity check the voltages that the card claims to
213          * support.
214          */
215         if (ocr & 0x7F) {
216                 printk(KERN_WARNING "%s: card claims to support voltages "
217                        "below the defined range. These will be ignored.\n",
218                        mmc_hostname(host));
219                 ocr &= ~0x7F;
220         }
221
222         if (ocr & MMC_VDD_165_195) {
223                 printk(KERN_WARNING "%s: SDIO card claims to support the "
224                        "incompletely defined 'low voltage range'. This "
225                        "will be ignored.\n", mmc_hostname(host));
226                 ocr &= ~MMC_VDD_165_195;
227         }
228
229         host->ocr = mmc_select_voltage(host, ocr);
230
231         /*
232          * Can we support the voltage(s) of the card(s)?
233          */
234         if (!host->ocr) {
235                 err = -EINVAL;
236                 goto err;
237         }
238
239         /*
240          * Inform the card of the voltage
241          */
242         err = mmc_send_io_op_cond(host, host->ocr, &ocr);
243         if (err)
244                 goto err;
245
246         /*
247          * The number of functions on the card is encoded inside
248          * the ocr.
249          */
250         funcs = (ocr & 0x70000000) >> 28;
251
252         /*
253          * Allocate card structure.
254          */
255         card = mmc_alloc_card(host);
256         if (IS_ERR(card)) {
257                 err = PTR_ERR(card);
258                 goto err;
259         }
260
261         card->type = MMC_TYPE_SDIO;
262         card->sdio_funcs = funcs;
263
264         host->card = card;
265
266         /*
267          * Set card RCA.
268          */
269         err = mmc_send_relative_addr(host, &card->rca);
270         if (err)
271                 goto remove;
272
273         mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
274
275         /*
276          * Select card, as all following commands rely on that.
277          */
278         err = mmc_select_card(card);
279         if (err)
280                 goto remove;
281
282         /*
283          * Read the common registers.
284          */
285         err = sdio_read_cccr(card);
286         if (err)
287                 goto remove;
288
289         /*
290          * Read the common CIS tuples.
291          */
292         err = sdio_read_common_cis(card);
293         if (err)
294                 goto remove;
295
296         /*
297          * No support for high-speed yet, so just set
298          * the card's maximum speed.
299          */
300         mmc_set_clock(host, card->cis.max_dtr);
301
302         /*
303          * Initialize (but don't add) all present functions.
304          */
305         for (i = 0;i < funcs;i++) {
306                 err = sdio_init_func(host->card, i + 1);
307                 if (err)
308                         goto remove;
309         }
310
311         mmc_release_host(host);
312
313         /*
314          * First add the card to the driver model...
315          */
316         err = mmc_add_card(host->card);
317         if (err)
318                 goto remove_added;
319
320         /*
321          * ...then the SDIO functions.
322          */
323         for (i = 0;i < funcs;i++) {
324                 err = sdio_add_func(host->card->sdio_func[i]);
325                 if (err)
326                         goto remove_added;
327         }
328
329         return 0;
330
331
332 remove_added:
333         /* Remove without lock if the device has been added. */
334         mmc_sdio_remove(host);
335         mmc_claim_host(host);
336 remove:
337         /* And with lock if it hasn't been added. */
338         if (host->card)
339                 mmc_sdio_remove(host);
340 err:
341         mmc_detach_bus(host);
342         mmc_release_host(host);
343
344         printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
345                 mmc_hostname(host), err);
346
347         return err;
348 }
349