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 |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 5 | * Copyright (C) 2015 Simon Glass |
| 6 | * Copyright (C) 2015 Stefan Tauner |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 22 | //#include "platform.h" |
| 23 | |
| 24 | #include <stdlib.h> |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 26 | #include <string.h> |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 27 | #include <limits.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | #if IS_WINDOWS |
| 31 | #include <lusb0_usb.h> |
| 32 | #else |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 33 | #include <libusb.h> |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 34 | #endif |
| 35 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 36 | #include "flash.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 37 | #include "chipdrivers.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 38 | #include "programmer.h" |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 39 | #include "spi.h" |
| 40 | |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 41 | #define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 42 | #define DEFAULT_TIMEOUT 3000 |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 43 | #define DEDIPROG_ASYNC_TRANSFERS 8 /* at most 8 asynchronous transfers */ |
| 44 | #define REQTYPE_OTHER_OUT (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0x43 */ |
| 45 | #define REQTYPE_OTHER_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_OTHER) /* 0xC3 */ |
| 46 | #define REQTYPE_EP_OUT (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0x42 */ |
| 47 | #define REQTYPE_EP_IN (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_ENDPOINT) /* 0xC2 */ |
| 48 | struct libusb_context *usb_ctx; |
| 49 | static libusb_device_handle *dediprog_handle; |
David Hendricks | d746858 | 2015-11-12 15:21:12 -0800 | [diff] [blame] | 50 | static int dediprog_in_endpoint; |
| 51 | static int dediprog_out_endpoint; |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 52 | static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 53 | |
David Hendricks | 6702e07 | 2015-08-15 18:09:04 -0700 | [diff] [blame] | 54 | enum dediprog_devtype { |
| 55 | DEV_UNKNOWN = 0, |
| 56 | DEV_SF100 = 100, |
| 57 | DEV_SF600 = 600, |
| 58 | }; |
| 59 | |
| 60 | enum dediprog_devtype dediprog_devicetype; |
| 61 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 62 | enum dediprog_leds { |
| 63 | LED_INVALID = -1, |
| 64 | LED_NONE = 0, |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 65 | LED_PASS = 1 << 0, |
| 66 | LED_BUSY = 1 << 1, |
| 67 | LED_ERROR = 1 << 2, |
| 68 | LED_ALL = 7, |
| 69 | }; |
| 70 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame] | 71 | /* IO bits for CMD_SET_IO_LED message */ |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 72 | enum dediprog_ios { |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame] | 73 | IO1 = 1 << 0, |
| 74 | IO2 = 1 << 1, |
| 75 | IO3 = 1 << 2, |
| 76 | IO4 = 1 << 3, |
| 77 | }; |
| 78 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 79 | enum dediprog_cmds { |
| 80 | CMD_TRANSCEIVE = 0x01, |
| 81 | CMD_POLL_STATUS_REG = 0x02, |
| 82 | CMD_SET_VPP = 0x03, |
| 83 | CMD_SET_TARGET = 0x04, |
| 84 | CMD_READ_EEPROM = 0x05, |
| 85 | CMD_WRITE_EEPROM = 0x06, |
| 86 | CMD_SET_IO_LED = 0x07, |
| 87 | CMD_READ_PROG_INFO = 0x08, |
| 88 | CMD_SET_VCC = 0x09, |
| 89 | CMD_SET_STANDALONE = 0x0A, |
David Hendricks | 28fbdfb | 2015-08-15 18:04:37 -0700 | [diff] [blame] | 90 | CMD_SET_VOLTAGE = 0x0B, /* Only in firmware older than 6.0.0 */ |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 91 | CMD_GET_BUTTON = 0x11, |
| 92 | CMD_GET_UID = 0x12, |
| 93 | CMD_SET_CS = 0x14, |
| 94 | CMD_IO_MODE = 0x15, |
| 95 | CMD_FW_UPDATE = 0x1A, |
| 96 | CMD_FPGA_UPDATE = 0x1B, |
| 97 | CMD_READ_FPGA_VERSION = 0x1C, |
| 98 | CMD_SET_HOLD = 0x1D, |
| 99 | CMD_READ = 0x20, |
| 100 | CMD_WRITE = 0x30, |
| 101 | CMD_WRITE_AT45DB = 0x31, |
| 102 | CMD_NAND_WRITE = 0x32, |
| 103 | CMD_NAND_READ = 0x33, |
| 104 | CMD_SET_SPI_CLK = 0x61, |
| 105 | CMD_CHECK_SOCKET = 0x62, |
| 106 | CMD_DOWNLOAD_PRJ = 0x63, |
| 107 | CMD_READ_PRJ_NAME = 0x64, |
| 108 | // New protocol/firmware only |
| 109 | CMD_CHECK_SDCARD = 0x65, |
| 110 | CMD_READ_PRJ = 0x66, |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 113 | enum dediprog_target { |
| 114 | FLASH_TYPE_APPLICATION_FLASH_1 = 0, |
| 115 | FLASH_TYPE_FLASH_CARD, |
| 116 | FLASH_TYPE_APPLICATION_FLASH_2, |
| 117 | FLASH_TYPE_SOCKET, |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 120 | enum dediprog_readmode { |
| 121 | READ_MODE_STD = 1, |
| 122 | READ_MODE_FAST = 2, |
| 123 | READ_MODE_ATMEL45 = 3, |
| 124 | READ_MODE_4B_ADDR_FAST = 4, |
| 125 | READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */ |
Simon Glass | d01002b | 2015-01-08 05:44:16 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 128 | enum dediprog_writemode { |
| 129 | WRITE_MODE_PAGE_PGM = 1, |
| 130 | WRITE_MODE_PAGE_WRITE = 2, |
| 131 | WRITE_MODE_1B_AAI = 3, |
| 132 | WRITE_MODE_2B_AAI = 4, |
| 133 | WRITE_MODE_128B_PAGE = 5, |
| 134 | WRITE_MODE_PAGE_AT26DF041 = 6, |
| 135 | WRITE_MODE_SILICON_BLUE_FPGA = 7, |
| 136 | WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of length 512 bytes */ |
| 137 | WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9, |
| 138 | WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of length 512 bytes */ |
| 139 | WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11, |
| 140 | WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12, |
Simon Glass | d01002b | 2015-01-08 05:44:16 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 143 | #ifndef LIBUSB_HAVE_ERROR_NAME |
| 144 | /* Quick and dirty replacement for missing libusb_error_name in older libusb 1.0. */ |
| 145 | const char *libusb_error_name(int error_code) |
| 146 | { |
| 147 | /* 18 chars for text, rest for number, sign, nullbyte. */ |
| 148 | static char my_libusb_error[18 + 6]; |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 149 | |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 150 | sprintf(my_libusb_error, "libusb error code %i", error_code); |
| 151 | return my_libusb_error; |
| 152 | } |
| 153 | #endif |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 154 | |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 155 | /* Returns true if firmware (and thus hardware) supports the "new" protocol */ |
| 156 | static int is_new_prot(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 157 | { |
David Hendricks | 6702e07 | 2015-08-15 18:09:04 -0700 | [diff] [blame] | 158 | switch (dediprog_devicetype) { |
| 159 | case DEV_SF100: |
| 160 | return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0); |
| 161 | case DEV_SF600: |
| 162 | return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0); |
| 163 | default: |
| 164 | return 0; |
| 165 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 166 | } |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 167 | |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 168 | struct dediprog_transfer_status { |
| 169 | int error; /* OK if 0, ERROR else */ |
| 170 | unsigned int queued_idx; |
| 171 | unsigned int finished_idx; |
| 172 | }; |
| 173 | |
| 174 | static void dediprog_bulk_read_cb(struct libusb_transfer *const transfer) |
| 175 | { |
| 176 | struct dediprog_transfer_status *const status = (struct dediprog_transfer_status *)transfer->user_data; |
| 177 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { |
| 178 | status->error = 1; |
| 179 | msg_perr("SPI bulk read failed!\n"); |
| 180 | } |
| 181 | ++status->finished_idx; |
| 182 | } |
| 183 | |
| 184 | static int dediprog_bulk_read_poll(const struct dediprog_transfer_status *const status, const int finish) |
| 185 | { |
| 186 | if (status->finished_idx >= status->queued_idx) |
| 187 | return 0; |
| 188 | |
| 189 | do { |
| 190 | struct timeval timeout = { 10, 0 }; |
| 191 | const int ret = libusb_handle_events_timeout_completed(usb_ctx, &timeout, NULL); |
| 192 | if (ret < 0) { |
| 193 | msg_perr("Polling read events failed: %i %s!\n", ret, libusb_error_name(ret)); |
| 194 | return 1; |
| 195 | } |
| 196 | } while (finish && (status->finished_idx < status->queued_idx)); |
| 197 | return 0; |
| 198 | } |
| 199 | |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 200 | static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size) |
| 201 | { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 202 | return libusb_control_transfer(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx, |
| 203 | (unsigned char *)bytes, size, DEFAULT_TIMEOUT); |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size) |
| 207 | { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 208 | return libusb_control_transfer(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx, |
| 209 | (unsigned char *)bytes, size, DEFAULT_TIMEOUT); |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 210 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 211 | |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 212 | |
| 213 | #if 0 |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 214 | /* Might be useful for other USB devices as well. static for now. */ |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 215 | /* device parameter allows user to specify one device of multiple installed */ |
| 216 | static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 217 | { |
| 218 | struct usb_bus *bus; |
| 219 | struct usb_device *dev; |
| 220 | |
| 221 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 222 | for (dev = bus->devices; dev; dev = dev->next) |
| 223 | if ((dev->descriptor.idVendor == vid) && |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 224 | (dev->descriptor.idProduct == pid)) { |
| 225 | if (device == 0) |
| 226 | return dev; |
| 227 | device--; |
| 228 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 229 | |
| 230 | return NULL; |
| 231 | } |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 232 | #endif |
| 233 | |
| 234 | /* Might be useful for other USB devices as well. static for now. */ |
| 235 | /* device parameter allows user to specify one device of multiple installed */ |
| 236 | static struct libusb_device_handle *get_device_by_vid_pid_number(uint16_t vid, uint16_t pid, unsigned int num) |
| 237 | { |
| 238 | struct libusb_device **list; |
| 239 | ssize_t i = 0; |
| 240 | int err = 0; |
| 241 | struct libusb_device_handle *handle = NULL; |
| 242 | struct libusb_device_descriptor desc = {}; |
| 243 | ssize_t count = libusb_get_device_list(usb_ctx, &list); |
| 244 | |
| 245 | if (count < 0) { |
| 246 | msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count)); |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | for (i = 0; i < count; i++) { |
| 251 | struct libusb_device *dev = list[i]; |
| 252 | err = libusb_get_device_descriptor(dev, &desc); |
| 253 | if (err != 0) { |
| 254 | msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(err)); |
| 255 | libusb_free_device_list(list, 1); |
| 256 | return NULL; |
| 257 | } |
| 258 | if ((desc.idVendor == vid) && (desc.idProduct == pid)) { |
| 259 | msg_pdbg("Found USB device %04hx:%04hx at address %hhx-%hhx.\n", desc.idVendor, |
| 260 | desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev)); |
| 261 | if (num == 0) { |
| 262 | err = libusb_open(dev, &handle); |
| 263 | if (err != 0) { |
| 264 | msg_perr("Opening the USB device failed (%s)!\n", |
| 265 | libusb_error_name(err)); |
| 266 | libusb_free_device_list(list, 1); |
| 267 | return NULL; |
| 268 | } |
| 269 | break; |
| 270 | } |
| 271 | num--; |
| 272 | } |
| 273 | } |
| 274 | libusb_free_device_list(list, 1); |
| 275 | |
| 276 | return handle; |
| 277 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 278 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 279 | /* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 280 | static int dediprog_set_leds(int leds) |
| 281 | { |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 282 | if (leds < LED_NONE || leds > LED_ALL) |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 283 | leds = LED_ALL; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 284 | |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 285 | /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map |
| 286 | * them around if we have an old device. On those devices the LEDs map as follows: |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 287 | * bit 2 == 0: green light is on. |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 288 | * bit 0 == 0: red light is on. |
| 289 | * |
| 290 | * Additionally, the command structure has changed with the "new" protocol. |
| 291 | * |
| 292 | * FIXME: take IO pins into account |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 293 | */ |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 294 | int target_leds, ret; |
| 295 | if (is_new_prot()) { |
| 296 | target_leds = (leds ^ 7) << 8; |
| 297 | ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 298 | } else { |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 299 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) { |
| 300 | target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2); |
| 301 | } else { |
| 302 | target_leds = leds; |
| 303 | } |
| 304 | target_leds ^= 7; |
| 305 | |
| 306 | ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0); |
Simon Glass | 543101a | 2015-01-08 06:28:13 -0700 | [diff] [blame] | 307 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 308 | |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 309 | if (ret != 0x0) { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 310 | msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, libusb_error_name(ret)); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 311 | return 1; |
| 312 | } |
| 313 | |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 317 | static int dediprog_set_spi_voltage(int millivolt) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 318 | { |
| 319 | int ret; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 320 | uint16_t voltage_selector; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 321 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 322 | switch (millivolt) { |
| 323 | case 0: |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 324 | /* Admittedly this one is an assumption. */ |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 325 | voltage_selector = 0x0; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 326 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 327 | case 1800: |
| 328 | voltage_selector = 0x12; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 329 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 330 | case 2500: |
| 331 | voltage_selector = 0x11; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 332 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 333 | case 3500: |
| 334 | voltage_selector = 0x10; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 335 | break; |
| 336 | default: |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 337 | msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 338 | return 1; |
| 339 | } |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 340 | msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000, |
| 341 | millivolt % 1000); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 342 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 343 | if (voltage_selector == 0) { |
| 344 | /* Wait some time as the original driver does. */ |
| 345 | programmer_delay(200 * 1000); |
| 346 | } |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 347 | ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 348 | if (ret != 0x0) { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 349 | msg_perr("Command Set SPI Voltage 0x%x failed!\n", |
| 350 | voltage_selector); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 351 | return 1; |
| 352 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 353 | if (voltage_selector != 0) { |
| 354 | /* Wait some time as the original driver does. */ |
| 355 | programmer_delay(200 * 1000); |
| 356 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 360 | struct dediprog_spispeeds { |
| 361 | const char *const name; |
| 362 | const int speed; |
| 363 | }; |
| 364 | |
| 365 | static const struct dediprog_spispeeds spispeeds[] = { |
| 366 | { "24M", 0x0 }, |
| 367 | { "12M", 0x2 }, |
| 368 | { "8M", 0x1 }, |
| 369 | { "3M", 0x3 }, |
| 370 | { "2.18M", 0x4 }, |
| 371 | { "1.5M", 0x5 }, |
| 372 | { "750k", 0x6 }, |
| 373 | { "375k", 0x7 }, |
| 374 | { NULL, 0x0 }, |
| 375 | }; |
| 376 | |
| 377 | static int dediprog_set_spi_speed(unsigned int spispeed_idx) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 378 | { |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 379 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) { |
| 380 | msg_pinfo("Skipping to set SPI speed because firmware is too old.\n"); |
| 381 | return 0; |
| 382 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 383 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 384 | const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx]; |
| 385 | msg_pdbg("SPI speed is %sHz\n", spispeed->name); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 386 | |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 387 | int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 388 | if (ret != 0x0) { |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 389 | msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 390 | return 1; |
| 391 | } |
| 392 | return 0; |
| 393 | } |
| 394 | |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 395 | /* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes. |
| 396 | * @start start address |
| 397 | * @len length |
| 398 | * @return 0 on success, 1 on failure |
| 399 | */ |
| 400 | static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 401 | unsigned int start, unsigned int len) |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 402 | { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 403 | int ret, err = 1; |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 404 | unsigned int i; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 405 | /* chunksize must be 512, other sizes will NOT work at all. */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 406 | const unsigned int chunksize = 0x200; |
| 407 | const unsigned int count = len / chunksize; |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 408 | unsigned int cmd_len; |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 409 | struct dediprog_transfer_status status = { 0, 0, 0 }; |
| 410 | struct libusb_transfer *transfers[DEDIPROG_ASYNC_TRANSFERS] = { NULL, }; |
| 411 | struct libusb_transfer *transfer; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 412 | |
| 413 | if ((start % chunksize) || (len % chunksize)) { |
| 414 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 415 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | /* No idea if the hardware can handle empty reads, so chicken out. */ |
| 420 | if (!len) |
| 421 | return 0; |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 422 | /* Command Read SPI Bulk. */ |
| 423 | if (is_new_prot()) { |
| 424 | const uint8_t read_cmd[] = { |
| 425 | count & 0xff, |
| 426 | (count >> 8) & 0xff, |
| 427 | 0, |
| 428 | READ_MODE_FAST, |
| 429 | 0, |
| 430 | 0, |
| 431 | start & 0xff, |
| 432 | (start >> 8) & 0xff, |
| 433 | (start >> 16) & 0xff, |
| 434 | (start >> 24) & 0xff, |
| 435 | }; |
| 436 | |
| 437 | cmd_len = sizeof(read_cmd); |
| 438 | ret = dediprog_write(CMD_READ, 0, 0, read_cmd, cmd_len); |
| 439 | } else { |
| 440 | const uint8_t read_cmd[] = {count & 0xff, |
| 441 | (count >> 8) & 0xff, |
| 442 | chunksize & 0xff, |
| 443 | (chunksize >> 8) & 0xff}; |
| 444 | |
| 445 | cmd_len = sizeof(read_cmd); |
| 446 | ret = dediprog_write(CMD_READ, start % 0x10000, start / 0x10000, read_cmd, cmd_len); |
| 447 | } |
| 448 | if (ret != cmd_len) { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 449 | msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret)); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 450 | return 1; |
| 451 | } |
| 452 | |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 453 | /* |
| 454 | * Ring buffer of bulk transfers. |
| 455 | * Poll until at least one transfer is ready, |
| 456 | * schedule next transfers until buffer is full. |
| 457 | */ |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 458 | |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 459 | /* Allocate bulk transfers. */ |
| 460 | for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) { |
| 461 | transfers[i] = libusb_alloc_transfer(0); |
| 462 | if (!transfers[i]) { |
| 463 | msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret)); |
| 464 | goto _err_free; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /* Now transfer requested chunks using libusb's asynchronous interface. */ |
| 469 | while (!status.error && (status.queued_idx < count)) { |
| 470 | while ((status.queued_idx - status.finished_idx) < DEDIPROG_ASYNC_TRANSFERS) { |
| 471 | transfer = transfers[status.queued_idx % DEDIPROG_ASYNC_TRANSFERS]; |
| 472 | libusb_fill_bulk_transfer(transfer, dediprog_handle, 0x80 | dediprog_in_endpoint, |
| 473 | (unsigned char *)buf + status.queued_idx * chunksize, chunksize, |
| 474 | dediprog_bulk_read_cb, &status, DEFAULT_TIMEOUT); |
| 475 | transfer->flags |= LIBUSB_TRANSFER_SHORT_NOT_OK; |
| 476 | ret = libusb_submit_transfer(transfer); |
| 477 | if (ret < 0) { |
| 478 | msg_perr("Submitting SPI bulk read %i failed: %i %s!\n", |
| 479 | status.queued_idx, ret, libusb_error_name(ret)); |
| 480 | goto _err_free; |
| 481 | } |
| 482 | ++status.queued_idx; |
| 483 | } |
| 484 | if (dediprog_bulk_read_poll(&status, 0)) |
| 485 | goto _err_free; |
| 486 | } |
| 487 | /* Wait for transfers to finish. */ |
| 488 | if (dediprog_bulk_read_poll(&status, 1)) |
| 489 | goto _err_free; |
| 490 | /* Check if everything has been transmitted. */ |
| 491 | if ((status.finished_idx < count) || status.error) |
| 492 | goto _err_free; |
| 493 | |
| 494 | err = 0; |
| 495 | |
| 496 | _err_free: |
| 497 | dediprog_bulk_read_poll(&status, 1); |
| 498 | for (i = 0; i < DEDIPROG_ASYNC_TRANSFERS; ++i) |
| 499 | if (transfers[i]) libusb_free_transfer(transfers[i]); |
| 500 | return err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 501 | } |
| 502 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 503 | static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, |
| 504 | unsigned int start, unsigned int len) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 505 | { |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 506 | int ret; |
| 507 | /* chunksize must be 512, other sizes will NOT work at all. */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 508 | const unsigned int chunksize = 0x200; |
| 509 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 510 | unsigned int bulklen; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 511 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 512 | dediprog_set_leds(LED_BUSY); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 513 | |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 514 | if (residue) { |
| 515 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 516 | start, residue); |
| 517 | ret = spi_read_chunked(flash, buf, start, residue, 16); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 518 | if (ret) |
| 519 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* Round down. */ |
| 523 | bulklen = (len - residue) / chunksize * chunksize; |
| 524 | ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, |
| 525 | bulklen); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 526 | if (ret) |
| 527 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 528 | |
| 529 | len -= residue + bulklen; |
| 530 | if (len) { |
| 531 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 532 | start, len); |
| 533 | ret = spi_read_chunked(flash, buf + residue + bulklen, |
| 534 | start + residue + bulklen, len, 16); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 535 | if (ret) |
| 536 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 539 | dediprog_set_leds(LED_PASS); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 540 | return 0; |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 541 | err: |
| 542 | dediprog_set_leds(LED_ERROR); |
| 543 | return ret; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 544 | } |
| 545 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 546 | /* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes. |
| 547 | * @chunksize length of data chunks, only 256 supported by now |
| 548 | * @start start address |
| 549 | * @len length |
| 550 | * @dedi_spi_cmd dediprog specific write command for spi bus |
| 551 | * @return 0 on success, 1 on failure |
| 552 | */ |
| 553 | static int dediprog_spi_bulk_write(struct flashchip *flash, const uint8_t *buf, unsigned int chunksize, |
| 554 | unsigned int start, unsigned int len, uint8_t dedi_spi_cmd) |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 555 | { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 556 | int ret, transferred; |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 557 | unsigned int i; |
| 558 | /* USB transfer size must be 512, other sizes will NOT work at all. |
| 559 | * chunksize is the real data size per USB bulk transfer. The remaining |
| 560 | * space in a USB bulk transfer must be filled with 0xff padding. |
| 561 | */ |
| 562 | const unsigned int count = len / chunksize; |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 563 | const unsigned char count_and_cmd_old[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd}; |
| 564 | const unsigned char count_and_cmd_new[] = { |
David Hendricks | 741efe2 | 2015-08-15 19:18:51 -0700 | [diff] [blame] | 565 | count & 0xff, |
| 566 | (count >> 8) & 0xff, |
| 567 | 0, /* used for 24-bit address support? */ |
| 568 | dedi_spi_cmd, |
| 569 | JEDEC_BYTE_PROGRAM, /* FIXME: needs to be determined based on byte 3? */ |
| 570 | 0, |
| 571 | start & 0xff, |
| 572 | (start >> 8) & 0xff, |
| 573 | (start >> 16) & 0xff, |
| 574 | (start >> 24) & 0xff, |
| 575 | }; |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 576 | unsigned char usbbuf[512]; |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * We should change this check to |
| 580 | * chunksize > 512 |
| 581 | * once we know how to handle different chunk sizes. |
| 582 | */ |
| 583 | if (chunksize != 256) { |
| 584 | msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n" |
| 585 | "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize); |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | if ((start % chunksize) || (len % chunksize)) { |
| 590 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 591 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 592 | return 1; |
| 593 | } |
| 594 | |
| 595 | /* No idea if the hardware can handle empty writes, so chicken out. */ |
| 596 | if (!len) |
| 597 | return 0; |
David Hendricks | 741efe2 | 2015-08-15 19:18:51 -0700 | [diff] [blame] | 598 | if (!is_new_prot()) { |
| 599 | /* Command Write SPI Bulk. No idea which write command is used on the |
| 600 | * SPI side. |
| 601 | */ |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 602 | ret = dediprog_write(CMD_WRITE, start % 0x10000, start / 0x10000, |
| 603 | count_and_cmd_old, sizeof(count_and_cmd_old)); |
David Hendricks | 741efe2 | 2015-08-15 19:18:51 -0700 | [diff] [blame] | 604 | if (ret != sizeof(count_and_cmd_old)) { |
| 605 | msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret, |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 606 | libusb_error_name(ret)); |
David Hendricks | 741efe2 | 2015-08-15 19:18:51 -0700 | [diff] [blame] | 607 | return 1; |
| 608 | } |
| 609 | } else { |
| 610 | /* Command Write SPI Bulk. No idea which write command is used on the |
| 611 | * SPI side. |
| 612 | */ |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 613 | ret = dediprog_write(CMD_WRITE, 0, 0, |
| 614 | count_and_cmd_new, sizeof(count_and_cmd_new)); |
David Hendricks | 741efe2 | 2015-08-15 19:18:51 -0700 | [diff] [blame] | 615 | if (ret != sizeof(count_and_cmd_new)) { |
| 616 | msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret, |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 617 | libusb_error_name(ret)); |
David Hendricks | 741efe2 | 2015-08-15 19:18:51 -0700 | [diff] [blame] | 618 | return 1; |
| 619 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | for (i = 0; i < count; i++) { |
| 623 | memset(usbbuf, 0xff, sizeof(usbbuf)); |
| 624 | memcpy(usbbuf, buf + i * chunksize, chunksize); |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 625 | ret = libusb_bulk_transfer(dediprog_handle, dediprog_out_endpoint, |
| 626 | usbbuf, 512, &transferred, DEFAULT_TIMEOUT); |
| 627 | if ((ret < 0) || (transferred != 512)) { |
| 628 | msg_perr("SPI bulk write failed, expected %i, got %i %s!\n", |
| 629 | 512, ret, libusb_error_name(ret)); |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 630 | return 1; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static int dediprog_spi_write(struct flashchip *flash, const uint8_t *buf, |
| 638 | unsigned int start, unsigned int len, uint8_t dedi_spi_cmd) |
| 639 | { |
| 640 | int ret; |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 641 | const unsigned int chunksize = flash->page_size; |
| 642 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 643 | unsigned int bulklen; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 644 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 645 | dediprog_set_leds(LED_BUSY); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 646 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 647 | if (chunksize != 256) { |
| 648 | msg_pdbg("Page sizes other than 256 bytes are unsupported as " |
| 649 | "we don't know how dediprog\nhandles them.\n"); |
| 650 | /* Write everything like it was residue. */ |
| 651 | residue = len; |
| 652 | } |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 653 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 654 | if (residue) { |
| 655 | msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n", |
| 656 | start, residue); |
| 657 | /* No idea about the real limit. Maybe 12, maybe more. */ |
| 658 | ret = spi_write_chunked(flash, (uint8_t *)buf, start, residue, 12); |
| 659 | if (ret) { |
| 660 | dediprog_set_leds(LED_ERROR); |
| 661 | return ret; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* Round down. */ |
| 666 | bulklen = (len - residue) / chunksize * chunksize; |
| 667 | ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd); |
| 668 | if (ret) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 669 | dediprog_set_leds(LED_ERROR); |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 670 | return ret; |
| 671 | } |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 672 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 673 | len -= residue + bulklen; |
| 674 | if (len) { |
| 675 | msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n", |
| 676 | start, len); |
| 677 | ret = spi_write_chunked(flash, (uint8_t *)(buf + residue + bulklen), |
| 678 | start + residue + bulklen, len, 12); |
| 679 | if (ret) { |
| 680 | dediprog_set_leds(LED_ERROR); |
| 681 | return ret; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | dediprog_set_leds(LED_PASS); |
| 686 | return 0; |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 687 | } |
| 688 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 689 | //static int dediprog_spi_write_256(struct flashchip *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
| 690 | static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len) |
| 691 | { |
| 692 | return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM); |
| 693 | } |
| 694 | |
| 695 | #if 0 |
| 696 | static int dediprog_spi_write_aai(struct flashchip *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
| 697 | { |
| 698 | return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI); |
| 699 | } |
| 700 | #endif |
| 701 | |
| 702 | //static int dediprog_spi_send_command(struct flashchip *flash, |
| 703 | static int dediprog_spi_send_command(unsigned int writecnt, |
| 704 | unsigned int readcnt, |
| 705 | const unsigned char *writearr, |
| 706 | unsigned char *readarr) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 707 | { |
| 708 | int ret; |
| 709 | |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 710 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 711 | if (writecnt > UINT16_MAX) { |
| 712 | msg_perr("Invalid writecnt=%i, aborting.\n", writecnt); |
| 713 | return 1; |
Simon Glass | 543101a | 2015-01-08 06:28:13 -0700 | [diff] [blame] | 714 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 715 | if (readcnt > UINT16_MAX) { |
| 716 | msg_perr("Invalid readcnt=%i, aborting.\n", readcnt); |
| 717 | return 1; |
| 718 | } |
| 719 | |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 720 | /* New protocol has the read flag as value while the old protocol had it in the index field. */ |
| 721 | if (is_new_prot()) { |
| 722 | ret = dediprog_write(CMD_TRANSCEIVE, readcnt ? 1 : 0, 0, writearr, writecnt); |
| 723 | } else { |
| 724 | ret = dediprog_write(CMD_TRANSCEIVE, 0, readcnt ? 1 : 0, writearr, writecnt); |
| 725 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 726 | if (ret != writecnt) { |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 727 | msg_perr("Send SPI failed, expected %i, got %i %s!\n", |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 728 | writecnt, ret, libusb_error_name(ret)); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 729 | return 1; |
| 730 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 731 | if (readcnt == 0) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 732 | return 0; |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 733 | |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 734 | ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 735 | if (ret != readcnt) { |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 736 | msg_perr("Receive SPI failed, expected %i, got %i %s!\n", |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 737 | readcnt, ret, libusb_error_name(ret)); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 738 | return 1; |
| 739 | } |
| 740 | return 0; |
| 741 | } |
| 742 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 743 | static int dediprog_check_devicestring(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 744 | { |
| 745 | int ret; |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 746 | int fw[3]; |
David Hendricks | 6702e07 | 2015-08-15 18:09:04 -0700 | [diff] [blame] | 747 | int sfnum; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 748 | char buf[0x11]; |
| 749 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 750 | /* Command Receive Device String. */ |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 751 | ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 752 | if (ret != 0x10) { |
| 753 | msg_perr("Incomplete/failed Command Receive Device String!\n"); |
| 754 | return 1; |
| 755 | } |
| 756 | buf[0x10] = '\0'; |
| 757 | msg_pdbg("Found a %s\n", buf); |
David Hendricks | 6702e07 | 2015-08-15 18:09:04 -0700 | [diff] [blame] | 758 | if (memcmp(buf, "SF100", 0x5) == 0) |
| 759 | dediprog_devicetype = DEV_SF100; |
| 760 | else if (memcmp(buf, "SF600", 0x5) == 0) |
| 761 | dediprog_devicetype = DEV_SF600; |
| 762 | else { |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 763 | msg_perr("Device not a SF100!\n"); |
| 764 | return 1; |
| 765 | } |
David Hendricks | 6702e07 | 2015-08-15 18:09:04 -0700 | [diff] [blame] | 766 | if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) |
| 767 | != 4 || sfnum != dediprog_devicetype) { |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 768 | msg_perr("Unexpected firmware version string '%s'\n", buf); |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 769 | return 1; |
| 770 | } |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 771 | /* Only these major versions were tested. */ |
David Hendricks | 6702e07 | 2015-08-15 18:09:04 -0700 | [diff] [blame] | 772 | if (fw[0] < 2 || fw[0] > 7) { |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 773 | msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 774 | return 1; |
| 775 | } |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 776 | dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]); |
David Hendricks | 14e82e5 | 2015-08-15 17:59:03 -0700 | [diff] [blame] | 777 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 778 | return 0; |
| 779 | } |
| 780 | |
David Hendricks | 28fbdfb | 2015-08-15 18:04:37 -0700 | [diff] [blame] | 781 | /* |
| 782 | * This command presumably sets the voltage for the SF100 itself (not the |
| 783 | * SPI flash). Only use this command with firmware older than V6.0.0. Newer |
| 784 | * (including all SF600s) do not support it. |
| 785 | */ |
| 786 | static int dediprog_set_voltage(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 787 | { |
| 788 | int ret; |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 789 | unsigned char buf[0x1]; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 790 | |
| 791 | memset(buf, 0, sizeof(buf)); |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 792 | ret = dediprog_write(CMD_SET_VOLTAGE, 0x0, 0x0, buf, 0x1); |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 793 | if (ret < 0) { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 794 | msg_perr("Command A failed (%s)!\n", libusb_error_name(ret)); |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 795 | return 1; |
| 796 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 797 | if ((ret != 0x1) || (buf[0] != 0x6f)) { |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame] | 798 | msg_perr("Unexpected response to init!\n"); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 799 | return 1; |
| 800 | } |
David Hendricks | 28fbdfb | 2015-08-15 18:04:37 -0700 | [diff] [blame] | 801 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 805 | #if 0 |
| 806 | /* Something. |
| 807 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 808 | * Always preceded by Command Receive Device String |
| 809 | */ |
| 810 | static int dediprog_command_b(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 811 | { |
| 812 | int ret; |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 813 | char buf[0x3]; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 814 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 815 | ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00, |
| 816 | buf, 0x3, DEFAULT_TIMEOUT); |
| 817 | if (ret < 0) { |
| 818 | msg_perr("Command B failed (%s)!\n", usb_strerror()); |
| 819 | return 1; |
| 820 | } |
| 821 | if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) || |
| 822 | (buf[2] != 0xff)) { |
| 823 | msg_perr("Unexpected response to Command B!\n"); |
| 824 | return 1; |
| 825 | } |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | #endif |
| 830 | |
| 831 | static int set_target_flash(enum dediprog_target target) |
| 832 | { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 833 | int ret = dediprog_write(CMD_SET_TARGET, target, 0, NULL, 0); |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 834 | if (ret != 0) { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 835 | msg_perr("set_target_flash failed (%s)!\n", libusb_error_name(ret)); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 836 | return 1; |
| 837 | } |
| 838 | return 0; |
| 839 | } |
| 840 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 841 | #if 0 |
| 842 | /* Returns true if the button is currently pressed. */ |
| 843 | static bool dediprog_get_button(void) |
Simon Glass | d01002b | 2015-01-08 05:44:16 -0700 | [diff] [blame] | 844 | { |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 845 | char buf[1]; |
| 846 | int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0, |
| 847 | buf, 0x1, DEFAULT_TIMEOUT); |
| 848 | if (ret != 0) { |
| 849 | msg_perr("Could not get button state (%s)!\n", usb_strerror()); |
| 850 | return 1; |
Simon Glass | d01002b | 2015-01-08 05:44:16 -0700 | [diff] [blame] | 851 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 852 | return buf[0] != 1; |
Simon Glass | d01002b | 2015-01-08 05:44:16 -0700 | [diff] [blame] | 853 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 854 | #endif |
Simon Glass | d01002b | 2015-01-08 05:44:16 -0700 | [diff] [blame] | 855 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 856 | static int parse_voltage(char *voltage) |
| 857 | { |
| 858 | char *tmp = NULL; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 859 | int i; |
| 860 | int millivolt = 0, fraction = 0; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 861 | |
| 862 | if (!voltage || !strlen(voltage)) { |
| 863 | msg_perr("Empty voltage= specified.\n"); |
| 864 | return -1; |
| 865 | } |
| 866 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 867 | voltage = tmp; |
| 868 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 869 | * to be in decimal notation. |
| 870 | */ |
| 871 | if ((*voltage == '.') || (*voltage == ',')) { |
| 872 | voltage++; |
| 873 | for (i = 0; i < 3; i++) { |
| 874 | fraction *= 10; |
| 875 | /* Don't advance if the current character is invalid, |
| 876 | * but continue multiplying. |
| 877 | */ |
| 878 | if ((*voltage < '0') || (*voltage > '9')) |
| 879 | continue; |
| 880 | fraction += *voltage - '0'; |
| 881 | voltage++; |
| 882 | } |
| 883 | /* Throw away remaining digits. */ |
| 884 | voltage += strspn(voltage, "0123456789"); |
| 885 | } |
| 886 | /* The remaining string must be empty or "mV" or "V". */ |
| 887 | tolower_string(voltage); |
| 888 | |
| 889 | /* No unit or "V". */ |
| 890 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 891 | millivolt *= 1000; |
| 892 | millivolt += fraction; |
| 893 | } else if (!strncmp(voltage, "mv", 2) || |
| 894 | !strncmp(voltage, "milliv", 6)) { |
| 895 | /* No adjustment. fraction is discarded. */ |
| 896 | } else { |
| 897 | /* Garbage at the end of the string. */ |
| 898 | msg_perr("Garbage voltage= specified.\n"); |
| 899 | return -1; |
| 900 | } |
| 901 | return millivolt; |
| 902 | } |
| 903 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 904 | #if 0 |
| 905 | static const struct spi_master spi_master_dediprog = { |
| 906 | .type = SPI_CONTROLLER_DEDIPROG, |
| 907 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 908 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 909 | .command = dediprog_spi_send_command, |
| 910 | .multicommand = default_spi_send_multicommand, |
| 911 | .read = dediprog_spi_read, |
| 912 | .write_256 = dediprog_spi_write_256, |
| 913 | .write_aai = dediprog_spi_write_aai, |
| 914 | }; |
| 915 | #endif |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 916 | static const struct spi_programmer spi_programmer_dediprog = { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 917 | .type = SPI_CONTROLLER_DEDIPROG, |
| 918 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 919 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 920 | .command = dediprog_spi_send_command, |
| 921 | .multicommand = default_spi_send_multicommand, |
| 922 | .read = dediprog_spi_read, |
| 923 | .write_256 = dediprog_spi_write_256, |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 924 | }; |
| 925 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 926 | static int dediprog_shutdown(void *data) |
| 927 | { |
| 928 | msg_pspew("%s\n", __func__); |
| 929 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 930 | dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0); |
David Hendricks | 6702e07 | 2015-08-15 18:09:04 -0700 | [diff] [blame] | 931 | dediprog_devicetype = DEV_UNKNOWN; |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 932 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 933 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 934 | if (dediprog_set_spi_voltage(0x0)) |
| 935 | return 1; |
| 936 | |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 937 | if (libusb_release_interface(dediprog_handle, 0)) { |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 938 | msg_perr("Could not release USB interface!\n"); |
| 939 | return 1; |
| 940 | } |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 941 | libusb_close(dediprog_handle); |
| 942 | libusb_exit(usb_ctx); |
| 943 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 944 | return 0; |
| 945 | } |
| 946 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 947 | /* URB numbers refer to the first log ever captured. */ |
| 948 | int dediprog_init(void) |
| 949 | { |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 950 | char *voltage, *device, *spispeed, *target_str; |
| 951 | int spispeed_idx = 1; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 952 | int millivolt = 3500; |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 953 | long usedevice = 0; |
| 954 | long target = 1; |
| 955 | int i, ret; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 956 | |
| 957 | msg_pspew("%s\n", __func__); |
| 958 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 959 | spispeed = extract_programmer_param("spispeed"); |
| 960 | if (spispeed) { |
| 961 | for (i = 0; spispeeds[i].name; ++i) { |
| 962 | if (!strcasecmp(spispeeds[i].name, spispeed)) { |
| 963 | spispeed_idx = i; |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | if (!spispeeds[i].name) { |
| 968 | msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed); |
| 969 | free(spispeed); |
| 970 | return 1; |
| 971 | } |
| 972 | free(spispeed); |
| 973 | } |
| 974 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 975 | voltage = extract_programmer_param("voltage"); |
| 976 | if (voltage) { |
| 977 | millivolt = parse_voltage(voltage); |
| 978 | free(voltage); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 979 | if (millivolt < 0) |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 980 | return 1; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 981 | msg_pinfo("Setting voltage to %i mV\n", millivolt); |
| 982 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 983 | |
| 984 | device = extract_programmer_param("device"); |
| 985 | if (device) { |
| 986 | char *dev_suffix; |
| 987 | errno = 0; |
| 988 | usedevice = strtol(device, &dev_suffix, 10); |
| 989 | if (errno != 0 || device == dev_suffix) { |
| 990 | msg_perr("Error: Could not convert 'device'.\n"); |
| 991 | free(device); |
Simon Glass | d01002b | 2015-01-08 05:44:16 -0700 | [diff] [blame] | 992 | return 1; |
| 993 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 994 | if (usedevice < 0 || usedevice > UINT_MAX) { |
| 995 | msg_perr("Error: Value for 'device' is out of range.\n"); |
| 996 | free(device); |
| 997 | return 1; |
| 998 | } |
| 999 | if (strlen(dev_suffix) > 0) { |
| 1000 | msg_perr("Error: Garbage following 'device' value.\n"); |
| 1001 | free(device); |
| 1002 | return 1; |
| 1003 | } |
| 1004 | msg_pinfo("Using device %li.\n", usedevice); |
Simon Glass | d01002b | 2015-01-08 05:44:16 -0700 | [diff] [blame] | 1005 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1006 | free(device); |
| 1007 | |
| 1008 | target_str = extract_programmer_param("target"); |
| 1009 | if (target_str) { |
| 1010 | char *target_suffix; |
| 1011 | errno = 0; |
| 1012 | target = strtol(target_str, &target_suffix, 10); |
| 1013 | if (errno != 0 || target_str == target_suffix) { |
| 1014 | msg_perr("Error: Could not convert 'target'.\n"); |
| 1015 | free(target_str); |
| 1016 | return 1; |
| 1017 | } |
| 1018 | if (target < 1 || target > 2) { |
| 1019 | msg_perr("Error: Value for 'target' is out of range.\n"); |
| 1020 | free(target_str); |
| 1021 | return 1; |
| 1022 | } |
| 1023 | if (strlen(target_suffix) > 0) { |
| 1024 | msg_perr("Error: Garbage following 'target' value.\n"); |
| 1025 | free(target_str); |
| 1026 | return 1; |
| 1027 | } |
| 1028 | msg_pinfo("Using target %li.\n", target); |
| 1029 | } |
| 1030 | free(target_str); |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 1031 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1032 | /* Here comes the USB stuff. */ |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 1033 | libusb_init(&usb_ctx); |
| 1034 | if (!usb_ctx) { |
| 1035 | msg_perr("Could not initialize libusb!\n"); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1036 | return 1; |
| 1037 | } |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 1038 | dediprog_handle = get_device_by_vid_pid_number(0x0483, 0xdada, (unsigned int) usedevice); |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1039 | if (!dediprog_handle) { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 1040 | msg_perr("Could not find a Dediprog SF100 on USB!\n"); |
| 1041 | libusb_exit(usb_ctx); |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1042 | return 1; |
| 1043 | } |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 1044 | ret = libusb_set_configuration(dediprog_handle, 1); |
| 1045 | if (ret != 0) { |
| 1046 | msg_perr("Could not set USB device configuration: %i %s\n", ret, libusb_error_name(ret)); |
| 1047 | libusb_close(dediprog_handle); |
| 1048 | libusb_exit(usb_ctx); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 1049 | return 1; |
| 1050 | } |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 1051 | ret = libusb_claim_interface(dediprog_handle, 0); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 1052 | if (ret < 0) { |
David Hendricks | f9ecb45 | 2015-11-04 15:40:27 -0800 | [diff] [blame^] | 1053 | msg_perr("Could not claim USB device interface %i: %i %s\n", 0, ret, libusb_error_name(ret)); |
| 1054 | libusb_close(dediprog_handle); |
| 1055 | libusb_exit(usb_ctx); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 1056 | return 1; |
| 1057 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1058 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 1059 | if (register_shutdown(dediprog_shutdown, NULL)) |
| 1060 | return 1; |
| 1061 | |
David Hendricks | 28fbdfb | 2015-08-15 18:04:37 -0700 | [diff] [blame] | 1062 | /* Try reading the devicestring. If that fails and the device is old |
| 1063 | * (FW < 6.0.0) then we need to try the "set voltage" command and then |
| 1064 | * attempt to read the devicestring again. */ |
| 1065 | if (dediprog_check_devicestring()) { |
| 1066 | if (dediprog_set_voltage()) |
| 1067 | return 1; |
| 1068 | if (dediprog_check_devicestring()) |
| 1069 | return 1; |
| 1070 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1071 | |
David Hendricks | d746858 | 2015-11-12 15:21:12 -0800 | [diff] [blame] | 1072 | /* SF100 firmware exposes only one endpoint for in/out, SF600 firmware |
| 1073 | exposes separate endpoints for in and out. */ |
| 1074 | dediprog_in_endpoint = 2; |
| 1075 | if (dediprog_devicetype == DEV_SF100) |
| 1076 | dediprog_out_endpoint = 2; |
| 1077 | else |
| 1078 | dediprog_out_endpoint = 1; |
| 1079 | |
| 1080 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1081 | /* Set all possible LEDs as soon as possible to indicate activity. |
| 1082 | * Because knowing the firmware version is required to set the LEDs correctly we need to this after |
| 1083 | * dediprog_setup() has queried the device and set dediprog_firmwareversion. */ |
| 1084 | dediprog_set_leds(LED_ALL); |
| 1085 | |
| 1086 | /* Select target/socket, frequency and VCC. */ |
| 1087 | if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) || |
| 1088 | dediprog_set_spi_speed(spispeed_idx) || |
| 1089 | dediprog_set_spi_voltage(millivolt)) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 1090 | dediprog_set_leds(LED_ERROR); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1091 | return 1; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 1092 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1093 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1094 | // register_spi_master(&spi_master_dediprog); |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 1095 | register_spi_programmer(&spi_programmer_dediprog); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1096 | |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1097 | dediprog_set_leds(LED_NONE); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 1098 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1099 | return 0; |
| 1100 | } |
David Hendricks | fe05e74 | 2015-08-15 17:29:39 -0700 | [diff] [blame] | 1101 | |