blob: 5627160eb38510bbc1a8af5b5649378e74799194 [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,
Edward O'Callaghancc0bf442019-02-27 22:35:44 +110054 DEV_SF200 = 200,
David Hendricks6702e072015-08-15 18:09:04 -070055 DEV_SF600 = 600,
56};
57
58enum dediprog_devtype dediprog_devicetype;
59
David Hendricksfe05e742015-08-15 17:29:39 -070060enum dediprog_leds {
61 LED_INVALID = -1,
62 LED_NONE = 0,
Simon Glasse53cc1d2015-01-08 05:48:51 -070063 LED_PASS = 1 << 0,
64 LED_BUSY = 1 << 1,
65 LED_ERROR = 1 << 2,
66 LED_ALL = 7,
67};
68
Simon Glassa1f80682015-01-08 05:55:29 -070069/* IO bits for CMD_SET_IO_LED message */
David Hendricksfe05e742015-08-15 17:29:39 -070070enum dediprog_ios {
Simon Glassa1f80682015-01-08 05:55:29 -070071 IO1 = 1 << 0,
72 IO2 = 1 << 1,
73 IO3 = 1 << 2,
74 IO4 = 1 << 3,
75};
76
David Hendricksfe05e742015-08-15 17:29:39 -070077enum dediprog_cmds {
78 CMD_TRANSCEIVE = 0x01,
79 CMD_POLL_STATUS_REG = 0x02,
80 CMD_SET_VPP = 0x03,
81 CMD_SET_TARGET = 0x04,
82 CMD_READ_EEPROM = 0x05,
83 CMD_WRITE_EEPROM = 0x06,
84 CMD_SET_IO_LED = 0x07,
85 CMD_READ_PROG_INFO = 0x08,
86 CMD_SET_VCC = 0x09,
87 CMD_SET_STANDALONE = 0x0A,
David Hendricks28fbdfb2015-08-15 18:04:37 -070088 CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */
David Hendricksfe05e742015-08-15 17:29:39 -070089 CMD_GET_BUTTON = 0x11,
90 CMD_GET_UID = 0x12,
91 CMD_SET_CS = 0x14,
92 CMD_IO_MODE = 0x15,
93 CMD_FW_UPDATE = 0x1A,
94 CMD_FPGA_UPDATE = 0x1B,
95 CMD_READ_FPGA_VERSION = 0x1C,
96 CMD_SET_HOLD = 0x1D,
97 CMD_READ = 0x20,
98 CMD_WRITE = 0x30,
99 CMD_WRITE_AT45DB = 0x31,
100 CMD_NAND_WRITE = 0x32,
101 CMD_NAND_READ = 0x33,
102 CMD_SET_SPI_CLK = 0x61,
103 CMD_CHECK_SOCKET = 0x62,
104 CMD_DOWNLOAD_PRJ = 0x63,
105 CMD_READ_PRJ_NAME = 0x64,
106 // New protocol/firmware only
107 CMD_CHECK_SDCARD = 0x65,
108 CMD_READ_PRJ = 0x66,
Simon Glassa1f80682015-01-08 05:55:29 -0700109};
110
David Hendricksfe05e742015-08-15 17:29:39 -0700111enum dediprog_target {
112 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
113 FLASH_TYPE_FLASH_CARD,
114 FLASH_TYPE_APPLICATION_FLASH_2,
115 FLASH_TYPE_SOCKET,
Simon Glassa1f80682015-01-08 05:55:29 -0700116};
117
David Hendricksfe05e742015-08-15 17:29:39 -0700118enum dediprog_readmode {
119 READ_MODE_STD = 1,
120 READ_MODE_FAST = 2,
121 READ_MODE_ATMEL45 = 3,
122 READ_MODE_4B_ADDR_FAST = 4,
123 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
Simon Glassd01002b2015-01-08 05:44:16 -0700124};
125
David Hendricksfe05e742015-08-15 17:29:39 -0700126enum dediprog_writemode {
127 WRITE_MODE_PAGE_PGM = 1,
128 WRITE_MODE_PAGE_WRITE = 2,
129 WRITE_MODE_1B_AAI = 3,
130 WRITE_MODE_2B_AAI = 4,
131 WRITE_MODE_128B_PAGE = 5,
132 WRITE_MODE_PAGE_AT26DF041 = 6,
133 WRITE_MODE_SILICON_BLUE_FPGA = 7,
134 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of length 512 bytes */
135 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
136 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of length 512 bytes */
137 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
138 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
Simon Glassd01002b2015-01-08 05:44:16 -0700139};
140
David Hendrickscf453d82015-11-16 20:19:57 -0800141enum dediprog_standalone_mode {
142 ENTER_STANDALONE_MODE = 0,
143 LEAVE_STANDALONE_MODE = 1,
144};
145
Edward O'Callaghancb338322019-02-27 19:25:18 +1100146/*
147 * These are not official designations; they are for use in flashrom only.
148 * Order must be preserved so that comparison operators work.
149 */
150enum protocol {
151 PROTOCOL_UNKNOWN,
152 PROTOCOL_V1,
153 PROTOCOL_V2,
154 PROTOCOL_V3,
155};
156
Edward O'Callaghana88395f2019-02-27 18:44:04 +1100157const struct dev_entry devs_dediprog[] = {
158 {0x0483, 0xDADA, OK, "Dediprog", "SF100/SF200/SF600"},
159
160 {0},
161};
162
David Hendricksf9ecb452015-11-04 15:40:27 -0800163#ifndef LIBUSB_HAVE_ERROR_NAME
164/* Quick and dirty replacement for missing libusb_error_name in older libusb 1.0. */
165const char *libusb_error_name(int error_code)
166{
167 /* 18 chars for text, rest for number, sign, nullbyte. */
168 static char my_libusb_error[18 + 6];
David Hendricksfe05e742015-08-15 17:29:39 -0700169
David Hendricksf9ecb452015-11-04 15:40:27 -0800170 sprintf(my_libusb_error, "libusb error code %i", error_code);
171 return my_libusb_error;
172}
173#endif
David Hendricksfe05e742015-08-15 17:29:39 -0700174
Edward O'Callaghancb338322019-02-27 19:25:18 +1100175static enum protocol protocol(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000176{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100177 /* Firmware version < 5.0.0 is handled explicitly in some cases. */
David Hendricks6702e072015-08-15 18:09:04 -0700178 switch (dediprog_devicetype) {
179 case DEV_SF100:
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100180 case DEV_SF200:
181 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 5, 0))
182 return PROTOCOL_V1;
183 else
184 return PROTOCOL_V2;
David Hendricks6702e072015-08-15 18:09:04 -0700185 case DEV_SF600:
Edward O'Callaghancb338322019-02-27 19:25:18 +1100186 if (dediprog_firmwareversion < FIRMWARE_VERSION(6, 9, 0))
187 return PROTOCOL_V1;
188 else if (dediprog_firmwareversion <= FIRMWARE_VERSION(7, 2, 21))
189 return PROTOCOL_V2;
190 else
191 return PROTOCOL_V3;
David Hendricks6702e072015-08-15 18:09:04 -0700192 default:
Edward O'Callaghancb338322019-02-27 19:25:18 +1100193 return PROTOCOL_UNKNOWN;
David Hendricks6702e072015-08-15 18:09:04 -0700194 }
hailfingerdfb32a02010-01-19 11:15:48 +0000195}
David Hendricks14e82e52015-08-15 17:59:03 -0700196
David Hendricksf9ecb452015-11-04 15:40:27 -0800197struct dediprog_transfer_status {
198 int error; /* OK if 0, ERROR else */
199 unsigned int queued_idx;
200 unsigned int finished_idx;
201};
202
203static void dediprog_bulk_read_cb(struct libusb_transfer *const transfer)
204{
205 struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data;
206 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
207 status->error = 1;
208 msg_perr("SPI bulk read failed!\n");
209 }
210 ++status->finished_idx;
211}
212
213static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish)
214{
215 if (status->finished_idx >= status->queued_idx)
216 return 0;
217
218 do {
219 struct timeval timeout = { 10, 0 };
220 const int ret = libusb_handle_events_timeout_completed(usb_ctx, &timeout, NULL);
221 if (ret < 0) {
222 msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret));
223 return 1;
224 }
225 } while (finish && (status->finished_idx < status->queued_idx));
226 return 0;
227}
228
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100229static 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 -0700230{
David Hendricksf9ecb452015-11-04 15:40:27 -0800231 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
232 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700233}
234
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100235static 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 -0700236{
David Hendricksf9ecb452015-11-04 15:40:27 -0800237 return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
238 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
David Hendricks14e82e52015-08-15 17:59:03 -0700239}
hailfingerdfb32a02010-01-19 11:15:48 +0000240
David Hendricksa7f99ac2015-11-12 16:42:03 -0800241static int dediprog_read_other(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
242{
243 return libusb_control_transfer(dediprog_handle, REQTYPE_OTHER_IN, cmd, value, idx,
244 (unsigned char *)bytes, size, DEFAULT_TIMEOUT);
245}
David Hendricksf9ecb452015-11-04 15:40:27 -0800246
David Hendricksfe05e742015-08-15 17:29:39 -0700247/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
stepan4c06de72011-01-28 09:00:15 +0000248static int dediprog_set_leds(int leds)
249{
David Hendricksfe05e742015-08-15 17:29:39 -0700250 if (leds < LED_NONE || leds > LED_ALL)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700251 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000252
David Hendricks14e82e52015-08-15 17:59:03 -0700253 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
254 * them around if we have an old device. On those devices the LEDs map as follows:
stepan4c06de72011-01-28 09:00:15 +0000255 * bit 2 == 0: green light is on.
David Hendricks14e82e52015-08-15 17:59:03 -0700256 * bit 0 == 0: red light is on.
257 *
258 * Additionally, the command structure has changed with the "new" protocol.
259 *
260 * FIXME: take IO pins into account
stepan4c06de72011-01-28 09:00:15 +0000261 */
David Hendricks14e82e52015-08-15 17:59:03 -0700262 int target_leds, ret;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100263 if (protocol() >= PROTOCOL_V2) {
David Hendricks14e82e52015-08-15 17:59:03 -0700264 target_leds = (leds ^ 7) << 8;
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100265 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
stepan4c06de72011-01-28 09:00:15 +0000266 } else {
David Hendricks14e82e52015-08-15 17:59:03 -0700267 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
268 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
269 } else {
270 target_leds = leds;
271 }
272 target_leds ^= 7;
273
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100274 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Simon Glass543101a2015-01-08 06:28:13 -0700275 }
David Hendricksfe05e742015-08-15 17:29:39 -0700276
stepan4c06de72011-01-28 09:00:15 +0000277 if (ret != 0x0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800278 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret));
stepan4c06de72011-01-28 09:00:15 +0000279 return 1;
280 }
281
stepan4c06de72011-01-28 09:00:15 +0000282 return 0;
283}
284
David Hendricksfe05e742015-08-15 17:29:39 -0700285struct dediprog_spispeeds {
286 const char *const name;
287 const int speed;
288};
289
290static const struct dediprog_spispeeds spispeeds[] = {
291 { "24M", 0x0 },
292 { "12M", 0x2 },
293 { "8M", 0x1 },
294 { "3M", 0x3 },
295 { "2.18M", 0x4 },
296 { "1.5M", 0x5 },
297 { "750k", 0x6 },
298 { "375k", 0x7 },
299 { NULL, 0x0 },
300};
301
302static int dediprog_set_spi_speed(unsigned int spispeed_idx)
hailfingerdfb32a02010-01-19 11:15:48 +0000303{
David Hendricksfe05e742015-08-15 17:29:39 -0700304 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100305 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
David Hendricksfe05e742015-08-15 17:29:39 -0700306 return 0;
307 }
hailfingerdfb32a02010-01-19 11:15:48 +0000308
David Hendricksfe05e742015-08-15 17:29:39 -0700309 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
310 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
hailfingerdfb32a02010-01-19 11:15:48 +0000311
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100312 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000313 if (ret != 0x0) {
David Hendricksfe05e742015-08-15 17:29:39 -0700314 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
hailfingerdfb32a02010-01-19 11:15:48 +0000315 return 1;
316 }
317 return 0;
318}
319
Edward O'Callaghancb338322019-02-27 19:25:18 +1100320static void fill_rw_cmd_payload(uint8_t *data_packet, unsigned int count, uint8_t dedi_spi_cmd,
321 unsigned int *value, unsigned int *idx, unsigned int start, int is_read) {
322 /* First 5 bytes are common in both generations. */
323 data_packet[0] = count & 0xff;
324 data_packet[1] = (count >> 8) & 0xff;
325 data_packet[2] = 0; /* RFU */
326 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
327 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
328
329 if (protocol() >= PROTOCOL_V2) {
330 *value = *idx = 0;
331 data_packet[5] = 0; /* RFU */
332 data_packet[6] = (start >> 0) & 0xff;
333 data_packet[7] = (start >> 8) & 0xff;
334 data_packet[8] = (start >> 16) & 0xff;
335 data_packet[9] = (start >> 24) & 0xff;
336 if (protocol() >= PROTOCOL_V3) {
337 if (is_read) {
338 data_packet[10] = 0x00; /* address length (3 or 4) */
339 data_packet[11] = 0x00; /* dummy cycle / 2 */
340 } else {
341 /* 16 LSBs and 16 HSBs of page size */
342 /* FIXME: This assumes page size of 256. */
343 data_packet[10] = 0x00;
344 data_packet[11] = 0x01;
345 data_packet[12] = 0x00;
346 data_packet[13] = 0x00;
347 }
348 }
349 } else {
350 *value = start % 0x10000;
351 *idx = start / 0x10000;
352 }
353}
354
hailfingerfb6287d2010-11-16 21:25:29 +0000355/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
356 * @start start address
357 * @len length
358 * @return 0 on success, 1 on failure
359 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700360static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000361 unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000362{
Edward O'Callaghancb338322019-02-27 19:25:18 +1100363 int err = 1;
364
hailfingerfb6287d2010-11-16 21:25:29 +0000365 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000366 const unsigned int chunksize = 0x200;
367 const unsigned int count = len / chunksize;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100368
David Hendricksf9ecb452015-11-04 15:40:27 -0800369 struct dediprog_transfer_status status = { 0, 0, 0 };
370 struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, };
371 struct libusb_transfer *transfer;
hailfingerfb6287d2010-11-16 21:25:29 +0000372
Edward O'Callaghancb338322019-02-27 19:25:18 +1100373 if (len == 0)
374 return 0;
375
hailfingerfb6287d2010-11-16 21:25:29 +0000376 if ((start % chunksize) || (len % chunksize)) {
377 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
378 "at flashrom@flashrom.org\n", __func__, start, len);
379 return 1;
380 }
381
Edward O'Callaghancb338322019-02-27 19:25:18 +1100382 int command_packet_size;
383 switch (protocol()) {
384 case PROTOCOL_V1:
385 command_packet_size = 5;
386 break;
387 case PROTOCOL_V2:
388 command_packet_size = 10;
389 break;
390 case PROTOCOL_V3:
391 command_packet_size = 12;
392 break;
393 default:
394 return 1;
David Hendricks14e82e52015-08-15 17:59:03 -0700395 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100396
397 uint8_t data_packet[command_packet_size];
398 unsigned int value, idx;
399 fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start, 1);
400
401 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
402 if (ret != sizeof(data_packet)) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800403 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
hailfingerfb6287d2010-11-16 21:25:29 +0000404 return 1;
405 }
406
David Hendricksf9ecb452015-11-04 15:40:27 -0800407 /*
408 * Ring buffer of bulk transfers.
409 * Poll until at least one transfer is ready,
410 * schedule next transfers until buffer is full.
411 */
hailfingerfb6287d2010-11-16 21:25:29 +0000412
David Hendricksf9ecb452015-11-04 15:40:27 -0800413 /* Allocate bulk transfers. */
Edward O'Callaghancb338322019-02-27 19:25:18 +1100414 unsigned int i;
David Hendricksf9ecb452015-11-04 15:40:27 -0800415 for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
416 transfers[i] = libusb_alloc_transfer(0);
417 if (!transfers[i]) {
418 msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
419 goto _err_free;
420 }
421 }
422
423 /* Now transfer requested chunks using libusb's asynchronous interface. */
424 while (!status.error && (status.queued_idx < count)) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100425 while ((status.queued_idx < count) &&
426 (status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS)
427 {
David Hendricksf9ecb452015-11-04 15:40:27 -0800428 transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS];
429 libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint,
430 (unsigned char *)buf + status.queued_idx * chunksize, chunksize,
431 dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT);
432 transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK;
433 ret = libusb_submit_transfer(transfer);
434 if (ret < 0) {
435 msg_perr("Submitting SPI bulk read %i failed: %i %s!\n",
436 status.queued_idx, ret, libusb_error_name(ret));
437 goto _err_free;
438 }
439 ++status.queued_idx;
440 }
441 if (dediprog_bulk_read_poll(&status, 0))
442 goto _err_free;
443 }
444 /* Wait for transfers to finish. */
445 if (dediprog_bulk_read_poll(&status, 1))
446 goto _err_free;
447 /* Check if everything has been transmitted. */
448 if ((status.finished_idx < count) || status.error)
449 goto _err_free;
450
451 err = 0;
452
453_err_free:
454 dediprog_bulk_read_poll(&status, 1);
455 for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i)
456 if (transfers[i]) libusb_free_transfer(transfers[i]);
457 return err;
hailfingerfb6287d2010-11-16 21:25:29 +0000458}
459
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700460static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000461 unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000462{
hailfingerfb6287d2010-11-16 21:25:29 +0000463 int ret;
464 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000465 const unsigned int chunksize = 0x200;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100466 unsigned int residue = start % chunksize ? min(len, chunksize - start % chunksize) : 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000467 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000468
Simon Glasse53cc1d2015-01-08 05:48:51 -0700469 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000470
hailfingerfb6287d2010-11-16 21:25:29 +0000471 if (residue) {
472 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
473 start, residue);
474 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700475 if (ret)
476 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000477 }
478
479 /* Round down. */
480 bulklen = (len - residue) / chunksize * chunksize;
481 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
482 bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700483 if (ret)
484 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000485
486 len -= residue + bulklen;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100487 if (len != 0) {
hailfingerfb6287d2010-11-16 21:25:29 +0000488 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
489 start, len);
490 ret = spi_read_chunked(flash, buf + residue + bulklen,
491 start + residue + bulklen, len, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700492 if (ret)
493 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000494 }
495
Simon Glasse53cc1d2015-01-08 05:48:51 -0700496 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000497 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700498err:
499 dediprog_set_leds(LED_ERROR);
500 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000501}
502
David Hendricksfe05e742015-08-15 17:29:39 -0700503/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
504 * @chunksize length of data chunks, only 256 supported by now
505 * @start start address
506 * @len length
507 * @dedi_spi_cmd dediprog specific write command for spi bus
508 * @return 0 on success, 1 on failure
509 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700510static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
David Hendricksfe05e742015-08-15 17:29:39 -0700511 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
hailfinger556e9c32010-11-23 21:28:16 +0000512{
David Hendricksfe05e742015-08-15 17:29:39 -0700513 /* USB transfer size must be 512, other sizes will NOT work at all.
514 * chunksize is the real data size per USB bulk transfer. The remaining
515 * space in a USB bulk transfer must be filled with 0xff padding.
516 */
517 const unsigned int count = len / chunksize;
David Hendricksfe05e742015-08-15 17:29:39 -0700518
519 /*
520 * We should change this check to
521 * chunksize > 512
522 * once we know how to handle different chunk sizes.
523 */
524 if (chunksize != 256) {
525 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
526 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
527 return 1;
528 }
529
530 if ((start % chunksize) || (len % chunksize)) {
531 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
532 "at flashrom@flashrom.org\n", __func__, start, len);
533 return 1;
534 }
535
536 /* No idea if the hardware can handle empty writes, so chicken out. */
Edward O'Callaghancb338322019-02-27 19:25:18 +1100537 if (len == 0)
David Hendricksfe05e742015-08-15 17:29:39 -0700538 return 0;
Edward O'Callaghancb338322019-02-27 19:25:18 +1100539
540 int command_packet_size;
541 switch (protocol()) {
542 case PROTOCOL_V1:
543 command_packet_size = 5;
544 break;
545 case PROTOCOL_V2:
546 command_packet_size = 10;
547 break;
548 case PROTOCOL_V3:
549 command_packet_size = 14;
550 break;
551 default:
552 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -0700553 }
554
Edward O'Callaghancb338322019-02-27 19:25:18 +1100555 uint8_t data_packet[command_packet_size];
556 unsigned int value, idx;
557 fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start, 0);
558 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
559 if (ret != sizeof(data_packet)) {
560 msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
561 return 1;
562 }
563
564 unsigned int i;
David Hendricksfe05e742015-08-15 17:29:39 -0700565 for (i = 0; i < count; i++) {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100566 unsigned char usbbuf[512];
David Hendricksfe05e742015-08-15 17:29:39 -0700567 memcpy(usbbuf, buf + i * chunksize, chunksize);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100568 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
569 int transferred;
570 ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, &transferred,
571 DEFAULT_TIMEOUT);
David Hendricksf9ecb452015-11-04 15:40:27 -0800572 if ((ret < 0) || (transferred != 512)) {
573 msg_perr("SPI bulk write failed, expected %i, got %i %s!\n",
574 512, ret, libusb_error_name(ret));
David Hendricksfe05e742015-08-15 17:29:39 -0700575 return 1;
576 }
577 }
578
579 return 0;
580}
581
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700582static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
David Hendricksfe05e742015-08-15 17:29:39 -0700583 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
584{
585 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100586 const unsigned int chunksize = flash->chip->page_size;
David Hendricksfe05e742015-08-15 17:29:39 -0700587 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
588 unsigned int bulklen;
stepan4c06de72011-01-28 09:00:15 +0000589
Simon Glasse53cc1d2015-01-08 05:48:51 -0700590 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000591
David Hendricksfe05e742015-08-15 17:29:39 -0700592 if (chunksize != 256) {
593 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
594 "we don't know how dediprog\nhandles them.\n");
595 /* Write everything like it was residue. */
596 residue = len;
597 }
stepan4c06de72011-01-28 09:00:15 +0000598
David Hendricksfe05e742015-08-15 17:29:39 -0700599 if (residue) {
600 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
601 start, residue);
602 /* No idea about the real limit. Maybe 12, maybe more. */
603 ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 12);
604 if (ret) {
605 dediprog_set_leds(LED_ERROR);
606 return ret;
607 }
608 }
609
610 /* Round down. */
611 bulklen = (len - residue) / chunksize * chunksize;
612 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
613 if (ret) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700614 dediprog_set_leds(LED_ERROR);
David Hendricksfe05e742015-08-15 17:29:39 -0700615 return ret;
616 }
stepan4c06de72011-01-28 09:00:15 +0000617
David Hendricksfe05e742015-08-15 17:29:39 -0700618 len -= residue + bulklen;
619 if (len) {
620 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
621 start, len);
622 ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen),
623 start + residue + bulklen, len, 12);
624 if (ret) {
625 dediprog_set_leds(LED_ERROR);
626 return ret;
627 }
628 }
629
630 dediprog_set_leds(LED_PASS);
631 return 0;
hailfinger556e9c32010-11-23 21:28:16 +0000632}
633
Patrick Georgiab8353e2017-02-03 18:32:01 +0100634static 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 -0700635{
636 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
637}
638
639#if 0
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700640static 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 -0700641{
642 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
643}
644#endif
645
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700646//static int dediprog_spi_send_command(struct flashctx *flash,
647static int dediprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
David Hendricksfe05e742015-08-15 17:29:39 -0700648 unsigned int readcnt,
649 const unsigned char *writearr,
650 unsigned char *readarr)
hailfingerdfb32a02010-01-19 11:15:48 +0000651{
652 int ret;
653
hailfingeraf389cc2010-01-22 02:53:30 +0000654 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
David Hendricksfe05e742015-08-15 17:29:39 -0700655 if (writecnt > UINT16_MAX) {
656 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
657 return 1;
Simon Glass543101a2015-01-08 06:28:13 -0700658 }
David Hendricksfe05e742015-08-15 17:29:39 -0700659 if (readcnt > UINT16_MAX) {
660 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
661 return 1;
662 }
663
Edward O'Callaghancb338322019-02-27 19:25:18 +1100664 unsigned int idx, value;
665 /* New protocol has options and timeout combined as value while the old one used the value field for
666 * timeout and the index field for options. */
667 if (protocol() >= PROTOCOL_V2) {
668 idx = 0;
669 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
David Hendricks14e82e52015-08-15 17:59:03 -0700670 } else {
Edward O'Callaghancb338322019-02-27 19:25:18 +1100671 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
672 value = 0;
David Hendricks14e82e52015-08-15 17:59:03 -0700673 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100674 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000675 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000676 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800677 writecnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000678 return 1;
679 }
David Hendricksfe05e742015-08-15 17:29:39 -0700680 if (readcnt == 0)
hailfingerdfb32a02010-01-19 11:15:48 +0000681 return 0;
David Hendricksfe05e742015-08-15 17:29:39 -0700682
Edward O'Callaghancb338322019-02-27 19:25:18 +1100683 /* The specifications do state the possibility to set a timeout for transceive transactions.
684 * Apparently the "timeout" is a delay, and you can use long delays to accelerate writing - in case you
685 * can predict the time needed by the previous command or so (untested). In any case, using this
686 * "feature" to set sane-looking timouts for the read below will completely trash performance with
687 * SF600 and/or firmwares >= 6.0 while they seem to be benign on SF100 with firmwares <= 5.5.2. *shrug*
688 *
689 * The specification also uses only 0 in its examples, so the lesson to learn here:
690 * "Never trust the description of an interface in the documentation but use the example code and pray."
691 const uint8_t read_timeout = 10 + readcnt/512;
692 if (protocol() >= PROTOCOL_V2) {
693 idx = 0;
694 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
695 } else {
696 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
697 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
698 }
699 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
700 */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100701 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000702 if (ret != readcnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000703 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
David Hendricksf9ecb452015-11-04 15:40:27 -0800704 readcnt, ret, libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000705 return 1;
706 }
707 return 0;
708}
709
hailfinger1ff33dc2010-07-03 11:02:10 +0000710static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000711{
712 int ret;
713 char buf[0x11];
714
hailfingerdfb32a02010-01-19 11:15:48 +0000715 /* Command Receive Device String. */
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100716 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000717 if (ret != 0x10) {
718 msg_perr("Incomplete/failed Command Receive Device String!\n");
719 return 1;
720 }
721 buf[0x10] = '\0';
722 msg_pdbg("Found a %s\n", buf);
David Hendricks6702e072015-08-15 18:09:04 -0700723 if (memcmp(buf, "SF100", 0x5) == 0)
724 dediprog_devicetype = DEV_SF100;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100725 else if (memcmp(buf, "SF200", 0x5) == 0)
726 dediprog_devicetype = DEV_SF200;
David Hendricks6702e072015-08-15 18:09:04 -0700727 else if (memcmp(buf, "SF600", 0x5) == 0)
728 dediprog_devicetype = DEV_SF600;
729 else {
Edward O'Callaghancc0bf442019-02-27 22:35:44 +1100730 msg_perr("Device not a SF100, SF200, or SF600!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000731 return 1;
732 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100733
734 int sfnum;
735 int fw[3];
736 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
737 sfnum != dediprog_devicetype) {
David Hendricks14e82e52015-08-15 17:59:03 -0700738 msg_perr("Unexpected firmware version string '%s'\n", buf);
hailfinger7a009082010-11-09 23:30:43 +0000739 return 1;
740 }
David Hendricks14e82e52015-08-15 17:59:03 -0700741 /* Only these major versions were tested. */
David Hendricks6702e072015-08-15 18:09:04 -0700742 if (fw[0] < 2 || fw[0] > 7) {
David Hendricks14e82e52015-08-15 17:59:03 -0700743 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000744 return 1;
745 }
Edward O'Callaghancb338322019-02-27 19:25:18 +1100746
stepan4c06de72011-01-28 09:00:15 +0000747 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Edward O'Callaghancb338322019-02-27 19:25:18 +1100748 if (protocol() == PROTOCOL_UNKNOWN) {
749 msg_perr("Internal error: Unable to determine protocol version.\n");
750 return 1;
751 }
David Hendricks14e82e52015-08-15 17:59:03 -0700752
hailfingerdfb32a02010-01-19 11:15:48 +0000753 return 0;
754}
755
David Hendricks91ca7ac2015-11-20 16:22:22 -0800756static int dediprog_supply_voltages[] = {
757 0, 1800, 2500, 3500,
758};
759
760static int dediprog_set_spi_flash_voltage_manual(int millivolt)
761{
762 int ret;
763 uint16_t voltage_selector;
764
765 switch (millivolt) {
766 case 0:
767 /* Admittedly this one is an assumption. */
768 voltage_selector = 0x0;
769 break;
770 case 1800:
771 voltage_selector = 0x12;
772 break;
773 case 2500:
774 voltage_selector = 0x11;
775 break;
776 case 3500:
777 voltage_selector = 0x10;
778 break;
779 default:
780 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
781 return 1;
782 }
783 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
784 millivolt % 1000);
785
786 if (voltage_selector == 0) {
787 /* Wait some time as the original driver does. */
788 programmer_delay(200 * 1000);
789 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100790 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
David Hendricks91ca7ac2015-11-20 16:22:22 -0800791 if (ret != 0x0) {
792 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
793 voltage_selector);
794 return 1;
795 }
796 if (voltage_selector != 0) {
797 /* Wait some time as the original driver does. */
798 programmer_delay(200 * 1000);
799 }
800 return 0;
801}
802
David Hendricks93784b42016-08-09 17:00:38 -0700803static int dediprog_set_spi_flash_voltage_auto(void)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800804{
David Hendricksac1d25c2016-08-09 17:00:58 -0700805 int i;
David Hendricks91ca7ac2015-11-20 16:22:22 -0800806 int spi_flash_ranges;
807
808 spi_flash_ranges = flash_supported_voltage_ranges(BUS_SPI);
809 if (spi_flash_ranges < 0)
810 return -1;
811
812 for (i = 0; i < ARRAY_SIZE(dediprog_supply_voltages); i++) {
813 int j;
814 int v = dediprog_supply_voltages[i]; /* shorthand */
815
816 for (j = 0; j < spi_flash_ranges; j++) {
817 /* Dediprog can supply a voltage in this range. */
818 if ((v >= voltage_ranges[j].min) && (v <= voltage_ranges[j].max)) {
David Hendricks93784b42016-08-09 17:00:38 -0700819 struct flashctx dummy;
820
David Hendricks91ca7ac2015-11-20 16:22:22 -0800821 msg_cdbg("%s: trying %d\n", __func__, v);
822 if (dediprog_set_spi_flash_voltage_manual(v)) {
823 msg_cerr("%s: Failed to set SPI voltage\n", __func__);
824 return 1;
825 }
826
827 clear_spi_id_cache();
David Hendricksac1d25c2016-08-09 17:00:58 -0700828 if (probe_flash(0, &dummy, 0) < 0) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800829 /* No dice, try next voltage supported by Dediprog. */
830 break;
831 }
832
Patrick Georgif3fa2992017-02-02 16:24:44 +0100833 if ((dummy.chip->manufacture_id == GENERIC_MANUF_ID) ||
834 (dummy.chip->model_id == GENERIC_DEVICE_ID)) {
David Hendricks91ca7ac2015-11-20 16:22:22 -0800835 break;
836 }
837
838 return 0;
839 }
840 }
841 }
842
843 return 1;
844}
845
846/* FIXME: ugly function signature */
David Hendricks93784b42016-08-09 17:00:38 -0700847static int dediprog_set_spi_voltage(int millivolt, int probe)
David Hendricks91ca7ac2015-11-20 16:22:22 -0800848{
849 if (probe)
David Hendricks93784b42016-08-09 17:00:38 -0700850 return dediprog_set_spi_flash_voltage_auto();
David Hendricks91ca7ac2015-11-20 16:22:22 -0800851 else
852 return dediprog_set_spi_flash_voltage_manual(millivolt);
853}
854
David Hendricks28fbdfb2015-08-15 18:04:37 -0700855/*
856 * This command presumably sets the voltage for the SF100 itself (not the
857 * SPI flash). Only use this command with firmware older than V6.0.0. Newer
858 * (including all SF600s) do not support it.
859 */
860static int dediprog_set_voltage(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000861{
862 int ret;
Edward O'Callaghane749db02019-03-01 08:50:47 +1100863 unsigned char buf[1] = {0};
David Hendricksa7f99ac2015-11-12 16:42:03 -0800864 ret = dediprog_read_other(CMD_SET_VOLTAGE, 0x0, 0x0, buf, 0x1);
hailfinger7a009082010-11-09 23:30:43 +0000865 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800866 msg_perr("Command A failed (%s)!\n", libusb_error_name(ret));
hailfinger7a009082010-11-09 23:30:43 +0000867 return 1;
868 }
hailfingerdfb32a02010-01-19 11:15:48 +0000869 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -0700870 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000871 return 1;
872 }
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100873
hailfingerdfb32a02010-01-19 11:15:48 +0000874 return 0;
875}
876
David Hendrickscf453d82015-11-16 20:19:57 -0800877static int dediprog_leave_standalone_mode(void)
878{
879 int ret;
880
881 if (dediprog_devicetype != DEV_SF600)
882 return 0;
883
884 msg_pdbg("Leaving standalone mode\n");
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100885 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
David Hendrickscf453d82015-11-16 20:19:57 -0800886 if (ret) {
887 msg_perr("Failed to leave standalone mode (%s)!\n", libusb_error_name(ret));
888 return 1;
889 }
890
891 return 0;
892}
893
David Hendricksfe05e742015-08-15 17:29:39 -0700894static int set_target_flash(enum dediprog_target target)
895{
Edward O'Callaghan0ea95ea2019-02-27 22:17:15 +1100896 int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0);
David Hendricksfe05e742015-08-15 17:29:39 -0700897 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -0800898 msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret));
hailfingerdfb32a02010-01-19 11:15:48 +0000899 return 1;
900 }
901 return 0;
902}
903
David Hendricksfe05e742015-08-15 17:29:39 -0700904#if 0
905/* Returns true if the button is currently pressed. */
906static bool dediprog_get_button(void)
Simon Glassd01002b2015-01-08 05:44:16 -0700907{
David Hendricksfe05e742015-08-15 17:29:39 -0700908 char buf[1];
909 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
910 buf, 0x1, DEFAULT_TIMEOUT);
911 if (ret != 0) {
912 msg_perr("Could not get button state (%s)!\n", usb_strerror());
913 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700914 }
David Hendricksfe05e742015-08-15 17:29:39 -0700915 return buf[0] != 1;
Simon Glassd01002b2015-01-08 05:44:16 -0700916}
David Hendricksfe05e742015-08-15 17:29:39 -0700917#endif
Simon Glassd01002b2015-01-08 05:44:16 -0700918
hailfingerf76cc322010-11-09 22:00:31 +0000919static int parse_voltage(char *voltage)
920{
921 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000922 int i;
923 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000924
925 if (!voltage || !strlen(voltage)) {
926 msg_perr("Empty voltage= specified.\n");
927 return -1;
928 }
929 millivolt = (int)strtol(voltage, &tmp, 0);
930 voltage = tmp;
931 /* Handle "," and "." as decimal point. Everything after it is assumed
932 * to be in decimal notation.
933 */
934 if ((*voltage == '.') || (*voltage == ',')) {
935 voltage++;
936 for (i = 0; i < 3; i++) {
937 fraction *= 10;
938 /* Don't advance if the current character is invalid,
939 * but continue multiplying.
940 */
941 if ((*voltage < '0') || (*voltage > '9'))
942 continue;
943 fraction += *voltage - '0';
944 voltage++;
945 }
946 /* Throw away remaining digits. */
947 voltage += strspn(voltage, "0123456789");
948 }
949 /* The remaining string must be empty or "mV" or "V". */
950 tolower_string(voltage);
951
952 /* No unit or "V". */
953 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
954 millivolt *= 1000;
955 millivolt += fraction;
956 } else if (!strncmp(voltage, "mv", 2) ||
957 !strncmp(voltage, "milliv", 6)) {
958 /* No adjustment. fraction is discarded. */
959 } else {
960 /* Garbage at the end of the string. */
961 msg_perr("Garbage voltage= specified.\n");
962 return -1;
963 }
964 return millivolt;
965}
966
David Hendricksfe05e742015-08-15 17:29:39 -0700967#if 0
968static const struct spi_master spi_master_dediprog = {
969 .type = SPI_CONTROLLER_DEDIPROG,
970 .max_data_read = MAX_DATA_UNSPECIFIED,
971 .max_data_write = MAX_DATA_UNSPECIFIED,
972 .command = dediprog_spi_send_command,
973 .multicommand = default_spi_send_multicommand,
974 .read = dediprog_spi_read,
975 .write_256 = dediprog_spi_write_256,
976 .write_aai = dediprog_spi_write_aai,
977};
978#endif
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100979static const struct spi_master spi_master_dediprog = {
uwe8d342eb2011-07-28 08:13:25 +0000980 .type = SPI_CONTROLLER_DEDIPROG,
981 .max_data_read = MAX_DATA_UNSPECIFIED,
982 .max_data_write = MAX_DATA_UNSPECIFIED,
983 .command = dediprog_spi_send_command,
984 .multicommand = default_spi_send_multicommand,
985 .read = dediprog_spi_read,
986 .write_256 = dediprog_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000987};
988
David Hendricks93784b42016-08-09 17:00:38 -0700989static int dediprog_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000990{
991 msg_pspew("%s\n", __func__);
992
David Hendricks6702e072015-08-15 18:09:04 -0700993 dediprog_devicetype = DEV_UNKNOWN;
David Hendricksfe05e742015-08-15 17:29:39 -0700994
dhendrix0ffc2eb2011-06-14 01:35:36 +0000995 /* URB 28. Command Set SPI Voltage to 0. */
David Hendricks93784b42016-08-09 17:00:38 -0700996 if (dediprog_set_spi_voltage(0x0, 0))
dhendrix0ffc2eb2011-06-14 01:35:36 +0000997 return 1;
998
David Hendricksf9ecb452015-11-04 15:40:27 -0800999 if (libusb_release_interface(dediprog_handle, 0)) {
dhendrix0ffc2eb2011-06-14 01:35:36 +00001000 msg_perr("Could not release USB interface!\n");
1001 return 1;
1002 }
David Hendricksf9ecb452015-11-04 15:40:27 -08001003 libusb_close(dediprog_handle);
1004 libusb_exit(usb_ctx);
1005
dhendrix0ffc2eb2011-06-14 01:35:36 +00001006 return 0;
1007}
1008
hailfingerdfb32a02010-01-19 11:15:48 +00001009/* URB numbers refer to the first log ever captured. */
David Hendricksac1d25c2016-08-09 17:00:58 -07001010int dediprog_init(void)
hailfingerdfb32a02010-01-19 11:15:48 +00001011{
David Hendricks98b3c572016-11-30 01:50:08 +00001012 char *voltage, *device, *spispeed, *target_str;
David Hendricksfe05e742015-08-15 17:29:39 -07001013 int spispeed_idx = 1;
David Hendrickscd20ea42015-11-17 22:33:35 -08001014 int millivolt = 0;
David Hendricksfe05e742015-08-15 17:29:39 -07001015 long usedevice = 0;
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001016 long target = FLASH_TYPE_APPLICATION_FLASH_1;
David Hendricks98b3c572016-11-30 01:50:08 +00001017 int i, ret;
hailfingerdfb32a02010-01-19 11:15:48 +00001018
1019 msg_pspew("%s\n", __func__);
1020
David Hendricksfe05e742015-08-15 17:29:39 -07001021 spispeed = extract_programmer_param("spispeed");
1022 if (spispeed) {
1023 for (i = 0; spispeeds[i].name; ++i) {
1024 if (!strcasecmp(spispeeds[i].name, spispeed)) {
1025 spispeed_idx = i;
1026 break;
1027 }
1028 }
1029 if (!spispeeds[i].name) {
1030 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
David Hendricks98b3c572016-11-30 01:50:08 +00001031 free(spispeed);
1032 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001033 }
David Hendricks98b3c572016-11-30 01:50:08 +00001034 free(spispeed);
David Hendricksfe05e742015-08-15 17:29:39 -07001035 }
1036
hailfingerf76cc322010-11-09 22:00:31 +00001037 voltage = extract_programmer_param("voltage");
1038 if (voltage) {
1039 millivolt = parse_voltage(voltage);
1040 free(voltage);
David Hendricks98b3c572016-11-30 01:50:08 +00001041 if (millivolt < 0)
1042 return 1;
hailfingerf76cc322010-11-09 22:00:31 +00001043 msg_pinfo("Setting voltage to %i mV\n", millivolt);
1044 }
David Hendricksfe05e742015-08-15 17:29:39 -07001045
1046 device = extract_programmer_param("device");
1047 if (device) {
1048 char *dev_suffix;
1049 errno = 0;
1050 usedevice = strtol(device, &dev_suffix, 10);
1051 if (errno != 0 || device == dev_suffix) {
1052 msg_perr("Error: Could not convert 'device'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001053 free(device);
1054 return 1;
Simon Glassd01002b2015-01-08 05:44:16 -07001055 }
David Hendricksfe05e742015-08-15 17:29:39 -07001056 if (usedevice < 0 || usedevice > UINT_MAX) {
1057 msg_perr("Error: Value for 'device' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001058 free(device);
1059 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001060 }
1061 if (strlen(dev_suffix) > 0) {
1062 msg_perr("Error: Garbage following 'device' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001063 free(device);
1064 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001065 }
1066 msg_pinfo("Using device %li.\n", usedevice);
Simon Glassd01002b2015-01-08 05:44:16 -07001067 }
David Hendricks98b3c572016-11-30 01:50:08 +00001068 free(device);
David Hendricksfe05e742015-08-15 17:29:39 -07001069
1070 target_str = extract_programmer_param("target");
1071 if (target_str) {
1072 char *target_suffix;
1073 errno = 0;
1074 target = strtol(target_str, &target_suffix, 10);
1075 if (errno != 0 || target_str == target_suffix) {
1076 msg_perr("Error: Could not convert 'target'.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001077 free(target_str);
1078 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001079 }
1080 if (target < 1 || target > 2) {
1081 msg_perr("Error: Value for 'target' is out of range.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001082 free(target_str);
1083 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001084 }
1085 if (strlen(target_suffix) > 0) {
1086 msg_perr("Error: Garbage following 'target' value.\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001087 free(target_str);
1088 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001089 }
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001090 switch (target) {
1091 case 1:
1092 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_1");
1093 target = FLASH_TYPE_APPLICATION_FLASH_1;
1094 break;
1095 case 2:
1096 msg_pinfo("Using target %s.\n", "FLASH_TYPE_APPLICATION_FLASH_2");
1097 target = FLASH_TYPE_APPLICATION_FLASH_2;
1098 break;
1099 default:
1100 break;
1101 }
David Hendricksfe05e742015-08-15 17:29:39 -07001102 }
David Hendricks98b3c572016-11-30 01:50:08 +00001103 free(target_str);
hailfingerf76cc322010-11-09 22:00:31 +00001104
hailfingerdfb32a02010-01-19 11:15:48 +00001105 /* Here comes the USB stuff. */
David Hendricksf9ecb452015-11-04 15:40:27 -08001106 libusb_init(&usb_ctx);
1107 if (!usb_ctx) {
1108 msg_perr("Could not initialize libusb!\n");
David Hendricks98b3c572016-11-30 01:50:08 +00001109 return 1;
hailfingerdfb32a02010-01-19 11:15:48 +00001110 }
Edward O'Callaghana88395f2019-02-27 18:44:04 +11001111 const uint16_t vid = devs_dediprog[0].vendor_id;
1112 const uint16_t pid = devs_dediprog[0].device_id;
1113 dediprog_handle = usb_dev_get_by_vid_pid_number(usb_ctx, vid, pid, (unsigned int) usedevice);
David Hendricksfe05e742015-08-15 17:29:39 -07001114 if (!dediprog_handle) {
David Hendricks22593802015-11-13 12:59:30 -08001115 msg_perr("Could not find a Dediprog programmer on USB!\n");
David Hendricksf9ecb452015-11-04 15:40:27 -08001116 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001117 return 1;
David Hendricksfe05e742015-08-15 17:29:39 -07001118 }
David Hendricks98b3c572016-11-30 01:50:08 +00001119 ret = libusb_set_configuration(dediprog_handle, 1);
1120 if (ret != 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001121 msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret));
1122 libusb_close(dediprog_handle);
1123 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001124 return 1;
hailfingerfb6287d2010-11-16 21:25:29 +00001125 }
David Hendricks98b3c572016-11-30 01:50:08 +00001126 ret = libusb_claim_interface(dediprog_handle, 0);
1127 if (ret < 0) {
David Hendricksf9ecb452015-11-04 15:40:27 -08001128 msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret));
1129 libusb_close(dediprog_handle);
1130 libusb_exit(usb_ctx);
David Hendricks98b3c572016-11-30 01:50:08 +00001131 return 1;
hailfingerfb6287d2010-11-16 21:25:29 +00001132 }
David Hendricksfe05e742015-08-15 17:29:39 -07001133
David Hendricks98b3c572016-11-30 01:50:08 +00001134 if (register_shutdown(dediprog_shutdown, NULL))
1135 return 1;
dhendrix0ffc2eb2011-06-14 01:35:36 +00001136
David Hendricks28fbdfb2015-08-15 18:04:37 -07001137 /* Try reading the devicestring. If that fails and the device is old
1138 * (FW < 6.0.0) then we need to try the "set voltage" command and then
1139 * attempt to read the devicestring again. */
1140 if (dediprog_check_devicestring()) {
David Hendricks98b3c572016-11-30 01:50:08 +00001141 if (dediprog_set_voltage())
1142 return 1;
1143 if (dediprog_check_devicestring())
1144 return 1;
David Hendricks28fbdfb2015-08-15 18:04:37 -07001145 }
David Hendricksfe05e742015-08-15 17:29:39 -07001146
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001147 /* SF100/SF200 uses one in/out endpoint, SF600 uses separate in/out endpoints */
David Hendricksd7468582015-11-12 15:21:12 -08001148 dediprog_in_endpoint = 2;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001149 switch (dediprog_devicetype) {
1150 case DEV_SF100:
1151 case DEV_SF200:
David Hendricksd7468582015-11-12 15:21:12 -08001152 dediprog_out_endpoint = 2;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001153 break;
1154 default:
David Hendricksd7468582015-11-12 15:21:12 -08001155 dediprog_out_endpoint = 1;
Edward O'Callaghancc0bf442019-02-27 22:35:44 +11001156 break;
1157 }
David Hendricksd7468582015-11-12 15:21:12 -08001158
David Hendricks93784b42016-08-09 17:00:38 -07001159
David Hendrickse4c73662015-11-20 16:26:07 -08001160 /* Set some LEDs as soon as possible to indicate activity.
David Hendricksfe05e742015-08-15 17:29:39 -07001161 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
Edward O'Callaghancb338322019-02-27 19:25:18 +11001162 * dediprog_setup() has queried the device. */
David Hendrickse4c73662015-11-20 16:26:07 -08001163 dediprog_set_leds(LED_PASS | LED_BUSY);
David Hendricksfe05e742015-08-15 17:29:39 -07001164
David Hendrickscd20ea42015-11-17 22:33:35 -08001165 /* FIXME: need to do this so buses_supported gets SPI */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +01001166 register_spi_master(&spi_master_dediprog);
David Hendricks93784b42016-08-09 17:00:38 -07001167
David Hendricksfe05e742015-08-15 17:29:39 -07001168 /* Select target/socket, frequency and VCC. */
Edward O'Callaghan16c77402019-02-27 22:56:22 +11001169 if (set_target_flash(target) ||
David Hendricksfe05e742015-08-15 17:29:39 -07001170 dediprog_set_spi_speed(spispeed_idx) ||
David Hendricks93784b42016-08-09 17:00:38 -07001171 dediprog_set_spi_voltage(millivolt, voltage ? 0 : 1)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -07001172 dediprog_set_leds(LED_ERROR);
David Hendricks98b3c572016-11-30 01:50:08 +00001173 return 1;
stepan4c06de72011-01-28 09:00:15 +00001174 }
hailfingerdfb32a02010-01-19 11:15:48 +00001175
David Hendricks98b3c572016-11-30 01:50:08 +00001176 if (dediprog_leave_standalone_mode())
1177 return 1;
1178
David Hendricks93784b42016-08-09 17:00:38 -07001179
David Hendricksfe05e742015-08-15 17:29:39 -07001180 dediprog_set_leds(LED_NONE);
stepan4c06de72011-01-28 09:00:15 +00001181
David Hendricks98b3c572016-11-30 01:50:08 +00001182 return 0;
hailfingerdfb32a02010-01-19 11:15:48 +00001183}