epcs.c 15 KB

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