ft2232_spi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
  5. * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #if CONFIG_FT2232_SPI == 1
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25. #include "flash.h"
  26. #include "programmer.h"
  27. #include "spi.h"
  28. #include <ftdi.h>
  29. /* Please keep sorted by vendor ID, then device ID. */
  30. #define FTDI_VID 0x0403
  31. #define FTDI_FT2232H_PID 0x6010
  32. #define FTDI_FT4232H_PID 0x6011
  33. #define TIAO_TUMPA_PID 0x8a98
  34. #define AMONTEC_JTAGKEY_PID 0xCFF8
  35. #define GOEPEL_VID 0x096C
  36. #define GOEPEL_PICOTAP_PID 0x1449
  37. #define FIC_VID 0x1457
  38. #define OPENMOKO_DBGBOARD_PID 0x5118
  39. #define OLIMEX_VID 0x15BA
  40. #define OLIMEX_ARM_OCD_PID 0x0003
  41. #define OLIMEX_ARM_TINY_PID 0x0004
  42. #define OLIMEX_ARM_OCD_H_PID 0x002B
  43. #define OLIMEX_ARM_TINY_H_PID 0x002A
  44. #define GOOGLE_VID 0x18D1
  45. #define GOOGLE_SERVO_PID 0x5001
  46. #define GOOGLE_SERVO_V2_PID0 0x5002
  47. #define GOOGLE_SERVO_V2_PID1 0x5003
  48. const struct usbdev_status devs_ft2232spi[] = {
  49. {FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"},
  50. {FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"},
  51. {FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
  52. {FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
  53. {GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"},
  54. {FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC",
  55. "OpenMoko Neo1973 Debug board (V2+)"},
  56. {OLIMEX_VID, OLIMEX_ARM_OCD_PID, NT, "Olimex", "ARM-USB-OCD"},
  57. {OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"},
  58. {OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, NT, "Olimex", "ARM-USB-OCD-H"},
  59. {OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, NT, "Olimex", "ARM-USB-TINY-H"},
  60. {GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"},
  61. {GOOGLE_VID, GOOGLE_SERVO_V2_PID0, OK, "Google", "Servo V2 Legacy"},
  62. {GOOGLE_VID, GOOGLE_SERVO_V2_PID1, OK, "Google", "Servo V2"},
  63. {},
  64. };
  65. /*
  66. * The 'H' chips can run internally at either 12MHz or 60MHz.
  67. * The non-H chips can only run at 12MHz.
  68. */
  69. static uint8_t clock_5x = 1;
  70. /*
  71. * In either case, the divisor is a simple integer clock divider.
  72. * If clock_5x is set, this divisor divides 30MHz, else it divides 6MHz.
  73. */
  74. #define DIVIDE_BY 3 /* e.g. '3' will give either 10MHz or 2MHz SPI clock. */
  75. #define BITMODE_BITBANG_NORMAL 1
  76. #define BITMODE_BITBANG_SPI 2
  77. /* Set data bits low-byte command:
  78. * value: 0x08 CS=high, DI=low, DO=low, SK=low
  79. * dir: 0x0b CS=output, DI=input, DO=output, SK=output
  80. *
  81. * JTAGkey(2) needs to enable its output via Bit4 / GPIOL0
  82. * value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low
  83. * dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output
  84. */
  85. static uint8_t cs_bits = 0x08;
  86. static uint8_t pindir = 0x0b;
  87. static struct ftdi_context ftdic_context;
  88. static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
  89. {
  90. int i;
  91. for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
  92. if ((devs_ft2232spi[i].device_id == ft2232_type)
  93. && (devs_ft2232spi[i].vendor_id == ft2232_vid))
  94. return devs_ft2232spi[i].device_name;
  95. }
  96. return "unknown device";
  97. }
  98. static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type)
  99. {
  100. int i;
  101. for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
  102. if ((devs_ft2232spi[i].device_id == ft2232_type)
  103. && (devs_ft2232spi[i].vendor_id == ft2232_vid))
  104. return devs_ft2232spi[i].vendor_name;
  105. }
  106. return "unknown vendor";
  107. }
  108. static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf,
  109. int size)
  110. {
  111. int r;
  112. r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
  113. if (r < 0) {
  114. msg_perr("ftdi_write_data: %d, %s\n", r,
  115. ftdi_get_error_string(ftdic));
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf,
  121. int size)
  122. {
  123. int r;
  124. while (size > 0) {
  125. r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
  126. if (r < 0) {
  127. msg_perr("ftdi_read_data: %d, %s\n", r,
  128. ftdi_get_error_string(ftdic));
  129. return 1;
  130. }
  131. buf += r;
  132. size -= r;
  133. }
  134. return 0;
  135. }
  136. static int ft2232_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  137. const unsigned char *writearr, unsigned char *readarr);
  138. static const struct spi_programmer spi_programmer_ft2232 = {
  139. .type = SPI_CONTROLLER_FT2232,
  140. .max_data_read = 64 * 1024,
  141. .max_data_write = 256,
  142. .command = ft2232_spi_send_command,
  143. .multicommand = default_spi_send_multicommand,
  144. .read = default_spi_read,
  145. .write_256 = default_spi_write_256,
  146. };
  147. /* Returns 0 upon success, a negative number upon errors. */
  148. int ft2232_spi_init(void)
  149. {
  150. int f, ret = 0;
  151. struct ftdi_context *ftdic = &ftdic_context;
  152. unsigned char buf[512];
  153. int ft2232_vid = FTDI_VID;
  154. int ft2232_type = FTDI_FT4232H_PID;
  155. enum ftdi_interface ft2232_interface = INTERFACE_B;
  156. char *endp;
  157. double spi_mhz = 0;
  158. uint16_t divide_by = DIVIDE_BY;
  159. char *arg;
  160. double mpsse_clk;
  161. arg = extract_programmer_param("type");
  162. if (arg) {
  163. if (!strcasecmp(arg, "2232H"))
  164. ft2232_type = FTDI_FT2232H_PID;
  165. else if (!strcasecmp(arg, "4232H"))
  166. ft2232_type = FTDI_FT4232H_PID;
  167. else if (!strcasecmp(arg, "jtagkey")) {
  168. ft2232_type = AMONTEC_JTAGKEY_PID;
  169. ft2232_interface = INTERFACE_A;
  170. cs_bits = 0x18;
  171. pindir = 0x1b;
  172. } else if (!strcasecmp(arg, "picotap")) {
  173. ft2232_vid = GOEPEL_VID;
  174. ft2232_type = GOEPEL_PICOTAP_PID;
  175. ft2232_interface = INTERFACE_A;
  176. } else if (!strcasecmp(arg, "tumpa")) {
  177. /* Interface A is SPI1, B is SPI2. */
  178. ft2232_type = TIAO_TUMPA_PID;
  179. ft2232_interface = INTERFACE_A;
  180. } else if (!strcasecmp(arg, "busblaster")) {
  181. /* In its default configuration it is a jtagkey clone */
  182. ft2232_type = FTDI_FT2232H_PID;
  183. ft2232_interface = INTERFACE_A;
  184. cs_bits = 0x18;
  185. pindir = 0x1b;
  186. } else if (!strcasecmp(arg, "openmoko")) {
  187. ft2232_vid = FIC_VID;
  188. ft2232_type = OPENMOKO_DBGBOARD_PID;
  189. ft2232_interface = INTERFACE_A;
  190. } else if (!strcasecmp(arg, "arm-usb-ocd")) {
  191. ft2232_vid = OLIMEX_VID;
  192. ft2232_type = OLIMEX_ARM_OCD_PID;
  193. ft2232_interface = INTERFACE_A;
  194. cs_bits = 0x08;
  195. pindir = 0x1b;
  196. } else if (!strcasecmp(arg, "arm-usb-tiny")) {
  197. ft2232_vid = OLIMEX_VID;
  198. ft2232_type = OLIMEX_ARM_TINY_PID;
  199. ft2232_interface = INTERFACE_A;
  200. } else if (!strcasecmp(arg, "arm-usb-ocd-h")) {
  201. ft2232_vid = OLIMEX_VID;
  202. ft2232_type = OLIMEX_ARM_OCD_H_PID;
  203. ft2232_interface = INTERFACE_A;
  204. cs_bits = 0x08;
  205. pindir = 0x1b;
  206. } else if (!strcasecmp(arg, "arm-usb-tiny-h")) {
  207. ft2232_vid = OLIMEX_VID;
  208. ft2232_type = OLIMEX_ARM_TINY_H_PID;
  209. ft2232_interface = INTERFACE_A;
  210. } else if (!strcasecmp(arg, "servo")) {
  211. ft2232_vid = GOOGLE_VID;
  212. ft2232_type = GOOGLE_SERVO_PID;
  213. ft2232_interface = INTERFACE_A;
  214. } else if (!strcasecmp(arg, "servo-v2")) {
  215. ft2232_vid = GOOGLE_VID;
  216. ft2232_type = GOOGLE_SERVO_V2_PID1;
  217. ft2232_interface = INTERFACE_A;
  218. } else if (!strcasecmp(arg, "servo-v2-legacy")) {
  219. ft2232_vid = GOOGLE_VID;
  220. ft2232_type = GOOGLE_SERVO_V2_PID0;
  221. ft2232_interface = INTERFACE_A;
  222. } else {
  223. msg_perr("Error: Invalid device type specified.\n");
  224. free(arg);
  225. return -1;
  226. }
  227. }
  228. free(arg);
  229. arg = extract_programmer_param("port");
  230. if (arg) {
  231. switch (toupper((unsigned char)*arg)) {
  232. case 'A':
  233. ft2232_interface = INTERFACE_A;
  234. break;
  235. case 'B':
  236. ft2232_interface = INTERFACE_B;
  237. break;
  238. default:
  239. msg_perr("Error: Invalid port/interface specified.\n");
  240. free(arg);
  241. return -2;
  242. }
  243. }
  244. free(arg);
  245. arg = extract_programmer_param("spi_mhz");
  246. if (arg) {
  247. spi_mhz = strtod(arg, &endp);
  248. if (arg == endp) {
  249. msg_perr("%s: Invalid clock %s MHz. Will use "
  250. "default\n", __func__, arg);
  251. }
  252. msg_pdbg("Clock %f MHz\n", spi_mhz);
  253. }
  254. free(arg);
  255. msg_pdbg("Using device type %s %s ",
  256. get_ft2232_vendorname(ft2232_vid, ft2232_type),
  257. get_ft2232_devicename(ft2232_vid, ft2232_type));
  258. msg_pdbg("interface %s\n",
  259. (ft2232_interface == INTERFACE_A) ? "A" : "B");
  260. if (ftdi_init(ftdic) < 0) {
  261. msg_perr("ftdi_init failed\n");
  262. return -3;
  263. }
  264. /* Must occur prior to ftdi_usb_open_* call */
  265. if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
  266. msg_perr("Unable to select interface: %s\n",
  267. ftdic->error_str);
  268. }
  269. arg = extract_programmer_param("serial");
  270. f = ftdi_usb_open_desc(ftdic, ft2232_vid, ft2232_type, NULL, arg);
  271. free(arg);
  272. if (f < 0 && f != -5) {
  273. msg_perr("Unable to open FTDI device: %d (%s)\n", f,
  274. ftdi_get_error_string(ftdic));
  275. return -4;
  276. }
  277. if (ftdic->type != TYPE_2232H && ftdic->type != TYPE_4232H) {
  278. msg_pdbg("FTDI chip type %d is not high-speed\n",
  279. ftdic->type);
  280. clock_5x = 0;
  281. }
  282. if (ftdi_usb_reset(ftdic) < 0) {
  283. msg_perr("Unable to reset FTDI device\n");
  284. }
  285. if (ftdi_set_latency_timer(ftdic, 2) < 0) {
  286. msg_perr("Unable to set latency timer\n");
  287. }
  288. if (ftdi_write_data_set_chunksize(ftdic, 256)) {
  289. msg_perr("Unable to set chunk size\n");
  290. }
  291. if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
  292. msg_perr("Unable to set bitmode to SPI\n");
  293. }
  294. if (clock_5x) {
  295. msg_pdbg("Disable divide-by-5 front stage\n");
  296. buf[0] = 0x8a; /* Disable divide-by-5. */
  297. if (send_buf(ftdic, buf, 1)) {
  298. ret = -5;
  299. goto ftdi_err;
  300. }
  301. mpsse_clk = 60.0;
  302. if (spi_mhz)
  303. divide_by = (uint16_t)(mpsse_clk / (spi_mhz * 2));
  304. } else {
  305. mpsse_clk = 12.0;
  306. if (spi_mhz)
  307. divide_by = (uint16_t)((mpsse_clk * 5) / (spi_mhz * 2));
  308. }
  309. if (divide_by < 1) {
  310. msg_perr("Can't set SPI clock to %f MHz, will be %f MHz\n",
  311. spi_mhz, mpsse_clk / 2);
  312. divide_by = 1;
  313. }
  314. msg_pdbg("Set clock divisor\n");
  315. buf[0] = 0x86; /* command "set divisor" */
  316. /* valueL/valueH are (desired_divisor - 1) */
  317. buf[1] = (divide_by - 1) & 0xff;
  318. buf[2] = ((divide_by - 1) >> 8) & 0xff;
  319. if (send_buf(ftdic, buf, 3)) {
  320. ret = -6;
  321. goto ftdi_err;
  322. }
  323. msg_pdbg("MPSSE clock: %f MHz divisor: %d "
  324. "SPI clock: %f MHz\n", mpsse_clk, divide_by,
  325. (double)(mpsse_clk / (((divide_by - 1) + 1) * 2)));
  326. /* Disconnect TDI/DO to TDO/DI for loopback. */
  327. msg_pdbg("No loopback of TDI/DO TDO/DI\n");
  328. buf[0] = 0x85;
  329. if (send_buf(ftdic, buf, 1)) {
  330. ret = -7;
  331. goto ftdi_err;
  332. }
  333. msg_pdbg("Set data bits\n");
  334. buf[0] = SET_BITS_LOW;
  335. buf[1] = cs_bits;
  336. buf[2] = pindir;
  337. if (send_buf(ftdic, buf, 3)) {
  338. ret = -8;
  339. goto ftdi_err;
  340. }
  341. // msg_pdbg("\nft2232 chosen\n");
  342. register_spi_programmer(&spi_programmer_ft2232);
  343. return 0;
  344. ftdi_err:
  345. if ((f = ftdi_usb_close(ftdic)) < 0) {
  346. msg_perr("Unable to close FTDI device: %d (%s)\n", f,
  347. ftdi_get_error_string(ftdic));
  348. }
  349. return ret;
  350. }
  351. /* Returns 0 upon success, a negative number upon errors. */
  352. static int ft2232_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  353. const unsigned char *writearr, unsigned char *readarr)
  354. {
  355. struct ftdi_context *ftdic = &ftdic_context;
  356. static unsigned char *buf = NULL;
  357. /* failed is special. We use bitwise ops, but it is essentially bool. */
  358. int i = 0, ret = 0, failed = 0;
  359. int bufsize;
  360. static int oldbufsize = 0;
  361. if (writecnt > 65536 || readcnt > 65536)
  362. return SPI_INVALID_LENGTH;
  363. /* buf is not used for the response from the chip. */
  364. bufsize = max(writecnt + 9, 260 + 9);
  365. /* Never shrink. realloc() calls are expensive. */
  366. if (bufsize > oldbufsize) {
  367. buf = realloc(buf, bufsize);
  368. if (!buf) {
  369. msg_perr("Out of memory!\n");
  370. /* TODO: What to do with buf? */
  371. return SPI_GENERIC_ERROR;
  372. }
  373. oldbufsize = bufsize;
  374. }
  375. /*
  376. * Minimize USB transfers by packing as many commands as possible
  377. * together. If we're not expecting to read, we can assert CS#, write,
  378. * and deassert CS# all in one shot. If reading, we do three separate
  379. * operations.
  380. */
  381. msg_pspew("Assert CS#\n");
  382. buf[i++] = SET_BITS_LOW;
  383. buf[i++] = 0 & ~cs_bits; /* assertive */
  384. buf[i++] = pindir;
  385. if (writecnt) {
  386. buf[i++] = 0x11;
  387. buf[i++] = (writecnt - 1) & 0xff;
  388. buf[i++] = ((writecnt - 1) >> 8) & 0xff;
  389. memcpy(buf + i, writearr, writecnt);
  390. i += writecnt;
  391. }
  392. /*
  393. * Optionally terminate this batch of commands with a
  394. * read command, then do the fetch of the results.
  395. */
  396. if (readcnt) {
  397. buf[i++] = 0x20;
  398. buf[i++] = (readcnt - 1) & 0xff;
  399. buf[i++] = ((readcnt - 1) >> 8) & 0xff;
  400. ret = send_buf(ftdic, buf, i);
  401. failed = ret;
  402. /* We can't abort here, we still have to deassert CS#. */
  403. if (ret)
  404. msg_perr("send_buf failed before read: %i\n", ret);
  405. i = 0;
  406. if (ret == 0) {
  407. /*
  408. * FIXME: This is unreliable. There's no guarantee that
  409. * we read the response directly after sending the read
  410. * command. We may be scheduled out etc.
  411. */
  412. ret = get_buf(ftdic, readarr, readcnt);
  413. failed |= ret;
  414. /* We can't abort here either. */
  415. if (ret)
  416. msg_perr("get_buf failed: %i\n", ret);
  417. }
  418. }
  419. msg_pspew("De-assert CS#\n");
  420. buf[i++] = SET_BITS_LOW;
  421. buf[i++] = cs_bits;
  422. buf[i++] = pindir;
  423. ret = send_buf(ftdic, buf, i);
  424. failed |= ret;
  425. if (ret)
  426. msg_perr("send_buf failed at end: %i\n", ret);
  427. return failed ? -1 : 0;
  428. }
  429. void print_supported_usbdevs(const struct usbdev_status *devs)
  430. {
  431. int i;
  432. msg_pinfo("USB devices:\n");
  433. for (i = 0; devs[i].vendor_name != NULL; i++) {
  434. msg_pinfo("%s %s [%04x:%04x]%s\n", devs[i].vendor_name,
  435. devs[i].device_name, devs[i].vendor_id,
  436. devs[i].device_id,
  437. (devs[i].status == NT) ? " (untested)" : "");
  438. }
  439. }
  440. #endif