blob: 027574745ccc321e3ff1b70fd4d7d487b027e06c [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 */
David Hendricks4e4af372020-06-23 17:36:09 -070042#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
David Hendricksf9ecb452015-11-04 15:40:27 -080043#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;
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,
Edward O'Callaghancc0bf442019-02-27 22:35:44 +110054 DEV_SF200 = 200,
David Hendricks6702e072015-08-15 18:09:04 -070055 DEV_SF600 = 600,
56};
57
David Hendricksfe05e742015-08-15 17:29:39 -070058enum dediprog_leds {
59 LED_INVALID = -1,
60 LED_NONE = 0,
Simon Glasse53cc1d2015-01-08 05:48:51 -070061 LED_PASS = 1 << 0,
62 LED_BUSY = 1 << 1,
63 LED_ERROR = 1 << 2,
64 LED_ALL = 7,
65};
66
Simon Glassa1f80682015-01-08 05:55:29 -070067/* IO bits for CMD_SET_IO_LED message */
David Hendricksfe05e742015-08-15 17:29:39 -070068enum dediprog_ios {
Simon Glassa1f80682015-01-08 05:55:29 -070069 IO1 = 1 << 0,
70 IO2 = 1 << 1,
71 IO3 = 1 << 2,
72 IO4 = 1 << 3,
73};
74
David Hendricksfe05e742015-08-15 17:29:39 -070075enum dediprog_cmds {
76 CMD_TRANSCEIVE = 0x01,
77 CMD_POLL_STATUS_REG = 0x02,
78 CMD_SET_VPP = 0x03,
79 CMD_SET_TARGET = 0x04,
80 CMD_READ_EEPROM = 0x05,
81 CMD_WRITE_EEPROM = 0x06,
82 CMD_SET_IO_LED = 0x07,
83 CMD_READ_PROG_INFO = 0x08,
84 CMD_SET_VCC = 0x09,
85 CMD_SET_STANDALONE = 0x0A,
David Hendricks28fbdfb2015-08-15 18:04:37 -070086 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
David Hendricksfe05e742015-08-15 17:29:39 -070087 CMD_GET_BUTTON = 0x11,
88 CMD_GET_UID = 0x12,
89 CMD_SET_CS = 0x14,
90 CMD_IO_MODE = 0x15,
91 CMD_FW_UPDATE = 0x1A,
92 CMD_FPGA_UPDATE = 0x1B,
93 CMD_READ_FPGA_VERSION = 0x1C,
94 CMD_SET_HOLD = 0x1D,
95 CMD_READ = 0x20,
96 CMD_WRITE = 0x30,
97 CMD_WRITE_AT45DB = 0x31,
98 CMD_NAND_WRITE = 0x32,
99 CMD_NAND_READ = 0x33,
100 CMD_SET_SPI_CLK = 0x61,
101 CMD_CHECK_SOCKET = 0x62,
102 CMD_DOWNLOAD_PRJ = 0x63,
103 CMD_READ_PRJ_NAME = 0x64,
104 // New protocol/firmware only
105 CMD_CHECK_SDCARD = 0x65,
106 CMD_READ_PRJ = 0x66,
Simon Glassa1f80682015-01-08 05:55:29 -0700107};
108
David Hendricksfe05e742015-08-15 17:29:39 -0700109enum dediprog_target {
110 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
111 FLASH_TYPE_FLASH_CARD,
112 FLASH_TYPE_APPLICATION_FLASH_2,
113 FLASH_TYPE_SOCKET,
Simon Glassa1f80682015-01-08 05:55:29 -0700114};
115
David Hendricksfe05e742015-08-15 17:29:39 -0700116enum dediprog_readmode {
117 READ_MODE_STD = 1,
118 READ_MODE_FAST = 2,
119 READ_MODE_ATMEL45 = 3,
120 READ_MODE_4B_ADDR_FAST = 4,
121 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Simon Glassd01002b2015-01-08 05:44:16 -0700122};
123
David Hendricksfe05e742015-08-15 17:29:39 -0700124enum dediprog_writemode {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100125 WRITE_MODE_PAGE_PGM = 1,
David Hendricksfe05e742015-08-15 17:29:39 -0700126 WRITE_MODE_PAGE_WRITE = 2,
127 WRITE_MODE_1B_AAI = 3,
128 WRITE_MODE_2B_AAI = 4,
129 WRITE_MODE_128B_PAGE = 5,
130 WRITE_MODE_PAGE_AT26DF041 = 6,
131 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100132 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
David Hendricksfe05e742015-08-15 17:29:39 -0700133 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100134 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
David Hendricksfe05e742015-08-15 17:29:39 -0700135 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
136 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
Simon Glassd01002b2015-01-08 05:44:16 -0700137};
138
David Hendrickscf453d82015-11-16 20:19:57 -0800139enum dediprog_standalone_mode {
140 ENTER_STANDALONE_MODE = 0,
141 LEAVE_STANDALONE_MODE = 1,
142};
143
Edward O'Callaghancb338322019-02-27 19:25:18 +1100144/*
145 * These are not official designations; they are for use in flashrom only.
146 * Order must be preserved so that comparison operators work.
147 */
148enum protocol {
149 PROTOCOL_UNKNOWN,
150 PROTOCOL_V1,
151 PROTOCOL_V2,
152 PROTOCOL_V3,
153};
154
Edward O'Callaghana88395f2019-02-27 18:44:04 +1100155const struct dev_entry devs_dediprog[] = {
156 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
157
158 {0},
159};
160
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100161static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
162static enum dediprog_devtype dediprog_devicetype = DEV_UNKNOWN;
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
Nico Huberd2d28962019-03-21 15:42:54 +0100320static int prepare_rw_cmd(
Nico Huber040b3382018-09-30 01:18:43 +0200321 struct flashctx *const flash, uint8_t *data_packet, unsigned int count,
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100322 uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start, int is_read)
323{
324 if (count >= 1 << 16) {
325 msg_perr("%s: Unsupported transfer length of %u blocks! "
326 "Please report a bug at flashrom@flashrom.org\n",
327 __func__, count);
328 return 1;
329 }
330
Edward O'Callaghancb338322019-02-27 19:25:18 +1100331 /* First 5 bytes are common in both generations. */
332 data_packet[0] = count & 0xff;
333 data_packet[1] = (count >> 8) & 0xff;
334 data_packet[2] = 0; /* RFU */
335 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
336 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
337
338 if (protocol() >= PROTOCOL_V2) {
Nico Huber040b3382018-09-30 01:18:43 +0200339 if (is_read && flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
340 data_packet[3] = READ_MODE_4B_ADDR_FAST_0x0C;
341 data_packet[4] = JEDEC_READ_4BA_FAST;
342 } else if (dedi_spi_cmd == WRITE_MODE_PAGE_PGM
343 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
344 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
345 data_packet[4] = JEDEC_BYTE_PROGRAM_4BA;
346 }
347
Edward O'Callaghancb338322019-02-27 19:25:18 +1100348 *value = *idx = 0;
349 data_packet[5] = 0; /* RFU */
350 data_packet[6] = (start >> 0) & 0xff;
351 data_packet[7] = (start >> 8) & 0xff;
352 data_packet[8] = (start >> 16) & 0xff;
353 data_packet[9] = (start >> 24) & 0xff;
354 if (protocol() >= PROTOCOL_V3) {
355 if (is_read) {
356 data_packet[10] = 0x00; /* address length (3 or 4) */
357 data_packet[11] = 0x00; /* dummy cycle / 2 */
358 } else {
359 /* 16 LSBs and 16 HSBs of page size */
360 /* FIXME: This assumes page size of 256. */
361 data_packet[10] = 0x00;
362 data_packet[11] = 0x01;
363 data_packet[12] = 0x00;
364 data_packet[13] = 0x00;
365 }
366 }
367 } else {
Nico Huberd2d28962019-03-21 15:42:54 +0100368 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
369 if (spi_set_extended_address(flash, start >> 24))
370 return 1;
371 } else if (start >> 24) {
372 msg_cerr("Can't handle 4-byte address with dediprog.\n");
373 return 1;
374 }
375 /*
376 * We don't know how the dediprog firmware handles 4-byte
377 * addresses. So let's not tell it what we are doing and
378 * only send the lower 3 bytes.
379 */
380 *value = start & 0xffff;
381 *idx = (start >> 16) & 0xff;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100382 }
Nico Huberd2d28962019-03-21 15:42:54 +0100383
384 return 0;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100385}
386
hailfingerfb6287d2010-11-16 21:25:29 +0000387/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
388 * @start start address
389 * @len length
390 * @return 0 on success, 1 on failure
391 */
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100392static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000393{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100394 int err = 1;
395
hailfingerfb6287d2010-11-16 21:25:29 +0000396 /* chunksize must be 512, other sizes will NOT work at all. */
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100397 const unsigned int chunksize = 512;
stefanctc5eb8a92011-11-23 09:13:48 +0000398 const unsigned int count = len / chunksize;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100399
David Hendricksf9ecb452015-11-04 15:40:27 -0800400 struct dediprog_transfer_status status = { 0, 0, 0 };
401 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
402 struct libusb_transfer *transfer;
hailfingerfb6287d2010-11-16 21:25:29 +0000403
Edward O'Callaghancb338322019-02-27 19:25:18 +1100404 if (len == 0)
405 return 0;
406
hailfingerfb6287d2010-11-16 21:25:29 +0000407 if ((start % chunksize) || (len % chunksize)) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100408 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
409 __func__, start, len);
hailfingerfb6287d2010-11-16 21:25:29 +0000410 return 1;
411 }
412
Edward O'Callaghancb338322019-02-27 19:25:18 +1100413 int command_packet_size;
414 switch (protocol()) {
415 case PROTOCOL_V1:
416 command_packet_size = 5;
417 break;
418 case PROTOCOL_V2:
419 command_packet_size = 10;
420 break;
421 case PROTOCOL_V3:
422 command_packet_size = 12;
423 break;
424 default:
425 return 1;
David Hendricks14e82e52015-08-15 17:59:03 -0700426 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100427
428 uint8_t data_packet[command_packet_size];
429 unsigned int value, idx;
Nico Huberd2d28962019-03-21 15:42:54 +0100430 if (prepare_rw_cmd(flash, data_packet, count, READ_MODE_STD, &value, &idx, start, 1))
431 return 1;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100432
433 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100434 if (ret != (int)sizeof(data_packet)) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800435 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
hailfingerfb6287d2010-11-16 21:25:29 +0000436 return 1;
437 }
438
David Hendricksf9ecb452015-11-04 15:40:27 -0800439 /*
440 * Ring buffer of bulk transfers.
441 * Poll until at least one transfer is ready,
442 * schedule next transfers until buffer is full.
443 */
hailfingerfb6287d2010-11-16 21:25:29 +0000444
David Hendricksf9ecb452015-11-04 15:40:27 -0800445 /* Allocate bulk transfers. */
Edward O'Callaghancb338322019-02-27 19:25:18 +1100446 unsigned int i;
David Hendricksf9ecb452015-11-04 15:40:27 -0800447 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
448 transfers[i] = libusb_alloc_transfer(0);
449 if (!transfers[i]) {
450 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100451 goto err_free;
452 }
453 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800454
455 /* Now transfer requested chunks using libusb's asynchronous interface. */
456 while (!status.error && (status.queued_idx < count)) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100457 while ((status.queued_idx < count) &&
458 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
459 {
David Hendricksf9ecb452015-11-04 15:40:27 -0800460 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
461 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
462 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
463 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
464 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
465 ret = libusb_submit_transfer(transfer);
466 if (ret < 0) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100467 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
468 status.queued_idx, libusb_error_name(ret));
469 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800470 }
471 ++status.queued_idx;
472 }
473 if (dediprog_bulk_read_poll(&status, 0))
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100474 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800475 }
476 /* Wait for transfers to finish. */
477 if (dediprog_bulk_read_poll(&status, 1))
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100478 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800479 /* Check if everything has been transmitted. */
480 if ((status.finished_idx < count) || status.error)
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100481 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800482
483 err = 0;
484
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100485err_free:
David Hendricksf9ecb452015-11-04 15:40:27 -0800486 dediprog_bulk_read_poll(&status, 1);
487 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
488 if (transfers[i]) libusb_free_transfer(transfers[i]);
489 return err;
hailfingerfb6287d2010-11-16 21:25:29 +0000490}
491
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100492static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000493{
hailfingerfb6287d2010-11-16 21:25:29 +0000494 int ret;
495 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000496 const unsigned int chunksize = 0x200;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100497 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000498 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000499
Simon Glasse53cc1d2015-01-08 05:48:51 -0700500 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000501
hailfingerfb6287d2010-11-16 21:25:29 +0000502 if (residue) {
503 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
504 start, residue);
Edward O'Callaghand2c93192020-10-09 12:56:53 +1100505 ret = default_spi_read(flash, buf, start, residue);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700506 if (ret)
507 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000508 }
509
510 /* Round down. */
511 bulklen = (len - residue) / chunksize * chunksize;
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100512 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700513 if (ret)
514 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000515
516 len -= residue + bulklen;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100517 if (len != 0) {
hailfingerfb6287d2010-11-16 21:25:29 +0000518 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
519 start, len);
Edward O'Callaghand2c93192020-10-09 12:56:53 +1100520 ret = default_spi_read(flash, buf + residue + bulklen,
521 start + residue + bulklen, len);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700522 if (ret)
523 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000524 }
525
Simon Glasse53cc1d2015-01-08 05:48:51 -0700526 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000527 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700528err:
529 dediprog_set_leds(LED_ERROR);
530 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000531}
532
David Hendricksfe05e742015-08-15 17:29:39 -0700533/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
534 * @chunksize length of data chunks, only 256 supported by now
535 * @start start address
536 * @len length
537 * @dedi_spi_cmd dediprog specific write command for spi bus
538 * @return 0 on success, 1 on failure
539 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700540static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
David Hendricksfe05e742015-08-15 17:29:39 -0700541 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
hailfinger556e9c32010-11-23 21:28:16 +0000542{
David Hendricksfe05e742015-08-15 17:29:39 -0700543 /* USB transfer size must be 512, other sizes will NOT work at all.
544 * chunksize is the real data size per USB bulk transfer. The remaining
545 * space in a USB bulk transfer must be filled with 0xff padding.
546 */
547 const unsigned int count = len / chunksize;
David Hendricksfe05e742015-08-15 17:29:39 -0700548
549 /*
550 * We should change this check to
551 * chunksize > 512
552 * once we know how to handle different chunk sizes.
553 */
554 if (chunksize != 256) {
555 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
556 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
557 return 1;
558 }
559
560 if ((start % chunksize) || (len % chunksize)) {
561 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
562 "at flashrom@flashrom.org\n", __func__, start, len);
563 return 1;
564 }
565
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100566 /* No idea if the hardware can handle empty writes, so chicken out. */
567 if (len == 0)
568 return 0;
569
Edward O'Callaghancb338322019-02-27 19:25:18 +1100570 int command_packet_size;
571 switch (protocol()) {
572 case PROTOCOL_V1:
573 command_packet_size = 5;
574 break;
575 case PROTOCOL_V2:
576 command_packet_size = 10;
577 break;
578 case PROTOCOL_V3:
579 command_packet_size = 14;
580 break;
581 default:
582 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -0700583 }
584
Edward O'Callaghancb338322019-02-27 19:25:18 +1100585 uint8_t data_packet[command_packet_size];
586 unsigned int value, idx;
Nico Huberd2d28962019-03-21 15:42:54 +0100587 if (prepare_rw_cmd(flash, data_packet, count, dedi_spi_cmd, &value, &idx, start, 0))
588 return 1;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100589 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100590 if (ret != (int)sizeof(data_packet)) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100591 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
592 return 1;
593 }
594
595 unsigned int i;
David Hendricksfe05e742015-08-15 17:29:39 -0700596 for (i = 0; i < count; i++) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100597 unsigned char usbbuf[512];
David Hendricksfe05e742015-08-15 17:29:39 -0700598 memcpy(usbbuf, buf + i * chunksize, chunksize);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100599 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
600 int transferred;
601 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
602 DEFAULT_TIMEOUT);
David Hendricksf9ecb452015-11-04 15:40:27 -0800603 if ((ret < 0) || (transferred != 512)) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100604 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700605 return 1;
606 }
607 }
608
609 return 0;
610}
611
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700612static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
David Hendricksfe05e742015-08-15 17:29:39 -0700613 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
614{
615 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100616 const unsigned int chunksize = flash->chip->page_size;
David Hendricksfe05e742015-08-15 17:29:39 -0700617 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
618 unsigned int bulklen;
stepan4c06de72011-01-28 09:00:15 +0000619
Simon Glasse53cc1d2015-01-08 05:48:51 -0700620 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000621
David Hendricksfe05e742015-08-15 17:29:39 -0700622 if (chunksize != 256) {
623 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
624 "we don't know how dediprog\nhandles them.\n");
625 /* Write everything like it was residue. */
626 residue = len;
627 }
stepan4c06de72011-01-28 09:00:15 +0000628
David Hendricksfe05e742015-08-15 17:29:39 -0700629 if (residue) {
630 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
631 start, residue);
Nico Huber040b3382018-09-30 01:18:43 +0200632 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
633 ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 11);
David Hendricksfe05e742015-08-15 17:29:39 -0700634 if (ret) {
635 dediprog_set_leds(LED_ERROR);
636 return ret;
637 }
638 }
639
640 /* Round down. */
641 bulklen = (len - residue) / chunksize * chunksize;
642 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
643 if (ret) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700644 dediprog_set_leds(LED_ERROR);
David Hendricksfe05e742015-08-15 17:29:39 -0700645 return ret;
646 }
stepan4c06de72011-01-28 09:00:15 +0000647
David Hendricksfe05e742015-08-15 17:29:39 -0700648 len -= residue + bulklen;
649 if (len) {
650 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
651 start, len);
652 ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen),
Nico Huber040b3382018-09-30 01:18:43 +0200653 start + residue + bulklen, len, 11);
David Hendricksfe05e742015-08-15 17:29:39 -0700654 if (ret) {
655 dediprog_set_leds(LED_ERROR);
656 return ret;
657 }
658 }
659
660 dediprog_set_leds(LED_PASS);
661 return 0;
hailfinger556e9c32010-11-23 21:28:16 +0000662}
663
Patrick Georgiab8353e2017-02-03 18:32:01 +0100664static 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 -0700665{
666 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
667}
668
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700669static 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 -0700670{
671 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
672}
David Hendricksfe05e742015-08-15 17:29:39 -0700673
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100674static int dediprog_spi_send_command(const struct flashctx *flash,
675 unsigned int writecnt,
David Hendricksfe05e742015-08-15 17:29:39 -0700676 unsigned int readcnt,
677 const unsigned char *writearr,
678 unsigned char *readarr)
hailfingerdfb32a02010-01-19 11:15:48 +0000679{
680 int ret;
681
hailfingeraf389cc2010-01-22 02:53:30 +0000682 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Craig Hesling65eb8812019-08-01 09:33:56 -0700683 if (writecnt > UINT16_MAX) {
David Hendricksfe05e742015-08-15 17:29:39 -0700684 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
685 return 1;
Simon Glass543101a2015-01-08 06:28:13 -0700686 }
Craig Hesling65eb8812019-08-01 09:33:56 -0700687 if (readcnt > UINT16_MAX) {
David Hendricksfe05e742015-08-15 17:29:39 -0700688 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
689 return 1;
690 }
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100691
Edward O'Callaghancb338322019-02-27 19:25:18 +1100692 unsigned int idx, value;
693 /* New protocol has options and timeout combined as value while the old one used the value field for
694 * timeout and the index field for options. */
695 if (protocol() >= PROTOCOL_V2) {
696 idx = 0;
697 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
David Hendricks14e82e52015-08-15 17:59:03 -0700698 } else {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100699 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
700 value = 0;
David Hendricks14e82e52015-08-15 17:59:03 -0700701 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100702 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100703 if (ret != (int)writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000704 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800705 writecnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000706 return 1;
707 }
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100708 if (readcnt == 0) // If we don't require a response, we are done here
hailfingerdfb32a02010-01-19 11:15:48 +0000709 return 0;
David Hendricksfe05e742015-08-15 17:29:39 -0700710
Edward O'Callaghancb338322019-02-27 19:25:18 +1100711 /* The specifications do state the possibility to set a timeout for transceive transactions.
712 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
713 * can predict the time needed by the previous command or so (untested). In any case, using this
714 * "feature" to set sane-looking timouts for the read below will completely trash performance with
715 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
716 *
717 * The specification also uses only 0 in its examples, so the lesson to learn here:
718 * "Never trust the description of an interface in the documentation but use the example code and pray."
719 const uint8_t read_timeout = 10 + readcnt/512;
720 if (protocol() >= PROTOCOL_V2) {
721 idx = 0;
722 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
723 } else {
724 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
725 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
726 }
727 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
728 */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100729 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100730 if (ret != (int)readcnt) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100731 msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000732 return 1;
733 }
734 return 0;
735}
736
hailfinger1ff33dc2010-07-03 11:02:10 +0000737static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000738{
739 int ret;
740 char buf[0x11];
741
hailfingerdfb32a02010-01-19 11:15:48 +0000742 /* Command Receive Device String. */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100743 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000744 if (ret != 0x10) {
745 msg_perr("Incomplete/failed Command Receive Device String!\n");
746 return 1;
747 }
748 buf[0x10] = '\0';
749 msg_pdbg("Found a %s\n", buf);
David Hendricks6702e072015-08-15 18:09:04 -0700750 if (memcmp(buf, "SF100", 0x5) == 0)
751 dediprog_devicetype = DEV_SF100;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100752 else if (memcmp(buf, "SF200", 0x5) == 0)
753 dediprog_devicetype = DEV_SF200;
David Hendricks6702e072015-08-15 18:09:04 -0700754 else if (memcmp(buf, "SF600", 0x5) == 0)
755 dediprog_devicetype = DEV_SF600;
756 else {
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100757 msg_perr("Device not a SF100, SF200, or SF600!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000758 return 1;
759 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100760
761 int sfnum;
762 int fw[3];
763 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100764 sfnum != (int)dediprog_devicetype) {
David Hendricks14e82e52015-08-15 17:59:03 -0700765 msg_perr("Unexpected firmware version string '%s'\n", buf);
hailfinger7a009082010-11-09 23:30:43 +0000766 return 1;
767 }
David Hendricks14e82e52015-08-15 17:59:03 -0700768 /* Only these major versions were tested. */
David Hendricks6702e072015-08-15 18:09:04 -0700769 if (fw[0] < 2 || fw[0] > 7) {
David Hendricks14e82e52015-08-15 17:59:03 -0700770 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000771 return 1;
772 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100773
stepan4c06de72011-01-28 09:00:15 +0000774 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100775 if (protocol() == PROTOCOL_UNKNOWN) {
776 msg_perr("Internal error: Unable to determine protocol version.\n");
777 return 1;
778 }
David Hendricks14e82e52015-08-15 17:59:03 -0700779
hailfingerdfb32a02010-01-19 11:15:48 +0000780 return 0;
781}
782
David Hendricks91ca7ac2015-11-20 16:22:22 -0800783static int dediprog_supply_voltages[] = {
784 0, 1800, 2500, 3500,
785};
786
787static int dediprog_set_spi_flash_voltage_manual(int millivolt)
788{
789 int ret;
790 uint16_t voltage_selector;
791
792 switch (millivolt) {
793 case 0:
794 /* Admittedly this one is an assumption. */
795 voltage_selector = 0x0;
796 break;
797 case 1800:
798 voltage_selector = 0x12;
799 break;
800 case 2500:
801 voltage_selector = 0x11;
802 break;
803 case 3500:
804 voltage_selector = 0x10;
805 break;
806 default:
807 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
808 return 1;
809 }
810 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
811 millivolt % 1000);
812
813 if (voltage_selector == 0) {
814 /* Wait some time as the original driver does. */
815 programmer_delay(200 * 1000);
816 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100817 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
David Hendricks91ca7ac2015-11-20 16:22:22 -0800818 if (ret != 0x0) {
819 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
820 voltage_selector);
821 return 1;
822 }
823 if (voltage_selector != 0) {
824 /* Wait some time as the original driver does. */
825 programmer_delay(200 * 1000);
826 }
827 return 0;
828}
829
Edward O'Callaghan1ccbe422020-11-19 21:33:55 +1100830static int compar(const void *_x, const void *_y)
831{
832 const struct voltage_range *x = _x;
833 const struct voltage_range *y = _y;
834
835 /*
836 * qsort() places entries in ascending order. We will sort by minimum
837 * voltage primarily and max voltage secondarily, and move empty sets
838 * to the end of array.
839 */
840 if (x->min == 0)
841 return 1;
842 if (y->min == 0)
843 return -1;
844
845 if (x->min < y->min)
846 return -1;
847 if (x->min > y->min)
848 return 1;
849
850 if (x->min == y->min) {
851 if (x->max < y->max)
852 return -1;
853 if (x->max > y->max)
854 return 1;
855 }
856
857 return 0;
858}
859
860#define NUM_VOLTAGE_RANGES 16
861static struct voltage_range voltage_ranges[NUM_VOLTAGE_RANGES];
862
863/* returns number of unique voltage ranges, or <0 to indicate failure */
864static int flash_supported_voltage_ranges(enum chipbustype bus)
865{
866 int i;
867 int unique_ranges = 0;
868
869 /* clear array in case user calls this function multiple times */
870 memset(voltage_ranges, 0, sizeof(voltage_ranges));
871
872 for (i = 0; i < flashchips_size; i++) {
873 int j;
874 int match_found = 0;
875
876 if (unique_ranges >= NUM_VOLTAGE_RANGES) {
877 msg_cerr("Increase NUM_VOLTAGE_RANGES.\n");
878 return -1;
879 }
880
881 if (!(flashchips[i].bustype & bus))
882 continue;
883
884 for (j = 0; j < NUM_VOLTAGE_RANGES; j++) {
885 if ((flashchips[i].voltage.min == voltage_ranges[j].min) &&
886 (flashchips[i].voltage.max == voltage_ranges[j].max))
887 match_found |= 1;
888
889 if (!voltage_ranges[j].min && !voltage_ranges[j].max)
890 break;
891 }
892
893 if (!match_found) {
894 voltage_ranges[j] = flashchips[i].voltage;
895 unique_ranges++;
896 }
897 }
898
899 qsort(&voltage_ranges[0], NUM_VOLTAGE_RANGES,
900 sizeof(voltage_ranges[0]), compar);
901
902 for (i = 0; i < NUM_VOLTAGE_RANGES; i++) {
903 msg_cspew("%s: voltage_range[%d]: { %u, %u }\n",
904 __func__, i, voltage_ranges[i].min, voltage_ranges[i].max);
905 }
906
907 return unique_ranges;
908}
909
David Hendricks93784b42016-08-09 17:00:38 -0700910static int dediprog_set_spi_flash_voltage_auto(void)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800911{
David Hendricksac1d25c2016-08-09 17:00:58 -0700912 int i;
David Hendricks91ca7ac2015-11-20 16:22:22 -0800913 int spi_flash_ranges;
914
915 spi_flash_ranges = flash_supported_voltage_ranges(BUS_SPI);
916 if (spi_flash_ranges < 0)
917 return -1;
918
919 for (i = 0; i < ARRAY_SIZE(dediprog_supply_voltages); i++) {
920 int j;
921 int v = dediprog_supply_voltages[i]; /* shorthand */
922
923 for (j = 0; j < spi_flash_ranges; j++) {
924 /* Dediprog can supply a voltage in this range. */
925 if ((v >= voltage_ranges[j].min) && (v <= voltage_ranges[j].max)) {
David Hendricks93784b42016-08-09 17:00:38 -0700926 struct flashctx dummy;
927
David Hendricks91ca7ac2015-11-20 16:22:22 -0800928 msg_cdbg("%s: trying %d\n", __func__, v);
929 if (dediprog_set_spi_flash_voltage_manual(v)) {
930 msg_cerr("%s: Failed to set SPI voltage\n", __func__);
931 return 1;
932 }
933
934 clear_spi_id_cache();
Craig Heslingb73c39f2019-08-01 09:33:22 -0700935 // FIXME(quasisec): Passing NULL for the registered_master as we
936 // don't have something sensible in scope at this dispatch site.
Craig Heslingb73c39f2019-08-01 09:33:22 -0700937 if (probe_flash(NULL, 0, &dummy, 0) < 0) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800938 /* No dice, try next voltage supported by Dediprog. */
939 break;
940 }
941
Patrick Georgif3fa2992017-02-02 16:24:44 +0100942 if ((dummy.chip->manufacture_id == GENERIC_MANUF_ID) ||
943 (dummy.chip->model_id == GENERIC_DEVICE_ID)) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800944 break;
945 }
946
947 return 0;
948 }
949 }
950 }
951
952 return 1;
953}
954
955/* FIXME: ugly function signature */
David Hendricks93784b42016-08-09 17:00:38 -0700956static int dediprog_set_spi_voltage(int millivolt, int probe)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800957{
958 if (probe)
David Hendricks93784b42016-08-09 17:00:38 -0700959 return dediprog_set_spi_flash_voltage_auto();
David Hendricks91ca7ac2015-11-20 16:22:22 -0800960 else
961 return dediprog_set_spi_flash_voltage_manual(millivolt);
962}
963
David Hendricks28fbdfb2015-08-15 18:04:37 -0700964/*
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -0700965 * Read the id from the dediprog. This should return the numeric part of the
966 * serial number found on a sticker on the back of the dediprog. Note this
967 * number is stored in writable eeprom, so it could get out of sync. Also note,
968 * this function only supports SF100 at this time, but SF600 support is not too
969 * much different.
970 * @return the id on success, -1 on failure
971 */
972static int dediprog_read_id(void)
973{
974 int ret;
975 uint8_t buf[3];
976
977 ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN,
978 0x7, /* request */
979 0, /* value */
980 0xEF00, /* index */
981 buf, sizeof(buf),
982 DEFAULT_TIMEOUT);
983 if (ret != sizeof(buf)) {
984 msg_perr("Failed to read dediprog id, error %d!\n", ret);
985 return -1;
986 }
987
988 return buf[0] << 16 | buf[1] << 8 | buf[2];
989}
990
991/*
David Hendricks28fbdfb2015-08-15 18:04:37 -0700992 * This command presumably sets the voltage for the SF100 itself (not the
993 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
994 * (including all SF600s) do not support it.
995 */
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100996
997/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
998 * Only use dediprog_set_voltage on SF100 programmers with firmware older
999 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
David Hendricks28fbdfb2015-08-15 18:04:37 -07001000static int dediprog_set_voltage(void)
hailfingerdfb32a02010-01-19 11:15:48 +00001001{
Edward O'Callaghane749db02019-03-01 08:50:47 +11001002 unsigned char buf[1] = {0};
Edward O'Callaghan9b0182b2019-03-28 13:45:13 +11001003 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
1004 buf, 0x1, DEFAULT_TIMEOUT);
hailfinger7a009082010-11-09 23:30:43 +00001005 if (ret < 0) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001006 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
hailfinger7a009082010-11-09 23:30:43 +00001007 return 1;
1008 }
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001009 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -07001010 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +00001011 return 1;
1012 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +11001013
hailfingerdfb32a02010-01-19 11:15:48 +00001014 return 0;
1015}
1016
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001017static int dediprog_standalone_mode(void)
David Hendrickscf453d82015-11-16 20:19:57 -08001018{
1019 int ret;
1020
1021 if (dediprog_devicetype != DEV_SF600)
1022 return 0;
1023
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001024 msg_pdbg2("Disabling standalone mode.\n");
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +11001025 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Hendrickscf453d82015-11-16 20:19:57 -08001026 if (ret) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001027 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Hendrickscf453d82015-11-16 20:19:57 -08001028 return 1;
1029 }
1030
1031 return 0;
1032}
1033
David Hendricksfe05e742015-08-15 17:29:39 -07001034static int set_target_flash(enum dediprog_target target)
1035{
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +11001036 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
David Hendricksfe05e742015-08-15 17:29:39 -07001037 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001038 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +00001039 return 1;
1040 }
1041 return 0;
1042}
1043
David Hendricksfe05e742015-08-15 17:29:39 -07001044#if 0
1045/* Returns true if the button is currently pressed. */
1046static bool dediprog_get_button(void)
Simon Glassd01002b2015-01-08 05:44:16 -07001047{
David Hendricksfe05e742015-08-15 17:29:39 -07001048 char buf[1];
1049 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
1050 buf, 0x1, DEFAULT_TIMEOUT);
1051 if (ret != 0) {
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +11001052 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -07001053 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001054 }
David Hendricksfe05e742015-08-15 17:29:39 -07001055 return buf[0] != 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001056}
David Hendricksfe05e742015-08-15 17:29:39 -07001057#endif
Simon Glassd01002b2015-01-08 05:44:16 -07001058
hailfingerf76cc322010-11-09 22:00:31 +00001059static int parse_voltage(char *voltage)
1060{
1061 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +00001062 int i;
1063 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +00001064
1065 if (!voltage || !strlen(voltage)) {
1066 msg_perr("Empty voltage= specified.\n");
1067 return -1;
1068 }
1069 millivolt = (int)strtol(voltage, &tmp, 0);
1070 voltage = tmp;
1071 /* Handle "," and "." as decimal point. Everything after it is assumed
1072 * to be in decimal notation.
1073 */
1074 if ((*voltage == '.') || (*voltage == ',')) {
1075 voltage++;
1076 for (i = 0; i < 3; i++) {
1077 fraction *= 10;
1078 /* Don't advance if the current character is invalid,
1079 * but continue multiplying.
1080 */
1081 if ((*voltage < '0') || (*voltage > '9'))
1082 continue;
1083 fraction += *voltage - '0';
1084 voltage++;
1085 }
1086 /* Throw away remaining digits. */
1087 voltage += strspn(voltage, "0123456789");
1088 }
1089 /* The remaining string must be empty or "mV" or "V". */
1090 tolower_string(voltage);
1091
1092 /* No unit or "V". */
1093 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1094 millivolt *= 1000;
1095 millivolt += fraction;
1096 } else if (!strncmp(voltage, "mv", 2) ||
1097 !strncmp(voltage, "milliv", 6)) {
1098 /* No adjustment. fraction is discarded. */
1099 } else {
1100 /* Garbage at the end of the string. */
1101 msg_perr("Garbage voltage= specified.\n");
1102 return -1;
1103 }
1104 return millivolt;
1105}
1106
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +11001107static struct spi_master spi_master_dediprog = {
1108 .features = SPI_MASTER_NO_4BA_MODES,
1109 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
1110 .max_data_write = 16,
Craig Hesling65eb8812019-08-01 09:33:56 -07001111 .command = dediprog_spi_send_command,
1112 .multicommand = default_spi_send_multicommand,
1113 .read = dediprog_spi_read,
1114 .write_256 = dediprog_spi_write_256,
1115 .write_aai = dediprog_spi_write_aai,
1116};
mkarcherd264e9e2011-05-11 17:07:07 +00001117
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001118/*
1119 * Open a dediprog_handle with the USB device at the given index.
1120 * @index index of the USB device
1121 * @return 0 for success, -1 for error, -2 for busy device
1122 */
1123static int dediprog_open(int index)
1124{
1125 const uint16_t vid = devs_dediprog[0].vendor_id;
1126 const uint16_t pid = devs_dediprog[0].device_id;
1127 int ret;
1128
1129 dediprog_handle = usb_dev_get_by_vid_pid_number(usb_ctx, vid, pid, (unsigned int) index);
1130 if (!dediprog_handle) {
1131 msg_perr("Could not find a Dediprog programmer on USB.\n");
1132 libusb_exit(usb_ctx);
1133 return -1;
1134 }
1135 ret = libusb_set_configuration(dediprog_handle, 1);
1136 if (ret != 0) {
1137 msg_perr("Could not set USB device configuration: %i %s\n",
1138 ret, libusb_error_name(ret));
1139 libusb_close(dediprog_handle);
1140 return -2;
1141 }
1142 ret = libusb_claim_interface(dediprog_handle, 0);
1143 if (ret < 0) {
1144 msg_perr("Could not claim USB device interface %i: %i %s\n",
1145 0, ret, libusb_error_name(ret));
1146 libusb_close(dediprog_handle);
1147 return -2;
1148 }
1149 return 0;
1150}
1151
David Hendricks93784b42016-08-09 17:00:38 -07001152static int dediprog_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +00001153{
David Hendricks6702e072015-08-15 18:09:04 -07001154 dediprog_devicetype = DEV_UNKNOWN;
David Hendricksfe05e742015-08-15 17:29:39 -07001155
dhendrix0ffc2eb2011-06-14 01:35:36 +00001156 /* URB 28. Command Set SPI Voltage to 0. */
David Hendricks93784b42016-08-09 17:00:38 -07001157 if (dediprog_set_spi_voltage(0x0, 0))
dhendrix0ffc2eb2011-06-14 01:35:36 +00001158 return 1;
1159
David Hendricksf9ecb452015-11-04 15:40:27 -08001160 if (libusb_release_interface(dediprog_handle, 0)) {
dhendrix0ffc2eb2011-06-14 01:35:36 +00001161 msg_perr("Could not release USB interface!\n");
1162 return 1;
1163 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001164 libusb_close(dediprog_handle);
1165 libusb_exit(usb_ctx);
1166
dhendrix0ffc2eb2011-06-14 01:35:36 +00001167 return 0;
1168}
1169
David Hendricksac1d25c2016-08-09 17:00:58 -07001170int dediprog_init(void)
hailfingerdfb32a02010-01-19 11:15:48 +00001171{
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001172 char *voltage, *id_str, *device, *spispeed, *target_str;
David Hendricksfe05e742015-08-15 17:29:39 -07001173 int spispeed_idx = 1;
David Hendrickscd20ea42015-11-17 22:33:35 -08001174 int millivolt = 0;
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001175 int id = -1; /* -1 defaults to enumeration order */
1176 int found_id;
David Hendricksfe05e742015-08-15 17:29:39 -07001177 long usedevice = 0;
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001178 long target = FLASH_TYPE_APPLICATION_FLASH_1;
David Hendricks98b3c572016-11-30 01:50:08 +00001179 int i, ret;
hailfingerdfb32a02010-01-19 11:15:48 +00001180
David Hendricksfe05e742015-08-15 17:29:39 -07001181 spispeed = extract_programmer_param("spispeed");
1182 if (spispeed) {
1183 for (i = 0; spispeeds[i].name; ++i) {
1184 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1185 spispeed_idx = i;
1186 break;
1187 }
1188 }
1189 if (!spispeeds[i].name) {
1190 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
David Hendricks98b3c572016-11-30 01:50:08 +00001191 free(spispeed);
1192 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001193 }
David Hendricks98b3c572016-11-30 01:50:08 +00001194 free(spispeed);
David Hendricksfe05e742015-08-15 17:29:39 -07001195 }
1196
hailfingerf76cc322010-11-09 22:00:31 +00001197 voltage = extract_programmer_param("voltage");
1198 if (voltage) {
1199 millivolt = parse_voltage(voltage);
1200 free(voltage);
David Hendricks98b3c572016-11-30 01:50:08 +00001201 if (millivolt < 0)
1202 return 1;
hailfingerf76cc322010-11-09 22:00:31 +00001203 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1204 }
David Hendricksfe05e742015-08-15 17:29:39 -07001205
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001206 id_str = extract_programmer_param("id");
1207 if (id_str) {
1208 char prefix0, prefix1;
1209 if (sscanf(id_str, "%c%c%d", &prefix0, &prefix1, &id) != 3) {
1210 msg_perr("Error: Could not parse dediprog 'id'.\n");
1211 msg_perr("Expected a string like SF012345 or DP012345.\n");
1212 free(id_str);
1213 return 1;
1214 }
1215 if (id < 0 || id >= 0x1000000) {
1216 msg_perr("Error: id %s is out of range!\n", id_str);
1217 free(id_str);
1218 return 1;
1219 }
1220 if (!(prefix0 == 'S' && prefix1 == 'F') && !(prefix0 == 'D' && prefix1 == 'P')) {
1221 msg_perr("Error: %s is an invalid id!\n", id_str);
1222 free(id_str);
1223 return 1;
1224 }
1225 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1226 }
1227 free(id_str);
1228
David Hendricksfe05e742015-08-15 17:29:39 -07001229 device = extract_programmer_param("device");
1230 if (device) {
1231 char *dev_suffix;
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001232 if (id != -1) {
1233 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1234 }
David Hendricksfe05e742015-08-15 17:29:39 -07001235 errno = 0;
1236 usedevice = strtol(device, &dev_suffix, 10);
1237 if (errno != 0 || device == dev_suffix) {
1238 msg_perr("Error: Could not convert 'device'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001239 free(device);
1240 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001241 }
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +11001242 if (usedevice < 0 || usedevice > INT_MAX) {
David Hendricksfe05e742015-08-15 17:29:39 -07001243 msg_perr("Error: Value for 'device' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001244 free(device);
1245 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001246 }
1247 if (strlen(dev_suffix) > 0) {
1248 msg_perr("Error: Garbage following 'device' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001249 free(device);
1250 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001251 }
1252 msg_pinfo("Using device %li.\n", usedevice);
Simon Glassd01002b2015-01-08 05:44:16 -07001253 }
David Hendricks98b3c572016-11-30 01:50:08 +00001254 free(device);
David Hendricksfe05e742015-08-15 17:29:39 -07001255
1256 target_str = extract_programmer_param("target");
1257 if (target_str) {
1258 char *target_suffix;
1259 errno = 0;
1260 target = strtol(target_str, &target_suffix, 10);
1261 if (errno != 0 || target_str == target_suffix) {
1262 msg_perr("Error: Could not convert 'target'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001263 free(target_str);
1264 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001265 }
1266 if (target < 1 || target > 2) {
1267 msg_perr("Error: Value for 'target' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001268 free(target_str);
1269 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001270 }
1271 if (strlen(target_suffix) > 0) {
1272 msg_perr("Error: Garbage following 'target' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001273 free(target_str);
1274 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001275 }
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001276 switch (target) {
1277 case 1:
1278 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1279 target = FLASH_TYPE_APPLICATION_FLASH_1;
1280 break;
1281 case 2:
1282 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1283 target = FLASH_TYPE_APPLICATION_FLASH_2;
1284 break;
1285 default:
1286 break;
1287 }
David Hendricksfe05e742015-08-15 17:29:39 -07001288 }
David Hendricks98b3c572016-11-30 01:50:08 +00001289 free(target_str);
hailfingerf76cc322010-11-09 22:00:31 +00001290
hailfingerdfb32a02010-01-19 11:15:48 +00001291 /* Here comes the USB stuff. */
David Hendricksf9ecb452015-11-04 15:40:27 -08001292 libusb_init(&usb_ctx);
1293 if (!usb_ctx) {
1294 msg_perr("Could not initialize libusb!\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001295 return 1;
hailfingerdfb32a02010-01-19 11:15:48 +00001296 }
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001297
1298 if (id != -1) {
1299 for (i = 0; ; i++) {
1300 ret = dediprog_open(i);
1301 if (ret == -1) {
1302 /* no dev */
1303 libusb_exit(usb_ctx);
1304 return 1;
1305 } else if (ret == -2) {
1306 /* busy dev */
1307 continue;
1308 }
1309
1310 /* Notice we can only call dediprog_read_id() after
1311 * libusb_set_configuration() and
1312 * libusb_claim_interface(). When searching by id and
1313 * either configuration or claim fails (usually the
1314 * device is in use by another instance of flashrom),
1315 * the device is skipped and the next device is tried.
1316 */
1317 found_id = dediprog_read_id();
1318 if (found_id < 0) {
1319 msg_perr("Could not read id.\n");
1320 libusb_release_interface(dediprog_handle, 0);
1321 libusb_close(dediprog_handle);
1322 continue;
1323 }
1324 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1325 if (found_id != id) {
1326 libusb_release_interface(dediprog_handle, 0);
1327 libusb_close(dediprog_handle);
1328 continue;
1329 }
1330 break;
1331 }
1332 } else {
1333 if (dediprog_open(usedevice)) {
1334 return 1;
1335 }
1336 found_id = dediprog_read_id();
David Hendricksfe05e742015-08-15 17:29:39 -07001337 }
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001338
1339 if (found_id >= 0) {
1340 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
hailfingerfb6287d2010-11-16 21:25:29 +00001341 }
David Hendricksfe05e742015-08-15 17:29:39 -07001342
David Hendricks98b3c572016-11-30 01:50:08 +00001343 if (register_shutdown(dediprog_shutdown, NULL))
1344 return 1;
dhendrix0ffc2eb2011-06-14 01:35:36 +00001345
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +11001346 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1347 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
David Hendricks28fbdfb2015-08-15 18:04:37 -07001348 if (dediprog_check_devicestring()) {
David Hendricks98b3c572016-11-30 01:50:08 +00001349 if (dediprog_set_voltage())
1350 return 1;
1351 if (dediprog_check_devicestring())
1352 return 1;
David Hendricks28fbdfb2015-08-15 18:04:37 -07001353 }
David Hendricksfe05e742015-08-15 17:29:39 -07001354
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001355 /* SF100/SF200 uses one in/out endpoint, SF600 uses separate in/out endpoints */
David Hendricksd7468582015-11-12 15:21:12 -08001356 dediprog_in_endpoint = 2;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001357 switch (dediprog_devicetype) {
1358 case DEV_SF100:
1359 case DEV_SF200:
David Hendricksd7468582015-11-12 15:21:12 -08001360 dediprog_out_endpoint = 2;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001361 break;
1362 default:
David Hendricksd7468582015-11-12 15:21:12 -08001363 dediprog_out_endpoint = 1;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001364 break;
1365 }
David Hendricksd7468582015-11-12 15:21:12 -08001366
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +11001367 /* Set all possible LEDs as soon as possible to indicate activity.
David Hendricksfe05e742015-08-15 17:29:39 -07001368 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +11001369 * dediprog_check_devicestring() has queried the device. */
1370 dediprog_set_leds(LED_ALL);
David Hendricksfe05e742015-08-15 17:29:39 -07001371
1372 /* Select target/socket, frequency and VCC. */
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001373 if (set_target_flash(target) ||
David Hendricksfe05e742015-08-15 17:29:39 -07001374 dediprog_set_spi_speed(spispeed_idx) ||
David Hendricks93784b42016-08-09 17:00:38 -07001375 dediprog_set_spi_voltage(millivolt, voltage ? 0 : 1)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -07001376 dediprog_set_leds(LED_ERROR);
David Hendricks98b3c572016-11-30 01:50:08 +00001377 return 1;
stepan4c06de72011-01-28 09:00:15 +00001378 }
hailfingerdfb32a02010-01-19 11:15:48 +00001379
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001380 if (dediprog_standalone_mode())
David Hendricks98b3c572016-11-30 01:50:08 +00001381 return 1;
1382
Nico Huberd2d28962019-03-21 15:42:54 +01001383 if (dediprog_devicetype == DEV_SF100 && protocol() == PROTOCOL_V1)
1384 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1385
Patrick Rudolph1edb44e2019-06-04 12:21:10 +02001386 if (protocol() == PROTOCOL_V2)
Nico Huber040b3382018-09-30 01:18:43 +02001387 spi_master_dediprog.features |= SPI_MASTER_4BA;
David Hendricks93784b42016-08-09 17:00:38 -07001388
Nico Huber040b3382018-09-30 01:18:43 +02001389 if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE))
1390 return 1;
stepan4c06de72011-01-28 09:00:15 +00001391
David Hendricks98b3c572016-11-30 01:50:08 +00001392 return 0;
hailfingerdfb32a02010-01-19 11:15:48 +00001393}