add SDHC support in mmc driver
[u-boot-openmoko/mini2440.git] / cpu / nios / asmi.c
blobce2863e5c624729202bc225bd5f365914bb10aa3
1 /*
2 * (C) Copyright 2003, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
5 * See file CREDITS for list of people who contributed to this
6 * project.
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,
21 * MA 02111-1307 USA
24 #include <common.h>
26 #if defined(CONFIG_NIOS_ASMI)
27 #include <command.h>
28 #include <nios-io.h>
30 #if !defined(CFG_NIOS_ASMIBASE)
31 #error "*** CFG_NIOS_ASMIBASE not defined ***"
32 #endif
34 /*-----------------------------------------------------------------------*/
35 #define SHORT_HELP\
36 "asmi - read/write Cyclone ASMI configuration device.\n"
38 #define LONG_HELP\
39 "\n"\
40 "asmi erase start [end]\n"\
41 " - erase sector start or sectors start through end.\n"\
42 "asmi info\n"\
43 " - display ASMI device information.\n"\
44 "asmi protect on | off\n"\
45 " - turn device protection on or off.\n"\
46 "asmi read addr offset count\n"\
47 " - read count bytes from offset to addr.\n"\
48 "asmi write addr offset count\n"\
49 " - write count bytes to offset from addr.\n"\
50 "asmi verify addr offset count\n"\
51 " - verify count bytes at offset from addr.\n"
54 /*-----------------------------------------------------------------------*/
55 /* Operation codes for serial configuration devices
57 #define ASMI_WRITE_ENA 0x06 /* Write enable */
58 #define ASMI_WRITE_DIS 0x04 /* Write disable */
59 #define ASMI_READ_STAT 0x05 /* Read status */
60 #define ASMI_READ_BYTES 0x03 /* Read bytes */
61 #define ASMI_READ_ID 0xab /* Read silicon id */
62 #define ASMI_WRITE_STAT 0x01 /* Write status */
63 #define ASMI_WRITE_BYTES 0x02 /* Write bytes */
64 #define ASMI_ERASE_BULK 0xc7 /* Erase entire device */
65 #define ASMI_ERASE_SECT 0xd8 /* Erase sector */
67 /* Device status register bits
69 #define ASMI_STATUS_WIP (1<<0) /* Write in progress */
70 #define ASMI_STATUS_WEL (1<<1) /* Write enable latch */
72 static nios_asmi_t *asmi = (nios_asmi_t *)CFG_NIOS_ASMIBASE;
74 /***********************************************************************
75 * Device access
76 ***********************************************************************/
77 static void asmi_cs (int assert)
79 if (assert) {
80 asmi->control |= NIOS_ASMI_SSO;
81 } else {
82 /* Let all bits shift out */
83 while ((asmi->status & NIOS_ASMI_TMT) == 0)
85 asmi->control &= ~NIOS_ASMI_SSO;
89 static void asmi_tx (unsigned char c)
91 while ((asmi->status & NIOS_ASMI_TRDY) == 0)
93 asmi->txdata = c;
96 static int asmi_rx (void)
98 while ((asmi->status & NIOS_ASMI_RRDY) == 0)
100 return (asmi->rxdata);
103 static unsigned char bitrev[] = {
104 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
105 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
108 static unsigned char asmi_bitrev( unsigned char c )
110 unsigned char val;
112 val = bitrev[c>>4];
113 val |= bitrev[c & 0x0f]<<4;
114 return (val);
117 static void asmi_rcv (unsigned char *dst, int len)
119 while (len--) {
120 asmi_tx (0);
121 *dst++ = asmi_rx ();
125 static void asmi_rrcv (unsigned char *dst, int len)
127 while (len--) {
128 asmi_tx (0);
129 *dst++ = asmi_bitrev (asmi_rx ());
133 static void asmi_snd (unsigned char *src, int len)
135 while (len--) {
136 asmi_tx (*src++);
137 asmi_rx ();
141 static void asmi_rsnd (unsigned char *src, int len)
143 while (len--) {
144 asmi_tx (asmi_bitrev (*src++));
145 asmi_rx ();
149 static void asmi_wr_enable (void)
151 asmi_cs (1);
152 asmi_tx (ASMI_WRITE_ENA);
153 asmi_rx ();
154 asmi_cs (0);
157 static unsigned char asmi_status_rd (void)
159 unsigned char status;
161 asmi_cs (1);
162 asmi_tx (ASMI_READ_STAT);
163 asmi_rx ();
164 asmi_tx (0);
165 status = asmi_rx ();
166 asmi_cs (0);
167 return (status);
170 static void asmi_status_wr (unsigned char status)
172 asmi_wr_enable ();
173 asmi_cs (1);
174 asmi_tx (ASMI_WRITE_STAT);
175 asmi_rx ();
176 asmi_tx (status);
177 asmi_rx ();
178 asmi_cs (0);
179 return;
182 /***********************************************************************
183 * Device information
184 ***********************************************************************/
185 typedef struct asmi_devinfo_t {
186 const char *name; /* Device name */
187 unsigned char id; /* Device silicon id */
188 unsigned char size; /* Total size log2(bytes)*/
189 unsigned char num_sects; /* Number of sectors */
190 unsigned char sz_sect; /* Sector size log2(bytes) */
191 unsigned char sz_page; /* Page size log2(bytes) */
192 unsigned char prot_mask; /* Protection mask */
193 }asmi_devinfo_t;
195 static struct asmi_devinfo_t devinfo[] = {
196 { "EPCS1 ", 0x10, 17, 4, 15, 8, 0x0c },
197 { "EPCS4 ", 0x12, 19, 8, 16, 8, 0x1c },
198 { 0, 0, 0, 0, 0, 0 }
201 static asmi_devinfo_t *asmi_dev_find (void)
203 unsigned char buf[4];
204 unsigned char id;
205 int i;
206 struct asmi_devinfo_t *dev = NULL;
208 /* Read silicon id requires 3 "dummy bytes" before it's put
209 * on the wire.
211 buf[0] = ASMI_READ_ID;
212 buf[1] = 0;
213 buf[2] = 0;
214 buf[3] = 0;
216 asmi_cs (1);
217 asmi_snd (buf,4);
218 asmi_rcv (buf,1);
219 asmi_cs (0);
220 id = buf[0];
222 /* Find the info struct */
223 i = 0;
224 while (devinfo[i].name) {
225 if (id == devinfo[i].id) {
226 dev = &devinfo[i];
227 break;
229 i++;
232 return (dev);
235 /***********************************************************************
236 * Misc Utilities
237 ***********************************************************************/
238 static unsigned asmi_cfgsz (void)
240 unsigned sz = 0;
241 unsigned char buf[128];
242 unsigned char *p;
244 /* Read in the first 128 bytes of the device */
245 buf[0] = ASMI_READ_BYTES;
246 buf[1] = 0;
247 buf[2] = 0;
248 buf[3] = 0;
250 asmi_cs (1);
251 asmi_snd (buf,4);
252 asmi_rrcv (buf, sizeof(buf));
253 asmi_cs (0);
255 /* Search for the starting 0x6a which is followed by the
256 * 4-byte 'register' and 4-byte bit-count.
258 p = buf;
259 while (p < buf + sizeof(buf)-8) {
260 if ( *p == 0x6a ) {
261 /* Point to bit count and extract */
262 p += 5;
263 sz = *p++;
264 sz |= *p++ << 8;
265 sz |= *p++ << 16;
266 sz |= *p++ << 24;
267 /* Convert to byte count */
268 sz += 7;
269 sz >>= 3;
270 } else if (*p == 0xff) {
271 /* 0xff is ok ... just skip */
272 p++;
273 continue;
274 } else {
275 /* Not 0xff or 0x6a ... something's not
276 * right ... report 'unknown' (sz=0).
278 break;
281 return (sz);
284 static int asmi_erase (unsigned start, unsigned end)
286 unsigned off, sectsz;
287 unsigned char buf[4];
288 struct asmi_devinfo_t *dev = asmi_dev_find ();
290 if (!dev || (start>end))
291 return (-1);
293 /* Erase the requested sectors. An address is required
294 * that lies within the requested sector -- we'll just
295 * use the first address in the sector.
297 printf ("asmi erasing sector %d ", start);
298 if (start != end)
299 printf ("to %d ", end);
300 sectsz = (1 << dev->sz_sect);
301 while (start <= end) {
302 off = start * sectsz;
303 start++;
305 buf[0] = ASMI_ERASE_SECT;
306 buf[1] = off >> 16;
307 buf[2] = off >> 8;
308 buf[3] = off;
310 asmi_wr_enable ();
311 asmi_cs (1);
312 asmi_snd (buf,4);
313 asmi_cs (0);
315 printf ("."); /* Some user feedback */
317 /* Wait for erase to complete */
318 while (asmi_status_rd() & ASMI_STATUS_WIP)
321 printf (" done.\n");
322 return (0);
325 static int asmi_read (ulong addr, ulong off, ulong cnt)
327 unsigned char buf[4];
329 buf[0] = ASMI_READ_BYTES;
330 buf[1] = off >> 16;
331 buf[2] = off >> 8;
332 buf[3] = off;
334 asmi_cs (1);
335 asmi_snd (buf,4);
336 asmi_rrcv ((unsigned char *)addr, cnt);
337 asmi_cs (0);
339 return (0);
342 static
343 int asmi_write (ulong addr, ulong off, ulong cnt)
345 ulong wrcnt;
346 unsigned pgsz;
347 unsigned char buf[4];
348 struct asmi_devinfo_t *dev = asmi_dev_find ();
350 if (!dev)
351 return (-1);
353 pgsz = (1<<dev->sz_page);
354 while (cnt) {
355 if (off % pgsz)
356 wrcnt = pgsz - (off % pgsz);
357 else
358 wrcnt = pgsz;
359 wrcnt = (wrcnt > cnt) ? cnt : wrcnt;
361 buf[0] = ASMI_WRITE_BYTES;
362 buf[1] = off >> 16;
363 buf[2] = off >> 8;
364 buf[3] = off;
366 asmi_wr_enable ();
367 asmi_cs (1);
368 asmi_snd (buf,4);
369 asmi_rsnd ((unsigned char *)addr, wrcnt);
370 asmi_cs (0);
372 /* Wait for write to complete */
373 while (asmi_status_rd() & ASMI_STATUS_WIP)
376 cnt -= wrcnt;
377 off += wrcnt;
378 addr += wrcnt;
381 return (0);
384 static
385 int asmi_verify (ulong addr, ulong off, ulong cnt, ulong *err)
387 ulong rdcnt;
388 unsigned char buf[256];
389 unsigned char *start,*end;
390 int i;
392 start = end = (unsigned char *)addr;
393 while (cnt) {
394 rdcnt = (cnt>sizeof(buf)) ? sizeof(buf) : cnt;
395 asmi_read ((ulong)buf, off, rdcnt);
396 for (i=0; i<rdcnt; i++) {
397 if (*end != buf[i]) {
398 *err = end - start;
399 return(-1);
401 end++;
403 cnt -= rdcnt;
404 off += rdcnt;
406 return (0);
409 static int asmi_sect_erased (int sect, unsigned *offset,
410 struct asmi_devinfo_t *dev)
412 unsigned char buf[128];
413 unsigned off, end;
414 unsigned sectsz;
415 int i;
417 sectsz = (1 << dev->sz_sect);
418 off = sectsz * sect;
419 end = off + sectsz;
421 while (off < end) {
422 asmi_read ((ulong)buf, off, sizeof(buf));
423 for (i=0; i < sizeof(buf); i++) {
424 if (buf[i] != 0xff) {
425 *offset = off + i;
426 return (0);
429 off += sizeof(buf);
431 return (1);
435 /***********************************************************************
436 * Commands
437 ***********************************************************************/
438 static
439 void do_asmi_info (struct asmi_devinfo_t *dev, int argc, char *argv[])
441 int i;
442 unsigned char stat;
443 unsigned tmp;
444 int erased;
446 /* Basic device info */
447 printf ("%s: %d kbytes (%d sectors x %d kbytes,"
448 " %d bytes/page)\n",
449 dev->name, 1 << (dev->size-10),
450 dev->num_sects, 1 << (dev->sz_sect-10),
451 1 << dev->sz_page );
453 /* Status -- for now protection is all-or-nothing */
454 stat = asmi_status_rd();
455 printf ("status: 0x%02x (WIP:%d, WEL:%d, PROT:%s)\n",
456 stat,
457 (stat & ASMI_STATUS_WIP) ? 1 : 0,
458 (stat & ASMI_STATUS_WEL) ? 1 : 0,
459 (stat & dev->prot_mask) ? "on" : "off" );
461 /* Configuration */
462 tmp = asmi_cfgsz ();
463 if (tmp) {
464 printf ("config: 0x%06x (%d) bytes\n", tmp, tmp );
465 } else {
466 printf ("config: unknown\n" );
469 /* Sector info */
470 for (i=0; i<dev->num_sects; i++) {
471 erased = asmi_sect_erased (i, &tmp, dev);
472 printf (" %d: %06x ",
473 i, i*(1<<dev->sz_sect) );
474 if (erased)
475 printf ("erased\n");
476 else
477 printf ("data @ 0x%06x\n", tmp);
480 return;
483 static
484 void do_asmi_erase (struct asmi_devinfo_t *dev, int argc, char *argv[])
486 unsigned start,end;
488 if ((argc < 3) || (argc > 4)) {
489 printf ("USAGE: asmi erase sect [end]\n");
490 return;
492 if ((asmi_status_rd() & dev->prot_mask) != 0) {
493 printf ( "asmi: device protected.\n");
494 return;
497 start = simple_strtoul (argv[2], NULL, 10);
498 if (argc > 3)
499 end = simple_strtoul (argv[3], NULL, 10);
500 else
501 end = start;
502 if ((start >= dev->num_sects) || (start > end)) {
503 printf ("asmi: invalid sector range: [%d:%d]\n",
504 start, end );
505 return;
508 asmi_erase (start, end);
510 return;
513 static
514 void do_asmi_protect (struct asmi_devinfo_t *dev, int argc, char *argv[])
516 unsigned char stat;
518 /* For now protection is all-or-nothing to keep things
519 * simple. The protection bits don't map in a linear
520 * fashion ... and we would rather protect the bottom
521 * of the device since it contains the config data and
522 * leave the top unprotected for app use. But unfortunately
523 * protection works from top-to-bottom so it does
524 * really help very much from a software app point-of-view.
526 if (argc < 3) {
527 printf ("USAGE: asmi protect on | off\n");
528 return;
530 if (!dev)
531 return;
533 /* Protection on/off is just a matter of setting/clearing
534 * all protection bits in the status register.
536 stat = asmi_status_rd ();
537 if (strcmp ("on", argv[2]) == 0) {
538 stat |= dev->prot_mask;
539 } else if (strcmp ("off", argv[2]) == 0 ) {
540 stat &= ~dev->prot_mask;
541 } else {
542 printf ("asmi: unknown protection: %s\n", argv[2]);
543 return;
545 asmi_status_wr (stat);
546 return;
549 static
550 void do_asmi_read (struct asmi_devinfo_t *dev, int argc, char *argv[])
552 ulong addr,off,cnt;
553 ulong sz;
555 if (argc < 5) {
556 printf ("USAGE: asmi read addr offset count\n");
557 return;
560 sz = 1 << dev->size;
561 addr = simple_strtoul (argv[2], NULL, 16);
562 off = simple_strtoul (argv[3], NULL, 16);
563 cnt = simple_strtoul (argv[4], NULL, 16);
564 if (off > sz) {
565 printf ("offset is greater than device size"
566 "... aborting.\n");
567 return;
569 if ((off + cnt) > sz) {
570 printf ("request exceeds device size"
571 "... truncating.\n");
572 cnt = sz - off;
574 printf ("asmi: read %08lx <- %06lx (0x%lx bytes)\n",
575 addr, off, cnt);
576 asmi_read (addr, off, cnt);
578 return;
581 static
582 void do_asmi_write (struct asmi_devinfo_t *dev, int argc, char *argv[])
584 ulong addr,off,cnt;
585 ulong sz;
586 ulong err;
588 if (argc < 5) {
589 printf ("USAGE: asmi write addr offset count\n");
590 return;
592 if ((asmi_status_rd() & dev->prot_mask) != 0) {
593 printf ( "asmi: device protected.\n");
594 return;
597 sz = 1 << dev->size;
598 addr = simple_strtoul (argv[2], NULL, 16);
599 off = simple_strtoul (argv[3], NULL, 16);
600 cnt = simple_strtoul (argv[4], NULL, 16);
601 if (off > sz) {
602 printf ("offset is greater than device size"
603 "... aborting.\n");
604 return;
606 if ((off + cnt) > sz) {
607 printf ("request exceeds device size"
608 "... truncating.\n");
609 cnt = sz - off;
611 printf ("asmi: write %08lx -> %06lx (0x%lx bytes)\n",
612 addr, off, cnt);
613 asmi_write (addr, off, cnt);
614 if (asmi_verify (addr, off, cnt, &err) != 0)
615 printf ("asmi: write error at offset %06lx\n", err);
617 return;
620 static
621 void do_asmi_verify (struct asmi_devinfo_t *dev, int argc, char *argv[])
623 ulong addr,off,cnt;
624 ulong sz;
625 ulong err;
627 if (argc < 5) {
628 printf ("USAGE: asmi verify addr offset count\n");
629 return;
632 sz = 1 << dev->size;
633 addr = simple_strtoul (argv[2], NULL, 16);
634 off = simple_strtoul (argv[3], NULL, 16);
635 cnt = simple_strtoul (argv[4], NULL, 16);
636 if (off > sz) {
637 printf ("offset is greater than device size"
638 "... aborting.\n");
639 return;
641 if ((off + cnt) > sz) {
642 printf ("request exceeds device size"
643 "... truncating.\n");
644 cnt = sz - off;
646 printf ("asmi: verify %08lx -> %06lx (0x%lx bytes)\n",
647 addr, off, cnt);
648 if (asmi_verify (addr, off, cnt, &err) != 0)
649 printf ("asmi: verify error at offset %06lx\n", err);
651 return;
654 /*-----------------------------------------------------------------------*/
655 int do_asmi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
657 int len;
658 struct asmi_devinfo_t *dev = asmi_dev_find ();
660 if (argc < 2) {
661 printf ("Usage:%s", LONG_HELP);
662 return (0);
665 if (!dev) {
666 printf ("asmi: device not found.\n");
667 return (0);
670 len = strlen (argv[1]);
671 if (strncmp ("info", argv[1], len) == 0) {
672 do_asmi_info ( dev, argc, argv);
673 } else if (strncmp ("erase", argv[1], len) == 0) {
674 do_asmi_erase (dev, argc, argv);
675 } else if (strncmp ("protect", argv[1], len) == 0) {
676 do_asmi_protect (dev, argc, argv);
677 } else if (strncmp ("read", argv[1], len) == 0) {
678 do_asmi_read (dev, argc, argv);
679 } else if (strncmp ("write", argv[1], len) == 0) {
680 do_asmi_write (dev, argc, argv);
681 } else if (strncmp ("verify", argv[1], len) == 0) {
682 do_asmi_verify (dev, argc, argv);
683 } else {
684 printf ("asmi: unknown operation: %s\n", argv[1]);
687 return (0);
690 /*-----------------------------------------------------------------------*/
693 U_BOOT_CMD( asmi, 5, 0, do_asmi, SHORT_HELP, LONG_HELP );
695 #endif /* CONFIG_NIOS_ASMI */