blob: 30c988efa5eebff8f63c2d0b91f718bbe3a0ec37 [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 Hendrickscf453d82015-11-16 20:19:57 -0800143enum dediprog_standalone_mode {
144 ENTER_STANDALONE_MODE = 0,
145 LEAVE_STANDALONE_MODE = 1,
146};
147
David Hendricksf9ecb452015-11-04 15:40:27 -0800148#ifndef LIBUSB_HAVE_ERROR_NAME
149/* Quick and dirty replacement for missing libusb_error_name in older libusb 1.0. */
150const char *libusb_error_name(int error_code)
151{
152 /* 18 chars for text, rest for number, sign, nullbyte. */
153 static char my_libusb_error[18 + 6];
David Hendricksfe05e742015-08-15 17:29:39 -0700154
David Hendricksf9ecb452015-11-04 15:40:27 -0800155 sprintf(my_libusb_error, "libusb error code %i", error_code);
156 return my_libusb_error;
157}
158#endif
David Hendricksfe05e742015-08-15 17:29:39 -0700159
David Hendricks14e82e52015-08-15 17:59:03 -0700160/* Returns true if firmware (and thus hardware) supports the "new" protocol */
161static int is_new_prot(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000162{
David Hendricks6702e072015-08-15 18:09:04 -0700163 switch (dediprog_devicetype) {
164 case DEV_SF100:
165 return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
166 case DEV_SF600:
167 return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0);
168 default:
169 return 0;
170 }
hailfingerdfb32a02010-01-19 11:15:48 +0000171}
David Hendricks14e82e52015-08-15 17:59:03 -0700172
David Hendricksf9ecb452015-11-04 15:40:27 -0800173struct dediprog_transfer_status {
174 int error; /* OK if 0, ERROR else */
175 unsigned int queued_idx;
176 unsigned int finished_idx;
177};
178
179static void dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
180{
181 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
182 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
183 status->error = 1;
184 msg_perr("SPI bulk read failed!\n");
185 }
186 ++status->finished_idx;
187}
188
189static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
190{
191 if (status->finished_idx >= status->queued_idx)
192 return 0;
193
194 do {
195 struct timeval timeout = { 10, 0 };
196 const int ret = libusb_handle_events_timeout_completed(usb_ctx, &timeout, NULL);
197 if (ret < 0) {
198 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
199 return 1;
200 }
201 } while (finish && (status->finished_idx < status->queued_idx));
202 return 0;
203}
204
David Hendricksa7f99ac2015-11-12 16:42:03 -0800205static int dediprog_read_ep(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
David Hendricks14e82e52015-08-15 17:59:03 -0700206{
David Hendricksf9ecb452015-11-04 15:40:27 -0800207 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
208 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700209}
210
David Hendricksa7f99ac2015-11-12 16:42:03 -0800211static int dediprog_write_ep(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
David Hendricks14e82e52015-08-15 17:59:03 -0700212{
David Hendricksf9ecb452015-11-04 15:40:27 -0800213 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
214 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700215}
hailfingerdfb32a02010-01-19 11:15:48 +0000216
David Hendricksa7f99ac2015-11-12 16:42:03 -0800217static int dediprog_read_other(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
218{
219 return libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, cmd, value, idx,
220 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
221}
David Hendricksf9ecb452015-11-04 15:40:27 -0800222
223#if 0
hailfinger1ff33dc2010-07-03 11:02:10 +0000224/* Might be useful for other USB devices as well. static for now. */
David Hendricksfe05e742015-08-15 17:29:39 -0700225/* device parameter allows user to specify one device of multiple installed */
226static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device)
hailfingerdfb32a02010-01-19 11:15:48 +0000227{
228 struct usb_bus *bus;
229 struct usb_device *dev;
230
231 for (bus = usb_get_busses(); bus; bus = bus->next)
232 for (dev = bus->devices; dev; dev = dev->next)
233 if ((dev->descriptor.idVendor == vid) &&
David Hendricksfe05e742015-08-15 17:29:39 -0700234 (dev->descriptor.idProduct == pid)) {
235 if (device == 0)
236 return dev;
237 device--;
238 }
hailfingerdfb32a02010-01-19 11:15:48 +0000239
240 return NULL;
241}
David Hendricksf9ecb452015-11-04 15:40:27 -0800242#endif
243
244/* Might be useful for other USB devices as well. static for now. */
245/* device parameter allows user to specify one device of multiple installed */
246static struct libusb_device_handle *get_device_by_vid_pid_number(uint16_t vid, uint16_t pid, unsigned int num)
247{
248 struct libusb_device **list;
249 ssize_t i = 0;
250 int err = 0;
251 struct libusb_device_handle *handle = NULL;
252 struct libusb_device_descriptor desc = {};
253 ssize_t count = libusb_get_device_list(usb_ctx, &list);
254
255 if (count < 0) {
256 msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
257 return NULL;
258 }
259
260 for (i = 0; i < count; i++) {
261 struct libusb_device *dev = list[i];
262 err = libusb_get_device_descriptor(dev, &desc);
263 if (err != 0) {
264 msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err));
265 libusb_free_device_list(list, 1);
266 return NULL;
267 }
268 if ((desc.idVendor == vid) && (desc.idProduct == pid)) {
269 msg_pdbg("Found USB device %04hx:%04hx at address %hhx-%hhx.\n", desc.idVendor,
270 desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev));
271 if (num == 0) {
272 err = libusb_open(dev, &handle);
273 if (err != 0) {
274 msg_perr("Opening the USB device failed (%s)!\n",
275 libusb_error_name(err));
276 libusb_free_device_list(list, 1);
277 return NULL;
278 }
279 break;
280 }
281 num--;
282 }
283 }
284 libusb_free_device_list(list, 1);
285
286 return handle;
287}
hailfingerdfb32a02010-01-19 11:15:48 +0000288
David Hendricksfe05e742015-08-15 17:29:39 -0700289/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
stepan4c06de72011-01-28 09:00:15 +0000290static int dediprog_set_leds(int leds)
291{
David Hendricksfe05e742015-08-15 17:29:39 -0700292 if (leds < LED_NONE || leds > LED_ALL)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700293 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000294
David Hendricks14e82e52015-08-15 17:59:03 -0700295 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
296 * them around if we have an old device. On those devices the LEDs map as follows:
stepan4c06de72011-01-28 09:00:15 +0000297 * bit 2 == 0: green light is on.
David Hendricks14e82e52015-08-15 17:59:03 -0700298 * bit 0 == 0: red light is on.
299 *
300 * Additionally, the command structure has changed with the "new" protocol.
301 *
302 * FIXME: take IO pins into account
stepan4c06de72011-01-28 09:00:15 +0000303 */
David Hendricks14e82e52015-08-15 17:59:03 -0700304 int target_leds, ret;
305 if (is_new_prot()) {
306 target_leds = (leds ^ 7) << 8;
David Hendricksa7f99ac2015-11-12 16:42:03 -0800307 ret = dediprog_write_ep(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
stepan4c06de72011-01-28 09:00:15 +0000308 } else {
David Hendricks14e82e52015-08-15 17:59:03 -0700309 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
310 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
311 } else {
312 target_leds = leds;
313 }
314 target_leds ^= 7;
315
David Hendricksa7f99ac2015-11-12 16:42:03 -0800316 ret = dediprog_write_ep(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Simon Glass543101a2015-01-08 06:28:13 -0700317 }
David Hendricksfe05e742015-08-15 17:29:39 -0700318
stepan4c06de72011-01-28 09:00:15 +0000319 if (ret != 0x0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800320 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
stepan4c06de72011-01-28 09:00:15 +0000321 return 1;
322 }
323
stepan4c06de72011-01-28 09:00:15 +0000324 return 0;
325}
326
hailfingerf76cc322010-11-09 22:00:31 +0000327static int dediprog_set_spi_voltage(int millivolt)
hailfingerdfb32a02010-01-19 11:15:48 +0000328{
329 int ret;
hailfingerf76cc322010-11-09 22:00:31 +0000330 uint16_t voltage_selector;
hailfingerdfb32a02010-01-19 11:15:48 +0000331
hailfingerf76cc322010-11-09 22:00:31 +0000332 switch (millivolt) {
333 case 0:
hailfingerdfb32a02010-01-19 11:15:48 +0000334 /* Admittedly this one is an assumption. */
hailfingerf76cc322010-11-09 22:00:31 +0000335 voltage_selector = 0x0;
hailfingerdfb32a02010-01-19 11:15:48 +0000336 break;
hailfingerf76cc322010-11-09 22:00:31 +0000337 case 1800:
338 voltage_selector = 0x12;
hailfingerdfb32a02010-01-19 11:15:48 +0000339 break;
hailfingerf76cc322010-11-09 22:00:31 +0000340 case 2500:
341 voltage_selector = 0x11;
hailfingerdfb32a02010-01-19 11:15:48 +0000342 break;
hailfingerf76cc322010-11-09 22:00:31 +0000343 case 3500:
344 voltage_selector = 0x10;
hailfingerdfb32a02010-01-19 11:15:48 +0000345 break;
346 default:
hailfingerf76cc322010-11-09 22:00:31 +0000347 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
hailfingerdfb32a02010-01-19 11:15:48 +0000348 return 1;
349 }
hailfingerf76cc322010-11-09 22:00:31 +0000350 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
351 millivolt % 1000);
hailfingerdfb32a02010-01-19 11:15:48 +0000352
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 }
David Hendricksa7f99ac2015-11-12 16:42:03 -0800357 ret = dediprog_write_ep(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000358 if (ret != 0x0) {
uwe8d342eb2011-07-28 08:13:25 +0000359 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
360 voltage_selector);
hailfingerdfb32a02010-01-19 11:15:48 +0000361 return 1;
362 }
David Hendricksfe05e742015-08-15 17:29:39 -0700363 if (voltage_selector != 0) {
364 /* Wait some time as the original driver does. */
365 programmer_delay(200 * 1000);
366 }
hailfingerdfb32a02010-01-19 11:15:48 +0000367 return 0;
368}
369
David Hendricksfe05e742015-08-15 17:29:39 -0700370struct dediprog_spispeeds {
371 const char *const name;
372 const int speed;
373};
374
375static const struct dediprog_spispeeds spispeeds[] = {
376 { "24M", 0x0 },
377 { "12M", 0x2 },
378 { "8M", 0x1 },
379 { "3M", 0x3 },
380 { "2.18M", 0x4 },
381 { "1.5M", 0x5 },
382 { "750k", 0x6 },
383 { "375k", 0x7 },
384 { NULL, 0x0 },
385};
386
387static int dediprog_set_spi_speed(unsigned int spispeed_idx)
hailfingerdfb32a02010-01-19 11:15:48 +0000388{
David Hendricksfe05e742015-08-15 17:29:39 -0700389 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
390 msg_pinfo("Skipping to set SPI speed because firmware is too old.\n");
391 return 0;
392 }
hailfingerdfb32a02010-01-19 11:15:48 +0000393
David Hendricksfe05e742015-08-15 17:29:39 -0700394 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
395 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
hailfingerdfb32a02010-01-19 11:15:48 +0000396
David Hendricksa7f99ac2015-11-12 16:42:03 -0800397 int ret = dediprog_write_ep(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000398 if (ret != 0x0) {
David Hendricksfe05e742015-08-15 17:29:39 -0700399 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
hailfingerdfb32a02010-01-19 11:15:48 +0000400 return 1;
401 }
402 return 0;
403}
404
hailfingerfb6287d2010-11-16 21:25:29 +0000405/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
406 * @start start address
407 * @len length
408 * @return 0 on success, 1 on failure
409 */
410static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000411 unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000412{
David Hendricksf9ecb452015-11-04 15:40:27 -0800413 int ret, err = 1;
stefanctc5eb8a92011-11-23 09:13:48 +0000414 unsigned int i;
hailfingerfb6287d2010-11-16 21:25:29 +0000415 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000416 const unsigned int chunksize = 0x200;
417 const unsigned int count = len / chunksize;
David Hendricks14e82e52015-08-15 17:59:03 -0700418 unsigned int cmd_len;
David Hendricksf9ecb452015-11-04 15:40:27 -0800419 struct dediprog_transfer_status status = { 0, 0, 0 };
420 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
421 struct libusb_transfer *transfer;
hailfingerfb6287d2010-11-16 21:25:29 +0000422
423 if ((start % chunksize) || (len % chunksize)) {
424 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
425 "at flashrom@flashrom.org\n", __func__, start, len);
426 return 1;
427 }
428
429 /* No idea if the hardware can handle empty reads, so chicken out. */
430 if (!len)
431 return 0;
David Hendricks14e82e52015-08-15 17:59:03 -0700432 /* Command Read SPI Bulk. */
433 if (is_new_prot()) {
434 const uint8_t read_cmd[] = {
435 count & 0xff,
436 (count >> 8) & 0xff,
437 0,
438 READ_MODE_FAST,
439 0,
440 0,
441 start & 0xff,
442 (start >> 8) & 0xff,
443 (start >> 16) & 0xff,
444 (start >> 24) & 0xff,
445 };
446
447 cmd_len = sizeof(read_cmd);
David Hendricksa7f99ac2015-11-12 16:42:03 -0800448 ret = dediprog_write_ep(CMD_READ, 0, 0, read_cmd, cmd_len);
David Hendricks14e82e52015-08-15 17:59:03 -0700449 } else {
450 const uint8_t read_cmd[] = {count & 0xff,
451 (count >> 8) & 0xff,
452 chunksize & 0xff,
453 (chunksize >> 8) & 0xff};
454
455 cmd_len = sizeof(read_cmd);
David Hendricksa7f99ac2015-11-12 16:42:03 -0800456 ret = dediprog_write_ep(CMD_READ, start % 0x10000, start / 0x10000, read_cmd, cmd_len);
David Hendricks14e82e52015-08-15 17:59:03 -0700457 }
458 if (ret != cmd_len) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800459 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
hailfingerfb6287d2010-11-16 21:25:29 +0000460 return 1;
461 }
462
David Hendricksf9ecb452015-11-04 15:40:27 -0800463 /*
464 * Ring buffer of bulk transfers.
465 * Poll until at least one transfer is ready,
466 * schedule next transfers until buffer is full.
467 */
hailfingerfb6287d2010-11-16 21:25:29 +0000468
David Hendricksf9ecb452015-11-04 15:40:27 -0800469 /* Allocate bulk transfers. */
470 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
471 transfers[i] = libusb_alloc_transfer(0);
472 if (!transfers[i]) {
473 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
474 goto _err_free;
475 }
476 }
477
478 /* Now transfer requested chunks using libusb's asynchronous interface. */
479 while (!status.error && (status.queued_idx < count)) {
480 while ((status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS) {
481 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
482 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
483 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
484 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
485 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
486 ret = libusb_submit_transfer(transfer);
487 if (ret < 0) {
488 msg_perr("Submitting SPI bulk read %i failed: %i %s!\n",
489 status.queued_idx, ret, libusb_error_name(ret));
490 goto _err_free;
491 }
492 ++status.queued_idx;
493 }
494 if (dediprog_bulk_read_poll(&status, 0))
495 goto _err_free;
496 }
497 /* Wait for transfers to finish. */
498 if (dediprog_bulk_read_poll(&status, 1))
499 goto _err_free;
500 /* Check if everything has been transmitted. */
501 if ((status.finished_idx < count) || status.error)
502 goto _err_free;
503
504 err = 0;
505
506_err_free:
507 dediprog_bulk_read_poll(&status, 1);
508 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
509 if (transfers[i]) libusb_free_transfer(transfers[i]);
510 return err;
hailfingerfb6287d2010-11-16 21:25:29 +0000511}
512
stefanctc5eb8a92011-11-23 09:13:48 +0000513static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf,
514 unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000515{
hailfingerfb6287d2010-11-16 21:25:29 +0000516 int ret;
517 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000518 const unsigned int chunksize = 0x200;
519 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
520 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000521
Simon Glasse53cc1d2015-01-08 05:48:51 -0700522 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000523
hailfingerfb6287d2010-11-16 21:25:29 +0000524 if (residue) {
525 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
526 start, residue);
527 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700528 if (ret)
529 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000530 }
531
532 /* Round down. */
533 bulklen = (len - residue) / chunksize * chunksize;
534 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
535 bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700536 if (ret)
537 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000538
539 len -= residue + bulklen;
540 if (len) {
541 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
542 start, len);
543 ret = spi_read_chunked(flash, buf + residue + bulklen,
544 start + residue + bulklen, len, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700545 if (ret)
546 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000547 }
548
Simon Glasse53cc1d2015-01-08 05:48:51 -0700549 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000550 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700551err:
552 dediprog_set_leds(LED_ERROR);
553 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000554}
555
David Hendricksfe05e742015-08-15 17:29:39 -0700556/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
557 * @chunksize length of data chunks, only 256 supported by now
558 * @start start address
559 * @len length
560 * @dedi_spi_cmd dediprog specific write command for spi bus
561 * @return 0 on success, 1 on failure
562 */
563static int dediprog_spi_bulk_write(struct flashchip *flash, const uint8_t *buf, unsigned int chunksize,
564 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
hailfinger556e9c32010-11-23 21:28:16 +0000565{
David Hendricksf9ecb452015-11-04 15:40:27 -0800566 int ret, transferred;
David Hendricksfe05e742015-08-15 17:29:39 -0700567 unsigned int i;
568 /* USB transfer size must be 512, other sizes will NOT work at all.
569 * chunksize is the real data size per USB bulk transfer. The remaining
570 * space in a USB bulk transfer must be filled with 0xff padding.
571 */
572 const unsigned int count = len / chunksize;
David Hendricksf9ecb452015-11-04 15:40:27 -0800573 const unsigned char count_and_cmd_old[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
574 const unsigned char count_and_cmd_new[] = {
David Hendricks741efe22015-08-15 19:18:51 -0700575 count & 0xff,
576 (count >> 8) & 0xff,
577 0, /* used for 24-bit address support? */
578 dedi_spi_cmd,
579 JEDEC_BYTE_PROGRAM, /* FIXME: needs to be determined based on byte 3? */
580 0,
581 start & 0xff,
582 (start >> 8) & 0xff,
583 (start >> 16) & 0xff,
584 (start >> 24) & 0xff,
585 };
David Hendricksf9ecb452015-11-04 15:40:27 -0800586 unsigned char usbbuf[512];
David Hendricksfe05e742015-08-15 17:29:39 -0700587
588 /*
589 * We should change this check to
590 * chunksize > 512
591 * once we know how to handle different chunk sizes.
592 */
593 if (chunksize != 256) {
594 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
595 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
596 return 1;
597 }
598
599 if ((start % chunksize) || (len % chunksize)) {
600 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
601 "at flashrom@flashrom.org\n", __func__, start, len);
602 return 1;
603 }
604
605 /* No idea if the hardware can handle empty writes, so chicken out. */
606 if (!len)
607 return 0;
David Hendricks741efe22015-08-15 19:18:51 -0700608 if (!is_new_prot()) {
609 /* Command Write SPI Bulk. No idea which write command is used on the
610 * SPI side.
611 */
David Hendricksa7f99ac2015-11-12 16:42:03 -0800612 ret = dediprog_write_ep(CMD_WRITE, start % 0x10000, start / 0x10000,
David Hendricksf9ecb452015-11-04 15:40:27 -0800613 count_and_cmd_old, sizeof(count_and_cmd_old));
David Hendricks741efe22015-08-15 19:18:51 -0700614 if (ret != sizeof(count_and_cmd_old)) {
615 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
David Hendricksf9ecb452015-11-04 15:40:27 -0800616 libusb_error_name(ret));
David Hendricks741efe22015-08-15 19:18:51 -0700617 return 1;
618 }
619 } else {
620 /* Command Write SPI Bulk. No idea which write command is used on the
621 * SPI side.
622 */
David Hendricksa7f99ac2015-11-12 16:42:03 -0800623 ret = dediprog_write_ep(CMD_WRITE, 0, 0,
David Hendricksf9ecb452015-11-04 15:40:27 -0800624 count_and_cmd_new, sizeof(count_and_cmd_new));
David Hendricks741efe22015-08-15 19:18:51 -0700625 if (ret != sizeof(count_and_cmd_new)) {
626 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
David Hendricksf9ecb452015-11-04 15:40:27 -0800627 libusb_error_name(ret));
David Hendricks741efe22015-08-15 19:18:51 -0700628 return 1;
629 }
David Hendricksfe05e742015-08-15 17:29:39 -0700630 }
631
632 for (i = 0; i < count; i++) {
633 memset(usbbuf, 0xff, sizeof(usbbuf));
634 memcpy(usbbuf, buf + i * chunksize, chunksize);
David Hendricksf9ecb452015-11-04 15:40:27 -0800635 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint,
636 usbbuf, 512, &transferred, DEFAULT_TIMEOUT);
637 if ((ret < 0) || (transferred != 512)) {
638 msg_perr("SPI bulk write failed, expected %i, got %i %s!\n",
639 512, ret, libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700640 return 1;
641 }
642 }
643
644 return 0;
645}
646
647static int dediprog_spi_write(struct flashchip *flash, const uint8_t *buf,
648 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
649{
650 int ret;
David Hendricksfe05e742015-08-15 17:29:39 -0700651 const unsigned int chunksize = flash->page_size;
652 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
653 unsigned int bulklen;
stepan4c06de72011-01-28 09:00:15 +0000654
Simon Glasse53cc1d2015-01-08 05:48:51 -0700655 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000656
David Hendricksfe05e742015-08-15 17:29:39 -0700657 if (chunksize != 256) {
658 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
659 "we don't know how dediprog\nhandles them.\n");
660 /* Write everything like it was residue. */
661 residue = len;
662 }
stepan4c06de72011-01-28 09:00:15 +0000663
David Hendricksfe05e742015-08-15 17:29:39 -0700664 if (residue) {
665 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
666 start, residue);
667 /* No idea about the real limit. Maybe 12, maybe more. */
668 ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 12);
669 if (ret) {
670 dediprog_set_leds(LED_ERROR);
671 return ret;
672 }
673 }
674
675 /* Round down. */
676 bulklen = (len - residue) / chunksize * chunksize;
677 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
678 if (ret) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700679 dediprog_set_leds(LED_ERROR);
David Hendricksfe05e742015-08-15 17:29:39 -0700680 return ret;
681 }
stepan4c06de72011-01-28 09:00:15 +0000682
David Hendricksfe05e742015-08-15 17:29:39 -0700683 len -= residue + bulklen;
684 if (len) {
685 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
686 start, len);
687 ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen),
688 start + residue + bulklen, len, 12);
689 if (ret) {
690 dediprog_set_leds(LED_ERROR);
691 return ret;
692 }
693 }
694
695 dediprog_set_leds(LED_PASS);
696 return 0;
hailfinger556e9c32010-11-23 21:28:16 +0000697}
698
David Hendricksfe05e742015-08-15 17:29:39 -0700699//static int dediprog_spi_write_256(struct flashchip *flash, const uint8_t *buf, unsigned int start, unsigned int len)
700static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
701{
702 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
703}
704
705#if 0
706static int dediprog_spi_write_aai(struct flashchip *flash, const uint8_t *buf, unsigned int start, unsigned int len)
707{
708 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
709}
710#endif
711
712//static int dediprog_spi_send_command(struct flashchip *flash,
713static int dediprog_spi_send_command(unsigned int writecnt,
714 unsigned int readcnt,
715 const unsigned char *writearr,
716 unsigned char *readarr)
hailfingerdfb32a02010-01-19 11:15:48 +0000717{
718 int ret;
719
hailfingeraf389cc2010-01-22 02:53:30 +0000720 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
David Hendricksfe05e742015-08-15 17:29:39 -0700721 if (writecnt > UINT16_MAX) {
722 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
723 return 1;
Simon Glass543101a2015-01-08 06:28:13 -0700724 }
David Hendricksfe05e742015-08-15 17:29:39 -0700725 if (readcnt > UINT16_MAX) {
726 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
727 return 1;
728 }
729
David Hendricks14e82e52015-08-15 17:59:03 -0700730 /* New protocol has the read flag as value while the old protocol had it in the index field. */
731 if (is_new_prot()) {
David Hendricksa7f99ac2015-11-12 16:42:03 -0800732 ret = dediprog_write_ep(CMD_TRANSCEIVE, readcnt ? 1 : 0, 0, writearr, writecnt);
David Hendricks14e82e52015-08-15 17:59:03 -0700733 } else {
David Hendricksa7f99ac2015-11-12 16:42:03 -0800734 ret = dediprog_write_ep(CMD_TRANSCEIVE, 0, readcnt ? 1 : 0, writearr, writecnt);
David Hendricks14e82e52015-08-15 17:59:03 -0700735 }
hailfingerdfb32a02010-01-19 11:15:48 +0000736 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000737 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800738 writecnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000739 return 1;
740 }
David Hendricksfe05e742015-08-15 17:29:39 -0700741 if (readcnt == 0)
hailfingerdfb32a02010-01-19 11:15:48 +0000742 return 0;
David Hendricksfe05e742015-08-15 17:29:39 -0700743
David Hendricksa7f99ac2015-11-12 16:42:03 -0800744 ret = dediprog_read_ep(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000745 if (ret != readcnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000746 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800747 readcnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000748 return 1;
749 }
750 return 0;
751}
752
hailfinger1ff33dc2010-07-03 11:02:10 +0000753static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000754{
755 int ret;
hailfinger7a009082010-11-09 23:30:43 +0000756 int fw[3];
David Hendricks6702e072015-08-15 18:09:04 -0700757 int sfnum;
hailfingerdfb32a02010-01-19 11:15:48 +0000758 char buf[0x11];
759
hailfingerdfb32a02010-01-19 11:15:48 +0000760 /* Command Receive Device String. */
David Hendricksa7f99ac2015-11-12 16:42:03 -0800761 ret = dediprog_read_ep(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000762 if (ret != 0x10) {
763 msg_perr("Incomplete/failed Command Receive Device String!\n");
764 return 1;
765 }
766 buf[0x10] = '\0';
767 msg_pdbg("Found a %s\n", buf);
David Hendricks6702e072015-08-15 18:09:04 -0700768 if (memcmp(buf, "SF100", 0x5) == 0)
769 dediprog_devicetype = DEV_SF100;
770 else if (memcmp(buf, "SF600", 0x5) == 0)
771 dediprog_devicetype = DEV_SF600;
772 else {
David Hendricks22593802015-11-13 12:59:30 -0800773 msg_perr("Device not a SF100 or SF600!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000774 return 1;
775 }
David Hendricks6702e072015-08-15 18:09:04 -0700776 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2])
777 != 4 || sfnum != dediprog_devicetype) {
David Hendricks14e82e52015-08-15 17:59:03 -0700778 msg_perr("Unexpected firmware version string '%s'\n", buf);
hailfinger7a009082010-11-09 23:30:43 +0000779 return 1;
780 }
David Hendricks14e82e52015-08-15 17:59:03 -0700781 /* Only these major versions were tested. */
David Hendricks6702e072015-08-15 18:09:04 -0700782 if (fw[0] < 2 || fw[0] > 7) {
David Hendricks14e82e52015-08-15 17:59:03 -0700783 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000784 return 1;
785 }
stepan4c06de72011-01-28 09:00:15 +0000786 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
David Hendricks14e82e52015-08-15 17:59:03 -0700787
hailfingerdfb32a02010-01-19 11:15:48 +0000788 return 0;
789}
790
David Hendricks28fbdfb2015-08-15 18:04:37 -0700791/*
792 * This command presumably sets the voltage for the SF100 itself (not the
793 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
794 * (including all SF600s) do not support it.
795 */
796static int dediprog_set_voltage(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000797{
798 int ret;
David Hendricksf9ecb452015-11-04 15:40:27 -0800799 unsigned char buf[0x1];
hailfingerdfb32a02010-01-19 11:15:48 +0000800
801 memset(buf, 0, sizeof(buf));
David Hendricksa7f99ac2015-11-12 16:42:03 -0800802 ret = dediprog_read_other(CMD_SET_VOLTAGE, 0x0, 0x0, buf, 0x1);
hailfinger7a009082010-11-09 23:30:43 +0000803 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800804 msg_perr("Command A failed (%s)!\n", libusb_error_name(ret));
hailfinger7a009082010-11-09 23:30:43 +0000805 return 1;
806 }
hailfingerdfb32a02010-01-19 11:15:48 +0000807 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -0700808 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000809 return 1;
810 }
811 return 0;
812}
813
David Hendrickscf453d82015-11-16 20:19:57 -0800814static int dediprog_leave_standalone_mode(void)
815{
816 int ret;
817
818 if (dediprog_devicetype != DEV_SF600)
819 return 0;
820
821 msg_pdbg("Leaving standalone mode\n");
822 ret = dediprog_write_ep(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
823 if (ret) {
824 msg_perr("Failed to leave standalone mode (%s)!\n", libusb_error_name(ret));
825 return 1;
826 }
827
828 return 0;
829}
830
David Hendricksfe05e742015-08-15 17:29:39 -0700831#if 0
832/* Something.
833 * Present in eng_detect_blink.log with firmware 3.1.8
834 * Always preceded by Command Receive Device String
835 */
836static int dediprog_command_b(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000837{
838 int ret;
David Hendricksfe05e742015-08-15 17:29:39 -0700839 char buf[0x3];
hailfingerdfb32a02010-01-19 11:15:48 +0000840
David Hendricksfe05e742015-08-15 17:29:39 -0700841 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
842 buf, 0x3, DEFAULT_TIMEOUT);
843 if (ret < 0) {
844 msg_perr("Command B failed (%s)!\n", usb_strerror());
845 return 1;
846 }
847 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
848 (buf[2] != 0xff)) {
849 msg_perr("Unexpected response to Command B!\n");
850 return 1;
851 }
852
853 return 0;
854}
855#endif
856
857static int set_target_flash(enum dediprog_target target)
858{
David Hendricksa7f99ac2015-11-12 16:42:03 -0800859 int ret = dediprog_write_ep(CMD_SET_TARGET, target, 0, NULL, 0);
David Hendricksfe05e742015-08-15 17:29:39 -0700860 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800861 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000862 return 1;
863 }
864 return 0;
865}
866
David Hendricksfe05e742015-08-15 17:29:39 -0700867#if 0
868/* Returns true if the button is currently pressed. */
869static bool dediprog_get_button(void)
Simon Glassd01002b2015-01-08 05:44:16 -0700870{
David Hendricksfe05e742015-08-15 17:29:39 -0700871 char buf[1];
872 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
873 buf, 0x1, DEFAULT_TIMEOUT);
874 if (ret != 0) {
875 msg_perr("Could not get button state (%s)!\n", usb_strerror());
876 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700877 }
David Hendricksfe05e742015-08-15 17:29:39 -0700878 return buf[0] != 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700879}
David Hendricksfe05e742015-08-15 17:29:39 -0700880#endif
Simon Glassd01002b2015-01-08 05:44:16 -0700881
hailfingerf76cc322010-11-09 22:00:31 +0000882static int parse_voltage(char *voltage)
883{
884 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000885 int i;
886 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000887
888 if (!voltage || !strlen(voltage)) {
889 msg_perr("Empty voltage= specified.\n");
890 return -1;
891 }
892 millivolt = (int)strtol(voltage, &tmp, 0);
893 voltage = tmp;
894 /* Handle "," and "." as decimal point. Everything after it is assumed
895 * to be in decimal notation.
896 */
897 if ((*voltage == '.') || (*voltage == ',')) {
898 voltage++;
899 for (i = 0; i < 3; i++) {
900 fraction *= 10;
901 /* Don't advance if the current character is invalid,
902 * but continue multiplying.
903 */
904 if ((*voltage < '0') || (*voltage > '9'))
905 continue;
906 fraction += *voltage - '0';
907 voltage++;
908 }
909 /* Throw away remaining digits. */
910 voltage += strspn(voltage, "0123456789");
911 }
912 /* The remaining string must be empty or "mV" or "V". */
913 tolower_string(voltage);
914
915 /* No unit or "V". */
916 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
917 millivolt *= 1000;
918 millivolt += fraction;
919 } else if (!strncmp(voltage, "mv", 2) ||
920 !strncmp(voltage, "milliv", 6)) {
921 /* No adjustment. fraction is discarded. */
922 } else {
923 /* Garbage at the end of the string. */
924 msg_perr("Garbage voltage= specified.\n");
925 return -1;
926 }
927 return millivolt;
928}
929
David Hendricksfe05e742015-08-15 17:29:39 -0700930#if 0
931static const struct spi_master spi_master_dediprog = {
932 .type = SPI_CONTROLLER_DEDIPROG,
933 .max_data_read = MAX_DATA_UNSPECIFIED,
934 .max_data_write = MAX_DATA_UNSPECIFIED,
935 .command = dediprog_spi_send_command,
936 .multicommand = default_spi_send_multicommand,
937 .read = dediprog_spi_read,
938 .write_256 = dediprog_spi_write_256,
939 .write_aai = dediprog_spi_write_aai,
940};
941#endif
mkarcherd264e9e2011-05-11 17:07:07 +0000942static const struct spi_programmer spi_programmer_dediprog = {
uwe8d342eb2011-07-28 08:13:25 +0000943 .type = SPI_CONTROLLER_DEDIPROG,
944 .max_data_read = MAX_DATA_UNSPECIFIED,
945 .max_data_write = MAX_DATA_UNSPECIFIED,
946 .command = dediprog_spi_send_command,
947 .multicommand = default_spi_send_multicommand,
948 .read = dediprog_spi_read,
949 .write_256 = dediprog_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000950};
951
dhendrix0ffc2eb2011-06-14 01:35:36 +0000952static int dediprog_shutdown(void *data)
953{
954 msg_pspew("%s\n", __func__);
955
David Hendricksfe05e742015-08-15 17:29:39 -0700956 dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Hendricks6702e072015-08-15 18:09:04 -0700957 dediprog_devicetype = DEV_UNKNOWN;
David Hendricksfe05e742015-08-15 17:29:39 -0700958
dhendrix0ffc2eb2011-06-14 01:35:36 +0000959 /* URB 28. Command Set SPI Voltage to 0. */
960 if (dediprog_set_spi_voltage(0x0))
961 return 1;
962
David Hendricksf9ecb452015-11-04 15:40:27 -0800963 if (libusb_release_interface(dediprog_handle, 0)) {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000964 msg_perr("Could not release USB interface!\n");
965 return 1;
966 }
David Hendricksf9ecb452015-11-04 15:40:27 -0800967 libusb_close(dediprog_handle);
968 libusb_exit(usb_ctx);
969
dhendrix0ffc2eb2011-06-14 01:35:36 +0000970 return 0;
971}
972
hailfingerdfb32a02010-01-19 11:15:48 +0000973/* URB numbers refer to the first log ever captured. */
974int dediprog_init(void)
975{
David Hendricksfe05e742015-08-15 17:29:39 -0700976 char *voltage, *device, *spispeed, *target_str;
977 int spispeed_idx = 1;
hailfingerb91c08c2011-08-15 19:54:20 +0000978 int millivolt = 3500;
David Hendricksfe05e742015-08-15 17:29:39 -0700979 long usedevice = 0;
980 long target = 1;
981 int i, ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000982
983 msg_pspew("%s\n", __func__);
984
David Hendricksfe05e742015-08-15 17:29:39 -0700985 spispeed = extract_programmer_param("spispeed");
986 if (spispeed) {
987 for (i = 0; spispeeds[i].name; ++i) {
988 if (!strcasecmp(spispeeds[i].name, spispeed)) {
989 spispeed_idx = i;
990 break;
991 }
992 }
993 if (!spispeeds[i].name) {
994 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
995 free(spispeed);
996 return 1;
997 }
998 free(spispeed);
999 }
1000
hailfingerf76cc322010-11-09 22:00:31 +00001001 voltage = extract_programmer_param("voltage");
1002 if (voltage) {
1003 millivolt = parse_voltage(voltage);
1004 free(voltage);
uwe8d342eb2011-07-28 08:13:25 +00001005 if (millivolt < 0)
hailfingerf76cc322010-11-09 22:00:31 +00001006 return 1;
hailfingerf76cc322010-11-09 22:00:31 +00001007 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1008 }
David Hendricksfe05e742015-08-15 17:29:39 -07001009
1010 device = extract_programmer_param("device");
1011 if (device) {
1012 char *dev_suffix;
1013 errno = 0;
1014 usedevice = strtol(device, &dev_suffix, 10);
1015 if (errno != 0 || device == dev_suffix) {
1016 msg_perr("Error: Could not convert 'device'.\n");
1017 free(device);
Simon Glassd01002b2015-01-08 05:44:16 -07001018 return 1;
1019 }
David Hendricksfe05e742015-08-15 17:29:39 -07001020 if (usedevice < 0 || usedevice > UINT_MAX) {
1021 msg_perr("Error: Value for 'device' is out of range.\n");
1022 free(device);
1023 return 1;
1024 }
1025 if (strlen(dev_suffix) > 0) {
1026 msg_perr("Error: Garbage following 'device' value.\n");
1027 free(device);
1028 return 1;
1029 }
1030 msg_pinfo("Using device %li.\n", usedevice);
Simon Glassd01002b2015-01-08 05:44:16 -07001031 }
David Hendricksfe05e742015-08-15 17:29:39 -07001032 free(device);
1033
1034 target_str = extract_programmer_param("target");
1035 if (target_str) {
1036 char *target_suffix;
1037 errno = 0;
1038 target = strtol(target_str, &target_suffix, 10);
1039 if (errno != 0 || target_str == target_suffix) {
1040 msg_perr("Error: Could not convert 'target'.\n");
1041 free(target_str);
1042 return 1;
1043 }
1044 if (target < 1 || target > 2) {
1045 msg_perr("Error: Value for 'target' is out of range.\n");
1046 free(target_str);
1047 return 1;
1048 }
1049 if (strlen(target_suffix) > 0) {
1050 msg_perr("Error: Garbage following 'target' value.\n");
1051 free(target_str);
1052 return 1;
1053 }
1054 msg_pinfo("Using target %li.\n", target);
1055 }
1056 free(target_str);
hailfingerf76cc322010-11-09 22:00:31 +00001057
hailfingerdfb32a02010-01-19 11:15:48 +00001058 /* Here comes the USB stuff. */
David Hendricksf9ecb452015-11-04 15:40:27 -08001059 libusb_init(&usb_ctx);
1060 if (!usb_ctx) {
1061 msg_perr("Could not initialize libusb!\n");
hailfingerdfb32a02010-01-19 11:15:48 +00001062 return 1;
1063 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001064 dediprog_handle = get_device_by_vid_pid_number(0x0483, 0xdada, (unsigned int) usedevice);
David Hendricksfe05e742015-08-15 17:29:39 -07001065 if (!dediprog_handle) {
David Hendricks22593802015-11-13 12:59:30 -08001066 msg_perr("Could not find a Dediprog programmer on USB!\n");
David Hendricksf9ecb452015-11-04 15:40:27 -08001067 libusb_exit(usb_ctx);
David Hendricksfe05e742015-08-15 17:29:39 -07001068 return 1;
1069 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001070 ret = libusb_set_configuration(dediprog_handle, 1);
1071 if (ret != 0) {
1072 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1073 libusb_close(dediprog_handle);
1074 libusb_exit(usb_ctx);
hailfingerfb6287d2010-11-16 21:25:29 +00001075 return 1;
1076 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001077 ret = libusb_claim_interface(dediprog_handle, 0);
hailfingerfb6287d2010-11-16 21:25:29 +00001078 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001079 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1080 libusb_close(dediprog_handle);
1081 libusb_exit(usb_ctx);
hailfingerfb6287d2010-11-16 21:25:29 +00001082 return 1;
1083 }
David Hendricksfe05e742015-08-15 17:29:39 -07001084
dhendrix0ffc2eb2011-06-14 01:35:36 +00001085 if (register_shutdown(dediprog_shutdown, NULL))
1086 return 1;
1087
David Hendricks28fbdfb2015-08-15 18:04:37 -07001088 /* Try reading the devicestring. If that fails and the device is old
1089 * (FW < 6.0.0) then we need to try the "set voltage" command and then
1090 * attempt to read the devicestring again. */
1091 if (dediprog_check_devicestring()) {
1092 if (dediprog_set_voltage())
1093 return 1;
1094 if (dediprog_check_devicestring())
1095 return 1;
1096 }
David Hendricksfe05e742015-08-15 17:29:39 -07001097
David Hendricksd7468582015-11-12 15:21:12 -08001098 /* SF100 firmware exposes only one endpoint for in/out, SF600 firmware
1099 exposes separate endpoints for in and out. */
1100 dediprog_in_endpoint = 2;
1101 if (dediprog_devicetype == DEV_SF100)
1102 dediprog_out_endpoint = 2;
1103 else
1104 dediprog_out_endpoint = 1;
1105
1106
David Hendricksfe05e742015-08-15 17:29:39 -07001107 /* Set all possible LEDs as soon as possible to indicate activity.
1108 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
1109 * dediprog_setup() has queried the device and set dediprog_firmwareversion. */
1110 dediprog_set_leds(LED_ALL);
1111
1112 /* Select target/socket, frequency and VCC. */
1113 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
1114 dediprog_set_spi_speed(spispeed_idx) ||
1115 dediprog_set_spi_voltage(millivolt)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -07001116 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +00001117 return 1;
stepan4c06de72011-01-28 09:00:15 +00001118 }
hailfingerdfb32a02010-01-19 11:15:48 +00001119
David Hendrickscf453d82015-11-16 20:19:57 -08001120 if (dediprog_leave_standalone_mode())
1121 return 1;
1122
David Hendricksfe05e742015-08-15 17:29:39 -07001123// register_spi_master(&spi_master_dediprog);
mkarcherd264e9e2011-05-11 17:07:07 +00001124 register_spi_programmer(&spi_programmer_dediprog);
hailfingerdfb32a02010-01-19 11:15:48 +00001125
David Hendricksfe05e742015-08-15 17:29:39 -07001126 dediprog_set_leds(LED_NONE);
stepan4c06de72011-01-28 09:00:15 +00001127
hailfingerdfb32a02010-01-19 11:15:48 +00001128 return 0;
1129}
David Hendricksfe05e742015-08-15 17:29:39 -07001130