blob: eaf2a24d72ac05df46162cd4dbca948afda6002c [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
hailfinger1ff33dc2010-07-03 11:02:10 +000094#if 0
95/* Might be useful for other pieces of code as well. */
96static void print_hex(void *buf, size_t len)
hailfingerdfb32a02010-01-19 11:15:48 +000097{
98 size_t i;
99
100 for (i = 0; i < len; i++)
101 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
102}
hailfinger1ff33dc2010-07-03 11:02:10 +0000103#endif
hailfingerdfb32a02010-01-19 11:15:48 +0000104
hailfinger1ff33dc2010-07-03 11:02:10 +0000105/* Might be useful for other USB devices as well. static for now. */
106static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
hailfingerdfb32a02010-01-19 11:15:48 +0000107{
108 struct usb_bus *bus;
109 struct usb_device *dev;
110
111 for (bus = usb_get_busses(); bus; bus = bus->next)
112 for (dev = bus->devices; dev; dev = dev->next)
113 if ((dev->descriptor.idVendor == vid) &&
114 (dev->descriptor.idProduct == pid))
115 return dev;
116
117 return NULL;
118}
119
120//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
121
stepan4c06de72011-01-28 09:00:15 +0000122static int dediprog_set_leds(int leds)
123{
124 int ret, target_leds;
uwe8d342eb2011-07-28 08:13:25 +0000125
Simon Glasse53cc1d2015-01-08 05:48:51 -0700126 if (leds < 0 || leds > LED_ALL)
127 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +0000128 if (leds == current_led_status)
129 return 0;
130
131 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
132 * two LEDs, and they were reversed. So map them around if
133 * we have an old device. On those devices the LEDs map as
134 * follows:
135 * bit 2 == 0: green light is on.
136 * bit 0 == 0: red light is on.
137 */
138 if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700139 target_leds = ((leds & LED_ERROR) >> 2) |
140 ((leds & LED_PASS) << 2);
stepan4c06de72011-01-28 09:00:15 +0000141 } else {
142 target_leds = leds;
143 }
144
Simon Glasse53cc1d2015-01-08 05:48:51 -0700145 target_leds ^= 7;
uwe8d342eb2011-07-28 08:13:25 +0000146 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
147 NULL, 0x0, DEFAULT_TIMEOUT);
stepan4c06de72011-01-28 09:00:15 +0000148 if (ret != 0x0) {
uwe8d342eb2011-07-28 08:13:25 +0000149 msg_perr("Command Set LED 0x%x failed (%s)!\n",
150 leds, usb_strerror());
stepan4c06de72011-01-28 09:00:15 +0000151 return 1;
152 }
153
154 current_led_status = leds;
155
156 return 0;
157}
158
hailfingerf76cc322010-11-09 22:00:31 +0000159static int dediprog_set_spi_voltage(int millivolt)
hailfingerdfb32a02010-01-19 11:15:48 +0000160{
161 int ret;
hailfingerf76cc322010-11-09 22:00:31 +0000162 uint16_t voltage_selector;
hailfingerdfb32a02010-01-19 11:15:48 +0000163
hailfingerf76cc322010-11-09 22:00:31 +0000164 switch (millivolt) {
165 case 0:
hailfingerdfb32a02010-01-19 11:15:48 +0000166 /* Admittedly this one is an assumption. */
hailfingerf76cc322010-11-09 22:00:31 +0000167 voltage_selector = 0x0;
hailfingerdfb32a02010-01-19 11:15:48 +0000168 break;
hailfingerf76cc322010-11-09 22:00:31 +0000169 case 1800:
170 voltage_selector = 0x12;
hailfingerdfb32a02010-01-19 11:15:48 +0000171 break;
hailfingerf76cc322010-11-09 22:00:31 +0000172 case 2500:
173 voltage_selector = 0x11;
hailfingerdfb32a02010-01-19 11:15:48 +0000174 break;
hailfingerf76cc322010-11-09 22:00:31 +0000175 case 3500:
176 voltage_selector = 0x10;
hailfingerdfb32a02010-01-19 11:15:48 +0000177 break;
178 default:
hailfingerf76cc322010-11-09 22:00:31 +0000179 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
hailfingerdfb32a02010-01-19 11:15:48 +0000180 return 1;
181 }
hailfingerf76cc322010-11-09 22:00:31 +0000182 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
183 millivolt % 1000);
hailfingerdfb32a02010-01-19 11:15:48 +0000184
Simon Glassa1f80682015-01-08 05:55:29 -0700185 ret = usb_control_msg(dediprog_handle, 0x42, CMD_SET_TARGET_FLASH_VCC,
186 voltage_selector, 0xff, NULL, 0x0,
187 DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000188 if (ret != 0x0) {
uwe8d342eb2011-07-28 08:13:25 +0000189 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
190 voltage_selector);
hailfingerdfb32a02010-01-19 11:15:48 +0000191 return 1;
192 }
193 return 0;
194}
195
hailfinger1ff33dc2010-07-03 11:02:10 +0000196#if 0
hailfingerdfb32a02010-01-19 11:15:48 +0000197/* After dediprog_set_spi_speed, the original app always calls
198 * dediprog_set_spi_voltage(0) and then
199 * dediprog_check_devicestring() four times in a row.
200 * After that, dediprog_command_a() is called.
201 * This looks suspiciously like the microprocessor in the SF100 has to be
202 * restarted/reinitialized in case the speed changes.
203 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000204static int dediprog_set_spi_speed(uint16_t speed)
hailfingerdfb32a02010-01-19 11:15:48 +0000205{
206 int ret;
207 unsigned int khz;
208
209 /* Case 1 and 2 are in weird order. Probably an organically "grown"
210 * interface.
211 * Base frequency is 24000 kHz, divisors are (in order)
212 * 1, 3, 2, 8, 11, 16, 32, 64.
213 */
214 switch (speed) {
215 case 0x0:
216 khz = 24000;
217 break;
218 case 0x1:
219 khz = 8000;
220 break;
221 case 0x2:
222 khz = 12000;
223 break;
224 case 0x3:
225 khz = 3000;
226 break;
227 case 0x4:
228 khz = 2180;
229 break;
230 case 0x5:
231 khz = 1500;
232 break;
233 case 0x6:
234 khz = 750;
235 break;
236 case 0x7:
237 khz = 375;
238 break;
239 default:
240 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
241 return 1;
242 }
243 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
244
uwe8d342eb2011-07-28 08:13:25 +0000245 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
246 0x0, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000247 if (ret != 0x0) {
248 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
249 return 1;
250 }
251 return 0;
252}
hailfinger1ff33dc2010-07-03 11:02:10 +0000253#endif
hailfingerdfb32a02010-01-19 11:15:48 +0000254
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;
hailfingerfb6287d2010-11-16 21:25:29 +0000268 const char count_and_chunk[] = {count & 0xff,
269 (count >> 8) & 0xff,
270 chunksize & 0xff,
271 (chunksize >> 8) & 0xff};
272
273 if ((start % chunksize) || (len % chunksize)) {
274 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
275 "at flashrom@flashrom.org\n", __func__, start, len);
276 return 1;
277 }
278
279 /* No idea if the hardware can handle empty reads, so chicken out. */
280 if (!len)
281 return 0;
282 /* Command Read SPI Bulk. No idea which read command is used on the
283 * SPI side.
284 */
285 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
286 start / 0x10000, (char *)count_and_chunk,
287 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
288 if (ret != sizeof(count_and_chunk)) {
289 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
290 usb_strerror());
291 return 1;
292 }
293
294 for (i = 0; i < count; i++) {
295 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
296 (char *)buf + i * chunksize, chunksize,
297 DEFAULT_TIMEOUT);
298 if (ret != chunksize) {
299 msg_perr("SPI bulk read %i failed, expected %i, got %i "
300 "%s!\n", i, chunksize, ret, usb_strerror());
301 return 1;
302 }
303 }
304
305 return 0;
306}
307
stefanctc5eb8a92011-11-23 09:13:48 +0000308static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf,
309 unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000310{
hailfingerfb6287d2010-11-16 21:25:29 +0000311 int ret;
312 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000313 const unsigned int chunksize = 0x200;
314 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
315 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000316
Simon Glasse53cc1d2015-01-08 05:48:51 -0700317 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000318
hailfingerfb6287d2010-11-16 21:25:29 +0000319 if (residue) {
320 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
321 start, residue);
322 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700323 if (ret)
324 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000325 }
326
327 /* Round down. */
328 bulklen = (len - residue) / chunksize * chunksize;
329 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
330 bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700331 if (ret)
332 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000333
334 len -= residue + bulklen;
335 if (len) {
336 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
337 start, len);
338 ret = spi_read_chunked(flash, buf + residue + bulklen,
339 start + residue + bulklen, len, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700340 if (ret)
341 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000342 }
343
Simon Glasse53cc1d2015-01-08 05:48:51 -0700344 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000345 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700346err:
347 dediprog_set_leds(LED_ERROR);
348 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000349}
350
uwe8d342eb2011-07-28 08:13:25 +0000351static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000352 unsigned int start, unsigned int len)
hailfinger556e9c32010-11-23 21:28:16 +0000353{
stepan4c06de72011-01-28 09:00:15 +0000354 int ret;
355
Simon Glasse53cc1d2015-01-08 05:48:51 -0700356 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000357
hailfinger556e9c32010-11-23 21:28:16 +0000358 /* No idea about the real limit. Maybe 12, maybe more, maybe less. */
stepan4c06de72011-01-28 09:00:15 +0000359 ret = spi_write_chunked(flash, buf, start, len, 12);
360
361 if (ret)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700362 dediprog_set_leds(LED_ERROR);
stepan4c06de72011-01-28 09:00:15 +0000363 else
Simon Glasse53cc1d2015-01-08 05:48:51 -0700364 dediprog_set_leds(LED_PASS);
stepan4c06de72011-01-28 09:00:15 +0000365
366 return ret;
hailfinger556e9c32010-11-23 21:28:16 +0000367}
368
mkarcherd264e9e2011-05-11 17:07:07 +0000369static int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerdfb32a02010-01-19 11:15:48 +0000370 const unsigned char *writearr, unsigned char *readarr)
371{
372 int ret;
373
hailfingeraf389cc2010-01-22 02:53:30 +0000374 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000375 /* Paranoid, but I don't want to be blamed if anything explodes. */
hailfinger556e9c32010-11-23 21:28:16 +0000376 if (writecnt > 16) {
hailfingerdfb32a02010-01-19 11:15:48 +0000377 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
hailfingeraf389cc2010-01-22 02:53:30 +0000378 return 1;
379 }
380 /* 16 byte reads should work. */
381 if (readcnt > 16) {
hailfingerdfb32a02010-01-19 11:15:48 +0000382 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
hailfingeraf389cc2010-01-22 02:53:30 +0000383 return 1;
384 }
hailfingerdfb32a02010-01-19 11:15:48 +0000385
Simon Glassa1f80682015-01-08 05:55:29 -0700386 ret = usb_control_msg(dediprog_handle, 0x42, CMD_TRANSCEIVE, 0,
uwe8d342eb2011-07-28 08:13:25 +0000387 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
388 DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000389 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000390 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
391 writecnt, ret, usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000392 return 1;
393 }
394 if (!readcnt)
395 return 0;
396 memset(readarr, 0, readcnt);
Simon Glassa1f80682015-01-08 05:55:29 -0700397 ret = usb_control_msg(dediprog_handle, 0xc2, CMD_TRANSCEIVE, 0, 0,
uwe8d342eb2011-07-28 08:13:25 +0000398 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000399 if (ret != readcnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000400 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
401 readcnt, ret, usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000402 return 1;
403 }
404 return 0;
405}
406
hailfinger1ff33dc2010-07-03 11:02:10 +0000407static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000408{
409 int ret;
hailfinger7a009082010-11-09 23:30:43 +0000410 int fw[3];
hailfingerdfb32a02010-01-19 11:15:48 +0000411 char buf[0x11];
412
413 /* Command Prepare Receive Device String. */
414 memset(buf, 0, sizeof(buf));
uwe8d342eb2011-07-28 08:13:25 +0000415 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
416 0x1, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000417 /* The char casting is needed to stop gcc complaining about an always true comparison. */
418 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
419 msg_perr("Unexpected response to Command Prepare Receive Device"
420 " String!\n");
421 return 1;
422 }
423 /* Command Receive Device String. */
424 memset(buf, 0, sizeof(buf));
Simon Glassa1f80682015-01-08 05:55:29 -0700425 ret = usb_control_msg(dediprog_handle, 0xc2, CMD_READ_PROGRAMMER_INFO,
426 0, 0, buf, 0x10, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000427 if (ret != 0x10) {
428 msg_perr("Incomplete/failed Command Receive Device String!\n");
429 return 1;
430 }
431 buf[0x10] = '\0';
432 msg_pdbg("Found a %s\n", buf);
433 if (memcmp(buf, "SF100", 0x5)) {
434 msg_perr("Device not a SF100!\n");
435 return 1;
436 }
hailfinger7a009082010-11-09 23:30:43 +0000437 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
438 msg_perr("Unexpected firmware version string!\n");
439 return 1;
440 }
hailfingerdfb32a02010-01-19 11:15:48 +0000441 /* Only these versions were tested. */
hailfinger7a009082010-11-09 23:30:43 +0000442 if (fw[0] < 2 || fw[0] > 5) {
443 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
444 fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000445 return 1;
446 }
stepan4c06de72011-01-28 09:00:15 +0000447 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000448 return 0;
449}
450
Simon Glassa1f80682015-01-08 05:55:29 -0700451static int dediprog_device_init(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000452{
453 int ret;
454 char buf[0x1];
455
456 memset(buf, 0, sizeof(buf));
Simon Glassa1f80682015-01-08 05:55:29 -0700457 ret = usb_control_msg(dediprog_handle, 0xc3, CMD_INIT, 0x0, 0x0, buf,
uwe8d342eb2011-07-28 08:13:25 +0000458 0x1, DEFAULT_TIMEOUT);
hailfinger7a009082010-11-09 23:30:43 +0000459 if (ret < 0) {
460 msg_perr("Command A failed (%s)!\n", usb_strerror());
461 return 1;
462 }
hailfingerdfb32a02010-01-19 11:15:48 +0000463 if ((ret != 0x1) || (buf[0] != 0x6f)) {
Simon Glassa1f80682015-01-08 05:55:29 -0700464 msg_perr("Unexpected response to init!\n");
hailfingerdfb32a02010-01-19 11:15:48 +0000465 return 1;
466 }
467 return 0;
468}
469
stepanff582cc2011-01-20 21:05:15 +0000470#if 0
471/* Something.
472 * Present in eng_detect_blink.log with firmware 3.1.8
473 * Always preceded by Command Receive Device String
474 */
475static int dediprog_command_b(void)
476{
477 int ret;
478 char buf[0x3];
479
480 memset(buf, 0, sizeof(buf));
uwe8d342eb2011-07-28 08:13:25 +0000481 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
482 0x3, DEFAULT_TIMEOUT);
stepanff582cc2011-01-20 21:05:15 +0000483 if (ret < 0) {
484 msg_perr("Command B failed (%s)!\n", usb_strerror());
485 return 1;
486 }
487 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
488 (buf[2] != 0xff)) {
489 msg_perr("Unexpected response to Command B!\n");
490 return 1;
491 }
492
493 return 0;
494}
495#endif
496
Simon Glassa1f80682015-01-08 05:55:29 -0700497static int set_target_flash(enum flash_type type)
hailfingerdfb32a02010-01-19 11:15:48 +0000498{
499 int ret;
500
Simon Glassa1f80682015-01-08 05:55:29 -0700501 ret = usb_control_msg(dediprog_handle, 0x42, CMD_SET_FLASH_TYPE, type,
502 0x0, NULL, 0, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000503 if (ret != 0x0) {
Simon Glassa1f80682015-01-08 05:55:29 -0700504 msg_perr("set_target_flash failed (%s)!\n", usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000505 return 1;
506 }
507 return 0;
508}
509
hailfinger1ff33dc2010-07-03 11:02:10 +0000510#if 0
hailfingerdfb32a02010-01-19 11:15:48 +0000511/* Very strange. Seems to be a programmer keepalive or somesuch.
512 * Wait unsuccessfully for timeout ms to read one byte.
513 * Is usually called after setting voltage to 0.
stepanff582cc2011-01-20 21:05:15 +0000514 * Present in all logs with Firmware 2.1.1 and 3.1.8
hailfingerdfb32a02010-01-19 11:15:48 +0000515 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000516static int dediprog_command_f(int timeout)
hailfingerdfb32a02010-01-19 11:15:48 +0000517{
518 int ret;
519 char buf[0x1];
520
521 memset(buf, 0, sizeof(buf));
uwe8d342eb2011-07-28 08:13:25 +0000522 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
523 0x1, timeout);
stepanff582cc2011-01-20 21:05:15 +0000524 /* This check is most probably wrong. Command F always causes a timeout
525 * in the logs, so we should check for timeout instead of checking for
526 * success.
527 */
528 if (ret != 0x1) {
529 msg_perr("Command F failed (%s)!\n", usb_strerror());
530 return 1;
531 }
532 return 0;
533}
hailfinger1ff33dc2010-07-03 11:02:10 +0000534#endif
hailfingerdfb32a02010-01-19 11:15:48 +0000535
hailfingerf76cc322010-11-09 22:00:31 +0000536static int parse_voltage(char *voltage)
537{
538 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000539 int i;
540 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000541
542 if (!voltage || !strlen(voltage)) {
543 msg_perr("Empty voltage= specified.\n");
544 return -1;
545 }
546 millivolt = (int)strtol(voltage, &tmp, 0);
547 voltage = tmp;
548 /* Handle "," and "." as decimal point. Everything after it is assumed
549 * to be in decimal notation.
550 */
551 if ((*voltage == '.') || (*voltage == ',')) {
552 voltage++;
553 for (i = 0; i < 3; i++) {
554 fraction *= 10;
555 /* Don't advance if the current character is invalid,
556 * but continue multiplying.
557 */
558 if ((*voltage < '0') || (*voltage > '9'))
559 continue;
560 fraction += *voltage - '0';
561 voltage++;
562 }
563 /* Throw away remaining digits. */
564 voltage += strspn(voltage, "0123456789");
565 }
566 /* The remaining string must be empty or "mV" or "V". */
567 tolower_string(voltage);
568
569 /* No unit or "V". */
570 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
571 millivolt *= 1000;
572 millivolt += fraction;
573 } else if (!strncmp(voltage, "mv", 2) ||
574 !strncmp(voltage, "milliv", 6)) {
575 /* No adjustment. fraction is discarded. */
576 } else {
577 /* Garbage at the end of the string. */
578 msg_perr("Garbage voltage= specified.\n");
579 return -1;
580 }
581 return millivolt;
582}
583
mkarcherd264e9e2011-05-11 17:07:07 +0000584static const struct spi_programmer spi_programmer_dediprog = {
uwe8d342eb2011-07-28 08:13:25 +0000585 .type = SPI_CONTROLLER_DEDIPROG,
586 .max_data_read = MAX_DATA_UNSPECIFIED,
587 .max_data_write = MAX_DATA_UNSPECIFIED,
588 .command = dediprog_spi_send_command,
589 .multicommand = default_spi_send_multicommand,
590 .read = dediprog_spi_read,
591 .write_256 = dediprog_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000592};
593
dhendrix0ffc2eb2011-06-14 01:35:36 +0000594static int dediprog_shutdown(void *data)
595{
596 msg_pspew("%s\n", __func__);
597
598 /* URB 28. Command Set SPI Voltage to 0. */
599 if (dediprog_set_spi_voltage(0x0))
600 return 1;
601
602 if (usb_release_interface(dediprog_handle, 0)) {
603 msg_perr("Could not release USB interface!\n");
604 return 1;
605 }
606 if (usb_close(dediprog_handle)) {
607 msg_perr("Could not close USB device!\n");
608 return 1;
609 }
610 return 0;
611}
612
hailfingerdfb32a02010-01-19 11:15:48 +0000613/* URB numbers refer to the first log ever captured. */
614int dediprog_init(void)
615{
616 struct usb_device *dev;
hailfingerf76cc322010-11-09 22:00:31 +0000617 char *voltage;
hailfingerb91c08c2011-08-15 19:54:20 +0000618 int millivolt = 3500;
619 int ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000620
621 msg_pspew("%s\n", __func__);
622
hailfingerf76cc322010-11-09 22:00:31 +0000623 voltage = extract_programmer_param("voltage");
624 if (voltage) {
625 millivolt = parse_voltage(voltage);
626 free(voltage);
uwe8d342eb2011-07-28 08:13:25 +0000627 if (millivolt < 0)
hailfingerf76cc322010-11-09 22:00:31 +0000628 return 1;
hailfingerf76cc322010-11-09 22:00:31 +0000629 msg_pinfo("Setting voltage to %i mV\n", millivolt);
630 }
631
hailfingerdfb32a02010-01-19 11:15:48 +0000632 /* Here comes the USB stuff. */
633 usb_init();
634 usb_find_busses();
635 usb_find_devices();
636 dev = get_device_by_vid_pid(0x0483, 0xdada);
637 if (!dev) {
638 msg_perr("Could not find a Dediprog SF100 on USB!\n");
639 return 1;
640 }
641 msg_pdbg("Found USB device (%04x:%04x).\n",
uwe8d342eb2011-07-28 08:13:25 +0000642 dev->descriptor.idVendor, dev->descriptor.idProduct);
hailfingerdfb32a02010-01-19 11:15:48 +0000643 dediprog_handle = usb_open(dev);
hailfingerfb6287d2010-11-16 21:25:29 +0000644 ret = usb_set_configuration(dediprog_handle, 1);
645 if (ret < 0) {
646 msg_perr("Could not set USB device configuration: %i %s\n",
647 ret, usb_strerror());
648 if (usb_close(dediprog_handle))
649 msg_perr("Could not close USB device!\n");
650 return 1;
651 }
652 ret = usb_claim_interface(dediprog_handle, 0);
653 if (ret < 0) {
654 msg_perr("Could not claim USB device interface %i: %i %s\n",
655 0, ret, usb_strerror());
656 if (usb_close(dediprog_handle))
657 msg_perr("Could not close USB device!\n");
658 return 1;
659 }
660 dediprog_endpoint = 2;
stepan4c06de72011-01-28 09:00:15 +0000661
dhendrix0ffc2eb2011-06-14 01:35:36 +0000662 if (register_shutdown(dediprog_shutdown, NULL))
663 return 1;
664
Simon Glassa1f80682015-01-08 05:55:29 -0700665 if (dediprog_device_init())
hailfingerdfb32a02010-01-19 11:15:48 +0000666 return 1;
Simon Glassa1f80682015-01-08 05:55:29 -0700667 if (dediprog_check_devicestring())
hailfingerdfb32a02010-01-19 11:15:48 +0000668 return 1;
Simon Glassa1f80682015-01-08 05:55:29 -0700669 if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700670 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000671 return 1;
stepan4c06de72011-01-28 09:00:15 +0000672 }
hailfingerdfb32a02010-01-19 11:15:48 +0000673 /* URB 11. Command Set SPI Voltage. */
stepan4c06de72011-01-28 09:00:15 +0000674 if (dediprog_set_spi_voltage(millivolt)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700675 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000676 return 1;
stepan4c06de72011-01-28 09:00:15 +0000677 }
hailfingerdfb32a02010-01-19 11:15:48 +0000678
mkarcherd264e9e2011-05-11 17:07:07 +0000679 register_spi_programmer(&spi_programmer_dediprog);
hailfingerdfb32a02010-01-19 11:15:48 +0000680
681 /* RE leftover, leave in until the driver is complete. */
682#if 0
683 /* Execute RDID by hand if you want to test it. */
684 dediprog_do_stuff();
685#endif
686
Simon Glasse53cc1d2015-01-08 05:48:51 -0700687 dediprog_set_leds(0);
stepan4c06de72011-01-28 09:00:15 +0000688
hailfingerdfb32a02010-01-19 11:15:48 +0000689 return 0;
690}
691
hailfinger1ff33dc2010-07-03 11:02:10 +0000692#if 0
hailfingerdfb32a02010-01-19 11:15:48 +0000693/* Leftovers from reverse engineering. Keep for documentation purposes until
694 * completely understood.
695 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000696static int dediprog_do_stuff(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000697{
698 char buf[0x4];
699 /* SPI command processing starts here. */
700
701 /* URB 12. Command Send SPI. */
702 /* URB 13. Command Receive SPI. */
703 memset(buf, 0, sizeof(buf));
704 /* JEDEC RDID */
705 msg_pdbg("Sending RDID\n");
706 buf[0] = JEDEC_RDID;
uwe8d342eb2011-07-28 08:13:25 +0000707 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
708 (unsigned char *)buf, (unsigned char *)buf))
hailfingerdfb32a02010-01-19 11:15:48 +0000709 return 1;
710 msg_pdbg("Receiving response: ");
711 print_hex(buf, JEDEC_RDID_INSIZE);
hailfingerdfb32a02010-01-19 11:15:48 +0000712 /* URB 14-27 are more SPI commands. */
713 /* URB 28. Command Set SPI Voltage. */
714 if (dediprog_set_spi_voltage(0x0))
715 return 1;
716 /* URB 29-38. Command F, unsuccessful wait. */
717 if (dediprog_command_f(544))
718 return 1;
719 /* URB 39. Command Set SPI Voltage. */
720 if (dediprog_set_spi_voltage(0x10))
721 return 1;
722 /* URB 40. Command Set SPI Speed. */
723 if (dediprog_set_spi_speed(0x2))
724 return 1;
725 /* URB 41 is just URB 28. */
726 /* URB 42,44,46,48,51,53 is just URB 8. */
727 /* URB 43,45,47,49,52,54 is just URB 9. */
728 /* URB 50 is just URB 6/7. */
729 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
730 /* URB 132,134 is just URB 6/7. */
731 /* URB 133 is just URB 29-38. */
732 /* URB 135 is just URB 8. */
733 /* URB 136 is just URB 9. */
734 /* URB 137 is just URB 11. */
735
stepanff582cc2011-01-20 21:05:15 +0000736 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
737 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
738 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
739 * 512 bytes, rest is filled with 0xff.
740 */
hailfingerdfb32a02010-01-19 11:15:48 +0000741
hailfingerdfb32a02010-01-19 11:15:48 +0000742 return 0;
743}
hailfinger1ff33dc2010-07-03 11:02:10 +0000744#endif