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