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