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