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