blob: 8c7e21ad28db90a075efbb87340468233bab857c [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;
David Woodhouse7f3b89f2016-02-18 21:42:15 +000048static int dediprog_in_endpoint;
49static int dediprog_out_endpoint;
50
51enum dediprog_devtype {
52 DEV_UNKNOWN = 0,
53 DEV_SF100 = 100,
54 DEV_SF600 = 600,
55};
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000056
Simon Glass25e9f402015-06-28 13:31:19 +000057enum dediprog_leds {
58 LED_INVALID = -1,
59 LED_NONE = 0,
60 LED_PASS = 1 << 0,
61 LED_BUSY = 1 << 1,
62 LED_ERROR = 1 << 2,
63 LED_ALL = 7,
64};
65
Simon Glass557eb4f2015-07-05 16:53:22 +000066/* IO bits for CMD_SET_IO_LED message */
67enum dediprog_ios {
68 IO1 = 1 << 0,
69 IO2 = 1 << 1,
70 IO3 = 1 << 2,
71 IO4 = 1 << 3,
72};
73
74enum dediprog_cmds {
75 CMD_TRANSCEIVE = 0x01,
76 CMD_POLL_STATUS_REG = 0x02,
77 CMD_SET_VPP = 0x03,
78 CMD_SET_TARGET = 0x04,
79 CMD_READ_EEPROM = 0x05,
80 CMD_WRITE_EEPROM = 0x06,
81 CMD_SET_IO_LED = 0x07,
82 CMD_READ_PROG_INFO = 0x08,
83 CMD_SET_VCC = 0x09,
84 CMD_SET_STANDALONE = 0x0A,
85 CMD_GET_BUTTON = 0x11,
86 CMD_GET_UID = 0x12,
87 CMD_SET_CS = 0x14,
88 CMD_IO_MODE = 0x15,
89 CMD_FW_UPDATE = 0x1A,
90 CMD_FPGA_UPDATE = 0x1B,
91 CMD_READ_FPGA_VERSION = 0x1C,
92 CMD_SET_HOLD = 0x1D,
93 CMD_READ = 0x20,
94 CMD_WRITE = 0x30,
95 CMD_WRITE_AT45DB = 0x31,
96 CMD_NAND_WRITE = 0x32,
97 CMD_NAND_READ = 0x33,
98 CMD_SET_SPI_CLK = 0x61,
99 CMD_CHECK_SOCKET = 0x62,
100 CMD_DOWNLOAD_PRJ = 0x63,
101 CMD_READ_PRJ_NAME = 0x64,
102 // New protocol/firmware only
103 CMD_CHECK_SDCARD = 0x65,
104 CMD_READ_PRJ = 0x66,
105};
106
107enum dediprog_target {
108 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
109 FLASH_TYPE_FLASH_CARD,
110 FLASH_TYPE_APPLICATION_FLASH_2,
111 FLASH_TYPE_SOCKET,
112};
113
114enum dediprog_readmode {
115 READ_MODE_STD = 1,
116 READ_MODE_FAST = 2,
117 READ_MODE_ATMEL45 = 3,
118 READ_MODE_4B_ADDR_FAST = 4,
119 READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */
120};
121
122enum dediprog_writemode {
123 WRITE_MODE_PAGE_PGM = 1,
124 WRITE_MODE_PAGE_WRITE = 2,
125 WRITE_MODE_1B_AAI = 3,
126 WRITE_MODE_2B_AAI = 4,
127 WRITE_MODE_128B_PAGE = 5,
128 WRITE_MODE_PAGE_AT26DF041 = 6,
129 WRITE_MODE_SILICON_BLUE_FPGA = 7,
Simon Glassae616512016-01-23 23:27:58 +0000130 WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000131 WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9,
Simon Glassae616512016-01-23 23:27:58 +0000132 WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */
Simon Glass557eb4f2015-07-05 16:53:22 +0000133 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11,
134 WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12,
135};
136
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000137enum dediprog_standalone_mode {
138 ENTER_STANDALONE_MODE = 0,
139 LEAVE_STANDALONE_MODE = 1,
140};
141
Simon Glass557eb4f2015-07-05 16:53:22 +0000142
143static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000144enum dediprog_devtype dediprog_devicetype = DEV_UNKNOWN;
Simon Glass557eb4f2015-07-05 16:53:22 +0000145
Simon Glassae616512016-01-23 23:27:58 +0000146/* Returns true if firmware (and thus hardware) supports the "new" protocol */
147static bool is_new_prot(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000148{
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000149 switch (dediprog_devicetype) {
150 case DEV_SF100:
151 return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0);
152 case DEV_SF600:
153 return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0);
154 default:
155 return 0;
156 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000157}
Simon Glassae616512016-01-23 23:27:58 +0000158
159static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size)
160{
161 return usb_control_msg(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx,
162 (char *)bytes, size, DEFAULT_TIMEOUT);
163}
164
165static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size)
166{
167 return usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx,
168 (char *)bytes, size, DEFAULT_TIMEOUT);
169}
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000170
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000171/* Might be useful for other USB devices as well. static for now. */
Nathan Laredo21541a62012-12-24 22:07:36 +0000172/* device parameter allows user to specify one device of multiple installed */
173static 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 +0000174{
175 struct usb_bus *bus;
176 struct usb_device *dev;
177
178 for (bus = usb_get_busses(); bus; bus = bus->next)
179 for (dev = bus->devices; dev; dev = dev->next)
180 if ((dev->descriptor.idVendor == vid) &&
Nathan Laredo21541a62012-12-24 22:07:36 +0000181 (dev->descriptor.idProduct == pid)) {
182 if (device == 0)
183 return dev;
184 device--;
185 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000186
187 return NULL;
188}
189
Simon Glass25e9f402015-06-28 13:31:19 +0000190/* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000191static int dediprog_set_leds(int leds)
192{
Simon Glass25e9f402015-06-28 13:31:19 +0000193 if (leds < LED_NONE || leds > LED_ALL)
194 leds = LED_ALL;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000195
Simon Glassae616512016-01-23 23:27:58 +0000196 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map
197 * them around if we have an old device. On those devices the LEDs map as follows:
Stefan Reinauer915b8402011-01-28 09:00:15 +0000198 * bit 2 == 0: green light is on.
Simon Glassae616512016-01-23 23:27:58 +0000199 * bit 0 == 0: red light is on.
200 *
201 * Additionally, the command structure has changed with the "new" protocol.
202 *
203 * FIXME: take IO pins into account
Stefan Reinauer915b8402011-01-28 09:00:15 +0000204 */
Simon Glassae616512016-01-23 23:27:58 +0000205 int target_leds, ret;
206 if (is_new_prot()) {
207 target_leds = (leds ^ 7) << 8;
208 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000209 } else {
Simon Glassae616512016-01-23 23:27:58 +0000210 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
211 target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2);
212 } else {
213 target_leds = leds;
214 }
215 target_leds ^= 7;
216
217 ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000218 }
219
Stefan Reinauer915b8402011-01-28 09:00:15 +0000220 if (ret != 0x0) {
Simon Glass25e9f402015-06-28 13:31:19 +0000221 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000222 return 1;
223 }
224
Stefan Reinauer915b8402011-01-28 09:00:15 +0000225 return 0;
226}
227
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000228static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000229{
230 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000231 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000232
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000233 switch (millivolt) {
234 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000235 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000236 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000237 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000238 case 1800:
239 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000240 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000241 case 2500:
242 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000243 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000244 case 3500:
245 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000246 break;
247 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000248 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000249 return 1;
250 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000251 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
252 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000253
Nico Huber4099a8a2012-06-16 00:02:27 +0000254 if (voltage_selector == 0) {
255 /* Wait some time as the original driver does. */
256 programmer_delay(200 * 1000);
257 }
Simon Glassae616512016-01-23 23:27:58 +0000258 ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000259 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000260 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
261 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000262 return 1;
263 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000264 if (voltage_selector != 0) {
265 /* Wait some time as the original driver does. */
266 programmer_delay(200 * 1000);
267 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000268 return 0;
269}
270
Nico Huber77fa67d2013-02-20 18:03:36 +0000271struct dediprog_spispeeds {
272 const char *const name;
273 const int speed;
274};
275
276static const struct dediprog_spispeeds spispeeds[] = {
277 { "24M", 0x0 },
278 { "12M", 0x2 },
279 { "8M", 0x1 },
280 { "3M", 0x3 },
281 { "2.18M", 0x4 },
282 { "1.5M", 0x5 },
283 { "750k", 0x6 },
284 { "375k", 0x7 },
285 { NULL, 0x0 },
286};
287
Nico Huber77fa67d2013-02-20 18:03:36 +0000288static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000289{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000290 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
291 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
292 return 0;
293 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000294
Simon Glass557eb4f2015-07-05 16:53:22 +0000295 const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx];
296 msg_pdbg("SPI speed is %sHz\n", spispeed->name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000297
Simon Glassae616512016-01-23 23:27:58 +0000298 int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000299 if (ret != 0x0) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000300 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000301 return 1;
302 }
303 return 0;
304}
305
Simon Glassae616512016-01-23 23:27:58 +0000306static 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) {
307 /* First 5 bytes are common in both generations. */
308 data_packet[0] = count & 0xff;
309 data_packet[1] = (count >> 8) & 0xff;
310 data_packet[2] = 0; /* RFU */
311 data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */
312 data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */
313
314 if (is_new_prot()) {
315 *value = *idx = 0;
316 data_packet[5] = 0; /* RFU */
317 data_packet[6] = (start >> 0) & 0xff;
318 data_packet[7] = (start >> 8) & 0xff;
319 data_packet[8] = (start >> 16) & 0xff;
320 data_packet[9] = (start >> 24) & 0xff;
321 } else {
322 *value = start % 0x10000;
323 *idx = start / 0x10000;
324 }
325}
326
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000327/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
328 * @start start address
329 * @len length
330 * @return 0 on success, 1 on failure
331 */
Simon Glassae616512016-01-23 23:27:58 +0000332static 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 +0000333{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000334 /* chunksize must be 512, other sizes will NOT work at all. */
Simon Glassae616512016-01-23 23:27:58 +0000335 const unsigned int chunksize = 512;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000336 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000337
338 if ((start % chunksize) || (len % chunksize)) {
Simon Glassae616512016-01-23 23:27:58 +0000339 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n",
340 __func__, start, len);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000341 return 1;
342 }
343
Simon Glassae616512016-01-23 23:27:58 +0000344 if (len == 0)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000345 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000346
347 /* Command packet size of protocols: new 10 B, old 5 B. */
348 uint8_t data_packet[is_new_prot() ? 10 : 5];
349 unsigned int value, idx;
350 fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start);
351
352 int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
353 if (ret != sizeof(data_packet)) {
354 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, usb_strerror());
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000355 return 1;
356 }
357
Simon Glassae616512016-01-23 23:27:58 +0000358 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000359 for (i = 0; i < count; i++) {
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000360 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_in_endpoint,
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000361 (char *)buf + i * chunksize, chunksize,
362 DEFAULT_TIMEOUT);
363 if (ret != chunksize) {
Simon Glassae616512016-01-23 23:27:58 +0000364 msg_perr("SPI bulk read %i failed, expected %i, got %i %s!\n",
365 i, chunksize, ret, usb_strerror());
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000366 return 1;
367 }
368 }
369
370 return 0;
371}
372
Simon Glassae616512016-01-23 23:27:58 +0000373static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000374{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000375 int ret;
376 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000377 const unsigned int chunksize = 0x200;
378 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
379 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000380
Simon Glass25e9f402015-06-28 13:31:19 +0000381 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000382
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000383 if (residue) {
384 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
385 start, residue);
386 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000387 if (ret)
388 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000389 }
390
391 /* Round down. */
392 bulklen = (len - residue) / chunksize * chunksize;
Simon Glassae616512016-01-23 23:27:58 +0000393 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen);
Simon Glass25e9f402015-06-28 13:31:19 +0000394 if (ret)
395 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000396
397 len -= residue + bulklen;
Simon Glassae616512016-01-23 23:27:58 +0000398 if (len != 0) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000399 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
400 start, len);
401 ret = spi_read_chunked(flash, buf + residue + bulklen,
402 start + residue + bulklen, len, 16);
Simon Glass25e9f402015-06-28 13:31:19 +0000403 if (ret)
404 goto err;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000405 }
406
Simon Glass25e9f402015-06-28 13:31:19 +0000407 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000408 return 0;
Simon Glass25e9f402015-06-28 13:31:19 +0000409err:
410 dediprog_set_leds(LED_ERROR);
411 return ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000412}
413
Nico Hubera4b14f72012-06-19 12:06:53 +0000414/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
415 * @chunksize length of data chunks, only 256 supported by now
416 * @start start address
417 * @len length
418 * @dedi_spi_cmd dediprog specific write command for spi bus
419 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000420 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000421static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000422 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000423{
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000424 /* USB transfer size must be 512, other sizes will NOT work at all.
425 * chunksize is the real data size per USB bulk transfer. The remaining
426 * space in a USB bulk transfer must be filled with 0xff padding.
427 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000428 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000429
Nico Hubera4b14f72012-06-19 12:06:53 +0000430 /*
431 * We should change this check to
432 * chunksize > 512
433 * once we know how to handle different chunk sizes.
434 */
435 if (chunksize != 256) {
436 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
437 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
438 return 1;
439 }
440
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000441 if ((start % chunksize) || (len % chunksize)) {
442 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
443 "at flashrom@flashrom.org\n", __func__, start, len);
444 return 1;
445 }
446
447 /* No idea if the hardware can handle empty writes, so chicken out. */
Simon Glassae616512016-01-23 23:27:58 +0000448 if (len == 0)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000449 return 0;
Simon Glassae616512016-01-23 23:27:58 +0000450
451 /* Command packet size of protocols: new 10 B, old 5 B. */
452 uint8_t data_packet[is_new_prot() ? 10 : 5];
453 unsigned int value, idx;
454 fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start);
455 int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
456 if (ret != sizeof(data_packet)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000457 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
458 usb_strerror());
459 return 1;
460 }
461
Simon Glassae616512016-01-23 23:27:58 +0000462 unsigned int i;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000463 for (i = 0; i < count; i++) {
Simon Glassae616512016-01-23 23:27:58 +0000464 char usbbuf[512];
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000465 memcpy(usbbuf, buf + i * chunksize, chunksize);
Simon Glassae616512016-01-23 23:27:58 +0000466 memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000467 ret = usb_bulk_write(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, DEFAULT_TIMEOUT);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000468 if (ret != 512) {
Simon Glassae616512016-01-23 23:27:58 +0000469 msg_perr("SPI bulk write failed, expected %i, got %i %s!\n", 512, ret, usb_strerror());
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000470 return 1;
471 }
472 }
473
474 return 0;
475}
476
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000477static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000478 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000479{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000480 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000481 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000482 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
483 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000484
Simon Glass25e9f402015-06-28 13:31:19 +0000485 dediprog_set_leds(LED_BUSY);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000486
Nico Hubera4b14f72012-06-19 12:06:53 +0000487 if (chunksize != 256) {
488 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
489 "we don't know how dediprog\nhandles them.\n");
490 /* Write everything like it was residue. */
491 residue = len;
492 }
493
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000494 if (residue) {
495 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
496 start, residue);
497 /* No idea about the real limit. Maybe 12, maybe more. */
498 ret = spi_write_chunked(flash, buf, start, residue, 12);
499 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000500 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000501 return ret;
502 }
503 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000504
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000505 /* Round down. */
506 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000507 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000508 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000509 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000510 return ret;
511 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000512
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000513 len -= residue + bulklen;
514 if (len) {
515 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
516 start, len);
517 ret = spi_write_chunked(flash, buf + residue + bulklen,
518 start + residue + bulklen, len, 12);
519 if (ret) {
Simon Glass25e9f402015-06-28 13:31:19 +0000520 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000521 return ret;
522 }
523 }
524
Simon Glass25e9f402015-06-28 13:31:19 +0000525 dediprog_set_leds(LED_PASS);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000526 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000527}
528
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000529static 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 +0000530{
Simon Glass557eb4f2015-07-05 16:53:22 +0000531 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM);
Nico Hubera4b14f72012-06-19 12:06:53 +0000532}
533
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000534static 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 +0000535{
Simon Glass557eb4f2015-07-05 16:53:22 +0000536 return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI);
Nico Hubera4b14f72012-06-19 12:06:53 +0000537}
538
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000539static int dediprog_spi_send_command(struct flashctx *flash,
540 unsigned int writecnt,
541 unsigned int readcnt,
542 const unsigned char *writearr,
543 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000544{
545 int ret;
546
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000547 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glassae616512016-01-23 23:27:58 +0000548 if (writecnt > flash->mst->spi.max_data_write) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000549 msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000550 return 1;
551 }
Simon Glassae616512016-01-23 23:27:58 +0000552 if (readcnt > flash->mst->spi.max_data_read) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000553 msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000554 return 1;
555 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000556
Simon Glassae616512016-01-23 23:27:58 +0000557 unsigned int idx, value;
558 /* New protocol has options and timeout combined as value while the old one used the value field for
559 * timeout and the index field for options. */
560 if (is_new_prot()) {
561 idx = 0;
562 value = readcnt ? 0x1 : 0x0; // Indicate if we require a read
563 } else {
564 idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read
565 value = 0;
566 }
567 ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000568 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000569 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
570 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000571 return 1;
572 }
Simon Glassae616512016-01-23 23:27:58 +0000573 if (readcnt == 0) // If we don't require a response, we are done here
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000574 return 0;
Simon Glass557eb4f2015-07-05 16:53:22 +0000575
Simon Glassae616512016-01-23 23:27:58 +0000576 const uint8_t read_timeout = 10 + readcnt/512;
577 if (is_new_prot()) {
578 idx = 0;
579 value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte
580 } else {
581 idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low)
582 value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here
583 }
584 ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000585 if (ret != readcnt) {
Simon Glassae616512016-01-23 23:27:58 +0000586 msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000587 return 1;
588 }
589 return 0;
590}
591
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000592static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000593{
594 int ret;
595 char buf[0x11];
596
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000597 /* Command Receive Device String. */
Simon Glassae616512016-01-23 23:27:58 +0000598 ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000599 if (ret != 0x10) {
600 msg_perr("Incomplete/failed Command Receive Device String!\n");
601 return 1;
602 }
603 buf[0x10] = '\0';
604 msg_pdbg("Found a %s\n", buf);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000605 if (memcmp(buf, "SF100", 0x5) == 0)
606 dediprog_devicetype = DEV_SF100;
607 else if (memcmp(buf, "SF600", 0x5) == 0)
608 dediprog_devicetype = DEV_SF600;
609 else {
610 msg_perr("Device not a SF100 or SF600!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000611 return 1;
612 }
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000613
614 int sfnum;
615 int fw[3];
616 if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
617 sfnum != dediprog_devicetype) {
Simon Glassae616512016-01-23 23:27:58 +0000618 msg_perr("Unexpected firmware version string '%s'\n", buf);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000619 return 1;
620 }
Simon Glassae616512016-01-23 23:27:58 +0000621 /* Only these major versions were tested. */
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000622 if (fw[0] < 2 || fw[0] > 7) {
Simon Glassae616512016-01-23 23:27:58 +0000623 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000624 return 1;
625 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000626 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Simon Glassae616512016-01-23 23:27:58 +0000627
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000628 return 0;
629}
630
Simon Glass557eb4f2015-07-05 16:53:22 +0000631static int dediprog_device_init(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000632{
633 int ret;
634 char buf[0x1];
635
636 memset(buf, 0, sizeof(buf));
Simon Glass557eb4f2015-07-05 16:53:22 +0000637 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x0B, 0x0, 0x0,
638 buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000639 if (ret < 0) {
640 msg_perr("Command A failed (%s)!\n", usb_strerror());
641 return 1;
642 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000643 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glass557eb4f2015-07-05 16:53:22 +0000644 msg_perr("Unexpected response to init!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000645 return 1;
646 }
647 return 0;
648}
649
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000650static int dediprog_standalone_mode(void)
651{
652 int ret;
653
654 if (dediprog_devicetype != DEV_SF600)
655 return 0;
656
657 msg_pdbg2("Disabling standalone mode.\n");
658 ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0);
659 if (ret) {
660 msg_perr("Failed to disable standalone mode: %s\n", usb_strerror());
661 return 1;
662 }
663
664 return 0;
665}
666
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000667#if 0
668/* Something.
669 * Present in eng_detect_blink.log with firmware 3.1.8
670 * Always preceded by Command Receive Device String
671 */
672static int dediprog_command_b(void)
673{
674 int ret;
675 char buf[0x3];
676
Simon Glass557eb4f2015-07-05 16:53:22 +0000677 ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00,
678 buf, 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000679 if (ret < 0) {
680 msg_perr("Command B failed (%s)!\n", usb_strerror());
681 return 1;
682 }
683 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
684 (buf[2] != 0xff)) {
685 msg_perr("Unexpected response to Command B!\n");
686 return 1;
687 }
688
689 return 0;
690}
691#endif
692
Simon Glass557eb4f2015-07-05 16:53:22 +0000693static int set_target_flash(enum dediprog_target target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000694{
Simon Glass557eb4f2015-07-05 16:53:22 +0000695 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_SET_TARGET, target, 0,
696 NULL, 0, DEFAULT_TIMEOUT);
697 if (ret != 0) {
698 msg_perr("set_target_flash failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000699 return 1;
700 }
701 return 0;
702}
703
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000704#if 0
Simon Glass557eb4f2015-07-05 16:53:22 +0000705/* Returns true if the button is currently pressed. */
706static bool dediprog_get_button(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000707{
Simon Glass557eb4f2015-07-05 16:53:22 +0000708 char buf[1];
709 int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0,
710 buf, 0x1, DEFAULT_TIMEOUT);
711 if (ret != 0) {
712 msg_perr("Could not get button state (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000713 return 1;
714 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000715 return buf[0] != 1;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000716}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000717#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000718
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000719static int parse_voltage(char *voltage)
720{
721 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000722 int i;
723 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000724
725 if (!voltage || !strlen(voltage)) {
726 msg_perr("Empty voltage= specified.\n");
727 return -1;
728 }
729 millivolt = (int)strtol(voltage, &tmp, 0);
730 voltage = tmp;
731 /* Handle "," and "." as decimal point. Everything after it is assumed
732 * to be in decimal notation.
733 */
734 if ((*voltage == '.') || (*voltage == ',')) {
735 voltage++;
736 for (i = 0; i < 3; i++) {
737 fraction *= 10;
738 /* Don't advance if the current character is invalid,
739 * but continue multiplying.
740 */
741 if ((*voltage < '0') || (*voltage > '9'))
742 continue;
743 fraction += *voltage - '0';
744 voltage++;
745 }
746 /* Throw away remaining digits. */
747 voltage += strspn(voltage, "0123456789");
748 }
749 /* The remaining string must be empty or "mV" or "V". */
750 tolower_string(voltage);
751
752 /* No unit or "V". */
753 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
754 millivolt *= 1000;
755 millivolt += fraction;
756 } else if (!strncmp(voltage, "mv", 2) ||
757 !strncmp(voltage, "milliv", 6)) {
758 /* No adjustment. fraction is discarded. */
759 } else {
760 /* Garbage at the end of the string. */
761 msg_perr("Garbage voltage= specified.\n");
762 return -1;
763 }
764 return millivolt;
765}
766
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000767static const struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000768 .type = SPI_CONTROLLER_DEDIPROG,
Simon Glassae616512016-01-23 23:27:58 +0000769 .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */
770 .max_data_write = 16,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000771 .command = dediprog_spi_send_command,
772 .multicommand = default_spi_send_multicommand,
773 .read = dediprog_spi_read,
774 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000775 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000776};
777
David Hendricks8bb20212011-06-14 01:35:36 +0000778static int dediprog_shutdown(void *data)
779{
Simon Glass557eb4f2015-07-05 16:53:22 +0000780 dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0);
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000781 dediprog_devicetype = DEV_UNKNOWN;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000782
David Hendricks8bb20212011-06-14 01:35:36 +0000783 /* URB 28. Command Set SPI Voltage to 0. */
784 if (dediprog_set_spi_voltage(0x0))
785 return 1;
786
787 if (usb_release_interface(dediprog_handle, 0)) {
788 msg_perr("Could not release USB interface!\n");
789 return 1;
790 }
791 if (usb_close(dediprog_handle)) {
792 msg_perr("Could not close USB device!\n");
793 return 1;
794 }
795 return 0;
796}
797
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000798int dediprog_init(void)
799{
800 struct usb_device *dev;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000801 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +0000802 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000803 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000804 long usedevice = 0;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000805 long target = 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000806 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000807
Nico Huber77fa67d2013-02-20 18:03:36 +0000808 spispeed = extract_programmer_param("spispeed");
809 if (spispeed) {
810 for (i = 0; spispeeds[i].name; ++i) {
811 if (!strcasecmp(spispeeds[i].name, spispeed)) {
812 spispeed_idx = i;
813 break;
814 }
815 }
816 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000817 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +0000818 free(spispeed);
819 return 1;
820 }
821 free(spispeed);
822 }
Simon Glass557eb4f2015-07-05 16:53:22 +0000823
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000824 voltage = extract_programmer_param("voltage");
825 if (voltage) {
826 millivolt = parse_voltage(voltage);
827 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000828 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000829 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000830 msg_pinfo("Setting voltage to %i mV\n", millivolt);
831 }
832
Nathan Laredo21541a62012-12-24 22:07:36 +0000833 device = extract_programmer_param("device");
834 if (device) {
835 char *dev_suffix;
836 errno = 0;
837 usedevice = strtol(device, &dev_suffix, 10);
838 if (errno != 0 || device == dev_suffix) {
839 msg_perr("Error: Could not convert 'device'.\n");
840 free(device);
841 return 1;
842 }
843 if (usedevice < 0 || usedevice > UINT_MAX) {
844 msg_perr("Error: Value for 'device' is out of range.\n");
845 free(device);
846 return 1;
847 }
848 if (strlen(dev_suffix) > 0) {
849 msg_perr("Error: Garbage following 'device' value.\n");
850 free(device);
851 return 1;
852 }
853 msg_pinfo("Using device %li.\n", usedevice);
854 }
855 free(device);
856
Stefan Taunere659d2d2013-05-03 21:58:28 +0000857 target_str = extract_programmer_param("target");
858 if (target_str) {
859 char *target_suffix;
860 errno = 0;
861 target = strtol(target_str, &target_suffix, 10);
862 if (errno != 0 || target_str == target_suffix) {
863 msg_perr("Error: Could not convert 'target'.\n");
864 free(target_str);
865 return 1;
866 }
867 if (target < 1 || target > 2) {
868 msg_perr("Error: Value for 'target' is out of range.\n");
869 free(target_str);
870 return 1;
871 }
872 if (strlen(target_suffix) > 0) {
873 msg_perr("Error: Garbage following 'target' value.\n");
874 free(target_str);
875 return 1;
876 }
877 msg_pinfo("Using target %li.\n", target);
878 }
879 free(target_str);
880
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000881 /* Here comes the USB stuff. */
882 usb_init();
883 usb_find_busses();
884 usb_find_devices();
Nathan Laredo21541a62012-12-24 22:07:36 +0000885 dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000886 if (!dev) {
887 msg_perr("Could not find a Dediprog SF100 on USB!\n");
888 return 1;
889 }
890 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000891 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000892 dediprog_handle = usb_open(dev);
David Woodhoused2a7e872013-07-30 09:34:44 +0000893 if (!dediprog_handle) {
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000894 msg_perr("Could not find a Dediprog programmer on USB: %s\n", usb_strerror());
David Woodhoused2a7e872013-07-30 09:34:44 +0000895 return 1;
896 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000897 ret = usb_set_configuration(dediprog_handle, 1);
898 if (ret < 0) {
899 msg_perr("Could not set USB device configuration: %i %s\n",
900 ret, usb_strerror());
901 if (usb_close(dediprog_handle))
902 msg_perr("Could not close USB device!\n");
903 return 1;
904 }
905 ret = usb_claim_interface(dediprog_handle, 0);
906 if (ret < 0) {
907 msg_perr("Could not claim USB device interface %i: %i %s\n",
908 0, ret, usb_strerror());
909 if (usb_close(dediprog_handle))
910 msg_perr("Could not close USB device!\n");
911 return 1;
912 }
Nico Huber77fa67d2013-02-20 18:03:36 +0000913
David Hendricks8bb20212011-06-14 01:35:36 +0000914 if (register_shutdown(dediprog_shutdown, NULL))
915 return 1;
916
Nico Huber77fa67d2013-02-20 18:03:36 +0000917 /* Perform basic setup. */
Simon Glass557eb4f2015-07-05 16:53:22 +0000918 if (dediprog_device_init())
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000919 return 1;
Simon Glass557eb4f2015-07-05 16:53:22 +0000920 if (dediprog_check_devicestring())
921 return 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000922
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000923 /* SF100 only has 1 endpoint for in/out, SF600 uses two separate endpoints instead. */
924 dediprog_in_endpoint = 2;
925 if (dediprog_devicetype == DEV_SF100)
926 dediprog_out_endpoint = 2;
927 else
928 dediprog_out_endpoint = 1;
929
Simon Glass25e9f402015-06-28 13:31:19 +0000930 /* Set all possible LEDs as soon as possible to indicate activity.
931 * Because knowing the firmware version is required to set the LEDs correctly we need to this after
Simon Glassae616512016-01-23 23:27:58 +0000932 * dediprog_check_devicestring() has queried the device and set dediprog_firmwareversion. */
Simon Glass25e9f402015-06-28 13:31:19 +0000933 dediprog_set_leds(LED_ALL);
934
Simon Glass557eb4f2015-07-05 16:53:22 +0000935 /* Select target/socket, frequency and VCC. */
936 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) ||
937 dediprog_set_spi_speed(spispeed_idx) ||
938 dediprog_set_spi_voltage(millivolt)) {
Simon Glass25e9f402015-06-28 13:31:19 +0000939 dediprog_set_leds(LED_ERROR);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000940 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000941 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000942
David Woodhouse7f3b89f2016-02-18 21:42:15 +0000943 if (dediprog_standalone_mode())
944 return 1;
945
Simon Glassae616512016-01-23 23:27:58 +0000946 if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE))
947 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000948
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000949 return 0;
950}
951