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
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,
26 #if defined(CONFIG_NIOS_ASMI)
30 #if !defined(CFG_NIOS_ASMIBASE)
31 #error "*** CFG_NIOS_ASMIBASE not defined ***"
34 /*-----------------------------------------------------------------------*/
36 "asmi - read/write Cyclone ASMI configuration device.\n"
40 "asmi erase start [end]\n"\
41 " - erase sector start or sectors start through end.\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 /***********************************************************************
76 ***********************************************************************/
77 static void asmi_cs (int assert)
80 asmi->control |= NIOS_ASMI_SSO;
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)
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 )
113 val |= bitrev[c & 0x0f]<<4;
117 static void asmi_rcv (unsigned char *dst, int len)
125 static void asmi_rrcv (unsigned char *dst, int len)
129 *dst++ = asmi_bitrev (asmi_rx ());
133 static void asmi_snd (unsigned char *src, int len)
141 static void asmi_rsnd (unsigned char *src, int len)
144 asmi_tx (asmi_bitrev (*src++));
149 static void asmi_wr_enable (void)
152 asmi_tx (ASMI_WRITE_ENA);
157 static unsigned char asmi_status_rd (void)
159 unsigned char status;
162 asmi_tx (ASMI_READ_STAT);
170 static void asmi_status_wr (unsigned char status)
174 asmi_tx (ASMI_WRITE_STAT);
182 /***********************************************************************
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 */
195 static struct asmi_devinfo_t devinfo[] = {
196 { "EPCS1 ", 0x10, 17, 4, 15, 8, 0x0c },
197 { "EPCS4 ", 0x12, 19, 8, 16, 8, 0x1c },
201 static asmi_devinfo_t *asmi_dev_find (void)
203 unsigned char buf[4];
206 struct asmi_devinfo_t *dev = NULL;
208 /* Read silicon id requires 3 "dummy bytes" before it's put
211 buf[0] = ASMI_READ_ID;
222 /* Find the info struct */
224 while (devinfo[i].name) {
225 if (id == devinfo[i].id) {
235 /***********************************************************************
237 ***********************************************************************/
238 static unsigned asmi_cfgsz (void)
241 unsigned char buf[128];
244 /* Read in the first 128 bytes of the device */
245 buf[0] = ASMI_READ_BYTES;
252 asmi_rrcv (buf, sizeof(buf));
255 /* Search for the starting 0x6a which is followed by the
256 * 4-byte 'register' and 4-byte bit-count.
259 while (p < buf + sizeof(buf)-8) {
261 /* Point to bit count and extract */
267 /* Convert to byte count */
270 } else if (*p == 0xff) {
271 /* 0xff is ok ... just skip */
275 /* Not 0xff or 0x6a ... something's not
276 * right ... report 'unknown' (sz=0).
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))
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);
299 printf ("to %d ", end);
300 sectsz = (1 << dev->sz_sect);
301 while (start <= end) {
302 off = start * sectsz;
305 buf[0] = ASMI_ERASE_SECT;
315 printf ("."); /* Some user feedback */
317 /* Wait for erase to complete */
318 while (asmi_status_rd() & ASMI_STATUS_WIP)
325 static int asmi_read (ulong addr, ulong off, ulong cnt)
327 unsigned char buf[4];
329 buf[0] = ASMI_READ_BYTES;
336 asmi_rrcv ((unsigned char *)addr, cnt);
343 int asmi_write (ulong addr, ulong off, ulong cnt)
347 unsigned char buf[4];
348 struct asmi_devinfo_t *dev = asmi_dev_find ();
353 pgsz = (1<<dev->sz_page);
356 wrcnt = pgsz - (off % pgsz);
359 wrcnt = (wrcnt > cnt) ? cnt : wrcnt;
361 buf[0] = ASMI_WRITE_BYTES;
369 asmi_rsnd ((unsigned char *)addr, wrcnt);
372 /* Wait for write to complete */
373 while (asmi_status_rd() & ASMI_STATUS_WIP)
385 int asmi_verify (ulong addr, ulong off, ulong cnt, ulong *err)
388 unsigned char buf[256];
389 unsigned char *start,*end;
392 start = end = (unsigned char *)addr;
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]) {
409 static int asmi_sect_erased (int sect, unsigned *offset,
410 struct asmi_devinfo_t *dev)
412 unsigned char buf[128];
417 sectsz = (1 << dev->sz_sect);
422 asmi_read ((ulong)buf, off, sizeof(buf));
423 for (i=0; i < sizeof(buf); i++) {
424 if (buf[i] != 0xff) {
435 /***********************************************************************
437 ***********************************************************************/
439 void do_asmi_info (struct asmi_devinfo_t *dev, int argc, char *argv[])
446 /* Basic device info */
447 printf ("%s: %d kbytes (%d sectors x %d kbytes,"
449 dev->name, 1 << (dev->size-10),
450 dev->num_sects, 1 << (dev->sz_sect-10),
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",
457 (stat & ASMI_STATUS_WIP) ? 1 : 0,
458 (stat & ASMI_STATUS_WEL) ? 1 : 0,
459 (stat & dev->prot_mask) ? "on" : "off" );
464 printf ("config: 0x%06x (%d) bytes\n", tmp, tmp );
466 printf ("config: unknown\n" );
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) );
477 printf ("data @ 0x%06x\n", tmp);
484 void do_asmi_erase (struct asmi_devinfo_t *dev, int argc, char *argv[])
488 if ((argc < 3) || (argc > 4)) {
489 printf ("USAGE: asmi erase sect [end]\n");
492 if ((asmi_status_rd() & dev->prot_mask) != 0) {
493 printf ( "asmi: device protected.\n");
497 start = simple_strtoul (argv[2], NULL, 10);
499 end = simple_strtoul (argv[3], NULL, 10);
502 if ((start >= dev->num_sects) || (start > end)) {
503 printf ("asmi: invalid sector range: [%d:%d]\n",
508 asmi_erase (start, end);
514 void do_asmi_protect (struct asmi_devinfo_t *dev, int argc, char *argv[])
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.
527 printf ("USAGE: asmi protect on | off\n");
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;
542 printf ("asmi: unknown protection: %s\n", argv[2]);
545 asmi_status_wr (stat);
550 void do_asmi_read (struct asmi_devinfo_t *dev, int argc, char *argv[])
556 printf ("USAGE: asmi read addr offset count\n");
561 addr = simple_strtoul (argv[2], NULL, 16);
562 off = simple_strtoul (argv[3], NULL, 16);
563 cnt = simple_strtoul (argv[4], NULL, 16);
565 printf ("offset is greater than device size"
569 if ((off + cnt) > sz) {
570 printf ("request exceeds device size"
571 "... truncating.\n");
574 printf ("asmi: read %08lx <- %06lx (0x%lx bytes)\n",
576 asmi_read (addr, off, cnt);
582 void do_asmi_write (struct asmi_devinfo_t *dev, int argc, char *argv[])
589 printf ("USAGE: asmi write addr offset count\n");
592 if ((asmi_status_rd() & dev->prot_mask) != 0) {
593 printf ( "asmi: device protected.\n");
598 addr = simple_strtoul (argv[2], NULL, 16);
599 off = simple_strtoul (argv[3], NULL, 16);
600 cnt = simple_strtoul (argv[4], NULL, 16);
602 printf ("offset is greater than device size"
606 if ((off + cnt) > sz) {
607 printf ("request exceeds device size"
608 "... truncating.\n");
611 printf ("asmi: write %08lx -> %06lx (0x%lx bytes)\n",
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);
621 void do_asmi_verify (struct asmi_devinfo_t *dev, int argc, char *argv[])
628 printf ("USAGE: asmi verify addr offset count\n");
633 addr = simple_strtoul (argv[2], NULL, 16);
634 off = simple_strtoul (argv[3], NULL, 16);
635 cnt = simple_strtoul (argv[4], NULL, 16);
637 printf ("offset is greater than device size"
641 if ((off + cnt) > sz) {
642 printf ("request exceeds device size"
643 "... truncating.\n");
646 printf ("asmi: verify %08lx -> %06lx (0x%lx bytes)\n",
648 if (asmi_verify (addr, off, cnt, &err) != 0)
649 printf ("asmi: verify error at offset %06lx\n", err);
654 /*-----------------------------------------------------------------------*/
655 int do_asmi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
658 struct asmi_devinfo_t *dev = asmi_dev_find ();
661 printf ("Usage:%s", LONG_HELP);
666 printf ("asmi: device not found.\n");
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);
684 printf ("asmi: unknown operation: %s\n", argv[1]);
690 /*-----------------------------------------------------------------------*/
693 U_BOOT_CMD( asmi, 5, 0, do_asmi, SHORT_HELP, LONG_HELP );
695 #endif /* CONFIG_NIOS_ASMI */