]>
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 | ||
d16f5770 PO |
167 | /* |
168 | * Test if the card supports high-speed mode and, if so, switch to it. | |
169 | */ | |
170 | static int sdio_enable_hs(struct mmc_card *card) | |
171 | { | |
172 | int ret; | |
173 | u8 speed; | |
174 | ||
175 | if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) | |
176 | return 0; | |
177 | ||
178 | if (!card->cccr.high_speed) | |
179 | return 0; | |
180 | ||
181 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); | |
182 | if (ret) | |
183 | return ret; | |
184 | ||
185 | speed |= SDIO_SPEED_EHS; | |
186 | ||
187 | ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); | |
188 | if (ret) | |
189 | return ret; | |
190 | ||
191 | mmc_card_set_highspeed(card); | |
192 | mmc_set_timing(card->host, MMC_TIMING_SD_HS); | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
5c4e6f13 PO |
197 | /* |
198 | * Host is being removed. Free up the current card. | |
199 | */ | |
200 | static void mmc_sdio_remove(struct mmc_host *host) | |
201 | { | |
e29a7d73 PO |
202 | int i; |
203 | ||
5c4e6f13 PO |
204 | BUG_ON(!host); |
205 | BUG_ON(!host->card); | |
206 | ||
e29a7d73 PO |
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; | |
211 | } | |
212 | } | |
213 | ||
5c4e6f13 PO |
214 | mmc_remove_card(host->card); |
215 | host->card = NULL; | |
216 | } | |
217 | ||
218 | /* | |
219 | * Card detection callback from host. | |
220 | */ | |
221 | static void mmc_sdio_detect(struct mmc_host *host) | |
222 | { | |
223 | int err; | |
224 | ||
225 | BUG_ON(!host); | |
226 | BUG_ON(!host->card); | |
227 | ||
228 | mmc_claim_host(host); | |
229 | ||
230 | /* | |
231 | * Just check if our card has been removed. | |
232 | */ | |
233 | err = mmc_select_card(host->card); | |
234 | ||
235 | mmc_release_host(host); | |
236 | ||
237 | if (err) { | |
238 | mmc_sdio_remove(host); | |
239 | ||
240 | mmc_claim_host(host); | |
241 | mmc_detach_bus(host); | |
242 | mmc_release_host(host); | |
243 | } | |
244 | } | |
245 | ||
246 | ||
247 | static const struct mmc_bus_ops mmc_sdio_ops = { | |
248 | .remove = mmc_sdio_remove, | |
249 | .detect = mmc_sdio_detect, | |
250 | }; | |
251 | ||
252 | ||
253 | /* | |
254 | * Starting point for SDIO card init. | |
255 | */ | |
256 | int mmc_attach_sdio(struct mmc_host *host, u32 ocr) | |
257 | { | |
258 | int err; | |
e29a7d73 | 259 | int i, funcs; |
5c4e6f13 PO |
260 | struct mmc_card *card; |
261 | ||
262 | BUG_ON(!host); | |
d84075c8 | 263 | WARN_ON(!host->claimed); |
5c4e6f13 PO |
264 | |
265 | mmc_attach_bus(host, &mmc_sdio_ops); | |
266 | ||
267 | /* | |
268 | * Sanity check the voltages that the card claims to | |
269 | * support. | |
270 | */ | |
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; | |
276 | } | |
277 | ||
5c4e6f13 PO |
278 | host->ocr = mmc_select_voltage(host, ocr); |
279 | ||
280 | /* | |
281 | * Can we support the voltage(s) of the card(s)? | |
282 | */ | |
283 | if (!host->ocr) { | |
284 | err = -EINVAL; | |
285 | goto err; | |
286 | } | |
287 | ||
288 | /* | |
289 | * Inform the card of the voltage | |
290 | */ | |
291 | err = mmc_send_io_op_cond(host, host->ocr, &ocr); | |
292 | if (err) | |
293 | goto err; | |
294 | ||
af517150 DB |
295 | /* |
296 | * For SPI, enable CRC as appropriate. | |
297 | */ | |
298 | if (mmc_host_is_spi(host)) { | |
299 | err = mmc_spi_set_crc(host, use_spi_crc); | |
300 | if (err) | |
301 | goto err; | |
302 | } | |
303 | ||
5c4e6f13 PO |
304 | /* |
305 | * The number of functions on the card is encoded inside | |
306 | * the ocr. | |
307 | */ | |
308 | funcs = (ocr & 0x70000000) >> 28; | |
309 | ||
310 | /* | |
311 | * Allocate card structure. | |
312 | */ | |
51ec92e2 | 313 | card = mmc_alloc_card(host, NULL); |
5c4e6f13 PO |
314 | if (IS_ERR(card)) { |
315 | err = PTR_ERR(card); | |
316 | goto err; | |
317 | } | |
318 | ||
319 | card->type = MMC_TYPE_SDIO; | |
e29a7d73 PO |
320 | card->sdio_funcs = funcs; |
321 | ||
322 | host->card = card; | |
5c4e6f13 PO |
323 | |
324 | /* | |
af517150 | 325 | * For native busses: set card RCA and quit open drain mode. |
5c4e6f13 | 326 | */ |
af517150 DB |
327 | if (!mmc_host_is_spi(host)) { |
328 | err = mmc_send_relative_addr(host, &card->rca); | |
329 | if (err) | |
330 | goto remove; | |
5c4e6f13 | 331 | |
af517150 DB |
332 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
333 | } | |
5c4e6f13 PO |
334 | |
335 | /* | |
336 | * Select card, as all following commands rely on that. | |
337 | */ | |
af517150 DB |
338 | if (!mmc_host_is_spi(host)) { |
339 | err = mmc_select_card(card); | |
340 | if (err) | |
341 | goto remove; | |
342 | } | |
5c4e6f13 | 343 | |
35c66c19 PO |
344 | /* |
345 | * Read the common registers. | |
346 | */ | |
347 | err = sdio_read_cccr(card); | |
348 | if (err) | |
349 | goto remove; | |
350 | ||
1a632f8c PO |
351 | /* |
352 | * Read the common CIS tuples. | |
353 | */ | |
354 | err = sdio_read_common_cis(card); | |
355 | if (err) | |
356 | goto remove; | |
357 | ||
6db5020e | 358 | /* |
d16f5770 | 359 | * Switch to high-speed (if supported). |
6db5020e | 360 | */ |
d16f5770 PO |
361 | err = sdio_enable_hs(card); |
362 | if (err) | |
363 | goto remove; | |
364 | ||
365 | /* | |
366 | * Change to the card's maximum speed. | |
367 | */ | |
368 | if (mmc_card_highspeed(card)) { | |
369 | /* | |
370 | * The SDIO specification doesn't mention how | |
371 | * the CIS transfer speed register relates to | |
372 | * high-speed, but it seems that 50 MHz is | |
373 | * mandatory. | |
374 | */ | |
375 | mmc_set_clock(host, 50000000); | |
376 | } else { | |
377 | mmc_set_clock(host, card->cis.max_dtr); | |
378 | } | |
6db5020e | 379 | |
4ff6471c PO |
380 | /* |
381 | * Switch to wider bus (if supported). | |
382 | */ | |
383 | err = sdio_enable_wide(card); | |
384 | if (err) | |
385 | goto remove; | |
386 | ||
e29a7d73 PO |
387 | /* |
388 | * Initialize (but don't add) all present functions. | |
389 | */ | |
390 | for (i = 0;i < funcs;i++) { | |
391 | err = sdio_init_func(host->card, i + 1); | |
392 | if (err) | |
393 | goto remove; | |
394 | } | |
5c4e6f13 PO |
395 | |
396 | mmc_release_host(host); | |
397 | ||
e29a7d73 PO |
398 | /* |
399 | * First add the card to the driver model... | |
400 | */ | |
5c4e6f13 PO |
401 | err = mmc_add_card(host->card); |
402 | if (err) | |
e29a7d73 PO |
403 | goto remove_added; |
404 | ||
405 | /* | |
406 | * ...then the SDIO functions. | |
407 | */ | |
408 | for (i = 0;i < funcs;i++) { | |
409 | err = sdio_add_func(host->card->sdio_func[i]); | |
410 | if (err) | |
411 | goto remove_added; | |
412 | } | |
5c4e6f13 PO |
413 | |
414 | return 0; | |
415 | ||
e29a7d73 PO |
416 | |
417 | remove_added: | |
418 | /* Remove without lock if the device has been added. */ | |
419 | mmc_sdio_remove(host); | |
5c4e6f13 | 420 | mmc_claim_host(host); |
e29a7d73 PO |
421 | remove: |
422 | /* And with lock if it hasn't been added. */ | |
423 | if (host->card) | |
424 | mmc_sdio_remove(host); | |
5c4e6f13 PO |
425 | err: |
426 | mmc_detach_bus(host); | |
427 | mmc_release_host(host); | |
428 | ||
429 | printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n", | |
430 | mmc_hostname(host), err); | |
431 | ||
432 | return err; | |
433 | } | |
434 |