blob: 996d2d7d9c2385a97ada0ce4a2588f4621a74dc3 [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 Glass557eb4f2015-07-05 16:53:22 +000043#define REQTYPE_OTHER_OUT (USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* 0x43 */
44#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,
123 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of length 512 bytes */
124 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
125 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of length 512 bytes */
126 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
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000133#if 0
134/* Might be useful for other pieces of code as well. */
135static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000136{
137 size_t i;
138
139 for (i = 0; i < len; i++)
140 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
141}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000142#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000143
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000144/* Might be useful for other USB devices as well. static for now. */
Nathan Laredo21541a62012-12-24 22:07:36 +0000145/* device parameter allows user to specify one device of multiple installed */
146static 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 +0000147{
148 struct usb_bus *bus;
149 struct usb_device *dev;
150
151 for (bus = usb_get_busses(); bus; bus = bus->next)
152 for (dev = bus->devices; dev; dev = dev->next)
153 if ((dev->descriptor.idVendor == vid) &&
Nathan Laredo21541a62012-12-24 22:07:36 +0000154 (dev->descriptor.idProduct == pid)) {
155 if (device == 0)
156 return dev;
157 device--;
158 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000159
160 return NULL;
161}
162
Simon Glass25e9f402015-06-28 13:31:19 +0000163/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000164static int dediprog_set_leds(int leds)
165{
Simon Glass25e9f402015-06-28 13:31:19 +0000166 if (leds < LED_NONE || leds > LED_ALL)
167 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000168
169 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
170 * two LEDs, and they were reversed. So map them around if
171 * we have an old device. On those devices the LEDs map as
172 * follows:
173 * bit 2 == 0: green light is on.
174 * bit 0 == 0: red light is on.
175 */
Simon Glass25e9f402015-06-28 13:31:19 +0000176 int target_leds;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000177 if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
Simon Glass25e9f402015-06-28 13:31:19 +0000178 target_leds = ((leds & LED_ERROR) >> 2) |
179 ((leds & LED_PASS) << 2);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000180 } else {
181 target_leds = leds;
182 }
183
Simon Glass25e9f402015-06-28 13:31:19 +0000184 target_leds ^= 7;
Simon Glass557eb4f2015-07-05 16:53:22 +0000185 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_SET_IO_LED, 0x09, target_leds,
186 NULL, 0x0, DEFAULT_TIMEOUT);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000187 if (ret != 0x0) {
Simon Glass25e9f402015-06-28 13:31:19 +0000188 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000189 return 1;
190 }
191
Stefan Reinauer915b8402011-01-28 09:00:15 +0000192 return 0;
193}
194
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000195static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000196{
197 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000198 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000199
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000200 switch (millivolt) {
201 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000202 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000203 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000204 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000205 case 1800:
206 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000207 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000208 case 2500:
209 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000210 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000211 case 3500:
212 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000213 break;
214 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000215 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000216 return 1;
217 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000218 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
219 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000220
Nico Huber4099a8a2012-06-16 00:02:27 +0000221 if (voltage_selector == 0) {
222 /* Wait some time as the original driver does. */
223 programmer_delay(200 * 1000);
224 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000225 ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_SET_VCC, voltage_selector, 0,
226 NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000227 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000228 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
229 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000230 return 1;
231 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000232 if (voltage_selector != 0) {
233 /* Wait some time as the original driver does. */
234 programmer_delay(200 * 1000);
235 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000236 return 0;
237}
238
Nico Huber77fa67d2013-02-20 18:03:36 +0000239struct dediprog_spispeeds {
240 const char *const name;
241 const int speed;
242};
243
244static const struct dediprog_spispeeds spispeeds[] = {
245 { "24M", 0x0 },
246 { "12M", 0x2 },
247 { "8M", 0x1 },
248 { "3M", 0x3 },
249 { "2.18M", 0x4 },
250 { "1.5M", 0x5 },
251 { "750k", 0x6 },
252 { "375k", 0x7 },
253 { NULL, 0x0 },
254};
255
Nico Huber77fa67d2013-02-20 18:03:36 +0000256static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000257{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000258 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
259 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
260 return 0;
261 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000262
Simon Glass557eb4f2015-07-05 16:53:22 +0000263 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
264 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000265
Simon Glass557eb4f2015-07-05 16:53:22 +0000266 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_SET_SPI_CLK, spispeed->speed, 0xff,
Patrick Georgiefe2d432013-05-23 21:47:46 +0000267 NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000268 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000269 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000270 return 1;
271 }
272 return 0;
273}
274
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000275/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
276 * @start start address
277 * @len length
278 * @return 0 on success, 1 on failure
279 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000280static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000281 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000282{
283 int ret;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000284 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000285 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000286 const unsigned int chunksize = 0x200;
287 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000288 const char count_and_chunk[] = {count & 0xff,
289 (count >> 8) & 0xff,
290 chunksize & 0xff,
291 (chunksize >> 8) & 0xff};
292
293 if ((start % chunksize) || (len % chunksize)) {
294 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
295 "at flashrom@flashrom.org\n", __func__, start, len);
296 return 1;
297 }
298
299 /* No idea if the hardware can handle empty reads, so chicken out. */
300 if (!len)
301 return 0;
302 /* Command Read SPI Bulk. No idea which read command is used on the
303 * SPI side.
304 */
Simon Glass557eb4f2015-07-05 16:53:22 +0000305 ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_READ, start % 0x10000,
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000306 start / 0x10000, (char *)count_and_chunk,
307 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
308 if (ret != sizeof(count_and_chunk)) {
309 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
310 usb_strerror());
311 return 1;
312 }
313
314 for (i = 0; i < count; i++) {
315 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
316 (char *)buf + i * chunksize, chunksize,
317 DEFAULT_TIMEOUT);
318 if (ret != chunksize) {
319 msg_perr("SPI bulk read %i failed, expected %i, got %i "
320 "%s!\n", i, chunksize, ret, usb_strerror());
321 return 1;
322 }
323 }
324
325 return 0;
326}
327
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000328static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000329 unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000330{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000331 int ret;
332 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000333 const unsigned int chunksize = 0x200;
334 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
335 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000336
Simon Glass25e9f402015-06-28 13:31:19 +0000337 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000338
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000339 if (residue) {
340 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
341 start, residue);
342 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000343 if (ret)
344 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000345 }
346
347 /* Round down. */
348 bulklen = (len - residue) / chunksize * chunksize;
349 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
350 bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000351 if (ret)
352 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000353
354 len -= residue + bulklen;
355 if (len) {
356 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
357 start, len);
358 ret = spi_read_chunked(flash, buf + residue + bulklen,
359 start + residue + bulklen, len, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000360 if (ret)
361 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000362 }
363
Simon Glass25e9f402015-06-28 13:31:19 +0000364 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000365 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000366err:
367 dediprog_set_leds(LED_ERROR);
368 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000369}
370
Nico Hubera4b14f72012-06-19 12:06:53 +0000371/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
372 * @chunksize length of data chunks, only 256 supported by now
373 * @start start address
374 * @len length
375 * @dedi_spi_cmd dediprog specific write command for spi bus
376 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000377 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000378static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000379 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000380{
381 int ret;
382 unsigned int i;
383 /* USB transfer size must be 512, other sizes will NOT work at all.
384 * chunksize is the real data size per USB bulk transfer. The remaining
385 * space in a USB bulk transfer must be filled with 0xff padding.
386 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000387 const unsigned int count = len / chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000388 const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000389 char usbbuf[512];
390
Nico Hubera4b14f72012-06-19 12:06:53 +0000391 /*
392 * We should change this check to
393 * chunksize > 512
394 * once we know how to handle different chunk sizes.
395 */
396 if (chunksize != 256) {
397 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
398 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
399 return 1;
400 }
401
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000402 if ((start % chunksize) || (len % chunksize)) {
403 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
404 "at flashrom@flashrom.org\n", __func__, start, len);
405 return 1;
406 }
407
408 /* No idea if the hardware can handle empty writes, so chicken out. */
409 if (!len)
410 return 0;
411 /* Command Write SPI Bulk. No idea which write command is used on the
412 * SPI side.
413 */
Simon Glass557eb4f2015-07-05 16:53:22 +0000414 ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_WRITE, start % 0x10000, start / 0x10000,
Nico Hubera4b14f72012-06-19 12:06:53 +0000415 (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT);
416 if (ret != sizeof(count_and_cmd)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000417 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
418 usb_strerror());
419 return 1;
420 }
421
422 for (i = 0; i < count; i++) {
423 memset(usbbuf, 0xff, sizeof(usbbuf));
424 memcpy(usbbuf, buf + i * chunksize, chunksize);
425 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
426 usbbuf, 512,
427 DEFAULT_TIMEOUT);
428 if (ret != 512) {
429 msg_perr("SPI bulk write failed, expected %i, got %i "
430 "%s!\n", 512, ret, usb_strerror());
431 return 1;
432 }
433 }
434
435 return 0;
436}
437
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000438static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000439 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000440{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000441 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000442 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000443 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
444 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000445
Simon Glass25e9f402015-06-28 13:31:19 +0000446 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000447
Nico Hubera4b14f72012-06-19 12:06:53 +0000448 if (chunksize != 256) {
449 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
450 "we don't know how dediprog\nhandles them.\n");
451 /* Write everything like it was residue. */
452 residue = len;
453 }
454
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000455 if (residue) {
456 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
457 start, residue);
458 /* No idea about the real limit. Maybe 12, maybe more. */
459 ret = spi_write_chunked(flash, buf, start, residue, 12);
460 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000461 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000462 return ret;
463 }
464 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000465
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000466 /* Round down. */
467 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000468 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000469 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000470 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000471 return ret;
472 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000473
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000474 len -= residue + bulklen;
475 if (len) {
476 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
477 start, len);
478 ret = spi_write_chunked(flash, buf + residue + bulklen,
479 start + residue + bulklen, len, 12);
480 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000481 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000482 return ret;
483 }
484 }
485
Simon Glass25e9f402015-06-28 13:31:19 +0000486 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000487 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000488}
489
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000490static 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 +0000491{
Simon Glass557eb4f2015-07-05 16:53:22 +0000492 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000493}
494
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000495static 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 +0000496{
Simon Glass557eb4f2015-07-05 16:53:22 +0000497 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000498}
499
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000500static int dediprog_spi_send_command(struct flashctx *flash,
501 unsigned int writecnt,
502 unsigned int readcnt,
503 const unsigned char *writearr,
504 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000505{
506 int ret;
507
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000508 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glass557eb4f2015-07-05 16:53:22 +0000509 if (writecnt > UINT16_MAX) {
510 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000511 return 1;
512 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000513 if (readcnt > UINT16_MAX) {
514 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000515 return 1;
516 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000517
Simon Glass557eb4f2015-07-05 16:53:22 +0000518 ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_TRANSCEIVE, 0, readcnt ? 0x1 : 0x0,
519 (char *)writearr, writecnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000520 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000521 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
522 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000523 return 1;
524 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000525 if (readcnt == 0)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000526 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000527
528 ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_TRANSCEIVE, 0, 0,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000529 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000530 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000531 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
532 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000533 return 1;
534 }
535 return 0;
536}
537
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000538static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000539{
540 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000541 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000542 char buf[0x11];
543
Simon Glass557eb4f2015-07-05 16:53:22 +0000544#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000545 /* Command Prepare Receive Device String. */
Simon Glass557eb4f2015-07-05 16:53:22 +0000546 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef03,
547 buf, 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000548 /* The char casting is needed to stop gcc complaining about an always true comparison. */
549 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
550 msg_perr("Unexpected response to Command Prepare Receive Device"
551 " String!\n");
552 return 1;
553 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000554#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000555 /* Command Receive Device String. */
Simon Glass557eb4f2015-07-05 16:53:22 +0000556 ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_READ_PROG_INFO, 0, 0,
557 buf, 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000558 if (ret != 0x10) {
559 msg_perr("Incomplete/failed Command Receive Device String!\n");
560 return 1;
561 }
562 buf[0x10] = '\0';
563 msg_pdbg("Found a %s\n", buf);
Simon Glass557eb4f2015-07-05 16:53:22 +0000564 if (memcmp(buf, "SF100", 0x5) != 0) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000565 msg_perr("Device not a SF100!\n");
566 return 1;
567 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000568 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
569 msg_perr("Unexpected firmware version string!\n");
570 return 1;
571 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000572 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000573 if (fw[0] < 2 || fw[0] > 5) {
574 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
575 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000576 return 1;
577 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000578 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000579 return 0;
580}
581
Simon Glass557eb4f2015-07-05 16:53:22 +0000582static int dediprog_device_init(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000583{
584 int ret;
585 char buf[0x1];
586
587 memset(buf, 0, sizeof(buf));
Simon Glass557eb4f2015-07-05 16:53:22 +0000588 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x0B, 0x0, 0x0,
589 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000590 if (ret < 0) {
591 msg_perr("Command A failed (%s)!\n", usb_strerror());
592 return 1;
593 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000594 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000595 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000596 return 1;
597 }
598 return 0;
599}
600
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000601#if 0
602/* Something.
603 * Present in eng_detect_blink.log with firmware 3.1.8
604 * Always preceded by Command Receive Device String
605 */
606static int dediprog_command_b(void)
607{
608 int ret;
609 char buf[0x3];
610
Simon Glass557eb4f2015-07-05 16:53:22 +0000611 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
612 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000613 if (ret < 0) {
614 msg_perr("Command B failed (%s)!\n", usb_strerror());
615 return 1;
616 }
617 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
618 (buf[2] != 0xff)) {
619 msg_perr("Unexpected response to Command B!\n");
620 return 1;
621 }
622
623 return 0;
624}
625#endif
626
Simon Glass557eb4f2015-07-05 16:53:22 +0000627static int set_target_flash(enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000628{
Simon Glass557eb4f2015-07-05 16:53:22 +0000629 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_SET_TARGET, target, 0,
630 NULL, 0, DEFAULT_TIMEOUT);
631 if (ret != 0) {
632 msg_perr("set_target_flash failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000633 return 1;
634 }
635 return 0;
636}
637
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000638#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000639/* Returns true if the button is currently pressed. */
640static bool dediprog_get_button(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000641{
Simon Glass557eb4f2015-07-05 16:53:22 +0000642 char buf[1];
643 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
644 buf, 0x1, DEFAULT_TIMEOUT);
645 if (ret != 0) {
646 msg_perr("Could not get button state (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000647 return 1;
648 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000649 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000650}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000651#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000652
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000653static int parse_voltage(char *voltage)
654{
655 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000656 int i;
657 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000658
659 if (!voltage || !strlen(voltage)) {
660 msg_perr("Empty voltage= specified.\n");
661 return -1;
662 }
663 millivolt = (int)strtol(voltage, &tmp, 0);
664 voltage = tmp;
665 /* Handle "," and "." as decimal point. Everything after it is assumed
666 * to be in decimal notation.
667 */
668 if ((*voltage == '.') || (*voltage == ',')) {
669 voltage++;
670 for (i = 0; i < 3; i++) {
671 fraction *= 10;
672 /* Don't advance if the current character is invalid,
673 * but continue multiplying.
674 */
675 if ((*voltage < '0') || (*voltage > '9'))
676 continue;
677 fraction += *voltage - '0';
678 voltage++;
679 }
680 /* Throw away remaining digits. */
681 voltage += strspn(voltage, "0123456789");
682 }
683 /* The remaining string must be empty or "mV" or "V". */
684 tolower_string(voltage);
685
686 /* No unit or "V". */
687 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
688 millivolt *= 1000;
689 millivolt += fraction;
690 } else if (!strncmp(voltage, "mv", 2) ||
691 !strncmp(voltage, "milliv", 6)) {
692 /* No adjustment. fraction is discarded. */
693 } else {
694 /* Garbage at the end of the string. */
695 msg_perr("Garbage voltage= specified.\n");
696 return -1;
697 }
698 return millivolt;
699}
700
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000701static const struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000702 .type = SPI_CONTROLLER_DEDIPROG,
703 .max_data_read = MAX_DATA_UNSPECIFIED,
704 .max_data_write = MAX_DATA_UNSPECIFIED,
705 .command = dediprog_spi_send_command,
706 .multicommand = default_spi_send_multicommand,
707 .read = dediprog_spi_read,
708 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000709 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000710};
711
David Hendricks8bb20212011-06-14 01:35:36 +0000712static int dediprog_shutdown(void *data)
713{
714 msg_pspew("%s\n", __func__);
715
Simon Glass557eb4f2015-07-05 16:53:22 +0000716 dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000717
David Hendricks8bb20212011-06-14 01:35:36 +0000718 /* URB 28. Command Set SPI Voltage to 0. */
719 if (dediprog_set_spi_voltage(0x0))
720 return 1;
721
722 if (usb_release_interface(dediprog_handle, 0)) {
723 msg_perr("Could not release USB interface!\n");
724 return 1;
725 }
726 if (usb_close(dediprog_handle)) {
727 msg_perr("Could not close USB device!\n");
728 return 1;
729 }
730 return 0;
731}
732
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000733/* URB numbers refer to the first log ever captured. */
734int dediprog_init(void)
735{
736 struct usb_device *dev;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000737 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +0000738 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000739 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000740 long usedevice = 0;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000741 long target = 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000742 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000743
744 msg_pspew("%s\n", __func__);
745
Nico Huber77fa67d2013-02-20 18:03:36 +0000746 spispeed = extract_programmer_param("spispeed");
747 if (spispeed) {
748 for (i = 0; spispeeds[i].name; ++i) {
749 if (!strcasecmp(spispeeds[i].name, spispeed)) {
750 spispeed_idx = i;
751 break;
752 }
753 }
754 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000755 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +0000756 free(spispeed);
757 return 1;
758 }
759 free(spispeed);
760 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000761
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000762 voltage = extract_programmer_param("voltage");
763 if (voltage) {
764 millivolt = parse_voltage(voltage);
765 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000766 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000767 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000768 msg_pinfo("Setting voltage to %i mV\n", millivolt);
769 }
770
Nathan Laredo21541a62012-12-24 22:07:36 +0000771 device = extract_programmer_param("device");
772 if (device) {
773 char *dev_suffix;
774 errno = 0;
775 usedevice = strtol(device, &dev_suffix, 10);
776 if (errno != 0 || device == dev_suffix) {
777 msg_perr("Error: Could not convert 'device'.\n");
778 free(device);
779 return 1;
780 }
781 if (usedevice < 0 || usedevice > UINT_MAX) {
782 msg_perr("Error: Value for 'device' is out of range.\n");
783 free(device);
784 return 1;
785 }
786 if (strlen(dev_suffix) > 0) {
787 msg_perr("Error: Garbage following 'device' value.\n");
788 free(device);
789 return 1;
790 }
791 msg_pinfo("Using device %li.\n", usedevice);
792 }
793 free(device);
794
Stefan Taunere659d2d2013-05-03 21:58:28 +0000795 target_str = extract_programmer_param("target");
796 if (target_str) {
797 char *target_suffix;
798 errno = 0;
799 target = strtol(target_str, &target_suffix, 10);
800 if (errno != 0 || target_str == target_suffix) {
801 msg_perr("Error: Could not convert 'target'.\n");
802 free(target_str);
803 return 1;
804 }
805 if (target < 1 || target > 2) {
806 msg_perr("Error: Value for 'target' is out of range.\n");
807 free(target_str);
808 return 1;
809 }
810 if (strlen(target_suffix) > 0) {
811 msg_perr("Error: Garbage following 'target' value.\n");
812 free(target_str);
813 return 1;
814 }
815 msg_pinfo("Using target %li.\n", target);
816 }
817 free(target_str);
818
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000819 /* Here comes the USB stuff. */
820 usb_init();
821 usb_find_busses();
822 usb_find_devices();
Nathan Laredo21541a62012-12-24 22:07:36 +0000823 dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000824 if (!dev) {
825 msg_perr("Could not find a Dediprog SF100 on USB!\n");
826 return 1;
827 }
828 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000829 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000830 dediprog_handle = usb_open(dev);
David Woodhoused2a7e872013-07-30 09:34:44 +0000831 if (!dediprog_handle) {
832 msg_perr("Could not open USB device: %s\n", usb_strerror());
833 return 1;
834 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000835 ret = usb_set_configuration(dediprog_handle, 1);
836 if (ret < 0) {
837 msg_perr("Could not set USB device configuration: %i %s\n",
838 ret, usb_strerror());
839 if (usb_close(dediprog_handle))
840 msg_perr("Could not close USB device!\n");
841 return 1;
842 }
843 ret = usb_claim_interface(dediprog_handle, 0);
844 if (ret < 0) {
845 msg_perr("Could not claim USB device interface %i: %i %s\n",
846 0, ret, usb_strerror());
847 if (usb_close(dediprog_handle))
848 msg_perr("Could not close USB device!\n");
849 return 1;
850 }
851 dediprog_endpoint = 2;
Nico Huber77fa67d2013-02-20 18:03:36 +0000852
David Hendricks8bb20212011-06-14 01:35:36 +0000853 if (register_shutdown(dediprog_shutdown, NULL))
854 return 1;
855
Nico Huber77fa67d2013-02-20 18:03:36 +0000856 /* Perform basic setup. */
Simon Glass557eb4f2015-07-05 16:53:22 +0000857 if (dediprog_device_init())
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000858 return 1;
Simon Glass557eb4f2015-07-05 16:53:22 +0000859 if (dediprog_check_devicestring())
860 return 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000861
Simon Glass25e9f402015-06-28 13:31:19 +0000862 /* Set all possible LEDs as soon as possible to indicate activity.
863 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
864 * dediprog_setup() has queried the device and set dediprog_firmwareversion. */
865 dediprog_set_leds(LED_ALL);
866
Simon Glass557eb4f2015-07-05 16:53:22 +0000867 /* Select target/socket, frequency and VCC. */
868 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
869 dediprog_set_spi_speed(spispeed_idx) ||
870 dediprog_set_spi_voltage(millivolt)) {
Simon Glass25e9f402015-06-28 13:31:19 +0000871 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000872 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000873 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000874
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000875 register_spi_master(&spi_master_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000876
Simon Glass25e9f402015-06-28 13:31:19 +0000877 dediprog_set_leds(LED_NONE);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000878
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000879 return 0;
880}
881