blob: 4eab79e09cccacbab8b7ecf7d2ae780f9e5b9e05 [file] [log] [blame]
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001/*
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>
Pierre Ossman35c66c12007-06-11 20:25:43 +020016#include <linux/mmc/sdio.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020017#include <linux/mmc/sdio_func.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020018
19#include "core.h"
20#include "bus.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020021#include "sdio_bus.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020022#include "mmc_ops.h"
23#include "sd_ops.h"
24#include "sdio_ops.h"
Nicolas Pitreb7261262007-06-16 02:04:16 -040025#include "sdio_cis.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020026
Pierre Ossman05970072007-06-11 21:01:00 +020027static 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,
David Vrabel7616ee92007-08-08 14:23:05 +010033 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020034 if (ret)
35 goto out;
36
37 data &= 0x0f;
38
39 if (data == 0x0f) {
40 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010041 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020042 if (ret)
43 goto out;
44 }
45
46 func->class = data;
47
48out:
49 return ret;
50}
51
Pierre Ossmane29a7d72007-05-26 13:48:18 +020052static int sdio_init_func(struct mmc_card *card, unsigned int fn)
53{
Pierre Ossman05970072007-06-11 21:01:00 +020054 int ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020055 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
Pierre Ossman05970072007-06-11 21:01:00 +020065 ret = sdio_read_fbr(func);
66 if (ret)
67 goto fail;
68
Pierre Ossman1a632f82007-07-30 15:15:30 +020069 ret = sdio_read_func_cis(func);
Nicolas Pitreb7261262007-06-16 02:04:16 -040070 if (ret)
71 goto fail;
72
Pierre Ossmane29a7d72007-05-26 13:48:18 +020073 card->sdio_func[fn - 1] = func;
74
75 return 0;
Pierre Ossman05970072007-06-11 21:01:00 +020076
77fail:
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;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020084}
85
Pierre Ossman35c66c12007-06-11 20:25:43 +020086static 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
137out:
138 return ret;
139}
140
Pierre Ossman4ff64712007-07-30 18:23:53 +0200141static int sdio_enable_wide(struct mmc_card *card)
142{
143 int ret;
144 u8 ctrl;
145
146 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
147 return 0;
148
149 if (card->cccr.low_speed && !card->cccr.wide_bus)
150 return 0;
151
152 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
153 if (ret)
154 return ret;
155
156 ctrl |= SDIO_BUS_WIDTH_4BIT;
157
158 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
159 if (ret)
160 return ret;
161
162 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
163
164 return 0;
165}
166
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200167/*
168 * Host is being removed. Free up the current card.
169 */
170static void mmc_sdio_remove(struct mmc_host *host)
171{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200172 int i;
173
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200174 BUG_ON(!host);
175 BUG_ON(!host->card);
176
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200177 for (i = 0;i < host->card->sdio_funcs;i++) {
178 if (host->card->sdio_func[i]) {
179 sdio_remove_func(host->card->sdio_func[i]);
180 host->card->sdio_func[i] = NULL;
181 }
182 }
183
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200184 mmc_remove_card(host->card);
185 host->card = NULL;
186}
187
188/*
189 * Card detection callback from host.
190 */
191static void mmc_sdio_detect(struct mmc_host *host)
192{
193 int err;
194
195 BUG_ON(!host);
196 BUG_ON(!host->card);
197
198 mmc_claim_host(host);
199
200 /*
201 * Just check if our card has been removed.
202 */
203 err = mmc_select_card(host->card);
204
205 mmc_release_host(host);
206
207 if (err) {
208 mmc_sdio_remove(host);
209
210 mmc_claim_host(host);
211 mmc_detach_bus(host);
212 mmc_release_host(host);
213 }
214}
215
216
217static const struct mmc_bus_ops mmc_sdio_ops = {
218 .remove = mmc_sdio_remove,
219 .detect = mmc_sdio_detect,
220};
221
222
223/*
224 * Starting point for SDIO card init.
225 */
226int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
227{
228 int err;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200229 int i, funcs;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200230 struct mmc_card *card;
231
232 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200233 WARN_ON(!host->claimed);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200234
235 mmc_attach_bus(host, &mmc_sdio_ops);
236
237 /*
238 * Sanity check the voltages that the card claims to
239 * support.
240 */
241 if (ocr & 0x7F) {
242 printk(KERN_WARNING "%s: card claims to support voltages "
243 "below the defined range. These will be ignored.\n",
244 mmc_hostname(host));
245 ocr &= ~0x7F;
246 }
247
248 if (ocr & MMC_VDD_165_195) {
249 printk(KERN_WARNING "%s: SDIO card claims to support the "
250 "incompletely defined 'low voltage range'. This "
251 "will be ignored.\n", mmc_hostname(host));
252 ocr &= ~MMC_VDD_165_195;
253 }
254
255 host->ocr = mmc_select_voltage(host, ocr);
256
257 /*
258 * Can we support the voltage(s) of the card(s)?
259 */
260 if (!host->ocr) {
261 err = -EINVAL;
262 goto err;
263 }
264
265 /*
266 * Inform the card of the voltage
267 */
268 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
269 if (err)
270 goto err;
271
272 /*
David Brownellaf517152007-08-08 09:11:32 -0700273 * For SPI, enable CRC as appropriate.
274 */
275 if (mmc_host_is_spi(host)) {
276 err = mmc_spi_set_crc(host, use_spi_crc);
277 if (err)
278 goto err;
279 }
280
281 /*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200282 * The number of functions on the card is encoded inside
283 * the ocr.
284 */
285 funcs = (ocr & 0x70000000) >> 28;
286
287 /*
288 * Allocate card structure.
289 */
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100290 card = mmc_alloc_card(host, NULL);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200291 if (IS_ERR(card)) {
292 err = PTR_ERR(card);
293 goto err;
294 }
295
296 card->type = MMC_TYPE_SDIO;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200297 card->sdio_funcs = funcs;
298
299 host->card = card;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200300
301 /*
David Brownellaf517152007-08-08 09:11:32 -0700302 * For native busses: set card RCA and quit open drain mode.
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200303 */
David Brownellaf517152007-08-08 09:11:32 -0700304 if (!mmc_host_is_spi(host)) {
305 err = mmc_send_relative_addr(host, &card->rca);
306 if (err)
307 goto remove;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200308
David Brownellaf517152007-08-08 09:11:32 -0700309 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
310 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200311
312 /*
313 * Select card, as all following commands rely on that.
314 */
David Brownellaf517152007-08-08 09:11:32 -0700315 if (!mmc_host_is_spi(host)) {
316 err = mmc_select_card(card);
317 if (err)
318 goto remove;
319 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200320
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200321 /*
Pierre Ossman35c66c12007-06-11 20:25:43 +0200322 * Read the common registers.
323 */
324 err = sdio_read_cccr(card);
325 if (err)
326 goto remove;
327
328 /*
Pierre Ossman1a632f82007-07-30 15:15:30 +0200329 * Read the common CIS tuples.
330 */
331 err = sdio_read_common_cis(card);
332 if (err)
333 goto remove;
334
335 /*
Pierre Ossman6db50202007-07-30 17:15:07 +0200336 * No support for high-speed yet, so just set
337 * the card's maximum speed.
338 */
339 mmc_set_clock(host, card->cis.max_dtr);
340
341 /*
Pierre Ossman4ff64712007-07-30 18:23:53 +0200342 * Switch to wider bus (if supported).
343 */
344 err = sdio_enable_wide(card);
345 if (err)
346 goto remove;
347
348 /*
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200349 * Initialize (but don't add) all present functions.
350 */
351 for (i = 0;i < funcs;i++) {
352 err = sdio_init_func(host->card, i + 1);
353 if (err)
354 goto remove;
355 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200356
357 mmc_release_host(host);
358
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200359 /*
360 * First add the card to the driver model...
361 */
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200362 err = mmc_add_card(host->card);
363 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200364 goto remove_added;
365
366 /*
367 * ...then the SDIO functions.
368 */
369 for (i = 0;i < funcs;i++) {
370 err = sdio_add_func(host->card->sdio_func[i]);
371 if (err)
372 goto remove_added;
373 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200374
375 return 0;
376
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200377
378remove_added:
379 /* Remove without lock if the device has been added. */
380 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200381 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200382remove:
383 /* And with lock if it hasn't been added. */
384 if (host->card)
385 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200386err:
387 mmc_detach_bus(host);
388 mmc_release_host(host);
389
390 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
391 mmc_hostname(host), err);
392
393 return err;
394}
395