blob: 03675e57ac710a764dba02784f2f1a17a58bf5cc [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 */
42#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
43#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 */
46struct libusb_context *usb_ctx;
47static libusb_device_handle *dediprog_handle;
David Woodhouse7f3b89f2016-02-18 21:42:15 +000048static int dediprog_in_endpoint;
49static int dediprog_out_endpoint;
50
51enum dediprog_devtype {
52 DEV_UNKNOWN = 0,
53 DEV_SF100 = 100,
Jay Thompsoncabe3202018-08-17 14:30:04 -050054 DEV_SF200 = 200,
David Woodhouse7f3b89f2016-02-18 21:42:15 +000055 DEV_SF600 = 600,
56};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000057
Simon Glass25e9f402015-06-28 13:31:19 +000058enum dediprog_leds {
59 LED_INVALID = -1,
60 LED_NONE = 0,
61 LED_PASS = 1 << 0,
62 LED_BUSY = 1 << 1,
63 LED_ERROR = 1 << 2,
64 LED_ALL = 7,
65};
66
Simon Glass557eb4f2015-07-05 16:53:22 +000067/* IO bits for CMD_SET_IO_LED message */
68enum dediprog_ios {
69 IO1 = 1 << 0,
70 IO2 = 1 << 1,
71 IO3 = 1 << 2,
72 IO4 = 1 << 3,
73};
74
75enum dediprog_cmds {
76 CMD_TRANSCEIVE = 0x01,
77 CMD_POLL_STATUS_REG = 0x02,
78 CMD_SET_VPP = 0x03,
79 CMD_SET_TARGET = 0x04,
80 CMD_READ_EEPROM = 0x05,
81 CMD_WRITE_EEPROM = 0x06,
82 CMD_SET_IO_LED = 0x07,
83 CMD_READ_PROG_INFO = 0x08,
84 CMD_SET_VCC = 0x09,
85 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000086 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000087 CMD_GET_BUTTON = 0x11,
88 CMD_GET_UID = 0x12,
89 CMD_SET_CS = 0x14,
90 CMD_IO_MODE = 0x15,
91 CMD_FW_UPDATE = 0x1A,
92 CMD_FPGA_UPDATE = 0x1B,
93 CMD_READ_FPGA_VERSION = 0x1C,
94 CMD_SET_HOLD = 0x1D,
95 CMD_READ = 0x20,
96 CMD_WRITE = 0x30,
97 CMD_WRITE_AT45DB = 0x31,
98 CMD_NAND_WRITE = 0x32,
99 CMD_NAND_READ = 0x33,
100 CMD_SET_SPI_CLK = 0x61,
101 CMD_CHECK_SOCKET = 0x62,
102 CMD_DOWNLOAD_PRJ = 0x63,
103 CMD_READ_PRJ_NAME = 0x64,
104 // New protocol/firmware only
105 CMD_CHECK_SDCARD = 0x65,
106 CMD_READ_PRJ = 0x66,
107};
108
109enum dediprog_target {
110 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
111 FLASH_TYPE_FLASH_CARD,
112 FLASH_TYPE_APPLICATION_FLASH_2,
113 FLASH_TYPE_SOCKET,
114};
115
116enum dediprog_readmode {
117 READ_MODE_STD = 1,
118 READ_MODE_FAST = 2,
119 READ_MODE_ATMEL45 = 3,
120 READ_MODE_4B_ADDR_FAST = 4,
121 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
122};
123
124enum dediprog_writemode {
Elyes HAOUASac01baa2018-05-28 16:52:21 +0200125 WRITE_MODE_PAGE_PGM = 1,
Simon Glass557eb4f2015-07-05 16:53:22 +0000126 WRITE_MODE_PAGE_WRITE = 2,
127 WRITE_MODE_1B_AAI = 3,
128 WRITE_MODE_2B_AAI = 4,
129 WRITE_MODE_128B_PAGE = 5,
130 WRITE_MODE_PAGE_AT26DF041 = 6,
131 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000132 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000133 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000134 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000135 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
136 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
137};
138
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000139enum dediprog_standalone_mode {
140 ENTER_STANDALONE_MODE = 0,
141 LEAVE_STANDALONE_MODE = 1,
142};
143
David Hendricksf73f8a72018-02-21 07:34:34 -0800144/*
145 * These are not official designations; they are for use in flashrom only.
146 * Order must be preserved so that comparison operators work.
147 */
148enum protocol {
149 PROTOCOL_UNKNOWN,
150 PROTOCOL_V1,
151 PROTOCOL_V2,
152 PROTOCOL_V3,
153};
154
Stefan Taunerfdec7472016-02-22 08:59:27 +0000155const struct dev_entry devs_dediprog[] = {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500156 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
Stefan Taunerfdec7472016-02-22 08:59:27 +0000157
158 {0},
159};
Simon Glass557eb4f2015-07-05 16:53:22 +0000160
161static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000162enum dediprog_devtype dediprog_devicetype = DEV_UNKNOWN;
Simon Glass557eb4f2015-07-05 16:53:22 +0000163
Nico Huberd99a2bd2016-02-18 21:42:49 +0000164#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
165 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
166/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
167const char * LIBUSB_CALL libusb_error_name(int error_code)
168{
169 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
170 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
171 static char my_libusb_error[18 + 5 + 2];
172 sprintf(my_libusb_error, "libusb error code %i", error_code);
173 return my_libusb_error;
174 } else {
175 return "UNKNOWN";
176 }
177}
178#endif
179
David Hendricksf73f8a72018-02-21 07:34:34 -0800180static enum protocol protocol(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000181{
David Hendricksf73f8a72018-02-21 07:34:34 -0800182 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000183 switch (dediprog_devicetype) {
184 case DEV_SF100:
Jay Thompsoncabe3202018-08-17 14:30:04 -0500185 case DEV_SF200:
David Hendricksf73f8a72018-02-21 07:34:34 -0800186 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 5, 0))
187 return PROTOCOL_V1;
188 else
189 return PROTOCOL_V2;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000190 case DEV_SF600:
David Hendricksf73f8a72018-02-21 07:34:34 -0800191 if (dediprog_firmwareversion < FIRMWARE_VERSION(6, 9, 0))
192 return PROTOCOL_V1;
193 else if (dediprog_firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
194 return PROTOCOL_V2;
195 else
196 return PROTOCOL_V3;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000197 default:
David Hendricksf73f8a72018-02-21 07:34:34 -0800198 return PROTOCOL_UNKNOWN;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000199 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000200}
Simon Glassae616512016-01-23 23:27:58 +0000201
Nico Huberd99a2bd2016-02-18 21:42:49 +0000202struct dediprog_transfer_status {
203 int error; /* OK if 0, ERROR else */
204 unsigned int queued_idx;
205 unsigned int finished_idx;
206};
207
208static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
209{
210 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
211 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
212 status->error = 1;
213 msg_perr("SPI bulk read failed!\n");
214 }
215 ++status->finished_idx;
216}
217
218static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
219{
220 if (status->finished_idx >= status->queued_idx)
221 return 0;
222
223 do {
224 struct timeval timeout = { 10, 0 };
225 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
226 if (ret < 0) {
227 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
228 return 1;
229 }
230 } while (finish && (status->finished_idx < status->queued_idx));
231 return 0;
232}
233
Simon Glassae616512016-01-23 23:27:58 +0000234static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
235{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000236 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
237 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000238}
239
240static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
241{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000242 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
243 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000244}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000245
Nico Huberd99a2bd2016-02-18 21:42:49 +0000246
Simon Glass25e9f402015-06-28 13:31:19 +0000247/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000248static int dediprog_set_leds(int leds)
249{
Simon Glass25e9f402015-06-28 13:31:19 +0000250 if (leds < LED_NONE || leds > LED_ALL)
251 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000252
Simon Glassae616512016-01-23 23:27:58 +0000253 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
254 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000255 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000256 * bit 0 == 0: red light is on.
257 *
258 * Additionally, the command structure has changed with the "new" protocol.
259 *
260 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000261 */
Simon Glassae616512016-01-23 23:27:58 +0000262 int target_leds, ret;
David Hendricksf73f8a72018-02-21 07:34:34 -0800263 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000264 target_leds = (leds ^ 7) << 8;
265 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000266 } else {
Simon Glassae616512016-01-23 23:27:58 +0000267 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
268 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
269 } else {
270 target_leds = leds;
271 }
272 target_leds ^= 7;
273
274 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000275 }
276
Stefan Reinauer915b8402011-01-28 09:00:15 +0000277 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000278 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000279 return 1;
280 }
281
Stefan Reinauer915b8402011-01-28 09:00:15 +0000282 return 0;
283}
284
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000285static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000286{
287 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000288 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000289
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000290 switch (millivolt) {
291 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000292 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000293 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000294 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000295 case 1800:
296 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000297 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000298 case 2500:
299 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000300 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000301 case 3500:
302 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000303 break;
304 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000305 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000306 return 1;
307 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000308 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
309 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000310
Nico Huber4099a8a2012-06-16 00:02:27 +0000311 if (voltage_selector == 0) {
312 /* Wait some time as the original driver does. */
313 programmer_delay(200 * 1000);
314 }
Simon Glassae616512016-01-23 23:27:58 +0000315 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000316 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000317 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
318 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000319 return 1;
320 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000321 if (voltage_selector != 0) {
322 /* Wait some time as the original driver does. */
323 programmer_delay(200 * 1000);
324 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000325 return 0;
326}
327
Nico Huber77fa67d2013-02-20 18:03:36 +0000328struct dediprog_spispeeds {
329 const char *const name;
330 const int speed;
331};
332
333static const struct dediprog_spispeeds spispeeds[] = {
334 { "24M", 0x0 },
335 { "12M", 0x2 },
336 { "8M", 0x1 },
337 { "3M", 0x3 },
338 { "2.18M", 0x4 },
339 { "1.5M", 0x5 },
340 { "750k", 0x6 },
341 { "375k", 0x7 },
342 { NULL, 0x0 },
343};
344
Nico Huber77fa67d2013-02-20 18:03:36 +0000345static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000346{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000347 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
348 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
349 return 0;
350 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000351
Simon Glass557eb4f2015-07-05 16:53:22 +0000352 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
353 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000354
Simon Glassae616512016-01-23 23:27:58 +0000355 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000356 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000357 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000358 return 1;
359 }
360 return 0;
361}
362
Nico Huber93db6e12018-09-30 01:18:43 +0200363static void prepare_rw_cmd(
364 struct flashctx *const flash, uint8_t *data_packet, unsigned int count,
365 uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start, int is_read) {
Simon Glassae616512016-01-23 23:27:58 +0000366 /* First 5 bytes are common in both generations. */
367 data_packet[0] = count & 0xff;
368 data_packet[1] = (count >> 8) & 0xff;
369 data_packet[2] = 0; /* RFU */
370 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
371 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
372
David Hendricksf73f8a72018-02-21 07:34:34 -0800373 if (protocol() >= PROTOCOL_V2) {
Nico Huber93db6e12018-09-30 01:18:43 +0200374 if (is_read && flash->chip->feature_bits & FEATURE_4BA_FAST_READ) {
375 data_packet[3] = READ_MODE_4B_ADDR_FAST_0x0C;
376 data_packet[4] = JEDEC_READ_4BA_FAST;
377 } else if (dedi_spi_cmd == WRITE_MODE_PAGE_PGM
378 && (flash->chip->feature_bits & FEATURE_4BA_WRITE)) {
379 data_packet[3] = WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12;
380 data_packet[4] = JEDEC_BYTE_PROGRAM_4BA;
381 }
382
Simon Glassae616512016-01-23 23:27:58 +0000383 *value = *idx = 0;
384 data_packet[5] = 0; /* RFU */
385 data_packet[6] = (start >> 0) & 0xff;
386 data_packet[7] = (start >> 8) & 0xff;
387 data_packet[8] = (start >> 16) & 0xff;
388 data_packet[9] = (start >> 24) & 0xff;
David Hendricksf73f8a72018-02-21 07:34:34 -0800389 if (protocol() >= PROTOCOL_V3) {
390 if (is_read) {
391 data_packet[10] = 0x00; /* address length (3 or 4) */
392 data_packet[11] = 0x00; /* dummy cycle / 2 */
393 } else {
394 /* 16 LSBs and 16 HSBs of page size */
395 /* FIXME: This assumes page size of 256. */
396 data_packet[10] = 0x00;
397 data_packet[11] = 0x01;
398 data_packet[12] = 0x00;
399 data_packet[13] = 0x00;
400 }
401 }
Simon Glassae616512016-01-23 23:27:58 +0000402 } else {
403 *value = start % 0x10000;
404 *idx = start / 0x10000;
405 }
406}
407
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000408/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
409 * @start start address
410 * @len length
411 * @return 0 on success, 1 on failure
412 */
Simon Glassae616512016-01-23 23:27:58 +0000413static 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 +0000414{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000415 int err = 1;
416
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000417 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000418 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000419 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000420
Nico Huberd99a2bd2016-02-18 21:42:49 +0000421 struct dediprog_transfer_status status = { 0, 0, 0 };
422 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
423 struct libusb_transfer *transfer;
424
Nico Huberbbaa1712018-12-05 13:26:20 +0100425 if (len == 0)
426 return 0;
427
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000428 if ((start % chunksize) || (len % chunksize)) {
Simon Glassae616512016-01-23 23:27:58 +0000429 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
430 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000431 return 1;
432 }
433
David Hendricksf73f8a72018-02-21 07:34:34 -0800434 int command_packet_size;
435 switch (protocol()) {
436 case PROTOCOL_V1:
437 command_packet_size = 5;
438 break;
439 case PROTOCOL_V2:
440 command_packet_size = 10;
441 break;
442 case PROTOCOL_V3:
443 command_packet_size = 12;
444 break;
445 default:
446 return 1;
447 }
448
449 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000450 unsigned int value, idx;
Nico Huber93db6e12018-09-30 01:18:43 +0200451 prepare_rw_cmd(flash, data_packet, count, READ_MODE_STD, &value, &idx, start, 1);
Simon Glassae616512016-01-23 23:27:58 +0000452
453 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
454 if (ret != sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000455 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000456 return 1;
457 }
458
Nico Huberd99a2bd2016-02-18 21:42:49 +0000459 /*
460 * Ring buffer of bulk transfers.
461 * Poll until at least one transfer is ready,
462 * schedule next transfers until buffer is full.
463 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000464
Nico Huberd99a2bd2016-02-18 21:42:49 +0000465 /* Allocate bulk transfers. */
466 unsigned int i;
467 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
468 transfers[i] = libusb_alloc_transfer(0);
469 if (!transfers[i]) {
470 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
471 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200472 }
473 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000474
475 /* Now transfer requested chunks using libusb's asynchronous interface. */
476 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200477 while ((status.queued_idx < count) &&
478 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
479 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000480 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
481 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
482 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
483 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
484 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
485 ret = libusb_submit_transfer(transfer);
486 if (ret < 0) {
487 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
488 status.queued_idx, libusb_error_name(ret));
489 goto err_free;
490 }
491 ++status.queued_idx;
492 }
493 if (dediprog_bulk_read_poll(&status, 0))
494 goto err_free;
495 }
496 /* Wait for transfers to finish. */
497 if (dediprog_bulk_read_poll(&status, 1))
498 goto err_free;
499 /* Check if everything has been transmitted. */
500 if ((status.finished_idx < count) || status.error)
501 goto err_free;
502
503 err = 0;
504
505err_free:
506 dediprog_bulk_read_poll(&status, 1);
507 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
508 if (transfers[i]) libusb_free_transfer(transfers[i]);
509 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000510}
511
Simon Glassae616512016-01-23 23:27:58 +0000512static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000513{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000514 int ret;
515 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000516 const unsigned int chunksize = 0x200;
Nico Huberbbaa1712018-12-05 13:26:20 +0100517 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000518 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000519
Simon Glass25e9f402015-06-28 13:31:19 +0000520 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000521
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000522 if (residue) {
523 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
524 start, residue);
525 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000526 if (ret)
527 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000528 }
529
530 /* Round down. */
531 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000532 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000533 if (ret)
534 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000535
536 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000537 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000538 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
539 start, len);
540 ret = spi_read_chunked(flash, buf + residue + bulklen,
541 start + residue + bulklen, len, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000542 if (ret)
543 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000544 }
545
Simon Glass25e9f402015-06-28 13:31:19 +0000546 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000547 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000548err:
549 dediprog_set_leds(LED_ERROR);
550 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000551}
552
Nico Hubera4b14f72012-06-19 12:06:53 +0000553/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
554 * @chunksize length of data chunks, only 256 supported by now
555 * @start start address
556 * @len length
557 * @dedi_spi_cmd dediprog specific write command for spi bus
558 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000559 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000560static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000561 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000562{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000563 /* USB transfer size must be 512, other sizes will NOT work at all.
564 * chunksize is the real data size per USB bulk transfer. The remaining
565 * space in a USB bulk transfer must be filled with 0xff padding.
566 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000567 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000568
Nico Hubera4b14f72012-06-19 12:06:53 +0000569 /*
570 * We should change this check to
571 * chunksize > 512
572 * once we know how to handle different chunk sizes.
573 */
574 if (chunksize != 256) {
575 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
576 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
577 return 1;
578 }
579
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000580 if ((start % chunksize) || (len % chunksize)) {
581 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
582 "at flashrom@flashrom.org\n", __func__, start, len);
583 return 1;
584 }
585
586 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000587 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000588 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000589
David Hendricksf73f8a72018-02-21 07:34:34 -0800590 int command_packet_size;
591 switch (protocol()) {
592 case PROTOCOL_V1:
593 command_packet_size = 5;
594 break;
595 case PROTOCOL_V2:
596 command_packet_size = 10;
597 break;
598 case PROTOCOL_V3:
599 command_packet_size = 14;
600 break;
601 default:
602 return 1;
603 }
604
605 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000606 unsigned int value, idx;
Nico Huber93db6e12018-09-30 01:18:43 +0200607 prepare_rw_cmd(flash, data_packet, count, dedi_spi_cmd, &value, &idx, start, 0);
Simon Glassae616512016-01-23 23:27:58 +0000608 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
609 if (ret != sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000610 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000611 return 1;
612 }
613
Simon Glassae616512016-01-23 23:27:58 +0000614 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000615 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000616 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000617 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000618 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000619 int transferred;
620 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
621 DEFAULT_TIMEOUT);
622 if ((ret < 0) || (transferred != 512)) {
623 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000624 return 1;
625 }
626 }
627
628 return 0;
629}
630
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000631static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000632 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000633{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000634 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000635 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000636 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
637 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000638
Simon Glass25e9f402015-06-28 13:31:19 +0000639 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000640
Nico Hubera4b14f72012-06-19 12:06:53 +0000641 if (chunksize != 256) {
642 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
643 "we don't know how dediprog\nhandles them.\n");
644 /* Write everything like it was residue. */
645 residue = len;
646 }
647
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000648 if (residue) {
649 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
650 start, residue);
Nico Huber93db6e12018-09-30 01:18:43 +0200651 /* No idea about the real limit. Maybe 16 including command and address, maybe more. */
652 ret = spi_write_chunked(flash, buf, start, residue, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000653 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000654 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000655 return ret;
656 }
657 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000658
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000659 /* Round down. */
660 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000661 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000662 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000663 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000664 return ret;
665 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000666
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000667 len -= residue + bulklen;
668 if (len) {
669 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
670 start, len);
671 ret = spi_write_chunked(flash, buf + residue + bulklen,
Nico Huber93db6e12018-09-30 01:18:43 +0200672 start + residue + bulklen, len, 11);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000673 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000674 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000675 return ret;
676 }
677 }
678
Simon Glass25e9f402015-06-28 13:31:19 +0000679 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000680 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000681}
682
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000683static 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 +0000684{
Simon Glass557eb4f2015-07-05 16:53:22 +0000685 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000686}
687
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000688static 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 +0000689{
Simon Glass557eb4f2015-07-05 16:53:22 +0000690 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000691}
692
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000693static int dediprog_spi_send_command(struct flashctx *flash,
694 unsigned int writecnt,
695 unsigned int readcnt,
696 const unsigned char *writearr,
697 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000698{
699 int ret;
700
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000701 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glassae616512016-01-23 23:27:58 +0000702 if (writecnt > flash->mst->spi.max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000703 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000704 return 1;
705 }
Simon Glassae616512016-01-23 23:27:58 +0000706 if (readcnt > flash->mst->spi.max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000707 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000708 return 1;
709 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100710
Simon Glassae616512016-01-23 23:27:58 +0000711 unsigned int idx, value;
712 /* New protocol has options and timeout combined as value while the old one used the value field for
713 * timeout and the index field for options. */
David Hendricksf73f8a72018-02-21 07:34:34 -0800714 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000715 idx = 0;
716 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
717 } else {
718 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
719 value = 0;
720 }
721 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000722 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000723 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000724 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000725 return 1;
726 }
Simon Glassae616512016-01-23 23:27:58 +0000727 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000728 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000729
Stefan Tauner74367bf2016-02-20 20:53:46 +0000730 /* The specifications do state the possibility to set a timeout for transceive transactions.
731 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
732 * can predict the time needed by the previous command or so (untested). In any case, using this
733 * "feature" to set sane-looking timouts for the read below will completely trash performance with
734 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
735 *
736 * The specification also uses only 0 in its examples, so the lesson to learn here:
737 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000738 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800739 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000740 idx = 0;
741 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
742 } else {
743 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
744 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
745 }
746 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000747 */
748 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000749 if (ret != readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000750 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 +0000751 return 1;
752 }
753 return 0;
754}
755
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000756static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000757{
758 int ret;
759 char buf[0x11];
760
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000761 /* Command Receive Device String. */
Simon Glassae616512016-01-23 23:27:58 +0000762 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000763 if (ret != 0x10) {
764 msg_perr("Incomplete/failed Command Receive Device String!\n");
765 return 1;
766 }
767 buf[0x10] = '\0';
768 msg_pdbg("Found a %s\n", buf);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000769 if (memcmp(buf, "SF100", 0x5) == 0)
770 dediprog_devicetype = DEV_SF100;
Jay Thompsoncabe3202018-08-17 14:30:04 -0500771 else if (memcmp(buf, "SF200", 0x5) == 0)
772 dediprog_devicetype = DEV_SF200;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000773 else if (memcmp(buf, "SF600", 0x5) == 0)
774 dediprog_devicetype = DEV_SF600;
775 else {
Jay Thompsoncabe3202018-08-17 14:30:04 -0500776 msg_perr("Device not a SF100, SF200, or SF600!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000777 return 1;
778 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000779
780 int sfnum;
781 int fw[3];
782 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
783 sfnum != dediprog_devicetype) {
Simon Glassae616512016-01-23 23:27:58 +0000784 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000785 return 1;
786 }
Simon Glassae616512016-01-23 23:27:58 +0000787 /* Only these major versions were tested. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000788 if (fw[0] < 2 || fw[0] > 7) {
Simon Glassae616512016-01-23 23:27:58 +0000789 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000790 return 1;
791 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800792
Stefan Reinauer915b8402011-01-28 09:00:15 +0000793 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
David Hendricksf73f8a72018-02-21 07:34:34 -0800794 if (protocol() == PROTOCOL_UNKNOWN) {
795 msg_perr("Internal error: Unable to determine protocol version.\n");
796 return 1;
797 }
Simon Glassae616512016-01-23 23:27:58 +0000798
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000799 return 0;
800}
801
David Hendricksc05900f2016-02-18 21:42:41 +0000802/*
803 * This command presumably sets the voltage for the SF100 itself (not the
804 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
805 * (including all SF600s) do not support it.
806 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000807
David Hendricksc05900f2016-02-18 21:42:41 +0000808/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
809 * Only use dediprog_set_voltage on SF100 programmers with firmware older
810 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
811static int dediprog_set_voltage(void)
812{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000813 unsigned char buf[1] = {0};
814 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000815 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000816 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000817 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000818 return 1;
819 }
David Hendricksc05900f2016-02-18 21:42:41 +0000820 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000821 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000822 return 1;
823 }
David Hendricksc05900f2016-02-18 21:42:41 +0000824
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000825 return 0;
826}
827
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000828static int dediprog_standalone_mode(void)
829{
830 int ret;
831
832 if (dediprog_devicetype != DEV_SF600)
833 return 0;
834
835 msg_pdbg2("Disabling standalone mode.\n");
836 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
837 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000838 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000839 return 1;
840 }
841
842 return 0;
843}
844
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000845#if 0
846/* Something.
847 * Present in eng_detect_blink.log with firmware 3.1.8
848 * Always preceded by Command Receive Device String
849 */
850static int dediprog_command_b(void)
851{
852 int ret;
853 char buf[0x3];
854
Simon Glass557eb4f2015-07-05 16:53:22 +0000855 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
856 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000857 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000858 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000859 return 1;
860 }
861 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
862 (buf[2] != 0xff)) {
863 msg_perr("Unexpected response to Command B!\n");
864 return 1;
865 }
866
867 return 0;
868}
869#endif
870
Simon Glass557eb4f2015-07-05 16:53:22 +0000871static int set_target_flash(enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000872{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000873 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +0000874 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000875 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000876 return 1;
877 }
878 return 0;
879}
880
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000881#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000882/* Returns true if the button is currently pressed. */
883static bool dediprog_get_button(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000884{
Simon Glass557eb4f2015-07-05 16:53:22 +0000885 char buf[1];
886 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
887 buf, 0x1, DEFAULT_TIMEOUT);
888 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000889 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000890 return 1;
891 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000892 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000893}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000894#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000895
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000896static int parse_voltage(char *voltage)
897{
898 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000899 int i;
900 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000901
902 if (!voltage || !strlen(voltage)) {
903 msg_perr("Empty voltage= specified.\n");
904 return -1;
905 }
906 millivolt = (int)strtol(voltage, &tmp, 0);
907 voltage = tmp;
908 /* Handle "," and "." as decimal point. Everything after it is assumed
909 * to be in decimal notation.
910 */
911 if ((*voltage == '.') || (*voltage == ',')) {
912 voltage++;
913 for (i = 0; i < 3; i++) {
914 fraction *= 10;
915 /* Don't advance if the current character is invalid,
916 * but continue multiplying.
917 */
918 if ((*voltage < '0') || (*voltage > '9'))
919 continue;
920 fraction += *voltage - '0';
921 voltage++;
922 }
923 /* Throw away remaining digits. */
924 voltage += strspn(voltage, "0123456789");
925 }
926 /* The remaining string must be empty or "mV" or "V". */
927 tolower_string(voltage);
928
929 /* No unit or "V". */
930 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
931 millivolt *= 1000;
932 millivolt += fraction;
933 } else if (!strncmp(voltage, "mv", 2) ||
934 !strncmp(voltage, "milliv", 6)) {
935 /* No adjustment. fraction is discarded. */
936 } else {
937 /* Garbage at the end of the string. */
938 msg_perr("Garbage voltage= specified.\n");
939 return -1;
940 }
941 return millivolt;
942}
943
Nico Huber93db6e12018-09-30 01:18:43 +0200944static struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000945 .type = SPI_CONTROLLER_DEDIPROG,
Nico Huberdc5af542018-12-22 16:54:59 +0100946 .features = SPI_MASTER_NO_4BA_MODES,
Simon Glassae616512016-01-23 23:27:58 +0000947 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
948 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000949 .command = dediprog_spi_send_command,
950 .multicommand = default_spi_send_multicommand,
951 .read = dediprog_spi_read,
952 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000953 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000954};
955
David Hendricks8bb20212011-06-14 01:35:36 +0000956static int dediprog_shutdown(void *data)
957{
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000958 dediprog_devicetype = DEV_UNKNOWN;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000959
David Hendricks8bb20212011-06-14 01:35:36 +0000960 /* URB 28. Command Set SPI Voltage to 0. */
961 if (dediprog_set_spi_voltage(0x0))
962 return 1;
963
Nico Huberd99a2bd2016-02-18 21:42:49 +0000964 if (libusb_release_interface(dediprog_handle, 0)) {
David Hendricks8bb20212011-06-14 01:35:36 +0000965 msg_perr("Could not release USB interface!\n");
966 return 1;
967 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000968 libusb_close(dediprog_handle);
969 libusb_exit(usb_ctx);
970
David Hendricks8bb20212011-06-14 01:35:36 +0000971 return 0;
972}
973
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000974int dediprog_init(void)
975{
Stefan Taunere659d2d2013-05-03 21:58:28 +0000976 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +0000977 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000978 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000979 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +0200980 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000981 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000982
Nico Huber77fa67d2013-02-20 18:03:36 +0000983 spispeed = extract_programmer_param("spispeed");
984 if (spispeed) {
985 for (i = 0; spispeeds[i].name; ++i) {
986 if (!strcasecmp(spispeeds[i].name, spispeed)) {
987 spispeed_idx = i;
988 break;
989 }
990 }
991 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000992 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +0000993 free(spispeed);
994 return 1;
995 }
996 free(spispeed);
997 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000998
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000999 voltage = extract_programmer_param("voltage");
1000 if (voltage) {
1001 millivolt = parse_voltage(voltage);
1002 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001003 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001004 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001005 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1006 }
1007
Nathan Laredo21541a62012-12-24 22:07:36 +00001008 device = extract_programmer_param("device");
1009 if (device) {
1010 char *dev_suffix;
1011 errno = 0;
1012 usedevice = strtol(device, &dev_suffix, 10);
1013 if (errno != 0 || device == dev_suffix) {
1014 msg_perr("Error: Could not convert 'device'.\n");
1015 free(device);
1016 return 1;
1017 }
1018 if (usedevice < 0 || usedevice > UINT_MAX) {
1019 msg_perr("Error: Value for 'device' is out of range.\n");
1020 free(device);
1021 return 1;
1022 }
1023 if (strlen(dev_suffix) > 0) {
1024 msg_perr("Error: Garbage following 'device' value.\n");
1025 free(device);
1026 return 1;
1027 }
1028 msg_pinfo("Using device %li.\n", usedevice);
1029 }
1030 free(device);
1031
Stefan Taunere659d2d2013-05-03 21:58:28 +00001032 target_str = extract_programmer_param("target");
1033 if (target_str) {
1034 char *target_suffix;
1035 errno = 0;
1036 target = strtol(target_str, &target_suffix, 10);
1037 if (errno != 0 || target_str == target_suffix) {
1038 msg_perr("Error: Could not convert 'target'.\n");
1039 free(target_str);
1040 return 1;
1041 }
1042 if (target < 1 || target > 2) {
1043 msg_perr("Error: Value for 'target' is out of range.\n");
1044 free(target_str);
1045 return 1;
1046 }
1047 if (strlen(target_suffix) > 0) {
1048 msg_perr("Error: Garbage following 'target' value.\n");
1049 free(target_str);
1050 return 1;
1051 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001052 switch (target) {
1053 case 1:
1054 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1055 target = FLASH_TYPE_APPLICATION_FLASH_1;
1056 break;
1057 case 2:
1058 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1059 target = FLASH_TYPE_APPLICATION_FLASH_2;
1060 break;
1061 default:
1062 break;
1063 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001064 }
1065 free(target_str);
1066
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001067 /* Here comes the USB stuff. */
Nico Huberd99a2bd2016-02-18 21:42:49 +00001068 libusb_init(&usb_ctx);
1069 if (!usb_ctx) {
1070 msg_perr("Could not initialize libusb!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001071 return 1;
1072 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001073
1074 const uint16_t vid = devs_dediprog[0].vendor_id;
1075 const uint16_t pid = devs_dediprog[0].device_id;
Daniel Thompson1d507a02018-07-12 11:02:28 +01001076 dediprog_handle = usb_dev_get_by_vid_pid_number(usb_ctx, vid, pid, (unsigned int) usedevice);
David Woodhoused2a7e872013-07-30 09:34:44 +00001077 if (!dediprog_handle) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001078 msg_perr("Could not find a Dediprog programmer on USB.\n");
1079 libusb_exit(usb_ctx);
David Woodhoused2a7e872013-07-30 09:34:44 +00001080 return 1;
1081 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001082 ret = libusb_set_configuration(dediprog_handle, 1);
1083 if (ret != 0) {
1084 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1085 libusb_close(dediprog_handle);
1086 libusb_exit(usb_ctx);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001087 return 1;
1088 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001089 ret = libusb_claim_interface(dediprog_handle, 0);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001090 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001091 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1092 libusb_close(dediprog_handle);
1093 libusb_exit(usb_ctx);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001094 return 1;
1095 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001096
David Hendricks8bb20212011-06-14 01:35:36 +00001097 if (register_shutdown(dediprog_shutdown, NULL))
1098 return 1;
1099
David Hendricksc05900f2016-02-18 21:42:41 +00001100 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1101 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
1102 if (dediprog_check_devicestring()) {
1103 if (dediprog_set_voltage())
1104 return 1;
1105 if (dediprog_check_devicestring())
1106 return 1;
1107 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001108
Jay Thompsoncabe3202018-08-17 14:30:04 -05001109 /* SF100/SF200 uses one in/out endpoint, SF600 uses separate in/out endpoints */
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001110 dediprog_in_endpoint = 2;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001111 switch (dediprog_devicetype) {
1112 case DEV_SF100:
1113 case DEV_SF200:
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001114 dediprog_out_endpoint = 2;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001115 break;
1116 default:
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001117 dediprog_out_endpoint = 1;
Jay Thompsoncabe3202018-08-17 14:30:04 -05001118 break;
1119 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001120
Simon Glass25e9f402015-06-28 13:31:19 +00001121 /* Set all possible LEDs as soon as possible to indicate activity.
1122 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001123 * dediprog_check_devicestring() has queried the device. */
Simon Glass25e9f402015-06-28 13:31:19 +00001124 dediprog_set_leds(LED_ALL);
1125
Simon Glass557eb4f2015-07-05 16:53:22 +00001126 /* Select target/socket, frequency and VCC. */
Nico Huber5e5e8212016-05-04 12:27:58 +02001127 if (set_target_flash(target) ||
Simon Glass557eb4f2015-07-05 16:53:22 +00001128 dediprog_set_spi_speed(spispeed_idx) ||
1129 dediprog_set_spi_voltage(millivolt)) {
Simon Glass25e9f402015-06-28 13:31:19 +00001130 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001131 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001132 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001133
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001134 if (dediprog_standalone_mode())
1135 return 1;
1136
Nico Huber93db6e12018-09-30 01:18:43 +02001137 if (dediprog_devicetype == DEV_SF600 && protocol() == PROTOCOL_V2)
1138 spi_master_dediprog.features |= SPI_MASTER_4BA;
1139
Simon Glassae616512016-01-23 23:27:58 +00001140 if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE))
1141 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001142
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001143 return 0;
1144}