3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 #include <asm/errno.h>
28 #include <asm/arch/hardware.h>
34 fat_register_device(block_dev_desc_t *dev_desc, int part_no);
36 static block_dev_desc_t mmc_dev;
38 block_dev_desc_t * mmc_get_dev(int dev)
40 return (dev == 0) ? &mmc_dev : NULL;
44 * FIXME needs to read cid and csd info to determine block size
45 * and other parameters
47 static uchar mmc_buf[MMC_BLOCK_SIZE];
48 static mmc_csd_t mmc_csd;
49 static int mmc_ready = 0;
53 /****************************************************/
54 mmc_cmd(ushort cmd, ushort argh, ushort argl, ushort cmdat)
55 /****************************************************/
57 static uchar resp[20];
61 debug("mmc_cmd %x %x %x %x\n", cmd, argh, argl, cmdat);
62 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
63 MMC_I_MASK = ~MMC_I_MASK_CLK_IS_OFF;
64 while (!(MMC_I_REG & MMC_I_REG_CLK_IS_OFF));
69 MMC_I_MASK = ~MMC_I_MASK_END_CMD_RES;
70 MMC_STRPCL = MMC_STRPCL_START_CLK;
71 while (!(MMC_I_REG & MMC_I_REG_END_CMD_RES));
74 debug("MMC status %x\n", status);
75 if (status & MMC_STAT_TIME_OUT_RESPONSE) {
79 switch (cmdat & 0x3) {
92 for (i = words-1; i >= 0; i--) {
93 ulong res_fifo = MMC_RES;
96 resp[offset] = ((uchar *)&res_fifo)[0];
97 resp[offset+1] = ((uchar *)&res_fifo)[1];
100 for (i=0; i<words*2; i += 2) {
101 printf("MMC resp[%d] = %02x\n", i, resp[i]);
102 printf("MMC resp[%d] = %02x\n", i+1, resp[i+1]);
109 /****************************************************/
110 mmc_block_read(uchar *dst, ulong src, ulong len)
111 /****************************************************/
121 debug("mmc_block_rd dst %lx src %lx len %d\n", (ulong)dst, src, len);
127 resp = mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
129 /* send read command */
132 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
136 resp = mmc_cmd(MMC_CMD_READ_BLOCK, argh, argl,
137 MMC_CMDAT_R1|MMC_CMDAT_READ|MMC_CMDAT_BLOCK|MMC_CMDAT_DATA_EN);
140 MMC_I_MASK = ~MMC_I_MASK_RXFIFO_RD_REQ;
142 if (MMC_I_REG & MMC_I_REG_RXFIFO_RD_REQ) {
145 for (i=min(len,32); i; i--) {
146 *dst++ = * ((volatile uchar *) &MMC_RXFIFO);
155 if (status & MMC_STAT_ERRORS) {
156 printf("MMC_STAT error %lx\n", status);
160 MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
161 while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE));
163 if (status & MMC_STAT_ERRORS) {
164 printf("MMC_STAT error %lx\n", status);
171 /****************************************************/
172 mmc_block_write(ulong dst, uchar *src, int len)
173 /****************************************************/
183 debug("mmc_block_wr dst %lx src %lx len %d\n", dst, (ulong)src, len);
189 resp = mmc_cmd(MMC_CMD_SET_BLOCKLEN, argh, argl, MMC_CMDAT_R1);
191 /* send write command */
194 MMC_STRPCL = MMC_STRPCL_STOP_CLK;
197 resp = mmc_cmd(MMC_CMD_WRITE_BLOCK, argh, argl,
198 MMC_CMDAT_R1|MMC_CMDAT_WRITE|MMC_CMDAT_BLOCK|MMC_CMDAT_DATA_EN);
200 MMC_I_MASK = ~MMC_I_MASK_TXFIFO_WR_REQ;
202 if (MMC_I_REG & MMC_I_REG_TXFIFO_WR_REQ) {
203 int i, bytes = min(32,len);
205 for (i=0; i<bytes; i++) {
209 MMC_PRTBUF = MMC_PRTBUF_BUF_PART_FULL;
214 if (status & MMC_STAT_ERRORS) {
215 printf("MMC_STAT error %lx\n", status);
219 MMC_I_MASK = ~MMC_I_MASK_DATA_TRAN_DONE;
220 while (!(MMC_I_REG & MMC_I_REG_DATA_TRAN_DONE));
221 MMC_I_MASK = ~MMC_I_MASK_PRG_DONE;
222 while (!(MMC_I_REG & MMC_I_REG_PRG_DONE));
224 if (status & MMC_STAT_ERRORS) {
225 printf("MMC_STAT error %lx\n", status);
233 /****************************************************/
234 mmc_read(ulong src, uchar *dst, int size)
235 /****************************************************/
237 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
238 ulong mmc_block_size, mmc_block_address;
245 printf("Please initial the MMC first\n");
249 mmc_block_size = MMC_BLOCK_SIZE;
250 mmc_block_address = ~(mmc_block_size - 1);
254 part_start = ~mmc_block_address & src;
255 part_end = ~mmc_block_address & end;
256 aligned_start = mmc_block_address & src;
257 aligned_end = mmc_block_address & end;
259 /* all block aligned accesses */
260 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
261 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
263 part_len = mmc_block_size - part_start;
264 debug("ps 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, aligned_end);
266 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) < 0) {
269 memcpy(dst, mmc_buf+part_start, part_len);
273 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
274 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
275 for (; src < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
276 debug("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
277 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
278 if ((mmc_block_read((uchar *)(dst), src, mmc_block_size)) < 0) {
282 debug("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, aligned_end);
284 if (part_end && src < end) {
285 debug("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
286 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
287 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
290 memcpy(dst, mmc_buf, part_end);
296 /****************************************************/
297 mmc_write(uchar *src, ulong dst, int size)
298 /****************************************************/
300 ulong end, part_start, part_end, part_len, aligned_start, aligned_end;
301 ulong mmc_block_size, mmc_block_address;
308 printf("Please initial the MMC first\n");
312 mmc_block_size = MMC_BLOCK_SIZE;
313 mmc_block_address = ~(mmc_block_size - 1);
317 part_start = ~mmc_block_address & dst;
318 part_end = ~mmc_block_address & end;
319 aligned_start = mmc_block_address & dst;
320 aligned_end = mmc_block_address & end;
322 /* all block aligned accesses */
323 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
324 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
326 part_len = mmc_block_size - part_start;
327 debug("ps src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
328 (ulong)src, dst, end, part_start, part_end, aligned_start, aligned_end);
329 if ((mmc_block_read(mmc_buf, aligned_start, mmc_block_size)) < 0) {
332 memcpy(mmc_buf+part_start, src, part_len);
333 if ((mmc_block_write(aligned_start, mmc_buf, mmc_block_size)) < 0) {
339 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
340 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
341 for (; dst < aligned_end; src += mmc_block_size, dst += mmc_block_size) {
342 debug("al src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
343 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
344 if ((mmc_block_write(dst, (uchar *)src, mmc_block_size)) < 0) {
348 debug("src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
349 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
350 if (part_end && dst < end) {
351 debug("pe src %lx dst %lx end %lx pstart %lx pend %lx astart %lx aend %lx\n",
352 src, (ulong)dst, end, part_start, part_end, aligned_start, aligned_end);
353 if ((mmc_block_read(mmc_buf, aligned_end, mmc_block_size)) < 0) {
356 memcpy(mmc_buf, src, part_end);
357 if ((mmc_block_write(aligned_end, mmc_buf, mmc_block_size)) < 0) {
365 /****************************************************/
366 mmc_bread(int dev_num, ulong blknr, ulong blkcnt, ulong *dst)
367 /****************************************************/
369 int mmc_block_size = MMC_BLOCK_SIZE;
370 ulong src = blknr * mmc_block_size + CFG_MMC_BASE;
372 mmc_read(src, (uchar *)dst, blkcnt*mmc_block_size);
377 /****************************************************/
378 mmc_init(int verbose)
379 /****************************************************/
381 int retries, rc = -ENODEV;
384 #ifdef CONFIG_LUBBOCK
385 set_GPIO_mode( GPIO6_MMCCLK_MD );
386 set_GPIO_mode( GPIO8_MMCCS0_MD );
388 CKEN |= CKEN12_MMC; /* enable MMC unit clock */
389 #if defined(CONFIG_ADSVIX)
390 /* turn on the power */
391 GPCR(114) = GPIO_bit(114);
397 MMC_CLKRT = MMC_CLKRT_0_3125MHZ;
398 MMC_RESTO = MMC_RES_TO_MAX;
399 MMC_SPI = MMC_SPI_DISABLE;
403 resp = mmc_cmd(0, 0, 0, 0);
404 resp = mmc_cmd(1, 0x00ff, 0xc000, MMC_CMDAT_INIT|MMC_CMDAT_BUSY|MMC_CMDAT_R3);
405 while (retries-- && resp && !(resp[4] & 0x80)) {
406 debug("resp %x %x\n", resp[0], resp[1]);
412 resp = mmc_cmd(1, 0x00ff, 0xff00, MMC_CMDAT_BUSY|MMC_CMDAT_R3);
415 /* try to get card id */
416 resp = mmc_cmd(2, 0, 0, MMC_CMDAT_R2);
418 /* TODO configure mmc driver depending on card attributes */
419 mmc_cid_t *cid = (mmc_cid_t *)resp;
421 printf("MMC found. Card desciption is:\n");
422 printf("Manufacturer ID = %02x%02x%02x\n",
423 cid->id[0], cid->id[1], cid->id[2]);
424 printf("HW/FW Revision = %x %x\n",cid->hwrev, cid->fwrev);
425 cid->hwrev = cid->fwrev = 0; /* null terminate string */
426 printf("Product Name = %s\n",cid->name);
427 printf("Serial Number = %02x%02x%02x\n",
428 cid->sn[0], cid->sn[1], cid->sn[2]);
429 printf("Month = %d\n",cid->month);
430 printf("Year = %d\n",1997 + cid->year);
432 /* fill in device description */
433 mmc_dev.if_type = IF_TYPE_MMC;
434 mmc_dev.part_type = PART_TYPE_DOS;
438 /* FIXME fill in the correct size (is set to 32MByte) */
440 mmc_dev.lba = 0x10000;
441 sprintf(mmc_dev.vendor,"Man %02x%02x%02x Snr %02x%02x%02x",
442 cid->id[0], cid->id[1], cid->id[2],
443 cid->sn[0], cid->sn[1], cid->sn[2]);
444 sprintf(mmc_dev.product,"%s",cid->name);
445 sprintf(mmc_dev.revision,"%x %x",cid->hwrev, cid->fwrev);
446 mmc_dev.removable = 0;
447 mmc_dev.block_read = mmc_bread;
449 /* MMC exists, get CSD too */
450 resp = mmc_cmd(MMC_CMD_SET_RCA, MMC_DEFAULT_RCA, 0, MMC_CMDAT_R1);
451 resp = mmc_cmd(MMC_CMD_SEND_CSD, MMC_DEFAULT_RCA, 0, MMC_CMDAT_R2);
453 mmc_csd_t *csd = (mmc_csd_t *)resp;
454 memcpy(&mmc_csd, csd, sizeof(csd));
457 /* FIXME add verbose printout for csd */
462 MMC_CLKRT = 1; /* 10 MHz - see Intel errata */
464 MMC_CLKRT = 0; /* 20 MHz */
466 resp = mmc_cmd(7, MMC_DEFAULT_RCA, 0, MMC_CMDAT_R1);
468 fat_register_device(&mmc_dev,1); /* partitions start counting with 1 */
474 mmc_ident(block_dev_desc_t *dev)
482 /* FIXME hard codes to 32 MB device */
483 if (addr >= CFG_MMC_BASE && addr < CFG_MMC_BASE + 0x02000000) {
489 #endif /* CONFIG_MMC */