7d4f47143604ec4b3e8e3b1deb629066b8d5fb21
[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 #ifdef CONFIG_MMC_EMBEDDED_SDIO
28 #include <linux/mmc/sdio_ids.h>
29 #endif
30
31 static int sdio_read_fbr(struct sdio_func *func)
32 {
33         int ret;
34         unsigned char data;
35
36         ret = mmc_io_rw_direct(func->card, 0, 0,
37                 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
38         if (ret)
39                 goto out;
40
41         data &= 0x0f;
42
43         if (data == 0x0f) {
44                 ret = mmc_io_rw_direct(func->card, 0, 0,
45                         SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
46                 if (ret)
47                         goto out;
48         }
49
50         func->class = data;
51
52 out:
53         return ret;
54 }
55
56 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
57 {
58         int ret;
59         struct sdio_func *func;
60
61         BUG_ON(fn > SDIO_MAX_FUNCS);
62
63         func = sdio_alloc_func(card);
64         if (IS_ERR(func))
65                 return PTR_ERR(func);
66
67         func->num = fn;
68
69         ret = sdio_read_fbr(func);
70         if (ret)
71                 goto fail;
72
73         ret = sdio_read_func_cis(func);
74         if (ret)
75                 goto fail;
76
77         card->sdio_func[fn - 1] = func;
78
79         return 0;
80
81 fail:
82         /*
83          * It is okay to remove the function here even though we hold
84          * the host lock as we haven't registered the device yet.
85          */
86         sdio_remove_func(func);
87         return ret;
88 }
89
90 static int sdio_read_cccr(struct mmc_card *card)
91 {
92         int ret;
93         int cccr_vsn;
94         unsigned char data;
95
96         memset(&card->cccr, 0, sizeof(struct sdio_cccr));
97
98         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
99         if (ret)
100                 goto out;
101
102         cccr_vsn = data & 0x0f;
103
104         if (cccr_vsn > SDIO_CCCR_REV_1_20) {
105                 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
106                         mmc_hostname(card->host), cccr_vsn);
107                 return -EINVAL;
108         }
109
110         card->cccr.sdio_vsn = (data & 0xf0) >> 4;
111
112         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
113         if (ret)
114                 goto out;
115
116         if (data & SDIO_CCCR_CAP_SMB)
117                 card->cccr.multi_block = 1;
118         if (data & SDIO_CCCR_CAP_LSC)
119                 card->cccr.low_speed = 1;
120         if (data & SDIO_CCCR_CAP_4BLS)
121                 card->cccr.wide_bus = 1;
122
123         if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
124                 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
125                 if (ret)
126                         goto out;
127
128                 if (data & SDIO_POWER_SMPC)
129                         card->cccr.high_power = 1;
130         }
131
132         if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
133                 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
134                 if (ret)
135                         goto out;
136
137                 if (data & SDIO_SPEED_SHS)
138                         card->cccr.high_speed = 1;
139         }
140
141 out:
142         return ret;
143 }
144
145 static int sdio_enable_wide(struct mmc_card *card)
146 {
147         int ret;
148         u8 ctrl;
149
150         if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
151                 return 0;
152
153         if (card->cccr.low_speed && !card->cccr.wide_bus)
154                 return 0;
155
156         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
157         if (ret)
158                 return ret;
159
160         ctrl |= SDIO_BUS_WIDTH_4BIT;
161
162         ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
163         if (ret)
164                 return ret;
165
166         mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
167
168         return 0;
169 }
170
171 /*
172  * Host is being removed. Free up the current card.
173  */
174 static void mmc_sdio_remove(struct mmc_host *host)
175 {
176         int i;
177
178         BUG_ON(!host);
179         BUG_ON(!host->card);
180
181         for (i = 0;i < host->card->sdio_funcs;i++) {
182                 if (host->card->sdio_func[i]) {
183                         sdio_remove_func(host->card->sdio_func[i]);
184                         host->card->sdio_func[i] = NULL;
185                 }
186         }
187
188         mmc_remove_card(host->card);
189         host->card = NULL;
190 }
191
192 /*
193  * Card detection callback from host.
194  */
195 static void mmc_sdio_detect(struct mmc_host *host)
196 {
197         int err;
198
199         BUG_ON(!host);
200         BUG_ON(!host->card);
201
202         mmc_claim_host(host);
203
204         /*
205          * Just check if our card has been removed.
206          */
207         err = mmc_select_card(host->card);
208
209         mmc_release_host(host);
210
211         if (err) {
212                 mmc_sdio_remove(host);
213
214                 mmc_claim_host(host);
215                 mmc_detach_bus(host);
216                 mmc_release_host(host);
217         }
218 }
219
220
221 static const struct mmc_bus_ops mmc_sdio_ops = {
222         .remove = mmc_sdio_remove,
223         .detect = mmc_sdio_detect,
224 };
225
226
227 /*
228  * Starting point for SDIO card init.
229  */
230 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
231 {
232         int err;
233         int i, funcs;
234         struct mmc_card *card;
235
236         BUG_ON(!host);
237         WARN_ON(!host->claimed);
238
239         mmc_attach_bus(host, &mmc_sdio_ops);
240
241         /*
242          * Sanity check the voltages that the card claims to
243          * support.
244          */
245         if (ocr & 0x7F) {
246                 printk(KERN_WARNING "%s: card claims to support voltages "
247                        "below the defined range. These will be ignored.\n",
248                        mmc_hostname(host));
249                 ocr &= ~0x7F;
250         }
251
252         if (ocr & MMC_VDD_165_195) {
253                 printk(KERN_WARNING "%s: SDIO card claims to support the "
254                        "incompletely defined 'low voltage range'. This "
255                        "will be ignored.\n", mmc_hostname(host));
256                 ocr &= ~MMC_VDD_165_195;
257         }
258
259         host->ocr = mmc_select_voltage(host, ocr);
260
261         /*
262          * Can we support the voltage(s) of the card(s)?
263          */
264         if (!host->ocr) {
265                 err = -EINVAL;
266                 goto err;
267         }
268
269         /*
270          * Inform the card of the voltage
271          */
272         err = mmc_send_io_op_cond(host, host->ocr, &ocr);
273         if (err)
274                 goto err;
275
276         /*
277          * For SPI, enable CRC as appropriate.
278          */
279         if (mmc_host_is_spi(host)) {
280                 err = mmc_spi_set_crc(host, use_spi_crc);
281                 if (err)
282                         goto err;
283         }
284
285         /*
286          * The number of functions on the card is encoded inside
287          * the ocr.
288          */
289         funcs = (ocr & 0x70000000) >> 28;
290
291 #ifdef CONFIG_MMC_EMBEDDED_SDIO
292         if (host->embedded_sdio_data.funcs)
293                 funcs = host->embedded_sdio_data.num_funcs;
294 #endif
295
296         /*
297          * Allocate card structure.
298          */
299         card = mmc_alloc_card(host, NULL);
300         if (IS_ERR(card)) {
301                 err = PTR_ERR(card);
302                 goto err;
303         }
304
305         card->type = MMC_TYPE_SDIO;
306         card->sdio_funcs = funcs;
307
308         host->card = card;
309
310         /*
311          * For native busses:  set card RCA and quit open drain mode.
312          */
313         if (!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 (!mmc_host_is_spi(host)) {
325                 err = mmc_select_card(card);
326                 if (err)
327                         goto remove;
328         }
329
330         /*
331          * Read the common registers.
332          */
333
334 #ifdef CONFIG_MMC_EMBEDDED_SDIO
335         if (host->embedded_sdio_data.cccr)
336                 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
337         else {
338 #endif
339                 err = sdio_read_cccr(card);
340                 if (err)
341                         goto remove;
342 #ifdef CONFIG_MMC_EMBEDDED_SDIO
343         }
344 #endif
345
346 #ifdef CONFIG_MMC_EMBEDDED_SDIO
347         if (host->embedded_sdio_data.cis)
348                 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
349         else {
350 #endif
351                 /*
352                  * Read the common CIS tuples.
353                  */
354                 err = sdio_read_common_cis(card);
355                 if (err)
356                         goto remove;
357 #ifdef CONFIG_MMC_EMBEDDED_SDIO
358         }
359 #endif
360         /*
361          * No support for high-speed yet, so just set
362          * the card's maximum speed.
363          */
364         mmc_set_clock(host, card->cis.max_dtr);
365
366         /*
367          * Switch to wider bus (if supported).
368          */
369         err = sdio_enable_wide(card);
370         if (err)
371                 goto remove;
372
373         /*
374          * Initialize (but don't add) all present functions.
375          */
376         for (i = 0;i < funcs;i++) {
377 #ifdef CONFIG_MMC_EMBEDDED_SDIO
378                 if (host->embedded_sdio_data.funcs) {
379                         struct sdio_func *tmp;
380
381                         tmp = sdio_alloc_func(host->card);
382                         if (IS_ERR(tmp))
383                                 goto remove;
384                         tmp->num = (i + 1);
385                         card->sdio_func[i] = tmp;
386                         tmp->class = host->embedded_sdio_data.funcs[i].f_class;
387                         tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
388                         tmp->vendor = card->cis.vendor;
389                         tmp->device = card->cis.device;
390                 } else {
391 #endif
392                         err = sdio_init_func(host->card, i + 1);
393                         if (err)
394                                 goto remove;
395 #ifdef CONFIG_MMC_EMBEDDED_SDIO
396                 }
397 #endif
398         }
399
400         mmc_release_host(host);
401
402         /*
403          * First add the card to the driver model...
404          */
405         err = mmc_add_card(host->card);
406         if (err)
407                 goto remove_added;
408
409         /*
410          * ...then the SDIO functions.
411          */
412         for (i = 0;i < funcs;i++) {
413                 err = sdio_add_func(host->card->sdio_func[i]);
414                 if (err)
415                         goto remove_added;
416         }
417
418         return 0;
419
420
421 remove_added:
422         /* Remove without lock if the device has been added. */
423         mmc_sdio_remove(host);
424         mmc_claim_host(host);
425 remove:
426         /* And with lock if it hasn't been added. */
427         if (host->card)
428                 mmc_sdio_remove(host);
429 err:
430         mmc_detach_bus(host);
431         mmc_release_host(host);
432
433         printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
434                 mmc_hostname(host), err);
435
436         return err;
437 }
438