blob: 2ce06c62b8a27333104960a49f501c3d03e218e6 [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
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000018#include "platform.h"
19
Nico Huber4d440a72017-08-15 11:26:48 +020020#include <sys/types.h>
Stefan Tauner5c316f92015-02-08 21:57:52 +000021#include <stdlib.h>
Mathias Krausedb7afc52010-11-09 23:30:43 +000022#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000023#include <string.h>
Stefan Taunere34e3e82013-01-01 00:06:51 +000024#include <limits.h>
Nathan Laredo21541a62012-12-24 22:07:36 +000025#include <errno.h>
Nico Huberd99a2bd2016-02-18 21:42:49 +000026#include <libusb.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000027#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000028#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000029#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000030#include "spi.h"
31
Nico Huberd99a2bd2016-02-18 21:42:49 +000032/* LIBUSB_CALL ensures the right calling conventions on libusb callbacks.
33 * However, the macro is not defined everywhere. m(
34 */
35#ifndef LIBUSB_CALL
36#define LIBUSB_CALL
37#endif
38
Stefan Reinauer915b8402011-01-28 09:00:15 +000039#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000040#define DEFAULT_TIMEOUT 3000
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,
52};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000053
Simon Glass25e9f402015-06-28 13:31:19 +000054enum dediprog_leds {
55 LED_INVALID = -1,
56 LED_NONE = 0,
57 LED_PASS = 1 << 0,
58 LED_BUSY = 1 << 1,
59 LED_ERROR = 1 << 2,
60 LED_ALL = 7,
61};
62
Simon Glass557eb4f2015-07-05 16:53:22 +000063/* IO bits for CMD_SET_IO_LED message */
64enum dediprog_ios {
65 IO1 = 1 << 0,
66 IO2 = 1 << 1,
67 IO3 = 1 << 2,
68 IO4 = 1 << 3,
69};
70
71enum dediprog_cmds {
72 CMD_TRANSCEIVE = 0x01,
73 CMD_POLL_STATUS_REG = 0x02,
74 CMD_SET_VPP = 0x03,
75 CMD_SET_TARGET = 0x04,
76 CMD_READ_EEPROM = 0x05,
77 CMD_WRITE_EEPROM = 0x06,
78 CMD_SET_IO_LED = 0x07,
79 CMD_READ_PROG_INFO = 0x08,
80 CMD_SET_VCC = 0x09,
81 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000082 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000083 CMD_GET_BUTTON = 0x11,
84 CMD_GET_UID = 0x12,
85 CMD_SET_CS = 0x14,
86 CMD_IO_MODE = 0x15,
87 CMD_FW_UPDATE = 0x1A,
88 CMD_FPGA_UPDATE = 0x1B,
89 CMD_READ_FPGA_VERSION = 0x1C,
90 CMD_SET_HOLD = 0x1D,
91 CMD_READ = 0x20,
92 CMD_WRITE = 0x30,
93 CMD_WRITE_AT45DB = 0x31,
94 CMD_NAND_WRITE = 0x32,
95 CMD_NAND_READ = 0x33,
96 CMD_SET_SPI_CLK = 0x61,
97 CMD_CHECK_SOCKET = 0x62,
98 CMD_DOWNLOAD_PRJ = 0x63,
99 CMD_READ_PRJ_NAME = 0x64,
100 // New protocol/firmware only
101 CMD_CHECK_SDCARD = 0x65,
102 CMD_READ_PRJ = 0x66,
103};
104
105enum dediprog_target {
106 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
107 FLASH_TYPE_FLASH_CARD,
108 FLASH_TYPE_APPLICATION_FLASH_2,
109 FLASH_TYPE_SOCKET,
110};
111
112enum dediprog_readmode {
113 READ_MODE_STD = 1,
114 READ_MODE_FAST = 2,
115 READ_MODE_ATMEL45 = 3,
116 READ_MODE_4B_ADDR_FAST = 4,
117 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
118};
119
120enum dediprog_writemode {
Elyes HAOUASac01baa2018-05-28 16:52:21 +0200121 WRITE_MODE_PAGE_PGM = 1,
Simon Glass557eb4f2015-07-05 16:53:22 +0000122 WRITE_MODE_PAGE_WRITE = 2,
123 WRITE_MODE_1B_AAI = 3,
124 WRITE_MODE_2B_AAI = 4,
125 WRITE_MODE_128B_PAGE = 5,
126 WRITE_MODE_PAGE_AT26DF041 = 6,
127 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000128 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000129 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000130 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000131 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
132 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
133};
134
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000135enum dediprog_standalone_mode {
136 ENTER_STANDALONE_MODE = 0,
137 LEAVE_STANDALONE_MODE = 1,
138};
139
David Hendricksf73f8a72018-02-21 07:34:34 -0800140/*
141 * These are not official designations; they are for use in flashrom only.
142 * Order must be preserved so that comparison operators work.
143 */
144enum protocol {
145 PROTOCOL_UNKNOWN,
146 PROTOCOL_V1,
147 PROTOCOL_V2,
148 PROTOCOL_V3,
149};
150
Thomas Heijligencc853d82021-05-04 15:32:17 +0200151static const struct dev_entry devs_dediprog[] = {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500152 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
Stefan Taunerfdec7472016-02-22 08:59:27 +0000153
154 {0},
155};
Simon Glass557eb4f2015-07-05 16:53:22 +0000156
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000157struct dediprog_data {
158 struct libusb_context *usb_ctx;
159 libusb_device_handle *dediprog_handle;
160 int dediprog_in_endpoint;
161 int dediprog_out_endpoint;
162 int dediprog_firmwareversion;
163 enum dediprog_devtype dediprog_devicetype;
164};
Simon Glass557eb4f2015-07-05 16:53:22 +0000165
Nico Huberd99a2bd2016-02-18 21:42:49 +0000166#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
167 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
168/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
169const char * LIBUSB_CALL libusb_error_name(int error_code)
170{
171 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
172 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
173 static char my_libusb_error[18 + 5 + 2];
174 sprintf(my_libusb_error, "libusb error code %i", error_code);
175 return my_libusb_error;
176 } else {
177 return "UNKNOWN";
178 }
179}
180#endif
181
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000182static enum protocol protocol(const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000183{
David Hendricksf73f8a72018-02-21 07:34:34 -0800184 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000185 switch (dp_data->dediprog_devicetype) {
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000186 case DEV_SF100:
Jay Thompsoncabe3202018-08-17 14:30:04 -0500187 case DEV_SF200:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000188 if (dp_data->dediprog_firmwareversion < FIRMWARE_VERSION(5, 5, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800189 return PROTOCOL_V1;
190 else
191 return PROTOCOL_V2;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000192 case DEV_SF600:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000193 if (dp_data->dediprog_firmwareversion < FIRMWARE_VERSION(6, 9, 0))
David Hendricksf73f8a72018-02-21 07:34:34 -0800194 return PROTOCOL_V1;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000195 else if (dp_data->dediprog_firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
David Hendricksf73f8a72018-02-21 07:34:34 -0800196 return PROTOCOL_V2;
197 else
198 return PROTOCOL_V3;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000199 default:
David Hendricksf73f8a72018-02-21 07:34:34 -0800200 return PROTOCOL_UNKNOWN;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000201 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000202}
Simon Glassae616512016-01-23 23:27:58 +0000203
Nico Huberd99a2bd2016-02-18 21:42:49 +0000204struct dediprog_transfer_status {
205 int error; /* OK if 0, ERROR else */
206 unsigned int queued_idx;
207 unsigned int finished_idx;
208};
209
210static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
211{
212 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
213 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
214 status->error = 1;
215 msg_perr("SPI bulk read failed!\n");
216 }
217 ++status->finished_idx;
218}
219
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000220static int dediprog_bulk_read_poll(struct libusb_context *usb_ctx,
221 const struct dediprog_transfer_status *const status,
222 const int finish)
Nico Huberd99a2bd2016-02-18 21:42:49 +0000223{
224 if (status->finished_idx >= status->queued_idx)
225 return 0;
226
227 do {
228 struct timeval timeout = { 10, 0 };
229 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
230 if (ret < 0) {
231 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
232 return 1;
233 }
234 } while (finish && (status->finished_idx < status->queued_idx));
235 return 0;
236}
237
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000238static int dediprog_read(libusb_device_handle *dediprog_handle,
239 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
240 uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000241{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000242 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
243 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000244}
245
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000246static int dediprog_write(libusb_device_handle *dediprog_handle,
247 enum dediprog_cmds cmd, unsigned int value, unsigned int idx,
248 const uint8_t *bytes, size_t size)
Simon Glassae616512016-01-23 23:27:58 +0000249{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000250 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
251 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000252}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000253
Nico Huberd99a2bd2016-02-18 21:42:49 +0000254
Simon Glass25e9f402015-06-28 13:31:19 +0000255/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000256static int dediprog_set_leds(int leds, const struct dediprog_data *dp_data)
Stefan Reinauer915b8402011-01-28 09:00:15 +0000257{
Simon Glass25e9f402015-06-28 13:31:19 +0000258 if (leds < LED_NONE || leds > LED_ALL)
259 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000260
Simon Glassae616512016-01-23 23:27:58 +0000261 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
262 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000263 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000264 * bit 0 == 0: red light is on.
265 *
266 * Additionally, the command structure has changed with the "new" protocol.
267 *
268 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000269 */
Simon Glassae616512016-01-23 23:27:58 +0000270 int target_leds, ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000271 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000272 target_leds = (leds ^ 7) << 8;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000273 ret = dediprog_write(dp_data->dediprog_handle, CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000274 } else {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000275 if (dp_data->dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Simon Glassae616512016-01-23 23:27:58 +0000276 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
277 } else {
278 target_leds = leds;
279 }
280 target_leds ^= 7;
281
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000282 ret = dediprog_write(dp_data->dediprog_handle, CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000283 }
284
Stefan Reinauer915b8402011-01-28 09:00:15 +0000285 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000286 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000287 return 1;
288 }
289
Stefan Reinauer915b8402011-01-28 09:00:15 +0000290 return 0;
291}
292
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000293static int dediprog_set_spi_voltage(libusb_device_handle *dediprog_handle, int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000294{
295 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000296 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000297
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000298 switch (millivolt) {
299 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000300 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000301 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000302 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000303 case 1800:
304 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000305 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000306 case 2500:
307 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000308 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000309 case 3500:
310 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000311 break;
312 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000313 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000314 return 1;
315 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000316 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
317 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000318
Nico Huber4099a8a2012-06-16 00:02:27 +0000319 if (voltage_selector == 0) {
320 /* Wait some time as the original driver does. */
321 programmer_delay(200 * 1000);
322 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000323 ret = dediprog_write(dediprog_handle, CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000324 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000325 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
326 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000327 return 1;
328 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000329 if (voltage_selector != 0) {
330 /* Wait some time as the original driver does. */
331 programmer_delay(200 * 1000);
332 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000333 return 0;
334}
335
Nico Huber77fa67d2013-02-20 18:03:36 +0000336struct dediprog_spispeeds {
337 const char *const name;
338 const int speed;
339};
340
341static const struct dediprog_spispeeds spispeeds[] = {
342 { "24M", 0x0 },
343 { "12M", 0x2 },
344 { "8M", 0x1 },
345 { "3M", 0x3 },
346 { "2.18M", 0x4 },
347 { "1.5M", 0x5 },
348 { "750k", 0x6 },
349 { "375k", 0x7 },
350 { NULL, 0x0 },
351};
352
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000353static int dediprog_set_spi_speed(unsigned int spispeed_idx, const struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000354{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000355 if (dp_data->dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000356 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
357 return 0;
358 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000359
Simon Glass557eb4f2015-07-05 16:53:22 +0000360 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
361 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000362
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000363 int ret = dediprog_write(dp_data->dediprog_handle, CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000364 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000365 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000366 return 1;
367 }
368 return 0;
369}
370
Nico Huber7eb38aa2019-03-21 15:42:54 +0100371static int prepare_rw_cmd(
Nico Huber93db6e12018-09-30 01:18:43 +0200372 struct flashctx *const flash, uint8_t *data_packet, unsigned int count,
Nico Huber477e1692019-06-18 23:56:01 +0200373 uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start, int is_read)
374{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000375 const struct dediprog_data *dp_data = flash->mst->spi.data;
376
Nico Huber477e1692019-06-18 23:56:01 +0200377 if (count >= 1 << 16) {
378 msg_perr("%s: Unsupported transfer length of %u blocks! "
379 "Please report a bug at flashrom@flashrom.org\n",
380 __func__, count);
381 return 1;
382 }
383
Simon Glassae616512016-01-23 23:27:58 +0000384 /* First 5 bytes are common in both generations. */
385 data_packet[0] = count & 0xff;
386 data_packet[1] = (count >> 8) & 0xff;
387 data_packet[2] = 0; /* RFU */
388 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
389 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
390
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000391 if (protocol(dp_data) >= PROTOCOL_V2) {
Nico Huber93db6e12018-09-30 01:18:43 +0200392 if (is_read && flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
393 data_packet[3] = READ_MODE_4B_ADDR_FAST_0x0C;
394 data_packet[4] = JEDEC_READ_4BA_FAST;
395 } else if (dedi_spi_cmd == WRITE_MODE_PAGE_PGM
396 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
397 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
398 data_packet[4] = JEDEC_BYTE_PROGRAM_4BA;
399 }
400
Simon Glassae616512016-01-23 23:27:58 +0000401 *value = *idx = 0;
402 data_packet[5] = 0; /* RFU */
403 data_packet[6] = (start >> 0) & 0xff;
404 data_packet[7] = (start >> 8) & 0xff;
405 data_packet[8] = (start >> 16) & 0xff;
406 data_packet[9] = (start >> 24) & 0xff;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000407 if (protocol(dp_data) >= PROTOCOL_V3) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800408 if (is_read) {
409 data_packet[10] = 0x00; /* address length (3 or 4) */
410 data_packet[11] = 0x00; /* dummy cycle / 2 */
411 } else {
412 /* 16 LSBs and 16 HSBs of page size */
413 /* FIXME: This assumes page size of 256. */
414 data_packet[10] = 0x00;
415 data_packet[11] = 0x01;
416 data_packet[12] = 0x00;
417 data_packet[13] = 0x00;
418 }
419 }
Simon Glassae616512016-01-23 23:27:58 +0000420 } else {
Nico Huber9bb8a322022-05-24 15:07:34 +0200421 if (flash->chip->feature_bits & FEATURE_4BA_EAR_ANY) {
Nico Huber7eb38aa2019-03-21 15:42:54 +0100422 if (spi_set_extended_address(flash, start >> 24))
423 return 1;
424 } else if (start >> 24) {
425 msg_cerr("Can't handle 4-byte address with dediprog.\n");
426 return 1;
427 }
428 /*
429 * We don't know how the dediprog firmware handles 4-byte
430 * addresses. So let's not tell it what we are doing and
431 * only send the lower 3 bytes.
432 */
433 *value = start & 0xffff;
434 *idx = (start >> 16) & 0xff;
Simon Glassae616512016-01-23 23:27:58 +0000435 }
Nico Huber7eb38aa2019-03-21 15:42:54 +0100436
437 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000438}
439
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000440/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
441 * @start start address
442 * @len length
443 * @return 0 on success, 1 on failure
444 */
Simon Glassae616512016-01-23 23:27:58 +0000445static 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 +0000446{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000447 int err = 1;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000448 const struct dediprog_data *dp_data = flash->mst->spi.data;
Nico Huberd99a2bd2016-02-18 21:42:49 +0000449
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000450 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000451 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000452 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000453
Nico Huberd99a2bd2016-02-18 21:42:49 +0000454 struct dediprog_transfer_status status = { 0, 0, 0 };
455 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
456 struct libusb_transfer *transfer;
457
Nico Huberbbaa1712018-12-05 13:26:20 +0100458 if (len == 0)
459 return 0;
460
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000461 if ((start % chunksize) || (len % chunksize)) {
Simon Glassae616512016-01-23 23:27:58 +0000462 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
463 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000464 return 1;
465 }
466
David Hendricksf73f8a72018-02-21 07:34:34 -0800467 int command_packet_size;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000468 switch (protocol(dp_data)) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800469 case PROTOCOL_V1:
470 command_packet_size = 5;
471 break;
472 case PROTOCOL_V2:
473 command_packet_size = 10;
474 break;
475 case PROTOCOL_V3:
476 command_packet_size = 12;
477 break;
478 default:
479 return 1;
480 }
481
482 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000483 unsigned int value, idx;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100484 if (prepare_rw_cmd(flash, data_packet, count, READ_MODE_STD, &value, &idx, start, 1))
485 return 1;
Simon Glassae616512016-01-23 23:27:58 +0000486
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000487 int ret = dediprog_write(dp_data->dediprog_handle,
488 CMD_READ, value, idx, data_packet, sizeof(data_packet));
Nico Huber519be662018-12-23 20:03:35 +0100489 if (ret != (int)sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000490 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000491 return 1;
492 }
493
Nico Huberd99a2bd2016-02-18 21:42:49 +0000494 /*
495 * Ring buffer of bulk transfers.
496 * Poll until at least one transfer is ready,
497 * schedule next transfers until buffer is full.
498 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000499
Nico Huberd99a2bd2016-02-18 21:42:49 +0000500 /* Allocate bulk transfers. */
501 unsigned int i;
Nico Huber519be662018-12-23 20:03:35 +0100502 for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000503 transfers[i] = libusb_alloc_transfer(0);
504 if (!transfers[i]) {
505 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
506 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200507 }
508 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000509
510 /* Now transfer requested chunks using libusb's asynchronous interface. */
511 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200512 while ((status.queued_idx < count) &&
513 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
514 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000515 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000516 libusb_fill_bulk_transfer(transfer,
517 dp_data->dediprog_handle, 0x80 | dp_data->dediprog_in_endpoint,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000518 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
519 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
520 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
521 ret = libusb_submit_transfer(transfer);
522 if (ret < 0) {
523 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
524 status.queued_idx, libusb_error_name(ret));
525 goto err_free;
526 }
527 ++status.queued_idx;
528 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000529 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 0))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000530 goto err_free;
531 }
532 /* Wait for transfers to finish. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000533 if (dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 0))
Nico Huberd99a2bd2016-02-18 21:42:49 +0000534 goto err_free;
535 /* Check if everything has been transmitted. */
536 if ((status.finished_idx < count) || status.error)
537 goto err_free;
538
539 err = 0;
540
541err_free:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000542 dediprog_bulk_read_poll(dp_data->usb_ctx, &status, 1);
Nico Huberd99a2bd2016-02-18 21:42:49 +0000543 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
544 if (transfers[i]) libusb_free_transfer(transfers[i]);
545 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000546}
547
Simon Glassae616512016-01-23 23:27:58 +0000548static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000549{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000550 int ret;
551 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000552 const unsigned int chunksize = 0x200;
Nico Huberbbaa1712018-12-05 13:26:20 +0100553 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000554 unsigned int bulklen;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000555 const struct dediprog_data *dp_data = flash->mst->spi.data;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000556
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000557 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000558
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000559 if (residue) {
560 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
561 start, residue);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100562 ret = default_spi_read(flash, buf, start, residue);
Simon Glass25e9f402015-06-28 13:31:19 +0000563 if (ret)
564 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000565 }
566
567 /* Round down. */
568 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000569 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000570 if (ret)
571 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000572
573 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000574 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000575 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
576 start, len);
Edward O'Callaghan317c67b2020-10-09 12:56:53 +1100577 ret = default_spi_read(flash, buf + residue + bulklen,
578 start + residue + bulklen, len);
Simon Glass25e9f402015-06-28 13:31:19 +0000579 if (ret)
580 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000581 }
582
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000583 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000584 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000585err:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000586 dediprog_set_leds(LED_ERROR, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +0000587 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000588}
589
Nico Hubera4b14f72012-06-19 12:06:53 +0000590/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
591 * @chunksize length of data chunks, only 256 supported by now
592 * @start start address
593 * @len length
594 * @dedi_spi_cmd dediprog specific write command for spi bus
595 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000596 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000597static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000598 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000599{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000600 /* USB transfer size must be 512, other sizes will NOT work at all.
601 * chunksize is the real data size per USB bulk transfer. The remaining
602 * space in a USB bulk transfer must be filled with 0xff padding.
603 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000604 const unsigned int count = len / chunksize;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000605 const struct dediprog_data *dp_data = flash->mst->spi.data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000606
Nico Hubera4b14f72012-06-19 12:06:53 +0000607 /*
608 * We should change this check to
609 * chunksize > 512
610 * once we know how to handle different chunk sizes.
611 */
612 if (chunksize != 256) {
613 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
614 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
615 return 1;
616 }
617
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000618 if ((start % chunksize) || (len % chunksize)) {
619 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
620 "at flashrom@flashrom.org\n", __func__, start, len);
621 return 1;
622 }
623
624 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000625 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000626 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000627
David Hendricksf73f8a72018-02-21 07:34:34 -0800628 int command_packet_size;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000629 switch (protocol(dp_data)) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800630 case PROTOCOL_V1:
631 command_packet_size = 5;
632 break;
633 case PROTOCOL_V2:
634 command_packet_size = 10;
635 break;
636 case PROTOCOL_V3:
637 command_packet_size = 14;
638 break;
639 default:
640 return 1;
641 }
642
643 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000644 unsigned int value, idx;
Nico Huber7eb38aa2019-03-21 15:42:54 +0100645 if (prepare_rw_cmd(flash, data_packet, count, dedi_spi_cmd, &value, &idx, start, 0))
646 return 1;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000647 int ret = dediprog_write(dp_data->dediprog_handle,
648 CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
Nico Huber519be662018-12-23 20:03:35 +0100649 if (ret != (int)sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000650 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000651 return 1;
652 }
653
Simon Glassae616512016-01-23 23:27:58 +0000654 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000655 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000656 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000657 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000658 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000659 int transferred;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000660 ret = libusb_bulk_transfer(dp_data->dediprog_handle, dp_data->dediprog_out_endpoint,
661 usbbuf, 512, &transferred,
Nico Huberd99a2bd2016-02-18 21:42:49 +0000662 DEFAULT_TIMEOUT);
663 if ((ret < 0) || (transferred != 512)) {
664 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000665 return 1;
666 }
667 }
668
669 return 0;
670}
671
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000672static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000673 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000674{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000675 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000676 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000677 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
678 unsigned int bulklen;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000679 const struct dediprog_data *dp_data = flash->mst->spi.data;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000680
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000681 dediprog_set_leds(LED_BUSY, dp_data);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000682
Nico Hubera4b14f72012-06-19 12:06:53 +0000683 if (chunksize != 256) {
684 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
685 "we don't know how dediprog\nhandles them.\n");
686 /* Write everything like it was residue. */
687 residue = len;
688 }
689
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000690 if (residue) {
691 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
692 start, residue);
Nico Huber93db6e12018-09-30 01:18:43 +0200693 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
694 ret = spi_write_chunked(flash, buf, start, residue, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000695 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000696 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000697 return ret;
698 }
699 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000700
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000701 /* Round down. */
702 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000703 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000704 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000705 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000706 return ret;
707 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000708
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000709 len -= residue + bulklen;
710 if (len) {
711 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
712 start, len);
713 ret = spi_write_chunked(flash, buf + residue + bulklen,
Nico Huber93db6e12018-09-30 01:18:43 +0200714 start + residue + bulklen, len, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000715 if (ret) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000716 dediprog_set_leds(LED_ERROR, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000717 return ret;
718 }
719 }
720
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000721 dediprog_set_leds(LED_PASS, dp_data);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000722 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000723}
724
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000725static 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 +0000726{
Simon Glass557eb4f2015-07-05 16:53:22 +0000727 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000728}
729
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000730static 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 +0000731{
Simon Glass557eb4f2015-07-05 16:53:22 +0000732 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000733}
734
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000735static int dediprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000736 unsigned int writecnt,
737 unsigned int readcnt,
738 const unsigned char *writearr,
739 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000740{
741 int ret;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000742 const struct dediprog_data *dp_data = flash->mst->spi.data;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000743
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000744 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glassae616512016-01-23 23:27:58 +0000745 if (writecnt > flash->mst->spi.max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000746 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000747 return 1;
748 }
Simon Glassae616512016-01-23 23:27:58 +0000749 if (readcnt > flash->mst->spi.max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000750 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000751 return 1;
752 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100753
Simon Glassae616512016-01-23 23:27:58 +0000754 unsigned int idx, value;
755 /* New protocol has options and timeout combined as value while the old one used the value field for
756 * timeout and the index field for options. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000757 if (protocol(dp_data) >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000758 idx = 0;
759 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
760 } else {
761 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
762 value = 0;
763 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000764 ret = dediprog_write(dp_data->dediprog_handle, CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Nico Huber519be662018-12-23 20:03:35 +0100765 if (ret != (int)writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000766 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000767 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000768 return 1;
769 }
Simon Glassae616512016-01-23 23:27:58 +0000770 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000771 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000772
Stefan Tauner74367bf2016-02-20 20:53:46 +0000773 /* The specifications do state the possibility to set a timeout for transceive transactions.
774 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
775 * can predict the time needed by the previous command or so (untested). In any case, using this
776 * "feature" to set sane-looking timouts for the read below will completely trash performance with
777 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
778 *
779 * The specification also uses only 0 in its examples, so the lesson to learn here:
780 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000781 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800782 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000783 idx = 0;
784 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
785 } else {
786 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
787 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
788 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000789 ret = dediprog_read(dp_data->dediprog_handle, CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000790 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000791 ret = dediprog_read(dp_data->dediprog_handle, CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Nico Huber519be662018-12-23 20:03:35 +0100792 if (ret != (int)readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000793 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 +0000794 return 1;
795 }
796 return 0;
797}
798
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000799static int dediprog_check_devicestring(struct dediprog_data *dp_data)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000800{
801 int ret;
802 char buf[0x11];
803
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000804 /* Command Receive Device String. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000805 ret = dediprog_read(dp_data->dediprog_handle, CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000806 if (ret != 0x10) {
807 msg_perr("Incomplete/failed Command Receive Device String!\n");
808 return 1;
809 }
810 buf[0x10] = '\0';
811 msg_pdbg("Found a %s\n", buf);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000812 if (memcmp(buf, "SF100", 0x5) == 0)
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000813 dp_data->dediprog_devicetype = DEV_SF100;
Jay Thompsoncabe3202018-08-17 14:30:04 -0500814 else if (memcmp(buf, "SF200", 0x5) == 0)
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000815 dp_data->dediprog_devicetype = DEV_SF200;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000816 else if (memcmp(buf, "SF600", 0x5) == 0)
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000817 dp_data->dediprog_devicetype = DEV_SF600;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000818 else {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500819 msg_perr("Device not a SF100, SF200, or SF600!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000820 return 1;
821 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000822
823 int sfnum;
824 int fw[3];
825 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000826 sfnum != (int)dp_data->dediprog_devicetype) {
Simon Glassae616512016-01-23 23:27:58 +0000827 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000828 return 1;
829 }
Simon Glassae616512016-01-23 23:27:58 +0000830 /* Only these major versions were tested. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000831 if (fw[0] < 2 || fw[0] > 7) {
Simon Glassae616512016-01-23 23:27:58 +0000832 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000833 return 1;
834 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800835
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000836 dp_data->dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
837 if (protocol(dp_data) == PROTOCOL_UNKNOWN) {
David Hendricksf73f8a72018-02-21 07:34:34 -0800838 msg_perr("Internal error: Unable to determine protocol version.\n");
839 return 1;
840 }
Simon Glassae616512016-01-23 23:27:58 +0000841
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000842 return 0;
843}
844
David Hendricksc05900f2016-02-18 21:42:41 +0000845/*
Ryan O'Leary301ae222019-06-24 19:14:33 -0700846 * Read the id from the dediprog. This should return the numeric part of the
847 * serial number found on a sticker on the back of the dediprog. Note this
848 * number is stored in writable eeprom, so it could get out of sync. Also note,
849 * this function only supports SF100 at this time, but SF600 support is not too
850 * much different.
851 * @return the id on success, -1 on failure
852 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000853static int dediprog_read_id(libusb_device_handle *dediprog_handle)
Ryan O'Leary301ae222019-06-24 19:14:33 -0700854{
855 int ret;
856 uint8_t buf[3];
857
858 ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN,
859 0x7, /* request */
860 0, /* value */
861 0xEF00, /* index */
862 buf, sizeof(buf),
863 DEFAULT_TIMEOUT);
864 if (ret != sizeof(buf)) {
865 msg_perr("Failed to read dediprog id, error %d!\n", ret);
866 return -1;
867 }
868
869 return buf[0] << 16 | buf[1] << 8 | buf[2];
870}
871
872/*
David Hendricksc05900f2016-02-18 21:42:41 +0000873 * This command presumably sets the voltage for the SF100 itself (not the
874 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
875 * (including all SF600s) do not support it.
876 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000877
David Hendricksc05900f2016-02-18 21:42:41 +0000878/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
879 * Only use dediprog_set_voltage on SF100 programmers with firmware older
880 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000881static int dediprog_set_voltage(libusb_device_handle *dediprog_handle)
David Hendricksc05900f2016-02-18 21:42:41 +0000882{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000883 unsigned char buf[1] = {0};
884 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000885 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000886 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000887 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000888 return 1;
889 }
David Hendricksc05900f2016-02-18 21:42:41 +0000890 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000891 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000892 return 1;
893 }
David Hendricksc05900f2016-02-18 21:42:41 +0000894
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000895 return 0;
896}
897
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000898static int dediprog_standalone_mode(const struct dediprog_data *dp_data)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000899{
900 int ret;
901
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000902 if (dp_data->dediprog_devicetype != DEV_SF600)
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000903 return 0;
904
905 msg_pdbg2("Disabling standalone mode.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000906 ret = dediprog_write(dp_data->dediprog_handle,
907 CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000908 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000909 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000910 return 1;
911 }
912
913 return 0;
914}
915
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000916#if 0
917/* Something.
918 * Present in eng_detect_blink.log with firmware 3.1.8
919 * Always preceded by Command Receive Device String
920 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000921static int dediprog_command_b(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000922{
923 int ret;
924 char buf[0x3];
925
Simon Glass557eb4f2015-07-05 16:53:22 +0000926 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
927 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000928 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000929 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000930 return 1;
931 }
932 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
933 (buf[2] != 0xff)) {
934 msg_perr("Unexpected response to Command B!\n");
935 return 1;
936 }
937
938 return 0;
939}
940#endif
941
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000942static int set_target_flash(libusb_device_handle *dediprog_handle, enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000943{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000944 int ret = dediprog_write(dediprog_handle, CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +0000945 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000946 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000947 return 1;
948 }
949 return 0;
950}
951
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000952#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000953/* Returns true if the button is currently pressed. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +1000954static bool dediprog_get_button(libusb_device_handle *dediprog_handle)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000955{
Simon Glass557eb4f2015-07-05 16:53:22 +0000956 char buf[1];
957 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
958 buf, 0x1, DEFAULT_TIMEOUT);
959 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000960 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000961 return 1;
962 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000963 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000964}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000965#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000966
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000967static int parse_voltage(char *voltage)
968{
969 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000970 int i;
971 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000972
973 if (!voltage || !strlen(voltage)) {
974 msg_perr("Empty voltage= specified.\n");
975 return -1;
976 }
977 millivolt = (int)strtol(voltage, &tmp, 0);
978 voltage = tmp;
979 /* Handle "," and "." as decimal point. Everything after it is assumed
980 * to be in decimal notation.
981 */
982 if ((*voltage == '.') || (*voltage == ',')) {
983 voltage++;
984 for (i = 0; i < 3; i++) {
985 fraction *= 10;
986 /* Don't advance if the current character is invalid,
987 * but continue multiplying.
988 */
989 if ((*voltage < '0') || (*voltage > '9'))
990 continue;
991 fraction += *voltage - '0';
992 voltage++;
993 }
994 /* Throw away remaining digits. */
995 voltage += strspn(voltage, "0123456789");
996 }
997 /* The remaining string must be empty or "mV" or "V". */
998 tolower_string(voltage);
999
1000 /* No unit or "V". */
1001 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
1002 millivolt *= 1000;
1003 millivolt += fraction;
1004 } else if (!strncmp(voltage, "mv", 2) ||
1005 !strncmp(voltage, "milliv", 6)) {
1006 /* No adjustment. fraction is discarded. */
1007 } else {
1008 /* Garbage at the end of the string. */
1009 msg_perr("Garbage voltage= specified.\n");
1010 return -1;
1011 }
1012 return millivolt;
1013}
1014
Nico Huber93db6e12018-09-30 01:18:43 +02001015static struct spi_master spi_master_dediprog = {
Nico Huberdc5af542018-12-22 16:54:59 +01001016 .features = SPI_MASTER_NO_4BA_MODES,
Simon Glassae616512016-01-23 23:27:58 +00001017 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
1018 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001019 .command = dediprog_spi_send_command,
1020 .multicommand = default_spi_send_multicommand,
1021 .read = dediprog_spi_read,
1022 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +00001023 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001024};
1025
Ryan O'Leary301ae222019-06-24 19:14:33 -07001026/*
1027 * Open a dediprog_handle with the USB device at the given index.
1028 * @index index of the USB device
1029 * @return 0 for success, -1 for error, -2 for busy device
1030 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001031static int dediprog_open(int index, struct dediprog_data *dp_data)
Ryan O'Leary301ae222019-06-24 19:14:33 -07001032{
1033 const uint16_t vid = devs_dediprog[0].vendor_id;
1034 const uint16_t pid = devs_dediprog[0].device_id;
1035 int ret;
1036
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001037 dp_data->dediprog_handle = usb_dev_get_by_vid_pid_number(dp_data->usb_ctx,
1038 vid, pid, (unsigned int) index);
1039 if (!dp_data->dediprog_handle) {
Ryan O'Leary301ae222019-06-24 19:14:33 -07001040 msg_perr("Could not find a Dediprog programmer on USB.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001041 libusb_exit(dp_data->usb_ctx);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001042 return -1;
1043 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001044 ret = libusb_set_configuration(dp_data->dediprog_handle, 1);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001045 if (ret != 0) {
1046 msg_perr("Could not set USB device configuration: %i %s\n",
1047 ret, libusb_error_name(ret));
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001048 libusb_close(dp_data->dediprog_handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001049 return -2;
1050 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001051 ret = libusb_claim_interface(dp_data->dediprog_handle, 0);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001052 if (ret < 0) {
1053 msg_perr("Could not claim USB device interface %i: %i %s\n",
1054 0, ret, libusb_error_name(ret));
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001055 libusb_close(dp_data->dediprog_handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001056 return -2;
1057 }
1058 return 0;
1059}
1060
David Hendricks8bb20212011-06-14 01:35:36 +00001061static int dediprog_shutdown(void *data)
1062{
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001063 int ret = 0;
1064 struct dediprog_data *dp_data = data;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +00001065
David Hendricks8bb20212011-06-14 01:35:36 +00001066 /* URB 28. Command Set SPI Voltage to 0. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001067 if (dediprog_set_spi_voltage(dp_data->dediprog_handle, 0x0)) {
1068 ret = 1;
1069 goto out;
David Hendricks8bb20212011-06-14 01:35:36 +00001070 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001071
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001072 if (libusb_release_interface(dp_data->dediprog_handle, 0)) {
1073 msg_perr("Could not release USB interface!\n");
1074 ret = 1;
1075 goto out;
1076 }
1077 libusb_close(dp_data->dediprog_handle);
1078 libusb_exit(dp_data->usb_ctx);
1079out:
1080 free(data);
1081 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +00001082}
1083
Thomas Heijligencc853d82021-05-04 15:32:17 +02001084static int dediprog_init(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001085{
Ryan O'Leary301ae222019-06-24 19:14:33 -07001086 char *voltage, *id_str, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +00001087 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001088 int millivolt = 3500;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001089 int id = -1; /* -1 defaults to enumeration order */
1090 int found_id;
Nathan Laredo21541a62012-12-24 22:07:36 +00001091 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +02001092 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +00001093 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001094
Nico Huber77fa67d2013-02-20 18:03:36 +00001095 spispeed = extract_programmer_param("spispeed");
1096 if (spispeed) {
1097 for (i = 0; spispeeds[i].name; ++i) {
1098 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1099 spispeed_idx = i;
1100 break;
1101 }
1102 }
1103 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +00001104 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +00001105 free(spispeed);
1106 return 1;
1107 }
1108 free(spispeed);
1109 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001110
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001111 voltage = extract_programmer_param("voltage");
1112 if (voltage) {
1113 millivolt = parse_voltage(voltage);
1114 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001115 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001116 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001117 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1118 }
1119
Ryan O'Leary301ae222019-06-24 19:14:33 -07001120 id_str = extract_programmer_param("id");
1121 if (id_str) {
1122 char prefix0, prefix1;
1123 if (sscanf(id_str, "%c%c%d", &prefix0, &prefix1, &id) != 3) {
1124 msg_perr("Error: Could not parse dediprog 'id'.\n");
1125 msg_perr("Expected a string like SF012345 or DP012345.\n");
1126 free(id_str);
1127 return 1;
1128 }
1129 if (id < 0 || id >= 0x1000000) {
1130 msg_perr("Error: id %s is out of range!\n", id_str);
1131 free(id_str);
1132 return 1;
1133 }
1134 if (!(prefix0 == 'S' && prefix1 == 'F') && !(prefix0 == 'D' && prefix1 == 'P')) {
1135 msg_perr("Error: %s is an invalid id!\n", id_str);
1136 free(id_str);
1137 return 1;
1138 }
1139 msg_pinfo("Will search for dediprog id %s.\n", id_str);
1140 }
1141 free(id_str);
1142
Nathan Laredo21541a62012-12-24 22:07:36 +00001143 device = extract_programmer_param("device");
1144 if (device) {
1145 char *dev_suffix;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001146 if (id != -1) {
1147 msg_perr("Error: Cannot use 'id' and 'device'.\n");
1148 }
Nathan Laredo21541a62012-12-24 22:07:36 +00001149 errno = 0;
1150 usedevice = strtol(device, &dev_suffix, 10);
1151 if (errno != 0 || device == dev_suffix) {
1152 msg_perr("Error: Could not convert 'device'.\n");
1153 free(device);
1154 return 1;
1155 }
Nico Huber961f4a12019-10-04 17:34:22 +02001156 if (usedevice < 0 || usedevice > INT_MAX) {
Nathan Laredo21541a62012-12-24 22:07:36 +00001157 msg_perr("Error: Value for 'device' is out of range.\n");
1158 free(device);
1159 return 1;
1160 }
1161 if (strlen(dev_suffix) > 0) {
1162 msg_perr("Error: Garbage following 'device' value.\n");
1163 free(device);
1164 return 1;
1165 }
1166 msg_pinfo("Using device %li.\n", usedevice);
1167 }
1168 free(device);
1169
Stefan Taunere659d2d2013-05-03 21:58:28 +00001170 target_str = extract_programmer_param("target");
1171 if (target_str) {
1172 char *target_suffix;
1173 errno = 0;
1174 target = strtol(target_str, &target_suffix, 10);
1175 if (errno != 0 || target_str == target_suffix) {
1176 msg_perr("Error: Could not convert 'target'.\n");
1177 free(target_str);
1178 return 1;
1179 }
1180 if (target < 1 || target > 2) {
1181 msg_perr("Error: Value for 'target' is out of range.\n");
1182 free(target_str);
1183 return 1;
1184 }
1185 if (strlen(target_suffix) > 0) {
1186 msg_perr("Error: Garbage following 'target' value.\n");
1187 free(target_str);
1188 return 1;
1189 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001190 switch (target) {
1191 case 1:
1192 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1193 target = FLASH_TYPE_APPLICATION_FLASH_1;
1194 break;
1195 case 2:
1196 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1197 target = FLASH_TYPE_APPLICATION_FLASH_2;
1198 break;
1199 default:
1200 break;
1201 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001202 }
1203 free(target_str);
1204
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001205 struct dediprog_data *dp_data = calloc(1, sizeof(*dp_data));
1206 if (!dp_data) {
1207 msg_perr("Unable to allocate space for SPI master data\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001208 return 1;
1209 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001210 dp_data->dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
1211 dp_data->dediprog_devicetype = DEV_UNKNOWN;
1212
1213 /* Here comes the USB stuff. */
1214 libusb_init(&dp_data->usb_ctx);
1215 if (!dp_data->usb_ctx) {
1216 msg_perr("Could not initialize libusb!\n");
1217 goto init_err_exit;
1218 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001219
Ryan O'Leary301ae222019-06-24 19:14:33 -07001220 if (id != -1) {
1221 for (i = 0; ; i++) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001222 ret = dediprog_open(i, dp_data);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001223 if (ret == -1) {
1224 /* no dev */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001225 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001226 } else if (ret == -2) {
1227 /* busy dev */
1228 continue;
1229 }
1230
1231 /* Notice we can only call dediprog_read_id() after
1232 * libusb_set_configuration() and
1233 * libusb_claim_interface(). When searching by id and
1234 * either configuration or claim fails (usually the
1235 * device is in use by another instance of flashrom),
1236 * the device is skipped and the next device is tried.
1237 */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001238 found_id = dediprog_read_id(dp_data->dediprog_handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001239 if (found_id < 0) {
1240 msg_perr("Could not read id.\n");
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001241 libusb_release_interface(dp_data->dediprog_handle, 0);
1242 libusb_close(dp_data->dediprog_handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001243 continue;
1244 }
1245 msg_pinfo("Found dediprog id SF%06d.\n", found_id);
1246 if (found_id != id) {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001247 libusb_release_interface(dp_data->dediprog_handle, 0);
1248 libusb_close(dp_data->dediprog_handle);
Ryan O'Leary301ae222019-06-24 19:14:33 -07001249 continue;
1250 }
1251 break;
1252 }
1253 } else {
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001254 if (dediprog_open(usedevice, dp_data)) {
1255 goto init_err_exit;
Ryan O'Leary301ae222019-06-24 19:14:33 -07001256 }
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001257 found_id = dediprog_read_id(dp_data->dediprog_handle);
David Woodhoused2a7e872013-07-30 09:34:44 +00001258 }
Ryan O'Leary301ae222019-06-24 19:14:33 -07001259
1260 if (found_id >= 0) {
1261 msg_pinfo("Using dediprog id SF%06d.\n", found_id);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001262 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001263
David Hendricksc05900f2016-02-18 21:42:41 +00001264 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1265 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001266 if (dediprog_check_devicestring(dp_data)) {
1267 if (dediprog_set_voltage(dp_data->dediprog_handle))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001268 goto init_err_cleanup_exit;
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001269 if (dediprog_check_devicestring(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001270 goto init_err_cleanup_exit;
David Hendricksc05900f2016-02-18 21:42:41 +00001271 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001272
Jay Thompsoncabe3202018-08-17 14:30:04 -05001273 /* SF100/SF200 uses one in/out endpoint, SF600 uses separate in/out endpoints */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001274 dp_data->dediprog_in_endpoint = 2;
1275 switch (dp_data->dediprog_devicetype) {
Jay Thompsoncabe3202018-08-17 14:30:04 -05001276 case DEV_SF100:
1277 case DEV_SF200:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001278 dp_data->dediprog_out_endpoint = 2;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001279 break;
1280 default:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001281 dp_data->dediprog_out_endpoint = 1;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001282 break;
1283 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001284
Simon Glass25e9f402015-06-28 13:31:19 +00001285 /* Set all possible LEDs as soon as possible to indicate activity.
1286 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001287 * dediprog_check_devicestring() has queried the device. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001288 dediprog_set_leds(LED_ALL, dp_data);
Simon Glass25e9f402015-06-28 13:31:19 +00001289
Simon Glass557eb4f2015-07-05 16:53:22 +00001290 /* Select target/socket, frequency and VCC. */
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001291 if (set_target_flash(dp_data->dediprog_handle, target) ||
1292 dediprog_set_spi_speed(spispeed_idx, dp_data) ||
1293 dediprog_set_spi_voltage(dp_data->dediprog_handle, millivolt)) {
1294 dediprog_set_leds(LED_ERROR, dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001295 goto init_err_cleanup_exit;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001296 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001297
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001298 if (dediprog_standalone_mode(dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001299 goto init_err_cleanup_exit;
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001300
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001301 if ((dp_data->dediprog_devicetype == DEV_SF100) ||
1302 (dp_data->dediprog_devicetype == DEV_SF600 && protocol(dp_data) == PROTOCOL_V3))
Nico Huber7eb38aa2019-03-21 15:42:54 +01001303 spi_master_dediprog.features &= ~SPI_MASTER_NO_4BA_MODES;
1304
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001305 if (protocol(dp_data) >= PROTOCOL_V2)
Nico Huber93db6e12018-09-30 01:18:43 +02001306 spi_master_dediprog.features |= SPI_MASTER_4BA;
1307
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001308 if (register_shutdown(dediprog_shutdown, dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001309 goto init_err_cleanup_exit;
1310
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001311 spi_master_dediprog.data = dp_data;
1312 if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE, dp_data))
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001313 return 1; /* shutdown function does cleanup */
Stefan Reinauer915b8402011-01-28 09:00:15 +00001314
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001315 return 0;
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001316
1317init_err_cleanup_exit:
Anastasia Klimchukb31f7ac2021-07-12 14:18:37 +10001318 dediprog_shutdown(dp_data);
1319 return 1;
1320
1321init_err_exit:
1322 free(dp_data);
Anastasia Klimchuk66e04562021-06-28 17:03:52 +10001323 return 1;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001324}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001325
1326const struct programmer_entry programmer_dediprog = {
1327 .name = "dediprog",
1328 .type = USB,
1329 .devs.dev = devs_dediprog,
1330 .init = dediprog_init,
1331 .map_flash_region = fallback_map,
1332 .unmap_flash_region = fallback_unmap,
1333 .delay = internal_delay,
1334};