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