blob: 866d628b2d486e6737d60ec5cc3375ffe49959e2 [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 Huberd99a2bd2016-02-18 21:42:49 +000039#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
David Hendricksc3685182020-06-23 17:36:09 -070040#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
Nico Huberd99a2bd2016-02-18 21:42:49 +000041#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
42#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
43#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
David Woodhouse7f3b89f2016-02-18 21:42:15 +000044
45enum dediprog_devtype {
46 DEV_UNKNOWN = 0,
47 DEV_SF100 = 100,
Jay Thompsoncabe3202018-08-17 14:30:04 -050048 DEV_SF200 = 200,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000049 DEV_SF600 = 600,
50};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000051
Simon Glass25e9f402015-06-28 13:31:19 +000052enum dediprog_leds {
53 LED_INVALID = -1,
54 LED_NONE = 0,
55 LED_PASS = 1 << 0,
56 LED_BUSY = 1 << 1,
57 LED_ERROR = 1 << 2,
58 LED_ALL = 7,
59};
60
Simon Glass557eb4f2015-07-05 16:53:22 +000061/* IO bits for CMD_SET_IO_LED message */
62enum dediprog_ios {
63 IO1 = 1 << 0,
64 IO2 = 1 << 1,
65 IO3 = 1 << 2,
66 IO4 = 1 << 3,
67};
68
69enum dediprog_cmds {
70 CMD_TRANSCEIVE = 0x01,
71 CMD_POLL_STATUS_REG = 0x02,
72 CMD_SET_VPP = 0x03,
73 CMD_SET_TARGET = 0x04,
74 CMD_READ_EEPROM = 0x05,
75 CMD_WRITE_EEPROM = 0x06,
76 CMD_SET_IO_LED = 0x07,
77 CMD_READ_PROG_INFO = 0x08,
78 CMD_SET_VCC = 0x09,
79 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000080 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000081 CMD_GET_BUTTON = 0x11,
82 CMD_GET_UID = 0x12,
83 CMD_SET_CS = 0x14,
84 CMD_IO_MODE = 0x15,
85 CMD_FW_UPDATE = 0x1A,
86 CMD_FPGA_UPDATE = 0x1B,
87 CMD_READ_FPGA_VERSION = 0x1C,
88 CMD_SET_HOLD = 0x1D,
89 CMD_READ = 0x20,
90 CMD_WRITE = 0x30,
91 CMD_WRITE_AT45DB = 0x31,
92 CMD_NAND_WRITE = 0x32,
93 CMD_NAND_READ = 0x33,
94 CMD_SET_SPI_CLK = 0x61,
95 CMD_CHECK_SOCKET = 0x62,
96 CMD_DOWNLOAD_PRJ = 0x63,
97 CMD_READ_PRJ_NAME = 0x64,
98 // New protocol/firmware only
99 CMD_CHECK_SDCARD = 0x65,
100 CMD_READ_PRJ = 0x66,
101};
102
103enum dediprog_target {
104 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
105 FLASH_TYPE_FLASH_CARD,
106 FLASH_TYPE_APPLICATION_FLASH_2,
107 FLASH_TYPE_SOCKET,
108};
109
110enum dediprog_readmode {
111 READ_MODE_STD = 1,
112 READ_MODE_FAST = 2,
113 READ_MODE_ATMEL45 = 3,
114 READ_MODE_4B_ADDR_FAST = 4,
115 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
116};
117
118enum dediprog_writemode {
Elyes HAOUASac01baa2018-05-28 16:52:21 +0200119 WRITE_MODE_PAGE_PGM = 1,
Simon Glass557eb4f2015-07-05 16:53:22 +0000120 WRITE_MODE_PAGE_WRITE = 2,
121 WRITE_MODE_1B_AAI = 3,
122 WRITE_MODE_2B_AAI = 4,
123 WRITE_MODE_128B_PAGE = 5,
124 WRITE_MODE_PAGE_AT26DF041 = 6,
125 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000126 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000127 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000128 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000129 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
130 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
131};
132
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000133enum dediprog_standalone_mode {
134 ENTER_STANDALONE_MODE = 0,
135 LEAVE_STANDALONE_MODE = 1,
136};
137
David Hendricksf73f8a72018-02-21 07:34:34 -0800138/*
Nico Huberc3b02dc2023-08-12 01:13:45 +0200139 * These are not official designations; they are for use in flashprog only.
David Hendricksf73f8a72018-02-21 07:34:34 -0800140 * Order must be preserved so that comparison operators work.
141 */
142enum protocol {
143 PROTOCOL_UNKNOWN,
144 PROTOCOL_V1,
145 PROTOCOL_V2,
146 PROTOCOL_V3,
147};
148
Thomas Heijligencc853d82021-05-04 15:32:17 +0200149static const struct dev_entry devs_dediprog[] = {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500150 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
Stefan Taunerfdec7472016-02-22 08:59:27 +0000151
152 {0},
153};
Simon Glass557eb4f2015-07-05 16:53:22 +0000154
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000155struct dediprog_data {
156 struct libusb_context *usb_ctx;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000157 libusb_device_handle *handle;
158 int in_endpoint;
159 int out_endpoint;
160 int firmwareversion;
161 enum dediprog_devtype devicetype;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000162};
Simon Glass557eb4f2015-07-05 16:53:22 +0000163
Nico Huberd99a2bd2016-02-18 21:42:49 +0000164#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
165 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
166/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
167const char * LIBUSB_CALL libusb_error_name(int error_code)
168{
169 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
170 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
171 static char my_libusb_error[18 + 5 + 2];
172 sprintf(my_libusb_error, "libusb error code %i", error_code);
173 return my_libusb_error;
174 } else {
175 return "UNKNOWN";
176 }
177}
178#endif
179
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000180static enum protocol protocol(const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000181{
David Hendricksf73f8a72018-02-21 07:34:34 -0800182 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000183 switch (dp_data->devicetype) {
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000184 case DEV_SF100:
Jay Thompsoncabe3202018-08-17 14:30:04 -0500185 case DEV_SF200:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000186 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 5, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800187 return PROTOCOL_V1;
188 else
189 return PROTOCOL_V2;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000190 case DEV_SF600:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000191 if (dp_data->firmwareversion < FIRMWARE_VERSION(6, 9, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800192 return PROTOCOL_V1;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000193 else if (dp_data->firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
David Hendricksf73f8a72018-02-21 07:34:34 -0800194 return PROTOCOL_V2;
195 else
196 return PROTOCOL_V3;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000197 default:
David Hendricksf73f8a72018-02-21 07:34:34 -0800198 return PROTOCOL_UNKNOWN;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000199 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000200}
Simon Glassae616512016-01-23 23:27:58 +0000201
Nico Huberd99a2bd2016-02-18 21:42:49 +0000202struct dediprog_transfer_status {
203 int error; /* OK if 0, ERROR else */
204 unsigned int queued_idx;
205 unsigned int finished_idx;
206};
207
208static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
209{
210 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
211 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
212 status->error = 1;
213 msg_perr("SPI bulk read failed!\n");
214 }
215 ++status->finished_idx;
216}
217
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000218static int dediprog_bulk_read_poll(struct libusb_context *usb_ctx,
219 const struct dediprog_transfer_status *const status,
220 const int finish)
Nico Huberd99a2bd2016-02-18 21:42:49 +0000221{
222 if (status->finished_idx >= status->queued_idx)
223 return 0;
224
225 do {
226 struct timeval timeout = { 10, 0 };
227 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
228 if (ret < 0) {
229 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
230 return 1;
231 }
232 } while (finish && (status->finished_idx < status->queued_idx));
233 return 0;
234}
235
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000236static int dediprog_read(libusb_device_handle *dediprog_handle,
237 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
238 uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000239{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000240 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
241 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000242}
243
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000244static int dediprog_write(libusb_device_handle *dediprog_handle,
245 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
246 const uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000247{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000248 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
249 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000250}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000251
Nico Huberd99a2bd2016-02-18 21:42:49 +0000252
Simon Glass25e9f402015-06-28 13:31:19 +0000253/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000254static int dediprog_set_leds(int leds, const struct dediprog_data *dp_data)
Stefan Reinauer915b8402011-01-28 09:00:15 +0000255{
Simon Glass25e9f402015-06-28 13:31:19 +0000256 if (leds < LED_NONE || leds > LED_ALL)
257 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000258
Simon Glassae616512016-01-23 23:27:58 +0000259 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
260 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000261 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000262 * bit 0 == 0: red light is on.
263 *
264 * Additionally, the command structure has changed with the "new" protocol.
265 *
266 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000267 */
Simon Glassae616512016-01-23 23:27:58 +0000268 int target_leds, ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000269 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000270 target_leds = (leds ^ 7) << 8;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000271 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000272 } else {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000273 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Simon Glassae616512016-01-23 23:27:58 +0000274 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
275 } else {
276 target_leds = leds;
277 }
278 target_leds ^= 7;
279
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000280 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000281 }
282
Stefan Reinauer915b8402011-01-28 09:00:15 +0000283 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000284 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000285 return 1;
286 }
287
Stefan Reinauer915b8402011-01-28 09:00:15 +0000288 return 0;
289}
290
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000291static int dediprog_set_spi_voltage(libusb_device_handle *dediprog_handle, int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000292{
293 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000294 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000295
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000296 switch (millivolt) {
297 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000298 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000299 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000300 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000301 case 1800:
302 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000303 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000304 case 2500:
305 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000306 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000307 case 3500:
308 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000309 break;
310 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000311 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000312 return 1;
313 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000314 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
315 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000316
Nico Huber4099a8a2012-06-16 00:02:27 +0000317 if (voltage_selector == 0) {
318 /* Wait some time as the original driver does. */
319 programmer_delay(200 * 1000);
320 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000321 ret = dediprog_write(dediprog_handle, CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000322 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000323 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
324 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000325 return 1;
326 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000327 if (voltage_selector != 0) {
328 /* Wait some time as the original driver does. */
329 programmer_delay(200 * 1000);
330 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000331 return 0;
332}
333
Nico Huber77fa67d2013-02-20 18:03:36 +0000334struct dediprog_spispeeds {
335 const char *const name;
336 const int speed;
337};
338
339static const struct dediprog_spispeeds spispeeds[] = {
340 { "24M", 0x0 },
341 { "12M", 0x2 },
342 { "8M", 0x1 },
343 { "3M", 0x3 },
344 { "2.18M", 0x4 },
345 { "1.5M", 0x5 },
346 { "750k", 0x6 },
347 { "375k", 0x7 },
348 { NULL, 0x0 },
349};
350
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000351static int dediprog_set_spi_speed(unsigned int spispeed_idx, const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000352{
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000353 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000354 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
355 return 0;
356 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000357
Simon Glass557eb4f2015-07-05 16:53:22 +0000358 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
359 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000360
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000361 int ret = dediprog_write(dp_data->handle, CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000362 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000363 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000364 return 1;
365 }
366 return 0;
367}
368
Nico Huber7eb38aa2019-03-21 15:42:54 +0100369static int prepare_rw_cmd(
Nico Huber93db6e12018-09-30 01:18:43 +0200370 struct flashctx *const flash, uint8_t *data_packet, unsigned int count,
Nico Huber477e1692019-06-18 23:56:01 +0200371 uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start, int is_read)
372{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000373 const struct dediprog_data *dp_data = flash->mst->spi.data;
374
Nico Huber477e1692019-06-18 23:56:01 +0200375 if (count >= 1 << 16) {
Nico Huberac90af62022-12-18 00:22:47 +0000376 msg_perr("%s: Unsupported transfer length of %u blocks!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200377 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber477e1692019-06-18 23:56:01 +0200378 __func__, count);
379 return 1;
380 }
381
Simon Glassae616512016-01-23 23:27:58 +0000382 /* First 5 bytes are common in both generations. */
383 data_packet[0] = count & 0xff;
384 data_packet[1] = (count >> 8) & 0xff;
385 data_packet[2] = 0; /* RFU */
386 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
387 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
388
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000389 if (protocol(dp_data) >= PROTOCOL_V2) {
Nico Huber93db6e12018-09-30 01:18:43 +0200390 if (is_read && flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
391 data_packet[3] = READ_MODE_4B_ADDR_FAST_0x0C;
392 data_packet[4] = JEDEC_READ_4BA_FAST;
393 } else if (dedi_spi_cmd == WRITE_MODE_PAGE_PGM
394 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
395 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
396 data_packet[4] = JEDEC_BYTE_PROGRAM_4BA;
397 }
398
Simon Glassae616512016-01-23 23:27:58 +0000399 *value = *idx = 0;
400 data_packet[5] = 0; /* RFU */
401 data_packet[6] = (start >> 0) & 0xff;
402 data_packet[7] = (start >> 8) & 0xff;
403 data_packet[8] = (start >> 16) & 0xff;
404 data_packet[9] = (start >> 24) & 0xff;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000405 if (protocol(dp_data) >= PROTOCOL_V3) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800406 if (is_read) {
407 data_packet[10] = 0x00; /* address length (3 or 4) */
408 data_packet[11] = 0x00; /* dummy cycle / 2 */
409 } else {
410 /* 16 LSBs and 16 HSBs of page size */
411 /* FIXME: This assumes page size of 256. */
412 data_packet[10] = 0x00;
413 data_packet[11] = 0x01;
414 data_packet[12] = 0x00;
415 data_packet[13] = 0x00;
416 }
417 }
Simon Glassae616512016-01-23 23:27:58 +0000418 } else {
Nico Huber9bb8a322022-05-24 15:07:34 +0200419 if (flash->chip->feature_bits & FEATURE_4BA_EAR_ANY) {
Nico Huber7eb38aa2019-03-21 15:42:54 +0100420 if (spi_set_extended_address(flash, start >> 24))
421 return 1;
422 } else if (start >> 24) {
423 msg_cerr("Can't handle 4-byte address with dediprog.\n");
424 return 1;
425 }
426 /*
427 * We don't know how the dediprog firmware handles 4-byte
428 * addresses. So let's not tell it what we are doing and
429 * only send the lower 3 bytes.
430 */
431 *value = start & 0xffff;
432 *idx = (start >> 16) & 0xff;
Simon Glassae616512016-01-23 23:27:58 +0000433 }
Nico Huber7eb38aa2019-03-21 15:42:54 +0100434
435 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000436}
437
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000438/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
439 * @start start address
440 * @len length
441 * @return 0 on success, 1 on failure
442 */
Simon Glassae616512016-01-23 23:27:58 +0000443static 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 +0000444{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000445 int err = 1;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000446 const struct dediprog_data *dp_data = flash->mst->spi.data;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000447
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000448 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000449 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000450 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000451
Nico Huberd99a2bd2016-02-18 21:42:49 +0000452 struct dediprog_transfer_status status = { 0, 0, 0 };
453 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
454 struct libusb_transfer *transfer;
455
Nico Huberbbaa1712018-12-05 13:26:20 +0100456 if (len == 0)
457 return 0;
458
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000459 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000460 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200461 "Please report a bug at flashprog@flashprog.org\n",
Simon Glassae616512016-01-23 23:27:58 +0000462 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000463 return 1;
464 }
465
David Hendricksf73f8a72018-02-21 07:34:34 -0800466 int command_packet_size;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000467 switch (protocol(dp_data)) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800468 case PROTOCOL_V1:
469 command_packet_size = 5;
470 break;
471 case PROTOCOL_V2:
472 command_packet_size = 10;
473 break;
474 case PROTOCOL_V3:
475 command_packet_size = 12;
476 break;
477 default:
478 return 1;
479 }
480
481 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000482 unsigned int value, idx;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100483 if (prepare_rw_cmd(flash, data_packet, count, READ_MODE_STD, &value, &idx, start, 1))
484 return 1;
Simon Glassae616512016-01-23 23:27:58 +0000485
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000486 int ret = dediprog_write(dp_data->handle, CMD_READ, value, idx, data_packet, sizeof(data_packet));
Nico Huber519be662018-12-23 20:03:35 +0100487 if (ret != (int)sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000488 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000489 return 1;
490 }
491
Nico Huberd99a2bd2016-02-18 21:42:49 +0000492 /*
493 * Ring buffer of bulk transfers.
494 * Poll until at least one transfer is ready,
495 * schedule next transfers until buffer is full.
496 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000497
Nico Huberd99a2bd2016-02-18 21:42:49 +0000498 /* Allocate bulk transfers. */
499 unsigned int i;
Nico Huber519be662018-12-23 20:03:35 +0100500 for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000501 transfers[i] = libusb_alloc_transfer(0);
502 if (!transfers[i]) {
503 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
504 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200505 }
506 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000507
508 /* Now transfer requested chunks using libusb's asynchronous interface. */
509 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200510 while ((status.queued_idx < count) &&
511 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
512 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000513 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000514 libusb_fill_bulk_transfer(transfer, dp_data->handle, 0x80 | dp_data->in_endpoint,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000515 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
516 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
517 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
518 ret = libusb_submit_transfer(transfer);
519 if (ret < 0) {
520 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
521 status.queued_idx, libusb_error_name(ret));
522 goto err_free;
523 }
524 ++status.queued_idx;
525 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000526 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 0))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000527 goto err_free;
528 }
529 /* Wait for transfers to finish. */
Rick Altherr4f9cd8b2021-12-13 17:10:00 -0800530 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000531 goto err_free;
532 /* Check if everything has been transmitted. */
533 if ((status.finished_idx < count) || status.error)
534 goto err_free;
535
536 err = 0;
537
538err_free:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000539 dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000540 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
541 if (transfers[i]) libusb_free_transfer(transfers[i]);
542 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000543}
544
Simon Glassae616512016-01-23 23:27:58 +0000545static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000546{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000547 int ret;
548 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000549 const unsigned int chunksize = 0x200;
Nico Huberbbaa1712018-12-05 13:26:20 +0100550 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000551 unsigned int bulklen;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000552 const struct dediprog_data *dp_data = flash->mst->spi.data;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000553
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000554 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000555
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000556 if (residue) {
557 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
558 start, residue);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100559 ret = default_spi_read(flash, buf, start, residue);
Simon Glass25e9f402015-06-28 13:31:19 +0000560 if (ret)
561 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000562 }
563
564 /* Round down. */
565 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000566 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000567 if (ret)
568 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000569
570 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000571 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000572 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
573 start, len);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100574 ret = default_spi_read(flash, buf + residue + bulklen,
575 start + residue + bulklen, len);
Simon Glass25e9f402015-06-28 13:31:19 +0000576 if (ret)
577 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000578 }
579
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000580 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000581 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000582err:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000583 dediprog_set_leds(LED_ERROR, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +0000584 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000585}
586
Nico Hubera4b14f72012-06-19 12:06:53 +0000587/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
588 * @chunksize length of data chunks, only 256 supported by now
589 * @start start address
590 * @len length
591 * @dedi_spi_cmd dediprog specific write command for spi bus
592 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000593 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000594static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000595 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000596{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000597 /* USB transfer size must be 512, other sizes will NOT work at all.
598 * chunksize is the real data size per USB bulk transfer. The remaining
599 * space in a USB bulk transfer must be filled with 0xff padding.
600 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000601 const unsigned int count = len / chunksize;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000602 const struct dediprog_data *dp_data = flash->mst->spi.data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000603
Nico Hubera4b14f72012-06-19 12:06:53 +0000604 /*
605 * We should change this check to
606 * chunksize > 512
607 * once we know how to handle different chunk sizes.
608 */
609 if (chunksize != 256) {
610 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200611 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000612 __func__, chunksize);
Nico Hubera4b14f72012-06-19 12:06:53 +0000613 return 1;
614 }
615
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000616 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000617 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200618 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000619 __func__, start, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000620 return 1;
621 }
622
623 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000624 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000625 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000626
David Hendricksf73f8a72018-02-21 07:34:34 -0800627 int command_packet_size;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000628 switch (protocol(dp_data)) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800629 case PROTOCOL_V1:
630 command_packet_size = 5;
631 break;
632 case PROTOCOL_V2:
633 command_packet_size = 10;
634 break;
635 case PROTOCOL_V3:
636 command_packet_size = 14;
637 break;
638 default:
639 return 1;
640 }
641
642 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000643 unsigned int value, idx;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100644 if (prepare_rw_cmd(flash, data_packet, count, dedi_spi_cmd, &value, &idx, start, 0))
645 return 1;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000646 int ret = dediprog_write(dp_data->handle, CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
Nico Huber519be662018-12-23 20:03:35 +0100647 if (ret != (int)sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000648 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000649 return 1;
650 }
651
Simon Glassae616512016-01-23 23:27:58 +0000652 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000653 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000654 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000655 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000656 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000657 int transferred;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000658 ret = libusb_bulk_transfer(dp_data->handle, dp_data->out_endpoint, usbbuf, 512, &transferred,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000659 DEFAULT_TIMEOUT);
660 if ((ret < 0) || (transferred != 512)) {
661 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000662 return 1;
663 }
664 }
665
666 return 0;
667}
668
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000669static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000670 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000671{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000672 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000673 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000674 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
675 unsigned int bulklen;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000676 const struct dediprog_data *dp_data = flash->mst->spi.data;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000677
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000678 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000679
Nico Hubera4b14f72012-06-19 12:06:53 +0000680 if (chunksize != 256) {
681 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
682 "we don't know how dediprog\nhandles them.\n");
683 /* Write everything like it was residue. */
684 residue = len;
685 }
686
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000687 if (residue) {
688 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
689 start, residue);
Nico Huber93db6e12018-09-30 01:18:43 +0200690 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
691 ret = spi_write_chunked(flash, buf, start, residue, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000692 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000693 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000694 return ret;
695 }
696 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000697
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000698 /* Round down. */
699 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000700 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000701 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000702 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000703 return ret;
704 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000705
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000706 len -= residue + bulklen;
707 if (len) {
708 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
709 start, len);
710 ret = spi_write_chunked(flash, buf + residue + bulklen,
Nico Huber93db6e12018-09-30 01:18:43 +0200711 start + residue + bulklen, len, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000712 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000713 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000714 return ret;
715 }
716 }
717
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000718 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000719 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000720}
721
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000722static 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 +0000723{
Simon Glass557eb4f2015-07-05 16:53:22 +0000724 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000725}
726
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000727static 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 +0000728{
Simon Glass557eb4f2015-07-05 16:53:22 +0000729 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000730}
731
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000732static int dediprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000733 unsigned int writecnt,
734 unsigned int readcnt,
735 const unsigned char *writearr,
736 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000737{
738 int ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000739 const struct dediprog_data *dp_data = flash->mst->spi.data;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000740
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000741 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glassae616512016-01-23 23:27:58 +0000742 if (writecnt > flash->mst->spi.max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000743 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000744 return 1;
745 }
Simon Glassae616512016-01-23 23:27:58 +0000746 if (readcnt > flash->mst->spi.max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000747 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000748 return 1;
749 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100750
Simon Glassae616512016-01-23 23:27:58 +0000751 unsigned int idx, value;
752 /* New protocol has options and timeout combined as value while the old one used the value field for
753 * timeout and the index field for options. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000754 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000755 idx = 0;
756 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
757 } else {
758 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
759 value = 0;
760 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000761 ret = dediprog_write(dp_data->handle, CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Nico Huber519be662018-12-23 20:03:35 +0100762 if (ret != (int)writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000763 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000764 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000765 return 1;
766 }
Simon Glassae616512016-01-23 23:27:58 +0000767 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000768 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000769
Stefan Tauner74367bf2016-02-20 20:53:46 +0000770 /* The specifications do state the possibility to set a timeout for transceive transactions.
771 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
772 * can predict the time needed by the previous command or so (untested). In any case, using this
773 * "feature" to set sane-looking timouts for the read below will completely trash performance with
774 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
775 *
776 * The specification also uses only 0 in its examples, so the lesson to learn here:
777 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000778 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800779 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000780 idx = 0;
781 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
782 } else {
783 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
784 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
785 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000786 ret = dediprog_read(dp_data->dediprog_handle, CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000787 */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000788 ret = dediprog_read(dp_data->handle, CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Nico Huber519be662018-12-23 20:03:35 +0100789 if (ret != (int)readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000790 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 +0000791 return 1;
792 }
793 return 0;
794}
795
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000796static int dediprog_check_devicestring(struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000797{
798 int ret;
799 char buf[0x11];
800
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000801 /* Command Receive Device String. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000802 ret = dediprog_read(dp_data->handle, CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000803 if (ret != 0x10) {
804 msg_perr("Incomplete/failed Command Receive Device String!\n");
805 return 1;
806 }
807 buf[0x10] = '\0';
808 msg_pdbg("Found a %s\n", buf);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000809 if (memcmp(buf, "SF100", 0x5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000810 dp_data->devicetype = DEV_SF100;
Jay Thompsoncabe3202018-08-17 14:30:04 -0500811 else if (memcmp(buf, "SF200", 0x5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000812 dp_data->devicetype = DEV_SF200;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000813 else if (memcmp(buf, "SF600", 0x5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000814 dp_data->devicetype = DEV_SF600;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000815 else {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500816 msg_perr("Device not a SF100, SF200, or SF600!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000817 return 1;
818 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000819
820 int sfnum;
821 int fw[3];
822 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000823 sfnum != (int)dp_data->devicetype) {
Simon Glassae616512016-01-23 23:27:58 +0000824 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000825 return 1;
826 }
Simon Glassae616512016-01-23 23:27:58 +0000827 /* Only these major versions were tested. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000828 if (fw[0] < 2 || fw[0] > 7) {
Simon Glassae616512016-01-23 23:27:58 +0000829 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000830 return 1;
831 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800832
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000833 dp_data->firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000834 if (protocol(dp_data) == PROTOCOL_UNKNOWN) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800835 msg_perr("Internal error: Unable to determine protocol version.\n");
836 return 1;
837 }
Simon Glassae616512016-01-23 23:27:58 +0000838
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000839 return 0;
840}
841
David Hendricksc05900f2016-02-18 21:42:41 +0000842/*
Ryan O'Leary301ae222019-06-24 19:14:33 -0700843 * Read the id from the dediprog. This should return the numeric part of the
844 * serial number found on a sticker on the back of the dediprog. Note this
845 * number is stored in writable eeprom, so it could get out of sync. Also note,
846 * this function only supports SF100 at this time, but SF600 support is not too
847 * much different.
848 * @return the id on success, -1 on failure
849 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000850static int dediprog_read_id(libusb_device_handle *dediprog_handle)
Ryan O'Leary301ae222019-06-24 19:14:33 -0700851{
852 int ret;
853 uint8_t buf[3];
854
855 ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN,
856 0x7, /* request */
857 0, /* value */
858 0xEF00, /* index */
859 buf, sizeof(buf),
860 DEFAULT_TIMEOUT);
861 if (ret != sizeof(buf)) {
862 msg_perr("Failed to read dediprog id, error %d!\n", ret);
863 return -1;
864 }
865
866 return buf[0] << 16 | buf[1] << 8 | buf[2];
867}
868
869/*
David Hendricksc05900f2016-02-18 21:42:41 +0000870 * This command presumably sets the voltage for the SF100 itself (not the
871 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
872 * (including all SF600s) do not support it.
873 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000874
David Hendricksc05900f2016-02-18 21:42:41 +0000875/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
876 * Only use dediprog_set_voltage on SF100 programmers with firmware older
877 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000878static int dediprog_set_voltage(libusb_device_handle *dediprog_handle)
David Hendricksc05900f2016-02-18 21:42:41 +0000879{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000880 unsigned char buf[1] = {0};
881 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000882 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000883 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000884 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000885 return 1;
886 }
David Hendricksc05900f2016-02-18 21:42:41 +0000887 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000888 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000889 return 1;
890 }
David Hendricksc05900f2016-02-18 21:42:41 +0000891
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000892 return 0;
893}
894
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000895static int dediprog_standalone_mode(const struct dediprog_data *dp_data)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000896{
897 int ret;
898
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000899 if (dp_data->devicetype != DEV_SF600)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000900 return 0;
901
902 msg_pdbg2("Disabling standalone mode.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000903 ret = dediprog_write(dp_data->handle, CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000904 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000905 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000906 return 1;
907 }
908
909 return 0;
910}
911
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000912#if 0
913/* Something.
914 * Present in eng_detect_blink.log with firmware 3.1.8
915 * Always preceded by Command Receive Device String
916 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000917static int dediprog_command_b(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000918{
919 int ret;
920 char buf[0x3];
921
Simon Glass557eb4f2015-07-05 16:53:22 +0000922 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
923 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000924 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000925 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000926 return 1;
927 }
928 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
929 (buf[2] != 0xff)) {
930 msg_perr("Unexpected response to Command B!\n");
931 return 1;
932 }
933
934 return 0;
935}
936#endif
937
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000938static int set_target_flash(libusb_device_handle *dediprog_handle, enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000939{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000940 int ret = dediprog_write(dediprog_handle, CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +0000941 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000942 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000943 return 1;
944 }
945 return 0;
946}
947
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000948#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000949/* Returns true if the button is currently pressed. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000950static bool dediprog_get_button(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000951{
Simon Glass557eb4f2015-07-05 16:53:22 +0000952 char buf[1];
953 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
954 buf, 0x1, DEFAULT_TIMEOUT);
955 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000956 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000957 return 1;
958 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000959 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000960}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000961#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000962
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000963static int parse_voltage(char *voltage)
964{
965 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000966 int i;
967 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000968
969 if (!voltage || !strlen(voltage)) {
970 msg_perr("Empty voltage= specified.\n");
971 return -1;
972 }
973 millivolt = (int)strtol(voltage, &tmp, 0);
974 voltage = tmp;
975 /* Handle "," and "." as decimal point. Everything after it is assumed
976 * to be in decimal notation.
977 */
978 if ((*voltage == '.') || (*voltage == ',')) {
979 voltage++;
980 for (i = 0; i < 3; i++) {
981 fraction *= 10;
982 /* Don't advance if the current character is invalid,
983 * but continue multiplying.
984 */
985 if ((*voltage < '0') || (*voltage > '9'))
986 continue;
987 fraction += *voltage - '0';
988 voltage++;
989 }
990 /* Throw away remaining digits. */
991 voltage += strspn(voltage, "0123456789");
992 }
993 /* The remaining string must be empty or "mV" or "V". */
994 tolower_string(voltage);
995
996 /* No unit or "V". */
997 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
998 millivolt *= 1000;
999 millivolt += fraction;
1000 } else if (!strncmp(voltage, "mv", 2) ||
1001 !strncmp(voltage, "milliv", 6)) {
1002 /* No adjustment. fraction is discarded. */
1003 } else {
1004 /* Garbage at the end of the string. */
1005 msg_perr("Garbage voltage= specified.\n");
1006 return -1;
1007 }
1008 return millivolt;
1009}
1010
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001011static int dediprog_shutdown(void *data);
1012
Nico Huber93db6e12018-09-30 01:18:43 +02001013static struct spi_master spi_master_dediprog = {
Nico Huberdc5af542018-12-22 16:54:59 +01001014 .features = SPI_MASTER_NO_4BA_MODES,
Simon Glassae616512016-01-23 23:27:58 +00001015 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
1016 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001017 .command = dediprog_spi_send_command,
1018 .multicommand = default_spi_send_multicommand,
1019 .read = dediprog_spi_read,
1020 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +00001021 .write_aai = dediprog_spi_write_aai,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001022 .shutdown = dediprog_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301023 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001024};
1025
Ryan O'Leary301ae222019-06-24 19:14:33 -07001026/*
1027 * Open a dediprog_handle with the USB device at the given index.
1028 * @index index of the USB device
1029 * @return 0 for success, -1 for error, -2 for busy device
1030 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001031static int dediprog_open(int index, struct dediprog_data *dp_data)
Ryan O'Leary301ae222019-06-24 19:14:33 -07001032{
1033 const uint16_t vid = devs_dediprog[0].vendor_id;
1034 const uint16_t pid = devs_dediprog[0].device_id;
1035 int ret;
1036
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001037 dp_data->handle = usb_dev_get_by_vid_pid_number(dp_data->usb_ctx, vid, pid, (unsigned int) index);
1038 if (!dp_data->handle) {
Ryan O'Leary301ae222019-06-24 19:14:33 -07001039 msg_perr("Could not find a Dediprog programmer on USB.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001040 libusb_exit(dp_data->usb_ctx);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001041 return -1;
1042 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001043 ret = libusb_set_configuration(dp_data->handle, 1);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001044 if (ret != 0) {
1045 msg_perr("Could not set USB device configuration: %i %s\n",
1046 ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001047 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001048 return -2;
1049 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001050 ret = libusb_claim_interface(dp_data->handle, 0);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001051 if (ret < 0) {
1052 msg_perr("Could not claim USB device interface %i: %i %s\n",
1053 0, ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001054 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001055 return -2;
1056 }
1057 return 0;
1058}
1059
David Hendricks8bb20212011-06-14 01:35:36 +00001060static int dediprog_shutdown(void *data)
1061{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001062 int ret = 0;
1063 struct dediprog_data *dp_data = data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001064
David Hendricks8bb20212011-06-14 01:35:36 +00001065 /* URB 28. Command Set SPI Voltage to 0. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001066 if (dediprog_set_spi_voltage(dp_data->handle, 0x0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001067 ret = 1;
1068 goto out;
David Hendricks8bb20212011-06-14 01:35:36 +00001069 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001070
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001071 if (libusb_release_interface(dp_data->handle, 0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001072 msg_perr("Could not release USB interface!\n");
1073 ret = 1;
1074 goto out;
1075 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001076 libusb_close(dp_data->handle);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001077 libusb_exit(dp_data->usb_ctx);
1078out:
1079 free(data);
1080 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +00001081}
1082
Thomas Heijligencc853d82021-05-04 15:32:17 +02001083static int dediprog_init(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001084{
Ryan O'Leary301ae222019-06-24 19:14:33 -07001085 char *voltage, *id_str, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +00001086 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001087 int millivolt = 3500;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001088 int id = -1; /* -1 defaults to enumeration order */
1089 int found_id;
Nathan Laredo21541a62012-12-24 22:07:36 +00001090 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +02001091 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +00001092 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001093
Nico Huber77fa67d2013-02-20 18:03:36 +00001094 spispeed = extract_programmer_param("spispeed");
1095 if (spispeed) {
1096 for (i = 0; spispeeds[i].name; ++i) {
1097 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1098 spispeed_idx = i;
1099 break;
1100 }
1101 }
1102 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +00001103 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +00001104 free(spispeed);
1105 return 1;
1106 }
1107 free(spispeed);
1108 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001109
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001110 voltage = extract_programmer_param("voltage");
1111 if (voltage) {
1112 millivolt = parse_voltage(voltage);
1113 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001114 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001115 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001116 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1117 }
1118
Ryan O'Leary301ae222019-06-24 19:14:33 -07001119 id_str = extract_programmer_param("id");
1120 if (id_str) {
1121 char prefix0, prefix1;
1122 if (sscanf(id_str, "%c%c%d", &prefix0, &prefix1, &id) != 3) {
1123 msg_perr("Error: Could not parse dediprog 'id'.\n");
1124 msg_perr("Expected a string like SF012345 or DP012345.\n");
1125 free(id_str);
1126 return 1;
1127 }
1128 if (id < 0 || id >= 0x1000000) {
1129 msg_perr("Error: id %s is out of range!\n", id_str);
1130 free(id_str);
1131 return 1;
1132 }
1133 if (!(prefix0 == 'S' && prefix1 == 'F') && !(prefix0 == 'D' && prefix1 == 'P')) {
1134 msg_perr("Error: %s is an invalid id!\n", id_str);
1135 free(id_str);
1136 return 1;
1137 }
1138 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1139 }
1140 free(id_str);
1141
Nathan Laredo21541a62012-12-24 22:07:36 +00001142 device = extract_programmer_param("device");
1143 if (device) {
1144 char *dev_suffix;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001145 if (id != -1) {
1146 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1147 }
Nathan Laredo21541a62012-12-24 22:07:36 +00001148 errno = 0;
1149 usedevice = strtol(device, &dev_suffix, 10);
1150 if (errno != 0 || device == dev_suffix) {
1151 msg_perr("Error: Could not convert 'device'.\n");
1152 free(device);
1153 return 1;
1154 }
Nico Huber961f4a12019-10-04 17:34:22 +02001155 if (usedevice < 0 || usedevice > INT_MAX) {
Nathan Laredo21541a62012-12-24 22:07:36 +00001156 msg_perr("Error: Value for 'device' is out of range.\n");
1157 free(device);
1158 return 1;
1159 }
1160 if (strlen(dev_suffix) > 0) {
1161 msg_perr("Error: Garbage following 'device' value.\n");
1162 free(device);
1163 return 1;
1164 }
1165 msg_pinfo("Using device %li.\n", usedevice);
1166 }
1167 free(device);
1168
Stefan Taunere659d2d2013-05-03 21:58:28 +00001169 target_str = extract_programmer_param("target");
1170 if (target_str) {
1171 char *target_suffix;
1172 errno = 0;
1173 target = strtol(target_str, &target_suffix, 10);
1174 if (errno != 0 || target_str == target_suffix) {
1175 msg_perr("Error: Could not convert 'target'.\n");
1176 free(target_str);
1177 return 1;
1178 }
1179 if (target < 1 || target > 2) {
1180 msg_perr("Error: Value for 'target' is out of range.\n");
1181 free(target_str);
1182 return 1;
1183 }
1184 if (strlen(target_suffix) > 0) {
1185 msg_perr("Error: Garbage following 'target' value.\n");
1186 free(target_str);
1187 return 1;
1188 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001189 switch (target) {
1190 case 1:
1191 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1192 target = FLASH_TYPE_APPLICATION_FLASH_1;
1193 break;
1194 case 2:
1195 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1196 target = FLASH_TYPE_APPLICATION_FLASH_2;
1197 break;
1198 default:
1199 break;
1200 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001201 }
1202 free(target_str);
1203
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001204 struct dediprog_data *dp_data = calloc(1, sizeof(*dp_data));
1205 if (!dp_data) {
1206 msg_perr("Unable to allocate space for SPI master data\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001207 return 1;
1208 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001209 dp_data->firmwareversion = FIRMWARE_VERSION(0, 0, 0);
1210 dp_data->devicetype = DEV_UNKNOWN;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001211
1212 /* Here comes the USB stuff. */
Thomas Heijligen88e87c52022-08-05 17:56:20 +02001213 ret = libusb_init(&dp_data->usb_ctx);
1214 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001215 msg_perr("Could not initialize libusb!\n");
1216 goto init_err_exit;
1217 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001218
Ryan O'Leary301ae222019-06-24 19:14:33 -07001219 if (id != -1) {
1220 for (i = 0; ; i++) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001221 ret = dediprog_open(i, dp_data);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001222 if (ret == -1) {
1223 /* no dev */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001224 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001225 } else if (ret == -2) {
1226 /* busy dev */
1227 continue;
1228 }
1229
1230 /* Notice we can only call dediprog_read_id() after
1231 * libusb_set_configuration() and
1232 * libusb_claim_interface(). When searching by id and
1233 * either configuration or claim fails (usually the
Nico Huberc3b02dc2023-08-12 01:13:45 +02001234 * device is in use by another instance of flashprog),
Ryan O'Leary301ae222019-06-24 19:14:33 -07001235 * the device is skipped and the next device is tried.
1236 */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001237 found_id = dediprog_read_id(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001238 if (found_id < 0) {
1239 msg_perr("Could not read id.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001240 libusb_release_interface(dp_data->handle, 0);
1241 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001242 continue;
1243 }
1244 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1245 if (found_id != id) {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001246 libusb_release_interface(dp_data->handle, 0);
1247 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001248 continue;
1249 }
1250 break;
1251 }
1252 } else {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001253 if (dediprog_open(usedevice, dp_data)) {
1254 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001255 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001256 found_id = dediprog_read_id(dp_data->handle);
David Woodhoused2a7e872013-07-30 09:34:44 +00001257 }
Ryan O'Leary301ae222019-06-24 19:14:33 -07001258
1259 if (found_id >= 0) {
1260 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001261 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001262
David Hendricksc05900f2016-02-18 21:42:41 +00001263 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1264 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001265 if (dediprog_check_devicestring(dp_data)) {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001266 if (dediprog_set_voltage(dp_data->handle))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001267 goto init_err_cleanup_exit;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001268 if (dediprog_check_devicestring(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001269 goto init_err_cleanup_exit;
David Hendricksc05900f2016-02-18 21:42:41 +00001270 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001271
Jay Thompsoncabe3202018-08-17 14:30:04 -05001272 /* SF100/SF200 uses one in/out endpoint, SF600 uses separate in/out endpoints */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001273 dp_data->in_endpoint = 2;
1274 switch (dp_data->devicetype) {
Jay Thompsoncabe3202018-08-17 14:30:04 -05001275 case DEV_SF100:
1276 case DEV_SF200:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001277 dp_data->out_endpoint = 2;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001278 break;
1279 default:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001280 dp_data->out_endpoint = 1;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001281 break;
1282 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001283
Simon Glass25e9f402015-06-28 13:31:19 +00001284 /* Set all possible LEDs as soon as possible to indicate activity.
1285 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001286 * dediprog_check_devicestring() has queried the device. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001287 dediprog_set_leds(LED_ALL, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +00001288
Simon Glass557eb4f2015-07-05 16:53:22 +00001289 /* Select target/socket, frequency and VCC. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001290 if (set_target_flash(dp_data->handle, target) ||
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001291 dediprog_set_spi_speed(spispeed_idx, dp_data) ||
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001292 dediprog_set_spi_voltage(dp_data->handle, millivolt)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001293 dediprog_set_leds(LED_ERROR, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001294 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001295 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001296
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001297 if (dediprog_standalone_mode(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001298 goto init_err_cleanup_exit;
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001299
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001300 if ((dp_data->devicetype == DEV_SF100) ||
1301 (dp_data->devicetype == DEV_SF600 && protocol(dp_data) == PROTOCOL_V3))
Nico Huber7eb38aa2019-03-21 15:42:54 +01001302 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1303
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001304 if (protocol(dp_data) >= PROTOCOL_V2)
Nico Huber93db6e12018-09-30 01:18:43 +02001305 spi_master_dediprog.features |= SPI_MASTER_4BA;
1306
Angel Pons116d56d2021-03-22 11:28:00 +01001307 if (dediprog_set_leds(LED_NONE, dp_data))
1308 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001309
Nico Huber03f3a6d2021-05-11 17:53:34 +02001310 return register_spi_master(&spi_master_dediprog, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001311
1312init_err_cleanup_exit:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001313 dediprog_shutdown(dp_data);
1314 return 1;
1315
1316init_err_exit:
1317 free(dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001318 return 1;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001319}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001320
1321const struct programmer_entry programmer_dediprog = {
1322 .name = "dediprog",
1323 .type = USB,
1324 .devs.dev = devs_dediprog,
1325 .init = dediprog_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001326};