blob: ef43e904bf5eebd41a04dcb0f84c650ec640716d [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,
54 DEV_SF600 = 600,
55};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000056
Simon Glass25e9f402015-06-28 13:31:19 +000057enum dediprog_leds {
58 LED_INVALID = -1,
59 LED_NONE = 0,
60 LED_PASS = 1 << 0,
61 LED_BUSY = 1 << 1,
62 LED_ERROR = 1 << 2,
63 LED_ALL = 7,
64};
65
Simon Glass557eb4f2015-07-05 16:53:22 +000066/* IO bits for CMD_SET_IO_LED message */
67enum dediprog_ios {
68 IO1 = 1 << 0,
69 IO2 = 1 << 1,
70 IO3 = 1 << 2,
71 IO4 = 1 << 3,
72};
73
74enum dediprog_cmds {
75 CMD_TRANSCEIVE = 0x01,
76 CMD_POLL_STATUS_REG = 0x02,
77 CMD_SET_VPP = 0x03,
78 CMD_SET_TARGET = 0x04,
79 CMD_READ_EEPROM = 0x05,
80 CMD_WRITE_EEPROM = 0x06,
81 CMD_SET_IO_LED = 0x07,
82 CMD_READ_PROG_INFO = 0x08,
83 CMD_SET_VCC = 0x09,
84 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000085 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000086 CMD_GET_BUTTON = 0x11,
87 CMD_GET_UID = 0x12,
88 CMD_SET_CS = 0x14,
89 CMD_IO_MODE = 0x15,
90 CMD_FW_UPDATE = 0x1A,
91 CMD_FPGA_UPDATE = 0x1B,
92 CMD_READ_FPGA_VERSION = 0x1C,
93 CMD_SET_HOLD = 0x1D,
94 CMD_READ = 0x20,
95 CMD_WRITE = 0x30,
96 CMD_WRITE_AT45DB = 0x31,
97 CMD_NAND_WRITE = 0x32,
98 CMD_NAND_READ = 0x33,
99 CMD_SET_SPI_CLK = 0x61,
100 CMD_CHECK_SOCKET = 0x62,
101 CMD_DOWNLOAD_PRJ = 0x63,
102 CMD_READ_PRJ_NAME = 0x64,
103 // New protocol/firmware only
104 CMD_CHECK_SDCARD = 0x65,
105 CMD_READ_PRJ = 0x66,
106};
107
108enum dediprog_target {
109 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
110 FLASH_TYPE_FLASH_CARD,
111 FLASH_TYPE_APPLICATION_FLASH_2,
112 FLASH_TYPE_SOCKET,
113};
114
115enum dediprog_readmode {
116 READ_MODE_STD = 1,
117 READ_MODE_FAST = 2,
118 READ_MODE_ATMEL45 = 3,
119 READ_MODE_4B_ADDR_FAST = 4,
120 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
121};
122
123enum dediprog_writemode {
124 WRITE_MODE_PAGE_PGM = 1,
125 WRITE_MODE_PAGE_WRITE = 2,
126 WRITE_MODE_1B_AAI = 3,
127 WRITE_MODE_2B_AAI = 4,
128 WRITE_MODE_128B_PAGE = 5,
129 WRITE_MODE_PAGE_AT26DF041 = 6,
130 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000131 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000132 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000133 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000134 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
135 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
136};
137
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000138enum dediprog_standalone_mode {
139 ENTER_STANDALONE_MODE = 0,
140 LEAVE_STANDALONE_MODE = 1,
141};
142
David Hendricksf73f8a72018-02-21 07:34:34 -0800143/*
144 * These are not official designations; they are for use in flashrom only.
145 * Order must be preserved so that comparison operators work.
146 */
147enum protocol {
148 PROTOCOL_UNKNOWN,
149 PROTOCOL_V1,
150 PROTOCOL_V2,
151 PROTOCOL_V3,
152};
153
Stefan Taunerfdec7472016-02-22 08:59:27 +0000154const struct dev_entry devs_dediprog[] = {
155 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF600"},
156
157 {0},
158};
Simon Glass557eb4f2015-07-05 16:53:22 +0000159
160static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000161enum dediprog_devtype dediprog_devicetype = DEV_UNKNOWN;
Simon Glass557eb4f2015-07-05 16:53:22 +0000162
Nico Huberd99a2bd2016-02-18 21:42:49 +0000163#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
164 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
165/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
166const char * LIBUSB_CALL libusb_error_name(int error_code)
167{
168 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
169 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
170 static char my_libusb_error[18 + 5 + 2];
171 sprintf(my_libusb_error, "libusb error code %i", error_code);
172 return my_libusb_error;
173 } else {
174 return "UNKNOWN";
175 }
176}
177#endif
178
David Hendricksf73f8a72018-02-21 07:34:34 -0800179static enum protocol protocol(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000180{
David Hendricksf73f8a72018-02-21 07:34:34 -0800181 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000182 switch (dediprog_devicetype) {
183 case DEV_SF100:
David Hendricksf73f8a72018-02-21 07:34:34 -0800184 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 5, 0))
185 return PROTOCOL_V1;
186 else
187 return PROTOCOL_V2;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000188 case DEV_SF600:
David Hendricksf73f8a72018-02-21 07:34:34 -0800189 if (dediprog_firmwareversion < FIRMWARE_VERSION(6, 9, 0))
190 return PROTOCOL_V1;
191 else if (dediprog_firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
192 return PROTOCOL_V2;
193 else
194 return PROTOCOL_V3;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000195 default:
David Hendricksf73f8a72018-02-21 07:34:34 -0800196 return PROTOCOL_UNKNOWN;
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000197 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000198}
Simon Glassae616512016-01-23 23:27:58 +0000199
Nico Huberd99a2bd2016-02-18 21:42:49 +0000200struct dediprog_transfer_status {
201 int error; /* OK if 0, ERROR else */
202 unsigned int queued_idx;
203 unsigned int finished_idx;
204};
205
206static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
207{
208 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
209 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
210 status->error = 1;
211 msg_perr("SPI bulk read failed!\n");
212 }
213 ++status->finished_idx;
214}
215
216static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
217{
218 if (status->finished_idx >= status->queued_idx)
219 return 0;
220
221 do {
222 struct timeval timeout = { 10, 0 };
223 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
224 if (ret < 0) {
225 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
226 return 1;
227 }
228 } while (finish && (status->finished_idx < status->queued_idx));
229 return 0;
230}
231
Simon Glassae616512016-01-23 23:27:58 +0000232static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
233{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000234 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
235 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000236}
237
238static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
239{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000240 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
241 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000242}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000243
Nico Huberd99a2bd2016-02-18 21:42:49 +0000244
245/* Might be useful for other USB devices as well. static for now.
246 * num parameter allows user to specify one device of multiple installed */
247static struct libusb_device_handle *get_device_by_vid_pid_number(uint16_t vid, uint16_t pid, unsigned int num)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000248{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000249 struct libusb_device **list;
250 ssize_t count = libusb_get_device_list(usb_ctx, &list);
251 if (count < 0) {
252 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
253 return NULL;
254 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000255
Nico Huberd99a2bd2016-02-18 21:42:49 +0000256 struct libusb_device_handle *handle = NULL;
257 ssize_t i = 0;
258 for (i = 0; i < count; i++) {
259 struct libusb_device *dev = list[i];
260 struct libusb_device_descriptor desc;
261 int err = libusb_get_device_descriptor(dev, &desc);
262 if (err != 0) {
263 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
264 libusb_free_device_list(list, 1);
265 return NULL;
266 }
267 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
268 msg_pdbg("Found USB device %04"PRIx16":%04"PRIx16" at address %d-%d.\n",
269 desc.idVendor, desc.idProduct,
270 libusb_get_bus_number(dev), libusb_get_device_address(dev));
271 if (num == 0) {
272 err = libusb_open(dev, &handle);
273 if (err != 0) {
274 msg_perr("Opening the USB device failed (%s)!\n",
275 libusb_error_name(err));
276 libusb_free_device_list(list, 1);
277 return NULL;
278 }
279 break;
Nathan Laredo21541a62012-12-24 22:07:36 +0000280 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000281 num--;
282 }
283 }
284 libusb_free_device_list(list, 1);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000285
Nico Huberd99a2bd2016-02-18 21:42:49 +0000286 return handle;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000287}
288
Simon Glass25e9f402015-06-28 13:31:19 +0000289/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000290static int dediprog_set_leds(int leds)
291{
Simon Glass25e9f402015-06-28 13:31:19 +0000292 if (leds < LED_NONE || leds > LED_ALL)
293 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000294
Simon Glassae616512016-01-23 23:27:58 +0000295 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
296 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000297 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000298 * bit 0 == 0: red light is on.
299 *
300 * Additionally, the command structure has changed with the "new" protocol.
301 *
302 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000303 */
Simon Glassae616512016-01-23 23:27:58 +0000304 int target_leds, ret;
David Hendricksf73f8a72018-02-21 07:34:34 -0800305 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000306 target_leds = (leds ^ 7) << 8;
307 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000308 } else {
Simon Glassae616512016-01-23 23:27:58 +0000309 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
310 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
311 } else {
312 target_leds = leds;
313 }
314 target_leds ^= 7;
315
316 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000317 }
318
Stefan Reinauer915b8402011-01-28 09:00:15 +0000319 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000320 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000321 return 1;
322 }
323
Stefan Reinauer915b8402011-01-28 09:00:15 +0000324 return 0;
325}
326
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000327static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000328{
329 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000330 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000331
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000332 switch (millivolt) {
333 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000334 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000335 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000336 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000337 case 1800:
338 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000339 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000340 case 2500:
341 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000342 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000343 case 3500:
344 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000345 break;
346 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000347 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000348 return 1;
349 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000350 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
351 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000352
Nico Huber4099a8a2012-06-16 00:02:27 +0000353 if (voltage_selector == 0) {
354 /* Wait some time as the original driver does. */
355 programmer_delay(200 * 1000);
356 }
Simon Glassae616512016-01-23 23:27:58 +0000357 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000358 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000359 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
360 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000361 return 1;
362 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000363 if (voltage_selector != 0) {
364 /* Wait some time as the original driver does. */
365 programmer_delay(200 * 1000);
366 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000367 return 0;
368}
369
Nico Huber77fa67d2013-02-20 18:03:36 +0000370struct dediprog_spispeeds {
371 const char *const name;
372 const int speed;
373};
374
375static const struct dediprog_spispeeds spispeeds[] = {
376 { "24M", 0x0 },
377 { "12M", 0x2 },
378 { "8M", 0x1 },
379 { "3M", 0x3 },
380 { "2.18M", 0x4 },
381 { "1.5M", 0x5 },
382 { "750k", 0x6 },
383 { "375k", 0x7 },
384 { NULL, 0x0 },
385};
386
Nico Huber77fa67d2013-02-20 18:03:36 +0000387static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000388{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000389 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
390 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
391 return 0;
392 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000393
Simon Glass557eb4f2015-07-05 16:53:22 +0000394 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
395 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000396
Simon Glassae616512016-01-23 23:27:58 +0000397 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000398 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000399 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000400 return 1;
401 }
402 return 0;
403}
404
David Hendricksf73f8a72018-02-21 07:34:34 -0800405static void fill_rw_cmd_payload(uint8_t *data_packet, unsigned int count, uint8_t dedi_spi_cmd,
406 unsigned int *value, unsigned int *idx, unsigned int start, int is_read) {
Simon Glassae616512016-01-23 23:27:58 +0000407 /* First 5 bytes are common in both generations. */
408 data_packet[0] = count & 0xff;
409 data_packet[1] = (count >> 8) & 0xff;
410 data_packet[2] = 0; /* RFU */
411 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
412 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
413
David Hendricksf73f8a72018-02-21 07:34:34 -0800414 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000415 *value = *idx = 0;
416 data_packet[5] = 0; /* RFU */
417 data_packet[6] = (start >> 0) & 0xff;
418 data_packet[7] = (start >> 8) & 0xff;
419 data_packet[8] = (start >> 16) & 0xff;
420 data_packet[9] = (start >> 24) & 0xff;
David Hendricksf73f8a72018-02-21 07:34:34 -0800421 if (protocol() >= PROTOCOL_V3) {
422 if (is_read) {
423 data_packet[10] = 0x00; /* address length (3 or 4) */
424 data_packet[11] = 0x00; /* dummy cycle / 2 */
425 } else {
426 /* 16 LSBs and 16 HSBs of page size */
427 /* FIXME: This assumes page size of 256. */
428 data_packet[10] = 0x00;
429 data_packet[11] = 0x01;
430 data_packet[12] = 0x00;
431 data_packet[13] = 0x00;
432 }
433 }
Simon Glassae616512016-01-23 23:27:58 +0000434 } else {
435 *value = start % 0x10000;
436 *idx = start / 0x10000;
437 }
438}
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;
448
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000449 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000450 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000451 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000452
Nico Huberd99a2bd2016-02-18 21:42:49 +0000453 struct dediprog_transfer_status status = { 0, 0, 0 };
454 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
455 struct libusb_transfer *transfer;
456
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000457 if ((start % chunksize) || (len % chunksize)) {
Simon Glassae616512016-01-23 23:27:58 +0000458 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
459 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000460 return 1;
461 }
462
Simon Glassae616512016-01-23 23:27:58 +0000463 if (len == 0)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000464 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000465
David Hendricksf73f8a72018-02-21 07:34:34 -0800466 int command_packet_size;
467 switch (protocol()) {
468 case PROTOCOL_V1:
469 command_packet_size = 5;
470 break;
471 case PROTOCOL_V2:
472 command_packet_size = 10;
473 break;
474 case PROTOCOL_V3:
475 command_packet_size = 12;
476 break;
477 default:
478 return 1;
479 }
480
481 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000482 unsigned int value, idx;
David Hendricksf73f8a72018-02-21 07:34:34 -0800483 fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start, 1);
Simon Glassae616512016-01-23 23:27:58 +0000484
485 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
486 if (ret != sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000487 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000488 return 1;
489 }
490
Nico Huberd99a2bd2016-02-18 21:42:49 +0000491 /*
492 * Ring buffer of bulk transfers.
493 * Poll until at least one transfer is ready,
494 * schedule next transfers until buffer is full.
495 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000496
Nico Huberd99a2bd2016-02-18 21:42:49 +0000497 /* Allocate bulk transfers. */
498 unsigned int i;
499 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
500 transfers[i] = libusb_alloc_transfer(0);
501 if (!transfers[i]) {
502 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
503 goto err_free;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200504 }
505 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000506
507 /* Now transfer requested chunks using libusb's asynchronous interface. */
508 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200509 while ((status.queued_idx < count) &&
510 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
511 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000512 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
513 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
514 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
515 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
516 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
517 ret = libusb_submit_transfer(transfer);
518 if (ret < 0) {
519 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
520 status.queued_idx, libusb_error_name(ret));
521 goto err_free;
522 }
523 ++status.queued_idx;
524 }
525 if (dediprog_bulk_read_poll(&status, 0))
526 goto err_free;
527 }
528 /* Wait for transfers to finish. */
529 if (dediprog_bulk_read_poll(&status, 1))
530 goto err_free;
531 /* Check if everything has been transmitted. */
532 if ((status.finished_idx < count) || status.error)
533 goto err_free;
534
535 err = 0;
536
537err_free:
538 dediprog_bulk_read_poll(&status, 1);
539 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
540 if (transfers[i]) libusb_free_transfer(transfers[i]);
541 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000542}
543
Simon Glassae616512016-01-23 23:27:58 +0000544static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000545{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000546 int ret;
547 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000548 const unsigned int chunksize = 0x200;
549 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
550 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000551
Simon Glass25e9f402015-06-28 13:31:19 +0000552 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000553
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000554 if (residue) {
555 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
556 start, residue);
557 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000558 if (ret)
559 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000560 }
561
562 /* Round down. */
563 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000564 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000565 if (ret)
566 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000567
568 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000569 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000570 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
571 start, len);
572 ret = spi_read_chunked(flash, buf + residue + bulklen,
573 start + residue + bulklen, len, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000574 if (ret)
575 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000576 }
577
Simon Glass25e9f402015-06-28 13:31:19 +0000578 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000579 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000580err:
581 dediprog_set_leds(LED_ERROR);
582 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000583}
584
Nico Hubera4b14f72012-06-19 12:06:53 +0000585/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
586 * @chunksize length of data chunks, only 256 supported by now
587 * @start start address
588 * @len length
589 * @dedi_spi_cmd dediprog specific write command for spi bus
590 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000591 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000592static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000593 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000594{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000595 /* USB transfer size must be 512, other sizes will NOT work at all.
596 * chunksize is the real data size per USB bulk transfer. The remaining
597 * space in a USB bulk transfer must be filled with 0xff padding.
598 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000599 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000600
Nico Hubera4b14f72012-06-19 12:06:53 +0000601 /*
602 * We should change this check to
603 * chunksize > 512
604 * once we know how to handle different chunk sizes.
605 */
606 if (chunksize != 256) {
607 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
608 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
609 return 1;
610 }
611
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000612 if ((start % chunksize) || (len % chunksize)) {
613 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
614 "at flashrom@flashrom.org\n", __func__, start, len);
615 return 1;
616 }
617
618 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000619 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000620 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000621
David Hendricksf73f8a72018-02-21 07:34:34 -0800622 int command_packet_size;
623 switch (protocol()) {
624 case PROTOCOL_V1:
625 command_packet_size = 5;
626 break;
627 case PROTOCOL_V2:
628 command_packet_size = 10;
629 break;
630 case PROTOCOL_V3:
631 command_packet_size = 14;
632 break;
633 default:
634 return 1;
635 }
636
637 uint8_t data_packet[command_packet_size];
Simon Glassae616512016-01-23 23:27:58 +0000638 unsigned int value, idx;
David Hendricksf73f8a72018-02-21 07:34:34 -0800639 fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start, 0);
Simon Glassae616512016-01-23 23:27:58 +0000640 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
641 if (ret != sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000642 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000643 return 1;
644 }
645
Simon Glassae616512016-01-23 23:27:58 +0000646 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000647 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000648 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000649 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000650 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000651 int transferred;
652 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
653 DEFAULT_TIMEOUT);
654 if ((ret < 0) || (transferred != 512)) {
655 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000656 return 1;
657 }
658 }
659
660 return 0;
661}
662
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000663static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000664 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000665{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000666 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000667 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000668 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
669 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000670
Simon Glass25e9f402015-06-28 13:31:19 +0000671 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000672
Nico Hubera4b14f72012-06-19 12:06:53 +0000673 if (chunksize != 256) {
674 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
675 "we don't know how dediprog\nhandles them.\n");
676 /* Write everything like it was residue. */
677 residue = len;
678 }
679
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000680 if (residue) {
681 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
682 start, residue);
683 /* No idea about the real limit. Maybe 12, maybe more. */
684 ret = spi_write_chunked(flash, buf, start, residue, 12);
685 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000686 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000687 return ret;
688 }
689 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000690
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000691 /* Round down. */
692 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000693 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000694 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000695 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000696 return ret;
697 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000698
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000699 len -= residue + bulklen;
700 if (len) {
701 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
702 start, len);
703 ret = spi_write_chunked(flash, buf + residue + bulklen,
704 start + residue + bulklen, len, 12);
705 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000706 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000707 return ret;
708 }
709 }
710
Simon Glass25e9f402015-06-28 13:31:19 +0000711 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000712 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000713}
714
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000715static 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 +0000716{
Simon Glass557eb4f2015-07-05 16:53:22 +0000717 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000718}
719
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000720static 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 +0000721{
Simon Glass557eb4f2015-07-05 16:53:22 +0000722 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000723}
724
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000725static int dediprog_spi_send_command(struct flashctx *flash,
726 unsigned int writecnt,
727 unsigned int readcnt,
728 const unsigned char *writearr,
729 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000730{
731 int ret;
732
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000733 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glassae616512016-01-23 23:27:58 +0000734 if (writecnt > flash->mst->spi.max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000735 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000736 return 1;
737 }
Simon Glassae616512016-01-23 23:27:58 +0000738 if (readcnt > flash->mst->spi.max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000739 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000740 return 1;
741 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000742
Simon Glassae616512016-01-23 23:27:58 +0000743 unsigned int idx, value;
744 /* New protocol has options and timeout combined as value while the old one used the value field for
745 * timeout and the index field for options. */
David Hendricksf73f8a72018-02-21 07:34:34 -0800746 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000747 idx = 0;
748 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
749 } else {
750 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
751 value = 0;
752 }
753 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000754 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000755 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000756 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000757 return 1;
758 }
Simon Glassae616512016-01-23 23:27:58 +0000759 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000760 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000761
Stefan Tauner74367bf2016-02-20 20:53:46 +0000762 /* The specifications do state the possibility to set a timeout for transceive transactions.
763 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
764 * can predict the time needed by the previous command or so (untested). In any case, using this
765 * "feature" to set sane-looking timouts for the read below will completely trash performance with
766 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
767 *
768 * The specification also uses only 0 in its examples, so the lesson to learn here:
769 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000770 const uint8_t read_timeout = 10 + readcnt/512;
David Hendricksf73f8a72018-02-21 07:34:34 -0800771 if (protocol() >= PROTOCOL_V2) {
Simon Glassae616512016-01-23 23:27:58 +0000772 idx = 0;
773 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
774 } else {
775 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
776 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
777 }
778 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000779 */
780 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000781 if (ret != readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000782 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 +0000783 return 1;
784 }
785 return 0;
786}
787
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000788static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000789{
790 int ret;
791 char buf[0x11];
792
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000793 /* Command Receive Device String. */
Simon Glassae616512016-01-23 23:27:58 +0000794 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000795 if (ret != 0x10) {
796 msg_perr("Incomplete/failed Command Receive Device String!\n");
797 return 1;
798 }
799 buf[0x10] = '\0';
800 msg_pdbg("Found a %s\n", buf);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000801 if (memcmp(buf, "SF100", 0x5) == 0)
802 dediprog_devicetype = DEV_SF100;
803 else if (memcmp(buf, "SF600", 0x5) == 0)
804 dediprog_devicetype = DEV_SF600;
805 else {
806 msg_perr("Device not a SF100 or SF600!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000807 return 1;
808 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000809
810 int sfnum;
811 int fw[3];
812 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
813 sfnum != dediprog_devicetype) {
Simon Glassae616512016-01-23 23:27:58 +0000814 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000815 return 1;
816 }
Simon Glassae616512016-01-23 23:27:58 +0000817 /* Only these major versions were tested. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000818 if (fw[0] < 2 || fw[0] > 7) {
Simon Glassae616512016-01-23 23:27:58 +0000819 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000820 return 1;
821 }
David Hendricksf73f8a72018-02-21 07:34:34 -0800822
Stefan Reinauer915b8402011-01-28 09:00:15 +0000823 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
David Hendricksf73f8a72018-02-21 07:34:34 -0800824 if (protocol() == PROTOCOL_UNKNOWN) {
825 msg_perr("Internal error: Unable to determine protocol version.\n");
826 return 1;
827 }
Simon Glassae616512016-01-23 23:27:58 +0000828
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000829 return 0;
830}
831
David Hendricksc05900f2016-02-18 21:42:41 +0000832/*
833 * This command presumably sets the voltage for the SF100 itself (not the
834 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
835 * (including all SF600s) do not support it.
836 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000837
David Hendricksc05900f2016-02-18 21:42:41 +0000838/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
839 * Only use dediprog_set_voltage on SF100 programmers with firmware older
840 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
841static int dediprog_set_voltage(void)
842{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000843 unsigned char buf[1] = {0};
844 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000845 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000846 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000847 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000848 return 1;
849 }
David Hendricksc05900f2016-02-18 21:42:41 +0000850 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000851 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000852 return 1;
853 }
David Hendricksc05900f2016-02-18 21:42:41 +0000854
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000855 return 0;
856}
857
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000858static int dediprog_standalone_mode(void)
859{
860 int ret;
861
862 if (dediprog_devicetype != DEV_SF600)
863 return 0;
864
865 msg_pdbg2("Disabling standalone mode.\n");
866 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
867 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000868 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000869 return 1;
870 }
871
872 return 0;
873}
874
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000875#if 0
876/* Something.
877 * Present in eng_detect_blink.log with firmware 3.1.8
878 * Always preceded by Command Receive Device String
879 */
880static int dediprog_command_b(void)
881{
882 int ret;
883 char buf[0x3];
884
Simon Glass557eb4f2015-07-05 16:53:22 +0000885 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
886 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000887 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000888 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000889 return 1;
890 }
891 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
892 (buf[2] != 0xff)) {
893 msg_perr("Unexpected response to Command B!\n");
894 return 1;
895 }
896
897 return 0;
898}
899#endif
900
Simon Glass557eb4f2015-07-05 16:53:22 +0000901static int set_target_flash(enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000902{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000903 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +0000904 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000905 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000906 return 1;
907 }
908 return 0;
909}
910
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000911#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000912/* Returns true if the button is currently pressed. */
913static bool dediprog_get_button(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000914{
Simon Glass557eb4f2015-07-05 16:53:22 +0000915 char buf[1];
916 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
917 buf, 0x1, DEFAULT_TIMEOUT);
918 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000919 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000920 return 1;
921 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000922 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000923}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000924#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000925
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000926static int parse_voltage(char *voltage)
927{
928 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000929 int i;
930 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000931
932 if (!voltage || !strlen(voltage)) {
933 msg_perr("Empty voltage= specified.\n");
934 return -1;
935 }
936 millivolt = (int)strtol(voltage, &tmp, 0);
937 voltage = tmp;
938 /* Handle "," and "." as decimal point. Everything after it is assumed
939 * to be in decimal notation.
940 */
941 if ((*voltage == '.') || (*voltage == ',')) {
942 voltage++;
943 for (i = 0; i < 3; i++) {
944 fraction *= 10;
945 /* Don't advance if the current character is invalid,
946 * but continue multiplying.
947 */
948 if ((*voltage < '0') || (*voltage > '9'))
949 continue;
950 fraction += *voltage - '0';
951 voltage++;
952 }
953 /* Throw away remaining digits. */
954 voltage += strspn(voltage, "0123456789");
955 }
956 /* The remaining string must be empty or "mV" or "V". */
957 tolower_string(voltage);
958
959 /* No unit or "V". */
960 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
961 millivolt *= 1000;
962 millivolt += fraction;
963 } else if (!strncmp(voltage, "mv", 2) ||
964 !strncmp(voltage, "milliv", 6)) {
965 /* No adjustment. fraction is discarded. */
966 } else {
967 /* Garbage at the end of the string. */
968 msg_perr("Garbage voltage= specified.\n");
969 return -1;
970 }
971 return millivolt;
972}
973
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000974static const struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000975 .type = SPI_CONTROLLER_DEDIPROG,
Simon Glassae616512016-01-23 23:27:58 +0000976 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
977 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000978 .command = dediprog_spi_send_command,
979 .multicommand = default_spi_send_multicommand,
980 .read = dediprog_spi_read,
981 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000982 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000983};
984
David Hendricks8bb20212011-06-14 01:35:36 +0000985static int dediprog_shutdown(void *data)
986{
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000987 dediprog_devicetype = DEV_UNKNOWN;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000988
David Hendricks8bb20212011-06-14 01:35:36 +0000989 /* URB 28. Command Set SPI Voltage to 0. */
990 if (dediprog_set_spi_voltage(0x0))
991 return 1;
992
Nico Huberd99a2bd2016-02-18 21:42:49 +0000993 if (libusb_release_interface(dediprog_handle, 0)) {
David Hendricks8bb20212011-06-14 01:35:36 +0000994 msg_perr("Could not release USB interface!\n");
995 return 1;
996 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000997 libusb_close(dediprog_handle);
998 libusb_exit(usb_ctx);
999
David Hendricks8bb20212011-06-14 01:35:36 +00001000 return 0;
1001}
1002
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001003int dediprog_init(void)
1004{
Stefan Taunere659d2d2013-05-03 21:58:28 +00001005 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +00001006 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001007 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +00001008 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +02001009 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +00001010 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001011
Nico Huber77fa67d2013-02-20 18:03:36 +00001012 spispeed = extract_programmer_param("spispeed");
1013 if (spispeed) {
1014 for (i = 0; spispeeds[i].name; ++i) {
1015 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1016 spispeed_idx = i;
1017 break;
1018 }
1019 }
1020 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +00001021 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +00001022 free(spispeed);
1023 return 1;
1024 }
1025 free(spispeed);
1026 }
Simon Glass557eb4f2015-07-05 16:53:22 +00001027
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001028 voltage = extract_programmer_param("voltage");
1029 if (voltage) {
1030 millivolt = parse_voltage(voltage);
1031 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001032 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001033 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +00001034 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1035 }
1036
Nathan Laredo21541a62012-12-24 22:07:36 +00001037 device = extract_programmer_param("device");
1038 if (device) {
1039 char *dev_suffix;
1040 errno = 0;
1041 usedevice = strtol(device, &dev_suffix, 10);
1042 if (errno != 0 || device == dev_suffix) {
1043 msg_perr("Error: Could not convert 'device'.\n");
1044 free(device);
1045 return 1;
1046 }
1047 if (usedevice < 0 || usedevice > UINT_MAX) {
1048 msg_perr("Error: Value for 'device' is out of range.\n");
1049 free(device);
1050 return 1;
1051 }
1052 if (strlen(dev_suffix) > 0) {
1053 msg_perr("Error: Garbage following 'device' value.\n");
1054 free(device);
1055 return 1;
1056 }
1057 msg_pinfo("Using device %li.\n", usedevice);
1058 }
1059 free(device);
1060
Stefan Taunere659d2d2013-05-03 21:58:28 +00001061 target_str = extract_programmer_param("target");
1062 if (target_str) {
1063 char *target_suffix;
1064 errno = 0;
1065 target = strtol(target_str, &target_suffix, 10);
1066 if (errno != 0 || target_str == target_suffix) {
1067 msg_perr("Error: Could not convert 'target'.\n");
1068 free(target_str);
1069 return 1;
1070 }
1071 if (target < 1 || target > 2) {
1072 msg_perr("Error: Value for 'target' is out of range.\n");
1073 free(target_str);
1074 return 1;
1075 }
1076 if (strlen(target_suffix) > 0) {
1077 msg_perr("Error: Garbage following 'target' value.\n");
1078 free(target_str);
1079 return 1;
1080 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001081 switch (target) {
1082 case 1:
1083 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1084 target = FLASH_TYPE_APPLICATION_FLASH_1;
1085 break;
1086 case 2:
1087 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1088 target = FLASH_TYPE_APPLICATION_FLASH_2;
1089 break;
1090 default:
1091 break;
1092 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001093 }
1094 free(target_str);
1095
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001096 /* Here comes the USB stuff. */
Nico Huberd99a2bd2016-02-18 21:42:49 +00001097 libusb_init(&usb_ctx);
1098 if (!usb_ctx) {
1099 msg_perr("Could not initialize libusb!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001100 return 1;
1101 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001102
1103 const uint16_t vid = devs_dediprog[0].vendor_id;
1104 const uint16_t pid = devs_dediprog[0].device_id;
1105 dediprog_handle = get_device_by_vid_pid_number(vid, pid, (unsigned int) usedevice);
David Woodhoused2a7e872013-07-30 09:34:44 +00001106 if (!dediprog_handle) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001107 msg_perr("Could not find a Dediprog programmer on USB.\n");
1108 libusb_exit(usb_ctx);
David Woodhoused2a7e872013-07-30 09:34:44 +00001109 return 1;
1110 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001111 ret = libusb_set_configuration(dediprog_handle, 1);
1112 if (ret != 0) {
1113 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1114 libusb_close(dediprog_handle);
1115 libusb_exit(usb_ctx);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001116 return 1;
1117 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001118 ret = libusb_claim_interface(dediprog_handle, 0);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001119 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001120 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1121 libusb_close(dediprog_handle);
1122 libusb_exit(usb_ctx);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001123 return 1;
1124 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001125
David Hendricks8bb20212011-06-14 01:35:36 +00001126 if (register_shutdown(dediprog_shutdown, NULL))
1127 return 1;
1128
David Hendricksc05900f2016-02-18 21:42:41 +00001129 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1130 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
1131 if (dediprog_check_devicestring()) {
1132 if (dediprog_set_voltage())
1133 return 1;
1134 if (dediprog_check_devicestring())
1135 return 1;
1136 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001137
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001138 /* SF100 only has 1 endpoint for in/out, SF600 uses two separate endpoints instead. */
1139 dediprog_in_endpoint = 2;
1140 if (dediprog_devicetype == DEV_SF100)
1141 dediprog_out_endpoint = 2;
1142 else
1143 dediprog_out_endpoint = 1;
1144
Simon Glass25e9f402015-06-28 13:31:19 +00001145 /* Set all possible LEDs as soon as possible to indicate activity.
1146 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
David Hendricksf73f8a72018-02-21 07:34:34 -08001147 * dediprog_check_devicestring() has queried the device. */
Simon Glass25e9f402015-06-28 13:31:19 +00001148 dediprog_set_leds(LED_ALL);
1149
Simon Glass557eb4f2015-07-05 16:53:22 +00001150 /* Select target/socket, frequency and VCC. */
Nico Huber5e5e8212016-05-04 12:27:58 +02001151 if (set_target_flash(target) ||
Simon Glass557eb4f2015-07-05 16:53:22 +00001152 dediprog_set_spi_speed(spispeed_idx) ||
1153 dediprog_set_spi_voltage(millivolt)) {
Simon Glass25e9f402015-06-28 13:31:19 +00001154 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001155 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001156 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001157
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001158 if (dediprog_standalone_mode())
1159 return 1;
1160
Simon Glassae616512016-01-23 23:27:58 +00001161 if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE))
1162 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001163
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001164 return 0;
1165}