blob: 454a72d9b63be2fbeb6899247a17fa2ccf4cabbc [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
Stefan Tauner5c316f92015-02-08 21:57:52 +000024#include <stdlib.h>
Mathias Krausedb7afc52010-11-09 23:30:43 +000025#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000026#include <string.h>
Stefan Taunere34e3e82013-01-01 00:06:51 +000027#include <limits.h>
Nathan Laredo21541a62012-12-24 22:07:36 +000028#include <errno.h>
Nico Huberd99a2bd2016-02-18 21:42:49 +000029#include <libusb.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000030#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000031#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000032#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000033#include "spi.h"
34
Nico Huberd99a2bd2016-02-18 21:42:49 +000035/* LIBUSB_CALL ensures the right calling conventions on libusb callbacks.
36 * However, the macro is not defined everywhere. m(
37 */
38#ifndef LIBUSB_CALL
39#define LIBUSB_CALL
40#endif
41
Stefan Reinauer915b8402011-01-28 09:00:15 +000042#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000043#define DEFAULT_TIMEOUT 3000
Nico Huberd99a2bd2016-02-18 21:42:49 +000044#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
45#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
46#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
47#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
48#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
49struct libusb_context *usb_ctx;
50static libusb_device_handle *dediprog_handle;
David Woodhouse7f3b89f2016-02-18 21:42:15 +000051static int dediprog_in_endpoint;
52static int dediprog_out_endpoint;
53
54enum dediprog_devtype {
55 DEV_UNKNOWN = 0,
56 DEV_SF100 = 100,
57 DEV_SF600 = 600,
58};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000059
Simon Glass25e9f402015-06-28 13:31:19 +000060enum dediprog_leds {
61 LED_INVALID = -1,
62 LED_NONE = 0,
63 LED_PASS = 1 << 0,
64 LED_BUSY = 1 << 1,
65 LED_ERROR = 1 << 2,
66 LED_ALL = 7,
67};
68
Simon Glass557eb4f2015-07-05 16:53:22 +000069/* IO bits for CMD_SET_IO_LED message */
70enum dediprog_ios {
71 IO1 = 1 << 0,
72 IO2 = 1 << 1,
73 IO3 = 1 << 2,
74 IO4 = 1 << 3,
75};
76
77enum dediprog_cmds {
78 CMD_TRANSCEIVE = 0x01,
79 CMD_POLL_STATUS_REG = 0x02,
80 CMD_SET_VPP = 0x03,
81 CMD_SET_TARGET = 0x04,
82 CMD_READ_EEPROM = 0x05,
83 CMD_WRITE_EEPROM = 0x06,
84 CMD_SET_IO_LED = 0x07,
85 CMD_READ_PROG_INFO = 0x08,
86 CMD_SET_VCC = 0x09,
87 CMD_SET_STANDALONE = 0x0A,
David Hendricksc05900f2016-02-18 21:42:41 +000088 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
Simon Glass557eb4f2015-07-05 16:53:22 +000089 CMD_GET_BUTTON = 0x11,
90 CMD_GET_UID = 0x12,
91 CMD_SET_CS = 0x14,
92 CMD_IO_MODE = 0x15,
93 CMD_FW_UPDATE = 0x1A,
94 CMD_FPGA_UPDATE = 0x1B,
95 CMD_READ_FPGA_VERSION = 0x1C,
96 CMD_SET_HOLD = 0x1D,
97 CMD_READ = 0x20,
98 CMD_WRITE = 0x30,
99 CMD_WRITE_AT45DB = 0x31,
100 CMD_NAND_WRITE = 0x32,
101 CMD_NAND_READ = 0x33,
102 CMD_SET_SPI_CLK = 0x61,
103 CMD_CHECK_SOCKET = 0x62,
104 CMD_DOWNLOAD_PRJ = 0x63,
105 CMD_READ_PRJ_NAME = 0x64,
106 // New protocol/firmware only
107 CMD_CHECK_SDCARD = 0x65,
108 CMD_READ_PRJ = 0x66,
109};
110
111enum dediprog_target {
112 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
113 FLASH_TYPE_FLASH_CARD,
114 FLASH_TYPE_APPLICATION_FLASH_2,
115 FLASH_TYPE_SOCKET,
116};
117
118enum dediprog_readmode {
119 READ_MODE_STD = 1,
120 READ_MODE_FAST = 2,
121 READ_MODE_ATMEL45 = 3,
122 READ_MODE_4B_ADDR_FAST = 4,
123 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
124};
125
126enum dediprog_writemode {
127 WRITE_MODE_PAGE_PGM = 1,
128 WRITE_MODE_PAGE_WRITE = 2,
129 WRITE_MODE_1B_AAI = 3,
130 WRITE_MODE_2B_AAI = 4,
131 WRITE_MODE_128B_PAGE = 5,
132 WRITE_MODE_PAGE_AT26DF041 = 6,
133 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000134 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000135 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000136 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000137 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
138 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
139};
140
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000141enum dediprog_standalone_mode {
142 ENTER_STANDALONE_MODE = 0,
143 LEAVE_STANDALONE_MODE = 1,
144};
145
Simon Glass557eb4f2015-07-05 16:53:22 +0000146
147static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000148enum dediprog_devtype dediprog_devicetype = DEV_UNKNOWN;
Simon Glass557eb4f2015-07-05 16:53:22 +0000149
Nico Huberd99a2bd2016-02-18 21:42:49 +0000150#if defined(LIBUSB_MAJOR) && defined(LIBUSB_MINOR) && defined(LIBUSB_MICRO) && \
151 LIBUSB_MAJOR <= 1 && LIBUSB_MINOR == 0 && LIBUSB_MICRO < 9
152/* Quick and dirty replacement for missing libusb_error_name in libusb < 1.0.9 */
153const char * LIBUSB_CALL libusb_error_name(int error_code)
154{
155 if (error_code >= INT16_MIN && error_code <= INT16_MAX) {
156 /* 18 chars for text, rest for number (16 b should be enough), sign, nullbyte. */
157 static char my_libusb_error[18 + 5 + 2];
158 sprintf(my_libusb_error, "libusb error code %i", error_code);
159 return my_libusb_error;
160 } else {
161 return "UNKNOWN";
162 }
163}
164#endif
165
Simon Glassae616512016-01-23 23:27:58 +0000166/* Returns true if firmware (and thus hardware) supports the "new" protocol */
167static bool is_new_prot(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000168{
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000169 switch (dediprog_devicetype) {
170 case DEV_SF100:
171 return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
172 case DEV_SF600:
173 return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0);
174 default:
175 return 0;
176 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000177}
Simon Glassae616512016-01-23 23:27:58 +0000178
Nico Huberd99a2bd2016-02-18 21:42:49 +0000179struct dediprog_transfer_status {
180 int error; /* OK if 0, ERROR else */
181 unsigned int queued_idx;
182 unsigned int finished_idx;
183};
184
185static void LIBUSB_CALL dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
186{
187 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
188 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
189 status->error = 1;
190 msg_perr("SPI bulk read failed!\n");
191 }
192 ++status->finished_idx;
193}
194
195static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
196{
197 if (status->finished_idx >= status->queued_idx)
198 return 0;
199
200 do {
201 struct timeval timeout = { 10, 0 };
202 const int ret = libusb_handle_events_timeout(usb_ctx, &timeout);
203 if (ret < 0) {
204 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
205 return 1;
206 }
207 } while (finish && (status->finished_idx < status->queued_idx));
208 return 0;
209}
210
Simon Glassae616512016-01-23 23:27:58 +0000211static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
212{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000213 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
214 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000215}
216
217static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
218{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000219 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
220 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
Simon Glassae616512016-01-23 23:27:58 +0000221}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000222
Nico Huberd99a2bd2016-02-18 21:42:49 +0000223
224/* Might be useful for other USB devices as well. static for now.
225 * num parameter allows user to specify one device of multiple installed */
226static 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 +0000227{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000228 struct libusb_device **list;
229 ssize_t count = libusb_get_device_list(usb_ctx, &list);
230 if (count < 0) {
231 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
232 return NULL;
233 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000234
Nico Huberd99a2bd2016-02-18 21:42:49 +0000235 struct libusb_device_handle *handle = NULL;
236 ssize_t i = 0;
237 for (i = 0; i < count; i++) {
238 struct libusb_device *dev = list[i];
239 struct libusb_device_descriptor desc;
240 int err = libusb_get_device_descriptor(dev, &desc);
241 if (err != 0) {
242 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
243 libusb_free_device_list(list, 1);
244 return NULL;
245 }
246 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
247 msg_pdbg("Found USB device %04"PRIx16":%04"PRIx16" at address %d-%d.\n",
248 desc.idVendor, desc.idProduct,
249 libusb_get_bus_number(dev), libusb_get_device_address(dev));
250 if (num == 0) {
251 err = libusb_open(dev, &handle);
252 if (err != 0) {
253 msg_perr("Opening the USB device failed (%s)!\n",
254 libusb_error_name(err));
255 libusb_free_device_list(list, 1);
256 return NULL;
257 }
258 break;
Nathan Laredo21541a62012-12-24 22:07:36 +0000259 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000260 num--;
261 }
262 }
263 libusb_free_device_list(list, 1);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000264
Nico Huberd99a2bd2016-02-18 21:42:49 +0000265 return handle;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000266}
267
Simon Glass25e9f402015-06-28 13:31:19 +0000268/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000269static int dediprog_set_leds(int leds)
270{
Simon Glass25e9f402015-06-28 13:31:19 +0000271 if (leds < LED_NONE || leds > LED_ALL)
272 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000273
Simon Glassae616512016-01-23 23:27:58 +0000274 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
275 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000276 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000277 * bit 0 == 0: red light is on.
278 *
279 * Additionally, the command structure has changed with the "new" protocol.
280 *
281 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000282 */
Simon Glassae616512016-01-23 23:27:58 +0000283 int target_leds, ret;
284 if (is_new_prot()) {
285 target_leds = (leds ^ 7) << 8;
286 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000287 } else {
Simon Glassae616512016-01-23 23:27:58 +0000288 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
289 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
290 } else {
291 target_leds = leds;
292 }
293 target_leds ^= 7;
294
295 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000296 }
297
Stefan Reinauer915b8402011-01-28 09:00:15 +0000298 if (ret != 0x0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000299 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
Stefan Reinauer915b8402011-01-28 09:00:15 +0000300 return 1;
301 }
302
Stefan Reinauer915b8402011-01-28 09:00:15 +0000303 return 0;
304}
305
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000306static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000307{
308 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000309 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000310
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000311 switch (millivolt) {
312 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000313 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000314 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000315 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000316 case 1800:
317 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000318 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000319 case 2500:
320 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000321 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000322 case 3500:
323 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000324 break;
325 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000326 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000327 return 1;
328 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000329 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
330 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000331
Nico Huber4099a8a2012-06-16 00:02:27 +0000332 if (voltage_selector == 0) {
333 /* Wait some time as the original driver does. */
334 programmer_delay(200 * 1000);
335 }
Simon Glassae616512016-01-23 23:27:58 +0000336 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000337 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000338 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
339 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000340 return 1;
341 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000342 if (voltage_selector != 0) {
343 /* Wait some time as the original driver does. */
344 programmer_delay(200 * 1000);
345 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000346 return 0;
347}
348
Nico Huber77fa67d2013-02-20 18:03:36 +0000349struct dediprog_spispeeds {
350 const char *const name;
351 const int speed;
352};
353
354static const struct dediprog_spispeeds spispeeds[] = {
355 { "24M", 0x0 },
356 { "12M", 0x2 },
357 { "8M", 0x1 },
358 { "3M", 0x3 },
359 { "2.18M", 0x4 },
360 { "1.5M", 0x5 },
361 { "750k", 0x6 },
362 { "375k", 0x7 },
363 { NULL, 0x0 },
364};
365
Nico Huber77fa67d2013-02-20 18:03:36 +0000366static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000367{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000368 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
369 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
370 return 0;
371 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000372
Simon Glass557eb4f2015-07-05 16:53:22 +0000373 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
374 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000375
Simon Glassae616512016-01-23 23:27:58 +0000376 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000377 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000378 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000379 return 1;
380 }
381 return 0;
382}
383
Simon Glassae616512016-01-23 23:27:58 +0000384static 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) {
385 /* First 5 bytes are common in both generations. */
386 data_packet[0] = count & 0xff;
387 data_packet[1] = (count >> 8) & 0xff;
388 data_packet[2] = 0; /* RFU */
389 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
390 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
391
392 if (is_new_prot()) {
393 *value = *idx = 0;
394 data_packet[5] = 0; /* RFU */
395 data_packet[6] = (start >> 0) & 0xff;
396 data_packet[7] = (start >> 8) & 0xff;
397 data_packet[8] = (start >> 16) & 0xff;
398 data_packet[9] = (start >> 24) & 0xff;
399 } else {
400 *value = start % 0x10000;
401 *idx = start / 0x10000;
402 }
403}
404
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000405/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
406 * @start start address
407 * @len length
408 * @return 0 on success, 1 on failure
409 */
Simon Glassae616512016-01-23 23:27:58 +0000410static 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 +0000411{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000412 int err = 1;
413
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000414 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000415 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000416 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000417
Nico Huberd99a2bd2016-02-18 21:42:49 +0000418 struct dediprog_transfer_status status = { 0, 0, 0 };
419 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
420 struct libusb_transfer *transfer;
421
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000422 if ((start % chunksize) || (len % chunksize)) {
Simon Glassae616512016-01-23 23:27:58 +0000423 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
424 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000425 return 1;
426 }
427
Simon Glassae616512016-01-23 23:27:58 +0000428 if (len == 0)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000429 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000430
431 /* Command packet size of protocols: new 10 B, old 5 B. */
432 uint8_t data_packet[is_new_prot() ? 10 : 5];
433 unsigned int value, idx;
434 fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start);
435
436 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
437 if (ret != sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000438 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000439 return 1;
440 }
441
Nico Huberd99a2bd2016-02-18 21:42:49 +0000442 /*
443 * Ring buffer of bulk transfers.
444 * Poll until at least one transfer is ready,
445 * schedule next transfers until buffer is full.
446 */
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000447
Nico Huberd99a2bd2016-02-18 21:42:49 +0000448 /* Allocate bulk transfers. */
449 unsigned int i;
450 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
451 transfers[i] = libusb_alloc_transfer(0);
452 if (!transfers[i]) {
453 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
454 goto err_free;
455 }
456 }
457
458 /* Now transfer requested chunks using libusb's asynchronous interface. */
459 while (!status.error && (status.queued_idx < count)) {
460 while ((status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS) {
461 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
462 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
463 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
464 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
465 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
466 ret = libusb_submit_transfer(transfer);
467 if (ret < 0) {
468 msg_perr("Submitting SPI bulk read %i failed: %s!\n",
469 status.queued_idx, libusb_error_name(ret));
470 goto err_free;
471 }
472 ++status.queued_idx;
473 }
474 if (dediprog_bulk_read_poll(&status, 0))
475 goto err_free;
476 }
477 /* Wait for transfers to finish. */
478 if (dediprog_bulk_read_poll(&status, 1))
479 goto err_free;
480 /* Check if everything has been transmitted. */
481 if ((status.finished_idx < count) || status.error)
482 goto err_free;
483
484 err = 0;
485
486err_free:
487 dediprog_bulk_read_poll(&status, 1);
488 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
489 if (transfers[i]) libusb_free_transfer(transfers[i]);
490 return err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000491}
492
Simon Glassae616512016-01-23 23:27:58 +0000493static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000494{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000495 int ret;
496 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000497 const unsigned int chunksize = 0x200;
498 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
499 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000500
Simon Glass25e9f402015-06-28 13:31:19 +0000501 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000502
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000503 if (residue) {
504 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
505 start, residue);
506 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000507 if (ret)
508 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000509 }
510
511 /* Round down. */
512 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000513 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000514 if (ret)
515 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000516
517 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000518 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000519 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
520 start, len);
521 ret = spi_read_chunked(flash, buf + residue + bulklen,
522 start + residue + bulklen, len, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000523 if (ret)
524 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000525 }
526
Simon Glass25e9f402015-06-28 13:31:19 +0000527 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000528 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000529err:
530 dediprog_set_leds(LED_ERROR);
531 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000532}
533
Nico Hubera4b14f72012-06-19 12:06:53 +0000534/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
535 * @chunksize length of data chunks, only 256 supported by now
536 * @start start address
537 * @len length
538 * @dedi_spi_cmd dediprog specific write command for spi bus
539 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000540 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000541static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000542 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000543{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000544 /* USB transfer size must be 512, other sizes will NOT work at all.
545 * chunksize is the real data size per USB bulk transfer. The remaining
546 * space in a USB bulk transfer must be filled with 0xff padding.
547 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000548 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000549
Nico Hubera4b14f72012-06-19 12:06:53 +0000550 /*
551 * We should change this check to
552 * chunksize > 512
553 * once we know how to handle different chunk sizes.
554 */
555 if (chunksize != 256) {
556 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
557 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
558 return 1;
559 }
560
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000561 if ((start % chunksize) || (len % chunksize)) {
562 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
563 "at flashrom@flashrom.org\n", __func__, start, len);
564 return 1;
565 }
566
567 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000568 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000569 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000570
571 /* Command packet size of protocols: new 10 B, old 5 B. */
572 uint8_t data_packet[is_new_prot() ? 10 : 5];
573 unsigned int value, idx;
574 fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start);
575 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
576 if (ret != sizeof(data_packet)) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000577 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000578 return 1;
579 }
580
Simon Glassae616512016-01-23 23:27:58 +0000581 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000582 for (i = 0; i < count; i++) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000583 unsigned char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000584 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000585 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
Nico Huberd99a2bd2016-02-18 21:42:49 +0000586 int transferred;
587 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
588 DEFAULT_TIMEOUT);
589 if ((ret < 0) || (transferred != 512)) {
590 msg_perr("SPI bulk write failed, expected %i, got %s!\n", 512, libusb_error_name(ret));
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000591 return 1;
592 }
593 }
594
595 return 0;
596}
597
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000598static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000599 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000600{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000601 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000602 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000603 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
604 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000605
Simon Glass25e9f402015-06-28 13:31:19 +0000606 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000607
Nico Hubera4b14f72012-06-19 12:06:53 +0000608 if (chunksize != 256) {
609 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
610 "we don't know how dediprog\nhandles them.\n");
611 /* Write everything like it was residue. */
612 residue = len;
613 }
614
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000615 if (residue) {
616 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
617 start, residue);
618 /* No idea about the real limit. Maybe 12, maybe more. */
619 ret = spi_write_chunked(flash, buf, start, residue, 12);
620 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000621 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000622 return ret;
623 }
624 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000625
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000626 /* Round down. */
627 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000628 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000629 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000630 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000631 return ret;
632 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000633
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000634 len -= residue + bulklen;
635 if (len) {
636 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
637 start, len);
638 ret = spi_write_chunked(flash, buf + residue + bulklen,
639 start + residue + bulklen, len, 12);
640 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000641 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000642 return ret;
643 }
644 }
645
Simon Glass25e9f402015-06-28 13:31:19 +0000646 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000647 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000648}
649
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000650static 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 +0000651{
Simon Glass557eb4f2015-07-05 16:53:22 +0000652 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000653}
654
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000655static 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 +0000656{
Simon Glass557eb4f2015-07-05 16:53:22 +0000657 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000658}
659
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000660static int dediprog_spi_send_command(struct flashctx *flash,
661 unsigned int writecnt,
662 unsigned int readcnt,
663 const unsigned char *writearr,
664 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000665{
666 int ret;
667
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000668 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glassae616512016-01-23 23:27:58 +0000669 if (writecnt > flash->mst->spi.max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000670 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000671 return 1;
672 }
Simon Glassae616512016-01-23 23:27:58 +0000673 if (readcnt > flash->mst->spi.max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000674 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000675 return 1;
676 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000677
Simon Glassae616512016-01-23 23:27:58 +0000678 unsigned int idx, value;
679 /* New protocol has options and timeout combined as value while the old one used the value field for
680 * timeout and the index field for options. */
681 if (is_new_prot()) {
682 idx = 0;
683 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
684 } else {
685 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
686 value = 0;
687 }
688 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000689 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000690 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
Nico Huberd99a2bd2016-02-18 21:42:49 +0000691 writecnt, ret, libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000692 return 1;
693 }
Simon Glassae616512016-01-23 23:27:58 +0000694 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000695 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000696
Simon Glassae616512016-01-23 23:27:58 +0000697 const uint8_t read_timeout = 10 + readcnt/512;
698 if (is_new_prot()) {
699 idx = 0;
700 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
701 } else {
702 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
703 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
704 }
705 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000706 if (ret != readcnt) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000707 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 +0000708 return 1;
709 }
710 return 0;
711}
712
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000713static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000714{
715 int ret;
716 char buf[0x11];
717
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000718 /* Command Receive Device String. */
Simon Glassae616512016-01-23 23:27:58 +0000719 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000720 if (ret != 0x10) {
721 msg_perr("Incomplete/failed Command Receive Device String!\n");
722 return 1;
723 }
724 buf[0x10] = '\0';
725 msg_pdbg("Found a %s\n", buf);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000726 if (memcmp(buf, "SF100", 0x5) == 0)
727 dediprog_devicetype = DEV_SF100;
728 else if (memcmp(buf, "SF600", 0x5) == 0)
729 dediprog_devicetype = DEV_SF600;
730 else {
731 msg_perr("Device not a SF100 or SF600!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000732 return 1;
733 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000734
735 int sfnum;
736 int fw[3];
737 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
738 sfnum != dediprog_devicetype) {
Simon Glassae616512016-01-23 23:27:58 +0000739 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000740 return 1;
741 }
Simon Glassae616512016-01-23 23:27:58 +0000742 /* Only these major versions were tested. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000743 if (fw[0] < 2 || fw[0] > 7) {
Simon Glassae616512016-01-23 23:27:58 +0000744 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000745 return 1;
746 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000747 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Simon Glassae616512016-01-23 23:27:58 +0000748
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000749 return 0;
750}
751
David Hendricksc05900f2016-02-18 21:42:41 +0000752/*
753 * This command presumably sets the voltage for the SF100 itself (not the
754 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
755 * (including all SF600s) do not support it.
756 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000757
David Hendricksc05900f2016-02-18 21:42:41 +0000758/* This command presumably sets the voltage for the SF100 itself (not the SPI flash).
759 * Only use dediprog_set_voltage on SF100 programmers with firmware older
760 * than V6.0.0. Newer programmers (including all SF600s) do not support it. */
761static int dediprog_set_voltage(void)
762{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000763 unsigned char buf[1] = {0};
764 int ret = libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, CMD_SET_VOLTAGE, 0x0, 0x0,
Simon Glass557eb4f2015-07-05 16:53:22 +0000765 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000766 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000767 msg_perr("Command Set Voltage failed (%s)!\n", libusb_error_name(ret));
Mathias Krausedb7afc52010-11-09 23:30:43 +0000768 return 1;
769 }
David Hendricksc05900f2016-02-18 21:42:41 +0000770 if ((ret != 1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000771 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000772 return 1;
773 }
David Hendricksc05900f2016-02-18 21:42:41 +0000774
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000775 return 0;
776}
777
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000778static int dediprog_standalone_mode(void)
779{
780 int ret;
781
782 if (dediprog_devicetype != DEV_SF600)
783 return 0;
784
785 msg_pdbg2("Disabling standalone mode.\n");
786 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
787 if (ret) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000788 msg_perr("Failed to disable standalone mode: %s\n", libusb_error_name(ret));
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000789 return 1;
790 }
791
792 return 0;
793}
794
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000795#if 0
796/* Something.
797 * Present in eng_detect_blink.log with firmware 3.1.8
798 * Always preceded by Command Receive Device String
799 */
800static int dediprog_command_b(void)
801{
802 int ret;
803 char buf[0x3];
804
Simon Glass557eb4f2015-07-05 16:53:22 +0000805 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
806 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000807 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000808 msg_perr("Command B failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000809 return 1;
810 }
811 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
812 (buf[2] != 0xff)) {
813 msg_perr("Unexpected response to Command B!\n");
814 return 1;
815 }
816
817 return 0;
818}
819#endif
820
Simon Glass557eb4f2015-07-05 16:53:22 +0000821static int set_target_flash(enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000822{
Nico Huberd99a2bd2016-02-18 21:42:49 +0000823 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
Simon Glass557eb4f2015-07-05 16:53:22 +0000824 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000825 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000826 return 1;
827 }
828 return 0;
829}
830
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000831#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000832/* Returns true if the button is currently pressed. */
833static bool dediprog_get_button(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000834{
Simon Glass557eb4f2015-07-05 16:53:22 +0000835 char buf[1];
836 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
837 buf, 0x1, DEFAULT_TIMEOUT);
838 if (ret != 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +0000839 msg_perr("Could not get button state (%s)!\n", libusb_error_name(ret));
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000840 return 1;
841 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000842 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000843}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000844#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000845
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000846static int parse_voltage(char *voltage)
847{
848 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000849 int i;
850 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000851
852 if (!voltage || !strlen(voltage)) {
853 msg_perr("Empty voltage= specified.\n");
854 return -1;
855 }
856 millivolt = (int)strtol(voltage, &tmp, 0);
857 voltage = tmp;
858 /* Handle "," and "." as decimal point. Everything after it is assumed
859 * to be in decimal notation.
860 */
861 if ((*voltage == '.') || (*voltage == ',')) {
862 voltage++;
863 for (i = 0; i < 3; i++) {
864 fraction *= 10;
865 /* Don't advance if the current character is invalid,
866 * but continue multiplying.
867 */
868 if ((*voltage < '0') || (*voltage > '9'))
869 continue;
870 fraction += *voltage - '0';
871 voltage++;
872 }
873 /* Throw away remaining digits. */
874 voltage += strspn(voltage, "0123456789");
875 }
876 /* The remaining string must be empty or "mV" or "V". */
877 tolower_string(voltage);
878
879 /* No unit or "V". */
880 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
881 millivolt *= 1000;
882 millivolt += fraction;
883 } else if (!strncmp(voltage, "mv", 2) ||
884 !strncmp(voltage, "milliv", 6)) {
885 /* No adjustment. fraction is discarded. */
886 } else {
887 /* Garbage at the end of the string. */
888 msg_perr("Garbage voltage= specified.\n");
889 return -1;
890 }
891 return millivolt;
892}
893
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000894static const struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000895 .type = SPI_CONTROLLER_DEDIPROG,
Simon Glassae616512016-01-23 23:27:58 +0000896 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
897 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000898 .command = dediprog_spi_send_command,
899 .multicommand = default_spi_send_multicommand,
900 .read = dediprog_spi_read,
901 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000902 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000903};
904
David Hendricks8bb20212011-06-14 01:35:36 +0000905static int dediprog_shutdown(void *data)
906{
Simon Glass557eb4f2015-07-05 16:53:22 +0000907 dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000908 dediprog_devicetype = DEV_UNKNOWN;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000909
David Hendricks8bb20212011-06-14 01:35:36 +0000910 /* URB 28. Command Set SPI Voltage to 0. */
911 if (dediprog_set_spi_voltage(0x0))
912 return 1;
913
Nico Huberd99a2bd2016-02-18 21:42:49 +0000914 if (libusb_release_interface(dediprog_handle, 0)) {
David Hendricks8bb20212011-06-14 01:35:36 +0000915 msg_perr("Could not release USB interface!\n");
916 return 1;
917 }
Nico Huberd99a2bd2016-02-18 21:42:49 +0000918 libusb_close(dediprog_handle);
919 libusb_exit(usb_ctx);
920
David Hendricks8bb20212011-06-14 01:35:36 +0000921 return 0;
922}
923
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000924int dediprog_init(void)
925{
Stefan Taunere659d2d2013-05-03 21:58:28 +0000926 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +0000927 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000928 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000929 long usedevice = 0;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000930 long target = 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000931 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000932
Nico Huber77fa67d2013-02-20 18:03:36 +0000933 spispeed = extract_programmer_param("spispeed");
934 if (spispeed) {
935 for (i = 0; spispeeds[i].name; ++i) {
936 if (!strcasecmp(spispeeds[i].name, spispeed)) {
937 spispeed_idx = i;
938 break;
939 }
940 }
941 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000942 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +0000943 free(spispeed);
944 return 1;
945 }
946 free(spispeed);
947 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000948
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000949 voltage = extract_programmer_param("voltage");
950 if (voltage) {
951 millivolt = parse_voltage(voltage);
952 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000953 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000954 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000955 msg_pinfo("Setting voltage to %i mV\n", millivolt);
956 }
957
Nathan Laredo21541a62012-12-24 22:07:36 +0000958 device = extract_programmer_param("device");
959 if (device) {
960 char *dev_suffix;
961 errno = 0;
962 usedevice = strtol(device, &dev_suffix, 10);
963 if (errno != 0 || device == dev_suffix) {
964 msg_perr("Error: Could not convert 'device'.\n");
965 free(device);
966 return 1;
967 }
968 if (usedevice < 0 || usedevice > UINT_MAX) {
969 msg_perr("Error: Value for 'device' is out of range.\n");
970 free(device);
971 return 1;
972 }
973 if (strlen(dev_suffix) > 0) {
974 msg_perr("Error: Garbage following 'device' value.\n");
975 free(device);
976 return 1;
977 }
978 msg_pinfo("Using device %li.\n", usedevice);
979 }
980 free(device);
981
Stefan Taunere659d2d2013-05-03 21:58:28 +0000982 target_str = extract_programmer_param("target");
983 if (target_str) {
984 char *target_suffix;
985 errno = 0;
986 target = strtol(target_str, &target_suffix, 10);
987 if (errno != 0 || target_str == target_suffix) {
988 msg_perr("Error: Could not convert 'target'.\n");
989 free(target_str);
990 return 1;
991 }
992 if (target < 1 || target > 2) {
993 msg_perr("Error: Value for 'target' is out of range.\n");
994 free(target_str);
995 return 1;
996 }
997 if (strlen(target_suffix) > 0) {
998 msg_perr("Error: Garbage following 'target' value.\n");
999 free(target_str);
1000 return 1;
1001 }
1002 msg_pinfo("Using target %li.\n", target);
1003 }
1004 free(target_str);
1005
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001006 /* Here comes the USB stuff. */
Nico Huberd99a2bd2016-02-18 21:42:49 +00001007 libusb_init(&usb_ctx);
1008 if (!usb_ctx) {
1009 msg_perr("Could not initialize libusb!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001010 return 1;
1011 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001012 dediprog_handle = get_device_by_vid_pid_number(0x0483, 0xdada, (unsigned int) usedevice);
David Woodhoused2a7e872013-07-30 09:34:44 +00001013 if (!dediprog_handle) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001014 msg_perr("Could not find a Dediprog programmer on USB.\n");
1015 libusb_exit(usb_ctx);
David Woodhoused2a7e872013-07-30 09:34:44 +00001016 return 1;
1017 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001018 ret = libusb_set_configuration(dediprog_handle, 1);
1019 if (ret != 0) {
1020 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1021 libusb_close(dediprog_handle);
1022 libusb_exit(usb_ctx);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001023 return 1;
1024 }
Nico Huberd99a2bd2016-02-18 21:42:49 +00001025 ret = libusb_claim_interface(dediprog_handle, 0);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001026 if (ret < 0) {
Nico Huberd99a2bd2016-02-18 21:42:49 +00001027 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1028 libusb_close(dediprog_handle);
1029 libusb_exit(usb_ctx);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +00001030 return 1;
1031 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001032
David Hendricks8bb20212011-06-14 01:35:36 +00001033 if (register_shutdown(dediprog_shutdown, NULL))
1034 return 1;
1035
David Hendricksc05900f2016-02-18 21:42:41 +00001036 /* Try reading the devicestring. If that fails and the device is old (FW < 6.0.0, which we can not know)
1037 * then we need to try the "set voltage" command and then attempt to read the devicestring again. */
1038 if (dediprog_check_devicestring()) {
1039 if (dediprog_set_voltage())
1040 return 1;
1041 if (dediprog_check_devicestring())
1042 return 1;
1043 }
Nico Huber77fa67d2013-02-20 18:03:36 +00001044
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001045 /* SF100 only has 1 endpoint for in/out, SF600 uses two separate endpoints instead. */
1046 dediprog_in_endpoint = 2;
1047 if (dediprog_devicetype == DEV_SF100)
1048 dediprog_out_endpoint = 2;
1049 else
1050 dediprog_out_endpoint = 1;
1051
Simon Glass25e9f402015-06-28 13:31:19 +00001052 /* Set all possible LEDs as soon as possible to indicate activity.
1053 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
Simon Glassae616512016-01-23 23:27:58 +00001054 * dediprog_check_devicestring() has queried the device and set dediprog_firmwareversion. */
Simon Glass25e9f402015-06-28 13:31:19 +00001055 dediprog_set_leds(LED_ALL);
1056
Simon Glass557eb4f2015-07-05 16:53:22 +00001057 /* Select target/socket, frequency and VCC. */
1058 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
1059 dediprog_set_spi_speed(spispeed_idx) ||
1060 dediprog_set_spi_voltage(millivolt)) {
Simon Glass25e9f402015-06-28 13:31:19 +00001061 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001062 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001063 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001064
David Woodhouse7f3b89f2016-02-18 21:42:15 +00001065 if (dediprog_standalone_mode())
1066 return 1;
1067
Simon Glassae616512016-01-23 23:27:58 +00001068 if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE))
1069 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +00001070
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001071 return 0;
1072}