Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
Stefan Tauner | 0cbd8c2 | 2015-01-26 22:06:04 +0000 | [diff] [blame^] | 20 | #include "platform.h" |
| 21 | |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 23 | #include <string.h> |
Stefan Tauner | e34e3e8 | 2013-01-01 00:06:51 +0000 | [diff] [blame] | 24 | #include <limits.h> |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Stefan Tauner | 0cbd8c2 | 2015-01-26 22:06:04 +0000 | [diff] [blame^] | 26 | |
| 27 | #if IS_WINDOWS |
| 28 | #include <lusb0_usb.h> |
| 29 | #else |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 30 | #include <usb.h> |
Stefan Tauner | 0cbd8c2 | 2015-01-26 22:06:04 +0000 | [diff] [blame^] | 31 | #endif |
| 32 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 33 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 34 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 35 | #include "programmer.h" |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 36 | #include "spi.h" |
| 37 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 38 | #define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 39 | #define DEFAULT_TIMEOUT 3000 |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 40 | static usb_dev_handle *dediprog_handle; |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 41 | static int dediprog_firmwareversion; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 42 | static int dediprog_endpoint; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 43 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 44 | #define DEDI_SPI_CMD_PAGEWRITE 0x1 |
| 45 | #define DEDI_SPI_CMD_AAIWRITE 0x4 |
| 46 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 47 | #if 0 |
| 48 | /* Might be useful for other pieces of code as well. */ |
| 49 | static void print_hex(void *buf, size_t len) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 50 | { |
| 51 | size_t i; |
| 52 | |
| 53 | for (i = 0; i < len; i++) |
| 54 | msg_pdbg(" %02x", ((uint8_t *)buf)[i]); |
| 55 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 56 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 57 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 58 | /* Might be useful for other USB devices as well. static for now. */ |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 59 | /* device parameter allows user to specify one device of multiple installed */ |
| 60 | static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 61 | { |
| 62 | struct usb_bus *bus; |
| 63 | struct usb_device *dev; |
| 64 | |
| 65 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 66 | for (dev = bus->devices; dev; dev = dev->next) |
| 67 | if ((dev->descriptor.idVendor == vid) && |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 68 | (dev->descriptor.idProduct == pid)) { |
| 69 | if (device == 0) |
| 70 | return dev; |
| 71 | device--; |
| 72 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | //int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); |
| 78 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 79 | /* Set/clear LEDs on dediprog */ |
| 80 | #define PASS_ON (0 << 0) |
| 81 | #define PASS_OFF (1 << 0) |
| 82 | #define BUSY_ON (0 << 1) |
| 83 | #define BUSY_OFF (1 << 1) |
| 84 | #define ERROR_ON (0 << 2) |
| 85 | #define ERROR_OFF (1 << 2) |
| 86 | static int current_led_status = -1; |
| 87 | |
| 88 | static int dediprog_set_leds(int leds) |
| 89 | { |
| 90 | int ret, target_leds; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 91 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 92 | if (leds < 0 || leds > 7) |
| 93 | leds = 0; // Bogus value, enable all LEDs |
| 94 | |
| 95 | if (leds == current_led_status) |
| 96 | return 0; |
| 97 | |
| 98 | /* Older Dediprogs with 2.x.x and 3.x.x firmware only had |
| 99 | * two LEDs, and they were reversed. So map them around if |
| 100 | * we have an old device. On those devices the LEDs map as |
| 101 | * follows: |
| 102 | * bit 2 == 0: green light is on. |
| 103 | * bit 0 == 0: red light is on. |
| 104 | */ |
| 105 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) { |
| 106 | target_leds = ((leds & ERROR_OFF) >> 2) | |
| 107 | ((leds & PASS_OFF) << 2); |
| 108 | } else { |
| 109 | target_leds = leds; |
| 110 | } |
| 111 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 112 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds, |
| 113 | NULL, 0x0, DEFAULT_TIMEOUT); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 114 | if (ret != 0x0) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 115 | msg_perr("Command Set LED 0x%x failed (%s)!\n", |
| 116 | leds, usb_strerror()); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | current_led_status = leds; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 125 | static int dediprog_set_spi_voltage(int millivolt) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 126 | { |
| 127 | int ret; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 128 | uint16_t voltage_selector; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 129 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 130 | switch (millivolt) { |
| 131 | case 0: |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 132 | /* Admittedly this one is an assumption. */ |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 133 | voltage_selector = 0x0; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 134 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 135 | case 1800: |
| 136 | voltage_selector = 0x12; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 137 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 138 | case 2500: |
| 139 | voltage_selector = 0x11; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 140 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 141 | case 3500: |
| 142 | voltage_selector = 0x10; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 143 | break; |
| 144 | default: |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 145 | msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 146 | return 1; |
| 147 | } |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 148 | msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000, |
| 149 | millivolt % 1000); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 150 | |
Nico Huber | 4099a8a | 2012-06-16 00:02:27 +0000 | [diff] [blame] | 151 | if (voltage_selector == 0) { |
| 152 | /* Wait some time as the original driver does. */ |
| 153 | programmer_delay(200 * 1000); |
| 154 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 155 | ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector, |
| 156 | 0xff, NULL, 0x0, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 157 | if (ret != 0x0) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 158 | msg_perr("Command Set SPI Voltage 0x%x failed!\n", |
| 159 | voltage_selector); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 160 | return 1; |
| 161 | } |
Nico Huber | 4099a8a | 2012-06-16 00:02:27 +0000 | [diff] [blame] | 162 | if (voltage_selector != 0) { |
| 163 | /* Wait some time as the original driver does. */ |
| 164 | programmer_delay(200 * 1000); |
| 165 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 169 | struct dediprog_spispeeds { |
| 170 | const char *const name; |
| 171 | const int speed; |
| 172 | }; |
| 173 | |
| 174 | static const struct dediprog_spispeeds spispeeds[] = { |
| 175 | { "24M", 0x0 }, |
| 176 | { "12M", 0x2 }, |
| 177 | { "8M", 0x1 }, |
| 178 | { "3M", 0x3 }, |
| 179 | { "2.18M", 0x4 }, |
| 180 | { "1.5M", 0x5 }, |
| 181 | { "750k", 0x6 }, |
| 182 | { "375k", 0x7 }, |
| 183 | { NULL, 0x0 }, |
| 184 | }; |
| 185 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 186 | /* After dediprog_set_spi_speed, the original app always calls |
| 187 | * dediprog_set_spi_voltage(0) and then |
| 188 | * dediprog_check_devicestring() four times in a row. |
| 189 | * After that, dediprog_command_a() is called. |
| 190 | * This looks suspiciously like the microprocessor in the SF100 has to be |
| 191 | * restarted/reinitialized in case the speed changes. |
| 192 | */ |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 193 | static int dediprog_set_spi_speed(unsigned int spispeed_idx) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 194 | { |
Patrick Georgi | efe2d43 | 2013-05-23 21:47:46 +0000 | [diff] [blame] | 195 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) { |
| 196 | msg_pwarn("Skipping to set SPI speed because firmware is too old.\n"); |
| 197 | return 0; |
| 198 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 199 | |
Patrick Georgi | efe2d43 | 2013-05-23 21:47:46 +0000 | [diff] [blame] | 200 | msg_pdbg("SPI speed is %s Hz\n", spispeeds[spispeed_idx].name); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 201 | |
Patrick Georgi | efe2d43 | 2013-05-23 21:47:46 +0000 | [diff] [blame] | 202 | int ret = usb_control_msg(dediprog_handle, 0x42, 0x61, spispeeds[spispeed_idx].speed, 0xff, |
| 203 | NULL, 0x0, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 204 | if (ret != 0x0) { |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 205 | msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeeds[spispeed_idx].speed); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 206 | return 1; |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 211 | /* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes. |
| 212 | * @start start address |
| 213 | * @len length |
| 214 | * @return 0 on success, 1 on failure |
| 215 | */ |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 216 | static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 217 | unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 218 | { |
| 219 | int ret; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 220 | unsigned int i; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 221 | /* chunksize must be 512, other sizes will NOT work at all. */ |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 222 | const unsigned int chunksize = 0x200; |
| 223 | const unsigned int count = len / chunksize; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 224 | const char count_and_chunk[] = {count & 0xff, |
| 225 | (count >> 8) & 0xff, |
| 226 | chunksize & 0xff, |
| 227 | (chunksize >> 8) & 0xff}; |
| 228 | |
| 229 | if ((start % chunksize) || (len % chunksize)) { |
| 230 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 231 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | /* No idea if the hardware can handle empty reads, so chicken out. */ |
| 236 | if (!len) |
| 237 | return 0; |
| 238 | /* Command Read SPI Bulk. No idea which read command is used on the |
| 239 | * SPI side. |
| 240 | */ |
| 241 | ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000, |
| 242 | start / 0x10000, (char *)count_and_chunk, |
| 243 | sizeof(count_and_chunk), DEFAULT_TIMEOUT); |
| 244 | if (ret != sizeof(count_and_chunk)) { |
| 245 | msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, |
| 246 | usb_strerror()); |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | for (i = 0; i < count; i++) { |
| 251 | ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint, |
| 252 | (char *)buf + i * chunksize, chunksize, |
| 253 | DEFAULT_TIMEOUT); |
| 254 | if (ret != chunksize) { |
| 255 | msg_perr("SPI bulk read %i failed, expected %i, got %i " |
| 256 | "%s!\n", i, chunksize, ret, usb_strerror()); |
| 257 | return 1; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 264 | static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 265 | unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 266 | { |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 267 | int ret; |
| 268 | /* chunksize must be 512, other sizes will NOT work at all. */ |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 269 | const unsigned int chunksize = 0x200; |
| 270 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 271 | unsigned int bulklen; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 272 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 273 | dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF); |
| 274 | |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 275 | if (residue) { |
| 276 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 277 | start, residue); |
| 278 | ret = spi_read_chunked(flash, buf, start, residue, 16); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 279 | if (ret) { |
| 280 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 281 | return ret; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 282 | } |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* Round down. */ |
| 286 | bulklen = (len - residue) / chunksize * chunksize; |
| 287 | ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, |
| 288 | bulklen); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 289 | if (ret) { |
| 290 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 291 | return ret; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 292 | } |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 293 | |
| 294 | len -= residue + bulklen; |
| 295 | if (len) { |
| 296 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 297 | start, len); |
| 298 | ret = spi_read_chunked(flash, buf + residue + bulklen, |
| 299 | start + residue + bulklen, len, 16); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 300 | if (ret) { |
| 301 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 302 | return ret; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 303 | } |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 306 | dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 307 | return 0; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 310 | /* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes. |
| 311 | * @chunksize length of data chunks, only 256 supported by now |
| 312 | * @start start address |
| 313 | * @len length |
| 314 | * @dedi_spi_cmd dediprog specific write command for spi bus |
| 315 | * @return 0 on success, 1 on failure |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 316 | */ |
Stefan Tauner | ffb0cf6 | 2014-05-25 07:47:47 +0000 | [diff] [blame] | 317 | static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize, |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 318 | unsigned int start, unsigned int len, uint8_t dedi_spi_cmd) |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 319 | { |
| 320 | int ret; |
| 321 | unsigned int i; |
| 322 | /* USB transfer size must be 512, other sizes will NOT work at all. |
| 323 | * chunksize is the real data size per USB bulk transfer. The remaining |
| 324 | * space in a USB bulk transfer must be filled with 0xff padding. |
| 325 | */ |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 326 | const unsigned int count = len / chunksize; |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 327 | const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd}; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 328 | char usbbuf[512]; |
| 329 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 330 | /* |
| 331 | * We should change this check to |
| 332 | * chunksize > 512 |
| 333 | * once we know how to handle different chunk sizes. |
| 334 | */ |
| 335 | if (chunksize != 256) { |
| 336 | msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n" |
| 337 | "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize); |
| 338 | return 1; |
| 339 | } |
| 340 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 341 | if ((start % chunksize) || (len % chunksize)) { |
| 342 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 343 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 344 | return 1; |
| 345 | } |
| 346 | |
| 347 | /* No idea if the hardware can handle empty writes, so chicken out. */ |
| 348 | if (!len) |
| 349 | return 0; |
| 350 | /* Command Write SPI Bulk. No idea which write command is used on the |
| 351 | * SPI side. |
| 352 | */ |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 353 | ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000, start / 0x10000, |
| 354 | (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT); |
| 355 | if (ret != sizeof(count_and_cmd)) { |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 356 | msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret, |
| 357 | usb_strerror()); |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | for (i = 0; i < count; i++) { |
| 362 | memset(usbbuf, 0xff, sizeof(usbbuf)); |
| 363 | memcpy(usbbuf, buf + i * chunksize, chunksize); |
| 364 | ret = usb_bulk_write(dediprog_handle, dediprog_endpoint, |
| 365 | usbbuf, 512, |
| 366 | DEFAULT_TIMEOUT); |
| 367 | if (ret != 512) { |
| 368 | msg_perr("SPI bulk write failed, expected %i, got %i " |
| 369 | "%s!\n", 512, ret, usb_strerror()); |
| 370 | return 1; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Stefan Tauner | ffb0cf6 | 2014-05-25 07:47:47 +0000 | [diff] [blame] | 377 | static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf, |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 378 | unsigned int start, unsigned int len, uint8_t dedi_spi_cmd) |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 379 | { |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 380 | int ret; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 381 | const unsigned int chunksize = flash->chip->page_size; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 382 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 383 | unsigned int bulklen; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 384 | |
| 385 | dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF); |
| 386 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 387 | if (chunksize != 256) { |
| 388 | msg_pdbg("Page sizes other than 256 bytes are unsupported as " |
| 389 | "we don't know how dediprog\nhandles them.\n"); |
| 390 | /* Write everything like it was residue. */ |
| 391 | residue = len; |
| 392 | } |
| 393 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 394 | if (residue) { |
| 395 | msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n", |
| 396 | start, residue); |
| 397 | /* No idea about the real limit. Maybe 12, maybe more. */ |
| 398 | ret = spi_write_chunked(flash, buf, start, residue, 12); |
| 399 | if (ret) { |
| 400 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
| 401 | return ret; |
| 402 | } |
| 403 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 404 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 405 | /* Round down. */ |
| 406 | bulklen = (len - residue) / chunksize * chunksize; |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 407 | ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 408 | if (ret) { |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 409 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 410 | return ret; |
| 411 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 412 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 413 | len -= residue + bulklen; |
| 414 | if (len) { |
| 415 | msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n", |
| 416 | start, len); |
| 417 | ret = spi_write_chunked(flash, buf + residue + bulklen, |
| 418 | start + residue + bulklen, len, 12); |
| 419 | if (ret) { |
| 420 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
| 421 | return ret; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF); |
| 426 | return 0; |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Stefan Tauner | ffb0cf6 | 2014-05-25 07:47:47 +0000 | [diff] [blame] | 429 | static int dediprog_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 430 | { |
| 431 | return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_PAGEWRITE); |
| 432 | } |
| 433 | |
Stefan Tauner | ffb0cf6 | 2014-05-25 07:47:47 +0000 | [diff] [blame] | 434 | static int dediprog_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 435 | { |
| 436 | return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_AAIWRITE); |
| 437 | } |
| 438 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 439 | static int dediprog_spi_send_command(struct flashctx *flash, |
| 440 | unsigned int writecnt, |
| 441 | unsigned int readcnt, |
| 442 | const unsigned char *writearr, |
| 443 | unsigned char *readarr) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 444 | { |
| 445 | int ret; |
| 446 | |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 447 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 448 | /* Paranoid, but I don't want to be blamed if anything explodes. */ |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 449 | if (writecnt > 16) { |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 450 | msg_perr("Untested writecnt=%i, aborting.\n", writecnt); |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 451 | return 1; |
| 452 | } |
| 453 | /* 16 byte reads should work. */ |
| 454 | if (readcnt > 16) { |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 455 | msg_perr("Untested readcnt=%i, aborting.\n", readcnt); |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 456 | return 1; |
| 457 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 458 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 459 | ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, |
| 460 | readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, |
| 461 | DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 462 | if (ret != writecnt) { |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 463 | msg_perr("Send SPI failed, expected %i, got %i %s!\n", |
| 464 | writecnt, ret, usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 465 | return 1; |
| 466 | } |
| 467 | if (!readcnt) |
| 468 | return 0; |
| 469 | memset(readarr, 0, readcnt); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 470 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, |
| 471 | (char *)readarr, readcnt, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 472 | if (ret != readcnt) { |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 473 | msg_perr("Receive SPI failed, expected %i, got %i %s!\n", |
| 474 | readcnt, ret, usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 475 | return 1; |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 480 | static int dediprog_check_devicestring(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 481 | { |
| 482 | int ret; |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 483 | int fw[3]; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 484 | char buf[0x11]; |
| 485 | |
| 486 | /* Command Prepare Receive Device String. */ |
| 487 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 488 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, |
| 489 | 0x1, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 490 | /* The char casting is needed to stop gcc complaining about an always true comparison. */ |
| 491 | if ((ret != 0x1) || (buf[0] != (char)0xff)) { |
| 492 | msg_perr("Unexpected response to Command Prepare Receive Device" |
| 493 | " String!\n"); |
| 494 | return 1; |
| 495 | } |
| 496 | /* Command Receive Device String. */ |
| 497 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 498 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, |
| 499 | 0x10, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 500 | if (ret != 0x10) { |
| 501 | msg_perr("Incomplete/failed Command Receive Device String!\n"); |
| 502 | return 1; |
| 503 | } |
| 504 | buf[0x10] = '\0'; |
| 505 | msg_pdbg("Found a %s\n", buf); |
| 506 | if (memcmp(buf, "SF100", 0x5)) { |
| 507 | msg_perr("Device not a SF100!\n"); |
| 508 | return 1; |
| 509 | } |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 510 | if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) { |
| 511 | msg_perr("Unexpected firmware version string!\n"); |
| 512 | return 1; |
| 513 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 514 | /* Only these versions were tested. */ |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 515 | if (fw[0] < 2 || fw[0] > 5) { |
| 516 | msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], |
| 517 | fw[1], fw[2]); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 518 | return 1; |
| 519 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 520 | dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* Command A seems to be some sort of device init. It is either followed by |
| 525 | * dediprog_check_devicestring (often) or Command A (often) or |
| 526 | * Command F (once). |
| 527 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 528 | static int dediprog_command_a(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 529 | { |
| 530 | int ret; |
| 531 | char buf[0x1]; |
| 532 | |
| 533 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 534 | ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, |
| 535 | 0x1, DEFAULT_TIMEOUT); |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 536 | if (ret < 0) { |
| 537 | msg_perr("Command A failed (%s)!\n", usb_strerror()); |
| 538 | return 1; |
| 539 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 540 | if ((ret != 0x1) || (buf[0] != 0x6f)) { |
| 541 | msg_perr("Unexpected response to Command A!\n"); |
| 542 | return 1; |
| 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 547 | #if 0 |
| 548 | /* Something. |
| 549 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 550 | * Always preceded by Command Receive Device String |
| 551 | */ |
| 552 | static int dediprog_command_b(void) |
| 553 | { |
| 554 | int ret; |
| 555 | char buf[0x3]; |
| 556 | |
| 557 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 558 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf, |
| 559 | 0x3, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 560 | if (ret < 0) { |
| 561 | msg_perr("Command B failed (%s)!\n", usb_strerror()); |
| 562 | return 1; |
| 563 | } |
| 564 | if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) || |
| 565 | (buf[2] != 0xff)) { |
| 566 | msg_perr("Unexpected response to Command B!\n"); |
| 567 | return 1; |
| 568 | } |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | #endif |
| 573 | |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 574 | /* Command Chip Select is only sent after dediprog_check_devicestring, but not after every |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 575 | * invocation of dediprog_check_devicestring. It is only sent after the first |
| 576 | * dediprog_command_a(); dediprog_check_devicestring() sequence in each session. |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 577 | * Bit #1 of the value changes the chip select: 0 is target 1, 1 is target 2 and parameter target can be 1 or 2 |
| 578 | * respectively. We don't know how to encode "3, Socket" and "0, reference card" yet. On SF100 the vendor |
| 579 | * software "DpCmd 6.0.4.06" selects target 2 when requesting 3 (which is unavailable on that hardware). |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 580 | */ |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 581 | static int dediprog_chip_select(int target) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 582 | { |
| 583 | int ret; |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 584 | uint16_t value = ((target - 1) & 1) << 1; |
| 585 | msg_pdbg("Selecting target chip %i\n", target); |
| 586 | ret = usb_control_msg(dediprog_handle, 0x42, 0x4, value, 0x0, NULL, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 587 | 0x0, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 588 | if (ret != 0x0) { |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 589 | msg_perr("Command Chip Select failed (%s)!\n", usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 590 | return 1; |
| 591 | } |
| 592 | return 0; |
| 593 | } |
| 594 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 595 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 596 | /* Very strange. Seems to be a programmer keepalive or somesuch. |
| 597 | * Wait unsuccessfully for timeout ms to read one byte. |
| 598 | * Is usually called after setting voltage to 0. |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 599 | * Present in all logs with Firmware 2.1.1 and 3.1.8 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 600 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 601 | static int dediprog_command_f(int timeout) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 602 | { |
| 603 | int ret; |
| 604 | char buf[0x1]; |
| 605 | |
| 606 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 607 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, |
| 608 | 0x1, timeout); |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 609 | /* This check is most probably wrong. Command F always causes a timeout |
| 610 | * in the logs, so we should check for timeout instead of checking for |
| 611 | * success. |
| 612 | */ |
| 613 | if (ret != 0x1) { |
| 614 | msg_perr("Command F failed (%s)!\n", usb_strerror()); |
| 615 | return 1; |
| 616 | } |
| 617 | return 0; |
| 618 | } |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 619 | |
| 620 | /* Start/stop blinking? |
| 621 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 622 | * Preceded by Command J |
| 623 | */ |
| 624 | static int dediprog_command_g(void) |
| 625 | { |
| 626 | int ret; |
| 627 | |
| 628 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT); |
| 629 | if (ret != 0x0) { |
| 630 | msg_perr("Command G failed (%s)!\n", usb_strerror()); |
| 631 | return 1; |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | /* Something. |
| 637 | * Present in all logs with firmware 5.1.5 |
| 638 | * Always preceded by Command Receive Device String |
| 639 | * Always followed by Command Set SPI Voltage nonzero |
| 640 | */ |
| 641 | static int dediprog_command_h(void) |
| 642 | { |
| 643 | int ret; |
| 644 | |
| 645 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT); |
| 646 | if (ret != 0x0) { |
| 647 | msg_perr("Command H failed (%s)!\n", usb_strerror()); |
| 648 | return 1; |
| 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | /* Shutdown for firmware 5.x? |
| 654 | * Present in all logs with firmware 5.1.5 |
| 655 | * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI) |
| 656 | * Always followed by Command Set SPI Voltage 0x0000 |
| 657 | */ |
| 658 | static int dediprog_command_i(void) |
| 659 | { |
| 660 | int ret; |
| 661 | |
| 662 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT); |
| 663 | if (ret != 0x0) { |
| 664 | msg_perr("Command I failed (%s)!\n", usb_strerror()); |
| 665 | return 1; |
| 666 | } |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | /* Start/stop blinking? |
| 671 | * Present in all logs with firmware 5.1.5 |
| 672 | * Always preceded by Command Receive Device String on 5.1.5 |
| 673 | * Always followed by Command Set SPI Voltage nonzero on 5.1.5 |
| 674 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 675 | * Preceded by Command B in eng_detect_blink.log |
| 676 | * Followed by Command G in eng_detect_blink.log |
| 677 | */ |
| 678 | static int dediprog_command_j(void) |
| 679 | { |
| 680 | int ret; |
| 681 | |
| 682 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT); |
| 683 | if (ret != 0x0) { |
| 684 | msg_perr("Command J failed (%s)!\n", usb_strerror()); |
| 685 | return 1; |
| 686 | } |
| 687 | return 0; |
| 688 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 689 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 690 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 691 | static int parse_voltage(char *voltage) |
| 692 | { |
| 693 | char *tmp = NULL; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 694 | int i; |
| 695 | int millivolt = 0, fraction = 0; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 696 | |
| 697 | if (!voltage || !strlen(voltage)) { |
| 698 | msg_perr("Empty voltage= specified.\n"); |
| 699 | return -1; |
| 700 | } |
| 701 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 702 | voltage = tmp; |
| 703 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 704 | * to be in decimal notation. |
| 705 | */ |
| 706 | if ((*voltage == '.') || (*voltage == ',')) { |
| 707 | voltage++; |
| 708 | for (i = 0; i < 3; i++) { |
| 709 | fraction *= 10; |
| 710 | /* Don't advance if the current character is invalid, |
| 711 | * but continue multiplying. |
| 712 | */ |
| 713 | if ((*voltage < '0') || (*voltage > '9')) |
| 714 | continue; |
| 715 | fraction += *voltage - '0'; |
| 716 | voltage++; |
| 717 | } |
| 718 | /* Throw away remaining digits. */ |
| 719 | voltage += strspn(voltage, "0123456789"); |
| 720 | } |
| 721 | /* The remaining string must be empty or "mV" or "V". */ |
| 722 | tolower_string(voltage); |
| 723 | |
| 724 | /* No unit or "V". */ |
| 725 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 726 | millivolt *= 1000; |
| 727 | millivolt += fraction; |
| 728 | } else if (!strncmp(voltage, "mv", 2) || |
| 729 | !strncmp(voltage, "milliv", 6)) { |
| 730 | /* No adjustment. fraction is discarded. */ |
| 731 | } else { |
| 732 | /* Garbage at the end of the string. */ |
| 733 | msg_perr("Garbage voltage= specified.\n"); |
| 734 | return -1; |
| 735 | } |
| 736 | return millivolt; |
| 737 | } |
| 738 | |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 739 | static int dediprog_setup(long target) |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 740 | { |
| 741 | /* URB 6. Command A. */ |
| 742 | if (dediprog_command_a()) { |
| 743 | return 1; |
| 744 | } |
| 745 | /* URB 7. Command A. */ |
| 746 | if (dediprog_command_a()) { |
| 747 | return 1; |
| 748 | } |
| 749 | /* URB 8. Command Prepare Receive Device String. */ |
| 750 | /* URB 9. Command Receive Device String. */ |
| 751 | if (dediprog_check_devicestring()) { |
| 752 | return 1; |
| 753 | } |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 754 | /* URB 10. Command Chip Select */ |
| 755 | if (dediprog_chip_select(target)) { |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 756 | return 1; |
| 757 | } |
| 758 | return 0; |
| 759 | } |
| 760 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 761 | static const struct spi_master spi_master_dediprog = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 762 | .type = SPI_CONTROLLER_DEDIPROG, |
| 763 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 764 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 765 | .command = dediprog_spi_send_command, |
| 766 | .multicommand = default_spi_send_multicommand, |
| 767 | .read = dediprog_spi_read, |
| 768 | .write_256 = dediprog_spi_write_256, |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 769 | .write_aai = dediprog_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 770 | }; |
| 771 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 772 | static int dediprog_shutdown(void *data) |
| 773 | { |
| 774 | msg_pspew("%s\n", __func__); |
| 775 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 776 | #if 0 |
| 777 | /* Shutdown on firmware 5.x */ |
| 778 | if (dediprog_firmwareversion == 5) |
| 779 | if (dediprog_command_i()) |
| 780 | return 1; |
| 781 | #endif |
| 782 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 783 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 784 | if (dediprog_set_spi_voltage(0x0)) |
| 785 | return 1; |
| 786 | |
| 787 | if (usb_release_interface(dediprog_handle, 0)) { |
| 788 | msg_perr("Could not release USB interface!\n"); |
| 789 | return 1; |
| 790 | } |
| 791 | if (usb_close(dediprog_handle)) { |
| 792 | msg_perr("Could not close USB device!\n"); |
| 793 | return 1; |
| 794 | } |
| 795 | return 0; |
| 796 | } |
| 797 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 798 | /* URB numbers refer to the first log ever captured. */ |
| 799 | int dediprog_init(void) |
| 800 | { |
| 801 | struct usb_device *dev; |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 802 | char *voltage, *device, *spispeed, *target_str; |
Patrick Georgi | efe2d43 | 2013-05-23 21:47:46 +0000 | [diff] [blame] | 803 | int spispeed_idx = 1; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 804 | int millivolt = 3500; |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 805 | long usedevice = 0; |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 806 | long target = 1; |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 807 | int i, ret; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 808 | |
| 809 | msg_pspew("%s\n", __func__); |
| 810 | |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 811 | spispeed = extract_programmer_param("spispeed"); |
| 812 | if (spispeed) { |
| 813 | for (i = 0; spispeeds[i].name; ++i) { |
| 814 | if (!strcasecmp(spispeeds[i].name, spispeed)) { |
| 815 | spispeed_idx = i; |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | if (!spispeeds[i].name) { |
Patrick Georgi | efe2d43 | 2013-05-23 21:47:46 +0000 | [diff] [blame] | 820 | msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed); |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 821 | free(spispeed); |
| 822 | return 1; |
| 823 | } |
| 824 | free(spispeed); |
| 825 | } |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 826 | voltage = extract_programmer_param("voltage"); |
| 827 | if (voltage) { |
| 828 | millivolt = parse_voltage(voltage); |
| 829 | free(voltage); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 830 | if (millivolt < 0) |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 831 | return 1; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 832 | msg_pinfo("Setting voltage to %i mV\n", millivolt); |
| 833 | } |
| 834 | |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 835 | device = extract_programmer_param("device"); |
| 836 | if (device) { |
| 837 | char *dev_suffix; |
| 838 | errno = 0; |
| 839 | usedevice = strtol(device, &dev_suffix, 10); |
| 840 | if (errno != 0 || device == dev_suffix) { |
| 841 | msg_perr("Error: Could not convert 'device'.\n"); |
| 842 | free(device); |
| 843 | return 1; |
| 844 | } |
| 845 | if (usedevice < 0 || usedevice > UINT_MAX) { |
| 846 | msg_perr("Error: Value for 'device' is out of range.\n"); |
| 847 | free(device); |
| 848 | return 1; |
| 849 | } |
| 850 | if (strlen(dev_suffix) > 0) { |
| 851 | msg_perr("Error: Garbage following 'device' value.\n"); |
| 852 | free(device); |
| 853 | return 1; |
| 854 | } |
| 855 | msg_pinfo("Using device %li.\n", usedevice); |
| 856 | } |
| 857 | free(device); |
| 858 | |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 859 | target_str = extract_programmer_param("target"); |
| 860 | if (target_str) { |
| 861 | char *target_suffix; |
| 862 | errno = 0; |
| 863 | target = strtol(target_str, &target_suffix, 10); |
| 864 | if (errno != 0 || target_str == target_suffix) { |
| 865 | msg_perr("Error: Could not convert 'target'.\n"); |
| 866 | free(target_str); |
| 867 | return 1; |
| 868 | } |
| 869 | if (target < 1 || target > 2) { |
| 870 | msg_perr("Error: Value for 'target' is out of range.\n"); |
| 871 | free(target_str); |
| 872 | return 1; |
| 873 | } |
| 874 | if (strlen(target_suffix) > 0) { |
| 875 | msg_perr("Error: Garbage following 'target' value.\n"); |
| 876 | free(target_str); |
| 877 | return 1; |
| 878 | } |
| 879 | msg_pinfo("Using target %li.\n", target); |
| 880 | } |
| 881 | free(target_str); |
| 882 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 883 | /* Here comes the USB stuff. */ |
| 884 | usb_init(); |
| 885 | usb_find_busses(); |
| 886 | usb_find_devices(); |
Nathan Laredo | 21541a6 | 2012-12-24 22:07:36 +0000 | [diff] [blame] | 887 | dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 888 | if (!dev) { |
| 889 | msg_perr("Could not find a Dediprog SF100 on USB!\n"); |
| 890 | return 1; |
| 891 | } |
| 892 | msg_pdbg("Found USB device (%04x:%04x).\n", |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 893 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 894 | dediprog_handle = usb_open(dev); |
David Woodhouse | d2a7e87 | 2013-07-30 09:34:44 +0000 | [diff] [blame] | 895 | if (!dediprog_handle) { |
| 896 | msg_perr("Could not open USB device: %s\n", usb_strerror()); |
| 897 | return 1; |
| 898 | } |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 899 | ret = usb_set_configuration(dediprog_handle, 1); |
| 900 | if (ret < 0) { |
| 901 | msg_perr("Could not set USB device configuration: %i %s\n", |
| 902 | ret, usb_strerror()); |
| 903 | if (usb_close(dediprog_handle)) |
| 904 | msg_perr("Could not close USB device!\n"); |
| 905 | return 1; |
| 906 | } |
| 907 | ret = usb_claim_interface(dediprog_handle, 0); |
| 908 | if (ret < 0) { |
| 909 | msg_perr("Could not claim USB device interface %i: %i %s\n", |
| 910 | 0, ret, usb_strerror()); |
| 911 | if (usb_close(dediprog_handle)) |
| 912 | msg_perr("Could not close USB device!\n"); |
| 913 | return 1; |
| 914 | } |
| 915 | dediprog_endpoint = 2; |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 916 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 917 | if (register_shutdown(dediprog_shutdown, NULL)) |
| 918 | return 1; |
| 919 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 920 | dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON); |
| 921 | |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 922 | /* Perform basic setup. */ |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 923 | if (dediprog_setup(target)) { |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 924 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 925 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 926 | } |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 927 | |
| 928 | /* After setting voltage and speed, perform setup again. */ |
Stefan Tauner | e659d2d | 2013-05-03 21:58:28 +0000 | [diff] [blame] | 929 | if (dediprog_set_spi_voltage(0) || dediprog_set_spi_speed(spispeed_idx) || dediprog_setup(target)) { |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 930 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 931 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 932 | } |
Nico Huber | 77fa67d | 2013-02-20 18:03:36 +0000 | [diff] [blame] | 933 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 934 | /* URB 11. Command Set SPI Voltage. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 935 | if (dediprog_set_spi_voltage(millivolt)) { |
| 936 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 937 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 938 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 939 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 940 | register_spi_master(&spi_master_dediprog); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 941 | |
| 942 | /* RE leftover, leave in until the driver is complete. */ |
| 943 | #if 0 |
| 944 | /* Execute RDID by hand if you want to test it. */ |
| 945 | dediprog_do_stuff(); |
| 946 | #endif |
| 947 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 948 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF); |
| 949 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 950 | return 0; |
| 951 | } |
| 952 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 953 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 954 | /* Leftovers from reverse engineering. Keep for documentation purposes until |
| 955 | * completely understood. |
| 956 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 957 | static int dediprog_do_stuff(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 958 | { |
| 959 | char buf[0x4]; |
| 960 | /* SPI command processing starts here. */ |
| 961 | |
| 962 | /* URB 12. Command Send SPI. */ |
| 963 | /* URB 13. Command Receive SPI. */ |
| 964 | memset(buf, 0, sizeof(buf)); |
| 965 | /* JEDEC RDID */ |
| 966 | msg_pdbg("Sending RDID\n"); |
| 967 | buf[0] = JEDEC_RDID; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 968 | if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, |
| 969 | (unsigned char *)buf, (unsigned char *)buf)) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 970 | return 1; |
| 971 | msg_pdbg("Receiving response: "); |
| 972 | print_hex(buf, JEDEC_RDID_INSIZE); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 973 | /* URB 14-27 are more SPI commands. */ |
| 974 | /* URB 28. Command Set SPI Voltage. */ |
| 975 | if (dediprog_set_spi_voltage(0x0)) |
| 976 | return 1; |
| 977 | /* URB 29-38. Command F, unsuccessful wait. */ |
| 978 | if (dediprog_command_f(544)) |
| 979 | return 1; |
| 980 | /* URB 39. Command Set SPI Voltage. */ |
| 981 | if (dediprog_set_spi_voltage(0x10)) |
| 982 | return 1; |
| 983 | /* URB 40. Command Set SPI Speed. */ |
| 984 | if (dediprog_set_spi_speed(0x2)) |
| 985 | return 1; |
| 986 | /* URB 41 is just URB 28. */ |
| 987 | /* URB 42,44,46,48,51,53 is just URB 8. */ |
| 988 | /* URB 43,45,47,49,52,54 is just URB 9. */ |
| 989 | /* URB 50 is just URB 6/7. */ |
| 990 | /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/ |
| 991 | /* URB 132,134 is just URB 6/7. */ |
| 992 | /* URB 133 is just URB 29-38. */ |
| 993 | /* URB 135 is just URB 8. */ |
| 994 | /* URB 136 is just URB 9. */ |
| 995 | /* URB 137 is just URB 11. */ |
| 996 | |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 997 | /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */ |
| 998 | /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */ |
| 999 | /* Bulk transfer sizes for Command Start Bulk Read/Write are always |
| 1000 | * 512 bytes, rest is filled with 0xff. |
| 1001 | */ |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1002 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1003 | return 0; |
| 1004 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 1005 | #endif |