blob: e68fdf971b97be390d1db33644c3c94e7a88fce8 [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
Mathias Krausedb7afc52010-11-09 23:30:43 +000020#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000021#include <string.h>
Stefan Taunere34e3e82013-01-01 00:06:51 +000022#include <limits.h>
Nathan Laredo21541a62012-12-24 22:07:36 +000023#include <errno.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000024#include <usb.h>
25#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000026#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000028#include "spi.h"
29
Stefan Reinauer915b8402011-01-28 09:00:15 +000030#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000031#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000032static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +000033static int dediprog_firmwareversion;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000034static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000035
Nico Hubera4b14f72012-06-19 12:06:53 +000036#define DEDI_SPI_CMD_PAGEWRITE 0x1
37#define DEDI_SPI_CMD_AAIWRITE 0x4
38
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000039#if 0
40/* Might be useful for other pieces of code as well. */
41static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000042{
43 size_t i;
44
45 for (i = 0; i < len; i++)
46 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
47}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000048#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000049
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000050/* Might be useful for other USB devices as well. static for now. */
Nathan Laredo21541a62012-12-24 22:07:36 +000051/* device parameter allows user to specify one device of multiple installed */
52static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000053{
54 struct usb_bus *bus;
55 struct usb_device *dev;
56
57 for (bus = usb_get_busses(); bus; bus = bus->next)
58 for (dev = bus->devices; dev; dev = dev->next)
59 if ((dev->descriptor.idVendor == vid) &&
Nathan Laredo21541a62012-12-24 22:07:36 +000060 (dev->descriptor.idProduct == pid)) {
61 if (device == 0)
62 return dev;
63 device--;
64 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000065
66 return NULL;
67}
68
69//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
70
Stefan Reinauer915b8402011-01-28 09:00:15 +000071/* Set/clear LEDs on dediprog */
72#define PASS_ON (0 << 0)
73#define PASS_OFF (1 << 0)
74#define BUSY_ON (0 << 1)
75#define BUSY_OFF (1 << 1)
76#define ERROR_ON (0 << 2)
77#define ERROR_OFF (1 << 2)
78static int current_led_status = -1;
79
80static int dediprog_set_leds(int leds)
81{
82 int ret, target_leds;
Uwe Hermann91f4afa2011-07-28 08:13:25 +000083
Stefan Reinauer915b8402011-01-28 09:00:15 +000084 if (leds < 0 || leds > 7)
85 leds = 0; // Bogus value, enable all LEDs
86
87 if (leds == current_led_status)
88 return 0;
89
90 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
91 * two LEDs, and they were reversed. So map them around if
92 * we have an old device. On those devices the LEDs map as
93 * follows:
94 * bit 2 == 0: green light is on.
95 * bit 0 == 0: red light is on.
96 */
97 if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
98 target_leds = ((leds & ERROR_OFF) >> 2) |
99 ((leds & PASS_OFF) << 2);
100 } else {
101 target_leds = leds;
102 }
103
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000104 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
105 NULL, 0x0, DEFAULT_TIMEOUT);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000106 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000107 msg_perr("Command Set LED 0x%x failed (%s)!\n",
108 leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000109 return 1;
110 }
111
112 current_led_status = leds;
113
114 return 0;
115}
116
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000117static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000118{
119 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000120 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000121
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000122 switch (millivolt) {
123 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000124 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000125 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000126 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000127 case 1800:
128 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000129 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000130 case 2500:
131 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000132 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000133 case 3500:
134 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000135 break;
136 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000137 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000138 return 1;
139 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000140 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
141 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000142
Nico Huber4099a8a2012-06-16 00:02:27 +0000143 if (voltage_selector == 0) {
144 /* Wait some time as the original driver does. */
145 programmer_delay(200 * 1000);
146 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000147 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
148 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000149 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000150 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
151 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000152 return 1;
153 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000154 if (voltage_selector != 0) {
155 /* Wait some time as the original driver does. */
156 programmer_delay(200 * 1000);
157 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000158 return 0;
159}
160
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000161#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000162/* After dediprog_set_spi_speed, the original app always calls
163 * dediprog_set_spi_voltage(0) and then
164 * dediprog_check_devicestring() four times in a row.
165 * After that, dediprog_command_a() is called.
166 * This looks suspiciously like the microprocessor in the SF100 has to be
167 * restarted/reinitialized in case the speed changes.
168 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000169static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000170{
171 int ret;
172 unsigned int khz;
173
174 /* Case 1 and 2 are in weird order. Probably an organically "grown"
175 * interface.
176 * Base frequency is 24000 kHz, divisors are (in order)
177 * 1, 3, 2, 8, 11, 16, 32, 64.
178 */
179 switch (speed) {
180 case 0x0:
181 khz = 24000;
182 break;
183 case 0x1:
184 khz = 8000;
185 break;
186 case 0x2:
187 khz = 12000;
188 break;
189 case 0x3:
190 khz = 3000;
191 break;
192 case 0x4:
193 khz = 2180;
194 break;
195 case 0x5:
196 khz = 1500;
197 break;
198 case 0x6:
199 khz = 750;
200 break;
201 case 0x7:
202 khz = 375;
203 break;
204 default:
205 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
206 return 1;
207 }
208 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
209
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000210 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
211 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000212 if (ret != 0x0) {
213 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
214 return 1;
215 }
216 return 0;
217}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000218#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000219
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000220/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
221 * @start start address
222 * @len length
223 * @return 0 on success, 1 on failure
224 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000225static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000226 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000227{
228 int ret;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000229 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000230 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000231 const unsigned int chunksize = 0x200;
232 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000233 const char count_and_chunk[] = {count & 0xff,
234 (count >> 8) & 0xff,
235 chunksize & 0xff,
236 (chunksize >> 8) & 0xff};
237
238 if ((start % chunksize) || (len % chunksize)) {
239 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
240 "at flashrom@flashrom.org\n", __func__, start, len);
241 return 1;
242 }
243
244 /* No idea if the hardware can handle empty reads, so chicken out. */
245 if (!len)
246 return 0;
247 /* Command Read SPI Bulk. No idea which read command is used on the
248 * SPI side.
249 */
250 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
251 start / 0x10000, (char *)count_and_chunk,
252 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
253 if (ret != sizeof(count_and_chunk)) {
254 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
255 usb_strerror());
256 return 1;
257 }
258
259 for (i = 0; i < count; i++) {
260 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
261 (char *)buf + i * chunksize, chunksize,
262 DEFAULT_TIMEOUT);
263 if (ret != chunksize) {
264 msg_perr("SPI bulk read %i failed, expected %i, got %i "
265 "%s!\n", i, chunksize, ret, usb_strerror());
266 return 1;
267 }
268 }
269
270 return 0;
271}
272
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000273static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000274 unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000275{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000276 int ret;
277 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000278 const unsigned int chunksize = 0x200;
279 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
280 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000281
Stefan Reinauer915b8402011-01-28 09:00:15 +0000282 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
283
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000284 if (residue) {
285 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
286 start, residue);
287 ret = spi_read_chunked(flash, buf, start, residue, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000288 if (ret) {
289 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000290 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000291 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000292 }
293
294 /* Round down. */
295 bulklen = (len - residue) / chunksize * chunksize;
296 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
297 bulklen);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000298 if (ret) {
299 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000300 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000301 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000302
303 len -= residue + bulklen;
304 if (len) {
305 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
306 start, len);
307 ret = spi_read_chunked(flash, buf + residue + bulklen,
308 start + residue + bulklen, len, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000309 if (ret) {
310 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000311 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000312 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000313 }
314
Stefan Reinauer915b8402011-01-28 09:00:15 +0000315 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000316 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000317}
318
Nico Hubera4b14f72012-06-19 12:06:53 +0000319/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
320 * @chunksize length of data chunks, only 256 supported by now
321 * @start start address
322 * @len length
323 * @dedi_spi_cmd dediprog specific write command for spi bus
324 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000325 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000326static int dediprog_spi_bulk_write(struct flashctx *flash, uint8_t *buf, unsigned int chunksize,
327 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000328{
329 int ret;
330 unsigned int i;
331 /* USB transfer size must be 512, other sizes will NOT work at all.
332 * chunksize is the real data size per USB bulk transfer. The remaining
333 * space in a USB bulk transfer must be filled with 0xff padding.
334 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000335 const unsigned int count = len / chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000336 const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000337 char usbbuf[512];
338
Nico Hubera4b14f72012-06-19 12:06:53 +0000339 /*
340 * We should change this check to
341 * chunksize > 512
342 * once we know how to handle different chunk sizes.
343 */
344 if (chunksize != 256) {
345 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
346 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
347 return 1;
348 }
349
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000350 if ((start % chunksize) || (len % chunksize)) {
351 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
352 "at flashrom@flashrom.org\n", __func__, start, len);
353 return 1;
354 }
355
356 /* No idea if the hardware can handle empty writes, so chicken out. */
357 if (!len)
358 return 0;
359 /* Command Write SPI Bulk. No idea which write command is used on the
360 * SPI side.
361 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000362 ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000, start / 0x10000,
363 (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT);
364 if (ret != sizeof(count_and_cmd)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000365 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
366 usb_strerror());
367 return 1;
368 }
369
370 for (i = 0; i < count; i++) {
371 memset(usbbuf, 0xff, sizeof(usbbuf));
372 memcpy(usbbuf, buf + i * chunksize, chunksize);
373 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
374 usbbuf, 512,
375 DEFAULT_TIMEOUT);
376 if (ret != 512) {
377 msg_perr("SPI bulk write failed, expected %i, got %i "
378 "%s!\n", 512, ret, usb_strerror());
379 return 1;
380 }
381 }
382
383 return 0;
384}
385
Nico Hubera4b14f72012-06-19 12:06:53 +0000386static int dediprog_spi_write(struct flashctx *flash, uint8_t *buf,
387 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000388{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000389 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000390 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000391 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
392 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000393
394 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
395
Nico Hubera4b14f72012-06-19 12:06:53 +0000396 if (chunksize != 256) {
397 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
398 "we don't know how dediprog\nhandles them.\n");
399 /* Write everything like it was residue. */
400 residue = len;
401 }
402
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000403 if (residue) {
404 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
405 start, residue);
406 /* No idea about the real limit. Maybe 12, maybe more. */
407 ret = spi_write_chunked(flash, buf, start, residue, 12);
408 if (ret) {
409 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
410 return ret;
411 }
412 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000413
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000414 /* Round down. */
415 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000416 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000417 if (ret) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000418 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000419 return ret;
420 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000421
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000422 len -= residue + bulklen;
423 if (len) {
424 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
425 start, len);
426 ret = spi_write_chunked(flash, buf + residue + bulklen,
427 start + residue + bulklen, len, 12);
428 if (ret) {
429 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
430 return ret;
431 }
432 }
433
434 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
435 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000436}
437
Nico Hubera4b14f72012-06-19 12:06:53 +0000438static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
439{
440 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_PAGEWRITE);
441}
442
443static int dediprog_spi_write_aai(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
444{
445 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_AAIWRITE);
446}
447
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000448static int dediprog_spi_send_command(struct flashctx *flash,
449 unsigned int writecnt,
450 unsigned int readcnt,
451 const unsigned char *writearr,
452 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000453{
454 int ret;
455
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000456 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000457 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000458 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000459 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000460 return 1;
461 }
462 /* 16 byte reads should work. */
463 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000464 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000465 return 1;
466 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000467
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000468 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
469 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
470 DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000471 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000472 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
473 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000474 return 1;
475 }
476 if (!readcnt)
477 return 0;
478 memset(readarr, 0, readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000479 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
480 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000481 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000482 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
483 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000484 return 1;
485 }
486 return 0;
487}
488
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000489static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000490{
491 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000492 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000493 char buf[0x11];
494
495 /* Command Prepare Receive Device String. */
496 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000497 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
498 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000499 /* The char casting is needed to stop gcc complaining about an always true comparison. */
500 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
501 msg_perr("Unexpected response to Command Prepare Receive Device"
502 " String!\n");
503 return 1;
504 }
505 /* Command Receive Device String. */
506 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000507 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
508 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000509 if (ret != 0x10) {
510 msg_perr("Incomplete/failed Command Receive Device String!\n");
511 return 1;
512 }
513 buf[0x10] = '\0';
514 msg_pdbg("Found a %s\n", buf);
515 if (memcmp(buf, "SF100", 0x5)) {
516 msg_perr("Device not a SF100!\n");
517 return 1;
518 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000519 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
520 msg_perr("Unexpected firmware version string!\n");
521 return 1;
522 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000523 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000524 if (fw[0] < 2 || fw[0] > 5) {
525 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
526 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000527 return 1;
528 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000529 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000530 return 0;
531}
532
533/* Command A seems to be some sort of device init. It is either followed by
534 * dediprog_check_devicestring (often) or Command A (often) or
535 * Command F (once).
536 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000537static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000538{
539 int ret;
540 char buf[0x1];
541
542 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000543 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
544 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000545 if (ret < 0) {
546 msg_perr("Command A failed (%s)!\n", usb_strerror());
547 return 1;
548 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000549 if ((ret != 0x1) || (buf[0] != 0x6f)) {
550 msg_perr("Unexpected response to Command A!\n");
551 return 1;
552 }
553 return 0;
554}
555
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000556#if 0
557/* Something.
558 * Present in eng_detect_blink.log with firmware 3.1.8
559 * Always preceded by Command Receive Device String
560 */
561static int dediprog_command_b(void)
562{
563 int ret;
564 char buf[0x3];
565
566 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000567 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
568 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000569 if (ret < 0) {
570 msg_perr("Command B failed (%s)!\n", usb_strerror());
571 return 1;
572 }
573 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
574 (buf[2] != 0xff)) {
575 msg_perr("Unexpected response to Command B!\n");
576 return 1;
577 }
578
579 return 0;
580}
581#endif
582
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000583/* Command C is only sent after dediprog_check_devicestring, but not after every
584 * invocation of dediprog_check_devicestring. It is only sent after the first
585 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
586 * I'm tempted to call this one start_SPI_engine or finish_init.
587 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000588static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000589{
590 int ret;
591
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000592 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
593 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000594 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000595 msg_perr("Command C failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000596 return 1;
597 }
598 return 0;
599}
600
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000601#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000602/* Very strange. Seems to be a programmer keepalive or somesuch.
603 * Wait unsuccessfully for timeout ms to read one byte.
604 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000605 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000606 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000607static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000608{
609 int ret;
610 char buf[0x1];
611
612 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000613 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
614 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000615 /* This check is most probably wrong. Command F always causes a timeout
616 * in the logs, so we should check for timeout instead of checking for
617 * success.
618 */
619 if (ret != 0x1) {
620 msg_perr("Command F failed (%s)!\n", usb_strerror());
621 return 1;
622 }
623 return 0;
624}
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000625
626/* Start/stop blinking?
627 * Present in eng_detect_blink.log with firmware 3.1.8
628 * Preceded by Command J
629 */
630static int dediprog_command_g(void)
631{
632 int ret;
633
634 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
635 if (ret != 0x0) {
636 msg_perr("Command G failed (%s)!\n", usb_strerror());
637 return 1;
638 }
639 return 0;
640}
641
642/* Something.
643 * Present in all logs with firmware 5.1.5
644 * Always preceded by Command Receive Device String
645 * Always followed by Command Set SPI Voltage nonzero
646 */
647static int dediprog_command_h(void)
648{
649 int ret;
650
651 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
652 if (ret != 0x0) {
653 msg_perr("Command H failed (%s)!\n", usb_strerror());
654 return 1;
655 }
656 return 0;
657}
658
659/* Shutdown for firmware 5.x?
660 * Present in all logs with firmware 5.1.5
661 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
662 * Always followed by Command Set SPI Voltage 0x0000
663 */
664static int dediprog_command_i(void)
665{
666 int ret;
667
668 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
669 if (ret != 0x0) {
670 msg_perr("Command I failed (%s)!\n", usb_strerror());
671 return 1;
672 }
673 return 0;
674}
675
676/* Start/stop blinking?
677 * Present in all logs with firmware 5.1.5
678 * Always preceded by Command Receive Device String on 5.1.5
679 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
680 * Present in eng_detect_blink.log with firmware 3.1.8
681 * Preceded by Command B in eng_detect_blink.log
682 * Followed by Command G in eng_detect_blink.log
683 */
684static int dediprog_command_j(void)
685{
686 int ret;
687
688 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
689 if (ret != 0x0) {
690 msg_perr("Command J failed (%s)!\n", usb_strerror());
691 return 1;
692 }
693 return 0;
694}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000695#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000696
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000697static int parse_voltage(char *voltage)
698{
699 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000700 int i;
701 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000702
703 if (!voltage || !strlen(voltage)) {
704 msg_perr("Empty voltage= specified.\n");
705 return -1;
706 }
707 millivolt = (int)strtol(voltage, &tmp, 0);
708 voltage = tmp;
709 /* Handle "," and "." as decimal point. Everything after it is assumed
710 * to be in decimal notation.
711 */
712 if ((*voltage == '.') || (*voltage == ',')) {
713 voltage++;
714 for (i = 0; i < 3; i++) {
715 fraction *= 10;
716 /* Don't advance if the current character is invalid,
717 * but continue multiplying.
718 */
719 if ((*voltage < '0') || (*voltage > '9'))
720 continue;
721 fraction += *voltage - '0';
722 voltage++;
723 }
724 /* Throw away remaining digits. */
725 voltage += strspn(voltage, "0123456789");
726 }
727 /* The remaining string must be empty or "mV" or "V". */
728 tolower_string(voltage);
729
730 /* No unit or "V". */
731 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
732 millivolt *= 1000;
733 millivolt += fraction;
734 } else if (!strncmp(voltage, "mv", 2) ||
735 !strncmp(voltage, "milliv", 6)) {
736 /* No adjustment. fraction is discarded. */
737 } else {
738 /* Garbage at the end of the string. */
739 msg_perr("Garbage voltage= specified.\n");
740 return -1;
741 }
742 return millivolt;
743}
744
Michael Karcherb9dbe482011-05-11 17:07:07 +0000745static const struct spi_programmer spi_programmer_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000746 .type = SPI_CONTROLLER_DEDIPROG,
747 .max_data_read = MAX_DATA_UNSPECIFIED,
748 .max_data_write = MAX_DATA_UNSPECIFIED,
749 .command = dediprog_spi_send_command,
750 .multicommand = default_spi_send_multicommand,
751 .read = dediprog_spi_read,
752 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000753 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000754};
755
David Hendricks8bb20212011-06-14 01:35:36 +0000756static int dediprog_shutdown(void *data)
757{
758 msg_pspew("%s\n", __func__);
759
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000760#if 0
761 /* Shutdown on firmware 5.x */
762 if (dediprog_firmwareversion == 5)
763 if (dediprog_command_i())
764 return 1;
765#endif
766
David Hendricks8bb20212011-06-14 01:35:36 +0000767 /* URB 28. Command Set SPI Voltage to 0. */
768 if (dediprog_set_spi_voltage(0x0))
769 return 1;
770
771 if (usb_release_interface(dediprog_handle, 0)) {
772 msg_perr("Could not release USB interface!\n");
773 return 1;
774 }
775 if (usb_close(dediprog_handle)) {
776 msg_perr("Could not close USB device!\n");
777 return 1;
778 }
779 return 0;
780}
781
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000782/* URB numbers refer to the first log ever captured. */
783int dediprog_init(void)
784{
785 struct usb_device *dev;
Nathan Laredo21541a62012-12-24 22:07:36 +0000786 char *voltage, *device;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000787 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000788 long usedevice = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000789 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000790
791 msg_pspew("%s\n", __func__);
792
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000793 voltage = extract_programmer_param("voltage");
794 if (voltage) {
795 millivolt = parse_voltage(voltage);
796 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000797 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000798 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000799 msg_pinfo("Setting voltage to %i mV\n", millivolt);
800 }
801
Nathan Laredo21541a62012-12-24 22:07:36 +0000802 device = extract_programmer_param("device");
803 if (device) {
804 char *dev_suffix;
805 errno = 0;
806 usedevice = strtol(device, &dev_suffix, 10);
807 if (errno != 0 || device == dev_suffix) {
808 msg_perr("Error: Could not convert 'device'.\n");
809 free(device);
810 return 1;
811 }
812 if (usedevice < 0 || usedevice > UINT_MAX) {
813 msg_perr("Error: Value for 'device' is out of range.\n");
814 free(device);
815 return 1;
816 }
817 if (strlen(dev_suffix) > 0) {
818 msg_perr("Error: Garbage following 'device' value.\n");
819 free(device);
820 return 1;
821 }
822 msg_pinfo("Using device %li.\n", usedevice);
823 }
824 free(device);
825
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000826 /* Here comes the USB stuff. */
827 usb_init();
828 usb_find_busses();
829 usb_find_devices();
Nathan Laredo21541a62012-12-24 22:07:36 +0000830 dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000831 if (!dev) {
832 msg_perr("Could not find a Dediprog SF100 on USB!\n");
833 return 1;
834 }
835 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000836 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000837 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000838 ret = usb_set_configuration(dediprog_handle, 1);
839 if (ret < 0) {
840 msg_perr("Could not set USB device configuration: %i %s\n",
841 ret, usb_strerror());
842 if (usb_close(dediprog_handle))
843 msg_perr("Could not close USB device!\n");
844 return 1;
845 }
846 ret = usb_claim_interface(dediprog_handle, 0);
847 if (ret < 0) {
848 msg_perr("Could not claim USB device interface %i: %i %s\n",
849 0, ret, usb_strerror());
850 if (usb_close(dediprog_handle))
851 msg_perr("Could not close USB device!\n");
852 return 1;
853 }
854 dediprog_endpoint = 2;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000855
David Hendricks8bb20212011-06-14 01:35:36 +0000856 if (register_shutdown(dediprog_shutdown, NULL))
857 return 1;
858
Stefan Reinauer915b8402011-01-28 09:00:15 +0000859 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
860
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000861 /* URB 6. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000862 if (dediprog_command_a()) {
863 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000864 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000865 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000866 /* URB 7. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000867 if (dediprog_command_a()) {
868 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000869 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000870 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000871 /* URB 8. Command Prepare Receive Device String. */
872 /* URB 9. Command Receive Device String. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000873 if (dediprog_check_devicestring()) {
874 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000875 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000876 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000877 /* URB 10. Command C. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000878 if (dediprog_command_c()) {
879 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000880 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000881 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000882 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000883 if (dediprog_set_spi_voltage(millivolt)) {
884 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000885 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000886 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000887
Michael Karcherb9dbe482011-05-11 17:07:07 +0000888 register_spi_programmer(&spi_programmer_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000889
890 /* RE leftover, leave in until the driver is complete. */
891#if 0
892 /* Execute RDID by hand if you want to test it. */
893 dediprog_do_stuff();
894#endif
895
Stefan Reinauer915b8402011-01-28 09:00:15 +0000896 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
897
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000898 return 0;
899}
900
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000901#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000902/* Leftovers from reverse engineering. Keep for documentation purposes until
903 * completely understood.
904 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000905static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000906{
907 char buf[0x4];
908 /* SPI command processing starts here. */
909
910 /* URB 12. Command Send SPI. */
911 /* URB 13. Command Receive SPI. */
912 memset(buf, 0, sizeof(buf));
913 /* JEDEC RDID */
914 msg_pdbg("Sending RDID\n");
915 buf[0] = JEDEC_RDID;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000916 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
917 (unsigned char *)buf, (unsigned char *)buf))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000918 return 1;
919 msg_pdbg("Receiving response: ");
920 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000921 /* URB 14-27 are more SPI commands. */
922 /* URB 28. Command Set SPI Voltage. */
923 if (dediprog_set_spi_voltage(0x0))
924 return 1;
925 /* URB 29-38. Command F, unsuccessful wait. */
926 if (dediprog_command_f(544))
927 return 1;
928 /* URB 39. Command Set SPI Voltage. */
929 if (dediprog_set_spi_voltage(0x10))
930 return 1;
931 /* URB 40. Command Set SPI Speed. */
932 if (dediprog_set_spi_speed(0x2))
933 return 1;
934 /* URB 41 is just URB 28. */
935 /* URB 42,44,46,48,51,53 is just URB 8. */
936 /* URB 43,45,47,49,52,54 is just URB 9. */
937 /* URB 50 is just URB 6/7. */
938 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
939 /* URB 132,134 is just URB 6/7. */
940 /* URB 133 is just URB 29-38. */
941 /* URB 135 is just URB 8. */
942 /* URB 136 is just URB 9. */
943 /* URB 137 is just URB 11. */
944
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000945 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
946 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
947 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
948 * 512 bytes, rest is filled with 0xff.
949 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000950
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000951 return 0;
952}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000953#endif