blob: a81cf8386067396f52897bf79301db50555acd93 [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>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000022#include <usb.h>
23#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000024#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000026#include "spi.h"
27
Stefan Reinauer915b8402011-01-28 09:00:15 +000028#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000029#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000030static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +000031static int dediprog_firmwareversion;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000032static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000033
Nico Hubera4b14f72012-06-19 12:06:53 +000034#define DEDI_SPI_CMD_PAGEWRITE 0x1
35#define DEDI_SPI_CMD_AAIWRITE 0x4
36
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000037#if 0
38/* Might be useful for other pieces of code as well. */
39static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000040{
41 size_t i;
42
43 for (i = 0; i < len; i++)
44 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
45}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000046#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000047
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000048/* Might be useful for other USB devices as well. static for now. */
49static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000050{
51 struct usb_bus *bus;
52 struct usb_device *dev;
53
54 for (bus = usb_get_busses(); bus; bus = bus->next)
55 for (dev = bus->devices; dev; dev = dev->next)
56 if ((dev->descriptor.idVendor == vid) &&
57 (dev->descriptor.idProduct == pid))
58 return dev;
59
60 return NULL;
61}
62
63//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
64
Stefan Reinauer915b8402011-01-28 09:00:15 +000065/* Set/clear LEDs on dediprog */
66#define PASS_ON (0 << 0)
67#define PASS_OFF (1 << 0)
68#define BUSY_ON (0 << 1)
69#define BUSY_OFF (1 << 1)
70#define ERROR_ON (0 << 2)
71#define ERROR_OFF (1 << 2)
72static int current_led_status = -1;
73
74static int dediprog_set_leds(int leds)
75{
76 int ret, target_leds;
Uwe Hermann91f4afa2011-07-28 08:13:25 +000077
Stefan Reinauer915b8402011-01-28 09:00:15 +000078 if (leds < 0 || leds > 7)
79 leds = 0; // Bogus value, enable all LEDs
80
81 if (leds == current_led_status)
82 return 0;
83
84 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
85 * two LEDs, and they were reversed. So map them around if
86 * we have an old device. On those devices the LEDs map as
87 * follows:
88 * bit 2 == 0: green light is on.
89 * bit 0 == 0: red light is on.
90 */
91 if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
92 target_leds = ((leds & ERROR_OFF) >> 2) |
93 ((leds & PASS_OFF) << 2);
94 } else {
95 target_leds = leds;
96 }
97
Uwe Hermann91f4afa2011-07-28 08:13:25 +000098 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
99 NULL, 0x0, DEFAULT_TIMEOUT);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000100 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000101 msg_perr("Command Set LED 0x%x failed (%s)!\n",
102 leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000103 return 1;
104 }
105
106 current_led_status = leds;
107
108 return 0;
109}
110
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000111static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000112{
113 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000114 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000115
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000116 switch (millivolt) {
117 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000118 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000119 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000120 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000121 case 1800:
122 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000123 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000124 case 2500:
125 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000126 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000127 case 3500:
128 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000129 break;
130 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000131 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000132 return 1;
133 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000134 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
135 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000136
Nico Huber4099a8a2012-06-16 00:02:27 +0000137 if (voltage_selector == 0) {
138 /* Wait some time as the original driver does. */
139 programmer_delay(200 * 1000);
140 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000141 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
142 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000143 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000144 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
145 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000146 return 1;
147 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000148 if (voltage_selector != 0) {
149 /* Wait some time as the original driver does. */
150 programmer_delay(200 * 1000);
151 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000152 return 0;
153}
154
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000155#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000156/* After dediprog_set_spi_speed, the original app always calls
157 * dediprog_set_spi_voltage(0) and then
158 * dediprog_check_devicestring() four times in a row.
159 * After that, dediprog_command_a() is called.
160 * This looks suspiciously like the microprocessor in the SF100 has to be
161 * restarted/reinitialized in case the speed changes.
162 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000163static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000164{
165 int ret;
166 unsigned int khz;
167
168 /* Case 1 and 2 are in weird order. Probably an organically "grown"
169 * interface.
170 * Base frequency is 24000 kHz, divisors are (in order)
171 * 1, 3, 2, 8, 11, 16, 32, 64.
172 */
173 switch (speed) {
174 case 0x0:
175 khz = 24000;
176 break;
177 case 0x1:
178 khz = 8000;
179 break;
180 case 0x2:
181 khz = 12000;
182 break;
183 case 0x3:
184 khz = 3000;
185 break;
186 case 0x4:
187 khz = 2180;
188 break;
189 case 0x5:
190 khz = 1500;
191 break;
192 case 0x6:
193 khz = 750;
194 break;
195 case 0x7:
196 khz = 375;
197 break;
198 default:
199 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
200 return 1;
201 }
202 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
203
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000204 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
205 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000206 if (ret != 0x0) {
207 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
208 return 1;
209 }
210 return 0;
211}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000212#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000213
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000214/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
215 * @start start address
216 * @len length
217 * @return 0 on success, 1 on failure
218 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000219static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000220 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000221{
222 int ret;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000223 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000224 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000225 const unsigned int chunksize = 0x200;
226 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000227 const char count_and_chunk[] = {count & 0xff,
228 (count >> 8) & 0xff,
229 chunksize & 0xff,
230 (chunksize >> 8) & 0xff};
231
232 if ((start % chunksize) || (len % chunksize)) {
233 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
234 "at flashrom@flashrom.org\n", __func__, start, len);
235 return 1;
236 }
237
238 /* No idea if the hardware can handle empty reads, so chicken out. */
239 if (!len)
240 return 0;
241 /* Command Read SPI Bulk. No idea which read command is used on the
242 * SPI side.
243 */
244 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
245 start / 0x10000, (char *)count_and_chunk,
246 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
247 if (ret != sizeof(count_and_chunk)) {
248 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
249 usb_strerror());
250 return 1;
251 }
252
253 for (i = 0; i < count; i++) {
254 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
255 (char *)buf + i * chunksize, chunksize,
256 DEFAULT_TIMEOUT);
257 if (ret != chunksize) {
258 msg_perr("SPI bulk read %i failed, expected %i, got %i "
259 "%s!\n", i, chunksize, ret, usb_strerror());
260 return 1;
261 }
262 }
263
264 return 0;
265}
266
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000267static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000268 unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000269{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000270 int ret;
271 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000272 const unsigned int chunksize = 0x200;
273 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
274 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000275
Stefan Reinauer915b8402011-01-28 09:00:15 +0000276 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
277
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000278 if (residue) {
279 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
280 start, residue);
281 ret = spi_read_chunked(flash, buf, start, residue, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000282 if (ret) {
283 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000284 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000285 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000286 }
287
288 /* Round down. */
289 bulklen = (len - residue) / chunksize * chunksize;
290 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
291 bulklen);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000292 if (ret) {
293 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000294 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000295 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000296
297 len -= residue + bulklen;
298 if (len) {
299 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
300 start, len);
301 ret = spi_read_chunked(flash, buf + residue + bulklen,
302 start + residue + bulklen, len, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000303 if (ret) {
304 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000305 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000306 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000307 }
308
Stefan Reinauer915b8402011-01-28 09:00:15 +0000309 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000310 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000311}
312
Nico Hubera4b14f72012-06-19 12:06:53 +0000313/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
314 * @chunksize length of data chunks, only 256 supported by now
315 * @start start address
316 * @len length
317 * @dedi_spi_cmd dediprog specific write command for spi bus
318 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000319 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000320static int dediprog_spi_bulk_write(struct flashctx *flash, uint8_t *buf, unsigned int chunksize,
321 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000322{
323 int ret;
324 unsigned int i;
325 /* USB transfer size must be 512, other sizes will NOT work at all.
326 * chunksize is the real data size per USB bulk transfer. The remaining
327 * space in a USB bulk transfer must be filled with 0xff padding.
328 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000329 const unsigned int count = len / chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000330 const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000331 char usbbuf[512];
332
Nico Hubera4b14f72012-06-19 12:06:53 +0000333 /*
334 * We should change this check to
335 * chunksize > 512
336 * once we know how to handle different chunk sizes.
337 */
338 if (chunksize != 256) {
339 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
340 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
341 return 1;
342 }
343
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000344 if ((start % chunksize) || (len % chunksize)) {
345 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
346 "at flashrom@flashrom.org\n", __func__, start, len);
347 return 1;
348 }
349
350 /* No idea if the hardware can handle empty writes, so chicken out. */
351 if (!len)
352 return 0;
353 /* Command Write SPI Bulk. No idea which write command is used on the
354 * SPI side.
355 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000356 ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000, start / 0x10000,
357 (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT);
358 if (ret != sizeof(count_and_cmd)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000359 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
360 usb_strerror());
361 return 1;
362 }
363
364 for (i = 0; i < count; i++) {
365 memset(usbbuf, 0xff, sizeof(usbbuf));
366 memcpy(usbbuf, buf + i * chunksize, chunksize);
367 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
368 usbbuf, 512,
369 DEFAULT_TIMEOUT);
370 if (ret != 512) {
371 msg_perr("SPI bulk write failed, expected %i, got %i "
372 "%s!\n", 512, ret, usb_strerror());
373 return 1;
374 }
375 }
376
377 return 0;
378}
379
Nico Hubera4b14f72012-06-19 12:06:53 +0000380static int dediprog_spi_write(struct flashctx *flash, uint8_t *buf,
381 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000382{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000383 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000384 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000385 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
386 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000387
388 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
389
Nico Hubera4b14f72012-06-19 12:06:53 +0000390 if (chunksize != 256) {
391 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
392 "we don't know how dediprog\nhandles them.\n");
393 /* Write everything like it was residue. */
394 residue = len;
395 }
396
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000397 if (residue) {
398 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
399 start, residue);
400 /* No idea about the real limit. Maybe 12, maybe more. */
401 ret = spi_write_chunked(flash, buf, start, residue, 12);
402 if (ret) {
403 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
404 return ret;
405 }
406 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000407
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000408 /* Round down. */
409 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000410 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000411 if (ret) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000412 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000413 return ret;
414 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000415
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000416 len -= residue + bulklen;
417 if (len) {
418 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
419 start, len);
420 ret = spi_write_chunked(flash, buf + residue + bulklen,
421 start + residue + bulklen, len, 12);
422 if (ret) {
423 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
424 return ret;
425 }
426 }
427
428 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
429 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000430}
431
Nico Hubera4b14f72012-06-19 12:06:53 +0000432static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
433{
434 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_PAGEWRITE);
435}
436
437static int dediprog_spi_write_aai(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_AAIWRITE);
440}
441
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000442static int dediprog_spi_send_command(struct flashctx *flash,
443 unsigned int writecnt,
444 unsigned int readcnt,
445 const unsigned char *writearr,
446 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000447{
448 int ret;
449
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000450 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000451 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000452 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000453 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000454 return 1;
455 }
456 /* 16 byte reads should work. */
457 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000458 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000459 return 1;
460 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000461
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000462 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
463 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
464 DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000465 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000466 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
467 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000468 return 1;
469 }
470 if (!readcnt)
471 return 0;
472 memset(readarr, 0, readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000473 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
474 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000475 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000476 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
477 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000478 return 1;
479 }
480 return 0;
481}
482
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000483static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000484{
485 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000486 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000487 char buf[0x11];
488
489 /* Command Prepare Receive Device String. */
490 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000491 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
492 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000493 /* The char casting is needed to stop gcc complaining about an always true comparison. */
494 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
495 msg_perr("Unexpected response to Command Prepare Receive Device"
496 " String!\n");
497 return 1;
498 }
499 /* Command Receive Device String. */
500 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000501 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
502 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000503 if (ret != 0x10) {
504 msg_perr("Incomplete/failed Command Receive Device String!\n");
505 return 1;
506 }
507 buf[0x10] = '\0';
508 msg_pdbg("Found a %s\n", buf);
509 if (memcmp(buf, "SF100", 0x5)) {
510 msg_perr("Device not a SF100!\n");
511 return 1;
512 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000513 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
514 msg_perr("Unexpected firmware version string!\n");
515 return 1;
516 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000517 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000518 if (fw[0] < 2 || fw[0] > 5) {
519 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
520 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000521 return 1;
522 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000523 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000524 return 0;
525}
526
527/* Command A seems to be some sort of device init. It is either followed by
528 * dediprog_check_devicestring (often) or Command A (often) or
529 * Command F (once).
530 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000531static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000532{
533 int ret;
534 char buf[0x1];
535
536 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000537 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
538 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000539 if (ret < 0) {
540 msg_perr("Command A failed (%s)!\n", usb_strerror());
541 return 1;
542 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000543 if ((ret != 0x1) || (buf[0] != 0x6f)) {
544 msg_perr("Unexpected response to Command A!\n");
545 return 1;
546 }
547 return 0;
548}
549
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000550#if 0
551/* Something.
552 * Present in eng_detect_blink.log with firmware 3.1.8
553 * Always preceded by Command Receive Device String
554 */
555static int dediprog_command_b(void)
556{
557 int ret;
558 char buf[0x3];
559
560 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000561 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
562 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000563 if (ret < 0) {
564 msg_perr("Command B failed (%s)!\n", usb_strerror());
565 return 1;
566 }
567 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
568 (buf[2] != 0xff)) {
569 msg_perr("Unexpected response to Command B!\n");
570 return 1;
571 }
572
573 return 0;
574}
575#endif
576
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000577/* Command C is only sent after dediprog_check_devicestring, but not after every
578 * invocation of dediprog_check_devicestring. It is only sent after the first
579 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
580 * I'm tempted to call this one start_SPI_engine or finish_init.
581 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000582static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000583{
584 int ret;
585
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000586 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
587 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000588 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000589 msg_perr("Command C failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000590 return 1;
591 }
592 return 0;
593}
594
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000595#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000596/* Very strange. Seems to be a programmer keepalive or somesuch.
597 * Wait unsuccessfully for timeout ms to read one byte.
598 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000599 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000600 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000601static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000602{
603 int ret;
604 char buf[0x1];
605
606 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000607 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
608 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000609 /* This check is most probably wrong. Command F always causes a timeout
610 * in the logs, so we should check for timeout instead of checking for
611 * success.
612 */
613 if (ret != 0x1) {
614 msg_perr("Command F failed (%s)!\n", usb_strerror());
615 return 1;
616 }
617 return 0;
618}
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000619
620/* Start/stop blinking?
621 * Present in eng_detect_blink.log with firmware 3.1.8
622 * Preceded by Command J
623 */
624static int dediprog_command_g(void)
625{
626 int ret;
627
628 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
629 if (ret != 0x0) {
630 msg_perr("Command G failed (%s)!\n", usb_strerror());
631 return 1;
632 }
633 return 0;
634}
635
636/* Something.
637 * Present in all logs with firmware 5.1.5
638 * Always preceded by Command Receive Device String
639 * Always followed by Command Set SPI Voltage nonzero
640 */
641static int dediprog_command_h(void)
642{
643 int ret;
644
645 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
646 if (ret != 0x0) {
647 msg_perr("Command H failed (%s)!\n", usb_strerror());
648 return 1;
649 }
650 return 0;
651}
652
653/* Shutdown for firmware 5.x?
654 * Present in all logs with firmware 5.1.5
655 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
656 * Always followed by Command Set SPI Voltage 0x0000
657 */
658static int dediprog_command_i(void)
659{
660 int ret;
661
662 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
663 if (ret != 0x0) {
664 msg_perr("Command I failed (%s)!\n", usb_strerror());
665 return 1;
666 }
667 return 0;
668}
669
670/* Start/stop blinking?
671 * Present in all logs with firmware 5.1.5
672 * Always preceded by Command Receive Device String on 5.1.5
673 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
674 * Present in eng_detect_blink.log with firmware 3.1.8
675 * Preceded by Command B in eng_detect_blink.log
676 * Followed by Command G in eng_detect_blink.log
677 */
678static int dediprog_command_j(void)
679{
680 int ret;
681
682 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
683 if (ret != 0x0) {
684 msg_perr("Command J failed (%s)!\n", usb_strerror());
685 return 1;
686 }
687 return 0;
688}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000689#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000690
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000691static int parse_voltage(char *voltage)
692{
693 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000694 int i;
695 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000696
697 if (!voltage || !strlen(voltage)) {
698 msg_perr("Empty voltage= specified.\n");
699 return -1;
700 }
701 millivolt = (int)strtol(voltage, &tmp, 0);
702 voltage = tmp;
703 /* Handle "," and "." as decimal point. Everything after it is assumed
704 * to be in decimal notation.
705 */
706 if ((*voltage == '.') || (*voltage == ',')) {
707 voltage++;
708 for (i = 0; i < 3; i++) {
709 fraction *= 10;
710 /* Don't advance if the current character is invalid,
711 * but continue multiplying.
712 */
713 if ((*voltage < '0') || (*voltage > '9'))
714 continue;
715 fraction += *voltage - '0';
716 voltage++;
717 }
718 /* Throw away remaining digits. */
719 voltage += strspn(voltage, "0123456789");
720 }
721 /* The remaining string must be empty or "mV" or "V". */
722 tolower_string(voltage);
723
724 /* No unit or "V". */
725 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
726 millivolt *= 1000;
727 millivolt += fraction;
728 } else if (!strncmp(voltage, "mv", 2) ||
729 !strncmp(voltage, "milliv", 6)) {
730 /* No adjustment. fraction is discarded. */
731 } else {
732 /* Garbage at the end of the string. */
733 msg_perr("Garbage voltage= specified.\n");
734 return -1;
735 }
736 return millivolt;
737}
738
Michael Karcherb9dbe482011-05-11 17:07:07 +0000739static const struct spi_programmer spi_programmer_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000740 .type = SPI_CONTROLLER_DEDIPROG,
741 .max_data_read = MAX_DATA_UNSPECIFIED,
742 .max_data_write = MAX_DATA_UNSPECIFIED,
743 .command = dediprog_spi_send_command,
744 .multicommand = default_spi_send_multicommand,
745 .read = dediprog_spi_read,
746 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000747 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000748};
749
David Hendricks8bb20212011-06-14 01:35:36 +0000750static int dediprog_shutdown(void *data)
751{
752 msg_pspew("%s\n", __func__);
753
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000754#if 0
755 /* Shutdown on firmware 5.x */
756 if (dediprog_firmwareversion == 5)
757 if (dediprog_command_i())
758 return 1;
759#endif
760
David Hendricks8bb20212011-06-14 01:35:36 +0000761 /* URB 28. Command Set SPI Voltage to 0. */
762 if (dediprog_set_spi_voltage(0x0))
763 return 1;
764
765 if (usb_release_interface(dediprog_handle, 0)) {
766 msg_perr("Could not release USB interface!\n");
767 return 1;
768 }
769 if (usb_close(dediprog_handle)) {
770 msg_perr("Could not close USB device!\n");
771 return 1;
772 }
773 return 0;
774}
775
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000776/* URB numbers refer to the first log ever captured. */
777int dediprog_init(void)
778{
779 struct usb_device *dev;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000780 char *voltage;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000781 int millivolt = 3500;
782 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000783
784 msg_pspew("%s\n", __func__);
785
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000786 voltage = extract_programmer_param("voltage");
787 if (voltage) {
788 millivolt = parse_voltage(voltage);
789 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000790 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000791 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000792 msg_pinfo("Setting voltage to %i mV\n", millivolt);
793 }
794
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000795 /* Here comes the USB stuff. */
796 usb_init();
797 usb_find_busses();
798 usb_find_devices();
799 dev = get_device_by_vid_pid(0x0483, 0xdada);
800 if (!dev) {
801 msg_perr("Could not find a Dediprog SF100 on USB!\n");
802 return 1;
803 }
804 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000805 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000806 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000807 ret = usb_set_configuration(dediprog_handle, 1);
808 if (ret < 0) {
809 msg_perr("Could not set USB device configuration: %i %s\n",
810 ret, usb_strerror());
811 if (usb_close(dediprog_handle))
812 msg_perr("Could not close USB device!\n");
813 return 1;
814 }
815 ret = usb_claim_interface(dediprog_handle, 0);
816 if (ret < 0) {
817 msg_perr("Could not claim USB device interface %i: %i %s\n",
818 0, ret, usb_strerror());
819 if (usb_close(dediprog_handle))
820 msg_perr("Could not close USB device!\n");
821 return 1;
822 }
823 dediprog_endpoint = 2;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000824
David Hendricks8bb20212011-06-14 01:35:36 +0000825 if (register_shutdown(dediprog_shutdown, NULL))
826 return 1;
827
Stefan Reinauer915b8402011-01-28 09:00:15 +0000828 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
829
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000830 /* URB 6. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000831 if (dediprog_command_a()) {
832 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000833 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000834 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000835 /* URB 7. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000836 if (dediprog_command_a()) {
837 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000838 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000839 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000840 /* URB 8. Command Prepare Receive Device String. */
841 /* URB 9. Command Receive Device String. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000842 if (dediprog_check_devicestring()) {
843 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000844 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000845 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000846 /* URB 10. Command C. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000847 if (dediprog_command_c()) {
848 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000849 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000850 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000851 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000852 if (dediprog_set_spi_voltage(millivolt)) {
853 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000854 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000855 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000856
Michael Karcherb9dbe482011-05-11 17:07:07 +0000857 register_spi_programmer(&spi_programmer_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000858
859 /* RE leftover, leave in until the driver is complete. */
860#if 0
861 /* Execute RDID by hand if you want to test it. */
862 dediprog_do_stuff();
863#endif
864
Stefan Reinauer915b8402011-01-28 09:00:15 +0000865 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
866
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000867 return 0;
868}
869
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000870#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000871/* Leftovers from reverse engineering. Keep for documentation purposes until
872 * completely understood.
873 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000874static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000875{
876 char buf[0x4];
877 /* SPI command processing starts here. */
878
879 /* URB 12. Command Send SPI. */
880 /* URB 13. Command Receive SPI. */
881 memset(buf, 0, sizeof(buf));
882 /* JEDEC RDID */
883 msg_pdbg("Sending RDID\n");
884 buf[0] = JEDEC_RDID;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000885 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
886 (unsigned char *)buf, (unsigned char *)buf))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000887 return 1;
888 msg_pdbg("Receiving response: ");
889 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000890 /* URB 14-27 are more SPI commands. */
891 /* URB 28. Command Set SPI Voltage. */
892 if (dediprog_set_spi_voltage(0x0))
893 return 1;
894 /* URB 29-38. Command F, unsuccessful wait. */
895 if (dediprog_command_f(544))
896 return 1;
897 /* URB 39. Command Set SPI Voltage. */
898 if (dediprog_set_spi_voltage(0x10))
899 return 1;
900 /* URB 40. Command Set SPI Speed. */
901 if (dediprog_set_spi_speed(0x2))
902 return 1;
903 /* URB 41 is just URB 28. */
904 /* URB 42,44,46,48,51,53 is just URB 8. */
905 /* URB 43,45,47,49,52,54 is just URB 9. */
906 /* URB 50 is just URB 6/7. */
907 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
908 /* URB 132,134 is just URB 6/7. */
909 /* URB 133 is just URB 29-38. */
910 /* URB 135 is just URB 8. */
911 /* URB 136 is just URB 9. */
912 /* URB 137 is just URB 11. */
913
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000914 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
915 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
916 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
917 * 512 bytes, rest is filled with 0xff.
918 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000919
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000920 return 0;
921}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000922#endif