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