blob: 2c2e838de84c22f7e394107592ad1fae82231a85 [file] [log] [blame]
wdenkad276f22004-01-04 16:28:35 +00001/*
2 * (C) Copyright 2003, 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(CONFIG_NIOS_ASMI)
27#include <command.h>
28#include <nios-io.h>
29
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020030#if !defined(CONFIG_SYS_NIOS_ASMIBASE)
31#error "*** CONFIG_SYS_NIOS_ASMIBASE not defined ***"
wdenkad276f22004-01-04 16:28:35 +000032#endif
33
34/*-----------------------------------------------------------------------*/
35#define SHORT_HELP\
36 "asmi - read/write Cyclone ASMI configuration device.\n"
37
38#define LONG_HELP\
39 "\n"\
40 "asmi erase start [end]\n"\
41 " - erase sector start or sectors start through end.\n"\
42 "asmi info\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"
52
53
54/*-----------------------------------------------------------------------*/
55/* Operation codes for serial configuration devices
56 */
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 */
66
67/* Device status register bits
68 */
69#define ASMI_STATUS_WIP (1<<0) /* Write in progress */
70#define ASMI_STATUS_WEL (1<<1) /* Write enable latch */
71
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020072static nios_asmi_t *asmi = (nios_asmi_t *)CONFIG_SYS_NIOS_ASMIBASE;
wdenkad276f22004-01-04 16:28:35 +000073
74/***********************************************************************
75 * Device access
76 ***********************************************************************/
77static void asmi_cs (int assert)
78{
79 if (assert) {
80 asmi->control |= NIOS_ASMI_SSO;
81 } else {
82 /* Let all bits shift out */
83 while ((asmi->status & NIOS_ASMI_TMT) == 0)
84 ;
85 asmi->control &= ~NIOS_ASMI_SSO;
86 }
87}
88
89static void asmi_tx (unsigned char c)
90{
91 while ((asmi->status & NIOS_ASMI_TRDY) == 0)
92 ;
93 asmi->txdata = c;
94}
95
96static int asmi_rx (void)
97{
98 while ((asmi->status & NIOS_ASMI_RRDY) == 0)
99 ;
100 return (asmi->rxdata);
101}
102
103static unsigned char bitrev[] = {
104 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
105 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
106};
107
108static unsigned char asmi_bitrev( unsigned char c )
109{
110 unsigned char val;
111
112 val = bitrev[c>>4];
113 val |= bitrev[c & 0x0f]<<4;
114 return (val);
115}
116
117static void asmi_rcv (unsigned char *dst, int len)
118{
119 while (len--) {
120 asmi_tx (0);
121 *dst++ = asmi_rx ();
122 }
123}
124
125static void asmi_rrcv (unsigned char *dst, int len)
126{
127 while (len--) {
128 asmi_tx (0);
129 *dst++ = asmi_bitrev (asmi_rx ());
130 }
131}
132
133static void asmi_snd (unsigned char *src, int len)
134{
135 while (len--) {
136 asmi_tx (*src++);
137 asmi_rx ();
138 }
139}
140
141static void asmi_rsnd (unsigned char *src, int len)
142{
143 while (len--) {
144 asmi_tx (asmi_bitrev (*src++));
145 asmi_rx ();
146 }
147}
148
149static void asmi_wr_enable (void)
150{
151 asmi_cs (1);
152 asmi_tx (ASMI_WRITE_ENA);
153 asmi_rx ();
154 asmi_cs (0);
155}
156
157static unsigned char asmi_status_rd (void)
158{
159 unsigned char status;
160
161 asmi_cs (1);
162 asmi_tx (ASMI_READ_STAT);
163 asmi_rx ();
164 asmi_tx (0);
165 status = asmi_rx ();
166 asmi_cs (0);
167 return (status);
168}
169
170static void asmi_status_wr (unsigned char status)
171{
172 asmi_wr_enable ();
173 asmi_cs (1);
174 asmi_tx (ASMI_WRITE_STAT);
175 asmi_rx ();
176 asmi_tx (status);
177 asmi_rx ();
178 asmi_cs (0);
179 return;
180}
181
182/***********************************************************************
183 * Device information
184 ***********************************************************************/
185typedef struct asmi_devinfo_t {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200186 const char *name; /* Device name */
wdenkad276f22004-01-04 16:28:35 +0000187 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 */
193}asmi_devinfo_t;
194
195static struct asmi_devinfo_t devinfo[] = {
196 { "EPCS1 ", 0x10, 17, 4, 15, 8, 0x0c },
197 { "EPCS4 ", 0x12, 19, 8, 16, 8, 0x1c },
198 { 0, 0, 0, 0, 0, 0 }
199};
200
201static asmi_devinfo_t *asmi_dev_find (void)
202{
203 unsigned char buf[4];
204 unsigned char id;
205 int i;
206 struct asmi_devinfo_t *dev = NULL;
207
208 /* Read silicon id requires 3 "dummy bytes" before it's put
209 * on the wire.
210 */
211 buf[0] = ASMI_READ_ID;
212 buf[1] = 0;
213 buf[2] = 0;
214 buf[3] = 0;
215
216 asmi_cs (1);
217 asmi_snd (buf,4);
218 asmi_rcv (buf,1);
219 asmi_cs (0);
220 id = buf[0];
221
222 /* Find the info struct */
223 i = 0;
224 while (devinfo[i].name) {
225 if (id == devinfo[i].id) {
226 dev = &devinfo[i];
227 break;
228 }
229 i++;
230 }
231
232 return (dev);
233}
234
235/***********************************************************************
236 * Misc Utilities
237 ***********************************************************************/
238static unsigned asmi_cfgsz (void)
239{
240 unsigned sz = 0;
241 unsigned char buf[128];
242 unsigned char *p;
243
244 /* Read in the first 128 bytes of the device */
245 buf[0] = ASMI_READ_BYTES;
246 buf[1] = 0;
247 buf[2] = 0;
248 buf[3] = 0;
249
250 asmi_cs (1);
251 asmi_snd (buf,4);
252 asmi_rrcv (buf, sizeof(buf));
253 asmi_cs (0);
254
255 /* Search for the starting 0x6a which is followed by the
256 * 4-byte 'register' and 4-byte bit-count.
257 */
258 p = buf;
259 while (p < buf + sizeof(buf)-8) {
260 if ( *p == 0x6a ) {
261 /* Point to bit count and extract */
262 p += 5;
263 sz = *p++;
264 sz |= *p++ << 8;
265 sz |= *p++ << 16;
266 sz |= *p++ << 24;
267 /* Convert to byte count */
268 sz += 7;
269 sz >>= 3;
270 } else if (*p == 0xff) {
271 /* 0xff is ok ... just skip */
272 p++;
273 continue;
274 } else {
275 /* Not 0xff or 0x6a ... something's not
276 * right ... report 'unknown' (sz=0).
277 */
278 break;
279 }
280 }
281 return (sz);
282}
283
284static int asmi_erase (unsigned start, unsigned end)
285{
286 unsigned off, sectsz;
287 unsigned char buf[4];
288 struct asmi_devinfo_t *dev = asmi_dev_find ();
289
290 if (!dev || (start>end))
291 return (-1);
292
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.
296 */
297 printf ("asmi erasing sector %d ", start);
298 if (start != end)
299 printf ("to %d ", end);
300 sectsz = (1 << dev->sz_sect);
301 while (start <= end) {
302 off = start * sectsz;
303 start++;
304
305 buf[0] = ASMI_ERASE_SECT;
306 buf[1] = off >> 16;
307 buf[2] = off >> 8;
308 buf[3] = off;
309
310 asmi_wr_enable ();
311 asmi_cs (1);
312 asmi_snd (buf,4);
313 asmi_cs (0);
314
315 printf ("."); /* Some user feedback */
316
317 /* Wait for erase to complete */
318 while (asmi_status_rd() & ASMI_STATUS_WIP)
319 ;
320 }
321 printf (" done.\n");
322 return (0);
323}
324
325static int asmi_read (ulong addr, ulong off, ulong cnt)
326{
327 unsigned char buf[4];
328
329 buf[0] = ASMI_READ_BYTES;
330 buf[1] = off >> 16;
331 buf[2] = off >> 8;
332 buf[3] = off;
333
334 asmi_cs (1);
335 asmi_snd (buf,4);
336 asmi_rrcv ((unsigned char *)addr, cnt);
337 asmi_cs (0);
338
339 return (0);
340}
341
342static
343int asmi_write (ulong addr, ulong off, ulong cnt)
344{
345 ulong wrcnt;
346 unsigned pgsz;
347 unsigned char buf[4];
348 struct asmi_devinfo_t *dev = asmi_dev_find ();
349
350 if (!dev)
351 return (-1);
352
353 pgsz = (1<<dev->sz_page);
354 while (cnt) {
355 if (off % pgsz)
356 wrcnt = pgsz - (off % pgsz);
357 else
358 wrcnt = pgsz;
359 wrcnt = (wrcnt > cnt) ? cnt : wrcnt;
360
361 buf[0] = ASMI_WRITE_BYTES;
362 buf[1] = off >> 16;
363 buf[2] = off >> 8;
364 buf[3] = off;
365
366 asmi_wr_enable ();
367 asmi_cs (1);
368 asmi_snd (buf,4);
369 asmi_rsnd ((unsigned char *)addr, wrcnt);
370 asmi_cs (0);
371
372 /* Wait for write to complete */
373 while (asmi_status_rd() & ASMI_STATUS_WIP)
374 ;
375
376 cnt -= wrcnt;
377 off += wrcnt;
378 addr += wrcnt;
379 }
380
381 return (0);
382}
383
384static
385int asmi_verify (ulong addr, ulong off, ulong cnt, ulong *err)
386{
387 ulong rdcnt;
388 unsigned char buf[256];
389 unsigned char *start,*end;
390 int i;
391
392 start = end = (unsigned char *)addr;
393 while (cnt) {
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]) {
398 *err = end - start;
399 return(-1);
400 }
401 end++;
402 }
403 cnt -= rdcnt;
404 off += rdcnt;
405 }
406 return (0);
407}
408
409static int asmi_sect_erased (int sect, unsigned *offset,
410 struct asmi_devinfo_t *dev)
411{
412 unsigned char buf[128];
413 unsigned off, end;
414 unsigned sectsz;
415 int i;
416
417 sectsz = (1 << dev->sz_sect);
418 off = sectsz * sect;
419 end = off + sectsz;
420
421 while (off < end) {
422 asmi_read ((ulong)buf, off, sizeof(buf));
423 for (i=0; i < sizeof(buf); i++) {
424 if (buf[i] != 0xff) {
425 *offset = off + i;
426 return (0);
427 }
428 }
429 off += sizeof(buf);
430 }
431 return (1);
432}
433
434
435/***********************************************************************
436 * Commands
437 ***********************************************************************/
438static
439void do_asmi_info (struct asmi_devinfo_t *dev, int argc, char *argv[])
440{
441 int i;
442 unsigned char stat;
443 unsigned tmp;
444 int erased;
445
446 /* Basic device info */
447 printf ("%s: %d kbytes (%d sectors x %d kbytes,"
448 " %d bytes/page)\n",
449 dev->name, 1 << (dev->size-10),
450 dev->num_sects, 1 << (dev->sz_sect-10),
451 1 << dev->sz_page );
452
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",
456 stat,
457 (stat & ASMI_STATUS_WIP) ? 1 : 0,
458 (stat & ASMI_STATUS_WEL) ? 1 : 0,
459 (stat & dev->prot_mask) ? "on" : "off" );
460
461 /* Configuration */
462 tmp = asmi_cfgsz ();
463 if (tmp) {
464 printf ("config: 0x%06x (%d) bytes\n", tmp, tmp );
465 } else {
466 printf ("config: unknown\n" );
467 }
468
469 /* Sector info */
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) );
474 if (erased)
475 printf ("erased\n");
476 else
477 printf ("data @ 0x%06x\n", tmp);
478 }
479
480 return;
481}
482
483static
484void do_asmi_erase (struct asmi_devinfo_t *dev, int argc, char *argv[])
485{
486 unsigned start,end;
487
488 if ((argc < 3) || (argc > 4)) {
489 printf ("USAGE: asmi erase sect [end]\n");
490 return;
491 }
492 if ((asmi_status_rd() & dev->prot_mask) != 0) {
493 printf ( "asmi: device protected.\n");
494 return;
495 }
496
497 start = simple_strtoul (argv[2], NULL, 10);
498 if (argc > 3)
499 end = simple_strtoul (argv[3], NULL, 10);
500 else
501 end = start;
502 if ((start >= dev->num_sects) || (start > end)) {
503 printf ("asmi: invalid sector range: [%d:%d]\n",
504 start, end );
505 return;
506 }
507
508 asmi_erase (start, end);
509
510 return;
511}
512
513static
514void do_asmi_protect (struct asmi_devinfo_t *dev, int argc, char *argv[])
515{
516 unsigned char stat;
517
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.
525 */
526 if (argc < 3) {
527 printf ("USAGE: asmi protect on | off\n");
528 return;
529 }
530 if (!dev)
531 return;
532
533 /* Protection on/off is just a matter of setting/clearing
534 * all protection bits in the status register.
535 */
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;
541 } else {
542 printf ("asmi: unknown protection: %s\n", argv[2]);
543 return;
544 }
545 asmi_status_wr (stat);
546 return;
547}
548
549static
550void do_asmi_read (struct asmi_devinfo_t *dev, int argc, char *argv[])
551{
552 ulong addr,off,cnt;
553 ulong sz;
554
555 if (argc < 5) {
556 printf ("USAGE: asmi read addr offset count\n");
557 return;
558 }
559
560 sz = 1 << dev->size;
561 addr = simple_strtoul (argv[2], NULL, 16);
562 off = simple_strtoul (argv[3], NULL, 16);
563 cnt = simple_strtoul (argv[4], NULL, 16);
564 if (off > sz) {
565 printf ("offset is greater than device size"
566 "... aborting.\n");
567 return;
568 }
569 if ((off + cnt) > sz) {
570 printf ("request exceeds device size"
571 "... truncating.\n");
572 cnt = sz - off;
573 }
574 printf ("asmi: read %08lx <- %06lx (0x%lx bytes)\n",
575 addr, off, cnt);
576 asmi_read (addr, off, cnt);
577
578 return;
579}
580
581static
582void do_asmi_write (struct asmi_devinfo_t *dev, int argc, char *argv[])
583{
584 ulong addr,off,cnt;
585 ulong sz;
586 ulong err;
587
588 if (argc < 5) {
589 printf ("USAGE: asmi write addr offset count\n");
590 return;
591 }
592 if ((asmi_status_rd() & dev->prot_mask) != 0) {
593 printf ( "asmi: device protected.\n");
594 return;
595 }
596
597 sz = 1 << dev->size;
598 addr = simple_strtoul (argv[2], NULL, 16);
599 off = simple_strtoul (argv[3], NULL, 16);
600 cnt = simple_strtoul (argv[4], NULL, 16);
601 if (off > sz) {
602 printf ("offset is greater than device size"
603 "... aborting.\n");
604 return;
605 }
606 if ((off + cnt) > sz) {
607 printf ("request exceeds device size"
608 "... truncating.\n");
609 cnt = sz - off;
610 }
611 printf ("asmi: write %08lx -> %06lx (0x%lx bytes)\n",
612 addr, off, cnt);
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);
616
617 return;
618}
619
620static
621void do_asmi_verify (struct asmi_devinfo_t *dev, int argc, char *argv[])
622{
623 ulong addr,off,cnt;
624 ulong sz;
625 ulong err;
626
627 if (argc < 5) {
628 printf ("USAGE: asmi verify addr offset count\n");
629 return;
630 }
631
632 sz = 1 << dev->size;
633 addr = simple_strtoul (argv[2], NULL, 16);
634 off = simple_strtoul (argv[3], NULL, 16);
635 cnt = simple_strtoul (argv[4], NULL, 16);
636 if (off > sz) {
637 printf ("offset is greater than device size"
638 "... aborting.\n");
639 return;
640 }
641 if ((off + cnt) > sz) {
642 printf ("request exceeds device size"
643 "... truncating.\n");
644 cnt = sz - off;
645 }
646 printf ("asmi: verify %08lx -> %06lx (0x%lx bytes)\n",
647 addr, off, cnt);
648 if (asmi_verify (addr, off, cnt, &err) != 0)
649 printf ("asmi: verify error at offset %06lx\n", err);
650
651 return;
652}
653
654/*-----------------------------------------------------------------------*/
655int do_asmi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
656{
657 int len;
658 struct asmi_devinfo_t *dev = asmi_dev_find ();
659
660 if (argc < 2) {
661 printf ("Usage:%s", LONG_HELP);
662 return (0);
663 }
664
665 if (!dev) {
666 printf ("asmi: device not found.\n");
667 return (0);
668 }
669
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);
683 } else {
684 printf ("asmi: unknown operation: %s\n", argv[1]);
685 }
686
687 return (0);
688}
689
690/*-----------------------------------------------------------------------*/
691
692
693U_BOOT_CMD( asmi, 5, 0, do_asmi, SHORT_HELP, LONG_HELP );
694
695#endif /* CONFIG_NIOS_ASMI */