blob: a6db17c1309f40a2b3c801c881cafb12f5fa1c3c [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
28#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000029static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000030static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000031
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000032#if 0
33/* Might be useful for other pieces of code as well. */
34static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000035{
36 size_t i;
37
38 for (i = 0; i < len; i++)
39 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
40}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000041#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000042
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000043/* Might be useful for other USB devices as well. static for now. */
44static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000045{
46 struct usb_bus *bus;
47 struct usb_device *dev;
48
49 for (bus = usb_get_busses(); bus; bus = bus->next)
50 for (dev = bus->devices; dev; dev = dev->next)
51 if ((dev->descriptor.idVendor == vid) &&
52 (dev->descriptor.idProduct == pid))
53 return dev;
54
55 return NULL;
56}
57
58//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
59
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000060static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000061{
62 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000063 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000064
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000065 switch (millivolt) {
66 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000067 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000068 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000069 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000070 case 1800:
71 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000072 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000073 case 2500:
74 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000075 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000076 case 3500:
77 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000078 break;
79 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000080 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000081 return 1;
82 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000083 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
84 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000085
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000086 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000087 if (ret != 0x0) {
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000088 msg_perr("Command Set SPI Voltage 0x%x failed!\n", voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000089 return 1;
90 }
91 return 0;
92}
93
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000094#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000095/* After dediprog_set_spi_speed, the original app always calls
96 * dediprog_set_spi_voltage(0) and then
97 * dediprog_check_devicestring() four times in a row.
98 * After that, dediprog_command_a() is called.
99 * This looks suspiciously like the microprocessor in the SF100 has to be
100 * restarted/reinitialized in case the speed changes.
101 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000102static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000103{
104 int ret;
105 unsigned int khz;
106
107 /* Case 1 and 2 are in weird order. Probably an organically "grown"
108 * interface.
109 * Base frequency is 24000 kHz, divisors are (in order)
110 * 1, 3, 2, 8, 11, 16, 32, 64.
111 */
112 switch (speed) {
113 case 0x0:
114 khz = 24000;
115 break;
116 case 0x1:
117 khz = 8000;
118 break;
119 case 0x2:
120 khz = 12000;
121 break;
122 case 0x3:
123 khz = 3000;
124 break;
125 case 0x4:
126 khz = 2180;
127 break;
128 case 0x5:
129 khz = 1500;
130 break;
131 case 0x6:
132 khz = 750;
133 break;
134 case 0x7:
135 khz = 375;
136 break;
137 default:
138 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
139 return 1;
140 }
141 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
142
143 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
144 if (ret != 0x0) {
145 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
146 return 1;
147 }
148 return 0;
149}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000150#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000151
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000152/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
153 * @start start address
154 * @len length
155 * @return 0 on success, 1 on failure
156 */
157static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf,
158 int start, int len)
159{
160 int ret;
161 int i;
162 /* chunksize must be 512, other sizes will NOT work at all. */
163 const int chunksize = 0x200;
164 const int count = len / chunksize;
165 const char count_and_chunk[] = {count & 0xff,
166 (count >> 8) & 0xff,
167 chunksize & 0xff,
168 (chunksize >> 8) & 0xff};
169
170 if ((start % chunksize) || (len % chunksize)) {
171 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
172 "at flashrom@flashrom.org\n", __func__, start, len);
173 return 1;
174 }
175
176 /* No idea if the hardware can handle empty reads, so chicken out. */
177 if (!len)
178 return 0;
179 /* Command Read SPI Bulk. No idea which read command is used on the
180 * SPI side.
181 */
182 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
183 start / 0x10000, (char *)count_and_chunk,
184 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
185 if (ret != sizeof(count_and_chunk)) {
186 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
187 usb_strerror());
188 return 1;
189 }
190
191 for (i = 0; i < count; i++) {
192 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
193 (char *)buf + i * chunksize, chunksize,
194 DEFAULT_TIMEOUT);
195 if (ret != chunksize) {
196 msg_perr("SPI bulk read %i failed, expected %i, got %i "
197 "%s!\n", i, chunksize, ret, usb_strerror());
198 return 1;
199 }
200 }
201
202 return 0;
203}
204
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000205int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
206{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000207 int ret;
208 /* chunksize must be 512, other sizes will NOT work at all. */
209 const int chunksize = 0x200;
210 int residue = start % chunksize ? chunksize - start % chunksize : 0;
211 int bulklen;
212
213 if (residue) {
214 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
215 start, residue);
216 ret = spi_read_chunked(flash, buf, start, residue, 16);
217 if (ret)
218 return ret;
219 }
220
221 /* Round down. */
222 bulklen = (len - residue) / chunksize * chunksize;
223 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
224 bulklen);
225 if (ret)
226 return ret;
227
228 len -= residue + bulklen;
229 if (len) {
230 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
231 start, len);
232 ret = spi_read_chunked(flash, buf + residue + bulklen,
233 start + residue + bulklen, len, 16);
234 if (ret)
235 return ret;
236 }
237
238 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000239}
240
241int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
242 const unsigned char *writearr, unsigned char *readarr)
243{
244 int ret;
245
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000246 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000247 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000248 if (writecnt > 5) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000249 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000250 return 1;
251 }
252 /* 16 byte reads should work. */
253 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000254 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000255 return 1;
256 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000257
258 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT);
259 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000260 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
261 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000262 return 1;
263 }
264 if (!readcnt)
265 return 0;
266 memset(readarr, 0, readcnt);
267 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT);
268 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000269 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
270 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000271 return 1;
272 }
273 return 0;
274}
275
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000276static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000277{
278 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000279 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000280 char buf[0x11];
281
282 /* Command Prepare Receive Device String. */
283 memset(buf, 0, sizeof(buf));
284 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT);
285 /* The char casting is needed to stop gcc complaining about an always true comparison. */
286 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
287 msg_perr("Unexpected response to Command Prepare Receive Device"
288 " String!\n");
289 return 1;
290 }
291 /* Command Receive Device String. */
292 memset(buf, 0, sizeof(buf));
293 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT);
294 if (ret != 0x10) {
295 msg_perr("Incomplete/failed Command Receive Device String!\n");
296 return 1;
297 }
298 buf[0x10] = '\0';
299 msg_pdbg("Found a %s\n", buf);
300 if (memcmp(buf, "SF100", 0x5)) {
301 msg_perr("Device not a SF100!\n");
302 return 1;
303 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000304 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
305 msg_perr("Unexpected firmware version string!\n");
306 return 1;
307 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000308 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000309 if (fw[0] < 2 || fw[0] > 5) {
310 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
311 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000312 return 1;
313 }
314 return 0;
315}
316
317/* Command A seems to be some sort of device init. It is either followed by
318 * dediprog_check_devicestring (often) or Command A (often) or
319 * Command F (once).
320 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000321static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000322{
323 int ret;
324 char buf[0x1];
325
326 memset(buf, 0, sizeof(buf));
327 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000328 if (ret < 0) {
329 msg_perr("Command A failed (%s)!\n", usb_strerror());
330 return 1;
331 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000332 if ((ret != 0x1) || (buf[0] != 0x6f)) {
333 msg_perr("Unexpected response to Command A!\n");
334 return 1;
335 }
336 return 0;
337}
338
339/* Command C is only sent after dediprog_check_devicestring, but not after every
340 * invocation of dediprog_check_devicestring. It is only sent after the first
341 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
342 * I'm tempted to call this one start_SPI_engine or finish_init.
343 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000344static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000345{
346 int ret;
347
348 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT);
349 if (ret != 0x0) {
350 msg_perr("Unexpected response to Command C!\n");
351 return 1;
352 }
353 return 0;
354}
355
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000356#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000357/* Very strange. Seems to be a programmer keepalive or somesuch.
358 * Wait unsuccessfully for timeout ms to read one byte.
359 * Is usually called after setting voltage to 0.
360 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000361static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000362{
363 int ret;
364 char buf[0x1];
365
366 memset(buf, 0, sizeof(buf));
367 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout);
368 if (ret != 0x0) {
369 msg_perr("Unexpected response to Command F!\n");
370 return 1;
371 }
372 return 0;
373}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000374#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000375
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000376static int parse_voltage(char *voltage)
377{
378 char *tmp = NULL;
379 int i;
380 int millivolt;
381 int fraction = 0;
382
383 if (!voltage || !strlen(voltage)) {
384 msg_perr("Empty voltage= specified.\n");
385 return -1;
386 }
387 millivolt = (int)strtol(voltage, &tmp, 0);
388 voltage = tmp;
389 /* Handle "," and "." as decimal point. Everything after it is assumed
390 * to be in decimal notation.
391 */
392 if ((*voltage == '.') || (*voltage == ',')) {
393 voltage++;
394 for (i = 0; i < 3; i++) {
395 fraction *= 10;
396 /* Don't advance if the current character is invalid,
397 * but continue multiplying.
398 */
399 if ((*voltage < '0') || (*voltage > '9'))
400 continue;
401 fraction += *voltage - '0';
402 voltage++;
403 }
404 /* Throw away remaining digits. */
405 voltage += strspn(voltage, "0123456789");
406 }
407 /* The remaining string must be empty or "mV" or "V". */
408 tolower_string(voltage);
409
410 /* No unit or "V". */
411 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
412 millivolt *= 1000;
413 millivolt += fraction;
414 } else if (!strncmp(voltage, "mv", 2) ||
415 !strncmp(voltage, "milliv", 6)) {
416 /* No adjustment. fraction is discarded. */
417 } else {
418 /* Garbage at the end of the string. */
419 msg_perr("Garbage voltage= specified.\n");
420 return -1;
421 }
422 return millivolt;
423}
424
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000425/* URB numbers refer to the first log ever captured. */
426int dediprog_init(void)
427{
428 struct usb_device *dev;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000429 char *voltage;
430 int millivolt = 3500;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000431 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000432
433 msg_pspew("%s\n", __func__);
434
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000435 voltage = extract_programmer_param("voltage");
436 if (voltage) {
437 millivolt = parse_voltage(voltage);
438 free(voltage);
439 if (millivolt < 0) {
440 return 1;
441 }
442 msg_pinfo("Setting voltage to %i mV\n", millivolt);
443 }
444
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000445 /* Here comes the USB stuff. */
446 usb_init();
447 usb_find_busses();
448 usb_find_devices();
449 dev = get_device_by_vid_pid(0x0483, 0xdada);
450 if (!dev) {
451 msg_perr("Could not find a Dediprog SF100 on USB!\n");
452 return 1;
453 }
454 msg_pdbg("Found USB device (%04x:%04x).\n",
455 dev->descriptor.idVendor,
456 dev->descriptor.idProduct);
457 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000458 ret = usb_set_configuration(dediprog_handle, 1);
459 if (ret < 0) {
460 msg_perr("Could not set USB device configuration: %i %s\n",
461 ret, usb_strerror());
462 if (usb_close(dediprog_handle))
463 msg_perr("Could not close USB device!\n");
464 return 1;
465 }
466 ret = usb_claim_interface(dediprog_handle, 0);
467 if (ret < 0) {
468 msg_perr("Could not claim USB device interface %i: %i %s\n",
469 0, ret, usb_strerror());
470 if (usb_close(dediprog_handle))
471 msg_perr("Could not close USB device!\n");
472 return 1;
473 }
474 dediprog_endpoint = 2;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000475 /* URB 6. Command A. */
476 if (dediprog_command_a())
477 return 1;
478 /* URB 7. Command A. */
479 if (dediprog_command_a())
480 return 1;
481 /* URB 8. Command Prepare Receive Device String. */
482 /* URB 9. Command Receive Device String. */
483 if (dediprog_check_devicestring())
484 return 1;
485 /* URB 10. Command C. */
486 if (dediprog_command_c())
487 return 1;
488 /* URB 11. Command Set SPI Voltage. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000489 if (dediprog_set_spi_voltage(millivolt))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000490 return 1;
491
492 buses_supported = CHIP_BUSTYPE_SPI;
493 spi_controller = SPI_CONTROLLER_DEDIPROG;
494
495 /* RE leftover, leave in until the driver is complete. */
496#if 0
497 /* Execute RDID by hand if you want to test it. */
498 dediprog_do_stuff();
499#endif
500
501 return 0;
502}
503
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000504#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000505/* Leftovers from reverse engineering. Keep for documentation purposes until
506 * completely understood.
507 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000508static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000509{
510 char buf[0x4];
511 /* SPI command processing starts here. */
512
513 /* URB 12. Command Send SPI. */
514 /* URB 13. Command Receive SPI. */
515 memset(buf, 0, sizeof(buf));
516 /* JEDEC RDID */
517 msg_pdbg("Sending RDID\n");
518 buf[0] = JEDEC_RDID;
519 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf))
520 return 1;
521 msg_pdbg("Receiving response: ");
522 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000523 /* URB 14-27 are more SPI commands. */
524 /* URB 28. Command Set SPI Voltage. */
525 if (dediprog_set_spi_voltage(0x0))
526 return 1;
527 /* URB 29-38. Command F, unsuccessful wait. */
528 if (dediprog_command_f(544))
529 return 1;
530 /* URB 39. Command Set SPI Voltage. */
531 if (dediprog_set_spi_voltage(0x10))
532 return 1;
533 /* URB 40. Command Set SPI Speed. */
534 if (dediprog_set_spi_speed(0x2))
535 return 1;
536 /* URB 41 is just URB 28. */
537 /* URB 42,44,46,48,51,53 is just URB 8. */
538 /* URB 43,45,47,49,52,54 is just URB 9. */
539 /* URB 50 is just URB 6/7. */
540 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
541 /* URB 132,134 is just URB 6/7. */
542 /* URB 133 is just URB 29-38. */
543 /* URB 135 is just URB 8. */
544 /* URB 136 is just URB 9. */
545 /* URB 137 is just URB 11. */
546
547 /* Command I is probably Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
548 /* Command J is probably Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
549 /* Bulk transfer sizes for Command I/J are always 512 bytes, rest is filled with 0xff. */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000550
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000551 return 0;
552}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000553#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000554
555int dediprog_shutdown(void)
556{
557 msg_pspew("%s\n", __func__);
558
559 /* URB 28. Command Set SPI Voltage to 0. */
560 if (dediprog_set_spi_voltage(0x0))
561 return 1;
562
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000563 if (usb_release_interface(dediprog_handle, 0)) {
564 msg_perr("Could not release USB interface!\n");
565 return 1;
566 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000567 if (usb_close(dediprog_handle)) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000568 msg_perr("Could not close USB device!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000569 return 1;
570 }
571 return 0;
572}