blob: bc7f0974b96f148323ddfcb2a4407c2dac36a658 [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.
hailfingerdfb32a02010-01-19 11:15:48 +000016 */
17
Patrick Georgibd31cdd2017-02-03 19:16:12 +010018#include "platform.h"
David Hendricksfe05e742015-08-15 17:29:39 -070019
20#include <stdlib.h>
hailfinger7a009082010-11-09 23:30:43 +000021#include <stdio.h>
hailfingerdfb32a02010-01-19 11:15:48 +000022#include <string.h>
David Hendricksfe05e742015-08-15 17:29:39 -070023#include <limits.h>
24#include <errno.h>
25
26#if IS_WINDOWS
27#include <lusb0_usb.h>
28#else
David Hendricksf9ecb452015-11-04 15:40:27 -080029#include <libusb.h>
David Hendricksfe05e742015-08-15 17:29:39 -070030#endif
31
hailfingerdfb32a02010-01-19 11:15:48 +000032#include "flash.h"
David Hendrickscd20ea42015-11-17 22:33:35 -080033#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000034#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000035#include "programmer.h"
hailfingerdfb32a02010-01-19 11:15:48 +000036#include "spi.h"
37
stepan4c06de72011-01-28 09:00:15 +000038#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
hailfingerdfb32a02010-01-19 11:15:48 +000039#define DEFAULT_TIMEOUT 3000
David Hendricksf9ecb452015-11-04 15:40:27 -080040#define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */
41#define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */
42#define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */
43#define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */
44#define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */
45struct libusb_context *usb_ctx;
46static libusb_device_handle *dediprog_handle;
David Hendricksd7468582015-11-12 15:21:12 -080047static int dediprog_in_endpoint;
48static int dediprog_out_endpoint;
David Hendricksf9ecb452015-11-04 15:40:27 -080049static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
hailfingerdfb32a02010-01-19 11:15:48 +000050
David Hendricks6702e072015-08-15 18:09:04 -070051enum dediprog_devtype {
52 DEV_UNKNOWN = 0,
53 DEV_SF100 = 100,
54 DEV_SF600 = 600,
55};
56
57enum dediprog_devtype dediprog_devicetype;
58
David Hendricksfe05e742015-08-15 17:29:39 -070059enum dediprog_leds {
60 LED_INVALID = -1,
61 LED_NONE = 0,
Simon Glasse53cc1d2015-01-08 05:48:51 -070062 LED_PASS = 1 << 0,
63 LED_BUSY = 1 << 1,
64 LED_ERROR = 1 << 2,
65 LED_ALL = 7,
66};
67
Simon Glassa1f80682015-01-08 05:55:29 -070068/* IO bits for CMD_SET_IO_LED message */
David Hendricksfe05e742015-08-15 17:29:39 -070069enum dediprog_ios {
Simon Glassa1f80682015-01-08 05:55:29 -070070 IO1 = 1 << 0,
71 IO2 = 1 << 1,
72 IO3 = 1 << 2,
73 IO4 = 1 << 3,
74};
75
David Hendricksfe05e742015-08-15 17:29:39 -070076enum dediprog_cmds {
77 CMD_TRANSCEIVE = 0x01,
78 CMD_POLL_STATUS_REG = 0x02,
79 CMD_SET_VPP = 0x03,
80 CMD_SET_TARGET = 0x04,
81 CMD_READ_EEPROM = 0x05,
82 CMD_WRITE_EEPROM = 0x06,
83 CMD_SET_IO_LED = 0x07,
84 CMD_READ_PROG_INFO = 0x08,
85 CMD_SET_VCC = 0x09,
86 CMD_SET_STANDALONE = 0x0A,
David Hendricks28fbdfb2015-08-15 18:04:37 -070087 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
David Hendricksfe05e742015-08-15 17:29:39 -070088 CMD_GET_BUTTON = 0x11,
89 CMD_GET_UID = 0x12,
90 CMD_SET_CS = 0x14,
91 CMD_IO_MODE = 0x15,
92 CMD_FW_UPDATE = 0x1A,
93 CMD_FPGA_UPDATE = 0x1B,
94 CMD_READ_FPGA_VERSION = 0x1C,
95 CMD_SET_HOLD = 0x1D,
96 CMD_READ = 0x20,
97 CMD_WRITE = 0x30,
98 CMD_WRITE_AT45DB = 0x31,
99 CMD_NAND_WRITE = 0x32,
100 CMD_NAND_READ = 0x33,
101 CMD_SET_SPI_CLK = 0x61,
102 CMD_CHECK_SOCKET = 0x62,
103 CMD_DOWNLOAD_PRJ = 0x63,
104 CMD_READ_PRJ_NAME = 0x64,
105 // New protocol/firmware only
106 CMD_CHECK_SDCARD = 0x65,
107 CMD_READ_PRJ = 0x66,
Simon Glassa1f80682015-01-08 05:55:29 -0700108};
109
David Hendricksfe05e742015-08-15 17:29:39 -0700110enum dediprog_target {
111 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
112 FLASH_TYPE_FLASH_CARD,
113 FLASH_TYPE_APPLICATION_FLASH_2,
114 FLASH_TYPE_SOCKET,
Simon Glassa1f80682015-01-08 05:55:29 -0700115};
116
David Hendricksfe05e742015-08-15 17:29:39 -0700117enum dediprog_readmode {
118 READ_MODE_STD = 1,
119 READ_MODE_FAST = 2,
120 READ_MODE_ATMEL45 = 3,
121 READ_MODE_4B_ADDR_FAST = 4,
122 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Simon Glassd01002b2015-01-08 05:44:16 -0700123};
124
David Hendricksfe05e742015-08-15 17:29:39 -0700125enum dediprog_writemode {
126 WRITE_MODE_PAGE_PGM = 1,
127 WRITE_MODE_PAGE_WRITE = 2,
128 WRITE_MODE_1B_AAI = 3,
129 WRITE_MODE_2B_AAI = 4,
130 WRITE_MODE_128B_PAGE = 5,
131 WRITE_MODE_PAGE_AT26DF041 = 6,
132 WRITE_MODE_SILICON_BLUE_FPGA = 7,
133 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of length 512 bytes */
134 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
135 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of length 512 bytes */
136 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
137 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
Simon Glassd01002b2015-01-08 05:44:16 -0700138};
139
David Hendrickscf453d82015-11-16 20:19:57 -0800140enum dediprog_standalone_mode {
141 ENTER_STANDALONE_MODE = 0,
142 LEAVE_STANDALONE_MODE = 1,
143};
144
David Hendricksf9ecb452015-11-04 15:40:27 -0800145#ifndef LIBUSB_HAVE_ERROR_NAME
146/* Quick and dirty replacement for missing libusb_error_name in older libusb 1.0. */
147const char *libusb_error_name(int error_code)
148{
149 /* 18 chars for text, rest for number, sign, nullbyte. */
150 static char my_libusb_error[18 + 6];
David Hendricksfe05e742015-08-15 17:29:39 -0700151
David Hendricksf9ecb452015-11-04 15:40:27 -0800152 sprintf(my_libusb_error, "libusb error code %i", error_code);
153 return my_libusb_error;
154}
155#endif
David Hendricksfe05e742015-08-15 17:29:39 -0700156
David Hendricks14e82e52015-08-15 17:59:03 -0700157/* Returns true if firmware (and thus hardware) supports the "new" protocol */
158static int is_new_prot(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000159{
David Hendricks6702e072015-08-15 18:09:04 -0700160 switch (dediprog_devicetype) {
161 case DEV_SF100:
162 return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
163 case DEV_SF600:
164 return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0);
165 default:
166 return 0;
167 }
hailfingerdfb32a02010-01-19 11:15:48 +0000168}
David Hendricks14e82e52015-08-15 17:59:03 -0700169
David Hendricksf9ecb452015-11-04 15:40:27 -0800170struct dediprog_transfer_status {
171 int error; /* OK if 0, ERROR else */
172 unsigned int queued_idx;
173 unsigned int finished_idx;
174};
175
176static void dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
177{
178 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
179 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
180 status->error = 1;
181 msg_perr("SPI bulk read failed!\n");
182 }
183 ++status->finished_idx;
184}
185
186static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
187{
188 if (status->finished_idx >= status->queued_idx)
189 return 0;
190
191 do {
192 struct timeval timeout = { 10, 0 };
193 const int ret = libusb_handle_events_timeout_completed(usb_ctx, &timeout, NULL);
194 if (ret < 0) {
195 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
196 return 1;
197 }
198 } while (finish && (status->finished_idx < status->queued_idx));
199 return 0;
200}
201
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100202static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
David Hendricks14e82e52015-08-15 17:59:03 -0700203{
David Hendricksf9ecb452015-11-04 15:40:27 -0800204 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
205 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700206}
207
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100208static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
David Hendricks14e82e52015-08-15 17:59:03 -0700209{
David Hendricksf9ecb452015-11-04 15:40:27 -0800210 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
211 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700212}
hailfingerdfb32a02010-01-19 11:15:48 +0000213
David Hendricksa7f99ac2015-11-12 16:42:03 -0800214static int dediprog_read_other(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
215{
216 return libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, cmd, value, idx,
217 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
218}
David Hendricksf9ecb452015-11-04 15:40:27 -0800219
220#if 0
hailfinger1ff33dc2010-07-03 11:02:10 +0000221/* Might be useful for other USB devices as well. static for now. */
David Hendricksfe05e742015-08-15 17:29:39 -0700222/* device parameter allows user to specify one device of multiple installed */
223static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device)
hailfingerdfb32a02010-01-19 11:15:48 +0000224{
225 struct usb_bus *bus;
226 struct usb_device *dev;
227
228 for (bus = usb_get_busses(); bus; bus = bus->next)
229 for (dev = bus->devices; dev; dev = dev->next)
230 if ((dev->descriptor.idVendor == vid) &&
David Hendricksfe05e742015-08-15 17:29:39 -0700231 (dev->descriptor.idProduct == pid)) {
232 if (device == 0)
233 return dev;
234 device--;
235 }
hailfingerdfb32a02010-01-19 11:15:48 +0000236
237 return NULL;
238}
David Hendricksf9ecb452015-11-04 15:40:27 -0800239#endif
240
241/* Might be useful for other USB devices as well. static for now. */
242/* device parameter allows user to specify one device of multiple installed */
243static struct libusb_device_handle *get_device_by_vid_pid_number(uint16_t vid, uint16_t pid, unsigned int num)
244{
245 struct libusb_device **list;
246 ssize_t i = 0;
247 int err = 0;
248 struct libusb_device_handle *handle = NULL;
249 struct libusb_device_descriptor desc = {};
250 ssize_t count = libusb_get_device_list(usb_ctx, &list);
251
252 if (count < 0) {
253 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
254 return NULL;
255 }
256
257 for (i = 0; i < count; i++) {
258 struct libusb_device *dev = list[i];
259 err = libusb_get_device_descriptor(dev, &desc);
260 if (err != 0) {
261 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
262 libusb_free_device_list(list, 1);
263 return NULL;
264 }
265 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
266 msg_pdbg("Found USB device %04hx:%04hx at address %hhx-%hhx.\n", desc.idVendor,
267 desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev));
268 if (num == 0) {
269 err = libusb_open(dev, &handle);
270 if (err != 0) {
271 msg_perr("Opening the USB device failed (%s)!\n",
272 libusb_error_name(err));
273 libusb_free_device_list(list, 1);
274 return NULL;
275 }
276 break;
277 }
278 num--;
279 }
280 }
281 libusb_free_device_list(list, 1);
282
283 return handle;
284}
hailfingerdfb32a02010-01-19 11:15:48 +0000285
David Hendricksfe05e742015-08-15 17:29:39 -0700286/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
stepan4c06de72011-01-28 09:00:15 +0000287static int dediprog_set_leds(int leds)
288{
David Hendricksfe05e742015-08-15 17:29:39 -0700289 if (leds < LED_NONE || leds > LED_ALL)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700290 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000291
David Hendricks14e82e52015-08-15 17:59:03 -0700292 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
293 * them around if we have an old device. On those devices the LEDs map as follows:
stepan4c06de72011-01-28 09:00:15 +0000294 * bit 2 == 0: green light is on.
David Hendricks14e82e52015-08-15 17:59:03 -0700295 * bit 0 == 0: red light is on.
296 *
297 * Additionally, the command structure has changed with the "new" protocol.
298 *
299 * FIXME: take IO pins into account
stepan4c06de72011-01-28 09:00:15 +0000300 */
David Hendricks14e82e52015-08-15 17:59:03 -0700301 int target_leds, ret;
302 if (is_new_prot()) {
303 target_leds = (leds ^ 7) << 8;
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100304 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
stepan4c06de72011-01-28 09:00:15 +0000305 } else {
David Hendricks14e82e52015-08-15 17:59:03 -0700306 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
307 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
308 } else {
309 target_leds = leds;
310 }
311 target_leds ^= 7;
312
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100313 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Simon Glass543101a2015-01-08 06:28:13 -0700314 }
David Hendricksfe05e742015-08-15 17:29:39 -0700315
stepan4c06de72011-01-28 09:00:15 +0000316 if (ret != 0x0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800317 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
stepan4c06de72011-01-28 09:00:15 +0000318 return 1;
319 }
320
stepan4c06de72011-01-28 09:00:15 +0000321 return 0;
322}
323
David Hendricksfe05e742015-08-15 17:29:39 -0700324struct dediprog_spispeeds {
325 const char *const name;
326 const int speed;
327};
328
329static const struct dediprog_spispeeds spispeeds[] = {
330 { "24M", 0x0 },
331 { "12M", 0x2 },
332 { "8M", 0x1 },
333 { "3M", 0x3 },
334 { "2.18M", 0x4 },
335 { "1.5M", 0x5 },
336 { "750k", 0x6 },
337 { "375k", 0x7 },
338 { NULL, 0x0 },
339};
340
341static int dediprog_set_spi_speed(unsigned int spispeed_idx)
hailfingerdfb32a02010-01-19 11:15:48 +0000342{
David Hendricksfe05e742015-08-15 17:29:39 -0700343 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100344 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
David Hendricksfe05e742015-08-15 17:29:39 -0700345 return 0;
346 }
hailfingerdfb32a02010-01-19 11:15:48 +0000347
David Hendricksfe05e742015-08-15 17:29:39 -0700348 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
349 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
hailfingerdfb32a02010-01-19 11:15:48 +0000350
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100351 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000352 if (ret != 0x0) {
David Hendricksfe05e742015-08-15 17:29:39 -0700353 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
hailfingerdfb32a02010-01-19 11:15:48 +0000354 return 1;
355 }
356 return 0;
357}
358
hailfingerfb6287d2010-11-16 21:25:29 +0000359/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
360 * @start start address
361 * @len length
362 * @return 0 on success, 1 on failure
363 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700364static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000365 unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000366{
David Hendricksf9ecb452015-11-04 15:40:27 -0800367 int ret, err = 1;
stefanctc5eb8a92011-11-23 09:13:48 +0000368 unsigned int i;
hailfingerfb6287d2010-11-16 21:25:29 +0000369 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000370 const unsigned int chunksize = 0x200;
371 const unsigned int count = len / chunksize;
David Hendricks14e82e52015-08-15 17:59:03 -0700372 unsigned int cmd_len;
David Hendricksf9ecb452015-11-04 15:40:27 -0800373 struct dediprog_transfer_status status = { 0, 0, 0 };
374 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
375 struct libusb_transfer *transfer;
hailfingerfb6287d2010-11-16 21:25:29 +0000376
377 if ((start % chunksize) || (len % chunksize)) {
378 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
379 "at flashrom@flashrom.org\n", __func__, start, len);
380 return 1;
381 }
382
383 /* No idea if the hardware can handle empty reads, so chicken out. */
384 if (!len)
385 return 0;
David Hendricks14e82e52015-08-15 17:59:03 -0700386 /* Command Read SPI Bulk. */
387 if (is_new_prot()) {
388 const uint8_t read_cmd[] = {
389 count & 0xff,
390 (count >> 8) & 0xff,
391 0,
392 READ_MODE_FAST,
393 0,
394 0,
395 start & 0xff,
396 (start >> 8) & 0xff,
397 (start >> 16) & 0xff,
398 (start >> 24) & 0xff,
399 };
400
401 cmd_len = sizeof(read_cmd);
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100402 ret = dediprog_write(CMD_READ, 0, 0, read_cmd, cmd_len);
David Hendricks14e82e52015-08-15 17:59:03 -0700403 } else {
404 const uint8_t read_cmd[] = {count & 0xff,
405 (count >> 8) & 0xff,
406 chunksize & 0xff,
407 (chunksize >> 8) & 0xff};
408
409 cmd_len = sizeof(read_cmd);
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100410 ret = dediprog_write(CMD_READ, start % 0x10000, start / 0x10000, read_cmd, cmd_len);
David Hendricks14e82e52015-08-15 17:59:03 -0700411 }
412 if (ret != cmd_len) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800413 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
hailfingerfb6287d2010-11-16 21:25:29 +0000414 return 1;
415 }
416
David Hendricksf9ecb452015-11-04 15:40:27 -0800417 /*
418 * Ring buffer of bulk transfers.
419 * Poll until at least one transfer is ready,
420 * schedule next transfers until buffer is full.
421 */
hailfingerfb6287d2010-11-16 21:25:29 +0000422
David Hendricksf9ecb452015-11-04 15:40:27 -0800423 /* Allocate bulk transfers. */
424 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
425 transfers[i] = libusb_alloc_transfer(0);
426 if (!transfers[i]) {
427 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
428 goto _err_free;
429 }
430 }
431
432 /* Now transfer requested chunks using libusb's asynchronous interface. */
433 while (!status.error && (status.queued_idx < count)) {
434 while ((status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS) {
435 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
436 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
437 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
438 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
439 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
440 ret = libusb_submit_transfer(transfer);
441 if (ret < 0) {
442 msg_perr("Submitting SPI bulk read %i failed: %i %s!\n",
443 status.queued_idx, ret, libusb_error_name(ret));
444 goto _err_free;
445 }
446 ++status.queued_idx;
447 }
448 if (dediprog_bulk_read_poll(&status, 0))
449 goto _err_free;
450 }
451 /* Wait for transfers to finish. */
452 if (dediprog_bulk_read_poll(&status, 1))
453 goto _err_free;
454 /* Check if everything has been transmitted. */
455 if ((status.finished_idx < count) || status.error)
456 goto _err_free;
457
458 err = 0;
459
460_err_free:
461 dediprog_bulk_read_poll(&status, 1);
462 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
463 if (transfers[i]) libusb_free_transfer(transfers[i]);
464 return err;
hailfingerfb6287d2010-11-16 21:25:29 +0000465}
466
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700467static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000468 unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000469{
hailfingerfb6287d2010-11-16 21:25:29 +0000470 int ret;
471 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000472 const unsigned int chunksize = 0x200;
473 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
474 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000475
Simon Glasse53cc1d2015-01-08 05:48:51 -0700476 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000477
hailfingerfb6287d2010-11-16 21:25:29 +0000478 if (residue) {
479 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
480 start, residue);
481 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700482 if (ret)
483 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000484 }
485
486 /* Round down. */
487 bulklen = (len - residue) / chunksize * chunksize;
488 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
489 bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700490 if (ret)
491 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000492
493 len -= residue + bulklen;
494 if (len) {
495 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
496 start, len);
497 ret = spi_read_chunked(flash, buf + residue + bulklen,
498 start + residue + bulklen, len, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700499 if (ret)
500 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000501 }
502
Simon Glasse53cc1d2015-01-08 05:48:51 -0700503 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000504 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700505err:
506 dediprog_set_leds(LED_ERROR);
507 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000508}
509
David Hendricksfe05e742015-08-15 17:29:39 -0700510/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
511 * @chunksize length of data chunks, only 256 supported by now
512 * @start start address
513 * @len length
514 * @dedi_spi_cmd dediprog specific write command for spi bus
515 * @return 0 on success, 1 on failure
516 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700517static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
David Hendricksfe05e742015-08-15 17:29:39 -0700518 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
hailfinger556e9c32010-11-23 21:28:16 +0000519{
David Hendricksf9ecb452015-11-04 15:40:27 -0800520 int ret, transferred;
David Hendricksfe05e742015-08-15 17:29:39 -0700521 unsigned int i;
522 /* USB transfer size must be 512, other sizes will NOT work at all.
523 * chunksize is the real data size per USB bulk transfer. The remaining
524 * space in a USB bulk transfer must be filled with 0xff padding.
525 */
526 const unsigned int count = len / chunksize;
David Hendricksf9ecb452015-11-04 15:40:27 -0800527 const unsigned char count_and_cmd_old[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
528 const unsigned char count_and_cmd_new[] = {
David Hendricks741efe22015-08-15 19:18:51 -0700529 count & 0xff,
530 (count >> 8) & 0xff,
531 0, /* used for 24-bit address support? */
532 dedi_spi_cmd,
533 JEDEC_BYTE_PROGRAM, /* FIXME: needs to be determined based on byte 3? */
534 0,
535 start & 0xff,
536 (start >> 8) & 0xff,
537 (start >> 16) & 0xff,
538 (start >> 24) & 0xff,
539 };
David Hendricksf9ecb452015-11-04 15:40:27 -0800540 unsigned char usbbuf[512];
David Hendricksfe05e742015-08-15 17:29:39 -0700541
542 /*
543 * We should change this check to
544 * chunksize > 512
545 * once we know how to handle different chunk sizes.
546 */
547 if (chunksize != 256) {
548 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
549 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
550 return 1;
551 }
552
553 if ((start % chunksize) || (len % chunksize)) {
554 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
555 "at flashrom@flashrom.org\n", __func__, start, len);
556 return 1;
557 }
558
559 /* No idea if the hardware can handle empty writes, so chicken out. */
560 if (!len)
561 return 0;
David Hendricks741efe22015-08-15 19:18:51 -0700562 if (!is_new_prot()) {
563 /* Command Write SPI Bulk. No idea which write command is used on the
564 * SPI side.
565 */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100566 ret = dediprog_write(CMD_WRITE, start % 0x10000, start / 0x10000,
David Hendricksf9ecb452015-11-04 15:40:27 -0800567 count_and_cmd_old, sizeof(count_and_cmd_old));
David Hendricks741efe22015-08-15 19:18:51 -0700568 if (ret != sizeof(count_and_cmd_old)) {
569 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
David Hendricksf9ecb452015-11-04 15:40:27 -0800570 libusb_error_name(ret));
David Hendricks741efe22015-08-15 19:18:51 -0700571 return 1;
572 }
573 } else {
574 /* Command Write SPI Bulk. No idea which write command is used on the
575 * SPI side.
576 */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100577 ret = dediprog_write(CMD_WRITE, 0, 0,
David Hendricksf9ecb452015-11-04 15:40:27 -0800578 count_and_cmd_new, sizeof(count_and_cmd_new));
David Hendricks741efe22015-08-15 19:18:51 -0700579 if (ret != sizeof(count_and_cmd_new)) {
580 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
David Hendricksf9ecb452015-11-04 15:40:27 -0800581 libusb_error_name(ret));
David Hendricks741efe22015-08-15 19:18:51 -0700582 return 1;
583 }
David Hendricksfe05e742015-08-15 17:29:39 -0700584 }
585
586 for (i = 0; i < count; i++) {
587 memset(usbbuf, 0xff, sizeof(usbbuf));
588 memcpy(usbbuf, buf + i * chunksize, chunksize);
David Hendricksf9ecb452015-11-04 15:40:27 -0800589 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint,
590 usbbuf, 512, &transferred, DEFAULT_TIMEOUT);
591 if ((ret < 0) || (transferred != 512)) {
592 msg_perr("SPI bulk write failed, expected %i, got %i %s!\n",
593 512, ret, libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700594 return 1;
595 }
596 }
597
598 return 0;
599}
600
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700601static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
David Hendricksfe05e742015-08-15 17:29:39 -0700602 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
603{
604 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100605 const unsigned int chunksize = flash->chip->page_size;
David Hendricksfe05e742015-08-15 17:29:39 -0700606 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
607 unsigned int bulklen;
stepan4c06de72011-01-28 09:00:15 +0000608
Simon Glasse53cc1d2015-01-08 05:48:51 -0700609 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000610
David Hendricksfe05e742015-08-15 17:29:39 -0700611 if (chunksize != 256) {
612 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
613 "we don't know how dediprog\nhandles them.\n");
614 /* Write everything like it was residue. */
615 residue = len;
616 }
stepan4c06de72011-01-28 09:00:15 +0000617
David Hendricksfe05e742015-08-15 17:29:39 -0700618 if (residue) {
619 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
620 start, residue);
621 /* No idea about the real limit. Maybe 12, maybe more. */
622 ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 12);
623 if (ret) {
624 dediprog_set_leds(LED_ERROR);
625 return ret;
626 }
627 }
628
629 /* Round down. */
630 bulklen = (len - residue) / chunksize * chunksize;
631 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
632 if (ret) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700633 dediprog_set_leds(LED_ERROR);
David Hendricksfe05e742015-08-15 17:29:39 -0700634 return ret;
635 }
stepan4c06de72011-01-28 09:00:15 +0000636
David Hendricksfe05e742015-08-15 17:29:39 -0700637 len -= residue + bulklen;
638 if (len) {
639 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
640 start, len);
641 ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen),
642 start + residue + bulklen, len, 12);
643 if (ret) {
644 dediprog_set_leds(LED_ERROR);
645 return ret;
646 }
647 }
648
649 dediprog_set_leds(LED_PASS);
650 return 0;
hailfinger556e9c32010-11-23 21:28:16 +0000651}
652
Patrick Georgiab8353e2017-02-03 18:32:01 +0100653static int dediprog_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
David Hendricksfe05e742015-08-15 17:29:39 -0700654{
655 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
656}
657
658#if 0
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700659static int dediprog_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
David Hendricksfe05e742015-08-15 17:29:39 -0700660{
661 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
662}
663#endif
664
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700665//static int dediprog_spi_send_command(struct flashctx *flash,
666static int dediprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
David Hendricksfe05e742015-08-15 17:29:39 -0700667 unsigned int readcnt,
668 const unsigned char *writearr,
669 unsigned char *readarr)
hailfingerdfb32a02010-01-19 11:15:48 +0000670{
671 int ret;
672
hailfingeraf389cc2010-01-22 02:53:30 +0000673 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
David Hendricksfe05e742015-08-15 17:29:39 -0700674 if (writecnt > UINT16_MAX) {
675 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
676 return 1;
Simon Glass543101a2015-01-08 06:28:13 -0700677 }
David Hendricksfe05e742015-08-15 17:29:39 -0700678 if (readcnt > UINT16_MAX) {
679 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
680 return 1;
681 }
682
David Hendricks14e82e52015-08-15 17:59:03 -0700683 /* New protocol has the read flag as value while the old protocol had it in the index field. */
684 if (is_new_prot()) {
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100685 ret = dediprog_write(CMD_TRANSCEIVE, readcnt ? 1 : 0, 0, writearr, writecnt);
David Hendricks14e82e52015-08-15 17:59:03 -0700686 } else {
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100687 ret = dediprog_write(CMD_TRANSCEIVE, 0, readcnt ? 1 : 0, writearr, writecnt);
David Hendricks14e82e52015-08-15 17:59:03 -0700688 }
hailfingerdfb32a02010-01-19 11:15:48 +0000689 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000690 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800691 writecnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000692 return 1;
693 }
David Hendricksfe05e742015-08-15 17:29:39 -0700694 if (readcnt == 0)
hailfingerdfb32a02010-01-19 11:15:48 +0000695 return 0;
David Hendricksfe05e742015-08-15 17:29:39 -0700696
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100697 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000698 if (ret != readcnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000699 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800700 readcnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000701 return 1;
702 }
703 return 0;
704}
705
hailfinger1ff33dc2010-07-03 11:02:10 +0000706static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000707{
708 int ret;
hailfinger7a009082010-11-09 23:30:43 +0000709 int fw[3];
David Hendricks6702e072015-08-15 18:09:04 -0700710 int sfnum;
hailfingerdfb32a02010-01-19 11:15:48 +0000711 char buf[0x11];
712
hailfingerdfb32a02010-01-19 11:15:48 +0000713 /* Command Receive Device String. */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100714 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000715 if (ret != 0x10) {
716 msg_perr("Incomplete/failed Command Receive Device String!\n");
717 return 1;
718 }
719 buf[0x10] = '\0';
720 msg_pdbg("Found a %s\n", buf);
David Hendricks6702e072015-08-15 18:09:04 -0700721 if (memcmp(buf, "SF100", 0x5) == 0)
722 dediprog_devicetype = DEV_SF100;
723 else if (memcmp(buf, "SF600", 0x5) == 0)
724 dediprog_devicetype = DEV_SF600;
725 else {
David Hendricks22593802015-11-13 12:59:30 -0800726 msg_perr("Device not a SF100 or SF600!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000727 return 1;
728 }
David Hendricks6702e072015-08-15 18:09:04 -0700729 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2])
730 != 4 || sfnum != dediprog_devicetype) {
David Hendricks14e82e52015-08-15 17:59:03 -0700731 msg_perr("Unexpected firmware version string '%s'\n", buf);
hailfinger7a009082010-11-09 23:30:43 +0000732 return 1;
733 }
David Hendricks14e82e52015-08-15 17:59:03 -0700734 /* Only these major versions were tested. */
David Hendricks6702e072015-08-15 18:09:04 -0700735 if (fw[0] < 2 || fw[0] > 7) {
David Hendricks14e82e52015-08-15 17:59:03 -0700736 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000737 return 1;
738 }
stepan4c06de72011-01-28 09:00:15 +0000739 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
David Hendricks14e82e52015-08-15 17:59:03 -0700740
hailfingerdfb32a02010-01-19 11:15:48 +0000741 return 0;
742}
743
David Hendricks91ca7ac2015-11-20 16:22:22 -0800744static int dediprog_supply_voltages[] = {
745 0, 1800, 2500, 3500,
746};
747
748static int dediprog_set_spi_flash_voltage_manual(int millivolt)
749{
750 int ret;
751 uint16_t voltage_selector;
752
753 switch (millivolt) {
754 case 0:
755 /* Admittedly this one is an assumption. */
756 voltage_selector = 0x0;
757 break;
758 case 1800:
759 voltage_selector = 0x12;
760 break;
761 case 2500:
762 voltage_selector = 0x11;
763 break;
764 case 3500:
765 voltage_selector = 0x10;
766 break;
767 default:
768 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
769 return 1;
770 }
771 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
772 millivolt % 1000);
773
774 if (voltage_selector == 0) {
775 /* Wait some time as the original driver does. */
776 programmer_delay(200 * 1000);
777 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100778 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
David Hendricks91ca7ac2015-11-20 16:22:22 -0800779 if (ret != 0x0) {
780 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
781 voltage_selector);
782 return 1;
783 }
784 if (voltage_selector != 0) {
785 /* Wait some time as the original driver does. */
786 programmer_delay(200 * 1000);
787 }
788 return 0;
789}
790
David Hendricks93784b42016-08-09 17:00:38 -0700791static int dediprog_set_spi_flash_voltage_auto(void)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800792{
David Hendricksac1d25c2016-08-09 17:00:58 -0700793 int i;
David Hendricks91ca7ac2015-11-20 16:22:22 -0800794 int spi_flash_ranges;
795
796 spi_flash_ranges = flash_supported_voltage_ranges(BUS_SPI);
797 if (spi_flash_ranges < 0)
798 return -1;
799
800 for (i = 0; i < ARRAY_SIZE(dediprog_supply_voltages); i++) {
801 int j;
802 int v = dediprog_supply_voltages[i]; /* shorthand */
803
804 for (j = 0; j < spi_flash_ranges; j++) {
805 /* Dediprog can supply a voltage in this range. */
806 if ((v >= voltage_ranges[j].min) && (v <= voltage_ranges[j].max)) {
David Hendricks93784b42016-08-09 17:00:38 -0700807 struct flashctx dummy;
808
David Hendricks91ca7ac2015-11-20 16:22:22 -0800809 msg_cdbg("%s: trying %d\n", __func__, v);
810 if (dediprog_set_spi_flash_voltage_manual(v)) {
811 msg_cerr("%s: Failed to set SPI voltage\n", __func__);
812 return 1;
813 }
814
815 clear_spi_id_cache();
David Hendricksac1d25c2016-08-09 17:00:58 -0700816 if (probe_flash(0, &dummy, 0) < 0) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800817 /* No dice, try next voltage supported by Dediprog. */
818 break;
819 }
820
Patrick Georgif3fa2992017-02-02 16:24:44 +0100821 if ((dummy.chip->manufacture_id == GENERIC_MANUF_ID) ||
822 (dummy.chip->model_id == GENERIC_DEVICE_ID)) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800823 break;
824 }
825
826 return 0;
827 }
828 }
829 }
830
831 return 1;
832}
833
834/* FIXME: ugly function signature */
David Hendricks93784b42016-08-09 17:00:38 -0700835static int dediprog_set_spi_voltage(int millivolt, int probe)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800836{
837 if (probe)
David Hendricks93784b42016-08-09 17:00:38 -0700838 return dediprog_set_spi_flash_voltage_auto();
David Hendricks91ca7ac2015-11-20 16:22:22 -0800839 else
840 return dediprog_set_spi_flash_voltage_manual(millivolt);
841}
842
David Hendricks28fbdfb2015-08-15 18:04:37 -0700843/*
844 * This command presumably sets the voltage for the SF100 itself (not the
845 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
846 * (including all SF600s) do not support it.
847 */
848static int dediprog_set_voltage(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000849{
850 int ret;
David Hendricksf9ecb452015-11-04 15:40:27 -0800851 unsigned char buf[0x1];
hailfingerdfb32a02010-01-19 11:15:48 +0000852
853 memset(buf, 0, sizeof(buf));
David Hendricksa7f99ac2015-11-12 16:42:03 -0800854 ret = dediprog_read_other(CMD_SET_VOLTAGE, 0x0, 0x0, buf, 0x1);
hailfinger7a009082010-11-09 23:30:43 +0000855 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800856 msg_perr("Command A failed (%s)!\n", libusb_error_name(ret));
hailfinger7a009082010-11-09 23:30:43 +0000857 return 1;
858 }
hailfingerdfb32a02010-01-19 11:15:48 +0000859 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -0700860 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000861 return 1;
862 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100863
hailfingerdfb32a02010-01-19 11:15:48 +0000864 return 0;
865}
866
David Hendrickscf453d82015-11-16 20:19:57 -0800867static int dediprog_leave_standalone_mode(void)
868{
869 int ret;
870
871 if (dediprog_devicetype != DEV_SF600)
872 return 0;
873
874 msg_pdbg("Leaving standalone mode\n");
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100875 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Hendrickscf453d82015-11-16 20:19:57 -0800876 if (ret) {
877 msg_perr("Failed to leave standalone mode (%s)!\n", libusb_error_name(ret));
878 return 1;
879 }
880
881 return 0;
882}
883
David Hendricksfe05e742015-08-15 17:29:39 -0700884static int set_target_flash(enum dediprog_target target)
885{
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100886 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
David Hendricksfe05e742015-08-15 17:29:39 -0700887 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800888 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000889 return 1;
890 }
891 return 0;
892}
893
David Hendricksfe05e742015-08-15 17:29:39 -0700894#if 0
895/* Returns true if the button is currently pressed. */
896static bool dediprog_get_button(void)
Simon Glassd01002b2015-01-08 05:44:16 -0700897{
David Hendricksfe05e742015-08-15 17:29:39 -0700898 char buf[1];
899 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
900 buf, 0x1, DEFAULT_TIMEOUT);
901 if (ret != 0) {
902 msg_perr("Could not get button state (%s)!\n", usb_strerror());
903 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700904 }
David Hendricksfe05e742015-08-15 17:29:39 -0700905 return buf[0] != 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700906}
David Hendricksfe05e742015-08-15 17:29:39 -0700907#endif
Simon Glassd01002b2015-01-08 05:44:16 -0700908
hailfingerf76cc322010-11-09 22:00:31 +0000909static int parse_voltage(char *voltage)
910{
911 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000912 int i;
913 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000914
915 if (!voltage || !strlen(voltage)) {
916 msg_perr("Empty voltage= specified.\n");
917 return -1;
918 }
919 millivolt = (int)strtol(voltage, &tmp, 0);
920 voltage = tmp;
921 /* Handle "," and "." as decimal point. Everything after it is assumed
922 * to be in decimal notation.
923 */
924 if ((*voltage == '.') || (*voltage == ',')) {
925 voltage++;
926 for (i = 0; i < 3; i++) {
927 fraction *= 10;
928 /* Don't advance if the current character is invalid,
929 * but continue multiplying.
930 */
931 if ((*voltage < '0') || (*voltage > '9'))
932 continue;
933 fraction += *voltage - '0';
934 voltage++;
935 }
936 /* Throw away remaining digits. */
937 voltage += strspn(voltage, "0123456789");
938 }
939 /* The remaining string must be empty or "mV" or "V". */
940 tolower_string(voltage);
941
942 /* No unit or "V". */
943 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
944 millivolt *= 1000;
945 millivolt += fraction;
946 } else if (!strncmp(voltage, "mv", 2) ||
947 !strncmp(voltage, "milliv", 6)) {
948 /* No adjustment. fraction is discarded. */
949 } else {
950 /* Garbage at the end of the string. */
951 msg_perr("Garbage voltage= specified.\n");
952 return -1;
953 }
954 return millivolt;
955}
956
David Hendricksfe05e742015-08-15 17:29:39 -0700957#if 0
958static const struct spi_master spi_master_dediprog = {
959 .type = SPI_CONTROLLER_DEDIPROG,
960 .max_data_read = MAX_DATA_UNSPECIFIED,
961 .max_data_write = MAX_DATA_UNSPECIFIED,
962 .command = dediprog_spi_send_command,
963 .multicommand = default_spi_send_multicommand,
964 .read = dediprog_spi_read,
965 .write_256 = dediprog_spi_write_256,
966 .write_aai = dediprog_spi_write_aai,
967};
968#endif
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100969static const struct spi_master spi_master_dediprog = {
uwe8d342eb2011-07-28 08:13:25 +0000970 .type = SPI_CONTROLLER_DEDIPROG,
971 .max_data_read = MAX_DATA_UNSPECIFIED,
972 .max_data_write = MAX_DATA_UNSPECIFIED,
973 .command = dediprog_spi_send_command,
974 .multicommand = default_spi_send_multicommand,
975 .read = dediprog_spi_read,
976 .write_256 = dediprog_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000977};
978
David Hendricks93784b42016-08-09 17:00:38 -0700979static int dediprog_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000980{
981 msg_pspew("%s\n", __func__);
982
David Hendricksfe05e742015-08-15 17:29:39 -0700983 dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Hendricks6702e072015-08-15 18:09:04 -0700984 dediprog_devicetype = DEV_UNKNOWN;
David Hendricksfe05e742015-08-15 17:29:39 -0700985
dhendrix0ffc2eb2011-06-14 01:35:36 +0000986 /* URB 28. Command Set SPI Voltage to 0. */
David Hendricks93784b42016-08-09 17:00:38 -0700987 if (dediprog_set_spi_voltage(0x0, 0))
dhendrix0ffc2eb2011-06-14 01:35:36 +0000988 return 1;
989
David Hendricksf9ecb452015-11-04 15:40:27 -0800990 if (libusb_release_interface(dediprog_handle, 0)) {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000991 msg_perr("Could not release USB interface!\n");
992 return 1;
993 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800994 libusb_close(dediprog_handle);
995 libusb_exit(usb_ctx);
996
dhendrix0ffc2eb2011-06-14 01:35:36 +0000997 return 0;
998}
999
hailfingerdfb32a02010-01-19 11:15:48 +00001000/* URB numbers refer to the first log ever captured. */
David Hendricksac1d25c2016-08-09 17:00:58 -07001001int dediprog_init(void)
hailfingerdfb32a02010-01-19 11:15:48 +00001002{
David Hendricks98b3c572016-11-30 01:50:08 +00001003 char *voltage, *device, *spispeed, *target_str;
David Hendricksfe05e742015-08-15 17:29:39 -07001004 int spispeed_idx = 1;
David Hendrickscd20ea42015-11-17 22:33:35 -08001005 int millivolt = 0;
David Hendricksfe05e742015-08-15 17:29:39 -07001006 long usedevice = 0;
1007 long target = 1;
David Hendricks98b3c572016-11-30 01:50:08 +00001008 int i, ret;
hailfingerdfb32a02010-01-19 11:15:48 +00001009
1010 msg_pspew("%s\n", __func__);
1011
David Hendricksfe05e742015-08-15 17:29:39 -07001012 spispeed = extract_programmer_param("spispeed");
1013 if (spispeed) {
1014 for (i = 0; spispeeds[i].name; ++i) {
1015 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1016 spispeed_idx = i;
1017 break;
1018 }
1019 }
1020 if (!spispeeds[i].name) {
1021 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
David Hendricks98b3c572016-11-30 01:50:08 +00001022 free(spispeed);
1023 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001024 }
David Hendricks98b3c572016-11-30 01:50:08 +00001025 free(spispeed);
David Hendricksfe05e742015-08-15 17:29:39 -07001026 }
1027
hailfingerf76cc322010-11-09 22:00:31 +00001028 voltage = extract_programmer_param("voltage");
1029 if (voltage) {
1030 millivolt = parse_voltage(voltage);
1031 free(voltage);
David Hendricks98b3c572016-11-30 01:50:08 +00001032 if (millivolt < 0)
1033 return 1;
hailfingerf76cc322010-11-09 22:00:31 +00001034 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1035 }
David Hendricksfe05e742015-08-15 17:29:39 -07001036
1037 device = extract_programmer_param("device");
1038 if (device) {
1039 char *dev_suffix;
1040 errno = 0;
1041 usedevice = strtol(device, &dev_suffix, 10);
1042 if (errno != 0 || device == dev_suffix) {
1043 msg_perr("Error: Could not convert 'device'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001044 free(device);
1045 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001046 }
David Hendricksfe05e742015-08-15 17:29:39 -07001047 if (usedevice < 0 || usedevice > UINT_MAX) {
1048 msg_perr("Error: Value for 'device' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001049 free(device);
1050 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001051 }
1052 if (strlen(dev_suffix) > 0) {
1053 msg_perr("Error: Garbage following 'device' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001054 free(device);
1055 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001056 }
1057 msg_pinfo("Using device %li.\n", usedevice);
Simon Glassd01002b2015-01-08 05:44:16 -07001058 }
David Hendricks98b3c572016-11-30 01:50:08 +00001059 free(device);
David Hendricksfe05e742015-08-15 17:29:39 -07001060
1061 target_str = extract_programmer_param("target");
1062 if (target_str) {
1063 char *target_suffix;
1064 errno = 0;
1065 target = strtol(target_str, &target_suffix, 10);
1066 if (errno != 0 || target_str == target_suffix) {
1067 msg_perr("Error: Could not convert 'target'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001068 free(target_str);
1069 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001070 }
1071 if (target < 1 || target > 2) {
1072 msg_perr("Error: Value for 'target' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001073 free(target_str);
1074 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001075 }
1076 if (strlen(target_suffix) > 0) {
1077 msg_perr("Error: Garbage following 'target' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001078 free(target_str);
1079 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001080 }
1081 msg_pinfo("Using target %li.\n", target);
1082 }
David Hendricks98b3c572016-11-30 01:50:08 +00001083 free(target_str);
hailfingerf76cc322010-11-09 22:00:31 +00001084
hailfingerdfb32a02010-01-19 11:15:48 +00001085 /* Here comes the USB stuff. */
David Hendricksf9ecb452015-11-04 15:40:27 -08001086 libusb_init(&usb_ctx);
1087 if (!usb_ctx) {
1088 msg_perr("Could not initialize libusb!\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001089 return 1;
hailfingerdfb32a02010-01-19 11:15:48 +00001090 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001091 dediprog_handle = get_device_by_vid_pid_number(0x0483, 0xdada, (unsigned int) usedevice);
David Hendricksfe05e742015-08-15 17:29:39 -07001092 if (!dediprog_handle) {
David Hendricks22593802015-11-13 12:59:30 -08001093 msg_perr("Could not find a Dediprog programmer on USB!\n");
David Hendricksf9ecb452015-11-04 15:40:27 -08001094 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001095 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001096 }
David Hendricks98b3c572016-11-30 01:50:08 +00001097 ret = libusb_set_configuration(dediprog_handle, 1);
1098 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001099 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1100 libusb_close(dediprog_handle);
1101 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001102 return 1;
hailfingerfb6287d2010-11-16 21:25:29 +00001103 }
David Hendricks98b3c572016-11-30 01:50:08 +00001104 ret = libusb_claim_interface(dediprog_handle, 0);
1105 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001106 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1107 libusb_close(dediprog_handle);
1108 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001109 return 1;
hailfingerfb6287d2010-11-16 21:25:29 +00001110 }
David Hendricksfe05e742015-08-15 17:29:39 -07001111
David Hendricks98b3c572016-11-30 01:50:08 +00001112 if (register_shutdown(dediprog_shutdown, NULL))
1113 return 1;
dhendrix0ffc2eb2011-06-14 01:35:36 +00001114
David Hendricks28fbdfb2015-08-15 18:04:37 -07001115 /* Try reading the devicestring. If that fails and the device is old
1116 * (FW < 6.0.0) then we need to try the "set voltage" command and then
1117 * attempt to read the devicestring again. */
1118 if (dediprog_check_devicestring()) {
David Hendricks98b3c572016-11-30 01:50:08 +00001119 if (dediprog_set_voltage())
1120 return 1;
1121 if (dediprog_check_devicestring())
1122 return 1;
David Hendricks28fbdfb2015-08-15 18:04:37 -07001123 }
David Hendricksfe05e742015-08-15 17:29:39 -07001124
David Hendricksd7468582015-11-12 15:21:12 -08001125 /* SF100 firmware exposes only one endpoint for in/out, SF600 firmware
1126 exposes separate endpoints for in and out. */
1127 dediprog_in_endpoint = 2;
1128 if (dediprog_devicetype == DEV_SF100)
1129 dediprog_out_endpoint = 2;
1130 else
1131 dediprog_out_endpoint = 1;
1132
David Hendricks93784b42016-08-09 17:00:38 -07001133
David Hendrickse4c73662015-11-20 16:26:07 -08001134 /* Set some LEDs as soon as possible to indicate activity.
David Hendricksfe05e742015-08-15 17:29:39 -07001135 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
1136 * dediprog_setup() has queried the device and set dediprog_firmwareversion. */
David Hendrickse4c73662015-11-20 16:26:07 -08001137 dediprog_set_leds(LED_PASS | LED_BUSY);
David Hendricksfe05e742015-08-15 17:29:39 -07001138
David Hendrickscd20ea42015-11-17 22:33:35 -08001139 /* FIXME: need to do this so buses_supported gets SPI */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +01001140 register_spi_master(&spi_master_dediprog);
David Hendricks93784b42016-08-09 17:00:38 -07001141
David Hendricksfe05e742015-08-15 17:29:39 -07001142 /* Select target/socket, frequency and VCC. */
1143 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
1144 dediprog_set_spi_speed(spispeed_idx) ||
David Hendricks93784b42016-08-09 17:00:38 -07001145 dediprog_set_spi_voltage(millivolt, voltage ? 0 : 1)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -07001146 dediprog_set_leds(LED_ERROR);
David Hendricks98b3c572016-11-30 01:50:08 +00001147 return 1;
stepan4c06de72011-01-28 09:00:15 +00001148 }
hailfingerdfb32a02010-01-19 11:15:48 +00001149
David Hendricks98b3c572016-11-30 01:50:08 +00001150 if (dediprog_leave_standalone_mode())
1151 return 1;
1152
David Hendricks93784b42016-08-09 17:00:38 -07001153
David Hendricksfe05e742015-08-15 17:29:39 -07001154 dediprog_set_leds(LED_NONE);
stepan4c06de72011-01-28 09:00:15 +00001155
David Hendricks98b3c572016-11-30 01:50:08 +00001156 return 0;
hailfingerdfb32a02010-01-19 11:15:48 +00001157}