Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 2015 Simon Glass |
| 6 | * Copyright (C) 2015 Stefan Tauner |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Stefan Tauner | 0cbd8c2 | 2015-01-26 22:06:04 +0000 | [diff] [blame] | 22 | #include "platform.h" |
| 23 | |
Stefan Tauner | 5c316f9 | 2015-02-08 21:57:52 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 26 | #include <string.h> |
Stefan Tauner | e34e3e8 | 2013-01-01 00:06:51 +0000 | [diff] [blame] | 27 | #include <limits.h> |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Stefan Tauner | 0cbd8c2 | 2015-01-26 22:06:04 +0000 | [diff] [blame] | 29 | |
| 30 | #if IS_WINDOWS |
| 31 | #include <lusb0_usb.h> |
| 32 | #else |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 33 | #include <usb.h> |
Stefan Tauner | 0cbd8c2 | 2015-01-26 22:06:04 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 36 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 37 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 38 | #include "programmer.h" |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 39 | #include "spi.h" |
| 40 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 41 | #define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 42 | #define DEFAULT_TIMEOUT 3000 |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 43 | #define REQTYPE_OTHER_OUT (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* 0x43 */ |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 44 | #define REQTYPE_OTHER_IN (USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER) /* 0xC3 */ |
| 45 | #define REQTYPE_EP_OUT (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT) /* 0x42 */ |
| 46 | #define REQTYPE_EP_IN (USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT) /* 0xC2 */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 47 | static usb_dev_handle *dediprog_handle; |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 48 | static int dediprog_in_endpoint; |
| 49 | static int dediprog_out_endpoint; |
| 50 | |
| 51 | enum dediprog_devtype { |
| 52 | DEV_UNKNOWN = 0, |
| 53 | DEV_SF100 = 100, |
| 54 | DEV_SF600 = 600, |
| 55 | }; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 56 | |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 57 | enum dediprog_leds { |
| 58 | LED_INVALID = -1, |
| 59 | LED_NONE = 0, |
| 60 | LED_PASS = 1 << 0, |
| 61 | LED_BUSY = 1 << 1, |
| 62 | LED_ERROR = 1 << 2, |
| 63 | LED_ALL = 7, |
| 64 | }; |
| 65 | |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 66 | /* IO bits for CMD_SET_IO_LED message */ |
| 67 | enum dediprog_ios { |
| 68 | IO1 = 1 << 0, |
| 69 | IO2 = 1 << 1, |
| 70 | IO3 = 1 << 2, |
| 71 | IO4 = 1 << 3, |
| 72 | }; |
| 73 | |
| 74 | enum dediprog_cmds { |
| 75 | CMD_TRANSCEIVE = 0x01, |
| 76 | CMD_POLL_STATUS_REG = 0x02, |
| 77 | CMD_SET_VPP = 0x03, |
| 78 | CMD_SET_TARGET = 0x04, |
| 79 | CMD_READ_EEPROM = 0x05, |
| 80 | CMD_WRITE_EEPROM = 0x06, |
| 81 | CMD_SET_IO_LED = 0x07, |
| 82 | CMD_READ_PROG_INFO = 0x08, |
| 83 | CMD_SET_VCC = 0x09, |
| 84 | CMD_SET_STANDALONE = 0x0A, |
| 85 | CMD_GET_BUTTON = 0x11, |
| 86 | CMD_GET_UID = 0x12, |
| 87 | CMD_SET_CS = 0x14, |
| 88 | CMD_IO_MODE = 0x15, |
| 89 | CMD_FW_UPDATE = 0x1A, |
| 90 | CMD_FPGA_UPDATE = 0x1B, |
| 91 | CMD_READ_FPGA_VERSION = 0x1C, |
| 92 | CMD_SET_HOLD = 0x1D, |
| 93 | CMD_READ = 0x20, |
| 94 | CMD_WRITE = 0x30, |
| 95 | CMD_WRITE_AT45DB = 0x31, |
| 96 | CMD_NAND_WRITE = 0x32, |
| 97 | CMD_NAND_READ = 0x33, |
| 98 | CMD_SET_SPI_CLK = 0x61, |
| 99 | CMD_CHECK_SOCKET = 0x62, |
| 100 | CMD_DOWNLOAD_PRJ = 0x63, |
| 101 | CMD_READ_PRJ_NAME = 0x64, |
| 102 | // New protocol/firmware only |
| 103 | CMD_CHECK_SDCARD = 0x65, |
| 104 | CMD_READ_PRJ = 0x66, |
| 105 | }; |
| 106 | |
| 107 | enum dediprog_target { |
| 108 | FLASH_TYPE_APPLICATION_FLASH_1 = 0, |
| 109 | FLASH_TYPE_FLASH_CARD, |
| 110 | FLASH_TYPE_APPLICATION_FLASH_2, |
| 111 | FLASH_TYPE_SOCKET, |
| 112 | }; |
| 113 | |
| 114 | enum dediprog_readmode { |
| 115 | READ_MODE_STD = 1, |
| 116 | READ_MODE_FAST = 2, |
| 117 | READ_MODE_ATMEL45 = 3, |
| 118 | READ_MODE_4B_ADDR_FAST = 4, |
| 119 | READ_MODE_4B_ADDR_FAST_0x0C = 5, /* New protocol only */ |
| 120 | }; |
| 121 | |
| 122 | enum dediprog_writemode { |
| 123 | WRITE_MODE_PAGE_PGM = 1, |
| 124 | WRITE_MODE_PAGE_WRITE = 2, |
| 125 | WRITE_MODE_1B_AAI = 3, |
| 126 | WRITE_MODE_2B_AAI = 4, |
| 127 | WRITE_MODE_128B_PAGE = 5, |
| 128 | WRITE_MODE_PAGE_AT26DF041 = 6, |
| 129 | WRITE_MODE_SILICON_BLUE_FPGA = 7, |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 130 | WRITE_MODE_64B_PAGE_NUMONYX_PCM = 8, /* unit of 512 bytes */ |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 131 | WRITE_MODE_4B_ADDR_256B_PAGE_PGM = 9, |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 132 | WRITE_MODE_32B_PAGE_PGM_MXIC_512K = 10, /* unit of 512 bytes */ |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 133 | WRITE_MODE_4B_ADDR_256B_PAGE_PGM_0x12 = 11, |
| 134 | WRITE_MODE_4B_ADDR_256B_PAGE_PGM_FLAGS = 12, |
| 135 | }; |
| 136 | |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 137 | enum dediprog_standalone_mode { |
| 138 | ENTER_STANDALONE_MODE = 0, |
| 139 | LEAVE_STANDALONE_MODE = 1, |
| 140 | }; |
| 141 | |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 142 | |
| 143 | static int dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0); |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 144 | enum dediprog_devtype dediprog_devicetype = DEV_UNKNOWN; |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 145 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 146 | /* Returns true if firmware (and thus hardware) supports the "new" protocol */ |
| 147 | static bool is_new_prot(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 148 | { |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 149 | switch (dediprog_devicetype) { |
| 150 | case DEV_SF100: |
| 151 | return dediprog_firmwareversion >= FIRMWARE_VERSION(5, 5, 0); |
| 152 | case DEV_SF600: |
| 153 | return dediprog_firmwareversion >= FIRMWARE_VERSION(6, 9, 0); |
| 154 | default: |
| 155 | return 0; |
| 156 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 157 | } |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 158 | |
| 159 | static int dediprog_read(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, uint8_t *bytes, size_t size) |
| 160 | { |
| 161 | return usb_control_msg(dediprog_handle, REQTYPE_EP_IN, cmd, value, idx, |
| 162 | (char *)bytes, size, DEFAULT_TIMEOUT); |
| 163 | } |
| 164 | |
| 165 | static int dediprog_write(enum dediprog_cmds cmd, unsigned int value, unsigned int idx, const uint8_t *bytes, size_t size) |
| 166 | { |
| 167 | return usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, cmd, value, idx, |
| 168 | (char *)bytes, size, DEFAULT_TIMEOUT); |
| 169 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 170 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 171 | /* Might be useful for other USB devices as well. static for now. */ |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 172 | /* device parameter allows user to specify one device of multiple installed */ |
| 173 | static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 174 | { |
| 175 | struct usb_bus *bus; |
| 176 | struct usb_device *dev; |
| 177 | |
| 178 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 179 | for (dev = bus->devices; dev; dev = dev->next) |
| 180 | if ((dev->descriptor.idVendor == vid) && |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 181 | (dev->descriptor.idProduct == pid)) { |
| 182 | if (device == 0) |
| 183 | return dev; |
| 184 | device--; |
| 185 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 186 | |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 190 | /* This function sets the GPIOs connected to the LEDs as well as IO1-IO4. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 191 | static int dediprog_set_leds(int leds) |
| 192 | { |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 193 | if (leds < LED_NONE || leds > LED_ALL) |
| 194 | leds = LED_ALL; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 195 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 196 | /* Older Dediprogs with 2.x.x and 3.x.x firmware only had two LEDs, assigned to different bits. So map |
| 197 | * them around if we have an old device. On those devices the LEDs map as follows: |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 198 | * bit 2 == 0: green light is on. |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 199 | * bit 0 == 0: red light is on. |
| 200 | * |
| 201 | * Additionally, the command structure has changed with the "new" protocol. |
| 202 | * |
| 203 | * FIXME: take IO pins into account |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 204 | */ |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 205 | int target_leds, ret; |
| 206 | if (is_new_prot()) { |
| 207 | target_leds = (leds ^ 7) << 8; |
| 208 | ret = dediprog_write(CMD_SET_IO_LED, target_leds, 0, NULL, 0); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 209 | } else { |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 210 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) { |
| 211 | target_leds = ((leds & LED_ERROR) >> 2) | ((leds & LED_PASS) << 2); |
| 212 | } else { |
| 213 | target_leds = leds; |
| 214 | } |
| 215 | target_leds ^= 7; |
| 216 | |
| 217 | ret = dediprog_write(CMD_SET_IO_LED, 0x9, target_leds, NULL, 0); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 220 | if (ret != 0x0) { |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 221 | msg_perr("Command Set LED 0x%x failed (%s)!\n", leds, usb_strerror()); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 222 | return 1; |
| 223 | } |
| 224 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 228 | static int dediprog_set_spi_voltage(int millivolt) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 229 | { |
| 230 | int ret; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 231 | uint16_t voltage_selector; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 232 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 233 | switch (millivolt) { |
| 234 | case 0: |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 235 | /* Admittedly this one is an assumption. */ |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 236 | voltage_selector = 0x0; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 237 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 238 | case 1800: |
| 239 | voltage_selector = 0x12; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 240 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 241 | case 2500: |
| 242 | voltage_selector = 0x11; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 243 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 244 | case 3500: |
| 245 | voltage_selector = 0x10; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 246 | break; |
| 247 | default: |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 248 | msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 249 | return 1; |
| 250 | } |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 251 | msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000, |
| 252 | millivolt % 1000); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 253 | |
Nico Huber | 4099a8a | 2012-06-16 00:02:27 +0000 | [diff] [blame] | 254 | if (voltage_selector == 0) { |
| 255 | /* Wait some time as the original driver does. */ |
| 256 | programmer_delay(200 * 1000); |
| 257 | } |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 258 | ret = dediprog_write(CMD_SET_VCC, voltage_selector, 0, NULL, 0); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 259 | if (ret != 0x0) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 260 | msg_perr("Command Set SPI Voltage 0x%x failed!\n", |
| 261 | voltage_selector); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 262 | return 1; |
| 263 | } |
Nico Huber | 4099a8a | 2012-06-16 00:02:27 +0000 | [diff] [blame] | 264 | if (voltage_selector != 0) { |
| 265 | /* Wait some time as the original driver does. */ |
| 266 | programmer_delay(200 * 1000); |
| 267 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 271 | struct dediprog_spispeeds { |
| 272 | const char *const name; |
| 273 | const int speed; |
| 274 | }; |
| 275 | |
| 276 | static const struct dediprog_spispeeds spispeeds[] = { |
| 277 | { "24M", 0x0 }, |
| 278 | { "12M", 0x2 }, |
| 279 | { "8M", 0x1 }, |
| 280 | { "3M", 0x3 }, |
| 281 | { "2.18M", 0x4 }, |
| 282 | { "1.5M", 0x5 }, |
| 283 | { "750k", 0x6 }, |
| 284 | { "375k", 0x7 }, |
| 285 | { NULL, 0x0 }, |
| 286 | }; |
| 287 | |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 288 | static int dediprog_set_spi_speed(unsigned int spispeed_idx) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 289 | { |
Patrick Georgi | efe2d43 | 2013-05-23 21:47:46 +0000 | [diff] [blame] | 290 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) { |
| 291 | msg_pwarn("Skipping to set SPI speed because firmware is too old.\n"); |
| 292 | return 0; |
| 293 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 294 | |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 295 | const struct dediprog_spispeeds *spispeed = &spispeeds[spispeed_idx]; |
| 296 | msg_pdbg("SPI speed is %sHz\n", spispeed->name); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 297 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 298 | int ret = dediprog_write(CMD_SET_SPI_CLK, spispeed->speed, 0, NULL, 0); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 299 | if (ret != 0x0) { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 300 | msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeed->speed); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 301 | return 1; |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 306 | static void fill_rw_cmd_payload(uint8_t *data_packet, unsigned int count, uint8_t dedi_spi_cmd, unsigned int *value, unsigned int *idx, unsigned int start) { |
| 307 | /* First 5 bytes are common in both generations. */ |
| 308 | data_packet[0] = count & 0xff; |
| 309 | data_packet[1] = (count >> 8) & 0xff; |
| 310 | data_packet[2] = 0; /* RFU */ |
| 311 | data_packet[3] = dedi_spi_cmd; /* Read/Write Mode (currently READ_MODE_STD, WRITE_MODE_PAGE_PGM or WRITE_MODE_2B_AAI) */ |
| 312 | data_packet[4] = 0; /* "Opcode". Specs imply necessity only for READ_MODE_4B_ADDR_FAST and WRITE_MODE_4B_ADDR_256B_PAGE_PGM */ |
| 313 | |
| 314 | if (is_new_prot()) { |
| 315 | *value = *idx = 0; |
| 316 | data_packet[5] = 0; /* RFU */ |
| 317 | data_packet[6] = (start >> 0) & 0xff; |
| 318 | data_packet[7] = (start >> 8) & 0xff; |
| 319 | data_packet[8] = (start >> 16) & 0xff; |
| 320 | data_packet[9] = (start >> 24) & 0xff; |
| 321 | } else { |
| 322 | *value = start % 0x10000; |
| 323 | *idx = start / 0x10000; |
| 324 | } |
| 325 | } |
| 326 | |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 327 | /* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes. |
| 328 | * @start start address |
| 329 | * @len length |
| 330 | * @return 0 on success, 1 on failure |
| 331 | */ |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 332 | static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 333 | { |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 334 | /* chunksize must be 512, other sizes will NOT work at all. */ |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 335 | const unsigned int chunksize = 512; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 336 | const unsigned int count = len / chunksize; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 337 | |
| 338 | if ((start % chunksize) || (len % chunksize)) { |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 339 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug at flashrom@flashrom.org\n", |
| 340 | __func__, start, len); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 341 | return 1; |
| 342 | } |
| 343 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 344 | if (len == 0) |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 345 | return 0; |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 346 | |
| 347 | /* Command packet size of protocols: new 10 B, old 5 B. */ |
| 348 | uint8_t data_packet[is_new_prot() ? 10 : 5]; |
| 349 | unsigned int value, idx; |
| 350 | fill_rw_cmd_payload(data_packet, count, READ_MODE_STD, &value, &idx, start); |
| 351 | |
| 352 | int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet)); |
| 353 | if (ret != sizeof(data_packet)) { |
| 354 | msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, usb_strerror()); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 355 | return 1; |
| 356 | } |
| 357 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 358 | unsigned int i; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 359 | for (i = 0; i < count; i++) { |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 360 | ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_in_endpoint, |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 361 | (char *)buf + i * chunksize, chunksize, |
| 362 | DEFAULT_TIMEOUT); |
| 363 | if (ret != chunksize) { |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 364 | msg_perr("SPI bulk read %i failed, expected %i, got %i %s!\n", |
| 365 | i, chunksize, ret, usb_strerror()); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 366 | return 1; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 373 | static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 374 | { |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 375 | int ret; |
| 376 | /* chunksize must be 512, other sizes will NOT work at all. */ |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 377 | const unsigned int chunksize = 0x200; |
| 378 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 379 | unsigned int bulklen; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 380 | |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 381 | dediprog_set_leds(LED_BUSY); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 382 | |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 383 | if (residue) { |
| 384 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 385 | start, residue); |
| 386 | ret = spi_read_chunked(flash, buf, start, residue, 16); |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 387 | if (ret) |
| 388 | goto err; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* Round down. */ |
| 392 | bulklen = (len - residue) / chunksize * chunksize; |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 393 | ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, bulklen); |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 394 | if (ret) |
| 395 | goto err; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 396 | |
| 397 | len -= residue + bulklen; |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 398 | if (len != 0) { |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 399 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 400 | start, len); |
| 401 | ret = spi_read_chunked(flash, buf + residue + bulklen, |
| 402 | start + residue + bulklen, len, 16); |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 403 | if (ret) |
| 404 | goto err; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 407 | dediprog_set_leds(LED_PASS); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 408 | return 0; |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 409 | err: |
| 410 | dediprog_set_leds(LED_ERROR); |
| 411 | return ret; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 414 | /* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes. |
| 415 | * @chunksize length of data chunks, only 256 supported by now |
| 416 | * @start start address |
| 417 | * @len length |
| 418 | * @dedi_spi_cmd dediprog specific write command for spi bus |
| 419 | * @return 0 on success, 1 on failure |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 420 | */ |
Stefan Tauner | ffb0cf6 | 2014-05-25 07:47:47 +0000 | [diff] [blame] | 421 | static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize, |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 422 | unsigned int start, unsigned int len, uint8_t dedi_spi_cmd) |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 423 | { |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 424 | /* USB transfer size must be 512, other sizes will NOT work at all. |
| 425 | * chunksize is the real data size per USB bulk transfer. The remaining |
| 426 | * space in a USB bulk transfer must be filled with 0xff padding. |
| 427 | */ |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 428 | const unsigned int count = len / chunksize; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 429 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 430 | /* |
| 431 | * We should change this check to |
| 432 | * chunksize > 512 |
| 433 | * once we know how to handle different chunk sizes. |
| 434 | */ |
| 435 | if (chunksize != 256) { |
| 436 | msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n" |
| 437 | "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize); |
| 438 | return 1; |
| 439 | } |
| 440 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 441 | if ((start % chunksize) || (len % chunksize)) { |
| 442 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 443 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | /* No idea if the hardware can handle empty writes, so chicken out. */ |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 448 | if (len == 0) |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 449 | return 0; |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 450 | |
| 451 | /* Command packet size of protocols: new 10 B, old 5 B. */ |
| 452 | uint8_t data_packet[is_new_prot() ? 10 : 5]; |
| 453 | unsigned int value, idx; |
| 454 | fill_rw_cmd_payload(data_packet, count, dedi_spi_cmd, &value, &idx, start); |
| 455 | int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet)); |
| 456 | if (ret != sizeof(data_packet)) { |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 457 | msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret, |
| 458 | usb_strerror()); |
| 459 | return 1; |
| 460 | } |
| 461 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 462 | unsigned int i; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 463 | for (i = 0; i < count; i++) { |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 464 | char usbbuf[512]; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 465 | memcpy(usbbuf, buf + i * chunksize, chunksize); |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 466 | memset(usbbuf + chunksize, 0xff, sizeof(usbbuf) - chunksize); // fill up with 0xFF |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 467 | ret = usb_bulk_write(dediprog_handle, dediprog_out_endpoint, usbbuf, 512, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 468 | if (ret != 512) { |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 469 | msg_perr("SPI bulk write failed, expected %i, got %i %s!\n", 512, ret, usb_strerror()); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 470 | return 1; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Stefan Tauner | ffb0cf6 | 2014-05-25 07:47:47 +0000 | [diff] [blame] | 477 | static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf, |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 478 | unsigned int start, unsigned int len, uint8_t dedi_spi_cmd) |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 479 | { |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 480 | int ret; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 481 | const unsigned int chunksize = flash->chip->page_size; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 482 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 483 | unsigned int bulklen; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 484 | |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 485 | dediprog_set_leds(LED_BUSY); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 486 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 487 | if (chunksize != 256) { |
| 488 | msg_pdbg("Page sizes other than 256 bytes are unsupported as " |
| 489 | "we don't know how dediprog\nhandles them.\n"); |
| 490 | /* Write everything like it was residue. */ |
| 491 | residue = len; |
| 492 | } |
| 493 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 494 | if (residue) { |
| 495 | msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n", |
| 496 | start, residue); |
| 497 | /* No idea about the real limit. Maybe 12, maybe more. */ |
| 498 | ret = spi_write_chunked(flash, buf, start, residue, 12); |
| 499 | if (ret) { |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 500 | dediprog_set_leds(LED_ERROR); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 501 | return ret; |
| 502 | } |
| 503 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 504 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 505 | /* Round down. */ |
| 506 | bulklen = (len - residue) / chunksize * chunksize; |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 507 | ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 508 | if (ret) { |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 509 | dediprog_set_leds(LED_ERROR); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 510 | return ret; |
| 511 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 512 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 513 | len -= residue + bulklen; |
| 514 | if (len) { |
| 515 | msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n", |
| 516 | start, len); |
| 517 | ret = spi_write_chunked(flash, buf + residue + bulklen, |
| 518 | start + residue + bulklen, len, 12); |
| 519 | if (ret) { |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 520 | dediprog_set_leds(LED_ERROR); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 521 | return ret; |
| 522 | } |
| 523 | } |
| 524 | |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 525 | dediprog_set_leds(LED_PASS); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 526 | return 0; |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Stefan Tauner | ffb0cf6 | 2014-05-25 07:47:47 +0000 | [diff] [blame] | 529 | static int dediprog_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 530 | { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 531 | return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_PAGE_PGM); |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Stefan Tauner | ffb0cf6 | 2014-05-25 07:47:47 +0000 | [diff] [blame] | 534 | static int dediprog_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 535 | { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 536 | return dediprog_spi_write(flash, buf, start, len, WRITE_MODE_2B_AAI); |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 539 | static int dediprog_spi_send_command(struct flashctx *flash, |
| 540 | unsigned int writecnt, |
| 541 | unsigned int readcnt, |
| 542 | const unsigned char *writearr, |
| 543 | unsigned char *readarr) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 544 | { |
| 545 | int ret; |
| 546 | |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 547 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 548 | if (writecnt > flash->mst->spi.max_data_write) { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 549 | msg_perr("Invalid writecnt=%i, aborting.\n", writecnt); |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 550 | return 1; |
| 551 | } |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 552 | if (readcnt > flash->mst->spi.max_data_read) { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 553 | msg_perr("Invalid readcnt=%i, aborting.\n", readcnt); |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 554 | return 1; |
| 555 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 556 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 557 | unsigned int idx, value; |
| 558 | /* New protocol has options and timeout combined as value while the old one used the value field for |
| 559 | * timeout and the index field for options. */ |
| 560 | if (is_new_prot()) { |
| 561 | idx = 0; |
| 562 | value = readcnt ? 0x1 : 0x0; // Indicate if we require a read |
| 563 | } else { |
| 564 | idx = readcnt ? 0x1 : 0x0; // Indicate if we require a read |
| 565 | value = 0; |
| 566 | } |
| 567 | ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 568 | if (ret != writecnt) { |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 569 | msg_perr("Send SPI failed, expected %i, got %i %s!\n", |
| 570 | writecnt, ret, usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 571 | return 1; |
| 572 | } |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 573 | if (readcnt == 0) // If we don't require a response, we are done here |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 574 | return 0; |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 575 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 576 | const uint8_t read_timeout = 10 + readcnt/512; |
| 577 | if (is_new_prot()) { |
| 578 | idx = 0; |
| 579 | value = min(read_timeout, 0xFF) | (0 << 8) ; // Timeout in lower byte, option in upper byte |
| 580 | } else { |
| 581 | idx = (0 & 0xFF); // Lower byte is option (0x01 = require SR, 0x02 keep CS low) |
| 582 | value = min(read_timeout, 0xFF); // Possibly two bytes but we play safe here |
| 583 | } |
| 584 | ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 585 | if (ret != readcnt) { |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 586 | msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 587 | return 1; |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 592 | static int dediprog_check_devicestring(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 593 | { |
| 594 | int ret; |
| 595 | char buf[0x11]; |
| 596 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 597 | /* Command Receive Device String. */ |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 598 | ret = dediprog_read(CMD_READ_PROG_INFO, 0, 0, (uint8_t *)buf, 0x10); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 599 | if (ret != 0x10) { |
| 600 | msg_perr("Incomplete/failed Command Receive Device String!\n"); |
| 601 | return 1; |
| 602 | } |
| 603 | buf[0x10] = '\0'; |
| 604 | msg_pdbg("Found a %s\n", buf); |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 605 | if (memcmp(buf, "SF100", 0x5) == 0) |
| 606 | dediprog_devicetype = DEV_SF100; |
| 607 | else if (memcmp(buf, "SF600", 0x5) == 0) |
| 608 | dediprog_devicetype = DEV_SF600; |
| 609 | else { |
| 610 | msg_perr("Device not a SF100 or SF600!\n"); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 611 | return 1; |
| 612 | } |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 613 | |
| 614 | int sfnum; |
| 615 | int fw[3]; |
| 616 | if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 || |
| 617 | sfnum != dediprog_devicetype) { |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 618 | msg_perr("Unexpected firmware version string '%s'\n", buf); |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 619 | return 1; |
| 620 | } |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 621 | /* Only these major versions were tested. */ |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 622 | if (fw[0] < 2 || fw[0] > 7) { |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 623 | msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], fw[1], fw[2]); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 624 | return 1; |
| 625 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 626 | dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]); |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 627 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 631 | static int dediprog_device_init(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 632 | { |
| 633 | int ret; |
| 634 | char buf[0x1]; |
| 635 | |
| 636 | memset(buf, 0, sizeof(buf)); |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 637 | ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x0B, 0x0, 0x0, |
| 638 | buf, 0x1, DEFAULT_TIMEOUT); |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 639 | if (ret < 0) { |
| 640 | msg_perr("Command A failed (%s)!\n", usb_strerror()); |
| 641 | return 1; |
| 642 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 643 | if ((ret != 0x1) || (buf[0] != 0x6f)) { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 644 | msg_perr("Unexpected response to init!\n"); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 645 | return 1; |
| 646 | } |
| 647 | return 0; |
| 648 | } |
| 649 | |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 650 | static int dediprog_standalone_mode(void) |
| 651 | { |
| 652 | int ret; |
| 653 | |
| 654 | if (dediprog_devicetype != DEV_SF600) |
| 655 | return 0; |
| 656 | |
| 657 | msg_pdbg2("Disabling standalone mode.\n"); |
| 658 | ret = dediprog_write(CMD_SET_STANDALONE, LEAVE_STANDALONE_MODE, 0, NULL, 0); |
| 659 | if (ret) { |
| 660 | msg_perr("Failed to disable standalone mode: %s\n", usb_strerror()); |
| 661 | return 1; |
| 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 667 | #if 0 |
| 668 | /* Something. |
| 669 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 670 | * Always preceded by Command Receive Device String |
| 671 | */ |
| 672 | static int dediprog_command_b(void) |
| 673 | { |
| 674 | int ret; |
| 675 | char buf[0x3]; |
| 676 | |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 677 | ret = usb_control_msg(dediprog_handle, REQTYPE_OTHER_IN, 0x7, 0x0, 0xef00, |
| 678 | buf, 0x3, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 679 | if (ret < 0) { |
| 680 | msg_perr("Command B failed (%s)!\n", usb_strerror()); |
| 681 | return 1; |
| 682 | } |
| 683 | if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) || |
| 684 | (buf[2] != 0xff)) { |
| 685 | msg_perr("Unexpected response to Command B!\n"); |
| 686 | return 1; |
| 687 | } |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | #endif |
| 692 | |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 693 | static int set_target_flash(enum dediprog_target target) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 694 | { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 695 | int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_OUT, CMD_SET_TARGET, target, 0, |
| 696 | NULL, 0, DEFAULT_TIMEOUT); |
| 697 | if (ret != 0) { |
| 698 | msg_perr("set_target_flash failed (%s)!\n", usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 699 | return 1; |
| 700 | } |
| 701 | return 0; |
| 702 | } |
| 703 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 704 | #if 0 |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 705 | /* Returns true if the button is currently pressed. */ |
| 706 | static bool dediprog_get_button(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 707 | { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 708 | char buf[1]; |
| 709 | int ret = usb_control_msg(dediprog_handle, REQTYPE_EP_IN, CMD_GET_BUTTON, 0, 0, |
| 710 | buf, 0x1, DEFAULT_TIMEOUT); |
| 711 | if (ret != 0) { |
| 712 | msg_perr("Could not get button state (%s)!\n", usb_strerror()); |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 713 | return 1; |
| 714 | } |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 715 | return buf[0] != 1; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 716 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 717 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 718 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 719 | static int parse_voltage(char *voltage) |
| 720 | { |
| 721 | char *tmp = NULL; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 722 | int i; |
| 723 | int millivolt = 0, fraction = 0; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 724 | |
| 725 | if (!voltage || !strlen(voltage)) { |
| 726 | msg_perr("Empty voltage= specified.\n"); |
| 727 | return -1; |
| 728 | } |
| 729 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 730 | voltage = tmp; |
| 731 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 732 | * to be in decimal notation. |
| 733 | */ |
| 734 | if ((*voltage == '.') || (*voltage == ',')) { |
| 735 | voltage++; |
| 736 | for (i = 0; i < 3; i++) { |
| 737 | fraction *= 10; |
| 738 | /* Don't advance if the current character is invalid, |
| 739 | * but continue multiplying. |
| 740 | */ |
| 741 | if ((*voltage < '0') || (*voltage > '9')) |
| 742 | continue; |
| 743 | fraction += *voltage - '0'; |
| 744 | voltage++; |
| 745 | } |
| 746 | /* Throw away remaining digits. */ |
| 747 | voltage += strspn(voltage, "0123456789"); |
| 748 | } |
| 749 | /* The remaining string must be empty or "mV" or "V". */ |
| 750 | tolower_string(voltage); |
| 751 | |
| 752 | /* No unit or "V". */ |
| 753 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 754 | millivolt *= 1000; |
| 755 | millivolt += fraction; |
| 756 | } else if (!strncmp(voltage, "mv", 2) || |
| 757 | !strncmp(voltage, "milliv", 6)) { |
| 758 | /* No adjustment. fraction is discarded. */ |
| 759 | } else { |
| 760 | /* Garbage at the end of the string. */ |
| 761 | msg_perr("Garbage voltage= specified.\n"); |
| 762 | return -1; |
| 763 | } |
| 764 | return millivolt; |
| 765 | } |
| 766 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 767 | static const struct spi_master spi_master_dediprog = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 768 | .type = SPI_CONTROLLER_DEDIPROG, |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 769 | .max_data_read = 16, /* 18 seems to work fine as well, but 19 times out sometimes with FW 5.15. */ |
| 770 | .max_data_write = 16, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 771 | .command = dediprog_spi_send_command, |
| 772 | .multicommand = default_spi_send_multicommand, |
| 773 | .read = dediprog_spi_read, |
| 774 | .write_256 = dediprog_spi_write_256, |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 775 | .write_aai = dediprog_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 776 | }; |
| 777 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 778 | static int dediprog_shutdown(void *data) |
| 779 | { |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 780 | dediprog_firmwareversion = FIRMWARE_VERSION(0, 0, 0); |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 781 | dediprog_devicetype = DEV_UNKNOWN; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 782 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 783 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 784 | if (dediprog_set_spi_voltage(0x0)) |
| 785 | return 1; |
| 786 | |
| 787 | if (usb_release_interface(dediprog_handle, 0)) { |
| 788 | msg_perr("Could not release USB interface!\n"); |
| 789 | return 1; |
| 790 | } |
| 791 | if (usb_close(dediprog_handle)) { |
| 792 | msg_perr("Could not close USB device!\n"); |
| 793 | return 1; |
| 794 | } |
| 795 | return 0; |
| 796 | } |
| 797 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 798 | int dediprog_init(void) |
| 799 | { |
| 800 | struct usb_device *dev; |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 801 | char *voltage, *device, *spispeed, *target_str; |
Patrick Georgi | efe2d43 | 2013-05-23 21:47:46 +0000 | [diff] [blame] | 802 | int spispeed_idx = 1; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 803 | int millivolt = 3500; |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 804 | long usedevice = 0; |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 805 | long target = 1; |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 806 | int i, ret; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 807 | |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 808 | spispeed = extract_programmer_param("spispeed"); |
| 809 | if (spispeed) { |
| 810 | for (i = 0; spispeeds[i].name; ++i) { |
| 811 | if (!strcasecmp(spispeeds[i].name, spispeed)) { |
| 812 | spispeed_idx = i; |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | if (!spispeeds[i].name) { |
Patrick Georgi | efe2d43 | 2013-05-23 21:47:46 +0000 | [diff] [blame] | 817 | msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed); |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 818 | free(spispeed); |
| 819 | return 1; |
| 820 | } |
| 821 | free(spispeed); |
| 822 | } |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 823 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 824 | voltage = extract_programmer_param("voltage"); |
| 825 | if (voltage) { |
| 826 | millivolt = parse_voltage(voltage); |
| 827 | free(voltage); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 828 | if (millivolt < 0) |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 829 | return 1; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 830 | msg_pinfo("Setting voltage to %i mV\n", millivolt); |
| 831 | } |
| 832 | |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 833 | device = extract_programmer_param("device"); |
| 834 | if (device) { |
| 835 | char *dev_suffix; |
| 836 | errno = 0; |
| 837 | usedevice = strtol(device, &dev_suffix, 10); |
| 838 | if (errno != 0 || device == dev_suffix) { |
| 839 | msg_perr("Error: Could not convert 'device'.\n"); |
| 840 | free(device); |
| 841 | return 1; |
| 842 | } |
| 843 | if (usedevice < 0 || usedevice > UINT_MAX) { |
| 844 | msg_perr("Error: Value for 'device' is out of range.\n"); |
| 845 | free(device); |
| 846 | return 1; |
| 847 | } |
| 848 | if (strlen(dev_suffix) > 0) { |
| 849 | msg_perr("Error: Garbage following 'device' value.\n"); |
| 850 | free(device); |
| 851 | return 1; |
| 852 | } |
| 853 | msg_pinfo("Using device %li.\n", usedevice); |
| 854 | } |
| 855 | free(device); |
| 856 | |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 857 | target_str = extract_programmer_param("target"); |
| 858 | if (target_str) { |
| 859 | char *target_suffix; |
| 860 | errno = 0; |
| 861 | target = strtol(target_str, &target_suffix, 10); |
| 862 | if (errno != 0 || target_str == target_suffix) { |
| 863 | msg_perr("Error: Could not convert 'target'.\n"); |
| 864 | free(target_str); |
| 865 | return 1; |
| 866 | } |
| 867 | if (target < 1 || target > 2) { |
| 868 | msg_perr("Error: Value for 'target' is out of range.\n"); |
| 869 | free(target_str); |
| 870 | return 1; |
| 871 | } |
| 872 | if (strlen(target_suffix) > 0) { |
| 873 | msg_perr("Error: Garbage following 'target' value.\n"); |
| 874 | free(target_str); |
| 875 | return 1; |
| 876 | } |
| 877 | msg_pinfo("Using target %li.\n", target); |
| 878 | } |
| 879 | free(target_str); |
| 880 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 881 | /* Here comes the USB stuff. */ |
| 882 | usb_init(); |
| 883 | usb_find_busses(); |
| 884 | usb_find_devices(); |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 885 | dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 886 | if (!dev) { |
| 887 | msg_perr("Could not find a Dediprog SF100 on USB!\n"); |
| 888 | return 1; |
| 889 | } |
| 890 | msg_pdbg("Found USB device (%04x:%04x).\n", |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 891 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 892 | dediprog_handle = usb_open(dev); |
David Woodhouse | d2a7e87 | 2013-07-30 09:34:44 +0000 | [diff] [blame] | 893 | if (!dediprog_handle) { |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 894 | msg_perr("Could not find a Dediprog programmer on USB: %s\n", usb_strerror()); |
David Woodhouse | d2a7e87 | 2013-07-30 09:34:44 +0000 | [diff] [blame] | 895 | return 1; |
| 896 | } |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 897 | ret = usb_set_configuration(dediprog_handle, 1); |
| 898 | if (ret < 0) { |
| 899 | msg_perr("Could not set USB device configuration: %i %s\n", |
| 900 | ret, usb_strerror()); |
| 901 | if (usb_close(dediprog_handle)) |
| 902 | msg_perr("Could not close USB device!\n"); |
| 903 | return 1; |
| 904 | } |
| 905 | ret = usb_claim_interface(dediprog_handle, 0); |
| 906 | if (ret < 0) { |
| 907 | msg_perr("Could not claim USB device interface %i: %i %s\n", |
| 908 | 0, ret, usb_strerror()); |
| 909 | if (usb_close(dediprog_handle)) |
| 910 | msg_perr("Could not close USB device!\n"); |
| 911 | return 1; |
| 912 | } |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 913 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 914 | if (register_shutdown(dediprog_shutdown, NULL)) |
| 915 | return 1; |
| 916 | |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 917 | /* Perform basic setup. */ |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 918 | if (dediprog_device_init()) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 919 | return 1; |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 920 | if (dediprog_check_devicestring()) |
| 921 | return 1; |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 922 | |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 923 | /* SF100 only has 1 endpoint for in/out, SF600 uses two separate endpoints instead. */ |
| 924 | dediprog_in_endpoint = 2; |
| 925 | if (dediprog_devicetype == DEV_SF100) |
| 926 | dediprog_out_endpoint = 2; |
| 927 | else |
| 928 | dediprog_out_endpoint = 1; |
| 929 | |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 930 | /* Set all possible LEDs as soon as possible to indicate activity. |
| 931 | * Because knowing the firmware version is required to set the LEDs correctly we need to this after |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 932 | * dediprog_check_devicestring() has queried the device and set dediprog_firmwareversion. */ |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 933 | dediprog_set_leds(LED_ALL); |
| 934 | |
Simon Glass | 557eb4f | 2015-07-05 16:53:22 +0000 | [diff] [blame] | 935 | /* Select target/socket, frequency and VCC. */ |
| 936 | if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1) || |
| 937 | dediprog_set_spi_speed(spispeed_idx) || |
| 938 | dediprog_set_spi_voltage(millivolt)) { |
Simon Glass | 25e9f40 | 2015-06-28 13:31:19 +0000 | [diff] [blame] | 939 | dediprog_set_leds(LED_ERROR); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 940 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 941 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 942 | |
David Woodhouse | 7f3b89f | 2016-02-18 21:42:15 +0000 | [diff] [blame^] | 943 | if (dediprog_standalone_mode()) |
| 944 | return 1; |
| 945 | |
Simon Glass | ae61651 | 2016-01-23 23:27:58 +0000 | [diff] [blame] | 946 | if (register_spi_master(&spi_master_dediprog) || dediprog_set_leds(LED_NONE)) |
| 947 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 948 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 949 | return 0; |
| 950 | } |
| 951 | |