]>
git.ipfire.org Git - people/ms/u-boot.git/blob - arch/nios2/cpu/epcs.c
2369431f1592ff8c7e84714432c352aa37de49df
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(CONFIG_SYS_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."
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
*)CONFIG_SYS_NIOS_EPCSBASE
;
77 /***********************************************************************
79 ***********************************************************************/
80 static int epcs_cs (int assert)
87 tmp
= readl (&epcs
->control
);
88 writel (tmp
| NIOS_SPI_SSO
, &epcs
->control
);
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 (tmp
& ~NIOS_SPI_SSO
, &epcs
->control
);
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 (c
, &epcs
->txdata
);
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 },
210 { "EPCS16", 0x14, 21, 32, 16, 8, 0x1c },
211 { "EPCS64", 0x16, 23,128, 16, 8, 0x1c },
215 int epcs_reset (void)
217 /* When booting from an epcs controller, the epcs bootrom
218 * code may leave the slave select in an asserted state.
219 * This causes two problems: (1) The initial epcs access
220 * will fail -- not a big deal, and (2) a software reset
221 * will cause the bootrom code to hang since it does not
222 * ensure the select is negated prior to first access -- a
223 * big deal. Here we just negate chip select and everything
226 epcs_cs (0); /* Negate chip select */
230 epcs_devinfo_t
*epcs_dev_find (void)
232 unsigned char buf
[4];
235 struct epcs_devinfo_t
*dev
= NULL
;
237 /* Read silicon id requires 3 "dummy bytes" before it's put
240 buf
[0] = EPCS_READ_ID
;
248 if (epcs_cs (0) == -1)
252 /* Find the info struct */
254 while (devinfo
[i
].name
) {
255 if (id
== devinfo
[i
].id
) {
265 /***********************************************************************
267 ***********************************************************************/
268 int epcs_cfgsz (void)
271 unsigned char buf
[128];
273 struct epcs_devinfo_t
*dev
= epcs_dev_find ();
278 /* Read in the first 128 bytes of the device */
279 buf
[0] = EPCS_READ_BYTES
;
286 epcs_rrcv (buf
, sizeof(buf
));
289 /* Search for the starting 0x6a which is followed by the
290 * 4-byte 'register' and 4-byte bit-count.
293 while (p
< buf
+ sizeof(buf
)-8) {
295 /* Point to bit count and extract */
301 /* Convert to byte count */
304 } else if (*p
== 0xff) {
305 /* 0xff is ok ... just skip */
309 /* Not 0xff or 0x6a ... something's not
310 * right ... report 'unknown' (sz=0).
318 int epcs_erase (unsigned start
, unsigned end
)
320 unsigned off
, sectsz
;
321 unsigned char buf
[4];
322 struct epcs_devinfo_t
*dev
= epcs_dev_find ();
324 if (!dev
|| (start
>end
))
327 /* Erase the requested sectors. An address is required
328 * that lies within the requested sector -- we'll just
329 * use the first address in the sector.
331 printf ("epcs erasing sector %d ", start
);
333 printf ("to %d ", end
);
334 sectsz
= (1 << dev
->sz_sect
);
335 while (start
<= end
) {
336 off
= start
* sectsz
;
339 buf
[0] = EPCS_ERASE_SECT
;
349 printf ("."); /* Some user feedback */
351 /* Wait for erase to complete */
352 while (epcs_status_rd() & EPCS_STATUS_WIP
)
359 int epcs_read (ulong addr
, ulong off
, ulong cnt
)
361 unsigned char buf
[4];
362 struct epcs_devinfo_t
*dev
= epcs_dev_find ();
367 buf
[0] = EPCS_READ_BYTES
;
374 epcs_rrcv ((unsigned char *)addr
, cnt
);
380 int epcs_write (ulong addr
, ulong off
, ulong cnt
)
384 unsigned char buf
[4];
385 struct epcs_devinfo_t
*dev
= epcs_dev_find ();
390 pgsz
= (1<<dev
->sz_page
);
393 wrcnt
= pgsz
- (off
% pgsz
);
396 wrcnt
= (wrcnt
> cnt
) ? cnt
: wrcnt
;
398 buf
[0] = EPCS_WRITE_BYTES
;
406 epcs_rsnd ((unsigned char *)addr
, wrcnt
);
409 /* Wait for write to complete */
410 while (epcs_status_rd() & EPCS_STATUS_WIP
)
421 int epcs_verify (ulong addr
, ulong off
, ulong cnt
, ulong
*err
)
424 unsigned char buf
[256];
425 unsigned char *start
,*end
;
428 start
= end
= (unsigned char *)addr
;
430 rdcnt
= (cnt
>sizeof(buf
)) ? sizeof(buf
) : cnt
;
431 epcs_read ((ulong
)buf
, off
, rdcnt
);
432 for (i
=0; i
<rdcnt
; i
++) {
433 if (*end
!= buf
[i
]) {
445 static int epcs_sect_erased (int sect
, unsigned *offset
,
446 struct epcs_devinfo_t
*dev
)
448 unsigned char buf
[128];
453 sectsz
= (1 << dev
->sz_sect
);
458 epcs_read ((ulong
)buf
, off
, sizeof(buf
));
459 for (i
=0; i
< sizeof(buf
); i
++) {
460 if (buf
[i
] != 0xff) {
471 /***********************************************************************
473 ***********************************************************************/
475 void do_epcs_info (struct epcs_devinfo_t
*dev
, int argc
, char * const argv
[])
482 /* Basic device info */
483 printf ("%s: %d kbytes (%d sectors x %d kbytes,"
485 dev
->name
, 1 << (dev
->size
-10),
486 dev
->num_sects
, 1 << (dev
->sz_sect
-10),
489 /* Status -- for now protection is all-or-nothing */
490 stat
= epcs_status_rd();
491 printf ("status: 0x%02x (WIP:%d, WEL:%d, PROT:%s)\n",
493 (stat
& EPCS_STATUS_WIP
) ? 1 : 0,
494 (stat
& EPCS_STATUS_WEL
) ? 1 : 0,
495 (stat
& dev
->prot_mask
) ? "on" : "off" );
500 printf ("config: 0x%06x (%d) bytes\n", tmp
, tmp
);
502 printf ("config: unknown\n" );
506 for (i
=0; (i
< dev
->num_sects
) && (argc
> 1); i
++) {
507 erased
= epcs_sect_erased (i
, &tmp
, dev
);
508 if ((i
& 0x03) == 0) printf ("\n");
509 printf ("%4d: %07x ",
510 i
, i
*(1<<dev
->sz_sect
) );
522 void do_epcs_erase (struct epcs_devinfo_t
*dev
, int argc
, char * const argv
[])
526 if ((argc
< 3) || (argc
> 4)) {
527 printf ("USAGE: epcs erase sect [end]\n");
530 if ((epcs_status_rd() & dev
->prot_mask
) != 0) {
531 printf ( "epcs: device protected.\n");
535 start
= simple_strtoul (argv
[2], NULL
, 10);
537 end
= simple_strtoul (argv
[3], NULL
, 10);
540 if ((start
>= dev
->num_sects
) || (start
> end
)) {
541 printf ("epcs: invalid sector range: [%d:%d]\n",
546 epcs_erase (start
, end
);
552 void do_epcs_protect (struct epcs_devinfo_t
*dev
, int argc
, char * const argv
[])
556 /* For now protection is all-or-nothing to keep things
557 * simple. The protection bits don't map in a linear
558 * fashion ... and we would rather protect the bottom
559 * of the device since it contains the config data and
560 * leave the top unprotected for app use. But unfortunately
561 * protection works from top-to-bottom so it does
562 * really help very much from a software app point-of-view.
565 printf ("USAGE: epcs protect on | off\n");
571 /* Protection on/off is just a matter of setting/clearing
572 * all protection bits in the status register.
574 stat
= epcs_status_rd ();
575 if (strcmp ("on", argv
[2]) == 0) {
576 stat
|= dev
->prot_mask
;
577 } else if (strcmp ("off", argv
[2]) == 0 ) {
578 stat
&= ~dev
->prot_mask
;
580 printf ("epcs: unknown protection: %s\n", argv
[2]);
583 epcs_status_wr (stat
);
588 void do_epcs_read (struct epcs_devinfo_t
*dev
, int argc
, char * const argv
[])
594 printf ("USAGE: epcs read addr offset count\n");
599 addr
= simple_strtoul (argv
[2], NULL
, 16);
600 off
= simple_strtoul (argv
[3], NULL
, 16);
601 cnt
= simple_strtoul (argv
[4], NULL
, 16);
603 printf ("offset is greater than device size"
607 if ((off
+ cnt
) > sz
) {
608 printf ("request exceeds device size"
609 "... truncating.\n");
612 printf ("epcs: read %08lx <- %06lx (0x%lx bytes)\n",
614 epcs_read (addr
, off
, cnt
);
620 void do_epcs_write (struct epcs_devinfo_t
*dev
, int argc
, char * const argv
[])
627 printf ("USAGE: epcs write addr offset count\n");
630 if ((epcs_status_rd() & dev
->prot_mask
) != 0) {
631 printf ( "epcs: device protected.\n");
636 addr
= simple_strtoul (argv
[2], NULL
, 16);
637 off
= simple_strtoul (argv
[3], NULL
, 16);
638 cnt
= simple_strtoul (argv
[4], NULL
, 16);
640 printf ("offset is greater than device size"
644 if ((off
+ cnt
) > sz
) {
645 printf ("request exceeds device size"
646 "... truncating.\n");
649 printf ("epcs: write %08lx -> %06lx (0x%lx bytes)\n",
651 epcs_write (addr
, off
, cnt
);
652 if (epcs_verify (addr
, off
, cnt
, &err
) != 0)
653 printf ("epcs: write error at offset %06lx\n", err
);
659 void do_epcs_verify (struct epcs_devinfo_t
*dev
, int argc
, char * const argv
[])
666 printf ("USAGE: epcs verify addr offset count\n");
671 addr
= simple_strtoul (argv
[2], NULL
, 16);
672 off
= simple_strtoul (argv
[3], NULL
, 16);
673 cnt
= simple_strtoul (argv
[4], NULL
, 16);
675 printf ("offset is greater than device size"
679 if ((off
+ cnt
) > sz
) {
680 printf ("request exceeds device size"
681 "... truncating.\n");
684 printf ("epcs: verify %08lx -> %06lx (0x%lx bytes)\n",
686 if (epcs_verify (addr
, off
, cnt
, &err
) != 0)
687 printf ("epcs: verify error at offset %06lx\n", err
);
692 /*-----------------------------------------------------------------------*/
693 int do_epcs (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
696 struct epcs_devinfo_t
*dev
= epcs_dev_find ();
699 printf ("epcs: device not found.\n");
704 do_epcs_info (dev
, argc
, argv
);
708 len
= strlen (argv
[1]);
709 if (strncmp ("info", argv
[1], len
) == 0) {
710 do_epcs_info (dev
, argc
, argv
);
711 } else if (strncmp ("erase", argv
[1], len
) == 0) {
712 do_epcs_erase (dev
, argc
, argv
);
713 } else if (strncmp ("protect", argv
[1], len
) == 0) {
714 do_epcs_protect (dev
, argc
, argv
);
715 } else if (strncmp ("read", argv
[1], len
) == 0) {
716 do_epcs_read (dev
, argc
, argv
);
717 } else if (strncmp ("write", argv
[1], len
) == 0) {
718 do_epcs_write (dev
, argc
, argv
);
719 } else if (strncmp ("verify", argv
[1], len
) == 0) {
720 do_epcs_verify (dev
, argc
, argv
);
722 printf ("epcs: unknown operation: %s\n", argv
[1]);
728 /*-----------------------------------------------------------------------*/
731 U_BOOT_CMD( epcs
, 5, 0, do_epcs
, SHORT_HELP
, LONG_HELP
);
733 #endif /* CONFIG_NIOS_EPCS */