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