blob: 0b92983886738427bc4ad60d9b06dc432c661578 [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 Hailfingerd38fac82010-01-19 11:15:48 +000024#include "spi.h"
25
26#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000027static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000028
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000029#if 0
30/* Might be useful for other pieces of code as well. */
31static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000032{
33 size_t i;
34
35 for (i = 0; i < len; i++)
36 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
37}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000038#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000039
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000040/* Might be useful for other USB devices as well. static for now. */
41static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000042{
43 struct usb_bus *bus;
44 struct usb_device *dev;
45
46 for (bus = usb_get_busses(); bus; bus = bus->next)
47 for (dev = bus->devices; dev; dev = dev->next)
48 if ((dev->descriptor.idVendor == vid) &&
49 (dev->descriptor.idProduct == pid))
50 return dev;
51
52 return NULL;
53}
54
55//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
56
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000057static int dediprog_set_spi_voltage(uint16_t voltage)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000058{
59 int ret;
60 unsigned int mv;
61
62 switch (voltage) {
63 case 0x0:
64 /* Admittedly this one is an assumption. */
65 mv = 0;
66 break;
67 case 0x12:
68 mv = 1800;
69 break;
70 case 0x11:
71 mv = 2500;
72 break;
73 case 0x10:
74 mv = 3500;
75 break;
76 default:
77 msg_perr("Unknown voltage selector 0x%x! Aborting.\n", voltage);
78 return 1;
79 }
80 msg_pdbg("Setting SPI voltage to %u.%03u V\n", mv / 1000, mv % 1000);
81
82 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
83 if (ret != 0x0) {
84 msg_perr("Command Set SPI Voltage 0x%x failed!\n", voltage);
85 return 1;
86 }
87 return 0;
88}
89
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000090#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000091/* After dediprog_set_spi_speed, the original app always calls
92 * dediprog_set_spi_voltage(0) and then
93 * dediprog_check_devicestring() four times in a row.
94 * After that, dediprog_command_a() is called.
95 * This looks suspiciously like the microprocessor in the SF100 has to be
96 * restarted/reinitialized in case the speed changes.
97 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000098static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000099{
100 int ret;
101 unsigned int khz;
102
103 /* Case 1 and 2 are in weird order. Probably an organically "grown"
104 * interface.
105 * Base frequency is 24000 kHz, divisors are (in order)
106 * 1, 3, 2, 8, 11, 16, 32, 64.
107 */
108 switch (speed) {
109 case 0x0:
110 khz = 24000;
111 break;
112 case 0x1:
113 khz = 8000;
114 break;
115 case 0x2:
116 khz = 12000;
117 break;
118 case 0x3:
119 khz = 3000;
120 break;
121 case 0x4:
122 khz = 2180;
123 break;
124 case 0x5:
125 khz = 1500;
126 break;
127 case 0x6:
128 khz = 750;
129 break;
130 case 0x7:
131 khz = 375;
132 break;
133 default:
134 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
135 return 1;
136 }
137 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
138
139 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
140 if (ret != 0x0) {
141 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
142 return 1;
143 }
144 return 0;
145}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000146#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000147
148int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
149{
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000150 msg_pspew("%s, start=0x%x, len=0x%x\n", __func__, start, len);
151 /* Chosen read length is 16 bytes for now. */
152 return spi_read_chunked(flash, buf, start, len, 16);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000153}
154
155int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
156 const unsigned char *writearr, unsigned char *readarr)
157{
158 int ret;
159
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000160 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000161 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000162 if (writecnt > 5) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000163 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000164 return 1;
165 }
166 /* 16 byte reads should work. */
167 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000168 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000169 return 1;
170 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000171
172 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT);
173 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000174 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
175 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000176 return 1;
177 }
178 if (!readcnt)
179 return 0;
180 memset(readarr, 0, readcnt);
181 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT);
182 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000183 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
184 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000185 return 1;
186 }
187 return 0;
188}
189
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000190static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000191{
192 int ret;
193 char buf[0x11];
194
195 /* Command Prepare Receive Device String. */
196 memset(buf, 0, sizeof(buf));
197 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT);
198 /* The char casting is needed to stop gcc complaining about an always true comparison. */
199 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
200 msg_perr("Unexpected response to Command Prepare Receive Device"
201 " String!\n");
202 return 1;
203 }
204 /* Command Receive Device String. */
205 memset(buf, 0, sizeof(buf));
206 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT);
207 if (ret != 0x10) {
208 msg_perr("Incomplete/failed Command Receive Device String!\n");
209 return 1;
210 }
211 buf[0x10] = '\0';
212 msg_pdbg("Found a %s\n", buf);
213 if (memcmp(buf, "SF100", 0x5)) {
214 msg_perr("Device not a SF100!\n");
215 return 1;
216 }
217 /* Only these versions were tested. */
218 if (memcmp(buf, "SF100 V:2.1.1 ", 0x10) &&
219 memcmp(buf, "SF100 V:3.1.8 ", 0x10)) {
220 msg_perr("Unexpected firmware version!\n");
221 return 1;
222 }
223 return 0;
224}
225
226/* Command A seems to be some sort of device init. It is either followed by
227 * dediprog_check_devicestring (often) or Command A (often) or
228 * Command F (once).
229 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000230static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000231{
232 int ret;
233 char buf[0x1];
234
235 memset(buf, 0, sizeof(buf));
236 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT);
237 if ((ret != 0x1) || (buf[0] != 0x6f)) {
238 msg_perr("Unexpected response to Command A!\n");
239 return 1;
240 }
241 return 0;
242}
243
244/* Command C is only sent after dediprog_check_devicestring, but not after every
245 * invocation of dediprog_check_devicestring. It is only sent after the first
246 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
247 * I'm tempted to call this one start_SPI_engine or finish_init.
248 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000249static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000250{
251 int ret;
252
253 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT);
254 if (ret != 0x0) {
255 msg_perr("Unexpected response to Command C!\n");
256 return 1;
257 }
258 return 0;
259}
260
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000261#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000262/* Very strange. Seems to be a programmer keepalive or somesuch.
263 * Wait unsuccessfully for timeout ms to read one byte.
264 * Is usually called after setting voltage to 0.
265 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000266static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000267{
268 int ret;
269 char buf[0x1];
270
271 memset(buf, 0, sizeof(buf));
272 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout);
273 if (ret != 0x0) {
274 msg_perr("Unexpected response to Command F!\n");
275 return 1;
276 }
277 return 0;
278}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000279#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000280
281/* URB numbers refer to the first log ever captured. */
282int dediprog_init(void)
283{
284 struct usb_device *dev;
285
286 msg_pspew("%s\n", __func__);
287
288 /* Here comes the USB stuff. */
289 usb_init();
290 usb_find_busses();
291 usb_find_devices();
292 dev = get_device_by_vid_pid(0x0483, 0xdada);
293 if (!dev) {
294 msg_perr("Could not find a Dediprog SF100 on USB!\n");
295 return 1;
296 }
297 msg_pdbg("Found USB device (%04x:%04x).\n",
298 dev->descriptor.idVendor,
299 dev->descriptor.idProduct);
300 dediprog_handle = usb_open(dev);
Patrick Georgi975aa7e2010-02-04 08:29:18 +0000301 usb_set_configuration(dediprog_handle, 1);
302 usb_claim_interface(dediprog_handle, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000303 /* URB 6. Command A. */
304 if (dediprog_command_a())
305 return 1;
306 /* URB 7. Command A. */
307 if (dediprog_command_a())
308 return 1;
309 /* URB 8. Command Prepare Receive Device String. */
310 /* URB 9. Command Receive Device String. */
311 if (dediprog_check_devicestring())
312 return 1;
313 /* URB 10. Command C. */
314 if (dediprog_command_c())
315 return 1;
316 /* URB 11. Command Set SPI Voltage. */
317 if (dediprog_set_spi_voltage(0x10))
318 return 1;
319
320 buses_supported = CHIP_BUSTYPE_SPI;
321 spi_controller = SPI_CONTROLLER_DEDIPROG;
322
323 /* RE leftover, leave in until the driver is complete. */
324#if 0
325 /* Execute RDID by hand if you want to test it. */
326 dediprog_do_stuff();
327#endif
328
329 return 0;
330}
331
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000332#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000333/* Leftovers from reverse engineering. Keep for documentation purposes until
334 * completely understood.
335 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000336static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000337{
338 char buf[0x4];
339 /* SPI command processing starts here. */
340
341 /* URB 12. Command Send SPI. */
342 /* URB 13. Command Receive SPI. */
343 memset(buf, 0, sizeof(buf));
344 /* JEDEC RDID */
345 msg_pdbg("Sending RDID\n");
346 buf[0] = JEDEC_RDID;
347 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf))
348 return 1;
349 msg_pdbg("Receiving response: ");
350 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000351 /* URB 14-27 are more SPI commands. */
352 /* URB 28. Command Set SPI Voltage. */
353 if (dediprog_set_spi_voltage(0x0))
354 return 1;
355 /* URB 29-38. Command F, unsuccessful wait. */
356 if (dediprog_command_f(544))
357 return 1;
358 /* URB 39. Command Set SPI Voltage. */
359 if (dediprog_set_spi_voltage(0x10))
360 return 1;
361 /* URB 40. Command Set SPI Speed. */
362 if (dediprog_set_spi_speed(0x2))
363 return 1;
364 /* URB 41 is just URB 28. */
365 /* URB 42,44,46,48,51,53 is just URB 8. */
366 /* URB 43,45,47,49,52,54 is just URB 9. */
367 /* URB 50 is just URB 6/7. */
368 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
369 /* URB 132,134 is just URB 6/7. */
370 /* URB 133 is just URB 29-38. */
371 /* URB 135 is just URB 8. */
372 /* URB 136 is just URB 9. */
373 /* URB 137 is just URB 11. */
374
375 /* Command I is probably Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
376 /* Command J is probably Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
377 /* Bulk transfer sizes for Command I/J are always 512 bytes, rest is filled with 0xff. */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000378
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000379 return 0;
380}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000381#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000382
383int dediprog_shutdown(void)
384{
385 msg_pspew("%s\n", __func__);
386
387 /* URB 28. Command Set SPI Voltage to 0. */
388 if (dediprog_set_spi_voltage(0x0))
389 return 1;
390
391 if (usb_close(dediprog_handle)) {
392 msg_perr("Couldn't close USB device!\n");
393 return 1;
394 }
395 return 0;
396}