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