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