ft2232_spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 <strings.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <ctype.h>
  26. #include "flash.h"
  27. #include "programmer.h"
  28. #include "spi.h"
  29. #include <ftdi.h>
  30. /* This is not defined in libftdi.h <0.20 (c7e4c09e68cfa6f5e112334aa1b3bb23401c8dc7 to be exact).
  31. * Some tests indicate that his is the only change that it is needed to support the FT232H in flashrom. */
  32. #if !defined(HAVE_FT232H)
  33. #define TYPE_232H 6
  34. #endif
  35. /* Please keep sorted by vendor ID, then device ID. */
  36. #define FTDI_VID 0x0403
  37. #define FTDI_FT2232H_PID 0x6010
  38. #define FTDI_FT4232H_PID 0x6011
  39. #define FTDI_FT232H_PID 0x6014
  40. #define TIAO_TUMPA_PID 0x8a98
  41. #define TIAO_TUMPA_LITE_PID 0x8a99
  42. #define AMONTEC_JTAGKEY_PID 0xCFF8
  43. #define GOEPEL_VID 0x096C
  44. #define GOEPEL_PICOTAP_PID 0x1449
  45. #define FIC_VID 0x1457
  46. #define OPENMOKO_DBGBOARD_PID 0x5118
  47. #define OLIMEX_VID 0x15BA
  48. #define OLIMEX_ARM_OCD_PID 0x0003
  49. #define OLIMEX_ARM_TINY_PID 0x0004
  50. #define OLIMEX_ARM_OCD_H_PID 0x002B
  51. #define OLIMEX_ARM_TINY_H_PID 0x002A
  52. #define GOOGLE_VID 0x18D1
  53. #define GOOGLE_SERVO_PID 0x5001
  54. #define GOOGLE_SERVO_V2_PID0 0x5002
  55. #define GOOGLE_SERVO_V2_PID1 0x5003
  56. const struct dev_entry devs_ft2232spi[] = {
  57. {FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"},
  58. {FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"},
  59. {FTDI_VID, FTDI_FT232H_PID, OK, "FTDI", "FT232H"},
  60. {FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
  61. {FTDI_VID, TIAO_TUMPA_LITE_PID, OK, "TIAO", "USB Multi-Protocol Adapter Lite"},
  62. {FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
  63. {GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"},
  64. {GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"},
  65. {GOOGLE_VID, GOOGLE_SERVO_V2_PID0, OK, "Google", "Servo V2 Legacy"},
  66. {GOOGLE_VID, GOOGLE_SERVO_V2_PID1, OK, "Google", "Servo V2"},
  67. {FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC", "OpenMoko Neo1973 Debug board (V2+)"},
  68. {OLIMEX_VID, OLIMEX_ARM_OCD_PID, OK, "Olimex", "ARM-USB-OCD"},
  69. {OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"},
  70. {OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, OK, "Olimex", "ARM-USB-OCD-H"},
  71. {OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, OK, "Olimex", "ARM-USB-TINY-H"},
  72. {0},
  73. };
  74. #define DEFAULT_DIVISOR 2
  75. #define BITMODE_BITBANG_NORMAL 1
  76. #define BITMODE_BITBANG_SPI 2
  77. /* The variables cs_bits and pindir store the values for the "set data bits low byte" MPSSE command that
  78. * sets the initial state and the direction of the I/O pins. The pin offsets are as follows:
  79. * SCK is bit 0.
  80. * DO is bit 1.
  81. * DI is bit 2.
  82. * CS is bit 3.
  83. *
  84. * The default values (set below) are used for most devices:
  85. * value: 0x08 CS=high, DI=low, DO=low, SK=low
  86. * dir: 0x0b CS=output, DI=input, DO=output, SK=output
  87. */
  88. static uint8_t cs_bits = 0x08;
  89. static uint8_t pindir = 0x0b;
  90. static struct ftdi_context ftdic_context;
  91. static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
  92. {
  93. int i;
  94. for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
  95. if ((devs_ft2232spi[i].device_id == ft2232_type) && (devs_ft2232spi[i].vendor_id == ft2232_vid))
  96. return devs_ft2232spi[i].device_name;
  97. }
  98. return "unknown device";
  99. }
  100. static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type)
  101. {
  102. int i;
  103. for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
  104. if ((devs_ft2232spi[i].device_id == ft2232_type) && (devs_ft2232spi[i].vendor_id == ft2232_vid))
  105. return devs_ft2232spi[i].vendor_name;
  106. }
  107. return "unknown vendor";
  108. }
  109. static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf,
  110. int size)
  111. {
  112. int r;
  113. r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
  114. if (r < 0) {
  115. msg_perr("ftdi_write_data: %d, %s\n", r, 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, ftdi_get_error_string(ftdic));
  128. return 1;
  129. }
  130. buf += r;
  131. size -= r;
  132. }
  133. return 0;
  134. }
  135. static int ft2232_spi_send_command(struct flashctx *flash,
  136. unsigned int writecnt, unsigned int readcnt,
  137. const unsigned char *writearr,
  138. unsigned char *readarr);
  139. static const struct spi_master spi_master_ft2232 = {
  140. .type = SPI_CONTROLLER_FT2232,
  141. .max_data_read = 64 * 1024,
  142. .max_data_write = 256,
  143. .command = ft2232_spi_send_command,
  144. .multicommand = default_spi_send_multicommand,
  145. .read = default_spi_read,
  146. .write_256 = default_spi_write_256,
  147. .write_aai = default_spi_write_aai,
  148. };
  149. /* Returns 0 upon success, a negative number upon errors. */
  150. int ft2232_spi_init(void)
  151. {
  152. int ret = 0;
  153. struct ftdi_context *ftdic = &ftdic_context;
  154. unsigned char buf[512];
  155. int ft2232_vid = FTDI_VID;
  156. int ft2232_type = FTDI_FT4232H_PID;
  157. int channel_count = 4; /* Stores the number of channels of the device. */
  158. enum ftdi_interface ft2232_interface = INTERFACE_A;
  159. /*
  160. * The 'H' chips can run with an internal clock of either 12 MHz or 60 MHz,
  161. * but the non-H chips can only run at 12 MHz. We enable the divide-by-5
  162. * prescaler on the former to run on the same speed.
  163. */
  164. uint8_t clock_5x = 1;
  165. /* In addition to the prescaler mentioned above there is also another
  166. * configurable one on all versions of the chips. Its divisor div can be
  167. * set by a 16 bit value x according to the following formula:
  168. * div = (1 + x) * 2 <-> x = div / 2 - 1
  169. * Hence the expressible divisors are all even numbers between 2 and
  170. * 2^17 (=131072) resulting in SCK frequencies of 6 MHz down to about
  171. * 92 Hz for 12 MHz inputs.
  172. */
  173. uint32_t divisor = DEFAULT_DIVISOR;
  174. int f;
  175. char *arg;
  176. double mpsse_clk;
  177. arg = extract_programmer_param("type");
  178. if (arg) {
  179. if (!strcasecmp(arg, "2232H")) {
  180. ft2232_type = FTDI_FT2232H_PID;
  181. channel_count = 2;
  182. } else if (!strcasecmp(arg, "4232H")) {
  183. ft2232_type = FTDI_FT4232H_PID;
  184. channel_count = 4;
  185. } else if (!strcasecmp(arg, "232H")) {
  186. ft2232_type = FTDI_FT232H_PID;
  187. channel_count = 1;
  188. } else if (!strcasecmp(arg, "jtagkey")) {
  189. ft2232_type = AMONTEC_JTAGKEY_PID;
  190. channel_count = 2;
  191. /* JTAGkey(2) needs to enable its output via Bit4 / GPIOL0
  192. * value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low
  193. * dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output */
  194. cs_bits = 0x18;
  195. pindir = 0x1b;
  196. } else if (!strcasecmp(arg, "picotap")) {
  197. ft2232_vid = GOEPEL_VID;
  198. ft2232_type = GOEPEL_PICOTAP_PID;
  199. channel_count = 2;
  200. } else if (!strcasecmp(arg, "tumpa")) {
  201. /* Interface A is SPI1, B is SPI2. */
  202. ft2232_type = TIAO_TUMPA_PID;
  203. channel_count = 2;
  204. } else if (!strcasecmp(arg, "tumpalite")) {
  205. /* Only one channel is used on lite edition */
  206. ft2232_type = TIAO_TUMPA_LITE_PID;
  207. channel_count = 1;
  208. } else if (!strcasecmp(arg, "busblaster")) {
  209. /* In its default configuration it is a jtagkey clone */
  210. ft2232_type = FTDI_FT2232H_PID;
  211. channel_count = 2;
  212. cs_bits = 0x18;
  213. pindir = 0x1b;
  214. } else if (!strcasecmp(arg, "openmoko")) {
  215. ft2232_vid = FIC_VID;
  216. ft2232_type = OPENMOKO_DBGBOARD_PID;
  217. channel_count = 2;
  218. } else if (!strcasecmp(arg, "arm-usb-ocd")) {
  219. ft2232_vid = OLIMEX_VID;
  220. ft2232_type = OLIMEX_ARM_OCD_PID;
  221. channel_count = 2;
  222. /* arm-usb-ocd(-h) has an output buffer that needs to be enabled by pulling ADBUS4 low.
  223. * value: 0x08 #OE=low, CS=high, DI=low, DO=low, SK=low
  224. * dir: 0x1b #OE=output, CS=output, DI=input, DO=output, SK=output */
  225. cs_bits = 0x08;
  226. pindir = 0x1b;
  227. } else if (!strcasecmp(arg, "arm-usb-tiny")) {
  228. ft2232_vid = OLIMEX_VID;
  229. ft2232_type = OLIMEX_ARM_TINY_PID;
  230. channel_count = 2;
  231. } else if (!strcasecmp(arg, "arm-usb-ocd-h")) {
  232. ft2232_vid = OLIMEX_VID;
  233. ft2232_type = OLIMEX_ARM_OCD_H_PID;
  234. channel_count = 2;
  235. /* See arm-usb-ocd */
  236. cs_bits = 0x08;
  237. pindir = 0x1b;
  238. } else if (!strcasecmp(arg, "arm-usb-tiny-h")) {
  239. ft2232_vid = OLIMEX_VID;
  240. ft2232_type = OLIMEX_ARM_TINY_H_PID;
  241. channel_count = 2;
  242. } else if (!strcasecmp(arg, "google-servo")) {
  243. ft2232_vid = GOOGLE_VID;
  244. ft2232_type = GOOGLE_SERVO_PID;
  245. } else if (!strcasecmp(arg, "google-servo-v2")) {
  246. ft2232_vid = GOOGLE_VID;
  247. ft2232_type = GOOGLE_SERVO_V2_PID1;
  248. /* Default divisor is too fast, and chip ID fails */
  249. divisor = 6;
  250. } else if (!strcasecmp(arg, "google-servo-v2-legacy")) {
  251. ft2232_vid = GOOGLE_VID;
  252. ft2232_type = GOOGLE_SERVO_V2_PID0;
  253. } else {
  254. msg_perr("Error: Invalid device type specified.\n");
  255. free(arg);
  256. return -1;
  257. }
  258. }
  259. free(arg);
  260. arg = extract_programmer_param("port");
  261. if (arg) {
  262. switch (toupper((unsigned char)*arg)) {
  263. case 'A':
  264. ft2232_interface = INTERFACE_A;
  265. break;
  266. case 'B':
  267. ft2232_interface = INTERFACE_B;
  268. if (channel_count < 2)
  269. channel_count = -1;
  270. break;
  271. case 'C':
  272. ft2232_interface = INTERFACE_C;
  273. if (channel_count < 3)
  274. channel_count = -1;
  275. break;
  276. case 'D':
  277. ft2232_interface = INTERFACE_D;
  278. if (channel_count < 4)
  279. channel_count = -1;
  280. break;
  281. default:
  282. channel_count = -1;
  283. break;
  284. }
  285. if (channel_count < 0 || strlen(arg) != 1) {
  286. msg_perr("Error: Invalid channel/port/interface specified: \"%s\".\n", arg);
  287. free(arg);
  288. return -2;
  289. }
  290. }
  291. free(arg);
  292. arg = extract_programmer_param("divisor");
  293. if (arg && strlen(arg)) {
  294. unsigned int temp = 0;
  295. char *endptr;
  296. temp = strtoul(arg, &endptr, 10);
  297. if (*endptr || temp < 2 || temp > 131072 || temp & 0x1) {
  298. msg_perr("Error: Invalid SPI frequency divisor specified: \"%s\".\n"
  299. "Valid are even values between 2 and 131072.\n", arg);
  300. free(arg);
  301. return -2;
  302. } else {
  303. divisor = (uint32_t)temp;
  304. }
  305. }
  306. free(arg);
  307. msg_pdbg("Using device type %s %s ",
  308. get_ft2232_vendorname(ft2232_vid, ft2232_type),
  309. get_ft2232_devicename(ft2232_vid, ft2232_type));
  310. msg_pdbg("channel %s.\n",
  311. (ft2232_interface == INTERFACE_A) ? "A" :
  312. (ft2232_interface == INTERFACE_B) ? "B" :
  313. (ft2232_interface == INTERFACE_C) ? "C" : "D");
  314. if (ftdi_init(ftdic) < 0) {
  315. msg_perr("ftdi_init failed.\n");
  316. return -3;
  317. }
  318. if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
  319. msg_perr("Unable to select channel (%s).\n", ftdi_get_error_string(ftdic));
  320. }
  321. arg = extract_programmer_param("serial");
  322. f = ftdi_usb_open_desc(ftdic, ft2232_vid, ft2232_type, NULL, arg);
  323. free(arg);
  324. if (f < 0 && f != -5) {
  325. msg_perr("Unable to open FTDI device: %d (%s).\n", f, ftdi_get_error_string(ftdic));
  326. return -4;
  327. }
  328. if (ftdic->type != TYPE_2232H && ftdic->type != TYPE_4232H && ftdic->type != TYPE_232H) {
  329. msg_pdbg("FTDI chip type %d is not high-speed.\n", ftdic->type);
  330. clock_5x = 0;
  331. }
  332. if (ftdi_usb_reset(ftdic) < 0) {
  333. msg_perr("Unable to reset FTDI device (%s).\n", ftdi_get_error_string(ftdic));
  334. }
  335. if (ftdi_set_latency_timer(ftdic, 2) < 0) {
  336. msg_perr("Unable to set latency timer (%s).\n", ftdi_get_error_string(ftdic));
  337. }
  338. if (ftdi_write_data_set_chunksize(ftdic, 256)) {
  339. msg_perr("Unable to set chunk size (%s).\n", ftdi_get_error_string(ftdic));
  340. }
  341. if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
  342. msg_perr("Unable to set bitmode to SPI (%s).\n", ftdi_get_error_string(ftdic));
  343. }
  344. if (clock_5x) {
  345. msg_pdbg("Disable divide-by-5 front stage\n");
  346. buf[0] = 0x8a; /* Disable divide-by-5. DIS_DIV_5 in newer libftdi */
  347. if (send_buf(ftdic, buf, 1)) {
  348. ret = -5;
  349. goto ftdi_err;
  350. }
  351. mpsse_clk = 60.0;
  352. } else {
  353. mpsse_clk = 12.0;
  354. }
  355. msg_pdbg("Set clock divisor\n");
  356. buf[0] = TCK_DIVISOR;
  357. buf[1] = (divisor / 2 - 1) & 0xff;
  358. buf[2] = ((divisor / 2 - 1) >> 8) & 0xff;
  359. if (send_buf(ftdic, buf, 3)) {
  360. ret = -6;
  361. goto ftdi_err;
  362. }
  363. msg_pdbg("MPSSE clock: %f MHz, divisor: %u, SPI clock: %f MHz\n",
  364. mpsse_clk, divisor, (double)(mpsse_clk / divisor));
  365. /* Disconnect TDI/DO to TDO/DI for loopback. */
  366. msg_pdbg("No loopback of TDI/DO TDO/DI\n");
  367. buf[0] = LOOPBACK_END;
  368. if (send_buf(ftdic, buf, 1)) {
  369. ret = -7;
  370. goto ftdi_err;
  371. }
  372. msg_pdbg("Set data bits\n");
  373. buf[0] = SET_BITS_LOW;
  374. buf[1] = cs_bits;
  375. buf[2] = pindir;
  376. if (send_buf(ftdic, buf, 3)) {
  377. ret = -8;
  378. goto ftdi_err;
  379. }
  380. register_spi_master(&spi_master_ft2232);
  381. return 0;
  382. ftdi_err:
  383. if ((f = ftdi_usb_close(ftdic)) < 0) {
  384. msg_perr("Unable to close FTDI device: %d (%s)\n", f, ftdi_get_error_string(ftdic));
  385. }
  386. return ret;
  387. }
  388. /* Returns 0 upon success, a negative number upon errors. */
  389. static int ft2232_spi_send_command(struct flashctx *flash,
  390. unsigned int writecnt, unsigned int readcnt,
  391. const unsigned char *writearr,
  392. unsigned char *readarr)
  393. {
  394. struct ftdi_context *ftdic = &ftdic_context;
  395. static unsigned char *buf = NULL;
  396. /* failed is special. We use bitwise ops, but it is essentially bool. */
  397. int i = 0, ret = 0, failed = 0;
  398. int bufsize;
  399. static int oldbufsize = 0;
  400. if (writecnt > 65536 || readcnt > 65536)
  401. return SPI_INVALID_LENGTH;
  402. /* buf is not used for the response from the chip. */
  403. bufsize = max(writecnt + 9, 260 + 9);
  404. /* Never shrink. realloc() calls are expensive. */
  405. if (bufsize > oldbufsize) {
  406. buf = realloc(buf, bufsize);
  407. if (!buf) {
  408. msg_perr("Out of memory!\n");
  409. /* TODO: What to do with buf? */
  410. return SPI_GENERIC_ERROR;
  411. }
  412. oldbufsize = bufsize;
  413. }
  414. /*
  415. * Minimize USB transfers by packing as many commands as possible
  416. * together. If we're not expecting to read, we can assert CS#, write,
  417. * and deassert CS# all in one shot. If reading, we do three separate
  418. * operations.
  419. */
  420. msg_pspew("Assert CS#\n");
  421. buf[i++] = SET_BITS_LOW;
  422. buf[i++] = 0 & ~cs_bits; /* assertive */
  423. buf[i++] = pindir;
  424. if (writecnt) {
  425. buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG;
  426. buf[i++] = (writecnt - 1) & 0xff;
  427. buf[i++] = ((writecnt - 1) >> 8) & 0xff;
  428. memcpy(buf + i, writearr, writecnt);
  429. i += writecnt;
  430. }
  431. /*
  432. * Optionally terminate this batch of commands with a
  433. * read command, then do the fetch of the results.
  434. */
  435. if (readcnt) {
  436. buf[i++] = MPSSE_DO_READ;
  437. buf[i++] = (readcnt - 1) & 0xff;
  438. buf[i++] = ((readcnt - 1) >> 8) & 0xff;
  439. ret = send_buf(ftdic, buf, i);
  440. failed = ret;
  441. /* We can't abort here, we still have to deassert CS#. */
  442. if (ret)
  443. msg_perr("send_buf failed before read: %i\n", ret);
  444. i = 0;
  445. if (ret == 0) {
  446. /*
  447. * FIXME: This is unreliable. There's no guarantee that
  448. * we read the response directly after sending the read
  449. * command. We may be scheduled out etc.
  450. */
  451. ret = get_buf(ftdic, readarr, readcnt);
  452. failed |= ret;
  453. /* We can't abort here either. */
  454. if (ret)
  455. msg_perr("get_buf failed: %i\n", ret);
  456. }
  457. }
  458. msg_pspew("De-assert CS#\n");
  459. buf[i++] = SET_BITS_LOW;
  460. buf[i++] = cs_bits;
  461. buf[i++] = pindir;
  462. ret = send_buf(ftdic, buf, i);
  463. failed |= ret;
  464. if (ret)
  465. msg_perr("send_buf failed at end: %i\n", ret);
  466. return failed ? -1 : 0;
  467. }
  468. #endif