]>
Commit | Line | Data |
---|---|---|
5c4e6f13 PO |
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> | |
35c66c19 | 16 | #include <linux/mmc/sdio.h> |
e29a7d73 | 17 | #include <linux/mmc/sdio_func.h> |
5c4e6f13 PO |
18 | |
19 | #include "core.h" | |
20 | #include "bus.h" | |
e29a7d73 | 21 | #include "sdio_bus.h" |
5c4e6f13 PO |
22 | #include "mmc_ops.h" |
23 | #include "sd_ops.h" | |
24 | #include "sdio_ops.h" | |
b7261261 | 25 | #include "sdio_cis.h" |
5c4e6f13 | 26 | |
0597007f PO |
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, | |
7616ee95 | 33 | SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data); |
0597007f PO |
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, | |
7616ee95 | 41 | SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data); |
0597007f PO |
42 | if (ret) |
43 | goto out; | |
44 | } | |
45 | ||
46 | func->class = data; | |
47 | ||
48 | out: | |
49 | return ret; | |
50 | } | |
51 | ||
e29a7d73 PO |
52 | static int sdio_init_func(struct mmc_card *card, unsigned int fn) |
53 | { | |
0597007f | 54 | int ret; |
e29a7d73 PO |
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 | ||
0597007f PO |
65 | ret = sdio_read_fbr(func); |
66 | if (ret) | |
67 | goto fail; | |
68 | ||
1a632f8c | 69 | ret = sdio_read_func_cis(func); |
b7261261 NP |
70 | if (ret) |
71 | goto fail; | |
72 | ||
e29a7d73 PO |
73 | card->sdio_func[fn - 1] = func; |
74 | ||
75 | return 0; | |
0597007f PO |
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; | |
e29a7d73 PO |
84 | } |
85 | ||
35c66c19 PO |
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 | ||
4ff6471c PO |
141 | static 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 | ||
006ebd5d OBC |
167 | /* |
168 | * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1) | |
169 | * of the card. This may be required on certain setups of boards, | |
170 | * controllers and embedded sdio device which do not need the card's | |
171 | * pull-up. As a result, card detection is disabled and power is saved. | |
172 | */ | |
173 | static int sdio_disable_cd(struct mmc_card *card) | |
174 | { | |
175 | int ret; | |
176 | u8 ctrl; | |
177 | ||
178 | if (!card->cccr.disable_cd) | |
179 | return 0; | |
180 | ||
181 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); | |
182 | if (ret) | |
183 | return ret; | |
184 | ||
185 | ctrl |= SDIO_BUS_CD_DISABLE; | |
186 | ||
187 | return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); | |
188 | } | |
189 | ||
d16f5770 PO |
190 | /* |
191 | * Test if the card supports high-speed mode and, if so, switch to it. | |
192 | */ | |
193 | static int sdio_enable_hs(struct mmc_card *card) | |
194 | { | |
195 | int ret; | |
196 | u8 speed; | |
197 | ||
198 | if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) | |
199 | return 0; | |
200 | ||
201 | if (!card->cccr.high_speed) | |
202 | return 0; | |
203 | ||
204 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); | |
205 | if (ret) | |
206 | return ret; | |
207 | ||
208 | speed |= SDIO_SPEED_EHS; | |
209 | ||
210 | ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); | |
211 | if (ret) | |
212 | return ret; | |
213 | ||
214 | mmc_card_set_highspeed(card); | |
215 | mmc_set_timing(card->host, MMC_TIMING_SD_HS); | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
17d33e14 NP |
220 | /* |
221 | * Handle the detection and initialisation of a card. | |
222 | * | |
223 | * In the case of a resume, "oldcard" will contain the card | |
224 | * we're trying to reinitialise. | |
225 | */ | |
226 | static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, | |
3bca4cf7 | 227 | struct mmc_card *oldcard, int powered_resume) |
17d33e14 NP |
228 | { |
229 | struct mmc_card *card; | |
230 | int err; | |
231 | ||
232 | BUG_ON(!host); | |
233 | WARN_ON(!host->claimed); | |
234 | ||
235 | /* | |
236 | * Inform the card of the voltage | |
237 | */ | |
3bca4cf7 CB |
238 | if (!powered_resume) { |
239 | err = mmc_send_io_op_cond(host, host->ocr, &ocr); | |
240 | if (err) | |
241 | goto err; | |
242 | } | |
17d33e14 NP |
243 | |
244 | /* | |
245 | * For SPI, enable CRC as appropriate. | |
246 | */ | |
247 | if (mmc_host_is_spi(host)) { | |
248 | err = mmc_spi_set_crc(host, use_spi_crc); | |
249 | if (err) | |
250 | goto err; | |
251 | } | |
252 | ||
253 | /* | |
254 | * Allocate card structure. | |
255 | */ | |
256 | card = mmc_alloc_card(host, NULL); | |
257 | if (IS_ERR(card)) { | |
258 | err = PTR_ERR(card); | |
259 | goto err; | |
260 | } | |
261 | ||
262 | card->type = MMC_TYPE_SDIO; | |
263 | ||
264 | /* | |
265 | * For native busses: set card RCA and quit open drain mode. | |
266 | */ | |
3bca4cf7 | 267 | if (!powered_resume && !mmc_host_is_spi(host)) { |
17d33e14 NP |
268 | err = mmc_send_relative_addr(host, &card->rca); |
269 | if (err) | |
270 | goto remove; | |
271 | ||
272 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); | |
273 | } | |
274 | ||
275 | /* | |
276 | * Select card, as all following commands rely on that. | |
277 | */ | |
3bca4cf7 | 278 | if (!powered_resume && !mmc_host_is_spi(host)) { |
17d33e14 NP |
279 | err = mmc_select_card(card); |
280 | if (err) | |
281 | goto remove; | |
282 | } | |
283 | ||
284 | /* | |
285 | * Read the common registers. | |
286 | */ | |
287 | err = sdio_read_cccr(card); | |
288 | if (err) | |
289 | goto remove; | |
290 | ||
291 | /* | |
292 | * Read the common CIS tuples. | |
293 | */ | |
294 | err = sdio_read_common_cis(card); | |
295 | if (err) | |
296 | goto remove; | |
297 | ||
298 | if (oldcard) { | |
299 | int same = (card->cis.vendor == oldcard->cis.vendor && | |
300 | card->cis.device == oldcard->cis.device); | |
301 | mmc_remove_card(card); | |
302 | if (!same) { | |
303 | err = -ENOENT; | |
304 | goto err; | |
305 | } | |
306 | card = oldcard; | |
95cdfb72 | 307 | return 0; |
17d33e14 NP |
308 | } |
309 | ||
310 | /* | |
311 | * Switch to high-speed (if supported). | |
312 | */ | |
313 | err = sdio_enable_hs(card); | |
314 | if (err) | |
315 | goto remove; | |
316 | ||
317 | /* | |
318 | * Change to the card's maximum speed. | |
319 | */ | |
320 | if (mmc_card_highspeed(card)) { | |
321 | /* | |
322 | * The SDIO specification doesn't mention how | |
323 | * the CIS transfer speed register relates to | |
324 | * high-speed, but it seems that 50 MHz is | |
325 | * mandatory. | |
326 | */ | |
327 | mmc_set_clock(host, 50000000); | |
328 | } else { | |
329 | mmc_set_clock(host, card->cis.max_dtr); | |
330 | } | |
331 | ||
332 | /* | |
333 | * Switch to wider bus (if supported). | |
334 | */ | |
335 | err = sdio_enable_wide(card); | |
336 | if (err) | |
337 | goto remove; | |
338 | ||
339 | if (!oldcard) | |
340 | host->card = card; | |
341 | return 0; | |
342 | ||
343 | remove: | |
344 | if (!oldcard) | |
345 | mmc_remove_card(card); | |
346 | ||
347 | err: | |
348 | return err; | |
349 | } | |
350 | ||
5c4e6f13 PO |
351 | /* |
352 | * Host is being removed. Free up the current card. | |
353 | */ | |
354 | static void mmc_sdio_remove(struct mmc_host *host) | |
355 | { | |
e29a7d73 PO |
356 | int i; |
357 | ||
5c4e6f13 PO |
358 | BUG_ON(!host); |
359 | BUG_ON(!host->card); | |
360 | ||
e29a7d73 PO |
361 | for (i = 0;i < host->card->sdio_funcs;i++) { |
362 | if (host->card->sdio_func[i]) { | |
363 | sdio_remove_func(host->card->sdio_func[i]); | |
364 | host->card->sdio_func[i] = NULL; | |
365 | } | |
366 | } | |
367 | ||
5c4e6f13 PO |
368 | mmc_remove_card(host->card); |
369 | host->card = NULL; | |
370 | } | |
371 | ||
372 | /* | |
373 | * Card detection callback from host. | |
374 | */ | |
375 | static void mmc_sdio_detect(struct mmc_host *host) | |
376 | { | |
377 | int err; | |
378 | ||
379 | BUG_ON(!host); | |
380 | BUG_ON(!host->card); | |
381 | ||
382 | mmc_claim_host(host); | |
383 | ||
384 | /* | |
385 | * Just check if our card has been removed. | |
386 | */ | |
387 | err = mmc_select_card(host->card); | |
388 | ||
389 | mmc_release_host(host); | |
390 | ||
391 | if (err) { | |
392 | mmc_sdio_remove(host); | |
393 | ||
394 | mmc_claim_host(host); | |
395 | mmc_detach_bus(host); | |
396 | mmc_release_host(host); | |
397 | } | |
398 | } | |
399 | ||
17d33e14 NP |
400 | /* |
401 | * SDIO suspend. We need to suspend all functions separately. | |
402 | * Therefore all registered functions must have drivers with suspend | |
403 | * and resume methods. Failing that we simply remove the whole card. | |
404 | */ | |
95cdfb72 | 405 | static int mmc_sdio_suspend(struct mmc_host *host) |
17d33e14 | 406 | { |
95cdfb72 | 407 | int i, err = 0; |
17d33e14 | 408 | |
17d33e14 NP |
409 | for (i = 0; i < host->card->sdio_funcs; i++) { |
410 | struct sdio_func *func = host->card->sdio_func[i]; | |
411 | if (func && sdio_func_present(func) && func->dev.driver) { | |
412 | const struct dev_pm_ops *pmops = func->dev.driver->pm; | |
413 | if (!pmops || !pmops->suspend || !pmops->resume) { | |
95cdfb72 NP |
414 | /* force removal of entire card in that case */ |
415 | err = -ENOSYS; | |
416 | } else | |
417 | err = pmops->suspend(&func->dev); | |
418 | if (err) | |
419 | break; | |
17d33e14 NP |
420 | } |
421 | } | |
95cdfb72 | 422 | while (err && --i >= 0) { |
17d33e14 NP |
423 | struct sdio_func *func = host->card->sdio_func[i]; |
424 | if (func && sdio_func_present(func) && func->dev.driver) { | |
425 | const struct dev_pm_ops *pmops = func->dev.driver->pm; | |
95cdfb72 | 426 | pmops->resume(&func->dev); |
17d33e14 NP |
427 | } |
428 | } | |
95cdfb72 NP |
429 | |
430 | return err; | |
17d33e14 NP |
431 | } |
432 | ||
95cdfb72 | 433 | static int mmc_sdio_resume(struct mmc_host *host) |
17d33e14 NP |
434 | { |
435 | int i, err; | |
436 | ||
437 | BUG_ON(!host); | |
438 | BUG_ON(!host->card); | |
439 | ||
95cdfb72 | 440 | /* Basic card reinitialization. */ |
17d33e14 | 441 | mmc_claim_host(host); |
3bca4cf7 CB |
442 | err = mmc_sdio_init_card(host, host->ocr, host->card, |
443 | (host->pm_flags & MMC_PM_KEEP_POWER)); | |
40216842 NP |
444 | if (!err && host->sdio_irqs) |
445 | mmc_signal_sdio_irq(host); | |
17d33e14 | 446 | mmc_release_host(host); |
17d33e14 | 447 | |
95cdfb72 NP |
448 | /* |
449 | * If the card looked to be the same as before suspending, then | |
450 | * we proceed to resume all card functions. If one of them returns | |
451 | * an error then we simply return that error to the core and the | |
452 | * card will be redetected as new. It is the responsibility of | |
453 | * the function driver to perform further tests with the extra | |
454 | * knowledge it has of the card to confirm the card is indeed the | |
455 | * same as before suspending (same MAC address for network cards, | |
456 | * etc.) and return an error otherwise. | |
457 | */ | |
458 | for (i = 0; !err && i < host->card->sdio_funcs; i++) { | |
17d33e14 NP |
459 | struct sdio_func *func = host->card->sdio_func[i]; |
460 | if (func && sdio_func_present(func) && func->dev.driver) { | |
461 | const struct dev_pm_ops *pmops = func->dev.driver->pm; | |
95cdfb72 | 462 | err = pmops->resume(&func->dev); |
17d33e14 NP |
463 | } |
464 | } | |
95cdfb72 NP |
465 | |
466 | return err; | |
17d33e14 | 467 | } |
5c4e6f13 PO |
468 | |
469 | static const struct mmc_bus_ops mmc_sdio_ops = { | |
470 | .remove = mmc_sdio_remove, | |
471 | .detect = mmc_sdio_detect, | |
17d33e14 NP |
472 | .suspend = mmc_sdio_suspend, |
473 | .resume = mmc_sdio_resume, | |
5c4e6f13 PO |
474 | }; |
475 | ||
476 | ||
477 | /* | |
478 | * Starting point for SDIO card init. | |
479 | */ | |
480 | int mmc_attach_sdio(struct mmc_host *host, u32 ocr) | |
481 | { | |
482 | int err; | |
e29a7d73 | 483 | int i, funcs; |
5c4e6f13 PO |
484 | struct mmc_card *card; |
485 | ||
486 | BUG_ON(!host); | |
d84075c8 | 487 | WARN_ON(!host->claimed); |
5c4e6f13 PO |
488 | |
489 | mmc_attach_bus(host, &mmc_sdio_ops); | |
490 | ||
491 | /* | |
492 | * Sanity check the voltages that the card claims to | |
493 | * support. | |
494 | */ | |
495 | if (ocr & 0x7F) { | |
496 | printk(KERN_WARNING "%s: card claims to support voltages " | |
497 | "below the defined range. These will be ignored.\n", | |
498 | mmc_hostname(host)); | |
499 | ocr &= ~0x7F; | |
500 | } | |
501 | ||
5c4e6f13 PO |
502 | host->ocr = mmc_select_voltage(host, ocr); |
503 | ||
504 | /* | |
505 | * Can we support the voltage(s) of the card(s)? | |
506 | */ | |
507 | if (!host->ocr) { | |
508 | err = -EINVAL; | |
509 | goto err; | |
510 | } | |
511 | ||
512 | /* | |
17d33e14 | 513 | * Detect and init the card. |
5c4e6f13 | 514 | */ |
3bca4cf7 | 515 | err = mmc_sdio_init_card(host, host->ocr, NULL, 0); |
5c4e6f13 PO |
516 | if (err) |
517 | goto err; | |
17d33e14 | 518 | card = host->card; |
af517150 | 519 | |
5c4e6f13 PO |
520 | /* |
521 | * The number of functions on the card is encoded inside | |
522 | * the ocr. | |
523 | */ | |
e8812793 MF |
524 | funcs = (ocr & 0x70000000) >> 28; |
525 | card->sdio_funcs = 0; | |
4ff6471c | 526 | |
006ebd5d OBC |
527 | /* |
528 | * If needed, disconnect card detection pull-up resistor. | |
529 | */ | |
530 | err = sdio_disable_cd(card); | |
531 | if (err) | |
532 | goto remove; | |
533 | ||
e29a7d73 PO |
534 | /* |
535 | * Initialize (but don't add) all present functions. | |
536 | */ | |
e8812793 | 537 | for (i = 0; i < funcs; i++, card->sdio_funcs++) { |
e29a7d73 PO |
538 | err = sdio_init_func(host->card, i + 1); |
539 | if (err) | |
540 | goto remove; | |
541 | } | |
5c4e6f13 PO |
542 | |
543 | mmc_release_host(host); | |
544 | ||
e29a7d73 PO |
545 | /* |
546 | * First add the card to the driver model... | |
547 | */ | |
5c4e6f13 PO |
548 | err = mmc_add_card(host->card); |
549 | if (err) | |
e29a7d73 PO |
550 | goto remove_added; |
551 | ||
552 | /* | |
553 | * ...then the SDIO functions. | |
554 | */ | |
555 | for (i = 0;i < funcs;i++) { | |
556 | err = sdio_add_func(host->card->sdio_func[i]); | |
557 | if (err) | |
558 | goto remove_added; | |
559 | } | |
5c4e6f13 PO |
560 | |
561 | return 0; | |
562 | ||
e29a7d73 PO |
563 | |
564 | remove_added: | |
565 | /* Remove without lock if the device has been added. */ | |
566 | mmc_sdio_remove(host); | |
5c4e6f13 | 567 | mmc_claim_host(host); |
e29a7d73 PO |
568 | remove: |
569 | /* And with lock if it hasn't been added. */ | |
570 | if (host->card) | |
571 | mmc_sdio_remove(host); | |
5c4e6f13 PO |
572 | err: |
573 | mmc_detach_bus(host); | |
574 | mmc_release_host(host); | |
575 | ||
576 | printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n", | |
577 | mmc_hostname(host), err); | |
578 | ||
579 | return err; | |
580 | } | |
581 |