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 | */ |
| 208 | static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf, |
| 209 | int start, int len) |
| 210 | { |
| 211 | int ret; |
| 212 | int i; |
| 213 | /* chunksize must be 512, other sizes will NOT work at all. */ |
| 214 | const int chunksize = 0x200; |
| 215 | const int count = len / chunksize; |
| 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 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 256 | static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, |
| 257 | 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. */ |
| 261 | const int chunksize = 0x200; |
| 262 | int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 263 | int bulklen; |
| 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 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 302 | static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, |
| 303 | int start, 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; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 499 | int i, millivolt, fraction = 0; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 500 | |
| 501 | if (!voltage || !strlen(voltage)) { |
| 502 | msg_perr("Empty voltage= specified.\n"); |
| 503 | return -1; |
| 504 | } |
| 505 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 506 | voltage = tmp; |
| 507 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 508 | * to be in decimal notation. |
| 509 | */ |
| 510 | if ((*voltage == '.') || (*voltage == ',')) { |
| 511 | voltage++; |
| 512 | for (i = 0; i < 3; i++) { |
| 513 | fraction *= 10; |
| 514 | /* Don't advance if the current character is invalid, |
| 515 | * but continue multiplying. |
| 516 | */ |
| 517 | if ((*voltage < '0') || (*voltage > '9')) |
| 518 | continue; |
| 519 | fraction += *voltage - '0'; |
| 520 | voltage++; |
| 521 | } |
| 522 | /* Throw away remaining digits. */ |
| 523 | voltage += strspn(voltage, "0123456789"); |
| 524 | } |
| 525 | /* The remaining string must be empty or "mV" or "V". */ |
| 526 | tolower_string(voltage); |
| 527 | |
| 528 | /* No unit or "V". */ |
| 529 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 530 | millivolt *= 1000; |
| 531 | millivolt += fraction; |
| 532 | } else if (!strncmp(voltage, "mv", 2) || |
| 533 | !strncmp(voltage, "milliv", 6)) { |
| 534 | /* No adjustment. fraction is discarded. */ |
| 535 | } else { |
| 536 | /* Garbage at the end of the string. */ |
| 537 | msg_perr("Garbage voltage= specified.\n"); |
| 538 | return -1; |
| 539 | } |
| 540 | return millivolt; |
| 541 | } |
| 542 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 543 | static const struct spi_programmer spi_programmer_dediprog = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 544 | .type = SPI_CONTROLLER_DEDIPROG, |
| 545 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 546 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 547 | .command = dediprog_spi_send_command, |
| 548 | .multicommand = default_spi_send_multicommand, |
| 549 | .read = dediprog_spi_read, |
| 550 | .write_256 = dediprog_spi_write_256, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 551 | }; |
| 552 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 553 | static int dediprog_shutdown(void *data) |
| 554 | { |
| 555 | msg_pspew("%s\n", __func__); |
| 556 | |
| 557 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 558 | if (dediprog_set_spi_voltage(0x0)) |
| 559 | return 1; |
| 560 | |
| 561 | if (usb_release_interface(dediprog_handle, 0)) { |
| 562 | msg_perr("Could not release USB interface!\n"); |
| 563 | return 1; |
| 564 | } |
| 565 | if (usb_close(dediprog_handle)) { |
| 566 | msg_perr("Could not close USB device!\n"); |
| 567 | return 1; |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 572 | /* URB numbers refer to the first log ever captured. */ |
| 573 | int dediprog_init(void) |
| 574 | { |
| 575 | struct usb_device *dev; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 576 | char *voltage; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 577 | int millivolt = 3500, ret; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 578 | |
| 579 | msg_pspew("%s\n", __func__); |
| 580 | |
Carl-Daniel Hailfinger | c244138 | 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 Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 585 | if (millivolt < 0) |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 586 | return 1; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 587 | msg_pinfo("Setting voltage to %i mV\n", millivolt); |
| 588 | } |
| 589 | |
Carl-Daniel Hailfinger | d38fac8 | 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 Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 600 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 601 | dediprog_handle = usb_open(dev); |
Carl-Daniel Hailfinger | 482e974 | 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; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 619 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 620 | if (register_shutdown(dediprog_shutdown, NULL)) |
| 621 | return 1; |
| 622 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 623 | dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON); |
| 624 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 625 | /* URB 6. Command A. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 626 | if (dediprog_command_a()) { |
| 627 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 628 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 629 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 630 | /* URB 7. Command A. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 631 | if (dediprog_command_a()) { |
| 632 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 633 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 634 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 635 | /* URB 8. Command Prepare Receive Device String. */ |
| 636 | /* URB 9. Command Receive Device String. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 637 | if (dediprog_check_devicestring()) { |
| 638 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 639 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 640 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 641 | /* URB 10. Command C. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 642 | if (dediprog_command_c()) { |
| 643 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 644 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 645 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 646 | /* URB 11. Command Set SPI Voltage. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 647 | if (dediprog_set_spi_voltage(millivolt)) { |
| 648 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 649 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 650 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 651 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 652 | register_spi_programmer(&spi_programmer_dediprog); |
Carl-Daniel Hailfinger | d38fac8 | 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 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 660 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF); |
| 661 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 665 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 666 | /* Leftovers from reverse engineering. Keep for documentation purposes until |
| 667 | * completely understood. |
| 668 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 669 | static int dediprog_do_stuff(void) |
Carl-Daniel Hailfinger | d38fac8 | 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 Hermann | 91f4afa | 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)) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 682 | return 1; |
| 683 | msg_pdbg("Receiving response: "); |
| 684 | print_hex(buf, JEDEC_RDID_INSIZE); |
Carl-Daniel Hailfinger | d38fac8 | 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 | |
Carl-Daniel Hailfinger | ff30d8a | 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 | */ |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 714 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 715 | return 0; |
| 716 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 717 | #endif |