epcs.c 15 KB

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