asmi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  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. #include <common.h>
  24. #if defined(CONFIG_NIOS_ASMI)
  25. #include <command.h>
  26. #include <nios-io.h>
  27. #if !defined(CONFIG_SYS_NIOS_ASMIBASE)
  28. #error "*** CONFIG_SYS_NIOS_ASMIBASE not defined ***"
  29. #endif
  30. /*-----------------------------------------------------------------------*/
  31. #define SHORT_HELP\
  32. "asmi - read/write Cyclone ASMI configuration device.\n"
  33. #define LONG_HELP\
  34. "\n"\
  35. "asmi erase start [end]\n"\
  36. " - erase sector start or sectors start through end.\n"\
  37. "asmi info\n"\
  38. " - display ASMI device information.\n"\
  39. "asmi protect on | off\n"\
  40. " - turn device protection on or off.\n"\
  41. "asmi read addr offset count\n"\
  42. " - read count bytes from offset to addr.\n"\
  43. "asmi write addr offset count\n"\
  44. " - write count bytes to offset from addr.\n"\
  45. "asmi verify addr offset count\n"\
  46. " - verify count bytes at offset from addr.\n"
  47. /*-----------------------------------------------------------------------*/
  48. /* Operation codes for serial configuration devices
  49. */
  50. #define ASMI_WRITE_ENA 0x06 /* Write enable */
  51. #define ASMI_WRITE_DIS 0x04 /* Write disable */
  52. #define ASMI_READ_STAT 0x05 /* Read status */
  53. #define ASMI_READ_BYTES 0x03 /* Read bytes */
  54. #define ASMI_READ_ID 0xab /* Read silicon id */
  55. #define ASMI_WRITE_STAT 0x01 /* Write status */
  56. #define ASMI_WRITE_BYTES 0x02 /* Write bytes */
  57. #define ASMI_ERASE_BULK 0xc7 /* Erase entire device */
  58. #define ASMI_ERASE_SECT 0xd8 /* Erase sector */
  59. /* Device status register bits
  60. */
  61. #define ASMI_STATUS_WIP (1<<0) /* Write in progress */
  62. #define ASMI_STATUS_WEL (1<<1) /* Write enable latch */
  63. static nios_asmi_t *asmi = (nios_asmi_t *)CONFIG_SYS_NIOS_ASMIBASE;
  64. /***********************************************************************
  65. * Device access
  66. ***********************************************************************/
  67. static void asmi_cs (int assert)
  68. {
  69. if (assert) {
  70. asmi->control |= NIOS_ASMI_SSO;
  71. } else {
  72. /* Let all bits shift out */
  73. while ((asmi->status & NIOS_ASMI_TMT) == 0)
  74. ;
  75. asmi->control &= ~NIOS_ASMI_SSO;
  76. }
  77. }
  78. static void asmi_tx (unsigned char c)
  79. {
  80. while ((asmi->status & NIOS_ASMI_TRDY) == 0)
  81. ;
  82. asmi->txdata = c;
  83. }
  84. static int asmi_rx (void)
  85. {
  86. while ((asmi->status & NIOS_ASMI_RRDY) == 0)
  87. ;
  88. return (asmi->rxdata);
  89. }
  90. static unsigned char bitrev[] = {
  91. 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
  92. 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
  93. };
  94. static unsigned char asmi_bitrev( unsigned char c )
  95. {
  96. unsigned char val;
  97. val = bitrev[c>>4];
  98. val |= bitrev[c & 0x0f]<<4;
  99. return (val);
  100. }
  101. static void asmi_rcv (unsigned char *dst, int len)
  102. {
  103. while (len--) {
  104. asmi_tx (0);
  105. *dst++ = asmi_rx ();
  106. }
  107. }
  108. static void asmi_rrcv (unsigned char *dst, int len)
  109. {
  110. while (len--) {
  111. asmi_tx (0);
  112. *dst++ = asmi_bitrev (asmi_rx ());
  113. }
  114. }
  115. static void asmi_snd (unsigned char *src, int len)
  116. {
  117. while (len--) {
  118. asmi_tx (*src++);
  119. asmi_rx ();
  120. }
  121. }
  122. static void asmi_rsnd (unsigned char *src, int len)
  123. {
  124. while (len--) {
  125. asmi_tx (asmi_bitrev (*src++));
  126. asmi_rx ();
  127. }
  128. }
  129. static void asmi_wr_enable (void)
  130. {
  131. asmi_cs (1);
  132. asmi_tx (ASMI_WRITE_ENA);
  133. asmi_rx ();
  134. asmi_cs (0);
  135. }
  136. static unsigned char asmi_status_rd (void)
  137. {
  138. unsigned char status;
  139. asmi_cs (1);
  140. asmi_tx (ASMI_READ_STAT);
  141. asmi_rx ();
  142. asmi_tx (0);
  143. status = asmi_rx ();
  144. asmi_cs (0);
  145. return (status);
  146. }
  147. static void asmi_status_wr (unsigned char status)
  148. {
  149. asmi_wr_enable ();
  150. asmi_cs (1);
  151. asmi_tx (ASMI_WRITE_STAT);
  152. asmi_rx ();
  153. asmi_tx (status);
  154. asmi_rx ();
  155. asmi_cs (0);
  156. return;
  157. }
  158. /***********************************************************************
  159. * Device information
  160. ***********************************************************************/
  161. typedef struct asmi_devinfo_t {
  162. const char *name; /* Device name */
  163. unsigned char id; /* Device silicon id */
  164. unsigned char size; /* Total size log2(bytes)*/
  165. unsigned char num_sects; /* Number of sectors */
  166. unsigned char sz_sect; /* Sector size log2(bytes) */
  167. unsigned char sz_page; /* Page size log2(bytes) */
  168. unsigned char prot_mask; /* Protection mask */
  169. }asmi_devinfo_t;
  170. static struct asmi_devinfo_t devinfo[] = {
  171. { "EPCS1 ", 0x10, 17, 4, 15, 8, 0x0c },
  172. { "EPCS4 ", 0x12, 19, 8, 16, 8, 0x1c },
  173. { 0, 0, 0, 0, 0, 0 }
  174. };
  175. static asmi_devinfo_t *asmi_dev_find (void)
  176. {
  177. unsigned char buf[4];
  178. unsigned char id;
  179. int i;
  180. struct asmi_devinfo_t *dev = NULL;
  181. /* Read silicon id requires 3 "dummy bytes" before it's put
  182. * on the wire.
  183. */
  184. buf[0] = ASMI_READ_ID;
  185. buf[1] = 0;
  186. buf[2] = 0;
  187. buf[3] = 0;
  188. asmi_cs (1);
  189. asmi_snd (buf,4);
  190. asmi_rcv (buf,1);
  191. asmi_cs (0);
  192. id = buf[0];
  193. /* Find the info struct */
  194. i = 0;
  195. while (devinfo[i].name) {
  196. if (id == devinfo[i].id) {
  197. dev = &devinfo[i];
  198. break;
  199. }
  200. i++;
  201. }
  202. return (dev);
  203. }
  204. /***********************************************************************
  205. * Misc Utilities
  206. ***********************************************************************/
  207. static unsigned asmi_cfgsz (void)
  208. {
  209. unsigned sz = 0;
  210. unsigned char buf[128];
  211. unsigned char *p;
  212. /* Read in the first 128 bytes of the device */
  213. buf[0] = ASMI_READ_BYTES;
  214. buf[1] = 0;
  215. buf[2] = 0;
  216. buf[3] = 0;
  217. asmi_cs (1);
  218. asmi_snd (buf,4);
  219. asmi_rrcv (buf, sizeof(buf));
  220. asmi_cs (0);
  221. /* Search for the starting 0x6a which is followed by the
  222. * 4-byte 'register' and 4-byte bit-count.
  223. */
  224. p = buf;
  225. while (p < buf + sizeof(buf)-8) {
  226. if ( *p == 0x6a ) {
  227. /* Point to bit count and extract */
  228. p += 5;
  229. sz = *p++;
  230. sz |= *p++ << 8;
  231. sz |= *p++ << 16;
  232. sz |= *p++ << 24;
  233. /* Convert to byte count */
  234. sz += 7;
  235. sz >>= 3;
  236. } else if (*p == 0xff) {
  237. /* 0xff is ok ... just skip */
  238. p++;
  239. continue;
  240. } else {
  241. /* Not 0xff or 0x6a ... something's not
  242. * right ... report 'unknown' (sz=0).
  243. */
  244. break;
  245. }
  246. }
  247. return (sz);
  248. }
  249. static int asmi_erase (unsigned start, unsigned end)
  250. {
  251. unsigned off, sectsz;
  252. unsigned char buf[4];
  253. struct asmi_devinfo_t *dev = asmi_dev_find ();
  254. if (!dev || (start>end))
  255. return (-1);
  256. /* Erase the requested sectors. An address is required
  257. * that lies within the requested sector -- we'll just
  258. * use the first address in the sector.
  259. */
  260. printf ("asmi erasing sector %d ", start);
  261. if (start != end)
  262. printf ("to %d ", end);
  263. sectsz = (1 << dev->sz_sect);
  264. while (start <= end) {
  265. off = start * sectsz;
  266. start++;
  267. buf[0] = ASMI_ERASE_SECT;
  268. buf[1] = off >> 16;
  269. buf[2] = off >> 8;
  270. buf[3] = off;
  271. asmi_wr_enable ();
  272. asmi_cs (1);
  273. asmi_snd (buf,4);
  274. asmi_cs (0);
  275. printf ("."); /* Some user feedback */
  276. /* Wait for erase to complete */
  277. while (asmi_status_rd() & ASMI_STATUS_WIP)
  278. ;
  279. }
  280. printf (" done.\n");
  281. return (0);
  282. }
  283. static int asmi_read (ulong addr, ulong off, ulong cnt)
  284. {
  285. unsigned char buf[4];
  286. buf[0] = ASMI_READ_BYTES;
  287. buf[1] = off >> 16;
  288. buf[2] = off >> 8;
  289. buf[3] = off;
  290. asmi_cs (1);
  291. asmi_snd (buf,4);
  292. asmi_rrcv ((unsigned char *)addr, cnt);
  293. asmi_cs (0);
  294. return (0);
  295. }
  296. static
  297. int asmi_write (ulong addr, ulong off, ulong cnt)
  298. {
  299. ulong wrcnt;
  300. unsigned pgsz;
  301. unsigned char buf[4];
  302. struct asmi_devinfo_t *dev = asmi_dev_find ();
  303. if (!dev)
  304. return (-1);
  305. pgsz = (1<<dev->sz_page);
  306. while (cnt) {
  307. if (off % pgsz)
  308. wrcnt = pgsz - (off % pgsz);
  309. else
  310. wrcnt = pgsz;
  311. wrcnt = (wrcnt > cnt) ? cnt : wrcnt;
  312. buf[0] = ASMI_WRITE_BYTES;
  313. buf[1] = off >> 16;
  314. buf[2] = off >> 8;
  315. buf[3] = off;
  316. asmi_wr_enable ();
  317. asmi_cs (1);
  318. asmi_snd (buf,4);
  319. asmi_rsnd ((unsigned char *)addr, wrcnt);
  320. asmi_cs (0);
  321. /* Wait for write to complete */
  322. while (asmi_status_rd() & ASMI_STATUS_WIP)
  323. ;
  324. cnt -= wrcnt;
  325. off += wrcnt;
  326. addr += wrcnt;
  327. }
  328. return (0);
  329. }
  330. static
  331. int asmi_verify (ulong addr, ulong off, ulong cnt, ulong *err)
  332. {
  333. ulong rdcnt;
  334. unsigned char buf[256];
  335. unsigned char *start,*end;
  336. int i;
  337. start = end = (unsigned char *)addr;
  338. while (cnt) {
  339. rdcnt = (cnt>sizeof(buf)) ? sizeof(buf) : cnt;
  340. asmi_read ((ulong)buf, off, rdcnt);
  341. for (i=0; i<rdcnt; i++) {
  342. if (*end != buf[i]) {
  343. *err = end - start;
  344. return(-1);
  345. }
  346. end++;
  347. }
  348. cnt -= rdcnt;
  349. off += rdcnt;
  350. }
  351. return (0);
  352. }
  353. static int asmi_sect_erased (int sect, unsigned *offset,
  354. struct asmi_devinfo_t *dev)
  355. {
  356. unsigned char buf[128];
  357. unsigned off, end;
  358. unsigned sectsz;
  359. int i;
  360. sectsz = (1 << dev->sz_sect);
  361. off = sectsz * sect;
  362. end = off + sectsz;
  363. while (off < end) {
  364. asmi_read ((ulong)buf, off, sizeof(buf));
  365. for (i=0; i < sizeof(buf); i++) {
  366. if (buf[i] != 0xff) {
  367. *offset = off + i;
  368. return (0);
  369. }
  370. }
  371. off += sizeof(buf);
  372. }
  373. return (1);
  374. }
  375. /***********************************************************************
  376. * Commands
  377. ***********************************************************************/
  378. static
  379. void do_asmi_info (struct asmi_devinfo_t *dev, int argc, char *argv[])
  380. {
  381. int i;
  382. unsigned char stat;
  383. unsigned tmp;
  384. int erased;
  385. /* Basic device info */
  386. printf ("%s: %d kbytes (%d sectors x %d kbytes,"
  387. " %d bytes/page)\n",
  388. dev->name, 1 << (dev->size-10),
  389. dev->num_sects, 1 << (dev->sz_sect-10),
  390. 1 << dev->sz_page );
  391. /* Status -- for now protection is all-or-nothing */
  392. stat = asmi_status_rd();
  393. printf ("status: 0x%02x (WIP:%d, WEL:%d, PROT:%s)\n",
  394. stat,
  395. (stat & ASMI_STATUS_WIP) ? 1 : 0,
  396. (stat & ASMI_STATUS_WEL) ? 1 : 0,
  397. (stat & dev->prot_mask) ? "on" : "off" );
  398. /* Configuration */
  399. tmp = asmi_cfgsz ();
  400. if (tmp) {
  401. printf ("config: 0x%06x (%d) bytes\n", tmp, tmp );
  402. } else {
  403. printf ("config: unknown\n" );
  404. }
  405. /* Sector info */
  406. for (i=0; i<dev->num_sects; i++) {
  407. erased = asmi_sect_erased (i, &tmp, dev);
  408. printf (" %d: %06x ",
  409. i, i*(1<<dev->sz_sect) );
  410. if (erased)
  411. printf ("erased\n");
  412. else
  413. printf ("data @ 0x%06x\n", tmp);
  414. }
  415. return;
  416. }
  417. static
  418. void do_asmi_erase (struct asmi_devinfo_t *dev, int argc, char *argv[])
  419. {
  420. unsigned start,end;
  421. if ((argc < 3) || (argc > 4)) {
  422. printf ("USAGE: asmi erase sect [end]\n");
  423. return;
  424. }
  425. if ((asmi_status_rd() & dev->prot_mask) != 0) {
  426. printf ( "asmi: device protected.\n");
  427. return;
  428. }
  429. start = simple_strtoul (argv[2], NULL, 10);
  430. if (argc > 3)
  431. end = simple_strtoul (argv[3], NULL, 10);
  432. else
  433. end = start;
  434. if ((start >= dev->num_sects) || (start > end)) {
  435. printf ("asmi: invalid sector range: [%d:%d]\n",
  436. start, end );
  437. return;
  438. }
  439. asmi_erase (start, end);
  440. return;
  441. }
  442. static
  443. void do_asmi_protect (struct asmi_devinfo_t *dev, int argc, char *argv[])
  444. {
  445. unsigned char stat;
  446. /* For now protection is all-or-nothing to keep things
  447. * simple. The protection bits don't map in a linear
  448. * fashion ... and we would rather protect the bottom
  449. * of the device since it contains the config data and
  450. * leave the top unprotected for app use. But unfortunately
  451. * protection works from top-to-bottom so it does
  452. * really help very much from a software app point-of-view.
  453. */
  454. if (argc < 3) {
  455. printf ("USAGE: asmi protect on | off\n");
  456. return;
  457. }
  458. if (!dev)
  459. return;
  460. /* Protection on/off is just a matter of setting/clearing
  461. * all protection bits in the status register.
  462. */
  463. stat = asmi_status_rd ();
  464. if (strcmp ("on", argv[2]) == 0) {
  465. stat |= dev->prot_mask;
  466. } else if (strcmp ("off", argv[2]) == 0 ) {
  467. stat &= ~dev->prot_mask;
  468. } else {
  469. printf ("asmi: unknown protection: %s\n", argv[2]);
  470. return;
  471. }
  472. asmi_status_wr (stat);
  473. return;
  474. }
  475. static
  476. void do_asmi_read (struct asmi_devinfo_t *dev, int argc, char *argv[])
  477. {
  478. ulong addr,off,cnt;
  479. ulong sz;
  480. if (argc < 5) {
  481. printf ("USAGE: asmi read addr offset count\n");
  482. return;
  483. }
  484. sz = 1 << dev->size;
  485. addr = simple_strtoul (argv[2], NULL, 16);
  486. off = simple_strtoul (argv[3], NULL, 16);
  487. cnt = simple_strtoul (argv[4], NULL, 16);
  488. if (off > sz) {
  489. printf ("offset is greater than device size"
  490. "... aborting.\n");
  491. return;
  492. }
  493. if ((off + cnt) > sz) {
  494. printf ("request exceeds device size"
  495. "... truncating.\n");
  496. cnt = sz - off;
  497. }
  498. printf ("asmi: read %08lx <- %06lx (0x%lx bytes)\n",
  499. addr, off, cnt);
  500. asmi_read (addr, off, cnt);
  501. return;
  502. }
  503. static
  504. void do_asmi_write (struct asmi_devinfo_t *dev, int argc, char *argv[])
  505. {
  506. ulong addr,off,cnt;
  507. ulong sz;
  508. ulong err;
  509. if (argc < 5) {
  510. printf ("USAGE: asmi write addr offset count\n");
  511. return;
  512. }
  513. if ((asmi_status_rd() & dev->prot_mask) != 0) {
  514. printf ( "asmi: device protected.\n");
  515. return;
  516. }
  517. sz = 1 << dev->size;
  518. addr = simple_strtoul (argv[2], NULL, 16);
  519. off = simple_strtoul (argv[3], NULL, 16);
  520. cnt = simple_strtoul (argv[4], NULL, 16);
  521. if (off > sz) {
  522. printf ("offset is greater than device size"
  523. "... aborting.\n");
  524. return;
  525. }
  526. if ((off + cnt) > sz) {
  527. printf ("request exceeds device size"
  528. "... truncating.\n");
  529. cnt = sz - off;
  530. }
  531. printf ("asmi: write %08lx -> %06lx (0x%lx bytes)\n",
  532. addr, off, cnt);
  533. asmi_write (addr, off, cnt);
  534. if (asmi_verify (addr, off, cnt, &err) != 0)
  535. printf ("asmi: write error at offset %06lx\n", err);
  536. return;
  537. }
  538. static
  539. void do_asmi_verify (struct asmi_devinfo_t *dev, int argc, char *argv[])
  540. {
  541. ulong addr,off,cnt;
  542. ulong sz;
  543. ulong err;
  544. if (argc < 5) {
  545. printf ("USAGE: asmi verify addr offset count\n");
  546. return;
  547. }
  548. sz = 1 << dev->size;
  549. addr = simple_strtoul (argv[2], NULL, 16);
  550. off = simple_strtoul (argv[3], NULL, 16);
  551. cnt = simple_strtoul (argv[4], NULL, 16);
  552. if (off > sz) {
  553. printf ("offset is greater than device size"
  554. "... aborting.\n");
  555. return;
  556. }
  557. if ((off + cnt) > sz) {
  558. printf ("request exceeds device size"
  559. "... truncating.\n");
  560. cnt = sz - off;
  561. }
  562. printf ("asmi: verify %08lx -> %06lx (0x%lx bytes)\n",
  563. addr, off, cnt);
  564. if (asmi_verify (addr, off, cnt, &err) != 0)
  565. printf ("asmi: verify error at offset %06lx\n", err);
  566. return;
  567. }
  568. /*-----------------------------------------------------------------------*/
  569. int do_asmi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  570. {
  571. int len;
  572. struct asmi_devinfo_t *dev = asmi_dev_find ();
  573. if (argc < 2) {
  574. printf ("Usage:%s", LONG_HELP);
  575. return (0);
  576. }
  577. if (!dev) {
  578. printf ("asmi: device not found.\n");
  579. return (0);
  580. }
  581. len = strlen (argv[1]);
  582. if (strncmp ("info", argv[1], len) == 0) {
  583. do_asmi_info ( dev, argc, argv);
  584. } else if (strncmp ("erase", argv[1], len) == 0) {
  585. do_asmi_erase (dev, argc, argv);
  586. } else if (strncmp ("protect", argv[1], len) == 0) {
  587. do_asmi_protect (dev, argc, argv);
  588. } else if (strncmp ("read", argv[1], len) == 0) {
  589. do_asmi_read (dev, argc, argv);
  590. } else if (strncmp ("write", argv[1], len) == 0) {
  591. do_asmi_write (dev, argc, argv);
  592. } else if (strncmp ("verify", argv[1], len) == 0) {
  593. do_asmi_verify (dev, argc, argv);
  594. } else {
  595. printf ("asmi: unknown operation: %s\n", argv[1]);
  596. }
  597. return (0);
  598. }
  599. /*-----------------------------------------------------------------------*/
  600. U_BOOT_CMD( asmi, 5, 0, do_asmi, SHORT_HELP, LONG_HELP );
  601. #endif /* CONFIG_NIOS_ASMI */