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