hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 21 | #include <string.h> |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 22 | #include <usb.h> |
| 23 | #include "flash.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 24 | #include "chipdrivers.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 25 | #include "programmer.h" |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 26 | #include "spi.h" |
| 27 | |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 28 | #define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 29 | #define DEFAULT_TIMEOUT 3000 |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 30 | static usb_dev_handle *dediprog_handle; |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 31 | static int dediprog_firmwareversion; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 32 | static int dediprog_endpoint; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 33 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 34 | /* Set/clear LEDs on dediprog */ |
| 35 | enum { |
| 36 | LED_PASS = 1 << 0, |
| 37 | LED_BUSY = 1 << 1, |
| 38 | LED_ERROR = 1 << 2, |
| 39 | LED_ALL = 7, |
| 40 | }; |
| 41 | |
| 42 | static int current_led_status = -1; |
| 43 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 44 | #if 0 |
| 45 | /* Might be useful for other pieces of code as well. */ |
| 46 | static void print_hex(void *buf, size_t len) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 47 | { |
| 48 | size_t i; |
| 49 | |
| 50 | for (i = 0; i < len; i++) |
| 51 | msg_pdbg(" %02x", ((uint8_t *)buf)[i]); |
| 52 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 53 | #endif |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 54 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 55 | /* Might be useful for other USB devices as well. static for now. */ |
| 56 | static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 57 | { |
| 58 | struct usb_bus *bus; |
| 59 | struct usb_device *dev; |
| 60 | |
| 61 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 62 | for (dev = bus->devices; dev; dev = dev->next) |
| 63 | if ((dev->descriptor.idVendor == vid) && |
| 64 | (dev->descriptor.idProduct == pid)) |
| 65 | return dev; |
| 66 | |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | //int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); |
| 71 | |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 72 | static int dediprog_set_leds(int leds) |
| 73 | { |
| 74 | int ret, target_leds; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 75 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 76 | if (leds < 0 || leds > LED_ALL) |
| 77 | leds = LED_ALL; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 78 | if (leds == current_led_status) |
| 79 | return 0; |
| 80 | |
| 81 | /* Older Dediprogs with 2.x.x and 3.x.x firmware only had |
| 82 | * two LEDs, and they were reversed. So map them around if |
| 83 | * we have an old device. On those devices the LEDs map as |
| 84 | * follows: |
| 85 | * bit 2 == 0: green light is on. |
| 86 | * bit 0 == 0: red light is on. |
| 87 | */ |
| 88 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 89 | target_leds = ((leds & LED_ERROR) >> 2) | |
| 90 | ((leds & LED_PASS) << 2); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 91 | } else { |
| 92 | target_leds = leds; |
| 93 | } |
| 94 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 95 | target_leds ^= 7; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 96 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds, |
| 97 | NULL, 0x0, DEFAULT_TIMEOUT); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 98 | if (ret != 0x0) { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 99 | msg_perr("Command Set LED 0x%x failed (%s)!\n", |
| 100 | leds, usb_strerror()); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | current_led_status = leds; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 109 | static int dediprog_set_spi_voltage(int millivolt) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 110 | { |
| 111 | int ret; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 112 | uint16_t voltage_selector; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 113 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 114 | switch (millivolt) { |
| 115 | case 0: |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 116 | /* Admittedly this one is an assumption. */ |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 117 | voltage_selector = 0x0; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 118 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 119 | case 1800: |
| 120 | voltage_selector = 0x12; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 121 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 122 | case 2500: |
| 123 | voltage_selector = 0x11; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 124 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 125 | case 3500: |
| 126 | voltage_selector = 0x10; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 127 | break; |
| 128 | default: |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 129 | msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 130 | return 1; |
| 131 | } |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 132 | msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000, |
| 133 | millivolt % 1000); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 134 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 135 | ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector, |
| 136 | 0xff, NULL, 0x0, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 137 | if (ret != 0x0) { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 138 | msg_perr("Command Set SPI Voltage 0x%x failed!\n", |
| 139 | voltage_selector); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 140 | return 1; |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 145 | #if 0 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 146 | /* After dediprog_set_spi_speed, the original app always calls |
| 147 | * dediprog_set_spi_voltage(0) and then |
| 148 | * dediprog_check_devicestring() four times in a row. |
| 149 | * After that, dediprog_command_a() is called. |
| 150 | * This looks suspiciously like the microprocessor in the SF100 has to be |
| 151 | * restarted/reinitialized in case the speed changes. |
| 152 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 153 | static int dediprog_set_spi_speed(uint16_t speed) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 154 | { |
| 155 | int ret; |
| 156 | unsigned int khz; |
| 157 | |
| 158 | /* Case 1 and 2 are in weird order. Probably an organically "grown" |
| 159 | * interface. |
| 160 | * Base frequency is 24000 kHz, divisors are (in order) |
| 161 | * 1, 3, 2, 8, 11, 16, 32, 64. |
| 162 | */ |
| 163 | switch (speed) { |
| 164 | case 0x0: |
| 165 | khz = 24000; |
| 166 | break; |
| 167 | case 0x1: |
| 168 | khz = 8000; |
| 169 | break; |
| 170 | case 0x2: |
| 171 | khz = 12000; |
| 172 | break; |
| 173 | case 0x3: |
| 174 | khz = 3000; |
| 175 | break; |
| 176 | case 0x4: |
| 177 | khz = 2180; |
| 178 | break; |
| 179 | case 0x5: |
| 180 | khz = 1500; |
| 181 | break; |
| 182 | case 0x6: |
| 183 | khz = 750; |
| 184 | break; |
| 185 | case 0x7: |
| 186 | khz = 375; |
| 187 | break; |
| 188 | default: |
| 189 | msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed); |
| 190 | return 1; |
| 191 | } |
| 192 | msg_pdbg("Setting SPI speed to %u kHz\n", khz); |
| 193 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 194 | ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, |
| 195 | 0x0, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 196 | if (ret != 0x0) { |
| 197 | msg_perr("Command Set SPI Speed 0x%x failed!\n", speed); |
| 198 | return 1; |
| 199 | } |
| 200 | return 0; |
| 201 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 202 | #endif |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 203 | |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 204 | /* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes. |
| 205 | * @start start address |
| 206 | * @len length |
| 207 | * @return 0 on success, 1 on failure |
| 208 | */ |
| 209 | static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 210 | unsigned int start, unsigned int len) |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 211 | { |
| 212 | int ret; |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 213 | unsigned int i; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 214 | /* chunksize must be 512, other sizes will NOT work at all. */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 215 | const unsigned int chunksize = 0x200; |
| 216 | const unsigned int count = len / chunksize; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 217 | const char count_and_chunk[] = {count & 0xff, |
| 218 | (count >> 8) & 0xff, |
| 219 | chunksize & 0xff, |
| 220 | (chunksize >> 8) & 0xff}; |
| 221 | |
| 222 | if ((start % chunksize) || (len % chunksize)) { |
| 223 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 224 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | /* No idea if the hardware can handle empty reads, so chicken out. */ |
| 229 | if (!len) |
| 230 | return 0; |
| 231 | /* Command Read SPI Bulk. No idea which read command is used on the |
| 232 | * SPI side. |
| 233 | */ |
| 234 | ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000, |
| 235 | start / 0x10000, (char *)count_and_chunk, |
| 236 | sizeof(count_and_chunk), DEFAULT_TIMEOUT); |
| 237 | if (ret != sizeof(count_and_chunk)) { |
| 238 | msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, |
| 239 | usb_strerror()); |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | for (i = 0; i < count; i++) { |
| 244 | ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint, |
| 245 | (char *)buf + i * chunksize, chunksize, |
| 246 | DEFAULT_TIMEOUT); |
| 247 | if (ret != chunksize) { |
| 248 | msg_perr("SPI bulk read %i failed, expected %i, got %i " |
| 249 | "%s!\n", i, chunksize, ret, usb_strerror()); |
| 250 | return 1; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 257 | static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, |
| 258 | unsigned int start, unsigned int len) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 259 | { |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 260 | int ret; |
| 261 | /* chunksize must be 512, other sizes will NOT work at all. */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 262 | const unsigned int chunksize = 0x200; |
| 263 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 264 | unsigned int bulklen; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 265 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 266 | dediprog_set_leds(LED_BUSY); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 267 | |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 268 | if (residue) { |
| 269 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 270 | start, residue); |
| 271 | ret = spi_read_chunked(flash, buf, start, residue, 16); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 272 | if (ret) |
| 273 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* Round down. */ |
| 277 | bulklen = (len - residue) / chunksize * chunksize; |
| 278 | ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, |
| 279 | bulklen); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 280 | if (ret) |
| 281 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 282 | |
| 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); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 289 | if (ret) |
| 290 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 293 | dediprog_set_leds(LED_PASS); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 294 | return 0; |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 295 | err: |
| 296 | dediprog_set_leds(LED_ERROR); |
| 297 | return ret; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 298 | } |
| 299 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 300 | static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 301 | unsigned int start, unsigned int len) |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 302 | { |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 303 | int ret; |
| 304 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 305 | dediprog_set_leds(LED_BUSY); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 306 | |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 307 | /* No idea about the real limit. Maybe 12, maybe more, maybe less. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 308 | ret = spi_write_chunked(flash, buf, start, len, 12); |
| 309 | |
| 310 | if (ret) |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 311 | dediprog_set_leds(LED_ERROR); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 312 | else |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 313 | dediprog_set_leds(LED_PASS); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 314 | |
| 315 | return ret; |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 316 | } |
| 317 | |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 318 | static int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 319 | const unsigned char *writearr, unsigned char *readarr) |
| 320 | { |
| 321 | int ret; |
| 322 | |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 323 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 324 | /* Paranoid, but I don't want to be blamed if anything explodes. */ |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 325 | if (writecnt > 16) { |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 326 | msg_perr("Untested writecnt=%i, aborting.\n", writecnt); |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 327 | return 1; |
| 328 | } |
| 329 | /* 16 byte reads should work. */ |
| 330 | if (readcnt > 16) { |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 331 | msg_perr("Untested readcnt=%i, aborting.\n", readcnt); |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 332 | return 1; |
| 333 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 334 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 335 | ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, |
| 336 | readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, |
| 337 | DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 338 | if (ret != writecnt) { |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 339 | msg_perr("Send SPI failed, expected %i, got %i %s!\n", |
| 340 | writecnt, ret, usb_strerror()); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 341 | return 1; |
| 342 | } |
| 343 | if (!readcnt) |
| 344 | return 0; |
| 345 | memset(readarr, 0, readcnt); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 346 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, |
| 347 | (char *)readarr, readcnt, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 348 | if (ret != readcnt) { |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 349 | msg_perr("Receive SPI failed, expected %i, got %i %s!\n", |
| 350 | readcnt, ret, usb_strerror()); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 351 | return 1; |
| 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 356 | static int dediprog_check_devicestring(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 357 | { |
| 358 | int ret; |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 359 | int fw[3]; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 360 | char buf[0x11]; |
| 361 | |
| 362 | /* Command Prepare Receive Device String. */ |
| 363 | memset(buf, 0, sizeof(buf)); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 364 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, |
| 365 | 0x1, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 366 | /* The char casting is needed to stop gcc complaining about an always true comparison. */ |
| 367 | if ((ret != 0x1) || (buf[0] != (char)0xff)) { |
| 368 | msg_perr("Unexpected response to Command Prepare Receive Device" |
| 369 | " String!\n"); |
| 370 | return 1; |
| 371 | } |
| 372 | /* Command Receive Device String. */ |
| 373 | memset(buf, 0, sizeof(buf)); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 374 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, |
| 375 | 0x10, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 376 | if (ret != 0x10) { |
| 377 | msg_perr("Incomplete/failed Command Receive Device String!\n"); |
| 378 | return 1; |
| 379 | } |
| 380 | buf[0x10] = '\0'; |
| 381 | msg_pdbg("Found a %s\n", buf); |
| 382 | if (memcmp(buf, "SF100", 0x5)) { |
| 383 | msg_perr("Device not a SF100!\n"); |
| 384 | return 1; |
| 385 | } |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 386 | if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) { |
| 387 | msg_perr("Unexpected firmware version string!\n"); |
| 388 | return 1; |
| 389 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 390 | /* Only these versions were tested. */ |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 391 | if (fw[0] < 2 || fw[0] > 5) { |
| 392 | msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], |
| 393 | fw[1], fw[2]); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 394 | return 1; |
| 395 | } |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 396 | dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /* Command A seems to be some sort of device init. It is either followed by |
| 401 | * dediprog_check_devicestring (often) or Command A (often) or |
| 402 | * Command F (once). |
| 403 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 404 | static int dediprog_command_a(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 405 | { |
| 406 | int ret; |
| 407 | char buf[0x1]; |
| 408 | |
| 409 | memset(buf, 0, sizeof(buf)); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 410 | ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, |
| 411 | 0x1, DEFAULT_TIMEOUT); |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 412 | if (ret < 0) { |
| 413 | msg_perr("Command A failed (%s)!\n", usb_strerror()); |
| 414 | return 1; |
| 415 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 416 | if ((ret != 0x1) || (buf[0] != 0x6f)) { |
| 417 | msg_perr("Unexpected response to Command A!\n"); |
| 418 | return 1; |
| 419 | } |
| 420 | return 0; |
| 421 | } |
| 422 | |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 423 | #if 0 |
| 424 | /* Something. |
| 425 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 426 | * Always preceded by Command Receive Device String |
| 427 | */ |
| 428 | static int dediprog_command_b(void) |
| 429 | { |
| 430 | int ret; |
| 431 | char buf[0x3]; |
| 432 | |
| 433 | memset(buf, 0, sizeof(buf)); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 434 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf, |
| 435 | 0x3, DEFAULT_TIMEOUT); |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 436 | if (ret < 0) { |
| 437 | msg_perr("Command B failed (%s)!\n", usb_strerror()); |
| 438 | return 1; |
| 439 | } |
| 440 | if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) || |
| 441 | (buf[2] != 0xff)) { |
| 442 | msg_perr("Unexpected response to Command B!\n"); |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | #endif |
| 449 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 450 | /* Command C is only sent after dediprog_check_devicestring, but not after every |
| 451 | * invocation of dediprog_check_devicestring. It is only sent after the first |
| 452 | * dediprog_command_a(); dediprog_check_devicestring() sequence in each session. |
| 453 | * I'm tempted to call this one start_SPI_engine or finish_init. |
| 454 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 455 | static int dediprog_command_c(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 456 | { |
| 457 | int ret; |
| 458 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 459 | ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, |
| 460 | 0x0, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 461 | if (ret != 0x0) { |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 462 | msg_perr("Command C failed (%s)!\n", usb_strerror()); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 463 | return 1; |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 468 | #if 0 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 469 | /* Very strange. Seems to be a programmer keepalive or somesuch. |
| 470 | * Wait unsuccessfully for timeout ms to read one byte. |
| 471 | * Is usually called after setting voltage to 0. |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 472 | * Present in all logs with Firmware 2.1.1 and 3.1.8 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 473 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 474 | static int dediprog_command_f(int timeout) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 475 | { |
| 476 | int ret; |
| 477 | char buf[0x1]; |
| 478 | |
| 479 | memset(buf, 0, sizeof(buf)); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 480 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, |
| 481 | 0x1, timeout); |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 482 | /* This check is most probably wrong. Command F always causes a timeout |
| 483 | * in the logs, so we should check for timeout instead of checking for |
| 484 | * success. |
| 485 | */ |
| 486 | if (ret != 0x1) { |
| 487 | msg_perr("Command F failed (%s)!\n", usb_strerror()); |
| 488 | return 1; |
| 489 | } |
| 490 | return 0; |
| 491 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 492 | #endif |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 493 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 494 | static int parse_voltage(char *voltage) |
| 495 | { |
| 496 | char *tmp = NULL; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 497 | int i; |
| 498 | int millivolt = 0, fraction = 0; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 499 | |
| 500 | if (!voltage || !strlen(voltage)) { |
| 501 | msg_perr("Empty voltage= specified.\n"); |
| 502 | return -1; |
| 503 | } |
| 504 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 505 | voltage = tmp; |
| 506 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 507 | * to be in decimal notation. |
| 508 | */ |
| 509 | if ((*voltage == '.') || (*voltage == ',')) { |
| 510 | voltage++; |
| 511 | for (i = 0; i < 3; i++) { |
| 512 | fraction *= 10; |
| 513 | /* Don't advance if the current character is invalid, |
| 514 | * but continue multiplying. |
| 515 | */ |
| 516 | if ((*voltage < '0') || (*voltage > '9')) |
| 517 | continue; |
| 518 | fraction += *voltage - '0'; |
| 519 | voltage++; |
| 520 | } |
| 521 | /* Throw away remaining digits. */ |
| 522 | voltage += strspn(voltage, "0123456789"); |
| 523 | } |
| 524 | /* The remaining string must be empty or "mV" or "V". */ |
| 525 | tolower_string(voltage); |
| 526 | |
| 527 | /* No unit or "V". */ |
| 528 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 529 | millivolt *= 1000; |
| 530 | millivolt += fraction; |
| 531 | } else if (!strncmp(voltage, "mv", 2) || |
| 532 | !strncmp(voltage, "milliv", 6)) { |
| 533 | /* No adjustment. fraction is discarded. */ |
| 534 | } else { |
| 535 | /* Garbage at the end of the string. */ |
| 536 | msg_perr("Garbage voltage= specified.\n"); |
| 537 | return -1; |
| 538 | } |
| 539 | return millivolt; |
| 540 | } |
| 541 | |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 542 | static const struct spi_programmer spi_programmer_dediprog = { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 543 | .type = SPI_CONTROLLER_DEDIPROG, |
| 544 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 545 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 546 | .command = dediprog_spi_send_command, |
| 547 | .multicommand = default_spi_send_multicommand, |
| 548 | .read = dediprog_spi_read, |
| 549 | .write_256 = dediprog_spi_write_256, |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 550 | }; |
| 551 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 552 | static int dediprog_shutdown(void *data) |
| 553 | { |
| 554 | msg_pspew("%s\n", __func__); |
| 555 | |
| 556 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 557 | if (dediprog_set_spi_voltage(0x0)) |
| 558 | return 1; |
| 559 | |
| 560 | if (usb_release_interface(dediprog_handle, 0)) { |
| 561 | msg_perr("Could not release USB interface!\n"); |
| 562 | return 1; |
| 563 | } |
| 564 | if (usb_close(dediprog_handle)) { |
| 565 | msg_perr("Could not close USB device!\n"); |
| 566 | return 1; |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 571 | /* URB numbers refer to the first log ever captured. */ |
| 572 | int dediprog_init(void) |
| 573 | { |
| 574 | struct usb_device *dev; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 575 | char *voltage; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 576 | int millivolt = 3500; |
| 577 | int ret; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 578 | |
| 579 | msg_pspew("%s\n", __func__); |
| 580 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 581 | voltage = extract_programmer_param("voltage"); |
| 582 | if (voltage) { |
| 583 | millivolt = parse_voltage(voltage); |
| 584 | free(voltage); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 585 | if (millivolt < 0) |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 586 | return 1; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 587 | msg_pinfo("Setting voltage to %i mV\n", millivolt); |
| 588 | } |
| 589 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 590 | /* Here comes the USB stuff. */ |
| 591 | usb_init(); |
| 592 | usb_find_busses(); |
| 593 | usb_find_devices(); |
| 594 | dev = get_device_by_vid_pid(0x0483, 0xdada); |
| 595 | if (!dev) { |
| 596 | msg_perr("Could not find a Dediprog SF100 on USB!\n"); |
| 597 | return 1; |
| 598 | } |
| 599 | msg_pdbg("Found USB device (%04x:%04x).\n", |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 600 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 601 | dediprog_handle = usb_open(dev); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 602 | ret = usb_set_configuration(dediprog_handle, 1); |
| 603 | if (ret < 0) { |
| 604 | msg_perr("Could not set USB device configuration: %i %s\n", |
| 605 | ret, usb_strerror()); |
| 606 | if (usb_close(dediprog_handle)) |
| 607 | msg_perr("Could not close USB device!\n"); |
| 608 | return 1; |
| 609 | } |
| 610 | ret = usb_claim_interface(dediprog_handle, 0); |
| 611 | if (ret < 0) { |
| 612 | msg_perr("Could not claim USB device interface %i: %i %s\n", |
| 613 | 0, ret, usb_strerror()); |
| 614 | if (usb_close(dediprog_handle)) |
| 615 | msg_perr("Could not close USB device!\n"); |
| 616 | return 1; |
| 617 | } |
| 618 | dediprog_endpoint = 2; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 619 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 620 | if (register_shutdown(dediprog_shutdown, NULL)) |
| 621 | return 1; |
| 622 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 623 | dediprog_set_leds(LED_ALL); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 624 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 625 | /* URB 6. Command A. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 626 | if (dediprog_command_a()) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 627 | dediprog_set_leds(LED_ERROR); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 628 | return 1; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 629 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 630 | /* URB 7. Command A. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 631 | if (dediprog_command_a()) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 632 | dediprog_set_leds(LED_ERROR); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 633 | return 1; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 634 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 635 | /* URB 8. Command Prepare Receive Device String. */ |
| 636 | /* URB 9. Command Receive Device String. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 637 | if (dediprog_check_devicestring()) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 638 | dediprog_set_leds(LED_ERROR); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 639 | return 1; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 640 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 641 | /* URB 10. Command C. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 642 | if (dediprog_command_c()) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 643 | dediprog_set_leds(LED_ERROR); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 644 | return 1; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 645 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 646 | /* URB 11. Command Set SPI Voltage. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 647 | if (dediprog_set_spi_voltage(millivolt)) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 648 | dediprog_set_leds(LED_ERROR); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 649 | return 1; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 650 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 651 | |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 652 | register_spi_programmer(&spi_programmer_dediprog); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 653 | |
| 654 | /* RE leftover, leave in until the driver is complete. */ |
| 655 | #if 0 |
| 656 | /* Execute RDID by hand if you want to test it. */ |
| 657 | dediprog_do_stuff(); |
| 658 | #endif |
| 659 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 660 | dediprog_set_leds(0); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 661 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 665 | #if 0 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 666 | /* Leftovers from reverse engineering. Keep for documentation purposes until |
| 667 | * completely understood. |
| 668 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 669 | static int dediprog_do_stuff(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 670 | { |
| 671 | char buf[0x4]; |
| 672 | /* SPI command processing starts here. */ |
| 673 | |
| 674 | /* URB 12. Command Send SPI. */ |
| 675 | /* URB 13. Command Receive SPI. */ |
| 676 | memset(buf, 0, sizeof(buf)); |
| 677 | /* JEDEC RDID */ |
| 678 | msg_pdbg("Sending RDID\n"); |
| 679 | buf[0] = JEDEC_RDID; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 680 | if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, |
| 681 | (unsigned char *)buf, (unsigned char *)buf)) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 682 | return 1; |
| 683 | msg_pdbg("Receiving response: "); |
| 684 | print_hex(buf, JEDEC_RDID_INSIZE); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 685 | /* URB 14-27 are more SPI commands. */ |
| 686 | /* URB 28. Command Set SPI Voltage. */ |
| 687 | if (dediprog_set_spi_voltage(0x0)) |
| 688 | return 1; |
| 689 | /* URB 29-38. Command F, unsuccessful wait. */ |
| 690 | if (dediprog_command_f(544)) |
| 691 | return 1; |
| 692 | /* URB 39. Command Set SPI Voltage. */ |
| 693 | if (dediprog_set_spi_voltage(0x10)) |
| 694 | return 1; |
| 695 | /* URB 40. Command Set SPI Speed. */ |
| 696 | if (dediprog_set_spi_speed(0x2)) |
| 697 | return 1; |
| 698 | /* URB 41 is just URB 28. */ |
| 699 | /* URB 42,44,46,48,51,53 is just URB 8. */ |
| 700 | /* URB 43,45,47,49,52,54 is just URB 9. */ |
| 701 | /* URB 50 is just URB 6/7. */ |
| 702 | /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/ |
| 703 | /* URB 132,134 is just URB 6/7. */ |
| 704 | /* URB 133 is just URB 29-38. */ |
| 705 | /* URB 135 is just URB 8. */ |
| 706 | /* URB 136 is just URB 9. */ |
| 707 | /* URB 137 is just URB 11. */ |
| 708 | |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 709 | /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */ |
| 710 | /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */ |
| 711 | /* Bulk transfer sizes for Command Start Bulk Read/Write are always |
| 712 | * 512 bytes, rest is filled with 0xff. |
| 713 | */ |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 714 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 715 | return 0; |
| 716 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 717 | #endif |