blob: f8dbd7570a1e5347e1e05ed46f26ceafa59a7bd6 [file] [log] [blame]
hailfingerdfb32a02010-01-19 11:15:48 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
hailfinger7a009082010-11-09 23:30:43 +000020#include <stdio.h>
hailfingerdfb32a02010-01-19 11:15:48 +000021#include <string.h>
hailfingerdfb32a02010-01-19 11:15:48 +000022#include <usb.h>
23#include "flash.h"
snelson8913d082010-02-26 05:48:29 +000024#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000025#include "programmer.h"
hailfingerdfb32a02010-01-19 11:15:48 +000026#include "spi.h"
27
stepan4c06de72011-01-28 09:00:15 +000028#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
hailfingerdfb32a02010-01-19 11:15:48 +000029#define DEFAULT_TIMEOUT 3000
hailfinger1ff33dc2010-07-03 11:02:10 +000030static usb_dev_handle *dediprog_handle;
stepanff582cc2011-01-20 21:05:15 +000031static int dediprog_firmwareversion;
hailfingerfb6287d2010-11-16 21:25:29 +000032static int dediprog_endpoint;
hailfingerdfb32a02010-01-19 11:15:48 +000033
Simon Glassa1f80682015-01-08 05:55:29 -070034enum cmd_t {
35 CMD_TRANSCEIVE = 0x1,
36 CMD_SET_FLASH_TYPE = 0x4,
37 CMD_SET_IO_LED = 0x7,
38 CMD_READ_PROGRAMMER_INFO = 0x8,
39 CMD_SET_TARGET_FLASH_VCC = 0x9,
40 CMD_INIT = 0xb,
41 CMD_GET_UID = 0x12,
42 CMD_READ = 0x20,
43 CMD_WRITE = 0x30,
44 CMD_SET_SPI_CLK = 0x61,
45};
46
47enum flash_type {
48 FLASH_TYPE_APPLICATION_FLASH_1 = 0,
49 FLASH_TYPE_FLASH_CARD,
50 FLASH_TYPE_APPLICATION_FLASH_2,
51};
52
Simon Glasse53cc1d2015-01-08 05:48:51 -070053/* Set/clear LEDs on dediprog */
54enum {
55 LED_PASS = 1 << 0,
56 LED_BUSY = 1 << 1,
57 LED_ERROR = 1 << 2,
58 LED_ALL = 7,
59};
60
Simon Glassa1f80682015-01-08 05:55:29 -070061/* IO bits for CMD_SET_IO_LED message */
62enum {
63 IO1 = 1 << 0,
64 IO2 = 1 << 1,
65 IO3 = 1 << 2,
66 IO4 = 1 << 3,
67};
68
69enum {
70 READ_MODE_STD = 1,
71 READ_FAST,
72 READ_MODE_ATMEL45,
73 READ_MODE_4BYTE_ADDR_MODE_FAST,
74 READ_MODE_4BYTE_ADDR_MODE_FAST_WITH_0C_CMD,
75};
76
77enum {
78 WRITE_MODE_PAGE_PGM = 1,
79 WRITE_MODE_PAGE_WRITE,
80 WRITE_MODE_1BYTE_AAI,
81 WRITE_MODE_2BYTE_AAI,
82 WRITE_MODE_128BYTE_PAGE,
83 WRITE_MODE_PAGE_AT26DF041,
84 WRITE_MODE_SILICON_BLUE_FPGA,
85 WRITE_MODE_64_BYTE_PAGE_NUMONYX_PCM, /* unit of length 512 bytes */
86 WRITE_MODE_4BYTE_ADDR_MODE_256BYTE_PAGE_PGM,
87 WRITE_MODE_32BYTE_PAGE_PGM_MXIC_512K, /* unit of length 512 bytes */
88 WRITE_MODE_4BYTE_ADDR_MODE_256BYTE_PAGE_PGM_12_COMMAND,
89 WRITE_MODE_4BYTE_ADDR_MODE_256BYTE_PAGE_PGM_CHECKING_FLAGS,
90};
91
Simon Glasse53cc1d2015-01-08 05:48:51 -070092static int current_led_status = -1;
93
Simon Glassd01002b2015-01-08 05:44:16 -070094enum {
95 SPEED_24M,
96 SPEED_8M,
97 SPEED_12M,
98 SPEED_3M,
99 SPEED_2_18M,
100 SPEED_1_5M,
101 SPEED_750K,
102 SPEED_375K,
103
104 SPEED_COUNT,
105 SPEED_UNKNOWN,
106};
107
108static const char *const speeds[SPEED_COUNT] = {
109 "24",
110 "8",
111 "12",
112 "3",
113 "2.18",
114 "1.5",
115 ".750",
116 ".375",
117};
118
hailfinger1ff33dc2010-07-03 11:02:10 +0000119/* Might be useful for other pieces of code as well. */
120static void print_hex(void *buf, size_t len)
hailfingerdfb32a02010-01-19 11:15:48 +0000121{
122 size_t i;
123
124 for (i = 0; i < len; i++)
125 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
126}
Simon Glass543101a2015-01-08 06:28:13 -0700127
128/* Helper function to read data from dediprog */
129static int dediprog_read(enum cmd_t cmd, int value, int index, char *bytes,
130 int size)
131{
132 return usb_control_msg(dediprog_handle,
133 USB_ENDPOINT_IN | USB_TYPE_VENDOR |
134 USB_RECIP_ENDPOINT, cmd, value, index,
135 bytes, size, DEFAULT_TIMEOUT);
136}
137
138/* Helper function to write data from dediprog */
139static int dediprog_write(enum cmd_t cmd, int value, int index, char *bytes,
140 int size)
141{
142 return usb_control_msg(dediprog_handle,
143 USB_ENDPOINT_OUT | USB_TYPE_VENDOR |
144 USB_RECIP_ENDPOINT, cmd, value, index,
145 bytes, size, DEFAULT_TIMEOUT);
146}
hailfingerdfb32a02010-01-19 11:15:48 +0000147
hailfinger1ff33dc2010-07-03 11:02:10 +0000148/* Might be useful for other USB devices as well. static for now. */
149static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
hailfingerdfb32a02010-01-19 11:15:48 +0000150{
151 struct usb_bus *bus;
152 struct usb_device *dev;
153
154 for (bus = usb_get_busses(); bus; bus = bus->next)
155 for (dev = bus->devices; dev; dev = dev->next)
156 if ((dev->descriptor.idVendor == vid) &&
157 (dev->descriptor.idProduct == pid))
158 return dev;
159
160 return NULL;
161}
162
stepan4c06de72011-01-28 09:00:15 +0000163static int dediprog_set_leds(int leds)
164{
165 int ret, target_leds;
uwe8d342eb2011-07-28 08:13:25 +0000166
Simon Glasse53cc1d2015-01-08 05:48:51 -0700167 if (leds < 0 || leds > LED_ALL)
168 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000169 if (leds == current_led_status)
170 return 0;
171
172 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
173 * two LEDs, and they were reversed. So map them around if
174 * we have an old device. On those devices the LEDs map as
175 * follows:
176 * bit 2 == 0: green light is on.
177 * bit 0 == 0: red light is on.
178 */
Simon Glass543101a2015-01-08 06:28:13 -0700179 if (dediprog_firmwareversion >= FIRMWARE_VERSION(6, 0, 0)) {
180 target_leds = (leds ^ 7) << 8;
181 ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0);
stepan4c06de72011-01-28 09:00:15 +0000182 } else {
Simon Glass543101a2015-01-08 06:28:13 -0700183 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
184 target_leds = ((leds & LED_ERROR) >> 2) |
185 ((leds & LED_PASS) << 2);
186 } else {
187 target_leds = leds;
188 }
189 target_leds ^= 7;
stepan4c06de72011-01-28 09:00:15 +0000190
Simon Glass543101a2015-01-08 06:28:13 -0700191 ret = dediprog_write(CMD_SET_IO_LED, 9, target_leds, NULL, 0);
192 }
stepan4c06de72011-01-28 09:00:15 +0000193 if (ret != 0x0) {
uwe8d342eb2011-07-28 08:13:25 +0000194 msg_perr("Command Set LED 0x%x failed (%s)!\n",
195 leds, usb_strerror());
stepan4c06de72011-01-28 09:00:15 +0000196 return 1;
197 }
198
199 current_led_status = leds;
200
201 return 0;
202}
203
hailfingerf76cc322010-11-09 22:00:31 +0000204static int dediprog_set_spi_voltage(int millivolt)
hailfingerdfb32a02010-01-19 11:15:48 +0000205{
206 int ret;
hailfingerf76cc322010-11-09 22:00:31 +0000207 uint16_t voltage_selector;
hailfingerdfb32a02010-01-19 11:15:48 +0000208
hailfingerf76cc322010-11-09 22:00:31 +0000209 switch (millivolt) {
210 case 0:
hailfingerdfb32a02010-01-19 11:15:48 +0000211 /* Admittedly this one is an assumption. */
hailfingerf76cc322010-11-09 22:00:31 +0000212 voltage_selector = 0x0;
hailfingerdfb32a02010-01-19 11:15:48 +0000213 break;
hailfingerf76cc322010-11-09 22:00:31 +0000214 case 1800:
215 voltage_selector = 0x12;
hailfingerdfb32a02010-01-19 11:15:48 +0000216 break;
hailfingerf76cc322010-11-09 22:00:31 +0000217 case 2500:
218 voltage_selector = 0x11;
hailfingerdfb32a02010-01-19 11:15:48 +0000219 break;
hailfingerf76cc322010-11-09 22:00:31 +0000220 case 3500:
221 voltage_selector = 0x10;
hailfingerdfb32a02010-01-19 11:15:48 +0000222 break;
223 default:
hailfingerf76cc322010-11-09 22:00:31 +0000224 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
hailfingerdfb32a02010-01-19 11:15:48 +0000225 return 1;
226 }
hailfingerf76cc322010-11-09 22:00:31 +0000227 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
228 millivolt % 1000);
hailfingerdfb32a02010-01-19 11:15:48 +0000229
Simon Glass543101a2015-01-08 06:28:13 -0700230 ret = dediprog_write(CMD_SET_TARGET_FLASH_VCC, voltage_selector, 0xff,
231 NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000232 if (ret != 0x0) {
uwe8d342eb2011-07-28 08:13:25 +0000233 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
234 voltage_selector);
hailfingerdfb32a02010-01-19 11:15:48 +0000235 return 1;
236 }
237 return 0;
238}
239
hailfinger1ff33dc2010-07-03 11:02:10 +0000240static int dediprog_set_spi_speed(uint16_t speed)
hailfingerdfb32a02010-01-19 11:15:48 +0000241{
242 int ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000243
Simon Glassd01002b2015-01-08 05:44:16 -0700244 msg_pdbg("Setting SPI speed to %u kHz\n",
245 (int)(atof(speeds[speed]) * 1000));
hailfingerdfb32a02010-01-19 11:15:48 +0000246
Simon Glass543101a2015-01-08 06:28:13 -0700247 ret = dediprog_write(CMD_SET_SPI_CLK, speed, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000248 if (ret != 0x0) {
249 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
250 return 1;
251 }
252 return 0;
253}
254
hailfingerfb6287d2010-11-16 21:25:29 +0000255/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
256 * @start start address
257 * @len length
258 * @return 0 on success, 1 on failure
259 */
260static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000261 unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000262{
263 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +0000264 unsigned int i;
hailfingerfb6287d2010-11-16 21:25:29 +0000265 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000266 const unsigned int chunksize = 0x200;
267 const unsigned int count = len / chunksize;
Simon Glass543101a2015-01-08 06:28:13 -0700268 unsigned int cmd_len;
hailfingerfb6287d2010-11-16 21:25:29 +0000269
270 if ((start % chunksize) || (len % chunksize)) {
271 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
272 "at flashrom@flashrom.org\n", __func__, start, len);
273 return 1;
274 }
275
276 /* No idea if the hardware can handle empty reads, so chicken out. */
277 if (!len)
278 return 0;
279 /* Command Read SPI Bulk. No idea which read command is used on the
280 * SPI side.
281 */
Simon Glass543101a2015-01-08 06:28:13 -0700282 if (dediprog_firmwareversion >= FIRMWARE_VERSION(6, 0, 0)) {
283 const char read_cmd_v6[] = {
284 count & 0xff,
285 (count >> 8) & 0xff,
286 0,
287 READ_FAST,
288 0,
289 0,
290 start & 0xff,
291 (start >> 8) & 0xff,
292 (start >> 16) & 0xff,
293 (start >> 24) & 0xff,
294 };
295
296 cmd_len = sizeof(read_cmd_v6);
297 ret = dediprog_write(CMD_READ, 0, 0, (char *)read_cmd_v6,
298 cmd_len);
299 } else {
300 const char read_cmd[] = {count & 0xff,
301 (count >> 8) & 0xff,
302 chunksize & 0xff,
303 (chunksize >> 8) & 0xff};
304
305 cmd_len = sizeof(read_cmd);
306 ret = dediprog_write(CMD_READ, start % 0x10000, start / 0x10000,
307 (char *)read_cmd, cmd_len);
308 }
309 if (ret != cmd_len) {
hailfingerfb6287d2010-11-16 21:25:29 +0000310 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
311 usb_strerror());
312 return 1;
313 }
314
315 for (i = 0; i < count; i++) {
316 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
317 (char *)buf + i * chunksize, chunksize,
318 DEFAULT_TIMEOUT);
319 if (ret != chunksize) {
320 msg_perr("SPI bulk read %i failed, expected %i, got %i "
321 "%s!\n", i, chunksize, ret, usb_strerror());
322 return 1;
323 }
324 }
325
326 return 0;
327}
328
stefanctc5eb8a92011-11-23 09:13:48 +0000329static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf,
330 unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000331{
hailfingerfb6287d2010-11-16 21:25:29 +0000332 int ret;
333 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000334 const unsigned int chunksize = 0x200;
335 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
336 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000337
Simon Glasse53cc1d2015-01-08 05:48:51 -0700338 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000339
hailfingerfb6287d2010-11-16 21:25:29 +0000340 if (residue) {
341 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
342 start, residue);
343 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700344 if (ret)
345 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000346 }
347
348 /* Round down. */
349 bulklen = (len - residue) / chunksize * chunksize;
350 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
351 bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700352 if (ret)
353 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000354
355 len -= residue + bulklen;
356 if (len) {
357 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
358 start, len);
359 ret = spi_read_chunked(flash, buf + residue + bulklen,
360 start + residue + bulklen, len, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700361 if (ret)
362 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000363 }
364
Simon Glasse53cc1d2015-01-08 05:48:51 -0700365 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000366 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700367err:
368 dediprog_set_leds(LED_ERROR);
369 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000370}
371
uwe8d342eb2011-07-28 08:13:25 +0000372static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000373 unsigned int start, unsigned int len)
hailfinger556e9c32010-11-23 21:28:16 +0000374{
stepan4c06de72011-01-28 09:00:15 +0000375 int ret;
376
Simon Glasse53cc1d2015-01-08 05:48:51 -0700377 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000378
hailfinger556e9c32010-11-23 21:28:16 +0000379 /* No idea about the real limit. Maybe 12, maybe more, maybe less. */
stepan4c06de72011-01-28 09:00:15 +0000380 ret = spi_write_chunked(flash, buf, start, len, 12);
381
382 if (ret)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700383 dediprog_set_leds(LED_ERROR);
stepan4c06de72011-01-28 09:00:15 +0000384 else
Simon Glasse53cc1d2015-01-08 05:48:51 -0700385 dediprog_set_leds(LED_PASS);
stepan4c06de72011-01-28 09:00:15 +0000386
387 return ret;
hailfinger556e9c32010-11-23 21:28:16 +0000388}
389
mkarcherd264e9e2011-05-11 17:07:07 +0000390static int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerdfb32a02010-01-19 11:15:48 +0000391 const unsigned char *writearr, unsigned char *readarr)
392{
393 int ret;
394
hailfingeraf389cc2010-01-22 02:53:30 +0000395 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Simon Glass543101a2015-01-08 06:28:13 -0700396 if (dediprog_firmwareversion >= FIRMWARE_VERSION(6, 0, 0)) {
397 ret = dediprog_write(CMD_TRANSCEIVE, readcnt ? 1 : 0, 0,
398 (char *)writearr, writecnt);
399 } else {
400 ret = dediprog_write(CMD_TRANSCEIVE, 0, readcnt ? 0x1 : 0x0,
401 (char *)writearr, writecnt);
402 }
hailfingerdfb32a02010-01-19 11:15:48 +0000403 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000404 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
405 writecnt, ret, usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000406 return 1;
407 }
408 if (!readcnt)
409 return 0;
410 memset(readarr, 0, readcnt);
Simon Glass543101a2015-01-08 06:28:13 -0700411 ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, (char *)readarr, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000412 if (ret != readcnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000413 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
414 readcnt, ret, usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000415 return 1;
416 }
417 return 0;
418}
419
hailfinger1ff33dc2010-07-03 11:02:10 +0000420static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000421{
422 int ret;
hailfinger7a009082010-11-09 23:30:43 +0000423 int fw[3];
hailfingerdfb32a02010-01-19 11:15:48 +0000424 char buf[0x11];
425
hailfingerdfb32a02010-01-19 11:15:48 +0000426 /* Command Receive Device String. */
427 memset(buf, 0, sizeof(buf));
Simon Glass543101a2015-01-08 06:28:13 -0700428 ret = dediprog_read(CMD_READ_PROGRAMMER_INFO, 0, 0, buf, 0x10);
hailfingerdfb32a02010-01-19 11:15:48 +0000429 if (ret != 0x10) {
430 msg_perr("Incomplete/failed Command Receive Device String!\n");
431 return 1;
432 }
433 buf[0x10] = '\0';
434 msg_pdbg("Found a %s\n", buf);
435 if (memcmp(buf, "SF100", 0x5)) {
436 msg_perr("Device not a SF100!\n");
437 return 1;
438 }
hailfinger7a009082010-11-09 23:30:43 +0000439 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
Simon Glass543101a2015-01-08 06:28:13 -0700440 msg_perr("Unexpected firmware version string '%s'\n", buf);
hailfinger7a009082010-11-09 23:30:43 +0000441 return 1;
442 }
hailfingerdfb32a02010-01-19 11:15:48 +0000443 /* Only these versions were tested. */
Simon Glass543101a2015-01-08 06:28:13 -0700444 if (fw[0] < 2 || fw[0] > 6) {
hailfinger7a009082010-11-09 23:30:43 +0000445 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
446 fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000447 return 1;
448 }
stepan4c06de72011-01-28 09:00:15 +0000449 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000450 return 0;
451}
452
Simon Glassa1f80682015-01-08 05:55:29 -0700453static int dediprog_device_init(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000454{
455 int ret;
456 char buf[0x1];
457
458 memset(buf, 0, sizeof(buf));
Simon Glassa1f80682015-01-08 05:55:29 -0700459 ret = usb_control_msg(dediprog_handle, 0xc3, CMD_INIT, 0x0, 0x0, buf,
uwe8d342eb2011-07-28 08:13:25 +0000460 0x1, DEFAULT_TIMEOUT);
hailfinger7a009082010-11-09 23:30:43 +0000461 if (ret < 0) {
462 msg_perr("Command A failed (%s)!\n", usb_strerror());
463 return 1;
464 }
hailfingerdfb32a02010-01-19 11:15:48 +0000465 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -0700466 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000467 return 1;
468 }
469 return 0;
470}
471
Simon Glassa1f80682015-01-08 05:55:29 -0700472static int set_target_flash(enum flash_type type)
hailfingerdfb32a02010-01-19 11:15:48 +0000473{
474 int ret;
475
Simon Glass543101a2015-01-08 06:28:13 -0700476 ret = dediprog_write(CMD_SET_FLASH_TYPE, type, 0, NULL, 0);
hailfingerdfb32a02010-01-19 11:15:48 +0000477 if (ret != 0x0) {
Simon Glassa1f80682015-01-08 05:55:29 -0700478 msg_perr("set_target_flash failed (%s)!\n", usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000479 return 1;
480 }
481 return 0;
482}
483
Simon Glassd01002b2015-01-08 05:44:16 -0700484static int parse_speed(char *speed_str)
485{
486 int i;
487
488 for (i = 0; i < SPEED_COUNT; i++) {
489 if (!strcmp(speed_str, speeds[i]))
490 return i;
491 }
492
493 return SPEED_UNKNOWN;
494}
495
496static void list_speeds(void)
497{
498 int i;
499
500 for (i = 0; i < SPEED_COUNT; i++)
501 msg_perr("%s%s", speeds[i], i == SPEED_COUNT - 1 ? "" : ", ");
502 msg_perr("\n");
503}
504
hailfingerf76cc322010-11-09 22:00:31 +0000505static int parse_voltage(char *voltage)
506{
507 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000508 int i;
509 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000510
511 if (!voltage || !strlen(voltage)) {
512 msg_perr("Empty voltage= specified.\n");
513 return -1;
514 }
515 millivolt = (int)strtol(voltage, &tmp, 0);
516 voltage = tmp;
517 /* Handle "," and "." as decimal point. Everything after it is assumed
518 * to be in decimal notation.
519 */
520 if ((*voltage == '.') || (*voltage == ',')) {
521 voltage++;
522 for (i = 0; i < 3; i++) {
523 fraction *= 10;
524 /* Don't advance if the current character is invalid,
525 * but continue multiplying.
526 */
527 if ((*voltage < '0') || (*voltage > '9'))
528 continue;
529 fraction += *voltage - '0';
530 voltage++;
531 }
532 /* Throw away remaining digits. */
533 voltage += strspn(voltage, "0123456789");
534 }
535 /* The remaining string must be empty or "mV" or "V". */
536 tolower_string(voltage);
537
538 /* No unit or "V". */
539 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
540 millivolt *= 1000;
541 millivolt += fraction;
542 } else if (!strncmp(voltage, "mv", 2) ||
543 !strncmp(voltage, "milliv", 6)) {
544 /* No adjustment. fraction is discarded. */
545 } else {
546 /* Garbage at the end of the string. */
547 msg_perr("Garbage voltage= specified.\n");
548 return -1;
549 }
550 return millivolt;
551}
552
mkarcherd264e9e2011-05-11 17:07:07 +0000553static const struct spi_programmer spi_programmer_dediprog = {
uwe8d342eb2011-07-28 08:13:25 +0000554 .type = SPI_CONTROLLER_DEDIPROG,
555 .max_data_read = MAX_DATA_UNSPECIFIED,
556 .max_data_write = MAX_DATA_UNSPECIFIED,
557 .command = dediprog_spi_send_command,
558 .multicommand = default_spi_send_multicommand,
559 .read = dediprog_spi_read,
560 .write_256 = dediprog_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000561};
562
dhendrix0ffc2eb2011-06-14 01:35:36 +0000563static int dediprog_shutdown(void *data)
564{
565 msg_pspew("%s\n", __func__);
566
567 /* URB 28. Command Set SPI Voltage to 0. */
568 if (dediprog_set_spi_voltage(0x0))
569 return 1;
570
571 if (usb_release_interface(dediprog_handle, 0)) {
572 msg_perr("Could not release USB interface!\n");
573 return 1;
574 }
575 if (usb_close(dediprog_handle)) {
576 msg_perr("Could not close USB device!\n");
577 return 1;
578 }
579 return 0;
580}
581
Simon Glass543101a2015-01-08 06:28:13 -0700582/* Return the 8-byte UID for the flash */
583static int get_uid(char buf[])
584{
585 int ret;
586
587 ret = dediprog_read(CMD_GET_UID, 0, 0, buf, 8);
588 if (ret != 8) {
589 msg_perr("get_uid failed (%s)!\n", usb_strerror());
590 return 1;
591 }
592
593 return 0;
594}
595
hailfingerdfb32a02010-01-19 11:15:48 +0000596/* URB numbers refer to the first log ever captured. */
597int dediprog_init(void)
598{
599 struct usb_device *dev;
hailfingerf76cc322010-11-09 22:00:31 +0000600 char *voltage;
hailfingerb91c08c2011-08-15 19:54:20 +0000601 int millivolt = 3500;
602 int ret;
Simon Glassd01002b2015-01-08 05:44:16 -0700603 char *speed_str;
604 int speed = SPEED_12M;
Simon Glass543101a2015-01-08 06:28:13 -0700605 char uid[8];
hailfingerdfb32a02010-01-19 11:15:48 +0000606
607 msg_pspew("%s\n", __func__);
608
hailfingerf76cc322010-11-09 22:00:31 +0000609 voltage = extract_programmer_param("voltage");
610 if (voltage) {
611 millivolt = parse_voltage(voltage);
612 free(voltage);
uwe8d342eb2011-07-28 08:13:25 +0000613 if (millivolt < 0)
hailfingerf76cc322010-11-09 22:00:31 +0000614 return 1;
hailfingerf76cc322010-11-09 22:00:31 +0000615 msg_pinfo("Setting voltage to %i mV\n", millivolt);
616 }
Simon Glassd01002b2015-01-08 05:44:16 -0700617 speed_str = extract_programmer_param("speed");
618 if (speed_str) {
619 speed = parse_speed(speed_str);
620 if (speed == SPEED_UNKNOWN) {
621 msg_perr("Invalid speed '%s', valid speeds in MHz are: ",
622 speed_str);
623 list_speeds();
624 free(speed_str);
625 return 1;
626 }
627 free(speed_str);
628 }
hailfingerf76cc322010-11-09 22:00:31 +0000629
hailfingerdfb32a02010-01-19 11:15:48 +0000630 /* Here comes the USB stuff. */
631 usb_init();
632 usb_find_busses();
633 usb_find_devices();
634 dev = get_device_by_vid_pid(0x0483, 0xdada);
635 if (!dev) {
636 msg_perr("Could not find a Dediprog SF100 on USB!\n");
637 return 1;
638 }
639 msg_pdbg("Found USB device (%04x:%04x).\n",
uwe8d342eb2011-07-28 08:13:25 +0000640 dev->descriptor.idVendor, dev->descriptor.idProduct);
hailfingerdfb32a02010-01-19 11:15:48 +0000641 dediprog_handle = usb_open(dev);
hailfingerfb6287d2010-11-16 21:25:29 +0000642 ret = usb_set_configuration(dediprog_handle, 1);
643 if (ret < 0) {
644 msg_perr("Could not set USB device configuration: %i %s\n",
645 ret, usb_strerror());
646 if (usb_close(dediprog_handle))
647 msg_perr("Could not close USB device!\n");
648 return 1;
649 }
650 ret = usb_claim_interface(dediprog_handle, 0);
651 if (ret < 0) {
652 msg_perr("Could not claim USB device interface %i: %i %s\n",
653 0, ret, usb_strerror());
654 if (usb_close(dediprog_handle))
655 msg_perr("Could not close USB device!\n");
656 return 1;
657 }
658 dediprog_endpoint = 2;
stepan4c06de72011-01-28 09:00:15 +0000659
dhendrix0ffc2eb2011-06-14 01:35:36 +0000660 if (register_shutdown(dediprog_shutdown, NULL))
661 return 1;
662
Simon Glassa1f80682015-01-08 05:55:29 -0700663 if (dediprog_device_init())
hailfingerdfb32a02010-01-19 11:15:48 +0000664 return 1;
Simon Glassa1f80682015-01-08 05:55:29 -0700665 if (dediprog_check_devicestring())
hailfingerdfb32a02010-01-19 11:15:48 +0000666 return 1;
Simon Glassa1f80682015-01-08 05:55:29 -0700667 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700668 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000669 return 1;
stepan4c06de72011-01-28 09:00:15 +0000670 }
hailfingerdfb32a02010-01-19 11:15:48 +0000671 /* URB 11. Command Set SPI Voltage. */
stepan4c06de72011-01-28 09:00:15 +0000672 if (dediprog_set_spi_voltage(millivolt)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700673 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000674 return 1;
stepan4c06de72011-01-28 09:00:15 +0000675 }
Simon Glassd01002b2015-01-08 05:44:16 -0700676 if (dediprog_set_spi_speed(speed)) {
677 dediprog_set_leds(LED_ERROR);
678 return 1;
679 }
Simon Glass543101a2015-01-08 06:28:13 -0700680 if (!get_uid(uid)) {
681 msg_pdbg("UID: ");
682 print_hex(uid, sizeof(uid));
683 msg_pdbg("\n");
684 }
hailfingerdfb32a02010-01-19 11:15:48 +0000685
mkarcherd264e9e2011-05-11 17:07:07 +0000686 register_spi_programmer(&spi_programmer_dediprog);
hailfingerdfb32a02010-01-19 11:15:48 +0000687
Simon Glasse53cc1d2015-01-08 05:48:51 -0700688 dediprog_set_leds(0);
stepan4c06de72011-01-28 09:00:15 +0000689
hailfingerdfb32a02010-01-19 11:15:48 +0000690 return 0;
691}