blob: cf4bd645e230877c515e2b1d01ab9f76a59852ac [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 Hailfingerad3cc552010-07-03 11:02:10 +000058static int dediprog_set_spi_voltage(uint16_t voltage)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000059{
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
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000091#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000092/* After dediprog_set_spi_speed, the original app always calls
93 * dediprog_set_spi_voltage(0) and then
94 * dediprog_check_devicestring() four times in a row.
95 * After that, dediprog_command_a() is called.
96 * This looks suspiciously like the microprocessor in the SF100 has to be
97 * restarted/reinitialized in case the speed changes.
98 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000099static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000100{
101 int ret;
102 unsigned int khz;
103
104 /* Case 1 and 2 are in weird order. Probably an organically "grown"
105 * interface.
106 * Base frequency is 24000 kHz, divisors are (in order)
107 * 1, 3, 2, 8, 11, 16, 32, 64.
108 */
109 switch (speed) {
110 case 0x0:
111 khz = 24000;
112 break;
113 case 0x1:
114 khz = 8000;
115 break;
116 case 0x2:
117 khz = 12000;
118 break;
119 case 0x3:
120 khz = 3000;
121 break;
122 case 0x4:
123 khz = 2180;
124 break;
125 case 0x5:
126 khz = 1500;
127 break;
128 case 0x6:
129 khz = 750;
130 break;
131 case 0x7:
132 khz = 375;
133 break;
134 default:
135 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
136 return 1;
137 }
138 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
139
140 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
141 if (ret != 0x0) {
142 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
143 return 1;
144 }
145 return 0;
146}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000147#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000148
149int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
150{
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000151 msg_pspew("%s, start=0x%x, len=0x%x\n", __func__, start, len);
152 /* Chosen read length is 16 bytes for now. */
153 return spi_read_chunked(flash, buf, start, len, 16);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000154}
155
156int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
157 const unsigned char *writearr, unsigned char *readarr)
158{
159 int ret;
160
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000161 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000162 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000163 if (writecnt > 5) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000164 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000165 return 1;
166 }
167 /* 16 byte reads should work. */
168 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000169 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000170 return 1;
171 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000172
173 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT);
174 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000175 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
176 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000177 return 1;
178 }
179 if (!readcnt)
180 return 0;
181 memset(readarr, 0, readcnt);
182 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT);
183 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000184 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
185 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000186 return 1;
187 }
188 return 0;
189}
190
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000191static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000192{
193 int ret;
194 char buf[0x11];
195
196 /* Command Prepare Receive Device String. */
197 memset(buf, 0, sizeof(buf));
198 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT);
199 /* The char casting is needed to stop gcc complaining about an always true comparison. */
200 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
201 msg_perr("Unexpected response to Command Prepare Receive Device"
202 " String!\n");
203 return 1;
204 }
205 /* Command Receive Device String. */
206 memset(buf, 0, sizeof(buf));
207 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT);
208 if (ret != 0x10) {
209 msg_perr("Incomplete/failed Command Receive Device String!\n");
210 return 1;
211 }
212 buf[0x10] = '\0';
213 msg_pdbg("Found a %s\n", buf);
214 if (memcmp(buf, "SF100", 0x5)) {
215 msg_perr("Device not a SF100!\n");
216 return 1;
217 }
218 /* Only these versions were tested. */
219 if (memcmp(buf, "SF100 V:2.1.1 ", 0x10) &&
220 memcmp(buf, "SF100 V:3.1.8 ", 0x10)) {
221 msg_perr("Unexpected firmware version!\n");
222 return 1;
223 }
224 return 0;
225}
226
227/* Command A seems to be some sort of device init. It is either followed by
228 * dediprog_check_devicestring (often) or Command A (often) or
229 * Command F (once).
230 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000231static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000232{
233 int ret;
234 char buf[0x1];
235
236 memset(buf, 0, sizeof(buf));
237 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT);
238 if ((ret != 0x1) || (buf[0] != 0x6f)) {
239 msg_perr("Unexpected response to Command A!\n");
240 return 1;
241 }
242 return 0;
243}
244
245/* Command C is only sent after dediprog_check_devicestring, but not after every
246 * invocation of dediprog_check_devicestring. It is only sent after the first
247 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
248 * I'm tempted to call this one start_SPI_engine or finish_init.
249 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000250static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000251{
252 int ret;
253
254 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT);
255 if (ret != 0x0) {
256 msg_perr("Unexpected response to Command C!\n");
257 return 1;
258 }
259 return 0;
260}
261
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000262#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000263/* Very strange. Seems to be a programmer keepalive or somesuch.
264 * Wait unsuccessfully for timeout ms to read one byte.
265 * Is usually called after setting voltage to 0.
266 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000267static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000268{
269 int ret;
270 char buf[0x1];
271
272 memset(buf, 0, sizeof(buf));
273 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout);
274 if (ret != 0x0) {
275 msg_perr("Unexpected response to Command F!\n");
276 return 1;
277 }
278 return 0;
279}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000280#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000281
282/* URB numbers refer to the first log ever captured. */
283int dediprog_init(void)
284{
285 struct usb_device *dev;
286
287 msg_pspew("%s\n", __func__);
288
289 /* Here comes the USB stuff. */
290 usb_init();
291 usb_find_busses();
292 usb_find_devices();
293 dev = get_device_by_vid_pid(0x0483, 0xdada);
294 if (!dev) {
295 msg_perr("Could not find a Dediprog SF100 on USB!\n");
296 return 1;
297 }
298 msg_pdbg("Found USB device (%04x:%04x).\n",
299 dev->descriptor.idVendor,
300 dev->descriptor.idProduct);
301 dediprog_handle = usb_open(dev);
Patrick Georgi975aa7e2010-02-04 08:29:18 +0000302 usb_set_configuration(dediprog_handle, 1);
303 usb_claim_interface(dediprog_handle, 0);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000304 /* URB 6. Command A. */
305 if (dediprog_command_a())
306 return 1;
307 /* URB 7. Command A. */
308 if (dediprog_command_a())
309 return 1;
310 /* URB 8. Command Prepare Receive Device String. */
311 /* URB 9. Command Receive Device String. */
312 if (dediprog_check_devicestring())
313 return 1;
314 /* URB 10. Command C. */
315 if (dediprog_command_c())
316 return 1;
317 /* URB 11. Command Set SPI Voltage. */
318 if (dediprog_set_spi_voltage(0x10))
319 return 1;
320
321 buses_supported = CHIP_BUSTYPE_SPI;
322 spi_controller = SPI_CONTROLLER_DEDIPROG;
323
324 /* RE leftover, leave in until the driver is complete. */
325#if 0
326 /* Execute RDID by hand if you want to test it. */
327 dediprog_do_stuff();
328#endif
329
330 return 0;
331}
332
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000333#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000334/* Leftovers from reverse engineering. Keep for documentation purposes until
335 * completely understood.
336 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000337static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000338{
339 char buf[0x4];
340 /* SPI command processing starts here. */
341
342 /* URB 12. Command Send SPI. */
343 /* URB 13. Command Receive SPI. */
344 memset(buf, 0, sizeof(buf));
345 /* JEDEC RDID */
346 msg_pdbg("Sending RDID\n");
347 buf[0] = JEDEC_RDID;
348 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf))
349 return 1;
350 msg_pdbg("Receiving response: ");
351 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000352 /* URB 14-27 are more SPI commands. */
353 /* URB 28. Command Set SPI Voltage. */
354 if (dediprog_set_spi_voltage(0x0))
355 return 1;
356 /* URB 29-38. Command F, unsuccessful wait. */
357 if (dediprog_command_f(544))
358 return 1;
359 /* URB 39. Command Set SPI Voltage. */
360 if (dediprog_set_spi_voltage(0x10))
361 return 1;
362 /* URB 40. Command Set SPI Speed. */
363 if (dediprog_set_spi_speed(0x2))
364 return 1;
365 /* URB 41 is just URB 28. */
366 /* URB 42,44,46,48,51,53 is just URB 8. */
367 /* URB 43,45,47,49,52,54 is just URB 9. */
368 /* URB 50 is just URB 6/7. */
369 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
370 /* URB 132,134 is just URB 6/7. */
371 /* URB 133 is just URB 29-38. */
372 /* URB 135 is just URB 8. */
373 /* URB 136 is just URB 9. */
374 /* URB 137 is just URB 11. */
375
376 /* Command I is probably Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
377 /* Command J is probably Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
378 /* Bulk transfer sizes for Command I/J are always 512 bytes, rest is filled with 0xff. */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000379
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000380 return 0;
381}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000382#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000383
384int dediprog_shutdown(void)
385{
386 msg_pspew("%s\n", __func__);
387
388 /* URB 28. Command Set SPI Voltage to 0. */
389 if (dediprog_set_spi_voltage(0x0))
390 return 1;
391
392 if (usb_close(dediprog_handle)) {
393 msg_perr("Couldn't close USB device!\n");
394 return 1;
395 }
396 return 0;
397}