blob: d77cbfb010358ad1249796693f3b3e645fe78aee [file] [log] [blame]
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
Simon Glass557eb4f2015-07-05 16:53:22 +00005 * Copyright (C) 2015 Simon Glass
6 * Copyright (C) 2015 Stefan Tauner
Carl-Daniel Hailfingerd38fac82010-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.
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000016 */
17
Nico Huber4d440a72017-08-15 11:26:48 +020018#include <sys/types.h>
Stefan Tauner5c316f92015-02-08 21:57:52 +000019#include <stdlib.h>
Mathias Krausedb7afc52010-11-09 23:30:43 +000020#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000021#include <string.h>
Stefan Taunere34e3e82013-01-01 00:06:51 +000022#include <limits.h>
Nathan Laredo21541a62012-12-24 22:07:36 +000023#include <errno.h>
Nico Huberd99a2bd2016-02-18 21:42:49 +000024#include <libusb.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000025#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000026#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000028#include "spi.h"
Nico Hubera1b7f352024-03-25 18:32:11 +010029#include "spi_command.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000030
Nico Huberd99a2bd2016-02-18 21:42:49 +000031/* LIBUSB_CALL ensures the right calling conventions on libusb callbacks.
32 * However, the macro is not defined everywhere. m(
33 */
34#ifndef LIBUSB_CALL
35#define LIBUSB_CALL
36#endif
37
Stefan Reinauer915b8402011-01-28 09:00:15 +000038#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000039#define DEFAULT_TIMEOUT 3000
Nico Hubera96aaa32024-03-05 12:56:13 +010040#define MAX_BLOCK_COUNT 65535
Nico Huber008a44f2024-04-14 23:39:47 +020041#define MAX_CMD_SIZE 15
Nico Huberd99a2bd2016-02-18 21:42:49 +000042#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
David Hendricksc3685182020-06-23 17:36:09 -070043#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
Nico Huberd99a2bd2016-02-18 21:42:49 +000044#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 */
David Woodhouse7f3b89f2016-02-18 21:42:15 +000047
48enum dediprog_devtype {
49 DEV_UNKNOWN = 0,
50 DEV_SF100 = 100,
Jay Thompsoncabe3202018-08-17 14:30:04 -050051 DEV_SF200 = 200,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000052 DEV_SF600 = 600,
Nico Huber00578222024-03-02 14:00:53 +010053 DEV_SF600PG2 = 600+2,
54 DEV_SF700 = 700,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000055};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000056
Simon Glass25e9f402015-06-28 13:31:19 +000057enum dediprog_leds {
58 LED_INVALID = -1,
59 LED_NONE = 0,
60 LED_PASS = 1 << 0,
61 LED_BUSY = 1 << 1,
62 LED_ERROR = 1 << 2,
63 LED_ALL = 7,
64};
65
Simon Glass557eb4f2015-07-05 16:53:22 +000066/* IO bits for CMD_SET_IO_LED message */
67enum dediprog_ios {
68 IO1 = 1 << 0,
69 IO2 = 1 << 1,
70 IO3 = 1 << 2,
71 IO4 = 1 << 3,
72};
73
74enum dediprog_cmds {
75 CMD_TRANSCEIVE = 0x01,
76 CMD_POLL_STATUS_REG = 0x02,
77 CMD_SET_VPP = 0x03,
78 CMD_SET_TARGET = 0x04,
79 CMD_READ_EEPROM = 0x05,
80 CMD_WRITE_EEPROM = 0x06,
81 CMD_SET_IO_LED = 0x07,
82 CMD_READ_PROG_INFO = 0x08,
83 CMD_SET_VCC = 0x09,
84 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000085 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000086 CMD_GET_BUTTON = 0x11,
87 CMD_GET_UID = 0x12,
88 CMD_SET_CS = 0x14,
89 CMD_IO_MODE = 0x15,
90 CMD_FW_UPDATE = 0x1A,
91 CMD_FPGA_UPDATE = 0x1B,
92 CMD_READ_FPGA_VERSION = 0x1C,
93 CMD_SET_HOLD = 0x1D,
94 CMD_READ = 0x20,
95 CMD_WRITE = 0x30,
96 CMD_WRITE_AT45DB = 0x31,
97 CMD_NAND_WRITE = 0x32,
98 CMD_NAND_READ = 0x33,
99 CMD_SET_SPI_CLK = 0x61,
100 CMD_CHECK_SOCKET = 0x62,
101 CMD_DOWNLOAD_PRJ = 0x63,
102 CMD_READ_PRJ_NAME = 0x64,
103 // New protocol/firmware only
104 CMD_CHECK_SDCARD = 0x65,
105 CMD_READ_PRJ = 0x66,
106};
107
108enum dediprog_target {
109 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
110 FLASH_TYPE_FLASH_CARD,
111 FLASH_TYPE_APPLICATION_FLASH_2,
112 FLASH_TYPE_SOCKET,
113};
114
115enum dediprog_readmode {
116 READ_MODE_STD = 1,
117 READ_MODE_FAST = 2,
118 READ_MODE_ATMEL45 = 3,
119 READ_MODE_4B_ADDR_FAST = 4,
120 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Nico Hubera1b7f352024-03-25 18:32:11 +0100121 READ_MODE_CONFIGURABLE = 9, /* Not seen documented so far */
Simon Glass557eb4f2015-07-05 16:53:22 +0000122};
123
124enum dediprog_writemode {
Elyes HAOUASac01baa2018-05-28 16:52:21 +0200125 WRITE_MODE_PAGE_PGM = 1,
Simon Glass557eb4f2015-07-05 16:53:22 +0000126 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,
Simon Glassae616512016-01-23 23:27:58 +0000132 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000133 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000134 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000135 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
136 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
137};
138
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000139enum dediprog_standalone_mode {
140 ENTER_STANDALONE_MODE = 0,
141 LEAVE_STANDALONE_MODE = 1,
142};
143
David Hendricksf73f8a72018-02-21 07:34:34 -0800144/*
Nico Huberc3b02dc2023-08-12 01:13:45 +0200145 * These are not official designations; they are for use in flashprog only.
David Hendricksf73f8a72018-02-21 07:34:34 -0800146 * 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
Thomas Heijligencc853d82021-05-04 15:32:17 +0200155static const struct dev_entry devs_dediprog[] = {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500156 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
Stefan Taunerfdec7472016-02-22 08:59:27 +0000157
158 {0},
159};
Simon Glass557eb4f2015-07-05 16:53:22 +0000160
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000161struct dediprog_data {
162 struct libusb_context *usb_ctx;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000163 libusb_device_handle *handle;
164 int in_endpoint;
165 int out_endpoint;
166 int firmwareversion;
Nico Huber274e6552024-03-24 12:34:57 +0100167 char devicestring[32+1];
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000168 enum dediprog_devtype devicetype;
Nico Huber008a44f2024-04-14 23:39:47 +0200169 int (*prepare_rw_cmd)(
170 struct flashctx *, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
171 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count);
Nico Hubera1b7f352024-03-25 18:32:11 +0100172 enum io_mode io_mode;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000173};
Simon Glass557eb4f2015-07-05 16:53:22 +0000174
Nico Huberd99a2bd2016-02-18 21:42:49 +0000175#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
176 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
177/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
178const char * LIBUSB_CALL libusb_error_name(int error_code)
179{
180 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
181 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
182 static char my_libusb_error[18 + 5 + 2];
183 sprintf(my_libusb_error, "libusb error code %i", error_code);
184 return my_libusb_error;
185 } else {
186 return "UNKNOWN";
187 }
188}
189#endif
190
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000191static enum protocol protocol(const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000192{
David Hendricksf73f8a72018-02-21 07:34:34 -0800193 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000194 switch (dp_data->devicetype) {
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000195 case DEV_SF100:
Jay Thompsoncabe3202018-08-17 14:30:04 -0500196 case DEV_SF200:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000197 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 5, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800198 return PROTOCOL_V1;
199 else
200 return PROTOCOL_V2;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000201 case DEV_SF600:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000202 if (dp_data->firmwareversion < FIRMWARE_VERSION(6, 9, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800203 return PROTOCOL_V1;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000204 else if (dp_data->firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
David Hendricksf73f8a72018-02-21 07:34:34 -0800205 return PROTOCOL_V2;
206 else
207 return PROTOCOL_V3;
Nico Huber00578222024-03-02 14:00:53 +0100208 case DEV_SF700:
209 case DEV_SF600PG2:
210 return PROTOCOL_V3;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000211 default:
David Hendricksf73f8a72018-02-21 07:34:34 -0800212 return PROTOCOL_UNKNOWN;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000213 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000214}
Simon Glassae616512016-01-23 23:27:58 +0000215
Nico Huberd99a2bd2016-02-18 21:42:49 +0000216struct dediprog_transfer_status {
Richard Hughes842d6782021-01-15 09:48:12 +0000217 struct flashctx *flash;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000218 int error; /* OK if 0, ERROR else */
219 unsigned int queued_idx;
220 unsigned int finished_idx;
221};
222
223static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
224{
225 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
226 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
227 status->error = 1;
228 msg_perr("SPI bulk read failed!\n");
229 }
Richard Hughes842d6782021-01-15 09:48:12 +0000230 flashprog_progress_add(status->flash, transfer->actual_length);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000231 ++status->finished_idx;
232}
233
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000234static int dediprog_bulk_read_poll(struct libusb_context *usb_ctx,
235 const struct dediprog_transfer_status *const status,
236 const int finish)
Nico Huberd99a2bd2016-02-18 21:42:49 +0000237{
238 if (status->finished_idx >= status->queued_idx)
239 return 0;
240
241 do {
242 struct timeval timeout = { 10, 0 };
243 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
244 if (ret < 0) {
245 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
246 return 1;
247 }
248 } while (finish && (status->finished_idx < status->queued_idx));
249 return 0;
250}
251
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000252static int dediprog_read(libusb_device_handle *dediprog_handle,
253 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
254 uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000255{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000256 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
257 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000258}
259
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000260static int dediprog_write(libusb_device_handle *dediprog_handle,
261 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
262 const uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000263{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000264 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
265 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000266}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000267
Nico Huberd99a2bd2016-02-18 21:42:49 +0000268
Simon Glass25e9f402015-06-28 13:31:19 +0000269/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000270static int dediprog_set_leds(int leds, const struct dediprog_data *dp_data)
Stefan Reinauer915b8402011-01-28 09:00:15 +0000271{
Simon Glass25e9f402015-06-28 13:31:19 +0000272 if (leds < LED_NONE || leds > LED_ALL)
273 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000274
Simon Glassae616512016-01-23 23:27:58 +0000275 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
276 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000277 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000278 * bit 0 == 0: red light is on.
279 *
280 * Additionally, the command structure has changed with the "new" protocol.
Stefan Reinauer915b8402011-01-28 09:00:15 +0000281 */
Nico Huberea27a6e2025-10-03 14:21:00 +0200282 int ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000283 if (protocol(dp_data) >= PROTOCOL_V2) {
Nico Huberea27a6e2025-10-03 14:21:00 +0200284 const unsigned int target_io_leds = (leds ^ 7) << 8 | 0x9;
285 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, target_io_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000286 } else {
Nico Huberea27a6e2025-10-03 14:21:00 +0200287 unsigned int target_leds;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000288 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Simon Glassae616512016-01-23 23:27:58 +0000289 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
290 } else {
291 target_leds = leds;
292 }
293 target_leds ^= 7;
294
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000295 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000296 }
297
Stefan Reinauer915b8402011-01-28 09:00:15 +0000298 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000299 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000300 return 1;
301 }
302
Stefan Reinauer915b8402011-01-28 09:00:15 +0000303 return 0;
304}
305
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000306static int dediprog_set_spi_voltage(libusb_device_handle *dediprog_handle, int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000307{
308 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000309 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000310
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000311 switch (millivolt) {
312 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000313 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000314 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000315 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000316 case 1800:
317 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000318 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000319 case 2500:
320 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000321 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000322 case 3500:
323 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000324 break;
325 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000326 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000327 return 1;
328 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000329 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
330 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000331
Nico Huber4099a8a2012-06-16 00:02:27 +0000332 if (voltage_selector == 0) {
333 /* Wait some time as the original driver does. */
334 programmer_delay(200 * 1000);
335 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000336 ret = dediprog_write(dediprog_handle, CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000337 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000338 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
339 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000340 return 1;
341 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000342 if (voltage_selector != 0) {
343 /* Wait some time as the original driver does. */
344 programmer_delay(200 * 1000);
345 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000346 return 0;
347}
348
Nico Huber77fa67d2013-02-20 18:03:36 +0000349struct dediprog_spispeeds {
350 const char *const name;
351 const int speed;
352};
353
354static const struct dediprog_spispeeds spispeeds[] = {
355 { "24M", 0x0 },
356 { "12M", 0x2 },
357 { "8M", 0x1 },
358 { "3M", 0x3 },
359 { "2.18M", 0x4 },
360 { "1.5M", 0x5 },
361 { "750k", 0x6 },
362 { "375k", 0x7 },
363 { NULL, 0x0 },
364};
365
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000366static int dediprog_set_spi_speed(unsigned int spispeed_idx, const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000367{
Nico Huber00578222024-03-02 14:00:53 +0100368 if (dp_data->devicetype < DEV_SF600PG2 && dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000369 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
370 return 0;
371 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000372
Simon Glass557eb4f2015-07-05 16:53:22 +0000373 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
374 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000375
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000376 int ret = dediprog_write(dp_data->handle, CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000377 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000378 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000379 return 1;
380 }
381 return 0;
382}
383
Nico Hubera1b7f352024-03-25 18:32:11 +0100384static int dediprog_set_io_mode(struct dediprog_data *const dp, const enum io_mode io_mode)
385{
386 const unsigned char dp_io_mode[] = {
387 [SINGLE_IO_1_1_1] = 0,
388 [DUAL_OUT_1_1_2] = 1,
389 [DUAL_IO_1_2_2] = 2,
390 [QUAD_OUT_1_1_4] = 3,
391 [QUAD_IO_1_4_4] = 4,
392 [QPI_4_4_4] = 5,
393 };
394
395 if (dp->devicetype < DEV_SF600)
396 return 0;
397
398 if (dp->io_mode == io_mode)
399 return 0;
400
401 if (io_mode >= ARRAY_SIZE(dp_io_mode)) {
402 msg_perr("%s: Unsupported I/O mode %d! "
403 "Please report a bug at flashprog@flashprog.org\n",
404 __func__, io_mode);
405 return 1;
406 }
407
408 const int ret = dediprog_write(dp->handle, CMD_IO_MODE, dp_io_mode[io_mode], 0, NULL, 0);
409 if (ret) {
410 msg_perr("Command I/O Mode 0x%x failed!\n", dp_io_mode[io_mode]);
411 return 1;
412 }
413
414 dp->io_mode = io_mode;
415 return 0;
416}
417
Nico Huber008a44f2024-04-14 23:39:47 +0200418static int prepare_rw_cmd_common(uint8_t cmd_buf[MAX_CMD_SIZE], uint8_t dp_spi_cmd, unsigned int count)
Nico Huber477e1692019-06-18 23:56:01 +0200419{
Nico Hubera96aaa32024-03-05 12:56:13 +0100420 if (count > MAX_BLOCK_COUNT) {
Nico Huberac90af62022-12-18 00:22:47 +0000421 msg_perr("%s: Unsupported transfer length of %u blocks!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200422 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber477e1692019-06-18 23:56:01 +0200423 __func__, count);
Nico Huber008a44f2024-04-14 23:39:47 +0200424 return -1;
Nico Huber477e1692019-06-18 23:56:01 +0200425 }
426
Nico Huber008a44f2024-04-14 23:39:47 +0200427 /* First 5 bytes are common in all generations. */
428 cmd_buf[0] = count & 0xff;
429 cmd_buf[1] = (count >> 8) & 0xff;
430 cmd_buf[2] = 0; /* RFU */
431 cmd_buf[3] = dp_spi_cmd; /* Read/Write Mode (see enums dediprog_readmode / dediprog_writemode) */
432 cmd_buf[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and
433 WRITE_MODE_4B_ADDR_256B_PAGE_PGM. */
434 return 5;
435}
Simon Glassae616512016-01-23 23:27:58 +0000436
Nico Huber008a44f2024-04-14 23:39:47 +0200437static int prepare_rw_cmd_v1(
438 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
439 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
440{
441 const int cmd_len = prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count);
442 if (cmd_len < 0)
443 return -1;
Nico Huber93db6e12018-09-30 01:18:43 +0200444
Nico Huber008a44f2024-04-14 23:39:47 +0200445 if (flash->chip->feature_bits & FEATURE_4BA_EAR_ANY) {
446 if (spi_set_extended_address(flash, start >> 24))
447 return -1;
448 } else if (start >> 24) {
449 msg_cerr("Can't handle 4-byte address with dediprog.\n");
450 return -1;
451 }
452 /*
453 * We don't know how the dediprog firmware handles 4-byte
454 * addresses. So let's not tell it what we are doing and
455 * only send the lower 3 bytes.
456 */
457 *value = start & 0xffff;
458 *idx = (start >> 16) & 0xff;
459 return cmd_len;
460}
461
462static int prepare_rw_cmd_v2(
463 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
464 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
465{
Nico Hubera1b7f352024-03-25 18:32:11 +0100466 struct dediprog_data *const dp = flash->mst.spi->data;
467
Nico Huber008a44f2024-04-14 23:39:47 +0200468 if (prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count) < 0)
469 return -1;
470
Nico Hubera1b7f352024-03-25 18:32:11 +0100471 if (is_read) {
472 const struct spi_read_op *const read_op = get_spi_read_op(flash);
473 if (dediprog_set_io_mode(dp, read_op->io_mode))
474 return -1;
475
476 if (read_op->native_4ba)
477 cmd_buf[3] = READ_MODE_4B_ADDR_FAST_0x0C;
478 else if (read_op->opcode != JEDEC_READ)
479 cmd_buf[3] = READ_MODE_FAST;
480
481 if (read_op->opcode == JEDEC_READ_4BA)
482 cmd_buf[4] = JEDEC_FAST_READ_4BA;
483 else
484 cmd_buf[4] = read_op->opcode;
485 } else {
486 if (dediprog_set_io_mode(dp, SINGLE_IO_1_1_1))
487 return -1;
488
489 if (dp_spi_cmd == WRITE_MODE_PAGE_PGM
490 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
491 cmd_buf[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
492 cmd_buf[4] = JEDEC_BYTE_PROGRAM_4BA;
493 }
Nico Huber008a44f2024-04-14 23:39:47 +0200494 }
Nico Hubera1b7f352024-03-25 18:32:11 +0100495
Nico Huber008a44f2024-04-14 23:39:47 +0200496 cmd_buf[5] = 0; /* RFU */
497 cmd_buf[6] = (start >> 0) & 0xff;
498 cmd_buf[7] = (start >> 8) & 0xff;
499 cmd_buf[8] = (start >> 16) & 0xff;
500 cmd_buf[9] = (start >> 24) & 0xff;
Nico Hubera1b7f352024-03-25 18:32:11 +0100501
Nico Huber008a44f2024-04-14 23:39:47 +0200502 return 10;
503}
504
505static int prepare_rw_cmd_v3(
506 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
507 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
508{
Nico Hubera1b7f352024-03-25 18:32:11 +0100509 struct dediprog_data *const dp = flash->mst.spi->data;
510
Nico Huber008a44f2024-04-14 23:39:47 +0200511 if (prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count) < 0)
512 return -1;
513
514 cmd_buf[5] = 0; /* RFU */
515 cmd_buf[6] = (start >> 0) & 0xff;
516 cmd_buf[7] = (start >> 8) & 0xff;
517 cmd_buf[8] = (start >> 16) & 0xff;
518 cmd_buf[9] = (start >> 24) & 0xff;
519
520 if (is_read) {
Nico Hubera1b7f352024-03-25 18:32:11 +0100521 const struct spi_read_op *const read_op = get_spi_read_op(flash);
522 if (dediprog_set_io_mode(dp, read_op->io_mode))
523 return -1;
524
525 cmd_buf[3] = READ_MODE_CONFIGURABLE;
526 cmd_buf[4] = read_op->opcode;
527 cmd_buf[10] = read_op->native_4ba || flash->in_4ba_mode ? 4 : 3;
528 cmd_buf[11] = spi_dummy_cycles(read_op) / 2;
Nico Huber008a44f2024-04-14 23:39:47 +0200529 return 12;
Simon Glassae616512016-01-23 23:27:58 +0000530 } else {
Nico Hubera1b7f352024-03-25 18:32:11 +0100531 if (dediprog_set_io_mode(dp, SINGLE_IO_1_1_1))
532 return -1;
533
Nico Huberb1d2bae2024-07-12 16:10:15 +0200534 if (dp_spi_cmd == WRITE_MODE_PAGE_PGM) {
535 if (flash->chip->feature_bits & FEATURE_4BA_WRITE) {
536 cmd_buf[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM;
537 cmd_buf[4] = JEDEC_BYTE_PROGRAM_4BA;
538 } else if (flash->in_4ba_mode) {
539 cmd_buf[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM;
540 cmd_buf[4] = JEDEC_BYTE_PROGRAM;
541 } else if (flashprog_flash_getsize(flash) > 16*MiB) {
542 msg_cerr("Can't handle 4-byte address with dediprog.\n");
543 return -1;
544 }
Nico Huber7eb38aa2019-03-21 15:42:54 +0100545 }
Nico Huber008a44f2024-04-14 23:39:47 +0200546 /* 16 LSBs and 16 HSBs of page size */
547 /* FIXME: This assumes page size of 256. */
548 cmd_buf[10] = 0x00;
549 cmd_buf[11] = 0x01;
550 cmd_buf[12] = 0x00;
551 cmd_buf[13] = 0x00;
552 return 14;
Simon Glassae616512016-01-23 23:27:58 +0000553 }
554}
555
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000556/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
557 * @start start address
558 * @len length
559 * @return 0 on success, 1 on failure
560 */
Simon Glassae616512016-01-23 23:27:58 +0000561static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000562{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000563 int err = 1;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100564 const struct dediprog_data *dp_data = flash->mst.spi->data;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000565
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000566 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000567 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000568 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000569
Richard Hughes842d6782021-01-15 09:48:12 +0000570 struct dediprog_transfer_status status = { flash, 0, 0, 0 };
Nico Huberd99a2bd2016-02-18 21:42:49 +0000571 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
572 struct libusb_transfer *transfer;
573
Nico Huberbbaa1712018-12-05 13:26:20 +0100574 if (len == 0)
575 return 0;
576
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000577 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000578 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200579 "Please report a bug at flashprog@flashprog.org\n",
Simon Glassae616512016-01-23 23:27:58 +0000580 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000581 return 1;
582 }
583
Nico Huber008a44f2024-04-14 23:39:47 +0200584 uint16_t value = 0, idx = 0;
585 uint8_t data_packet[MAX_CMD_SIZE];
586 const int command_packet_size = dp_data->prepare_rw_cmd(
587 flash, data_packet, &value, &idx, /* is_read => */true, READ_MODE_STD, start, count);
588 if (command_packet_size < 0)
Nico Huber7eb38aa2019-03-21 15:42:54 +0100589 return 1;
Simon Glassae616512016-01-23 23:27:58 +0000590
Nico Huber008a44f2024-04-14 23:39:47 +0200591 int ret = dediprog_write(dp_data->handle, CMD_READ, value, idx, data_packet, command_packet_size);
592 if (ret != command_packet_size) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000593 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000594 return 1;
595 }
596
Nico Huberd99a2bd2016-02-18 21:42:49 +0000597 /*
598 * Ring buffer of bulk transfers.
599 * Poll until at least one transfer is ready,
600 * schedule next transfers until buffer is full.
601 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000602
Nico Huberd99a2bd2016-02-18 21:42:49 +0000603 /* Allocate bulk transfers. */
604 unsigned int i;
Nico Huber519be662018-12-23 20:03:35 +0100605 for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000606 transfers[i] = libusb_alloc_transfer(0);
607 if (!transfers[i]) {
608 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
609 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200610 }
611 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000612
613 /* Now transfer requested chunks using libusb's asynchronous interface. */
614 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200615 while ((status.queued_idx < count) &&
616 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
617 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000618 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
Nico Huber821a0852024-03-24 13:34:51 +0100619 libusb_fill_bulk_transfer(transfer, dp_data->handle, dp_data->in_endpoint,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000620 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
621 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
622 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
623 ret = libusb_submit_transfer(transfer);
624 if (ret < 0) {
625 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
626 status.queued_idx, libusb_error_name(ret));
627 goto err_free;
628 }
629 ++status.queued_idx;
630 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000631 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 0))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000632 goto err_free;
633 }
634 /* Wait for transfers to finish. */
Rick Altherr4f9cd8b2021-12-13 17:10:00 -0800635 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000636 goto err_free;
637 /* Check if everything has been transmitted. */
638 if ((status.finished_idx < count) || status.error)
639 goto err_free;
640
641 err = 0;
642
643err_free:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000644 dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000645 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
646 if (transfers[i]) libusb_free_transfer(transfers[i]);
647 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000648}
649
Nico Hubera1b7f352024-03-25 18:32:11 +0100650static int dediprog_slow_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
651{
652 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", start, len);
653
654 /* Override fast-read function for a moment: */
Nico Huber1b1deda2024-04-18 00:35:48 +0200655 struct spi_read_op *const backup = flash->spi_fast_read;
Nico Hubera1b7f352024-03-25 18:32:11 +0100656 flash->spi_fast_read = NULL;
657
658 const int ret = default_spi_read(flash, buf, start, len);
659
660 flash->spi_fast_read = backup;
661 return ret;
662}
663
Simon Glassae616512016-01-23 23:27:58 +0000664static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000665{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000666 int ret;
667 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000668 const unsigned int chunksize = 0x200;
Nico Huberbbaa1712018-12-05 13:26:20 +0100669 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000670 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100671 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000672
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000673 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000674
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000675 if (residue) {
Nico Hubera1b7f352024-03-25 18:32:11 +0100676 ret = dediprog_slow_read(flash, buf, start, residue);
Simon Glass25e9f402015-06-28 13:31:19 +0000677 if (ret)
678 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000679 }
680
681 /* Round down. */
682 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000683 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000684 if (ret)
685 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000686
687 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000688 if (len != 0) {
Nico Hubera1b7f352024-03-25 18:32:11 +0100689 ret = dediprog_slow_read(flash, buf + residue + bulklen,
690 start + residue + bulklen, len);
Simon Glass25e9f402015-06-28 13:31:19 +0000691 if (ret)
692 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000693 }
694
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000695 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000696 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000697err:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000698 dediprog_set_leds(LED_ERROR, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +0000699 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000700}
701
Nico Hubera4b14f72012-06-19 12:06:53 +0000702/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
703 * @chunksize length of data chunks, only 256 supported by now
704 * @start start address
705 * @len length
706 * @dedi_spi_cmd dediprog specific write command for spi bus
707 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000708 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000709static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000710 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000711{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000712 /* USB transfer size must be 512, other sizes will NOT work at all.
713 * chunksize is the real data size per USB bulk transfer. The remaining
714 * space in a USB bulk transfer must be filled with 0xff padding.
715 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000716 const unsigned int count = len / chunksize;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100717 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000718
Nico Hubera4b14f72012-06-19 12:06:53 +0000719 /*
720 * We should change this check to
721 * chunksize > 512
722 * once we know how to handle different chunk sizes.
723 */
724 if (chunksize != 256) {
725 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200726 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000727 __func__, chunksize);
Nico Hubera4b14f72012-06-19 12:06:53 +0000728 return 1;
729 }
730
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000731 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000732 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200733 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000734 __func__, start, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000735 return 1;
736 }
737
738 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000739 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000740 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000741
Nico Huber008a44f2024-04-14 23:39:47 +0200742 uint16_t value = 0, idx = 0;
743 uint8_t data_packet[MAX_CMD_SIZE];
744 const int command_packet_size = dp_data->prepare_rw_cmd(
745 flash, data_packet, &value, &idx, /* is_read => */false, dedi_spi_cmd, start, count);
746 if (command_packet_size < 0)
David Hendricksf73f8a72018-02-21 07:34:34 -0800747 return 1;
David Hendricksf73f8a72018-02-21 07:34:34 -0800748
Nico Huber008a44f2024-04-14 23:39:47 +0200749 int ret = dediprog_write(dp_data->handle, CMD_WRITE, value, idx, data_packet, command_packet_size);
750 if (ret != command_packet_size) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000751 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000752 return 1;
753 }
754
Simon Glassae616512016-01-23 23:27:58 +0000755 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000756 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000757 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000758 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000759 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000760 int transferred;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000761 ret = libusb_bulk_transfer(dp_data->handle, dp_data->out_endpoint, usbbuf, 512, &transferred,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000762 DEFAULT_TIMEOUT);
763 if ((ret < 0) || (transferred != 512)) {
764 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000765 return 1;
766 }
Richard Hughes842d6782021-01-15 09:48:12 +0000767 flashprog_progress_add(flash, chunksize);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000768 }
769
770 return 0;
771}
772
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000773static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000774 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000775{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000776 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000777 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000778 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
779 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100780 const struct dediprog_data *dp_data = flash->mst.spi->data;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000781
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000782 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000783
Nico Hubera4b14f72012-06-19 12:06:53 +0000784 if (chunksize != 256) {
785 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
786 "we don't know how dediprog\nhandles them.\n");
787 /* Write everything like it was residue. */
788 residue = len;
789 }
790
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000791 if (residue) {
792 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
793 start, residue);
Nico Huber93db6e12018-09-30 01:18:43 +0200794 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
Nico Hubere36e3dc2023-04-28 21:23:08 +0000795 ret = default_spi_write_256(flash, buf, start, residue);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000796 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000797 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000798 return ret;
799 }
800 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000801
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000802 /* Round down. */
803 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000804 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000805 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000806 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000807 return ret;
808 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000809
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000810 len -= residue + bulklen;
811 if (len) {
812 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
813 start, len);
Nico Hubere36e3dc2023-04-28 21:23:08 +0000814 ret = default_spi_write_256(flash, buf + residue + bulklen, start + residue + bulklen, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000815 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000816 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000817 return ret;
818 }
819 }
820
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000821 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000822 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000823}
824
Nico Hubera96aaa32024-03-05 12:56:13 +0100825static int dediprog_spi_write_chunked(struct flashctx *flash, const uint8_t *buf,
826 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
827{
828 /* We can write only up to 65535 pages at once: */
829 while (len) {
830 const size_t len_here = MIN(len, flash->chip->page_size * MAX_BLOCK_COUNT);
831 const int ret = dediprog_spi_write(flash, buf, start, len_here, dedi_spi_cmd);
832 if (ret)
833 return ret;
834
835 start += len_here;
836 buf += len_here;
837 len -= len_here;
838 }
839 return 0;
840}
841
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000842static int dediprog_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Nico Hubera4b14f72012-06-19 12:06:53 +0000843{
Nico Hubera96aaa32024-03-05 12:56:13 +0100844 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000845}
846
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000847static int dediprog_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Nico Hubera4b14f72012-06-19 12:06:53 +0000848{
Nico Hubera96aaa32024-03-05 12:56:13 +0100849 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000850}
851
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000852static int dediprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000853 unsigned int writecnt,
854 unsigned int readcnt,
855 const unsigned char *writearr,
856 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000857{
Nico Hubera1b7f352024-03-25 18:32:11 +0100858 struct dediprog_data *const dp_data = flash->mst.spi->data;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000859 int ret;
860
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000861 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Nico Hubere36e3dc2023-04-28 21:23:08 +0000862 if (writecnt > flash->mst.spi->max_data_write + 5) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000863 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000864 return 1;
865 }
Nico Huber9a11cbf2023-01-13 01:19:07 +0100866 if (readcnt > flash->mst.spi->max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000867 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000868 return 1;
869 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100870
Nico Hubera1b7f352024-03-25 18:32:11 +0100871 if (dediprog_set_io_mode(dp_data, SINGLE_IO_1_1_1))
872 return 1;
873
Simon Glassae616512016-01-23 23:27:58 +0000874 unsigned int idx, value;
875 /* New protocol has options and timeout combined as value while the old one used the value field for
876 * timeout and the index field for options. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000877 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000878 idx = 0;
879 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
880 } else {
881 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
882 value = 0;
883 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000884 ret = dediprog_write(dp_data->handle, CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Nico Huber519be662018-12-23 20:03:35 +0100885 if (ret != (int)writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000886 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000887 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000888 return 1;
889 }
Simon Glassae616512016-01-23 23:27:58 +0000890 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000891 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000892
Stefan Tauner74367bf2016-02-20 20:53:46 +0000893 /* The specifications do state the possibility to set a timeout for transceive transactions.
894 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
895 * can predict the time needed by the previous command or so (untested). In any case, using this
896 * "feature" to set sane-looking timouts for the read below will completely trash performance with
897 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
898 *
899 * The specification also uses only 0 in its examples, so the lesson to learn here:
900 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000901 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800902 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000903 idx = 0;
904 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
905 } else {
906 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
907 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
908 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000909 ret = dediprog_read(dp_data->dediprog_handle, CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000910 */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000911 ret = dediprog_read(dp_data->handle, CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Nico Huber519be662018-12-23 20:03:35 +0100912 if (ret != (int)readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000913 msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000914 return 1;
915 }
916 return 0;
917}
918
Nico Huber823a7042024-03-24 18:32:58 +0100919static int dediprog_read_devicestring(struct dediprog_data *dp_data, bool warn)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000920{
Nico Huber274e6552024-03-24 12:34:57 +0100921 const int devstr_len = sizeof(dp_data->devicestring) - 1, old_devstr_len = 16;
922 char *const buf = dp_data->devicestring;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000923 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000924
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000925 /* Command Receive Device String. */
Nico Hubere76e21f2024-02-29 23:33:35 +0100926 ret = dediprog_read(dp_data->handle, CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, devstr_len);
Nico Huber5262e292024-03-02 13:21:25 +0100927 if (ret < old_devstr_len || ret > devstr_len) {
Nico Huber823a7042024-03-24 18:32:58 +0100928 if (warn)
929 msg_perr("Incomplete/failed Command Receive Device String!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000930 return 1;
931 }
Nico Huber5262e292024-03-02 13:21:25 +0100932 buf[ret] = '\0';
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000933 msg_pdbg("Found a %s\n", buf);
Nico Hubere76e21f2024-02-29 23:33:35 +0100934 if (memcmp(buf, "SF100", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000935 dp_data->devicetype = DEV_SF100;
Nico Hubere76e21f2024-02-29 23:33:35 +0100936 else if (memcmp(buf, "SF200", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000937 dp_data->devicetype = DEV_SF200;
Nico Huber00578222024-03-02 14:00:53 +0100938 else if (memcmp(buf, "SF600PG2", 8) == 0) /* match first, before shorter, generic SF600 */
939 dp_data->devicetype = DEV_SF600PG2;
Nico Hubere76e21f2024-02-29 23:33:35 +0100940 else if (memcmp(buf, "SF600", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000941 dp_data->devicetype = DEV_SF600;
Nico Huber00578222024-03-02 14:00:53 +0100942 else if (memcmp(buf, "SF700", 5) == 0)
943 dp_data->devicetype = DEV_SF700;
Nico Huber274e6552024-03-24 12:34:57 +0100944 else
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000945 return 1;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000946
Nico Huber274e6552024-03-24 12:34:57 +0100947 return 0;
948}
949
950static int dediprog_check_devicestring(struct dediprog_data *dp_data)
951{
952 char *const buf = dp_data->devicestring;
Nico Huberbdef5c22024-03-02 13:51:39 +0100953 unsigned int sfnum;
954 unsigned int fw[3];
Nico Huber274e6552024-03-24 12:34:57 +0100955
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100956 if (sscanf(buf, "SF%u", &sfnum) != 1 ||
Nico Huber00578222024-03-02 14:00:53 +0100957 sfnum != dp_data->devicetype / 100 * 100 ||
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100958 sscanf(buf, "SF%*s V:%u.%u.%u ", &fw[0], &fw[1], &fw[2]) != 3) {
Simon Glassae616512016-01-23 23:27:58 +0000959 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000960 return 1;
961 }
Nico Huber00578222024-03-02 14:00:53 +0100962
963 /* Only allow major versions that were tested. */
964 if ((dp_data->devicetype == DEV_SF600PG2 && fw[0] > 1) ||
965 (dp_data->devicetype == DEV_SF700 && fw[0] != 4) ||
966 (dp_data->devicetype <= DEV_SF600 && (fw[0] < 2 || fw[0] > 7))) {
Simon Glassae616512016-01-23 23:27:58 +0000967 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000968 return 1;
969 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800970
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000971 dp_data->firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000972 if (protocol(dp_data) == PROTOCOL_UNKNOWN) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800973 msg_perr("Internal error: Unable to determine protocol version.\n");
974 return 1;
975 }
Simon Glassae616512016-01-23 23:27:58 +0000976
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000977 return 0;
978}
979
David Hendricksc05900f2016-02-18 21:42:41 +0000980/*
Ryan O'Leary301ae222019-06-24 19:14:33 -0700981 * Read the id from the dediprog. This should return the numeric part of the
982 * serial number found on a sticker on the back of the dediprog. Note this
Nico Huber821a0852024-03-24 13:34:51 +0100983 * number is stored in writable eeprom, so it could get out of sync.
Ryan O'Leary301ae222019-06-24 19:14:33 -0700984 * @return the id on success, -1 on failure
985 */
Nico Huber821a0852024-03-24 13:34:51 +0100986static int dediprog_read_id(struct dediprog_data *const dp)
Ryan O'Leary301ae222019-06-24 19:14:33 -0700987{
Nico Huber821a0852024-03-24 13:34:51 +0100988 const int min_len = 3;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700989 int ret;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700990
Nico Huber821a0852024-03-24 13:34:51 +0100991 if (dp->devicetype >= DEV_SF600PG2) {
992 const uint8_t out[6] = { 0x00, 0x00, 0x00, 0x02, 0x00, 0x00 };
993 const uint8_t cmd = 0x71;
994 uint8_t buf[512];
995 int transferred;
996 int try;
997
998 /* Always query the id twice as the endpoint
999 can lock up in mysterious ways otherwise. */
1000 for (try = 0; try < 2; ++try) {
1001 ret = dediprog_write(dp->handle, cmd, 0, 0, out, sizeof(out));
1002 if (ret != (int)sizeof(out))
1003 goto failed_ret;
1004
1005 ret = libusb_bulk_transfer(dp->handle, dp->in_endpoint,
1006 buf, sizeof(buf), &transferred, DEFAULT_TIMEOUT);
1007 }
1008
1009 if (ret == 0 && transferred >= min_len)
1010 return buf[2] << 16 | buf[1] << 8 | buf[0];
1011 } else {
1012 uint8_t buf[16];
1013
1014 if (dp->devicetype >= DEV_SF600) {
1015 ret = dediprog_read(dp->handle, CMD_READ_EEPROM, 0, 0, buf, sizeof(buf));
1016 } else {
1017 ret = libusb_control_transfer(dp->handle, REQTYPE_OTHER_IN,
1018 0x7, /* request */
1019 0, /* value */
1020 0xEF00, /* index */
1021 buf, min_len, DEFAULT_TIMEOUT);
1022 }
1023
1024 if (ret >= min_len)
1025 return buf[0] << 16 | buf[1] << 8 | buf[2];
Ryan O'Leary301ae222019-06-24 19:14:33 -07001026 }
1027
Nico Huber821a0852024-03-24 13:34:51 +01001028failed_ret:
1029 msg_perr("Failed to read dediprog id: ");
1030 if (ret < 0)
1031 msg_perr("%s (%d)\n", libusb_strerror(ret), ret);
1032 else
1033 msg_perr("short read!\n");
1034 return -1;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001035}
1036
1037/*
David Hendricksc05900f2016-02-18 21:42:41 +00001038 * This command presumably sets the voltage for the SF100 itself (not the
1039 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
1040 * (including all SF600s) do not support it.
1041 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001042
David Hendricksc05900f2016-02-18 21:42:41 +00001043/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
1044 * Only use dediprog_set_voltage on SF100 programmers with firmware older
1045 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001046static int dediprog_set_voltage(libusb_device_handle *dediprog_handle)
David Hendricksc05900f2016-02-18 21:42:41 +00001047{
Nico Huberd99a2bd2016-02-18 21:42:49 +00001048 unsigned char buf[1] = {0};
1049 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +00001050 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +00001051 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001052 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +00001053 return 1;
1054 }
David Hendricksc05900f2016-02-18 21:42:41 +00001055 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +00001056 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001057 return 1;
1058 }
David Hendricksc05900f2016-02-18 21:42:41 +00001059
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001060 return 0;
1061}
1062
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001063static int dediprog_standalone_mode(const struct dediprog_data *dp_data)
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001064{
1065 int ret;
1066
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001067 if (dp_data->devicetype != DEV_SF600)
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001068 return 0;
1069
1070 msg_pdbg2("Disabling standalone mode.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001071 ret = dediprog_write(dp_data->handle, CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001072 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001073 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001074 return 1;
1075 }
1076
1077 return 0;
1078}
1079
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001080#if 0
1081/* Something.
1082 * Present in eng_detect_blink.log with firmware 3.1.8
1083 * Always preceded by Command Receive Device String
1084 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001085static int dediprog_command_b(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001086{
1087 int ret;
1088 char buf[0x3];
1089
Simon Glass557eb4f2015-07-05 16:53:22 +00001090 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
1091 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001092 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001093 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001094 return 1;
1095 }
1096 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
1097 (buf[2] != 0xff)) {
1098 msg_perr("Unexpected response to Command B!\n");
1099 return 1;
1100 }
1101
1102 return 0;
1103}
1104#endif
1105
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001106static int set_target_flash(libusb_device_handle *dediprog_handle, enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001107{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001108 int ret = dediprog_write(dediprog_handle, CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +00001109 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001110 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001111 return 1;
1112 }
1113 return 0;
1114}
1115
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001116#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +00001117/* Returns true if the button is currently pressed. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001118static bool dediprog_get_button(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001119{
Simon Glass557eb4f2015-07-05 16:53:22 +00001120 char buf[1];
1121 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
1122 buf, 0x1, DEFAULT_TIMEOUT);
1123 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001124 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001125 return 1;
1126 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001127 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001128}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001129#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001130
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001131static int parse_voltage(char *voltage)
1132{
1133 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001134 int i;
1135 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001136
1137 if (!voltage || !strlen(voltage)) {
1138 msg_perr("Empty voltage= specified.\n");
1139 return -1;
1140 }
1141 millivolt = (int)strtol(voltage, &tmp, 0);
1142 voltage = tmp;
1143 /* Handle "," and "." as decimal point. Everything after it is assumed
1144 * to be in decimal notation.
1145 */
1146 if ((*voltage == '.') || (*voltage == ',')) {
1147 voltage++;
1148 for (i = 0; i < 3; i++) {
1149 fraction *= 10;
1150 /* Don't advance if the current character is invalid,
1151 * but continue multiplying.
1152 */
1153 if ((*voltage < '0') || (*voltage > '9'))
1154 continue;
1155 fraction += *voltage - '0';
1156 voltage++;
1157 }
1158 /* Throw away remaining digits. */
1159 voltage += strspn(voltage, "0123456789");
1160 }
1161 /* The remaining string must be empty or "mV" or "V". */
1162 tolower_string(voltage);
1163
1164 /* No unit or "V". */
1165 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1166 millivolt *= 1000;
1167 millivolt += fraction;
1168 } else if (!strncmp(voltage, "mv", 2) ||
1169 !strncmp(voltage, "milliv", 6)) {
1170 /* No adjustment. fraction is discarded. */
1171 } else {
1172 /* Garbage at the end of the string. */
1173 msg_perr("Garbage voltage= specified.\n");
1174 return -1;
1175 }
1176 return millivolt;
1177}
1178
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001179static int dediprog_shutdown(void *data);
1180
Nico Huber93db6e12018-09-30 01:18:43 +02001181static struct spi_master spi_master_dediprog = {
Nico Huberdc5af542018-12-22 16:54:59 +01001182 .features = SPI_MASTER_NO_4BA_MODES,
Simon Glassae616512016-01-23 23:27:58 +00001183 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
Nico Hubere36e3dc2023-04-28 21:23:08 +00001184 .max_data_write = 16 - 5, /* Account for 5 bytes cmd/addr. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001185 .command = dediprog_spi_send_command,
1186 .multicommand = default_spi_send_multicommand,
1187 .read = dediprog_spi_read,
1188 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +00001189 .write_aai = dediprog_spi_write_aai,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001190 .shutdown = dediprog_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301191 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001192};
1193
Ryan O'Leary301ae222019-06-24 19:14:33 -07001194/*
1195 * Open a dediprog_handle with the USB device at the given index.
1196 * @index index of the USB device
Nico Huber274e6552024-03-24 12:34:57 +01001197 * @return 0 for success, -1 for error, -2 for busy device, -3 for unknown device
Ryan O'Leary301ae222019-06-24 19:14:33 -07001198 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001199static int dediprog_open(int index, struct dediprog_data *dp_data)
Ryan O'Leary301ae222019-06-24 19:14:33 -07001200{
1201 const uint16_t vid = devs_dediprog[0].vendor_id;
1202 const uint16_t pid = devs_dediprog[0].device_id;
1203 int ret;
1204
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001205 dp_data->handle = usb_dev_get_by_vid_pid_number(dp_data->usb_ctx, vid, pid, (unsigned int) index);
1206 if (!dp_data->handle) {
Ryan O'Leary301ae222019-06-24 19:14:33 -07001207 msg_perr("Could not find a Dediprog programmer on USB.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001208 libusb_exit(dp_data->usb_ctx);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001209 return -1;
1210 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001211 ret = libusb_set_configuration(dp_data->handle, 1);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001212 if (ret != 0) {
1213 msg_perr("Could not set USB device configuration: %i %s\n",
1214 ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001215 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001216 return -2;
1217 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001218 ret = libusb_claim_interface(dp_data->handle, 0);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001219 if (ret < 0) {
1220 msg_perr("Could not claim USB device interface %i: %i %s\n",
1221 0, ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001222 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001223 return -2;
1224 }
Nico Huber274e6552024-03-24 12:34:57 +01001225
1226 /* Try reading the devicestring. If that fails and the device is old
1227 (FW < 6.0.0, which we cannot know), then we need to try the "set
1228 voltage" command and then attempt to read the devicestring again. */
Nico Huber823a7042024-03-24 18:32:58 +01001229 if (dediprog_read_devicestring(dp_data, /* warn => */false)) {
Nico Huber274e6552024-03-24 12:34:57 +01001230 if (dediprog_set_voltage(dp_data->handle))
1231 goto unknown_dev;
Nico Huber823a7042024-03-24 18:32:58 +01001232 if (dediprog_read_devicestring(dp_data, /* warn => */true))
Nico Huber274e6552024-03-24 12:34:57 +01001233 goto unknown_dev;
1234 }
Nico Huber821a0852024-03-24 13:34:51 +01001235
1236 dp_data->in_endpoint = LIBUSB_ENDPOINT_IN | 2;
1237 if (dp_data->devicetype <= DEV_SF200)
1238 dp_data->out_endpoint = 2;
1239 else
1240 dp_data->out_endpoint = 1;
1241
Ryan O'Leary301ae222019-06-24 19:14:33 -07001242 return 0;
Nico Huber274e6552024-03-24 12:34:57 +01001243
1244unknown_dev:
1245 msg_pwarn("Ignoring unknown Dediprog device. Not a SF100, SF200, SF600(Plus(G2)), or SF700!\n");
1246 libusb_release_interface(dp_data->handle, 0);
1247 libusb_close(dp_data->handle);
1248 return -3;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001249}
1250
David Hendricks8bb20212011-06-14 01:35:36 +00001251static int dediprog_shutdown(void *data)
1252{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001253 int ret = 0;
1254 struct dediprog_data *dp_data = data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001255
Nico Hubera1b7f352024-03-25 18:32:11 +01001256 dediprog_set_io_mode(dp_data, SINGLE_IO_1_1_1);
1257
David Hendricks8bb20212011-06-14 01:35:36 +00001258 /* URB 28. Command Set SPI Voltage to 0. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001259 if (dediprog_set_spi_voltage(dp_data->handle, 0x0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001260 ret = 1;
1261 goto out;
David Hendricks8bb20212011-06-14 01:35:36 +00001262 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001263
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001264 if (libusb_release_interface(dp_data->handle, 0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001265 msg_perr("Could not release USB interface!\n");
1266 ret = 1;
1267 goto out;
1268 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001269 libusb_close(dp_data->handle);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001270 libusb_exit(dp_data->usb_ctx);
1271out:
1272 free(data);
1273 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +00001274}
1275
Nico Hubere3a26882023-01-11 21:45:51 +01001276static int dediprog_init(struct flashprog_programmer *const prog)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001277{
Ryan O'Leary301ae222019-06-24 19:14:33 -07001278 char *voltage, *id_str, *device, *spispeed, *target_str;
Nico Hubera1b7f352024-03-25 18:32:11 +01001279 enum {
1280 DEFAULT,
1281 SINGLE,
1282 DUAL,
1283 QUAD,
1284 } io_mode = DEFAULT;
Patrick Georgiefe2d432013-05-23 21:47:46 +00001285 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001286 int millivolt = 3500;
Nico Hubere8463c82024-03-24 16:50:50 +01001287 long id = -1; /* -1 defaults to enumeration order */
Ryan O'Leary301ae222019-06-24 19:14:33 -07001288 int found_id;
Nathan Laredo21541a62012-12-24 22:07:36 +00001289 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +02001290 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +00001291 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001292
Nico Hubera1b7f352024-03-25 18:32:11 +01001293 char *const io_mode_str = extract_programmer_param("iomode");
1294 if (io_mode_str) {
1295 if (strcmp(io_mode_str, "single") == 0) {
1296 io_mode = SINGLE;
1297 } else if (strcmp(io_mode_str, "dual") == 0) {
1298 io_mode = DUAL;
1299 } else if (strcmp(io_mode_str, "quad") == 0) {
1300 io_mode = QUAD;
1301 } else {
1302 msg_perr("Invalid iomode setting: %s\n", io_mode_str);
1303 return SPI_GENERIC_ERROR;
1304 }
1305 }
1306 free(io_mode_str);
1307
Nico Huber77fa67d2013-02-20 18:03:36 +00001308 spispeed = extract_programmer_param("spispeed");
1309 if (spispeed) {
1310 for (i = 0; spispeeds[i].name; ++i) {
1311 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1312 spispeed_idx = i;
1313 break;
1314 }
1315 }
1316 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +00001317 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +00001318 free(spispeed);
1319 return 1;
1320 }
1321 free(spispeed);
1322 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001323
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001324 voltage = extract_programmer_param("voltage");
1325 if (voltage) {
1326 millivolt = parse_voltage(voltage);
1327 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001328 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001329 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001330 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1331 }
1332
Ryan O'Leary301ae222019-06-24 19:14:33 -07001333 id_str = extract_programmer_param("id");
1334 if (id_str) {
Nico Hubere8463c82024-03-24 16:50:50 +01001335 const char prefixes[][4] = { "SF", "DP", "S6B", };
1336 for (i = 0; i < (int)ARRAY_SIZE(prefixes); ++i) {
1337 if (!strncmp(id_str, prefixes[i], strlen(prefixes[i])))
1338 break;
1339 }
1340 if (i >= (int)ARRAY_SIZE(prefixes)) {
1341 msg_perr("Error: Could not parse dediprog `id'.\n");
1342 msg_perr("Expected a string prefixed with any of ");
1343 for (i = 0; i < (int)ARRAY_SIZE(prefixes); ++i)
1344 msg_perr("%s`%s'", i > 0 ? ", " : "", prefixes[i]);
1345 msg_perr(".\n");
1346 free(id_str);
1347 return 1;
1348 }
1349 char *endptr;
1350 id = strtol(id_str + strlen(prefixes[i]), &endptr, 10);
1351 if (strlen(id_str) <= strlen(prefixes[i]) || *endptr) {
1352 msg_perr("Error: Could not parse dediprog `id'.\n");
1353 msg_perr("Expected a number after string prefix.\n");
Ryan O'Leary301ae222019-06-24 19:14:33 -07001354 free(id_str);
1355 return 1;
1356 }
1357 if (id < 0 || id >= 0x1000000) {
Nico Hubere8463c82024-03-24 16:50:50 +01001358 msg_perr("Error: id `%s' is out of range!\n", id_str);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001359 free(id_str);
1360 return 1;
1361 }
1362 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1363 }
1364 free(id_str);
1365
Nathan Laredo21541a62012-12-24 22:07:36 +00001366 device = extract_programmer_param("device");
1367 if (device) {
1368 char *dev_suffix;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001369 if (id != -1) {
1370 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1371 }
Nathan Laredo21541a62012-12-24 22:07:36 +00001372 errno = 0;
1373 usedevice = strtol(device, &dev_suffix, 10);
1374 if (errno != 0 || device == dev_suffix) {
1375 msg_perr("Error: Could not convert 'device'.\n");
1376 free(device);
1377 return 1;
1378 }
Nico Huber961f4a12019-10-04 17:34:22 +02001379 if (usedevice < 0 || usedevice > INT_MAX) {
Nathan Laredo21541a62012-12-24 22:07:36 +00001380 msg_perr("Error: Value for 'device' is out of range.\n");
1381 free(device);
1382 return 1;
1383 }
1384 if (strlen(dev_suffix) > 0) {
1385 msg_perr("Error: Garbage following 'device' value.\n");
1386 free(device);
1387 return 1;
1388 }
1389 msg_pinfo("Using device %li.\n", usedevice);
1390 }
1391 free(device);
1392
Stefan Taunere659d2d2013-05-03 21:58:28 +00001393 target_str = extract_programmer_param("target");
1394 if (target_str) {
1395 char *target_suffix;
1396 errno = 0;
1397 target = strtol(target_str, &target_suffix, 10);
1398 if (errno != 0 || target_str == target_suffix) {
1399 msg_perr("Error: Could not convert 'target'.\n");
1400 free(target_str);
1401 return 1;
1402 }
1403 if (target < 1 || target > 2) {
1404 msg_perr("Error: Value for 'target' is out of range.\n");
1405 free(target_str);
1406 return 1;
1407 }
1408 if (strlen(target_suffix) > 0) {
1409 msg_perr("Error: Garbage following 'target' value.\n");
1410 free(target_str);
1411 return 1;
1412 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001413 switch (target) {
1414 case 1:
1415 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1416 target = FLASH_TYPE_APPLICATION_FLASH_1;
1417 break;
1418 case 2:
1419 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1420 target = FLASH_TYPE_APPLICATION_FLASH_2;
1421 break;
1422 default:
1423 break;
1424 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001425 }
1426 free(target_str);
1427
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001428 struct dediprog_data *dp_data = calloc(1, sizeof(*dp_data));
1429 if (!dp_data) {
1430 msg_perr("Unable to allocate space for SPI master data\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001431 return 1;
1432 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001433 dp_data->firmwareversion = FIRMWARE_VERSION(0, 0, 0);
1434 dp_data->devicetype = DEV_UNKNOWN;
Nico Hubera1b7f352024-03-25 18:32:11 +01001435 dp_data->io_mode = -1;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001436
1437 /* Here comes the USB stuff. */
Thomas Heijligen88e87c52022-08-05 17:56:20 +02001438 ret = libusb_init(&dp_data->usb_ctx);
1439 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001440 msg_perr("Could not initialize libusb!\n");
1441 goto init_err_exit;
1442 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001443
Ryan O'Leary301ae222019-06-24 19:14:33 -07001444 if (id != -1) {
1445 for (i = 0; ; i++) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001446 ret = dediprog_open(i, dp_data);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001447 if (ret == -1) {
1448 /* no dev */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001449 goto init_err_exit;
Nico Huber274e6552024-03-24 12:34:57 +01001450 } else if (ret < 0) {
1451 /* busy or unknown dev */
Ryan O'Leary301ae222019-06-24 19:14:33 -07001452 continue;
1453 }
1454
1455 /* Notice we can only call dediprog_read_id() after
1456 * libusb_set_configuration() and
1457 * libusb_claim_interface(). When searching by id and
1458 * either configuration or claim fails (usually the
Nico Huberc3b02dc2023-08-12 01:13:45 +02001459 * device is in use by another instance of flashprog),
Ryan O'Leary301ae222019-06-24 19:14:33 -07001460 * the device is skipped and the next device is tried.
1461 */
Nico Huber821a0852024-03-24 13:34:51 +01001462 found_id = dediprog_read_id(dp_data);
Nico Huber38af1a12024-03-24 15:04:20 +01001463 if (found_id >= 0)
1464 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1465 if (found_id == id)
1466 break;
1467
1468 libusb_release_interface(dp_data->handle, 0);
1469 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001470 }
1471 } else {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001472 if (dediprog_open(usedevice, dp_data)) {
1473 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001474 }
Nico Huber821a0852024-03-24 13:34:51 +01001475 found_id = dediprog_read_id(dp_data);
David Woodhoused2a7e872013-07-30 09:34:44 +00001476 }
Ryan O'Leary301ae222019-06-24 19:14:33 -07001477
1478 if (found_id >= 0) {
1479 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001480 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001481
Nico Huber274e6552024-03-24 12:34:57 +01001482 if (dediprog_check_devicestring(dp_data))
1483 goto init_err_cleanup_exit;
Nico Huber77fa67d2013-02-20 18:03:36 +00001484
Simon Glass25e9f402015-06-28 13:31:19 +00001485 /* Set all possible LEDs as soon as possible to indicate activity.
1486 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001487 * dediprog_check_devicestring() has queried the device. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001488 dediprog_set_leds(LED_ALL, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +00001489
Simon Glass557eb4f2015-07-05 16:53:22 +00001490 /* Select target/socket, frequency and VCC. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001491 if (set_target_flash(dp_data->handle, target) ||
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001492 dediprog_set_spi_speed(spispeed_idx, dp_data) ||
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001493 dediprog_set_spi_voltage(dp_data->handle, millivolt)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001494 dediprog_set_leds(LED_ERROR, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001495 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001496 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001497
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001498 if (dediprog_standalone_mode(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001499 goto init_err_cleanup_exit;
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001500
Nico Huber008a44f2024-04-14 23:39:47 +02001501 switch (protocol(dp_data)) {
1502 case PROTOCOL_V3: dp_data->prepare_rw_cmd = prepare_rw_cmd_v3; break;
1503 case PROTOCOL_V2: dp_data->prepare_rw_cmd = prepare_rw_cmd_v2; break;
1504 default: dp_data->prepare_rw_cmd = prepare_rw_cmd_v1; break;
1505 }
1506
Nico Hubera1b7f352024-03-25 18:32:11 +01001507 if (io_mode == DEFAULT) {
Nico Huberff9526b2025-02-27 21:52:02 +01001508 if (dp_data->devicetype != DEV_SF600PG2) {
1509 msg_pdbg("Multi i/o is only tested with SF600Plus-G2, not enabling by default.\n");
Nico Hubera1b7f352024-03-25 18:32:11 +01001510 } else {
1511 io_mode = DUAL;
1512 }
1513 } else if (io_mode > SINGLE &&
1514 (dp_data->devicetype < DEV_SF600 || protocol(dp_data) < PROTOCOL_V2)) {
1515 msg_pinfo("Multi i/o is only supported for SF600 and SF700 models w/ protocol v2 or later.\n");
1516 io_mode = SINGLE;
1517 }
1518
1519 switch (io_mode) {
1520 case DUAL: spi_master_dediprog.features |= SPI_MASTER_DUAL; break;
1521 case QUAD: spi_master_dediprog.features |= SPI_MASTER_DUAL | SPI_MASTER_QUAD; break;
1522 default: break;
1523 }
1524
1525 /* The v2, fixed-op JEDEC_FAST_READ_DUAL_DIO command
1526 seems to use the wrong number of dummy cycles. */
1527 if (protocol(dp_data) < PROTOCOL_V3)
1528 spi_master_dediprog.features &= ~SPI_MASTER_DUAL_IO;
1529
Nico Huberd0afeef2024-07-12 16:08:29 +02001530 if ((dp_data->devicetype == DEV_SF100 && protocol(dp_data) == PROTOCOL_V1) ||
Nico Huberb1d2bae2024-07-12 16:10:15 +02001531 (dp_data->devicetype >= DEV_SF600 && protocol(dp_data) == PROTOCOL_V3))
Nico Huber7eb38aa2019-03-21 15:42:54 +01001532 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1533
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001534 if (protocol(dp_data) >= PROTOCOL_V2)
Nico Huber93db6e12018-09-30 01:18:43 +02001535 spi_master_dediprog.features |= SPI_MASTER_4BA;
1536
Angel Pons116d56d2021-03-22 11:28:00 +01001537 if (dediprog_set_leds(LED_NONE, dp_data))
1538 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001539
Nico Huber89569d62023-01-12 23:31:40 +01001540 return register_spi_master(&spi_master_dediprog, 0, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001541
1542init_err_cleanup_exit:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001543 dediprog_shutdown(dp_data);
1544 return 1;
1545
1546init_err_exit:
1547 free(dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001548 return 1;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001549}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001550
1551const struct programmer_entry programmer_dediprog = {
1552 .name = "dediprog",
1553 .type = USB,
1554 .devs.dev = devs_dediprog,
1555 .init = dediprog_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001556};