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