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