USB: usbtmc: fix switch statment
[linux-2.6/mini2440.git] / drivers / mmc / core / sdio.c
blobfb99ccff908077f1a5bdf1cdc78099fd0ecd19c2
1 /*
2 * linux/drivers/mmc/sdio.c
4 * Copyright 2006-2007 Pierre Ossman
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.
12 #include <linux/err.h>
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>
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"
27 static int sdio_read_fbr(struct sdio_func *func)
29 int ret;
30 unsigned char data;
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;
37 data &= 0x0f;
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;
46 func->class = data;
48 out:
49 return ret;
52 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
54 int ret;
55 struct sdio_func *func;
57 BUG_ON(fn > SDIO_MAX_FUNCS);
59 func = sdio_alloc_func(card);
60 if (IS_ERR(func))
61 return PTR_ERR(func);
63 func->num = fn;
65 ret = sdio_read_fbr(func);
66 if (ret)
67 goto fail;
69 ret = sdio_read_func_cis(func);
70 if (ret)
71 goto fail;
73 card->sdio_func[fn - 1] = func;
75 return 0;
77 fail:
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.
82 sdio_remove_func(func);
83 return ret;
86 static int sdio_read_cccr(struct mmc_card *card)
88 int ret;
89 int cccr_vsn;
90 unsigned char data;
92 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
94 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
95 if (ret)
96 goto out;
98 cccr_vsn = data & 0x0f;
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;
106 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
108 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
109 if (ret)
110 goto out;
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;
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;
124 if (data & SDIO_POWER_SMPC)
125 card->cccr.high_power = 1;
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;
133 if (data & SDIO_SPEED_SHS)
134 card->cccr.high_speed = 1;
137 out:
138 return ret;
141 static int sdio_enable_wide(struct mmc_card *card)
143 int ret;
144 u8 ctrl;
146 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
147 return 0;
149 if (card->cccr.low_speed && !card->cccr.wide_bus)
150 return 0;
152 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
153 if (ret)
154 return ret;
156 ctrl |= SDIO_BUS_WIDTH_4BIT;
158 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
159 if (ret)
160 return ret;
162 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
164 return 0;
168 * Test if the card supports high-speed mode and, if so, switch to it.
170 static int sdio_enable_hs(struct mmc_card *card)
172 int ret;
173 u8 speed;
175 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
176 return 0;
178 if (!card->cccr.high_speed)
179 return 0;
181 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
182 if (ret)
183 return ret;
185 speed |= SDIO_SPEED_EHS;
187 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
188 if (ret)
189 return ret;
191 mmc_card_set_highspeed(card);
192 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
194 return 0;
198 * Host is being removed. Free up the current card.
200 static void mmc_sdio_remove(struct mmc_host *host)
202 int i;
204 BUG_ON(!host);
205 BUG_ON(!host->card);
207 for (i = 0;i < host->card->sdio_funcs;i++) {
208 if (host->card->sdio_func[i]) {
209 sdio_remove_func(host->card->sdio_func[i]);
210 host->card->sdio_func[i] = NULL;
214 mmc_remove_card(host->card);
215 host->card = NULL;
219 * Card detection callback from host.
221 static void mmc_sdio_detect(struct mmc_host *host)
223 int err;
225 BUG_ON(!host);
226 BUG_ON(!host->card);
228 mmc_claim_host(host);
231 * Just check if our card has been removed.
233 err = mmc_select_card(host->card);
235 mmc_release_host(host);
237 if (err) {
238 mmc_sdio_remove(host);
240 mmc_claim_host(host);
241 mmc_detach_bus(host);
242 mmc_release_host(host);
247 static const struct mmc_bus_ops mmc_sdio_ops = {
248 .remove = mmc_sdio_remove,
249 .detect = mmc_sdio_detect,
254 * Starting point for SDIO card init.
256 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
258 int err;
259 int i, funcs;
260 struct mmc_card *card;
262 BUG_ON(!host);
263 WARN_ON(!host->claimed);
265 mmc_attach_bus(host, &mmc_sdio_ops);
268 * Sanity check the voltages that the card claims to
269 * support.
271 if (ocr & 0x7F) {
272 printk(KERN_WARNING "%s: card claims to support voltages "
273 "below the defined range. These will be ignored.\n",
274 mmc_hostname(host));
275 ocr &= ~0x7F;
278 if (ocr & MMC_VDD_165_195) {
279 printk(KERN_WARNING "%s: SDIO card claims to support the "
280 "incompletely defined 'low voltage range'. This "
281 "will be ignored.\n", mmc_hostname(host));
282 ocr &= ~MMC_VDD_165_195;
285 host->ocr = mmc_select_voltage(host, ocr);
288 * Can we support the voltage(s) of the card(s)?
290 if (!host->ocr) {
291 err = -EINVAL;
292 goto err;
296 * Inform the card of the voltage
298 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
299 if (err)
300 goto err;
303 * For SPI, enable CRC as appropriate.
305 if (mmc_host_is_spi(host)) {
306 err = mmc_spi_set_crc(host, use_spi_crc);
307 if (err)
308 goto err;
312 * The number of functions on the card is encoded inside
313 * the ocr.
315 funcs = (ocr & 0x70000000) >> 28;
318 * Allocate card structure.
320 card = mmc_alloc_card(host, NULL);
321 if (IS_ERR(card)) {
322 err = PTR_ERR(card);
323 goto err;
326 card->type = MMC_TYPE_SDIO;
327 card->sdio_funcs = funcs;
329 host->card = card;
332 * For native busses: set card RCA and quit open drain mode.
334 if (!mmc_host_is_spi(host)) {
335 err = mmc_send_relative_addr(host, &card->rca);
336 if (err)
337 goto remove;
339 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
343 * Select card, as all following commands rely on that.
345 if (!mmc_host_is_spi(host)) {
346 err = mmc_select_card(card);
347 if (err)
348 goto remove;
352 * Read the common registers.
354 err = sdio_read_cccr(card);
355 if (err)
356 goto remove;
359 * Read the common CIS tuples.
361 err = sdio_read_common_cis(card);
362 if (err)
363 goto remove;
366 * Switch to high-speed (if supported).
368 err = sdio_enable_hs(card);
369 if (err)
370 goto remove;
373 * Change to the card's maximum speed.
375 if (mmc_card_highspeed(card)) {
377 * The SDIO specification doesn't mention how
378 * the CIS transfer speed register relates to
379 * high-speed, but it seems that 50 MHz is
380 * mandatory.
382 mmc_set_clock(host, 50000000);
383 } else {
384 mmc_set_clock(host, card->cis.max_dtr);
388 * Switch to wider bus (if supported).
390 err = sdio_enable_wide(card);
391 if (err)
392 goto remove;
395 * Initialize (but don't add) all present functions.
397 for (i = 0;i < funcs;i++) {
398 err = sdio_init_func(host->card, i + 1);
399 if (err)
400 goto remove;
403 mmc_release_host(host);
406 * First add the card to the driver model...
408 err = mmc_add_card(host->card);
409 if (err)
410 goto remove_added;
413 * ...then the SDIO functions.
415 for (i = 0;i < funcs;i++) {
416 err = sdio_add_func(host->card->sdio_func[i]);
417 if (err)
418 goto remove_added;
421 return 0;
424 remove_added:
425 /* Remove without lock if the device has been added. */
426 mmc_sdio_remove(host);
427 mmc_claim_host(host);
428 remove:
429 /* And with lock if it hasn't been added. */
430 if (host->card)
431 mmc_sdio_remove(host);
432 err:
433 mmc_detach_bus(host);
434 mmc_release_host(host);
436 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
437 mmc_hostname(host), err);
439 return err;