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