blob: 33df7462cfaa04b61a716156eb9888d0d250f73e [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>
David Hendricksf9ecb452015-11-04 15:40:27 -080025#include <libusb.h>
hailfingerdfb32a02010-01-19 11:15:48 +000026#include "flash.h"
David Hendrickscd20ea42015-11-17 22:33:35 -080027#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000028#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000029#include "programmer.h"
hailfingerdfb32a02010-01-19 11:15:48 +000030#include "spi.h"
31
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +110032/* LIBUSB_CALL ensures the right calling conventions on libusb callbacks.
33 * However, the macro is not defined everywhere. m(
34 */
35#ifndef LIBUSB_CALL
36#define LIBUSB_CALL
37#endif
38
stepan4c06de72011-01-28 09:00:15 +000039#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
hailfingerdfb32a02010-01-19 11:15:48 +000040#define DEFAULT_TIMEOUT 3000
David Hendricksf9ecb452015-11-04 15:40:27 -080041#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
42#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
43#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
44#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
45#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
Edward O'Callaghanef4e28b2019-06-28 13:18:41 +100046static struct libusb_context *usb_ctx;
David Hendricksf9ecb452015-11-04 15:40:27 -080047static libusb_device_handle *dediprog_handle;
David Hendricksd7468582015-11-12 15:21:12 -080048static int dediprog_in_endpoint;
49static int dediprog_out_endpoint;
David Hendricksf9ecb452015-11-04 15:40:27 -080050static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
hailfingerdfb32a02010-01-19 11:15:48 +000051
David Hendricks6702e072015-08-15 18:09:04 -070052enum dediprog_devtype {
53 DEV_UNKNOWN = 0,
54 DEV_SF100 = 100,
Edward O'Callaghancc0bf442019-02-27 22:35:44 +110055 DEV_SF200 = 200,
David Hendricks6702e072015-08-15 18:09:04 -070056 DEV_SF600 = 600,
57};
58
59enum dediprog_devtype dediprog_devicetype;
60
David Hendricksfe05e742015-08-15 17:29:39 -070061enum dediprog_leds {
62 LED_INVALID = -1,
63 LED_NONE = 0,
Simon Glasse53cc1d2015-01-08 05:48:51 -070064 LED_PASS = 1 << 0,
65 LED_BUSY = 1 << 1,
66 LED_ERROR = 1 << 2,
67 LED_ALL = 7,
68};
69
Simon Glassa1f80682015-01-08 05:55:29 -070070/* IO bits for CMD_SET_IO_LED message */
David Hendricksfe05e742015-08-15 17:29:39 -070071enum dediprog_ios {
Simon Glassa1f80682015-01-08 05:55:29 -070072 IO1 = 1 << 0,
73 IO2 = 1 << 1,
74 IO3 = 1 << 2,
75 IO4 = 1 << 3,
76};
77
David Hendricksfe05e742015-08-15 17:29:39 -070078enum dediprog_cmds {
79 CMD_TRANSCEIVE = 0x01,
80 CMD_POLL_STATUS_REG = 0x02,
81 CMD_SET_VPP = 0x03,
82 CMD_SET_TARGET = 0x04,
83 CMD_READ_EEPROM = 0x05,
84 CMD_WRITE_EEPROM = 0x06,
85 CMD_SET_IO_LED = 0x07,
86 CMD_READ_PROG_INFO = 0x08,
87 CMD_SET_VCC = 0x09,
88 CMD_SET_STANDALONE = 0x0A,
David Hendricks28fbdfb2015-08-15 18:04:37 -070089 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
David Hendricksfe05e742015-08-15 17:29:39 -070090 CMD_GET_BUTTON = 0x11,
91 CMD_GET_UID = 0x12,
92 CMD_SET_CS = 0x14,
93 CMD_IO_MODE = 0x15,
94 CMD_FW_UPDATE = 0x1A,
95 CMD_FPGA_UPDATE = 0x1B,
96 CMD_READ_FPGA_VERSION = 0x1C,
97 CMD_SET_HOLD = 0x1D,
98 CMD_READ = 0x20,
99 CMD_WRITE = 0x30,
100 CMD_WRITE_AT45DB = 0x31,
101 CMD_NAND_WRITE = 0x32,
102 CMD_NAND_READ = 0x33,
103 CMD_SET_SPI_CLK = 0x61,
104 CMD_CHECK_SOCKET = 0x62,
105 CMD_DOWNLOAD_PRJ = 0x63,
106 CMD_READ_PRJ_NAME = 0x64,
107 // New protocol/firmware only
108 CMD_CHECK_SDCARD = 0x65,
109 CMD_READ_PRJ = 0x66,
Simon Glassa1f80682015-01-08 05:55:29 -0700110};
111
David Hendricksfe05e742015-08-15 17:29:39 -0700112enum dediprog_target {
113 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
114 FLASH_TYPE_FLASH_CARD,
115 FLASH_TYPE_APPLICATION_FLASH_2,
116 FLASH_TYPE_SOCKET,
Simon Glassa1f80682015-01-08 05:55:29 -0700117};
118
David Hendricksfe05e742015-08-15 17:29:39 -0700119enum dediprog_readmode {
120 READ_MODE_STD = 1,
121 READ_MODE_FAST = 2,
122 READ_MODE_ATMEL45 = 3,
123 READ_MODE_4B_ADDR_FAST = 4,
124 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Simon Glassd01002b2015-01-08 05:44:16 -0700125};
126
David Hendricksfe05e742015-08-15 17:29:39 -0700127enum dediprog_writemode {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100128 WRITE_MODE_PAGE_PGM = 1,
David Hendricksfe05e742015-08-15 17:29:39 -0700129 WRITE_MODE_PAGE_WRITE = 2,
130 WRITE_MODE_1B_AAI = 3,
131 WRITE_MODE_2B_AAI = 4,
132 WRITE_MODE_128B_PAGE = 5,
133 WRITE_MODE_PAGE_AT26DF041 = 6,
134 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100135 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
David Hendricksfe05e742015-08-15 17:29:39 -0700136 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100137 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
David Hendricksfe05e742015-08-15 17:29:39 -0700138 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
139 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
Simon Glassd01002b2015-01-08 05:44:16 -0700140};
141
David Hendrickscf453d82015-11-16 20:19:57 -0800142enum dediprog_standalone_mode {
143 ENTER_STANDALONE_MODE = 0,
144 LEAVE_STANDALONE_MODE = 1,
145};
146
Edward O'Callaghancb338322019-02-27 19:25:18 +1100147/*
148 * These are not official designations; they are for use in flashrom only.
149 * Order must be preserved so that comparison operators work.
150 */
151enum protocol {
152 PROTOCOL_UNKNOWN,
153 PROTOCOL_V1,
154 PROTOCOL_V2,
155 PROTOCOL_V3,
156};
157
Edward O'Callaghana88395f2019-02-27 18:44:04 +1100158const struct dev_entry devs_dediprog[] = {
159 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
160
161 {0},
162};
163
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100164#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
165 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
166/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
167const char * LIBUSB_CALL libusb_error_name(int error_code)
David Hendricksf9ecb452015-11-04 15:40:27 -0800168{
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100169 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
170 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
171 static char my_libusb_error[18 + 5 + 2];
172 sprintf(my_libusb_error, "libusb error code %i", error_code);
173 return my_libusb_error;
174 } else {
175 return "UNKNOWN";
176 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800177}
178#endif
David Hendricksfe05e742015-08-15 17:29:39 -0700179
Edward O'Callaghancb338322019-02-27 19:25:18 +1100180static enum protocol protocol(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000181{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100182 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
David Hendricks6702e072015-08-15 18:09:04 -0700183 switch (dediprog_devicetype) {
184 case DEV_SF100:
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100185 case DEV_SF200:
186 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 5, 0))
187 return PROTOCOL_V1;
188 else
189 return PROTOCOL_V2;
David Hendricks6702e072015-08-15 18:09:04 -0700190 case DEV_SF600:
Edward O'Callaghancb338322019-02-27 19:25:18 +1100191 if (dediprog_firmwareversion < FIRMWARE_VERSION(6, 9, 0))
192 return PROTOCOL_V1;
193 else if (dediprog_firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
194 return PROTOCOL_V2;
195 else
196 return PROTOCOL_V3;
David Hendricks6702e072015-08-15 18:09:04 -0700197 default:
Edward O'Callaghancb338322019-02-27 19:25:18 +1100198 return PROTOCOL_UNKNOWN;
David Hendricks6702e072015-08-15 18:09:04 -0700199 }
hailfingerdfb32a02010-01-19 11:15:48 +0000200}
David Hendricks14e82e52015-08-15 17:59:03 -0700201
David Hendricksf9ecb452015-11-04 15:40:27 -0800202struct dediprog_transfer_status {
203 int error; /* OK if 0, ERROR else */
204 unsigned int queued_idx;
205 unsigned int finished_idx;
206};
207
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100208static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
David Hendricksf9ecb452015-11-04 15:40:27 -0800209{
210 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
211 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
212 status->error = 1;
213 msg_perr("SPI bulk read failed!\n");
214 }
215 ++status->finished_idx;
216}
217
218static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
219{
220 if (status->finished_idx >= status->queued_idx)
221 return 0;
222
223 do {
224 struct timeval timeout = { 10, 0 };
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100225 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
David Hendricksf9ecb452015-11-04 15:40:27 -0800226 if (ret < 0) {
227 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
228 return 1;
229 }
230 } while (finish && (status->finished_idx < status->queued_idx));
231 return 0;
232}
233
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100234static 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 -0700235{
David Hendricksf9ecb452015-11-04 15:40:27 -0800236 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
237 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700238}
239
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100240static 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 -0700241{
David Hendricksf9ecb452015-11-04 15:40:27 -0800242 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
243 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700244}
hailfingerdfb32a02010-01-19 11:15:48 +0000245
David Hendricksf9ecb452015-11-04 15:40:27 -0800246
David Hendricksfe05e742015-08-15 17:29:39 -0700247/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
stepan4c06de72011-01-28 09:00:15 +0000248static int dediprog_set_leds(int leds)
249{
David Hendricksfe05e742015-08-15 17:29:39 -0700250 if (leds < LED_NONE || leds > LED_ALL)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700251 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000252
David Hendricks14e82e52015-08-15 17:59:03 -0700253 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
254 * them around if we have an old device. On those devices the LEDs map as follows:
stepan4c06de72011-01-28 09:00:15 +0000255 * bit 2 == 0: green light is on.
David Hendricks14e82e52015-08-15 17:59:03 -0700256 * bit 0 == 0: red light is on.
257 *
258 * Additionally, the command structure has changed with the "new" protocol.
259 *
260 * FIXME: take IO pins into account
stepan4c06de72011-01-28 09:00:15 +0000261 */
David Hendricks14e82e52015-08-15 17:59:03 -0700262 int target_leds, ret;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100263 if (protocol() >= PROTOCOL_V2) {
David Hendricks14e82e52015-08-15 17:59:03 -0700264 target_leds = (leds ^ 7) << 8;
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100265 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
stepan4c06de72011-01-28 09:00:15 +0000266 } else {
David Hendricks14e82e52015-08-15 17:59:03 -0700267 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
268 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
269 } else {
270 target_leds = leds;
271 }
272 target_leds ^= 7;
273
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100274 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Simon Glass543101a2015-01-08 06:28:13 -0700275 }
David Hendricksfe05e742015-08-15 17:29:39 -0700276
stepan4c06de72011-01-28 09:00:15 +0000277 if (ret != 0x0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800278 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
stepan4c06de72011-01-28 09:00:15 +0000279 return 1;
280 }
281
stepan4c06de72011-01-28 09:00:15 +0000282 return 0;
283}
284
David Hendricksfe05e742015-08-15 17:29:39 -0700285struct dediprog_spispeeds {
286 const char *const name;
287 const int speed;
288};
289
290static const struct dediprog_spispeeds spispeeds[] = {
291 { "24M", 0x0 },
292 { "12M", 0x2 },
293 { "8M", 0x1 },
294 { "3M", 0x3 },
295 { "2.18M", 0x4 },
296 { "1.5M", 0x5 },
297 { "750k", 0x6 },
298 { "375k", 0x7 },
299 { NULL, 0x0 },
300};
301
302static int dediprog_set_spi_speed(unsigned int spispeed_idx)
hailfingerdfb32a02010-01-19 11:15:48 +0000303{
David Hendricksfe05e742015-08-15 17:29:39 -0700304 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100305 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
David Hendricksfe05e742015-08-15 17:29:39 -0700306 return 0;
307 }
hailfingerdfb32a02010-01-19 11:15:48 +0000308
David Hendricksfe05e742015-08-15 17:29:39 -0700309 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
310 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
hailfingerdfb32a02010-01-19 11:15:48 +0000311
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100312 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000313 if (ret != 0x0) {
David Hendricksfe05e742015-08-15 17:29:39 -0700314 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
hailfingerdfb32a02010-01-19 11:15:48 +0000315 return 1;
316 }
317 return 0;
318}
319
Edward O'Callaghancb338322019-02-27 19:25:18 +1100320static void fill_rw_cmd_payload(uint8_t *data_packet, unsigned int count, uint8_t dedi_spi_cmd,
321 unsigned int *value, unsigned int *idx, unsigned int start, int is_read) {
322 /* First 5 bytes are common in both generations. */
323 data_packet[0] = count & 0xff;
324 data_packet[1] = (count >> 8) & 0xff;
325 data_packet[2] = 0; /* RFU */
326 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
327 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
328
329 if (protocol() >= PROTOCOL_V2) {
330 *value = *idx = 0;
331 data_packet[5] = 0; /* RFU */
332 data_packet[6] = (start >> 0) & 0xff;
333 data_packet[7] = (start >> 8) & 0xff;
334 data_packet[8] = (start >> 16) & 0xff;
335 data_packet[9] = (start >> 24) & 0xff;
336 if (protocol() >= PROTOCOL_V3) {
337 if (is_read) {
338 data_packet[10] = 0x00; /* address length (3 or 4) */
339 data_packet[11] = 0x00; /* dummy cycle / 2 */
340 } else {
341 /* 16 LSBs and 16 HSBs of page size */
342 /* FIXME: This assumes page size of 256. */
343 data_packet[10] = 0x00;
344 data_packet[11] = 0x01;
345 data_packet[12] = 0x00;
346 data_packet[13] = 0x00;
347 }
348 }
349 } else {
350 *value = start % 0x10000;
351 *idx = start / 0x10000;
352 }
353}
354
hailfingerfb6287d2010-11-16 21:25:29 +0000355/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
356 * @start start address
357 * @len length
358 * @return 0 on success, 1 on failure
359 */
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100360static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000361{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100362 int err = 1;
363
hailfingerfb6287d2010-11-16 21:25:29 +0000364 /* chunksize must be 512, other sizes will NOT work at all. */
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100365 const unsigned int chunksize = 512;
stefanctc5eb8a92011-11-23 09:13:48 +0000366 const unsigned int count = len / chunksize;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100367
David Hendricksf9ecb452015-11-04 15:40:27 -0800368 struct dediprog_transfer_status status = { 0, 0, 0 };
369 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
370 struct libusb_transfer *transfer;
hailfingerfb6287d2010-11-16 21:25:29 +0000371
Edward O'Callaghancb338322019-02-27 19:25:18 +1100372 if (len == 0)
373 return 0;
374
hailfingerfb6287d2010-11-16 21:25:29 +0000375 if ((start % chunksize) || (len % chunksize)) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100376 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
377 __func__, start, len);
hailfingerfb6287d2010-11-16 21:25:29 +0000378 return 1;
379 }
380
Edward O'Callaghancb338322019-02-27 19:25:18 +1100381 int command_packet_size;
382 switch (protocol()) {
383 case PROTOCOL_V1:
384 command_packet_size = 5;
385 break;
386 case PROTOCOL_V2:
387 command_packet_size = 10;
388 break;
389 case PROTOCOL_V3:
390 command_packet_size = 12;
391 break;
392 default:
393 return 1;
David Hendricks14e82e52015-08-15 17:59:03 -0700394 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100395
396 uint8_t data_packet[command_packet_size];
397 unsigned int value, idx;
398 fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start, 1);
399
400 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
401 if (ret != sizeof(data_packet)) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800402 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
hailfingerfb6287d2010-11-16 21:25:29 +0000403 return 1;
404 }
405
David Hendricksf9ecb452015-11-04 15:40:27 -0800406 /*
407 * Ring buffer of bulk transfers.
408 * Poll until at least one transfer is ready,
409 * schedule next transfers until buffer is full.
410 */
hailfingerfb6287d2010-11-16 21:25:29 +0000411
David Hendricksf9ecb452015-11-04 15:40:27 -0800412 /* Allocate bulk transfers. */
Edward O'Callaghancb338322019-02-27 19:25:18 +1100413 unsigned int i;
David Hendricksf9ecb452015-11-04 15:40:27 -0800414 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
415 transfers[i] = libusb_alloc_transfer(0);
416 if (!transfers[i]) {
417 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100418 goto err_free;
419 }
420 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800421
422 /* Now transfer requested chunks using libusb's asynchronous interface. */
423 while (!status.error && (status.queued_idx < count)) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100424 while ((status.queued_idx < count) &&
425 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
426 {
David Hendricksf9ecb452015-11-04 15:40:27 -0800427 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
428 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
429 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
430 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
431 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
432 ret = libusb_submit_transfer(transfer);
433 if (ret < 0) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100434 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
435 status.queued_idx, libusb_error_name(ret));
436 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800437 }
438 ++status.queued_idx;
439 }
440 if (dediprog_bulk_read_poll(&status, 0))
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100441 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800442 }
443 /* Wait for transfers to finish. */
444 if (dediprog_bulk_read_poll(&status, 1))
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100445 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800446 /* Check if everything has been transmitted. */
447 if ((status.finished_idx < count) || status.error)
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100448 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800449
450 err = 0;
451
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100452err_free:
David Hendricksf9ecb452015-11-04 15:40:27 -0800453 dediprog_bulk_read_poll(&status, 1);
454 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
455 if (transfers[i]) libusb_free_transfer(transfers[i]);
456 return err;
hailfingerfb6287d2010-11-16 21:25:29 +0000457}
458
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100459static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000460{
hailfingerfb6287d2010-11-16 21:25:29 +0000461 int ret;
462 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000463 const unsigned int chunksize = 0x200;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100464 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000465 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000466
Simon Glasse53cc1d2015-01-08 05:48:51 -0700467 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000468
hailfingerfb6287d2010-11-16 21:25:29 +0000469 if (residue) {
470 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
471 start, residue);
Edward O'Callaghand2c93192020-10-09 12:56:53 +1100472 ret = default_spi_read(flash, buf, start, residue);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700473 if (ret)
474 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000475 }
476
477 /* Round down. */
478 bulklen = (len - residue) / chunksize * chunksize;
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100479 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700480 if (ret)
481 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000482
483 len -= residue + bulklen;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100484 if (len != 0) {
hailfingerfb6287d2010-11-16 21:25:29 +0000485 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
486 start, len);
Edward O'Callaghand2c93192020-10-09 12:56:53 +1100487 ret = default_spi_read(flash, buf + residue + bulklen,
488 start + residue + bulklen, len);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700489 if (ret)
490 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000491 }
492
Simon Glasse53cc1d2015-01-08 05:48:51 -0700493 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000494 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700495err:
496 dediprog_set_leds(LED_ERROR);
497 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000498}
499
David Hendricksfe05e742015-08-15 17:29:39 -0700500/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
501 * @chunksize length of data chunks, only 256 supported by now
502 * @start start address
503 * @len length
504 * @dedi_spi_cmd dediprog specific write command for spi bus
505 * @return 0 on success, 1 on failure
506 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700507static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
David Hendricksfe05e742015-08-15 17:29:39 -0700508 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
hailfinger556e9c32010-11-23 21:28:16 +0000509{
David Hendricksfe05e742015-08-15 17:29:39 -0700510 /* USB transfer size must be 512, other sizes will NOT work at all.
511 * chunksize is the real data size per USB bulk transfer. The remaining
512 * space in a USB bulk transfer must be filled with 0xff padding.
513 */
514 const unsigned int count = len / chunksize;
David Hendricksfe05e742015-08-15 17:29:39 -0700515
516 /*
517 * We should change this check to
518 * chunksize > 512
519 * once we know how to handle different chunk sizes.
520 */
521 if (chunksize != 256) {
522 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
523 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
524 return 1;
525 }
526
Edward O'Callaghand17149e2019-09-07 15:19:52 +1000527 /* No idea if the hardware can handle empty writes, so chicken out. */
528 if (len == 0)
529 return 0;
530
David Hendricksfe05e742015-08-15 17:29:39 -0700531 if ((start % chunksize) || (len % chunksize)) {
532 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
533 "at flashrom@flashrom.org\n", __func__, start, len);
534 return 1;
535 }
536
Edward O'Callaghancb338322019-02-27 19:25:18 +1100537 int command_packet_size;
538 switch (protocol()) {
539 case PROTOCOL_V1:
540 command_packet_size = 5;
541 break;
542 case PROTOCOL_V2:
543 command_packet_size = 10;
544 break;
545 case PROTOCOL_V3:
546 command_packet_size = 14;
547 break;
548 default:
549 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -0700550 }
551
Edward O'Callaghancb338322019-02-27 19:25:18 +1100552 uint8_t data_packet[command_packet_size];
553 unsigned int value, idx;
554 fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start, 0);
555 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
556 if (ret != sizeof(data_packet)) {
557 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
558 return 1;
559 }
560
561 unsigned int i;
David Hendricksfe05e742015-08-15 17:29:39 -0700562 for (i = 0; i < count; i++) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100563 unsigned char usbbuf[512];
David Hendricksfe05e742015-08-15 17:29:39 -0700564 memcpy(usbbuf, buf + i * chunksize, chunksize);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100565 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
566 int transferred;
567 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
568 DEFAULT_TIMEOUT);
David Hendricksf9ecb452015-11-04 15:40:27 -0800569 if ((ret < 0) || (transferred != 512)) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100570 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700571 return 1;
572 }
573 }
574
575 return 0;
576}
577
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700578static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
David Hendricksfe05e742015-08-15 17:29:39 -0700579 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
580{
581 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100582 const unsigned int chunksize = flash->chip->page_size;
David Hendricksfe05e742015-08-15 17:29:39 -0700583 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
584 unsigned int bulklen;
stepan4c06de72011-01-28 09:00:15 +0000585
Simon Glasse53cc1d2015-01-08 05:48:51 -0700586 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000587
David Hendricksfe05e742015-08-15 17:29:39 -0700588 if (chunksize != 256) {
589 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
590 "we don't know how dediprog\nhandles them.\n");
591 /* Write everything like it was residue. */
592 residue = len;
593 }
stepan4c06de72011-01-28 09:00:15 +0000594
David Hendricksfe05e742015-08-15 17:29:39 -0700595 if (residue) {
596 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
597 start, residue);
598 /* No idea about the real limit. Maybe 12, maybe more. */
599 ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 12);
600 if (ret) {
601 dediprog_set_leds(LED_ERROR);
602 return ret;
603 }
604 }
605
606 /* Round down. */
607 bulklen = (len - residue) / chunksize * chunksize;
608 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
609 if (ret) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700610 dediprog_set_leds(LED_ERROR);
David Hendricksfe05e742015-08-15 17:29:39 -0700611 return ret;
612 }
stepan4c06de72011-01-28 09:00:15 +0000613
David Hendricksfe05e742015-08-15 17:29:39 -0700614 len -= residue + bulklen;
615 if (len) {
616 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
617 start, len);
618 ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen),
619 start + residue + bulklen, len, 12);
620 if (ret) {
621 dediprog_set_leds(LED_ERROR);
622 return ret;
623 }
624 }
625
626 dediprog_set_leds(LED_PASS);
627 return 0;
hailfinger556e9c32010-11-23 21:28:16 +0000628}
629
Patrick Georgiab8353e2017-02-03 18:32:01 +0100630static 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 -0700631{
632 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
633}
634
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}
David Hendricksfe05e742015-08-15 17:29:39 -0700639
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100640static int dediprog_spi_send_command(const struct flashctx *flash,
641 unsigned int writecnt,
David Hendricksfe05e742015-08-15 17:29:39 -0700642 unsigned int readcnt,
643 const unsigned char *writearr,
644 unsigned char *readarr)
hailfingerdfb32a02010-01-19 11:15:48 +0000645{
646 int ret;
647
hailfingeraf389cc2010-01-22 02:53:30 +0000648 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Craig Hesling65eb8812019-08-01 09:33:56 -0700649 if (writecnt > UINT16_MAX) {
David Hendricksfe05e742015-08-15 17:29:39 -0700650 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
651 return 1;
Simon Glass543101a2015-01-08 06:28:13 -0700652 }
Craig Hesling65eb8812019-08-01 09:33:56 -0700653 if (readcnt > UINT16_MAX) {
David Hendricksfe05e742015-08-15 17:29:39 -0700654 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
655 return 1;
656 }
657
Edward O'Callaghancb338322019-02-27 19:25:18 +1100658 unsigned int idx, value;
659 /* New protocol has options and timeout combined as value while the old one used the value field for
660 * timeout and the index field for options. */
661 if (protocol() >= PROTOCOL_V2) {
662 idx = 0;
663 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
David Hendricks14e82e52015-08-15 17:59:03 -0700664 } else {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100665 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
666 value = 0;
David Hendricks14e82e52015-08-15 17:59:03 -0700667 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100668 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000669 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000670 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800671 writecnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000672 return 1;
673 }
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100674 if (readcnt == 0) // If we don't require a response, we are done here
hailfingerdfb32a02010-01-19 11:15:48 +0000675 return 0;
David Hendricksfe05e742015-08-15 17:29:39 -0700676
Edward O'Callaghancb338322019-02-27 19:25:18 +1100677 /* The specifications do state the possibility to set a timeout for transceive transactions.
678 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
679 * can predict the time needed by the previous command or so (untested). In any case, using this
680 * "feature" to set sane-looking timouts for the read below will completely trash performance with
681 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
682 *
683 * The specification also uses only 0 in its examples, so the lesson to learn here:
684 * "Never trust the description of an interface in the documentation but use the example code and pray."
685 const uint8_t read_timeout = 10 + readcnt/512;
686 if (protocol() >= PROTOCOL_V2) {
687 idx = 0;
688 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
689 } else {
690 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
691 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
692 }
693 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
694 */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100695 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000696 if (ret != readcnt) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100697 msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000698 return 1;
699 }
700 return 0;
701}
702
hailfinger1ff33dc2010-07-03 11:02:10 +0000703static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000704{
705 int ret;
706 char buf[0x11];
707
hailfingerdfb32a02010-01-19 11:15:48 +0000708 /* Command Receive Device String. */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100709 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000710 if (ret != 0x10) {
711 msg_perr("Incomplete/failed Command Receive Device String!\n");
712 return 1;
713 }
714 buf[0x10] = '\0';
715 msg_pdbg("Found a %s\n", buf);
David Hendricks6702e072015-08-15 18:09:04 -0700716 if (memcmp(buf, "SF100", 0x5) == 0)
717 dediprog_devicetype = DEV_SF100;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100718 else if (memcmp(buf, "SF200", 0x5) == 0)
719 dediprog_devicetype = DEV_SF200;
David Hendricks6702e072015-08-15 18:09:04 -0700720 else if (memcmp(buf, "SF600", 0x5) == 0)
721 dediprog_devicetype = DEV_SF600;
722 else {
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100723 msg_perr("Device not a SF100, SF200, 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
Edward O'Callaghan1ccbe422020-11-19 21:33:55 +1100796static int compar(const void *_x, const void *_y)
797{
798 const struct voltage_range *x = _x;
799 const struct voltage_range *y = _y;
800
801 /*
802 * qsort() places entries in ascending order. We will sort by minimum
803 * voltage primarily and max voltage secondarily, and move empty sets
804 * to the end of array.
805 */
806 if (x->min == 0)
807 return 1;
808 if (y->min == 0)
809 return -1;
810
811 if (x->min < y->min)
812 return -1;
813 if (x->min > y->min)
814 return 1;
815
816 if (x->min == y->min) {
817 if (x->max < y->max)
818 return -1;
819 if (x->max > y->max)
820 return 1;
821 }
822
823 return 0;
824}
825
826#define NUM_VOLTAGE_RANGES 16
827static struct voltage_range voltage_ranges[NUM_VOLTAGE_RANGES];
828
829/* returns number of unique voltage ranges, or <0 to indicate failure */
830static int flash_supported_voltage_ranges(enum chipbustype bus)
831{
832 int i;
833 int unique_ranges = 0;
834
835 /* clear array in case user calls this function multiple times */
836 memset(voltage_ranges, 0, sizeof(voltage_ranges));
837
838 for (i = 0; i < flashchips_size; i++) {
839 int j;
840 int match_found = 0;
841
842 if (unique_ranges >= NUM_VOLTAGE_RANGES) {
843 msg_cerr("Increase NUM_VOLTAGE_RANGES.\n");
844 return -1;
845 }
846
847 if (!(flashchips[i].bustype & bus))
848 continue;
849
850 for (j = 0; j < NUM_VOLTAGE_RANGES; j++) {
851 if ((flashchips[i].voltage.min == voltage_ranges[j].min) &&
852 (flashchips[i].voltage.max == voltage_ranges[j].max))
853 match_found |= 1;
854
855 if (!voltage_ranges[j].min && !voltage_ranges[j].max)
856 break;
857 }
858
859 if (!match_found) {
860 voltage_ranges[j] = flashchips[i].voltage;
861 unique_ranges++;
862 }
863 }
864
865 qsort(&voltage_ranges[0], NUM_VOLTAGE_RANGES,
866 sizeof(voltage_ranges[0]), compar);
867
868 for (i = 0; i < NUM_VOLTAGE_RANGES; i++) {
869 msg_cspew("%s: voltage_range[%d]: { %u, %u }\n",
870 __func__, i, voltage_ranges[i].min, voltage_ranges[i].max);
871 }
872
873 return unique_ranges;
874}
875
David Hendricks93784b42016-08-09 17:00:38 -0700876static int dediprog_set_spi_flash_voltage_auto(void)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800877{
David Hendricksac1d25c2016-08-09 17:00:58 -0700878 int i;
David Hendricks91ca7ac2015-11-20 16:22:22 -0800879 int spi_flash_ranges;
880
881 spi_flash_ranges = flash_supported_voltage_ranges(BUS_SPI);
882 if (spi_flash_ranges < 0)
883 return -1;
884
885 for (i = 0; i < ARRAY_SIZE(dediprog_supply_voltages); i++) {
886 int j;
887 int v = dediprog_supply_voltages[i]; /* shorthand */
888
889 for (j = 0; j < spi_flash_ranges; j++) {
890 /* Dediprog can supply a voltage in this range. */
891 if ((v >= voltage_ranges[j].min) && (v <= voltage_ranges[j].max)) {
David Hendricks93784b42016-08-09 17:00:38 -0700892 struct flashctx dummy;
893
David Hendricks91ca7ac2015-11-20 16:22:22 -0800894 msg_cdbg("%s: trying %d\n", __func__, v);
895 if (dediprog_set_spi_flash_voltage_manual(v)) {
896 msg_cerr("%s: Failed to set SPI voltage\n", __func__);
897 return 1;
898 }
899
900 clear_spi_id_cache();
Craig Heslingb73c39f2019-08-01 09:33:22 -0700901 // FIXME(quasisec): Passing NULL for the registered_master as we
902 // don't have something sensible in scope at this dispatch site.
Craig Heslingb73c39f2019-08-01 09:33:22 -0700903 if (probe_flash(NULL, 0, &dummy, 0) < 0) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800904 /* No dice, try next voltage supported by Dediprog. */
905 break;
906 }
907
Patrick Georgif3fa2992017-02-02 16:24:44 +0100908 if ((dummy.chip->manufacture_id == GENERIC_MANUF_ID) ||
909 (dummy.chip->model_id == GENERIC_DEVICE_ID)) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800910 break;
911 }
912
913 return 0;
914 }
915 }
916 }
917
918 return 1;
919}
920
921/* FIXME: ugly function signature */
David Hendricks93784b42016-08-09 17:00:38 -0700922static int dediprog_set_spi_voltage(int millivolt, int probe)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800923{
924 if (probe)
David Hendricks93784b42016-08-09 17:00:38 -0700925 return dediprog_set_spi_flash_voltage_auto();
David Hendricks91ca7ac2015-11-20 16:22:22 -0800926 else
927 return dediprog_set_spi_flash_voltage_manual(millivolt);
928}
929
David Hendricks28fbdfb2015-08-15 18:04:37 -0700930/*
931 * This command presumably sets the voltage for the SF100 itself (not the
932 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
933 * (including all SF600s) do not support it.
934 */
935static int dediprog_set_voltage(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000936{
Edward O'Callaghane749db02019-03-01 08:50:47 +1100937 unsigned char buf[1] = {0};
Edward O'Callaghan9b0182b2019-03-28 13:45:13 +1100938 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
939 buf, 0x1, DEFAULT_TIMEOUT);
hailfinger7a009082010-11-09 23:30:43 +0000940 if (ret < 0) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100941 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
hailfinger7a009082010-11-09 23:30:43 +0000942 return 1;
943 }
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100944 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -0700945 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000946 return 1;
947 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100948
hailfingerdfb32a02010-01-19 11:15:48 +0000949 return 0;
950}
951
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100952static int dediprog_standalone_mode(void)
David Hendrickscf453d82015-11-16 20:19:57 -0800953{
954 int ret;
955
956 if (dediprog_devicetype != DEV_SF600)
957 return 0;
958
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100959 msg_pdbg2("Disabling standalone mode.\n");
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100960 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Hendrickscf453d82015-11-16 20:19:57 -0800961 if (ret) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100962 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Hendrickscf453d82015-11-16 20:19:57 -0800963 return 1;
964 }
965
966 return 0;
967}
968
David Hendricksfe05e742015-08-15 17:29:39 -0700969static int set_target_flash(enum dediprog_target target)
970{
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100971 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
David Hendricksfe05e742015-08-15 17:29:39 -0700972 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800973 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000974 return 1;
975 }
976 return 0;
977}
978
David Hendricksfe05e742015-08-15 17:29:39 -0700979#if 0
980/* Returns true if the button is currently pressed. */
981static bool dediprog_get_button(void)
Simon Glassd01002b2015-01-08 05:44:16 -0700982{
David Hendricksfe05e742015-08-15 17:29:39 -0700983 char buf[1];
984 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
985 buf, 0x1, DEFAULT_TIMEOUT);
986 if (ret != 0) {
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100987 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700988 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700989 }
David Hendricksfe05e742015-08-15 17:29:39 -0700990 return buf[0] != 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700991}
David Hendricksfe05e742015-08-15 17:29:39 -0700992#endif
Simon Glassd01002b2015-01-08 05:44:16 -0700993
hailfingerf76cc322010-11-09 22:00:31 +0000994static int parse_voltage(char *voltage)
995{
996 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000997 int i;
998 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000999
1000 if (!voltage || !strlen(voltage)) {
1001 msg_perr("Empty voltage= specified.\n");
1002 return -1;
1003 }
1004 millivolt = (int)strtol(voltage, &tmp, 0);
1005 voltage = tmp;
1006 /* Handle "," and "." as decimal point. Everything after it is assumed
1007 * to be in decimal notation.
1008 */
1009 if ((*voltage == '.') || (*voltage == ',')) {
1010 voltage++;
1011 for (i = 0; i < 3; i++) {
1012 fraction *= 10;
1013 /* Don't advance if the current character is invalid,
1014 * but continue multiplying.
1015 */
1016 if ((*voltage < '0') || (*voltage > '9'))
1017 continue;
1018 fraction += *voltage - '0';
1019 voltage++;
1020 }
1021 /* Throw away remaining digits. */
1022 voltage += strspn(voltage, "0123456789");
1023 }
1024 /* The remaining string must be empty or "mV" or "V". */
1025 tolower_string(voltage);
1026
1027 /* No unit or "V". */
1028 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1029 millivolt *= 1000;
1030 millivolt += fraction;
1031 } else if (!strncmp(voltage, "mv", 2) ||
1032 !strncmp(voltage, "milliv", 6)) {
1033 /* No adjustment. fraction is discarded. */
1034 } else {
1035 /* Garbage at the end of the string. */
1036 msg_perr("Garbage voltage= specified.\n");
1037 return -1;
1038 }
1039 return millivolt;
1040}
1041
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +11001042static struct spi_master spi_master_dediprog = {
1043 .features = SPI_MASTER_NO_4BA_MODES,
1044 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
1045 .max_data_write = 16,
Craig Hesling65eb8812019-08-01 09:33:56 -07001046 .command = dediprog_spi_send_command,
1047 .multicommand = default_spi_send_multicommand,
1048 .read = dediprog_spi_read,
1049 .write_256 = dediprog_spi_write_256,
1050 .write_aai = dediprog_spi_write_aai,
1051};
mkarcherd264e9e2011-05-11 17:07:07 +00001052
David Hendricks93784b42016-08-09 17:00:38 -07001053static int dediprog_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +00001054{
1055 msg_pspew("%s\n", __func__);
1056
David Hendricks6702e072015-08-15 18:09:04 -07001057 dediprog_devicetype = DEV_UNKNOWN;
David Hendricksfe05e742015-08-15 17:29:39 -07001058
dhendrix0ffc2eb2011-06-14 01:35:36 +00001059 /* URB 28. Command Set SPI Voltage to 0. */
David Hendricks93784b42016-08-09 17:00:38 -07001060 if (dediprog_set_spi_voltage(0x0, 0))
dhendrix0ffc2eb2011-06-14 01:35:36 +00001061 return 1;
1062
David Hendricksf9ecb452015-11-04 15:40:27 -08001063 if (libusb_release_interface(dediprog_handle, 0)) {
dhendrix0ffc2eb2011-06-14 01:35:36 +00001064 msg_perr("Could not release USB interface!\n");
1065 return 1;
1066 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001067 libusb_close(dediprog_handle);
1068 libusb_exit(usb_ctx);
1069
dhendrix0ffc2eb2011-06-14 01:35:36 +00001070 return 0;
1071}
1072
hailfingerdfb32a02010-01-19 11:15:48 +00001073/* URB numbers refer to the first log ever captured. */
David Hendricksac1d25c2016-08-09 17:00:58 -07001074int dediprog_init(void)
hailfingerdfb32a02010-01-19 11:15:48 +00001075{
David Hendricks98b3c572016-11-30 01:50:08 +00001076 char *voltage, *device, *spispeed, *target_str;
David Hendricksfe05e742015-08-15 17:29:39 -07001077 int spispeed_idx = 1;
David Hendrickscd20ea42015-11-17 22:33:35 -08001078 int millivolt = 0;
David Hendricksfe05e742015-08-15 17:29:39 -07001079 long usedevice = 0;
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001080 long target = FLASH_TYPE_APPLICATION_FLASH_1;
David Hendricks98b3c572016-11-30 01:50:08 +00001081 int i, ret;
hailfingerdfb32a02010-01-19 11:15:48 +00001082
1083 msg_pspew("%s\n", __func__);
1084
David Hendricksfe05e742015-08-15 17:29:39 -07001085 spispeed = extract_programmer_param("spispeed");
1086 if (spispeed) {
1087 for (i = 0; spispeeds[i].name; ++i) {
1088 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1089 spispeed_idx = i;
1090 break;
1091 }
1092 }
1093 if (!spispeeds[i].name) {
1094 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
David Hendricks98b3c572016-11-30 01:50:08 +00001095 free(spispeed);
1096 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001097 }
David Hendricks98b3c572016-11-30 01:50:08 +00001098 free(spispeed);
David Hendricksfe05e742015-08-15 17:29:39 -07001099 }
1100
hailfingerf76cc322010-11-09 22:00:31 +00001101 voltage = extract_programmer_param("voltage");
1102 if (voltage) {
1103 millivolt = parse_voltage(voltage);
1104 free(voltage);
David Hendricks98b3c572016-11-30 01:50:08 +00001105 if (millivolt < 0)
1106 return 1;
hailfingerf76cc322010-11-09 22:00:31 +00001107 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1108 }
David Hendricksfe05e742015-08-15 17:29:39 -07001109
1110 device = extract_programmer_param("device");
1111 if (device) {
1112 char *dev_suffix;
1113 errno = 0;
1114 usedevice = strtol(device, &dev_suffix, 10);
1115 if (errno != 0 || device == dev_suffix) {
1116 msg_perr("Error: Could not convert 'device'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001117 free(device);
1118 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001119 }
David Hendricksfe05e742015-08-15 17:29:39 -07001120 if (usedevice < 0 || usedevice > UINT_MAX) {
1121 msg_perr("Error: Value for 'device' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001122 free(device);
1123 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001124 }
1125 if (strlen(dev_suffix) > 0) {
1126 msg_perr("Error: Garbage following 'device' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001127 free(device);
1128 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001129 }
1130 msg_pinfo("Using device %li.\n", usedevice);
Simon Glassd01002b2015-01-08 05:44:16 -07001131 }
David Hendricks98b3c572016-11-30 01:50:08 +00001132 free(device);
David Hendricksfe05e742015-08-15 17:29:39 -07001133
1134 target_str = extract_programmer_param("target");
1135 if (target_str) {
1136 char *target_suffix;
1137 errno = 0;
1138 target = strtol(target_str, &target_suffix, 10);
1139 if (errno != 0 || target_str == target_suffix) {
1140 msg_perr("Error: Could not convert 'target'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001141 free(target_str);
1142 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001143 }
1144 if (target < 1 || target > 2) {
1145 msg_perr("Error: Value for 'target' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001146 free(target_str);
1147 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001148 }
1149 if (strlen(target_suffix) > 0) {
1150 msg_perr("Error: Garbage following 'target' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001151 free(target_str);
1152 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001153 }
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001154 switch (target) {
1155 case 1:
1156 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1157 target = FLASH_TYPE_APPLICATION_FLASH_1;
1158 break;
1159 case 2:
1160 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1161 target = FLASH_TYPE_APPLICATION_FLASH_2;
1162 break;
1163 default:
1164 break;
1165 }
David Hendricksfe05e742015-08-15 17:29:39 -07001166 }
David Hendricks98b3c572016-11-30 01:50:08 +00001167 free(target_str);
hailfingerf76cc322010-11-09 22:00:31 +00001168
hailfingerdfb32a02010-01-19 11:15:48 +00001169 /* Here comes the USB stuff. */
David Hendricksf9ecb452015-11-04 15:40:27 -08001170 libusb_init(&usb_ctx);
1171 if (!usb_ctx) {
1172 msg_perr("Could not initialize libusb!\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001173 return 1;
hailfingerdfb32a02010-01-19 11:15:48 +00001174 }
Edward O'Callaghana88395f2019-02-27 18:44:04 +11001175 const uint16_t vid = devs_dediprog[0].vendor_id;
1176 const uint16_t pid = devs_dediprog[0].device_id;
1177 dediprog_handle = usb_dev_get_by_vid_pid_number(usb_ctx, vid, pid, (unsigned int) usedevice);
David Hendricksfe05e742015-08-15 17:29:39 -07001178 if (!dediprog_handle) {
David Hendricks22593802015-11-13 12:59:30 -08001179 msg_perr("Could not find a Dediprog programmer on USB!\n");
David Hendricksf9ecb452015-11-04 15:40:27 -08001180 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001181 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001182 }
David Hendricks98b3c572016-11-30 01:50:08 +00001183 ret = libusb_set_configuration(dediprog_handle, 1);
1184 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001185 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1186 libusb_close(dediprog_handle);
1187 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001188 return 1;
hailfingerfb6287d2010-11-16 21:25:29 +00001189 }
David Hendricks98b3c572016-11-30 01:50:08 +00001190 ret = libusb_claim_interface(dediprog_handle, 0);
1191 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001192 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1193 libusb_close(dediprog_handle);
1194 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001195 return 1;
hailfingerfb6287d2010-11-16 21:25:29 +00001196 }
David Hendricksfe05e742015-08-15 17:29:39 -07001197
David Hendricks98b3c572016-11-30 01:50:08 +00001198 if (register_shutdown(dediprog_shutdown, NULL))
1199 return 1;
dhendrix0ffc2eb2011-06-14 01:35:36 +00001200
David Hendricks28fbdfb2015-08-15 18:04:37 -07001201 /* Try reading the devicestring. If that fails and the device is old
1202 * (FW < 6.0.0) then we need to try the "set voltage" command and then
1203 * attempt to read the devicestring again. */
1204 if (dediprog_check_devicestring()) {
David Hendricks98b3c572016-11-30 01:50:08 +00001205 if (dediprog_set_voltage())
1206 return 1;
1207 if (dediprog_check_devicestring())
1208 return 1;
David Hendricks28fbdfb2015-08-15 18:04:37 -07001209 }
David Hendricksfe05e742015-08-15 17:29:39 -07001210
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001211 /* SF100/SF200 uses one in/out endpoint, SF600 uses separate in/out endpoints */
David Hendricksd7468582015-11-12 15:21:12 -08001212 dediprog_in_endpoint = 2;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001213 switch (dediprog_devicetype) {
1214 case DEV_SF100:
1215 case DEV_SF200:
David Hendricksd7468582015-11-12 15:21:12 -08001216 dediprog_out_endpoint = 2;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001217 break;
1218 default:
David Hendricksd7468582015-11-12 15:21:12 -08001219 dediprog_out_endpoint = 1;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001220 break;
1221 }
David Hendricksd7468582015-11-12 15:21:12 -08001222
David Hendricks93784b42016-08-09 17:00:38 -07001223
David Hendrickse4c73662015-11-20 16:26:07 -08001224 /* Set some LEDs as soon as possible to indicate activity.
David Hendricksfe05e742015-08-15 17:29:39 -07001225 * 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 +11001226 * dediprog_setup() has queried the device. */
David Hendrickse4c73662015-11-20 16:26:07 -08001227 dediprog_set_leds(LED_PASS | LED_BUSY);
David Hendricksfe05e742015-08-15 17:29:39 -07001228
David Hendrickscd20ea42015-11-17 22:33:35 -08001229 /* FIXME: need to do this so buses_supported gets SPI */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +01001230 register_spi_master(&spi_master_dediprog);
David Hendricks93784b42016-08-09 17:00:38 -07001231
David Hendricksfe05e742015-08-15 17:29:39 -07001232 /* Select target/socket, frequency and VCC. */
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001233 if (set_target_flash(target) ||
David Hendricksfe05e742015-08-15 17:29:39 -07001234 dediprog_set_spi_speed(spispeed_idx) ||
David Hendricks93784b42016-08-09 17:00:38 -07001235 dediprog_set_spi_voltage(millivolt, voltage ? 0 : 1)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -07001236 dediprog_set_leds(LED_ERROR);
David Hendricks98b3c572016-11-30 01:50:08 +00001237 return 1;
stepan4c06de72011-01-28 09:00:15 +00001238 }
hailfingerdfb32a02010-01-19 11:15:48 +00001239
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001240 if (dediprog_standalone_mode())
David Hendricks98b3c572016-11-30 01:50:08 +00001241 return 1;
1242
David Hendricks93784b42016-08-09 17:00:38 -07001243
David Hendricksfe05e742015-08-15 17:29:39 -07001244 dediprog_set_leds(LED_NONE);
stepan4c06de72011-01-28 09:00:15 +00001245
David Hendricks98b3c572016-11-30 01:50:08 +00001246 return 0;
hailfingerdfb32a02010-01-19 11:15:48 +00001247}