blob: 376c9744b9c6e34d150252e59fe5c1d0298e1359 [file] [log] [blame]
hailfingerdfb32a02010-01-19 11:15:48 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
David Hendricksfe05e742015-08-15 17:29:39 -07005 * Copyright (C) 2015 Simon Glass
6 * Copyright (C) 2015 Stefan Tauner
hailfingerdfb32a02010-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
David Hendricksfe05e742015-08-15 17:29:39 -070022//#include "platform.h"
23
24#include <stdlib.h>
hailfinger7a009082010-11-09 23:30:43 +000025#include <stdio.h>
hailfingerdfb32a02010-01-19 11:15:48 +000026#include <string.h>
David Hendricksfe05e742015-08-15 17:29:39 -070027#include <limits.h>
28#include <errno.h>
29
30#if IS_WINDOWS
31#include <lusb0_usb.h>
32#else
David Hendricksf9ecb452015-11-04 15:40:27 -080033#include <libusb.h>
David Hendricksfe05e742015-08-15 17:29:39 -070034#endif
35
hailfingerdfb32a02010-01-19 11:15:48 +000036#include "flash.h"
snelson8913d082010-02-26 05:48:29 +000037#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000038#include "programmer.h"
hailfingerdfb32a02010-01-19 11:15:48 +000039#include "spi.h"
40
stepan4c06de72011-01-28 09:00:15 +000041#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
hailfingerdfb32a02010-01-19 11:15:48 +000042#define DEFAULT_TIMEOUT 3000
David Hendricksf9ecb452015-11-04 15:40:27 -080043#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
44#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
45#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
46#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
47#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
48struct libusb_context *usb_ctx;
49static libusb_device_handle *dediprog_handle;
David Hendricksd7468582015-11-12 15:21:12 -080050static int dediprog_in_endpoint;
51static int dediprog_out_endpoint;
David Hendricksf9ecb452015-11-04 15:40:27 -080052static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
hailfingerdfb32a02010-01-19 11:15:48 +000053
David Hendricks6702e072015-08-15 18:09:04 -070054enum dediprog_devtype {
55 DEV_UNKNOWN = 0,
56 DEV_SF100 = 100,
57 DEV_SF600 = 600,
58};
59
60enum dediprog_devtype dediprog_devicetype;
61
David Hendricksfe05e742015-08-15 17:29:39 -070062enum dediprog_leds {
63 LED_INVALID = -1,
64 LED_NONE = 0,
Simon Glasse53cc1d2015-01-08 05:48:51 -070065 LED_PASS = 1 << 0,
66 LED_BUSY = 1 << 1,
67 LED_ERROR = 1 << 2,
68 LED_ALL = 7,
69};
70
Simon Glassa1f80682015-01-08 05:55:29 -070071/* IO bits for CMD_SET_IO_LED message */
David Hendricksfe05e742015-08-15 17:29:39 -070072enum dediprog_ios {
Simon Glassa1f80682015-01-08 05:55:29 -070073 IO1 = 1 << 0,
74 IO2 = 1 << 1,
75 IO3 = 1 << 2,
76 IO4 = 1 << 3,
77};
78
David Hendricksfe05e742015-08-15 17:29:39 -070079enum dediprog_cmds {
80 CMD_TRANSCEIVE = 0x01,
81 CMD_POLL_STATUS_REG = 0x02,
82 CMD_SET_VPP = 0x03,
83 CMD_SET_TARGET = 0x04,
84 CMD_READ_EEPROM = 0x05,
85 CMD_WRITE_EEPROM = 0x06,
86 CMD_SET_IO_LED = 0x07,
87 CMD_READ_PROG_INFO = 0x08,
88 CMD_SET_VCC = 0x09,
89 CMD_SET_STANDALONE = 0x0A,
David Hendricks28fbdfb2015-08-15 18:04:37 -070090 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
David Hendricksfe05e742015-08-15 17:29:39 -070091 CMD_GET_BUTTON = 0x11,
92 CMD_GET_UID = 0x12,
93 CMD_SET_CS = 0x14,
94 CMD_IO_MODE = 0x15,
95 CMD_FW_UPDATE = 0x1A,
96 CMD_FPGA_UPDATE = 0x1B,
97 CMD_READ_FPGA_VERSION = 0x1C,
98 CMD_SET_HOLD = 0x1D,
99 CMD_READ = 0x20,
100 CMD_WRITE = 0x30,
101 CMD_WRITE_AT45DB = 0x31,
102 CMD_NAND_WRITE = 0x32,
103 CMD_NAND_READ = 0x33,
104 CMD_SET_SPI_CLK = 0x61,
105 CMD_CHECK_SOCKET = 0x62,
106 CMD_DOWNLOAD_PRJ = 0x63,
107 CMD_READ_PRJ_NAME = 0x64,
108 // New protocol/firmware only
109 CMD_CHECK_SDCARD = 0x65,
110 CMD_READ_PRJ = 0x66,
Simon Glassa1f80682015-01-08 05:55:29 -0700111};
112
David Hendricksfe05e742015-08-15 17:29:39 -0700113enum dediprog_target {
114 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
115 FLASH_TYPE_FLASH_CARD,
116 FLASH_TYPE_APPLICATION_FLASH_2,
117 FLASH_TYPE_SOCKET,
Simon Glassa1f80682015-01-08 05:55:29 -0700118};
119
David Hendricksfe05e742015-08-15 17:29:39 -0700120enum dediprog_readmode {
121 READ_MODE_STD = 1,
122 READ_MODE_FAST = 2,
123 READ_MODE_ATMEL45 = 3,
124 READ_MODE_4B_ADDR_FAST = 4,
125 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Simon Glassd01002b2015-01-08 05:44:16 -0700126};
127
David Hendricksfe05e742015-08-15 17:29:39 -0700128enum dediprog_writemode {
129 WRITE_MODE_PAGE_PGM = 1,
130 WRITE_MODE_PAGE_WRITE = 2,
131 WRITE_MODE_1B_AAI = 3,
132 WRITE_MODE_2B_AAI = 4,
133 WRITE_MODE_128B_PAGE = 5,
134 WRITE_MODE_PAGE_AT26DF041 = 6,
135 WRITE_MODE_SILICON_BLUE_FPGA = 7,
136 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of length 512 bytes */
137 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
138 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of length 512 bytes */
139 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
140 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
Simon Glassd01002b2015-01-08 05:44:16 -0700141};
142
David Hendricksf9ecb452015-11-04 15:40:27 -0800143#ifndef LIBUSB_HAVE_ERROR_NAME
144/* Quick and dirty replacement for missing libusb_error_name in older libusb 1.0. */
145const char *libusb_error_name(int error_code)
146{
147 /* 18 chars for text, rest for number, sign, nullbyte. */
148 static char my_libusb_error[18 + 6];
David Hendricksfe05e742015-08-15 17:29:39 -0700149
David Hendricksf9ecb452015-11-04 15:40:27 -0800150 sprintf(my_libusb_error, "libusb error code %i", error_code);
151 return my_libusb_error;
152}
153#endif
David Hendricksfe05e742015-08-15 17:29:39 -0700154
David Hendricks14e82e52015-08-15 17:59:03 -0700155/* Returns true if firmware (and thus hardware) supports the "new" protocol */
156static int is_new_prot(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000157{
David Hendricks6702e072015-08-15 18:09:04 -0700158 switch (dediprog_devicetype) {
159 case DEV_SF100:
160 return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
161 case DEV_SF600:
162 return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0);
163 default:
164 return 0;
165 }
hailfingerdfb32a02010-01-19 11:15:48 +0000166}
David Hendricks14e82e52015-08-15 17:59:03 -0700167
David Hendricksf9ecb452015-11-04 15:40:27 -0800168struct dediprog_transfer_status {
169 int error; /* OK if 0, ERROR else */
170 unsigned int queued_idx;
171 unsigned int finished_idx;
172};
173
174static void dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
175{
176 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
177 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
178 status->error = 1;
179 msg_perr("SPI bulk read failed!\n");
180 }
181 ++status->finished_idx;
182}
183
184static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
185{
186 if (status->finished_idx >= status->queued_idx)
187 return 0;
188
189 do {
190 struct timeval timeout = { 10, 0 };
191 const int ret = libusb_handle_events_timeout_completed(usb_ctx, &timeout, NULL);
192 if (ret < 0) {
193 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
194 return 1;
195 }
196 } while (finish && (status->finished_idx < status->queued_idx));
197 return 0;
198}
199
David Hendricks14e82e52015-08-15 17:59:03 -0700200static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
201{
David Hendricksf9ecb452015-11-04 15:40:27 -0800202 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
203 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700204}
205
206static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
207{
David Hendricksf9ecb452015-11-04 15:40:27 -0800208 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
209 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700210}
hailfingerdfb32a02010-01-19 11:15:48 +0000211
David Hendricksf9ecb452015-11-04 15:40:27 -0800212
213#if 0
hailfinger1ff33dc2010-07-03 11:02:10 +0000214/* Might be useful for other USB devices as well. static for now. */
David Hendricksfe05e742015-08-15 17:29:39 -0700215/* device parameter allows user to specify one device of multiple installed */
216static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device)
hailfingerdfb32a02010-01-19 11:15:48 +0000217{
218 struct usb_bus *bus;
219 struct usb_device *dev;
220
221 for (bus = usb_get_busses(); bus; bus = bus->next)
222 for (dev = bus->devices; dev; dev = dev->next)
223 if ((dev->descriptor.idVendor == vid) &&
David Hendricksfe05e742015-08-15 17:29:39 -0700224 (dev->descriptor.idProduct == pid)) {
225 if (device == 0)
226 return dev;
227 device--;
228 }
hailfingerdfb32a02010-01-19 11:15:48 +0000229
230 return NULL;
231}
David Hendricksf9ecb452015-11-04 15:40:27 -0800232#endif
233
234/* Might be useful for other USB devices as well. static for now. */
235/* device parameter allows user to specify one device of multiple installed */
236static struct libusb_device_handle *get_device_by_vid_pid_number(uint16_t vid, uint16_t pid, unsigned int num)
237{
238 struct libusb_device **list;
239 ssize_t i = 0;
240 int err = 0;
241 struct libusb_device_handle *handle = NULL;
242 struct libusb_device_descriptor desc = {};
243 ssize_t count = libusb_get_device_list(usb_ctx, &list);
244
245 if (count < 0) {
246 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
247 return NULL;
248 }
249
250 for (i = 0; i < count; i++) {
251 struct libusb_device *dev = list[i];
252 err = libusb_get_device_descriptor(dev, &desc);
253 if (err != 0) {
254 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
255 libusb_free_device_list(list, 1);
256 return NULL;
257 }
258 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
259 msg_pdbg("Found USB device %04hx:%04hx at address %hhx-%hhx.\n", desc.idVendor,
260 desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev));
261 if (num == 0) {
262 err = libusb_open(dev, &handle);
263 if (err != 0) {
264 msg_perr("Opening the USB device failed (%s)!\n",
265 libusb_error_name(err));
266 libusb_free_device_list(list, 1);
267 return NULL;
268 }
269 break;
270 }
271 num--;
272 }
273 }
274 libusb_free_device_list(list, 1);
275
276 return handle;
277}
hailfingerdfb32a02010-01-19 11:15:48 +0000278
David Hendricksfe05e742015-08-15 17:29:39 -0700279/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
stepan4c06de72011-01-28 09:00:15 +0000280static int dediprog_set_leds(int leds)
281{
David Hendricksfe05e742015-08-15 17:29:39 -0700282 if (leds < LED_NONE || leds > LED_ALL)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700283 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000284
David Hendricks14e82e52015-08-15 17:59:03 -0700285 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
286 * them around if we have an old device. On those devices the LEDs map as follows:
stepan4c06de72011-01-28 09:00:15 +0000287 * bit 2 == 0: green light is on.
David Hendricks14e82e52015-08-15 17:59:03 -0700288 * bit 0 == 0: red light is on.
289 *
290 * Additionally, the command structure has changed with the "new" protocol.
291 *
292 * FIXME: take IO pins into account
stepan4c06de72011-01-28 09:00:15 +0000293 */
David Hendricks14e82e52015-08-15 17:59:03 -0700294 int target_leds, ret;
295 if (is_new_prot()) {
296 target_leds = (leds ^ 7) << 8;
297 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
stepan4c06de72011-01-28 09:00:15 +0000298 } else {
David Hendricks14e82e52015-08-15 17:59:03 -0700299 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
300 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
301 } else {
302 target_leds = leds;
303 }
304 target_leds ^= 7;
305
306 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Simon Glass543101a2015-01-08 06:28:13 -0700307 }
David Hendricksfe05e742015-08-15 17:29:39 -0700308
stepan4c06de72011-01-28 09:00:15 +0000309 if (ret != 0x0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800310 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
stepan4c06de72011-01-28 09:00:15 +0000311 return 1;
312 }
313
stepan4c06de72011-01-28 09:00:15 +0000314 return 0;
315}
316
hailfingerf76cc322010-11-09 22:00:31 +0000317static int dediprog_set_spi_voltage(int millivolt)
hailfingerdfb32a02010-01-19 11:15:48 +0000318{
319 int ret;
hailfingerf76cc322010-11-09 22:00:31 +0000320 uint16_t voltage_selector;
hailfingerdfb32a02010-01-19 11:15:48 +0000321
hailfingerf76cc322010-11-09 22:00:31 +0000322 switch (millivolt) {
323 case 0:
hailfingerdfb32a02010-01-19 11:15:48 +0000324 /* Admittedly this one is an assumption. */
hailfingerf76cc322010-11-09 22:00:31 +0000325 voltage_selector = 0x0;
hailfingerdfb32a02010-01-19 11:15:48 +0000326 break;
hailfingerf76cc322010-11-09 22:00:31 +0000327 case 1800:
328 voltage_selector = 0x12;
hailfingerdfb32a02010-01-19 11:15:48 +0000329 break;
hailfingerf76cc322010-11-09 22:00:31 +0000330 case 2500:
331 voltage_selector = 0x11;
hailfingerdfb32a02010-01-19 11:15:48 +0000332 break;
hailfingerf76cc322010-11-09 22:00:31 +0000333 case 3500:
334 voltage_selector = 0x10;
hailfingerdfb32a02010-01-19 11:15:48 +0000335 break;
336 default:
hailfingerf76cc322010-11-09 22:00:31 +0000337 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
hailfingerdfb32a02010-01-19 11:15:48 +0000338 return 1;
339 }
hailfingerf76cc322010-11-09 22:00:31 +0000340 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
341 millivolt % 1000);
hailfingerdfb32a02010-01-19 11:15:48 +0000342
David Hendricksfe05e742015-08-15 17:29:39 -0700343 if (voltage_selector == 0) {
344 /* Wait some time as the original driver does. */
345 programmer_delay(200 * 1000);
346 }
David Hendricks14e82e52015-08-15 17:59:03 -0700347 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000348 if (ret != 0x0) {
uwe8d342eb2011-07-28 08:13:25 +0000349 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
350 voltage_selector);
hailfingerdfb32a02010-01-19 11:15:48 +0000351 return 1;
352 }
David Hendricksfe05e742015-08-15 17:29:39 -0700353 if (voltage_selector != 0) {
354 /* Wait some time as the original driver does. */
355 programmer_delay(200 * 1000);
356 }
hailfingerdfb32a02010-01-19 11:15:48 +0000357 return 0;
358}
359
David Hendricksfe05e742015-08-15 17:29:39 -0700360struct dediprog_spispeeds {
361 const char *const name;
362 const int speed;
363};
364
365static const struct dediprog_spispeeds spispeeds[] = {
366 { "24M", 0x0 },
367 { "12M", 0x2 },
368 { "8M", 0x1 },
369 { "3M", 0x3 },
370 { "2.18M", 0x4 },
371 { "1.5M", 0x5 },
372 { "750k", 0x6 },
373 { "375k", 0x7 },
374 { NULL, 0x0 },
375};
376
377static int dediprog_set_spi_speed(unsigned int spispeed_idx)
hailfingerdfb32a02010-01-19 11:15:48 +0000378{
David Hendricksfe05e742015-08-15 17:29:39 -0700379 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
380 msg_pinfo("Skipping to set SPI speed because firmware is too old.\n");
381 return 0;
382 }
hailfingerdfb32a02010-01-19 11:15:48 +0000383
David Hendricksfe05e742015-08-15 17:29:39 -0700384 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
385 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
hailfingerdfb32a02010-01-19 11:15:48 +0000386
David Hendricks14e82e52015-08-15 17:59:03 -0700387 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000388 if (ret != 0x0) {
David Hendricksfe05e742015-08-15 17:29:39 -0700389 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
hailfingerdfb32a02010-01-19 11:15:48 +0000390 return 1;
391 }
392 return 0;
393}
394
hailfingerfb6287d2010-11-16 21:25:29 +0000395/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
396 * @start start address
397 * @len length
398 * @return 0 on success, 1 on failure
399 */
400static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000401 unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000402{
David Hendricksf9ecb452015-11-04 15:40:27 -0800403 int ret, err = 1;
stefanctc5eb8a92011-11-23 09:13:48 +0000404 unsigned int i;
hailfingerfb6287d2010-11-16 21:25:29 +0000405 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000406 const unsigned int chunksize = 0x200;
407 const unsigned int count = len / chunksize;
David Hendricks14e82e52015-08-15 17:59:03 -0700408 unsigned int cmd_len;
David Hendricksf9ecb452015-11-04 15:40:27 -0800409 struct dediprog_transfer_status status = { 0, 0, 0 };
410 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
411 struct libusb_transfer *transfer;
hailfingerfb6287d2010-11-16 21:25:29 +0000412
413 if ((start % chunksize) || (len % chunksize)) {
414 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
415 "at flashrom@flashrom.org\n", __func__, start, len);
416 return 1;
417 }
418
419 /* No idea if the hardware can handle empty reads, so chicken out. */
420 if (!len)
421 return 0;
David Hendricks14e82e52015-08-15 17:59:03 -0700422 /* Command Read SPI Bulk. */
423 if (is_new_prot()) {
424 const uint8_t read_cmd[] = {
425 count & 0xff,
426 (count >> 8) & 0xff,
427 0,
428 READ_MODE_FAST,
429 0,
430 0,
431 start & 0xff,
432 (start >> 8) & 0xff,
433 (start >> 16) & 0xff,
434 (start >> 24) & 0xff,
435 };
436
437 cmd_len = sizeof(read_cmd);
438 ret = dediprog_write(CMD_READ, 0, 0, read_cmd, cmd_len);
439 } else {
440 const uint8_t read_cmd[] = {count & 0xff,
441 (count >> 8) & 0xff,
442 chunksize & 0xff,
443 (chunksize >> 8) & 0xff};
444
445 cmd_len = sizeof(read_cmd);
446 ret = dediprog_write(CMD_READ, start % 0x10000, start / 0x10000, read_cmd, cmd_len);
447 }
448 if (ret != cmd_len) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800449 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
hailfingerfb6287d2010-11-16 21:25:29 +0000450 return 1;
451 }
452
David Hendricksf9ecb452015-11-04 15:40:27 -0800453 /*
454 * Ring buffer of bulk transfers.
455 * Poll until at least one transfer is ready,
456 * schedule next transfers until buffer is full.
457 */
hailfingerfb6287d2010-11-16 21:25:29 +0000458
David Hendricksf9ecb452015-11-04 15:40:27 -0800459 /* Allocate bulk transfers. */
460 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
461 transfers[i] = libusb_alloc_transfer(0);
462 if (!transfers[i]) {
463 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
464 goto _err_free;
465 }
466 }
467
468 /* Now transfer requested chunks using libusb's asynchronous interface. */
469 while (!status.error && (status.queued_idx < count)) {
470 while ((status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS) {
471 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
472 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
473 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
474 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
475 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
476 ret = libusb_submit_transfer(transfer);
477 if (ret < 0) {
478 msg_perr("Submitting SPI bulk read %i failed: %i %s!\n",
479 status.queued_idx, ret, libusb_error_name(ret));
480 goto _err_free;
481 }
482 ++status.queued_idx;
483 }
484 if (dediprog_bulk_read_poll(&status, 0))
485 goto _err_free;
486 }
487 /* Wait for transfers to finish. */
488 if (dediprog_bulk_read_poll(&status, 1))
489 goto _err_free;
490 /* Check if everything has been transmitted. */
491 if ((status.finished_idx < count) || status.error)
492 goto _err_free;
493
494 err = 0;
495
496_err_free:
497 dediprog_bulk_read_poll(&status, 1);
498 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
499 if (transfers[i]) libusb_free_transfer(transfers[i]);
500 return err;
hailfingerfb6287d2010-11-16 21:25:29 +0000501}
502
stefanctc5eb8a92011-11-23 09:13:48 +0000503static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf,
504 unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000505{
hailfingerfb6287d2010-11-16 21:25:29 +0000506 int ret;
507 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000508 const unsigned int chunksize = 0x200;
509 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
510 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000511
Simon Glasse53cc1d2015-01-08 05:48:51 -0700512 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000513
hailfingerfb6287d2010-11-16 21:25:29 +0000514 if (residue) {
515 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
516 start, residue);
517 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700518 if (ret)
519 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000520 }
521
522 /* Round down. */
523 bulklen = (len - residue) / chunksize * chunksize;
524 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
525 bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700526 if (ret)
527 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000528
529 len -= residue + bulklen;
530 if (len) {
531 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
532 start, len);
533 ret = spi_read_chunked(flash, buf + residue + bulklen,
534 start + residue + bulklen, len, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700535 if (ret)
536 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000537 }
538
Simon Glasse53cc1d2015-01-08 05:48:51 -0700539 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000540 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700541err:
542 dediprog_set_leds(LED_ERROR);
543 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000544}
545
David Hendricksfe05e742015-08-15 17:29:39 -0700546/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
547 * @chunksize length of data chunks, only 256 supported by now
548 * @start start address
549 * @len length
550 * @dedi_spi_cmd dediprog specific write command for spi bus
551 * @return 0 on success, 1 on failure
552 */
553static int dediprog_spi_bulk_write(struct flashchip *flash, const uint8_t *buf, unsigned int chunksize,
554 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
hailfinger556e9c32010-11-23 21:28:16 +0000555{
David Hendricksf9ecb452015-11-04 15:40:27 -0800556 int ret, transferred;
David Hendricksfe05e742015-08-15 17:29:39 -0700557 unsigned int i;
558 /* USB transfer size must be 512, other sizes will NOT work at all.
559 * chunksize is the real data size per USB bulk transfer. The remaining
560 * space in a USB bulk transfer must be filled with 0xff padding.
561 */
562 const unsigned int count = len / chunksize;
David Hendricksf9ecb452015-11-04 15:40:27 -0800563 const unsigned char count_and_cmd_old[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
564 const unsigned char count_and_cmd_new[] = {
David Hendricks741efe22015-08-15 19:18:51 -0700565 count & 0xff,
566 (count >> 8) & 0xff,
567 0, /* used for 24-bit address support? */
568 dedi_spi_cmd,
569 JEDEC_BYTE_PROGRAM, /* FIXME: needs to be determined based on byte 3? */
570 0,
571 start & 0xff,
572 (start >> 8) & 0xff,
573 (start >> 16) & 0xff,
574 (start >> 24) & 0xff,
575 };
David Hendricksf9ecb452015-11-04 15:40:27 -0800576 unsigned char usbbuf[512];
David Hendricksfe05e742015-08-15 17:29:39 -0700577
578 /*
579 * We should change this check to
580 * chunksize > 512
581 * once we know how to handle different chunk sizes.
582 */
583 if (chunksize != 256) {
584 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
585 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
586 return 1;
587 }
588
589 if ((start % chunksize) || (len % chunksize)) {
590 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
591 "at flashrom@flashrom.org\n", __func__, start, len);
592 return 1;
593 }
594
595 /* No idea if the hardware can handle empty writes, so chicken out. */
596 if (!len)
597 return 0;
David Hendricks741efe22015-08-15 19:18:51 -0700598 if (!is_new_prot()) {
599 /* Command Write SPI Bulk. No idea which write command is used on the
600 * SPI side.
601 */
David Hendricksf9ecb452015-11-04 15:40:27 -0800602 ret = dediprog_write(CMD_WRITE, start % 0x10000, start / 0x10000,
603 count_and_cmd_old, sizeof(count_and_cmd_old));
David Hendricks741efe22015-08-15 19:18:51 -0700604 if (ret != sizeof(count_and_cmd_old)) {
605 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
David Hendricksf9ecb452015-11-04 15:40:27 -0800606 libusb_error_name(ret));
David Hendricks741efe22015-08-15 19:18:51 -0700607 return 1;
608 }
609 } else {
610 /* Command Write SPI Bulk. No idea which write command is used on the
611 * SPI side.
612 */
David Hendricksf9ecb452015-11-04 15:40:27 -0800613 ret = dediprog_write(CMD_WRITE, 0, 0,
614 count_and_cmd_new, sizeof(count_and_cmd_new));
David Hendricks741efe22015-08-15 19:18:51 -0700615 if (ret != sizeof(count_and_cmd_new)) {
616 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
David Hendricksf9ecb452015-11-04 15:40:27 -0800617 libusb_error_name(ret));
David Hendricks741efe22015-08-15 19:18:51 -0700618 return 1;
619 }
David Hendricksfe05e742015-08-15 17:29:39 -0700620 }
621
622 for (i = 0; i < count; i++) {
623 memset(usbbuf, 0xff, sizeof(usbbuf));
624 memcpy(usbbuf, buf + i * chunksize, chunksize);
David Hendricksf9ecb452015-11-04 15:40:27 -0800625 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint,
626 usbbuf, 512, &transferred, DEFAULT_TIMEOUT);
627 if ((ret < 0) || (transferred != 512)) {
628 msg_perr("SPI bulk write failed, expected %i, got %i %s!\n",
629 512, ret, libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700630 return 1;
631 }
632 }
633
634 return 0;
635}
636
637static int dediprog_spi_write(struct flashchip *flash, const uint8_t *buf,
638 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
639{
640 int ret;
David Hendricksfe05e742015-08-15 17:29:39 -0700641 const unsigned int chunksize = flash->page_size;
642 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
643 unsigned int bulklen;
stepan4c06de72011-01-28 09:00:15 +0000644
Simon Glasse53cc1d2015-01-08 05:48:51 -0700645 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000646
David Hendricksfe05e742015-08-15 17:29:39 -0700647 if (chunksize != 256) {
648 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
649 "we don't know how dediprog\nhandles them.\n");
650 /* Write everything like it was residue. */
651 residue = len;
652 }
stepan4c06de72011-01-28 09:00:15 +0000653
David Hendricksfe05e742015-08-15 17:29:39 -0700654 if (residue) {
655 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
656 start, residue);
657 /* No idea about the real limit. Maybe 12, maybe more. */
658 ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 12);
659 if (ret) {
660 dediprog_set_leds(LED_ERROR);
661 return ret;
662 }
663 }
664
665 /* Round down. */
666 bulklen = (len - residue) / chunksize * chunksize;
667 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
668 if (ret) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700669 dediprog_set_leds(LED_ERROR);
David Hendricksfe05e742015-08-15 17:29:39 -0700670 return ret;
671 }
stepan4c06de72011-01-28 09:00:15 +0000672
David Hendricksfe05e742015-08-15 17:29:39 -0700673 len -= residue + bulklen;
674 if (len) {
675 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
676 start, len);
677 ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen),
678 start + residue + bulklen, len, 12);
679 if (ret) {
680 dediprog_set_leds(LED_ERROR);
681 return ret;
682 }
683 }
684
685 dediprog_set_leds(LED_PASS);
686 return 0;
hailfinger556e9c32010-11-23 21:28:16 +0000687}
688
David Hendricksfe05e742015-08-15 17:29:39 -0700689//static int dediprog_spi_write_256(struct flashchip *flash, const uint8_t *buf, unsigned int start, unsigned int len)
690static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
691{
692 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
693}
694
695#if 0
696static int dediprog_spi_write_aai(struct flashchip *flash, const uint8_t *buf, unsigned int start, unsigned int len)
697{
698 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
699}
700#endif
701
702//static int dediprog_spi_send_command(struct flashchip *flash,
703static int dediprog_spi_send_command(unsigned int writecnt,
704 unsigned int readcnt,
705 const unsigned char *writearr,
706 unsigned char *readarr)
hailfingerdfb32a02010-01-19 11:15:48 +0000707{
708 int ret;
709
hailfingeraf389cc2010-01-22 02:53:30 +0000710 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
David Hendricksfe05e742015-08-15 17:29:39 -0700711 if (writecnt > UINT16_MAX) {
712 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
713 return 1;
Simon Glass543101a2015-01-08 06:28:13 -0700714 }
David Hendricksfe05e742015-08-15 17:29:39 -0700715 if (readcnt > UINT16_MAX) {
716 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
717 return 1;
718 }
719
David Hendricks14e82e52015-08-15 17:59:03 -0700720 /* New protocol has the read flag as value while the old protocol had it in the index field. */
721 if (is_new_prot()) {
722 ret = dediprog_write(CMD_TRANSCEIVE, readcnt ? 1 : 0, 0, writearr, writecnt);
723 } else {
724 ret = dediprog_write(CMD_TRANSCEIVE, 0, readcnt ? 1 : 0, writearr, writecnt);
725 }
hailfingerdfb32a02010-01-19 11:15:48 +0000726 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000727 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800728 writecnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000729 return 1;
730 }
David Hendricksfe05e742015-08-15 17:29:39 -0700731 if (readcnt == 0)
hailfingerdfb32a02010-01-19 11:15:48 +0000732 return 0;
David Hendricksfe05e742015-08-15 17:29:39 -0700733
David Hendricks14e82e52015-08-15 17:59:03 -0700734 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000735 if (ret != readcnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000736 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800737 readcnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000738 return 1;
739 }
740 return 0;
741}
742
hailfinger1ff33dc2010-07-03 11:02:10 +0000743static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000744{
745 int ret;
hailfinger7a009082010-11-09 23:30:43 +0000746 int fw[3];
David Hendricks6702e072015-08-15 18:09:04 -0700747 int sfnum;
hailfingerdfb32a02010-01-19 11:15:48 +0000748 char buf[0x11];
749
hailfingerdfb32a02010-01-19 11:15:48 +0000750 /* Command Receive Device String. */
David Hendricks14e82e52015-08-15 17:59:03 -0700751 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000752 if (ret != 0x10) {
753 msg_perr("Incomplete/failed Command Receive Device String!\n");
754 return 1;
755 }
756 buf[0x10] = '\0';
757 msg_pdbg("Found a %s\n", buf);
David Hendricks6702e072015-08-15 18:09:04 -0700758 if (memcmp(buf, "SF100", 0x5) == 0)
759 dediprog_devicetype = DEV_SF100;
760 else if (memcmp(buf, "SF600", 0x5) == 0)
761 dediprog_devicetype = DEV_SF600;
762 else {
hailfingerdfb32a02010-01-19 11:15:48 +0000763 msg_perr("Device not a SF100!\n");
764 return 1;
765 }
David Hendricks6702e072015-08-15 18:09:04 -0700766 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2])
767 != 4 || sfnum != dediprog_devicetype) {
David Hendricks14e82e52015-08-15 17:59:03 -0700768 msg_perr("Unexpected firmware version string '%s'\n", buf);
hailfinger7a009082010-11-09 23:30:43 +0000769 return 1;
770 }
David Hendricks14e82e52015-08-15 17:59:03 -0700771 /* Only these major versions were tested. */
David Hendricks6702e072015-08-15 18:09:04 -0700772 if (fw[0] < 2 || fw[0] > 7) {
David Hendricks14e82e52015-08-15 17:59:03 -0700773 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000774 return 1;
775 }
stepan4c06de72011-01-28 09:00:15 +0000776 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
David Hendricks14e82e52015-08-15 17:59:03 -0700777
hailfingerdfb32a02010-01-19 11:15:48 +0000778 return 0;
779}
780
David Hendricks28fbdfb2015-08-15 18:04:37 -0700781/*
782 * This command presumably sets the voltage for the SF100 itself (not the
783 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
784 * (including all SF600s) do not support it.
785 */
786static int dediprog_set_voltage(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000787{
788 int ret;
David Hendricksf9ecb452015-11-04 15:40:27 -0800789 unsigned char buf[0x1];
hailfingerdfb32a02010-01-19 11:15:48 +0000790
791 memset(buf, 0, sizeof(buf));
David Hendricksf9ecb452015-11-04 15:40:27 -0800792 ret = dediprog_write(CMD_SET_VOLTAGE, 0x0, 0x0, buf, 0x1);
hailfinger7a009082010-11-09 23:30:43 +0000793 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800794 msg_perr("Command A failed (%s)!\n", libusb_error_name(ret));
hailfinger7a009082010-11-09 23:30:43 +0000795 return 1;
796 }
hailfingerdfb32a02010-01-19 11:15:48 +0000797 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -0700798 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000799 return 1;
800 }
David Hendricks28fbdfb2015-08-15 18:04:37 -0700801
hailfingerdfb32a02010-01-19 11:15:48 +0000802 return 0;
803}
804
David Hendricksfe05e742015-08-15 17:29:39 -0700805#if 0
806/* Something.
807 * Present in eng_detect_blink.log with firmware 3.1.8
808 * Always preceded by Command Receive Device String
809 */
810static int dediprog_command_b(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000811{
812 int ret;
David Hendricksfe05e742015-08-15 17:29:39 -0700813 char buf[0x3];
hailfingerdfb32a02010-01-19 11:15:48 +0000814
David Hendricksfe05e742015-08-15 17:29:39 -0700815 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
816 buf, 0x3, DEFAULT_TIMEOUT);
817 if (ret < 0) {
818 msg_perr("Command B failed (%s)!\n", usb_strerror());
819 return 1;
820 }
821 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
822 (buf[2] != 0xff)) {
823 msg_perr("Unexpected response to Command B!\n");
824 return 1;
825 }
826
827 return 0;
828}
829#endif
830
831static int set_target_flash(enum dediprog_target target)
832{
David Hendricksf9ecb452015-11-04 15:40:27 -0800833 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
David Hendricksfe05e742015-08-15 17:29:39 -0700834 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800835 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000836 return 1;
837 }
838 return 0;
839}
840
David Hendricksfe05e742015-08-15 17:29:39 -0700841#if 0
842/* Returns true if the button is currently pressed. */
843static bool dediprog_get_button(void)
Simon Glassd01002b2015-01-08 05:44:16 -0700844{
David Hendricksfe05e742015-08-15 17:29:39 -0700845 char buf[1];
846 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
847 buf, 0x1, DEFAULT_TIMEOUT);
848 if (ret != 0) {
849 msg_perr("Could not get button state (%s)!\n", usb_strerror());
850 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700851 }
David Hendricksfe05e742015-08-15 17:29:39 -0700852 return buf[0] != 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700853}
David Hendricksfe05e742015-08-15 17:29:39 -0700854#endif
Simon Glassd01002b2015-01-08 05:44:16 -0700855
hailfingerf76cc322010-11-09 22:00:31 +0000856static int parse_voltage(char *voltage)
857{
858 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000859 int i;
860 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000861
862 if (!voltage || !strlen(voltage)) {
863 msg_perr("Empty voltage= specified.\n");
864 return -1;
865 }
866 millivolt = (int)strtol(voltage, &tmp, 0);
867 voltage = tmp;
868 /* Handle "," and "." as decimal point. Everything after it is assumed
869 * to be in decimal notation.
870 */
871 if ((*voltage == '.') || (*voltage == ',')) {
872 voltage++;
873 for (i = 0; i < 3; i++) {
874 fraction *= 10;
875 /* Don't advance if the current character is invalid,
876 * but continue multiplying.
877 */
878 if ((*voltage < '0') || (*voltage > '9'))
879 continue;
880 fraction += *voltage - '0';
881 voltage++;
882 }
883 /* Throw away remaining digits. */
884 voltage += strspn(voltage, "0123456789");
885 }
886 /* The remaining string must be empty or "mV" or "V". */
887 tolower_string(voltage);
888
889 /* No unit or "V". */
890 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
891 millivolt *= 1000;
892 millivolt += fraction;
893 } else if (!strncmp(voltage, "mv", 2) ||
894 !strncmp(voltage, "milliv", 6)) {
895 /* No adjustment. fraction is discarded. */
896 } else {
897 /* Garbage at the end of the string. */
898 msg_perr("Garbage voltage= specified.\n");
899 return -1;
900 }
901 return millivolt;
902}
903
David Hendricksfe05e742015-08-15 17:29:39 -0700904#if 0
905static const struct spi_master spi_master_dediprog = {
906 .type = SPI_CONTROLLER_DEDIPROG,
907 .max_data_read = MAX_DATA_UNSPECIFIED,
908 .max_data_write = MAX_DATA_UNSPECIFIED,
909 .command = dediprog_spi_send_command,
910 .multicommand = default_spi_send_multicommand,
911 .read = dediprog_spi_read,
912 .write_256 = dediprog_spi_write_256,
913 .write_aai = dediprog_spi_write_aai,
914};
915#endif
mkarcherd264e9e2011-05-11 17:07:07 +0000916static const struct spi_programmer spi_programmer_dediprog = {
uwe8d342eb2011-07-28 08:13:25 +0000917 .type = SPI_CONTROLLER_DEDIPROG,
918 .max_data_read = MAX_DATA_UNSPECIFIED,
919 .max_data_write = MAX_DATA_UNSPECIFIED,
920 .command = dediprog_spi_send_command,
921 .multicommand = default_spi_send_multicommand,
922 .read = dediprog_spi_read,
923 .write_256 = dediprog_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000924};
925
dhendrix0ffc2eb2011-06-14 01:35:36 +0000926static int dediprog_shutdown(void *data)
927{
928 msg_pspew("%s\n", __func__);
929
David Hendricksfe05e742015-08-15 17:29:39 -0700930 dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Hendricks6702e072015-08-15 18:09:04 -0700931 dediprog_devicetype = DEV_UNKNOWN;
David Hendricksfe05e742015-08-15 17:29:39 -0700932
dhendrix0ffc2eb2011-06-14 01:35:36 +0000933 /* URB 28. Command Set SPI Voltage to 0. */
934 if (dediprog_set_spi_voltage(0x0))
935 return 1;
936
David Hendricksf9ecb452015-11-04 15:40:27 -0800937 if (libusb_release_interface(dediprog_handle, 0)) {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000938 msg_perr("Could not release USB interface!\n");
939 return 1;
940 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800941 libusb_close(dediprog_handle);
942 libusb_exit(usb_ctx);
943
dhendrix0ffc2eb2011-06-14 01:35:36 +0000944 return 0;
945}
946
hailfingerdfb32a02010-01-19 11:15:48 +0000947/* URB numbers refer to the first log ever captured. */
948int dediprog_init(void)
949{
David Hendricksfe05e742015-08-15 17:29:39 -0700950 char *voltage, *device, *spispeed, *target_str;
951 int spispeed_idx = 1;
hailfingerb91c08c2011-08-15 19:54:20 +0000952 int millivolt = 3500;
David Hendricksfe05e742015-08-15 17:29:39 -0700953 long usedevice = 0;
954 long target = 1;
955 int i, ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000956
957 msg_pspew("%s\n", __func__);
958
David Hendricksfe05e742015-08-15 17:29:39 -0700959 spispeed = extract_programmer_param("spispeed");
960 if (spispeed) {
961 for (i = 0; spispeeds[i].name; ++i) {
962 if (!strcasecmp(spispeeds[i].name, spispeed)) {
963 spispeed_idx = i;
964 break;
965 }
966 }
967 if (!spispeeds[i].name) {
968 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
969 free(spispeed);
970 return 1;
971 }
972 free(spispeed);
973 }
974
hailfingerf76cc322010-11-09 22:00:31 +0000975 voltage = extract_programmer_param("voltage");
976 if (voltage) {
977 millivolt = parse_voltage(voltage);
978 free(voltage);
uwe8d342eb2011-07-28 08:13:25 +0000979 if (millivolt < 0)
hailfingerf76cc322010-11-09 22:00:31 +0000980 return 1;
hailfingerf76cc322010-11-09 22:00:31 +0000981 msg_pinfo("Setting voltage to %i mV\n", millivolt);
982 }
David Hendricksfe05e742015-08-15 17:29:39 -0700983
984 device = extract_programmer_param("device");
985 if (device) {
986 char *dev_suffix;
987 errno = 0;
988 usedevice = strtol(device, &dev_suffix, 10);
989 if (errno != 0 || device == dev_suffix) {
990 msg_perr("Error: Could not convert 'device'.\n");
991 free(device);
Simon Glassd01002b2015-01-08 05:44:16 -0700992 return 1;
993 }
David Hendricksfe05e742015-08-15 17:29:39 -0700994 if (usedevice < 0 || usedevice > UINT_MAX) {
995 msg_perr("Error: Value for 'device' is out of range.\n");
996 free(device);
997 return 1;
998 }
999 if (strlen(dev_suffix) > 0) {
1000 msg_perr("Error: Garbage following 'device' value.\n");
1001 free(device);
1002 return 1;
1003 }
1004 msg_pinfo("Using device %li.\n", usedevice);
Simon Glassd01002b2015-01-08 05:44:16 -07001005 }
David Hendricksfe05e742015-08-15 17:29:39 -07001006 free(device);
1007
1008 target_str = extract_programmer_param("target");
1009 if (target_str) {
1010 char *target_suffix;
1011 errno = 0;
1012 target = strtol(target_str, &target_suffix, 10);
1013 if (errno != 0 || target_str == target_suffix) {
1014 msg_perr("Error: Could not convert 'target'.\n");
1015 free(target_str);
1016 return 1;
1017 }
1018 if (target < 1 || target > 2) {
1019 msg_perr("Error: Value for 'target' is out of range.\n");
1020 free(target_str);
1021 return 1;
1022 }
1023 if (strlen(target_suffix) > 0) {
1024 msg_perr("Error: Garbage following 'target' value.\n");
1025 free(target_str);
1026 return 1;
1027 }
1028 msg_pinfo("Using target %li.\n", target);
1029 }
1030 free(target_str);
hailfingerf76cc322010-11-09 22:00:31 +00001031
hailfingerdfb32a02010-01-19 11:15:48 +00001032 /* Here comes the USB stuff. */
David Hendricksf9ecb452015-11-04 15:40:27 -08001033 libusb_init(&usb_ctx);
1034 if (!usb_ctx) {
1035 msg_perr("Could not initialize libusb!\n");
hailfingerdfb32a02010-01-19 11:15:48 +00001036 return 1;
1037 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001038 dediprog_handle = get_device_by_vid_pid_number(0x0483, 0xdada, (unsigned int) usedevice);
David Hendricksfe05e742015-08-15 17:29:39 -07001039 if (!dediprog_handle) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001040 msg_perr("Could not find a Dediprog SF100 on USB!\n");
1041 libusb_exit(usb_ctx);
David Hendricksfe05e742015-08-15 17:29:39 -07001042 return 1;
1043 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001044 ret = libusb_set_configuration(dediprog_handle, 1);
1045 if (ret != 0) {
1046 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1047 libusb_close(dediprog_handle);
1048 libusb_exit(usb_ctx);
hailfingerfb6287d2010-11-16 21:25:29 +00001049 return 1;
1050 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001051 ret = libusb_claim_interface(dediprog_handle, 0);
hailfingerfb6287d2010-11-16 21:25:29 +00001052 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001053 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1054 libusb_close(dediprog_handle);
1055 libusb_exit(usb_ctx);
hailfingerfb6287d2010-11-16 21:25:29 +00001056 return 1;
1057 }
David Hendricksfe05e742015-08-15 17:29:39 -07001058
dhendrix0ffc2eb2011-06-14 01:35:36 +00001059 if (register_shutdown(dediprog_shutdown, NULL))
1060 return 1;
1061
David Hendricks28fbdfb2015-08-15 18:04:37 -07001062 /* Try reading the devicestring. If that fails and the device is old
1063 * (FW < 6.0.0) then we need to try the "set voltage" command and then
1064 * attempt to read the devicestring again. */
1065 if (dediprog_check_devicestring()) {
1066 if (dediprog_set_voltage())
1067 return 1;
1068 if (dediprog_check_devicestring())
1069 return 1;
1070 }
David Hendricksfe05e742015-08-15 17:29:39 -07001071
David Hendricksd7468582015-11-12 15:21:12 -08001072 /* SF100 firmware exposes only one endpoint for in/out, SF600 firmware
1073 exposes separate endpoints for in and out. */
1074 dediprog_in_endpoint = 2;
1075 if (dediprog_devicetype == DEV_SF100)
1076 dediprog_out_endpoint = 2;
1077 else
1078 dediprog_out_endpoint = 1;
1079
1080
David Hendricksfe05e742015-08-15 17:29:39 -07001081 /* Set all possible LEDs as soon as possible to indicate activity.
1082 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
1083 * dediprog_setup() has queried the device and set dediprog_firmwareversion. */
1084 dediprog_set_leds(LED_ALL);
1085
1086 /* Select target/socket, frequency and VCC. */
1087 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
1088 dediprog_set_spi_speed(spispeed_idx) ||
1089 dediprog_set_spi_voltage(millivolt)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -07001090 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +00001091 return 1;
stepan4c06de72011-01-28 09:00:15 +00001092 }
hailfingerdfb32a02010-01-19 11:15:48 +00001093
David Hendricksfe05e742015-08-15 17:29:39 -07001094// register_spi_master(&spi_master_dediprog);
mkarcherd264e9e2011-05-11 17:07:07 +00001095 register_spi_programmer(&spi_programmer_dediprog);
hailfingerdfb32a02010-01-19 11:15:48 +00001096
David Hendricksfe05e742015-08-15 17:29:39 -07001097 dediprog_set_leds(LED_NONE);
stepan4c06de72011-01-28 09:00:15 +00001098
hailfingerdfb32a02010-01-19 11:15:48 +00001099 return 0;
1100}
David Hendricksfe05e742015-08-15 17:29:39 -07001101