blob: ae86810e8d2a3aa549d6ae20e48503f3953ddf29 [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
Nico Huber77fa67d2013-02-20 18:03:36 +0000161struct dediprog_spispeeds {
162 const char *const name;
163 const int speed;
164};
165
166static const struct dediprog_spispeeds spispeeds[] = {
167 { "24M", 0x0 },
168 { "12M", 0x2 },
169 { "8M", 0x1 },
170 { "3M", 0x3 },
171 { "2.18M", 0x4 },
172 { "1.5M", 0x5 },
173 { "750k", 0x6 },
174 { "375k", 0x7 },
175 { NULL, 0x0 },
176};
177
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000178/* After dediprog_set_spi_speed, the original app always calls
179 * dediprog_set_spi_voltage(0) and then
180 * dediprog_check_devicestring() four times in a row.
181 * After that, dediprog_command_a() is called.
182 * This looks suspiciously like the microprocessor in the SF100 has to be
183 * restarted/reinitialized in case the speed changes.
184 */
Nico Huber77fa67d2013-02-20 18:03:36 +0000185static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000186{
187 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000188
Nico Huber77fa67d2013-02-20 18:03:36 +0000189 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed_idx].name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000190
Nico Huber77fa67d2013-02-20 18:03:36 +0000191 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, spispeeds[spispeed_idx].speed, 0xff,
192 NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000193 if (ret != 0x0) {
Nico Huber77fa67d2013-02-20 18:03:36 +0000194 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeeds[spispeed_idx].speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000195 return 1;
196 }
197 return 0;
198}
199
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000200/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
201 * @start start address
202 * @len length
203 * @return 0 on success, 1 on failure
204 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000205static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000206 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000207{
208 int ret;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000209 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000210 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000211 const unsigned int chunksize = 0x200;
212 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000213 const char count_and_chunk[] = {count & 0xff,
214 (count >> 8) & 0xff,
215 chunksize & 0xff,
216 (chunksize >> 8) & 0xff};
217
218 if ((start % chunksize) || (len % chunksize)) {
219 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
220 "at flashrom@flashrom.org\n", __func__, start, len);
221 return 1;
222 }
223
224 /* No idea if the hardware can handle empty reads, so chicken out. */
225 if (!len)
226 return 0;
227 /* Command Read SPI Bulk. No idea which read command is used on the
228 * SPI side.
229 */
230 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
231 start / 0x10000, (char *)count_and_chunk,
232 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
233 if (ret != sizeof(count_and_chunk)) {
234 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
235 usb_strerror());
236 return 1;
237 }
238
239 for (i = 0; i < count; i++) {
240 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
241 (char *)buf + i * chunksize, chunksize,
242 DEFAULT_TIMEOUT);
243 if (ret != chunksize) {
244 msg_perr("SPI bulk read %i failed, expected %i, got %i "
245 "%s!\n", i, chunksize, ret, usb_strerror());
246 return 1;
247 }
248 }
249
250 return 0;
251}
252
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000253static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000254 unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000255{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000256 int ret;
257 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000258 const unsigned int chunksize = 0x200;
259 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
260 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000261
Stefan Reinauer915b8402011-01-28 09:00:15 +0000262 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
263
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000264 if (residue) {
265 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
266 start, residue);
267 ret = spi_read_chunked(flash, buf, start, residue, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000268 if (ret) {
269 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000270 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000271 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000272 }
273
274 /* Round down. */
275 bulklen = (len - residue) / chunksize * chunksize;
276 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
277 bulklen);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000278 if (ret) {
279 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000280 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000281 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000282
283 len -= residue + bulklen;
284 if (len) {
285 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
286 start, len);
287 ret = spi_read_chunked(flash, buf + residue + bulklen,
288 start + residue + bulklen, len, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000289 if (ret) {
290 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000291 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000292 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000293 }
294
Stefan Reinauer915b8402011-01-28 09:00:15 +0000295 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000296 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000297}
298
Nico Hubera4b14f72012-06-19 12:06:53 +0000299/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
300 * @chunksize length of data chunks, only 256 supported by now
301 * @start start address
302 * @len length
303 * @dedi_spi_cmd dediprog specific write command for spi bus
304 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000305 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000306static int dediprog_spi_bulk_write(struct flashctx *flash, uint8_t *buf, unsigned int chunksize,
307 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000308{
309 int ret;
310 unsigned int i;
311 /* USB transfer size must be 512, other sizes will NOT work at all.
312 * chunksize is the real data size per USB bulk transfer. The remaining
313 * space in a USB bulk transfer must be filled with 0xff padding.
314 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000315 const unsigned int count = len / chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000316 const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000317 char usbbuf[512];
318
Nico Hubera4b14f72012-06-19 12:06:53 +0000319 /*
320 * We should change this check to
321 * chunksize > 512
322 * once we know how to handle different chunk sizes.
323 */
324 if (chunksize != 256) {
325 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
326 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
327 return 1;
328 }
329
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000330 if ((start % chunksize) || (len % chunksize)) {
331 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
332 "at flashrom@flashrom.org\n", __func__, start, len);
333 return 1;
334 }
335
336 /* No idea if the hardware can handle empty writes, so chicken out. */
337 if (!len)
338 return 0;
339 /* Command Write SPI Bulk. No idea which write command is used on the
340 * SPI side.
341 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000342 ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000, start / 0x10000,
343 (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT);
344 if (ret != sizeof(count_and_cmd)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000345 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
346 usb_strerror());
347 return 1;
348 }
349
350 for (i = 0; i < count; i++) {
351 memset(usbbuf, 0xff, sizeof(usbbuf));
352 memcpy(usbbuf, buf + i * chunksize, chunksize);
353 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
354 usbbuf, 512,
355 DEFAULT_TIMEOUT);
356 if (ret != 512) {
357 msg_perr("SPI bulk write failed, expected %i, got %i "
358 "%s!\n", 512, ret, usb_strerror());
359 return 1;
360 }
361 }
362
363 return 0;
364}
365
Nico Hubera4b14f72012-06-19 12:06:53 +0000366static int dediprog_spi_write(struct flashctx *flash, uint8_t *buf,
367 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000368{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000369 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000370 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000371 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
372 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000373
374 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
375
Nico Hubera4b14f72012-06-19 12:06:53 +0000376 if (chunksize != 256) {
377 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
378 "we don't know how dediprog\nhandles them.\n");
379 /* Write everything like it was residue. */
380 residue = len;
381 }
382
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000383 if (residue) {
384 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
385 start, residue);
386 /* No idea about the real limit. Maybe 12, maybe more. */
387 ret = spi_write_chunked(flash, buf, start, residue, 12);
388 if (ret) {
389 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
390 return ret;
391 }
392 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000393
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000394 /* Round down. */
395 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000396 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000397 if (ret) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000398 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000399 return ret;
400 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000401
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000402 len -= residue + bulklen;
403 if (len) {
404 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
405 start, len);
406 ret = spi_write_chunked(flash, buf + residue + bulklen,
407 start + residue + bulklen, len, 12);
408 if (ret) {
409 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
410 return ret;
411 }
412 }
413
414 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
415 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000416}
417
Nico Hubera4b14f72012-06-19 12:06:53 +0000418static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
419{
420 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_PAGEWRITE);
421}
422
423static int dediprog_spi_write_aai(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
424{
425 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_AAIWRITE);
426}
427
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000428static int dediprog_spi_send_command(struct flashctx *flash,
429 unsigned int writecnt,
430 unsigned int readcnt,
431 const unsigned char *writearr,
432 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000433{
434 int ret;
435
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000436 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000437 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000438 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000439 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000440 return 1;
441 }
442 /* 16 byte reads should work. */
443 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000444 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000445 return 1;
446 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000447
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000448 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
449 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
450 DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000451 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000452 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
453 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000454 return 1;
455 }
456 if (!readcnt)
457 return 0;
458 memset(readarr, 0, readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000459 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
460 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000461 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000462 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
463 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000464 return 1;
465 }
466 return 0;
467}
468
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000469static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000470{
471 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000472 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000473 char buf[0x11];
474
475 /* Command Prepare Receive Device String. */
476 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000477 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
478 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000479 /* The char casting is needed to stop gcc complaining about an always true comparison. */
480 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
481 msg_perr("Unexpected response to Command Prepare Receive Device"
482 " String!\n");
483 return 1;
484 }
485 /* Command Receive Device String. */
486 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000487 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
488 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000489 if (ret != 0x10) {
490 msg_perr("Incomplete/failed Command Receive Device String!\n");
491 return 1;
492 }
493 buf[0x10] = '\0';
494 msg_pdbg("Found a %s\n", buf);
495 if (memcmp(buf, "SF100", 0x5)) {
496 msg_perr("Device not a SF100!\n");
497 return 1;
498 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000499 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
500 msg_perr("Unexpected firmware version string!\n");
501 return 1;
502 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000503 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000504 if (fw[0] < 2 || fw[0] > 5) {
505 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
506 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000507 return 1;
508 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000509 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000510 return 0;
511}
512
513/* Command A seems to be some sort of device init. It is either followed by
514 * dediprog_check_devicestring (often) or Command A (often) or
515 * Command F (once).
516 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000517static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000518{
519 int ret;
520 char buf[0x1];
521
522 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000523 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
524 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000525 if (ret < 0) {
526 msg_perr("Command A failed (%s)!\n", usb_strerror());
527 return 1;
528 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000529 if ((ret != 0x1) || (buf[0] != 0x6f)) {
530 msg_perr("Unexpected response to Command A!\n");
531 return 1;
532 }
533 return 0;
534}
535
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000536#if 0
537/* Something.
538 * Present in eng_detect_blink.log with firmware 3.1.8
539 * Always preceded by Command Receive Device String
540 */
541static int dediprog_command_b(void)
542{
543 int ret;
544 char buf[0x3];
545
546 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000547 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
548 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000549 if (ret < 0) {
550 msg_perr("Command B failed (%s)!\n", usb_strerror());
551 return 1;
552 }
553 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
554 (buf[2] != 0xff)) {
555 msg_perr("Unexpected response to Command B!\n");
556 return 1;
557 }
558
559 return 0;
560}
561#endif
562
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000563/* Command C is only sent after dediprog_check_devicestring, but not after every
564 * invocation of dediprog_check_devicestring. It is only sent after the first
565 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
566 * I'm tempted to call this one start_SPI_engine or finish_init.
567 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000568static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000569{
570 int ret;
571
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000572 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
573 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000574 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000575 msg_perr("Command C failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000576 return 1;
577 }
578 return 0;
579}
580
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000581#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000582/* Very strange. Seems to be a programmer keepalive or somesuch.
583 * Wait unsuccessfully for timeout ms to read one byte.
584 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000585 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000586 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000587static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000588{
589 int ret;
590 char buf[0x1];
591
592 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000593 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
594 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000595 /* This check is most probably wrong. Command F always causes a timeout
596 * in the logs, so we should check for timeout instead of checking for
597 * success.
598 */
599 if (ret != 0x1) {
600 msg_perr("Command F failed (%s)!\n", usb_strerror());
601 return 1;
602 }
603 return 0;
604}
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000605
606/* Start/stop blinking?
607 * Present in eng_detect_blink.log with firmware 3.1.8
608 * Preceded by Command J
609 */
610static int dediprog_command_g(void)
611{
612 int ret;
613
614 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
615 if (ret != 0x0) {
616 msg_perr("Command G failed (%s)!\n", usb_strerror());
617 return 1;
618 }
619 return 0;
620}
621
622/* Something.
623 * Present in all logs with firmware 5.1.5
624 * Always preceded by Command Receive Device String
625 * Always followed by Command Set SPI Voltage nonzero
626 */
627static int dediprog_command_h(void)
628{
629 int ret;
630
631 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
632 if (ret != 0x0) {
633 msg_perr("Command H failed (%s)!\n", usb_strerror());
634 return 1;
635 }
636 return 0;
637}
638
639/* Shutdown for firmware 5.x?
640 * Present in all logs with firmware 5.1.5
641 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
642 * Always followed by Command Set SPI Voltage 0x0000
643 */
644static int dediprog_command_i(void)
645{
646 int ret;
647
648 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
649 if (ret != 0x0) {
650 msg_perr("Command I failed (%s)!\n", usb_strerror());
651 return 1;
652 }
653 return 0;
654}
655
656/* Start/stop blinking?
657 * Present in all logs with firmware 5.1.5
658 * Always preceded by Command Receive Device String on 5.1.5
659 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
660 * Present in eng_detect_blink.log with firmware 3.1.8
661 * Preceded by Command B in eng_detect_blink.log
662 * Followed by Command G in eng_detect_blink.log
663 */
664static int dediprog_command_j(void)
665{
666 int ret;
667
668 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
669 if (ret != 0x0) {
670 msg_perr("Command J failed (%s)!\n", usb_strerror());
671 return 1;
672 }
673 return 0;
674}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000675#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000676
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000677static int parse_voltage(char *voltage)
678{
679 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000680 int i;
681 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000682
683 if (!voltage || !strlen(voltage)) {
684 msg_perr("Empty voltage= specified.\n");
685 return -1;
686 }
687 millivolt = (int)strtol(voltage, &tmp, 0);
688 voltage = tmp;
689 /* Handle "," and "." as decimal point. Everything after it is assumed
690 * to be in decimal notation.
691 */
692 if ((*voltage == '.') || (*voltage == ',')) {
693 voltage++;
694 for (i = 0; i < 3; i++) {
695 fraction *= 10;
696 /* Don't advance if the current character is invalid,
697 * but continue multiplying.
698 */
699 if ((*voltage < '0') || (*voltage > '9'))
700 continue;
701 fraction += *voltage - '0';
702 voltage++;
703 }
704 /* Throw away remaining digits. */
705 voltage += strspn(voltage, "0123456789");
706 }
707 /* The remaining string must be empty or "mV" or "V". */
708 tolower_string(voltage);
709
710 /* No unit or "V". */
711 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
712 millivolt *= 1000;
713 millivolt += fraction;
714 } else if (!strncmp(voltage, "mv", 2) ||
715 !strncmp(voltage, "milliv", 6)) {
716 /* No adjustment. fraction is discarded. */
717 } else {
718 /* Garbage at the end of the string. */
719 msg_perr("Garbage voltage= specified.\n");
720 return -1;
721 }
722 return millivolt;
723}
724
Nico Huber77fa67d2013-02-20 18:03:36 +0000725static int dediprog_setup(void)
726{
727 /* URB 6. Command A. */
728 if (dediprog_command_a()) {
729 return 1;
730 }
731 /* URB 7. Command A. */
732 if (dediprog_command_a()) {
733 return 1;
734 }
735 /* URB 8. Command Prepare Receive Device String. */
736 /* URB 9. Command Receive Device String. */
737 if (dediprog_check_devicestring()) {
738 return 1;
739 }
740 /* URB 10. Command C. */
741 if (dediprog_command_c()) {
742 return 1;
743 }
744 return 0;
745}
746
Michael Karcherb9dbe482011-05-11 17:07:07 +0000747static const struct spi_programmer spi_programmer_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000748 .type = SPI_CONTROLLER_DEDIPROG,
749 .max_data_read = MAX_DATA_UNSPECIFIED,
750 .max_data_write = MAX_DATA_UNSPECIFIED,
751 .command = dediprog_spi_send_command,
752 .multicommand = default_spi_send_multicommand,
753 .read = dediprog_spi_read,
754 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000755 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000756};
757
David Hendricks8bb20212011-06-14 01:35:36 +0000758static int dediprog_shutdown(void *data)
759{
760 msg_pspew("%s\n", __func__);
761
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000762#if 0
763 /* Shutdown on firmware 5.x */
764 if (dediprog_firmwareversion == 5)
765 if (dediprog_command_i())
766 return 1;
767#endif
768
David Hendricks8bb20212011-06-14 01:35:36 +0000769 /* URB 28. Command Set SPI Voltage to 0. */
770 if (dediprog_set_spi_voltage(0x0))
771 return 1;
772
773 if (usb_release_interface(dediprog_handle, 0)) {
774 msg_perr("Could not release USB interface!\n");
775 return 1;
776 }
777 if (usb_close(dediprog_handle)) {
778 msg_perr("Could not close USB device!\n");
779 return 1;
780 }
781 return 0;
782}
783
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000784/* URB numbers refer to the first log ever captured. */
785int dediprog_init(void)
786{
787 struct usb_device *dev;
Nico Huber77fa67d2013-02-20 18:03:36 +0000788 char *voltage, *device, *spispeed;
789 int spispeed_idx = 2;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000790 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000791 long usedevice = 0;
Nico Huber77fa67d2013-02-20 18:03:36 +0000792 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000793
794 msg_pspew("%s\n", __func__);
795
Nico Huber77fa67d2013-02-20 18:03:36 +0000796 spispeed = extract_programmer_param("spispeed");
797 if (spispeed) {
798 for (i = 0; spispeeds[i].name; ++i) {
799 if (!strcasecmp(spispeeds[i].name, spispeed)) {
800 spispeed_idx = i;
801 break;
802 }
803 }
804 if (!spispeeds[i].name) {
805 msg_perr("Error: Invalid 'spispeed' value.\n");
806 free(spispeed);
807 return 1;
808 }
809 free(spispeed);
810 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000811 voltage = extract_programmer_param("voltage");
812 if (voltage) {
813 millivolt = parse_voltage(voltage);
814 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000815 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000816 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000817 msg_pinfo("Setting voltage to %i mV\n", millivolt);
818 }
819
Nathan Laredo21541a62012-12-24 22:07:36 +0000820 device = extract_programmer_param("device");
821 if (device) {
822 char *dev_suffix;
823 errno = 0;
824 usedevice = strtol(device, &dev_suffix, 10);
825 if (errno != 0 || device == dev_suffix) {
826 msg_perr("Error: Could not convert 'device'.\n");
827 free(device);
828 return 1;
829 }
830 if (usedevice < 0 || usedevice > UINT_MAX) {
831 msg_perr("Error: Value for 'device' is out of range.\n");
832 free(device);
833 return 1;
834 }
835 if (strlen(dev_suffix) > 0) {
836 msg_perr("Error: Garbage following 'device' value.\n");
837 free(device);
838 return 1;
839 }
840 msg_pinfo("Using device %li.\n", usedevice);
841 }
842 free(device);
843
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000844 /* Here comes the USB stuff. */
845 usb_init();
846 usb_find_busses();
847 usb_find_devices();
Nathan Laredo21541a62012-12-24 22:07:36 +0000848 dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000849 if (!dev) {
850 msg_perr("Could not find a Dediprog SF100 on USB!\n");
851 return 1;
852 }
853 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000854 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000855 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000856 ret = usb_set_configuration(dediprog_handle, 1);
857 if (ret < 0) {
858 msg_perr("Could not set USB device configuration: %i %s\n",
859 ret, usb_strerror());
860 if (usb_close(dediprog_handle))
861 msg_perr("Could not close USB device!\n");
862 return 1;
863 }
864 ret = usb_claim_interface(dediprog_handle, 0);
865 if (ret < 0) {
866 msg_perr("Could not claim USB device interface %i: %i %s\n",
867 0, ret, usb_strerror());
868 if (usb_close(dediprog_handle))
869 msg_perr("Could not close USB device!\n");
870 return 1;
871 }
872 dediprog_endpoint = 2;
Nico Huber77fa67d2013-02-20 18:03:36 +0000873
David Hendricks8bb20212011-06-14 01:35:36 +0000874 if (register_shutdown(dediprog_shutdown, NULL))
875 return 1;
876
Stefan Reinauer915b8402011-01-28 09:00:15 +0000877 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
878
Nico Huber77fa67d2013-02-20 18:03:36 +0000879 /* Perform basic setup. */
880 if (dediprog_setup()) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000881 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000882 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000883 }
Nico Huber77fa67d2013-02-20 18:03:36 +0000884
885 /* After setting voltage and speed, perform setup again. */
886 if (dediprog_set_spi_voltage(0) || dediprog_set_spi_speed(spispeed_idx) || dediprog_setup()) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000887 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000888 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000889 }
Nico Huber77fa67d2013-02-20 18:03:36 +0000890
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000891 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000892 if (dediprog_set_spi_voltage(millivolt)) {
893 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000894 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000895 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000896
Michael Karcherb9dbe482011-05-11 17:07:07 +0000897 register_spi_programmer(&spi_programmer_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000898
899 /* RE leftover, leave in until the driver is complete. */
900#if 0
901 /* Execute RDID by hand if you want to test it. */
902 dediprog_do_stuff();
903#endif
904
Stefan Reinauer915b8402011-01-28 09:00:15 +0000905 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
906
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000907 return 0;
908}
909
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000910#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000911/* Leftovers from reverse engineering. Keep for documentation purposes until
912 * completely understood.
913 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000914static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000915{
916 char buf[0x4];
917 /* SPI command processing starts here. */
918
919 /* URB 12. Command Send SPI. */
920 /* URB 13. Command Receive SPI. */
921 memset(buf, 0, sizeof(buf));
922 /* JEDEC RDID */
923 msg_pdbg("Sending RDID\n");
924 buf[0] = JEDEC_RDID;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000925 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
926 (unsigned char *)buf, (unsigned char *)buf))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000927 return 1;
928 msg_pdbg("Receiving response: ");
929 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000930 /* URB 14-27 are more SPI commands. */
931 /* URB 28. Command Set SPI Voltage. */
932 if (dediprog_set_spi_voltage(0x0))
933 return 1;
934 /* URB 29-38. Command F, unsuccessful wait. */
935 if (dediprog_command_f(544))
936 return 1;
937 /* URB 39. Command Set SPI Voltage. */
938 if (dediprog_set_spi_voltage(0x10))
939 return 1;
940 /* URB 40. Command Set SPI Speed. */
941 if (dediprog_set_spi_speed(0x2))
942 return 1;
943 /* URB 41 is just URB 28. */
944 /* URB 42,44,46,48,51,53 is just URB 8. */
945 /* URB 43,45,47,49,52,54 is just URB 9. */
946 /* URB 50 is just URB 6/7. */
947 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
948 /* URB 132,134 is just URB 6/7. */
949 /* URB 133 is just URB 29-38. */
950 /* URB 135 is just URB 8. */
951 /* URB 136 is just URB 9. */
952 /* URB 137 is just URB 11. */
953
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000954 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
955 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
956 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
957 * 512 bytes, rest is filled with 0xff.
958 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000959
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000960 return 0;
961}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000962#endif