blob: c2b66ccc5f0fa234c749eb6fa13ad3ca4d57ee14 [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>
Nathan Laredo21541a62012-12-24 22:07:36 +000022#include <errno.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000023#include <usb.h>
24#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000025#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000026#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000027#include "spi.h"
28
Stefan Reinauer915b8402011-01-28 09:00:15 +000029#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000030#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000031static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +000032static int dediprog_firmwareversion;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000033static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000034
Nico Hubera4b14f72012-06-19 12:06:53 +000035#define DEDI_SPI_CMD_PAGEWRITE 0x1
36#define DEDI_SPI_CMD_AAIWRITE 0x4
37
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000038#if 0
39/* Might be useful for other pieces of code as well. */
40static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000041{
42 size_t i;
43
44 for (i = 0; i < len; i++)
45 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
46}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000047#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000048
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000049/* Might be useful for other USB devices as well. static for now. */
Nathan Laredo21541a62012-12-24 22:07:36 +000050/* device parameter allows user to specify one device of multiple installed */
51static 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 +000052{
53 struct usb_bus *bus;
54 struct usb_device *dev;
55
56 for (bus = usb_get_busses(); bus; bus = bus->next)
57 for (dev = bus->devices; dev; dev = dev->next)
58 if ((dev->descriptor.idVendor == vid) &&
Nathan Laredo21541a62012-12-24 22:07:36 +000059 (dev->descriptor.idProduct == pid)) {
60 if (device == 0)
61 return dev;
62 device--;
63 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000064
65 return NULL;
66}
67
68//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
69
Stefan Reinauer915b8402011-01-28 09:00:15 +000070/* Set/clear LEDs on dediprog */
71#define PASS_ON (0 << 0)
72#define PASS_OFF (1 << 0)
73#define BUSY_ON (0 << 1)
74#define BUSY_OFF (1 << 1)
75#define ERROR_ON (0 << 2)
76#define ERROR_OFF (1 << 2)
77static int current_led_status = -1;
78
79static int dediprog_set_leds(int leds)
80{
81 int ret, target_leds;
Uwe Hermann91f4afa2011-07-28 08:13:25 +000082
Stefan Reinauer915b8402011-01-28 09:00:15 +000083 if (leds < 0 || leds > 7)
84 leds = 0; // Bogus value, enable all LEDs
85
86 if (leds == current_led_status)
87 return 0;
88
89 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
90 * two LEDs, and they were reversed. So map them around if
91 * we have an old device. On those devices the LEDs map as
92 * follows:
93 * bit 2 == 0: green light is on.
94 * bit 0 == 0: red light is on.
95 */
96 if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
97 target_leds = ((leds & ERROR_OFF) >> 2) |
98 ((leds & PASS_OFF) << 2);
99 } else {
100 target_leds = leds;
101 }
102
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000103 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
104 NULL, 0x0, DEFAULT_TIMEOUT);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000105 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000106 msg_perr("Command Set LED 0x%x failed (%s)!\n",
107 leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000108 return 1;
109 }
110
111 current_led_status = leds;
112
113 return 0;
114}
115
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000116static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000117{
118 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000119 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000120
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000121 switch (millivolt) {
122 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000123 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000124 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000125 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000126 case 1800:
127 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000128 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000129 case 2500:
130 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000131 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000132 case 3500:
133 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000134 break;
135 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000136 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000137 return 1;
138 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000139 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
140 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000141
Nico Huber4099a8a2012-06-16 00:02:27 +0000142 if (voltage_selector == 0) {
143 /* Wait some time as the original driver does. */
144 programmer_delay(200 * 1000);
145 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000146 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
147 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000148 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000149 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
150 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000151 return 1;
152 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000153 if (voltage_selector != 0) {
154 /* Wait some time as the original driver does. */
155 programmer_delay(200 * 1000);
156 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000157 return 0;
158}
159
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000160#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000161/* After dediprog_set_spi_speed, the original app always calls
162 * dediprog_set_spi_voltage(0) and then
163 * dediprog_check_devicestring() four times in a row.
164 * After that, dediprog_command_a() is called.
165 * This looks suspiciously like the microprocessor in the SF100 has to be
166 * restarted/reinitialized in case the speed changes.
167 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000168static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000169{
170 int ret;
171 unsigned int khz;
172
173 /* Case 1 and 2 are in weird order. Probably an organically "grown"
174 * interface.
175 * Base frequency is 24000 kHz, divisors are (in order)
176 * 1, 3, 2, 8, 11, 16, 32, 64.
177 */
178 switch (speed) {
179 case 0x0:
180 khz = 24000;
181 break;
182 case 0x1:
183 khz = 8000;
184 break;
185 case 0x2:
186 khz = 12000;
187 break;
188 case 0x3:
189 khz = 3000;
190 break;
191 case 0x4:
192 khz = 2180;
193 break;
194 case 0x5:
195 khz = 1500;
196 break;
197 case 0x6:
198 khz = 750;
199 break;
200 case 0x7:
201 khz = 375;
202 break;
203 default:
204 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
205 return 1;
206 }
207 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
208
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000209 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
210 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000211 if (ret != 0x0) {
212 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
213 return 1;
214 }
215 return 0;
216}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000217#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000218
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000219/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
220 * @start start address
221 * @len length
222 * @return 0 on success, 1 on failure
223 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000224static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000225 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000226{
227 int ret;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000228 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000229 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000230 const unsigned int chunksize = 0x200;
231 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000232 const char count_and_chunk[] = {count & 0xff,
233 (count >> 8) & 0xff,
234 chunksize & 0xff,
235 (chunksize >> 8) & 0xff};
236
237 if ((start % chunksize) || (len % chunksize)) {
238 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
239 "at flashrom@flashrom.org\n", __func__, start, len);
240 return 1;
241 }
242
243 /* No idea if the hardware can handle empty reads, so chicken out. */
244 if (!len)
245 return 0;
246 /* Command Read SPI Bulk. No idea which read command is used on the
247 * SPI side.
248 */
249 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
250 start / 0x10000, (char *)count_and_chunk,
251 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
252 if (ret != sizeof(count_and_chunk)) {
253 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
254 usb_strerror());
255 return 1;
256 }
257
258 for (i = 0; i < count; i++) {
259 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
260 (char *)buf + i * chunksize, chunksize,
261 DEFAULT_TIMEOUT);
262 if (ret != chunksize) {
263 msg_perr("SPI bulk read %i failed, expected %i, got %i "
264 "%s!\n", i, chunksize, ret, usb_strerror());
265 return 1;
266 }
267 }
268
269 return 0;
270}
271
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000272static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000273 unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000274{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000275 int ret;
276 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000277 const unsigned int chunksize = 0x200;
278 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
279 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000280
Stefan Reinauer915b8402011-01-28 09:00:15 +0000281 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
282
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000283 if (residue) {
284 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
285 start, residue);
286 ret = spi_read_chunked(flash, buf, start, residue, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000287 if (ret) {
288 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000289 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000290 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000291 }
292
293 /* Round down. */
294 bulklen = (len - residue) / chunksize * chunksize;
295 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
296 bulklen);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000297 if (ret) {
298 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000299 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000300 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000301
302 len -= residue + bulklen;
303 if (len) {
304 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
305 start, len);
306 ret = spi_read_chunked(flash, buf + residue + bulklen,
307 start + residue + bulklen, len, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000308 if (ret) {
309 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000310 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000311 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000312 }
313
Stefan Reinauer915b8402011-01-28 09:00:15 +0000314 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000315 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000316}
317
Nico Hubera4b14f72012-06-19 12:06:53 +0000318/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
319 * @chunksize length of data chunks, only 256 supported by now
320 * @start start address
321 * @len length
322 * @dedi_spi_cmd dediprog specific write command for spi bus
323 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000324 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000325static int dediprog_spi_bulk_write(struct flashctx *flash, uint8_t *buf, unsigned int chunksize,
326 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000327{
328 int ret;
329 unsigned int i;
330 /* USB transfer size must be 512, other sizes will NOT work at all.
331 * chunksize is the real data size per USB bulk transfer. The remaining
332 * space in a USB bulk transfer must be filled with 0xff padding.
333 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000334 const unsigned int count = len / chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000335 const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000336 char usbbuf[512];
337
Nico Hubera4b14f72012-06-19 12:06:53 +0000338 /*
339 * We should change this check to
340 * chunksize > 512
341 * once we know how to handle different chunk sizes.
342 */
343 if (chunksize != 256) {
344 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
345 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
346 return 1;
347 }
348
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000349 if ((start % chunksize) || (len % chunksize)) {
350 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
351 "at flashrom@flashrom.org\n", __func__, start, len);
352 return 1;
353 }
354
355 /* No idea if the hardware can handle empty writes, so chicken out. */
356 if (!len)
357 return 0;
358 /* Command Write SPI Bulk. No idea which write command is used on the
359 * SPI side.
360 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000361 ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000, start / 0x10000,
362 (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT);
363 if (ret != sizeof(count_and_cmd)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000364 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
365 usb_strerror());
366 return 1;
367 }
368
369 for (i = 0; i < count; i++) {
370 memset(usbbuf, 0xff, sizeof(usbbuf));
371 memcpy(usbbuf, buf + i * chunksize, chunksize);
372 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
373 usbbuf, 512,
374 DEFAULT_TIMEOUT);
375 if (ret != 512) {
376 msg_perr("SPI bulk write failed, expected %i, got %i "
377 "%s!\n", 512, ret, usb_strerror());
378 return 1;
379 }
380 }
381
382 return 0;
383}
384
Nico Hubera4b14f72012-06-19 12:06:53 +0000385static int dediprog_spi_write(struct flashctx *flash, uint8_t *buf,
386 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000387{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000388 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000389 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000390 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
391 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000392
393 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
394
Nico Hubera4b14f72012-06-19 12:06:53 +0000395 if (chunksize != 256) {
396 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
397 "we don't know how dediprog\nhandles them.\n");
398 /* Write everything like it was residue. */
399 residue = len;
400 }
401
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000402 if (residue) {
403 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
404 start, residue);
405 /* No idea about the real limit. Maybe 12, maybe more. */
406 ret = spi_write_chunked(flash, buf, start, residue, 12);
407 if (ret) {
408 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
409 return ret;
410 }
411 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000412
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000413 /* Round down. */
414 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000415 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000416 if (ret) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000417 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000418 return ret;
419 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000420
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000421 len -= residue + bulklen;
422 if (len) {
423 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
424 start, len);
425 ret = spi_write_chunked(flash, buf + residue + bulklen,
426 start + residue + bulklen, len, 12);
427 if (ret) {
428 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
429 return ret;
430 }
431 }
432
433 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
434 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000435}
436
Nico Hubera4b14f72012-06-19 12:06:53 +0000437static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
438{
439 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_PAGEWRITE);
440}
441
442static int dediprog_spi_write_aai(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
443{
444 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_AAIWRITE);
445}
446
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000447static int dediprog_spi_send_command(struct flashctx *flash,
448 unsigned int writecnt,
449 unsigned int readcnt,
450 const unsigned char *writearr,
451 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000452{
453 int ret;
454
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000455 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000456 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000457 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000458 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000459 return 1;
460 }
461 /* 16 byte reads should work. */
462 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000463 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000464 return 1;
465 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000466
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000467 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
468 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
469 DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000470 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000471 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
472 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000473 return 1;
474 }
475 if (!readcnt)
476 return 0;
477 memset(readarr, 0, readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000478 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
479 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000480 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000481 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
482 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000483 return 1;
484 }
485 return 0;
486}
487
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000488static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000489{
490 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000491 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000492 char buf[0x11];
493
494 /* Command Prepare Receive Device String. */
495 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000496 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
497 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000498 /* The char casting is needed to stop gcc complaining about an always true comparison. */
499 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
500 msg_perr("Unexpected response to Command Prepare Receive Device"
501 " String!\n");
502 return 1;
503 }
504 /* Command Receive Device String. */
505 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000506 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
507 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000508 if (ret != 0x10) {
509 msg_perr("Incomplete/failed Command Receive Device String!\n");
510 return 1;
511 }
512 buf[0x10] = '\0';
513 msg_pdbg("Found a %s\n", buf);
514 if (memcmp(buf, "SF100", 0x5)) {
515 msg_perr("Device not a SF100!\n");
516 return 1;
517 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000518 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
519 msg_perr("Unexpected firmware version string!\n");
520 return 1;
521 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000522 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000523 if (fw[0] < 2 || fw[0] > 5) {
524 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
525 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000526 return 1;
527 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000528 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000529 return 0;
530}
531
532/* Command A seems to be some sort of device init. It is either followed by
533 * dediprog_check_devicestring (often) or Command A (often) or
534 * Command F (once).
535 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000536static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000537{
538 int ret;
539 char buf[0x1];
540
541 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000542 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
543 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000544 if (ret < 0) {
545 msg_perr("Command A failed (%s)!\n", usb_strerror());
546 return 1;
547 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000548 if ((ret != 0x1) || (buf[0] != 0x6f)) {
549 msg_perr("Unexpected response to Command A!\n");
550 return 1;
551 }
552 return 0;
553}
554
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000555#if 0
556/* Something.
557 * Present in eng_detect_blink.log with firmware 3.1.8
558 * Always preceded by Command Receive Device String
559 */
560static int dediprog_command_b(void)
561{
562 int ret;
563 char buf[0x3];
564
565 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000566 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
567 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000568 if (ret < 0) {
569 msg_perr("Command B failed (%s)!\n", usb_strerror());
570 return 1;
571 }
572 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
573 (buf[2] != 0xff)) {
574 msg_perr("Unexpected response to Command B!\n");
575 return 1;
576 }
577
578 return 0;
579}
580#endif
581
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000582/* Command C is only sent after dediprog_check_devicestring, but not after every
583 * invocation of dediprog_check_devicestring. It is only sent after the first
584 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
585 * I'm tempted to call this one start_SPI_engine or finish_init.
586 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000587static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000588{
589 int ret;
590
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000591 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
592 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000593 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000594 msg_perr("Command C failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000595 return 1;
596 }
597 return 0;
598}
599
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000600#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000601/* Very strange. Seems to be a programmer keepalive or somesuch.
602 * Wait unsuccessfully for timeout ms to read one byte.
603 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000604 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000605 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000606static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000607{
608 int ret;
609 char buf[0x1];
610
611 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000612 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
613 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000614 /* This check is most probably wrong. Command F always causes a timeout
615 * in the logs, so we should check for timeout instead of checking for
616 * success.
617 */
618 if (ret != 0x1) {
619 msg_perr("Command F failed (%s)!\n", usb_strerror());
620 return 1;
621 }
622 return 0;
623}
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000624
625/* Start/stop blinking?
626 * Present in eng_detect_blink.log with firmware 3.1.8
627 * Preceded by Command J
628 */
629static int dediprog_command_g(void)
630{
631 int ret;
632
633 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
634 if (ret != 0x0) {
635 msg_perr("Command G failed (%s)!\n", usb_strerror());
636 return 1;
637 }
638 return 0;
639}
640
641/* Something.
642 * Present in all logs with firmware 5.1.5
643 * Always preceded by Command Receive Device String
644 * Always followed by Command Set SPI Voltage nonzero
645 */
646static int dediprog_command_h(void)
647{
648 int ret;
649
650 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
651 if (ret != 0x0) {
652 msg_perr("Command H failed (%s)!\n", usb_strerror());
653 return 1;
654 }
655 return 0;
656}
657
658/* Shutdown for firmware 5.x?
659 * Present in all logs with firmware 5.1.5
660 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
661 * Always followed by Command Set SPI Voltage 0x0000
662 */
663static int dediprog_command_i(void)
664{
665 int ret;
666
667 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
668 if (ret != 0x0) {
669 msg_perr("Command I failed (%s)!\n", usb_strerror());
670 return 1;
671 }
672 return 0;
673}
674
675/* Start/stop blinking?
676 * Present in all logs with firmware 5.1.5
677 * Always preceded by Command Receive Device String on 5.1.5
678 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
679 * Present in eng_detect_blink.log with firmware 3.1.8
680 * Preceded by Command B in eng_detect_blink.log
681 * Followed by Command G in eng_detect_blink.log
682 */
683static int dediprog_command_j(void)
684{
685 int ret;
686
687 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
688 if (ret != 0x0) {
689 msg_perr("Command J failed (%s)!\n", usb_strerror());
690 return 1;
691 }
692 return 0;
693}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000694#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000695
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000696static int parse_voltage(char *voltage)
697{
698 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000699 int i;
700 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000701
702 if (!voltage || !strlen(voltage)) {
703 msg_perr("Empty voltage= specified.\n");
704 return -1;
705 }
706 millivolt = (int)strtol(voltage, &tmp, 0);
707 voltage = tmp;
708 /* Handle "," and "." as decimal point. Everything after it is assumed
709 * to be in decimal notation.
710 */
711 if ((*voltage == '.') || (*voltage == ',')) {
712 voltage++;
713 for (i = 0; i < 3; i++) {
714 fraction *= 10;
715 /* Don't advance if the current character is invalid,
716 * but continue multiplying.
717 */
718 if ((*voltage < '0') || (*voltage > '9'))
719 continue;
720 fraction += *voltage - '0';
721 voltage++;
722 }
723 /* Throw away remaining digits. */
724 voltage += strspn(voltage, "0123456789");
725 }
726 /* The remaining string must be empty or "mV" or "V". */
727 tolower_string(voltage);
728
729 /* No unit or "V". */
730 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
731 millivolt *= 1000;
732 millivolt += fraction;
733 } else if (!strncmp(voltage, "mv", 2) ||
734 !strncmp(voltage, "milliv", 6)) {
735 /* No adjustment. fraction is discarded. */
736 } else {
737 /* Garbage at the end of the string. */
738 msg_perr("Garbage voltage= specified.\n");
739 return -1;
740 }
741 return millivolt;
742}
743
Michael Karcherb9dbe482011-05-11 17:07:07 +0000744static const struct spi_programmer spi_programmer_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000745 .type = SPI_CONTROLLER_DEDIPROG,
746 .max_data_read = MAX_DATA_UNSPECIFIED,
747 .max_data_write = MAX_DATA_UNSPECIFIED,
748 .command = dediprog_spi_send_command,
749 .multicommand = default_spi_send_multicommand,
750 .read = dediprog_spi_read,
751 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000752 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000753};
754
David Hendricks8bb20212011-06-14 01:35:36 +0000755static int dediprog_shutdown(void *data)
756{
757 msg_pspew("%s\n", __func__);
758
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000759#if 0
760 /* Shutdown on firmware 5.x */
761 if (dediprog_firmwareversion == 5)
762 if (dediprog_command_i())
763 return 1;
764#endif
765
David Hendricks8bb20212011-06-14 01:35:36 +0000766 /* URB 28. Command Set SPI Voltage to 0. */
767 if (dediprog_set_spi_voltage(0x0))
768 return 1;
769
770 if (usb_release_interface(dediprog_handle, 0)) {
771 msg_perr("Could not release USB interface!\n");
772 return 1;
773 }
774 if (usb_close(dediprog_handle)) {
775 msg_perr("Could not close USB device!\n");
776 return 1;
777 }
778 return 0;
779}
780
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000781/* URB numbers refer to the first log ever captured. */
782int dediprog_init(void)
783{
784 struct usb_device *dev;
Nathan Laredo21541a62012-12-24 22:07:36 +0000785 char *voltage, *device;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000786 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000787 long usedevice = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000788 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000789
790 msg_pspew("%s\n", __func__);
791
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000792 voltage = extract_programmer_param("voltage");
793 if (voltage) {
794 millivolt = parse_voltage(voltage);
795 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000796 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000797 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000798 msg_pinfo("Setting voltage to %i mV\n", millivolt);
799 }
800
Nathan Laredo21541a62012-12-24 22:07:36 +0000801 device = extract_programmer_param("device");
802 if (device) {
803 char *dev_suffix;
804 errno = 0;
805 usedevice = strtol(device, &dev_suffix, 10);
806 if (errno != 0 || device == dev_suffix) {
807 msg_perr("Error: Could not convert 'device'.\n");
808 free(device);
809 return 1;
810 }
811 if (usedevice < 0 || usedevice > UINT_MAX) {
812 msg_perr("Error: Value for 'device' is out of range.\n");
813 free(device);
814 return 1;
815 }
816 if (strlen(dev_suffix) > 0) {
817 msg_perr("Error: Garbage following 'device' value.\n");
818 free(device);
819 return 1;
820 }
821 msg_pinfo("Using device %li.\n", usedevice);
822 }
823 free(device);
824
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000825 /* Here comes the USB stuff. */
826 usb_init();
827 usb_find_busses();
828 usb_find_devices();
Nathan Laredo21541a62012-12-24 22:07:36 +0000829 dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000830 if (!dev) {
831 msg_perr("Could not find a Dediprog SF100 on USB!\n");
832 return 1;
833 }
834 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000835 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000836 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000837 ret = usb_set_configuration(dediprog_handle, 1);
838 if (ret < 0) {
839 msg_perr("Could not set USB device configuration: %i %s\n",
840 ret, usb_strerror());
841 if (usb_close(dediprog_handle))
842 msg_perr("Could not close USB device!\n");
843 return 1;
844 }
845 ret = usb_claim_interface(dediprog_handle, 0);
846 if (ret < 0) {
847 msg_perr("Could not claim USB device interface %i: %i %s\n",
848 0, ret, usb_strerror());
849 if (usb_close(dediprog_handle))
850 msg_perr("Could not close USB device!\n");
851 return 1;
852 }
853 dediprog_endpoint = 2;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000854
David Hendricks8bb20212011-06-14 01:35:36 +0000855 if (register_shutdown(dediprog_shutdown, NULL))
856 return 1;
857
Stefan Reinauer915b8402011-01-28 09:00:15 +0000858 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
859
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000860 /* URB 6. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000861 if (dediprog_command_a()) {
862 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000863 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000864 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000865 /* URB 7. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000866 if (dediprog_command_a()) {
867 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000868 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000869 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000870 /* URB 8. Command Prepare Receive Device String. */
871 /* URB 9. Command Receive Device String. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000872 if (dediprog_check_devicestring()) {
873 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000874 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000875 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000876 /* URB 10. Command C. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000877 if (dediprog_command_c()) {
878 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000879 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000880 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000881 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000882 if (dediprog_set_spi_voltage(millivolt)) {
883 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000884 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000885 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000886
Michael Karcherb9dbe482011-05-11 17:07:07 +0000887 register_spi_programmer(&spi_programmer_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000888
889 /* RE leftover, leave in until the driver is complete. */
890#if 0
891 /* Execute RDID by hand if you want to test it. */
892 dediprog_do_stuff();
893#endif
894
Stefan Reinauer915b8402011-01-28 09:00:15 +0000895 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
896
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000897 return 0;
898}
899
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000900#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000901/* Leftovers from reverse engineering. Keep for documentation purposes until
902 * completely understood.
903 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000904static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000905{
906 char buf[0x4];
907 /* SPI command processing starts here. */
908
909 /* URB 12. Command Send SPI. */
910 /* URB 13. Command Receive SPI. */
911 memset(buf, 0, sizeof(buf));
912 /* JEDEC RDID */
913 msg_pdbg("Sending RDID\n");
914 buf[0] = JEDEC_RDID;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000915 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
916 (unsigned char *)buf, (unsigned char *)buf))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000917 return 1;
918 msg_pdbg("Receiving response: ");
919 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000920 /* URB 14-27 are more SPI commands. */
921 /* URB 28. Command Set SPI Voltage. */
922 if (dediprog_set_spi_voltage(0x0))
923 return 1;
924 /* URB 29-38. Command F, unsuccessful wait. */
925 if (dediprog_command_f(544))
926 return 1;
927 /* URB 39. Command Set SPI Voltage. */
928 if (dediprog_set_spi_voltage(0x10))
929 return 1;
930 /* URB 40. Command Set SPI Speed. */
931 if (dediprog_set_spi_speed(0x2))
932 return 1;
933 /* URB 41 is just URB 28. */
934 /* URB 42,44,46,48,51,53 is just URB 8. */
935 /* URB 43,45,47,49,52,54 is just URB 9. */
936 /* URB 50 is just URB 6/7. */
937 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
938 /* URB 132,134 is just URB 6/7. */
939 /* URB 133 is just URB 29-38. */
940 /* URB 135 is just URB 8. */
941 /* URB 136 is just URB 9. */
942 /* URB 137 is just URB 11. */
943
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000944 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
945 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
946 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
947 * 512 bytes, rest is filled with 0xff.
948 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000949
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000950 return 0;
951}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000952#endif