blob: c7b4ae8edf246c08c71dd3c853df1d15fd9cfd94 [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,
51};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000052
Simon Glass25e9f402015-06-28 13:31:19 +000053enum dediprog_leds {
54 LED_INVALID = -1,
55 LED_NONE = 0,
56 LED_PASS = 1 << 0,
57 LED_BUSY = 1 << 1,
58 LED_ERROR = 1 << 2,
59 LED_ALL = 7,
60};
61
Simon Glass557eb4f2015-07-05 16:53:22 +000062/* IO bits for CMD_SET_IO_LED message */
63enum dediprog_ios {
64 IO1 = 1 << 0,
65 IO2 = 1 << 1,
66 IO3 = 1 << 2,
67 IO4 = 1 << 3,
68};
69
70enum dediprog_cmds {
71 CMD_TRANSCEIVE = 0x01,
72 CMD_POLL_STATUS_REG = 0x02,
73 CMD_SET_VPP = 0x03,
74 CMD_SET_TARGET = 0x04,
75 CMD_READ_EEPROM = 0x05,
76 CMD_WRITE_EEPROM = 0x06,
77 CMD_SET_IO_LED = 0x07,
78 CMD_READ_PROG_INFO = 0x08,
79 CMD_SET_VCC = 0x09,
80 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000081 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000082 CMD_GET_BUTTON = 0x11,
83 CMD_GET_UID = 0x12,
84 CMD_SET_CS = 0x14,
85 CMD_IO_MODE = 0x15,
86 CMD_FW_UPDATE = 0x1A,
87 CMD_FPGA_UPDATE = 0x1B,
88 CMD_READ_FPGA_VERSION = 0x1C,
89 CMD_SET_HOLD = 0x1D,
90 CMD_READ = 0x20,
91 CMD_WRITE = 0x30,
92 CMD_WRITE_AT45DB = 0x31,
93 CMD_NAND_WRITE = 0x32,
94 CMD_NAND_READ = 0x33,
95 CMD_SET_SPI_CLK = 0x61,
96 CMD_CHECK_SOCKET = 0x62,
97 CMD_DOWNLOAD_PRJ = 0x63,
98 CMD_READ_PRJ_NAME = 0x64,
99 // New protocol/firmware only
100 CMD_CHECK_SDCARD = 0x65,
101 CMD_READ_PRJ = 0x66,
102};
103
104enum dediprog_target {
105 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
106 FLASH_TYPE_FLASH_CARD,
107 FLASH_TYPE_APPLICATION_FLASH_2,
108 FLASH_TYPE_SOCKET,
109};
110
111enum dediprog_readmode {
112 READ_MODE_STD = 1,
113 READ_MODE_FAST = 2,
114 READ_MODE_ATMEL45 = 3,
115 READ_MODE_4B_ADDR_FAST = 4,
116 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
117};
118
119enum dediprog_writemode {
Elyes HAOUASac01baa2018-05-28 16:52:21 +0200120 WRITE_MODE_PAGE_PGM = 1,
Simon Glass557eb4f2015-07-05 16:53:22 +0000121 WRITE_MODE_PAGE_WRITE = 2,
122 WRITE_MODE_1B_AAI = 3,
123 WRITE_MODE_2B_AAI = 4,
124 WRITE_MODE_128B_PAGE = 5,
125 WRITE_MODE_PAGE_AT26DF041 = 6,
126 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000127 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000128 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000129 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000130 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
131 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
132};
133
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000134enum dediprog_standalone_mode {
135 ENTER_STANDALONE_MODE = 0,
136 LEAVE_STANDALONE_MODE = 1,
137};
138
David Hendricksf73f8a72018-02-21 07:34:34 -0800139/*
Nico Huberc3b02dc2023-08-12 01:13:45 +0200140 * These are not official designations; they are for use in flashprog only.
David Hendricksf73f8a72018-02-21 07:34:34 -0800141 * Order must be preserved so that comparison operators work.
142 */
143enum protocol {
144 PROTOCOL_UNKNOWN,
145 PROTOCOL_V1,
146 PROTOCOL_V2,
147 PROTOCOL_V3,
148};
149
Thomas Heijligencc853d82021-05-04 15:32:17 +0200150static const struct dev_entry devs_dediprog[] = {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500151 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
Stefan Taunerfdec7472016-02-22 08:59:27 +0000152
153 {0},
154};
Simon Glass557eb4f2015-07-05 16:53:22 +0000155
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000156struct dediprog_data {
157 struct libusb_context *usb_ctx;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000158 libusb_device_handle *handle;
159 int in_endpoint;
160 int out_endpoint;
161 int firmwareversion;
162 enum dediprog_devtype devicetype;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000163};
Simon Glass557eb4f2015-07-05 16:53:22 +0000164
Nico Huberd99a2bd2016-02-18 21:42:49 +0000165#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
166 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
167/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
168const char * LIBUSB_CALL libusb_error_name(int error_code)
169{
170 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
171 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
172 static char my_libusb_error[18 + 5 + 2];
173 sprintf(my_libusb_error, "libusb error code %i", error_code);
174 return my_libusb_error;
175 } else {
176 return "UNKNOWN";
177 }
178}
179#endif
180
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000181static enum protocol protocol(const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000182{
David Hendricksf73f8a72018-02-21 07:34:34 -0800183 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000184 switch (dp_data->devicetype) {
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000185 case DEV_SF100:
Jay Thompsoncabe3202018-08-17 14:30:04 -0500186 case DEV_SF200:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000187 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 5, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800188 return PROTOCOL_V1;
189 else
190 return PROTOCOL_V2;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000191 case DEV_SF600:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000192 if (dp_data->firmwareversion < FIRMWARE_VERSION(6, 9, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800193 return PROTOCOL_V1;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000194 else if (dp_data->firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
David Hendricksf73f8a72018-02-21 07:34:34 -0800195 return PROTOCOL_V2;
196 else
197 return PROTOCOL_V3;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000198 default:
David Hendricksf73f8a72018-02-21 07:34:34 -0800199 return PROTOCOL_UNKNOWN;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000200 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000201}
Simon Glassae616512016-01-23 23:27:58 +0000202
Nico Huberd99a2bd2016-02-18 21:42:49 +0000203struct dediprog_transfer_status {
204 int error; /* OK if 0, ERROR else */
205 unsigned int queued_idx;
206 unsigned int finished_idx;
207};
208
209static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
210{
211 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
212 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
213 status->error = 1;
214 msg_perr("SPI bulk read failed!\n");
215 }
216 ++status->finished_idx;
217}
218
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000219static int dediprog_bulk_read_poll(struct libusb_context *usb_ctx,
220 const struct dediprog_transfer_status *const status,
221 const int finish)
Nico Huberd99a2bd2016-02-18 21:42:49 +0000222{
223 if (status->finished_idx >= status->queued_idx)
224 return 0;
225
226 do {
227 struct timeval timeout = { 10, 0 };
228 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
229 if (ret < 0) {
230 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
231 return 1;
232 }
233 } while (finish && (status->finished_idx < status->queued_idx));
234 return 0;
235}
236
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000237static int dediprog_read(libusb_device_handle *dediprog_handle,
238 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
239 uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000240{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000241 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
242 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000243}
244
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000245static int dediprog_write(libusb_device_handle *dediprog_handle,
246 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
247 const uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000248{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000249 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
250 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000251}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000252
Nico Huberd99a2bd2016-02-18 21:42:49 +0000253
Simon Glass25e9f402015-06-28 13:31:19 +0000254/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000255static int dediprog_set_leds(int leds, const struct dediprog_data *dp_data)
Stefan Reinauer915b8402011-01-28 09:00:15 +0000256{
Simon Glass25e9f402015-06-28 13:31:19 +0000257 if (leds < LED_NONE || leds > LED_ALL)
258 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000259
Simon Glassae616512016-01-23 23:27:58 +0000260 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
261 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000262 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000263 * bit 0 == 0: red light is on.
264 *
265 * Additionally, the command structure has changed with the "new" protocol.
266 *
267 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000268 */
Simon Glassae616512016-01-23 23:27:58 +0000269 int target_leds, ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000270 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000271 target_leds = (leds ^ 7) << 8;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000272 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000273 } else {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000274 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Simon Glassae616512016-01-23 23:27:58 +0000275 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
276 } else {
277 target_leds = leds;
278 }
279 target_leds ^= 7;
280
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000281 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000282 }
283
Stefan Reinauer915b8402011-01-28 09:00:15 +0000284 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000285 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000286 return 1;
287 }
288
Stefan Reinauer915b8402011-01-28 09:00:15 +0000289 return 0;
290}
291
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000292static int dediprog_set_spi_voltage(libusb_device_handle *dediprog_handle, int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000293{
294 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000295 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000296
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000297 switch (millivolt) {
298 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000299 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000300 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000301 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000302 case 1800:
303 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000304 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000305 case 2500:
306 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000307 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000308 case 3500:
309 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000310 break;
311 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000312 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000313 return 1;
314 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000315 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
316 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000317
Nico Huber4099a8a2012-06-16 00:02:27 +0000318 if (voltage_selector == 0) {
319 /* Wait some time as the original driver does. */
320 programmer_delay(200 * 1000);
321 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000322 ret = dediprog_write(dediprog_handle, CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000323 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000324 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
325 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000326 return 1;
327 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000328 if (voltage_selector != 0) {
329 /* Wait some time as the original driver does. */
330 programmer_delay(200 * 1000);
331 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000332 return 0;
333}
334
Nico Huber77fa67d2013-02-20 18:03:36 +0000335struct dediprog_spispeeds {
336 const char *const name;
337 const int speed;
338};
339
340static const struct dediprog_spispeeds spispeeds[] = {
341 { "24M", 0x0 },
342 { "12M", 0x2 },
343 { "8M", 0x1 },
344 { "3M", 0x3 },
345 { "2.18M", 0x4 },
346 { "1.5M", 0x5 },
347 { "750k", 0x6 },
348 { "375k", 0x7 },
349 { NULL, 0x0 },
350};
351
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000352static int dediprog_set_spi_speed(unsigned int spispeed_idx, const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000353{
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000354 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000355 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
356 return 0;
357 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000358
Simon Glass557eb4f2015-07-05 16:53:22 +0000359 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
360 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000361
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000362 int ret = dediprog_write(dp_data->handle, CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000363 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000364 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000365 return 1;
366 }
367 return 0;
368}
369
Nico Huber7eb38aa2019-03-21 15:42:54 +0100370static int prepare_rw_cmd(
Nico Huber93db6e12018-09-30 01:18:43 +0200371 struct flashctx *const flash, uint8_t *data_packet, unsigned int count,
Nico Huber477e1692019-06-18 23:56:01 +0200372 uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start, int is_read)
373{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100374 const struct dediprog_data *dp_data = flash->mst.spi->data;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000375
Nico Hubera96aaa32024-03-05 12:56:13 +0100376 if (count > MAX_BLOCK_COUNT) {
Nico Huberac90af62022-12-18 00:22:47 +0000377 msg_perr("%s: Unsupported transfer length of %u blocks!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200378 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber477e1692019-06-18 23:56:01 +0200379 __func__, count);
380 return 1;
381 }
382
Simon Glassae616512016-01-23 23:27:58 +0000383 /* First 5 bytes are common in both generations. */
384 data_packet[0] = count & 0xff;
385 data_packet[1] = (count >> 8) & 0xff;
386 data_packet[2] = 0; /* RFU */
387 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
388 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
389
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000390 if (protocol(dp_data) >= PROTOCOL_V2) {
Nico Huber93db6e12018-09-30 01:18:43 +0200391 if (is_read && flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
392 data_packet[3] = READ_MODE_4B_ADDR_FAST_0x0C;
393 data_packet[4] = JEDEC_READ_4BA_FAST;
394 } else if (dedi_spi_cmd == WRITE_MODE_PAGE_PGM
395 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
Nico Huberfb176d22024-03-25 15:55:13 +0100396 if (protocol(dp_data) >= PROTOCOL_V3)
397 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM;
398 else
399 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
Nico Huber93db6e12018-09-30 01:18:43 +0200400 data_packet[4] = JEDEC_BYTE_PROGRAM_4BA;
401 }
402
Simon Glassae616512016-01-23 23:27:58 +0000403 *value = *idx = 0;
404 data_packet[5] = 0; /* RFU */
405 data_packet[6] = (start >> 0) & 0xff;
406 data_packet[7] = (start >> 8) & 0xff;
407 data_packet[8] = (start >> 16) & 0xff;
408 data_packet[9] = (start >> 24) & 0xff;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000409 if (protocol(dp_data) >= PROTOCOL_V3) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800410 if (is_read) {
411 data_packet[10] = 0x00; /* address length (3 or 4) */
412 data_packet[11] = 0x00; /* dummy cycle / 2 */
413 } else {
414 /* 16 LSBs and 16 HSBs of page size */
415 /* FIXME: This assumes page size of 256. */
416 data_packet[10] = 0x00;
417 data_packet[11] = 0x01;
418 data_packet[12] = 0x00;
419 data_packet[13] = 0x00;
420 }
421 }
Simon Glassae616512016-01-23 23:27:58 +0000422 } else {
Nico Huber9bb8a322022-05-24 15:07:34 +0200423 if (flash->chip->feature_bits & FEATURE_4BA_EAR_ANY) {
Nico Huber7eb38aa2019-03-21 15:42:54 +0100424 if (spi_set_extended_address(flash, start >> 24))
425 return 1;
426 } else if (start >> 24) {
427 msg_cerr("Can't handle 4-byte address with dediprog.\n");
428 return 1;
429 }
430 /*
431 * We don't know how the dediprog firmware handles 4-byte
432 * addresses. So let's not tell it what we are doing and
433 * only send the lower 3 bytes.
434 */
435 *value = start & 0xffff;
436 *idx = (start >> 16) & 0xff;
Simon Glassae616512016-01-23 23:27:58 +0000437 }
Nico Huber7eb38aa2019-03-21 15:42:54 +0100438
439 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000440}
441
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000442/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
443 * @start start address
444 * @len length
445 * @return 0 on success, 1 on failure
446 */
Simon Glassae616512016-01-23 23:27:58 +0000447static 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 +0000448{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000449 int err = 1;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100450 const struct dediprog_data *dp_data = flash->mst.spi->data;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000451
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000452 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000453 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000454 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000455
Nico Huberd99a2bd2016-02-18 21:42:49 +0000456 struct dediprog_transfer_status status = { 0, 0, 0 };
457 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
458 struct libusb_transfer *transfer;
459
Nico Huberbbaa1712018-12-05 13:26:20 +0100460 if (len == 0)
461 return 0;
462
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000463 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000464 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200465 "Please report a bug at flashprog@flashprog.org\n",
Simon Glassae616512016-01-23 23:27:58 +0000466 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000467 return 1;
468 }
469
David Hendricksf73f8a72018-02-21 07:34:34 -0800470 int command_packet_size;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000471 switch (protocol(dp_data)) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800472 case PROTOCOL_V1:
473 command_packet_size = 5;
474 break;
475 case PROTOCOL_V2:
476 command_packet_size = 10;
477 break;
478 case PROTOCOL_V3:
479 command_packet_size = 12;
480 break;
481 default:
482 return 1;
483 }
484
485 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000486 unsigned int value, idx;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100487 if (prepare_rw_cmd(flash, data_packet, count, READ_MODE_STD, &value, &idx, start, 1))
488 return 1;
Simon Glassae616512016-01-23 23:27:58 +0000489
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000490 int ret = dediprog_write(dp_data->handle, CMD_READ, value, idx, data_packet, sizeof(data_packet));
Nico Huber519be662018-12-23 20:03:35 +0100491 if (ret != (int)sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000492 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000493 return 1;
494 }
495
Nico Huberd99a2bd2016-02-18 21:42:49 +0000496 /*
497 * Ring buffer of bulk transfers.
498 * Poll until at least one transfer is ready,
499 * schedule next transfers until buffer is full.
500 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000501
Nico Huberd99a2bd2016-02-18 21:42:49 +0000502 /* Allocate bulk transfers. */
503 unsigned int i;
Nico Huber519be662018-12-23 20:03:35 +0100504 for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000505 transfers[i] = libusb_alloc_transfer(0);
506 if (!transfers[i]) {
507 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
508 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200509 }
510 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000511
512 /* Now transfer requested chunks using libusb's asynchronous interface. */
513 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200514 while ((status.queued_idx < count) &&
515 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
516 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000517 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000518 libusb_fill_bulk_transfer(transfer, dp_data->handle, 0x80 | dp_data->in_endpoint,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000519 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
520 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
521 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
522 ret = libusb_submit_transfer(transfer);
523 if (ret < 0) {
524 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
525 status.queued_idx, libusb_error_name(ret));
526 goto err_free;
527 }
528 ++status.queued_idx;
529 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000530 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 0))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000531 goto err_free;
532 }
533 /* Wait for transfers to finish. */
Rick Altherr4f9cd8b2021-12-13 17:10:00 -0800534 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000535 goto err_free;
536 /* Check if everything has been transmitted. */
537 if ((status.finished_idx < count) || status.error)
538 goto err_free;
539
540 err = 0;
541
542err_free:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000543 dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000544 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
545 if (transfers[i]) libusb_free_transfer(transfers[i]);
546 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000547}
548
Simon Glassae616512016-01-23 23:27:58 +0000549static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000550{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000551 int ret;
552 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000553 const unsigned int chunksize = 0x200;
Nico Huberbbaa1712018-12-05 13:26:20 +0100554 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000555 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100556 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000557
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000558 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000559
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000560 if (residue) {
561 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
562 start, residue);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100563 ret = default_spi_read(flash, buf, start, residue);
Simon Glass25e9f402015-06-28 13:31:19 +0000564 if (ret)
565 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000566 }
567
568 /* Round down. */
569 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000570 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000571 if (ret)
572 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000573
574 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000575 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000576 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
577 start, len);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100578 ret = default_spi_read(flash, buf + residue + bulklen,
579 start + residue + bulklen, len);
Simon Glass25e9f402015-06-28 13:31:19 +0000580 if (ret)
581 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000582 }
583
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000584 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000585 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000586err:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000587 dediprog_set_leds(LED_ERROR, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +0000588 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000589}
590
Nico Hubera4b14f72012-06-19 12:06:53 +0000591/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
592 * @chunksize length of data chunks, only 256 supported by now
593 * @start start address
594 * @len length
595 * @dedi_spi_cmd dediprog specific write command for spi bus
596 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000597 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000598static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000599 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000600{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000601 /* USB transfer size must be 512, other sizes will NOT work at all.
602 * chunksize is the real data size per USB bulk transfer. The remaining
603 * space in a USB bulk transfer must be filled with 0xff padding.
604 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000605 const unsigned int count = len / chunksize;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100606 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000607
Nico Hubera4b14f72012-06-19 12:06:53 +0000608 /*
609 * We should change this check to
610 * chunksize > 512
611 * once we know how to handle different chunk sizes.
612 */
613 if (chunksize != 256) {
614 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200615 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000616 __func__, chunksize);
Nico Hubera4b14f72012-06-19 12:06:53 +0000617 return 1;
618 }
619
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000620 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000621 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200622 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000623 __func__, start, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000624 return 1;
625 }
626
627 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000628 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000629 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000630
David Hendricksf73f8a72018-02-21 07:34:34 -0800631 int command_packet_size;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000632 switch (protocol(dp_data)) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800633 case PROTOCOL_V1:
634 command_packet_size = 5;
635 break;
636 case PROTOCOL_V2:
637 command_packet_size = 10;
638 break;
639 case PROTOCOL_V3:
640 command_packet_size = 14;
641 break;
642 default:
643 return 1;
644 }
645
646 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000647 unsigned int value, idx;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100648 if (prepare_rw_cmd(flash, data_packet, count, dedi_spi_cmd, &value, &idx, start, 0))
649 return 1;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000650 int ret = dediprog_write(dp_data->handle, CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
Nico Huber519be662018-12-23 20:03:35 +0100651 if (ret != (int)sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000652 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000653 return 1;
654 }
655
Simon Glassae616512016-01-23 23:27:58 +0000656 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000657 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000658 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000659 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000660 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000661 int transferred;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000662 ret = libusb_bulk_transfer(dp_data->handle, dp_data->out_endpoint, usbbuf, 512, &transferred,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000663 DEFAULT_TIMEOUT);
664 if ((ret < 0) || (transferred != 512)) {
665 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000666 return 1;
667 }
668 }
669
670 return 0;
671}
672
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000673static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000674 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000675{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000676 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000677 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000678 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
679 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100680 const struct dediprog_data *dp_data = flash->mst.spi->data;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000681
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000682 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000683
Nico Hubera4b14f72012-06-19 12:06:53 +0000684 if (chunksize != 256) {
685 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
686 "we don't know how dediprog\nhandles them.\n");
687 /* Write everything like it was residue. */
688 residue = len;
689 }
690
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000691 if (residue) {
692 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
693 start, residue);
Nico Huber93db6e12018-09-30 01:18:43 +0200694 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
695 ret = spi_write_chunked(flash, buf, start, residue, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000696 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000697 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000698 return ret;
699 }
700 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000701
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000702 /* Round down. */
703 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000704 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000705 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000706 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000707 return ret;
708 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000709
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000710 len -= residue + bulklen;
711 if (len) {
712 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
713 start, len);
714 ret = spi_write_chunked(flash, buf + residue + bulklen,
Nico Huber93db6e12018-09-30 01:18:43 +0200715 start + residue + bulklen, len, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000716 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000717 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000718 return ret;
719 }
720 }
721
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000722 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000723 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000724}
725
Nico Hubera96aaa32024-03-05 12:56:13 +0100726static int dediprog_spi_write_chunked(struct flashctx *flash, const uint8_t *buf,
727 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
728{
729 /* We can write only up to 65535 pages at once: */
730 while (len) {
731 const size_t len_here = MIN(len, flash->chip->page_size * MAX_BLOCK_COUNT);
732 const int ret = dediprog_spi_write(flash, buf, start, len_here, dedi_spi_cmd);
733 if (ret)
734 return ret;
735
736 start += len_here;
737 buf += len_here;
738 len -= len_here;
739 }
740 return 0;
741}
742
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000743static 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 +0000744{
Nico Hubera96aaa32024-03-05 12:56:13 +0100745 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000746}
747
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000748static 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 +0000749{
Nico Hubera96aaa32024-03-05 12:56:13 +0100750 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000751}
752
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000753static int dediprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000754 unsigned int writecnt,
755 unsigned int readcnt,
756 const unsigned char *writearr,
757 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000758{
759 int ret;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100760 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000761
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000762 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Nico Huber9a11cbf2023-01-13 01:19:07 +0100763 if (writecnt > flash->mst.spi->max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000764 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000765 return 1;
766 }
Nico Huber9a11cbf2023-01-13 01:19:07 +0100767 if (readcnt > flash->mst.spi->max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000768 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000769 return 1;
770 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100771
Simon Glassae616512016-01-23 23:27:58 +0000772 unsigned int idx, value;
773 /* New protocol has options and timeout combined as value while the old one used the value field for
774 * timeout and the index field for options. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000775 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000776 idx = 0;
777 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
778 } else {
779 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
780 value = 0;
781 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000782 ret = dediprog_write(dp_data->handle, CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Nico Huber519be662018-12-23 20:03:35 +0100783 if (ret != (int)writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000784 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000785 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000786 return 1;
787 }
Simon Glassae616512016-01-23 23:27:58 +0000788 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000789 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000790
Stefan Tauner74367bf2016-02-20 20:53:46 +0000791 /* The specifications do state the possibility to set a timeout for transceive transactions.
792 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
793 * can predict the time needed by the previous command or so (untested). In any case, using this
794 * "feature" to set sane-looking timouts for the read below will completely trash performance with
795 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
796 *
797 * The specification also uses only 0 in its examples, so the lesson to learn here:
798 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000799 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800800 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000801 idx = 0;
802 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
803 } else {
804 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
805 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
806 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000807 ret = dediprog_read(dp_data->dediprog_handle, CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000808 */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000809 ret = dediprog_read(dp_data->handle, CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Nico Huber519be662018-12-23 20:03:35 +0100810 if (ret != (int)readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000811 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 +0000812 return 1;
813 }
814 return 0;
815}
816
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000817static int dediprog_check_devicestring(struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000818{
Nico Huber5262e292024-03-02 13:21:25 +0100819 const int devstr_len = 32, old_devstr_len = 16;
Nico Hubere76e21f2024-02-29 23:33:35 +0100820 char buf[devstr_len + 1];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000821 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000822
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000823 /* Command Receive Device String. */
Nico Hubere76e21f2024-02-29 23:33:35 +0100824 ret = dediprog_read(dp_data->handle, CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, devstr_len);
Nico Huber5262e292024-03-02 13:21:25 +0100825 if (ret < old_devstr_len || ret > devstr_len) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000826 msg_perr("Incomplete/failed Command Receive Device String!\n");
827 return 1;
828 }
Nico Huber5262e292024-03-02 13:21:25 +0100829 buf[ret] = '\0';
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000830 msg_pdbg("Found a %s\n", buf);
Nico Hubere76e21f2024-02-29 23:33:35 +0100831 if (memcmp(buf, "SF100", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000832 dp_data->devicetype = DEV_SF100;
Nico Hubere76e21f2024-02-29 23:33:35 +0100833 else if (memcmp(buf, "SF200", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000834 dp_data->devicetype = DEV_SF200;
Nico Hubere76e21f2024-02-29 23:33:35 +0100835 else if (memcmp(buf, "SF600", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000836 dp_data->devicetype = DEV_SF600;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000837 else {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500838 msg_perr("Device not a SF100, SF200, or SF600!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000839 return 1;
840 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000841
Nico Huberbdef5c22024-03-02 13:51:39 +0100842 unsigned int sfnum;
843 unsigned int fw[3];
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100844 if (sscanf(buf, "SF%u", &sfnum) != 1 ||
845 sfnum != dp_data->devicetype ||
846 sscanf(buf, "SF%*s V:%u.%u.%u ", &fw[0], &fw[1], &fw[2]) != 3) {
Simon Glassae616512016-01-23 23:27:58 +0000847 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000848 return 1;
849 }
Simon Glassae616512016-01-23 23:27:58 +0000850 /* Only these major versions were tested. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000851 if (fw[0] < 2 || fw[0] > 7) {
Simon Glassae616512016-01-23 23:27:58 +0000852 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000853 return 1;
854 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800855
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000856 dp_data->firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000857 if (protocol(dp_data) == PROTOCOL_UNKNOWN) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800858 msg_perr("Internal error: Unable to determine protocol version.\n");
859 return 1;
860 }
Simon Glassae616512016-01-23 23:27:58 +0000861
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000862 return 0;
863}
864
David Hendricksc05900f2016-02-18 21:42:41 +0000865/*
Ryan O'Leary301ae222019-06-24 19:14:33 -0700866 * Read the id from the dediprog. This should return the numeric part of the
867 * serial number found on a sticker on the back of the dediprog. Note this
868 * number is stored in writable eeprom, so it could get out of sync. Also note,
869 * this function only supports SF100 at this time, but SF600 support is not too
870 * much different.
871 * @return the id on success, -1 on failure
872 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000873static int dediprog_read_id(libusb_device_handle *dediprog_handle)
Ryan O'Leary301ae222019-06-24 19:14:33 -0700874{
875 int ret;
876 uint8_t buf[3];
877
878 ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN,
879 0x7, /* request */
880 0, /* value */
881 0xEF00, /* index */
882 buf, sizeof(buf),
883 DEFAULT_TIMEOUT);
884 if (ret != sizeof(buf)) {
885 msg_perr("Failed to read dediprog id, error %d!\n", ret);
886 return -1;
887 }
888
889 return buf[0] << 16 | buf[1] << 8 | buf[2];
890}
891
892/*
David Hendricksc05900f2016-02-18 21:42:41 +0000893 * This command presumably sets the voltage for the SF100 itself (not the
894 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
895 * (including all SF600s) do not support it.
896 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000897
David Hendricksc05900f2016-02-18 21:42:41 +0000898/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
899 * Only use dediprog_set_voltage on SF100 programmers with firmware older
900 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000901static int dediprog_set_voltage(libusb_device_handle *dediprog_handle)
David Hendricksc05900f2016-02-18 21:42:41 +0000902{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000903 unsigned char buf[1] = {0};
904 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000905 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000906 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000907 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000908 return 1;
909 }
David Hendricksc05900f2016-02-18 21:42:41 +0000910 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000911 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000912 return 1;
913 }
David Hendricksc05900f2016-02-18 21:42:41 +0000914
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000915 return 0;
916}
917
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000918static int dediprog_standalone_mode(const struct dediprog_data *dp_data)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000919{
920 int ret;
921
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000922 if (dp_data->devicetype != DEV_SF600)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000923 return 0;
924
925 msg_pdbg2("Disabling standalone mode.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000926 ret = dediprog_write(dp_data->handle, CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000927 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000928 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000929 return 1;
930 }
931
932 return 0;
933}
934
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000935#if 0
936/* Something.
937 * Present in eng_detect_blink.log with firmware 3.1.8
938 * Always preceded by Command Receive Device String
939 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000940static int dediprog_command_b(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000941{
942 int ret;
943 char buf[0x3];
944
Simon Glass557eb4f2015-07-05 16:53:22 +0000945 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
946 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000947 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000948 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000949 return 1;
950 }
951 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
952 (buf[2] != 0xff)) {
953 msg_perr("Unexpected response to Command B!\n");
954 return 1;
955 }
956
957 return 0;
958}
959#endif
960
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000961static int set_target_flash(libusb_device_handle *dediprog_handle, enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000962{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000963 int ret = dediprog_write(dediprog_handle, CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +0000964 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000965 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000966 return 1;
967 }
968 return 0;
969}
970
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000971#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000972/* Returns true if the button is currently pressed. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000973static bool dediprog_get_button(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000974{
Simon Glass557eb4f2015-07-05 16:53:22 +0000975 char buf[1];
976 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
977 buf, 0x1, DEFAULT_TIMEOUT);
978 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000979 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000980 return 1;
981 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000982 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000983}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000984#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000985
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000986static int parse_voltage(char *voltage)
987{
988 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000989 int i;
990 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000991
992 if (!voltage || !strlen(voltage)) {
993 msg_perr("Empty voltage= specified.\n");
994 return -1;
995 }
996 millivolt = (int)strtol(voltage, &tmp, 0);
997 voltage = tmp;
998 /* Handle "," and "." as decimal point. Everything after it is assumed
999 * to be in decimal notation.
1000 */
1001 if ((*voltage == '.') || (*voltage == ',')) {
1002 voltage++;
1003 for (i = 0; i < 3; i++) {
1004 fraction *= 10;
1005 /* Don't advance if the current character is invalid,
1006 * but continue multiplying.
1007 */
1008 if ((*voltage < '0') || (*voltage > '9'))
1009 continue;
1010 fraction += *voltage - '0';
1011 voltage++;
1012 }
1013 /* Throw away remaining digits. */
1014 voltage += strspn(voltage, "0123456789");
1015 }
1016 /* The remaining string must be empty or "mV" or "V". */
1017 tolower_string(voltage);
1018
1019 /* No unit or "V". */
1020 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1021 millivolt *= 1000;
1022 millivolt += fraction;
1023 } else if (!strncmp(voltage, "mv", 2) ||
1024 !strncmp(voltage, "milliv", 6)) {
1025 /* No adjustment. fraction is discarded. */
1026 } else {
1027 /* Garbage at the end of the string. */
1028 msg_perr("Garbage voltage= specified.\n");
1029 return -1;
1030 }
1031 return millivolt;
1032}
1033
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001034static int dediprog_shutdown(void *data);
1035
Nico Huber93db6e12018-09-30 01:18:43 +02001036static struct spi_master spi_master_dediprog = {
Nico Huberdc5af542018-12-22 16:54:59 +01001037 .features = SPI_MASTER_NO_4BA_MODES,
Simon Glassae616512016-01-23 23:27:58 +00001038 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
1039 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001040 .command = dediprog_spi_send_command,
1041 .multicommand = default_spi_send_multicommand,
1042 .read = dediprog_spi_read,
1043 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +00001044 .write_aai = dediprog_spi_write_aai,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001045 .shutdown = dediprog_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301046 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001047};
1048
Ryan O'Leary301ae222019-06-24 19:14:33 -07001049/*
1050 * Open a dediprog_handle with the USB device at the given index.
1051 * @index index of the USB device
1052 * @return 0 for success, -1 for error, -2 for busy device
1053 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001054static int dediprog_open(int index, struct dediprog_data *dp_data)
Ryan O'Leary301ae222019-06-24 19:14:33 -07001055{
1056 const uint16_t vid = devs_dediprog[0].vendor_id;
1057 const uint16_t pid = devs_dediprog[0].device_id;
1058 int ret;
1059
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001060 dp_data->handle = usb_dev_get_by_vid_pid_number(dp_data->usb_ctx, vid, pid, (unsigned int) index);
1061 if (!dp_data->handle) {
Ryan O'Leary301ae222019-06-24 19:14:33 -07001062 msg_perr("Could not find a Dediprog programmer on USB.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001063 libusb_exit(dp_data->usb_ctx);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001064 return -1;
1065 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001066 ret = libusb_set_configuration(dp_data->handle, 1);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001067 if (ret != 0) {
1068 msg_perr("Could not set USB device configuration: %i %s\n",
1069 ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001070 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001071 return -2;
1072 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001073 ret = libusb_claim_interface(dp_data->handle, 0);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001074 if (ret < 0) {
1075 msg_perr("Could not claim USB device interface %i: %i %s\n",
1076 0, ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001077 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001078 return -2;
1079 }
1080 return 0;
1081}
1082
David Hendricks8bb20212011-06-14 01:35:36 +00001083static int dediprog_shutdown(void *data)
1084{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001085 int ret = 0;
1086 struct dediprog_data *dp_data = data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001087
David Hendricks8bb20212011-06-14 01:35:36 +00001088 /* URB 28. Command Set SPI Voltage to 0. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001089 if (dediprog_set_spi_voltage(dp_data->handle, 0x0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001090 ret = 1;
1091 goto out;
David Hendricks8bb20212011-06-14 01:35:36 +00001092 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001093
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001094 if (libusb_release_interface(dp_data->handle, 0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001095 msg_perr("Could not release USB interface!\n");
1096 ret = 1;
1097 goto out;
1098 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001099 libusb_close(dp_data->handle);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001100 libusb_exit(dp_data->usb_ctx);
1101out:
1102 free(data);
1103 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +00001104}
1105
Nico Hubere3a26882023-01-11 21:45:51 +01001106static int dediprog_init(struct flashprog_programmer *const prog)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001107{
Ryan O'Leary301ae222019-06-24 19:14:33 -07001108 char *voltage, *id_str, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +00001109 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001110 int millivolt = 3500;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001111 int id = -1; /* -1 defaults to enumeration order */
1112 int found_id;
Nathan Laredo21541a62012-12-24 22:07:36 +00001113 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +02001114 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +00001115 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001116
Nico Huber77fa67d2013-02-20 18:03:36 +00001117 spispeed = extract_programmer_param("spispeed");
1118 if (spispeed) {
1119 for (i = 0; spispeeds[i].name; ++i) {
1120 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1121 spispeed_idx = i;
1122 break;
1123 }
1124 }
1125 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +00001126 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +00001127 free(spispeed);
1128 return 1;
1129 }
1130 free(spispeed);
1131 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001132
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001133 voltage = extract_programmer_param("voltage");
1134 if (voltage) {
1135 millivolt = parse_voltage(voltage);
1136 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001137 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001138 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001139 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1140 }
1141
Ryan O'Leary301ae222019-06-24 19:14:33 -07001142 id_str = extract_programmer_param("id");
1143 if (id_str) {
1144 char prefix0, prefix1;
1145 if (sscanf(id_str, "%c%c%d", &prefix0, &prefix1, &id) != 3) {
1146 msg_perr("Error: Could not parse dediprog 'id'.\n");
1147 msg_perr("Expected a string like SF012345 or DP012345.\n");
1148 free(id_str);
1149 return 1;
1150 }
1151 if (id < 0 || id >= 0x1000000) {
1152 msg_perr("Error: id %s is out of range!\n", id_str);
1153 free(id_str);
1154 return 1;
1155 }
1156 if (!(prefix0 == 'S' && prefix1 == 'F') && !(prefix0 == 'D' && prefix1 == 'P')) {
1157 msg_perr("Error: %s is an invalid id!\n", id_str);
1158 free(id_str);
1159 return 1;
1160 }
1161 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1162 }
1163 free(id_str);
1164
Nathan Laredo21541a62012-12-24 22:07:36 +00001165 device = extract_programmer_param("device");
1166 if (device) {
1167 char *dev_suffix;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001168 if (id != -1) {
1169 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1170 }
Nathan Laredo21541a62012-12-24 22:07:36 +00001171 errno = 0;
1172 usedevice = strtol(device, &dev_suffix, 10);
1173 if (errno != 0 || device == dev_suffix) {
1174 msg_perr("Error: Could not convert 'device'.\n");
1175 free(device);
1176 return 1;
1177 }
Nico Huber961f4a12019-10-04 17:34:22 +02001178 if (usedevice < 0 || usedevice > INT_MAX) {
Nathan Laredo21541a62012-12-24 22:07:36 +00001179 msg_perr("Error: Value for 'device' is out of range.\n");
1180 free(device);
1181 return 1;
1182 }
1183 if (strlen(dev_suffix) > 0) {
1184 msg_perr("Error: Garbage following 'device' value.\n");
1185 free(device);
1186 return 1;
1187 }
1188 msg_pinfo("Using device %li.\n", usedevice);
1189 }
1190 free(device);
1191
Stefan Taunere659d2d2013-05-03 21:58:28 +00001192 target_str = extract_programmer_param("target");
1193 if (target_str) {
1194 char *target_suffix;
1195 errno = 0;
1196 target = strtol(target_str, &target_suffix, 10);
1197 if (errno != 0 || target_str == target_suffix) {
1198 msg_perr("Error: Could not convert 'target'.\n");
1199 free(target_str);
1200 return 1;
1201 }
1202 if (target < 1 || target > 2) {
1203 msg_perr("Error: Value for 'target' is out of range.\n");
1204 free(target_str);
1205 return 1;
1206 }
1207 if (strlen(target_suffix) > 0) {
1208 msg_perr("Error: Garbage following 'target' value.\n");
1209 free(target_str);
1210 return 1;
1211 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001212 switch (target) {
1213 case 1:
1214 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1215 target = FLASH_TYPE_APPLICATION_FLASH_1;
1216 break;
1217 case 2:
1218 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1219 target = FLASH_TYPE_APPLICATION_FLASH_2;
1220 break;
1221 default:
1222 break;
1223 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001224 }
1225 free(target_str);
1226
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001227 struct dediprog_data *dp_data = calloc(1, sizeof(*dp_data));
1228 if (!dp_data) {
1229 msg_perr("Unable to allocate space for SPI master data\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001230 return 1;
1231 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001232 dp_data->firmwareversion = FIRMWARE_VERSION(0, 0, 0);
1233 dp_data->devicetype = DEV_UNKNOWN;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001234
1235 /* Here comes the USB stuff. */
Thomas Heijligen88e87c52022-08-05 17:56:20 +02001236 ret = libusb_init(&dp_data->usb_ctx);
1237 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001238 msg_perr("Could not initialize libusb!\n");
1239 goto init_err_exit;
1240 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001241
Ryan O'Leary301ae222019-06-24 19:14:33 -07001242 if (id != -1) {
1243 for (i = 0; ; i++) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001244 ret = dediprog_open(i, dp_data);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001245 if (ret == -1) {
1246 /* no dev */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001247 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001248 } else if (ret == -2) {
1249 /* busy dev */
1250 continue;
1251 }
1252
1253 /* Notice we can only call dediprog_read_id() after
1254 * libusb_set_configuration() and
1255 * libusb_claim_interface(). When searching by id and
1256 * either configuration or claim fails (usually the
Nico Huberc3b02dc2023-08-12 01:13:45 +02001257 * device is in use by another instance of flashprog),
Ryan O'Leary301ae222019-06-24 19:14:33 -07001258 * the device is skipped and the next device is tried.
1259 */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001260 found_id = dediprog_read_id(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001261 if (found_id < 0) {
1262 msg_perr("Could not read id.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001263 libusb_release_interface(dp_data->handle, 0);
1264 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001265 continue;
1266 }
1267 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1268 if (found_id != id) {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001269 libusb_release_interface(dp_data->handle, 0);
1270 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001271 continue;
1272 }
1273 break;
1274 }
1275 } else {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001276 if (dediprog_open(usedevice, dp_data)) {
1277 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001278 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001279 found_id = dediprog_read_id(dp_data->handle);
David Woodhoused2a7e872013-07-30 09:34:44 +00001280 }
Ryan O'Leary301ae222019-06-24 19:14:33 -07001281
1282 if (found_id >= 0) {
1283 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001284 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001285
David Hendricksc05900f2016-02-18 21:42:41 +00001286 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1287 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001288 if (dediprog_check_devicestring(dp_data)) {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001289 if (dediprog_set_voltage(dp_data->handle))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001290 goto init_err_cleanup_exit;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001291 if (dediprog_check_devicestring(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001292 goto init_err_cleanup_exit;
David Hendricksc05900f2016-02-18 21:42:41 +00001293 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001294
Jay Thompsoncabe3202018-08-17 14:30:04 -05001295 /* SF100/SF200 uses one in/out endpoint, SF600 uses separate in/out endpoints */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001296 dp_data->in_endpoint = 2;
1297 switch (dp_data->devicetype) {
Jay Thompsoncabe3202018-08-17 14:30:04 -05001298 case DEV_SF100:
1299 case DEV_SF200:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001300 dp_data->out_endpoint = 2;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001301 break;
1302 default:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001303 dp_data->out_endpoint = 1;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001304 break;
1305 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001306
Simon Glass25e9f402015-06-28 13:31:19 +00001307 /* Set all possible LEDs as soon as possible to indicate activity.
1308 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001309 * dediprog_check_devicestring() has queried the device. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001310 dediprog_set_leds(LED_ALL, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +00001311
Simon Glass557eb4f2015-07-05 16:53:22 +00001312 /* Select target/socket, frequency and VCC. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001313 if (set_target_flash(dp_data->handle, target) ||
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001314 dediprog_set_spi_speed(spispeed_idx, dp_data) ||
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001315 dediprog_set_spi_voltage(dp_data->handle, millivolt)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001316 dediprog_set_leds(LED_ERROR, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001317 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001318 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001319
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001320 if (dediprog_standalone_mode(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001321 goto init_err_cleanup_exit;
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001322
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001323 if ((dp_data->devicetype == DEV_SF100) ||
1324 (dp_data->devicetype == DEV_SF600 && protocol(dp_data) == PROTOCOL_V3))
Nico Huber7eb38aa2019-03-21 15:42:54 +01001325 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1326
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001327 if (protocol(dp_data) >= PROTOCOL_V2)
Nico Huber93db6e12018-09-30 01:18:43 +02001328 spi_master_dediprog.features |= SPI_MASTER_4BA;
1329
Angel Pons116d56d2021-03-22 11:28:00 +01001330 if (dediprog_set_leds(LED_NONE, dp_data))
1331 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001332
Nico Huber89569d62023-01-12 23:31:40 +01001333 return register_spi_master(&spi_master_dediprog, 0, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001334
1335init_err_cleanup_exit:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001336 dediprog_shutdown(dp_data);
1337 return 1;
1338
1339init_err_exit:
1340 free(dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001341 return 1;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001342}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001343
1344const struct programmer_entry programmer_dediprog = {
1345 .name = "dediprog",
1346 .type = USB,
1347 .devs.dev = devs_dediprog,
1348 .init = dediprog_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001349};