blob: d18ce36d3f88858ac23b8a4ab6bfb6d397db4664 [file] [log] [blame]
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
Simon Glass557eb4f2015-07-05 16:53:22 +00005 * Copyright (C) 2015 Simon Glass
6 * Copyright (C) 2015 Stefan Tauner
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000022#include "platform.h"
23
Stefan Tauner5c316f92015-02-08 21:57:52 +000024#include <stdlib.h>
Mathias Krausedb7afc52010-11-09 23:30:43 +000025#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000026#include <string.h>
Stefan Taunere34e3e82013-01-01 00:06:51 +000027#include <limits.h>
Nathan Laredo21541a62012-12-24 22:07:36 +000028#include <errno.h>
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000029
30#if IS_WINDOWS
31#include <lusb0_usb.h>
32#else
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000033#include <usb.h>
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000034#endif
35
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000036#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000037#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000038#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000039#include "spi.h"
40
Stefan Reinauer915b8402011-01-28 09:00:15 +000041#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000042#define DEFAULT_TIMEOUT 3000
Simon Glassae616512016-01-23 23:27:58 +000043#define REQTYPE_OTHER_OUT (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* 0x43 */
Simon Glass557eb4f2015-07-05 16:53:22 +000044#define REQTYPE_OTHER_IN (USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* 0xC3 */
45#define REQTYPE_EP_OUT (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT) /* 0x42 */
46#define REQTYPE_EP_IN (USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT) /* 0xC2 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000047static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000048static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000049
Simon Glass25e9f402015-06-28 13:31:19 +000050enum dediprog_leds {
51 LED_INVALID = -1,
52 LED_NONE = 0,
53 LED_PASS = 1 << 0,
54 LED_BUSY = 1 << 1,
55 LED_ERROR = 1 << 2,
56 LED_ALL = 7,
57};
58
Simon Glass557eb4f2015-07-05 16:53:22 +000059/* IO bits for CMD_SET_IO_LED message */
60enum dediprog_ios {
61 IO1 = 1 << 0,
62 IO2 = 1 << 1,
63 IO3 = 1 << 2,
64 IO4 = 1 << 3,
65};
66
67enum dediprog_cmds {
68 CMD_TRANSCEIVE = 0x01,
69 CMD_POLL_STATUS_REG = 0x02,
70 CMD_SET_VPP = 0x03,
71 CMD_SET_TARGET = 0x04,
72 CMD_READ_EEPROM = 0x05,
73 CMD_WRITE_EEPROM = 0x06,
74 CMD_SET_IO_LED = 0x07,
75 CMD_READ_PROG_INFO = 0x08,
76 CMD_SET_VCC = 0x09,
77 CMD_SET_STANDALONE = 0x0A,
78 CMD_GET_BUTTON = 0x11,
79 CMD_GET_UID = 0x12,
80 CMD_SET_CS = 0x14,
81 CMD_IO_MODE = 0x15,
82 CMD_FW_UPDATE = 0x1A,
83 CMD_FPGA_UPDATE = 0x1B,
84 CMD_READ_FPGA_VERSION = 0x1C,
85 CMD_SET_HOLD = 0x1D,
86 CMD_READ = 0x20,
87 CMD_WRITE = 0x30,
88 CMD_WRITE_AT45DB = 0x31,
89 CMD_NAND_WRITE = 0x32,
90 CMD_NAND_READ = 0x33,
91 CMD_SET_SPI_CLK = 0x61,
92 CMD_CHECK_SOCKET = 0x62,
93 CMD_DOWNLOAD_PRJ = 0x63,
94 CMD_READ_PRJ_NAME = 0x64,
95 // New protocol/firmware only
96 CMD_CHECK_SDCARD = 0x65,
97 CMD_READ_PRJ = 0x66,
98};
99
100enum dediprog_target {
101 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
102 FLASH_TYPE_FLASH_CARD,
103 FLASH_TYPE_APPLICATION_FLASH_2,
104 FLASH_TYPE_SOCKET,
105};
106
107enum dediprog_readmode {
108 READ_MODE_STD = 1,
109 READ_MODE_FAST = 2,
110 READ_MODE_ATMEL45 = 3,
111 READ_MODE_4B_ADDR_FAST = 4,
112 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
113};
114
115enum dediprog_writemode {
116 WRITE_MODE_PAGE_PGM = 1,
117 WRITE_MODE_PAGE_WRITE = 2,
118 WRITE_MODE_1B_AAI = 3,
119 WRITE_MODE_2B_AAI = 4,
120 WRITE_MODE_128B_PAGE = 5,
121 WRITE_MODE_PAGE_AT26DF041 = 6,
122 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000123 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000124 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000125 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000126 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
127 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
128};
129
130
131static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
132
Simon Glassae616512016-01-23 23:27:58 +0000133/* Returns true if firmware (and thus hardware) supports the "new" protocol */
134static bool is_new_prot(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000135{
Simon Glassae616512016-01-23 23:27:58 +0000136 /* if (SF100) */
137 return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
138 /* else if (SF600)
139 return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0); */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000140}
Simon Glassae616512016-01-23 23:27:58 +0000141
142static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
143{
144 return usb_control_msg(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
145 (char *)bytes, size, DEFAULT_TIMEOUT);
146}
147
148static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
149{
150 return usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
151 (char *)bytes, size, DEFAULT_TIMEOUT);
152}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000153
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000154/* Might be useful for other USB devices as well. static for now. */
Nathan Laredo21541a62012-12-24 22:07:36 +0000155/* device parameter allows user to specify one device of multiple installed */
156static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000157{
158 struct usb_bus *bus;
159 struct usb_device *dev;
160
161 for (bus = usb_get_busses(); bus; bus = bus->next)
162 for (dev = bus->devices; dev; dev = dev->next)
163 if ((dev->descriptor.idVendor == vid) &&
Nathan Laredo21541a62012-12-24 22:07:36 +0000164 (dev->descriptor.idProduct == pid)) {
165 if (device == 0)
166 return dev;
167 device--;
168 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000169
170 return NULL;
171}
172
Simon Glass25e9f402015-06-28 13:31:19 +0000173/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000174static int dediprog_set_leds(int leds)
175{
Simon Glass25e9f402015-06-28 13:31:19 +0000176 if (leds < LED_NONE || leds > LED_ALL)
177 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000178
Simon Glassae616512016-01-23 23:27:58 +0000179 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
180 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000181 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000182 * bit 0 == 0: red light is on.
183 *
184 * Additionally, the command structure has changed with the "new" protocol.
185 *
186 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000187 */
Simon Glassae616512016-01-23 23:27:58 +0000188 int target_leds, ret;
189 if (is_new_prot()) {
190 target_leds = (leds ^ 7) << 8;
191 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000192 } else {
Simon Glassae616512016-01-23 23:27:58 +0000193 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
194 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
195 } else {
196 target_leds = leds;
197 }
198 target_leds ^= 7;
199
200 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000201 }
202
Stefan Reinauer915b8402011-01-28 09:00:15 +0000203 if (ret != 0x0) {
Simon Glass25e9f402015-06-28 13:31:19 +0000204 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000205 return 1;
206 }
207
Stefan Reinauer915b8402011-01-28 09:00:15 +0000208 return 0;
209}
210
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000211static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000212{
213 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000214 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000215
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000216 switch (millivolt) {
217 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000218 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000219 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000220 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000221 case 1800:
222 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000223 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000224 case 2500:
225 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000226 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000227 case 3500:
228 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000229 break;
230 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000231 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000232 return 1;
233 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000234 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
235 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000236
Nico Huber4099a8a2012-06-16 00:02:27 +0000237 if (voltage_selector == 0) {
238 /* Wait some time as the original driver does. */
239 programmer_delay(200 * 1000);
240 }
Simon Glassae616512016-01-23 23:27:58 +0000241 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000242 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000243 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
244 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000245 return 1;
246 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000247 if (voltage_selector != 0) {
248 /* Wait some time as the original driver does. */
249 programmer_delay(200 * 1000);
250 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000251 return 0;
252}
253
Nico Huber77fa67d2013-02-20 18:03:36 +0000254struct dediprog_spispeeds {
255 const char *const name;
256 const int speed;
257};
258
259static const struct dediprog_spispeeds spispeeds[] = {
260 { "24M", 0x0 },
261 { "12M", 0x2 },
262 { "8M", 0x1 },
263 { "3M", 0x3 },
264 { "2.18M", 0x4 },
265 { "1.5M", 0x5 },
266 { "750k", 0x6 },
267 { "375k", 0x7 },
268 { NULL, 0x0 },
269};
270
Nico Huber77fa67d2013-02-20 18:03:36 +0000271static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000272{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000273 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
274 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
275 return 0;
276 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000277
Simon Glass557eb4f2015-07-05 16:53:22 +0000278 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
279 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000280
Simon Glassae616512016-01-23 23:27:58 +0000281 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000282 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000283 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000284 return 1;
285 }
286 return 0;
287}
288
Simon Glassae616512016-01-23 23:27:58 +0000289static void fill_rw_cmd_payload(uint8_t *data_packet, unsigned int count, uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start) {
290 /* First 5 bytes are common in both generations. */
291 data_packet[0] = count & 0xff;
292 data_packet[1] = (count >> 8) & 0xff;
293 data_packet[2] = 0; /* RFU */
294 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
295 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
296
297 if (is_new_prot()) {
298 *value = *idx = 0;
299 data_packet[5] = 0; /* RFU */
300 data_packet[6] = (start >> 0) & 0xff;
301 data_packet[7] = (start >> 8) & 0xff;
302 data_packet[8] = (start >> 16) & 0xff;
303 data_packet[9] = (start >> 24) & 0xff;
304 } else {
305 *value = start % 0x10000;
306 *idx = start / 0x10000;
307 }
308}
309
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000310/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
311 * @start start address
312 * @len length
313 * @return 0 on success, 1 on failure
314 */
Simon Glassae616512016-01-23 23:27:58 +0000315static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000316{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000317 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000318 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000319 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000320
321 if ((start % chunksize) || (len % chunksize)) {
Simon Glassae616512016-01-23 23:27:58 +0000322 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
323 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000324 return 1;
325 }
326
Simon Glassae616512016-01-23 23:27:58 +0000327 if (len == 0)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000328 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000329
330 /* Command packet size of protocols: new 10 B, old 5 B. */
331 uint8_t data_packet[is_new_prot() ? 10 : 5];
332 unsigned int value, idx;
333 fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start);
334
335 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
336 if (ret != sizeof(data_packet)) {
337 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, usb_strerror());
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000338 return 1;
339 }
340
Simon Glassae616512016-01-23 23:27:58 +0000341 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000342 for (i = 0; i < count; i++) {
343 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
344 (char *)buf + i * chunksize, chunksize,
345 DEFAULT_TIMEOUT);
346 if (ret != chunksize) {
Simon Glassae616512016-01-23 23:27:58 +0000347 msg_perr("SPI bulk read %i failed, expected %i, got %i %s!\n",
348 i, chunksize, ret, usb_strerror());
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000349 return 1;
350 }
351 }
352
353 return 0;
354}
355
Simon Glassae616512016-01-23 23:27:58 +0000356static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000357{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000358 int ret;
359 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000360 const unsigned int chunksize = 0x200;
361 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
362 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000363
Simon Glass25e9f402015-06-28 13:31:19 +0000364 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000365
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000366 if (residue) {
367 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
368 start, residue);
369 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000370 if (ret)
371 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000372 }
373
374 /* Round down. */
375 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000376 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000377 if (ret)
378 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000379
380 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000381 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000382 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
383 start, len);
384 ret = spi_read_chunked(flash, buf + residue + bulklen,
385 start + residue + bulklen, len, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000386 if (ret)
387 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000388 }
389
Simon Glass25e9f402015-06-28 13:31:19 +0000390 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000391 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000392err:
393 dediprog_set_leds(LED_ERROR);
394 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000395}
396
Nico Hubera4b14f72012-06-19 12:06:53 +0000397/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
398 * @chunksize length of data chunks, only 256 supported by now
399 * @start start address
400 * @len length
401 * @dedi_spi_cmd dediprog specific write command for spi bus
402 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000403 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000404static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000405 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000406{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000407 /* USB transfer size must be 512, other sizes will NOT work at all.
408 * chunksize is the real data size per USB bulk transfer. The remaining
409 * space in a USB bulk transfer must be filled with 0xff padding.
410 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000411 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000412
Nico Hubera4b14f72012-06-19 12:06:53 +0000413 /*
414 * We should change this check to
415 * chunksize > 512
416 * once we know how to handle different chunk sizes.
417 */
418 if (chunksize != 256) {
419 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
420 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
421 return 1;
422 }
423
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000424 if ((start % chunksize) || (len % chunksize)) {
425 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
426 "at flashrom@flashrom.org\n", __func__, start, len);
427 return 1;
428 }
429
430 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000431 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000432 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000433
434 /* Command packet size of protocols: new 10 B, old 5 B. */
435 uint8_t data_packet[is_new_prot() ? 10 : 5];
436 unsigned int value, idx;
437 fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start);
438 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
439 if (ret != sizeof(data_packet)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000440 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
441 usb_strerror());
442 return 1;
443 }
444
Simon Glassae616512016-01-23 23:27:58 +0000445 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000446 for (i = 0; i < count; i++) {
Simon Glassae616512016-01-23 23:27:58 +0000447 char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000448 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000449 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
450 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint, usbbuf, 512, DEFAULT_TIMEOUT);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000451 if (ret != 512) {
Simon Glassae616512016-01-23 23:27:58 +0000452 msg_perr("SPI bulk write failed, expected %i, got %i %s!\n", 512, ret, usb_strerror());
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000453 return 1;
454 }
455 }
456
457 return 0;
458}
459
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000460static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000461 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000462{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000463 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000464 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000465 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
466 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000467
Simon Glass25e9f402015-06-28 13:31:19 +0000468 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000469
Nico Hubera4b14f72012-06-19 12:06:53 +0000470 if (chunksize != 256) {
471 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
472 "we don't know how dediprog\nhandles them.\n");
473 /* Write everything like it was residue. */
474 residue = len;
475 }
476
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000477 if (residue) {
478 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
479 start, residue);
480 /* No idea about the real limit. Maybe 12, maybe more. */
481 ret = spi_write_chunked(flash, buf, start, residue, 12);
482 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000483 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000484 return ret;
485 }
486 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000487
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000488 /* Round down. */
489 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000490 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000491 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000492 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000493 return ret;
494 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000495
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000496 len -= residue + bulklen;
497 if (len) {
498 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
499 start, len);
500 ret = spi_write_chunked(flash, buf + residue + bulklen,
501 start + residue + bulklen, len, 12);
502 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000503 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000504 return ret;
505 }
506 }
507
Simon Glass25e9f402015-06-28 13:31:19 +0000508 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000509 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000510}
511
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000512static int dediprog_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Nico Hubera4b14f72012-06-19 12:06:53 +0000513{
Simon Glass557eb4f2015-07-05 16:53:22 +0000514 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000515}
516
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000517static int dediprog_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Nico Hubera4b14f72012-06-19 12:06:53 +0000518{
Simon Glass557eb4f2015-07-05 16:53:22 +0000519 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000520}
521
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000522static int dediprog_spi_send_command(struct flashctx *flash,
523 unsigned int writecnt,
524 unsigned int readcnt,
525 const unsigned char *writearr,
526 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000527{
528 int ret;
529
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000530 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glassae616512016-01-23 23:27:58 +0000531 if (writecnt > flash->mst->spi.max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000532 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000533 return 1;
534 }
Simon Glassae616512016-01-23 23:27:58 +0000535 if (readcnt > flash->mst->spi.max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000536 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000537 return 1;
538 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000539
Simon Glassae616512016-01-23 23:27:58 +0000540 unsigned int idx, value;
541 /* New protocol has options and timeout combined as value while the old one used the value field for
542 * timeout and the index field for options. */
543 if (is_new_prot()) {
544 idx = 0;
545 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
546 } else {
547 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
548 value = 0;
549 }
550 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000551 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000552 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
553 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000554 return 1;
555 }
Simon Glassae616512016-01-23 23:27:58 +0000556 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000557 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000558
Simon Glassae616512016-01-23 23:27:58 +0000559 const uint8_t read_timeout = 10 + readcnt/512;
560 if (is_new_prot()) {
561 idx = 0;
562 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
563 } else {
564 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
565 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
566 }
567 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000568 if (ret != readcnt) {
Simon Glassae616512016-01-23 23:27:58 +0000569 msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000570 return 1;
571 }
572 return 0;
573}
574
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000575static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000576{
577 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000578 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000579 char buf[0x11];
580
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000581 /* Command Receive Device String. */
Simon Glassae616512016-01-23 23:27:58 +0000582 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000583 if (ret != 0x10) {
584 msg_perr("Incomplete/failed Command Receive Device String!\n");
585 return 1;
586 }
587 buf[0x10] = '\0';
588 msg_pdbg("Found a %s\n", buf);
Simon Glass557eb4f2015-07-05 16:53:22 +0000589 if (memcmp(buf, "SF100", 0x5) != 0) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000590 msg_perr("Device not a SF100!\n");
591 return 1;
592 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000593 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
Simon Glassae616512016-01-23 23:27:58 +0000594 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000595 return 1;
596 }
Simon Glassae616512016-01-23 23:27:58 +0000597 /* Only these major versions were tested. */
598 if (fw[0] < 2 || fw[0] > 6) {
599 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000600 return 1;
601 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000602 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Simon Glassae616512016-01-23 23:27:58 +0000603
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000604 return 0;
605}
606
Simon Glass557eb4f2015-07-05 16:53:22 +0000607static int dediprog_device_init(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000608{
609 int ret;
610 char buf[0x1];
611
612 memset(buf, 0, sizeof(buf));
Simon Glass557eb4f2015-07-05 16:53:22 +0000613 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x0B, 0x0, 0x0,
614 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000615 if (ret < 0) {
616 msg_perr("Command A failed (%s)!\n", usb_strerror());
617 return 1;
618 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000619 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000620 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000621 return 1;
622 }
623 return 0;
624}
625
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000626#if 0
627/* Something.
628 * Present in eng_detect_blink.log with firmware 3.1.8
629 * Always preceded by Command Receive Device String
630 */
631static int dediprog_command_b(void)
632{
633 int ret;
634 char buf[0x3];
635
Simon Glass557eb4f2015-07-05 16:53:22 +0000636 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
637 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000638 if (ret < 0) {
639 msg_perr("Command B failed (%s)!\n", usb_strerror());
640 return 1;
641 }
642 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
643 (buf[2] != 0xff)) {
644 msg_perr("Unexpected response to Command B!\n");
645 return 1;
646 }
647
648 return 0;
649}
650#endif
651
Simon Glass557eb4f2015-07-05 16:53:22 +0000652static int set_target_flash(enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000653{
Simon Glass557eb4f2015-07-05 16:53:22 +0000654 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_SET_TARGET, target, 0,
655 NULL, 0, DEFAULT_TIMEOUT);
656 if (ret != 0) {
657 msg_perr("set_target_flash failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000658 return 1;
659 }
660 return 0;
661}
662
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000663#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000664/* Returns true if the button is currently pressed. */
665static bool dediprog_get_button(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000666{
Simon Glass557eb4f2015-07-05 16:53:22 +0000667 char buf[1];
668 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
669 buf, 0x1, DEFAULT_TIMEOUT);
670 if (ret != 0) {
671 msg_perr("Could not get button state (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000672 return 1;
673 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000674 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000675}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000676#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000677
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000678static int parse_voltage(char *voltage)
679{
680 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000681 int i;
682 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000683
684 if (!voltage || !strlen(voltage)) {
685 msg_perr("Empty voltage= specified.\n");
686 return -1;
687 }
688 millivolt = (int)strtol(voltage, &tmp, 0);
689 voltage = tmp;
690 /* Handle "," and "." as decimal point. Everything after it is assumed
691 * to be in decimal notation.
692 */
693 if ((*voltage == '.') || (*voltage == ',')) {
694 voltage++;
695 for (i = 0; i < 3; i++) {
696 fraction *= 10;
697 /* Don't advance if the current character is invalid,
698 * but continue multiplying.
699 */
700 if ((*voltage < '0') || (*voltage > '9'))
701 continue;
702 fraction += *voltage - '0';
703 voltage++;
704 }
705 /* Throw away remaining digits. */
706 voltage += strspn(voltage, "0123456789");
707 }
708 /* The remaining string must be empty or "mV" or "V". */
709 tolower_string(voltage);
710
711 /* No unit or "V". */
712 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
713 millivolt *= 1000;
714 millivolt += fraction;
715 } else if (!strncmp(voltage, "mv", 2) ||
716 !strncmp(voltage, "milliv", 6)) {
717 /* No adjustment. fraction is discarded. */
718 } else {
719 /* Garbage at the end of the string. */
720 msg_perr("Garbage voltage= specified.\n");
721 return -1;
722 }
723 return millivolt;
724}
725
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000726static const struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000727 .type = SPI_CONTROLLER_DEDIPROG,
Simon Glassae616512016-01-23 23:27:58 +0000728 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
729 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000730 .command = dediprog_spi_send_command,
731 .multicommand = default_spi_send_multicommand,
732 .read = dediprog_spi_read,
733 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000734 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000735};
736
David Hendricks8bb20212011-06-14 01:35:36 +0000737static int dediprog_shutdown(void *data)
738{
Simon Glass557eb4f2015-07-05 16:53:22 +0000739 dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000740
David Hendricks8bb20212011-06-14 01:35:36 +0000741 /* URB 28. Command Set SPI Voltage to 0. */
742 if (dediprog_set_spi_voltage(0x0))
743 return 1;
744
745 if (usb_release_interface(dediprog_handle, 0)) {
746 msg_perr("Could not release USB interface!\n");
747 return 1;
748 }
749 if (usb_close(dediprog_handle)) {
750 msg_perr("Could not close USB device!\n");
751 return 1;
752 }
753 return 0;
754}
755
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000756int dediprog_init(void)
757{
758 struct usb_device *dev;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000759 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +0000760 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000761 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000762 long usedevice = 0;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000763 long target = 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000764 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000765
Nico Huber77fa67d2013-02-20 18:03:36 +0000766 spispeed = extract_programmer_param("spispeed");
767 if (spispeed) {
768 for (i = 0; spispeeds[i].name; ++i) {
769 if (!strcasecmp(spispeeds[i].name, spispeed)) {
770 spispeed_idx = i;
771 break;
772 }
773 }
774 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000775 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +0000776 free(spispeed);
777 return 1;
778 }
779 free(spispeed);
780 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000781
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000782 voltage = extract_programmer_param("voltage");
783 if (voltage) {
784 millivolt = parse_voltage(voltage);
785 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000786 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000787 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000788 msg_pinfo("Setting voltage to %i mV\n", millivolt);
789 }
790
Nathan Laredo21541a62012-12-24 22:07:36 +0000791 device = extract_programmer_param("device");
792 if (device) {
793 char *dev_suffix;
794 errno = 0;
795 usedevice = strtol(device, &dev_suffix, 10);
796 if (errno != 0 || device == dev_suffix) {
797 msg_perr("Error: Could not convert 'device'.\n");
798 free(device);
799 return 1;
800 }
801 if (usedevice < 0 || usedevice > UINT_MAX) {
802 msg_perr("Error: Value for 'device' is out of range.\n");
803 free(device);
804 return 1;
805 }
806 if (strlen(dev_suffix) > 0) {
807 msg_perr("Error: Garbage following 'device' value.\n");
808 free(device);
809 return 1;
810 }
811 msg_pinfo("Using device %li.\n", usedevice);
812 }
813 free(device);
814
Stefan Taunere659d2d2013-05-03 21:58:28 +0000815 target_str = extract_programmer_param("target");
816 if (target_str) {
817 char *target_suffix;
818 errno = 0;
819 target = strtol(target_str, &target_suffix, 10);
820 if (errno != 0 || target_str == target_suffix) {
821 msg_perr("Error: Could not convert 'target'.\n");
822 free(target_str);
823 return 1;
824 }
825 if (target < 1 || target > 2) {
826 msg_perr("Error: Value for 'target' is out of range.\n");
827 free(target_str);
828 return 1;
829 }
830 if (strlen(target_suffix) > 0) {
831 msg_perr("Error: Garbage following 'target' value.\n");
832 free(target_str);
833 return 1;
834 }
835 msg_pinfo("Using target %li.\n", target);
836 }
837 free(target_str);
838
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000839 /* Here comes the USB stuff. */
840 usb_init();
841 usb_find_busses();
842 usb_find_devices();
Nathan Laredo21541a62012-12-24 22:07:36 +0000843 dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000844 if (!dev) {
845 msg_perr("Could not find a Dediprog SF100 on USB!\n");
846 return 1;
847 }
848 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000849 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000850 dediprog_handle = usb_open(dev);
David Woodhoused2a7e872013-07-30 09:34:44 +0000851 if (!dediprog_handle) {
852 msg_perr("Could not open USB device: %s\n", usb_strerror());
853 return 1;
854 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000855 ret = usb_set_configuration(dediprog_handle, 1);
856 if (ret < 0) {
857 msg_perr("Could not set USB device configuration: %i %s\n",
858 ret, usb_strerror());
859 if (usb_close(dediprog_handle))
860 msg_perr("Could not close USB device!\n");
861 return 1;
862 }
863 ret = usb_claim_interface(dediprog_handle, 0);
864 if (ret < 0) {
865 msg_perr("Could not claim USB device interface %i: %i %s\n",
866 0, ret, usb_strerror());
867 if (usb_close(dediprog_handle))
868 msg_perr("Could not close USB device!\n");
869 return 1;
870 }
871 dediprog_endpoint = 2;
Nico Huber77fa67d2013-02-20 18:03:36 +0000872
David Hendricks8bb20212011-06-14 01:35:36 +0000873 if (register_shutdown(dediprog_shutdown, NULL))
874 return 1;
875
Nico Huber77fa67d2013-02-20 18:03:36 +0000876 /* Perform basic setup. */
Simon Glass557eb4f2015-07-05 16:53:22 +0000877 if (dediprog_device_init())
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000878 return 1;
Simon Glass557eb4f2015-07-05 16:53:22 +0000879 if (dediprog_check_devicestring())
880 return 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000881
Simon Glass25e9f402015-06-28 13:31:19 +0000882 /* Set all possible LEDs as soon as possible to indicate activity.
883 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
Simon Glassae616512016-01-23 23:27:58 +0000884 * dediprog_check_devicestring() has queried the device and set dediprog_firmwareversion. */
Simon Glass25e9f402015-06-28 13:31:19 +0000885 dediprog_set_leds(LED_ALL);
886
Simon Glass557eb4f2015-07-05 16:53:22 +0000887 /* Select target/socket, frequency and VCC. */
888 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
889 dediprog_set_spi_speed(spispeed_idx) ||
890 dediprog_set_spi_voltage(millivolt)) {
Simon Glass25e9f402015-06-28 13:31:19 +0000891 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000892 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000893 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000894
Simon Glassae616512016-01-23 23:27:58 +0000895 if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE))
896 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000897
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000898 return 0;
899}
900