2 * (C) Copyright 2004, 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(CFG_NIOS_EPCSBASE)
30 #include <nios2-epcs.h>
33 /*-----------------------------------------------------------------------*/
35 "epcs - read/write Cyclone EPCS configuration device.\n"
39 "epcs erase start [end]\n"\
40 " - erase sector start or sectors start through end.\n"\
42 " - display EPCS device information.\n"\
43 "epcs protect on | off\n"\
44 " - turn device protection on or off.\n"\
45 "epcs read addr offset count\n"\
46 " - read count bytes from offset to addr.\n"\
47 "epcs write addr offset count\n"\
48 " - write count bytes to offset from addr.\n"\
49 "epcs verify addr offset count\n"\
50 " - verify count bytes at offset from addr.\n"
53 /*-----------------------------------------------------------------------*/
54 /* Operation codes for serial configuration devices
56 #define EPCS_WRITE_ENA 0x06 /* Write enable */
57 #define EPCS_WRITE_DIS 0x04 /* Write disable */
58 #define EPCS_READ_STAT 0x05 /* Read status */
59 #define EPCS_READ_BYTES 0x03 /* Read bytes */
60 #define EPCS_READ_ID 0xab /* Read silicon id */
61 #define EPCS_WRITE_STAT 0x01 /* Write status */
62 #define EPCS_WRITE_BYTES 0x02 /* Write bytes */
63 #define EPCS_ERASE_BULK 0xc7 /* Erase entire device */
64 #define EPCS_ERASE_SECT 0xd8 /* Erase sector */
66 /* Device status register bits
68 #define EPCS_STATUS_WIP (1<<0) /* Write in progress */
69 #define EPCS_STATUS_WEL (1<<1) /* Write enable latch */
73 #define EPCS_TIMEOUT 100 /* 100 msec timeout */
75 static nios_spi_t *epcs = (nios_spi_t *)CFG_NIOS_EPCSBASE;
77 /***********************************************************************
79 ***********************************************************************/
80 static int epcs_cs (int assert)
87 tmp = readl (&epcs->control);
88 writel (&epcs->control, tmp | NIOS_SPI_SSO);
90 /* Let all bits shift out */
91 start = get_timer (0);
92 while ((readl (&epcs->status) & NIOS_SPI_TMT) == 0)
93 if (get_timer (start) > EPCS_TIMEOUT)
95 tmp = readl (&epcs->control);
96 writel (&epcs->control, tmp & ~NIOS_SPI_SSO);
101 static int epcs_tx (unsigned char c)
105 start = get_timer (0);
106 while ((readl (&epcs->status) & NIOS_SPI_TRDY) == 0)
107 if (get_timer (start) > EPCS_TIMEOUT)
109 writel (&epcs->txdata, c);
113 static int epcs_rx (void)
117 start = get_timer (0);
118 while ((readl (&epcs->status) & NIOS_SPI_RRDY) == 0)
119 if (get_timer (start) > EPCS_TIMEOUT)
121 return (readl (&epcs->rxdata));
124 static unsigned char bitrev[] = {
125 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
126 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
129 static unsigned char epcs_bitrev (unsigned char c)
134 val |= bitrev[c & 0x0f]<<4;
138 static void epcs_rcv (unsigned char *dst, int len)
146 static void epcs_rrcv (unsigned char *dst, int len)
150 *dst++ = epcs_bitrev (epcs_rx ());
154 static void epcs_snd (unsigned char *src, int len)
162 static void epcs_rsnd (unsigned char *src, int len)
165 epcs_tx (epcs_bitrev (*src++));
170 static void epcs_wr_enable (void)
173 epcs_tx (EPCS_WRITE_ENA);
178 static unsigned char epcs_status_rd (void)
180 unsigned char status;
183 epcs_tx (EPCS_READ_STAT);
191 static void epcs_status_wr (unsigned char status)
195 epcs_tx (EPCS_WRITE_STAT);
203 /***********************************************************************
205 ***********************************************************************/
207 static struct epcs_devinfo_t devinfo[] = {
208 { "EPCS1 ", 0x10, 17, 4, 15, 8, 0x0c },
209 { "EPCS4 ", 0x12, 19, 8, 16, 8, 0x1c },
213 int epcs_reset (void)
215 /* When booting from an epcs controller, the epcs bootrom
216 * code may leave the slave select in an asserted state.
217 * This causes two problems: (1) The initial epcs access
218 * will fail -- not a big deal, and (2) a software reset
219 * will cause the bootrom code to hang since it does not
220 * ensure the select is negated prior to first access -- a
221 * big deal. Here we just negate chip select and everything
224 epcs_cs (0); /* Negate chip select */
228 epcs_devinfo_t *epcs_dev_find (void)
230 unsigned char buf[4];
233 struct epcs_devinfo_t *dev = NULL;
235 /* Read silicon id requires 3 "dummy bytes" before it's put
238 buf[0] = EPCS_READ_ID;
246 if (epcs_cs (0) == -1)
250 /* Find the info struct */
252 while (devinfo[i].name) {
253 if (id == devinfo[i].id) {
263 /***********************************************************************
265 ***********************************************************************/
266 int epcs_cfgsz (void)
269 unsigned char buf[128];
271 struct epcs_devinfo_t *dev = epcs_dev_find ();
276 /* Read in the first 128 bytes of the device */
277 buf[0] = EPCS_READ_BYTES;
284 epcs_rrcv (buf, sizeof(buf));
287 /* Search for the starting 0x6a which is followed by the
288 * 4-byte 'register' and 4-byte bit-count.
291 while (p < buf + sizeof(buf)-8) {
293 /* Point to bit count and extract */
299 /* Convert to byte count */
302 } else if (*p == 0xff) {
303 /* 0xff is ok ... just skip */
307 /* Not 0xff or 0x6a ... something's not
308 * right ... report 'unknown' (sz=0).
316 int epcs_erase (unsigned start, unsigned end)
318 unsigned off, sectsz;
319 unsigned char buf[4];
320 struct epcs_devinfo_t *dev = epcs_dev_find ();
322 if (!dev || (start>end))
325 /* Erase the requested sectors. An address is required
326 * that lies within the requested sector -- we'll just
327 * use the first address in the sector.
329 printf ("epcs erasing sector %d ", start);
331 printf ("to %d ", end);
332 sectsz = (1 << dev->sz_sect);
333 while (start <= end) {
334 off = start * sectsz;
337 buf[0] = EPCS_ERASE_SECT;
347 printf ("."); /* Some user feedback */
349 /* Wait for erase to complete */
350 while (epcs_status_rd() & EPCS_STATUS_WIP)
357 int epcs_read (ulong addr, ulong off, ulong cnt)
359 unsigned char buf[4];
360 struct epcs_devinfo_t *dev = epcs_dev_find ();
365 buf[0] = EPCS_READ_BYTES;
372 epcs_rrcv ((unsigned char *)addr, cnt);
378 int epcs_write (ulong addr, ulong off, ulong cnt)
382 unsigned char buf[4];
383 struct epcs_devinfo_t *dev = epcs_dev_find ();
388 pgsz = (1<<dev->sz_page);
391 wrcnt = pgsz - (off % pgsz);
394 wrcnt = (wrcnt > cnt) ? cnt : wrcnt;
396 buf[0] = EPCS_WRITE_BYTES;
404 epcs_rsnd ((unsigned char *)addr, wrcnt);
407 /* Wait for write to complete */
408 while (epcs_status_rd() & EPCS_STATUS_WIP)
419 int epcs_verify (ulong addr, ulong off, ulong cnt, ulong *err)
422 unsigned char buf[256];
423 unsigned char *start,*end;
426 start = end = (unsigned char *)addr;
428 rdcnt = (cnt>sizeof(buf)) ? sizeof(buf) : cnt;
429 epcs_read ((ulong)buf, off, rdcnt);
430 for (i=0; i<rdcnt; i++) {
431 if (*end != buf[i]) {
443 static int epcs_sect_erased (int sect, unsigned *offset,
444 struct epcs_devinfo_t *dev)
446 unsigned char buf[128];
451 sectsz = (1 << dev->sz_sect);
456 epcs_read ((ulong)buf, off, sizeof(buf));
457 for (i=0; i < sizeof(buf); i++) {
458 if (buf[i] != 0xff) {
469 /***********************************************************************
471 ***********************************************************************/
473 void do_epcs_info (struct epcs_devinfo_t *dev, int argc, char *argv[])
480 /* Basic device info */
481 printf ("%s: %d kbytes (%d sectors x %d kbytes,"
483 dev->name, 1 << (dev->size-10),
484 dev->num_sects, 1 << (dev->sz_sect-10),
487 /* Status -- for now protection is all-or-nothing */
488 stat = epcs_status_rd();
489 printf ("status: 0x%02x (WIP:%d, WEL:%d, PROT:%s)\n",
491 (stat & EPCS_STATUS_WIP) ? 1 : 0,
492 (stat & EPCS_STATUS_WEL) ? 1 : 0,
493 (stat & dev->prot_mask) ? "on" : "off" );
498 printf ("config: 0x%06x (%d) bytes\n", tmp, tmp );
500 printf ("config: unknown\n" );
504 for (i=0; i<dev->num_sects; i++) {
505 erased = epcs_sect_erased (i, &tmp, dev);
506 printf (" %d: %06x ",
507 i, i*(1<<dev->sz_sect) );
511 printf ("data @ 0x%06x\n", tmp);
518 void do_epcs_erase (struct epcs_devinfo_t *dev, int argc, char *argv[])
522 if ((argc < 3) || (argc > 4)) {
523 printf ("USAGE: epcs erase sect [end]\n");
526 if ((epcs_status_rd() & dev->prot_mask) != 0) {
527 printf ( "epcs: device protected.\n");
531 start = simple_strtoul (argv[2], NULL, 10);
533 end = simple_strtoul (argv[3], NULL, 10);
536 if ((start >= dev->num_sects) || (start > end)) {
537 printf ("epcs: invalid sector range: [%d:%d]\n",
542 epcs_erase (start, end);
548 void do_epcs_protect (struct epcs_devinfo_t *dev, int argc, char *argv[])
552 /* For now protection is all-or-nothing to keep things
553 * simple. The protection bits don't map in a linear
554 * fashion ... and we would rather protect the bottom
555 * of the device since it contains the config data and
556 * leave the top unprotected for app use. But unfortunately
557 * protection works from top-to-bottom so it does
558 * really help very much from a software app point-of-view.
561 printf ("USAGE: epcs protect on | off\n");
567 /* Protection on/off is just a matter of setting/clearing
568 * all protection bits in the status register.
570 stat = epcs_status_rd ();
571 if (strcmp ("on", argv[2]) == 0) {
572 stat |= dev->prot_mask;
573 } else if (strcmp ("off", argv[2]) == 0 ) {
574 stat &= ~dev->prot_mask;
576 printf ("epcs: unknown protection: %s\n", argv[2]);
579 epcs_status_wr (stat);
584 void do_epcs_read (struct epcs_devinfo_t *dev, int argc, char *argv[])
590 printf ("USAGE: epcs read addr offset count\n");
595 addr = simple_strtoul (argv[2], NULL, 16);
596 off = simple_strtoul (argv[3], NULL, 16);
597 cnt = simple_strtoul (argv[4], NULL, 16);
599 printf ("offset is greater than device size"
603 if ((off + cnt) > sz) {
604 printf ("request exceeds device size"
605 "... truncating.\n");
608 printf ("epcs: read %08lx <- %06lx (0x%lx bytes)\n",
610 epcs_read (addr, off, cnt);
616 void do_epcs_write (struct epcs_devinfo_t *dev, int argc, char *argv[])
623 printf ("USAGE: epcs write addr offset count\n");
626 if ((epcs_status_rd() & dev->prot_mask) != 0) {
627 printf ( "epcs: device protected.\n");
632 addr = simple_strtoul (argv[2], NULL, 16);
633 off = simple_strtoul (argv[3], NULL, 16);
634 cnt = simple_strtoul (argv[4], NULL, 16);
636 printf ("offset is greater than device size"
640 if ((off + cnt) > sz) {
641 printf ("request exceeds device size"
642 "... truncating.\n");
645 printf ("epcs: write %08lx -> %06lx (0x%lx bytes)\n",
647 epcs_write (addr, off, cnt);
648 if (epcs_verify (addr, off, cnt, &err) != 0)
649 printf ("epcs: write error at offset %06lx\n", err);
655 void do_epcs_verify (struct epcs_devinfo_t *dev, int argc, char *argv[])
662 printf ("USAGE: epcs verify addr offset count\n");
667 addr = simple_strtoul (argv[2], NULL, 16);
668 off = simple_strtoul (argv[3], NULL, 16);
669 cnt = simple_strtoul (argv[4], NULL, 16);
671 printf ("offset is greater than device size"
675 if ((off + cnt) > sz) {
676 printf ("request exceeds device size"
677 "... truncating.\n");
680 printf ("epcs: verify %08lx -> %06lx (0x%lx bytes)\n",
682 if (epcs_verify (addr, off, cnt, &err) != 0)
683 printf ("epcs: verify error at offset %06lx\n", err);
688 /*-----------------------------------------------------------------------*/
689 int do_epcs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
692 struct epcs_devinfo_t *dev = epcs_dev_find ();
695 printf ("epcs: device not found.\n");
700 do_epcs_info (dev, argc, argv);
704 len = strlen (argv[1]);
705 if (strncmp ("info", argv[1], len) == 0) {
706 do_epcs_info (dev, argc, argv);
707 } else if (strncmp ("erase", argv[1], len) == 0) {
708 do_epcs_erase (dev, argc, argv);
709 } else if (strncmp ("protect", argv[1], len) == 0) {
710 do_epcs_protect (dev, argc, argv);
711 } else if (strncmp ("read", argv[1], len) == 0) {
712 do_epcs_read (dev, argc, argv);
713 } else if (strncmp ("write", argv[1], len) == 0) {
714 do_epcs_write (dev, argc, argv);
715 } else if (strncmp ("verify", argv[1], len) == 0) {
716 do_epcs_verify (dev, argc, argv);
718 printf ("epcs: unknown operation: %s\n", argv[1]);
724 /*-----------------------------------------------------------------------*/
727 U_BOOT_CMD( epcs, 5, 0, do_epcs, SHORT_HELP, LONG_HELP );
729 #endif /* CONFIG_NIOS_EPCS */