]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - cpu/nios2/epcs.c
* Patch by Scott McNutt, 21 Oct 2004:
[karo-tx-uboot.git] / cpu / nios2 / epcs.c
1 /*
2  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3  * Scott McNutt <smcnutt@psyent.com>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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
22  */
23
24 #include <common.h>
25
26 #if defined(CFG_NIOS_EPCSBASE)
27 #include <command.h>
28 #include <nios2.h>
29 #include <nios2-io.h>
30 #include <nios2-epcs.h>
31
32
33 /*-----------------------------------------------------------------------*/
34 #define SHORT_HELP\
35         "epcs    - read/write Cyclone EPCS configuration device.\n"
36
37 #define LONG_HELP\
38         "\n"\
39         "epcs erase start [end]\n"\
40         "    - erase sector start or sectors start through end.\n"\
41         "epcs info\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"
51
52
53 /*-----------------------------------------------------------------------*/
54 /* Operation codes for serial configuration devices
55  */
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 */
65
66 /* Device status register bits
67  */
68 #define EPCS_STATUS_WIP         (1<<0)  /* Write in progress */
69 #define EPCS_STATUS_WEL         (1<<1)  /* Write enable latch */
70
71 /* Misc
72  */
73 #define EPCS_TIMEOUT            100     /* 100 msec timeout */
74
75 static nios_spi_t *epcs =
76         (nios_spi_t *)CACHE_BYPASS(CFG_NIOS_EPCSBASE);
77
78 /***********************************************************************
79  * Device access
80  ***********************************************************************/
81 static int epcs_cs (int assert)
82 {
83         ulong start;
84
85         if (assert) {
86                 epcs->control |= NIOS_SPI_SSO;
87         } else {
88                 /* Let all bits shift out */
89                 start = get_timer (0);
90                 while ((epcs->status & NIOS_SPI_TMT) == 0)
91                         if (get_timer (start) > EPCS_TIMEOUT)
92                                 return (-1);
93                 epcs->control &= ~NIOS_SPI_SSO;
94         }
95         return (0);
96 }
97
98 static int epcs_tx (unsigned char c)
99 {
100         ulong start;
101
102         start = get_timer (0);
103         while ((epcs->status & NIOS_SPI_TRDY) == 0)
104                 if (get_timer (start) > EPCS_TIMEOUT)
105                         return (-1);
106         epcs->txdata = c;
107         return (0);
108 }
109
110 static int epcs_rx (void)
111 {
112         ulong start;
113
114         start = get_timer (0);
115         while ((epcs->status & NIOS_SPI_RRDY) == 0)
116                 if (get_timer (start) > EPCS_TIMEOUT)
117                         return (-1);
118         return (epcs->rxdata);
119 }
120
121 static unsigned char bitrev[] = {
122         0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
123         0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
124 };
125
126 static unsigned char epcs_bitrev (unsigned char c)
127 {
128         unsigned char val;
129
130         val  = bitrev[c>>4];
131         val |= bitrev[c & 0x0f]<<4;
132         return (val);
133 }
134
135 static void epcs_rcv (unsigned char *dst, int len)
136 {
137         while (len--) {
138                 epcs_tx (0);
139                 *dst++ = epcs_rx ();
140         }
141 }
142
143 static void epcs_rrcv (unsigned char *dst, int len)
144 {
145         while (len--) {
146                 epcs_tx (0);
147                 *dst++ = epcs_bitrev (epcs_rx ());
148         }
149 }
150
151 static void epcs_snd (unsigned char *src, int len)
152 {
153         while (len--) {
154                 epcs_tx (*src++);
155                 epcs_rx ();
156         }
157 }
158
159 static void epcs_rsnd (unsigned char *src, int len)
160 {
161         while (len--) {
162                 epcs_tx (epcs_bitrev (*src++));
163                 epcs_rx ();
164         }
165 }
166
167 static void epcs_wr_enable (void)
168 {
169         epcs_cs (1);
170         epcs_tx (EPCS_WRITE_ENA);
171         epcs_rx ();
172         epcs_cs (0);
173 }
174
175 static unsigned char epcs_status_rd (void)
176 {
177         unsigned char status;
178
179         epcs_cs (1);
180         epcs_tx (EPCS_READ_STAT);
181         epcs_rx ();
182         epcs_tx (0);
183         status = epcs_rx ();
184         epcs_cs (0);
185         return (status);
186 }
187
188 static void epcs_status_wr (unsigned char status)
189 {
190         epcs_wr_enable ();
191         epcs_cs (1);
192         epcs_tx (EPCS_WRITE_STAT);
193         epcs_rx ();
194         epcs_tx (status);
195         epcs_rx ();
196         epcs_cs (0);
197         return;
198 }
199
200 /***********************************************************************
201  * Device information
202  ***********************************************************************/
203
204 static struct epcs_devinfo_t devinfo[] = {
205         { "EPCS1 ", 0x10, 17, 4, 15, 8, 0x0c },
206         { "EPCS4 ", 0x12, 19, 8, 16, 8, 0x1c },
207         { 0, 0, 0, 0, 0, 0 }
208 };
209
210 epcs_devinfo_t *epcs_dev_find (void)
211 {
212         unsigned char buf[4];
213         unsigned char id;
214         int i;
215         struct epcs_devinfo_t *dev = NULL;
216
217         /* Read silicon id requires 3 "dummy bytes" before it's put
218          * on the wire.
219          */
220         buf[0] = EPCS_READ_ID;
221         buf[1] = 0;
222         buf[2] = 0;
223         buf[3] = 0;
224
225         epcs_cs (1);
226         epcs_snd (buf,4);
227         epcs_rcv (buf,1);
228         if (epcs_cs (0) == -1)
229                 return (NULL);
230         id = buf[0];
231
232         /* Find the info struct */
233         i = 0;
234         while (devinfo[i].name) {
235                 if (id == devinfo[i].id) {
236                         dev = &devinfo[i];
237                         break;
238                 }
239                 i++;
240         }
241
242         return (dev);
243 }
244
245 /***********************************************************************
246  * Misc Utilities
247  ***********************************************************************/
248 int epcs_cfgsz (void)
249 {
250         int sz = 0;
251         unsigned char buf[128];
252         unsigned char *p;
253         struct epcs_devinfo_t *dev = epcs_dev_find ();
254
255         if (!dev)
256                 return (-1);
257
258         /* Read in the first 128 bytes of the device */
259         buf[0] = EPCS_READ_BYTES;
260         buf[1] = 0;
261         buf[2] = 0;
262         buf[3] = 0;
263
264         epcs_cs (1);
265         epcs_snd (buf,4);
266         epcs_rrcv (buf, sizeof(buf));
267         epcs_cs (0);
268
269         /* Search for the starting 0x6a which is followed by the
270          * 4-byte 'register' and 4-byte bit-count.
271          */
272         p = buf;
273         while (p < buf + sizeof(buf)-8) {
274                 if ( *p == 0x6a ) {
275                         /* Point to bit count and extract */
276                         p += 5;
277                         sz = *p++;
278                         sz |= *p++ << 8;
279                         sz |= *p++ << 16;
280                         sz |= *p++ << 24;
281                         /* Convert to byte count */
282                         sz += 7;
283                         sz >>= 3;
284                 } else if (*p == 0xff) {
285                         /* 0xff is ok ... just skip */
286                         p++;
287                         continue;
288                 } else {
289                         /* Not 0xff or 0x6a ... something's not
290                          * right ... report 'unknown' (sz=0).
291                          */
292                         break;
293                 }
294         }
295         return (sz);
296 }
297
298 int epcs_erase (unsigned start, unsigned end)
299 {
300         unsigned off, sectsz;
301         unsigned char buf[4];
302         struct epcs_devinfo_t *dev = epcs_dev_find ();
303
304         if (!dev || (start>end))
305                 return (-1);
306
307         /* Erase the requested sectors. An address is required
308          * that lies within the requested sector -- we'll just
309          * use the first address in the sector.
310          */
311         printf ("epcs erasing sector %d ", start);
312         if (start != end)
313                 printf ("to %d ", end);
314         sectsz = (1 << dev->sz_sect);
315         while (start <= end) {
316                 off = start * sectsz;
317                 start++;
318
319                 buf[0] = EPCS_ERASE_SECT;
320                 buf[1] = off >> 16;
321                 buf[2] = off >> 8;
322                 buf[3] = off;
323
324                 epcs_wr_enable ();
325                 epcs_cs (1);
326                 epcs_snd (buf,4);
327                 epcs_cs (0);
328
329                 printf ("."); /* Some user feedback */
330
331                 /* Wait for erase to complete */
332                 while (epcs_status_rd() & EPCS_STATUS_WIP)
333                         ;
334         }
335         printf (" done.\n");
336         return (0);
337 }
338
339 int epcs_read (ulong addr, ulong off, ulong cnt)
340 {
341         unsigned char buf[4];
342         struct epcs_devinfo_t *dev = epcs_dev_find ();
343
344         if (!dev)
345                 return (-1);
346
347         buf[0] = EPCS_READ_BYTES;
348         buf[1] = off >> 16;
349         buf[2] = off >> 8;
350         buf[3] = off;
351
352         epcs_cs (1);
353         epcs_snd (buf,4);
354         epcs_rrcv ((unsigned char *)addr, cnt);
355         epcs_cs (0);
356
357         return (0);
358 }
359
360 int epcs_write (ulong addr, ulong off, ulong cnt)
361 {
362         ulong wrcnt;
363         unsigned pgsz;
364         unsigned char buf[4];
365         struct epcs_devinfo_t *dev = epcs_dev_find ();
366
367         if (!dev)
368                 return (-1);
369
370         pgsz = (1<<dev->sz_page);
371         while (cnt) {
372                 if (off % pgsz)
373                         wrcnt = pgsz - (off % pgsz);
374                 else
375                         wrcnt = pgsz;
376                 wrcnt = (wrcnt > cnt) ? cnt : wrcnt;
377
378                 buf[0] = EPCS_WRITE_BYTES;
379                 buf[1] = off >> 16;
380                 buf[2] = off >> 8;
381                 buf[3] = off;
382
383                 epcs_wr_enable ();
384                 epcs_cs (1);
385                 epcs_snd (buf,4);
386                 epcs_rsnd ((unsigned char *)addr, wrcnt);
387                 epcs_cs (0);
388
389                 /* Wait for write to complete */
390                 while (epcs_status_rd() & EPCS_STATUS_WIP)
391                         ;
392
393                 cnt -= wrcnt;
394                 off += wrcnt;
395                 addr += wrcnt;
396         }
397
398         return (0);
399 }
400
401 int epcs_verify (ulong addr, ulong off, ulong cnt, ulong *err)
402 {
403         ulong rdcnt;
404         unsigned char buf[256];
405         unsigned char *start,*end;
406         int i;
407
408         start = end = (unsigned char *)addr;
409         while (cnt) {
410                 rdcnt = (cnt>sizeof(buf)) ? sizeof(buf) : cnt;
411                 epcs_read ((ulong)buf, off, rdcnt);
412                 for (i=0; i<rdcnt; i++) {
413                         if (*end != buf[i]) {
414                                 *err = end - start;
415                                 return(-1);
416                         }
417                         end++;
418                 }
419                 cnt -= rdcnt;
420                 off += rdcnt;
421         }
422         return (0);
423 }
424
425 static int epcs_sect_erased (int sect, unsigned *offset,
426                 struct epcs_devinfo_t *dev)
427 {
428         unsigned char buf[128];
429         unsigned off, end;
430         unsigned sectsz;
431         int i;
432
433         sectsz = (1 << dev->sz_sect);
434         off = sectsz * sect;
435         end = off + sectsz;
436
437         while (off < end) {
438                 epcs_read ((ulong)buf, off, sizeof(buf));
439                 for (i=0; i < sizeof(buf); i++) {
440                         if (buf[i] != 0xff) {
441                                 *offset = off + i;
442                                 return (0);
443                         }
444                 }
445                 off += sizeof(buf);
446         }
447         return (1);
448 }
449
450
451 /***********************************************************************
452  * Commands
453  ***********************************************************************/
454 static
455 void do_epcs_info (struct epcs_devinfo_t *dev, int argc, char *argv[])
456 {
457         int i;
458         unsigned char stat;
459         unsigned tmp;
460         int erased;
461
462         /* Basic device info */
463         printf ("%s: %d kbytes (%d sectors x %d kbytes,"
464                 " %d bytes/page)\n",
465                 dev->name, 1 << (dev->size-10),
466                 dev->num_sects, 1 << (dev->sz_sect-10),
467                 1 << dev->sz_page );
468
469         /* Status -- for now protection is all-or-nothing */
470         stat = epcs_status_rd();
471         printf ("status: 0x%02x (WIP:%d, WEL:%d, PROT:%s)\n",
472                 stat,
473                 (stat & EPCS_STATUS_WIP) ? 1 : 0,
474                 (stat & EPCS_STATUS_WEL) ? 1 : 0,
475                 (stat & dev->prot_mask) ? "on" : "off" );
476
477         /* Configuration  */
478         tmp = epcs_cfgsz ();
479         if (tmp) {
480                 printf ("config: 0x%06x (%d) bytes\n", tmp, tmp );
481         } else {
482                 printf ("config: unknown\n" );
483         }
484
485         /* Sector info */
486         for (i=0; i<dev->num_sects; i++) {
487                 erased = epcs_sect_erased (i, &tmp, dev);
488                 printf ("     %d: %06x ",
489                         i, i*(1<<dev->sz_sect) );
490                 if (erased)
491                         printf ("erased\n");
492                 else
493                         printf ("data @ 0x%06x\n", tmp);
494         }
495
496         return;
497 }
498
499 static
500 void do_epcs_erase (struct epcs_devinfo_t *dev, int argc, char *argv[])
501 {
502         unsigned start,end;
503
504         if ((argc < 3) || (argc > 4)) {
505                 printf ("USAGE: epcs erase sect [end]\n");
506                 return;
507         }
508         if ((epcs_status_rd() & dev->prot_mask) != 0) {
509                 printf ( "epcs: device protected.\n");
510                 return;
511         }
512
513         start = simple_strtoul (argv[2], NULL, 10);
514         if (argc > 3)
515                 end = simple_strtoul (argv[3], NULL, 10);
516         else
517                 end = start;
518         if ((start >= dev->num_sects) || (start > end)) {
519                 printf ("epcs: invalid sector range: [%d:%d]\n",
520                         start, end );
521                 return;
522         }
523
524         epcs_erase (start, end);
525
526         return;
527 }
528
529 static
530 void do_epcs_protect (struct epcs_devinfo_t *dev, int argc, char *argv[])
531 {
532         unsigned char stat;
533
534         /* For now protection is all-or-nothing to keep things
535          * simple. The protection bits don't map in a linear
536          * fashion ... and we would rather protect the bottom
537          * of the device since it contains the config data and
538          * leave the top unprotected for app use. But unfortunately
539          * protection works from top-to-bottom so it does
540          * really help very much from a software app point-of-view.
541          */
542         if (argc < 3) {
543                 printf ("USAGE: epcs protect on | off\n");
544                 return;
545         }
546         if (!dev)
547                 return;
548
549         /* Protection on/off is just a matter of setting/clearing
550          * all protection bits in the status register.
551          */
552         stat = epcs_status_rd ();
553         if (strcmp ("on", argv[2]) == 0) {
554                 stat |= dev->prot_mask;
555         } else if (strcmp ("off", argv[2]) == 0 ) {
556                 stat &= ~dev->prot_mask;
557         } else {
558                 printf ("epcs: unknown protection: %s\n", argv[2]);
559                 return;
560         }
561         epcs_status_wr (stat);
562         return;
563 }
564
565 static
566 void do_epcs_read (struct epcs_devinfo_t *dev, int argc, char *argv[])
567 {
568         ulong addr,off,cnt;
569         ulong sz;
570
571         if (argc < 5) {
572                 printf ("USAGE: epcs read addr offset count\n");
573                 return;
574         }
575
576         sz = 1 << dev->size;
577         addr = simple_strtoul (argv[2], NULL, 16);
578         off  = simple_strtoul (argv[3], NULL, 16);
579         cnt  = simple_strtoul (argv[4], NULL, 16);
580         if (off > sz) {
581                 printf ("offset is greater than device size"
582                         "... aborting.\n");
583                 return;
584         }
585         if ((off + cnt) > sz) {
586                 printf ("request exceeds device size"
587                         "... truncating.\n");
588                 cnt = sz - off;
589         }
590         printf ("epcs: read %08lx <- %06lx (0x%lx bytes)\n",
591                         addr, off, cnt);
592         epcs_read (addr, off, cnt);
593
594         return;
595 }
596
597 static
598 void do_epcs_write (struct epcs_devinfo_t *dev, int argc, char *argv[])
599 {
600         ulong addr,off,cnt;
601         ulong sz;
602         ulong err;
603
604         if (argc < 5) {
605                 printf ("USAGE: epcs write addr offset count\n");
606                 return;
607         }
608         if ((epcs_status_rd() & dev->prot_mask) != 0) {
609                 printf ( "epcs: device protected.\n");
610                 return;
611         }
612
613         sz = 1 << dev->size;
614         addr = simple_strtoul (argv[2], NULL, 16);
615         off  = simple_strtoul (argv[3], NULL, 16);
616         cnt  = simple_strtoul (argv[4], NULL, 16);
617         if (off > sz) {
618                 printf ("offset is greater than device size"
619                         "... aborting.\n");
620                 return;
621         }
622         if ((off + cnt) > sz) {
623                 printf ("request exceeds device size"
624                         "... truncating.\n");
625                 cnt = sz - off;
626         }
627         printf ("epcs: write %08lx -> %06lx (0x%lx bytes)\n",
628                         addr, off, cnt);
629         epcs_write (addr, off, cnt);
630         if (epcs_verify (addr, off, cnt, &err) != 0)
631                 printf ("epcs: write error at offset %06lx\n", err);
632
633         return;
634 }
635
636 static
637 void do_epcs_verify (struct epcs_devinfo_t *dev, int argc, char *argv[])
638 {
639         ulong addr,off,cnt;
640         ulong sz;
641         ulong err;
642
643         if (argc < 5) {
644                 printf ("USAGE: epcs verify addr offset count\n");
645                 return;
646         }
647
648         sz = 1 << dev->size;
649         addr = simple_strtoul (argv[2], NULL, 16);
650         off  = simple_strtoul (argv[3], NULL, 16);
651         cnt  = simple_strtoul (argv[4], NULL, 16);
652         if (off > sz) {
653                 printf ("offset is greater than device size"
654                         "... aborting.\n");
655                 return;
656         }
657         if ((off + cnt) > sz) {
658                 printf ("request exceeds device size"
659                         "... truncating.\n");
660                 cnt = sz - off;
661         }
662         printf ("epcs: verify %08lx -> %06lx (0x%lx bytes)\n",
663                         addr, off, cnt);
664         if (epcs_verify (addr, off, cnt, &err) != 0)
665                 printf ("epcs: verify error at offset %06lx\n", err);
666
667         return;
668 }
669
670 /*-----------------------------------------------------------------------*/
671 int do_epcs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
672 {
673         int len;
674         struct epcs_devinfo_t *dev = epcs_dev_find ();
675
676         if (!dev) {
677                 printf ("epcs: device not found.\n");
678                 return (-1);
679         }
680
681         if (argc < 2) {
682                 do_epcs_info (dev, argc, argv);
683                 return (0);
684         }
685
686         len = strlen (argv[1]);
687         if (strncmp ("info", argv[1], len) == 0) {
688                 do_epcs_info (dev, argc, argv);
689         } else if (strncmp ("erase", argv[1], len) == 0) {
690                 do_epcs_erase (dev, argc, argv);
691         } else if (strncmp ("protect", argv[1], len) == 0) {
692                 do_epcs_protect (dev, argc, argv);
693         } else if (strncmp ("read", argv[1], len) == 0) {
694                 do_epcs_read (dev, argc, argv);
695         } else if (strncmp ("write", argv[1], len) == 0) {
696                 do_epcs_write (dev, argc, argv);
697         } else if (strncmp ("verify", argv[1], len) == 0) {
698                 do_epcs_verify (dev, argc, argv);
699         } else {
700                 printf ("epcs: unknown operation: %s\n", argv[1]);
701         }
702
703         return (0);
704 }
705
706 /*-----------------------------------------------------------------------*/
707
708
709 U_BOOT_CMD( epcs, 5, 0, do_epcs, SHORT_HELP, LONG_HELP );
710
711 #endif /* CONFIG_NIOS_EPCS */