blob: 1cbde18b32b2d7bc6e10a74350edf9a13600536d [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
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
Mathias Krausedb7afc52010-11-09 23:30:43 +000020#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000021#include <string.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000022#include <usb.h>
23#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000024#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000026#include "spi.h"
27
Stefan Reinauer915b8402011-01-28 09:00:15 +000028#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000029#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000030static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +000031static int dediprog_firmwareversion;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000032static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000033
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000034#if 0
35/* Might be useful for other pieces of code as well. */
36static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000037{
38 size_t i;
39
40 for (i = 0; i < len; i++)
41 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
42}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000043#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000044
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000045/* Might be useful for other USB devices as well. static for now. */
46static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000047{
48 struct usb_bus *bus;
49 struct usb_device *dev;
50
51 for (bus = usb_get_busses(); bus; bus = bus->next)
52 for (dev = bus->devices; dev; dev = dev->next)
53 if ((dev->descriptor.idVendor == vid) &&
54 (dev->descriptor.idProduct == pid))
55 return dev;
56
57 return NULL;
58}
59
60//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
61
Stefan Reinauer915b8402011-01-28 09:00:15 +000062/* Set/clear LEDs on dediprog */
63#define PASS_ON (0 << 0)
64#define PASS_OFF (1 << 0)
65#define BUSY_ON (0 << 1)
66#define BUSY_OFF (1 << 1)
67#define ERROR_ON (0 << 2)
68#define ERROR_OFF (1 << 2)
69static int current_led_status = -1;
70
71static int dediprog_set_leds(int leds)
72{
73 int ret, target_leds;
74
75 if (leds < 0 || leds > 7)
76 leds = 0; // Bogus value, enable all LEDs
77
78 if (leds == current_led_status)
79 return 0;
80
81 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
82 * two LEDs, and they were reversed. So map them around if
83 * we have an old device. On those devices the LEDs map as
84 * follows:
85 * bit 2 == 0: green light is on.
86 * bit 0 == 0: red light is on.
87 */
88 if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
89 target_leds = ((leds & ERROR_OFF) >> 2) |
90 ((leds & PASS_OFF) << 2);
91 } else {
92 target_leds = leds;
93 }
94
95 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds, NULL, 0x0, DEFAULT_TIMEOUT);
96 if (ret != 0x0) {
97 msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, usb_strerror());
98 return 1;
99 }
100
101 current_led_status = leds;
102
103 return 0;
104}
105
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000106static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000107{
108 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000109 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000110
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000111 switch (millivolt) {
112 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000113 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000114 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000115 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000116 case 1800:
117 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000118 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000119 case 2500:
120 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000121 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000122 case 3500:
123 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000124 break;
125 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000126 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000127 return 1;
128 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000129 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
130 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000131
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000132 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000133 if (ret != 0x0) {
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000134 msg_perr("Command Set SPI Voltage 0x%x failed!\n", voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000135 return 1;
136 }
137 return 0;
138}
139
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000140#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000141/* After dediprog_set_spi_speed, the original app always calls
142 * dediprog_set_spi_voltage(0) and then
143 * dediprog_check_devicestring() four times in a row.
144 * After that, dediprog_command_a() is called.
145 * This looks suspiciously like the microprocessor in the SF100 has to be
146 * restarted/reinitialized in case the speed changes.
147 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000148static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000149{
150 int ret;
151 unsigned int khz;
152
153 /* Case 1 and 2 are in weird order. Probably an organically "grown"
154 * interface.
155 * Base frequency is 24000 kHz, divisors are (in order)
156 * 1, 3, 2, 8, 11, 16, 32, 64.
157 */
158 switch (speed) {
159 case 0x0:
160 khz = 24000;
161 break;
162 case 0x1:
163 khz = 8000;
164 break;
165 case 0x2:
166 khz = 12000;
167 break;
168 case 0x3:
169 khz = 3000;
170 break;
171 case 0x4:
172 khz = 2180;
173 break;
174 case 0x5:
175 khz = 1500;
176 break;
177 case 0x6:
178 khz = 750;
179 break;
180 case 0x7:
181 khz = 375;
182 break;
183 default:
184 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
185 return 1;
186 }
187 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
188
189 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
190 if (ret != 0x0) {
191 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
192 return 1;
193 }
194 return 0;
195}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000196#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000197
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000198/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
199 * @start start address
200 * @len length
201 * @return 0 on success, 1 on failure
202 */
203static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf,
204 int start, int len)
205{
206 int ret;
207 int i;
208 /* chunksize must be 512, other sizes will NOT work at all. */
209 const int chunksize = 0x200;
210 const int count = len / chunksize;
211 const char count_and_chunk[] = {count & 0xff,
212 (count >> 8) & 0xff,
213 chunksize & 0xff,
214 (chunksize >> 8) & 0xff};
215
216 if ((start % chunksize) || (len % chunksize)) {
217 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
218 "at flashrom@flashrom.org\n", __func__, start, len);
219 return 1;
220 }
221
222 /* No idea if the hardware can handle empty reads, so chicken out. */
223 if (!len)
224 return 0;
225 /* Command Read SPI Bulk. No idea which read command is used on the
226 * SPI side.
227 */
228 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
229 start / 0x10000, (char *)count_and_chunk,
230 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
231 if (ret != sizeof(count_and_chunk)) {
232 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
233 usb_strerror());
234 return 1;
235 }
236
237 for (i = 0; i < count; i++) {
238 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
239 (char *)buf + i * chunksize, chunksize,
240 DEFAULT_TIMEOUT);
241 if (ret != chunksize) {
242 msg_perr("SPI bulk read %i failed, expected %i, got %i "
243 "%s!\n", i, chunksize, ret, usb_strerror());
244 return 1;
245 }
246 }
247
248 return 0;
249}
250
Michael Karcherb9dbe482011-05-11 17:07:07 +0000251static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000252{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000253 int ret;
254 /* chunksize must be 512, other sizes will NOT work at all. */
255 const int chunksize = 0x200;
256 int residue = start % chunksize ? chunksize - start % chunksize : 0;
257 int bulklen;
258
Stefan Reinauer915b8402011-01-28 09:00:15 +0000259 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
260
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000261 if (residue) {
262 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
263 start, residue);
264 ret = spi_read_chunked(flash, buf, start, residue, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000265 if (ret) {
266 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000267 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000268 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000269 }
270
271 /* Round down. */
272 bulklen = (len - residue) / chunksize * chunksize;
273 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
274 bulklen);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000275 if (ret) {
276 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000277 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000278 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000279
280 len -= residue + bulklen;
281 if (len) {
282 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
283 start, len);
284 ret = spi_read_chunked(flash, buf + residue + bulklen,
285 start + residue + bulklen, len, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000286 if (ret) {
287 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000288 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000289 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000290 }
291
Stefan Reinauer915b8402011-01-28 09:00:15 +0000292 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000293 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000294}
295
Michael Karcherb9dbe482011-05-11 17:07:07 +0000296static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000297{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000298 int ret;
299
300 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
301
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000302 /* No idea about the real limit. Maybe 12, maybe more, maybe less. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000303 ret = spi_write_chunked(flash, buf, start, len, 12);
304
305 if (ret)
306 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
307 else
308 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
309
310 return ret;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000311}
312
Michael Karcherb9dbe482011-05-11 17:07:07 +0000313static int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000314 const unsigned char *writearr, unsigned char *readarr)
315{
316 int ret;
317
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000318 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000319 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000320 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000321 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000322 return 1;
323 }
324 /* 16 byte reads should work. */
325 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000326 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000327 return 1;
328 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000329
330 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT);
331 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000332 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
333 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000334 return 1;
335 }
336 if (!readcnt)
337 return 0;
338 memset(readarr, 0, readcnt);
339 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT);
340 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000341 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
342 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000343 return 1;
344 }
345 return 0;
346}
347
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000348static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000349{
350 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000351 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000352 char buf[0x11];
353
354 /* Command Prepare Receive Device String. */
355 memset(buf, 0, sizeof(buf));
356 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT);
357 /* The char casting is needed to stop gcc complaining about an always true comparison. */
358 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
359 msg_perr("Unexpected response to Command Prepare Receive Device"
360 " String!\n");
361 return 1;
362 }
363 /* Command Receive Device String. */
364 memset(buf, 0, sizeof(buf));
365 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT);
366 if (ret != 0x10) {
367 msg_perr("Incomplete/failed Command Receive Device String!\n");
368 return 1;
369 }
370 buf[0x10] = '\0';
371 msg_pdbg("Found a %s\n", buf);
372 if (memcmp(buf, "SF100", 0x5)) {
373 msg_perr("Device not a SF100!\n");
374 return 1;
375 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000376 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
377 msg_perr("Unexpected firmware version string!\n");
378 return 1;
379 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000380 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000381 if (fw[0] < 2 || fw[0] > 5) {
382 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
383 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000384 return 1;
385 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000386 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000387 return 0;
388}
389
390/* Command A seems to be some sort of device init. It is either followed by
391 * dediprog_check_devicestring (often) or Command A (often) or
392 * Command F (once).
393 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000394static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000395{
396 int ret;
397 char buf[0x1];
398
399 memset(buf, 0, sizeof(buf));
400 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000401 if (ret < 0) {
402 msg_perr("Command A failed (%s)!\n", usb_strerror());
403 return 1;
404 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000405 if ((ret != 0x1) || (buf[0] != 0x6f)) {
406 msg_perr("Unexpected response to Command A!\n");
407 return 1;
408 }
409 return 0;
410}
411
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000412#if 0
413/* Something.
414 * Present in eng_detect_blink.log with firmware 3.1.8
415 * Always preceded by Command Receive Device String
416 */
417static int dediprog_command_b(void)
418{
419 int ret;
420 char buf[0x3];
421
422 memset(buf, 0, sizeof(buf));
423 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf, 0x3, DEFAULT_TIMEOUT);
424 if (ret < 0) {
425 msg_perr("Command B failed (%s)!\n", usb_strerror());
426 return 1;
427 }
428 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
429 (buf[2] != 0xff)) {
430 msg_perr("Unexpected response to Command B!\n");
431 return 1;
432 }
433
434 return 0;
435}
436#endif
437
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000438/* Command C is only sent after dediprog_check_devicestring, but not after every
439 * invocation of dediprog_check_devicestring. It is only sent after the first
440 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
441 * I'm tempted to call this one start_SPI_engine or finish_init.
442 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000443static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000444{
445 int ret;
446
447 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT);
448 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000449 msg_perr("Command C failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000450 return 1;
451 }
452 return 0;
453}
454
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000455#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000456/* Very strange. Seems to be a programmer keepalive or somesuch.
457 * Wait unsuccessfully for timeout ms to read one byte.
458 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000459 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000460 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000461static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000462{
463 int ret;
464 char buf[0x1];
465
466 memset(buf, 0, sizeof(buf));
467 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000468 /* This check is most probably wrong. Command F always causes a timeout
469 * in the logs, so we should check for timeout instead of checking for
470 * success.
471 */
472 if (ret != 0x1) {
473 msg_perr("Command F failed (%s)!\n", usb_strerror());
474 return 1;
475 }
476 return 0;
477}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000478#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000479
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000480static int parse_voltage(char *voltage)
481{
482 char *tmp = NULL;
483 int i;
484 int millivolt;
485 int fraction = 0;
486
487 if (!voltage || !strlen(voltage)) {
488 msg_perr("Empty voltage= specified.\n");
489 return -1;
490 }
491 millivolt = (int)strtol(voltage, &tmp, 0);
492 voltage = tmp;
493 /* Handle "," and "." as decimal point. Everything after it is assumed
494 * to be in decimal notation.
495 */
496 if ((*voltage == '.') || (*voltage == ',')) {
497 voltage++;
498 for (i = 0; i < 3; i++) {
499 fraction *= 10;
500 /* Don't advance if the current character is invalid,
501 * but continue multiplying.
502 */
503 if ((*voltage < '0') || (*voltage > '9'))
504 continue;
505 fraction += *voltage - '0';
506 voltage++;
507 }
508 /* Throw away remaining digits. */
509 voltage += strspn(voltage, "0123456789");
510 }
511 /* The remaining string must be empty or "mV" or "V". */
512 tolower_string(voltage);
513
514 /* No unit or "V". */
515 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
516 millivolt *= 1000;
517 millivolt += fraction;
518 } else if (!strncmp(voltage, "mv", 2) ||
519 !strncmp(voltage, "milliv", 6)) {
520 /* No adjustment. fraction is discarded. */
521 } else {
522 /* Garbage at the end of the string. */
523 msg_perr("Garbage voltage= specified.\n");
524 return -1;
525 }
526 return millivolt;
527}
528
Michael Karcherb9dbe482011-05-11 17:07:07 +0000529static const struct spi_programmer spi_programmer_dediprog = {
530 .type = SPI_CONTROLLER_DEDIPROG,
531 .max_data_read = MAX_DATA_UNSPECIFIED,
532 .max_data_write = MAX_DATA_UNSPECIFIED,
533 .command = dediprog_spi_send_command,
534 .multicommand = default_spi_send_multicommand,
535 .read = dediprog_spi_read,
536 .write_256 = dediprog_spi_write_256,
537};
538
David Hendricks8bb20212011-06-14 01:35:36 +0000539static int dediprog_shutdown(void *data)
540{
541 msg_pspew("%s\n", __func__);
542
543 /* URB 28. Command Set SPI Voltage to 0. */
544 if (dediprog_set_spi_voltage(0x0))
545 return 1;
546
547 if (usb_release_interface(dediprog_handle, 0)) {
548 msg_perr("Could not release USB interface!\n");
549 return 1;
550 }
551 if (usb_close(dediprog_handle)) {
552 msg_perr("Could not close USB device!\n");
553 return 1;
554 }
555 return 0;
556}
557
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000558/* URB numbers refer to the first log ever captured. */
559int dediprog_init(void)
560{
561 struct usb_device *dev;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000562 char *voltage;
563 int millivolt = 3500;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000564 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000565
566 msg_pspew("%s\n", __func__);
567
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000568 voltage = extract_programmer_param("voltage");
569 if (voltage) {
570 millivolt = parse_voltage(voltage);
571 free(voltage);
572 if (millivolt < 0) {
573 return 1;
574 }
575 msg_pinfo("Setting voltage to %i mV\n", millivolt);
576 }
577
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000578 /* Here comes the USB stuff. */
579 usb_init();
580 usb_find_busses();
581 usb_find_devices();
582 dev = get_device_by_vid_pid(0x0483, 0xdada);
583 if (!dev) {
584 msg_perr("Could not find a Dediprog SF100 on USB!\n");
585 return 1;
586 }
587 msg_pdbg("Found USB device (%04x:%04x).\n",
588 dev->descriptor.idVendor,
589 dev->descriptor.idProduct);
590 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000591 ret = usb_set_configuration(dediprog_handle, 1);
592 if (ret < 0) {
593 msg_perr("Could not set USB device configuration: %i %s\n",
594 ret, usb_strerror());
595 if (usb_close(dediprog_handle))
596 msg_perr("Could not close USB device!\n");
597 return 1;
598 }
599 ret = usb_claim_interface(dediprog_handle, 0);
600 if (ret < 0) {
601 msg_perr("Could not claim USB device interface %i: %i %s\n",
602 0, ret, usb_strerror());
603 if (usb_close(dediprog_handle))
604 msg_perr("Could not close USB device!\n");
605 return 1;
606 }
607 dediprog_endpoint = 2;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000608
David Hendricks8bb20212011-06-14 01:35:36 +0000609 if (register_shutdown(dediprog_shutdown, NULL))
610 return 1;
611
Stefan Reinauer915b8402011-01-28 09:00:15 +0000612 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
613
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000614 /* URB 6. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000615 if (dediprog_command_a()) {
616 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000617 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000618 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000619 /* URB 7. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000620 if (dediprog_command_a()) {
621 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000622 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000623 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000624 /* URB 8. Command Prepare Receive Device String. */
625 /* URB 9. Command Receive Device String. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000626 if (dediprog_check_devicestring()) {
627 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000628 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000629 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000630 /* URB 10. Command C. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000631 if (dediprog_command_c()) {
632 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000633 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000634 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000635 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000636 if (dediprog_set_spi_voltage(millivolt)) {
637 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000638 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000639 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000640
Michael Karcherb9dbe482011-05-11 17:07:07 +0000641 register_spi_programmer(&spi_programmer_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000642
643 /* RE leftover, leave in until the driver is complete. */
644#if 0
645 /* Execute RDID by hand if you want to test it. */
646 dediprog_do_stuff();
647#endif
648
Stefan Reinauer915b8402011-01-28 09:00:15 +0000649 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
650
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000651 return 0;
652}
653
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000654#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000655/* Leftovers from reverse engineering. Keep for documentation purposes until
656 * completely understood.
657 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000658static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000659{
660 char buf[0x4];
661 /* SPI command processing starts here. */
662
663 /* URB 12. Command Send SPI. */
664 /* URB 13. Command Receive SPI. */
665 memset(buf, 0, sizeof(buf));
666 /* JEDEC RDID */
667 msg_pdbg("Sending RDID\n");
668 buf[0] = JEDEC_RDID;
669 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf))
670 return 1;
671 msg_pdbg("Receiving response: ");
672 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000673 /* URB 14-27 are more SPI commands. */
674 /* URB 28. Command Set SPI Voltage. */
675 if (dediprog_set_spi_voltage(0x0))
676 return 1;
677 /* URB 29-38. Command F, unsuccessful wait. */
678 if (dediprog_command_f(544))
679 return 1;
680 /* URB 39. Command Set SPI Voltage. */
681 if (dediprog_set_spi_voltage(0x10))
682 return 1;
683 /* URB 40. Command Set SPI Speed. */
684 if (dediprog_set_spi_speed(0x2))
685 return 1;
686 /* URB 41 is just URB 28. */
687 /* URB 42,44,46,48,51,53 is just URB 8. */
688 /* URB 43,45,47,49,52,54 is just URB 9. */
689 /* URB 50 is just URB 6/7. */
690 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
691 /* URB 132,134 is just URB 6/7. */
692 /* URB 133 is just URB 29-38. */
693 /* URB 135 is just URB 8. */
694 /* URB 136 is just URB 9. */
695 /* URB 137 is just URB 11. */
696
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000697 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
698 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
699 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
700 * 512 bytes, rest is filled with 0xff.
701 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000702
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000703 return 0;
704}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000705#endif