2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
5 * SPDX-License-Identifier: GPL-2.0+
10 #if defined(CONFIG_SYS_NIOS_EPCSBASE)
14 #include <nios2-epcs.h>
17 /*-----------------------------------------------------------------------*/
19 "epcs - read/write Cyclone EPCS configuration device.\n"
23 "epcs erase start [end]\n"\
24 " - erase sector start or sectors start through end.\n"\
26 " - display EPCS device information.\n"\
27 "epcs protect on | off\n"\
28 " - turn device protection on or off.\n"\
29 "epcs read addr offset count\n"\
30 " - read count bytes from offset to addr.\n"\
31 "epcs write addr offset count\n"\
32 " - write count bytes to offset from addr.\n"\
33 "epcs verify addr offset count\n"\
34 " - verify count bytes at offset from addr."
37 /*-----------------------------------------------------------------------*/
38 /* Operation codes for serial configuration devices
40 #define EPCS_WRITE_ENA 0x06 /* Write enable */
41 #define EPCS_WRITE_DIS 0x04 /* Write disable */
42 #define EPCS_READ_STAT 0x05 /* Read status */
43 #define EPCS_READ_BYTES 0x03 /* Read bytes */
44 #define EPCS_READ_ID 0xab /* Read silicon id */
45 #define EPCS_WRITE_STAT 0x01 /* Write status */
46 #define EPCS_WRITE_BYTES 0x02 /* Write bytes */
47 #define EPCS_ERASE_BULK 0xc7 /* Erase entire device */
48 #define EPCS_ERASE_SECT 0xd8 /* Erase sector */
50 /* Device status register bits
52 #define EPCS_STATUS_WIP (1<<0) /* Write in progress */
53 #define EPCS_STATUS_WEL (1<<1) /* Write enable latch */
57 #define EPCS_TIMEOUT 100 /* 100 msec timeout */
59 static nios_spi_t *epcs = (nios_spi_t *)CONFIG_SYS_NIOS_EPCSBASE;
61 /***********************************************************************
63 ***********************************************************************/
64 static int epcs_cs (int assert)
71 tmp = readl (&epcs->control);
72 writel (tmp | NIOS_SPI_SSO, &epcs->control);
74 /* Let all bits shift out */
75 start = get_timer (0);
76 while ((readl (&epcs->status) & NIOS_SPI_TMT) == 0)
77 if (get_timer (start) > EPCS_TIMEOUT)
79 tmp = readl (&epcs->control);
80 writel (tmp & ~NIOS_SPI_SSO, &epcs->control);
85 static int epcs_tx (unsigned char c)
89 start = get_timer (0);
90 while ((readl (&epcs->status) & NIOS_SPI_TRDY) == 0)
91 if (get_timer (start) > EPCS_TIMEOUT)
93 writel (c, &epcs->txdata);
97 static int epcs_rx (void)
101 start = get_timer (0);
102 while ((readl (&epcs->status) & NIOS_SPI_RRDY) == 0)
103 if (get_timer (start) > EPCS_TIMEOUT)
105 return (readl (&epcs->rxdata));
108 static unsigned char bitrev[] = {
109 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
110 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
113 static unsigned char epcs_bitrev (unsigned char c)
118 val |= bitrev[c & 0x0f]<<4;
122 static void epcs_rcv (unsigned char *dst, int len)
130 static void epcs_rrcv (unsigned char *dst, int len)
134 *dst++ = epcs_bitrev (epcs_rx ());
138 static void epcs_snd (unsigned char *src, int len)
146 static void epcs_rsnd (unsigned char *src, int len)
149 epcs_tx (epcs_bitrev (*src++));
154 static void epcs_wr_enable (void)
157 epcs_tx (EPCS_WRITE_ENA);
162 static unsigned char epcs_status_rd (void)
164 unsigned char status;
167 epcs_tx (EPCS_READ_STAT);
175 static void epcs_status_wr (unsigned char status)
179 epcs_tx (EPCS_WRITE_STAT);
187 /***********************************************************************
189 ***********************************************************************/
191 static struct epcs_devinfo_t devinfo[] = {
192 { "EPCS1 ", 0x10, 17, 4, 15, 8, 0x0c },
193 { "EPCS4 ", 0x12, 19, 8, 16, 8, 0x1c },
194 { "EPCS16", 0x14, 21, 32, 16, 8, 0x1c },
195 { "EPCS64", 0x16, 23,128, 16, 8, 0x1c },
199 int epcs_reset (void)
201 /* When booting from an epcs controller, the epcs bootrom
202 * code may leave the slave select in an asserted state.
203 * This causes two problems: (1) The initial epcs access
204 * will fail -- not a big deal, and (2) a software reset
205 * will cause the bootrom code to hang since it does not
206 * ensure the select is negated prior to first access -- a
207 * big deal. Here we just negate chip select and everything
210 epcs_cs (0); /* Negate chip select */
214 epcs_devinfo_t *epcs_dev_find (void)
216 unsigned char buf[4];
219 struct epcs_devinfo_t *dev = NULL;
221 /* Read silicon id requires 3 "dummy bytes" before it's put
224 buf[0] = EPCS_READ_ID;
232 if (epcs_cs (0) == -1)
236 /* Find the info struct */
238 while (devinfo[i].name) {
239 if (id == devinfo[i].id) {
249 /***********************************************************************
251 ***********************************************************************/
252 int epcs_cfgsz (void)
255 unsigned char buf[128];
257 struct epcs_devinfo_t *dev = epcs_dev_find ();
262 /* Read in the first 128 bytes of the device */
263 buf[0] = EPCS_READ_BYTES;
270 epcs_rrcv (buf, sizeof(buf));
273 /* Search for the starting 0x6a which is followed by the
274 * 4-byte 'register' and 4-byte bit-count.
277 while (p < buf + sizeof(buf)-8) {
279 /* Point to bit count and extract */
285 /* Convert to byte count */
288 } else if (*p == 0xff) {
289 /* 0xff is ok ... just skip */
293 /* Not 0xff or 0x6a ... something's not
294 * right ... report 'unknown' (sz=0).
302 int epcs_erase (unsigned start, unsigned end)
304 unsigned off, sectsz;
305 unsigned char buf[4];
306 struct epcs_devinfo_t *dev = epcs_dev_find ();
308 if (!dev || (start>end))
311 /* Erase the requested sectors. An address is required
312 * that lies within the requested sector -- we'll just
313 * use the first address in the sector.
315 printf ("epcs erasing sector %d ", start);
317 printf ("to %d ", end);
318 sectsz = (1 << dev->sz_sect);
319 while (start <= end) {
320 off = start * sectsz;
323 buf[0] = EPCS_ERASE_SECT;
333 printf ("."); /* Some user feedback */
335 /* Wait for erase to complete */
336 while (epcs_status_rd() & EPCS_STATUS_WIP)
343 int epcs_read (ulong addr, ulong off, ulong cnt)
345 unsigned char buf[4];
346 struct epcs_devinfo_t *dev = epcs_dev_find ();
351 buf[0] = EPCS_READ_BYTES;
358 epcs_rrcv ((unsigned char *)addr, cnt);
364 int epcs_write (ulong addr, ulong off, ulong cnt)
368 unsigned char buf[4];
369 struct epcs_devinfo_t *dev = epcs_dev_find ();
374 pgsz = (1<<dev->sz_page);
377 wrcnt = pgsz - (off % pgsz);
380 wrcnt = (wrcnt > cnt) ? cnt : wrcnt;
382 buf[0] = EPCS_WRITE_BYTES;
390 epcs_rsnd ((unsigned char *)addr, wrcnt);
393 /* Wait for write to complete */
394 while (epcs_status_rd() & EPCS_STATUS_WIP)
405 int epcs_verify (ulong addr, ulong off, ulong cnt, ulong *err)
408 unsigned char buf[256];
409 unsigned char *start,*end;
412 start = end = (unsigned char *)addr;
414 rdcnt = (cnt>sizeof(buf)) ? sizeof(buf) : cnt;
415 epcs_read ((ulong)buf, off, rdcnt);
416 for (i=0; i<rdcnt; i++) {
417 if (*end != buf[i]) {
429 static int epcs_sect_erased (int sect, unsigned *offset,
430 struct epcs_devinfo_t *dev)
432 unsigned char buf[128];
437 sectsz = (1 << dev->sz_sect);
442 epcs_read ((ulong)buf, off, sizeof(buf));
443 for (i=0; i < sizeof(buf); i++) {
444 if (buf[i] != 0xff) {
455 /***********************************************************************
457 ***********************************************************************/
459 void do_epcs_info (struct epcs_devinfo_t *dev, int argc, char * const argv[])
466 /* Basic device info */
467 printf ("%s: %d kbytes (%d sectors x %d kbytes,"
469 dev->name, 1 << (dev->size-10),
470 dev->num_sects, 1 << (dev->sz_sect-10),
473 /* Status -- for now protection is all-or-nothing */
474 stat = epcs_status_rd();
475 printf ("status: 0x%02x (WIP:%d, WEL:%d, PROT:%s)\n",
477 (stat & EPCS_STATUS_WIP) ? 1 : 0,
478 (stat & EPCS_STATUS_WEL) ? 1 : 0,
479 (stat & dev->prot_mask) ? "on" : "off" );
484 printf ("config: 0x%06x (%d) bytes\n", tmp, tmp );
486 printf ("config: unknown\n" );
490 for (i=0; (i < dev->num_sects) && (argc > 1); i++) {
491 erased = epcs_sect_erased (i, &tmp, dev);
492 if ((i & 0x03) == 0) printf ("\n");
493 printf ("%4d: %07x ",
494 i, i*(1<<dev->sz_sect) );
506 void do_epcs_erase (struct epcs_devinfo_t *dev, int argc, char * const argv[])
510 if ((argc < 3) || (argc > 4)) {
511 printf ("USAGE: epcs erase sect [end]\n");
514 if ((epcs_status_rd() & dev->prot_mask) != 0) {
515 printf ( "epcs: device protected.\n");
519 start = simple_strtoul (argv[2], NULL, 10);
521 end = simple_strtoul (argv[3], NULL, 10);
524 if ((start >= dev->num_sects) || (start > end)) {
525 printf ("epcs: invalid sector range: [%d:%d]\n",
530 epcs_erase (start, end);
536 void do_epcs_protect (struct epcs_devinfo_t *dev, int argc, char * const argv[])
540 /* For now protection is all-or-nothing to keep things
541 * simple. The protection bits don't map in a linear
542 * fashion ... and we would rather protect the bottom
543 * of the device since it contains the config data and
544 * leave the top unprotected for app use. But unfortunately
545 * protection works from top-to-bottom so it does
546 * really help very much from a software app point-of-view.
549 printf ("USAGE: epcs protect on | off\n");
555 /* Protection on/off is just a matter of setting/clearing
556 * all protection bits in the status register.
558 stat = epcs_status_rd ();
559 if (strcmp ("on", argv[2]) == 0) {
560 stat |= dev->prot_mask;
561 } else if (strcmp ("off", argv[2]) == 0 ) {
562 stat &= ~dev->prot_mask;
564 printf ("epcs: unknown protection: %s\n", argv[2]);
567 epcs_status_wr (stat);
572 void do_epcs_read (struct epcs_devinfo_t *dev, int argc, char * const argv[])
578 printf ("USAGE: epcs read addr offset count\n");
583 addr = simple_strtoul (argv[2], NULL, 16);
584 off = simple_strtoul (argv[3], NULL, 16);
585 cnt = simple_strtoul (argv[4], NULL, 16);
587 printf ("offset is greater than device size"
591 if ((off + cnt) > sz) {
592 printf ("request exceeds device size"
593 "... truncating.\n");
596 printf ("epcs: read %08lx <- %06lx (0x%lx bytes)\n",
598 epcs_read (addr, off, cnt);
604 void do_epcs_write (struct epcs_devinfo_t *dev, int argc, char * const argv[])
611 printf ("USAGE: epcs write addr offset count\n");
614 if ((epcs_status_rd() & dev->prot_mask) != 0) {
615 printf ( "epcs: device protected.\n");
620 addr = simple_strtoul (argv[2], NULL, 16);
621 off = simple_strtoul (argv[3], NULL, 16);
622 cnt = simple_strtoul (argv[4], NULL, 16);
624 printf ("offset is greater than device size"
628 if ((off + cnt) > sz) {
629 printf ("request exceeds device size"
630 "... truncating.\n");
633 printf ("epcs: write %08lx -> %06lx (0x%lx bytes)\n",
635 epcs_write (addr, off, cnt);
636 if (epcs_verify (addr, off, cnt, &err) != 0)
637 printf ("epcs: write error at offset %06lx\n", err);
643 void do_epcs_verify (struct epcs_devinfo_t *dev, int argc, char * const argv[])
650 printf ("USAGE: epcs verify addr offset count\n");
655 addr = simple_strtoul (argv[2], NULL, 16);
656 off = simple_strtoul (argv[3], NULL, 16);
657 cnt = simple_strtoul (argv[4], NULL, 16);
659 printf ("offset is greater than device size"
663 if ((off + cnt) > sz) {
664 printf ("request exceeds device size"
665 "... truncating.\n");
668 printf ("epcs: verify %08lx -> %06lx (0x%lx bytes)\n",
670 if (epcs_verify (addr, off, cnt, &err) != 0)
671 printf ("epcs: verify error at offset %06lx\n", err);
676 /*-----------------------------------------------------------------------*/
677 int do_epcs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
680 struct epcs_devinfo_t *dev = epcs_dev_find ();
683 printf ("epcs: device not found.\n");
688 do_epcs_info (dev, argc, argv);
692 len = strlen (argv[1]);
693 if (strncmp ("info", argv[1], len) == 0) {
694 do_epcs_info (dev, argc, argv);
695 } else if (strncmp ("erase", argv[1], len) == 0) {
696 do_epcs_erase (dev, argc, argv);
697 } else if (strncmp ("protect", argv[1], len) == 0) {
698 do_epcs_protect (dev, argc, argv);
699 } else if (strncmp ("read", argv[1], len) == 0) {
700 do_epcs_read (dev, argc, argv);
701 } else if (strncmp ("write", argv[1], len) == 0) {
702 do_epcs_write (dev, argc, argv);
703 } else if (strncmp ("verify", argv[1], len) == 0) {
704 do_epcs_verify (dev, argc, argv);
706 printf ("epcs: unknown operation: %s\n", argv[1]);
712 /*-----------------------------------------------------------------------*/
715 U_BOOT_CMD( epcs, 5, 0, do_epcs, SHORT_HELP, LONG_HELP );
717 #endif /* CONFIG_NIOS_EPCS */