blob: 1889bfe1a28d23fdfbd94466be2bb3dd05d0d61b [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
20#include <string.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000021#include <usb.h>
22#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000023#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000024#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000025#include "spi.h"
26
27#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000028static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000029
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000030#if 0
31/* Might be useful for other pieces of code as well. */
32static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000033{
34 size_t i;
35
36 for (i = 0; i < len; i++)
37 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
38}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000039#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000040
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000041/* Might be useful for other USB devices as well. static for now. */
42static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000043{
44 struct usb_bus *bus;
45 struct usb_device *dev;
46
47 for (bus = usb_get_busses(); bus; bus = bus->next)
48 for (dev = bus->devices; dev; dev = dev->next)
49 if ((dev->descriptor.idVendor == vid) &&
50 (dev->descriptor.idProduct == pid))
51 return dev;
52
53 return NULL;
54}
55
56//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
57
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000058static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000059{
60 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000061 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000062
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000063 switch (millivolt) {
64 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000065 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000066 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000067 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000068 case 1800:
69 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000070 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000071 case 2500:
72 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000073 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000074 case 3500:
75 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000076 break;
77 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000078 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000079 return 1;
80 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000081 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
82 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000083
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000084 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000085 if (ret != 0x0) {
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000086 msg_perr("Command Set SPI Voltage 0x%x failed!\n", voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000087 return 1;
88 }
89 return 0;
90}
91
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000092#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000093/* After dediprog_set_spi_speed, the original app always calls
94 * dediprog_set_spi_voltage(0) and then
95 * dediprog_check_devicestring() four times in a row.
96 * After that, dediprog_command_a() is called.
97 * This looks suspiciously like the microprocessor in the SF100 has to be
98 * restarted/reinitialized in case the speed changes.
99 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000100static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000101{
102 int ret;
103 unsigned int khz;
104
105 /* Case 1 and 2 are in weird order. Probably an organically "grown"
106 * interface.
107 * Base frequency is 24000 kHz, divisors are (in order)
108 * 1, 3, 2, 8, 11, 16, 32, 64.
109 */
110 switch (speed) {
111 case 0x0:
112 khz = 24000;
113 break;
114 case 0x1:
115 khz = 8000;
116 break;
117 case 0x2:
118 khz = 12000;
119 break;
120 case 0x3:
121 khz = 3000;
122 break;
123 case 0x4:
124 khz = 2180;
125 break;
126 case 0x5:
127 khz = 1500;
128 break;
129 case 0x6:
130 khz = 750;
131 break;
132 case 0x7:
133 khz = 375;
134 break;
135 default:
136 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
137 return 1;
138 }
139 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
140
141 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
142 if (ret != 0x0) {
143 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
144 return 1;
145 }
146 return 0;
147}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000148#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000149
150int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
151{
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000152 msg_pspew("%s, start=0x%x, len=0x%x\n", __func__, start, len);
153 /* Chosen read length is 16 bytes for now. */
154 return spi_read_chunked(flash, buf, start, len, 16);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000155}
156
157int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
158 const unsigned char *writearr, unsigned char *readarr)
159{
160 int ret;
161
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000162 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000163 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000164 if (writecnt > 5) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000165 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000166 return 1;
167 }
168 /* 16 byte reads should work. */
169 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000170 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000171 return 1;
172 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000173
174 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT);
175 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000176 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
177 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000178 return 1;
179 }
180 if (!readcnt)
181 return 0;
182 memset(readarr, 0, readcnt);
183 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT);
184 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000185 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
186 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000187 return 1;
188 }
189 return 0;
190}
191
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000192static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000193{
194 int ret;
195 char buf[0x11];
196
197 /* Command Prepare Receive Device String. */
198 memset(buf, 0, sizeof(buf));
199 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT);
200 /* The char casting is needed to stop gcc complaining about an always true comparison. */
201 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
202 msg_perr("Unexpected response to Command Prepare Receive Device"
203 " String!\n");
204 return 1;
205 }
206 /* Command Receive Device String. */
207 memset(buf, 0, sizeof(buf));
208 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT);
209 if (ret != 0x10) {
210 msg_perr("Incomplete/failed Command Receive Device String!\n");
211 return 1;
212 }
213 buf[0x10] = '\0';
214 msg_pdbg("Found a %s\n", buf);
215 if (memcmp(buf, "SF100", 0x5)) {
216 msg_perr("Device not a SF100!\n");
217 return 1;
218 }
219 /* Only these versions were tested. */
220 if (memcmp(buf, "SF100 V:2.1.1 ", 0x10) &&
221 memcmp(buf, "SF100 V:3.1.8 ", 0x10)) {
222 msg_perr("Unexpected firmware version!\n");
223 return 1;
224 }
225 return 0;
226}
227
228/* Command A seems to be some sort of device init. It is either followed by
229 * dediprog_check_devicestring (often) or Command A (often) or
230 * Command F (once).
231 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000232static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000233{
234 int ret;
235 char buf[0x1];
236
237 memset(buf, 0, sizeof(buf));
238 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT);
239 if ((ret != 0x1) || (buf[0] != 0x6f)) {
240 msg_perr("Unexpected response to Command A!\n");
241 return 1;
242 }
243 return 0;
244}
245
246/* Command C is only sent after dediprog_check_devicestring, but not after every
247 * invocation of dediprog_check_devicestring. It is only sent after the first
248 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
249 * I'm tempted to call this one start_SPI_engine or finish_init.
250 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000251static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000252{
253 int ret;
254
255 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT);
256 if (ret != 0x0) {
257 msg_perr("Unexpected response to Command C!\n");
258 return 1;
259 }
260 return 0;
261}
262
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000263#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000264/* Very strange. Seems to be a programmer keepalive or somesuch.
265 * Wait unsuccessfully for timeout ms to read one byte.
266 * Is usually called after setting voltage to 0.
267 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000268static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000269{
270 int ret;
271 char buf[0x1];
272
273 memset(buf, 0, sizeof(buf));
274 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout);
275 if (ret != 0x0) {
276 msg_perr("Unexpected response to Command F!\n");
277 return 1;
278 }
279 return 0;
280}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000281#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000282
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000283static int parse_voltage(char *voltage)
284{
285 char *tmp = NULL;
286 int i;
287 int millivolt;
288 int fraction = 0;
289
290 if (!voltage || !strlen(voltage)) {
291 msg_perr("Empty voltage= specified.\n");
292 return -1;
293 }
294 millivolt = (int)strtol(voltage, &tmp, 0);
295 voltage = tmp;
296 /* Handle "," and "." as decimal point. Everything after it is assumed
297 * to be in decimal notation.
298 */
299 if ((*voltage == '.') || (*voltage == ',')) {
300 voltage++;
301 for (i = 0; i < 3; i++) {
302 fraction *= 10;
303 /* Don't advance if the current character is invalid,
304 * but continue multiplying.
305 */
306 if ((*voltage < '0') || (*voltage > '9'))
307 continue;
308 fraction += *voltage - '0';
309 voltage++;
310 }
311 /* Throw away remaining digits. */
312 voltage += strspn(voltage, "0123456789");
313 }
314 /* The remaining string must be empty or "mV" or "V". */
315 tolower_string(voltage);
316
317 /* No unit or "V". */
318 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
319 millivolt *= 1000;
320 millivolt += fraction;
321 } else if (!strncmp(voltage, "mv", 2) ||
322 !strncmp(voltage, "milliv", 6)) {
323 /* No adjustment. fraction is discarded. */
324 } else {
325 /* Garbage at the end of the string. */
326 msg_perr("Garbage voltage= specified.\n");
327 return -1;
328 }
329 return millivolt;
330}
331
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000332/* URB numbers refer to the first log ever captured. */
333int dediprog_init(void)
334{
335 struct usb_device *dev;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000336 char *voltage;
337 int millivolt = 3500;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000338
339 msg_pspew("%s\n", __func__);
340
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000341 voltage = extract_programmer_param("voltage");
342 if (voltage) {
343 millivolt = parse_voltage(voltage);
344 free(voltage);
345 if (millivolt < 0) {
346 return 1;
347 }
348 msg_pinfo("Setting voltage to %i mV\n", millivolt);
349 }
350
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000351 /* Here comes the USB stuff. */
352 usb_init();
353 usb_find_busses();
354 usb_find_devices();
355 dev = get_device_by_vid_pid(0x0483, 0xdada);
356 if (!dev) {
357 msg_perr("Could not find a Dediprog SF100 on USB!\n");
358 return 1;
359 }
360 msg_pdbg("Found USB device (%04x:%04x).\n",
361 dev->descriptor.idVendor,
362 dev->descriptor.idProduct);
363 dediprog_handle = usb_open(dev);
Patrick Georgi975aa7e2010-02-04 08:29:18 +0000364 usb_set_configuration(dediprog_handle, 1);
365 usb_claim_interface(dediprog_handle, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000366 /* URB 6. Command A. */
367 if (dediprog_command_a())
368 return 1;
369 /* URB 7. Command A. */
370 if (dediprog_command_a())
371 return 1;
372 /* URB 8. Command Prepare Receive Device String. */
373 /* URB 9. Command Receive Device String. */
374 if (dediprog_check_devicestring())
375 return 1;
376 /* URB 10. Command C. */
377 if (dediprog_command_c())
378 return 1;
379 /* URB 11. Command Set SPI Voltage. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000380 if (dediprog_set_spi_voltage(millivolt))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000381 return 1;
382
383 buses_supported = CHIP_BUSTYPE_SPI;
384 spi_controller = SPI_CONTROLLER_DEDIPROG;
385
386 /* RE leftover, leave in until the driver is complete. */
387#if 0
388 /* Execute RDID by hand if you want to test it. */
389 dediprog_do_stuff();
390#endif
391
392 return 0;
393}
394
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000395#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000396/* Leftovers from reverse engineering. Keep for documentation purposes until
397 * completely understood.
398 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000399static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000400{
401 char buf[0x4];
402 /* SPI command processing starts here. */
403
404 /* URB 12. Command Send SPI. */
405 /* URB 13. Command Receive SPI. */
406 memset(buf, 0, sizeof(buf));
407 /* JEDEC RDID */
408 msg_pdbg("Sending RDID\n");
409 buf[0] = JEDEC_RDID;
410 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf))
411 return 1;
412 msg_pdbg("Receiving response: ");
413 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000414 /* URB 14-27 are more SPI commands. */
415 /* URB 28. Command Set SPI Voltage. */
416 if (dediprog_set_spi_voltage(0x0))
417 return 1;
418 /* URB 29-38. Command F, unsuccessful wait. */
419 if (dediprog_command_f(544))
420 return 1;
421 /* URB 39. Command Set SPI Voltage. */
422 if (dediprog_set_spi_voltage(0x10))
423 return 1;
424 /* URB 40. Command Set SPI Speed. */
425 if (dediprog_set_spi_speed(0x2))
426 return 1;
427 /* URB 41 is just URB 28. */
428 /* URB 42,44,46,48,51,53 is just URB 8. */
429 /* URB 43,45,47,49,52,54 is just URB 9. */
430 /* URB 50 is just URB 6/7. */
431 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
432 /* URB 132,134 is just URB 6/7. */
433 /* URB 133 is just URB 29-38. */
434 /* URB 135 is just URB 8. */
435 /* URB 136 is just URB 9. */
436 /* URB 137 is just URB 11. */
437
438 /* Command I is probably Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
439 /* Command J is probably Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
440 /* Bulk transfer sizes for Command I/J are always 512 bytes, rest is filled with 0xff. */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000441
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000442 return 0;
443}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000444#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000445
446int dediprog_shutdown(void)
447{
448 msg_pspew("%s\n", __func__);
449
450 /* URB 28. Command Set SPI Voltage to 0. */
451 if (dediprog_set_spi_voltage(0x0))
452 return 1;
453
454 if (usb_close(dediprog_handle)) {
455 msg_perr("Couldn't close USB device!\n");
456 return 1;
457 }
458 return 0;
459}