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