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