blob: aebf1ad7501c1e8bcd7578b78d9304d6cfea99b3 [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
Edward O'Callaghanc6a36112021-01-22 14:52:52 +110020#include <sys/types.h>
David Hendricksfe05e742015-08-15 17:29:39 -070021#include <stdlib.h>
hailfinger7a009082010-11-09 23:30:43 +000022#include <stdio.h>
hailfingerdfb32a02010-01-19 11:15:48 +000023#include <string.h>
David Hendricksfe05e742015-08-15 17:29:39 -070024#include <limits.h>
25#include <errno.h>
David Hendricksf9ecb452015-11-04 15:40:27 -080026#include <libusb.h>
hailfingerdfb32a02010-01-19 11:15:48 +000027#include "flash.h"
David Hendrickscd20ea42015-11-17 22:33:35 -080028#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000029#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000030#include "programmer.h"
hailfingerdfb32a02010-01-19 11:15:48 +000031#include "spi.h"
32
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +110033/* LIBUSB_CALL ensures the right calling conventions on libusb callbacks.
34 * However, the macro is not defined everywhere. m(
35 */
36#ifndef LIBUSB_CALL
37#define LIBUSB_CALL
38#endif
39
stepan4c06de72011-01-28 09:00:15 +000040#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
hailfingerdfb32a02010-01-19 11:15:48 +000041#define DEFAULT_TIMEOUT 3000
David Hendricksf9ecb452015-11-04 15:40:27 -080042#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
David Hendricks4e4af372020-06-23 17:36:09 -070043#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
David Hendricksf9ecb452015-11-04 15:40:27 -080044#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
45#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
46#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
Edward O'Callaghanef4e28b2019-06-28 13:18:41 +100047static struct libusb_context *usb_ctx;
David Hendricksf9ecb452015-11-04 15:40:27 -080048static libusb_device_handle *dediprog_handle;
David Hendricksd7468582015-11-12 15:21:12 -080049static int dediprog_in_endpoint;
50static int dediprog_out_endpoint;
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
David Hendricksfe05e742015-08-15 17:29:39 -070059enum dediprog_leds {
60 LED_INVALID = -1,
61 LED_NONE = 0,
Simon Glasse53cc1d2015-01-08 05:48:51 -070062 LED_PASS = 1 << 0,
63 LED_BUSY = 1 << 1,
64 LED_ERROR = 1 << 2,
65 LED_ALL = 7,
66};
67
Simon Glassa1f80682015-01-08 05:55:29 -070068/* IO bits for CMD_SET_IO_LED message */
David Hendricksfe05e742015-08-15 17:29:39 -070069enum dediprog_ios {
Simon Glassa1f80682015-01-08 05:55:29 -070070 IO1 = 1 << 0,
71 IO2 = 1 << 1,
72 IO3 = 1 << 2,
73 IO4 = 1 << 3,
74};
75
David Hendricksfe05e742015-08-15 17:29:39 -070076enum dediprog_cmds {
77 CMD_TRANSCEIVE = 0x01,
78 CMD_POLL_STATUS_REG = 0x02,
79 CMD_SET_VPP = 0x03,
80 CMD_SET_TARGET = 0x04,
81 CMD_READ_EEPROM = 0x05,
82 CMD_WRITE_EEPROM = 0x06,
83 CMD_SET_IO_LED = 0x07,
84 CMD_READ_PROG_INFO = 0x08,
85 CMD_SET_VCC = 0x09,
86 CMD_SET_STANDALONE = 0x0A,
David Hendricks28fbdfb2015-08-15 18:04:37 -070087 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
David Hendricksfe05e742015-08-15 17:29:39 -070088 CMD_GET_BUTTON = 0x11,
89 CMD_GET_UID = 0x12,
90 CMD_SET_CS = 0x14,
91 CMD_IO_MODE = 0x15,
92 CMD_FW_UPDATE = 0x1A,
93 CMD_FPGA_UPDATE = 0x1B,
94 CMD_READ_FPGA_VERSION = 0x1C,
95 CMD_SET_HOLD = 0x1D,
96 CMD_READ = 0x20,
97 CMD_WRITE = 0x30,
98 CMD_WRITE_AT45DB = 0x31,
99 CMD_NAND_WRITE = 0x32,
100 CMD_NAND_READ = 0x33,
101 CMD_SET_SPI_CLK = 0x61,
102 CMD_CHECK_SOCKET = 0x62,
103 CMD_DOWNLOAD_PRJ = 0x63,
104 CMD_READ_PRJ_NAME = 0x64,
105 // New protocol/firmware only
106 CMD_CHECK_SDCARD = 0x65,
107 CMD_READ_PRJ = 0x66,
Simon Glassa1f80682015-01-08 05:55:29 -0700108};
109
David Hendricksfe05e742015-08-15 17:29:39 -0700110enum dediprog_target {
111 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
112 FLASH_TYPE_FLASH_CARD,
113 FLASH_TYPE_APPLICATION_FLASH_2,
114 FLASH_TYPE_SOCKET,
Simon Glassa1f80682015-01-08 05:55:29 -0700115};
116
David Hendricksfe05e742015-08-15 17:29:39 -0700117enum dediprog_readmode {
118 READ_MODE_STD = 1,
119 READ_MODE_FAST = 2,
120 READ_MODE_ATMEL45 = 3,
121 READ_MODE_4B_ADDR_FAST = 4,
122 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Simon Glassd01002b2015-01-08 05:44:16 -0700123};
124
David Hendricksfe05e742015-08-15 17:29:39 -0700125enum dediprog_writemode {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100126 WRITE_MODE_PAGE_PGM = 1,
David Hendricksfe05e742015-08-15 17:29:39 -0700127 WRITE_MODE_PAGE_WRITE = 2,
128 WRITE_MODE_1B_AAI = 3,
129 WRITE_MODE_2B_AAI = 4,
130 WRITE_MODE_128B_PAGE = 5,
131 WRITE_MODE_PAGE_AT26DF041 = 6,
132 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100133 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
David Hendricksfe05e742015-08-15 17:29:39 -0700134 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100135 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
David Hendricksfe05e742015-08-15 17:29:39 -0700136 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
137 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
Simon Glassd01002b2015-01-08 05:44:16 -0700138};
139
David Hendrickscf453d82015-11-16 20:19:57 -0800140enum dediprog_standalone_mode {
141 ENTER_STANDALONE_MODE = 0,
142 LEAVE_STANDALONE_MODE = 1,
143};
144
Edward O'Callaghancb338322019-02-27 19:25:18 +1100145/*
146 * These are not official designations; they are for use in flashrom only.
147 * Order must be preserved so that comparison operators work.
148 */
149enum protocol {
150 PROTOCOL_UNKNOWN,
151 PROTOCOL_V1,
152 PROTOCOL_V2,
153 PROTOCOL_V3,
154};
155
Edward O'Callaghana88395f2019-02-27 18:44:04 +1100156const struct dev_entry devs_dediprog[] = {
157 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
158
159 {0},
160};
161
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100162static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
163static enum dediprog_devtype dediprog_devicetype = DEV_UNKNOWN;
164
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100165#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
166 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
167/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
168const char * LIBUSB_CALL libusb_error_name(int error_code)
David Hendricksf9ecb452015-11-04 15:40:27 -0800169{
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100170 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
171 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
172 static char my_libusb_error[18 + 5 + 2];
173 sprintf(my_libusb_error, "libusb error code %i", error_code);
174 return my_libusb_error;
175 } else {
176 return "UNKNOWN";
177 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800178}
179#endif
David Hendricksfe05e742015-08-15 17:29:39 -0700180
Edward O'Callaghancb338322019-02-27 19:25:18 +1100181static enum protocol protocol(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000182{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100183 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
David Hendricks6702e072015-08-15 18:09:04 -0700184 switch (dediprog_devicetype) {
185 case DEV_SF100:
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100186 case DEV_SF200:
187 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 5, 0))
188 return PROTOCOL_V1;
189 else
190 return PROTOCOL_V2;
David Hendricks6702e072015-08-15 18:09:04 -0700191 case DEV_SF600:
Edward O'Callaghancb338322019-02-27 19:25:18 +1100192 if (dediprog_firmwareversion < FIRMWARE_VERSION(6, 9, 0))
193 return PROTOCOL_V1;
194 else if (dediprog_firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
195 return PROTOCOL_V2;
196 else
197 return PROTOCOL_V3;
David Hendricks6702e072015-08-15 18:09:04 -0700198 default:
Edward O'Callaghancb338322019-02-27 19:25:18 +1100199 return PROTOCOL_UNKNOWN;
David Hendricks6702e072015-08-15 18:09:04 -0700200 }
hailfingerdfb32a02010-01-19 11:15:48 +0000201}
David Hendricks14e82e52015-08-15 17:59:03 -0700202
David Hendricksf9ecb452015-11-04 15:40:27 -0800203struct dediprog_transfer_status {
204 int error; /* OK if 0, ERROR else */
205 unsigned int queued_idx;
206 unsigned int finished_idx;
207};
208
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100209static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
David Hendricksf9ecb452015-11-04 15:40:27 -0800210{
211 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
212 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
213 status->error = 1;
214 msg_perr("SPI bulk read failed!\n");
215 }
216 ++status->finished_idx;
217}
218
219static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
220{
221 if (status->finished_idx >= status->queued_idx)
222 return 0;
223
224 do {
225 struct timeval timeout = { 10, 0 };
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +1100226 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
David Hendricksf9ecb452015-11-04 15:40:27 -0800227 if (ret < 0) {
228 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
229 return 1;
230 }
231 } while (finish && (status->finished_idx < status->queued_idx));
232 return 0;
233}
234
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100235static 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 -0700236{
David Hendricksf9ecb452015-11-04 15:40:27 -0800237 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
238 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700239}
240
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100241static 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 -0700242{
David Hendricksf9ecb452015-11-04 15:40:27 -0800243 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
244 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700245}
hailfingerdfb32a02010-01-19 11:15:48 +0000246
David Hendricksf9ecb452015-11-04 15:40:27 -0800247
David Hendricksfe05e742015-08-15 17:29:39 -0700248/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
stepan4c06de72011-01-28 09:00:15 +0000249static int dediprog_set_leds(int leds)
250{
David Hendricksfe05e742015-08-15 17:29:39 -0700251 if (leds < LED_NONE || leds > LED_ALL)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700252 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000253
David Hendricks14e82e52015-08-15 17:59:03 -0700254 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
255 * them around if we have an old device. On those devices the LEDs map as follows:
stepan4c06de72011-01-28 09:00:15 +0000256 * bit 2 == 0: green light is on.
David Hendricks14e82e52015-08-15 17:59:03 -0700257 * bit 0 == 0: red light is on.
258 *
259 * Additionally, the command structure has changed with the "new" protocol.
260 *
261 * FIXME: take IO pins into account
stepan4c06de72011-01-28 09:00:15 +0000262 */
David Hendricks14e82e52015-08-15 17:59:03 -0700263 int target_leds, ret;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100264 if (protocol() >= PROTOCOL_V2) {
David Hendricks14e82e52015-08-15 17:59:03 -0700265 target_leds = (leds ^ 7) << 8;
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100266 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
stepan4c06de72011-01-28 09:00:15 +0000267 } else {
David Hendricks14e82e52015-08-15 17:59:03 -0700268 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
269 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
270 } else {
271 target_leds = leds;
272 }
273 target_leds ^= 7;
274
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100275 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Simon Glass543101a2015-01-08 06:28:13 -0700276 }
David Hendricksfe05e742015-08-15 17:29:39 -0700277
stepan4c06de72011-01-28 09:00:15 +0000278 if (ret != 0x0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800279 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
stepan4c06de72011-01-28 09:00:15 +0000280 return 1;
281 }
282
stepan4c06de72011-01-28 09:00:15 +0000283 return 0;
284}
285
Edward O'Callaghan809735b2020-11-28 18:12:56 +1100286static int dediprog_set_spi_flash_voltage_manual(int millivolt)
287{
288 int ret;
289 uint16_t voltage_selector;
290
291 switch (millivolt) {
292 case 0:
293 /* Admittedly this one is an assumption. */
294 voltage_selector = 0x0;
295 break;
296 case 1800:
297 voltage_selector = 0x12;
298 break;
299 case 2500:
300 voltage_selector = 0x11;
301 break;
302 case 3500:
303 voltage_selector = 0x10;
304 break;
305 default:
306 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
307 return 1;
308 }
309 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
310 millivolt % 1000);
311
312 if (voltage_selector == 0) {
313 /* Wait some time as the original driver does. */
314 programmer_delay(200 * 1000);
315 }
316 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
317 if (ret != 0x0) {
318 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
319 voltage_selector);
320 return 1;
321 }
322 if (voltage_selector != 0) {
323 /* Wait some time as the original driver does. */
324 programmer_delay(200 * 1000);
325 }
326 return 0;
327}
328
David Hendricksfe05e742015-08-15 17:29:39 -0700329struct dediprog_spispeeds {
330 const char *const name;
331 const int speed;
332};
333
334static const struct dediprog_spispeeds spispeeds[] = {
335 { "24M", 0x0 },
336 { "12M", 0x2 },
337 { "8M", 0x1 },
338 { "3M", 0x3 },
339 { "2.18M", 0x4 },
340 { "1.5M", 0x5 },
341 { "750k", 0x6 },
342 { "375k", 0x7 },
343 { NULL, 0x0 },
344};
345
346static int dediprog_set_spi_speed(unsigned int spispeed_idx)
hailfingerdfb32a02010-01-19 11:15:48 +0000347{
David Hendricksfe05e742015-08-15 17:29:39 -0700348 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100349 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
David Hendricksfe05e742015-08-15 17:29:39 -0700350 return 0;
351 }
hailfingerdfb32a02010-01-19 11:15:48 +0000352
David Hendricksfe05e742015-08-15 17:29:39 -0700353 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
354 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
hailfingerdfb32a02010-01-19 11:15:48 +0000355
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100356 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000357 if (ret != 0x0) {
David Hendricksfe05e742015-08-15 17:29:39 -0700358 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
hailfingerdfb32a02010-01-19 11:15:48 +0000359 return 1;
360 }
361 return 0;
362}
363
Nico Huberd2d28962019-03-21 15:42:54 +0100364static int prepare_rw_cmd(
Nico Huber040b3382018-09-30 01:18:43 +0200365 struct flashctx *const flash, uint8_t *data_packet, unsigned int count,
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100366 uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start, int is_read)
367{
368 if (count >= 1 << 16) {
369 msg_perr("%s: Unsupported transfer length of %u blocks! "
370 "Please report a bug at flashrom@flashrom.org\n",
371 __func__, count);
372 return 1;
373 }
374
Edward O'Callaghancb338322019-02-27 19:25:18 +1100375 /* First 5 bytes are common in both generations. */
376 data_packet[0] = count & 0xff;
377 data_packet[1] = (count >> 8) & 0xff;
378 data_packet[2] = 0; /* RFU */
379 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
380 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
381
382 if (protocol() >= PROTOCOL_V2) {
Nico Huber040b3382018-09-30 01:18:43 +0200383 if (is_read && flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
384 data_packet[3] = READ_MODE_4B_ADDR_FAST_0x0C;
385 data_packet[4] = JEDEC_READ_4BA_FAST;
386 } else if (dedi_spi_cmd == WRITE_MODE_PAGE_PGM
387 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
388 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
389 data_packet[4] = JEDEC_BYTE_PROGRAM_4BA;
390 }
391
Edward O'Callaghancb338322019-02-27 19:25:18 +1100392 *value = *idx = 0;
393 data_packet[5] = 0; /* RFU */
394 data_packet[6] = (start >> 0) & 0xff;
395 data_packet[7] = (start >> 8) & 0xff;
396 data_packet[8] = (start >> 16) & 0xff;
397 data_packet[9] = (start >> 24) & 0xff;
398 if (protocol() >= PROTOCOL_V3) {
399 if (is_read) {
400 data_packet[10] = 0x00; /* address length (3 or 4) */
401 data_packet[11] = 0x00; /* dummy cycle / 2 */
402 } else {
403 /* 16 LSBs and 16 HSBs of page size */
404 /* FIXME: This assumes page size of 256. */
405 data_packet[10] = 0x00;
406 data_packet[11] = 0x01;
407 data_packet[12] = 0x00;
408 data_packet[13] = 0x00;
409 }
410 }
411 } else {
Nico Huberd2d28962019-03-21 15:42:54 +0100412 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
413 if (spi_set_extended_address(flash, start >> 24))
414 return 1;
415 } else if (start >> 24) {
416 msg_cerr("Can't handle 4-byte address with dediprog.\n");
417 return 1;
418 }
419 /*
420 * We don't know how the dediprog firmware handles 4-byte
421 * addresses. So let's not tell it what we are doing and
422 * only send the lower 3 bytes.
423 */
424 *value = start & 0xffff;
425 *idx = (start >> 16) & 0xff;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100426 }
Nico Huberd2d28962019-03-21 15:42:54 +0100427
428 return 0;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100429}
430
hailfingerfb6287d2010-11-16 21:25:29 +0000431/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
432 * @start start address
433 * @len length
434 * @return 0 on success, 1 on failure
435 */
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100436static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000437{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100438 int err = 1;
439
hailfingerfb6287d2010-11-16 21:25:29 +0000440 /* chunksize must be 512, other sizes will NOT work at all. */
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100441 const unsigned int chunksize = 512;
stefanctc5eb8a92011-11-23 09:13:48 +0000442 const unsigned int count = len / chunksize;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100443
David Hendricksf9ecb452015-11-04 15:40:27 -0800444 struct dediprog_transfer_status status = { 0, 0, 0 };
445 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
446 struct libusb_transfer *transfer;
hailfingerfb6287d2010-11-16 21:25:29 +0000447
Edward O'Callaghancb338322019-02-27 19:25:18 +1100448 if (len == 0)
449 return 0;
450
hailfingerfb6287d2010-11-16 21:25:29 +0000451 if ((start % chunksize) || (len % chunksize)) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100452 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
453 __func__, start, len);
hailfingerfb6287d2010-11-16 21:25:29 +0000454 return 1;
455 }
456
Edward O'Callaghancb338322019-02-27 19:25:18 +1100457 int command_packet_size;
458 switch (protocol()) {
459 case PROTOCOL_V1:
460 command_packet_size = 5;
461 break;
462 case PROTOCOL_V2:
463 command_packet_size = 10;
464 break;
465 case PROTOCOL_V3:
466 command_packet_size = 12;
467 break;
468 default:
469 return 1;
David Hendricks14e82e52015-08-15 17:59:03 -0700470 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100471
472 uint8_t data_packet[command_packet_size];
473 unsigned int value, idx;
Nico Huberd2d28962019-03-21 15:42:54 +0100474 if (prepare_rw_cmd(flash, data_packet, count, READ_MODE_STD, &value, &idx, start, 1))
475 return 1;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100476
477 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100478 if (ret != (int)sizeof(data_packet)) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800479 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
hailfingerfb6287d2010-11-16 21:25:29 +0000480 return 1;
481 }
482
David Hendricksf9ecb452015-11-04 15:40:27 -0800483 /*
484 * Ring buffer of bulk transfers.
485 * Poll until at least one transfer is ready,
486 * schedule next transfers until buffer is full.
487 */
hailfingerfb6287d2010-11-16 21:25:29 +0000488
David Hendricksf9ecb452015-11-04 15:40:27 -0800489 /* Allocate bulk transfers. */
Edward O'Callaghancb338322019-02-27 19:25:18 +1100490 unsigned int i;
Edward O'Callaghanc6a36112021-01-22 14:52:52 +1100491 for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800492 transfers[i] = libusb_alloc_transfer(0);
493 if (!transfers[i]) {
494 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100495 goto err_free;
496 }
497 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800498
499 /* Now transfer requested chunks using libusb's asynchronous interface. */
500 while (!status.error && (status.queued_idx < count)) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100501 while ((status.queued_idx < count) &&
502 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
503 {
David Hendricksf9ecb452015-11-04 15:40:27 -0800504 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
505 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
506 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
507 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
508 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
509 ret = libusb_submit_transfer(transfer);
510 if (ret < 0) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100511 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
512 status.queued_idx, libusb_error_name(ret));
513 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800514 }
515 ++status.queued_idx;
516 }
517 if (dediprog_bulk_read_poll(&status, 0))
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100518 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800519 }
520 /* Wait for transfers to finish. */
521 if (dediprog_bulk_read_poll(&status, 1))
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100522 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800523 /* Check if everything has been transmitted. */
524 if ((status.finished_idx < count) || status.error)
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100525 goto err_free;
David Hendricksf9ecb452015-11-04 15:40:27 -0800526
527 err = 0;
528
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100529err_free:
David Hendricksf9ecb452015-11-04 15:40:27 -0800530 dediprog_bulk_read_poll(&status, 1);
531 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
532 if (transfers[i]) libusb_free_transfer(transfers[i]);
533 return err;
hailfingerfb6287d2010-11-16 21:25:29 +0000534}
535
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100536static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000537{
hailfingerfb6287d2010-11-16 21:25:29 +0000538 int ret;
539 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000540 const unsigned int chunksize = 0x200;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100541 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000542 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000543
Simon Glasse53cc1d2015-01-08 05:48:51 -0700544 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000545
hailfingerfb6287d2010-11-16 21:25:29 +0000546 if (residue) {
547 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
548 start, residue);
Edward O'Callaghand2c93192020-10-09 12:56:53 +1100549 ret = default_spi_read(flash, buf, start, residue);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700550 if (ret)
551 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000552 }
553
554 /* Round down. */
555 bulklen = (len - residue) / chunksize * chunksize;
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100556 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700557 if (ret)
558 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000559
560 len -= residue + bulklen;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100561 if (len != 0) {
hailfingerfb6287d2010-11-16 21:25:29 +0000562 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
563 start, len);
Edward O'Callaghand2c93192020-10-09 12:56:53 +1100564 ret = default_spi_read(flash, buf + residue + bulklen,
565 start + residue + bulklen, len);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700566 if (ret)
567 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000568 }
569
Simon Glasse53cc1d2015-01-08 05:48:51 -0700570 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000571 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700572err:
573 dediprog_set_leds(LED_ERROR);
574 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000575}
576
David Hendricksfe05e742015-08-15 17:29:39 -0700577/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
578 * @chunksize length of data chunks, only 256 supported by now
579 * @start start address
580 * @len length
581 * @dedi_spi_cmd dediprog specific write command for spi bus
582 * @return 0 on success, 1 on failure
583 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700584static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
David Hendricksfe05e742015-08-15 17:29:39 -0700585 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
hailfinger556e9c32010-11-23 21:28:16 +0000586{
David Hendricksfe05e742015-08-15 17:29:39 -0700587 /* USB transfer size must be 512, other sizes will NOT work at all.
588 * chunksize is the real data size per USB bulk transfer. The remaining
589 * space in a USB bulk transfer must be filled with 0xff padding.
590 */
591 const unsigned int count = len / chunksize;
David Hendricksfe05e742015-08-15 17:29:39 -0700592
593 /*
594 * We should change this check to
595 * chunksize > 512
596 * once we know how to handle different chunk sizes.
597 */
598 if (chunksize != 256) {
599 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
600 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
601 return 1;
602 }
603
604 if ((start % chunksize) || (len % chunksize)) {
605 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
606 "at flashrom@flashrom.org\n", __func__, start, len);
607 return 1;
608 }
609
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100610 /* No idea if the hardware can handle empty writes, so chicken out. */
611 if (len == 0)
612 return 0;
613
Edward O'Callaghancb338322019-02-27 19:25:18 +1100614 int command_packet_size;
615 switch (protocol()) {
616 case PROTOCOL_V1:
617 command_packet_size = 5;
618 break;
619 case PROTOCOL_V2:
620 command_packet_size = 10;
621 break;
622 case PROTOCOL_V3:
623 command_packet_size = 14;
624 break;
625 default:
626 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -0700627 }
628
Edward O'Callaghancb338322019-02-27 19:25:18 +1100629 uint8_t data_packet[command_packet_size];
630 unsigned int value, idx;
Nico Huberd2d28962019-03-21 15:42:54 +0100631 if (prepare_rw_cmd(flash, data_packet, count, dedi_spi_cmd, &value, &idx, start, 0))
632 return 1;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100633 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100634 if (ret != (int)sizeof(data_packet)) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100635 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
636 return 1;
637 }
638
639 unsigned int i;
David Hendricksfe05e742015-08-15 17:29:39 -0700640 for (i = 0; i < count; i++) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100641 unsigned char usbbuf[512];
David Hendricksfe05e742015-08-15 17:29:39 -0700642 memcpy(usbbuf, buf + i * chunksize, chunksize);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100643 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
644 int transferred;
645 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
646 DEFAULT_TIMEOUT);
David Hendricksf9ecb452015-11-04 15:40:27 -0800647 if ((ret < 0) || (transferred != 512)) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100648 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700649 return 1;
650 }
651 }
652
653 return 0;
654}
655
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700656static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
David Hendricksfe05e742015-08-15 17:29:39 -0700657 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
658{
659 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100660 const unsigned int chunksize = flash->chip->page_size;
David Hendricksfe05e742015-08-15 17:29:39 -0700661 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
662 unsigned int bulklen;
stepan4c06de72011-01-28 09:00:15 +0000663
Simon Glasse53cc1d2015-01-08 05:48:51 -0700664 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000665
David Hendricksfe05e742015-08-15 17:29:39 -0700666 if (chunksize != 256) {
667 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
668 "we don't know how dediprog\nhandles them.\n");
669 /* Write everything like it was residue. */
670 residue = len;
671 }
stepan4c06de72011-01-28 09:00:15 +0000672
David Hendricksfe05e742015-08-15 17:29:39 -0700673 if (residue) {
674 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
675 start, residue);
Nico Huber040b3382018-09-30 01:18:43 +0200676 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
Edward O'Callaghanc6a36112021-01-22 14:52:52 +1100677 ret = spi_write_chunked(flash, buf, start, residue, 11);
David Hendricksfe05e742015-08-15 17:29:39 -0700678 if (ret) {
679 dediprog_set_leds(LED_ERROR);
680 return ret;
681 }
682 }
683
684 /* Round down. */
685 bulklen = (len - residue) / chunksize * chunksize;
686 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
687 if (ret) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700688 dediprog_set_leds(LED_ERROR);
David Hendricksfe05e742015-08-15 17:29:39 -0700689 return ret;
690 }
stepan4c06de72011-01-28 09:00:15 +0000691
David Hendricksfe05e742015-08-15 17:29:39 -0700692 len -= residue + bulklen;
693 if (len) {
694 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
695 start, len);
Edward O'Callaghanc6a36112021-01-22 14:52:52 +1100696 ret = spi_write_chunked(flash, buf + residue + bulklen,
Nico Huber040b3382018-09-30 01:18:43 +0200697 start + residue + bulklen, len, 11);
David Hendricksfe05e742015-08-15 17:29:39 -0700698 if (ret) {
699 dediprog_set_leds(LED_ERROR);
700 return ret;
701 }
702 }
703
704 dediprog_set_leds(LED_PASS);
705 return 0;
hailfinger556e9c32010-11-23 21:28:16 +0000706}
707
Patrick Georgiab8353e2017-02-03 18:32:01 +0100708static 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 -0700709{
710 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
711}
712
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700713static 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 -0700714{
715 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
716}
David Hendricksfe05e742015-08-15 17:29:39 -0700717
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100718static int dediprog_spi_send_command(const struct flashctx *flash,
719 unsigned int writecnt,
David Hendricksfe05e742015-08-15 17:29:39 -0700720 unsigned int readcnt,
721 const unsigned char *writearr,
722 unsigned char *readarr)
hailfingerdfb32a02010-01-19 11:15:48 +0000723{
724 int ret;
725
hailfingeraf389cc2010-01-22 02:53:30 +0000726 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Edward O'Callaghanc6a36112021-01-22 14:52:52 +1100727 if (writecnt > flash->mst->spi.max_data_write) {
David Hendricksfe05e742015-08-15 17:29:39 -0700728 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
729 return 1;
Simon Glass543101a2015-01-08 06:28:13 -0700730 }
Edward O'Callaghanc6a36112021-01-22 14:52:52 +1100731 if (readcnt > flash->mst->spi.max_data_read) {
David Hendricksfe05e742015-08-15 17:29:39 -0700732 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
733 return 1;
734 }
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100735
Edward O'Callaghancb338322019-02-27 19:25:18 +1100736 unsigned int idx, value;
737 /* New protocol has options and timeout combined as value while the old one used the value field for
738 * timeout and the index field for options. */
739 if (protocol() >= PROTOCOL_V2) {
740 idx = 0;
741 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
David Hendricks14e82e52015-08-15 17:59:03 -0700742 } else {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100743 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
744 value = 0;
David Hendricks14e82e52015-08-15 17:59:03 -0700745 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100746 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100747 if (ret != (int)writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000748 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800749 writecnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000750 return 1;
751 }
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100752 if (readcnt == 0) // If we don't require a response, we are done here
hailfingerdfb32a02010-01-19 11:15:48 +0000753 return 0;
David Hendricksfe05e742015-08-15 17:29:39 -0700754
Edward O'Callaghancb338322019-02-27 19:25:18 +1100755 /* The specifications do state the possibility to set a timeout for transceive transactions.
756 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
757 * can predict the time needed by the previous command or so (untested). In any case, using this
758 * "feature" to set sane-looking timouts for the read below will completely trash performance with
759 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
760 *
761 * The specification also uses only 0 in its examples, so the lesson to learn here:
762 * "Never trust the description of an interface in the documentation but use the example code and pray."
763 const uint8_t read_timeout = 10 + readcnt/512;
764 if (protocol() >= PROTOCOL_V2) {
765 idx = 0;
766 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
767 } else {
768 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
769 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
770 }
771 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
772 */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100773 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100774 if (ret != (int)readcnt) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +1100775 msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000776 return 1;
777 }
778 return 0;
779}
780
hailfinger1ff33dc2010-07-03 11:02:10 +0000781static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000782{
783 int ret;
784 char buf[0x11];
785
hailfingerdfb32a02010-01-19 11:15:48 +0000786 /* Command Receive Device String. */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100787 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000788 if (ret != 0x10) {
789 msg_perr("Incomplete/failed Command Receive Device String!\n");
790 return 1;
791 }
792 buf[0x10] = '\0';
793 msg_pdbg("Found a %s\n", buf);
David Hendricks6702e072015-08-15 18:09:04 -0700794 if (memcmp(buf, "SF100", 0x5) == 0)
795 dediprog_devicetype = DEV_SF100;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100796 else if (memcmp(buf, "SF200", 0x5) == 0)
797 dediprog_devicetype = DEV_SF200;
David Hendricks6702e072015-08-15 18:09:04 -0700798 else if (memcmp(buf, "SF600", 0x5) == 0)
799 dediprog_devicetype = DEV_SF600;
800 else {
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100801 msg_perr("Device not a SF100, SF200, or SF600!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000802 return 1;
803 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100804
805 int sfnum;
806 int fw[3];
807 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 +1100808 sfnum != (int)dediprog_devicetype) {
David Hendricks14e82e52015-08-15 17:59:03 -0700809 msg_perr("Unexpected firmware version string '%s'\n", buf);
hailfinger7a009082010-11-09 23:30:43 +0000810 return 1;
811 }
David Hendricks14e82e52015-08-15 17:59:03 -0700812 /* Only these major versions were tested. */
David Hendricks6702e072015-08-15 18:09:04 -0700813 if (fw[0] < 2 || fw[0] > 7) {
David Hendricks14e82e52015-08-15 17:59:03 -0700814 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000815 return 1;
816 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100817
stepan4c06de72011-01-28 09:00:15 +0000818 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100819 if (protocol() == PROTOCOL_UNKNOWN) {
820 msg_perr("Internal error: Unable to determine protocol version.\n");
821 return 1;
822 }
David Hendricks14e82e52015-08-15 17:59:03 -0700823
hailfingerdfb32a02010-01-19 11:15:48 +0000824 return 0;
825}
826
David Hendricks91ca7ac2015-11-20 16:22:22 -0800827static int dediprog_supply_voltages[] = {
828 0, 1800, 2500, 3500,
829};
830
Edward O'Callaghan1ccbe422020-11-19 21:33:55 +1100831static int compar(const void *_x, const void *_y)
832{
833 const struct voltage_range *x = _x;
834 const struct voltage_range *y = _y;
835
836 /*
837 * qsort() places entries in ascending order. We will sort by minimum
838 * voltage primarily and max voltage secondarily, and move empty sets
839 * to the end of array.
840 */
841 if (x->min == 0)
842 return 1;
843 if (y->min == 0)
844 return -1;
845
846 if (x->min < y->min)
847 return -1;
848 if (x->min > y->min)
849 return 1;
850
851 if (x->min == y->min) {
852 if (x->max < y->max)
853 return -1;
854 if (x->max > y->max)
855 return 1;
856 }
857
858 return 0;
859}
860
861#define NUM_VOLTAGE_RANGES 16
862static struct voltage_range voltage_ranges[NUM_VOLTAGE_RANGES];
863
864/* returns number of unique voltage ranges, or <0 to indicate failure */
865static int flash_supported_voltage_ranges(enum chipbustype bus)
866{
Edward O'Callaghan798d2ad2021-01-08 12:19:07 +1100867 unsigned i;
Edward O'Callaghan1ccbe422020-11-19 21:33:55 +1100868 int unique_ranges = 0;
869
870 /* clear array in case user calls this function multiple times */
871 memset(voltage_ranges, 0, sizeof(voltage_ranges));
872
873 for (i = 0; i < flashchips_size; i++) {
874 int j;
875 int match_found = 0;
876
877 if (unique_ranges >= NUM_VOLTAGE_RANGES) {
878 msg_cerr("Increase NUM_VOLTAGE_RANGES.\n");
879 return -1;
880 }
881
882 if (!(flashchips[i].bustype & bus))
883 continue;
884
885 for (j = 0; j < NUM_VOLTAGE_RANGES; j++) {
886 if ((flashchips[i].voltage.min == voltage_ranges[j].min) &&
887 (flashchips[i].voltage.max == voltage_ranges[j].max))
888 match_found |= 1;
889
890 if (!voltage_ranges[j].min && !voltage_ranges[j].max)
891 break;
892 }
893
894 if (!match_found) {
895 voltage_ranges[j] = flashchips[i].voltage;
896 unique_ranges++;
897 }
898 }
899
900 qsort(&voltage_ranges[0], NUM_VOLTAGE_RANGES,
901 sizeof(voltage_ranges[0]), compar);
902
903 for (i = 0; i < NUM_VOLTAGE_RANGES; i++) {
904 msg_cspew("%s: voltage_range[%d]: { %u, %u }\n",
905 __func__, i, voltage_ranges[i].min, voltage_ranges[i].max);
906 }
907
908 return unique_ranges;
909}
910
Edward O'Callaghance0eee82021-01-27 17:38:52 +1100911static struct spi_master spi_master_dediprog;
David Hendricks93784b42016-08-09 17:00:38 -0700912static int dediprog_set_spi_flash_voltage_auto(void)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800913{
David Hendricks91ca7ac2015-11-20 16:22:22 -0800914 int spi_flash_ranges;
915
916 spi_flash_ranges = flash_supported_voltage_ranges(BUS_SPI);
917 if (spi_flash_ranges < 0)
918 return -1;
919
Edward O'Callaghan798d2ad2021-01-08 12:19:07 +1100920 for (unsigned i = 0; i < ARRAY_SIZE(dediprog_supply_voltages); i++) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800921 int j;
922 int v = dediprog_supply_voltages[i]; /* shorthand */
923
924 for (j = 0; j < spi_flash_ranges; j++) {
925 /* Dediprog can supply a voltage in this range. */
926 if ((v >= voltage_ranges[j].min) && (v <= voltage_ranges[j].max)) {
David Hendricks93784b42016-08-09 17:00:38 -0700927 struct flashctx dummy;
928
David Hendricks91ca7ac2015-11-20 16:22:22 -0800929 msg_cdbg("%s: trying %d\n", __func__, v);
930 if (dediprog_set_spi_flash_voltage_manual(v)) {
931 msg_cerr("%s: Failed to set SPI voltage\n", __func__);
932 return 1;
933 }
934
935 clear_spi_id_cache();
Edward O'Callaghance0eee82021-01-27 17:38:52 +1100936 struct registered_master rmst = {
937 .buses_supported = BUS_SPI,
938 .spi = spi_master_dediprog,
939 };
940 if (probe_flash(&rmst, 0, &dummy, 0) < 0) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800941 /* No dice, try next voltage supported by Dediprog. */
942 break;
943 }
944
Patrick Georgif3fa2992017-02-02 16:24:44 +0100945 if ((dummy.chip->manufacture_id == GENERIC_MANUF_ID) ||
946 (dummy.chip->model_id == GENERIC_DEVICE_ID)) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800947 break;
948 }
949
950 return 0;
951 }
952 }
953 }
954
955 return 1;
956}
957
958/* FIXME: ugly function signature */
David Hendricks93784b42016-08-09 17:00:38 -0700959static int dediprog_set_spi_voltage(int millivolt, int probe)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800960{
961 if (probe)
David Hendricks93784b42016-08-09 17:00:38 -0700962 return dediprog_set_spi_flash_voltage_auto();
David Hendricks91ca7ac2015-11-20 16:22:22 -0800963 else
964 return dediprog_set_spi_flash_voltage_manual(millivolt);
965}
966
David Hendricks28fbdfb2015-08-15 18:04:37 -0700967/*
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -0700968 * Read the id from the dediprog. This should return the numeric part of the
969 * serial number found on a sticker on the back of the dediprog. Note this
970 * number is stored in writable eeprom, so it could get out of sync. Also note,
971 * this function only supports SF100 at this time, but SF600 support is not too
972 * much different.
973 * @return the id on success, -1 on failure
974 */
975static int dediprog_read_id(void)
976{
977 int ret;
978 uint8_t buf[3];
979
980 ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN,
981 0x7, /* request */
982 0, /* value */
983 0xEF00, /* index */
984 buf, sizeof(buf),
985 DEFAULT_TIMEOUT);
986 if (ret != sizeof(buf)) {
987 msg_perr("Failed to read dediprog id, error %d!\n", ret);
988 return -1;
989 }
990
991 return buf[0] << 16 | buf[1] << 8 | buf[2];
992}
993
994/*
David Hendricks28fbdfb2015-08-15 18:04:37 -0700995 * This command presumably sets the voltage for the SF100 itself (not the
996 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
997 * (including all SF600s) do not support it.
998 */
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +1100999
1000/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
1001 * Only use dediprog_set_voltage on SF100 programmers with firmware older
1002 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
David Hendricks28fbdfb2015-08-15 18:04:37 -07001003static int dediprog_set_voltage(void)
hailfingerdfb32a02010-01-19 11:15:48 +00001004{
Edward O'Callaghane749db02019-03-01 08:50:47 +11001005 unsigned char buf[1] = {0};
Edward O'Callaghan9b0182b2019-03-28 13:45:13 +11001006 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
1007 buf, 0x1, DEFAULT_TIMEOUT);
hailfinger7a009082010-11-09 23:30:43 +00001008 if (ret < 0) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001009 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
hailfinger7a009082010-11-09 23:30:43 +00001010 return 1;
1011 }
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001012 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -07001013 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +00001014 return 1;
1015 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +11001016
hailfingerdfb32a02010-01-19 11:15:48 +00001017 return 0;
1018}
1019
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001020static int dediprog_standalone_mode(void)
David Hendrickscf453d82015-11-16 20:19:57 -08001021{
1022 int ret;
1023
1024 if (dediprog_devicetype != DEV_SF600)
1025 return 0;
1026
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001027 msg_pdbg2("Disabling standalone mode.\n");
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +11001028 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Hendrickscf453d82015-11-16 20:19:57 -08001029 if (ret) {
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001030 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Hendrickscf453d82015-11-16 20:19:57 -08001031 return 1;
1032 }
1033
1034 return 0;
1035}
1036
Edward O'Callaghanc6a36112021-01-22 14:52:52 +11001037#if 0
1038/* Something.
1039 * Present in eng_detect_blink.log with firmware 3.1.8
1040 * Always preceded by Command Receive Device String
1041 */
1042static int dediprog_command_b(void)
1043{
1044 int ret;
1045 char buf[0x3];
1046
1047 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
1048 buf, 0x3, DEFAULT_TIMEOUT);
1049 if (ret < 0) {
1050 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
1051 return 1;
1052 }
1053 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
1054 (buf[2] != 0xff)) {
1055 msg_perr("Unexpected response to Command B!\n");
1056 return 1;
1057 }
1058
1059 return 0;
1060}
1061#endif
1062
David Hendricksfe05e742015-08-15 17:29:39 -07001063static int set_target_flash(enum dediprog_target target)
1064{
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +11001065 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
David Hendricksfe05e742015-08-15 17:29:39 -07001066 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001067 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +00001068 return 1;
1069 }
1070 return 0;
1071}
1072
David Hendricksfe05e742015-08-15 17:29:39 -07001073#if 0
1074/* Returns true if the button is currently pressed. */
1075static bool dediprog_get_button(void)
Simon Glassd01002b2015-01-08 05:44:16 -07001076{
David Hendricksfe05e742015-08-15 17:29:39 -07001077 char buf[1];
1078 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
1079 buf, 0x1, DEFAULT_TIMEOUT);
1080 if (ret != 0) {
Edward O'Callaghan4ddf44b2019-02-27 20:00:46 +11001081 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -07001082 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001083 }
David Hendricksfe05e742015-08-15 17:29:39 -07001084 return buf[0] != 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001085}
David Hendricksfe05e742015-08-15 17:29:39 -07001086#endif
Simon Glassd01002b2015-01-08 05:44:16 -07001087
hailfingerf76cc322010-11-09 22:00:31 +00001088static int parse_voltage(char *voltage)
1089{
1090 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +00001091 int i;
1092 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +00001093
1094 if (!voltage || !strlen(voltage)) {
1095 msg_perr("Empty voltage= specified.\n");
1096 return -1;
1097 }
1098 millivolt = (int)strtol(voltage, &tmp, 0);
1099 voltage = tmp;
1100 /* Handle "," and "." as decimal point. Everything after it is assumed
1101 * to be in decimal notation.
1102 */
1103 if ((*voltage == '.') || (*voltage == ',')) {
1104 voltage++;
1105 for (i = 0; i < 3; i++) {
1106 fraction *= 10;
1107 /* Don't advance if the current character is invalid,
1108 * but continue multiplying.
1109 */
1110 if ((*voltage < '0') || (*voltage > '9'))
1111 continue;
1112 fraction += *voltage - '0';
1113 voltage++;
1114 }
1115 /* Throw away remaining digits. */
1116 voltage += strspn(voltage, "0123456789");
1117 }
1118 /* The remaining string must be empty or "mV" or "V". */
1119 tolower_string(voltage);
1120
1121 /* No unit or "V". */
1122 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1123 millivolt *= 1000;
1124 millivolt += fraction;
1125 } else if (!strncmp(voltage, "mv", 2) ||
1126 !strncmp(voltage, "milliv", 6)) {
1127 /* No adjustment. fraction is discarded. */
1128 } else {
1129 /* Garbage at the end of the string. */
1130 msg_perr("Garbage voltage= specified.\n");
1131 return -1;
1132 }
1133 return millivolt;
1134}
1135
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +11001136static struct spi_master spi_master_dediprog = {
1137 .features = SPI_MASTER_NO_4BA_MODES,
1138 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
1139 .max_data_write = 16,
Craig Hesling65eb8812019-08-01 09:33:56 -07001140 .command = dediprog_spi_send_command,
1141 .multicommand = default_spi_send_multicommand,
1142 .read = dediprog_spi_read,
1143 .write_256 = dediprog_spi_write_256,
1144 .write_aai = dediprog_spi_write_aai,
1145};
mkarcherd264e9e2011-05-11 17:07:07 +00001146
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001147/*
1148 * Open a dediprog_handle with the USB device at the given index.
1149 * @index index of the USB device
1150 * @return 0 for success, -1 for error, -2 for busy device
1151 */
1152static int dediprog_open(int index)
1153{
1154 const uint16_t vid = devs_dediprog[0].vendor_id;
1155 const uint16_t pid = devs_dediprog[0].device_id;
1156 int ret;
1157
1158 dediprog_handle = usb_dev_get_by_vid_pid_number(usb_ctx, vid, pid, (unsigned int) index);
1159 if (!dediprog_handle) {
1160 msg_perr("Could not find a Dediprog programmer on USB.\n");
1161 libusb_exit(usb_ctx);
1162 return -1;
1163 }
1164 ret = libusb_set_configuration(dediprog_handle, 1);
1165 if (ret != 0) {
1166 msg_perr("Could not set USB device configuration: %i %s\n",
1167 ret, libusb_error_name(ret));
1168 libusb_close(dediprog_handle);
1169 return -2;
1170 }
1171 ret = libusb_claim_interface(dediprog_handle, 0);
1172 if (ret < 0) {
1173 msg_perr("Could not claim USB device interface %i: %i %s\n",
1174 0, ret, libusb_error_name(ret));
1175 libusb_close(dediprog_handle);
1176 return -2;
1177 }
1178 return 0;
1179}
1180
David Hendricks93784b42016-08-09 17:00:38 -07001181static int dediprog_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +00001182{
David Hendricks6702e072015-08-15 18:09:04 -07001183 dediprog_devicetype = DEV_UNKNOWN;
David Hendricksfe05e742015-08-15 17:29:39 -07001184
dhendrix0ffc2eb2011-06-14 01:35:36 +00001185 /* URB 28. Command Set SPI Voltage to 0. */
David Hendricks93784b42016-08-09 17:00:38 -07001186 if (dediprog_set_spi_voltage(0x0, 0))
dhendrix0ffc2eb2011-06-14 01:35:36 +00001187 return 1;
1188
David Hendricksf9ecb452015-11-04 15:40:27 -08001189 if (libusb_release_interface(dediprog_handle, 0)) {
dhendrix0ffc2eb2011-06-14 01:35:36 +00001190 msg_perr("Could not release USB interface!\n");
1191 return 1;
1192 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001193 libusb_close(dediprog_handle);
1194 libusb_exit(usb_ctx);
1195
dhendrix0ffc2eb2011-06-14 01:35:36 +00001196 return 0;
1197}
1198
David Hendricksac1d25c2016-08-09 17:00:58 -07001199int dediprog_init(void)
hailfingerdfb32a02010-01-19 11:15:48 +00001200{
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001201 char *voltage, *id_str, *device, *spispeed, *target_str;
David Hendricksfe05e742015-08-15 17:29:39 -07001202 int spispeed_idx = 1;
David Hendrickscd20ea42015-11-17 22:33:35 -08001203 int millivolt = 0;
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001204 int id = -1; /* -1 defaults to enumeration order */
1205 int found_id;
David Hendricksfe05e742015-08-15 17:29:39 -07001206 long usedevice = 0;
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001207 long target = FLASH_TYPE_APPLICATION_FLASH_1;
David Hendricks98b3c572016-11-30 01:50:08 +00001208 int i, ret;
hailfingerdfb32a02010-01-19 11:15:48 +00001209
David Hendricksfe05e742015-08-15 17:29:39 -07001210 spispeed = extract_programmer_param("spispeed");
1211 if (spispeed) {
1212 for (i = 0; spispeeds[i].name; ++i) {
1213 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1214 spispeed_idx = i;
1215 break;
1216 }
1217 }
1218 if (!spispeeds[i].name) {
1219 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
David Hendricks98b3c572016-11-30 01:50:08 +00001220 free(spispeed);
1221 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001222 }
David Hendricks98b3c572016-11-30 01:50:08 +00001223 free(spispeed);
David Hendricksfe05e742015-08-15 17:29:39 -07001224 }
1225
hailfingerf76cc322010-11-09 22:00:31 +00001226 voltage = extract_programmer_param("voltage");
1227 if (voltage) {
1228 millivolt = parse_voltage(voltage);
1229 free(voltage);
David Hendricks98b3c572016-11-30 01:50:08 +00001230 if (millivolt < 0)
1231 return 1;
hailfingerf76cc322010-11-09 22:00:31 +00001232 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1233 }
David Hendricksfe05e742015-08-15 17:29:39 -07001234
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001235 id_str = extract_programmer_param("id");
1236 if (id_str) {
1237 char prefix0, prefix1;
1238 if (sscanf(id_str, "%c%c%d", &prefix0, &prefix1, &id) != 3) {
1239 msg_perr("Error: Could not parse dediprog 'id'.\n");
1240 msg_perr("Expected a string like SF012345 or DP012345.\n");
1241 free(id_str);
1242 return 1;
1243 }
1244 if (id < 0 || id >= 0x1000000) {
1245 msg_perr("Error: id %s is out of range!\n", id_str);
1246 free(id_str);
1247 return 1;
1248 }
1249 if (!(prefix0 == 'S' && prefix1 == 'F') && !(prefix0 == 'D' && prefix1 == 'P')) {
1250 msg_perr("Error: %s is an invalid id!\n", id_str);
1251 free(id_str);
1252 return 1;
1253 }
1254 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1255 }
1256 free(id_str);
1257
David Hendricksfe05e742015-08-15 17:29:39 -07001258 device = extract_programmer_param("device");
1259 if (device) {
1260 char *dev_suffix;
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001261 if (id != -1) {
1262 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1263 }
David Hendricksfe05e742015-08-15 17:29:39 -07001264 errno = 0;
1265 usedevice = strtol(device, &dev_suffix, 10);
1266 if (errno != 0 || device == dev_suffix) {
1267 msg_perr("Error: Could not convert 'device'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001268 free(device);
1269 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001270 }
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +11001271 if (usedevice < 0 || usedevice > INT_MAX) {
David Hendricksfe05e742015-08-15 17:29:39 -07001272 msg_perr("Error: Value for 'device' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001273 free(device);
1274 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001275 }
1276 if (strlen(dev_suffix) > 0) {
1277 msg_perr("Error: Garbage following 'device' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001278 free(device);
1279 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001280 }
1281 msg_pinfo("Using device %li.\n", usedevice);
Simon Glassd01002b2015-01-08 05:44:16 -07001282 }
David Hendricks98b3c572016-11-30 01:50:08 +00001283 free(device);
David Hendricksfe05e742015-08-15 17:29:39 -07001284
1285 target_str = extract_programmer_param("target");
1286 if (target_str) {
1287 char *target_suffix;
1288 errno = 0;
1289 target = strtol(target_str, &target_suffix, 10);
1290 if (errno != 0 || target_str == target_suffix) {
1291 msg_perr("Error: Could not convert 'target'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001292 free(target_str);
1293 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001294 }
1295 if (target < 1 || target > 2) {
1296 msg_perr("Error: Value for 'target' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001297 free(target_str);
1298 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001299 }
1300 if (strlen(target_suffix) > 0) {
1301 msg_perr("Error: Garbage following 'target' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001302 free(target_str);
1303 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001304 }
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001305 switch (target) {
1306 case 1:
1307 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1308 target = FLASH_TYPE_APPLICATION_FLASH_1;
1309 break;
1310 case 2:
1311 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1312 target = FLASH_TYPE_APPLICATION_FLASH_2;
1313 break;
1314 default:
1315 break;
1316 }
David Hendricksfe05e742015-08-15 17:29:39 -07001317 }
David Hendricks98b3c572016-11-30 01:50:08 +00001318 free(target_str);
hailfingerf76cc322010-11-09 22:00:31 +00001319
hailfingerdfb32a02010-01-19 11:15:48 +00001320 /* Here comes the USB stuff. */
David Hendricksf9ecb452015-11-04 15:40:27 -08001321 libusb_init(&usb_ctx);
1322 if (!usb_ctx) {
1323 msg_perr("Could not initialize libusb!\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001324 return 1;
hailfingerdfb32a02010-01-19 11:15:48 +00001325 }
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001326
1327 if (id != -1) {
1328 for (i = 0; ; i++) {
1329 ret = dediprog_open(i);
1330 if (ret == -1) {
1331 /* no dev */
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001332 return 1;
1333 } else if (ret == -2) {
1334 /* busy dev */
1335 continue;
1336 }
1337
1338 /* Notice we can only call dediprog_read_id() after
1339 * libusb_set_configuration() and
1340 * libusb_claim_interface(). When searching by id and
1341 * either configuration or claim fails (usually the
1342 * device is in use by another instance of flashrom),
1343 * the device is skipped and the next device is tried.
1344 */
1345 found_id = dediprog_read_id();
1346 if (found_id < 0) {
1347 msg_perr("Could not read id.\n");
1348 libusb_release_interface(dediprog_handle, 0);
1349 libusb_close(dediprog_handle);
1350 continue;
1351 }
1352 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1353 if (found_id != id) {
1354 libusb_release_interface(dediprog_handle, 0);
1355 libusb_close(dediprog_handle);
1356 continue;
1357 }
1358 break;
1359 }
1360 } else {
1361 if (dediprog_open(usedevice)) {
1362 return 1;
1363 }
1364 found_id = dediprog_read_id();
David Hendricksfe05e742015-08-15 17:29:39 -07001365 }
Ryan O'Leary2d62a5a2019-06-24 19:14:33 -07001366
1367 if (found_id >= 0) {
1368 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
hailfingerfb6287d2010-11-16 21:25:29 +00001369 }
David Hendricksfe05e742015-08-15 17:29:39 -07001370
David Hendricks98b3c572016-11-30 01:50:08 +00001371 if (register_shutdown(dediprog_shutdown, NULL))
1372 return 1;
dhendrix0ffc2eb2011-06-14 01:35:36 +00001373
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +11001374 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1375 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
David Hendricks28fbdfb2015-08-15 18:04:37 -07001376 if (dediprog_check_devicestring()) {
David Hendricks98b3c572016-11-30 01:50:08 +00001377 if (dediprog_set_voltage())
1378 return 1;
1379 if (dediprog_check_devicestring())
1380 return 1;
David Hendricks28fbdfb2015-08-15 18:04:37 -07001381 }
David Hendricksfe05e742015-08-15 17:29:39 -07001382
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001383 /* SF100/SF200 uses one in/out endpoint, SF600 uses separate in/out endpoints */
David Hendricksd7468582015-11-12 15:21:12 -08001384 dediprog_in_endpoint = 2;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001385 switch (dediprog_devicetype) {
1386 case DEV_SF100:
1387 case DEV_SF200:
David Hendricksd7468582015-11-12 15:21:12 -08001388 dediprog_out_endpoint = 2;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001389 break;
1390 default:
David Hendricksd7468582015-11-12 15:21:12 -08001391 dediprog_out_endpoint = 1;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001392 break;
1393 }
David Hendricksd7468582015-11-12 15:21:12 -08001394
Edward O'Callaghanc4ba80c2020-11-24 12:49:15 +11001395 /* Set all possible LEDs as soon as possible to indicate activity.
David Hendricksfe05e742015-08-15 17:29:39 -07001396 * 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 +11001397 * dediprog_check_devicestring() has queried the device. */
1398 dediprog_set_leds(LED_ALL);
David Hendricksfe05e742015-08-15 17:29:39 -07001399
1400 /* Select target/socket, frequency and VCC. */
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001401 if (set_target_flash(target) ||
David Hendricksfe05e742015-08-15 17:29:39 -07001402 dediprog_set_spi_speed(spispeed_idx) ||
David Hendricks93784b42016-08-09 17:00:38 -07001403 dediprog_set_spi_voltage(millivolt, voltage ? 0 : 1)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -07001404 dediprog_set_leds(LED_ERROR);
David Hendricks98b3c572016-11-30 01:50:08 +00001405 return 1;
stepan4c06de72011-01-28 09:00:15 +00001406 }
hailfingerdfb32a02010-01-19 11:15:48 +00001407
Edward O'Callaghanc0363742019-02-27 23:08:00 +11001408 if (dediprog_standalone_mode())
David Hendricks98b3c572016-11-30 01:50:08 +00001409 return 1;
1410
Patrick Rudolphcce47252020-08-26 07:58:16 +02001411 if ((dediprog_devicetype == DEV_SF100) ||
1412 (dediprog_devicetype == DEV_SF600 && protocol() == PROTOCOL_V3))
Nico Huberd2d28962019-03-21 15:42:54 +01001413 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1414
Patrick Rudolph6fc5b8b2020-08-26 10:02:08 +02001415 if (protocol() >= PROTOCOL_V2)
Nico Huber040b3382018-09-30 01:18:43 +02001416 spi_master_dediprog.features |= SPI_MASTER_4BA;
David Hendricks93784b42016-08-09 17:00:38 -07001417
Nico Huberf1eeda62021-05-11 17:38:14 +02001418 if (register_spi_master(&spi_master_dediprog, NULL) || dediprog_set_leds(LED_NONE))
Nico Huber040b3382018-09-30 01:18:43 +02001419 return 1;
stepan4c06de72011-01-28 09:00:15 +00001420
David Hendricks98b3c572016-11-30 01:50:08 +00001421 return 0;
hailfingerdfb32a02010-01-19 11:15:48 +00001422}