sdio: read and decode interesting parts of the CCCR
[linux-2.6/pdupreez.git] / drivers / mmc / core / sdio.c
blob7ce3e3104d2193f6c5413408979b9346e35d0b8f
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"
26 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
28 struct sdio_func *func;
30 BUG_ON(fn > SDIO_MAX_FUNCS);
32 func = sdio_alloc_func(card);
33 if (IS_ERR(func))
34 return PTR_ERR(func);
36 func->num = fn;
38 card->sdio_func[fn - 1] = func;
40 return 0;
43 static int sdio_read_cccr(struct mmc_card *card)
45 int ret;
46 int cccr_vsn;
47 unsigned char data;
49 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
51 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
52 if (ret)
53 goto out;
55 cccr_vsn = data & 0x0f;
57 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
58 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
59 mmc_hostname(card->host), cccr_vsn);
60 return -EINVAL;
63 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
65 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
66 if (ret)
67 goto out;
69 if (data & SDIO_CCCR_CAP_SMB)
70 card->cccr.multi_block = 1;
71 if (data & SDIO_CCCR_CAP_LSC)
72 card->cccr.low_speed = 1;
73 if (data & SDIO_CCCR_CAP_4BLS)
74 card->cccr.wide_bus = 1;
76 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
77 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
78 if (ret)
79 goto out;
81 if (data & SDIO_POWER_SMPC)
82 card->cccr.high_power = 1;
85 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
86 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
87 if (ret)
88 goto out;
90 if (data & SDIO_SPEED_SHS)
91 card->cccr.high_speed = 1;
94 out:
95 return ret;
99 * Host is being removed. Free up the current card.
101 static void mmc_sdio_remove(struct mmc_host *host)
103 int i;
105 BUG_ON(!host);
106 BUG_ON(!host->card);
108 for (i = 0;i < host->card->sdio_funcs;i++) {
109 if (host->card->sdio_func[i]) {
110 sdio_remove_func(host->card->sdio_func[i]);
111 host->card->sdio_func[i] = NULL;
115 mmc_remove_card(host->card);
116 host->card = NULL;
120 * Card detection callback from host.
122 static void mmc_sdio_detect(struct mmc_host *host)
124 int err;
126 BUG_ON(!host);
127 BUG_ON(!host->card);
129 mmc_claim_host(host);
132 * Just check if our card has been removed.
134 err = mmc_select_card(host->card);
136 mmc_release_host(host);
138 if (err) {
139 mmc_sdio_remove(host);
141 mmc_claim_host(host);
142 mmc_detach_bus(host);
143 mmc_release_host(host);
148 static const struct mmc_bus_ops mmc_sdio_ops = {
149 .remove = mmc_sdio_remove,
150 .detect = mmc_sdio_detect,
155 * Starting point for SDIO card init.
157 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
159 int err;
160 int i, funcs;
161 struct mmc_card *card;
163 BUG_ON(!host);
164 BUG_ON(!host->claimed);
166 mmc_attach_bus(host, &mmc_sdio_ops);
169 * Sanity check the voltages that the card claims to
170 * support.
172 if (ocr & 0x7F) {
173 printk(KERN_WARNING "%s: card claims to support voltages "
174 "below the defined range. These will be ignored.\n",
175 mmc_hostname(host));
176 ocr &= ~0x7F;
179 if (ocr & MMC_VDD_165_195) {
180 printk(KERN_WARNING "%s: SDIO card claims to support the "
181 "incompletely defined 'low voltage range'. This "
182 "will be ignored.\n", mmc_hostname(host));
183 ocr &= ~MMC_VDD_165_195;
186 host->ocr = mmc_select_voltage(host, ocr);
189 * Can we support the voltage(s) of the card(s)?
191 if (!host->ocr) {
192 err = -EINVAL;
193 goto err;
197 * Inform the card of the voltage
199 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
200 if (err)
201 goto err;
204 * The number of functions on the card is encoded inside
205 * the ocr.
207 funcs = (ocr & 0x70000000) >> 28;
210 * Allocate card structure.
212 card = mmc_alloc_card(host);
213 if (IS_ERR(card)) {
214 err = PTR_ERR(card);
215 goto err;
218 card->type = MMC_TYPE_SDIO;
219 card->sdio_funcs = funcs;
221 host->card = card;
224 * Set card RCA.
226 err = mmc_send_relative_addr(host, &card->rca);
227 if (err)
228 goto remove;
230 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
233 * Select card, as all following commands rely on that.
235 err = mmc_select_card(card);
236 if (err)
237 goto remove;
240 * Read the common registers.
242 err = sdio_read_cccr(card);
243 if (err)
244 goto remove;
247 * Initialize (but don't add) all present functions.
249 for (i = 0;i < funcs;i++) {
250 err = sdio_init_func(host->card, i + 1);
251 if (err)
252 goto remove;
255 mmc_release_host(host);
258 * First add the card to the driver model...
260 err = mmc_add_card(host->card);
261 if (err)
262 goto remove_added;
265 * ...then the SDIO functions.
267 for (i = 0;i < funcs;i++) {
268 err = sdio_add_func(host->card->sdio_func[i]);
269 if (err)
270 goto remove_added;
273 return 0;
276 remove_added:
277 /* Remove without lock if the device has been added. */
278 mmc_sdio_remove(host);
279 mmc_claim_host(host);
280 remove:
281 /* And with lock if it hasn't been added. */
282 if (host->card)
283 mmc_sdio_remove(host);
284 err:
285 mmc_detach_bus(host);
286 mmc_release_host(host);
288 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
289 mmc_hostname(host), err);
291 return err;