blob: d8a452f92511cc0e6df47549b4eca24813df4535 [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"
Nico Huberfbc41d22026-02-22 23:04:01 +010026#include "chipdrivers/spi.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 */
Arthur Heymansdb236b02026-06-21 14:16:28 +020043#define DEDIPROG_INTERFACE 0
David Hendricksc3685182020-06-23 17:36:09 -070044#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
Nico Huberd99a2bd2016-02-18 21:42:49 +000045#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
46#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
47#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
David Woodhouse7f3b89f2016-02-18 21:42:15 +000048
49enum dediprog_devtype {
50 DEV_UNKNOWN = 0,
51 DEV_SF100 = 100,
Jay Thompsoncabe3202018-08-17 14:30:04 -050052 DEV_SF200 = 200,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000053 DEV_SF600 = 600,
Nico Huber00578222024-03-02 14:00:53 +010054 DEV_SF600PG2 = 600+2,
55 DEV_SF700 = 700,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000056};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000057
Simon Glass25e9f402015-06-28 13:31:19 +000058enum dediprog_leds {
59 LED_INVALID = -1,
60 LED_NONE = 0,
61 LED_PASS = 1 << 0,
62 LED_BUSY = 1 << 1,
63 LED_ERROR = 1 << 2,
64 LED_ALL = 7,
65};
66
Simon Glass557eb4f2015-07-05 16:53:22 +000067enum dediprog_cmds {
68 CMD_TRANSCEIVE = 0x01,
69 CMD_POLL_STATUS_REG = 0x02,
70 CMD_SET_VPP = 0x03,
71 CMD_SET_TARGET = 0x04,
72 CMD_READ_EEPROM = 0x05,
73 CMD_WRITE_EEPROM = 0x06,
74 CMD_SET_IO_LED = 0x07,
75 CMD_READ_PROG_INFO = 0x08,
76 CMD_SET_VCC = 0x09,
77 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000078 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000079 CMD_GET_BUTTON = 0x11,
80 CMD_GET_UID = 0x12,
81 CMD_SET_CS = 0x14,
82 CMD_IO_MODE = 0x15,
83 CMD_FW_UPDATE = 0x1A,
84 CMD_FPGA_UPDATE = 0x1B,
85 CMD_READ_FPGA_VERSION = 0x1C,
86 CMD_SET_HOLD = 0x1D,
87 CMD_READ = 0x20,
88 CMD_WRITE = 0x30,
89 CMD_WRITE_AT45DB = 0x31,
90 CMD_NAND_WRITE = 0x32,
91 CMD_NAND_READ = 0x33,
92 CMD_SET_SPI_CLK = 0x61,
93 CMD_CHECK_SOCKET = 0x62,
94 CMD_DOWNLOAD_PRJ = 0x63,
95 CMD_READ_PRJ_NAME = 0x64,
96 // New protocol/firmware only
97 CMD_CHECK_SDCARD = 0x65,
98 CMD_READ_PRJ = 0x66,
99};
100
101enum dediprog_target {
102 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
103 FLASH_TYPE_FLASH_CARD,
104 FLASH_TYPE_APPLICATION_FLASH_2,
105 FLASH_TYPE_SOCKET,
106};
107
108enum dediprog_readmode {
109 READ_MODE_STD = 1,
110 READ_MODE_FAST = 2,
111 READ_MODE_ATMEL45 = 3,
112 READ_MODE_4B_ADDR_FAST = 4,
113 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Nico Hubera1b7f352024-03-25 18:32:11 +0100114 READ_MODE_CONFIGURABLE = 9, /* Not seen documented so far */
Simon Glass557eb4f2015-07-05 16:53:22 +0000115};
116
117enum dediprog_writemode {
Elyes HAOUASac01baa2018-05-28 16:52:21 +0200118 WRITE_MODE_PAGE_PGM = 1,
Simon Glass557eb4f2015-07-05 16:53:22 +0000119 WRITE_MODE_PAGE_WRITE = 2,
120 WRITE_MODE_1B_AAI = 3,
121 WRITE_MODE_2B_AAI = 4,
122 WRITE_MODE_128B_PAGE = 5,
123 WRITE_MODE_PAGE_AT26DF041 = 6,
124 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000125 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000126 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000127 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000128 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
129 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
130};
131
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000132enum dediprog_standalone_mode {
133 ENTER_STANDALONE_MODE = 0,
134 LEAVE_STANDALONE_MODE = 1,
135};
136
David Hendricksf73f8a72018-02-21 07:34:34 -0800137/*
Nico Huberc3b02dc2023-08-12 01:13:45 +0200138 * These are not official designations; they are for use in flashprog only.
David Hendricksf73f8a72018-02-21 07:34:34 -0800139 * Order must be preserved so that comparison operators work.
140 */
141enum protocol {
142 PROTOCOL_UNKNOWN,
143 PROTOCOL_V1,
144 PROTOCOL_V2,
145 PROTOCOL_V3,
146};
147
Thomas Heijligencc853d82021-05-04 15:32:17 +0200148static const struct dev_entry devs_dediprog[] = {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500149 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
Stefan Taunerfdec7472016-02-22 08:59:27 +0000150
151 {0},
152};
Simon Glass557eb4f2015-07-05 16:53:22 +0000153
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000154struct dediprog_data {
155 struct libusb_context *usb_ctx;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000156 libusb_device_handle *handle;
157 int in_endpoint;
158 int out_endpoint;
159 int firmwareversion;
Nico Huber274e6552024-03-24 12:34:57 +0100160 char devicestring[32+1];
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000161 enum dediprog_devtype devicetype;
Nico Huber008a44f2024-04-14 23:39:47 +0200162 int (*prepare_rw_cmd)(
163 struct flashctx *, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
164 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count);
Nico Hubera1b7f352024-03-25 18:32:11 +0100165 enum io_mode io_mode;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000166};
Simon Glass557eb4f2015-07-05 16:53:22 +0000167
Nico Huberd99a2bd2016-02-18 21:42:49 +0000168#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
169 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
170/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
171const char * LIBUSB_CALL libusb_error_name(int error_code)
172{
173 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
174 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
175 static char my_libusb_error[18 + 5 + 2];
176 sprintf(my_libusb_error, "libusb error code %i", error_code);
177 return my_libusb_error;
178 } else {
179 return "UNKNOWN";
180 }
181}
182#endif
183
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000184static enum protocol protocol(const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000185{
David Hendricksf73f8a72018-02-21 07:34:34 -0800186 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000187 switch (dp_data->devicetype) {
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000188 case DEV_SF100:
Jay Thompsoncabe3202018-08-17 14:30:04 -0500189 case DEV_SF200:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000190 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 5, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800191 return PROTOCOL_V1;
192 else
193 return PROTOCOL_V2;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000194 case DEV_SF600:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000195 if (dp_data->firmwareversion < FIRMWARE_VERSION(6, 9, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800196 return PROTOCOL_V1;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000197 else if (dp_data->firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
David Hendricksf73f8a72018-02-21 07:34:34 -0800198 return PROTOCOL_V2;
199 else
200 return PROTOCOL_V3;
Nico Huber00578222024-03-02 14:00:53 +0100201 case DEV_SF700:
202 case DEV_SF600PG2:
203 return PROTOCOL_V3;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000204 default:
David Hendricksf73f8a72018-02-21 07:34:34 -0800205 return PROTOCOL_UNKNOWN;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000206 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000207}
Simon Glassae616512016-01-23 23:27:58 +0000208
Nico Huberd99a2bd2016-02-18 21:42:49 +0000209struct dediprog_transfer_status {
Richard Hughes842d6782021-01-15 09:48:12 +0000210 struct flashctx *flash;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000211 int error; /* OK if 0, ERROR else */
212 unsigned int queued_idx;
213 unsigned int finished_idx;
214};
215
216static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
217{
218 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
219 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
220 status->error = 1;
221 msg_perr("SPI bulk read failed!\n");
222 }
Richard Hughes842d6782021-01-15 09:48:12 +0000223 flashprog_progress_add(status->flash, transfer->actual_length);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000224 ++status->finished_idx;
225}
226
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000227static int dediprog_bulk_read_poll(struct libusb_context *usb_ctx,
228 const struct dediprog_transfer_status *const status,
229 const int finish)
Nico Huberd99a2bd2016-02-18 21:42:49 +0000230{
231 if (status->finished_idx >= status->queued_idx)
232 return 0;
233
234 do {
235 struct timeval timeout = { 10, 0 };
236 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
237 if (ret < 0) {
238 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
239 return 1;
240 }
241 } while (finish && (status->finished_idx < status->queued_idx));
242 return 0;
243}
244
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000245static int dediprog_read(libusb_device_handle *dediprog_handle,
246 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
247 uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000248{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000249 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
250 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000251}
252
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000253static int dediprog_write(libusb_device_handle *dediprog_handle,
254 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
255 const uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000256{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000257 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
258 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000259}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000260
Nico Huberd99a2bd2016-02-18 21:42:49 +0000261
Simon Glass25e9f402015-06-28 13:31:19 +0000262/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000263static int dediprog_set_leds(int leds, const struct dediprog_data *dp_data)
Stefan Reinauer915b8402011-01-28 09:00:15 +0000264{
Simon Glass25e9f402015-06-28 13:31:19 +0000265 if (leds < LED_NONE || leds > LED_ALL)
266 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000267
Simon Glassae616512016-01-23 23:27:58 +0000268 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
269 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000270 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000271 * bit 0 == 0: red light is on.
272 *
273 * Additionally, the command structure has changed with the "new" protocol.
Stefan Reinauer915b8402011-01-28 09:00:15 +0000274 */
Nico Huberea27a6e2025-10-03 14:21:00 +0200275 int ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000276 if (protocol(dp_data) >= PROTOCOL_V2) {
Nico Huberea27a6e2025-10-03 14:21:00 +0200277 const unsigned int target_io_leds = (leds ^ 7) << 8 | 0x9;
278 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, target_io_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000279 } else {
Nico Huberea27a6e2025-10-03 14:21:00 +0200280 unsigned int target_leds;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000281 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Simon Glassae616512016-01-23 23:27:58 +0000282 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
283 } else {
284 target_leds = leds;
285 }
286 target_leds ^= 7;
287
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000288 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000289 }
290
Stefan Reinauer915b8402011-01-28 09:00:15 +0000291 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000292 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000293 return 1;
294 }
295
Stefan Reinauer915b8402011-01-28 09:00:15 +0000296 return 0;
297}
298
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000299static int dediprog_set_spi_voltage(libusb_device_handle *dediprog_handle, int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000300{
301 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000302 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000303
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000304 switch (millivolt) {
305 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000306 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000307 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000308 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000309 case 1800:
310 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000311 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000312 case 2500:
313 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000314 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000315 case 3500:
316 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000317 break;
318 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000319 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000320 return 1;
321 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000322 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
323 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000324
Nico Huber4099a8a2012-06-16 00:02:27 +0000325 if (voltage_selector == 0) {
326 /* Wait some time as the original driver does. */
327 programmer_delay(200 * 1000);
328 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000329 ret = dediprog_write(dediprog_handle, CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000330 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000331 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
332 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000333 return 1;
334 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000335 if (voltage_selector != 0) {
336 /* Wait some time as the original driver does. */
337 programmer_delay(200 * 1000);
338 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000339 return 0;
340}
341
Nico Huber77fa67d2013-02-20 18:03:36 +0000342struct dediprog_spispeeds {
343 const char *const name;
344 const int speed;
345};
346
347static const struct dediprog_spispeeds spispeeds[] = {
348 { "24M", 0x0 },
349 { "12M", 0x2 },
350 { "8M", 0x1 },
351 { "3M", 0x3 },
352 { "2.18M", 0x4 },
353 { "1.5M", 0x5 },
354 { "750k", 0x6 },
355 { "375k", 0x7 },
356 { NULL, 0x0 },
357};
358
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000359static int dediprog_set_spi_speed(unsigned int spispeed_idx, const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000360{
Nico Huber00578222024-03-02 14:00:53 +0100361 if (dp_data->devicetype < DEV_SF600PG2 && dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000362 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
363 return 0;
364 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000365
Simon Glass557eb4f2015-07-05 16:53:22 +0000366 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
367 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000368
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000369 int ret = dediprog_write(dp_data->handle, CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000370 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000371 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000372 return 1;
373 }
374 return 0;
375}
376
Nico Hubera1b7f352024-03-25 18:32:11 +0100377static int dediprog_set_io_mode(struct dediprog_data *const dp, const enum io_mode io_mode)
378{
379 const unsigned char dp_io_mode[] = {
380 [SINGLE_IO_1_1_1] = 0,
381 [DUAL_OUT_1_1_2] = 1,
382 [DUAL_IO_1_2_2] = 2,
383 [QUAD_OUT_1_1_4] = 3,
384 [QUAD_IO_1_4_4] = 4,
385 [QPI_4_4_4] = 5,
386 };
387
388 if (dp->devicetype < DEV_SF600)
389 return 0;
390
391 if (dp->io_mode == io_mode)
392 return 0;
393
394 if (io_mode >= ARRAY_SIZE(dp_io_mode)) {
395 msg_perr("%s: Unsupported I/O mode %d! "
396 "Please report a bug at flashprog@flashprog.org\n",
397 __func__, io_mode);
398 return 1;
399 }
400
401 const int ret = dediprog_write(dp->handle, CMD_IO_MODE, dp_io_mode[io_mode], 0, NULL, 0);
402 if (ret) {
403 msg_perr("Command I/O Mode 0x%x failed!\n", dp_io_mode[io_mode]);
404 return 1;
405 }
406
407 dp->io_mode = io_mode;
408 return 0;
409}
410
Nico Huber008a44f2024-04-14 23:39:47 +0200411static 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 +0200412{
Nico Hubera96aaa32024-03-05 12:56:13 +0100413 if (count > MAX_BLOCK_COUNT) {
Nico Huberac90af62022-12-18 00:22:47 +0000414 msg_perr("%s: Unsupported transfer length of %u blocks!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200415 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber477e1692019-06-18 23:56:01 +0200416 __func__, count);
Nico Huber008a44f2024-04-14 23:39:47 +0200417 return -1;
Nico Huber477e1692019-06-18 23:56:01 +0200418 }
419
Nico Huber008a44f2024-04-14 23:39:47 +0200420 /* First 5 bytes are common in all generations. */
421 cmd_buf[0] = count & 0xff;
422 cmd_buf[1] = (count >> 8) & 0xff;
423 cmd_buf[2] = 0; /* RFU */
424 cmd_buf[3] = dp_spi_cmd; /* Read/Write Mode (see enums dediprog_readmode / dediprog_writemode) */
425 cmd_buf[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and
426 WRITE_MODE_4B_ADDR_256B_PAGE_PGM. */
427 return 5;
428}
Simon Glassae616512016-01-23 23:27:58 +0000429
Nico Huber008a44f2024-04-14 23:39:47 +0200430static int prepare_rw_cmd_v1(
431 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
432 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
433{
434 const int cmd_len = prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count);
435 if (cmd_len < 0)
436 return -1;
Nico Huber93db6e12018-09-30 01:18:43 +0200437
Nico Huber008a44f2024-04-14 23:39:47 +0200438 if (flash->chip->feature_bits & FEATURE_4BA_EAR_ANY) {
439 if (spi_set_extended_address(flash, start >> 24))
440 return -1;
441 } else if (start >> 24) {
442 msg_cerr("Can't handle 4-byte address with dediprog.\n");
443 return -1;
444 }
445 /*
446 * We don't know how the dediprog firmware handles 4-byte
447 * addresses. So let's not tell it what we are doing and
448 * only send the lower 3 bytes.
449 */
450 *value = start & 0xffff;
451 *idx = (start >> 16) & 0xff;
452 return cmd_len;
453}
454
455static int prepare_rw_cmd_v2(
456 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
457 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
458{
Nico Hubera1b7f352024-03-25 18:32:11 +0100459 struct dediprog_data *const dp = flash->mst.spi->data;
460
Nico Huber008a44f2024-04-14 23:39:47 +0200461 if (prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count) < 0)
462 return -1;
463
Nico Hubera1b7f352024-03-25 18:32:11 +0100464 if (is_read) {
465 const struct spi_read_op *const read_op = get_spi_read_op(flash);
466 if (dediprog_set_io_mode(dp, read_op->io_mode))
467 return -1;
468
469 if (read_op->native_4ba)
470 cmd_buf[3] = READ_MODE_4B_ADDR_FAST_0x0C;
471 else if (read_op->opcode != JEDEC_READ)
472 cmd_buf[3] = READ_MODE_FAST;
473
474 if (read_op->opcode == JEDEC_READ_4BA)
475 cmd_buf[4] = JEDEC_FAST_READ_4BA;
476 else
477 cmd_buf[4] = read_op->opcode;
478 } else {
479 if (dediprog_set_io_mode(dp, SINGLE_IO_1_1_1))
480 return -1;
481
482 if (dp_spi_cmd == WRITE_MODE_PAGE_PGM
483 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
484 cmd_buf[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
485 cmd_buf[4] = JEDEC_BYTE_PROGRAM_4BA;
486 }
Nico Huber008a44f2024-04-14 23:39:47 +0200487 }
Nico Hubera1b7f352024-03-25 18:32:11 +0100488
Nico Huber008a44f2024-04-14 23:39:47 +0200489 cmd_buf[5] = 0; /* RFU */
490 cmd_buf[6] = (start >> 0) & 0xff;
491 cmd_buf[7] = (start >> 8) & 0xff;
492 cmd_buf[8] = (start >> 16) & 0xff;
493 cmd_buf[9] = (start >> 24) & 0xff;
Nico Hubera1b7f352024-03-25 18:32:11 +0100494
Nico Huber008a44f2024-04-14 23:39:47 +0200495 return 10;
496}
497
498static int prepare_rw_cmd_v3(
499 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
500 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
501{
Nico Hubera1b7f352024-03-25 18:32:11 +0100502 struct dediprog_data *const dp = flash->mst.spi->data;
503
Nico Huber008a44f2024-04-14 23:39:47 +0200504 if (prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count) < 0)
505 return -1;
506
507 cmd_buf[5] = 0; /* RFU */
508 cmd_buf[6] = (start >> 0) & 0xff;
509 cmd_buf[7] = (start >> 8) & 0xff;
510 cmd_buf[8] = (start >> 16) & 0xff;
511 cmd_buf[9] = (start >> 24) & 0xff;
512
513 if (is_read) {
Nico Hubera1b7f352024-03-25 18:32:11 +0100514 const struct spi_read_op *const read_op = get_spi_read_op(flash);
515 if (dediprog_set_io_mode(dp, read_op->io_mode))
516 return -1;
517
518 cmd_buf[3] = READ_MODE_CONFIGURABLE;
519 cmd_buf[4] = read_op->opcode;
520 cmd_buf[10] = read_op->native_4ba || flash->in_4ba_mode ? 4 : 3;
521 cmd_buf[11] = spi_dummy_cycles(read_op) / 2;
Nico Huber008a44f2024-04-14 23:39:47 +0200522 return 12;
Simon Glassae616512016-01-23 23:27:58 +0000523 } else {
Nico Hubera1b7f352024-03-25 18:32:11 +0100524 if (dediprog_set_io_mode(dp, SINGLE_IO_1_1_1))
525 return -1;
526
Nico Huberb1d2bae2024-07-12 16:10:15 +0200527 if (dp_spi_cmd == WRITE_MODE_PAGE_PGM) {
528 if (flash->chip->feature_bits & FEATURE_4BA_WRITE) {
529 cmd_buf[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM;
530 cmd_buf[4] = JEDEC_BYTE_PROGRAM_4BA;
531 } else if (flash->in_4ba_mode) {
532 cmd_buf[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM;
533 cmd_buf[4] = JEDEC_BYTE_PROGRAM;
534 } else if (flashprog_flash_getsize(flash) > 16*MiB) {
535 msg_cerr("Can't handle 4-byte address with dediprog.\n");
536 return -1;
537 }
Nico Huber7eb38aa2019-03-21 15:42:54 +0100538 }
Nico Huber008a44f2024-04-14 23:39:47 +0200539 /* 16 LSBs and 16 HSBs of page size */
540 /* FIXME: This assumes page size of 256. */
541 cmd_buf[10] = 0x00;
542 cmd_buf[11] = 0x01;
543 cmd_buf[12] = 0x00;
544 cmd_buf[13] = 0x00;
545 return 14;
Simon Glassae616512016-01-23 23:27:58 +0000546 }
547}
548
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000549/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
550 * @start start address
551 * @len length
552 * @return 0 on success, 1 on failure
553 */
Simon Glassae616512016-01-23 23:27:58 +0000554static 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 +0000555{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000556 int err = 1;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100557 const struct dediprog_data *dp_data = flash->mst.spi->data;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000558
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000559 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000560 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000561 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000562
Richard Hughes842d6782021-01-15 09:48:12 +0000563 struct dediprog_transfer_status status = { flash, 0, 0, 0 };
Nico Huberd99a2bd2016-02-18 21:42:49 +0000564 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
565 struct libusb_transfer *transfer;
566
Nico Huberbbaa1712018-12-05 13:26:20 +0100567 if (len == 0)
568 return 0;
569
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000570 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000571 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200572 "Please report a bug at flashprog@flashprog.org\n",
Simon Glassae616512016-01-23 23:27:58 +0000573 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000574 return 1;
575 }
576
Nico Huber008a44f2024-04-14 23:39:47 +0200577 uint16_t value = 0, idx = 0;
578 uint8_t data_packet[MAX_CMD_SIZE];
579 const int command_packet_size = dp_data->prepare_rw_cmd(
580 flash, data_packet, &value, &idx, /* is_read => */true, READ_MODE_STD, start, count);
581 if (command_packet_size < 0)
Nico Huber7eb38aa2019-03-21 15:42:54 +0100582 return 1;
Simon Glassae616512016-01-23 23:27:58 +0000583
Nico Huber008a44f2024-04-14 23:39:47 +0200584 int ret = dediprog_write(dp_data->handle, CMD_READ, value, idx, data_packet, command_packet_size);
585 if (ret != command_packet_size) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000586 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000587 return 1;
588 }
589
Nico Huberd99a2bd2016-02-18 21:42:49 +0000590 /*
591 * Ring buffer of bulk transfers.
592 * Poll until at least one transfer is ready,
593 * schedule next transfers until buffer is full.
594 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000595
Nico Huberd99a2bd2016-02-18 21:42:49 +0000596 /* Allocate bulk transfers. */
597 unsigned int i;
Nico Huber519be662018-12-23 20:03:35 +0100598 for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000599 transfers[i] = libusb_alloc_transfer(0);
600 if (!transfers[i]) {
601 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
602 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200603 }
604 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000605
606 /* Now transfer requested chunks using libusb's asynchronous interface. */
607 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200608 while ((status.queued_idx < count) &&
609 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
610 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000611 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
Nico Huber821a0852024-03-24 13:34:51 +0100612 libusb_fill_bulk_transfer(transfer, dp_data->handle, dp_data->in_endpoint,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000613 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
614 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
615 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
616 ret = libusb_submit_transfer(transfer);
617 if (ret < 0) {
618 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
619 status.queued_idx, libusb_error_name(ret));
620 goto err_free;
621 }
622 ++status.queued_idx;
623 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000624 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 0))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000625 goto err_free;
626 }
627 /* Wait for transfers to finish. */
Rick Altherr4f9cd8b2021-12-13 17:10:00 -0800628 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000629 goto err_free;
630 /* Check if everything has been transmitted. */
631 if ((status.finished_idx < count) || status.error)
632 goto err_free;
633
634 err = 0;
635
636err_free:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000637 dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000638 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
639 if (transfers[i]) libusb_free_transfer(transfers[i]);
640 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000641}
642
Nico Hubera1b7f352024-03-25 18:32:11 +0100643static int dediprog_slow_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
644{
645 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", start, len);
646
647 /* Override fast-read function for a moment: */
Nico Huber1b1deda2024-04-18 00:35:48 +0200648 struct spi_read_op *const backup = flash->spi_fast_read;
Nico Hubera1b7f352024-03-25 18:32:11 +0100649 flash->spi_fast_read = NULL;
650
651 const int ret = default_spi_read(flash, buf, start, len);
652
653 flash->spi_fast_read = backup;
654 return ret;
655}
656
Simon Glassae616512016-01-23 23:27:58 +0000657static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000658{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000659 int ret;
660 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000661 const unsigned int chunksize = 0x200;
Nico Huberbbaa1712018-12-05 13:26:20 +0100662 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000663 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100664 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000665
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000666 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000667
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000668 if (residue) {
Nico Hubera1b7f352024-03-25 18:32:11 +0100669 ret = dediprog_slow_read(flash, buf, start, residue);
Simon Glass25e9f402015-06-28 13:31:19 +0000670 if (ret)
671 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000672 }
673
674 /* Round down. */
675 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000676 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000677 if (ret)
678 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000679
680 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000681 if (len != 0) {
Nico Hubera1b7f352024-03-25 18:32:11 +0100682 ret = dediprog_slow_read(flash, buf + residue + bulklen,
683 start + residue + bulklen, len);
Simon Glass25e9f402015-06-28 13:31:19 +0000684 if (ret)
685 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000686 }
687
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000688 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000689 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000690err:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000691 dediprog_set_leds(LED_ERROR, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +0000692 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000693}
694
Nico Hubera4b14f72012-06-19 12:06:53 +0000695/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
696 * @chunksize length of data chunks, only 256 supported by now
697 * @start start address
698 * @len length
699 * @dedi_spi_cmd dediprog specific write command for spi bus
700 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000701 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000702static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000703 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000704{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000705 /* USB transfer size must be 512, other sizes will NOT work at all.
706 * chunksize is the real data size per USB bulk transfer. The remaining
707 * space in a USB bulk transfer must be filled with 0xff padding.
708 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000709 const unsigned int count = len / chunksize;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100710 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000711
Nico Hubera4b14f72012-06-19 12:06:53 +0000712 /*
713 * We should change this check to
714 * chunksize > 512
715 * once we know how to handle different chunk sizes.
716 */
717 if (chunksize != 256) {
718 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200719 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000720 __func__, chunksize);
Nico Hubera4b14f72012-06-19 12:06:53 +0000721 return 1;
722 }
723
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000724 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000725 msg_perr("%s: Unaligned start=%i, len=%i!\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__, start, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000728 return 1;
729 }
730
731 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000732 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000733 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000734
Nico Huber008a44f2024-04-14 23:39:47 +0200735 uint16_t value = 0, idx = 0;
736 uint8_t data_packet[MAX_CMD_SIZE];
737 const int command_packet_size = dp_data->prepare_rw_cmd(
738 flash, data_packet, &value, &idx, /* is_read => */false, dedi_spi_cmd, start, count);
739 if (command_packet_size < 0)
David Hendricksf73f8a72018-02-21 07:34:34 -0800740 return 1;
David Hendricksf73f8a72018-02-21 07:34:34 -0800741
Nico Huber008a44f2024-04-14 23:39:47 +0200742 int ret = dediprog_write(dp_data->handle, CMD_WRITE, value, idx, data_packet, command_packet_size);
743 if (ret != command_packet_size) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000744 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000745 return 1;
746 }
747
Simon Glassae616512016-01-23 23:27:58 +0000748 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000749 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000750 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000751 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000752 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000753 int transferred;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000754 ret = libusb_bulk_transfer(dp_data->handle, dp_data->out_endpoint, usbbuf, 512, &transferred,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000755 DEFAULT_TIMEOUT);
756 if ((ret < 0) || (transferred != 512)) {
757 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000758 return 1;
759 }
Richard Hughes842d6782021-01-15 09:48:12 +0000760 flashprog_progress_add(flash, chunksize);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000761 }
762
763 return 0;
764}
765
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000766static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000767 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000768{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000769 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000770 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000771 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
772 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100773 const struct dediprog_data *dp_data = flash->mst.spi->data;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000774
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000775 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000776
Nico Hubera4b14f72012-06-19 12:06:53 +0000777 if (chunksize != 256) {
778 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
779 "we don't know how dediprog\nhandles them.\n");
780 /* Write everything like it was residue. */
781 residue = len;
782 }
783
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000784 if (residue) {
785 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
786 start, residue);
Nico Huber93db6e12018-09-30 01:18:43 +0200787 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
Nico Hubere36e3dc2023-04-28 21:23:08 +0000788 ret = default_spi_write_256(flash, buf, start, residue);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000789 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000790 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000791 return ret;
792 }
793 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000794
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000795 /* Round down. */
796 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000797 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000798 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000799 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000800 return ret;
801 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000802
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000803 len -= residue + bulklen;
804 if (len) {
805 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
806 start, len);
Nico Hubere36e3dc2023-04-28 21:23:08 +0000807 ret = default_spi_write_256(flash, buf + residue + bulklen, start + residue + bulklen, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000808 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000809 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000810 return ret;
811 }
812 }
813
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000814 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000815 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000816}
817
Nico Hubera96aaa32024-03-05 12:56:13 +0100818static int dediprog_spi_write_chunked(struct flashctx *flash, const uint8_t *buf,
819 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
820{
821 /* We can write only up to 65535 pages at once: */
822 while (len) {
823 const size_t len_here = MIN(len, flash->chip->page_size * MAX_BLOCK_COUNT);
824 const int ret = dediprog_spi_write(flash, buf, start, len_here, dedi_spi_cmd);
825 if (ret)
826 return ret;
827
828 start += len_here;
829 buf += len_here;
830 len -= len_here;
831 }
832 return 0;
833}
834
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000835static 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 +0000836{
Nico Hubera96aaa32024-03-05 12:56:13 +0100837 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000838}
839
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000840static 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 +0000841{
Nico Hubera96aaa32024-03-05 12:56:13 +0100842 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000843}
844
Nico Huber610c1aa2023-02-15 02:56:05 +0100845static int dediprog_spi_send_command(const struct spi_master *mst,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000846 unsigned int writecnt,
847 unsigned int readcnt,
848 const unsigned char *writearr,
849 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000850{
Nico Huber610c1aa2023-02-15 02:56:05 +0100851 struct dediprog_data *dp_data = mst->data;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000852 int ret;
853
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000854 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Nico Huber610c1aa2023-02-15 02:56:05 +0100855 if (writecnt > mst->max_data_write + 5) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000856 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000857 return 1;
858 }
Nico Huber610c1aa2023-02-15 02:56:05 +0100859 if (readcnt > mst->max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000860 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000861 return 1;
862 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100863
Nico Hubera1b7f352024-03-25 18:32:11 +0100864 if (dediprog_set_io_mode(dp_data, SINGLE_IO_1_1_1))
865 return 1;
866
Simon Glassae616512016-01-23 23:27:58 +0000867 unsigned int idx, value;
868 /* New protocol has options and timeout combined as value while the old one used the value field for
869 * timeout and the index field for options. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000870 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000871 idx = 0;
872 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
873 } else {
874 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
875 value = 0;
876 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000877 ret = dediprog_write(dp_data->handle, CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Nico Huber519be662018-12-23 20:03:35 +0100878 if (ret != (int)writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000879 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000880 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000881 return 1;
882 }
Simon Glassae616512016-01-23 23:27:58 +0000883 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000884 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000885
Stefan Tauner74367bf2016-02-20 20:53:46 +0000886 /* The specifications do state the possibility to set a timeout for transceive transactions.
887 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
888 * can predict the time needed by the previous command or so (untested). In any case, using this
889 * "feature" to set sane-looking timouts for the read below will completely trash performance with
890 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
891 *
892 * The specification also uses only 0 in its examples, so the lesson to learn here:
893 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000894 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800895 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000896 idx = 0;
897 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
898 } else {
899 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
900 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
901 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000902 ret = dediprog_read(dp_data->dediprog_handle, CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000903 */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000904 ret = dediprog_read(dp_data->handle, CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Nico Huber519be662018-12-23 20:03:35 +0100905 if (ret != (int)readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000906 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 +0000907 return 1;
908 }
909 return 0;
910}
911
Nico Huber823a7042024-03-24 18:32:58 +0100912static int dediprog_read_devicestring(struct dediprog_data *dp_data, bool warn)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000913{
Nico Huber274e6552024-03-24 12:34:57 +0100914 const int devstr_len = sizeof(dp_data->devicestring) - 1, old_devstr_len = 16;
915 char *const buf = dp_data->devicestring;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000916 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000917
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000918 /* Command Receive Device String. */
Nico Hubere76e21f2024-02-29 23:33:35 +0100919 ret = dediprog_read(dp_data->handle, CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, devstr_len);
Nico Huber5262e292024-03-02 13:21:25 +0100920 if (ret < old_devstr_len || ret > devstr_len) {
Nico Huber823a7042024-03-24 18:32:58 +0100921 if (warn)
922 msg_perr("Incomplete/failed Command Receive Device String!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000923 return 1;
924 }
Nico Huber5262e292024-03-02 13:21:25 +0100925 buf[ret] = '\0';
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000926 msg_pdbg("Found a %s\n", buf);
Nico Hubere76e21f2024-02-29 23:33:35 +0100927 if (memcmp(buf, "SF100", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000928 dp_data->devicetype = DEV_SF100;
Nico Hubere76e21f2024-02-29 23:33:35 +0100929 else if (memcmp(buf, "SF200", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000930 dp_data->devicetype = DEV_SF200;
Nico Huber00578222024-03-02 14:00:53 +0100931 else if (memcmp(buf, "SF600PG2", 8) == 0) /* match first, before shorter, generic SF600 */
932 dp_data->devicetype = DEV_SF600PG2;
Nico Hubere76e21f2024-02-29 23:33:35 +0100933 else if (memcmp(buf, "SF600", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000934 dp_data->devicetype = DEV_SF600;
Nico Huber00578222024-03-02 14:00:53 +0100935 else if (memcmp(buf, "SF700", 5) == 0)
936 dp_data->devicetype = DEV_SF700;
Nico Huber274e6552024-03-24 12:34:57 +0100937 else
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000938 return 1;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000939
Nico Huber274e6552024-03-24 12:34:57 +0100940 return 0;
941}
942
943static int dediprog_check_devicestring(struct dediprog_data *dp_data)
944{
945 char *const buf = dp_data->devicestring;
Nico Huberbdef5c22024-03-02 13:51:39 +0100946 unsigned int sfnum;
947 unsigned int fw[3];
Nico Huber274e6552024-03-24 12:34:57 +0100948
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100949 if (sscanf(buf, "SF%u", &sfnum) != 1 ||
Nico Huber00578222024-03-02 14:00:53 +0100950 sfnum != dp_data->devicetype / 100 * 100 ||
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100951 sscanf(buf, "SF%*s V:%u.%u.%u ", &fw[0], &fw[1], &fw[2]) != 3) {
Simon Glassae616512016-01-23 23:27:58 +0000952 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000953 return 1;
954 }
Nico Huber00578222024-03-02 14:00:53 +0100955
956 /* Only allow major versions that were tested. */
957 if ((dp_data->devicetype == DEV_SF600PG2 && fw[0] > 1) ||
958 (dp_data->devicetype == DEV_SF700 && fw[0] != 4) ||
959 (dp_data->devicetype <= DEV_SF600 && (fw[0] < 2 || fw[0] > 7))) {
Simon Glassae616512016-01-23 23:27:58 +0000960 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000961 return 1;
962 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800963
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000964 dp_data->firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000965 if (protocol(dp_data) == PROTOCOL_UNKNOWN) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800966 msg_perr("Internal error: Unable to determine protocol version.\n");
967 return 1;
968 }
Simon Glassae616512016-01-23 23:27:58 +0000969
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000970 return 0;
971}
972
David Hendricksc05900f2016-02-18 21:42:41 +0000973/*
Ryan O'Leary301ae222019-06-24 19:14:33 -0700974 * Read the id from the dediprog. This should return the numeric part of the
975 * serial number found on a sticker on the back of the dediprog. Note this
Nico Huber821a0852024-03-24 13:34:51 +0100976 * number is stored in writable eeprom, so it could get out of sync.
Ryan O'Leary301ae222019-06-24 19:14:33 -0700977 * @return the id on success, -1 on failure
978 */
Nico Huber821a0852024-03-24 13:34:51 +0100979static int dediprog_read_id(struct dediprog_data *const dp)
Ryan O'Leary301ae222019-06-24 19:14:33 -0700980{
Nico Huber821a0852024-03-24 13:34:51 +0100981 const int min_len = 3;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700982 int ret;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700983
Nico Huber821a0852024-03-24 13:34:51 +0100984 if (dp->devicetype >= DEV_SF600PG2) {
985 const uint8_t out[6] = { 0x00, 0x00, 0x00, 0x02, 0x00, 0x00 };
986 const uint8_t cmd = 0x71;
987 uint8_t buf[512];
988 int transferred;
989 int try;
990
991 /* Always query the id twice as the endpoint
992 can lock up in mysterious ways otherwise. */
993 for (try = 0; try < 2; ++try) {
994 ret = dediprog_write(dp->handle, cmd, 0, 0, out, sizeof(out));
995 if (ret != (int)sizeof(out))
996 goto failed_ret;
997
998 ret = libusb_bulk_transfer(dp->handle, dp->in_endpoint,
999 buf, sizeof(buf), &transferred, DEFAULT_TIMEOUT);
1000 }
1001
1002 if (ret == 0 && transferred >= min_len)
1003 return buf[2] << 16 | buf[1] << 8 | buf[0];
1004 } else {
1005 uint8_t buf[16];
1006
1007 if (dp->devicetype >= DEV_SF600) {
1008 ret = dediprog_read(dp->handle, CMD_READ_EEPROM, 0, 0, buf, sizeof(buf));
1009 } else {
1010 ret = libusb_control_transfer(dp->handle, REQTYPE_OTHER_IN,
1011 0x7, /* request */
1012 0, /* value */
1013 0xEF00, /* index */
1014 buf, min_len, DEFAULT_TIMEOUT);
1015 }
1016
1017 if (ret >= min_len)
1018 return buf[0] << 16 | buf[1] << 8 | buf[2];
Ryan O'Leary301ae222019-06-24 19:14:33 -07001019 }
1020
Nico Huber821a0852024-03-24 13:34:51 +01001021failed_ret:
1022 msg_perr("Failed to read dediprog id: ");
1023 if (ret < 0)
1024 msg_perr("%s (%d)\n", libusb_strerror(ret), ret);
1025 else
1026 msg_perr("short read!\n");
1027 return -1;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001028}
1029
1030/*
David Hendricksc05900f2016-02-18 21:42:41 +00001031 * This command presumably sets the voltage for the SF100 itself (not the
1032 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
1033 * (including all SF600s) do not support it.
1034 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001035
David Hendricksc05900f2016-02-18 21:42:41 +00001036/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
1037 * Only use dediprog_set_voltage on SF100 programmers with firmware older
1038 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001039static int dediprog_set_voltage(libusb_device_handle *dediprog_handle)
David Hendricksc05900f2016-02-18 21:42:41 +00001040{
Nico Huberd99a2bd2016-02-18 21:42:49 +00001041 unsigned char buf[1] = {0};
1042 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +00001043 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +00001044 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001045 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +00001046 return 1;
1047 }
David Hendricksc05900f2016-02-18 21:42:41 +00001048 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +00001049 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001050 return 1;
1051 }
David Hendricksc05900f2016-02-18 21:42:41 +00001052
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001053 return 0;
1054}
1055
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001056static int dediprog_standalone_mode(const struct dediprog_data *dp_data)
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001057{
1058 int ret;
1059
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001060 if (dp_data->devicetype != DEV_SF600)
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001061 return 0;
1062
1063 msg_pdbg2("Disabling standalone mode.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001064 ret = dediprog_write(dp_data->handle, CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001065 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001066 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001067 return 1;
1068 }
1069
1070 return 0;
1071}
1072
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001073#if 0
1074/* Something.
1075 * Present in eng_detect_blink.log with firmware 3.1.8
1076 * Always preceded by Command Receive Device String
1077 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001078static int dediprog_command_b(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001079{
1080 int ret;
1081 char buf[0x3];
1082
Simon Glass557eb4f2015-07-05 16:53:22 +00001083 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
1084 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001085 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001086 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001087 return 1;
1088 }
1089 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
1090 (buf[2] != 0xff)) {
1091 msg_perr("Unexpected response to Command B!\n");
1092 return 1;
1093 }
1094
1095 return 0;
1096}
1097#endif
1098
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001099static int set_target_flash(libusb_device_handle *dediprog_handle, enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001100{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001101 int ret = dediprog_write(dediprog_handle, CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +00001102 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001103 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001104 return 1;
1105 }
1106 return 0;
1107}
1108
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001109#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +00001110/* Returns true if the button is currently pressed. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001111static bool dediprog_get_button(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001112{
Simon Glass557eb4f2015-07-05 16:53:22 +00001113 char buf[1];
1114 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
1115 buf, 0x1, DEFAULT_TIMEOUT);
1116 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001117 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001118 return 1;
1119 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001120 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001121}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001122#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001123
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001124static int parse_voltage(char *voltage)
1125{
1126 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001127 int i;
1128 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001129
1130 if (!voltage || !strlen(voltage)) {
1131 msg_perr("Empty voltage= specified.\n");
1132 return -1;
1133 }
1134 millivolt = (int)strtol(voltage, &tmp, 0);
1135 voltage = tmp;
1136 /* Handle "," and "." as decimal point. Everything after it is assumed
1137 * to be in decimal notation.
1138 */
1139 if ((*voltage == '.') || (*voltage == ',')) {
1140 voltage++;
1141 for (i = 0; i < 3; i++) {
1142 fraction *= 10;
1143 /* Don't advance if the current character is invalid,
1144 * but continue multiplying.
1145 */
1146 if ((*voltage < '0') || (*voltage > '9'))
1147 continue;
1148 fraction += *voltage - '0';
1149 voltage++;
1150 }
1151 /* Throw away remaining digits. */
1152 voltage += strspn(voltage, "0123456789");
1153 }
1154 /* The remaining string must be empty or "mV" or "V". */
1155 tolower_string(voltage);
1156
1157 /* No unit or "V". */
1158 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1159 millivolt *= 1000;
1160 millivolt += fraction;
1161 } else if (!strncmp(voltage, "mv", 2) ||
1162 !strncmp(voltage, "milliv", 6)) {
1163 /* No adjustment. fraction is discarded. */
1164 } else {
1165 /* Garbage at the end of the string. */
1166 msg_perr("Garbage voltage= specified.\n");
1167 return -1;
1168 }
1169 return millivolt;
1170}
1171
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001172static int dediprog_shutdown(void *data);
1173
Nico Huber93db6e12018-09-30 01:18:43 +02001174static struct spi_master spi_master_dediprog = {
Nico Huberdc5af542018-12-22 16:54:59 +01001175 .features = SPI_MASTER_NO_4BA_MODES,
Simon Glassae616512016-01-23 23:27:58 +00001176 .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 +00001177 .max_data_write = 16 - 5, /* Account for 5 bytes cmd/addr. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001178 .command = dediprog_spi_send_command,
1179 .multicommand = default_spi_send_multicommand,
1180 .read = dediprog_spi_read,
1181 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +00001182 .write_aai = dediprog_spi_write_aai,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001183 .shutdown = dediprog_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301184 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001185};
1186
Arthur Heymansdb236b02026-06-21 14:16:28 +02001187static int dediprog_find_bulk_endpoints(struct dediprog_data *dp_data)
1188{
1189 struct libusb_config_descriptor *config;
1190 const struct libusb_interface_descriptor *interface = NULL;
1191 int in_endpoint = 0;
1192 int out_endpoint = 0;
1193 int ret;
1194 int i;
1195
1196 ret = libusb_get_active_config_descriptor(libusb_get_device(dp_data->handle), &config);
1197 if (ret != 0) {
1198 msg_perr("Could not read USB configuration descriptor: %i %s\n",
1199 ret, libusb_error_name(ret));
1200 return 1;
1201 }
1202
1203 for (i = 0; i < config->bNumInterfaces; i++) {
1204 const struct libusb_interface *const iface = &config->interface[i];
1205 if (iface->num_altsetting > 0 && iface->altsetting[0].bInterfaceNumber == DEDIPROG_INTERFACE) {
1206 interface = &iface->altsetting[0];
1207 break;
1208 }
1209 }
1210
1211 if (!interface) {
1212 msg_perr("Could not find USB interface %u in configuration descriptor.\n", DEDIPROG_INTERFACE);
1213 libusb_free_config_descriptor(config);
1214 return 1;
1215 }
1216
1217 for (i = 0; i < interface->bNumEndpoints; i++) {
1218 const struct libusb_endpoint_descriptor *const endpoint = &interface->endpoint[i];
1219 const uint8_t type = endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK;
1220 const uint8_t address = endpoint->bEndpointAddress;
1221
1222 if (type != LIBUSB_TRANSFER_TYPE_BULK)
1223 continue;
1224
1225 if ((address & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) {
1226 if (!in_endpoint)
1227 in_endpoint = address;
1228 } else if (!out_endpoint) {
1229 out_endpoint = address;
1230 }
1231 }
1232
1233 libusb_free_config_descriptor(config);
1234
1235 if (!in_endpoint || !out_endpoint) {
1236 msg_perr("Could not find required bulk endpoints (in=0x%02x, out=0x%02x).\n",
1237 in_endpoint, out_endpoint);
1238 return 1;
1239 }
1240
1241 dp_data->in_endpoint = in_endpoint;
1242 dp_data->out_endpoint = out_endpoint;
1243
1244 msg_pdbg("Using bulk endpoints in=0x%02x, out=0x%02x.\n",
1245 dp_data->in_endpoint, dp_data->out_endpoint);
1246 return 0;
1247}
1248
Ryan O'Leary301ae222019-06-24 19:14:33 -07001249/*
1250 * Open a dediprog_handle with the USB device at the given index.
1251 * @index index of the USB device
Nico Huber274e6552024-03-24 12:34:57 +01001252 * @return 0 for success, -1 for error, -2 for busy device, -3 for unknown device
Ryan O'Leary301ae222019-06-24 19:14:33 -07001253 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001254static int dediprog_open(int index, struct dediprog_data *dp_data)
Ryan O'Leary301ae222019-06-24 19:14:33 -07001255{
1256 const uint16_t vid = devs_dediprog[0].vendor_id;
1257 const uint16_t pid = devs_dediprog[0].device_id;
1258 int ret;
1259
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001260 dp_data->handle = usb_dev_get_by_vid_pid_number(dp_data->usb_ctx, vid, pid, (unsigned int) index);
1261 if (!dp_data->handle) {
Ryan O'Leary301ae222019-06-24 19:14:33 -07001262 msg_perr("Could not find a Dediprog programmer on USB.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001263 libusb_exit(dp_data->usb_ctx);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001264 return -1;
1265 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001266 ret = libusb_set_configuration(dp_data->handle, 1);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001267 if (ret != 0) {
1268 msg_perr("Could not set USB device configuration: %i %s\n",
1269 ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001270 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001271 return -2;
1272 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001273 ret = libusb_claim_interface(dp_data->handle, 0);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001274 if (ret < 0) {
1275 msg_perr("Could not claim USB device interface %i: %i %s\n",
1276 0, ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001277 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001278 return -2;
1279 }
Nico Huber274e6552024-03-24 12:34:57 +01001280
1281 /* Try reading the devicestring. If that fails and the device is old
1282 (FW < 6.0.0, which we cannot know), then we need to try the "set
1283 voltage" command and then attempt to read the devicestring again. */
Nico Huber823a7042024-03-24 18:32:58 +01001284 if (dediprog_read_devicestring(dp_data, /* warn => */false)) {
Nico Huber274e6552024-03-24 12:34:57 +01001285 if (dediprog_set_voltage(dp_data->handle))
1286 goto unknown_dev;
Nico Huber823a7042024-03-24 18:32:58 +01001287 if (dediprog_read_devicestring(dp_data, /* warn => */true))
Nico Huber274e6552024-03-24 12:34:57 +01001288 goto unknown_dev;
1289 }
Nico Huber821a0852024-03-24 13:34:51 +01001290
Arthur Heymansdb236b02026-06-21 14:16:28 +02001291 if (dediprog_find_bulk_endpoints(dp_data))
1292 goto unknown_dev;
Nico Huber821a0852024-03-24 13:34:51 +01001293
Ryan O'Leary301ae222019-06-24 19:14:33 -07001294 return 0;
Nico Huber274e6552024-03-24 12:34:57 +01001295
1296unknown_dev:
1297 msg_pwarn("Ignoring unknown Dediprog device. Not a SF100, SF200, SF600(Plus(G2)), or SF700!\n");
1298 libusb_release_interface(dp_data->handle, 0);
1299 libusb_close(dp_data->handle);
1300 return -3;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001301}
1302
David Hendricks8bb20212011-06-14 01:35:36 +00001303static int dediprog_shutdown(void *data)
1304{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001305 int ret = 0;
1306 struct dediprog_data *dp_data = data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001307
Nico Hubera1b7f352024-03-25 18:32:11 +01001308 dediprog_set_io_mode(dp_data, SINGLE_IO_1_1_1);
1309
David Hendricks8bb20212011-06-14 01:35:36 +00001310 /* URB 28. Command Set SPI Voltage to 0. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001311 if (dediprog_set_spi_voltage(dp_data->handle, 0x0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001312 ret = 1;
1313 goto out;
David Hendricks8bb20212011-06-14 01:35:36 +00001314 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001315
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001316 if (libusb_release_interface(dp_data->handle, 0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001317 msg_perr("Could not release USB interface!\n");
1318 ret = 1;
1319 goto out;
1320 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001321 libusb_close(dp_data->handle);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001322 libusb_exit(dp_data->usb_ctx);
1323out:
1324 free(data);
1325 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +00001326}
1327
Nico Hubere3a26882023-01-11 21:45:51 +01001328static int dediprog_init(struct flashprog_programmer *const prog)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001329{
Ryan O'Leary301ae222019-06-24 19:14:33 -07001330 char *voltage, *id_str, *device, *spispeed, *target_str;
Nico Hubera1b7f352024-03-25 18:32:11 +01001331 enum {
1332 DEFAULT,
1333 SINGLE,
1334 DUAL,
1335 QUAD,
1336 } io_mode = DEFAULT;
Patrick Georgiefe2d432013-05-23 21:47:46 +00001337 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001338 int millivolt = 3500;
Nico Hubere8463c82024-03-24 16:50:50 +01001339 long id = -1; /* -1 defaults to enumeration order */
Ryan O'Leary301ae222019-06-24 19:14:33 -07001340 int found_id;
Nathan Laredo21541a62012-12-24 22:07:36 +00001341 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +02001342 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +00001343 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001344
Nico Hubera1b7f352024-03-25 18:32:11 +01001345 char *const io_mode_str = extract_programmer_param("iomode");
1346 if (io_mode_str) {
1347 if (strcmp(io_mode_str, "single") == 0) {
1348 io_mode = SINGLE;
1349 } else if (strcmp(io_mode_str, "dual") == 0) {
1350 io_mode = DUAL;
1351 } else if (strcmp(io_mode_str, "quad") == 0) {
1352 io_mode = QUAD;
1353 } else {
1354 msg_perr("Invalid iomode setting: %s\n", io_mode_str);
1355 return SPI_GENERIC_ERROR;
1356 }
1357 }
1358 free(io_mode_str);
1359
Nico Huber77fa67d2013-02-20 18:03:36 +00001360 spispeed = extract_programmer_param("spispeed");
1361 if (spispeed) {
1362 for (i = 0; spispeeds[i].name; ++i) {
1363 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1364 spispeed_idx = i;
1365 break;
1366 }
1367 }
1368 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +00001369 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +00001370 free(spispeed);
1371 return 1;
1372 }
1373 free(spispeed);
1374 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001375
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001376 voltage = extract_programmer_param("voltage");
1377 if (voltage) {
1378 millivolt = parse_voltage(voltage);
1379 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001380 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001381 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001382 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1383 }
1384
Ryan O'Leary301ae222019-06-24 19:14:33 -07001385 id_str = extract_programmer_param("id");
1386 if (id_str) {
Nico Hubere8463c82024-03-24 16:50:50 +01001387 const char prefixes[][4] = { "SF", "DP", "S6B", };
1388 for (i = 0; i < (int)ARRAY_SIZE(prefixes); ++i) {
1389 if (!strncmp(id_str, prefixes[i], strlen(prefixes[i])))
1390 break;
1391 }
1392 if (i >= (int)ARRAY_SIZE(prefixes)) {
1393 msg_perr("Error: Could not parse dediprog `id'.\n");
1394 msg_perr("Expected a string prefixed with any of ");
1395 for (i = 0; i < (int)ARRAY_SIZE(prefixes); ++i)
1396 msg_perr("%s`%s'", i > 0 ? ", " : "", prefixes[i]);
1397 msg_perr(".\n");
1398 free(id_str);
1399 return 1;
1400 }
1401 char *endptr;
1402 id = strtol(id_str + strlen(prefixes[i]), &endptr, 10);
1403 if (strlen(id_str) <= strlen(prefixes[i]) || *endptr) {
1404 msg_perr("Error: Could not parse dediprog `id'.\n");
1405 msg_perr("Expected a number after string prefix.\n");
Ryan O'Leary301ae222019-06-24 19:14:33 -07001406 free(id_str);
1407 return 1;
1408 }
1409 if (id < 0 || id >= 0x1000000) {
Nico Hubere8463c82024-03-24 16:50:50 +01001410 msg_perr("Error: id `%s' is out of range!\n", id_str);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001411 free(id_str);
1412 return 1;
1413 }
1414 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1415 }
1416 free(id_str);
1417
Nathan Laredo21541a62012-12-24 22:07:36 +00001418 device = extract_programmer_param("device");
1419 if (device) {
1420 char *dev_suffix;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001421 if (id != -1) {
1422 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1423 }
Nathan Laredo21541a62012-12-24 22:07:36 +00001424 errno = 0;
1425 usedevice = strtol(device, &dev_suffix, 10);
1426 if (errno != 0 || device == dev_suffix) {
1427 msg_perr("Error: Could not convert 'device'.\n");
1428 free(device);
1429 return 1;
1430 }
Nico Huber961f4a12019-10-04 17:34:22 +02001431 if (usedevice < 0 || usedevice > INT_MAX) {
Nathan Laredo21541a62012-12-24 22:07:36 +00001432 msg_perr("Error: Value for 'device' is out of range.\n");
1433 free(device);
1434 return 1;
1435 }
1436 if (strlen(dev_suffix) > 0) {
1437 msg_perr("Error: Garbage following 'device' value.\n");
1438 free(device);
1439 return 1;
1440 }
1441 msg_pinfo("Using device %li.\n", usedevice);
1442 }
1443 free(device);
1444
Stefan Taunere659d2d2013-05-03 21:58:28 +00001445 target_str = extract_programmer_param("target");
1446 if (target_str) {
1447 char *target_suffix;
1448 errno = 0;
1449 target = strtol(target_str, &target_suffix, 10);
1450 if (errno != 0 || target_str == target_suffix) {
1451 msg_perr("Error: Could not convert 'target'.\n");
1452 free(target_str);
1453 return 1;
1454 }
1455 if (target < 1 || target > 2) {
1456 msg_perr("Error: Value for 'target' is out of range.\n");
1457 free(target_str);
1458 return 1;
1459 }
1460 if (strlen(target_suffix) > 0) {
1461 msg_perr("Error: Garbage following 'target' value.\n");
1462 free(target_str);
1463 return 1;
1464 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001465 switch (target) {
1466 case 1:
1467 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1468 target = FLASH_TYPE_APPLICATION_FLASH_1;
1469 break;
1470 case 2:
1471 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1472 target = FLASH_TYPE_APPLICATION_FLASH_2;
1473 break;
1474 default:
1475 break;
1476 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001477 }
1478 free(target_str);
1479
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001480 struct dediprog_data *dp_data = calloc(1, sizeof(*dp_data));
1481 if (!dp_data) {
1482 msg_perr("Unable to allocate space for SPI master data\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001483 return 1;
1484 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001485 dp_data->firmwareversion = FIRMWARE_VERSION(0, 0, 0);
1486 dp_data->devicetype = DEV_UNKNOWN;
Nico Hubera1b7f352024-03-25 18:32:11 +01001487 dp_data->io_mode = -1;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001488
1489 /* Here comes the USB stuff. */
Thomas Heijligen88e87c52022-08-05 17:56:20 +02001490 ret = libusb_init(&dp_data->usb_ctx);
1491 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001492 msg_perr("Could not initialize libusb!\n");
1493 goto init_err_exit;
1494 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001495
Ryan O'Leary301ae222019-06-24 19:14:33 -07001496 if (id != -1) {
1497 for (i = 0; ; i++) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001498 ret = dediprog_open(i, dp_data);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001499 if (ret == -1) {
1500 /* no dev */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001501 goto init_err_exit;
Nico Huber274e6552024-03-24 12:34:57 +01001502 } else if (ret < 0) {
1503 /* busy or unknown dev */
Ryan O'Leary301ae222019-06-24 19:14:33 -07001504 continue;
1505 }
1506
1507 /* Notice we can only call dediprog_read_id() after
1508 * libusb_set_configuration() and
1509 * libusb_claim_interface(). When searching by id and
1510 * either configuration or claim fails (usually the
Nico Huberc3b02dc2023-08-12 01:13:45 +02001511 * device is in use by another instance of flashprog),
Ryan O'Leary301ae222019-06-24 19:14:33 -07001512 * the device is skipped and the next device is tried.
1513 */
Nico Huber821a0852024-03-24 13:34:51 +01001514 found_id = dediprog_read_id(dp_data);
Nico Huber38af1a12024-03-24 15:04:20 +01001515 if (found_id >= 0)
1516 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1517 if (found_id == id)
1518 break;
1519
1520 libusb_release_interface(dp_data->handle, 0);
1521 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001522 }
1523 } else {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001524 if (dediprog_open(usedevice, dp_data)) {
1525 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001526 }
Nico Huber821a0852024-03-24 13:34:51 +01001527 found_id = dediprog_read_id(dp_data);
David Woodhoused2a7e872013-07-30 09:34:44 +00001528 }
Ryan O'Leary301ae222019-06-24 19:14:33 -07001529
1530 if (found_id >= 0) {
1531 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001532 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001533
Nico Huber274e6552024-03-24 12:34:57 +01001534 if (dediprog_check_devicestring(dp_data))
1535 goto init_err_cleanup_exit;
Nico Huber77fa67d2013-02-20 18:03:36 +00001536
Simon Glass25e9f402015-06-28 13:31:19 +00001537 /* Set all possible LEDs as soon as possible to indicate activity.
1538 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001539 * dediprog_check_devicestring() has queried the device. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001540 dediprog_set_leds(LED_ALL, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +00001541
Simon Glass557eb4f2015-07-05 16:53:22 +00001542 /* Select target/socket, frequency and VCC. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001543 if (set_target_flash(dp_data->handle, target) ||
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001544 dediprog_set_spi_speed(spispeed_idx, dp_data) ||
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001545 dediprog_set_spi_voltage(dp_data->handle, millivolt)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001546 dediprog_set_leds(LED_ERROR, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001547 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001548 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001549
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001550 if (dediprog_standalone_mode(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001551 goto init_err_cleanup_exit;
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001552
Nico Huber008a44f2024-04-14 23:39:47 +02001553 switch (protocol(dp_data)) {
1554 case PROTOCOL_V3: dp_data->prepare_rw_cmd = prepare_rw_cmd_v3; break;
1555 case PROTOCOL_V2: dp_data->prepare_rw_cmd = prepare_rw_cmd_v2; break;
1556 default: dp_data->prepare_rw_cmd = prepare_rw_cmd_v1; break;
1557 }
1558
Nico Hubera1b7f352024-03-25 18:32:11 +01001559 if (io_mode == DEFAULT) {
Nico Huberff9526b2025-02-27 21:52:02 +01001560 if (dp_data->devicetype != DEV_SF600PG2) {
1561 msg_pdbg("Multi i/o is only tested with SF600Plus-G2, not enabling by default.\n");
Nico Hubera1b7f352024-03-25 18:32:11 +01001562 } else {
1563 io_mode = DUAL;
1564 }
1565 } else if (io_mode > SINGLE &&
1566 (dp_data->devicetype < DEV_SF600 || protocol(dp_data) < PROTOCOL_V2)) {
1567 msg_pinfo("Multi i/o is only supported for SF600 and SF700 models w/ protocol v2 or later.\n");
1568 io_mode = SINGLE;
1569 }
1570
1571 switch (io_mode) {
1572 case DUAL: spi_master_dediprog.features |= SPI_MASTER_DUAL; break;
1573 case QUAD: spi_master_dediprog.features |= SPI_MASTER_DUAL | SPI_MASTER_QUAD; break;
1574 default: break;
1575 }
1576
1577 /* The v2, fixed-op JEDEC_FAST_READ_DUAL_DIO command
1578 seems to use the wrong number of dummy cycles. */
1579 if (protocol(dp_data) < PROTOCOL_V3)
1580 spi_master_dediprog.features &= ~SPI_MASTER_DUAL_IO;
1581
Nico Huberd0afeef2024-07-12 16:08:29 +02001582 if ((dp_data->devicetype == DEV_SF100 && protocol(dp_data) == PROTOCOL_V1) ||
Nico Huberb1d2bae2024-07-12 16:10:15 +02001583 (dp_data->devicetype >= DEV_SF600 && protocol(dp_data) == PROTOCOL_V3))
Nico Huber7eb38aa2019-03-21 15:42:54 +01001584 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1585
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001586 if (protocol(dp_data) >= PROTOCOL_V2)
Nico Huber93db6e12018-09-30 01:18:43 +02001587 spi_master_dediprog.features |= SPI_MASTER_4BA;
1588
Angel Pons116d56d2021-03-22 11:28:00 +01001589 if (dediprog_set_leds(LED_NONE, dp_data))
1590 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001591
Nico Huber89569d62023-01-12 23:31:40 +01001592 return register_spi_master(&spi_master_dediprog, 0, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001593
1594init_err_cleanup_exit:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001595 dediprog_shutdown(dp_data);
1596 return 1;
1597
1598init_err_exit:
1599 free(dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001600 return 1;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001601}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001602
1603const struct programmer_entry programmer_dediprog = {
1604 .name = "dediprog",
1605 .type = USB,
1606 .devs.dev = devs_dediprog,
1607 .init = dediprog_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001608};