blob: d735c8d4850c1d85ada432474a44246e265ac1d6 [file] [log] [blame]
wdenk71f95112003-06-15 22:40:42 +00001/*
2 * (C) Copyright 2003
3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <config.h>
25#include <common.h>
26#include <mmc.h>
27#include <asm/errno.h>
28#include <asm/arch/hardware.h>
wdenk7205e402003-09-10 22:30:53 +000029#include <part.h>
wdenk71f95112003-06-15 22:40:42 +000030
31#ifdef CONFIG_MMC
32
stefano babicc95219f2007-11-20 10:40:24 +010033extern int fat_register_device(block_dev_desc_t * dev_desc, int part_no);
wdenk7205e402003-09-10 22:30:53 +000034
35static block_dev_desc_t mmc_dev;
36
stefano babicc95219f2007-11-20 10:40:24 +010037block_dev_desc_t *mmc_get_dev(int dev)
wdenk7205e402003-09-10 22:30:53 +000038{
stefano babicc95219f2007-11-20 10:40:24 +010039 return ((block_dev_desc_t *) & mmc_dev);
wdenk7205e402003-09-10 22:30:53 +000040}
wdenk71f95112003-06-15 22:40:42 +000041
wdenk8bde7f72003-06-27 21:31:46 +000042/*
wdenk71f95112003-06-15 22:40:42 +000043 * FIXME needs to read cid and csd info to determine block size
44 * and other parameters
45 */
46static uchar mmc_buf[MMC_BLOCK_SIZE];
stefano babicc95219f2007-11-20 10:40:24 +010047static uchar spec_ver;
wdenk71f95112003-06-15 22:40:42 +000048static int mmc_ready = 0;
stefano babicc95219f2007-11-20 10:40:24 +010049static int wide = 0;
wdenk71f95112003-06-15 22:40:42 +000050
stefano babicc95219f2007-11-20 10:40:24 +010051static uint32_t *
wdenk71f95112003-06-15 22:40:42 +000052/****************************************************/
53mmc_cmd(ushort cmd, ushort argh, ushort argl, ushort cmdat)
54/****************************************************/
55{
stefano babicc95219f2007-11-20 10:40:24 +010056 static uint32_t resp[4], a, b, c;
wdenk71f95112003-06-15 22:40:42 +000057 ulong status;
stefano babicc95219f2007-11-20 10:40:24 +010058 int i;
wdenk71f95112003-06-15 22:40:42 +000059
stefano babicc95219f2007-11-20 10:40:24 +010060 debug("mmc_cmd %u 0x%04x 0x%04x 0x%04x\n", cmd, argh, argl,
61 cmdat | wide);
wdenk71f95112003-06-15 22:40:42 +000062 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
63 MMC_I_MASK = ~MMC_I_MASK_CLK_IS_OFF;
stefano babicc95219f2007-11-20 10:40:24 +010064 while (!(MMC_I_REG & MMC_I_REG_CLK_IS_OFF)) ;
65 MMC_CMD = cmd;
66 MMC_ARGH = argh;
67 MMC_ARGL = argl;
68 MMC_CMDAT = cmdat | wide;
wdenk71f95112003-06-15 22:40:42 +000069 MMC_I_MASK = ~MMC_I_MASK_END_CMD_RES;
70 MMC_STRPCL = MMC_STRPCL_START_CLK;
stefano babicc95219f2007-11-20 10:40:24 +010071 while (!(MMC_I_REG & MMC_I_REG_END_CMD_RES)) ;
wdenk71f95112003-06-15 22:40:42 +000072
73 status = MMC_STAT;
stefano babicc95219f2007-11-20 10:40:24 +010074 debug("MMC status 0x%08x\n", status);
wdenk20787e22005-04-06 00:04:16 +000075 if (status & MMC_STAT_TIME_OUT_RESPONSE) {
wdenk71f95112003-06-15 22:40:42 +000076 return 0;
77 }
78
stefano babicc95219f2007-11-20 10:40:24 +010079 /* Linux says:
80 * Did I mention this is Sick. We always need to
81 * discard the upper 8 bits of the first 16-bit word.
82 */
83 a = (MMC_RES & 0xffff);
84 for (i = 0; i < 4; i++) {
85 b = (MMC_RES & 0xffff);
86 c = (MMC_RES & 0xffff);
87 resp[i] = (a << 24) | (b << 8) | (c >> 8);
88 a = c;
89 debug("MMC resp[%d] = %#08x\n", i, resp[i]);
wdenk71f95112003-06-15 22:40:42 +000090 }
wdenk71f95112003-06-15 22:40:42 +000091
wdenk71f95112003-06-15 22:40:42 +000092 return resp;
93}
94
95int
96/****************************************************/
stefano babicc95219f2007-11-20 10:40:24 +010097mmc_block_read(uchar * dst, ulong src, ulong len)
wdenk71f95112003-06-15 22:40:42 +000098/****************************************************/
99{
wdenk71f95112003-06-15 22:40:42 +0000100 ushort argh, argl;
101 ulong status;
102
wdenk20787e22005-04-06 00:04:16 +0000103 if (len == 0) {
wdenk71f95112003-06-15 22:40:42 +0000104 return 0;
105 }
106
stefano babicc95219f2007-11-20 10:40:24 +0100107 debug("mmc_block_rd dst %lx src %lx len %d\n", (ulong) dst, src, len);
wdenk71f95112003-06-15 22:40:42 +0000108
109 argh = len >> 16;
110 argl = len & 0xffff;
111
112 /* set block len */
stefano babicc95219f2007-11-20 10:40:24 +0100113 mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
wdenk71f95112003-06-15 22:40:42 +0000114
115 /* send read command */
116 argh = src >> 16;
117 argl = src & 0xffff;
118 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
119 MMC_RDTO = 0xffff;
120 MMC_NOB = 1;
121 MMC_BLKLEN = len;
Haavard Skinnemoen341188b2008-05-22 11:09:59 +0200122 mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK, argh, argl,
stefano babicc95219f2007-11-20 10:40:24 +0100123 MMC_CMDAT_R1 | MMC_CMDAT_READ | MMC_CMDAT_BLOCK |
124 MMC_CMDAT_DATA_EN);
wdenk8bde7f72003-06-27 21:31:46 +0000125
wdenk71f95112003-06-15 22:40:42 +0000126 MMC_I_MASK = ~MMC_I_MASK_RXFIFO_RD_REQ;
wdenk20787e22005-04-06 00:04:16 +0000127 while (len) {
128 if (MMC_I_REG & MMC_I_REG_RXFIFO_RD_REQ) {
129#ifdef CONFIG_PXA27X
130 int i;
stefano babicc95219f2007-11-20 10:40:24 +0100131 for (i = min(len, 32); i; i--) {
132 *dst++ = *((volatile uchar *)&MMC_RXFIFO);
wdenk20787e22005-04-06 00:04:16 +0000133 len--;
134 }
135#else
wdenk71f95112003-06-15 22:40:42 +0000136 *dst++ = MMC_RXFIFO;
137 len--;
wdenk20787e22005-04-06 00:04:16 +0000138#endif
wdenk71f95112003-06-15 22:40:42 +0000139 }
140 status = MMC_STAT;
wdenk20787e22005-04-06 00:04:16 +0000141 if (status & MMC_STAT_ERRORS) {
wdenk71f95112003-06-15 22:40:42 +0000142 printf("MMC_STAT error %lx\n", status);
143 return -1;
144 }
145 }
146 MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
stefano babicc95219f2007-11-20 10:40:24 +0100147 while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
wdenk71f95112003-06-15 22:40:42 +0000148 status = MMC_STAT;
wdenk20787e22005-04-06 00:04:16 +0000149 if (status & MMC_STAT_ERRORS) {
wdenk71f95112003-06-15 22:40:42 +0000150 printf("MMC_STAT error %lx\n", status);
151 return -1;
152 }
153 return 0;
154}
155
156int
157/****************************************************/
stefano babicc95219f2007-11-20 10:40:24 +0100158mmc_block_write(ulong dst, uchar * src, int len)
wdenk71f95112003-06-15 22:40:42 +0000159/****************************************************/
160{
wdenk71f95112003-06-15 22:40:42 +0000161 ushort argh, argl;
162 ulong status;
163
wdenk20787e22005-04-06 00:04:16 +0000164 if (len == 0) {
wdenk71f95112003-06-15 22:40:42 +0000165 return 0;
166 }
167
stefano babicc95219f2007-11-20 10:40:24 +0100168 debug("mmc_block_wr dst %lx src %lx len %d\n", dst, (ulong) src, len);
wdenk71f95112003-06-15 22:40:42 +0000169
170 argh = len >> 16;
171 argl = len & 0xffff;
172
173 /* set block len */
stefano babicc95219f2007-11-20 10:40:24 +0100174 mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
wdenk71f95112003-06-15 22:40:42 +0000175
176 /* send write command */
177 argh = dst >> 16;
178 argl = dst & 0xffff;
179 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
180 MMC_NOB = 1;
181 MMC_BLKLEN = len;
stefano babicc95219f2007-11-20 10:40:24 +0100182 mmc_cmd(MMC_CMD_WRITE_BLOCK, argh, argl,
183 MMC_CMDAT_R1 | MMC_CMDAT_WRITE | MMC_CMDAT_BLOCK |
184 MMC_CMDAT_DATA_EN);
wdenk8bde7f72003-06-27 21:31:46 +0000185
wdenk71f95112003-06-15 22:40:42 +0000186 MMC_I_MASK = ~MMC_I_MASK_TXFIFO_WR_REQ;
wdenk20787e22005-04-06 00:04:16 +0000187 while (len) {
188 if (MMC_I_REG & MMC_I_REG_TXFIFO_WR_REQ) {
stefano babicc95219f2007-11-20 10:40:24 +0100189 int i, bytes = min(32, len);
wdenk71f95112003-06-15 22:40:42 +0000190
stefano babicc95219f2007-11-20 10:40:24 +0100191 for (i = 0; i < bytes; i++) {
wdenk71f95112003-06-15 22:40:42 +0000192 MMC_TXFIFO = *src++;
193 }
wdenk20787e22005-04-06 00:04:16 +0000194 if (bytes < 32) {
wdenk71f95112003-06-15 22:40:42 +0000195 MMC_PRTBUF = MMC_PRTBUF_BUF_PART_FULL;
196 }
197 len -= bytes;
198 }
199 status = MMC_STAT;
wdenk20787e22005-04-06 00:04:16 +0000200 if (status & MMC_STAT_ERRORS) {
wdenk71f95112003-06-15 22:40:42 +0000201 printf("MMC_STAT error %lx\n", status);
202 return -1;
203 }
204 }
205 MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
stefano babicc95219f2007-11-20 10:40:24 +0100206 while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE)) ;
wdenk71f95112003-06-15 22:40:42 +0000207 MMC_I_MASK = ~MMC_I_MASK_PRG_DONE;
stefano babicc95219f2007-11-20 10:40:24 +0100208 while (!(MMC_I_REG & MMC_I_REG_PRG_DONE)) ;
wdenk71f95112003-06-15 22:40:42 +0000209 status = MMC_STAT;
wdenk20787e22005-04-06 00:04:16 +0000210 if (status & MMC_STAT_ERRORS) {
wdenk71f95112003-06-15 22:40:42 +0000211 printf("MMC_STAT error %lx\n", status);
212 return -1;
213 }
214 return 0;
215}
216
wdenk71f95112003-06-15 22:40:42 +0000217int
218/****************************************************/
stefano babicc95219f2007-11-20 10:40:24 +0100219mmc_read(ulong src, uchar * dst, int size)
wdenk71f95112003-06-15 22:40:42 +0000220/****************************************************/
221{
222 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
223 ulong mmc_block_size, mmc_block_address;
224
wdenk20787e22005-04-06 00:04:16 +0000225 if (size == 0) {
wdenk71f95112003-06-15 22:40:42 +0000226 return 0;
227 }
228
wdenk20787e22005-04-06 00:04:16 +0000229 if (!mmc_ready) {
wdenk71f95112003-06-15 22:40:42 +0000230 printf("Please initial the MMC first\n");
231 return -1;
232 }
233
234 mmc_block_size = MMC_BLOCK_SIZE;
235 mmc_block_address = ~(mmc_block_size - 1);
236
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200237 src -= CONFIG_SYS_MMC_BASE;
wdenk71f95112003-06-15 22:40:42 +0000238 end = src + size;
239 part_start = ~mmc_block_address & src;
240 part_end = ~mmc_block_address & end;
241 aligned_start = mmc_block_address & src;
242 aligned_end = mmc_block_address & end;
243
244 /* all block aligned accesses */
stefano babicc95219f2007-11-20 10:40:24 +0100245 debug
246 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
247 src, (ulong) dst, end, part_start, part_end, aligned_start,
248 aligned_end);
wdenk20787e22005-04-06 00:04:16 +0000249 if (part_start) {
wdenk71f95112003-06-15 22:40:42 +0000250 part_len = mmc_block_size - part_start;
stefano babicc95219f2007-11-20 10:40:24 +0100251 debug
252 ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
253 src, (ulong) dst, end, part_start, part_end, aligned_start,
254 aligned_end);
255 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
256 0) {
wdenk71f95112003-06-15 22:40:42 +0000257 return -1;
258 }
stefano babicc95219f2007-11-20 10:40:24 +0100259 memcpy(dst, mmc_buf + part_start, part_len);
wdenk71f95112003-06-15 22:40:42 +0000260 dst += part_len;
261 src += part_len;
262 }
stefano babicc95219f2007-11-20 10:40:24 +0100263 debug
264 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
265 src, (ulong) dst, end, part_start, part_end, aligned_start,
266 aligned_end);
wdenk20787e22005-04-06 00:04:16 +0000267 for (; src < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
stefano babicc95219f2007-11-20 10:40:24 +0100268 debug
269 ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
270 src, (ulong) dst, end, part_start, part_end, aligned_start,
271 aligned_end);
272 if ((mmc_block_read((uchar *) (dst), src, mmc_block_size)) < 0) {
wdenk71f95112003-06-15 22:40:42 +0000273 return -1;
274 }
275 }
stefano babicc95219f2007-11-20 10:40:24 +0100276 debug
277 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
278 src, (ulong) dst, end, part_start, part_end, aligned_start,
279 aligned_end);
wdenk20787e22005-04-06 00:04:16 +0000280 if (part_end && src < end) {
stefano babicc95219f2007-11-20 10:40:24 +0100281 debug
282 ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
283 src, (ulong) dst, end, part_start, part_end, aligned_start,
284 aligned_end);
wdenk20787e22005-04-06 00:04:16 +0000285 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
wdenk71f95112003-06-15 22:40:42 +0000286 return -1;
287 }
288 memcpy(dst, mmc_buf, part_end);
289 }
290 return 0;
291}
292
293int
294/****************************************************/
stefano babicc95219f2007-11-20 10:40:24 +0100295mmc_write(uchar * src, ulong dst, int size)
wdenk71f95112003-06-15 22:40:42 +0000296/****************************************************/
297{
298 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
299 ulong mmc_block_size, mmc_block_address;
300
wdenk20787e22005-04-06 00:04:16 +0000301 if (size == 0) {
wdenk71f95112003-06-15 22:40:42 +0000302 return 0;
303 }
304
wdenk20787e22005-04-06 00:04:16 +0000305 if (!mmc_ready) {
wdenk71f95112003-06-15 22:40:42 +0000306 printf("Please initial the MMC first\n");
307 return -1;
308 }
309
310 mmc_block_size = MMC_BLOCK_SIZE;
311 mmc_block_address = ~(mmc_block_size - 1);
312
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200313 dst -= CONFIG_SYS_MMC_BASE;
wdenk71f95112003-06-15 22:40:42 +0000314 end = dst + size;
315 part_start = ~mmc_block_address & dst;
316 part_end = ~mmc_block_address & end;
317 aligned_start = mmc_block_address & dst;
318 aligned_end = mmc_block_address & end;
319
320 /* all block aligned accesses */
stefano babicc95219f2007-11-20 10:40:24 +0100321 debug
322 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
323 src, (ulong) dst, end, part_start, part_end, aligned_start,
324 aligned_end);
wdenk20787e22005-04-06 00:04:16 +0000325 if (part_start) {
wdenk71f95112003-06-15 22:40:42 +0000326 part_len = mmc_block_size - part_start;
stefano babicc95219f2007-11-20 10:40:24 +0100327 debug
328 ("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
329 (ulong) src, dst, end, part_start, part_end, aligned_start,
330 aligned_end);
331 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) <
332 0) {
wdenk71f95112003-06-15 22:40:42 +0000333 return -1;
334 }
stefano babicc95219f2007-11-20 10:40:24 +0100335 memcpy(mmc_buf + part_start, src, part_len);
336 if ((mmc_block_write(aligned_start, mmc_buf, mmc_block_size)) <
337 0) {
wdenk71f95112003-06-15 22:40:42 +0000338 return -1;
339 }
340 dst += part_len;
341 src += part_len;
342 }
stefano babicc95219f2007-11-20 10:40:24 +0100343 debug
344 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
345 src, (ulong) dst, end, part_start, part_end, aligned_start,
346 aligned_end);
wdenk20787e22005-04-06 00:04:16 +0000347 for (; dst < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
stefano babicc95219f2007-11-20 10:40:24 +0100348 debug
349 ("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
350 src, (ulong) dst, end, part_start, part_end, aligned_start,
351 aligned_end);
352 if ((mmc_block_write(dst, (uchar *) src, mmc_block_size)) < 0) {
wdenk71f95112003-06-15 22:40:42 +0000353 return -1;
354 }
355 }
stefano babicc95219f2007-11-20 10:40:24 +0100356 debug
357 ("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
358 src, (ulong) dst, end, part_start, part_end, aligned_start,
359 aligned_end);
wdenk20787e22005-04-06 00:04:16 +0000360 if (part_end && dst < end) {
stefano babicc95219f2007-11-20 10:40:24 +0100361 debug
362 ("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
363 src, (ulong) dst, end, part_start, part_end, aligned_start,
364 aligned_end);
wdenk20787e22005-04-06 00:04:16 +0000365 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
wdenk71f95112003-06-15 22:40:42 +0000366 return -1;
367 }
368 memcpy(mmc_buf, src, part_end);
wdenk20787e22005-04-06 00:04:16 +0000369 if ((mmc_block_write(aligned_end, mmc_buf, mmc_block_size)) < 0) {
wdenk71f95112003-06-15 22:40:42 +0000370 return -1;
371 }
372 }
373 return 0;
374}
375
wdenk7205e402003-09-10 22:30:53 +0000376ulong
wdenk71f95112003-06-15 22:40:42 +0000377/****************************************************/
Jean-Christophe PLAGNIOL-VILLARD0937b8d2008-02-17 14:15:32 +0100378mmc_bread(int dev_num, ulong blknr, lbaint_t blkcnt, void *dst)
wdenk71f95112003-06-15 22:40:42 +0000379/****************************************************/
380{
381 int mmc_block_size = MMC_BLOCK_SIZE;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200382 ulong src = blknr * mmc_block_size + CONFIG_SYS_MMC_BASE;
wdenk71f95112003-06-15 22:40:42 +0000383
stefano babicc95219f2007-11-20 10:40:24 +0100384 mmc_read(src, (uchar *) dst, blkcnt * mmc_block_size);
wdenk71f95112003-06-15 22:40:42 +0000385 return blkcnt;
386}
387
stefano babicc95219f2007-11-20 10:40:24 +0100388#ifdef __GNUC__
389#define likely(x) __builtin_expect(!!(x), 1)
390#define unlikely(x) __builtin_expect(!!(x), 0)
391#else
392#define likely(x) (x)
393#define unlikely(x) (x)
394#endif
395
396#define UNSTUFF_BITS(resp,start,size) \
397 ({ \
398 const int __size = size; \
399 const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \
400 const int32_t __off = 3 - ((start) / 32); \
401 const int32_t __shft = (start) & 31; \
402 uint32_t __res; \
403 \
404 __res = resp[__off] >> __shft; \
405 if (__size + __shft > 32) \
406 __res |= resp[__off-1] << ((32 - __shft) % 32); \
407 __res & __mask; \
408 })
409
410/*
411 * Given the decoded CSD structure, decode the raw CID to our CID structure.
412 */
413static void mmc_decode_cid(uint32_t * resp)
414{
415 if (IF_TYPE_SD == mmc_dev.if_type) {
416 /*
417 * SD doesn't currently have a version field so we will
418 * have to assume we can parse this.
419 */
420 sprintf((char *)mmc_dev.vendor,
421 "Man %02x OEM %c%c \"%c%c%c%c%c\" Date %02u/%04u",
422 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp, 112, 8),
423 UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp, 96, 8),
424 UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp, 80, 8),
425 UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp, 64, 8),
426 UNSTUFF_BITS(resp, 8, 4), UNSTUFF_BITS(resp, 12,
427 8) + 2000);
428 sprintf((char *)mmc_dev.revision, "%d.%d",
429 UNSTUFF_BITS(resp, 60, 4), UNSTUFF_BITS(resp, 56, 4));
430 sprintf((char *)mmc_dev.product, "%u",
431 UNSTUFF_BITS(resp, 24, 32));
432 } else {
433 /*
434 * The selection of the format here is based upon published
435 * specs from sandisk and from what people have reported.
436 */
437 switch (spec_ver) {
438 case 0: /* MMC v1.0 - v1.2 */
439 case 1: /* MMC v1.4 */
440 sprintf((char *)mmc_dev.vendor,
441 "Man %02x%02x%02x \"%c%c%c%c%c%c%c\" Date %02u/%04u",
442 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
443 112,
444 8),
445 UNSTUFF_BITS(resp, 104, 8), UNSTUFF_BITS(resp,
446 96, 8),
447 UNSTUFF_BITS(resp, 88, 8), UNSTUFF_BITS(resp,
448 80, 8),
449 UNSTUFF_BITS(resp, 72, 8), UNSTUFF_BITS(resp,
450 64, 8),
451 UNSTUFF_BITS(resp, 56, 8), UNSTUFF_BITS(resp,
452 48, 8),
453 UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
454 4) +
455 1997);
456 sprintf((char *)mmc_dev.revision, "%d.%d",
457 UNSTUFF_BITS(resp, 44, 4), UNSTUFF_BITS(resp,
458 40, 4));
459 sprintf((char *)mmc_dev.product, "%u",
460 UNSTUFF_BITS(resp, 16, 24));
461 break;
462
463 case 2: /* MMC v2.0 - v2.2 */
464 case 3: /* MMC v3.1 - v3.3 */
465 case 4: /* MMC v4 */
466 sprintf((char *)mmc_dev.vendor,
467 "Man %02x OEM %04x \"%c%c%c%c%c%c\" Date %02u/%04u",
468 UNSTUFF_BITS(resp, 120, 8), UNSTUFF_BITS(resp,
469 104,
470 16),
471 UNSTUFF_BITS(resp, 96, 8), UNSTUFF_BITS(resp,
472 88, 8),
473 UNSTUFF_BITS(resp, 80, 8), UNSTUFF_BITS(resp,
474 72, 8),
475 UNSTUFF_BITS(resp, 64, 8), UNSTUFF_BITS(resp,
476 56, 8),
477 UNSTUFF_BITS(resp, 12, 4), UNSTUFF_BITS(resp, 8,
478 4) +
479 1997);
480 sprintf((char *)mmc_dev.product, "%u",
481 UNSTUFF_BITS(resp, 16, 32));
482 sprintf((char *)mmc_dev.revision, "N/A");
483 break;
484
485 default:
486 printf("MMC card has unknown MMCA version %d\n",
487 spec_ver);
488 break;
489 }
490 }
491 printf("%s card.\nVendor: %s\nProduct: %s\nRevision: %s\n",
492 (IF_TYPE_SD == mmc_dev.if_type) ? "SD" : "MMC", mmc_dev.vendor,
493 mmc_dev.product, mmc_dev.revision);
494}
495
496/*
497 * Given a 128-bit response, decode to our card CSD structure.
498 */
499static void mmc_decode_csd(uint32_t * resp)
500{
501 unsigned int mult, csd_struct;
502
503 if (IF_TYPE_SD == mmc_dev.if_type) {
504 csd_struct = UNSTUFF_BITS(resp, 126, 2);
505 if (csd_struct != 0) {
506 printf("SD: unrecognised CSD structure version %d\n",
507 csd_struct);
508 return;
509 }
510 } else {
511 /*
512 * We only understand CSD structure v1.1 and v1.2.
513 * v1.2 has extra information in bits 15, 11 and 10.
514 */
515 csd_struct = UNSTUFF_BITS(resp, 126, 2);
516 if (csd_struct != 1 && csd_struct != 2) {
517 printf("MMC: unrecognised CSD structure version %d\n",
518 csd_struct);
519 return;
520 }
521
522 spec_ver = UNSTUFF_BITS(resp, 122, 4);
523 mmc_dev.if_type = IF_TYPE_MMC;
524 }
525
526 mult = 1 << (UNSTUFF_BITS(resp, 47, 3) + 2);
527 mmc_dev.lba = (1 + UNSTUFF_BITS(resp, 62, 12)) * mult;
528 mmc_dev.blksz = 1 << UNSTUFF_BITS(resp, 80, 4);
529
530 /* FIXME: The following just makes assumes that's the partition type -- should really read it */
531 mmc_dev.part_type = PART_TYPE_DOS;
532 mmc_dev.dev = 0;
533 mmc_dev.lun = 0;
534 mmc_dev.type = DEV_TYPE_HARDDISK;
535 mmc_dev.removable = 0;
536 mmc_dev.block_read = mmc_bread;
537
Jean-Christophe PLAGNIOL-VILLARD0a5676b2008-07-12 14:36:34 +0200538 printf("Detected: %lu blocks of %lu bytes (%luMB) ",
539 mmc_dev.lba,
540 mmc_dev.blksz,
541 mmc_dev.lba * mmc_dev.blksz / (1024 * 1024));
stefano babicc95219f2007-11-20 10:40:24 +0100542}
543
wdenk71f95112003-06-15 22:40:42 +0000544int
545/****************************************************/
546mmc_init(int verbose)
547/****************************************************/
548{
stefano babicc95219f2007-11-20 10:40:24 +0100549 int retries, rc = -ENODEV;
550 uint32_t cid_resp[4];
551 uint32_t *resp;
552 uint16_t rca = 0;
wdenk71f95112003-06-15 22:40:42 +0000553
stefano babicc95219f2007-11-20 10:40:24 +0100554 /* Reset device interface type */
555 mmc_dev.if_type = IF_TYPE_UNKNOWN;
556
557#if defined (CONFIG_LUBBOCK) || (defined (CONFIG_GUMSTIX) && !defined(CONFIG_PXA27X))
558 set_GPIO_mode(GPIO6_MMCCLK_MD);
559 set_GPIO_mode(GPIO8_MMCCS0_MD);
wdenk71f95112003-06-15 22:40:42 +0000560#endif
stefano babicc95219f2007-11-20 10:40:24 +0100561 CKEN |= CKEN12_MMC; /* enable MMC unit clock */
wdenk71f95112003-06-15 22:40:42 +0000562
stefano babicc95219f2007-11-20 10:40:24 +0100563 MMC_CLKRT = MMC_CLKRT_0_3125MHZ;
564 MMC_RESTO = MMC_RES_TO_MAX;
565 MMC_SPI = MMC_SPI_DISABLE;
wdenk71f95112003-06-15 22:40:42 +0000566
567 /* reset */
Haavard Skinnemoen341188b2008-05-22 11:09:59 +0200568 mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, 0, MMC_CMDAT_INIT | MMC_CMDAT_R0);
stefano babicc95219f2007-11-20 10:40:24 +0100569 udelay(200000);
570 retries = 3;
571 while (retries--) {
572 resp = mmc_cmd(MMC_CMD_APP_CMD, 0, 0, MMC_CMDAT_R1);
573 if (!(resp[0] & 0x00000020)) { /* Card does not support APP_CMD */
574 debug("Card does not support APP_CMD\n");
575 break;
576 }
577
Adrian Filipic455d072008-07-18 11:52:02 -0400578 /* Select 3.2-3.3V and 3.3-3.4V */
579 resp = mmc_cmd(SD_CMD_APP_SEND_OP_COND, 0x0030, 0x0000,
Haavard Skinnemoen341188b2008-05-22 11:09:59 +0200580 MMC_CMDAT_R3 | (retries < 2 ? 0
581 : MMC_CMDAT_INIT));
stefano babicc95219f2007-11-20 10:40:24 +0100582 if (resp[0] & 0x80000000) {
583 mmc_dev.if_type = IF_TYPE_SD;
584 debug("Detected SD card\n");
585 break;
586 }
wdenk20787e22005-04-06 00:04:16 +0000587#ifdef CONFIG_PXA27X
588 udelay(10000);
589#else
stefano babicc95219f2007-11-20 10:40:24 +0100590 udelay(200000);
wdenk20787e22005-04-06 00:04:16 +0000591#endif
stefano babicc95219f2007-11-20 10:40:24 +0100592 }
593
594 if (retries <= 0 || !(IF_TYPE_SD == mmc_dev.if_type)) {
595 debug("Failed to detect SD Card, trying MMC\n");
596 resp =
597 mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000, MMC_CMDAT_R3);
598
599 retries = 10;
600 while (retries-- && resp && !(resp[0] & 0x80000000)) {
601#ifdef CONFIG_PXA27X
602 udelay(10000);
603#else
604 udelay(200000);
605#endif
606 resp =
607 mmc_cmd(MMC_CMD_SEND_OP_COND, 0x00ff, 0x8000,
608 MMC_CMDAT_R3);
609 }
wdenk71f95112003-06-15 22:40:42 +0000610 }
611
612 /* try to get card id */
stefano babicc95219f2007-11-20 10:40:24 +0100613 resp =
614 mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, 0, MMC_CMDAT_R2 | MMC_CMDAT_BUSY);
wdenk20787e22005-04-06 00:04:16 +0000615 if (resp) {
stefano babicc95219f2007-11-20 10:40:24 +0100616 memcpy(cid_resp, resp, sizeof(cid_resp));
wdenk71f95112003-06-15 22:40:42 +0000617
618 /* MMC exists, get CSD too */
Haavard Skinnemoen341188b2008-05-22 11:09:59 +0200619 resp = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, 0, 0, MMC_CMDAT_R1);
stefano babicc95219f2007-11-20 10:40:24 +0100620 if (IF_TYPE_SD == mmc_dev.if_type)
621 rca = ((resp[0] & 0xffff0000) >> 16);
622 resp = mmc_cmd(MMC_CMD_SEND_CSD, rca, 0, MMC_CMDAT_R2);
wdenk20787e22005-04-06 00:04:16 +0000623 if (resp) {
stefano babicc95219f2007-11-20 10:40:24 +0100624 mmc_decode_csd(resp);
wdenk71f95112003-06-15 22:40:42 +0000625 rc = 0;
626 mmc_ready = 1;
wdenk71f95112003-06-15 22:40:42 +0000627 }
stefano babicc95219f2007-11-20 10:40:24 +0100628
629 mmc_decode_cid(cid_resp);
wdenk71f95112003-06-15 22:40:42 +0000630 }
631
stefano babicc95219f2007-11-20 10:40:24 +0100632 MMC_CLKRT = 0; /* 20 MHz */
633 resp = mmc_cmd(MMC_CMD_SELECT_CARD, rca, 0, MMC_CMDAT_R1);
wdenk71f95112003-06-15 22:40:42 +0000634
stefano babicc95219f2007-11-20 10:40:24 +0100635#ifdef CONFIG_PXA27X
636 if (IF_TYPE_SD == mmc_dev.if_type) {
637 resp = mmc_cmd(MMC_CMD_APP_CMD, rca, 0, MMC_CMDAT_R1);
638 resp = mmc_cmd(SD_CMD_APP_SET_BUS_WIDTH, 0, 2, MMC_CMDAT_R1);
639 wide = MMC_CMDAT_SD_4DAT;
640 }
641#endif
642
643 fat_register_device(&mmc_dev, 1); /* partitions start counting with 1 */
wdenk71f95112003-06-15 22:40:42 +0000644
645 return rc;
646}
647
stefano babicc95219f2007-11-20 10:40:24 +0100648int mmc_ident(block_dev_desc_t * dev)
wdenk71f95112003-06-15 22:40:42 +0000649{
650 return 0;
651}
652
stefano babicc95219f2007-11-20 10:40:24 +0100653int mmc2info(ulong addr)
wdenk71f95112003-06-15 22:40:42 +0000654{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200655 if (addr >= CONFIG_SYS_MMC_BASE
656 && addr < CONFIG_SYS_MMC_BASE + (mmc_dev.lba * mmc_dev.blksz)) {
wdenk71f95112003-06-15 22:40:42 +0000657 return 1;
658 }
659 return 0;
660}
661
stefano babicc95219f2007-11-20 10:40:24 +0100662#endif /* CONFIG_MMC */