dediprog.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2010 Carl-Daniel Hailfinger
  5. * Copyright (C) 2015 Simon Glass
  6. * Copyright (C) 2015 Stefan Tauner
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. //#include "platform.h"
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <limits.h>
  26. #include <errno.h>
  27. #if IS_WINDOWS
  28. #include <lusb0_usb.h>
  29. #else
  30. #include <libusb.h>
  31. #endif
  32. #include "flash.h"
  33. #include "flashchips.h"
  34. #include "chipdrivers.h"
  35. #include "programmer.h"
  36. #include "spi.h"
  37. #define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
  38. #define DEFAULT_TIMEOUT 3000
  39. #define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
  40. #define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
  41. #define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
  42. #define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
  43. #define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
  44. struct libusb_context *usb_ctx;
  45. static libusb_device_handle *dediprog_handle;
  46. static int dediprog_in_endpoint;
  47. static int dediprog_out_endpoint;
  48. static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
  49. enum dediprog_devtype {
  50. DEV_UNKNOWN = 0,
  51. DEV_SF100 = 100,
  52. DEV_SF600 = 600,
  53. };
  54. enum dediprog_devtype dediprog_devicetype;
  55. enum dediprog_leds {
  56. LED_INVALID = -1,
  57. LED_NONE = 0,
  58. LED_PASS = 1 << 0,
  59. LED_BUSY = 1 << 1,
  60. LED_ERROR = 1 << 2,
  61. LED_ALL = 7,
  62. };
  63. /* IO bits for CMD_SET_IO_LED message */
  64. enum dediprog_ios {
  65. IO1 = 1 << 0,
  66. IO2 = 1 << 1,
  67. IO3 = 1 << 2,
  68. IO4 = 1 << 3,
  69. };
  70. enum dediprog_cmds {
  71. CMD_TRANSCEIVE = 0x01,
  72. CMD_POLL_STATUS_REG = 0x02,
  73. CMD_SET_VPP = 0x03,
  74. CMD_SET_TARGET = 0x04,
  75. CMD_READ_EEPROM = 0x05,
  76. CMD_WRITE_EEPROM = 0x06,
  77. CMD_SET_IO_LED = 0x07,
  78. CMD_READ_PROG_INFO = 0x08,
  79. CMD_SET_VCC = 0x09,
  80. CMD_SET_STANDALONE = 0x0A,
  81. CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
  82. CMD_GET_BUTTON = 0x11,
  83. CMD_GET_UID = 0x12,
  84. CMD_SET_CS = 0x14,
  85. CMD_IO_MODE = 0x15,
  86. CMD_FW_UPDATE = 0x1A,
  87. CMD_FPGA_UPDATE = 0x1B,
  88. CMD_READ_FPGA_VERSION = 0x1C,
  89. CMD_SET_HOLD = 0x1D,
  90. CMD_READ = 0x20,
  91. CMD_WRITE = 0x30,
  92. CMD_WRITE_AT45DB = 0x31,
  93. CMD_NAND_WRITE = 0x32,
  94. CMD_NAND_READ = 0x33,
  95. CMD_SET_SPI_CLK = 0x61,
  96. CMD_CHECK_SOCKET = 0x62,
  97. CMD_DOWNLOAD_PRJ = 0x63,
  98. CMD_READ_PRJ_NAME = 0x64,
  99. // New protocol/firmware only
  100. CMD_CHECK_SDCARD = 0x65,
  101. CMD_READ_PRJ = 0x66,
  102. };
  103. enum dediprog_target {
  104. FLASH_TYPE_APPLICATION_FLASH_1 = 0,
  105. FLASH_TYPE_FLASH_CARD,
  106. FLASH_TYPE_APPLICATION_FLASH_2,
  107. FLASH_TYPE_SOCKET,
  108. };
  109. enum dediprog_readmode {
  110. READ_MODE_STD = 1,
  111. READ_MODE_FAST = 2,
  112. READ_MODE_ATMEL45 = 3,
  113. READ_MODE_4B_ADDR_FAST = 4,
  114. READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
  115. };
  116. enum dediprog_writemode {
  117. WRITE_MODE_PAGE_PGM = 1,
  118. WRITE_MODE_PAGE_WRITE = 2,
  119. WRITE_MODE_1B_AAI = 3,
  120. WRITE_MODE_2B_AAI = 4,
  121. WRITE_MODE_128B_PAGE = 5,
  122. WRITE_MODE_PAGE_AT26DF041 = 6,
  123. WRITE_MODE_SILICON_BLUE_FPGA = 7,
  124. WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of length 512 bytes */
  125. WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
  126. WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of length 512 bytes */
  127. WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
  128. WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
  129. };
  130. enum dediprog_standalone_mode {
  131. ENTER_STANDALONE_MODE = 0,
  132. LEAVE_STANDALONE_MODE = 1,
  133. };
  134. #ifndef LIBUSB_HAVE_ERROR_NAME
  135. /* Quick and dirty replacement for missing libusb_error_name in older libusb 1.0. */
  136. const char *libusb_error_name(int error_code)
  137. {
  138. /* 18 chars for text, rest for number, sign, nullbyte. */
  139. static char my_libusb_error[18 + 6];
  140. sprintf(my_libusb_error, "libusb error code %i", error_code);
  141. return my_libusb_error;
  142. }
  143. #endif
  144. /* Returns true if firmware (and thus hardware) supports the "new" protocol */
  145. static int is_new_prot(void)
  146. {
  147. switch (dediprog_devicetype) {
  148. case DEV_SF100:
  149. return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
  150. case DEV_SF600:
  151. return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0);
  152. default:
  153. return 0;
  154. }
  155. }
  156. struct dediprog_transfer_status {
  157. int error; /* OK if 0, ERROR else */
  158. unsigned int queued_idx;
  159. unsigned int finished_idx;
  160. };
  161. static void dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
  162. {
  163. struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
  164. if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
  165. status->error = 1;
  166. msg_perr("SPI bulk read failed!\n");
  167. }
  168. ++status->finished_idx;
  169. }
  170. static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
  171. {
  172. if (status->finished_idx >= status->queued_idx)
  173. return 0;
  174. do {
  175. struct timeval timeout = { 10, 0 };
  176. const int ret = libusb_handle_events_timeout_completed(usb_ctx, &timeout, NULL);
  177. if (ret < 0) {
  178. msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
  179. return 1;
  180. }
  181. } while (finish && (status->finished_idx < status->queued_idx));
  182. return 0;
  183. }
  184. static int dediprog_read_ep(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
  185. {
  186. return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
  187. (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
  188. }
  189. static int dediprog_write_ep(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
  190. {
  191. return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
  192. (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
  193. }
  194. static int dediprog_read_other(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
  195. {
  196. return libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, cmd, value, idx,
  197. (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
  198. }
  199. #if 0
  200. /* Might be useful for other USB devices as well. static for now. */
  201. /* device parameter allows user to specify one device of multiple installed */
  202. static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device)
  203. {
  204. struct usb_bus *bus;
  205. struct usb_device *dev;
  206. for (bus = usb_get_busses(); bus; bus = bus->next)
  207. for (dev = bus->devices; dev; dev = dev->next)
  208. if ((dev->descriptor.idVendor == vid) &&
  209. (dev->descriptor.idProduct == pid)) {
  210. if (device == 0)
  211. return dev;
  212. device--;
  213. }
  214. return NULL;
  215. }
  216. #endif
  217. /* Might be useful for other USB devices as well. static for now. */
  218. /* device parameter allows user to specify one device of multiple installed */
  219. static struct libusb_device_handle *get_device_by_vid_pid_number(uint16_t vid, uint16_t pid, unsigned int num)
  220. {
  221. struct libusb_device **list;
  222. ssize_t i = 0;
  223. int err = 0;
  224. struct libusb_device_handle *handle = NULL;
  225. struct libusb_device_descriptor desc = {};
  226. ssize_t count = libusb_get_device_list(usb_ctx, &list);
  227. if (count < 0) {
  228. msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
  229. return NULL;
  230. }
  231. for (i = 0; i < count; i++) {
  232. struct libusb_device *dev = list[i];
  233. err = libusb_get_device_descriptor(dev, &desc);
  234. if (err != 0) {
  235. msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
  236. libusb_free_device_list(list, 1);
  237. return NULL;
  238. }
  239. if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
  240. msg_pdbg("Found USB device %04hx:%04hx at address %hhx-%hhx.\n", desc.idVendor,
  241. desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  242. if (num == 0) {
  243. err = libusb_open(dev, &handle);
  244. if (err != 0) {
  245. msg_perr("Opening the USB device failed (%s)!\n",
  246. libusb_error_name(err));
  247. libusb_free_device_list(list, 1);
  248. return NULL;
  249. }
  250. break;
  251. }
  252. num--;
  253. }
  254. }
  255. libusb_free_device_list(list, 1);
  256. return handle;
  257. }
  258. /* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
  259. static int dediprog_set_leds(int leds)
  260. {
  261. if (leds < LED_NONE || leds > LED_ALL)
  262. leds = LED_ALL;
  263. /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
  264. * them around if we have an old device. On those devices the LEDs map as follows:
  265. * bit 2 == 0: green light is on.
  266. * bit 0 == 0: red light is on.
  267. *
  268. * Additionally, the command structure has changed with the "new" protocol.
  269. *
  270. * FIXME: take IO pins into account
  271. */
  272. int target_leds, ret;
  273. if (is_new_prot()) {
  274. target_leds = (leds ^ 7) << 8;
  275. ret = dediprog_write_ep(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
  276. } else {
  277. if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
  278. target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
  279. } else {
  280. target_leds = leds;
  281. }
  282. target_leds ^= 7;
  283. ret = dediprog_write_ep(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
  284. }
  285. if (ret != 0x0) {
  286. msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. struct dediprog_spispeeds {
  292. const char *const name;
  293. const int speed;
  294. };
  295. static const struct dediprog_spispeeds spispeeds[] = {
  296. { "24M", 0x0 },
  297. { "12M", 0x2 },
  298. { "8M", 0x1 },
  299. { "3M", 0x3 },
  300. { "2.18M", 0x4 },
  301. { "1.5M", 0x5 },
  302. { "750k", 0x6 },
  303. { "375k", 0x7 },
  304. { NULL, 0x0 },
  305. };
  306. static int dediprog_set_spi_speed(unsigned int spispeed_idx)
  307. {
  308. if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
  309. msg_pinfo("Skipping to set SPI speed because firmware is too old.\n");
  310. return 0;
  311. }
  312. const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
  313. msg_pdbg("SPI speed is %sHz\n", spispeed->name);
  314. int ret = dediprog_write_ep(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
  315. if (ret != 0x0) {
  316. msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
  317. return 1;
  318. }
  319. return 0;
  320. }
  321. /* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
  322. * @start start address
  323. * @len length
  324. * @return 0 on success, 1 on failure
  325. */
  326. static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
  327. unsigned int start, unsigned int len)
  328. {
  329. int ret, err = 1;
  330. unsigned int i;
  331. /* chunksize must be 512, other sizes will NOT work at all. */
  332. const unsigned int chunksize = 0x200;
  333. const unsigned int count = len / chunksize;
  334. unsigned int cmd_len;
  335. struct dediprog_transfer_status status = { 0, 0, 0 };
  336. struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
  337. struct libusb_transfer *transfer;
  338. if ((start % chunksize) || (len % chunksize)) {
  339. msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
  340. "at flashrom@flashrom.org\n", __func__, start, len);
  341. return 1;
  342. }
  343. /* No idea if the hardware can handle empty reads, so chicken out. */
  344. if (!len)
  345. return 0;
  346. /* Command Read SPI Bulk. */
  347. if (is_new_prot()) {
  348. const uint8_t read_cmd[] = {
  349. count & 0xff,
  350. (count >> 8) & 0xff,
  351. 0,
  352. READ_MODE_FAST,
  353. 0,
  354. 0,
  355. start & 0xff,
  356. (start >> 8) & 0xff,
  357. (start >> 16) & 0xff,
  358. (start >> 24) & 0xff,
  359. };
  360. cmd_len = sizeof(read_cmd);
  361. ret = dediprog_write_ep(CMD_READ, 0, 0, read_cmd, cmd_len);
  362. } else {
  363. const uint8_t read_cmd[] = {count & 0xff,
  364. (count >> 8) & 0xff,
  365. chunksize & 0xff,
  366. (chunksize >> 8) & 0xff};
  367. cmd_len = sizeof(read_cmd);
  368. ret = dediprog_write_ep(CMD_READ, start % 0x10000, start / 0x10000, read_cmd, cmd_len);
  369. }
  370. if (ret != cmd_len) {
  371. msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
  372. return 1;
  373. }
  374. /*
  375. * Ring buffer of bulk transfers.
  376. * Poll until at least one transfer is ready,
  377. * schedule next transfers until buffer is full.
  378. */
  379. /* Allocate bulk transfers. */
  380. for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
  381. transfers[i] = libusb_alloc_transfer(0);
  382. if (!transfers[i]) {
  383. msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
  384. goto _err_free;
  385. }
  386. }
  387. /* Now transfer requested chunks using libusb's asynchronous interface. */
  388. while (!status.error && (status.queued_idx < count)) {
  389. while ((status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS) {
  390. transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
  391. libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
  392. (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
  393. dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
  394. transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
  395. ret = libusb_submit_transfer(transfer);
  396. if (ret < 0) {
  397. msg_perr("Submitting SPI bulk read %i failed: %i %s!\n",
  398. status.queued_idx, ret, libusb_error_name(ret));
  399. goto _err_free;
  400. }
  401. ++status.queued_idx;
  402. }
  403. if (dediprog_bulk_read_poll(&status, 0))
  404. goto _err_free;
  405. }
  406. /* Wait for transfers to finish. */
  407. if (dediprog_bulk_read_poll(&status, 1))
  408. goto _err_free;
  409. /* Check if everything has been transmitted. */
  410. if ((status.finished_idx < count) || status.error)
  411. goto _err_free;
  412. err = 0;
  413. _err_free:
  414. dediprog_bulk_read_poll(&status, 1);
  415. for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
  416. if (transfers[i]) libusb_free_transfer(transfers[i]);
  417. return err;
  418. }
  419. static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
  420. unsigned int start, unsigned int len)
  421. {
  422. int ret;
  423. /* chunksize must be 512, other sizes will NOT work at all. */
  424. const unsigned int chunksize = 0x200;
  425. unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
  426. unsigned int bulklen;
  427. dediprog_set_leds(LED_BUSY);
  428. if (residue) {
  429. msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
  430. start, residue);
  431. ret = spi_read_chunked(flash, buf, start, residue, 16);
  432. if (ret)
  433. goto err;
  434. }
  435. /* Round down. */
  436. bulklen = (len - residue) / chunksize * chunksize;
  437. ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
  438. bulklen);
  439. if (ret)
  440. goto err;
  441. len -= residue + bulklen;
  442. if (len) {
  443. msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
  444. start, len);
  445. ret = spi_read_chunked(flash, buf + residue + bulklen,
  446. start + residue + bulklen, len, 16);
  447. if (ret)
  448. goto err;
  449. }
  450. dediprog_set_leds(LED_PASS);
  451. return 0;
  452. err:
  453. dediprog_set_leds(LED_ERROR);
  454. return ret;
  455. }
  456. /* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
  457. * @chunksize length of data chunks, only 256 supported by now
  458. * @start start address
  459. * @len length
  460. * @dedi_spi_cmd dediprog specific write command for spi bus
  461. * @return 0 on success, 1 on failure
  462. */
  463. static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
  464. unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
  465. {
  466. int ret, transferred;
  467. unsigned int i;
  468. /* USB transfer size must be 512, other sizes will NOT work at all.
  469. * chunksize is the real data size per USB bulk transfer. The remaining
  470. * space in a USB bulk transfer must be filled with 0xff padding.
  471. */
  472. const unsigned int count = len / chunksize;
  473. const unsigned char count_and_cmd_old[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
  474. const unsigned char count_and_cmd_new[] = {
  475. count & 0xff,
  476. (count >> 8) & 0xff,
  477. 0, /* used for 24-bit address support? */
  478. dedi_spi_cmd,
  479. JEDEC_BYTE_PROGRAM, /* FIXME: needs to be determined based on byte 3? */
  480. 0,
  481. start & 0xff,
  482. (start >> 8) & 0xff,
  483. (start >> 16) & 0xff,
  484. (start >> 24) & 0xff,
  485. };
  486. unsigned char usbbuf[512];
  487. /*
  488. * We should change this check to
  489. * chunksize > 512
  490. * once we know how to handle different chunk sizes.
  491. */
  492. if (chunksize != 256) {
  493. msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
  494. "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
  495. return 1;
  496. }
  497. if ((start % chunksize) || (len % chunksize)) {
  498. msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
  499. "at flashrom@flashrom.org\n", __func__, start, len);
  500. return 1;
  501. }
  502. /* No idea if the hardware can handle empty writes, so chicken out. */
  503. if (!len)
  504. return 0;
  505. if (!is_new_prot()) {
  506. /* Command Write SPI Bulk. No idea which write command is used on the
  507. * SPI side.
  508. */
  509. ret = dediprog_write_ep(CMD_WRITE, start % 0x10000, start / 0x10000,
  510. count_and_cmd_old, sizeof(count_and_cmd_old));
  511. if (ret != sizeof(count_and_cmd_old)) {
  512. msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
  513. libusb_error_name(ret));
  514. return 1;
  515. }
  516. } else {
  517. /* Command Write SPI Bulk. No idea which write command is used on the
  518. * SPI side.
  519. */
  520. ret = dediprog_write_ep(CMD_WRITE, 0, 0,
  521. count_and_cmd_new, sizeof(count_and_cmd_new));
  522. if (ret != sizeof(count_and_cmd_new)) {
  523. msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
  524. libusb_error_name(ret));
  525. return 1;
  526. }
  527. }
  528. for (i = 0; i < count; i++) {
  529. memset(usbbuf, 0xff, sizeof(usbbuf));
  530. memcpy(usbbuf, buf + i * chunksize, chunksize);
  531. ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint,
  532. usbbuf, 512, &transferred, DEFAULT_TIMEOUT);
  533. if ((ret < 0) || (transferred != 512)) {
  534. msg_perr("SPI bulk write failed, expected %i, got %i %s!\n",
  535. 512, ret, libusb_error_name(ret));
  536. return 1;
  537. }
  538. }
  539. return 0;
  540. }
  541. static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
  542. unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
  543. {
  544. int ret;
  545. const unsigned int chunksize = flash->page_size;
  546. unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
  547. unsigned int bulklen;
  548. dediprog_set_leds(LED_BUSY);
  549. if (chunksize != 256) {
  550. msg_pdbg("Page sizes other than 256 bytes are unsupported as "
  551. "we don't know how dediprog\nhandles them.\n");
  552. /* Write everything like it was residue. */
  553. residue = len;
  554. }
  555. if (residue) {
  556. msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
  557. start, residue);
  558. /* No idea about the real limit. Maybe 12, maybe more. */
  559. ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 12);
  560. if (ret) {
  561. dediprog_set_leds(LED_ERROR);
  562. return ret;
  563. }
  564. }
  565. /* Round down. */
  566. bulklen = (len - residue) / chunksize * chunksize;
  567. ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
  568. if (ret) {
  569. dediprog_set_leds(LED_ERROR);
  570. return ret;
  571. }
  572. len -= residue + bulklen;
  573. if (len) {
  574. msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
  575. start, len);
  576. ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen),
  577. start + residue + bulklen, len, 12);
  578. if (ret) {
  579. dediprog_set_leds(LED_ERROR);
  580. return ret;
  581. }
  582. }
  583. dediprog_set_leds(LED_PASS);
  584. return 0;
  585. }
  586. //static int dediprog_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
  587. static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  588. {
  589. return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
  590. }
  591. #if 0
  592. static int dediprog_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
  593. {
  594. return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
  595. }
  596. #endif
  597. //static int dediprog_spi_send_command(struct flashctx *flash,
  598. static int dediprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
  599. unsigned int readcnt,
  600. const unsigned char *writearr,
  601. unsigned char *readarr)
  602. {
  603. int ret;
  604. msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
  605. if (writecnt > UINT16_MAX) {
  606. msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
  607. return 1;
  608. }
  609. if (readcnt > UINT16_MAX) {
  610. msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
  611. return 1;
  612. }
  613. /* New protocol has the read flag as value while the old protocol had it in the index field. */
  614. if (is_new_prot()) {
  615. ret = dediprog_write_ep(CMD_TRANSCEIVE, readcnt ? 1 : 0, 0, writearr, writecnt);
  616. } else {
  617. ret = dediprog_write_ep(CMD_TRANSCEIVE, 0, readcnt ? 1 : 0, writearr, writecnt);
  618. }
  619. if (ret != writecnt) {
  620. msg_perr("Send SPI failed, expected %i, got %i %s!\n",
  621. writecnt, ret, libusb_error_name(ret));
  622. return 1;
  623. }
  624. if (readcnt == 0)
  625. return 0;
  626. ret = dediprog_read_ep(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
  627. if (ret != readcnt) {
  628. msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
  629. readcnt, ret, libusb_error_name(ret));
  630. return 1;
  631. }
  632. return 0;
  633. }
  634. static int dediprog_check_devicestring(void)
  635. {
  636. int ret;
  637. int fw[3];
  638. int sfnum;
  639. char buf[0x11];
  640. /* Command Receive Device String. */
  641. ret = dediprog_read_ep(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
  642. if (ret != 0x10) {
  643. msg_perr("Incomplete/failed Command Receive Device String!\n");
  644. return 1;
  645. }
  646. buf[0x10] = '\0';
  647. msg_pdbg("Found a %s\n", buf);
  648. if (memcmp(buf, "SF100", 0x5) == 0)
  649. dediprog_devicetype = DEV_SF100;
  650. else if (memcmp(buf, "SF600", 0x5) == 0)
  651. dediprog_devicetype = DEV_SF600;
  652. else {
  653. msg_perr("Device not a SF100 or SF600!\n");
  654. return 1;
  655. }
  656. if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2])
  657. != 4 || sfnum != dediprog_devicetype) {
  658. msg_perr("Unexpected firmware version string '%s'\n", buf);
  659. return 1;
  660. }
  661. /* Only these major versions were tested. */
  662. if (fw[0] < 2 || fw[0] > 7) {
  663. msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
  664. return 1;
  665. }
  666. dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
  667. return 0;
  668. }
  669. static int dediprog_supply_voltages[] = {
  670. 0, 1800, 2500, 3500,
  671. };
  672. static int dediprog_set_spi_flash_voltage_manual(int millivolt)
  673. {
  674. int ret;
  675. uint16_t voltage_selector;
  676. switch (millivolt) {
  677. case 0:
  678. /* Admittedly this one is an assumption. */
  679. voltage_selector = 0x0;
  680. break;
  681. case 1800:
  682. voltage_selector = 0x12;
  683. break;
  684. case 2500:
  685. voltage_selector = 0x11;
  686. break;
  687. case 3500:
  688. voltage_selector = 0x10;
  689. break;
  690. default:
  691. msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
  692. return 1;
  693. }
  694. msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
  695. millivolt % 1000);
  696. if (voltage_selector == 0) {
  697. /* Wait some time as the original driver does. */
  698. programmer_delay(200 * 1000);
  699. }
  700. ret = dediprog_write_ep(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
  701. if (ret != 0x0) {
  702. msg_perr("Command Set SPI Voltage 0x%x failed!\n",
  703. voltage_selector);
  704. return 1;
  705. }
  706. if (voltage_selector != 0) {
  707. /* Wait some time as the original driver does. */
  708. programmer_delay(200 * 1000);
  709. }
  710. return 0;
  711. }
  712. static int dediprog_set_spi_flash_voltage_auto(void)
  713. {
  714. int i;
  715. int spi_flash_ranges;
  716. spi_flash_ranges = flash_supported_voltage_ranges(BUS_SPI);
  717. if (spi_flash_ranges < 0)
  718. return -1;
  719. for (i = 0; i < ARRAY_SIZE(dediprog_supply_voltages); i++) {
  720. int j;
  721. int v = dediprog_supply_voltages[i]; /* shorthand */
  722. for (j = 0; j < spi_flash_ranges; j++) {
  723. /* Dediprog can supply a voltage in this range. */
  724. if ((v >= voltage_ranges[j].min) && (v <= voltage_ranges[j].max)) {
  725. struct flashctx dummy;
  726. msg_cdbg("%s: trying %d\n", __func__, v);
  727. if (dediprog_set_spi_flash_voltage_manual(v)) {
  728. msg_cerr("%s: Failed to set SPI voltage\n", __func__);
  729. return 1;
  730. }
  731. clear_spi_id_cache();
  732. if (probe_flash(0, &dummy, 0) < 0) {
  733. /* No dice, try next voltage supported by Dediprog. */
  734. break;
  735. }
  736. if ((dummy.manufacture_id == GENERIC_MANUF_ID) ||
  737. (dummy.model_id == GENERIC_DEVICE_ID)) {
  738. break;
  739. }
  740. return 0;
  741. }
  742. }
  743. }
  744. return 1;
  745. }
  746. /* FIXME: ugly function signature */
  747. static int dediprog_set_spi_voltage(int millivolt, int probe)
  748. {
  749. if (probe)
  750. return dediprog_set_spi_flash_voltage_auto();
  751. else
  752. return dediprog_set_spi_flash_voltage_manual(millivolt);
  753. }
  754. /*
  755. * This command presumably sets the voltage for the SF100 itself (not the
  756. * SPI flash). Only use this command with firmware older than V6.0.0. Newer
  757. * (including all SF600s) do not support it.
  758. */
  759. static int dediprog_set_voltage(void)
  760. {
  761. int ret;
  762. unsigned char buf[0x1];
  763. memset(buf, 0, sizeof(buf));
  764. ret = dediprog_read_other(CMD_SET_VOLTAGE, 0x0, 0x0, buf, 0x1);
  765. if (ret < 0) {
  766. msg_perr("Command A failed (%s)!\n", libusb_error_name(ret));
  767. return 1;
  768. }
  769. if ((ret != 0x1) || (buf[0] != 0x6f)) {
  770. msg_perr("Unexpected response to init!\n");
  771. return 1;
  772. }
  773. return 0;
  774. }
  775. static int dediprog_leave_standalone_mode(void)
  776. {
  777. int ret;
  778. if (dediprog_devicetype != DEV_SF600)
  779. return 0;
  780. msg_pdbg("Leaving standalone mode\n");
  781. ret = dediprog_write_ep(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
  782. if (ret) {
  783. msg_perr("Failed to leave standalone mode (%s)!\n", libusb_error_name(ret));
  784. return 1;
  785. }
  786. return 0;
  787. }
  788. static int set_target_flash(enum dediprog_target target)
  789. {
  790. int ret = dediprog_write_ep(CMD_SET_TARGET, target, 0, NULL, 0);
  791. if (ret != 0) {
  792. msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
  793. return 1;
  794. }
  795. return 0;
  796. }
  797. #if 0
  798. /* Returns true if the button is currently pressed. */
  799. static bool dediprog_get_button(void)
  800. {
  801. char buf[1];
  802. int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
  803. buf, 0x1, DEFAULT_TIMEOUT);
  804. if (ret != 0) {
  805. msg_perr("Could not get button state (%s)!\n", usb_strerror());
  806. return 1;
  807. }
  808. return buf[0] != 1;
  809. }
  810. #endif
  811. static int parse_voltage(char *voltage)
  812. {
  813. char *tmp = NULL;
  814. int i;
  815. int millivolt = 0, fraction = 0;
  816. if (!voltage || !strlen(voltage)) {
  817. msg_perr("Empty voltage= specified.\n");
  818. return -1;
  819. }
  820. millivolt = (int)strtol(voltage, &tmp, 0);
  821. voltage = tmp;
  822. /* Handle "," and "." as decimal point. Everything after it is assumed
  823. * to be in decimal notation.
  824. */
  825. if ((*voltage == '.') || (*voltage == ',')) {
  826. voltage++;
  827. for (i = 0; i < 3; i++) {
  828. fraction *= 10;
  829. /* Don't advance if the current character is invalid,
  830. * but continue multiplying.
  831. */
  832. if ((*voltage < '0') || (*voltage > '9'))
  833. continue;
  834. fraction += *voltage - '0';
  835. voltage++;
  836. }
  837. /* Throw away remaining digits. */
  838. voltage += strspn(voltage, "0123456789");
  839. }
  840. /* The remaining string must be empty or "mV" or "V". */
  841. tolower_string(voltage);
  842. /* No unit or "V". */
  843. if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
  844. millivolt *= 1000;
  845. millivolt += fraction;
  846. } else if (!strncmp(voltage, "mv", 2) ||
  847. !strncmp(voltage, "milliv", 6)) {
  848. /* No adjustment. fraction is discarded. */
  849. } else {
  850. /* Garbage at the end of the string. */
  851. msg_perr("Garbage voltage= specified.\n");
  852. return -1;
  853. }
  854. return millivolt;
  855. }
  856. #if 0
  857. static const struct spi_master spi_master_dediprog = {
  858. .type = SPI_CONTROLLER_DEDIPROG,
  859. .max_data_read = MAX_DATA_UNSPECIFIED,
  860. .max_data_write = MAX_DATA_UNSPECIFIED,
  861. .command = dediprog_spi_send_command,
  862. .multicommand = default_spi_send_multicommand,
  863. .read = dediprog_spi_read,
  864. .write_256 = dediprog_spi_write_256,
  865. .write_aai = dediprog_spi_write_aai,
  866. };
  867. #endif
  868. static const struct spi_programmer spi_programmer_dediprog = {
  869. .type = SPI_CONTROLLER_DEDIPROG,
  870. .max_data_read = MAX_DATA_UNSPECIFIED,
  871. .max_data_write = MAX_DATA_UNSPECIFIED,
  872. .command = dediprog_spi_send_command,
  873. .multicommand = default_spi_send_multicommand,
  874. .read = dediprog_spi_read,
  875. .write_256 = dediprog_spi_write_256,
  876. };
  877. static int dediprog_shutdown(void *data)
  878. {
  879. msg_pspew("%s\n", __func__);
  880. dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
  881. dediprog_devicetype = DEV_UNKNOWN;
  882. /* URB 28. Command Set SPI Voltage to 0. */
  883. if (dediprog_set_spi_voltage(0x0, 0))
  884. return 1;
  885. if (libusb_release_interface(dediprog_handle, 0)) {
  886. msg_perr("Could not release USB interface!\n");
  887. return 1;
  888. }
  889. libusb_close(dediprog_handle);
  890. libusb_exit(usb_ctx);
  891. return 0;
  892. }
  893. /* URB numbers refer to the first log ever captured. */
  894. int dediprog_init(void)
  895. {
  896. char *voltage, *device, *spispeed, *target_str;
  897. int spispeed_idx = 1;
  898. int millivolt = 0;
  899. long usedevice = 0;
  900. long target = 1;
  901. int i, ret;
  902. msg_pspew("%s\n", __func__);
  903. spispeed = extract_programmer_param("spispeed");
  904. if (spispeed) {
  905. for (i = 0; spispeeds[i].name; ++i) {
  906. if (!strcasecmp(spispeeds[i].name, spispeed)) {
  907. spispeed_idx = i;
  908. break;
  909. }
  910. }
  911. if (!spispeeds[i].name) {
  912. msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
  913. free(spispeed);
  914. return 1;
  915. }
  916. free(spispeed);
  917. }
  918. voltage = extract_programmer_param("voltage");
  919. if (voltage) {
  920. millivolt = parse_voltage(voltage);
  921. free(voltage);
  922. if (millivolt < 0)
  923. return 1;
  924. msg_pinfo("Setting voltage to %i mV\n", millivolt);
  925. }
  926. device = extract_programmer_param("device");
  927. if (device) {
  928. char *dev_suffix;
  929. errno = 0;
  930. usedevice = strtol(device, &dev_suffix, 10);
  931. if (errno != 0 || device == dev_suffix) {
  932. msg_perr("Error: Could not convert 'device'.\n");
  933. free(device);
  934. return 1;
  935. }
  936. if (usedevice < 0 || usedevice > UINT_MAX) {
  937. msg_perr("Error: Value for 'device' is out of range.\n");
  938. free(device);
  939. return 1;
  940. }
  941. if (strlen(dev_suffix) > 0) {
  942. msg_perr("Error: Garbage following 'device' value.\n");
  943. free(device);
  944. return 1;
  945. }
  946. msg_pinfo("Using device %li.\n", usedevice);
  947. }
  948. free(device);
  949. target_str = extract_programmer_param("target");
  950. if (target_str) {
  951. char *target_suffix;
  952. errno = 0;
  953. target = strtol(target_str, &target_suffix, 10);
  954. if (errno != 0 || target_str == target_suffix) {
  955. msg_perr("Error: Could not convert 'target'.\n");
  956. free(target_str);
  957. return 1;
  958. }
  959. if (target < 1 || target > 2) {
  960. msg_perr("Error: Value for 'target' is out of range.\n");
  961. free(target_str);
  962. return 1;
  963. }
  964. if (strlen(target_suffix) > 0) {
  965. msg_perr("Error: Garbage following 'target' value.\n");
  966. free(target_str);
  967. return 1;
  968. }
  969. msg_pinfo("Using target %li.\n", target);
  970. }
  971. free(target_str);
  972. /* Here comes the USB stuff. */
  973. libusb_init(&usb_ctx);
  974. if (!usb_ctx) {
  975. msg_perr("Could not initialize libusb!\n");
  976. return 1;
  977. }
  978. dediprog_handle = get_device_by_vid_pid_number(0x0483, 0xdada, (unsigned int) usedevice);
  979. if (!dediprog_handle) {
  980. msg_perr("Could not find a Dediprog programmer on USB!\n");
  981. libusb_exit(usb_ctx);
  982. return 1;
  983. }
  984. ret = libusb_set_configuration(dediprog_handle, 1);
  985. if (ret != 0) {
  986. msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
  987. libusb_close(dediprog_handle);
  988. libusb_exit(usb_ctx);
  989. return 1;
  990. }
  991. ret = libusb_claim_interface(dediprog_handle, 0);
  992. if (ret < 0) {
  993. msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
  994. libusb_close(dediprog_handle);
  995. libusb_exit(usb_ctx);
  996. return 1;
  997. }
  998. if (register_shutdown(dediprog_shutdown, NULL))
  999. return 1;
  1000. /* Try reading the devicestring. If that fails and the device is old
  1001. * (FW < 6.0.0) then we need to try the "set voltage" command and then
  1002. * attempt to read the devicestring again. */
  1003. if (dediprog_check_devicestring()) {
  1004. if (dediprog_set_voltage())
  1005. return 1;
  1006. if (dediprog_check_devicestring())
  1007. return 1;
  1008. }
  1009. /* SF100 firmware exposes only one endpoint for in/out, SF600 firmware
  1010. exposes separate endpoints for in and out. */
  1011. dediprog_in_endpoint = 2;
  1012. if (dediprog_devicetype == DEV_SF100)
  1013. dediprog_out_endpoint = 2;
  1014. else
  1015. dediprog_out_endpoint = 1;
  1016. /* Set some LEDs as soon as possible to indicate activity.
  1017. * Because knowing the firmware version is required to set the LEDs correctly we need to this after
  1018. * dediprog_setup() has queried the device and set dediprog_firmwareversion. */
  1019. dediprog_set_leds(LED_PASS | LED_BUSY);
  1020. /* FIXME: need to do this so buses_supported gets SPI */
  1021. register_spi_programmer(&spi_programmer_dediprog);
  1022. /* Select target/socket, frequency and VCC. */
  1023. if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
  1024. dediprog_set_spi_speed(spispeed_idx) ||
  1025. dediprog_set_spi_voltage(millivolt, voltage ? 0 : 1)) {
  1026. dediprog_set_leds(LED_ERROR);
  1027. return 1;
  1028. }
  1029. if (dediprog_leave_standalone_mode())
  1030. return 1;
  1031. dediprog_set_leds(LED_NONE);
  1032. return 0;
  1033. }