blob: b8cc2317f3030e0b4ce712c1cf191617b4d6c160 [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 Huber008a44f2024-04-14 23:39:47 +020040#define MAX_CMD_SIZE 15
Nico Huberd99a2bd2016-02-18 21:42:49 +000041#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
David Hendricksc3685182020-06-23 17:36:09 -070042#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
Nico Huberd99a2bd2016-02-18 21:42:49 +000043#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
44#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
45#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
David Woodhouse7f3b89f2016-02-18 21:42:15 +000046
47enum dediprog_devtype {
48 DEV_UNKNOWN = 0,
49 DEV_SF100 = 100,
Jay Thompsoncabe3202018-08-17 14:30:04 -050050 DEV_SF200 = 200,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000051 DEV_SF600 = 600,
Nico Huber00578222024-03-02 14:00:53 +010052 DEV_SF600PG2 = 600+2,
53 DEV_SF700 = 700,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000054};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000055
Simon Glass25e9f402015-06-28 13:31:19 +000056enum dediprog_leds {
57 LED_INVALID = -1,
58 LED_NONE = 0,
59 LED_PASS = 1 << 0,
60 LED_BUSY = 1 << 1,
61 LED_ERROR = 1 << 2,
62 LED_ALL = 7,
63};
64
Simon Glass557eb4f2015-07-05 16:53:22 +000065/* IO bits for CMD_SET_IO_LED message */
66enum dediprog_ios {
67 IO1 = 1 << 0,
68 IO2 = 1 << 1,
69 IO3 = 1 << 2,
70 IO4 = 1 << 3,
71};
72
73enum dediprog_cmds {
74 CMD_TRANSCEIVE = 0x01,
75 CMD_POLL_STATUS_REG = 0x02,
76 CMD_SET_VPP = 0x03,
77 CMD_SET_TARGET = 0x04,
78 CMD_READ_EEPROM = 0x05,
79 CMD_WRITE_EEPROM = 0x06,
80 CMD_SET_IO_LED = 0x07,
81 CMD_READ_PROG_INFO = 0x08,
82 CMD_SET_VCC = 0x09,
83 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000084 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000085 CMD_GET_BUTTON = 0x11,
86 CMD_GET_UID = 0x12,
87 CMD_SET_CS = 0x14,
88 CMD_IO_MODE = 0x15,
89 CMD_FW_UPDATE = 0x1A,
90 CMD_FPGA_UPDATE = 0x1B,
91 CMD_READ_FPGA_VERSION = 0x1C,
92 CMD_SET_HOLD = 0x1D,
93 CMD_READ = 0x20,
94 CMD_WRITE = 0x30,
95 CMD_WRITE_AT45DB = 0x31,
96 CMD_NAND_WRITE = 0x32,
97 CMD_NAND_READ = 0x33,
98 CMD_SET_SPI_CLK = 0x61,
99 CMD_CHECK_SOCKET = 0x62,
100 CMD_DOWNLOAD_PRJ = 0x63,
101 CMD_READ_PRJ_NAME = 0x64,
102 // New protocol/firmware only
103 CMD_CHECK_SDCARD = 0x65,
104 CMD_READ_PRJ = 0x66,
105};
106
107enum dediprog_target {
108 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
109 FLASH_TYPE_FLASH_CARD,
110 FLASH_TYPE_APPLICATION_FLASH_2,
111 FLASH_TYPE_SOCKET,
112};
113
114enum dediprog_readmode {
115 READ_MODE_STD = 1,
116 READ_MODE_FAST = 2,
117 READ_MODE_ATMEL45 = 3,
118 READ_MODE_4B_ADDR_FAST = 4,
119 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
120};
121
122enum dediprog_writemode {
Elyes HAOUASac01baa2018-05-28 16:52:21 +0200123 WRITE_MODE_PAGE_PGM = 1,
Simon Glass557eb4f2015-07-05 16:53:22 +0000124 WRITE_MODE_PAGE_WRITE = 2,
125 WRITE_MODE_1B_AAI = 3,
126 WRITE_MODE_2B_AAI = 4,
127 WRITE_MODE_128B_PAGE = 5,
128 WRITE_MODE_PAGE_AT26DF041 = 6,
129 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000130 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000131 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000132 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000133 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
134 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
135};
136
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000137enum dediprog_standalone_mode {
138 ENTER_STANDALONE_MODE = 0,
139 LEAVE_STANDALONE_MODE = 1,
140};
141
David Hendricksf73f8a72018-02-21 07:34:34 -0800142/*
Nico Huberc3b02dc2023-08-12 01:13:45 +0200143 * These are not official designations; they are for use in flashprog only.
David Hendricksf73f8a72018-02-21 07:34:34 -0800144 * Order must be preserved so that comparison operators work.
145 */
146enum protocol {
147 PROTOCOL_UNKNOWN,
148 PROTOCOL_V1,
149 PROTOCOL_V2,
150 PROTOCOL_V3,
151};
152
Thomas Heijligencc853d82021-05-04 15:32:17 +0200153static const struct dev_entry devs_dediprog[] = {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500154 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
Stefan Taunerfdec7472016-02-22 08:59:27 +0000155
156 {0},
157};
Simon Glass557eb4f2015-07-05 16:53:22 +0000158
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000159struct dediprog_data {
160 struct libusb_context *usb_ctx;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000161 libusb_device_handle *handle;
162 int in_endpoint;
163 int out_endpoint;
164 int firmwareversion;
Nico Huber274e6552024-03-24 12:34:57 +0100165 char devicestring[32+1];
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000166 enum dediprog_devtype devicetype;
Nico Huber008a44f2024-04-14 23:39:47 +0200167 int (*prepare_rw_cmd)(
168 struct flashctx *, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
169 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000170};
Simon Glass557eb4f2015-07-05 16:53:22 +0000171
Nico Huberd99a2bd2016-02-18 21:42:49 +0000172#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
173 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
174/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
175const char * LIBUSB_CALL libusb_error_name(int error_code)
176{
177 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
178 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
179 static char my_libusb_error[18 + 5 + 2];
180 sprintf(my_libusb_error, "libusb error code %i", error_code);
181 return my_libusb_error;
182 } else {
183 return "UNKNOWN";
184 }
185}
186#endif
187
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000188static enum protocol protocol(const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000189{
David Hendricksf73f8a72018-02-21 07:34:34 -0800190 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000191 switch (dp_data->devicetype) {
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000192 case DEV_SF100:
Jay Thompsoncabe3202018-08-17 14:30:04 -0500193 case DEV_SF200:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000194 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 5, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800195 return PROTOCOL_V1;
196 else
197 return PROTOCOL_V2;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000198 case DEV_SF600:
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000199 if (dp_data->firmwareversion < FIRMWARE_VERSION(6, 9, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800200 return PROTOCOL_V1;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000201 else if (dp_data->firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
David Hendricksf73f8a72018-02-21 07:34:34 -0800202 return PROTOCOL_V2;
203 else
204 return PROTOCOL_V3;
Nico Huber00578222024-03-02 14:00:53 +0100205 case DEV_SF700:
206 case DEV_SF600PG2:
207 return PROTOCOL_V3;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000208 default:
David Hendricksf73f8a72018-02-21 07:34:34 -0800209 return PROTOCOL_UNKNOWN;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000210 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000211}
Simon Glassae616512016-01-23 23:27:58 +0000212
Nico Huberd99a2bd2016-02-18 21:42:49 +0000213struct dediprog_transfer_status {
Richard Hughes842d6782021-01-15 09:48:12 +0000214 struct flashctx *flash;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000215 int error; /* OK if 0, ERROR else */
216 unsigned int queued_idx;
217 unsigned int finished_idx;
218};
219
220static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
221{
222 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
223 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
224 status->error = 1;
225 msg_perr("SPI bulk read failed!\n");
226 }
Richard Hughes842d6782021-01-15 09:48:12 +0000227 flashprog_progress_add(status->flash, transfer->actual_length);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000228 ++status->finished_idx;
229}
230
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000231static int dediprog_bulk_read_poll(struct libusb_context *usb_ctx,
232 const struct dediprog_transfer_status *const status,
233 const int finish)
Nico Huberd99a2bd2016-02-18 21:42:49 +0000234{
235 if (status->finished_idx >= status->queued_idx)
236 return 0;
237
238 do {
239 struct timeval timeout = { 10, 0 };
240 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
241 if (ret < 0) {
242 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
243 return 1;
244 }
245 } while (finish && (status->finished_idx < status->queued_idx));
246 return 0;
247}
248
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000249static int dediprog_read(libusb_device_handle *dediprog_handle,
250 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
251 uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000252{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000253 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
254 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000255}
256
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000257static int dediprog_write(libusb_device_handle *dediprog_handle,
258 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
259 const uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000260{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000261 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
262 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000263}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000264
Nico Huberd99a2bd2016-02-18 21:42:49 +0000265
Simon Glass25e9f402015-06-28 13:31:19 +0000266/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000267static int dediprog_set_leds(int leds, const struct dediprog_data *dp_data)
Stefan Reinauer915b8402011-01-28 09:00:15 +0000268{
Simon Glass25e9f402015-06-28 13:31:19 +0000269 if (leds < LED_NONE || leds > LED_ALL)
270 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000271
Simon Glassae616512016-01-23 23:27:58 +0000272 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
273 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000274 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000275 * bit 0 == 0: red light is on.
276 *
277 * Additionally, the command structure has changed with the "new" protocol.
278 *
279 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000280 */
Simon Glassae616512016-01-23 23:27:58 +0000281 int target_leds, ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000282 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000283 target_leds = (leds ^ 7) << 8;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000284 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000285 } else {
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000286 if (dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Simon Glassae616512016-01-23 23:27:58 +0000287 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
288 } else {
289 target_leds = leds;
290 }
291 target_leds ^= 7;
292
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000293 ret = dediprog_write(dp_data->handle, CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000294 }
295
Stefan Reinauer915b8402011-01-28 09:00:15 +0000296 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000297 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000298 return 1;
299 }
300
Stefan Reinauer915b8402011-01-28 09:00:15 +0000301 return 0;
302}
303
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000304static int dediprog_set_spi_voltage(libusb_device_handle *dediprog_handle, int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000305{
306 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000307 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000308
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000309 switch (millivolt) {
310 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000311 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000312 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000313 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000314 case 1800:
315 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000316 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000317 case 2500:
318 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000319 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000320 case 3500:
321 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000322 break;
323 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000324 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000325 return 1;
326 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000327 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
328 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000329
Nico Huber4099a8a2012-06-16 00:02:27 +0000330 if (voltage_selector == 0) {
331 /* Wait some time as the original driver does. */
332 programmer_delay(200 * 1000);
333 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000334 ret = dediprog_write(dediprog_handle, CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000335 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000336 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
337 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000338 return 1;
339 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000340 if (voltage_selector != 0) {
341 /* Wait some time as the original driver does. */
342 programmer_delay(200 * 1000);
343 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000344 return 0;
345}
346
Nico Huber77fa67d2013-02-20 18:03:36 +0000347struct dediprog_spispeeds {
348 const char *const name;
349 const int speed;
350};
351
352static const struct dediprog_spispeeds spispeeds[] = {
353 { "24M", 0x0 },
354 { "12M", 0x2 },
355 { "8M", 0x1 },
356 { "3M", 0x3 },
357 { "2.18M", 0x4 },
358 { "1.5M", 0x5 },
359 { "750k", 0x6 },
360 { "375k", 0x7 },
361 { NULL, 0x0 },
362};
363
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000364static int dediprog_set_spi_speed(unsigned int spispeed_idx, const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000365{
Nico Huber00578222024-03-02 14:00:53 +0100366 if (dp_data->devicetype < DEV_SF600PG2 && dp_data->firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000367 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
368 return 0;
369 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000370
Simon Glass557eb4f2015-07-05 16:53:22 +0000371 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
372 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000373
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000374 int ret = dediprog_write(dp_data->handle, CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000375 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000376 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000377 return 1;
378 }
379 return 0;
380}
381
Nico Huber008a44f2024-04-14 23:39:47 +0200382static int prepare_rw_cmd_common(uint8_t cmd_buf[MAX_CMD_SIZE], uint8_t dp_spi_cmd, unsigned int count)
Nico Huber477e1692019-06-18 23:56:01 +0200383{
Nico Hubera96aaa32024-03-05 12:56:13 +0100384 if (count > MAX_BLOCK_COUNT) {
Nico Huberac90af62022-12-18 00:22:47 +0000385 msg_perr("%s: Unsupported transfer length of %u blocks!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200386 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber477e1692019-06-18 23:56:01 +0200387 __func__, count);
Nico Huber008a44f2024-04-14 23:39:47 +0200388 return -1;
Nico Huber477e1692019-06-18 23:56:01 +0200389 }
390
Nico Huber008a44f2024-04-14 23:39:47 +0200391 /* First 5 bytes are common in all generations. */
392 cmd_buf[0] = count & 0xff;
393 cmd_buf[1] = (count >> 8) & 0xff;
394 cmd_buf[2] = 0; /* RFU */
395 cmd_buf[3] = dp_spi_cmd; /* Read/Write Mode (see enums dediprog_readmode / dediprog_writemode) */
396 cmd_buf[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and
397 WRITE_MODE_4B_ADDR_256B_PAGE_PGM. */
398 return 5;
399}
Simon Glassae616512016-01-23 23:27:58 +0000400
Nico Huber008a44f2024-04-14 23:39:47 +0200401static int prepare_rw_cmd_v1(
402 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
403 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
404{
405 const int cmd_len = prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count);
406 if (cmd_len < 0)
407 return -1;
Nico Huber93db6e12018-09-30 01:18:43 +0200408
Nico Huber008a44f2024-04-14 23:39:47 +0200409 if (flash->chip->feature_bits & FEATURE_4BA_EAR_ANY) {
410 if (spi_set_extended_address(flash, start >> 24))
411 return -1;
412 } else if (start >> 24) {
413 msg_cerr("Can't handle 4-byte address with dediprog.\n");
414 return -1;
415 }
416 /*
417 * We don't know how the dediprog firmware handles 4-byte
418 * addresses. So let's not tell it what we are doing and
419 * only send the lower 3 bytes.
420 */
421 *value = start & 0xffff;
422 *idx = (start >> 16) & 0xff;
423 return cmd_len;
424}
425
426static int prepare_rw_cmd_v2(
427 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
428 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
429{
430 if (prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count) < 0)
431 return -1;
432
433 if (is_read && flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
434 cmd_buf[3] = READ_MODE_4B_ADDR_FAST_0x0C;
435 cmd_buf[4] = JEDEC_FAST_READ_4BA;
436 } else if (dp_spi_cmd == WRITE_MODE_PAGE_PGM
437 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
438 cmd_buf[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
439 cmd_buf[4] = JEDEC_BYTE_PROGRAM_4BA;
440 }
441 cmd_buf[5] = 0; /* RFU */
442 cmd_buf[6] = (start >> 0) & 0xff;
443 cmd_buf[7] = (start >> 8) & 0xff;
444 cmd_buf[8] = (start >> 16) & 0xff;
445 cmd_buf[9] = (start >> 24) & 0xff;
446 return 10;
447}
448
449static int prepare_rw_cmd_v3(
450 struct flashctx *flash, uint8_t cmd_buf[MAX_CMD_SIZE], uint16_t *value, uint16_t *idx,
451 bool is_read, uint8_t dp_spi_cmd, unsigned int start, unsigned int block_count)
452{
453 if (prepare_rw_cmd_common(cmd_buf, dp_spi_cmd, block_count) < 0)
454 return -1;
455
456 cmd_buf[5] = 0; /* RFU */
457 cmd_buf[6] = (start >> 0) & 0xff;
458 cmd_buf[7] = (start >> 8) & 0xff;
459 cmd_buf[8] = (start >> 16) & 0xff;
460 cmd_buf[9] = (start >> 24) & 0xff;
461
462 if (is_read) {
463 if (flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
464 cmd_buf[3] = READ_MODE_4B_ADDR_FAST_0x0C;
465 cmd_buf[4] = JEDEC_FAST_READ_4BA;
David Hendricksf73f8a72018-02-21 07:34:34 -0800466 }
Nico Huber008a44f2024-04-14 23:39:47 +0200467 cmd_buf[10] = 0x00; /* address length (3 or 4) */
468 cmd_buf[11] = 0x00; /* dummy cycle / 2 */
469 return 12;
Simon Glassae616512016-01-23 23:27:58 +0000470 } else {
Nico Huber008a44f2024-04-14 23:39:47 +0200471 if (dp_spi_cmd == WRITE_MODE_PAGE_PGM
472 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
473 cmd_buf[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM;
474 cmd_buf[4] = JEDEC_BYTE_PROGRAM_4BA;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100475 }
Nico Huber008a44f2024-04-14 23:39:47 +0200476 /* 16 LSBs and 16 HSBs of page size */
477 /* FIXME: This assumes page size of 256. */
478 cmd_buf[10] = 0x00;
479 cmd_buf[11] = 0x01;
480 cmd_buf[12] = 0x00;
481 cmd_buf[13] = 0x00;
482 return 14;
Simon Glassae616512016-01-23 23:27:58 +0000483 }
484}
485
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000486/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
487 * @start start address
488 * @len length
489 * @return 0 on success, 1 on failure
490 */
Simon Glassae616512016-01-23 23:27:58 +0000491static 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 +0000492{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000493 int err = 1;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100494 const struct dediprog_data *dp_data = flash->mst.spi->data;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000495
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000496 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000497 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000498 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000499
Richard Hughes842d6782021-01-15 09:48:12 +0000500 struct dediprog_transfer_status status = { flash, 0, 0, 0 };
Nico Huberd99a2bd2016-02-18 21:42:49 +0000501 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
502 struct libusb_transfer *transfer;
503
Nico Huberbbaa1712018-12-05 13:26:20 +0100504 if (len == 0)
505 return 0;
506
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000507 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000508 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200509 "Please report a bug at flashprog@flashprog.org\n",
Simon Glassae616512016-01-23 23:27:58 +0000510 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000511 return 1;
512 }
513
Nico Huber008a44f2024-04-14 23:39:47 +0200514 uint16_t value = 0, idx = 0;
515 uint8_t data_packet[MAX_CMD_SIZE];
516 const int command_packet_size = dp_data->prepare_rw_cmd(
517 flash, data_packet, &value, &idx, /* is_read => */true, READ_MODE_STD, start, count);
518 if (command_packet_size < 0)
Nico Huber7eb38aa2019-03-21 15:42:54 +0100519 return 1;
Simon Glassae616512016-01-23 23:27:58 +0000520
Nico Huber008a44f2024-04-14 23:39:47 +0200521 int ret = dediprog_write(dp_data->handle, CMD_READ, value, idx, data_packet, command_packet_size);
522 if (ret != command_packet_size) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000523 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000524 return 1;
525 }
526
Nico Huberd99a2bd2016-02-18 21:42:49 +0000527 /*
528 * Ring buffer of bulk transfers.
529 * Poll until at least one transfer is ready,
530 * schedule next transfers until buffer is full.
531 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000532
Nico Huberd99a2bd2016-02-18 21:42:49 +0000533 /* Allocate bulk transfers. */
534 unsigned int i;
Nico Huber519be662018-12-23 20:03:35 +0100535 for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000536 transfers[i] = libusb_alloc_transfer(0);
537 if (!transfers[i]) {
538 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
539 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200540 }
541 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000542
543 /* Now transfer requested chunks using libusb's asynchronous interface. */
544 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200545 while ((status.queued_idx < count) &&
546 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
547 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000548 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
Nico Huber821a0852024-03-24 13:34:51 +0100549 libusb_fill_bulk_transfer(transfer, dp_data->handle, dp_data->in_endpoint,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000550 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
551 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
552 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
553 ret = libusb_submit_transfer(transfer);
554 if (ret < 0) {
555 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
556 status.queued_idx, libusb_error_name(ret));
557 goto err_free;
558 }
559 ++status.queued_idx;
560 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000561 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 0))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000562 goto err_free;
563 }
564 /* Wait for transfers to finish. */
Rick Altherr4f9cd8b2021-12-13 17:10:00 -0800565 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000566 goto err_free;
567 /* Check if everything has been transmitted. */
568 if ((status.finished_idx < count) || status.error)
569 goto err_free;
570
571 err = 0;
572
573err_free:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000574 dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000575 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
576 if (transfers[i]) libusb_free_transfer(transfers[i]);
577 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000578}
579
Simon Glassae616512016-01-23 23:27:58 +0000580static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000581{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000582 int ret;
583 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000584 const unsigned int chunksize = 0x200;
Nico Huberbbaa1712018-12-05 13:26:20 +0100585 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000586 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100587 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000588
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000589 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000590
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000591 if (residue) {
592 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
593 start, residue);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100594 ret = default_spi_read(flash, buf, start, residue);
Simon Glass25e9f402015-06-28 13:31:19 +0000595 if (ret)
596 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000597 }
598
599 /* Round down. */
600 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000601 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000602 if (ret)
603 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000604
605 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000606 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000607 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
608 start, len);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100609 ret = default_spi_read(flash, buf + residue + bulklen,
610 start + residue + bulklen, len);
Simon Glass25e9f402015-06-28 13:31:19 +0000611 if (ret)
612 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000613 }
614
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000615 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000616 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000617err:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000618 dediprog_set_leds(LED_ERROR, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +0000619 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000620}
621
Nico Hubera4b14f72012-06-19 12:06:53 +0000622/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
623 * @chunksize length of data chunks, only 256 supported by now
624 * @start start address
625 * @len length
626 * @dedi_spi_cmd dediprog specific write command for spi bus
627 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000628 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000629static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000630 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000631{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000632 /* USB transfer size must be 512, other sizes will NOT work at all.
633 * chunksize is the real data size per USB bulk transfer. The remaining
634 * space in a USB bulk transfer must be filled with 0xff padding.
635 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000636 const unsigned int count = len / chunksize;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100637 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000638
Nico Hubera4b14f72012-06-19 12:06:53 +0000639 /*
640 * We should change this check to
641 * chunksize > 512
642 * once we know how to handle different chunk sizes.
643 */
644 if (chunksize != 256) {
645 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200646 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000647 __func__, chunksize);
Nico Hubera4b14f72012-06-19 12:06:53 +0000648 return 1;
649 }
650
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000651 if ((start % chunksize) || (len % chunksize)) {
Nico Huberac90af62022-12-18 00:22:47 +0000652 msg_perr("%s: Unaligned start=%i, len=%i!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200653 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000654 __func__, start, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000655 return 1;
656 }
657
658 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000659 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000660 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000661
Nico Huber008a44f2024-04-14 23:39:47 +0200662 uint16_t value = 0, idx = 0;
663 uint8_t data_packet[MAX_CMD_SIZE];
664 const int command_packet_size = dp_data->prepare_rw_cmd(
665 flash, data_packet, &value, &idx, /* is_read => */false, dedi_spi_cmd, start, count);
666 if (command_packet_size < 0)
David Hendricksf73f8a72018-02-21 07:34:34 -0800667 return 1;
David Hendricksf73f8a72018-02-21 07:34:34 -0800668
Nico Huber008a44f2024-04-14 23:39:47 +0200669 int ret = dediprog_write(dp_data->handle, CMD_WRITE, value, idx, data_packet, command_packet_size);
670 if (ret != command_packet_size) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000671 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000672 return 1;
673 }
674
Simon Glassae616512016-01-23 23:27:58 +0000675 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000676 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000677 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000678 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000679 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000680 int transferred;
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000681 ret = libusb_bulk_transfer(dp_data->handle, dp_data->out_endpoint, usbbuf, 512, &transferred,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000682 DEFAULT_TIMEOUT);
683 if ((ret < 0) || (transferred != 512)) {
684 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000685 return 1;
686 }
Richard Hughes842d6782021-01-15 09:48:12 +0000687 flashprog_progress_add(flash, chunksize);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000688 }
689
690 return 0;
691}
692
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000693static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000694 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000695{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000696 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000697 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000698 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
699 unsigned int bulklen;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100700 const struct dediprog_data *dp_data = flash->mst.spi->data;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000701
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000702 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000703
Nico Hubera4b14f72012-06-19 12:06:53 +0000704 if (chunksize != 256) {
705 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
706 "we don't know how dediprog\nhandles them.\n");
707 /* Write everything like it was residue. */
708 residue = len;
709 }
710
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000711 if (residue) {
712 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
713 start, residue);
Nico Huber93db6e12018-09-30 01:18:43 +0200714 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
Nico Hubere36e3dc2023-04-28 21:23:08 +0000715 ret = default_spi_write_256(flash, buf, start, residue);
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 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000721
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000722 /* Round down. */
723 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000724 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000725 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000726 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000727 return ret;
728 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000729
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000730 len -= residue + bulklen;
731 if (len) {
732 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
733 start, len);
Nico Hubere36e3dc2023-04-28 21:23:08 +0000734 ret = default_spi_write_256(flash, buf + residue + bulklen, start + residue + bulklen, len);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000735 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000736 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000737 return ret;
738 }
739 }
740
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000741 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000742 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000743}
744
Nico Hubera96aaa32024-03-05 12:56:13 +0100745static int dediprog_spi_write_chunked(struct flashctx *flash, const uint8_t *buf,
746 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
747{
748 /* We can write only up to 65535 pages at once: */
749 while (len) {
750 const size_t len_here = MIN(len, flash->chip->page_size * MAX_BLOCK_COUNT);
751 const int ret = dediprog_spi_write(flash, buf, start, len_here, dedi_spi_cmd);
752 if (ret)
753 return ret;
754
755 start += len_here;
756 buf += len_here;
757 len -= len_here;
758 }
759 return 0;
760}
761
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000762static 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 +0000763{
Nico Hubera96aaa32024-03-05 12:56:13 +0100764 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000765}
766
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000767static 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 +0000768{
Nico Hubera96aaa32024-03-05 12:56:13 +0100769 return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000770}
771
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000772static int dediprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000773 unsigned int writecnt,
774 unsigned int readcnt,
775 const unsigned char *writearr,
776 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000777{
778 int ret;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100779 const struct dediprog_data *dp_data = flash->mst.spi->data;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000780
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000781 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Nico Hubere36e3dc2023-04-28 21:23:08 +0000782 if (writecnt > flash->mst.spi->max_data_write + 5) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000783 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000784 return 1;
785 }
Nico Huber9a11cbf2023-01-13 01:19:07 +0100786 if (readcnt > flash->mst.spi->max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000787 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000788 return 1;
789 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100790
Simon Glassae616512016-01-23 23:27:58 +0000791 unsigned int idx, value;
792 /* New protocol has options and timeout combined as value while the old one used the value field for
793 * timeout and the index field for options. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000794 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000795 idx = 0;
796 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
797 } else {
798 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
799 value = 0;
800 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000801 ret = dediprog_write(dp_data->handle, CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Nico Huber519be662018-12-23 20:03:35 +0100802 if (ret != (int)writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000803 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000804 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000805 return 1;
806 }
Simon Glassae616512016-01-23 23:27:58 +0000807 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000808 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000809
Stefan Tauner74367bf2016-02-20 20:53:46 +0000810 /* The specifications do state the possibility to set a timeout for transceive transactions.
811 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
812 * can predict the time needed by the previous command or so (untested). In any case, using this
813 * "feature" to set sane-looking timouts for the read below will completely trash performance with
814 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
815 *
816 * The specification also uses only 0 in its examples, so the lesson to learn here:
817 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000818 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800819 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000820 idx = 0;
821 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
822 } else {
823 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
824 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
825 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000826 ret = dediprog_read(dp_data->dediprog_handle, CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000827 */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000828 ret = dediprog_read(dp_data->handle, CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Nico Huber519be662018-12-23 20:03:35 +0100829 if (ret != (int)readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000830 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 +0000831 return 1;
832 }
833 return 0;
834}
835
Nico Huber823a7042024-03-24 18:32:58 +0100836static int dediprog_read_devicestring(struct dediprog_data *dp_data, bool warn)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000837{
Nico Huber274e6552024-03-24 12:34:57 +0100838 const int devstr_len = sizeof(dp_data->devicestring) - 1, old_devstr_len = 16;
839 char *const buf = dp_data->devicestring;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000840 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000841
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000842 /* Command Receive Device String. */
Nico Hubere76e21f2024-02-29 23:33:35 +0100843 ret = dediprog_read(dp_data->handle, CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, devstr_len);
Nico Huber5262e292024-03-02 13:21:25 +0100844 if (ret < old_devstr_len || ret > devstr_len) {
Nico Huber823a7042024-03-24 18:32:58 +0100845 if (warn)
846 msg_perr("Incomplete/failed Command Receive Device String!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000847 return 1;
848 }
Nico Huber5262e292024-03-02 13:21:25 +0100849 buf[ret] = '\0';
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000850 msg_pdbg("Found a %s\n", buf);
Nico Hubere76e21f2024-02-29 23:33:35 +0100851 if (memcmp(buf, "SF100", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000852 dp_data->devicetype = DEV_SF100;
Nico Hubere76e21f2024-02-29 23:33:35 +0100853 else if (memcmp(buf, "SF200", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000854 dp_data->devicetype = DEV_SF200;
Nico Huber00578222024-03-02 14:00:53 +0100855 else if (memcmp(buf, "SF600PG2", 8) == 0) /* match first, before shorter, generic SF600 */
856 dp_data->devicetype = DEV_SF600PG2;
Nico Hubere76e21f2024-02-29 23:33:35 +0100857 else if (memcmp(buf, "SF600", 5) == 0)
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000858 dp_data->devicetype = DEV_SF600;
Nico Huber00578222024-03-02 14:00:53 +0100859 else if (memcmp(buf, "SF700", 5) == 0)
860 dp_data->devicetype = DEV_SF700;
Nico Huber274e6552024-03-24 12:34:57 +0100861 else
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000862 return 1;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000863
Nico Huber274e6552024-03-24 12:34:57 +0100864 return 0;
865}
866
867static int dediprog_check_devicestring(struct dediprog_data *dp_data)
868{
869 char *const buf = dp_data->devicestring;
Nico Huberbdef5c22024-03-02 13:51:39 +0100870 unsigned int sfnum;
871 unsigned int fw[3];
Nico Huber274e6552024-03-24 12:34:57 +0100872
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100873 if (sscanf(buf, "SF%u", &sfnum) != 1 ||
Nico Huber00578222024-03-02 14:00:53 +0100874 sfnum != dp_data->devicetype / 100 * 100 ||
Nico Huber0ab5c3d2024-03-02 13:53:16 +0100875 sscanf(buf, "SF%*s V:%u.%u.%u ", &fw[0], &fw[1], &fw[2]) != 3) {
Simon Glassae616512016-01-23 23:27:58 +0000876 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000877 return 1;
878 }
Nico Huber00578222024-03-02 14:00:53 +0100879
880 /* Only allow major versions that were tested. */
881 if ((dp_data->devicetype == DEV_SF600PG2 && fw[0] > 1) ||
882 (dp_data->devicetype == DEV_SF700 && fw[0] != 4) ||
883 (dp_data->devicetype <= DEV_SF600 && (fw[0] < 2 || fw[0] > 7))) {
Simon Glassae616512016-01-23 23:27:58 +0000884 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000885 return 1;
886 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800887
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000888 dp_data->firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000889 if (protocol(dp_data) == PROTOCOL_UNKNOWN) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800890 msg_perr("Internal error: Unable to determine protocol version.\n");
891 return 1;
892 }
Simon Glassae616512016-01-23 23:27:58 +0000893
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000894 return 0;
895}
896
David Hendricksc05900f2016-02-18 21:42:41 +0000897/*
Ryan O'Leary301ae222019-06-24 19:14:33 -0700898 * Read the id from the dediprog. This should return the numeric part of the
899 * serial number found on a sticker on the back of the dediprog. Note this
Nico Huber821a0852024-03-24 13:34:51 +0100900 * number is stored in writable eeprom, so it could get out of sync.
Ryan O'Leary301ae222019-06-24 19:14:33 -0700901 * @return the id on success, -1 on failure
902 */
Nico Huber821a0852024-03-24 13:34:51 +0100903static int dediprog_read_id(struct dediprog_data *const dp)
Ryan O'Leary301ae222019-06-24 19:14:33 -0700904{
Nico Huber821a0852024-03-24 13:34:51 +0100905 const int min_len = 3;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700906 int ret;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700907
Nico Huber821a0852024-03-24 13:34:51 +0100908 if (dp->devicetype >= DEV_SF600PG2) {
909 const uint8_t out[6] = { 0x00, 0x00, 0x00, 0x02, 0x00, 0x00 };
910 const uint8_t cmd = 0x71;
911 uint8_t buf[512];
912 int transferred;
913 int try;
914
915 /* Always query the id twice as the endpoint
916 can lock up in mysterious ways otherwise. */
917 for (try = 0; try < 2; ++try) {
918 ret = dediprog_write(dp->handle, cmd, 0, 0, out, sizeof(out));
919 if (ret != (int)sizeof(out))
920 goto failed_ret;
921
922 ret = libusb_bulk_transfer(dp->handle, dp->in_endpoint,
923 buf, sizeof(buf), &transferred, DEFAULT_TIMEOUT);
924 }
925
926 if (ret == 0 && transferred >= min_len)
927 return buf[2] << 16 | buf[1] << 8 | buf[0];
928 } else {
929 uint8_t buf[16];
930
931 if (dp->devicetype >= DEV_SF600) {
932 ret = dediprog_read(dp->handle, CMD_READ_EEPROM, 0, 0, buf, sizeof(buf));
933 } else {
934 ret = libusb_control_transfer(dp->handle, REQTYPE_OTHER_IN,
935 0x7, /* request */
936 0, /* value */
937 0xEF00, /* index */
938 buf, min_len, DEFAULT_TIMEOUT);
939 }
940
941 if (ret >= min_len)
942 return buf[0] << 16 | buf[1] << 8 | buf[2];
Ryan O'Leary301ae222019-06-24 19:14:33 -0700943 }
944
Nico Huber821a0852024-03-24 13:34:51 +0100945failed_ret:
946 msg_perr("Failed to read dediprog id: ");
947 if (ret < 0)
948 msg_perr("%s (%d)\n", libusb_strerror(ret), ret);
949 else
950 msg_perr("short read!\n");
951 return -1;
Ryan O'Leary301ae222019-06-24 19:14:33 -0700952}
953
954/*
David Hendricksc05900f2016-02-18 21:42:41 +0000955 * This command presumably sets the voltage for the SF100 itself (not the
956 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
957 * (including all SF600s) do not support it.
958 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000959
David Hendricksc05900f2016-02-18 21:42:41 +0000960/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
961 * Only use dediprog_set_voltage on SF100 programmers with firmware older
962 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000963static int dediprog_set_voltage(libusb_device_handle *dediprog_handle)
David Hendricksc05900f2016-02-18 21:42:41 +0000964{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000965 unsigned char buf[1] = {0};
966 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000967 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000968 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000969 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000970 return 1;
971 }
David Hendricksc05900f2016-02-18 21:42:41 +0000972 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000973 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000974 return 1;
975 }
David Hendricksc05900f2016-02-18 21:42:41 +0000976
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000977 return 0;
978}
979
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000980static int dediprog_standalone_mode(const struct dediprog_data *dp_data)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000981{
982 int ret;
983
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000984 if (dp_data->devicetype != DEV_SF600)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000985 return 0;
986
987 msg_pdbg2("Disabling standalone mode.\n");
Anastasia Klimchuk7d973882021-07-14 09:33:50 +1000988 ret = dediprog_write(dp_data->handle, CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000989 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000990 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000991 return 1;
992 }
993
994 return 0;
995}
996
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000997#if 0
998/* Something.
999 * Present in eng_detect_blink.log with firmware 3.1.8
1000 * Always preceded by Command Receive Device String
1001 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001002static int dediprog_command_b(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001003{
1004 int ret;
1005 char buf[0x3];
1006
Simon Glass557eb4f2015-07-05 16:53:22 +00001007 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
1008 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001009 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001010 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001011 return 1;
1012 }
1013 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
1014 (buf[2] != 0xff)) {
1015 msg_perr("Unexpected response to Command B!\n");
1016 return 1;
1017 }
1018
1019 return 0;
1020}
1021#endif
1022
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001023static int set_target_flash(libusb_device_handle *dediprog_handle, enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001024{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001025 int ret = dediprog_write(dediprog_handle, CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +00001026 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001027 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001028 return 1;
1029 }
1030 return 0;
1031}
1032
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001033#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +00001034/* Returns true if the button is currently pressed. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001035static bool dediprog_get_button(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001036{
Simon Glass557eb4f2015-07-05 16:53:22 +00001037 char buf[1];
1038 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
1039 buf, 0x1, DEFAULT_TIMEOUT);
1040 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001041 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +00001042 return 1;
1043 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001044 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001045}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001046#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001047
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001048static int parse_voltage(char *voltage)
1049{
1050 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001051 int i;
1052 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001053
1054 if (!voltage || !strlen(voltage)) {
1055 msg_perr("Empty voltage= specified.\n");
1056 return -1;
1057 }
1058 millivolt = (int)strtol(voltage, &tmp, 0);
1059 voltage = tmp;
1060 /* Handle "," and "." as decimal point. Everything after it is assumed
1061 * to be in decimal notation.
1062 */
1063 if ((*voltage == '.') || (*voltage == ',')) {
1064 voltage++;
1065 for (i = 0; i < 3; i++) {
1066 fraction *= 10;
1067 /* Don't advance if the current character is invalid,
1068 * but continue multiplying.
1069 */
1070 if ((*voltage < '0') || (*voltage > '9'))
1071 continue;
1072 fraction += *voltage - '0';
1073 voltage++;
1074 }
1075 /* Throw away remaining digits. */
1076 voltage += strspn(voltage, "0123456789");
1077 }
1078 /* The remaining string must be empty or "mV" or "V". */
1079 tolower_string(voltage);
1080
1081 /* No unit or "V". */
1082 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1083 millivolt *= 1000;
1084 millivolt += fraction;
1085 } else if (!strncmp(voltage, "mv", 2) ||
1086 !strncmp(voltage, "milliv", 6)) {
1087 /* No adjustment. fraction is discarded. */
1088 } else {
1089 /* Garbage at the end of the string. */
1090 msg_perr("Garbage voltage= specified.\n");
1091 return -1;
1092 }
1093 return millivolt;
1094}
1095
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001096static int dediprog_shutdown(void *data);
1097
Nico Huber93db6e12018-09-30 01:18:43 +02001098static struct spi_master spi_master_dediprog = {
Nico Huberdc5af542018-12-22 16:54:59 +01001099 .features = SPI_MASTER_NO_4BA_MODES,
Simon Glassae616512016-01-23 23:27:58 +00001100 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
Nico Hubere36e3dc2023-04-28 21:23:08 +00001101 .max_data_write = 16 - 5, /* Account for 5 bytes cmd/addr. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001102 .command = dediprog_spi_send_command,
1103 .multicommand = default_spi_send_multicommand,
1104 .read = dediprog_spi_read,
1105 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +00001106 .write_aai = dediprog_spi_write_aai,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +10001107 .shutdown = dediprog_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301108 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001109};
1110
Ryan O'Leary301ae222019-06-24 19:14:33 -07001111/*
1112 * Open a dediprog_handle with the USB device at the given index.
1113 * @index index of the USB device
Nico Huber274e6552024-03-24 12:34:57 +01001114 * @return 0 for success, -1 for error, -2 for busy device, -3 for unknown device
Ryan O'Leary301ae222019-06-24 19:14:33 -07001115 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001116static int dediprog_open(int index, struct dediprog_data *dp_data)
Ryan O'Leary301ae222019-06-24 19:14:33 -07001117{
1118 const uint16_t vid = devs_dediprog[0].vendor_id;
1119 const uint16_t pid = devs_dediprog[0].device_id;
1120 int ret;
1121
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001122 dp_data->handle = usb_dev_get_by_vid_pid_number(dp_data->usb_ctx, vid, pid, (unsigned int) index);
1123 if (!dp_data->handle) {
Ryan O'Leary301ae222019-06-24 19:14:33 -07001124 msg_perr("Could not find a Dediprog programmer on USB.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001125 libusb_exit(dp_data->usb_ctx);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001126 return -1;
1127 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001128 ret = libusb_set_configuration(dp_data->handle, 1);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001129 if (ret != 0) {
1130 msg_perr("Could not set USB device configuration: %i %s\n",
1131 ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001132 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001133 return -2;
1134 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001135 ret = libusb_claim_interface(dp_data->handle, 0);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001136 if (ret < 0) {
1137 msg_perr("Could not claim USB device interface %i: %i %s\n",
1138 0, ret, libusb_error_name(ret));
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001139 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001140 return -2;
1141 }
Nico Huber274e6552024-03-24 12:34:57 +01001142
1143 /* Try reading the devicestring. If that fails and the device is old
1144 (FW < 6.0.0, which we cannot know), then we need to try the "set
1145 voltage" command and then attempt to read the devicestring again. */
Nico Huber823a7042024-03-24 18:32:58 +01001146 if (dediprog_read_devicestring(dp_data, /* warn => */false)) {
Nico Huber274e6552024-03-24 12:34:57 +01001147 if (dediprog_set_voltage(dp_data->handle))
1148 goto unknown_dev;
Nico Huber823a7042024-03-24 18:32:58 +01001149 if (dediprog_read_devicestring(dp_data, /* warn => */true))
Nico Huber274e6552024-03-24 12:34:57 +01001150 goto unknown_dev;
1151 }
Nico Huber821a0852024-03-24 13:34:51 +01001152
1153 dp_data->in_endpoint = LIBUSB_ENDPOINT_IN | 2;
1154 if (dp_data->devicetype <= DEV_SF200)
1155 dp_data->out_endpoint = 2;
1156 else
1157 dp_data->out_endpoint = 1;
1158
Ryan O'Leary301ae222019-06-24 19:14:33 -07001159 return 0;
Nico Huber274e6552024-03-24 12:34:57 +01001160
1161unknown_dev:
1162 msg_pwarn("Ignoring unknown Dediprog device. Not a SF100, SF200, SF600(Plus(G2)), or SF700!\n");
1163 libusb_release_interface(dp_data->handle, 0);
1164 libusb_close(dp_data->handle);
1165 return -3;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001166}
1167
David Hendricks8bb20212011-06-14 01:35:36 +00001168static int dediprog_shutdown(void *data)
1169{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001170 int ret = 0;
1171 struct dediprog_data *dp_data = data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001172
David Hendricks8bb20212011-06-14 01:35:36 +00001173 /* URB 28. Command Set SPI Voltage to 0. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001174 if (dediprog_set_spi_voltage(dp_data->handle, 0x0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001175 ret = 1;
1176 goto out;
David Hendricks8bb20212011-06-14 01:35:36 +00001177 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001178
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001179 if (libusb_release_interface(dp_data->handle, 0)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001180 msg_perr("Could not release USB interface!\n");
1181 ret = 1;
1182 goto out;
1183 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001184 libusb_close(dp_data->handle);
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001185 libusb_exit(dp_data->usb_ctx);
1186out:
1187 free(data);
1188 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +00001189}
1190
Nico Hubere3a26882023-01-11 21:45:51 +01001191static int dediprog_init(struct flashprog_programmer *const prog)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001192{
Ryan O'Leary301ae222019-06-24 19:14:33 -07001193 char *voltage, *id_str, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +00001194 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001195 int millivolt = 3500;
Nico Hubere8463c82024-03-24 16:50:50 +01001196 long id = -1; /* -1 defaults to enumeration order */
Ryan O'Leary301ae222019-06-24 19:14:33 -07001197 int found_id;
Nathan Laredo21541a62012-12-24 22:07:36 +00001198 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +02001199 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +00001200 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001201
Nico Huber77fa67d2013-02-20 18:03:36 +00001202 spispeed = extract_programmer_param("spispeed");
1203 if (spispeed) {
1204 for (i = 0; spispeeds[i].name; ++i) {
1205 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1206 spispeed_idx = i;
1207 break;
1208 }
1209 }
1210 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +00001211 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +00001212 free(spispeed);
1213 return 1;
1214 }
1215 free(spispeed);
1216 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001217
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001218 voltage = extract_programmer_param("voltage");
1219 if (voltage) {
1220 millivolt = parse_voltage(voltage);
1221 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001222 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001223 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001224 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1225 }
1226
Ryan O'Leary301ae222019-06-24 19:14:33 -07001227 id_str = extract_programmer_param("id");
1228 if (id_str) {
Nico Hubere8463c82024-03-24 16:50:50 +01001229 const char prefixes[][4] = { "SF", "DP", "S6B", };
1230 for (i = 0; i < (int)ARRAY_SIZE(prefixes); ++i) {
1231 if (!strncmp(id_str, prefixes[i], strlen(prefixes[i])))
1232 break;
1233 }
1234 if (i >= (int)ARRAY_SIZE(prefixes)) {
1235 msg_perr("Error: Could not parse dediprog `id'.\n");
1236 msg_perr("Expected a string prefixed with any of ");
1237 for (i = 0; i < (int)ARRAY_SIZE(prefixes); ++i)
1238 msg_perr("%s`%s'", i > 0 ? ", " : "", prefixes[i]);
1239 msg_perr(".\n");
1240 free(id_str);
1241 return 1;
1242 }
1243 char *endptr;
1244 id = strtol(id_str + strlen(prefixes[i]), &endptr, 10);
1245 if (strlen(id_str) <= strlen(prefixes[i]) || *endptr) {
1246 msg_perr("Error: Could not parse dediprog `id'.\n");
1247 msg_perr("Expected a number after string prefix.\n");
Ryan O'Leary301ae222019-06-24 19:14:33 -07001248 free(id_str);
1249 return 1;
1250 }
1251 if (id < 0 || id >= 0x1000000) {
Nico Hubere8463c82024-03-24 16:50:50 +01001252 msg_perr("Error: id `%s' is out of range!\n", id_str);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001253 free(id_str);
1254 return 1;
1255 }
1256 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1257 }
1258 free(id_str);
1259
Nathan Laredo21541a62012-12-24 22:07:36 +00001260 device = extract_programmer_param("device");
1261 if (device) {
1262 char *dev_suffix;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001263 if (id != -1) {
1264 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1265 }
Nathan Laredo21541a62012-12-24 22:07:36 +00001266 errno = 0;
1267 usedevice = strtol(device, &dev_suffix, 10);
1268 if (errno != 0 || device == dev_suffix) {
1269 msg_perr("Error: Could not convert 'device'.\n");
1270 free(device);
1271 return 1;
1272 }
Nico Huber961f4a12019-10-04 17:34:22 +02001273 if (usedevice < 0 || usedevice > INT_MAX) {
Nathan Laredo21541a62012-12-24 22:07:36 +00001274 msg_perr("Error: Value for 'device' is out of range.\n");
1275 free(device);
1276 return 1;
1277 }
1278 if (strlen(dev_suffix) > 0) {
1279 msg_perr("Error: Garbage following 'device' value.\n");
1280 free(device);
1281 return 1;
1282 }
1283 msg_pinfo("Using device %li.\n", usedevice);
1284 }
1285 free(device);
1286
Stefan Taunere659d2d2013-05-03 21:58:28 +00001287 target_str = extract_programmer_param("target");
1288 if (target_str) {
1289 char *target_suffix;
1290 errno = 0;
1291 target = strtol(target_str, &target_suffix, 10);
1292 if (errno != 0 || target_str == target_suffix) {
1293 msg_perr("Error: Could not convert 'target'.\n");
1294 free(target_str);
1295 return 1;
1296 }
1297 if (target < 1 || target > 2) {
1298 msg_perr("Error: Value for 'target' is out of range.\n");
1299 free(target_str);
1300 return 1;
1301 }
1302 if (strlen(target_suffix) > 0) {
1303 msg_perr("Error: Garbage following 'target' value.\n");
1304 free(target_str);
1305 return 1;
1306 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001307 switch (target) {
1308 case 1:
1309 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1310 target = FLASH_TYPE_APPLICATION_FLASH_1;
1311 break;
1312 case 2:
1313 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1314 target = FLASH_TYPE_APPLICATION_FLASH_2;
1315 break;
1316 default:
1317 break;
1318 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001319 }
1320 free(target_str);
1321
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001322 struct dediprog_data *dp_data = calloc(1, sizeof(*dp_data));
1323 if (!dp_data) {
1324 msg_perr("Unable to allocate space for SPI master data\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001325 return 1;
1326 }
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001327 dp_data->firmwareversion = FIRMWARE_VERSION(0, 0, 0);
1328 dp_data->devicetype = DEV_UNKNOWN;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001329
1330 /* Here comes the USB stuff. */
Thomas Heijligen88e87c52022-08-05 17:56:20 +02001331 ret = libusb_init(&dp_data->usb_ctx);
1332 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001333 msg_perr("Could not initialize libusb!\n");
1334 goto init_err_exit;
1335 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001336
Ryan O'Leary301ae222019-06-24 19:14:33 -07001337 if (id != -1) {
1338 for (i = 0; ; i++) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001339 ret = dediprog_open(i, dp_data);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001340 if (ret == -1) {
1341 /* no dev */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001342 goto init_err_exit;
Nico Huber274e6552024-03-24 12:34:57 +01001343 } else if (ret < 0) {
1344 /* busy or unknown dev */
Ryan O'Leary301ae222019-06-24 19:14:33 -07001345 continue;
1346 }
1347
1348 /* Notice we can only call dediprog_read_id() after
1349 * libusb_set_configuration() and
1350 * libusb_claim_interface(). When searching by id and
1351 * either configuration or claim fails (usually the
Nico Huberc3b02dc2023-08-12 01:13:45 +02001352 * device is in use by another instance of flashprog),
Ryan O'Leary301ae222019-06-24 19:14:33 -07001353 * the device is skipped and the next device is tried.
1354 */
Nico Huber821a0852024-03-24 13:34:51 +01001355 found_id = dediprog_read_id(dp_data);
Nico Huber38af1a12024-03-24 15:04:20 +01001356 if (found_id >= 0)
1357 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1358 if (found_id == id)
1359 break;
1360
1361 libusb_release_interface(dp_data->handle, 0);
1362 libusb_close(dp_data->handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001363 }
1364 } else {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001365 if (dediprog_open(usedevice, dp_data)) {
1366 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001367 }
Nico Huber821a0852024-03-24 13:34:51 +01001368 found_id = dediprog_read_id(dp_data);
David Woodhoused2a7e872013-07-30 09:34:44 +00001369 }
Ryan O'Leary301ae222019-06-24 19:14:33 -07001370
1371 if (found_id >= 0) {
1372 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001373 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001374
Nico Huber274e6552024-03-24 12:34:57 +01001375 if (dediprog_check_devicestring(dp_data))
1376 goto init_err_cleanup_exit;
Nico Huber77fa67d2013-02-20 18:03:36 +00001377
Simon Glass25e9f402015-06-28 13:31:19 +00001378 /* Set all possible LEDs as soon as possible to indicate activity.
1379 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001380 * dediprog_check_devicestring() has queried the device. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001381 dediprog_set_leds(LED_ALL, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +00001382
Simon Glass557eb4f2015-07-05 16:53:22 +00001383 /* Select target/socket, frequency and VCC. */
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001384 if (set_target_flash(dp_data->handle, target) ||
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001385 dediprog_set_spi_speed(spispeed_idx, dp_data) ||
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001386 dediprog_set_spi_voltage(dp_data->handle, millivolt)) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001387 dediprog_set_leds(LED_ERROR, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001388 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001389 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001390
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001391 if (dediprog_standalone_mode(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001392 goto init_err_cleanup_exit;
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001393
Nico Huber008a44f2024-04-14 23:39:47 +02001394 switch (protocol(dp_data)) {
1395 case PROTOCOL_V3: dp_data->prepare_rw_cmd = prepare_rw_cmd_v3; break;
1396 case PROTOCOL_V2: dp_data->prepare_rw_cmd = prepare_rw_cmd_v2; break;
1397 default: dp_data->prepare_rw_cmd = prepare_rw_cmd_v1; break;
1398 }
1399
Anastasia Klimchuk7d973882021-07-14 09:33:50 +10001400 if ((dp_data->devicetype == DEV_SF100) ||
1401 (dp_data->devicetype == DEV_SF600 && protocol(dp_data) == PROTOCOL_V3))
Nico Huber7eb38aa2019-03-21 15:42:54 +01001402 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1403
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001404 if (protocol(dp_data) >= PROTOCOL_V2)
Nico Huber93db6e12018-09-30 01:18:43 +02001405 spi_master_dediprog.features |= SPI_MASTER_4BA;
1406
Angel Pons116d56d2021-03-22 11:28:00 +01001407 if (dediprog_set_leds(LED_NONE, dp_data))
1408 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001409
Nico Huber89569d62023-01-12 23:31:40 +01001410 return register_spi_master(&spi_master_dediprog, 0, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001411
1412init_err_cleanup_exit:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001413 dediprog_shutdown(dp_data);
1414 return 1;
1415
1416init_err_exit:
1417 free(dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001418 return 1;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001419}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001420
1421const struct programmer_entry programmer_dediprog = {
1422 .name = "dediprog",
1423 .type = USB,
1424 .devs.dev = devs_dediprog,
1425 .init = dediprog_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001426};