atmel_mci: Use 512 byte blocksize if possible
[platform/kernel/u-boot.git] / cpu / at32ap / atmel_mci.c
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 #include <common.h>
23
24 #ifdef CONFIG_MMC
25
26 #include <part.h>
27 #include <mmc.h>
28
29 #include <asm/io.h>
30 #include <asm/errno.h>
31 #include <asm/byteorder.h>
32 #include <asm/arch/clk.h>
33 #include <asm/arch/memory-map.h>
34
35 #include "atmel_mci.h"
36
37 #ifdef DEBUG
38 #define pr_debug(fmt, args...) printf(fmt, ##args)
39 #else
40 #define pr_debug(...) do { } while(0)
41 #endif
42
43 #ifndef CFG_MMC_CLK_OD
44 #define CFG_MMC_CLK_OD          150000
45 #endif
46
47 #ifndef CFG_MMC_CLK_PP
48 #define CFG_MMC_CLK_PP          5000000
49 #endif
50
51 #ifndef CFG_MMC_OP_COND
52 #define CFG_MMC_OP_COND         0x00100000
53 #endif
54
55 #define MMC_DEFAULT_BLKLEN      512
56 #define MMC_DEFAULT_RCA         1
57
58 static unsigned int mmc_rca;
59 static block_dev_desc_t mmc_blkdev;
60
61 block_dev_desc_t *mmc_get_dev(int dev)
62 {
63         return &mmc_blkdev;
64 }
65
66 static void mci_set_mode(unsigned long hz, unsigned long blklen)
67 {
68         unsigned long bus_hz;
69         unsigned long clkdiv;
70
71         bus_hz = get_mci_clk_rate();
72         clkdiv = (bus_hz / hz) / 2 - 1;
73
74         pr_debug("mmc: setting clock %lu Hz, block size %lu\n",
75                  hz, blklen);
76
77         if (clkdiv & ~255UL) {
78                 clkdiv = 255;
79                 printf("mmc: clock %lu too low; setting CLKDIV to 255\n",
80                         hz);
81         }
82
83         blklen &= 0xfffc;
84         mmci_writel(MR, (MMCI_BF(CLKDIV, clkdiv)
85                          | MMCI_BF(BLKLEN, blklen)
86                          | MMCI_BIT(RDPROOF)
87                          | MMCI_BIT(WRPROOF)));
88 }
89
90 #define RESP_NO_CRC     1
91 #define R1              MMCI_BF(RSPTYP, 1)
92 #define R2              MMCI_BF(RSPTYP, 2)
93 #define R3              (R1 | RESP_NO_CRC)
94 #define R6              R1
95 #define NID             MMCI_BF(MAXLAT, 0)
96 #define NCR             MMCI_BF(MAXLAT, 1)
97 #define TRCMD_START     MMCI_BF(TRCMD, 1)
98 #define TRDIR_READ      MMCI_BF(TRDIR, 1)
99 #define TRTYP_BLOCK     MMCI_BF(TRTYP, 0)
100 #define INIT_CMD        MMCI_BF(SPCMD, 1)
101 #define OPEN_DRAIN      MMCI_BF(OPDCMD, 1)
102
103 #define ERROR_FLAGS     (MMCI_BIT(DTOE)                 \
104                          | MMCI_BIT(RDIRE)              \
105                          | MMCI_BIT(RENDE)              \
106                          | MMCI_BIT(RINDE)              \
107                          | MMCI_BIT(RTOE))
108
109 static int
110 mmc_cmd(unsigned long cmd, unsigned long arg,
111         void *resp, unsigned long flags)
112 {
113         unsigned long *response = resp;
114         int i, response_words = 0;
115         unsigned long error_flags;
116         u32 status;
117
118         pr_debug("mmc: CMD%lu 0x%lx (flags 0x%lx)\n",
119                  cmd, arg, flags);
120
121         error_flags = ERROR_FLAGS;
122         if (!(flags & RESP_NO_CRC))
123                 error_flags |= MMCI_BIT(RCRCE);
124
125         flags &= ~MMCI_BF(CMDNB, ~0UL);
126
127         if (MMCI_BFEXT(RSPTYP, flags) == MMCI_RSPTYP_48_BIT_RESP)
128                 response_words = 1;
129         else if (MMCI_BFEXT(RSPTYP, flags) == MMCI_RSPTYP_136_BIT_RESP)
130                 response_words = 4;
131
132         mmci_writel(ARGR, arg);
133         mmci_writel(CMDR, cmd | flags);
134         do {
135                 udelay(40);
136                 status = mmci_readl(SR);
137         } while (!(status & MMCI_BIT(CMDRDY)));
138
139         pr_debug("mmc: status 0x%08lx\n", status);
140
141         if (status & ERROR_FLAGS) {
142                 printf("mmc: command %lu failed (status: 0x%08lx)\n",
143                        cmd, status);
144                 return -EIO;
145         }
146
147         if (response_words)
148                 pr_debug("mmc: response:");
149
150         for (i = 0; i < response_words; i++) {
151                 response[i] = mmci_readl(RSPR);
152                 pr_debug(" %08lx", response[i]);
153         }
154         pr_debug("\n");
155
156         return 0;
157 }
158
159 static int mmc_acmd(unsigned long cmd, unsigned long arg,
160                     void *resp, unsigned long flags)
161 {
162         unsigned long aresp[4];
163         int ret;
164
165         /*
166          * Seems like the APP_CMD part of an ACMD has 64 cycles max
167          * latency even though the ACMD part doesn't. This isn't
168          * entirely clear in the SD Card spec, but some cards refuse
169          * to work if we attempt to use 5 cycles max latency here...
170          */
171         ret = mmc_cmd(MMC_CMD_APP_CMD, 0, aresp,
172                       R1 | NCR | (flags & OPEN_DRAIN));
173         if (ret)
174                 return ret;
175         if ((aresp[0] & (R1_ILLEGAL_COMMAND | R1_APP_CMD)) != R1_APP_CMD)
176                 return -ENODEV;
177
178         ret = mmc_cmd(cmd, arg, resp, flags);
179         return ret;
180 }
181
182 static unsigned long
183 mmc_bread(int dev, unsigned long start, lbaint_t blkcnt,
184           unsigned long *buffer)
185 {
186         int ret, i = 0;
187         unsigned long resp[4];
188         unsigned long card_status, data;
189         unsigned long wordcount;
190         u32 status;
191
192         if (blkcnt == 0)
193                 return 0;
194
195         pr_debug("mmc_bread: dev %d, start %lx, blkcnt %lx\n",
196                  dev, start, blkcnt);
197
198         /* Put the device into Transfer state */
199         ret = mmc_cmd(MMC_CMD_SELECT_CARD, mmc_rca << 16, resp, R1 | NCR);
200         if (ret) goto fail;
201
202         /* Set block length */
203         ret = mmc_cmd(MMC_CMD_SET_BLOCKLEN, mmc_blkdev.blksz, resp, R1 | NCR);
204         if (ret) goto fail;
205
206         pr_debug("MCI_DTOR = %08lx\n", mmci_readl(DTOR));
207
208         for (i = 0; i < blkcnt; i++, start++) {
209                 ret = mmc_cmd(MMC_CMD_READ_SINGLE_BLOCK,
210                               start * mmc_blkdev.blksz, resp,
211                               (R1 | NCR | TRCMD_START | TRDIR_READ
212                                | TRTYP_BLOCK));
213                 if (ret) goto fail;
214
215                 ret = -EIO;
216                 wordcount = 0;
217                 do {
218                         do {
219                                 status = mmci_readl(SR);
220                                 if (status & (ERROR_FLAGS | MMCI_BIT(OVRE)))
221                                         goto fail;
222                         } while (!(status & MMCI_BIT(RXRDY)));
223
224                         if (status & MMCI_BIT(RXRDY)) {
225                                 data = mmci_readl(RDR);
226                                 /* pr_debug("%x\n", data); */
227                                 *buffer++ = data;
228                                 wordcount++;
229                         }
230                 } while(wordcount < (mmc_blkdev.blksz / 4));
231
232                 pr_debug("mmc: read %u words, waiting for BLKE\n", wordcount);
233
234                 do {
235                         status = mmci_readl(SR);
236                 } while (!(status & MMCI_BIT(BLKE)));
237
238                 putc('.');
239         }
240
241 out:
242         /* Put the device back into Standby state */
243         mmc_cmd(MMC_CMD_SELECT_CARD, 0, resp, NCR);
244         return i;
245
246 fail:
247         mmc_cmd(MMC_CMD_SEND_STATUS, mmc_rca << 16, &card_status, R1 | NCR);
248         printf("mmc: bread failed, card status = %08x\n", card_status);
249         goto out;
250 }
251
252 static void mmc_parse_cid(struct mmc_cid *cid, unsigned long *resp)
253 {
254         cid->mid = resp[0] >> 24;
255         cid->oid = (resp[0] >> 8) & 0xffff;
256         cid->pnm[0] = resp[0];
257         cid->pnm[1] = resp[1] >> 24;
258         cid->pnm[2] = resp[1] >> 16;
259         cid->pnm[3] = resp[1] >> 8;
260         cid->pnm[4] = resp[1];
261         cid->pnm[5] = resp[2] >> 24;
262         cid->pnm[6] = 0;
263         cid->prv = resp[2] >> 16;
264         cid->psn = (resp[2] << 16) | (resp[3] >> 16);
265         cid->mdt = resp[3] >> 8;
266 }
267
268 static void sd_parse_cid(struct mmc_cid *cid, unsigned long *resp)
269 {
270         cid->mid = resp[0] >> 24;
271         cid->oid = (resp[0] >> 8) & 0xffff;
272         cid->pnm[0] = resp[0];
273         cid->pnm[1] = resp[1] >> 24;
274         cid->pnm[2] = resp[1] >> 16;
275         cid->pnm[3] = resp[1] >> 8;
276         cid->pnm[4] = resp[1];
277         cid->pnm[5] = 0;
278         cid->pnm[6] = 0;
279         cid->prv = resp[2] >> 24;
280         cid->psn = (resp[2] << 8) | (resp[3] >> 24);
281         cid->mdt = (resp[3] >> 8) & 0x0fff;
282 }
283
284 static void mmc_dump_cid(const struct mmc_cid *cid)
285 {
286         printf("Manufacturer ID:       %02lX\n", cid->mid);
287         printf("OEM/Application ID:    %04lX\n", cid->oid);
288         printf("Product name:          %s\n", cid->pnm);
289         printf("Product Revision:      %lu.%lu\n",
290                cid->prv >> 4, cid->prv & 0x0f);
291         printf("Product Serial Number: %lu\n", cid->psn);
292         printf("Manufacturing Date:    %02lu/%02lu\n",
293                cid->mdt >> 4, cid->mdt & 0x0f);
294 }
295
296 static void mmc_dump_csd(const struct mmc_csd *csd)
297 {
298         unsigned long *csd_raw = (unsigned long *)csd;
299         printf("CSD data: %08lx %08lx %08lx %08lx\n",
300                csd_raw[0], csd_raw[1], csd_raw[2], csd_raw[3]);
301         printf("CSD structure version:   1.%u\n", csd->csd_structure);
302         printf("MMC System Spec version: %u\n", csd->spec_vers);
303         printf("Card command classes:    %03x\n", csd->ccc);
304         printf("Read block length:       %u\n", 1 << csd->read_bl_len);
305         if (csd->read_bl_partial)
306                 puts("Supports partial reads\n");
307         else
308                 puts("Does not support partial reads\n");
309         printf("Write block length:      %u\n", 1 << csd->write_bl_len);
310         if (csd->write_bl_partial)
311                 puts("Supports partial writes\n");
312         else
313                 puts("Does not support partial writes\n");
314         if (csd->wp_grp_enable)
315                 printf("Supports group WP:      %u\n", csd->wp_grp_size + 1);
316         else
317                 puts("Does not support group WP\n");
318         printf("Card capacity:          %u bytes\n",
319                (csd->c_size + 1) * (1 << (csd->c_size_mult + 2)) *
320                (1 << csd->read_bl_len));
321         printf("File format:            %u/%u\n",
322                csd->file_format_grp, csd->file_format);
323         puts("Write protection:        ");
324         if (csd->perm_write_protect)
325                 puts(" permanent");
326         if (csd->tmp_write_protect)
327                 puts(" temporary");
328         putc('\n');
329 }
330
331 static int mmc_idle_cards(void)
332 {
333         int ret;
334
335         /* Reset and initialize all cards */
336         ret = mmc_cmd(MMC_CMD_GO_IDLE_STATE, 0, NULL, 0);
337         if (ret)
338                 return ret;
339
340         /* Keep the bus idle for 74 clock cycles */
341         return mmc_cmd(0, 0, NULL, INIT_CMD);
342 }
343
344 static int sd_init_card(struct mmc_cid *cid, int verbose)
345 {
346         unsigned long resp[4];
347         int i, ret = 0;
348
349         mmc_idle_cards();
350         for (i = 0; i < 1000; i++) {
351                 ret = mmc_acmd(MMC_ACMD_SD_SEND_OP_COND, CFG_MMC_OP_COND,
352                                resp, R3 | NID);
353                 if (ret || (resp[0] & 0x80000000))
354                         break;
355                 ret = -ETIMEDOUT;
356         }
357
358         if (ret)
359                 return ret;
360
361         ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID);
362         if (ret)
363                 return ret;
364         sd_parse_cid(cid, resp);
365         if (verbose)
366                 mmc_dump_cid(cid);
367
368         /* Get RCA of the card that responded */
369         ret = mmc_cmd(MMC_CMD_SD_SEND_RELATIVE_ADDR, 0, resp, R6 | NCR);
370         if (ret)
371                 return ret;
372
373         mmc_rca = resp[0] >> 16;
374         if (verbose)
375                 printf("SD Card detected (RCA %u)\n", mmc_rca);
376         return 0;
377 }
378
379 static int mmc_init_card(struct mmc_cid *cid, int verbose)
380 {
381         unsigned long resp[4];
382         int i, ret = 0;
383
384         mmc_idle_cards();
385         for (i = 0; i < 1000; i++) {
386                 ret = mmc_cmd(MMC_CMD_SEND_OP_COND, CFG_MMC_OP_COND, resp,
387                               R3 | NID | OPEN_DRAIN);
388                 if (ret || (resp[0] & 0x80000000))
389                         break;
390                 ret = -ETIMEDOUT;
391         }
392
393         if (ret)
394                 return ret;
395
396         /* Get CID of all cards. FIXME: Support more than one card */
397         ret = mmc_cmd(MMC_CMD_ALL_SEND_CID, 0, resp, R2 | NID | OPEN_DRAIN);
398         if (ret)
399                 return ret;
400         mmc_parse_cid(cid, resp);
401         if (verbose)
402                 mmc_dump_cid(cid);
403
404         /* Set Relative Address of the card that responded */
405         ret = mmc_cmd(MMC_CMD_SET_RELATIVE_ADDR, mmc_rca << 16, resp,
406                       R1 | NCR | OPEN_DRAIN);
407         return ret;
408 }
409
410 int mmc_init(int verbose)
411 {
412         struct mmc_cid cid;
413         struct mmc_csd csd;
414         unsigned int max_blksz;
415         int ret;
416
417         /* Initialize controller */
418         mmci_writel(CR, MMCI_BIT(SWRST));
419         mmci_writel(CR, MMCI_BIT(MCIEN));
420         mmci_writel(DTOR, 0x5f);
421         mmci_writel(IDR, ~0UL);
422         mci_set_mode(CFG_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
423
424         ret = sd_init_card(&cid, verbose);
425         if (ret) {
426                 mmc_rca = MMC_DEFAULT_RCA;
427                 ret = mmc_init_card(&cid, verbose);
428         }
429         if (ret)
430                 return ret;
431
432         /* Get CSD from the card */
433         ret = mmc_cmd(MMC_CMD_SEND_CSD, mmc_rca << 16, &csd, R2 | NCR);
434         if (ret)
435                 return ret;
436         if (verbose)
437                 mmc_dump_csd(&csd);
438
439         /* Initialize the blockdev structure */
440         mmc_blkdev.if_type = IF_TYPE_MMC;
441         mmc_blkdev.part_type = PART_TYPE_DOS;
442         mmc_blkdev.block_read = mmc_bread;
443         sprintf((char *)mmc_blkdev.vendor,
444                 "Man %02x%04x Snr %08x",
445                 cid.mid, cid.oid, cid.psn);
446         strncpy((char *)mmc_blkdev.product, cid.pnm,
447                 sizeof(mmc_blkdev.product));
448         sprintf((char *)mmc_blkdev.revision, "%x %x",
449                 cid.prv >> 4, cid.prv & 0x0f);
450
451         /*
452          * If we can't use 512 byte blocks, refuse to deal with the
453          * card. Tons of code elsewhere seems to depend on this.
454          */
455         max_blksz = 1 << csd.read_bl_len;
456         if (max_blksz < 512 || (max_blksz > 512 && !csd.read_bl_partial)) {
457                 printf("Card does not support 512 byte reads, aborting.\n");
458                 return -ENODEV;
459         }
460         mmc_blkdev.blksz = 512;
461         mmc_blkdev.lba = (csd.c_size + 1) * (1 << (csd.c_size_mult + 2));
462
463         mci_set_mode(CFG_MMC_CLK_PP, mmc_blkdev.blksz);
464
465 #if 0
466         if (fat_register_device(&mmc_blkdev, 1))
467                 printf("Could not register MMC fat device\n");
468 #else
469         init_part(&mmc_blkdev);
470 #endif
471
472         return 0;
473 }
474
475 int mmc_read(ulong src, uchar *dst, int size)
476 {
477         return -ENOSYS;
478 }
479
480 int mmc_write(uchar *src, ulong dst, int size)
481 {
482         return -ENOSYS;
483 }
484
485 int mmc2info(ulong addr)
486 {
487         return 0;
488 }
489
490 #endif /* CONFIG_MMC */