blob: 2f5b441fe4f8c39419823bdaafd101339ad3b7bc [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000022#include "platform.h"
23
Nico Huber4d440a72017-08-15 11:26:48 +020024#include <sys/types.h>
Stefan Tauner5c316f92015-02-08 21:57:52 +000025#include <stdlib.h>
Mathias Krausedb7afc52010-11-09 23:30:43 +000026#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000027#include <string.h>
Stefan Taunere34e3e82013-01-01 00:06:51 +000028#include <limits.h>
Nathan Laredo21541a62012-12-24 22:07:36 +000029#include <errno.h>
Nico Huberd99a2bd2016-02-18 21:42:49 +000030#include <libusb.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000031#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000032#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000033#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000034#include "spi.h"
35
Nico Huberd99a2bd2016-02-18 21:42:49 +000036/* LIBUSB_CALL ensures the right calling conventions on libusb callbacks.
37 * However, the macro is not defined everywhere. m(
38 */
39#ifndef LIBUSB_CALL
40#define LIBUSB_CALL
41#endif
42
Stefan Reinauer915b8402011-01-28 09:00:15 +000043#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000044#define DEFAULT_TIMEOUT 3000
Nico Huberd99a2bd2016-02-18 21:42:49 +000045#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
46#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
47#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
48#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
49#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
50struct libusb_context *usb_ctx;
51static libusb_device_handle *dediprog_handle;
David Woodhouse7f3b89f2016-02-18 21:42:15 +000052static int dediprog_in_endpoint;
53static int dediprog_out_endpoint;
54
55enum dediprog_devtype {
56 DEV_UNKNOWN = 0,
57 DEV_SF100 = 100,
58 DEV_SF600 = 600,
59};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000060
Simon Glass25e9f402015-06-28 13:31:19 +000061enum dediprog_leds {
62 LED_INVALID = -1,
63 LED_NONE = 0,
64 LED_PASS = 1 << 0,
65 LED_BUSY = 1 << 1,
66 LED_ERROR = 1 << 2,
67 LED_ALL = 7,
68};
69
Simon Glass557eb4f2015-07-05 16:53:22 +000070/* IO bits for CMD_SET_IO_LED message */
71enum dediprog_ios {
72 IO1 = 1 << 0,
73 IO2 = 1 << 1,
74 IO3 = 1 << 2,
75 IO4 = 1 << 3,
76};
77
78enum dediprog_cmds {
79 CMD_TRANSCEIVE = 0x01,
80 CMD_POLL_STATUS_REG = 0x02,
81 CMD_SET_VPP = 0x03,
82 CMD_SET_TARGET = 0x04,
83 CMD_READ_EEPROM = 0x05,
84 CMD_WRITE_EEPROM = 0x06,
85 CMD_SET_IO_LED = 0x07,
86 CMD_READ_PROG_INFO = 0x08,
87 CMD_SET_VCC = 0x09,
88 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000089 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000090 CMD_GET_BUTTON = 0x11,
91 CMD_GET_UID = 0x12,
92 CMD_SET_CS = 0x14,
93 CMD_IO_MODE = 0x15,
94 CMD_FW_UPDATE = 0x1A,
95 CMD_FPGA_UPDATE = 0x1B,
96 CMD_READ_FPGA_VERSION = 0x1C,
97 CMD_SET_HOLD = 0x1D,
98 CMD_READ = 0x20,
99 CMD_WRITE = 0x30,
100 CMD_WRITE_AT45DB = 0x31,
101 CMD_NAND_WRITE = 0x32,
102 CMD_NAND_READ = 0x33,
103 CMD_SET_SPI_CLK = 0x61,
104 CMD_CHECK_SOCKET = 0x62,
105 CMD_DOWNLOAD_PRJ = 0x63,
106 CMD_READ_PRJ_NAME = 0x64,
107 // New protocol/firmware only
108 CMD_CHECK_SDCARD = 0x65,
109 CMD_READ_PRJ = 0x66,
110};
111
112enum dediprog_target {
113 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
114 FLASH_TYPE_FLASH_CARD,
115 FLASH_TYPE_APPLICATION_FLASH_2,
116 FLASH_TYPE_SOCKET,
117};
118
119enum dediprog_readmode {
120 READ_MODE_STD = 1,
121 READ_MODE_FAST = 2,
122 READ_MODE_ATMEL45 = 3,
123 READ_MODE_4B_ADDR_FAST = 4,
124 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
125};
126
127enum dediprog_writemode {
128 WRITE_MODE_PAGE_PGM = 1,
129 WRITE_MODE_PAGE_WRITE = 2,
130 WRITE_MODE_1B_AAI = 3,
131 WRITE_MODE_2B_AAI = 4,
132 WRITE_MODE_128B_PAGE = 5,
133 WRITE_MODE_PAGE_AT26DF041 = 6,
134 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000135 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000136 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000137 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000138 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
139 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
140};
141
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000142enum dediprog_standalone_mode {
143 ENTER_STANDALONE_MODE = 0,
144 LEAVE_STANDALONE_MODE = 1,
145};
146
Stefan Taunerfdec7472016-02-22 08:59:27 +0000147const struct dev_entry devs_dediprog[] = {
148 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF600"},
149
150 {0},
151};
Simon Glass557eb4f2015-07-05 16:53:22 +0000152
153static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000154enum dediprog_devtype dediprog_devicetype = DEV_UNKNOWN;
Simon Glass557eb4f2015-07-05 16:53:22 +0000155
Nico Huberd99a2bd2016-02-18 21:42:49 +0000156#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
157 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
158/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
159const char * LIBUSB_CALL libusb_error_name(int error_code)
160{
161 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
162 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
163 static char my_libusb_error[18 + 5 + 2];
164 sprintf(my_libusb_error, "libusb error code %i", error_code);
165 return my_libusb_error;
166 } else {
167 return "UNKNOWN";
168 }
169}
170#endif
171
Simon Glassae616512016-01-23 23:27:58 +0000172/* Returns true if firmware (and thus hardware) supports the "new" protocol */
173static bool is_new_prot(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000174{
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000175 switch (dediprog_devicetype) {
176 case DEV_SF100:
177 return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
178 case DEV_SF600:
179 return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0);
180 default:
181 return 0;
182 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000183}
Simon Glassae616512016-01-23 23:27:58 +0000184
Nico Huberd99a2bd2016-02-18 21:42:49 +0000185struct dediprog_transfer_status {
186 int error; /* OK if 0, ERROR else */
187 unsigned int queued_idx;
188 unsigned int finished_idx;
189};
190
191static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
192{
193 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
194 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
195 status->error = 1;
196 msg_perr("SPI bulk read failed!\n");
197 }
198 ++status->finished_idx;
199}
200
201static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
202{
203 if (status->finished_idx >= status->queued_idx)
204 return 0;
205
206 do {
207 struct timeval timeout = { 10, 0 };
208 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
209 if (ret < 0) {
210 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
211 return 1;
212 }
213 } while (finish && (status->finished_idx < status->queued_idx));
214 return 0;
215}
216
Simon Glassae616512016-01-23 23:27:58 +0000217static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
218{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000219 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
220 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000221}
222
223static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
224{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000225 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
226 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000227}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000228
Nico Huberd99a2bd2016-02-18 21:42:49 +0000229
230/* Might be useful for other USB devices as well. static for now.
231 * num parameter allows user to specify one device of multiple installed */
232static 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 +0000233{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000234 struct libusb_device **list;
235 ssize_t count = libusb_get_device_list(usb_ctx, &list);
236 if (count < 0) {
237 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
238 return NULL;
239 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000240
Nico Huberd99a2bd2016-02-18 21:42:49 +0000241 struct libusb_device_handle *handle = NULL;
242 ssize_t i = 0;
243 for (i = 0; i < count; i++) {
244 struct libusb_device *dev = list[i];
245 struct libusb_device_descriptor desc;
246 int err = libusb_get_device_descriptor(dev, &desc);
247 if (err != 0) {
248 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
249 libusb_free_device_list(list, 1);
250 return NULL;
251 }
252 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
253 msg_pdbg("Found USB device %04"PRIx16":%04"PRIx16" at address %d-%d.\n",
254 desc.idVendor, desc.idProduct,
255 libusb_get_bus_number(dev), libusb_get_device_address(dev));
256 if (num == 0) {
257 err = libusb_open(dev, &handle);
258 if (err != 0) {
259 msg_perr("Opening the USB device failed (%s)!\n",
260 libusb_error_name(err));
261 libusb_free_device_list(list, 1);
262 return NULL;
263 }
264 break;
Nathan Laredo21541a62012-12-24 22:07:36 +0000265 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000266 num--;
267 }
268 }
269 libusb_free_device_list(list, 1);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000270
Nico Huberd99a2bd2016-02-18 21:42:49 +0000271 return handle;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000272}
273
Simon Glass25e9f402015-06-28 13:31:19 +0000274/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000275static int dediprog_set_leds(int leds)
276{
Simon Glass25e9f402015-06-28 13:31:19 +0000277 if (leds < LED_NONE || leds > LED_ALL)
278 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000279
Simon Glassae616512016-01-23 23:27:58 +0000280 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
281 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000282 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000283 * bit 0 == 0: red light is on.
284 *
285 * Additionally, the command structure has changed with the "new" protocol.
286 *
287 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000288 */
Simon Glassae616512016-01-23 23:27:58 +0000289 int target_leds, ret;
290 if (is_new_prot()) {
291 target_leds = (leds ^ 7) << 8;
292 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000293 } else {
Simon Glassae616512016-01-23 23:27:58 +0000294 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
295 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
296 } else {
297 target_leds = leds;
298 }
299 target_leds ^= 7;
300
301 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000302 }
303
Stefan Reinauer915b8402011-01-28 09:00:15 +0000304 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000305 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000306 return 1;
307 }
308
Stefan Reinauer915b8402011-01-28 09:00:15 +0000309 return 0;
310}
311
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000312static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000313{
314 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000315 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000316
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000317 switch (millivolt) {
318 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000319 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000320 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000321 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000322 case 1800:
323 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000324 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000325 case 2500:
326 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000327 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000328 case 3500:
329 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000330 break;
331 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000332 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000333 return 1;
334 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000335 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
336 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000337
Nico Huber4099a8a2012-06-16 00:02:27 +0000338 if (voltage_selector == 0) {
339 /* Wait some time as the original driver does. */
340 programmer_delay(200 * 1000);
341 }
Simon Glassae616512016-01-23 23:27:58 +0000342 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000343 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000344 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
345 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000346 return 1;
347 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000348 if (voltage_selector != 0) {
349 /* Wait some time as the original driver does. */
350 programmer_delay(200 * 1000);
351 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000352 return 0;
353}
354
Nico Huber77fa67d2013-02-20 18:03:36 +0000355struct dediprog_spispeeds {
356 const char *const name;
357 const int speed;
358};
359
360static const struct dediprog_spispeeds spispeeds[] = {
361 { "24M", 0x0 },
362 { "12M", 0x2 },
363 { "8M", 0x1 },
364 { "3M", 0x3 },
365 { "2.18M", 0x4 },
366 { "1.5M", 0x5 },
367 { "750k", 0x6 },
368 { "375k", 0x7 },
369 { NULL, 0x0 },
370};
371
Nico Huber77fa67d2013-02-20 18:03:36 +0000372static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000373{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000374 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
375 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
376 return 0;
377 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000378
Simon Glass557eb4f2015-07-05 16:53:22 +0000379 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
380 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000381
Simon Glassae616512016-01-23 23:27:58 +0000382 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000383 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000384 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000385 return 1;
386 }
387 return 0;
388}
389
Simon Glassae616512016-01-23 23:27:58 +0000390static void fill_rw_cmd_payload(uint8_t *data_packet, unsigned int count, uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start) {
391 /* First 5 bytes are common in both generations. */
392 data_packet[0] = count & 0xff;
393 data_packet[1] = (count >> 8) & 0xff;
394 data_packet[2] = 0; /* RFU */
395 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
396 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
397
398 if (is_new_prot()) {
399 *value = *idx = 0;
400 data_packet[5] = 0; /* RFU */
401 data_packet[6] = (start >> 0) & 0xff;
402 data_packet[7] = (start >> 8) & 0xff;
403 data_packet[8] = (start >> 16) & 0xff;
404 data_packet[9] = (start >> 24) & 0xff;
405 } else {
406 *value = start % 0x10000;
407 *idx = start / 0x10000;
408 }
409}
410
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000411/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
412 * @start start address
413 * @len length
414 * @return 0 on success, 1 on failure
415 */
Simon Glassae616512016-01-23 23:27:58 +0000416static 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 +0000417{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000418 int err = 1;
419
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000420 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000421 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000422 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000423
Nico Huberd99a2bd2016-02-18 21:42:49 +0000424 struct dediprog_transfer_status status = { 0, 0, 0 };
425 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
426 struct libusb_transfer *transfer;
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
Simon Glassae616512016-01-23 23:27:58 +0000434 if (len == 0)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000435 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000436
437 /* Command packet size of protocols: new 10 B, old 5 B. */
438 uint8_t data_packet[is_new_prot() ? 10 : 5];
439 unsigned int value, idx;
440 fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start);
441
442 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
443 if (ret != sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000444 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000445 return 1;
446 }
447
Nico Huberd99a2bd2016-02-18 21:42:49 +0000448 /*
449 * Ring buffer of bulk transfers.
450 * Poll until at least one transfer is ready,
451 * schedule next transfers until buffer is full.
452 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000453
Nico Huberd99a2bd2016-02-18 21:42:49 +0000454 /* Allocate bulk transfers. */
455 unsigned int i;
456 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
457 transfers[i] = libusb_alloc_transfer(0);
458 if (!transfers[i]) {
459 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
460 goto err_free;
461 }
462 }
463
464 /* Now transfer requested chunks using libusb's asynchronous interface. */
465 while (!status.error && (status.queued_idx < count)) {
Nico Huberf84df9a2016-05-04 13:24:07 +0200466 while ((status.queued_idx < count) &&
467 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
468 {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000469 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
470 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
471 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
472 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
473 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
474 ret = libusb_submit_transfer(transfer);
475 if (ret < 0) {
476 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
477 status.queued_idx, libusb_error_name(ret));
478 goto err_free;
479 }
480 ++status.queued_idx;
481 }
482 if (dediprog_bulk_read_poll(&status, 0))
483 goto err_free;
484 }
485 /* Wait for transfers to finish. */
486 if (dediprog_bulk_read_poll(&status, 1))
487 goto err_free;
488 /* Check if everything has been transmitted. */
489 if ((status.finished_idx < count) || status.error)
490 goto err_free;
491
492 err = 0;
493
494err_free:
495 dediprog_bulk_read_poll(&status, 1);
496 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
497 if (transfers[i]) libusb_free_transfer(transfers[i]);
498 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000499}
500
Simon Glassae616512016-01-23 23:27:58 +0000501static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000502{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000503 int ret;
504 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000505 const unsigned int chunksize = 0x200;
506 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
507 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000508
Simon Glass25e9f402015-06-28 13:31:19 +0000509 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000510
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000511 if (residue) {
512 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
513 start, residue);
514 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000515 if (ret)
516 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000517 }
518
519 /* Round down. */
520 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000521 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000522 if (ret)
523 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000524
525 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000526 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000527 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
528 start, len);
529 ret = spi_read_chunked(flash, buf + residue + bulklen,
530 start + residue + bulklen, len, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000531 if (ret)
532 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000533 }
534
Simon Glass25e9f402015-06-28 13:31:19 +0000535 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000536 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000537err:
538 dediprog_set_leds(LED_ERROR);
539 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000540}
541
Nico Hubera4b14f72012-06-19 12:06:53 +0000542/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
543 * @chunksize length of data chunks, only 256 supported by now
544 * @start start address
545 * @len length
546 * @dedi_spi_cmd dediprog specific write command for spi bus
547 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000548 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000549static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000550 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000551{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000552 /* USB transfer size must be 512, other sizes will NOT work at all.
553 * chunksize is the real data size per USB bulk transfer. The remaining
554 * space in a USB bulk transfer must be filled with 0xff padding.
555 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000556 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000557
Nico Hubera4b14f72012-06-19 12:06:53 +0000558 /*
559 * We should change this check to
560 * chunksize > 512
561 * once we know how to handle different chunk sizes.
562 */
563 if (chunksize != 256) {
564 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
565 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
566 return 1;
567 }
568
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000569 if ((start % chunksize) || (len % chunksize)) {
570 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
571 "at flashrom@flashrom.org\n", __func__, start, len);
572 return 1;
573 }
574
575 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000576 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000577 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000578
579 /* Command packet size of protocols: new 10 B, old 5 B. */
580 uint8_t data_packet[is_new_prot() ? 10 : 5];
581 unsigned int value, idx;
582 fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start);
583 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
584 if (ret != sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000585 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000586 return 1;
587 }
588
Simon Glassae616512016-01-23 23:27:58 +0000589 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000590 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000591 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000592 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000593 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000594 int transferred;
595 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
596 DEFAULT_TIMEOUT);
597 if ((ret < 0) || (transferred != 512)) {
598 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000599 return 1;
600 }
601 }
602
603 return 0;
604}
605
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000606static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000607 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000608{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000609 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000610 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000611 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
612 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000613
Simon Glass25e9f402015-06-28 13:31:19 +0000614 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000615
Nico Hubera4b14f72012-06-19 12:06:53 +0000616 if (chunksize != 256) {
617 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
618 "we don't know how dediprog\nhandles them.\n");
619 /* Write everything like it was residue. */
620 residue = len;
621 }
622
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000623 if (residue) {
624 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
625 start, residue);
626 /* No idea about the real limit. Maybe 12, maybe more. */
627 ret = spi_write_chunked(flash, buf, start, residue, 12);
628 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000629 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000630 return ret;
631 }
632 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000633
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000634 /* Round down. */
635 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000636 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000637 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000638 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000639 return ret;
640 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000641
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000642 len -= residue + bulklen;
643 if (len) {
644 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
645 start, len);
646 ret = spi_write_chunked(flash, buf + residue + bulklen,
647 start + residue + bulklen, len, 12);
648 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000649 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000650 return ret;
651 }
652 }
653
Simon Glass25e9f402015-06-28 13:31:19 +0000654 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000655 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000656}
657
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000658static 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 +0000659{
Simon Glass557eb4f2015-07-05 16:53:22 +0000660 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000661}
662
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000663static 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 +0000664{
Simon Glass557eb4f2015-07-05 16:53:22 +0000665 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000666}
667
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000668static int dediprog_spi_send_command(struct flashctx *flash,
669 unsigned int writecnt,
670 unsigned int readcnt,
671 const unsigned char *writearr,
672 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000673{
674 int ret;
675
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000676 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glassae616512016-01-23 23:27:58 +0000677 if (writecnt > flash->mst->spi.max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000678 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000679 return 1;
680 }
Simon Glassae616512016-01-23 23:27:58 +0000681 if (readcnt > flash->mst->spi.max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000682 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000683 return 1;
684 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000685
Simon Glassae616512016-01-23 23:27:58 +0000686 unsigned int idx, value;
687 /* New protocol has options and timeout combined as value while the old one used the value field for
688 * timeout and the index field for options. */
689 if (is_new_prot()) {
690 idx = 0;
691 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
692 } else {
693 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
694 value = 0;
695 }
696 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000697 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000698 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000699 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000700 return 1;
701 }
Simon Glassae616512016-01-23 23:27:58 +0000702 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000703 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000704
Stefan Tauner74367bf2016-02-20 20:53:46 +0000705 /* The specifications do state the possibility to set a timeout for transceive transactions.
706 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
707 * can predict the time needed by the previous command or so (untested). In any case, using this
708 * "feature" to set sane-looking timouts for the read below will completely trash performance with
709 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
710 *
711 * The specification also uses only 0 in its examples, so the lesson to learn here:
712 * "Never trust the description of an interface in the documentation but use the example code and pray."
Simon Glassae616512016-01-23 23:27:58 +0000713 const uint8_t read_timeout = 10 + readcnt/512;
714 if (is_new_prot()) {
715 idx = 0;
716 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
717 } else {
718 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
719 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
720 }
721 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Stefan Tauner74367bf2016-02-20 20:53:46 +0000722 */
723 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000724 if (ret != readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000725 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 +0000726 return 1;
727 }
728 return 0;
729}
730
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000731static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000732{
733 int ret;
734 char buf[0x11];
735
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000736 /* Command Receive Device String. */
Simon Glassae616512016-01-23 23:27:58 +0000737 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000738 if (ret != 0x10) {
739 msg_perr("Incomplete/failed Command Receive Device String!\n");
740 return 1;
741 }
742 buf[0x10] = '\0';
743 msg_pdbg("Found a %s\n", buf);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000744 if (memcmp(buf, "SF100", 0x5) == 0)
745 dediprog_devicetype = DEV_SF100;
746 else if (memcmp(buf, "SF600", 0x5) == 0)
747 dediprog_devicetype = DEV_SF600;
748 else {
749 msg_perr("Device not a SF100 or SF600!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000750 return 1;
751 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000752
753 int sfnum;
754 int fw[3];
755 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
756 sfnum != dediprog_devicetype) {
Simon Glassae616512016-01-23 23:27:58 +0000757 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000758 return 1;
759 }
Simon Glassae616512016-01-23 23:27:58 +0000760 /* Only these major versions were tested. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000761 if (fw[0] < 2 || fw[0] > 7) {
Simon Glassae616512016-01-23 23:27:58 +0000762 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000763 return 1;
764 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000765 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Simon Glassae616512016-01-23 23:27:58 +0000766
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000767 return 0;
768}
769
David Hendricksc05900f2016-02-18 21:42:41 +0000770/*
771 * This command presumably sets the voltage for the SF100 itself (not the
772 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
773 * (including all SF600s) do not support it.
774 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000775
David Hendricksc05900f2016-02-18 21:42:41 +0000776/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
777 * Only use dediprog_set_voltage on SF100 programmers with firmware older
778 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
779static int dediprog_set_voltage(void)
780{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000781 unsigned char buf[1] = {0};
782 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000783 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000784 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000785 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000786 return 1;
787 }
David Hendricksc05900f2016-02-18 21:42:41 +0000788 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000789 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000790 return 1;
791 }
David Hendricksc05900f2016-02-18 21:42:41 +0000792
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000793 return 0;
794}
795
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000796static int dediprog_standalone_mode(void)
797{
798 int ret;
799
800 if (dediprog_devicetype != DEV_SF600)
801 return 0;
802
803 msg_pdbg2("Disabling standalone mode.\n");
804 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
805 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000806 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000807 return 1;
808 }
809
810 return 0;
811}
812
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000813#if 0
814/* Something.
815 * Present in eng_detect_blink.log with firmware 3.1.8
816 * Always preceded by Command Receive Device String
817 */
818static int dediprog_command_b(void)
819{
820 int ret;
821 char buf[0x3];
822
Simon Glass557eb4f2015-07-05 16:53:22 +0000823 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
824 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000825 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000826 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000827 return 1;
828 }
829 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
830 (buf[2] != 0xff)) {
831 msg_perr("Unexpected response to Command B!\n");
832 return 1;
833 }
834
835 return 0;
836}
837#endif
838
Simon Glass557eb4f2015-07-05 16:53:22 +0000839static int set_target_flash(enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000840{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000841 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +0000842 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000843 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000844 return 1;
845 }
846 return 0;
847}
848
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000849#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000850/* Returns true if the button is currently pressed. */
851static bool dediprog_get_button(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000852{
Simon Glass557eb4f2015-07-05 16:53:22 +0000853 char buf[1];
854 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
855 buf, 0x1, DEFAULT_TIMEOUT);
856 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000857 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000858 return 1;
859 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000860 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000861}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000862#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000863
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000864static int parse_voltage(char *voltage)
865{
866 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000867 int i;
868 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000869
870 if (!voltage || !strlen(voltage)) {
871 msg_perr("Empty voltage= specified.\n");
872 return -1;
873 }
874 millivolt = (int)strtol(voltage, &tmp, 0);
875 voltage = tmp;
876 /* Handle "," and "." as decimal point. Everything after it is assumed
877 * to be in decimal notation.
878 */
879 if ((*voltage == '.') || (*voltage == ',')) {
880 voltage++;
881 for (i = 0; i < 3; i++) {
882 fraction *= 10;
883 /* Don't advance if the current character is invalid,
884 * but continue multiplying.
885 */
886 if ((*voltage < '0') || (*voltage > '9'))
887 continue;
888 fraction += *voltage - '0';
889 voltage++;
890 }
891 /* Throw away remaining digits. */
892 voltage += strspn(voltage, "0123456789");
893 }
894 /* The remaining string must be empty or "mV" or "V". */
895 tolower_string(voltage);
896
897 /* No unit or "V". */
898 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
899 millivolt *= 1000;
900 millivolt += fraction;
901 } else if (!strncmp(voltage, "mv", 2) ||
902 !strncmp(voltage, "milliv", 6)) {
903 /* No adjustment. fraction is discarded. */
904 } else {
905 /* Garbage at the end of the string. */
906 msg_perr("Garbage voltage= specified.\n");
907 return -1;
908 }
909 return millivolt;
910}
911
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000912static const struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000913 .type = SPI_CONTROLLER_DEDIPROG,
Simon Glassae616512016-01-23 23:27:58 +0000914 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
915 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000916 .command = dediprog_spi_send_command,
917 .multicommand = default_spi_send_multicommand,
918 .read = dediprog_spi_read,
919 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000920 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000921};
922
David Hendricks8bb20212011-06-14 01:35:36 +0000923static int dediprog_shutdown(void *data)
924{
Simon Glass557eb4f2015-07-05 16:53:22 +0000925 dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000926 dediprog_devicetype = DEV_UNKNOWN;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000927
David Hendricks8bb20212011-06-14 01:35:36 +0000928 /* URB 28. Command Set SPI Voltage to 0. */
929 if (dediprog_set_spi_voltage(0x0))
930 return 1;
931
Nico Huberd99a2bd2016-02-18 21:42:49 +0000932 if (libusb_release_interface(dediprog_handle, 0)) {
David Hendricks8bb20212011-06-14 01:35:36 +0000933 msg_perr("Could not release USB interface!\n");
934 return 1;
935 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000936 libusb_close(dediprog_handle);
937 libusb_exit(usb_ctx);
938
David Hendricks8bb20212011-06-14 01:35:36 +0000939 return 0;
940}
941
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000942int dediprog_init(void)
943{
Stefan Taunere659d2d2013-05-03 21:58:28 +0000944 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +0000945 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000946 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000947 long usedevice = 0;
Nico Huber5e5e8212016-05-04 12:27:58 +0200948 long target = FLASH_TYPE_APPLICATION_FLASH_1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000949 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000950
Nico Huber77fa67d2013-02-20 18:03:36 +0000951 spispeed = extract_programmer_param("spispeed");
952 if (spispeed) {
953 for (i = 0; spispeeds[i].name; ++i) {
954 if (!strcasecmp(spispeeds[i].name, spispeed)) {
955 spispeed_idx = i;
956 break;
957 }
958 }
959 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000960 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +0000961 free(spispeed);
962 return 1;
963 }
964 free(spispeed);
965 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000966
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000967 voltage = extract_programmer_param("voltage");
968 if (voltage) {
969 millivolt = parse_voltage(voltage);
970 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000971 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000972 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000973 msg_pinfo("Setting voltage to %i mV\n", millivolt);
974 }
975
Nathan Laredo21541a62012-12-24 22:07:36 +0000976 device = extract_programmer_param("device");
977 if (device) {
978 char *dev_suffix;
979 errno = 0;
980 usedevice = strtol(device, &dev_suffix, 10);
981 if (errno != 0 || device == dev_suffix) {
982 msg_perr("Error: Could not convert 'device'.\n");
983 free(device);
984 return 1;
985 }
986 if (usedevice < 0 || usedevice > UINT_MAX) {
987 msg_perr("Error: Value for 'device' is out of range.\n");
988 free(device);
989 return 1;
990 }
991 if (strlen(dev_suffix) > 0) {
992 msg_perr("Error: Garbage following 'device' value.\n");
993 free(device);
994 return 1;
995 }
996 msg_pinfo("Using device %li.\n", usedevice);
997 }
998 free(device);
999
Stefan Taunere659d2d2013-05-03 21:58:28 +00001000 target_str = extract_programmer_param("target");
1001 if (target_str) {
1002 char *target_suffix;
1003 errno = 0;
1004 target = strtol(target_str, &target_suffix, 10);
1005 if (errno != 0 || target_str == target_suffix) {
1006 msg_perr("Error: Could not convert 'target'.\n");
1007 free(target_str);
1008 return 1;
1009 }
1010 if (target < 1 || target > 2) {
1011 msg_perr("Error: Value for 'target' is out of range.\n");
1012 free(target_str);
1013 return 1;
1014 }
1015 if (strlen(target_suffix) > 0) {
1016 msg_perr("Error: Garbage following 'target' value.\n");
1017 free(target_str);
1018 return 1;
1019 }
Nico Huber5e5e8212016-05-04 12:27:58 +02001020 switch (target) {
1021 case 1:
1022 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1023 target = FLASH_TYPE_APPLICATION_FLASH_1;
1024 break;
1025 case 2:
1026 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1027 target = FLASH_TYPE_APPLICATION_FLASH_2;
1028 break;
1029 default:
1030 break;
1031 }
Stefan Taunere659d2d2013-05-03 21:58:28 +00001032 }
1033 free(target_str);
1034
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001035 /* Here comes the USB stuff. */
Nico Huberd99a2bd2016-02-18 21:42:49 +00001036 libusb_init(&usb_ctx);
1037 if (!usb_ctx) {
1038 msg_perr("Could not initialize libusb!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001039 return 1;
1040 }
Stefan Taunerfdec7472016-02-22 08:59:27 +00001041
1042 const uint16_t vid = devs_dediprog[0].vendor_id;
1043 const uint16_t pid = devs_dediprog[0].device_id;
1044 dediprog_handle = get_device_by_vid_pid_number(vid, pid, (unsigned int) usedevice);
David Woodhoused2a7e872013-07-30 09:34:44 +00001045 if (!dediprog_handle) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001046 msg_perr("Could not find a Dediprog programmer on USB.\n");
1047 libusb_exit(usb_ctx);
David Woodhoused2a7e872013-07-30 09:34:44 +00001048 return 1;
1049 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001050 ret = libusb_set_configuration(dediprog_handle, 1);
1051 if (ret != 0) {
1052 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1053 libusb_close(dediprog_handle);
1054 libusb_exit(usb_ctx);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001055 return 1;
1056 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001057 ret = libusb_claim_interface(dediprog_handle, 0);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001058 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001059 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1060 libusb_close(dediprog_handle);
1061 libusb_exit(usb_ctx);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001062 return 1;
1063 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001064
David Hendricks8bb20212011-06-14 01:35:36 +00001065 if (register_shutdown(dediprog_shutdown, NULL))
1066 return 1;
1067
David Hendricksc05900f2016-02-18 21:42:41 +00001068 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1069 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
1070 if (dediprog_check_devicestring()) {
1071 if (dediprog_set_voltage())
1072 return 1;
1073 if (dediprog_check_devicestring())
1074 return 1;
1075 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001076
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001077 /* SF100 only has 1 endpoint for in/out, SF600 uses two separate endpoints instead. */
1078 dediprog_in_endpoint = 2;
1079 if (dediprog_devicetype == DEV_SF100)
1080 dediprog_out_endpoint = 2;
1081 else
1082 dediprog_out_endpoint = 1;
1083
Simon Glass25e9f402015-06-28 13:31:19 +00001084 /* Set all possible LEDs as soon as possible to indicate activity.
1085 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
Simon Glassae616512016-01-23 23:27:58 +00001086 * dediprog_check_devicestring() has queried the device and set dediprog_firmwareversion. */
Simon Glass25e9f402015-06-28 13:31:19 +00001087 dediprog_set_leds(LED_ALL);
1088
Simon Glass557eb4f2015-07-05 16:53:22 +00001089 /* Select target/socket, frequency and VCC. */
Nico Huber5e5e8212016-05-04 12:27:58 +02001090 if (set_target_flash(target) ||
Simon Glass557eb4f2015-07-05 16:53:22 +00001091 dediprog_set_spi_speed(spispeed_idx) ||
1092 dediprog_set_spi_voltage(millivolt)) {
Simon Glass25e9f402015-06-28 13:31:19 +00001093 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001094 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001095 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001096
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001097 if (dediprog_standalone_mode())
1098 return 1;
1099
Simon Glassae616512016-01-23 23:27:58 +00001100 if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE))
1101 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001102
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001103 return 0;
1104}