blob: 4d6df5c82a42c4e22a9bf4a66532b6eb4a6942aa [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"
29
Nico Huberd99a2bd2016-02-18 21:42:49 +000030/* LIBUSB_CALL ensures the right calling conventions on libusb callbacks.
31 * However, the macro is not defined everywhere. m(
32 */
33#ifndef LIBUSB_CALL
34#define LIBUSB_CALL
35#endif
36
Stefan Reinauer915b8402011-01-28 09:00:15 +000037#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000038#define DEFAULT_TIMEOUT 3000
Nico Hubera96aaa32024-03-05 12:56:13 +010039#define MAX_BLOCK_COUNT 65535
Nico Huberd99a2bd2016-02-18 21:42:49 +000040#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
David Hendricksc3685182020-06-23 17:36:09 -070041#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
Nico Huberd99a2bd2016-02-18 21:42:49 +000042#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
43#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
44#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
David Woodhouse7f3b89f2016-02-18 21:42:15 +000045
46enum dediprog_devtype {
47 DEV_UNKNOWN = 0,
48 DEV_SF100 = 100,
Jay Thompsoncabe3202018-08-17 14:30:04 -050049 DEV_SF200 = 200,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000050 DEV_SF600 = 600,
Nico Huber00578222024-03-02 14:00:53 +010051 DEV_SF600PG2 = 600+2,
52 DEV_SF700 = 700,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000053};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000054
Simon Glass25e9f402015-06-28 13:31:19 +000055enum dediprog_leds {
56 LED_INVALID = -1,
57 LED_NONE = 0,
58 LED_PASS = 1 << 0,
59 LED_BUSY = 1 << 1,
60 LED_ERROR = 1 << 2,
61 LED_ALL = 7,
62};
63
Simon Glass557eb4f2015-07-05 16:53:22 +000064/* IO bits for CMD_SET_IO_LED message */
65enum dediprog_ios {
66 IO1 = 1 << 0,
67 IO2 = 1 << 1,
68 IO3 = 1 << 2,
69 IO4 = 1 << 3,
70};
71
72enum dediprog_cmds {
73 CMD_TRANSCEIVE = 0x01,
74 CMD_POLL_STATUS_REG = 0x02,
75 CMD_SET_VPP = 0x03,
76 CMD_SET_TARGET = 0x04,
77 CMD_READ_EEPROM = 0x05,
78 CMD_WRITE_EEPROM = 0x06,
79 CMD_SET_IO_LED = 0x07,
80 CMD_READ_PROG_INFO = 0x08,
81 CMD_SET_VCC = 0x09,
82 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000083 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000084 CMD_GET_BUTTON = 0x11,
85 CMD_GET_UID = 0x12,
86 CMD_SET_CS = 0x14,
87 CMD_IO_MODE = 0x15,
88 CMD_FW_UPDATE = 0x1A,
89 CMD_FPGA_UPDATE = 0x1B,
90 CMD_READ_FPGA_VERSION = 0x1C,
91 CMD_SET_HOLD = 0x1D,
92 CMD_READ = 0x20,
93 CMD_WRITE = 0x30,
94 CMD_WRITE_AT45DB = 0x31,
95 CMD_NAND_WRITE = 0x32,
96 CMD_NAND_READ = 0x33,
97 CMD_SET_SPI_CLK = 0x61,
98 CMD_CHECK_SOCKET = 0x62,
99 CMD_DOWNLOAD_PRJ = 0x63,
100 CMD_READ_PRJ_NAME = 0x64,
101 // New protocol/firmware only
102 CMD_CHECK_SDCARD = 0x65,
103 CMD_READ_PRJ = 0x66,
104};
105
106enum dediprog_target {
107 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
108 FLASH_TYPE_FLASH_CARD,
109 FLASH_TYPE_APPLICATION_FLASH_2,
110 FLASH_TYPE_SOCKET,
111};
112
113enum dediprog_readmode {
114 READ_MODE_STD = 1,
115 READ_MODE_FAST = 2,
116 READ_MODE_ATMEL45 = 3,
117 READ_MODE_4B_ADDR_FAST = 4,
118 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
119};
120
121enum dediprog_writemode {
Elyes HAOUASac01baa2018-05-28 16:52:21 +0200122 WRITE_MODE_PAGE_PGM = 1,
Simon Glass557eb4f2015-07-05 16:53:22 +0000123 WRITE_MODE_PAGE_WRITE = 2,
124 WRITE_MODE_1B_AAI = 3,
125 WRITE_MODE_2B_AAI = 4,
126 WRITE_MODE_128B_PAGE = 5,
127 WRITE_MODE_PAGE_AT26DF041 = 6,
128 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000129 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000130 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000131 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000132 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
133 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
134};
135
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000136enum dediprog_standalone_mode {
137 ENTER_STANDALONE_MODE = 0,
138 LEAVE_STANDALONE_MODE = 1,
139};
140
David Hendricksf73f8a72018-02-21 07:34:34 -0800141/*
Nico Huberc3b02dc2023-08-12 01:13:45 +0200142 * These are not official designations; they are for use in flashprog only.
David Hendricksf73f8a72018-02-21 07:34:34 -0800143 * Order must be preserved so that comparison operators work.
144 */
145enum protocol {
146 PROTOCOL_UNKNOWN,
147 PROTOCOL_V1,
148 PROTOCOL_V2,
149 PROTOCOL_V3,
150};
151
Thomas Heijligencc853d82021-05-04 15:32:17 +0200152static const struct dev_entry devs_dediprog[] = {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500153 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
Stefan Taunerfdec7472016-02-22 08:59:27 +0000154
155 {0},
156};
Simon Glass557eb4f2015-07-05 16:53:22 +0000157
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000158struct dediprog_data {
159 struct libusb_context *usb_ctx;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000160 libusb_device_handle *handle;
161 int in_endpoint;
162 int out_endpoint;
163 int firmwareversion;
Nico Huber274e6552024-03-24 12:34:57 +0100164 char devicestring[32+1];
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000165 enum dediprog_devtype devicetype;
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 {
210 int error; /* OK if 0, ERROR else */
211 unsigned int queued_idx;
212 unsigned int finished_idx;
213};
214
215static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
216{
217 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
218 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
219 status->error = 1;
220 msg_perr("SPI bulk read failed!\n");
221 }
222 ++status->finished_idx;
223}
224
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000225static int dediprog_bulk_read_poll(struct libusb_context *usb_ctx,
226 const struct dediprog_transfer_status *const status,
227 const int finish)
Nico Huberd99a2bd2016-02-18 21:42:49 +0000228{
229 if (status->finished_idx >= status->queued_idx)
230 return 0;
231
232 do {
233 struct timeval timeout = { 10, 0 };
234 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
235 if (ret < 0) {
236 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
237 return 1;
238 }
239 } while (finish && (status->finished_idx < status->queued_idx));
240 return 0;
241}
242
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000243static int dediprog_read(libusb_device_handle *dediprog_handle,
244 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
245 uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000246{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000247 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
248 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000249}
250
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000251static int dediprog_write(libusb_device_handle *dediprog_handle,
252 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
253 const uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000254{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000255 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
256 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000257}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000258
Nico Huberd99a2bd2016-02-18 21:42:49 +0000259
Simon Glass25e9f402015-06-28 13:31:19 +0000260/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000261static int dediprog_set_leds(int leds, const struct dediprog_data *dp_data)
Stefan Reinauer915b8402011-01-28 09:00:15 +0000262{
Simon Glass25e9f402015-06-28 13:31:19 +0000263 if (leds < LED_NONE || leds > LED_ALL)
264 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000265
Simon Glassae616512016-01-23 23:27:58 +0000266 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
267 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000268 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000269 * bit 0 == 0: red light is on.
270 *
271 * Additionally, the command structure has changed with the "new" protocol.
272 *
273 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000274 */
Simon Glassae616512016-01-23 23:27:58 +0000275 int target_leds, ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000276 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000277 target_leds = (leds ^ 7) << 8;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000278 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000279 } else {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000280 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Simon Glassae616512016-01-23 23:27:58 +0000281 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
282 } else {
283 target_leds = leds;
284 }
285 target_leds ^= 7;
286
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000287 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000288 }
289
Stefan Reinauer915b8402011-01-28 09:00:15 +0000290 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000291 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000292 return 1;
293 }
294
Stefan Reinauer915b8402011-01-28 09:00:15 +0000295 return 0;
296}
297
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000298static int dediprog_set_spi_voltage(libusb_device_handle *dediprog_handle, int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000299{
300 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000301 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000302
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000303 switch (millivolt) {
304 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000305 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000306 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000307 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000308 case 1800:
309 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000310 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000311 case 2500:
312 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000313 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000314 case 3500:
315 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000316 break;
317 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000318 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000319 return 1;
320 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000321 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
322 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000323
Nico Huber4099a8a2012-06-16 00:02:27 +0000324 if (voltage_selector == 0) {
325 /* Wait some time as the original driver does. */
326 programmer_delay(200 * 1000);
327 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000328 ret = dediprog_write(dediprog_handle, CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000329 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000330 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
331 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000332 return 1;
333 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000334 if (voltage_selector != 0) {
335 /* Wait some time as the original driver does. */
336 programmer_delay(200 * 1000);
337 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000338 return 0;
339}
340
Nico Huber77fa67d2013-02-20 18:03:36 +0000341struct dediprog_spispeeds {
342 const char *const name;
343 const int speed;
344};
345
346static const struct dediprog_spispeeds spispeeds[] = {
347 { "24M", 0x0 },
348 { "12M", 0x2 },
349 { "8M", 0x1 },
350 { "3M", 0x3 },
351 { "2.18M", 0x4 },
352 { "1.5M", 0x5 },
353 { "750k", 0x6 },
354 { "375k", 0x7 },
355 { NULL, 0x0 },
356};
357
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000358static int dediprog_set_spi_speed(unsigned int spispeed_idx, const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000359{
Nico Huber00578222024-03-02 14:00:53 +0100360 if (dp_data->devicetype < DEV_SF600PG2 && dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000361 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
362 return 0;
363 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000364
Simon Glass557eb4f2015-07-05 16:53:22 +0000365 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
366 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000367
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000368 int ret = dediprog_write(dp_data->handle, CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000369 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000370 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000371 return 1;
372 }
373 return 0;
374}
375
Nico Huber7eb38aa2019-03-21 15:42:54 +0100376static int prepare_rw_cmd(
Nico Huber93db6e12018-09-30 01:18:43 +0200377 struct flashctx *const flash, uint8_t *data_packet, unsigned int count,
Nico Huber477e1692019-06-18 23:56:01 +0200378 uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start, int is_read)
379{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100380 const struct dediprog_data *dp_data = flash->mst.spi->data;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000381
Nico Hubera96aaa32024-03-05 12:56:13 +0100382 if (count > MAX_BLOCK_COUNT) {
Nico Huberac90af62022-12-18 00:22:47 +0000383 msg_perr("%s: Unsupported transfer length of %u blocks!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200384 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber477e1692019-06-18 23:56:01 +0200385 __func__, count);
386 return 1;
387 }
388
Simon Glassae616512016-01-23 23:27:58 +0000389 /* First 5 bytes are common in both generations. */
390 data_packet[0] = count & 0xff;
391 data_packet[1] = (count >> 8) & 0xff;
392 data_packet[2] = 0; /* RFU */
393 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
394 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
395
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000396 if (protocol(dp_data) >= PROTOCOL_V2) {
Nico Huber93db6e12018-09-30 01:18:43 +0200397 if (is_read && flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
398 data_packet[3] = READ_MODE_4B_ADDR_FAST_0x0C;
399 data_packet[4] = JEDEC_READ_4BA_FAST;
400 } else if (dedi_spi_cmd == WRITE_MODE_PAGE_PGM
401 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
Nico Huberfb176d22024-03-25 15:55:13 +0100402 if (protocol(dp_data) >= PROTOCOL_V3)
403 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM;
404 else
405 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
Nico Huber93db6e12018-09-30 01:18:43 +0200406 data_packet[4] = JEDEC_BYTE_PROGRAM_4BA;
407 }
408
Simon Glassae616512016-01-23 23:27:58 +0000409 *value = *idx = 0;
410 data_packet[5] = 0; /* RFU */
411 data_packet[6] = (start >> 0) & 0xff;
412 data_packet[7] = (start >> 8) & 0xff;
413 data_packet[8] = (start >> 16) & 0xff;
414 data_packet[9] = (start >> 24) & 0xff;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000415 if (protocol(dp_data) >= PROTOCOL_V3) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800416 if (is_read) {
417 data_packet[10] = 0x00; /* address length (3 or 4) */
418 data_packet[11] = 0x00; /* dummy cycle / 2 */
419 } else {
420 /* 16 LSBs and 16 HSBs of page size */
421 /* FIXME: This assumes page size of 256. */
422 data_packet[10] = 0x00;
423 data_packet[11] = 0x01;
424 data_packet[12] = 0x00;
425 data_packet[13] = 0x00;
426 }
427 }
Simon Glassae616512016-01-23 23:27:58 +0000428 } else {
Nico Huber9bb8a322022-05-24 15:07:34 +0200429 if (flash->chip->feature_bits & FEATURE_4BA_EAR_ANY) {
Nico Huber7eb38aa2019-03-21 15:42:54 +0100430 if (spi_set_extended_address(flash, start >> 24))
431 return 1;
432 } else if (start >> 24) {
433 msg_cerr("Can't handle 4-byte address with dediprog.\n");
434 return 1;
435 }
436 /*
437 * We don't know how the dediprog firmware handles 4-byte
438 * addresses. So let's not tell it what we are doing and
439 * only send the lower 3 bytes.
440 */
441 *value = start & 0xffff;
442 *idx = (start >> 16) & 0xff;
Simon Glassae616512016-01-23 23:27:58 +0000443 }
Nico Huber7eb38aa2019-03-21 15:42:54 +0100444
445 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000446}
447
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000448/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
449 * @start start address
450 * @len length
451 * @return 0 on success, 1 on failure
452 */
Simon Glassae616512016-01-23 23:27:58 +0000453static 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 +0000454{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000455 int err = 1;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100456 const struct dediprog_data *dp_data = flash->mst.spi->data;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000457
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000458 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000459 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000460 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000461
Nico Huberd99a2bd2016-02-18 21:42:49 +0000462 struct dediprog_transfer_status status = { 0, 0, 0 };
463 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
464 struct libusb_transfer *transfer;
465
Nico Huberbbaa1712018-12-05 13:26:20 +0100466 if (len == 0)
467 return 0;
468
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000469 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000470 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200471 "Please report a bug at flashprog@flashprog.org\n",
Simon Glassae616512016-01-23 23:27:58 +0000472 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000473 return 1;
474 }
475
David Hendricksf73f8a72018-02-21 07:34:34 -0800476 int command_packet_size;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000477 switch (protocol(dp_data)) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800478 case PROTOCOL_V1:
479 command_packet_size = 5;
480 break;
481 case PROTOCOL_V2:
482 command_packet_size = 10;
483 break;
484 case PROTOCOL_V3:
485 command_packet_size = 12;
486 break;
487 default:
488 return 1;
489 }
490
491 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000492 unsigned int value, idx;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100493 if (prepare_rw_cmd(flash, data_packet, count, READ_MODE_STD, &value, &idx, start, 1))
494 return 1;
Simon Glassae616512016-01-23 23:27:58 +0000495
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000496 int ret = dediprog_write(dp_data->handle, CMD_READ, value, idx, data_packet, sizeof(data_packet));
Nico Huber519be662018-12-23 20:03:35 +0100497 if (ret != (int)sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000498 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000499 return 1;
500 }
501
Nico Huberd99a2bd2016-02-18 21:42:49 +0000502 /*
503 * Ring buffer of bulk transfers.
504 * Poll until at least one transfer is ready,
505 * schedule next transfers until buffer is full.
506 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000507
Nico Huberd99a2bd2016-02-18 21:42:49 +0000508 /* Allocate bulk transfers. */
509 unsigned int i;
Nico Huber519be662018-12-23 20:03:35 +0100510 for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000511 transfers[i] = libusb_alloc_transfer(0);
512 if (!transfers[i]) {
513 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
514 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200515 }
516 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000517
518 /* Now transfer requested chunks using libusb's asynchronous interface. */
519 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200520 while ((status.queued_idx < count) &&
521 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
522 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000523 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
Nico Huber821a0852024-03-24 13:34:51 +0100524 libusb_fill_bulk_transfer(transfer, dp_data->handle, dp_data->in_endpoint,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000525 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
526 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
527 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
528 ret = libusb_submit_transfer(transfer);
529 if (ret < 0) {
530 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
531 status.queued_idx, libusb_error_name(ret));
532 goto err_free;
533 }
534 ++status.queued_idx;
535 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000536 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 0))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000537 goto err_free;
538 }
539 /* Wait for transfers to finish. */
Rick Altherr4f9cd8b2021-12-13 17:10:00 -0800540 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000541 goto err_free;
542 /* Check if everything has been transmitted. */
543 if ((status.finished_idx < count) || status.error)
544 goto err_free;
545
546 err = 0;
547
548err_free:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000549 dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000550 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
551 if (transfers[i]) libusb_free_transfer(transfers[i]);
552 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000553}
554
Simon Glassae616512016-01-23 23:27:58 +0000555static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000556{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000557 int ret;
558 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000559 const unsigned int chunksize = 0x200;
Nico Huberbbaa1712018-12-05 13:26:20 +0100560 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000561 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100562 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000563
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000564 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000565
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000566 if (residue) {
567 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
568 start, residue);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100569 ret = default_spi_read(flash, buf, start, residue);
Simon Glass25e9f402015-06-28 13:31:19 +0000570 if (ret)
571 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000572 }
573
574 /* Round down. */
575 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000576 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000577 if (ret)
578 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000579
580 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000581 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000582 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
583 start, len);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100584 ret = default_spi_read(flash, buf + residue + bulklen,
585 start + residue + bulklen, len);
Simon Glass25e9f402015-06-28 13:31:19 +0000586 if (ret)
587 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000588 }
589
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000590 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000591 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000592err:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000593 dediprog_set_leds(LED_ERROR, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +0000594 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000595}
596
Nico Hubera4b14f72012-06-19 12:06:53 +0000597/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
598 * @chunksize length of data chunks, only 256 supported by now
599 * @start start address
600 * @len length
601 * @dedi_spi_cmd dediprog specific write command for spi bus
602 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000603 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000604static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000605 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000606{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000607 /* USB transfer size must be 512, other sizes will NOT work at all.
608 * chunksize is the real data size per USB bulk transfer. The remaining
609 * space in a USB bulk transfer must be filled with 0xff padding.
610 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000611 const unsigned int count = len / chunksize;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100612 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000613
Nico Hubera4b14f72012-06-19 12:06:53 +0000614 /*
615 * We should change this check to
616 * chunksize > 512
617 * once we know how to handle different chunk sizes.
618 */
619 if (chunksize != 256) {
620 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200621 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000622 __func__, chunksize);
Nico Hubera4b14f72012-06-19 12:06:53 +0000623 return 1;
624 }
625
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000626 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000627 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200628 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000629 __func__, start, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000630 return 1;
631 }
632
633 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000634 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000635 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000636
David Hendricksf73f8a72018-02-21 07:34:34 -0800637 int command_packet_size;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000638 switch (protocol(dp_data)) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800639 case PROTOCOL_V1:
640 command_packet_size = 5;
641 break;
642 case PROTOCOL_V2:
643 command_packet_size = 10;
644 break;
645 case PROTOCOL_V3:
646 command_packet_size = 14;
647 break;
648 default:
649 return 1;
650 }
651
652 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000653 unsigned int value, idx;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100654 if (prepare_rw_cmd(flash, data_packet, count, dedi_spi_cmd, &value, &idx, start, 0))
655 return 1;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000656 int ret = dediprog_write(dp_data->handle, CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
Nico Huber519be662018-12-23 20:03:35 +0100657 if (ret != (int)sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000658 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000659 return 1;
660 }
661
Simon Glassae616512016-01-23 23:27:58 +0000662 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000663 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000664 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000665 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000666 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000667 int transferred;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000668 ret = libusb_bulk_transfer(dp_data->handle, dp_data->out_endpoint, usbbuf, 512, &transferred,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000669 DEFAULT_TIMEOUT);
670 if ((ret < 0) || (transferred != 512)) {
671 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000672 return 1;
673 }
674 }
675
676 return 0;
677}
678
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000679static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000680 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000681{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000682 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000683 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000684 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
685 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100686 const struct dediprog_data *dp_data = flash->mst.spi->data;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000687
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000688 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000689
Nico Hubera4b14f72012-06-19 12:06:53 +0000690 if (chunksize != 256) {
691 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
692 "we don't know how dediprog\nhandles them.\n");
693 /* Write everything like it was residue. */
694 residue = len;
695 }
696
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000697 if (residue) {
698 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
699 start, residue);
Nico Huber93db6e12018-09-30 01:18:43 +0200700 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
701 ret = spi_write_chunked(flash, buf, start, residue, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000702 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000703 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000704 return ret;
705 }
706 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000707
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000708 /* Round down. */
709 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000710 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000711 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000712 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000713 return ret;
714 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000715
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000716 len -= residue + bulklen;
717 if (len) {
718 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
719 start, len);
720 ret = spi_write_chunked(flash, buf + residue + bulklen,
Nico Huber93db6e12018-09-30 01:18:43 +0200721 start + residue + bulklen, len, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000722 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000723 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000724 return ret;
725 }
726 }
727
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000728 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000729 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000730}
731
Nico Hubera96aaa32024-03-05 12:56:13 +0100732static int dediprog_spi_write_chunked(struct flashctx *flash, const uint8_t *buf,
733 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
734{
735 /* We can write only up to 65535 pages at once: */
736 while (len) {
737 const size_t len_here = MIN(len, flash->chip->page_size * MAX_BLOCK_COUNT);
738 const int ret = dediprog_spi_write(flash, buf, start, len_here, dedi_spi_cmd);
739 if (ret)
740 return ret;
741
742 start += len_here;
743 buf += len_here;
744 len -= len_here;
745 }
746 return 0;
747}
748
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000749static 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 +0000750{
Nico Hubera96aaa32024-03-05 12:56:13 +0100751 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000752}
753
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000754static 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 +0000755{
Nico Hubera96aaa32024-03-05 12:56:13 +0100756 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000757}
758
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000759static int dediprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000760 unsigned int writecnt,
761 unsigned int readcnt,
762 const unsigned char *writearr,
763 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000764{
765 int ret;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100766 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000767
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000768 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Nico Huber9a11cbf2023-01-13 01:19:07 +0100769 if (writecnt > flash->mst.spi->max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000770 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000771 return 1;
772 }
Nico Huber9a11cbf2023-01-13 01:19:07 +0100773 if (readcnt > flash->mst.spi->max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000774 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000775 return 1;
776 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100777
Simon Glassae616512016-01-23 23:27:58 +0000778 unsigned int idx, value;
779 /* New protocol has options and timeout combined as value while the old one used the value field for
780 * timeout and the index field for options. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000781 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000782 idx = 0;
783 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
784 } else {
785 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
786 value = 0;
787 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000788 ret = dediprog_write(dp_data->handle, CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Nico Huber519be662018-12-23 20:03:35 +0100789 if (ret != (int)writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000790 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000791 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000792 return 1;
793 }
Simon Glassae616512016-01-23 23:27:58 +0000794 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000795 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000796
Stefan Tauner74367bf2016-02-20 20:53:46 +0000797 /* The specifications do state the possibility to set a timeout for transceive transactions.
798 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
799 * can predict the time needed by the previous command or so (untested). In any case, using this
800 * "feature" to set sane-looking timouts for the read below will completely trash performance with
801 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
802 *
803 * The specification also uses only 0 in its examples, so the lesson to learn here:
804 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000805 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800806 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000807 idx = 0;
808 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
809 } else {
810 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
811 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
812 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000813 ret = dediprog_read(dp_data->dediprog_handle, CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000814 */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000815 ret = dediprog_read(dp_data->handle, CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Nico Huber519be662018-12-23 20:03:35 +0100816 if (ret != (int)readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000817 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 +0000818 return 1;
819 }
820 return 0;
821}
822
Nico Huber274e6552024-03-24 12:34:57 +0100823static int dediprog_read_devicestring(struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000824{
Nico Huber274e6552024-03-24 12:34:57 +0100825 const int devstr_len = sizeof(dp_data->devicestring) - 1, old_devstr_len = 16;
826 char *const buf = dp_data->devicestring;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000827 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000828
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000829 /* Command Receive Device String. */
Nico Hubere76e21f2024-02-29 23:33:35 +0100830 ret = dediprog_read(dp_data->handle, CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, devstr_len);
Nico Huber5262e292024-03-02 13:21:25 +0100831 if (ret < old_devstr_len || ret > devstr_len) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000832 msg_perr("Incomplete/failed Command Receive Device String!\n");
833 return 1;
834 }
Nico Huber5262e292024-03-02 13:21:25 +0100835 buf[ret] = '\0';
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000836 msg_pdbg("Found a %s\n", buf);
Nico Hubere76e21f2024-02-29 23:33:35 +0100837 if (memcmp(buf, "SF100", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000838 dp_data->devicetype = DEV_SF100;
Nico Hubere76e21f2024-02-29 23:33:35 +0100839 else if (memcmp(buf, "SF200", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000840 dp_data->devicetype = DEV_SF200;
Nico Huber00578222024-03-02 14:00:53 +0100841 else if (memcmp(buf, "SF600PG2", 8) == 0) /* match first, before shorter, generic SF600 */
842 dp_data->devicetype = DEV_SF600PG2;
Nico Hubere76e21f2024-02-29 23:33:35 +0100843 else if (memcmp(buf, "SF600", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000844 dp_data->devicetype = DEV_SF600;
Nico Huber00578222024-03-02 14:00:53 +0100845 else if (memcmp(buf, "SF700", 5) == 0)
846 dp_data->devicetype = DEV_SF700;
Nico Huber274e6552024-03-24 12:34:57 +0100847 else
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000848 return 1;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000849
Nico Huber274e6552024-03-24 12:34:57 +0100850 return 0;
851}
852
853static int dediprog_check_devicestring(struct dediprog_data *dp_data)
854{
855 char *const buf = dp_data->devicestring;
Nico Huberbdef5c22024-03-02 13:51:39 +0100856 unsigned int sfnum;
857 unsigned int fw[3];
Nico Huber274e6552024-03-24 12:34:57 +0100858
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100859 if (sscanf(buf, "SF%u", &sfnum) != 1 ||
Nico Huber00578222024-03-02 14:00:53 +0100860 sfnum != dp_data->devicetype / 100 * 100 ||
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100861 sscanf(buf, "SF%*s V:%u.%u.%u ", &fw[0], &fw[1], &fw[2]) != 3) {
Simon Glassae616512016-01-23 23:27:58 +0000862 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000863 return 1;
864 }
Nico Huber00578222024-03-02 14:00:53 +0100865
866 /* Only allow major versions that were tested. */
867 if ((dp_data->devicetype == DEV_SF600PG2 && fw[0] > 1) ||
868 (dp_data->devicetype == DEV_SF700 && fw[0] != 4) ||
869 (dp_data->devicetype <= DEV_SF600 && (fw[0] < 2 || fw[0] > 7))) {
Simon Glassae616512016-01-23 23:27:58 +0000870 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000871 return 1;
872 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800873
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000874 dp_data->firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000875 if (protocol(dp_data) == PROTOCOL_UNKNOWN) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800876 msg_perr("Internal error: Unable to determine protocol version.\n");
877 return 1;
878 }
Simon Glassae616512016-01-23 23:27:58 +0000879
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000880 return 0;
881}
882
David Hendricksc05900f2016-02-18 21:42:41 +0000883/*
Ryan O'Leary301ae222019-06-24 19:14:33 -0700884 * Read the id from the dediprog. This should return the numeric part of the
885 * serial number found on a sticker on the back of the dediprog. Note this
Nico Huber821a0852024-03-24 13:34:51 +0100886 * number is stored in writable eeprom, so it could get out of sync.
Ryan O'Leary301ae222019-06-24 19:14:33 -0700887 * @return the id on success, -1 on failure
888 */
Nico Huber821a0852024-03-24 13:34:51 +0100889static int dediprog_read_id(struct dediprog_data *const dp)
Ryan O'Leary301ae222019-06-24 19:14:33 -0700890{
Nico Huber821a0852024-03-24 13:34:51 +0100891 const int min_len = 3;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700892 int ret;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700893
Nico Huber821a0852024-03-24 13:34:51 +0100894 if (dp->devicetype >= DEV_SF600PG2) {
895 const uint8_t out[6] = { 0x00, 0x00, 0x00, 0x02, 0x00, 0x00 };
896 const uint8_t cmd = 0x71;
897 uint8_t buf[512];
898 int transferred;
899 int try;
900
901 /* Always query the id twice as the endpoint
902 can lock up in mysterious ways otherwise. */
903 for (try = 0; try < 2; ++try) {
904 ret = dediprog_write(dp->handle, cmd, 0, 0, out, sizeof(out));
905 if (ret != (int)sizeof(out))
906 goto failed_ret;
907
908 ret = libusb_bulk_transfer(dp->handle, dp->in_endpoint,
909 buf, sizeof(buf), &transferred, DEFAULT_TIMEOUT);
910 }
911
912 if (ret == 0 && transferred >= min_len)
913 return buf[2] << 16 | buf[1] << 8 | buf[0];
914 } else {
915 uint8_t buf[16];
916
917 if (dp->devicetype >= DEV_SF600) {
918 ret = dediprog_read(dp->handle, CMD_READ_EEPROM, 0, 0, buf, sizeof(buf));
919 } else {
920 ret = libusb_control_transfer(dp->handle, REQTYPE_OTHER_IN,
921 0x7, /* request */
922 0, /* value */
923 0xEF00, /* index */
924 buf, min_len, DEFAULT_TIMEOUT);
925 }
926
927 if (ret >= min_len)
928 return buf[0] << 16 | buf[1] << 8 | buf[2];
Ryan O'Leary301ae222019-06-24 19:14:33 -0700929 }
930
Nico Huber821a0852024-03-24 13:34:51 +0100931failed_ret:
932 msg_perr("Failed to read dediprog id: ");
933 if (ret < 0)
934 msg_perr("%s (%d)\n", libusb_strerror(ret), ret);
935 else
936 msg_perr("short read!\n");
937 return -1;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700938}
939
940/*
David Hendricksc05900f2016-02-18 21:42:41 +0000941 * This command presumably sets the voltage for the SF100 itself (not the
942 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
943 * (including all SF600s) do not support it.
944 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000945
David Hendricksc05900f2016-02-18 21:42:41 +0000946/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
947 * Only use dediprog_set_voltage on SF100 programmers with firmware older
948 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000949static int dediprog_set_voltage(libusb_device_handle *dediprog_handle)
David Hendricksc05900f2016-02-18 21:42:41 +0000950{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000951 unsigned char buf[1] = {0};
952 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000953 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000954 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000955 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000956 return 1;
957 }
David Hendricksc05900f2016-02-18 21:42:41 +0000958 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000959 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000960 return 1;
961 }
David Hendricksc05900f2016-02-18 21:42:41 +0000962
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000963 return 0;
964}
965
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000966static int dediprog_standalone_mode(const struct dediprog_data *dp_data)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000967{
968 int ret;
969
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000970 if (dp_data->devicetype != DEV_SF600)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000971 return 0;
972
973 msg_pdbg2("Disabling standalone mode.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000974 ret = dediprog_write(dp_data->handle, CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000975 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000976 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000977 return 1;
978 }
979
980 return 0;
981}
982
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000983#if 0
984/* Something.
985 * Present in eng_detect_blink.log with firmware 3.1.8
986 * Always preceded by Command Receive Device String
987 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000988static int dediprog_command_b(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000989{
990 int ret;
991 char buf[0x3];
992
Simon Glass557eb4f2015-07-05 16:53:22 +0000993 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
994 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000995 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000996 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000997 return 1;
998 }
999 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
1000 (buf[2] != 0xff)) {
1001 msg_perr("Unexpected response to Command B!\n");
1002 return 1;
1003 }
1004
1005 return 0;
1006}
1007#endif
1008
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001009static int set_target_flash(libusb_device_handle *dediprog_handle, enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001010{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001011 int ret = dediprog_write(dediprog_handle, CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +00001012 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001013 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001014 return 1;
1015 }
1016 return 0;
1017}
1018
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001019#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +00001020/* Returns true if the button is currently pressed. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001021static bool dediprog_get_button(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001022{
Simon Glass557eb4f2015-07-05 16:53:22 +00001023 char buf[1];
1024 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
1025 buf, 0x1, DEFAULT_TIMEOUT);
1026 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001027 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001028 return 1;
1029 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001030 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001031}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001032#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001033
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001034static int parse_voltage(char *voltage)
1035{
1036 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001037 int i;
1038 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001039
1040 if (!voltage || !strlen(voltage)) {
1041 msg_perr("Empty voltage= specified.\n");
1042 return -1;
1043 }
1044 millivolt = (int)strtol(voltage, &tmp, 0);
1045 voltage = tmp;
1046 /* Handle "," and "." as decimal point. Everything after it is assumed
1047 * to be in decimal notation.
1048 */
1049 if ((*voltage == '.') || (*voltage == ',')) {
1050 voltage++;
1051 for (i = 0; i < 3; i++) {
1052 fraction *= 10;
1053 /* Don't advance if the current character is invalid,
1054 * but continue multiplying.
1055 */
1056 if ((*voltage < '0') || (*voltage > '9'))
1057 continue;
1058 fraction += *voltage - '0';
1059 voltage++;
1060 }
1061 /* Throw away remaining digits. */
1062 voltage += strspn(voltage, "0123456789");
1063 }
1064 /* The remaining string must be empty or "mV" or "V". */
1065 tolower_string(voltage);
1066
1067 /* No unit or "V". */
1068 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1069 millivolt *= 1000;
1070 millivolt += fraction;
1071 } else if (!strncmp(voltage, "mv", 2) ||
1072 !strncmp(voltage, "milliv", 6)) {
1073 /* No adjustment. fraction is discarded. */
1074 } else {
1075 /* Garbage at the end of the string. */
1076 msg_perr("Garbage voltage= specified.\n");
1077 return -1;
1078 }
1079 return millivolt;
1080}
1081
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001082static int dediprog_shutdown(void *data);
1083
Nico Huber93db6e12018-09-30 01:18:43 +02001084static struct spi_master spi_master_dediprog = {
Nico Huberdc5af542018-12-22 16:54:59 +01001085 .features = SPI_MASTER_NO_4BA_MODES,
Simon Glassae616512016-01-23 23:27:58 +00001086 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
1087 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001088 .command = dediprog_spi_send_command,
1089 .multicommand = default_spi_send_multicommand,
1090 .read = dediprog_spi_read,
1091 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +00001092 .write_aai = dediprog_spi_write_aai,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001093 .shutdown = dediprog_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301094 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001095};
1096
Ryan O'Leary301ae222019-06-24 19:14:33 -07001097/*
1098 * Open a dediprog_handle with the USB device at the given index.
1099 * @index index of the USB device
Nico Huber274e6552024-03-24 12:34:57 +01001100 * @return 0 for success, -1 for error, -2 for busy device, -3 for unknown device
Ryan O'Leary301ae222019-06-24 19:14:33 -07001101 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001102static int dediprog_open(int index, struct dediprog_data *dp_data)
Ryan O'Leary301ae222019-06-24 19:14:33 -07001103{
1104 const uint16_t vid = devs_dediprog[0].vendor_id;
1105 const uint16_t pid = devs_dediprog[0].device_id;
1106 int ret;
1107
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001108 dp_data->handle = usb_dev_get_by_vid_pid_number(dp_data->usb_ctx, vid, pid, (unsigned int) index);
1109 if (!dp_data->handle) {
Ryan O'Leary301ae222019-06-24 19:14:33 -07001110 msg_perr("Could not find a Dediprog programmer on USB.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001111 libusb_exit(dp_data->usb_ctx);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001112 return -1;
1113 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001114 ret = libusb_set_configuration(dp_data->handle, 1);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001115 if (ret != 0) {
1116 msg_perr("Could not set USB device configuration: %i %s\n",
1117 ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001118 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001119 return -2;
1120 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001121 ret = libusb_claim_interface(dp_data->handle, 0);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001122 if (ret < 0) {
1123 msg_perr("Could not claim USB device interface %i: %i %s\n",
1124 0, ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001125 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001126 return -2;
1127 }
Nico Huber274e6552024-03-24 12:34:57 +01001128
1129 /* Try reading the devicestring. If that fails and the device is old
1130 (FW < 6.0.0, which we cannot know), then we need to try the "set
1131 voltage" command and then attempt to read the devicestring again. */
1132 if (dediprog_read_devicestring(dp_data)) {
1133 if (dediprog_set_voltage(dp_data->handle))
1134 goto unknown_dev;
1135 if (dediprog_read_devicestring(dp_data))
1136 goto unknown_dev;
1137 }
Nico Huber821a0852024-03-24 13:34:51 +01001138
1139 dp_data->in_endpoint = LIBUSB_ENDPOINT_IN | 2;
1140 if (dp_data->devicetype <= DEV_SF200)
1141 dp_data->out_endpoint = 2;
1142 else
1143 dp_data->out_endpoint = 1;
1144
Ryan O'Leary301ae222019-06-24 19:14:33 -07001145 return 0;
Nico Huber274e6552024-03-24 12:34:57 +01001146
1147unknown_dev:
1148 msg_pwarn("Ignoring unknown Dediprog device. Not a SF100, SF200, SF600(Plus(G2)), or SF700!\n");
1149 libusb_release_interface(dp_data->handle, 0);
1150 libusb_close(dp_data->handle);
1151 return -3;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001152}
1153
David Hendricks8bb20212011-06-14 01:35:36 +00001154static int dediprog_shutdown(void *data)
1155{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001156 int ret = 0;
1157 struct dediprog_data *dp_data = data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001158
David Hendricks8bb20212011-06-14 01:35:36 +00001159 /* URB 28. Command Set SPI Voltage to 0. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001160 if (dediprog_set_spi_voltage(dp_data->handle, 0x0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001161 ret = 1;
1162 goto out;
David Hendricks8bb20212011-06-14 01:35:36 +00001163 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001164
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001165 if (libusb_release_interface(dp_data->handle, 0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001166 msg_perr("Could not release USB interface!\n");
1167 ret = 1;
1168 goto out;
1169 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001170 libusb_close(dp_data->handle);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001171 libusb_exit(dp_data->usb_ctx);
1172out:
1173 free(data);
1174 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +00001175}
1176
Nico Hubere3a26882023-01-11 21:45:51 +01001177static int dediprog_init(struct flashprog_programmer *const prog)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001178{
Ryan O'Leary301ae222019-06-24 19:14:33 -07001179 char *voltage, *id_str, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +00001180 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001181 int millivolt = 3500;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001182 int id = -1; /* -1 defaults to enumeration order */
1183 int found_id;
Nathan Laredo21541a62012-12-24 22:07:36 +00001184 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +02001185 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +00001186 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001187
Nico Huber77fa67d2013-02-20 18:03:36 +00001188 spispeed = extract_programmer_param("spispeed");
1189 if (spispeed) {
1190 for (i = 0; spispeeds[i].name; ++i) {
1191 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1192 spispeed_idx = i;
1193 break;
1194 }
1195 }
1196 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +00001197 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +00001198 free(spispeed);
1199 return 1;
1200 }
1201 free(spispeed);
1202 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001203
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001204 voltage = extract_programmer_param("voltage");
1205 if (voltage) {
1206 millivolt = parse_voltage(voltage);
1207 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001208 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001209 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001210 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1211 }
1212
Ryan O'Leary301ae222019-06-24 19:14:33 -07001213 id_str = extract_programmer_param("id");
1214 if (id_str) {
1215 char prefix0, prefix1;
1216 if (sscanf(id_str, "%c%c%d", &prefix0, &prefix1, &id) != 3) {
1217 msg_perr("Error: Could not parse dediprog 'id'.\n");
1218 msg_perr("Expected a string like SF012345 or DP012345.\n");
1219 free(id_str);
1220 return 1;
1221 }
1222 if (id < 0 || id >= 0x1000000) {
1223 msg_perr("Error: id %s is out of range!\n", id_str);
1224 free(id_str);
1225 return 1;
1226 }
1227 if (!(prefix0 == 'S' && prefix1 == 'F') && !(prefix0 == 'D' && prefix1 == 'P')) {
1228 msg_perr("Error: %s is an invalid id!\n", id_str);
1229 free(id_str);
1230 return 1;
1231 }
1232 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1233 }
1234 free(id_str);
1235
Nathan Laredo21541a62012-12-24 22:07:36 +00001236 device = extract_programmer_param("device");
1237 if (device) {
1238 char *dev_suffix;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001239 if (id != -1) {
1240 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1241 }
Nathan Laredo21541a62012-12-24 22:07:36 +00001242 errno = 0;
1243 usedevice = strtol(device, &dev_suffix, 10);
1244 if (errno != 0 || device == dev_suffix) {
1245 msg_perr("Error: Could not convert 'device'.\n");
1246 free(device);
1247 return 1;
1248 }
Nico Huber961f4a12019-10-04 17:34:22 +02001249 if (usedevice < 0 || usedevice > INT_MAX) {
Nathan Laredo21541a62012-12-24 22:07:36 +00001250 msg_perr("Error: Value for 'device' is out of range.\n");
1251 free(device);
1252 return 1;
1253 }
1254 if (strlen(dev_suffix) > 0) {
1255 msg_perr("Error: Garbage following 'device' value.\n");
1256 free(device);
1257 return 1;
1258 }
1259 msg_pinfo("Using device %li.\n", usedevice);
1260 }
1261 free(device);
1262
Stefan Taunere659d2d2013-05-03 21:58:28 +00001263 target_str = extract_programmer_param("target");
1264 if (target_str) {
1265 char *target_suffix;
1266 errno = 0;
1267 target = strtol(target_str, &target_suffix, 10);
1268 if (errno != 0 || target_str == target_suffix) {
1269 msg_perr("Error: Could not convert 'target'.\n");
1270 free(target_str);
1271 return 1;
1272 }
1273 if (target < 1 || target > 2) {
1274 msg_perr("Error: Value for 'target' is out of range.\n");
1275 free(target_str);
1276 return 1;
1277 }
1278 if (strlen(target_suffix) > 0) {
1279 msg_perr("Error: Garbage following 'target' value.\n");
1280 free(target_str);
1281 return 1;
1282 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001283 switch (target) {
1284 case 1:
1285 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1286 target = FLASH_TYPE_APPLICATION_FLASH_1;
1287 break;
1288 case 2:
1289 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1290 target = FLASH_TYPE_APPLICATION_FLASH_2;
1291 break;
1292 default:
1293 break;
1294 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001295 }
1296 free(target_str);
1297
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001298 struct dediprog_data *dp_data = calloc(1, sizeof(*dp_data));
1299 if (!dp_data) {
1300 msg_perr("Unable to allocate space for SPI master data\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001301 return 1;
1302 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001303 dp_data->firmwareversion = FIRMWARE_VERSION(0, 0, 0);
1304 dp_data->devicetype = DEV_UNKNOWN;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001305
1306 /* Here comes the USB stuff. */
Thomas Heijligen88e87c52022-08-05 17:56:20 +02001307 ret = libusb_init(&dp_data->usb_ctx);
1308 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001309 msg_perr("Could not initialize libusb!\n");
1310 goto init_err_exit;
1311 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001312
Ryan O'Leary301ae222019-06-24 19:14:33 -07001313 if (id != -1) {
1314 for (i = 0; ; i++) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001315 ret = dediprog_open(i, dp_data);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001316 if (ret == -1) {
1317 /* no dev */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001318 goto init_err_exit;
Nico Huber274e6552024-03-24 12:34:57 +01001319 } else if (ret < 0) {
1320 /* busy or unknown dev */
Ryan O'Leary301ae222019-06-24 19:14:33 -07001321 continue;
1322 }
1323
1324 /* Notice we can only call dediprog_read_id() after
1325 * libusb_set_configuration() and
1326 * libusb_claim_interface(). When searching by id and
1327 * either configuration or claim fails (usually the
Nico Huberc3b02dc2023-08-12 01:13:45 +02001328 * device is in use by another instance of flashprog),
Ryan O'Leary301ae222019-06-24 19:14:33 -07001329 * the device is skipped and the next device is tried.
1330 */
Nico Huber821a0852024-03-24 13:34:51 +01001331 found_id = dediprog_read_id(dp_data);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001332 if (found_id < 0) {
1333 msg_perr("Could not read id.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001334 libusb_release_interface(dp_data->handle, 0);
1335 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001336 continue;
1337 }
1338 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1339 if (found_id != id) {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001340 libusb_release_interface(dp_data->handle, 0);
1341 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001342 continue;
1343 }
1344 break;
1345 }
1346 } else {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001347 if (dediprog_open(usedevice, dp_data)) {
1348 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001349 }
Nico Huber821a0852024-03-24 13:34:51 +01001350 found_id = dediprog_read_id(dp_data);
David Woodhoused2a7e872013-07-30 09:34:44 +00001351 }
Ryan O'Leary301ae222019-06-24 19:14:33 -07001352
1353 if (found_id >= 0) {
1354 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001355 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001356
Nico Huber274e6552024-03-24 12:34:57 +01001357 if (dediprog_check_devicestring(dp_data))
1358 goto init_err_cleanup_exit;
Nico Huber77fa67d2013-02-20 18:03:36 +00001359
Simon Glass25e9f402015-06-28 13:31:19 +00001360 /* Set all possible LEDs as soon as possible to indicate activity.
1361 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001362 * dediprog_check_devicestring() has queried the device. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001363 dediprog_set_leds(LED_ALL, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +00001364
Simon Glass557eb4f2015-07-05 16:53:22 +00001365 /* Select target/socket, frequency and VCC. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001366 if (set_target_flash(dp_data->handle, target) ||
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001367 dediprog_set_spi_speed(spispeed_idx, dp_data) ||
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001368 dediprog_set_spi_voltage(dp_data->handle, millivolt)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001369 dediprog_set_leds(LED_ERROR, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001370 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001371 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001372
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001373 if (dediprog_standalone_mode(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001374 goto init_err_cleanup_exit;
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001375
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001376 if ((dp_data->devicetype == DEV_SF100) ||
1377 (dp_data->devicetype == DEV_SF600 && protocol(dp_data) == PROTOCOL_V3))
Nico Huber7eb38aa2019-03-21 15:42:54 +01001378 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1379
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001380 if (protocol(dp_data) >= PROTOCOL_V2)
Nico Huber93db6e12018-09-30 01:18:43 +02001381 spi_master_dediprog.features |= SPI_MASTER_4BA;
1382
Angel Pons116d56d2021-03-22 11:28:00 +01001383 if (dediprog_set_leds(LED_NONE, dp_data))
1384 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001385
Nico Huber89569d62023-01-12 23:31:40 +01001386 return register_spi_master(&spi_master_dediprog, 0, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001387
1388init_err_cleanup_exit:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001389 dediprog_shutdown(dp_data);
1390 return 1;
1391
1392init_err_exit:
1393 free(dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001394 return 1;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001395}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001396
1397const struct programmer_entry programmer_dediprog = {
1398 .name = "dediprog",
1399 .type = USB,
1400 .devs.dev = devs_dediprog,
1401 .init = dediprog_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001402};