blob: 2d0d060c066a482dd10f71a7762a60ee6ea7cda6 [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>
21#include <stdlib.h>
22#include <ctype.h>
23#include <sys/types.h>
24#include <usb.h>
25#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000026#include "chipdrivers.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000027#include "spi.h"
28
29#define DEFAULT_TIMEOUT 3000
30usb_dev_handle *dediprog_handle;
31
32int dediprog_do_stuff(void);
33
34void print_hex(void *buf, size_t len)
35{
36 size_t i;
37
38 for (i = 0; i < len; i++)
39 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
40}
41
42struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
43{
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
58int dediprog_set_spi_voltage(uint16_t voltage)
59{
60 int ret;
61 unsigned int mv;
62
63 switch (voltage) {
64 case 0x0:
65 /* Admittedly this one is an assumption. */
66 mv = 0;
67 break;
68 case 0x12:
69 mv = 1800;
70 break;
71 case 0x11:
72 mv = 2500;
73 break;
74 case 0x10:
75 mv = 3500;
76 break;
77 default:
78 msg_perr("Unknown voltage selector 0x%x! Aborting.\n", voltage);
79 return 1;
80 }
81 msg_pdbg("Setting SPI voltage to %u.%03u V\n", mv / 1000, mv % 1000);
82
83 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
84 if (ret != 0x0) {
85 msg_perr("Command Set SPI Voltage 0x%x failed!\n", voltage);
86 return 1;
87 }
88 return 0;
89}
90
91/* 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 */
98int dediprog_set_spi_speed(uint16_t speed)
99{
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}
146
147int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
148{
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000149 msg_pspew("%s, start=0x%x, len=0x%x\n", __func__, start, len);
150 /* Chosen read length is 16 bytes for now. */
151 return spi_read_chunked(flash, buf, start, len, 16);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000152}
153
154int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
155 const unsigned char *writearr, unsigned char *readarr)
156{
157 int ret;
158
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000159 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000160 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000161 if (writecnt > 5) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000162 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000163 return 1;
164 }
165 /* 16 byte reads should work. */
166 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000167 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000168 return 1;
169 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000170
171 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT);
172 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000173 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
174 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000175 return 1;
176 }
177 if (!readcnt)
178 return 0;
179 memset(readarr, 0, readcnt);
180 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT);
181 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000182 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
183 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000184 return 1;
185 }
186 return 0;
187}
188
189int dediprog_check_devicestring(void)
190{
191 int ret;
192 char buf[0x11];
193
194 /* Command Prepare Receive Device String. */
195 memset(buf, 0, sizeof(buf));
196 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT);
197 /* The char casting is needed to stop gcc complaining about an always true comparison. */
198 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
199 msg_perr("Unexpected response to Command Prepare Receive Device"
200 " String!\n");
201 return 1;
202 }
203 /* Command Receive Device String. */
204 memset(buf, 0, sizeof(buf));
205 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT);
206 if (ret != 0x10) {
207 msg_perr("Incomplete/failed Command Receive Device String!\n");
208 return 1;
209 }
210 buf[0x10] = '\0';
211 msg_pdbg("Found a %s\n", buf);
212 if (memcmp(buf, "SF100", 0x5)) {
213 msg_perr("Device not a SF100!\n");
214 return 1;
215 }
216 /* Only these versions were tested. */
217 if (memcmp(buf, "SF100 V:2.1.1 ", 0x10) &&
218 memcmp(buf, "SF100 V:3.1.8 ", 0x10)) {
219 msg_perr("Unexpected firmware version!\n");
220 return 1;
221 }
222 return 0;
223}
224
225/* Command A seems to be some sort of device init. It is either followed by
226 * dediprog_check_devicestring (often) or Command A (often) or
227 * Command F (once).
228 */
229int dediprog_command_a(void)
230{
231 int ret;
232 char buf[0x1];
233
234 memset(buf, 0, sizeof(buf));
235 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT);
236 if ((ret != 0x1) || (buf[0] != 0x6f)) {
237 msg_perr("Unexpected response to Command A!\n");
238 return 1;
239 }
240 return 0;
241}
242
243/* Command C is only sent after dediprog_check_devicestring, but not after every
244 * invocation of dediprog_check_devicestring. It is only sent after the first
245 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
246 * I'm tempted to call this one start_SPI_engine or finish_init.
247 */
248int dediprog_command_c(void)
249{
250 int ret;
251
252 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT);
253 if (ret != 0x0) {
254 msg_perr("Unexpected response to Command C!\n");
255 return 1;
256 }
257 return 0;
258}
259
260/* Very strange. Seems to be a programmer keepalive or somesuch.
261 * Wait unsuccessfully for timeout ms to read one byte.
262 * Is usually called after setting voltage to 0.
263 */
264int dediprog_command_f(int timeout)
265{
266 int ret;
267 char buf[0x1];
268
269 memset(buf, 0, sizeof(buf));
270 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout);
271 if (ret != 0x0) {
272 msg_perr("Unexpected response to Command F!\n");
273 return 1;
274 }
275 return 0;
276}
277
278/* URB numbers refer to the first log ever captured. */
279int dediprog_init(void)
280{
281 struct usb_device *dev;
282
283 msg_pspew("%s\n", __func__);
284
285 /* Here comes the USB stuff. */
286 usb_init();
287 usb_find_busses();
288 usb_find_devices();
289 dev = get_device_by_vid_pid(0x0483, 0xdada);
290 if (!dev) {
291 msg_perr("Could not find a Dediprog SF100 on USB!\n");
292 return 1;
293 }
294 msg_pdbg("Found USB device (%04x:%04x).\n",
295 dev->descriptor.idVendor,
296 dev->descriptor.idProduct);
297 dediprog_handle = usb_open(dev);
Patrick Georgi975aa7e2010-02-04 08:29:18 +0000298 usb_set_configuration(dediprog_handle, 1);
299 usb_claim_interface(dediprog_handle, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000300 /* URB 6. Command A. */
301 if (dediprog_command_a())
302 return 1;
303 /* URB 7. Command A. */
304 if (dediprog_command_a())
305 return 1;
306 /* URB 8. Command Prepare Receive Device String. */
307 /* URB 9. Command Receive Device String. */
308 if (dediprog_check_devicestring())
309 return 1;
310 /* URB 10. Command C. */
311 if (dediprog_command_c())
312 return 1;
313 /* URB 11. Command Set SPI Voltage. */
314 if (dediprog_set_spi_voltage(0x10))
315 return 1;
316
317 buses_supported = CHIP_BUSTYPE_SPI;
318 spi_controller = SPI_CONTROLLER_DEDIPROG;
319
320 /* RE leftover, leave in until the driver is complete. */
321#if 0
322 /* Execute RDID by hand if you want to test it. */
323 dediprog_do_stuff();
324#endif
325
326 return 0;
327}
328
329/* Leftovers from reverse engineering. Keep for documentation purposes until
330 * completely understood.
331 */
332int dediprog_do_stuff(void)
333{
334 char buf[0x4];
335 /* SPI command processing starts here. */
336
337 /* URB 12. Command Send SPI. */
338 /* URB 13. Command Receive SPI. */
339 memset(buf, 0, sizeof(buf));
340 /* JEDEC RDID */
341 msg_pdbg("Sending RDID\n");
342 buf[0] = JEDEC_RDID;
343 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf))
344 return 1;
345 msg_pdbg("Receiving response: ");
346 print_hex(buf, JEDEC_RDID_INSIZE);
347#if 0
348 /* URB 14-27 are more SPI commands. */
349 /* URB 28. Command Set SPI Voltage. */
350 if (dediprog_set_spi_voltage(0x0))
351 return 1;
352 /* URB 29-38. Command F, unsuccessful wait. */
353 if (dediprog_command_f(544))
354 return 1;
355 /* URB 39. Command Set SPI Voltage. */
356 if (dediprog_set_spi_voltage(0x10))
357 return 1;
358 /* URB 40. Command Set SPI Speed. */
359 if (dediprog_set_spi_speed(0x2))
360 return 1;
361 /* URB 41 is just URB 28. */
362 /* URB 42,44,46,48,51,53 is just URB 8. */
363 /* URB 43,45,47,49,52,54 is just URB 9. */
364 /* URB 50 is just URB 6/7. */
365 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
366 /* URB 132,134 is just URB 6/7. */
367 /* URB 133 is just URB 29-38. */
368 /* URB 135 is just URB 8. */
369 /* URB 136 is just URB 9. */
370 /* URB 137 is just URB 11. */
371
372 /* Command I is probably Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
373 /* Command J is probably Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
374 /* Bulk transfer sizes for Command I/J are always 512 bytes, rest is filled with 0xff. */
375#endif
376
377 msg_pinfo("All probes will fail because this driver is not hooked up "
378 "to the SPI infrastructure yet.");
379 return 0;
380}
381
382int dediprog_shutdown(void)
383{
384 msg_pspew("%s\n", __func__);
385
386 /* URB 28. Command Set SPI Voltage to 0. */
387 if (dediprog_set_spi_voltage(0x0))
388 return 1;
389
390 if (usb_close(dediprog_handle)) {
391 msg_perr("Couldn't close USB device!\n");
392 return 1;
393 }
394 return 0;
395}