]>
Commit | Line | Data |
---|---|---|
272cc70b AF |
1 | /* |
2 | * Copyright 2008, Freescale Semiconductor, Inc | |
3 | * Andy Fleming | |
4 | * | |
5 | * Based vaguely on the Linux code | |
6 | * | |
7 | * See file CREDITS for list of people who contributed to this | |
8 | * project. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License as | |
12 | * published by the Free Software Foundation; either version 2 of | |
13 | * the License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
23 | * MA 02111-1307 USA | |
24 | */ | |
25 | ||
26 | #include <config.h> | |
27 | #include <common.h> | |
28 | #include <command.h> | |
29 | #include <mmc.h> | |
30 | #include <part.h> | |
31 | #include <malloc.h> | |
32 | #include <linux/list.h> | |
33 | #include <mmc.h> | |
9b1f942c | 34 | #include <div64.h> |
272cc70b | 35 | |
ce0fbcd2 MW |
36 | /* Set block count limit because of 16 bit register limit on some hardware*/ |
37 | #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT | |
38 | #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535 | |
39 | #endif | |
40 | ||
272cc70b AF |
41 | static struct list_head mmc_devices; |
42 | static int cur_dev_num = -1; | |
43 | ||
11fdade2 SB |
44 | int __board_mmc_getcd(u8 *cd, struct mmc *mmc) { |
45 | return -1; | |
46 | } | |
47 | ||
48 | int board_mmc_getcd(u8 *cd, struct mmc *mmc)__attribute__((weak, | |
49 | alias("__board_mmc_getcd"))); | |
50 | ||
272cc70b AF |
51 | int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
52 | { | |
53 | return mmc->send_cmd(mmc, cmd, data); | |
54 | } | |
55 | ||
56 | int mmc_set_blocklen(struct mmc *mmc, int len) | |
57 | { | |
58 | struct mmc_cmd cmd; | |
59 | ||
60 | cmd.cmdidx = MMC_CMD_SET_BLOCKLEN; | |
61 | cmd.resp_type = MMC_RSP_R1; | |
62 | cmd.cmdarg = len; | |
63 | cmd.flags = 0; | |
64 | ||
65 | return mmc_send_cmd(mmc, &cmd, NULL); | |
66 | } | |
67 | ||
68 | struct mmc *find_mmc_device(int dev_num) | |
69 | { | |
70 | struct mmc *m; | |
71 | struct list_head *entry; | |
72 | ||
73 | list_for_each(entry, &mmc_devices) { | |
74 | m = list_entry(entry, struct mmc, link); | |
75 | ||
76 | if (m->block_dev.dev == dev_num) | |
77 | return m; | |
78 | } | |
79 | ||
80 | printf("MMC Device %d not found\n", dev_num); | |
81 | ||
82 | return NULL; | |
83 | } | |
84 | ||
85 | static ulong | |
0158126e | 86 | mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src) |
272cc70b AF |
87 | { |
88 | struct mmc_cmd cmd; | |
89 | struct mmc_data data; | |
272cc70b | 90 | |
d2bf29e3 | 91 | if ((start + blkcnt) > mmc->block_dev.lba) { |
def412b6 | 92 | printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", |
d2bf29e3 LW |
93 | start + blkcnt, mmc->block_dev.lba); |
94 | return 0; | |
95 | } | |
272cc70b AF |
96 | |
97 | if (blkcnt > 1) | |
98 | cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK; | |
99 | else | |
100 | cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK; | |
101 | ||
102 | if (mmc->high_capacity) | |
103 | cmd.cmdarg = start; | |
104 | else | |
def412b6 | 105 | cmd.cmdarg = start * mmc->write_bl_len; |
272cc70b AF |
106 | |
107 | cmd.resp_type = MMC_RSP_R1; | |
108 | cmd.flags = 0; | |
109 | ||
110 | data.src = src; | |
111 | data.blocks = blkcnt; | |
def412b6 | 112 | data.blocksize = mmc->write_bl_len; |
272cc70b AF |
113 | data.flags = MMC_DATA_WRITE; |
114 | ||
def412b6 SS |
115 | if (mmc_send_cmd(mmc, &cmd, &data)) { |
116 | printf("mmc write failed\n"); | |
117 | return 0; | |
272cc70b AF |
118 | } |
119 | ||
120 | if (blkcnt > 1) { | |
121 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; | |
122 | cmd.cmdarg = 0; | |
123 | cmd.resp_type = MMC_RSP_R1b; | |
124 | cmd.flags = 0; | |
def412b6 SS |
125 | if (mmc_send_cmd(mmc, &cmd, NULL)) { |
126 | printf("mmc fail to send stop cmd\n"); | |
127 | return 0; | |
0158126e | 128 | } |
272cc70b AF |
129 | } |
130 | ||
131 | return blkcnt; | |
132 | } | |
133 | ||
0158126e LW |
134 | static ulong |
135 | mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src) | |
136 | { | |
0158126e LW |
137 | lbaint_t cur, blocks_todo = blkcnt; |
138 | ||
def412b6 | 139 | struct mmc *mmc = find_mmc_device(dev_num); |
0158126e | 140 | if (!mmc) |
def412b6 | 141 | return 0; |
0158126e | 142 | |
def412b6 SS |
143 | if (mmc_set_blocklen(mmc, mmc->write_bl_len)) |
144 | return 0; | |
0158126e LW |
145 | |
146 | do { | |
ce0fbcd2 MW |
147 | cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ? |
148 | CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo; | |
0158126e | 149 | if(mmc_write_blocks(mmc, start, cur, src) != cur) |
def412b6 | 150 | return 0; |
0158126e LW |
151 | blocks_todo -= cur; |
152 | start += cur; | |
153 | src += cur * mmc->write_bl_len; | |
154 | } while (blocks_todo > 0); | |
155 | ||
156 | return blkcnt; | |
157 | } | |
158 | ||
4a1a06bc | 159 | int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt) |
272cc70b AF |
160 | { |
161 | struct mmc_cmd cmd; | |
162 | struct mmc_data data; | |
163 | ||
4a1a06bc AS |
164 | if (blkcnt > 1) |
165 | cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK; | |
166 | else | |
167 | cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK; | |
272cc70b AF |
168 | |
169 | if (mmc->high_capacity) | |
4a1a06bc | 170 | cmd.cmdarg = start; |
272cc70b | 171 | else |
4a1a06bc | 172 | cmd.cmdarg = start * mmc->read_bl_len; |
272cc70b AF |
173 | |
174 | cmd.resp_type = MMC_RSP_R1; | |
175 | cmd.flags = 0; | |
176 | ||
177 | data.dest = dst; | |
4a1a06bc | 178 | data.blocks = blkcnt; |
272cc70b AF |
179 | data.blocksize = mmc->read_bl_len; |
180 | data.flags = MMC_DATA_READ; | |
181 | ||
4a1a06bc AS |
182 | if (mmc_send_cmd(mmc, &cmd, &data)) |
183 | return 0; | |
272cc70b | 184 | |
4a1a06bc AS |
185 | if (blkcnt > 1) { |
186 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; | |
187 | cmd.cmdarg = 0; | |
188 | cmd.resp_type = MMC_RSP_R1b; | |
189 | cmd.flags = 0; | |
190 | if (mmc_send_cmd(mmc, &cmd, NULL)) { | |
191 | printf("mmc fail to send stop cmd\n"); | |
192 | return 0; | |
193 | } | |
272cc70b AF |
194 | } |
195 | ||
4a1a06bc | 196 | return blkcnt; |
272cc70b AF |
197 | } |
198 | ||
199 | static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst) | |
200 | { | |
4a1a06bc AS |
201 | lbaint_t cur, blocks_todo = blkcnt; |
202 | ||
203 | if (blkcnt == 0) | |
204 | return 0; | |
272cc70b | 205 | |
4a1a06bc | 206 | struct mmc *mmc = find_mmc_device(dev_num); |
272cc70b AF |
207 | if (!mmc) |
208 | return 0; | |
209 | ||
d2bf29e3 | 210 | if ((start + blkcnt) > mmc->block_dev.lba) { |
4a1a06bc | 211 | printf("MMC: block number 0x%lx exceeds max(0x%lx)\n", |
d2bf29e3 LW |
212 | start + blkcnt, mmc->block_dev.lba); |
213 | return 0; | |
214 | } | |
272cc70b | 215 | |
4a1a06bc | 216 | if (mmc_set_blocklen(mmc, mmc->read_bl_len)) |
272cc70b | 217 | return 0; |
272cc70b | 218 | |
4a1a06bc | 219 | do { |
ce0fbcd2 MW |
220 | cur = (blocks_todo > CONFIG_SYS_MMC_MAX_BLK_COUNT) ? |
221 | CONFIG_SYS_MMC_MAX_BLK_COUNT : blocks_todo; | |
4a1a06bc AS |
222 | if(mmc_read_blocks(mmc, dst, start, cur) != cur) |
223 | return 0; | |
224 | blocks_todo -= cur; | |
225 | start += cur; | |
226 | dst += cur * mmc->read_bl_len; | |
227 | } while (blocks_todo > 0); | |
272cc70b AF |
228 | |
229 | return blkcnt; | |
230 | } | |
231 | ||
232 | int mmc_go_idle(struct mmc* mmc) | |
233 | { | |
234 | struct mmc_cmd cmd; | |
235 | int err; | |
236 | ||
237 | udelay(1000); | |
238 | ||
239 | cmd.cmdidx = MMC_CMD_GO_IDLE_STATE; | |
240 | cmd.cmdarg = 0; | |
241 | cmd.resp_type = MMC_RSP_NONE; | |
242 | cmd.flags = 0; | |
243 | ||
244 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
245 | ||
246 | if (err) | |
247 | return err; | |
248 | ||
249 | udelay(2000); | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
254 | int | |
255 | sd_send_op_cond(struct mmc *mmc) | |
256 | { | |
257 | int timeout = 1000; | |
258 | int err; | |
259 | struct mmc_cmd cmd; | |
260 | ||
261 | do { | |
262 | cmd.cmdidx = MMC_CMD_APP_CMD; | |
263 | cmd.resp_type = MMC_RSP_R1; | |
264 | cmd.cmdarg = 0; | |
265 | cmd.flags = 0; | |
266 | ||
267 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
268 | ||
269 | if (err) | |
270 | return err; | |
271 | ||
272 | cmd.cmdidx = SD_CMD_APP_SEND_OP_COND; | |
273 | cmd.resp_type = MMC_RSP_R3; | |
250de12b SB |
274 | |
275 | /* | |
276 | * Most cards do not answer if some reserved bits | |
277 | * in the ocr are set. However, Some controller | |
278 | * can set bit 7 (reserved for low voltages), but | |
279 | * how to manage low voltages SD card is not yet | |
280 | * specified. | |
281 | */ | |
282 | cmd.cmdarg = mmc->voltages & 0xff8000; | |
272cc70b AF |
283 | |
284 | if (mmc->version == SD_VERSION_2) | |
285 | cmd.cmdarg |= OCR_HCS; | |
286 | ||
287 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
288 | ||
289 | if (err) | |
290 | return err; | |
291 | ||
292 | udelay(1000); | |
293 | } while ((!(cmd.response[0] & OCR_BUSY)) && timeout--); | |
294 | ||
295 | if (timeout <= 0) | |
296 | return UNUSABLE_ERR; | |
297 | ||
298 | if (mmc->version != SD_VERSION_2) | |
299 | mmc->version = SD_VERSION_1_0; | |
300 | ||
998be3dd | 301 | mmc->ocr = cmd.response[0]; |
272cc70b AF |
302 | |
303 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); | |
304 | mmc->rca = 0; | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
309 | int mmc_send_op_cond(struct mmc *mmc) | |
310 | { | |
311 | int timeout = 1000; | |
312 | struct mmc_cmd cmd; | |
313 | int err; | |
314 | ||
315 | /* Some cards seem to need this */ | |
316 | mmc_go_idle(mmc); | |
317 | ||
318 | do { | |
319 | cmd.cmdidx = MMC_CMD_SEND_OP_COND; | |
320 | cmd.resp_type = MMC_RSP_R3; | |
321 | cmd.cmdarg = OCR_HCS | mmc->voltages; | |
322 | cmd.flags = 0; | |
323 | ||
324 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
325 | ||
326 | if (err) | |
327 | return err; | |
328 | ||
329 | udelay(1000); | |
330 | } while (!(cmd.response[0] & OCR_BUSY) && timeout--); | |
331 | ||
332 | if (timeout <= 0) | |
333 | return UNUSABLE_ERR; | |
334 | ||
335 | mmc->version = MMC_VERSION_UNKNOWN; | |
998be3dd | 336 | mmc->ocr = cmd.response[0]; |
272cc70b AF |
337 | |
338 | mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS); | |
339 | mmc->rca = 0; | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
344 | ||
345 | int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd) | |
346 | { | |
347 | struct mmc_cmd cmd; | |
348 | struct mmc_data data; | |
349 | int err; | |
350 | ||
351 | /* Get the Card Status Register */ | |
352 | cmd.cmdidx = MMC_CMD_SEND_EXT_CSD; | |
353 | cmd.resp_type = MMC_RSP_R1; | |
354 | cmd.cmdarg = 0; | |
355 | cmd.flags = 0; | |
356 | ||
357 | data.dest = ext_csd; | |
358 | data.blocks = 1; | |
359 | data.blocksize = 512; | |
360 | data.flags = MMC_DATA_READ; | |
361 | ||
362 | err = mmc_send_cmd(mmc, &cmd, &data); | |
363 | ||
364 | return err; | |
365 | } | |
366 | ||
367 | ||
368 | int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value) | |
369 | { | |
370 | struct mmc_cmd cmd; | |
371 | ||
372 | cmd.cmdidx = MMC_CMD_SWITCH; | |
373 | cmd.resp_type = MMC_RSP_R1b; | |
374 | cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | | |
375 | (index << 16) | | |
376 | (value << 8); | |
377 | cmd.flags = 0; | |
378 | ||
379 | return mmc_send_cmd(mmc, &cmd, NULL); | |
380 | } | |
381 | ||
382 | int mmc_change_freq(struct mmc *mmc) | |
383 | { | |
384 | char ext_csd[512]; | |
385 | char cardtype; | |
386 | int err; | |
387 | ||
388 | mmc->card_caps = 0; | |
389 | ||
390 | /* Only version 4 supports high-speed */ | |
391 | if (mmc->version < MMC_VERSION_4) | |
392 | return 0; | |
393 | ||
394 | mmc->card_caps |= MMC_MODE_4BIT; | |
395 | ||
396 | err = mmc_send_ext_csd(mmc, ext_csd); | |
397 | ||
398 | if (err) | |
399 | return err; | |
400 | ||
401 | if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215]) | |
402 | mmc->high_capacity = 1; | |
403 | ||
404 | cardtype = ext_csd[196] & 0xf; | |
405 | ||
406 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1); | |
407 | ||
408 | if (err) | |
409 | return err; | |
410 | ||
411 | /* Now check to see that it worked */ | |
412 | err = mmc_send_ext_csd(mmc, ext_csd); | |
413 | ||
414 | if (err) | |
415 | return err; | |
416 | ||
417 | /* No high-speed support */ | |
418 | if (!ext_csd[185]) | |
419 | return 0; | |
420 | ||
421 | /* High Speed is set, there are two types: 52MHz and 26MHz */ | |
422 | if (cardtype & MMC_HS_52MHZ) | |
423 | mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; | |
424 | else | |
425 | mmc->card_caps |= MMC_MODE_HS; | |
426 | ||
427 | return 0; | |
428 | } | |
429 | ||
430 | int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp) | |
431 | { | |
432 | struct mmc_cmd cmd; | |
433 | struct mmc_data data; | |
434 | ||
435 | /* Switch the frequency */ | |
436 | cmd.cmdidx = SD_CMD_SWITCH_FUNC; | |
437 | cmd.resp_type = MMC_RSP_R1; | |
438 | cmd.cmdarg = (mode << 31) | 0xffffff; | |
439 | cmd.cmdarg &= ~(0xf << (group * 4)); | |
440 | cmd.cmdarg |= value << (group * 4); | |
441 | cmd.flags = 0; | |
442 | ||
443 | data.dest = (char *)resp; | |
444 | data.blocksize = 64; | |
445 | data.blocks = 1; | |
446 | data.flags = MMC_DATA_READ; | |
447 | ||
448 | return mmc_send_cmd(mmc, &cmd, &data); | |
449 | } | |
450 | ||
451 | ||
452 | int sd_change_freq(struct mmc *mmc) | |
453 | { | |
454 | int err; | |
455 | struct mmc_cmd cmd; | |
456 | uint scr[2]; | |
457 | uint switch_status[16]; | |
458 | struct mmc_data data; | |
459 | int timeout; | |
460 | ||
461 | mmc->card_caps = 0; | |
462 | ||
463 | /* Read the SCR to find out if this card supports higher speeds */ | |
464 | cmd.cmdidx = MMC_CMD_APP_CMD; | |
465 | cmd.resp_type = MMC_RSP_R1; | |
466 | cmd.cmdarg = mmc->rca << 16; | |
467 | cmd.flags = 0; | |
468 | ||
469 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
470 | ||
471 | if (err) | |
472 | return err; | |
473 | ||
474 | cmd.cmdidx = SD_CMD_APP_SEND_SCR; | |
475 | cmd.resp_type = MMC_RSP_R1; | |
476 | cmd.cmdarg = 0; | |
477 | cmd.flags = 0; | |
478 | ||
479 | timeout = 3; | |
480 | ||
481 | retry_scr: | |
482 | data.dest = (char *)&scr; | |
483 | data.blocksize = 8; | |
484 | data.blocks = 1; | |
485 | data.flags = MMC_DATA_READ; | |
486 | ||
487 | err = mmc_send_cmd(mmc, &cmd, &data); | |
488 | ||
489 | if (err) { | |
490 | if (timeout--) | |
491 | goto retry_scr; | |
492 | ||
493 | return err; | |
494 | } | |
495 | ||
4e3d89ba YK |
496 | mmc->scr[0] = __be32_to_cpu(scr[0]); |
497 | mmc->scr[1] = __be32_to_cpu(scr[1]); | |
272cc70b AF |
498 | |
499 | switch ((mmc->scr[0] >> 24) & 0xf) { | |
500 | case 0: | |
501 | mmc->version = SD_VERSION_1_0; | |
502 | break; | |
503 | case 1: | |
504 | mmc->version = SD_VERSION_1_10; | |
505 | break; | |
506 | case 2: | |
507 | mmc->version = SD_VERSION_2; | |
508 | break; | |
509 | default: | |
510 | mmc->version = SD_VERSION_1_0; | |
511 | break; | |
512 | } | |
513 | ||
b44c7083 AS |
514 | if (mmc->scr[0] & SD_DATA_4BIT) |
515 | mmc->card_caps |= MMC_MODE_4BIT; | |
516 | ||
272cc70b AF |
517 | /* Version 1.0 doesn't support switching */ |
518 | if (mmc->version == SD_VERSION_1_0) | |
519 | return 0; | |
520 | ||
521 | timeout = 4; | |
522 | while (timeout--) { | |
523 | err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1, | |
524 | (u8 *)&switch_status); | |
525 | ||
526 | if (err) | |
527 | return err; | |
528 | ||
529 | /* The high-speed function is busy. Try again */ | |
4e3d89ba | 530 | if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY)) |
272cc70b AF |
531 | break; |
532 | } | |
533 | ||
272cc70b | 534 | /* If high-speed isn't supported, we return */ |
4e3d89ba | 535 | if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)) |
272cc70b AF |
536 | return 0; |
537 | ||
538 | err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status); | |
539 | ||
540 | if (err) | |
541 | return err; | |
542 | ||
4e3d89ba | 543 | if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000) |
272cc70b AF |
544 | mmc->card_caps |= MMC_MODE_HS; |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
549 | /* frequency bases */ | |
550 | /* divided by 10 to be nice to platforms without floating point */ | |
551 | int fbase[] = { | |
552 | 10000, | |
553 | 100000, | |
554 | 1000000, | |
555 | 10000000, | |
556 | }; | |
557 | ||
558 | /* Multiplier values for TRAN_SPEED. Multiplied by 10 to be nice | |
559 | * to platforms without floating point. | |
560 | */ | |
561 | int multipliers[] = { | |
562 | 0, /* reserved */ | |
563 | 10, | |
564 | 12, | |
565 | 13, | |
566 | 15, | |
567 | 20, | |
568 | 25, | |
569 | 30, | |
570 | 35, | |
571 | 40, | |
572 | 45, | |
573 | 50, | |
574 | 55, | |
575 | 60, | |
576 | 70, | |
577 | 80, | |
578 | }; | |
579 | ||
580 | void mmc_set_ios(struct mmc *mmc) | |
581 | { | |
582 | mmc->set_ios(mmc); | |
583 | } | |
584 | ||
585 | void mmc_set_clock(struct mmc *mmc, uint clock) | |
586 | { | |
587 | if (clock > mmc->f_max) | |
588 | clock = mmc->f_max; | |
589 | ||
590 | if (clock < mmc->f_min) | |
591 | clock = mmc->f_min; | |
592 | ||
593 | mmc->clock = clock; | |
594 | ||
595 | mmc_set_ios(mmc); | |
596 | } | |
597 | ||
598 | void mmc_set_bus_width(struct mmc *mmc, uint width) | |
599 | { | |
600 | mmc->bus_width = width; | |
601 | ||
602 | mmc_set_ios(mmc); | |
603 | } | |
604 | ||
605 | int mmc_startup(struct mmc *mmc) | |
606 | { | |
607 | int err; | |
608 | uint mult, freq; | |
609 | u64 cmult, csize; | |
610 | struct mmc_cmd cmd; | |
d23e2c09 | 611 | char ext_csd[512]; |
272cc70b AF |
612 | |
613 | /* Put the Card in Identify Mode */ | |
614 | cmd.cmdidx = MMC_CMD_ALL_SEND_CID; | |
615 | cmd.resp_type = MMC_RSP_R2; | |
616 | cmd.cmdarg = 0; | |
617 | cmd.flags = 0; | |
618 | ||
619 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
620 | ||
621 | if (err) | |
622 | return err; | |
623 | ||
624 | memcpy(mmc->cid, cmd.response, 16); | |
625 | ||
626 | /* | |
627 | * For MMC cards, set the Relative Address. | |
628 | * For SD cards, get the Relatvie Address. | |
629 | * This also puts the cards into Standby State | |
630 | */ | |
631 | cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR; | |
632 | cmd.cmdarg = mmc->rca << 16; | |
633 | cmd.resp_type = MMC_RSP_R6; | |
634 | cmd.flags = 0; | |
635 | ||
636 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
637 | ||
638 | if (err) | |
639 | return err; | |
640 | ||
641 | if (IS_SD(mmc)) | |
998be3dd | 642 | mmc->rca = (cmd.response[0] >> 16) & 0xffff; |
272cc70b AF |
643 | |
644 | /* Get the Card-Specific Data */ | |
645 | cmd.cmdidx = MMC_CMD_SEND_CSD; | |
646 | cmd.resp_type = MMC_RSP_R2; | |
647 | cmd.cmdarg = mmc->rca << 16; | |
648 | cmd.flags = 0; | |
649 | ||
650 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
651 | ||
652 | if (err) | |
653 | return err; | |
654 | ||
998be3dd RV |
655 | mmc->csd[0] = cmd.response[0]; |
656 | mmc->csd[1] = cmd.response[1]; | |
657 | mmc->csd[2] = cmd.response[2]; | |
658 | mmc->csd[3] = cmd.response[3]; | |
272cc70b AF |
659 | |
660 | if (mmc->version == MMC_VERSION_UNKNOWN) { | |
0b453ffe | 661 | int version = (cmd.response[0] >> 26) & 0xf; |
272cc70b AF |
662 | |
663 | switch (version) { | |
664 | case 0: | |
665 | mmc->version = MMC_VERSION_1_2; | |
666 | break; | |
667 | case 1: | |
668 | mmc->version = MMC_VERSION_1_4; | |
669 | break; | |
670 | case 2: | |
671 | mmc->version = MMC_VERSION_2_2; | |
672 | break; | |
673 | case 3: | |
674 | mmc->version = MMC_VERSION_3; | |
675 | break; | |
676 | case 4: | |
677 | mmc->version = MMC_VERSION_4; | |
678 | break; | |
679 | default: | |
680 | mmc->version = MMC_VERSION_1_2; | |
681 | break; | |
682 | } | |
683 | } | |
684 | ||
685 | /* divide frequency by 10, since the mults are 10x bigger */ | |
0b453ffe RV |
686 | freq = fbase[(cmd.response[0] & 0x7)]; |
687 | mult = multipliers[((cmd.response[0] >> 3) & 0xf)]; | |
272cc70b AF |
688 | |
689 | mmc->tran_speed = freq * mult; | |
690 | ||
998be3dd | 691 | mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf); |
272cc70b AF |
692 | |
693 | if (IS_SD(mmc)) | |
694 | mmc->write_bl_len = mmc->read_bl_len; | |
695 | else | |
998be3dd | 696 | mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf); |
272cc70b AF |
697 | |
698 | if (mmc->high_capacity) { | |
699 | csize = (mmc->csd[1] & 0x3f) << 16 | |
700 | | (mmc->csd[2] & 0xffff0000) >> 16; | |
701 | cmult = 8; | |
702 | } else { | |
703 | csize = (mmc->csd[1] & 0x3ff) << 2 | |
704 | | (mmc->csd[2] & 0xc0000000) >> 30; | |
705 | cmult = (mmc->csd[2] & 0x00038000) >> 15; | |
706 | } | |
707 | ||
708 | mmc->capacity = (csize + 1) << (cmult + 2); | |
709 | mmc->capacity *= mmc->read_bl_len; | |
710 | ||
711 | if (mmc->read_bl_len > 512) | |
712 | mmc->read_bl_len = 512; | |
713 | ||
714 | if (mmc->write_bl_len > 512) | |
715 | mmc->write_bl_len = 512; | |
716 | ||
717 | /* Select the card, and put it into Transfer Mode */ | |
718 | cmd.cmdidx = MMC_CMD_SELECT_CARD; | |
719 | cmd.resp_type = MMC_RSP_R1b; | |
720 | cmd.cmdarg = mmc->rca << 16; | |
721 | cmd.flags = 0; | |
722 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
723 | ||
724 | if (err) | |
725 | return err; | |
726 | ||
d23e2c09 SG |
727 | if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) { |
728 | /* check ext_csd version and capacity */ | |
729 | err = mmc_send_ext_csd(mmc, ext_csd); | |
730 | if (!err & (ext_csd[192] >= 2)) { | |
731 | mmc->capacity = ext_csd[212] << 0 | ext_csd[213] << 8 | | |
732 | ext_csd[214] << 16 | ext_csd[215] << 24; | |
733 | mmc->capacity *= 512; | |
734 | } | |
735 | } | |
736 | ||
272cc70b AF |
737 | if (IS_SD(mmc)) |
738 | err = sd_change_freq(mmc); | |
739 | else | |
740 | err = mmc_change_freq(mmc); | |
741 | ||
742 | if (err) | |
743 | return err; | |
744 | ||
745 | /* Restrict card's capabilities by what the host can do */ | |
746 | mmc->card_caps &= mmc->host_caps; | |
747 | ||
748 | if (IS_SD(mmc)) { | |
749 | if (mmc->card_caps & MMC_MODE_4BIT) { | |
750 | cmd.cmdidx = MMC_CMD_APP_CMD; | |
751 | cmd.resp_type = MMC_RSP_R1; | |
752 | cmd.cmdarg = mmc->rca << 16; | |
753 | cmd.flags = 0; | |
754 | ||
755 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
756 | if (err) | |
757 | return err; | |
758 | ||
759 | cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH; | |
760 | cmd.resp_type = MMC_RSP_R1; | |
761 | cmd.cmdarg = 2; | |
762 | cmd.flags = 0; | |
763 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
764 | if (err) | |
765 | return err; | |
766 | ||
767 | mmc_set_bus_width(mmc, 4); | |
768 | } | |
769 | ||
770 | if (mmc->card_caps & MMC_MODE_HS) | |
771 | mmc_set_clock(mmc, 50000000); | |
772 | else | |
773 | mmc_set_clock(mmc, 25000000); | |
774 | } else { | |
775 | if (mmc->card_caps & MMC_MODE_4BIT) { | |
776 | /* Set the card to use 4 bit*/ | |
777 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, | |
778 | EXT_CSD_BUS_WIDTH, | |
779 | EXT_CSD_BUS_WIDTH_4); | |
780 | ||
781 | if (err) | |
782 | return err; | |
783 | ||
784 | mmc_set_bus_width(mmc, 4); | |
785 | } else if (mmc->card_caps & MMC_MODE_8BIT) { | |
786 | /* Set the card to use 8 bit*/ | |
787 | err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, | |
788 | EXT_CSD_BUS_WIDTH, | |
789 | EXT_CSD_BUS_WIDTH_8); | |
790 | ||
791 | if (err) | |
792 | return err; | |
793 | ||
794 | mmc_set_bus_width(mmc, 8); | |
795 | } | |
796 | ||
797 | if (mmc->card_caps & MMC_MODE_HS) { | |
798 | if (mmc->card_caps & MMC_MODE_HS_52MHz) | |
799 | mmc_set_clock(mmc, 52000000); | |
800 | else | |
801 | mmc_set_clock(mmc, 26000000); | |
802 | } else | |
803 | mmc_set_clock(mmc, 20000000); | |
804 | } | |
805 | ||
806 | /* fill in device description */ | |
807 | mmc->block_dev.lun = 0; | |
808 | mmc->block_dev.type = 0; | |
809 | mmc->block_dev.blksz = mmc->read_bl_len; | |
9b1f942c | 810 | mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len); |
0b453ffe RV |
811 | sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8, |
812 | (mmc->cid[2] << 8) | (mmc->cid[3] >> 24)); | |
813 | sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff, | |
814 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, | |
815 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); | |
816 | sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28, | |
817 | (mmc->cid[2] >> 24) & 0xf); | |
272cc70b AF |
818 | init_part(&mmc->block_dev); |
819 | ||
820 | return 0; | |
821 | } | |
822 | ||
823 | int mmc_send_if_cond(struct mmc *mmc) | |
824 | { | |
825 | struct mmc_cmd cmd; | |
826 | int err; | |
827 | ||
828 | cmd.cmdidx = SD_CMD_SEND_IF_COND; | |
829 | /* We set the bit if the host supports voltages between 2.7 and 3.6 V */ | |
830 | cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa; | |
831 | cmd.resp_type = MMC_RSP_R7; | |
832 | cmd.flags = 0; | |
833 | ||
834 | err = mmc_send_cmd(mmc, &cmd, NULL); | |
835 | ||
836 | if (err) | |
837 | return err; | |
838 | ||
998be3dd | 839 | if ((cmd.response[0] & 0xff) != 0xaa) |
272cc70b AF |
840 | return UNUSABLE_ERR; |
841 | else | |
842 | mmc->version = SD_VERSION_2; | |
843 | ||
844 | return 0; | |
845 | } | |
846 | ||
847 | int mmc_register(struct mmc *mmc) | |
848 | { | |
849 | /* Setup the universal parts of the block interface just once */ | |
850 | mmc->block_dev.if_type = IF_TYPE_MMC; | |
851 | mmc->block_dev.dev = cur_dev_num++; | |
852 | mmc->block_dev.removable = 1; | |
853 | mmc->block_dev.block_read = mmc_bread; | |
854 | mmc->block_dev.block_write = mmc_bwrite; | |
855 | ||
856 | INIT_LIST_HEAD (&mmc->link); | |
857 | ||
858 | list_add_tail (&mmc->link, &mmc_devices); | |
859 | ||
860 | return 0; | |
861 | } | |
862 | ||
863 | block_dev_desc_t *mmc_get_dev(int dev) | |
864 | { | |
865 | struct mmc *mmc = find_mmc_device(dev); | |
866 | ||
e85649c7 | 867 | return mmc ? &mmc->block_dev : NULL; |
272cc70b AF |
868 | } |
869 | ||
870 | int mmc_init(struct mmc *mmc) | |
871 | { | |
872 | int err; | |
873 | ||
874 | err = mmc->init(mmc); | |
875 | ||
876 | if (err) | |
877 | return err; | |
878 | ||
b86b85e2 IY |
879 | mmc_set_bus_width(mmc, 1); |
880 | mmc_set_clock(mmc, 1); | |
881 | ||
272cc70b AF |
882 | /* Reset the Card */ |
883 | err = mmc_go_idle(mmc); | |
884 | ||
885 | if (err) | |
886 | return err; | |
887 | ||
888 | /* Test for SD version 2 */ | |
889 | err = mmc_send_if_cond(mmc); | |
890 | ||
272cc70b AF |
891 | /* Now try to get the SD card's operating condition */ |
892 | err = sd_send_op_cond(mmc); | |
893 | ||
894 | /* If the command timed out, we check for an MMC card */ | |
895 | if (err == TIMEOUT) { | |
896 | err = mmc_send_op_cond(mmc); | |
897 | ||
898 | if (err) { | |
899 | printf("Card did not respond to voltage select!\n"); | |
900 | return UNUSABLE_ERR; | |
901 | } | |
902 | } | |
903 | ||
904 | return mmc_startup(mmc); | |
905 | } | |
906 | ||
907 | /* | |
908 | * CPU and board-specific MMC initializations. Aliased function | |
909 | * signals caller to move on | |
910 | */ | |
911 | static int __def_mmc_init(bd_t *bis) | |
912 | { | |
913 | return -1; | |
914 | } | |
915 | ||
f9a109b3 PT |
916 | int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); |
917 | int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init"))); | |
272cc70b AF |
918 | |
919 | void print_mmc_devices(char separator) | |
920 | { | |
921 | struct mmc *m; | |
922 | struct list_head *entry; | |
923 | ||
924 | list_for_each(entry, &mmc_devices) { | |
925 | m = list_entry(entry, struct mmc, link); | |
926 | ||
927 | printf("%s: %d", m->name, m->block_dev.dev); | |
928 | ||
929 | if (entry->next != &mmc_devices) | |
930 | printf("%c ", separator); | |
931 | } | |
932 | ||
933 | printf("\n"); | |
934 | } | |
935 | ||
936 | int mmc_initialize(bd_t *bis) | |
937 | { | |
938 | INIT_LIST_HEAD (&mmc_devices); | |
939 | cur_dev_num = 0; | |
940 | ||
941 | if (board_mmc_init(bis) < 0) | |
942 | cpu_mmc_init(bis); | |
943 | ||
944 | print_mmc_devices(','); | |
945 | ||
946 | return 0; | |
947 | } |