buspirate_spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009, 2010, 2011, 2012 Carl-Daniel Hailfinger
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <stdio.h>
  20. #include <strings.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <unistd.h>
  25. #include "flash.h"
  26. #include "programmer.h"
  27. #include "spi.h"
  28. /* Change this to #define if you want to test without a serial implementation */
  29. #undef FAKE_COMMUNICATION
  30. struct buspirate_spispeeds {
  31. const char *name;
  32. const int speed;
  33. };
  34. #ifndef FAKE_COMMUNICATION
  35. static int buspirate_serialport_setup(char *dev)
  36. {
  37. /* 115200bps, 8 databits, no parity, 1 stopbit */
  38. sp_fd = sp_openserport(dev, 115200);
  39. if (sp_fd == SER_INV_FD)
  40. return 1;
  41. return 0;
  42. }
  43. #else
  44. #define buspirate_serialport_setup(...) 0
  45. #define serialport_shutdown(...) 0
  46. #define serialport_write(...) 0
  47. #define serialport_read(...) 0
  48. #define sp_flush_incoming(...) 0
  49. #endif
  50. static unsigned char *bp_commbuf = NULL;
  51. static int bp_commbufsize = 0;
  52. static int buspirate_commbuf_grow(int bufsize)
  53. {
  54. unsigned char *tmpbuf;
  55. /* Never shrink. realloc() calls are expensive. */
  56. if (bufsize <= bp_commbufsize)
  57. return 0;
  58. tmpbuf = realloc(bp_commbuf, bufsize);
  59. if (!tmpbuf) {
  60. /* Keep the existing buffer because memory is already tight. */
  61. msg_perr("Out of memory!\n");
  62. return ERROR_OOM;
  63. }
  64. bp_commbuf = tmpbuf;
  65. bp_commbufsize = bufsize;
  66. return 0;
  67. }
  68. static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
  69. unsigned int readcnt)
  70. {
  71. int i, ret = 0;
  72. msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
  73. if (!writecnt && !readcnt) {
  74. msg_perr("Zero length command!\n");
  75. return 1;
  76. }
  77. if (writecnt)
  78. msg_pspew("Sending");
  79. for (i = 0; i < writecnt; i++)
  80. msg_pspew(" 0x%02x", buf[i]);
  81. #ifdef FAKE_COMMUNICATION
  82. /* Placate the caller for now. */
  83. if (readcnt) {
  84. buf[0] = 0x01;
  85. memset(buf + 1, 0xff, readcnt - 1);
  86. }
  87. ret = 0;
  88. #else
  89. if (writecnt)
  90. ret = serialport_write(buf, writecnt);
  91. if (ret)
  92. return ret;
  93. if (readcnt)
  94. ret = serialport_read(buf, readcnt);
  95. if (ret)
  96. return ret;
  97. #endif
  98. if (readcnt)
  99. msg_pspew(", receiving");
  100. for (i = 0; i < readcnt; i++)
  101. msg_pspew(" 0x%02x", buf[i]);
  102. msg_pspew("\n");
  103. return 0;
  104. }
  105. static int buspirate_wait_for_string(unsigned char *buf, char *key)
  106. {
  107. unsigned int keylen = strlen(key);
  108. int ret;
  109. ret = buspirate_sendrecv(buf, 0, keylen);
  110. while (!ret) {
  111. if (!memcmp(buf, key, keylen))
  112. return 0;
  113. memmove(buf, buf + 1, keylen - 1);
  114. ret = buspirate_sendrecv(buf + keylen - 1, 0, 1);
  115. }
  116. return ret;
  117. }
  118. static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  119. const unsigned char *writearr, unsigned char *readarr);
  120. static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  121. const unsigned char *writearr, unsigned char *readarr);
  122. static struct spi_master spi_master_buspirate = {
  123. .type = SPI_CONTROLLER_BUSPIRATE,
  124. .max_data_read = MAX_DATA_UNSPECIFIED,
  125. .max_data_write = MAX_DATA_UNSPECIFIED,
  126. .command = NULL,
  127. .multicommand = default_spi_send_multicommand,
  128. .read = default_spi_read,
  129. .write_256 = default_spi_write_256,
  130. .write_aai = default_spi_write_aai,
  131. };
  132. static const struct buspirate_spispeeds spispeeds[] = {
  133. {"30k", 0x0},
  134. {"125k", 0x1},
  135. {"250k", 0x2},
  136. {"1M", 0x3},
  137. {"2M", 0x4},
  138. {"2.6M", 0x5},
  139. {"4M", 0x6},
  140. {"8M", 0x7},
  141. {NULL, 0x0},
  142. };
  143. static int buspirate_spi_shutdown(void *data)
  144. {
  145. int ret = 0, ret2 = 0;
  146. /* No need to allocate a buffer here, we know that bp_commbuf is at least DEFAULT_BUFSIZE big. */
  147. /* Exit raw SPI mode (enter raw bitbang mode) */
  148. bp_commbuf[0] = 0x00;
  149. if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
  150. goto out_shutdown;
  151. if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
  152. goto out_shutdown;
  153. if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
  154. goto out_shutdown;
  155. msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
  156. if (bp_commbuf[0] != '1') {
  157. msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
  158. ret = 1;
  159. goto out_shutdown;
  160. }
  161. /* Reset Bus Pirate (return to user terminal) */
  162. bp_commbuf[0] = 0x0f;
  163. ret = buspirate_sendrecv(bp_commbuf, 1, 0);
  164. out_shutdown:
  165. /* Shut down serial port communication */
  166. ret2 = serialport_shutdown(NULL);
  167. /* Keep the oldest error, it is probably the best indicator. */
  168. if (ret2 && !ret)
  169. ret = ret2;
  170. bp_commbufsize = 0;
  171. free(bp_commbuf);
  172. bp_commbuf = NULL;
  173. if (ret)
  174. msg_pdbg("Bus Pirate shutdown failed.\n");
  175. else
  176. msg_pdbg("Bus Pirate shutdown completed.\n");
  177. return ret;
  178. }
  179. #define BP_FWVERSION(a,b) ((a) << 8 | (b))
  180. int buspirate_spi_init(void)
  181. {
  182. char *tmp;
  183. char *dev;
  184. int i;
  185. unsigned int fw_version_major = 0;
  186. unsigned int fw_version_minor = 0;
  187. int spispeed = 0x7;
  188. int ret = 0;
  189. int pullup = 0;
  190. dev = extract_programmer_param("dev");
  191. if (dev && !strlen(dev)) {
  192. free(dev);
  193. dev = NULL;
  194. }
  195. if (!dev) {
  196. msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
  197. return 1;
  198. }
  199. tmp = extract_programmer_param("spispeed");
  200. if (tmp) {
  201. for (i = 0; spispeeds[i].name; i++) {
  202. if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) {
  203. spispeed = spispeeds[i].speed;
  204. break;
  205. }
  206. }
  207. if (!spispeeds[i].name)
  208. msg_perr("Invalid SPI speed, using default.\n");
  209. }
  210. free(tmp);
  211. tmp = extract_programmer_param("pullups");
  212. if (tmp) {
  213. if (strcasecmp("on", tmp) == 0)
  214. pullup = 1;
  215. else if (strcasecmp("off", tmp) == 0)
  216. ; // ignore
  217. else
  218. msg_perr("Invalid pullups state, not using them.\n");
  219. }
  220. free(tmp);
  221. /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
  222. #define DEFAULT_BUFSIZE (16 + 3)
  223. bp_commbuf = malloc(DEFAULT_BUFSIZE);
  224. if (!bp_commbuf) {
  225. bp_commbufsize = 0;
  226. msg_perr("Out of memory!\n");
  227. free(dev);
  228. return ERROR_OOM;
  229. }
  230. bp_commbufsize = DEFAULT_BUFSIZE;
  231. ret = buspirate_serialport_setup(dev);
  232. free(dev);
  233. if (ret) {
  234. bp_commbufsize = 0;
  235. free(bp_commbuf);
  236. bp_commbuf = NULL;
  237. return ret;
  238. }
  239. if (register_shutdown(buspirate_spi_shutdown, NULL) != 0) {
  240. bp_commbufsize = 0;
  241. free(bp_commbuf);
  242. bp_commbuf = NULL;
  243. return 1;
  244. }
  245. /* This is the brute force version, but it should work.
  246. * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands
  247. * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to
  248. * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays.
  249. */
  250. for (i = 0; i < 20; i++) {
  251. /* Enter raw bitbang mode */
  252. bp_commbuf[0] = 0x00;
  253. /* Send the command, don't read the response. */
  254. ret = buspirate_sendrecv(bp_commbuf, 1, 0);
  255. if (ret)
  256. return ret;
  257. /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
  258. * response which came in over serial. Unfortunately that does not work reliably on Linux
  259. * with FTDI USB-serial.
  260. */
  261. //sp_flush_incoming();
  262. /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
  263. * of 0x00 too fast apparently triggers such an UART input buffer overflow.
  264. */
  265. internal_sleep(10000);
  266. }
  267. /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
  268. if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
  269. return ret;
  270. /* Reset the Bus Pirate. */
  271. bp_commbuf[0] = 0x0f;
  272. /* Send the command, don't read the response. */
  273. if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
  274. return ret;
  275. if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
  276. return ret;
  277. /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
  278. for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
  279. if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
  280. return ret;
  281. if (strchr("\r\n\t ", bp_commbuf[i]))
  282. break;
  283. }
  284. bp_commbuf[i] = '\0';
  285. msg_pdbg("Detected Bus Pirate hardware %s\n", bp_commbuf);
  286. if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
  287. return ret;
  288. /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
  289. for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
  290. if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
  291. return ret;
  292. if (strchr("\r\n\t ", bp_commbuf[i]))
  293. break;
  294. }
  295. bp_commbuf[i] = '\0';
  296. msg_pdbg("Detected Bus Pirate firmware ");
  297. if (bp_commbuf[0] != 'v')
  298. msg_pdbg("(unknown version number format)");
  299. else if (!strchr("0123456789", bp_commbuf[1]))
  300. msg_pdbg("(unknown version number format)");
  301. else {
  302. fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
  303. while ((*tmp != '\0') && !strchr("0123456789", *tmp))
  304. tmp++;
  305. fw_version_minor = strtoul(tmp, NULL, 10);
  306. msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
  307. }
  308. msg_pdbg2(" (\"%s\")", bp_commbuf);
  309. msg_pdbg("\n");
  310. if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
  311. return ret;
  312. /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
  313. if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
  314. msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
  315. msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
  316. return SPI_PROGRAMMER_ERROR;
  317. }
  318. /* Use fast SPI mode in firmware 5.5 and newer. */
  319. if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) {
  320. msg_pdbg("Using SPI command set v2.\n");
  321. /* Sensible default buffer size. */
  322. if (buspirate_commbuf_grow(260 + 5))
  323. return ERROR_OOM;
  324. spi_master_buspirate.max_data_read = 2048;
  325. spi_master_buspirate.max_data_write = 256;
  326. spi_master_buspirate.command = buspirate_spi_send_command_v2;
  327. } else {
  328. msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n");
  329. msg_pinfo("Reading/writing a flash chip may take hours.\n");
  330. msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n");
  331. /* Sensible default buffer size. */
  332. if (buspirate_commbuf_grow(16 + 3))
  333. return ERROR_OOM;
  334. spi_master_buspirate.max_data_read = 12;
  335. spi_master_buspirate.max_data_write = 12;
  336. spi_master_buspirate.command = buspirate_spi_send_command_v1;
  337. }
  338. /* Workaround for broken speed settings in firmware 6.1 and older. */
  339. if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
  340. if (spispeed > 0x4) {
  341. msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
  342. "Limiting speed to 2 MHz.\n");
  343. msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
  344. spispeed = 0x4;
  345. }
  346. /* This works because speeds numbering starts at 0 and is contiguous. */
  347. msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
  348. /* Enter raw bitbang mode */
  349. for (i = 0; i < 20; i++) {
  350. bp_commbuf[0] = 0x00;
  351. if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
  352. return ret;
  353. }
  354. if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
  355. return ret;
  356. if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
  357. return ret;
  358. msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
  359. if (bp_commbuf[0] != '1') {
  360. msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
  361. return 1;
  362. }
  363. /* Enter raw SPI mode */
  364. bp_commbuf[0] = 0x01;
  365. ret = buspirate_sendrecv(bp_commbuf, 1, 0);
  366. if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
  367. return ret;
  368. if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
  369. return ret;
  370. msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
  371. if (bp_commbuf[0] != '1') {
  372. msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
  373. return 1;
  374. }
  375. /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
  376. bp_commbuf[0] = 0x40 | 0x0b;
  377. if (pullup == 1) {
  378. bp_commbuf[0] |= (1 << 2);
  379. msg_pdbg("Enabling pull-up resistors.\n");
  380. }
  381. ret = buspirate_sendrecv(bp_commbuf, 1, 1);
  382. if (ret)
  383. return 1;
  384. if (bp_commbuf[0] != 0x01) {
  385. msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n");
  386. return 1;
  387. }
  388. /* Set SPI speed */
  389. bp_commbuf[0] = 0x60 | spispeed;
  390. ret = buspirate_sendrecv(bp_commbuf, 1, 1);
  391. if (ret)
  392. return 1;
  393. if (bp_commbuf[0] != 0x01) {
  394. msg_perr("Protocol error while setting SPI speed!\n");
  395. return 1;
  396. }
  397. /* Set SPI config: output type, idle, clock edge, sample */
  398. bp_commbuf[0] = 0x80 | 0xa;
  399. ret = buspirate_sendrecv(bp_commbuf, 1, 1);
  400. if (ret)
  401. return 1;
  402. if (bp_commbuf[0] != 0x01) {
  403. msg_perr("Protocol error while setting SPI config!\n");
  404. return 1;
  405. }
  406. /* De-assert CS# */
  407. bp_commbuf[0] = 0x03;
  408. ret = buspirate_sendrecv(bp_commbuf, 1, 1);
  409. if (ret)
  410. return 1;
  411. if (bp_commbuf[0] != 0x01) {
  412. msg_perr("Protocol error while raising CS#!\n");
  413. return 1;
  414. }
  415. register_spi_master(&spi_master_buspirate);
  416. return 0;
  417. }
  418. static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  419. const unsigned char *writearr, unsigned char *readarr)
  420. {
  421. unsigned int i = 0;
  422. int ret = 0;
  423. if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
  424. return SPI_INVALID_LENGTH;
  425. /* 3 bytes extra for CS#, len, CS#. */
  426. if (buspirate_commbuf_grow(writecnt + readcnt + 3))
  427. return ERROR_OOM;
  428. /* Assert CS# */
  429. bp_commbuf[i++] = 0x02;
  430. bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
  431. memcpy(bp_commbuf + i, writearr, writecnt);
  432. i += writecnt;
  433. memset(bp_commbuf + i, 0, readcnt);
  434. i += readcnt;
  435. /* De-assert CS# */
  436. bp_commbuf[i++] = 0x03;
  437. ret = buspirate_sendrecv(bp_commbuf, i, i);
  438. if (ret) {
  439. msg_perr("Bus Pirate communication error!\n");
  440. return SPI_GENERIC_ERROR;
  441. }
  442. if (bp_commbuf[0] != 0x01) {
  443. msg_perr("Protocol error while lowering CS#!\n");
  444. return SPI_GENERIC_ERROR;
  445. }
  446. if (bp_commbuf[1] != 0x01) {
  447. msg_perr("Protocol error while reading/writing SPI!\n");
  448. return SPI_GENERIC_ERROR;
  449. }
  450. if (bp_commbuf[i - 1] != 0x01) {
  451. msg_perr("Protocol error while raising CS#!\n");
  452. return SPI_GENERIC_ERROR;
  453. }
  454. /* Skip CS#, length, writearr. */
  455. memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
  456. return ret;
  457. }
  458. static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  459. const unsigned char *writearr, unsigned char *readarr)
  460. {
  461. int i = 0, ret = 0;
  462. if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
  463. return SPI_INVALID_LENGTH;
  464. /* 5 bytes extra for command, writelen, readlen.
  465. * 1 byte extra for Ack/Nack.
  466. */
  467. if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1)))
  468. return ERROR_OOM;
  469. /* Combined SPI write/read. */
  470. bp_commbuf[i++] = 0x04;
  471. bp_commbuf[i++] = (writecnt >> 8) & 0xff;
  472. bp_commbuf[i++] = writecnt & 0xff;
  473. bp_commbuf[i++] = (readcnt >> 8) & 0xff;
  474. bp_commbuf[i++] = readcnt & 0xff;
  475. memcpy(bp_commbuf + i, writearr, writecnt);
  476. ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt);
  477. if (ret) {
  478. msg_perr("Bus Pirate communication error!\n");
  479. return SPI_GENERIC_ERROR;
  480. }
  481. if (bp_commbuf[0] != 0x01) {
  482. msg_perr("Protocol error while sending SPI write/read!\n");
  483. return SPI_GENERIC_ERROR;
  484. }
  485. /* Skip Ack. */
  486. memcpy(readarr, bp_commbuf + 1, readcnt);
  487. return ret;
  488. }