blob: b34338d2111a0aa1af868a89b90281810210b163 [file] [log] [blame]
hailfingerdfb32a02010-01-19 11:15:48 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
David Hendricksfe05e742015-08-15 17:29:39 -07005 * Copyright (C) 2015 Simon Glass
6 * Copyright (C) 2015 Stefan Tauner
hailfingerdfb32a02010-01-19 11:15:48 +00007 *
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.
hailfingerdfb32a02010-01-19 11:15:48 +000016 */
17
Patrick Georgibd31cdd2017-02-03 19:16:12 +010018#include "platform.h"
David Hendricksfe05e742015-08-15 17:29:39 -070019
20#include <stdlib.h>
hailfinger7a009082010-11-09 23:30:43 +000021#include <stdio.h>
hailfingerdfb32a02010-01-19 11:15:48 +000022#include <string.h>
David Hendricksfe05e742015-08-15 17:29:39 -070023#include <limits.h>
24#include <errno.h>
25
26#if IS_WINDOWS
27#include <lusb0_usb.h>
28#else
David Hendricksf9ecb452015-11-04 15:40:27 -080029#include <libusb.h>
David Hendricksfe05e742015-08-15 17:29:39 -070030#endif
31
hailfingerdfb32a02010-01-19 11:15:48 +000032#include "flash.h"
David Hendrickscd20ea42015-11-17 22:33:35 -080033#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000034#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000035#include "programmer.h"
hailfingerdfb32a02010-01-19 11:15:48 +000036#include "spi.h"
37
stepan4c06de72011-01-28 09:00:15 +000038#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
hailfingerdfb32a02010-01-19 11:15:48 +000039#define DEFAULT_TIMEOUT 3000
David Hendricksf9ecb452015-11-04 15:40:27 -080040#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
41#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
42#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
43#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
44#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
45struct libusb_context *usb_ctx;
46static libusb_device_handle *dediprog_handle;
David Hendricksd7468582015-11-12 15:21:12 -080047static int dediprog_in_endpoint;
48static int dediprog_out_endpoint;
David Hendricksf9ecb452015-11-04 15:40:27 -080049static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
hailfingerdfb32a02010-01-19 11:15:48 +000050
David Hendricks6702e072015-08-15 18:09:04 -070051enum dediprog_devtype {
52 DEV_UNKNOWN = 0,
53 DEV_SF100 = 100,
54 DEV_SF600 = 600,
55};
56
57enum dediprog_devtype dediprog_devicetype;
58
David Hendricksfe05e742015-08-15 17:29:39 -070059enum dediprog_leds {
60 LED_INVALID = -1,
61 LED_NONE = 0,
Simon Glasse53cc1d2015-01-08 05:48:51 -070062 LED_PASS = 1 << 0,
63 LED_BUSY = 1 << 1,
64 LED_ERROR = 1 << 2,
65 LED_ALL = 7,
66};
67
Simon Glassa1f80682015-01-08 05:55:29 -070068/* IO bits for CMD_SET_IO_LED message */
David Hendricksfe05e742015-08-15 17:29:39 -070069enum dediprog_ios {
Simon Glassa1f80682015-01-08 05:55:29 -070070 IO1 = 1 << 0,
71 IO2 = 1 << 1,
72 IO3 = 1 << 2,
73 IO4 = 1 << 3,
74};
75
David Hendricksfe05e742015-08-15 17:29:39 -070076enum dediprog_cmds {
77 CMD_TRANSCEIVE = 0x01,
78 CMD_POLL_STATUS_REG = 0x02,
79 CMD_SET_VPP = 0x03,
80 CMD_SET_TARGET = 0x04,
81 CMD_READ_EEPROM = 0x05,
82 CMD_WRITE_EEPROM = 0x06,
83 CMD_SET_IO_LED = 0x07,
84 CMD_READ_PROG_INFO = 0x08,
85 CMD_SET_VCC = 0x09,
86 CMD_SET_STANDALONE = 0x0A,
David Hendricks28fbdfb2015-08-15 18:04:37 -070087 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
David Hendricksfe05e742015-08-15 17:29:39 -070088 CMD_GET_BUTTON = 0x11,
89 CMD_GET_UID = 0x12,
90 CMD_SET_CS = 0x14,
91 CMD_IO_MODE = 0x15,
92 CMD_FW_UPDATE = 0x1A,
93 CMD_FPGA_UPDATE = 0x1B,
94 CMD_READ_FPGA_VERSION = 0x1C,
95 CMD_SET_HOLD = 0x1D,
96 CMD_READ = 0x20,
97 CMD_WRITE = 0x30,
98 CMD_WRITE_AT45DB = 0x31,
99 CMD_NAND_WRITE = 0x32,
100 CMD_NAND_READ = 0x33,
101 CMD_SET_SPI_CLK = 0x61,
102 CMD_CHECK_SOCKET = 0x62,
103 CMD_DOWNLOAD_PRJ = 0x63,
104 CMD_READ_PRJ_NAME = 0x64,
105 // New protocol/firmware only
106 CMD_CHECK_SDCARD = 0x65,
107 CMD_READ_PRJ = 0x66,
Simon Glassa1f80682015-01-08 05:55:29 -0700108};
109
David Hendricksfe05e742015-08-15 17:29:39 -0700110enum dediprog_target {
111 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
112 FLASH_TYPE_FLASH_CARD,
113 FLASH_TYPE_APPLICATION_FLASH_2,
114 FLASH_TYPE_SOCKET,
Simon Glassa1f80682015-01-08 05:55:29 -0700115};
116
David Hendricksfe05e742015-08-15 17:29:39 -0700117enum dediprog_readmode {
118 READ_MODE_STD = 1,
119 READ_MODE_FAST = 2,
120 READ_MODE_ATMEL45 = 3,
121 READ_MODE_4B_ADDR_FAST = 4,
122 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Simon Glassd01002b2015-01-08 05:44:16 -0700123};
124
David Hendricksfe05e742015-08-15 17:29:39 -0700125enum dediprog_writemode {
126 WRITE_MODE_PAGE_PGM = 1,
127 WRITE_MODE_PAGE_WRITE = 2,
128 WRITE_MODE_1B_AAI = 3,
129 WRITE_MODE_2B_AAI = 4,
130 WRITE_MODE_128B_PAGE = 5,
131 WRITE_MODE_PAGE_AT26DF041 = 6,
132 WRITE_MODE_SILICON_BLUE_FPGA = 7,
133 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of length 512 bytes */
134 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
135 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of length 512 bytes */
136 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
137 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
Simon Glassd01002b2015-01-08 05:44:16 -0700138};
139
David Hendrickscf453d82015-11-16 20:19:57 -0800140enum dediprog_standalone_mode {
141 ENTER_STANDALONE_MODE = 0,
142 LEAVE_STANDALONE_MODE = 1,
143};
144
Edward O'Callaghancb338322019-02-27 19:25:18 +1100145/*
146 * These are not official designations; they are for use in flashrom only.
147 * Order must be preserved so that comparison operators work.
148 */
149enum protocol {
150 PROTOCOL_UNKNOWN,
151 PROTOCOL_V1,
152 PROTOCOL_V2,
153 PROTOCOL_V3,
154};
155
Edward O'Callaghana88395f2019-02-27 18:44:04 +1100156const struct dev_entry devs_dediprog[] = {
157 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
158
159 {0},
160};
161
David Hendricksf9ecb452015-11-04 15:40:27 -0800162#ifndef LIBUSB_HAVE_ERROR_NAME
163/* Quick and dirty replacement for missing libusb_error_name in older libusb 1.0. */
164const char *libusb_error_name(int error_code)
165{
166 /* 18 chars for text, rest for number, sign, nullbyte. */
167 static char my_libusb_error[18 + 6];
David Hendricksfe05e742015-08-15 17:29:39 -0700168
David Hendricksf9ecb452015-11-04 15:40:27 -0800169 sprintf(my_libusb_error, "libusb error code %i", error_code);
170 return my_libusb_error;
171}
172#endif
David Hendricksfe05e742015-08-15 17:29:39 -0700173
Edward O'Callaghancb338322019-02-27 19:25:18 +1100174static enum protocol protocol(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000175{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100176 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
David Hendricks6702e072015-08-15 18:09:04 -0700177 switch (dediprog_devicetype) {
178 case DEV_SF100:
179 return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
180 case DEV_SF600:
Edward O'Callaghancb338322019-02-27 19:25:18 +1100181 if (dediprog_firmwareversion < FIRMWARE_VERSION(6, 9, 0))
182 return PROTOCOL_V1;
183 else if (dediprog_firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
184 return PROTOCOL_V2;
185 else
186 return PROTOCOL_V3;
David Hendricks6702e072015-08-15 18:09:04 -0700187 default:
Edward O'Callaghancb338322019-02-27 19:25:18 +1100188 return PROTOCOL_UNKNOWN;
David Hendricks6702e072015-08-15 18:09:04 -0700189 }
hailfingerdfb32a02010-01-19 11:15:48 +0000190}
David Hendricks14e82e52015-08-15 17:59:03 -0700191
David Hendricksf9ecb452015-11-04 15:40:27 -0800192struct dediprog_transfer_status {
193 int error; /* OK if 0, ERROR else */
194 unsigned int queued_idx;
195 unsigned int finished_idx;
196};
197
198static void dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
199{
200 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
201 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
202 status->error = 1;
203 msg_perr("SPI bulk read failed!\n");
204 }
205 ++status->finished_idx;
206}
207
208static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
209{
210 if (status->finished_idx >= status->queued_idx)
211 return 0;
212
213 do {
214 struct timeval timeout = { 10, 0 };
215 const int ret = libusb_handle_events_timeout_completed(usb_ctx, &timeout, NULL);
216 if (ret < 0) {
217 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
218 return 1;
219 }
220 } while (finish && (status->finished_idx < status->queued_idx));
221 return 0;
222}
223
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100224static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
David Hendricks14e82e52015-08-15 17:59:03 -0700225{
David Hendricksf9ecb452015-11-04 15:40:27 -0800226 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
227 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700228}
229
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100230static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
David Hendricks14e82e52015-08-15 17:59:03 -0700231{
David Hendricksf9ecb452015-11-04 15:40:27 -0800232 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
233 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700234}
hailfingerdfb32a02010-01-19 11:15:48 +0000235
David Hendricksa7f99ac2015-11-12 16:42:03 -0800236static int dediprog_read_other(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
237{
238 return libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, cmd, value, idx,
239 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
240}
David Hendricksf9ecb452015-11-04 15:40:27 -0800241
David Hendricksfe05e742015-08-15 17:29:39 -0700242/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
stepan4c06de72011-01-28 09:00:15 +0000243static int dediprog_set_leds(int leds)
244{
David Hendricksfe05e742015-08-15 17:29:39 -0700245 if (leds < LED_NONE || leds > LED_ALL)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700246 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000247
David Hendricks14e82e52015-08-15 17:59:03 -0700248 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
249 * them around if we have an old device. On those devices the LEDs map as follows:
stepan4c06de72011-01-28 09:00:15 +0000250 * bit 2 == 0: green light is on.
David Hendricks14e82e52015-08-15 17:59:03 -0700251 * bit 0 == 0: red light is on.
252 *
253 * Additionally, the command structure has changed with the "new" protocol.
254 *
255 * FIXME: take IO pins into account
stepan4c06de72011-01-28 09:00:15 +0000256 */
David Hendricks14e82e52015-08-15 17:59:03 -0700257 int target_leds, ret;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100258 if (protocol() >= PROTOCOL_V2) {
David Hendricks14e82e52015-08-15 17:59:03 -0700259 target_leds = (leds ^ 7) << 8;
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100260 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
stepan4c06de72011-01-28 09:00:15 +0000261 } else {
David Hendricks14e82e52015-08-15 17:59:03 -0700262 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
263 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
264 } else {
265 target_leds = leds;
266 }
267 target_leds ^= 7;
268
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100269 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Simon Glass543101a2015-01-08 06:28:13 -0700270 }
David Hendricksfe05e742015-08-15 17:29:39 -0700271
stepan4c06de72011-01-28 09:00:15 +0000272 if (ret != 0x0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800273 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
stepan4c06de72011-01-28 09:00:15 +0000274 return 1;
275 }
276
stepan4c06de72011-01-28 09:00:15 +0000277 return 0;
278}
279
David Hendricksfe05e742015-08-15 17:29:39 -0700280struct dediprog_spispeeds {
281 const char *const name;
282 const int speed;
283};
284
285static const struct dediprog_spispeeds spispeeds[] = {
286 { "24M", 0x0 },
287 { "12M", 0x2 },
288 { "8M", 0x1 },
289 { "3M", 0x3 },
290 { "2.18M", 0x4 },
291 { "1.5M", 0x5 },
292 { "750k", 0x6 },
293 { "375k", 0x7 },
294 { NULL, 0x0 },
295};
296
297static int dediprog_set_spi_speed(unsigned int spispeed_idx)
hailfingerdfb32a02010-01-19 11:15:48 +0000298{
David Hendricksfe05e742015-08-15 17:29:39 -0700299 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100300 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
David Hendricksfe05e742015-08-15 17:29:39 -0700301 return 0;
302 }
hailfingerdfb32a02010-01-19 11:15:48 +0000303
David Hendricksfe05e742015-08-15 17:29:39 -0700304 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
305 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
hailfingerdfb32a02010-01-19 11:15:48 +0000306
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100307 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000308 if (ret != 0x0) {
David Hendricksfe05e742015-08-15 17:29:39 -0700309 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
hailfingerdfb32a02010-01-19 11:15:48 +0000310 return 1;
311 }
312 return 0;
313}
314
Edward O'Callaghancb338322019-02-27 19:25:18 +1100315static void fill_rw_cmd_payload(uint8_t *data_packet, unsigned int count, uint8_t dedi_spi_cmd,
316 unsigned int *value, unsigned int *idx, unsigned int start, int is_read) {
317 /* First 5 bytes are common in both generations. */
318 data_packet[0] = count & 0xff;
319 data_packet[1] = (count >> 8) & 0xff;
320 data_packet[2] = 0; /* RFU */
321 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
322 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
323
324 if (protocol() >= PROTOCOL_V2) {
325 *value = *idx = 0;
326 data_packet[5] = 0; /* RFU */
327 data_packet[6] = (start >> 0) & 0xff;
328 data_packet[7] = (start >> 8) & 0xff;
329 data_packet[8] = (start >> 16) & 0xff;
330 data_packet[9] = (start >> 24) & 0xff;
331 if (protocol() >= PROTOCOL_V3) {
332 if (is_read) {
333 data_packet[10] = 0x00; /* address length (3 or 4) */
334 data_packet[11] = 0x00; /* dummy cycle / 2 */
335 } else {
336 /* 16 LSBs and 16 HSBs of page size */
337 /* FIXME: This assumes page size of 256. */
338 data_packet[10] = 0x00;
339 data_packet[11] = 0x01;
340 data_packet[12] = 0x00;
341 data_packet[13] = 0x00;
342 }
343 }
344 } else {
345 *value = start % 0x10000;
346 *idx = start / 0x10000;
347 }
348}
349
hailfingerfb6287d2010-11-16 21:25:29 +0000350/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
351 * @start start address
352 * @len length
353 * @return 0 on success, 1 on failure
354 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700355static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000356 unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000357{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100358 int err = 1;
359
hailfingerfb6287d2010-11-16 21:25:29 +0000360 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000361 const unsigned int chunksize = 0x200;
362 const unsigned int count = len / chunksize;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100363
David Hendricksf9ecb452015-11-04 15:40:27 -0800364 struct dediprog_transfer_status status = { 0, 0, 0 };
365 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
366 struct libusb_transfer *transfer;
hailfingerfb6287d2010-11-16 21:25:29 +0000367
Edward O'Callaghancb338322019-02-27 19:25:18 +1100368 if (len == 0)
369 return 0;
370
hailfingerfb6287d2010-11-16 21:25:29 +0000371 if ((start % chunksize) || (len % chunksize)) {
372 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
373 "at flashrom@flashrom.org\n", __func__, start, len);
374 return 1;
375 }
376
Edward O'Callaghancb338322019-02-27 19:25:18 +1100377 int command_packet_size;
378 switch (protocol()) {
379 case PROTOCOL_V1:
380 command_packet_size = 5;
381 break;
382 case PROTOCOL_V2:
383 command_packet_size = 10;
384 break;
385 case PROTOCOL_V3:
386 command_packet_size = 12;
387 break;
388 default:
389 return 1;
David Hendricks14e82e52015-08-15 17:59:03 -0700390 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100391
392 uint8_t data_packet[command_packet_size];
393 unsigned int value, idx;
394 fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start, 1);
395
396 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
397 if (ret != sizeof(data_packet)) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800398 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
hailfingerfb6287d2010-11-16 21:25:29 +0000399 return 1;
400 }
401
David Hendricksf9ecb452015-11-04 15:40:27 -0800402 /*
403 * Ring buffer of bulk transfers.
404 * Poll until at least one transfer is ready,
405 * schedule next transfers until buffer is full.
406 */
hailfingerfb6287d2010-11-16 21:25:29 +0000407
David Hendricksf9ecb452015-11-04 15:40:27 -0800408 /* Allocate bulk transfers. */
Edward O'Callaghancb338322019-02-27 19:25:18 +1100409 unsigned int i;
David Hendricksf9ecb452015-11-04 15:40:27 -0800410 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
411 transfers[i] = libusb_alloc_transfer(0);
412 if (!transfers[i]) {
413 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
414 goto _err_free;
415 }
416 }
417
418 /* Now transfer requested chunks using libusb's asynchronous interface. */
419 while (!status.error && (status.queued_idx < count)) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100420 while ((status.queued_idx < count) &&
421 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
422 {
David Hendricksf9ecb452015-11-04 15:40:27 -0800423 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
424 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
425 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
426 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
427 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
428 ret = libusb_submit_transfer(transfer);
429 if (ret < 0) {
430 msg_perr("Submitting SPI bulk read %i failed: %i %s!\n",
431 status.queued_idx, ret, libusb_error_name(ret));
432 goto _err_free;
433 }
434 ++status.queued_idx;
435 }
436 if (dediprog_bulk_read_poll(&status, 0))
437 goto _err_free;
438 }
439 /* Wait for transfers to finish. */
440 if (dediprog_bulk_read_poll(&status, 1))
441 goto _err_free;
442 /* Check if everything has been transmitted. */
443 if ((status.finished_idx < count) || status.error)
444 goto _err_free;
445
446 err = 0;
447
448_err_free:
449 dediprog_bulk_read_poll(&status, 1);
450 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
451 if (transfers[i]) libusb_free_transfer(transfers[i]);
452 return err;
hailfingerfb6287d2010-11-16 21:25:29 +0000453}
454
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700455static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000456 unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000457{
hailfingerfb6287d2010-11-16 21:25:29 +0000458 int ret;
459 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000460 const unsigned int chunksize = 0x200;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100461 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000462 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000463
Simon Glasse53cc1d2015-01-08 05:48:51 -0700464 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000465
hailfingerfb6287d2010-11-16 21:25:29 +0000466 if (residue) {
467 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
468 start, residue);
469 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700470 if (ret)
471 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000472 }
473
474 /* Round down. */
475 bulklen = (len - residue) / chunksize * chunksize;
476 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
477 bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700478 if (ret)
479 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000480
481 len -= residue + bulklen;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100482 if (len != 0) {
hailfingerfb6287d2010-11-16 21:25:29 +0000483 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
484 start, len);
485 ret = spi_read_chunked(flash, buf + residue + bulklen,
486 start + residue + bulklen, len, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700487 if (ret)
488 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000489 }
490
Simon Glasse53cc1d2015-01-08 05:48:51 -0700491 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000492 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700493err:
494 dediprog_set_leds(LED_ERROR);
495 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000496}
497
David Hendricksfe05e742015-08-15 17:29:39 -0700498/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
499 * @chunksize length of data chunks, only 256 supported by now
500 * @start start address
501 * @len length
502 * @dedi_spi_cmd dediprog specific write command for spi bus
503 * @return 0 on success, 1 on failure
504 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700505static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
David Hendricksfe05e742015-08-15 17:29:39 -0700506 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
hailfinger556e9c32010-11-23 21:28:16 +0000507{
David Hendricksfe05e742015-08-15 17:29:39 -0700508 /* USB transfer size must be 512, other sizes will NOT work at all.
509 * chunksize is the real data size per USB bulk transfer. The remaining
510 * space in a USB bulk transfer must be filled with 0xff padding.
511 */
512 const unsigned int count = len / chunksize;
David Hendricksfe05e742015-08-15 17:29:39 -0700513
514 /*
515 * We should change this check to
516 * chunksize > 512
517 * once we know how to handle different chunk sizes.
518 */
519 if (chunksize != 256) {
520 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
521 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
522 return 1;
523 }
524
525 if ((start % chunksize) || (len % chunksize)) {
526 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
527 "at flashrom@flashrom.org\n", __func__, start, len);
528 return 1;
529 }
530
531 /* No idea if the hardware can handle empty writes, so chicken out. */
Edward O'Callaghancb338322019-02-27 19:25:18 +1100532 if (len == 0)
David Hendricksfe05e742015-08-15 17:29:39 -0700533 return 0;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100534
535 int command_packet_size;
536 switch (protocol()) {
537 case PROTOCOL_V1:
538 command_packet_size = 5;
539 break;
540 case PROTOCOL_V2:
541 command_packet_size = 10;
542 break;
543 case PROTOCOL_V3:
544 command_packet_size = 14;
545 break;
546 default:
547 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -0700548 }
549
Edward O'Callaghancb338322019-02-27 19:25:18 +1100550 uint8_t data_packet[command_packet_size];
551 unsigned int value, idx;
552 fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start, 0);
553 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
554 if (ret != sizeof(data_packet)) {
555 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
556 return 1;
557 }
558
559 unsigned int i;
David Hendricksfe05e742015-08-15 17:29:39 -0700560 for (i = 0; i < count; i++) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100561 unsigned char usbbuf[512];
David Hendricksfe05e742015-08-15 17:29:39 -0700562 memcpy(usbbuf, buf + i * chunksize, chunksize);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100563 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
564 int transferred;
565 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
566 DEFAULT_TIMEOUT);
David Hendricksf9ecb452015-11-04 15:40:27 -0800567 if ((ret < 0) || (transferred != 512)) {
568 msg_perr("SPI bulk write failed, expected %i, got %i %s!\n",
569 512, ret, libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700570 return 1;
571 }
572 }
573
574 return 0;
575}
576
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700577static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
David Hendricksfe05e742015-08-15 17:29:39 -0700578 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
579{
580 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100581 const unsigned int chunksize = flash->chip->page_size;
David Hendricksfe05e742015-08-15 17:29:39 -0700582 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
583 unsigned int bulklen;
stepan4c06de72011-01-28 09:00:15 +0000584
Simon Glasse53cc1d2015-01-08 05:48:51 -0700585 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000586
David Hendricksfe05e742015-08-15 17:29:39 -0700587 if (chunksize != 256) {
588 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
589 "we don't know how dediprog\nhandles them.\n");
590 /* Write everything like it was residue. */
591 residue = len;
592 }
stepan4c06de72011-01-28 09:00:15 +0000593
David Hendricksfe05e742015-08-15 17:29:39 -0700594 if (residue) {
595 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
596 start, residue);
597 /* No idea about the real limit. Maybe 12, maybe more. */
598 ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 12);
599 if (ret) {
600 dediprog_set_leds(LED_ERROR);
601 return ret;
602 }
603 }
604
605 /* Round down. */
606 bulklen = (len - residue) / chunksize * chunksize;
607 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
608 if (ret) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700609 dediprog_set_leds(LED_ERROR);
David Hendricksfe05e742015-08-15 17:29:39 -0700610 return ret;
611 }
stepan4c06de72011-01-28 09:00:15 +0000612
David Hendricksfe05e742015-08-15 17:29:39 -0700613 len -= residue + bulklen;
614 if (len) {
615 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
616 start, len);
617 ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen),
618 start + residue + bulklen, len, 12);
619 if (ret) {
620 dediprog_set_leds(LED_ERROR);
621 return ret;
622 }
623 }
624
625 dediprog_set_leds(LED_PASS);
626 return 0;
hailfinger556e9c32010-11-23 21:28:16 +0000627}
628
Patrick Georgiab8353e2017-02-03 18:32:01 +0100629static int dediprog_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
David Hendricksfe05e742015-08-15 17:29:39 -0700630{
631 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
632}
633
634#if 0
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700635static int dediprog_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
David Hendricksfe05e742015-08-15 17:29:39 -0700636{
637 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
638}
639#endif
640
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700641//static int dediprog_spi_send_command(struct flashctx *flash,
642static int dediprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
David Hendricksfe05e742015-08-15 17:29:39 -0700643 unsigned int readcnt,
644 const unsigned char *writearr,
645 unsigned char *readarr)
hailfingerdfb32a02010-01-19 11:15:48 +0000646{
647 int ret;
648
hailfingeraf389cc2010-01-22 02:53:30 +0000649 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
David Hendricksfe05e742015-08-15 17:29:39 -0700650 if (writecnt > UINT16_MAX) {
651 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
652 return 1;
Simon Glass543101a2015-01-08 06:28:13 -0700653 }
David Hendricksfe05e742015-08-15 17:29:39 -0700654 if (readcnt > UINT16_MAX) {
655 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
656 return 1;
657 }
658
Edward O'Callaghancb338322019-02-27 19:25:18 +1100659 unsigned int idx, value;
660 /* New protocol has options and timeout combined as value while the old one used the value field for
661 * timeout and the index field for options. */
662 if (protocol() >= PROTOCOL_V2) {
663 idx = 0;
664 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
David Hendricks14e82e52015-08-15 17:59:03 -0700665 } else {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100666 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
667 value = 0;
David Hendricks14e82e52015-08-15 17:59:03 -0700668 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100669 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000670 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000671 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800672 writecnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000673 return 1;
674 }
David Hendricksfe05e742015-08-15 17:29:39 -0700675 if (readcnt == 0)
hailfingerdfb32a02010-01-19 11:15:48 +0000676 return 0;
David Hendricksfe05e742015-08-15 17:29:39 -0700677
Edward O'Callaghancb338322019-02-27 19:25:18 +1100678 /* The specifications do state the possibility to set a timeout for transceive transactions.
679 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
680 * can predict the time needed by the previous command or so (untested). In any case, using this
681 * "feature" to set sane-looking timouts for the read below will completely trash performance with
682 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
683 *
684 * The specification also uses only 0 in its examples, so the lesson to learn here:
685 * "Never trust the description of an interface in the documentation but use the example code and pray."
686 const uint8_t read_timeout = 10 + readcnt/512;
687 if (protocol() >= PROTOCOL_V2) {
688 idx = 0;
689 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
690 } else {
691 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
692 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
693 }
694 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
695 */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100696 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000697 if (ret != readcnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000698 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800699 readcnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000700 return 1;
701 }
702 return 0;
703}
704
hailfinger1ff33dc2010-07-03 11:02:10 +0000705static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000706{
707 int ret;
708 char buf[0x11];
709
hailfingerdfb32a02010-01-19 11:15:48 +0000710 /* Command Receive Device String. */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100711 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000712 if (ret != 0x10) {
713 msg_perr("Incomplete/failed Command Receive Device String!\n");
714 return 1;
715 }
716 buf[0x10] = '\0';
717 msg_pdbg("Found a %s\n", buf);
David Hendricks6702e072015-08-15 18:09:04 -0700718 if (memcmp(buf, "SF100", 0x5) == 0)
719 dediprog_devicetype = DEV_SF100;
720 else if (memcmp(buf, "SF600", 0x5) == 0)
721 dediprog_devicetype = DEV_SF600;
722 else {
David Hendricks22593802015-11-13 12:59:30 -0800723 msg_perr("Device not a SF100 or SF600!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000724 return 1;
725 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100726
727 int sfnum;
728 int fw[3];
729 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
730 sfnum != dediprog_devicetype) {
David Hendricks14e82e52015-08-15 17:59:03 -0700731 msg_perr("Unexpected firmware version string '%s'\n", buf);
hailfinger7a009082010-11-09 23:30:43 +0000732 return 1;
733 }
David Hendricks14e82e52015-08-15 17:59:03 -0700734 /* Only these major versions were tested. */
David Hendricks6702e072015-08-15 18:09:04 -0700735 if (fw[0] < 2 || fw[0] > 7) {
David Hendricks14e82e52015-08-15 17:59:03 -0700736 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000737 return 1;
738 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100739
stepan4c06de72011-01-28 09:00:15 +0000740 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100741 if (protocol() == PROTOCOL_UNKNOWN) {
742 msg_perr("Internal error: Unable to determine protocol version.\n");
743 return 1;
744 }
David Hendricks14e82e52015-08-15 17:59:03 -0700745
hailfingerdfb32a02010-01-19 11:15:48 +0000746 return 0;
747}
748
David Hendricks91ca7ac2015-11-20 16:22:22 -0800749static int dediprog_supply_voltages[] = {
750 0, 1800, 2500, 3500,
751};
752
753static int dediprog_set_spi_flash_voltage_manual(int millivolt)
754{
755 int ret;
756 uint16_t voltage_selector;
757
758 switch (millivolt) {
759 case 0:
760 /* Admittedly this one is an assumption. */
761 voltage_selector = 0x0;
762 break;
763 case 1800:
764 voltage_selector = 0x12;
765 break;
766 case 2500:
767 voltage_selector = 0x11;
768 break;
769 case 3500:
770 voltage_selector = 0x10;
771 break;
772 default:
773 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
774 return 1;
775 }
776 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
777 millivolt % 1000);
778
779 if (voltage_selector == 0) {
780 /* Wait some time as the original driver does. */
781 programmer_delay(200 * 1000);
782 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100783 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
David Hendricks91ca7ac2015-11-20 16:22:22 -0800784 if (ret != 0x0) {
785 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
786 voltage_selector);
787 return 1;
788 }
789 if (voltage_selector != 0) {
790 /* Wait some time as the original driver does. */
791 programmer_delay(200 * 1000);
792 }
793 return 0;
794}
795
David Hendricks93784b42016-08-09 17:00:38 -0700796static int dediprog_set_spi_flash_voltage_auto(void)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800797{
David Hendricksac1d25c2016-08-09 17:00:58 -0700798 int i;
David Hendricks91ca7ac2015-11-20 16:22:22 -0800799 int spi_flash_ranges;
800
801 spi_flash_ranges = flash_supported_voltage_ranges(BUS_SPI);
802 if (spi_flash_ranges < 0)
803 return -1;
804
805 for (i = 0; i < ARRAY_SIZE(dediprog_supply_voltages); i++) {
806 int j;
807 int v = dediprog_supply_voltages[i]; /* shorthand */
808
809 for (j = 0; j < spi_flash_ranges; j++) {
810 /* Dediprog can supply a voltage in this range. */
811 if ((v >= voltage_ranges[j].min) && (v <= voltage_ranges[j].max)) {
David Hendricks93784b42016-08-09 17:00:38 -0700812 struct flashctx dummy;
813
David Hendricks91ca7ac2015-11-20 16:22:22 -0800814 msg_cdbg("%s: trying %d\n", __func__, v);
815 if (dediprog_set_spi_flash_voltage_manual(v)) {
816 msg_cerr("%s: Failed to set SPI voltage\n", __func__);
817 return 1;
818 }
819
820 clear_spi_id_cache();
David Hendricksac1d25c2016-08-09 17:00:58 -0700821 if (probe_flash(0, &dummy, 0) < 0) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800822 /* No dice, try next voltage supported by Dediprog. */
823 break;
824 }
825
Patrick Georgif3fa2992017-02-02 16:24:44 +0100826 if ((dummy.chip->manufacture_id == GENERIC_MANUF_ID) ||
827 (dummy.chip->model_id == GENERIC_DEVICE_ID)) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800828 break;
829 }
830
831 return 0;
832 }
833 }
834 }
835
836 return 1;
837}
838
839/* FIXME: ugly function signature */
David Hendricks93784b42016-08-09 17:00:38 -0700840static int dediprog_set_spi_voltage(int millivolt, int probe)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800841{
842 if (probe)
David Hendricks93784b42016-08-09 17:00:38 -0700843 return dediprog_set_spi_flash_voltage_auto();
David Hendricks91ca7ac2015-11-20 16:22:22 -0800844 else
845 return dediprog_set_spi_flash_voltage_manual(millivolt);
846}
847
David Hendricks28fbdfb2015-08-15 18:04:37 -0700848/*
849 * This command presumably sets the voltage for the SF100 itself (not the
850 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
851 * (including all SF600s) do not support it.
852 */
853static int dediprog_set_voltage(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000854{
855 int ret;
David Hendricksf9ecb452015-11-04 15:40:27 -0800856 unsigned char buf[0x1];
hailfingerdfb32a02010-01-19 11:15:48 +0000857
858 memset(buf, 0, sizeof(buf));
David Hendricksa7f99ac2015-11-12 16:42:03 -0800859 ret = dediprog_read_other(CMD_SET_VOLTAGE, 0x0, 0x0, buf, 0x1);
hailfinger7a009082010-11-09 23:30:43 +0000860 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800861 msg_perr("Command A failed (%s)!\n", libusb_error_name(ret));
hailfinger7a009082010-11-09 23:30:43 +0000862 return 1;
863 }
hailfingerdfb32a02010-01-19 11:15:48 +0000864 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -0700865 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000866 return 1;
867 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100868
hailfingerdfb32a02010-01-19 11:15:48 +0000869 return 0;
870}
871
David Hendrickscf453d82015-11-16 20:19:57 -0800872static int dediprog_leave_standalone_mode(void)
873{
874 int ret;
875
876 if (dediprog_devicetype != DEV_SF600)
877 return 0;
878
879 msg_pdbg("Leaving standalone mode\n");
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100880 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Hendrickscf453d82015-11-16 20:19:57 -0800881 if (ret) {
882 msg_perr("Failed to leave standalone mode (%s)!\n", libusb_error_name(ret));
883 return 1;
884 }
885
886 return 0;
887}
888
David Hendricksfe05e742015-08-15 17:29:39 -0700889static int set_target_flash(enum dediprog_target target)
890{
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100891 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
David Hendricksfe05e742015-08-15 17:29:39 -0700892 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800893 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000894 return 1;
895 }
896 return 0;
897}
898
David Hendricksfe05e742015-08-15 17:29:39 -0700899#if 0
900/* Returns true if the button is currently pressed. */
901static bool dediprog_get_button(void)
Simon Glassd01002b2015-01-08 05:44:16 -0700902{
David Hendricksfe05e742015-08-15 17:29:39 -0700903 char buf[1];
904 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
905 buf, 0x1, DEFAULT_TIMEOUT);
906 if (ret != 0) {
907 msg_perr("Could not get button state (%s)!\n", usb_strerror());
908 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700909 }
David Hendricksfe05e742015-08-15 17:29:39 -0700910 return buf[0] != 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700911}
David Hendricksfe05e742015-08-15 17:29:39 -0700912#endif
Simon Glassd01002b2015-01-08 05:44:16 -0700913
hailfingerf76cc322010-11-09 22:00:31 +0000914static int parse_voltage(char *voltage)
915{
916 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000917 int i;
918 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000919
920 if (!voltage || !strlen(voltage)) {
921 msg_perr("Empty voltage= specified.\n");
922 return -1;
923 }
924 millivolt = (int)strtol(voltage, &tmp, 0);
925 voltage = tmp;
926 /* Handle "," and "." as decimal point. Everything after it is assumed
927 * to be in decimal notation.
928 */
929 if ((*voltage == '.') || (*voltage == ',')) {
930 voltage++;
931 for (i = 0; i < 3; i++) {
932 fraction *= 10;
933 /* Don't advance if the current character is invalid,
934 * but continue multiplying.
935 */
936 if ((*voltage < '0') || (*voltage > '9'))
937 continue;
938 fraction += *voltage - '0';
939 voltage++;
940 }
941 /* Throw away remaining digits. */
942 voltage += strspn(voltage, "0123456789");
943 }
944 /* The remaining string must be empty or "mV" or "V". */
945 tolower_string(voltage);
946
947 /* No unit or "V". */
948 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
949 millivolt *= 1000;
950 millivolt += fraction;
951 } else if (!strncmp(voltage, "mv", 2) ||
952 !strncmp(voltage, "milliv", 6)) {
953 /* No adjustment. fraction is discarded. */
954 } else {
955 /* Garbage at the end of the string. */
956 msg_perr("Garbage voltage= specified.\n");
957 return -1;
958 }
959 return millivolt;
960}
961
David Hendricksfe05e742015-08-15 17:29:39 -0700962#if 0
963static const struct spi_master spi_master_dediprog = {
964 .type = SPI_CONTROLLER_DEDIPROG,
965 .max_data_read = MAX_DATA_UNSPECIFIED,
966 .max_data_write = MAX_DATA_UNSPECIFIED,
967 .command = dediprog_spi_send_command,
968 .multicommand = default_spi_send_multicommand,
969 .read = dediprog_spi_read,
970 .write_256 = dediprog_spi_write_256,
971 .write_aai = dediprog_spi_write_aai,
972};
973#endif
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100974static const struct spi_master spi_master_dediprog = {
uwe8d342eb2011-07-28 08:13:25 +0000975 .type = SPI_CONTROLLER_DEDIPROG,
976 .max_data_read = MAX_DATA_UNSPECIFIED,
977 .max_data_write = MAX_DATA_UNSPECIFIED,
978 .command = dediprog_spi_send_command,
979 .multicommand = default_spi_send_multicommand,
980 .read = dediprog_spi_read,
981 .write_256 = dediprog_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000982};
983
David Hendricks93784b42016-08-09 17:00:38 -0700984static int dediprog_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000985{
986 msg_pspew("%s\n", __func__);
987
David Hendricks6702e072015-08-15 18:09:04 -0700988 dediprog_devicetype = DEV_UNKNOWN;
David Hendricksfe05e742015-08-15 17:29:39 -0700989
dhendrix0ffc2eb2011-06-14 01:35:36 +0000990 /* URB 28. Command Set SPI Voltage to 0. */
David Hendricks93784b42016-08-09 17:00:38 -0700991 if (dediprog_set_spi_voltage(0x0, 0))
dhendrix0ffc2eb2011-06-14 01:35:36 +0000992 return 1;
993
David Hendricksf9ecb452015-11-04 15:40:27 -0800994 if (libusb_release_interface(dediprog_handle, 0)) {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000995 msg_perr("Could not release USB interface!\n");
996 return 1;
997 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800998 libusb_close(dediprog_handle);
999 libusb_exit(usb_ctx);
1000
dhendrix0ffc2eb2011-06-14 01:35:36 +00001001 return 0;
1002}
1003
hailfingerdfb32a02010-01-19 11:15:48 +00001004/* URB numbers refer to the first log ever captured. */
David Hendricksac1d25c2016-08-09 17:00:58 -07001005int dediprog_init(void)
hailfingerdfb32a02010-01-19 11:15:48 +00001006{
David Hendricks98b3c572016-11-30 01:50:08 +00001007 char *voltage, *device, *spispeed, *target_str;
David Hendricksfe05e742015-08-15 17:29:39 -07001008 int spispeed_idx = 1;
David Hendrickscd20ea42015-11-17 22:33:35 -08001009 int millivolt = 0;
David Hendricksfe05e742015-08-15 17:29:39 -07001010 long usedevice = 0;
1011 long target = 1;
David Hendricks98b3c572016-11-30 01:50:08 +00001012 int i, ret;
hailfingerdfb32a02010-01-19 11:15:48 +00001013
1014 msg_pspew("%s\n", __func__);
1015
David Hendricksfe05e742015-08-15 17:29:39 -07001016 spispeed = extract_programmer_param("spispeed");
1017 if (spispeed) {
1018 for (i = 0; spispeeds[i].name; ++i) {
1019 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1020 spispeed_idx = i;
1021 break;
1022 }
1023 }
1024 if (!spispeeds[i].name) {
1025 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
David Hendricks98b3c572016-11-30 01:50:08 +00001026 free(spispeed);
1027 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001028 }
David Hendricks98b3c572016-11-30 01:50:08 +00001029 free(spispeed);
David Hendricksfe05e742015-08-15 17:29:39 -07001030 }
1031
hailfingerf76cc322010-11-09 22:00:31 +00001032 voltage = extract_programmer_param("voltage");
1033 if (voltage) {
1034 millivolt = parse_voltage(voltage);
1035 free(voltage);
David Hendricks98b3c572016-11-30 01:50:08 +00001036 if (millivolt < 0)
1037 return 1;
hailfingerf76cc322010-11-09 22:00:31 +00001038 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1039 }
David Hendricksfe05e742015-08-15 17:29:39 -07001040
1041 device = extract_programmer_param("device");
1042 if (device) {
1043 char *dev_suffix;
1044 errno = 0;
1045 usedevice = strtol(device, &dev_suffix, 10);
1046 if (errno != 0 || device == dev_suffix) {
1047 msg_perr("Error: Could not convert 'device'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001048 free(device);
1049 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001050 }
David Hendricksfe05e742015-08-15 17:29:39 -07001051 if (usedevice < 0 || usedevice > UINT_MAX) {
1052 msg_perr("Error: Value for 'device' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001053 free(device);
1054 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001055 }
1056 if (strlen(dev_suffix) > 0) {
1057 msg_perr("Error: Garbage following 'device' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001058 free(device);
1059 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001060 }
1061 msg_pinfo("Using device %li.\n", usedevice);
Simon Glassd01002b2015-01-08 05:44:16 -07001062 }
David Hendricks98b3c572016-11-30 01:50:08 +00001063 free(device);
David Hendricksfe05e742015-08-15 17:29:39 -07001064
1065 target_str = extract_programmer_param("target");
1066 if (target_str) {
1067 char *target_suffix;
1068 errno = 0;
1069 target = strtol(target_str, &target_suffix, 10);
1070 if (errno != 0 || target_str == target_suffix) {
1071 msg_perr("Error: Could not convert 'target'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001072 free(target_str);
1073 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001074 }
1075 if (target < 1 || target > 2) {
1076 msg_perr("Error: Value for 'target' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001077 free(target_str);
1078 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001079 }
1080 if (strlen(target_suffix) > 0) {
1081 msg_perr("Error: Garbage following 'target' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001082 free(target_str);
1083 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001084 }
1085 msg_pinfo("Using target %li.\n", target);
1086 }
David Hendricks98b3c572016-11-30 01:50:08 +00001087 free(target_str);
hailfingerf76cc322010-11-09 22:00:31 +00001088
hailfingerdfb32a02010-01-19 11:15:48 +00001089 /* Here comes the USB stuff. */
David Hendricksf9ecb452015-11-04 15:40:27 -08001090 libusb_init(&usb_ctx);
1091 if (!usb_ctx) {
1092 msg_perr("Could not initialize libusb!\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001093 return 1;
hailfingerdfb32a02010-01-19 11:15:48 +00001094 }
Edward O'Callaghana88395f2019-02-27 18:44:04 +11001095 const uint16_t vid = devs_dediprog[0].vendor_id;
1096 const uint16_t pid = devs_dediprog[0].device_id;
1097 dediprog_handle = usb_dev_get_by_vid_pid_number(usb_ctx, vid, pid, (unsigned int) usedevice);
David Hendricksfe05e742015-08-15 17:29:39 -07001098 if (!dediprog_handle) {
David Hendricks22593802015-11-13 12:59:30 -08001099 msg_perr("Could not find a Dediprog programmer on USB!\n");
David Hendricksf9ecb452015-11-04 15:40:27 -08001100 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001101 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001102 }
David Hendricks98b3c572016-11-30 01:50:08 +00001103 ret = libusb_set_configuration(dediprog_handle, 1);
1104 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001105 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1106 libusb_close(dediprog_handle);
1107 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001108 return 1;
hailfingerfb6287d2010-11-16 21:25:29 +00001109 }
David Hendricks98b3c572016-11-30 01:50:08 +00001110 ret = libusb_claim_interface(dediprog_handle, 0);
1111 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001112 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1113 libusb_close(dediprog_handle);
1114 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001115 return 1;
hailfingerfb6287d2010-11-16 21:25:29 +00001116 }
David Hendricksfe05e742015-08-15 17:29:39 -07001117
David Hendricks98b3c572016-11-30 01:50:08 +00001118 if (register_shutdown(dediprog_shutdown, NULL))
1119 return 1;
dhendrix0ffc2eb2011-06-14 01:35:36 +00001120
David Hendricks28fbdfb2015-08-15 18:04:37 -07001121 /* Try reading the devicestring. If that fails and the device is old
1122 * (FW < 6.0.0) then we need to try the "set voltage" command and then
1123 * attempt to read the devicestring again. */
1124 if (dediprog_check_devicestring()) {
David Hendricks98b3c572016-11-30 01:50:08 +00001125 if (dediprog_set_voltage())
1126 return 1;
1127 if (dediprog_check_devicestring())
1128 return 1;
David Hendricks28fbdfb2015-08-15 18:04:37 -07001129 }
David Hendricksfe05e742015-08-15 17:29:39 -07001130
David Hendricksd7468582015-11-12 15:21:12 -08001131 /* SF100 firmware exposes only one endpoint for in/out, SF600 firmware
1132 exposes separate endpoints for in and out. */
1133 dediprog_in_endpoint = 2;
1134 if (dediprog_devicetype == DEV_SF100)
1135 dediprog_out_endpoint = 2;
1136 else
1137 dediprog_out_endpoint = 1;
1138
David Hendricks93784b42016-08-09 17:00:38 -07001139
David Hendrickse4c73662015-11-20 16:26:07 -08001140 /* Set some LEDs as soon as possible to indicate activity.
David Hendricksfe05e742015-08-15 17:29:39 -07001141 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
Edward O'Callaghancb338322019-02-27 19:25:18 +11001142 * dediprog_setup() has queried the device. */
David Hendrickse4c73662015-11-20 16:26:07 -08001143 dediprog_set_leds(LED_PASS | LED_BUSY);
David Hendricksfe05e742015-08-15 17:29:39 -07001144
David Hendrickscd20ea42015-11-17 22:33:35 -08001145 /* FIXME: need to do this so buses_supported gets SPI */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +01001146 register_spi_master(&spi_master_dediprog);
David Hendricks93784b42016-08-09 17:00:38 -07001147
David Hendricksfe05e742015-08-15 17:29:39 -07001148 /* Select target/socket, frequency and VCC. */
1149 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
1150 dediprog_set_spi_speed(spispeed_idx) ||
David Hendricks93784b42016-08-09 17:00:38 -07001151 dediprog_set_spi_voltage(millivolt, voltage ? 0 : 1)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -07001152 dediprog_set_leds(LED_ERROR);
David Hendricks98b3c572016-11-30 01:50:08 +00001153 return 1;
stepan4c06de72011-01-28 09:00:15 +00001154 }
hailfingerdfb32a02010-01-19 11:15:48 +00001155
David Hendricks98b3c572016-11-30 01:50:08 +00001156 if (dediprog_leave_standalone_mode())
1157 return 1;
1158
David Hendricks93784b42016-08-09 17:00:38 -07001159
David Hendricksfe05e742015-08-15 17:29:39 -07001160 dediprog_set_leds(LED_NONE);
stepan4c06de72011-01-28 09:00:15 +00001161
David Hendricks98b3c572016-11-30 01:50:08 +00001162 return 0;
hailfingerdfb32a02010-01-19 11:15:48 +00001163}