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