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