hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
| 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 | |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 21 | #include <string.h> |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 22 | #include <usb.h> |
| 23 | #include "flash.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 24 | #include "chipdrivers.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 25 | #include "programmer.h" |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 26 | #include "spi.h" |
| 27 | |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 28 | #define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 29 | #define DEFAULT_TIMEOUT 3000 |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 30 | static usb_dev_handle *dediprog_handle; |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 31 | static int dediprog_firmwareversion; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 32 | static int dediprog_endpoint; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 33 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 34 | enum cmd_t { |
| 35 | CMD_TRANSCEIVE = 0x1, |
| 36 | CMD_SET_FLASH_TYPE = 0x4, |
| 37 | CMD_SET_IO_LED = 0x7, |
| 38 | CMD_READ_PROGRAMMER_INFO = 0x8, |
| 39 | CMD_SET_TARGET_FLASH_VCC = 0x9, |
| 40 | CMD_INIT = 0xb, |
| 41 | CMD_GET_UID = 0x12, |
| 42 | CMD_READ = 0x20, |
| 43 | CMD_WRITE = 0x30, |
| 44 | CMD_SET_SPI_CLK = 0x61, |
| 45 | }; |
| 46 | |
| 47 | enum flash_type { |
| 48 | FLASH_TYPE_APPLICATION_FLASH_1 = 0, |
| 49 | FLASH_TYPE_FLASH_CARD, |
| 50 | FLASH_TYPE_APPLICATION_FLASH_2, |
| 51 | }; |
| 52 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 53 | /* Set/clear LEDs on dediprog */ |
| 54 | enum { |
| 55 | LED_PASS = 1 << 0, |
| 56 | LED_BUSY = 1 << 1, |
| 57 | LED_ERROR = 1 << 2, |
| 58 | LED_ALL = 7, |
| 59 | }; |
| 60 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 61 | /* IO bits for CMD_SET_IO_LED message */ |
| 62 | enum { |
| 63 | IO1 = 1 << 0, |
| 64 | IO2 = 1 << 1, |
| 65 | IO3 = 1 << 2, |
| 66 | IO4 = 1 << 3, |
| 67 | }; |
| 68 | |
| 69 | enum { |
| 70 | READ_MODE_STD = 1, |
| 71 | READ_FAST, |
| 72 | READ_MODE_ATMEL45, |
| 73 | READ_MODE_4BYTE_ADDR_MODE_FAST, |
| 74 | READ_MODE_4BYTE_ADDR_MODE_FAST_WITH_0C_CMD, |
| 75 | }; |
| 76 | |
| 77 | enum { |
| 78 | WRITE_MODE_PAGE_PGM = 1, |
| 79 | WRITE_MODE_PAGE_WRITE, |
| 80 | WRITE_MODE_1BYTE_AAI, |
| 81 | WRITE_MODE_2BYTE_AAI, |
| 82 | WRITE_MODE_128BYTE_PAGE, |
| 83 | WRITE_MODE_PAGE_AT26DF041, |
| 84 | WRITE_MODE_SILICON_BLUE_FPGA, |
| 85 | WRITE_MODE_64_BYTE_PAGE_NUMONYX_PCM, /* unit of length 512 bytes */ |
| 86 | WRITE_MODE_4BYTE_ADDR_MODE_256BYTE_PAGE_PGM, |
| 87 | WRITE_MODE_32BYTE_PAGE_PGM_MXIC_512K, /* unit of length 512 bytes */ |
| 88 | WRITE_MODE_4BYTE_ADDR_MODE_256BYTE_PAGE_PGM_12_COMMAND, |
| 89 | WRITE_MODE_4BYTE_ADDR_MODE_256BYTE_PAGE_PGM_CHECKING_FLAGS, |
| 90 | }; |
| 91 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 92 | static int current_led_status = -1; |
| 93 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 94 | #if 0 |
| 95 | /* Might be useful for other pieces of code as well. */ |
| 96 | static void print_hex(void *buf, size_t len) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 97 | { |
| 98 | size_t i; |
| 99 | |
| 100 | for (i = 0; i < len; i++) |
| 101 | msg_pdbg(" %02x", ((uint8_t *)buf)[i]); |
| 102 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 103 | #endif |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 104 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 105 | /* Might be useful for other USB devices as well. static for now. */ |
| 106 | static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 107 | { |
| 108 | struct usb_bus *bus; |
| 109 | struct usb_device *dev; |
| 110 | |
| 111 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 112 | for (dev = bus->devices; dev; dev = dev->next) |
| 113 | if ((dev->descriptor.idVendor == vid) && |
| 114 | (dev->descriptor.idProduct == pid)) |
| 115 | return dev; |
| 116 | |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | //int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); |
| 121 | |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 122 | static int dediprog_set_leds(int leds) |
| 123 | { |
| 124 | int ret, target_leds; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 125 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 126 | if (leds < 0 || leds > LED_ALL) |
| 127 | leds = LED_ALL; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 128 | if (leds == current_led_status) |
| 129 | return 0; |
| 130 | |
| 131 | /* Older Dediprogs with 2.x.x and 3.x.x firmware only had |
| 132 | * two LEDs, and they were reversed. So map them around if |
| 133 | * we have an old device. On those devices the LEDs map as |
| 134 | * follows: |
| 135 | * bit 2 == 0: green light is on. |
| 136 | * bit 0 == 0: red light is on. |
| 137 | */ |
| 138 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 139 | target_leds = ((leds & LED_ERROR) >> 2) | |
| 140 | ((leds & LED_PASS) << 2); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 141 | } else { |
| 142 | target_leds = leds; |
| 143 | } |
| 144 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 145 | target_leds ^= 7; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 146 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds, |
| 147 | NULL, 0x0, DEFAULT_TIMEOUT); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 148 | if (ret != 0x0) { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 149 | msg_perr("Command Set LED 0x%x failed (%s)!\n", |
| 150 | leds, usb_strerror()); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | current_led_status = leds; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 159 | static int dediprog_set_spi_voltage(int millivolt) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 160 | { |
| 161 | int ret; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 162 | uint16_t voltage_selector; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 163 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 164 | switch (millivolt) { |
| 165 | case 0: |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 166 | /* Admittedly this one is an assumption. */ |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 167 | voltage_selector = 0x0; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 168 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 169 | case 1800: |
| 170 | voltage_selector = 0x12; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 171 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 172 | case 2500: |
| 173 | voltage_selector = 0x11; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 174 | break; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 175 | case 3500: |
| 176 | voltage_selector = 0x10; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 177 | break; |
| 178 | default: |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 179 | msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 180 | return 1; |
| 181 | } |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 182 | msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000, |
| 183 | millivolt % 1000); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 184 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 185 | ret = usb_control_msg(dediprog_handle, 0x42, CMD_SET_TARGET_FLASH_VCC, |
| 186 | voltage_selector, 0xff, NULL, 0x0, |
| 187 | DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 188 | if (ret != 0x0) { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 189 | msg_perr("Command Set SPI Voltage 0x%x failed!\n", |
| 190 | voltage_selector); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 191 | return 1; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 196 | #if 0 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 197 | /* After dediprog_set_spi_speed, the original app always calls |
| 198 | * dediprog_set_spi_voltage(0) and then |
| 199 | * dediprog_check_devicestring() four times in a row. |
| 200 | * After that, dediprog_command_a() is called. |
| 201 | * This looks suspiciously like the microprocessor in the SF100 has to be |
| 202 | * restarted/reinitialized in case the speed changes. |
| 203 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 204 | static int dediprog_set_spi_speed(uint16_t speed) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 205 | { |
| 206 | int ret; |
| 207 | unsigned int khz; |
| 208 | |
| 209 | /* Case 1 and 2 are in weird order. Probably an organically "grown" |
| 210 | * interface. |
| 211 | * Base frequency is 24000 kHz, divisors are (in order) |
| 212 | * 1, 3, 2, 8, 11, 16, 32, 64. |
| 213 | */ |
| 214 | switch (speed) { |
| 215 | case 0x0: |
| 216 | khz = 24000; |
| 217 | break; |
| 218 | case 0x1: |
| 219 | khz = 8000; |
| 220 | break; |
| 221 | case 0x2: |
| 222 | khz = 12000; |
| 223 | break; |
| 224 | case 0x3: |
| 225 | khz = 3000; |
| 226 | break; |
| 227 | case 0x4: |
| 228 | khz = 2180; |
| 229 | break; |
| 230 | case 0x5: |
| 231 | khz = 1500; |
| 232 | break; |
| 233 | case 0x6: |
| 234 | khz = 750; |
| 235 | break; |
| 236 | case 0x7: |
| 237 | khz = 375; |
| 238 | break; |
| 239 | default: |
| 240 | msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed); |
| 241 | return 1; |
| 242 | } |
| 243 | msg_pdbg("Setting SPI speed to %u kHz\n", khz); |
| 244 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 245 | ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, |
| 246 | 0x0, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 247 | if (ret != 0x0) { |
| 248 | msg_perr("Command Set SPI Speed 0x%x failed!\n", speed); |
| 249 | return 1; |
| 250 | } |
| 251 | return 0; |
| 252 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 253 | #endif |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 254 | |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 255 | /* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes. |
| 256 | * @start start address |
| 257 | * @len length |
| 258 | * @return 0 on success, 1 on failure |
| 259 | */ |
| 260 | static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 261 | unsigned int start, unsigned int len) |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 262 | { |
| 263 | int ret; |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 264 | unsigned int i; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 265 | /* chunksize must be 512, other sizes will NOT work at all. */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 266 | const unsigned int chunksize = 0x200; |
| 267 | const unsigned int count = len / chunksize; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 268 | const char count_and_chunk[] = {count & 0xff, |
| 269 | (count >> 8) & 0xff, |
| 270 | chunksize & 0xff, |
| 271 | (chunksize >> 8) & 0xff}; |
| 272 | |
| 273 | if ((start % chunksize) || (len % chunksize)) { |
| 274 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 275 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | /* No idea if the hardware can handle empty reads, so chicken out. */ |
| 280 | if (!len) |
| 281 | return 0; |
| 282 | /* Command Read SPI Bulk. No idea which read command is used on the |
| 283 | * SPI side. |
| 284 | */ |
| 285 | ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000, |
| 286 | start / 0x10000, (char *)count_and_chunk, |
| 287 | sizeof(count_and_chunk), DEFAULT_TIMEOUT); |
| 288 | if (ret != sizeof(count_and_chunk)) { |
| 289 | msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, |
| 290 | usb_strerror()); |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | for (i = 0; i < count; i++) { |
| 295 | ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint, |
| 296 | (char *)buf + i * chunksize, chunksize, |
| 297 | DEFAULT_TIMEOUT); |
| 298 | if (ret != chunksize) { |
| 299 | msg_perr("SPI bulk read %i failed, expected %i, got %i " |
| 300 | "%s!\n", i, chunksize, ret, usb_strerror()); |
| 301 | return 1; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 308 | static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, |
| 309 | unsigned int start, unsigned int len) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 310 | { |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 311 | int ret; |
| 312 | /* chunksize must be 512, other sizes will NOT work at all. */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 313 | const unsigned int chunksize = 0x200; |
| 314 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 315 | unsigned int bulklen; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 316 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 317 | dediprog_set_leds(LED_BUSY); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 318 | |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 319 | if (residue) { |
| 320 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 321 | start, residue); |
| 322 | ret = spi_read_chunked(flash, buf, start, residue, 16); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 323 | if (ret) |
| 324 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* Round down. */ |
| 328 | bulklen = (len - residue) / chunksize * chunksize; |
| 329 | ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, |
| 330 | bulklen); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 331 | if (ret) |
| 332 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 333 | |
| 334 | len -= residue + bulklen; |
| 335 | if (len) { |
| 336 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 337 | start, len); |
| 338 | ret = spi_read_chunked(flash, buf + residue + bulklen, |
| 339 | start + residue + bulklen, len, 16); |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 340 | if (ret) |
| 341 | goto err; |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 344 | dediprog_set_leds(LED_PASS); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 345 | return 0; |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 346 | err: |
| 347 | dediprog_set_leds(LED_ERROR); |
| 348 | return ret; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 349 | } |
| 350 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 351 | static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 352 | unsigned int start, unsigned int len) |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 353 | { |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 354 | int ret; |
| 355 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 356 | dediprog_set_leds(LED_BUSY); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 357 | |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 358 | /* No idea about the real limit. Maybe 12, maybe more, maybe less. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 359 | ret = spi_write_chunked(flash, buf, start, len, 12); |
| 360 | |
| 361 | if (ret) |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 362 | dediprog_set_leds(LED_ERROR); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 363 | else |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 364 | dediprog_set_leds(LED_PASS); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 365 | |
| 366 | return ret; |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 367 | } |
| 368 | |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 369 | static int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 370 | const unsigned char *writearr, unsigned char *readarr) |
| 371 | { |
| 372 | int ret; |
| 373 | |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 374 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 375 | /* Paranoid, but I don't want to be blamed if anything explodes. */ |
hailfinger | 556e9c3 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 376 | if (writecnt > 16) { |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 377 | msg_perr("Untested writecnt=%i, aborting.\n", writecnt); |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 378 | return 1; |
| 379 | } |
| 380 | /* 16 byte reads should work. */ |
| 381 | if (readcnt > 16) { |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 382 | msg_perr("Untested readcnt=%i, aborting.\n", readcnt); |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 383 | return 1; |
| 384 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 385 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 386 | ret = usb_control_msg(dediprog_handle, 0x42, CMD_TRANSCEIVE, 0, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 387 | readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, |
| 388 | DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 389 | if (ret != writecnt) { |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 390 | msg_perr("Send SPI failed, expected %i, got %i %s!\n", |
| 391 | writecnt, ret, usb_strerror()); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 392 | return 1; |
| 393 | } |
| 394 | if (!readcnt) |
| 395 | return 0; |
| 396 | memset(readarr, 0, readcnt); |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 397 | ret = usb_control_msg(dediprog_handle, 0xc2, CMD_TRANSCEIVE, 0, 0, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 398 | (char *)readarr, readcnt, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 399 | if (ret != readcnt) { |
hailfinger | af389cc | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 400 | msg_perr("Receive SPI failed, expected %i, got %i %s!\n", |
| 401 | readcnt, ret, usb_strerror()); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 402 | return 1; |
| 403 | } |
| 404 | return 0; |
| 405 | } |
| 406 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 407 | static int dediprog_check_devicestring(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 408 | { |
| 409 | int ret; |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 410 | int fw[3]; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 411 | char buf[0x11]; |
| 412 | |
| 413 | /* Command Prepare Receive Device String. */ |
| 414 | memset(buf, 0, sizeof(buf)); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 415 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, |
| 416 | 0x1, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 417 | /* The char casting is needed to stop gcc complaining about an always true comparison. */ |
| 418 | if ((ret != 0x1) || (buf[0] != (char)0xff)) { |
| 419 | msg_perr("Unexpected response to Command Prepare Receive Device" |
| 420 | " String!\n"); |
| 421 | return 1; |
| 422 | } |
| 423 | /* Command Receive Device String. */ |
| 424 | memset(buf, 0, sizeof(buf)); |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 425 | ret = usb_control_msg(dediprog_handle, 0xc2, CMD_READ_PROGRAMMER_INFO, |
| 426 | 0, 0, buf, 0x10, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 427 | if (ret != 0x10) { |
| 428 | msg_perr("Incomplete/failed Command Receive Device String!\n"); |
| 429 | return 1; |
| 430 | } |
| 431 | buf[0x10] = '\0'; |
| 432 | msg_pdbg("Found a %s\n", buf); |
| 433 | if (memcmp(buf, "SF100", 0x5)) { |
| 434 | msg_perr("Device not a SF100!\n"); |
| 435 | return 1; |
| 436 | } |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 437 | if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) { |
| 438 | msg_perr("Unexpected firmware version string!\n"); |
| 439 | return 1; |
| 440 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 441 | /* Only these versions were tested. */ |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 442 | if (fw[0] < 2 || fw[0] > 5) { |
| 443 | msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], |
| 444 | fw[1], fw[2]); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 445 | return 1; |
| 446 | } |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 447 | dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 451 | static int dediprog_device_init(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 452 | { |
| 453 | int ret; |
| 454 | char buf[0x1]; |
| 455 | |
| 456 | memset(buf, 0, sizeof(buf)); |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 457 | ret = usb_control_msg(dediprog_handle, 0xc3, CMD_INIT, 0x0, 0x0, buf, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 458 | 0x1, DEFAULT_TIMEOUT); |
hailfinger | 7a00908 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 459 | if (ret < 0) { |
| 460 | msg_perr("Command A failed (%s)!\n", usb_strerror()); |
| 461 | return 1; |
| 462 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 463 | if ((ret != 0x1) || (buf[0] != 0x6f)) { |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 464 | msg_perr("Unexpected response to init!\n"); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 465 | return 1; |
| 466 | } |
| 467 | return 0; |
| 468 | } |
| 469 | |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 470 | #if 0 |
| 471 | /* Something. |
| 472 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 473 | * Always preceded by Command Receive Device String |
| 474 | */ |
| 475 | static int dediprog_command_b(void) |
| 476 | { |
| 477 | int ret; |
| 478 | char buf[0x3]; |
| 479 | |
| 480 | memset(buf, 0, sizeof(buf)); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 481 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf, |
| 482 | 0x3, DEFAULT_TIMEOUT); |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 483 | if (ret < 0) { |
| 484 | msg_perr("Command B failed (%s)!\n", usb_strerror()); |
| 485 | return 1; |
| 486 | } |
| 487 | if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) || |
| 488 | (buf[2] != 0xff)) { |
| 489 | msg_perr("Unexpected response to Command B!\n"); |
| 490 | return 1; |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | #endif |
| 496 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 497 | static int set_target_flash(enum flash_type type) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 498 | { |
| 499 | int ret; |
| 500 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 501 | ret = usb_control_msg(dediprog_handle, 0x42, CMD_SET_FLASH_TYPE, type, |
| 502 | 0x0, NULL, 0, DEFAULT_TIMEOUT); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 503 | if (ret != 0x0) { |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 504 | msg_perr("set_target_flash failed (%s)!\n", usb_strerror()); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 505 | return 1; |
| 506 | } |
| 507 | return 0; |
| 508 | } |
| 509 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 510 | #if 0 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 511 | /* Very strange. Seems to be a programmer keepalive or somesuch. |
| 512 | * Wait unsuccessfully for timeout ms to read one byte. |
| 513 | * Is usually called after setting voltage to 0. |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 514 | * Present in all logs with Firmware 2.1.1 and 3.1.8 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 515 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 516 | static int dediprog_command_f(int timeout) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 517 | { |
| 518 | int ret; |
| 519 | char buf[0x1]; |
| 520 | |
| 521 | memset(buf, 0, sizeof(buf)); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 522 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, |
| 523 | 0x1, timeout); |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 524 | /* This check is most probably wrong. Command F always causes a timeout |
| 525 | * in the logs, so we should check for timeout instead of checking for |
| 526 | * success. |
| 527 | */ |
| 528 | if (ret != 0x1) { |
| 529 | msg_perr("Command F failed (%s)!\n", usb_strerror()); |
| 530 | return 1; |
| 531 | } |
| 532 | return 0; |
| 533 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 534 | #endif |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 535 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 536 | static int parse_voltage(char *voltage) |
| 537 | { |
| 538 | char *tmp = NULL; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 539 | int i; |
| 540 | int millivolt = 0, fraction = 0; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 541 | |
| 542 | if (!voltage || !strlen(voltage)) { |
| 543 | msg_perr("Empty voltage= specified.\n"); |
| 544 | return -1; |
| 545 | } |
| 546 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 547 | voltage = tmp; |
| 548 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 549 | * to be in decimal notation. |
| 550 | */ |
| 551 | if ((*voltage == '.') || (*voltage == ',')) { |
| 552 | voltage++; |
| 553 | for (i = 0; i < 3; i++) { |
| 554 | fraction *= 10; |
| 555 | /* Don't advance if the current character is invalid, |
| 556 | * but continue multiplying. |
| 557 | */ |
| 558 | if ((*voltage < '0') || (*voltage > '9')) |
| 559 | continue; |
| 560 | fraction += *voltage - '0'; |
| 561 | voltage++; |
| 562 | } |
| 563 | /* Throw away remaining digits. */ |
| 564 | voltage += strspn(voltage, "0123456789"); |
| 565 | } |
| 566 | /* The remaining string must be empty or "mV" or "V". */ |
| 567 | tolower_string(voltage); |
| 568 | |
| 569 | /* No unit or "V". */ |
| 570 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 571 | millivolt *= 1000; |
| 572 | millivolt += fraction; |
| 573 | } else if (!strncmp(voltage, "mv", 2) || |
| 574 | !strncmp(voltage, "milliv", 6)) { |
| 575 | /* No adjustment. fraction is discarded. */ |
| 576 | } else { |
| 577 | /* Garbage at the end of the string. */ |
| 578 | msg_perr("Garbage voltage= specified.\n"); |
| 579 | return -1; |
| 580 | } |
| 581 | return millivolt; |
| 582 | } |
| 583 | |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 584 | static const struct spi_programmer spi_programmer_dediprog = { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 585 | .type = SPI_CONTROLLER_DEDIPROG, |
| 586 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 587 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 588 | .command = dediprog_spi_send_command, |
| 589 | .multicommand = default_spi_send_multicommand, |
| 590 | .read = dediprog_spi_read, |
| 591 | .write_256 = dediprog_spi_write_256, |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 594 | static int dediprog_shutdown(void *data) |
| 595 | { |
| 596 | msg_pspew("%s\n", __func__); |
| 597 | |
| 598 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 599 | if (dediprog_set_spi_voltage(0x0)) |
| 600 | return 1; |
| 601 | |
| 602 | if (usb_release_interface(dediprog_handle, 0)) { |
| 603 | msg_perr("Could not release USB interface!\n"); |
| 604 | return 1; |
| 605 | } |
| 606 | if (usb_close(dediprog_handle)) { |
| 607 | msg_perr("Could not close USB device!\n"); |
| 608 | return 1; |
| 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 613 | /* URB numbers refer to the first log ever captured. */ |
| 614 | int dediprog_init(void) |
| 615 | { |
| 616 | struct usb_device *dev; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 617 | char *voltage; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 618 | int millivolt = 3500; |
| 619 | int ret; |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 620 | |
| 621 | msg_pspew("%s\n", __func__); |
| 622 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 623 | voltage = extract_programmer_param("voltage"); |
| 624 | if (voltage) { |
| 625 | millivolt = parse_voltage(voltage); |
| 626 | free(voltage); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 627 | if (millivolt < 0) |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 628 | return 1; |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 629 | msg_pinfo("Setting voltage to %i mV\n", millivolt); |
| 630 | } |
| 631 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 632 | /* Here comes the USB stuff. */ |
| 633 | usb_init(); |
| 634 | usb_find_busses(); |
| 635 | usb_find_devices(); |
| 636 | dev = get_device_by_vid_pid(0x0483, 0xdada); |
| 637 | if (!dev) { |
| 638 | msg_perr("Could not find a Dediprog SF100 on USB!\n"); |
| 639 | return 1; |
| 640 | } |
| 641 | msg_pdbg("Found USB device (%04x:%04x).\n", |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 642 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 643 | dediprog_handle = usb_open(dev); |
hailfinger | fb6287d | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 644 | ret = usb_set_configuration(dediprog_handle, 1); |
| 645 | if (ret < 0) { |
| 646 | msg_perr("Could not set USB device configuration: %i %s\n", |
| 647 | ret, usb_strerror()); |
| 648 | if (usb_close(dediprog_handle)) |
| 649 | msg_perr("Could not close USB device!\n"); |
| 650 | return 1; |
| 651 | } |
| 652 | ret = usb_claim_interface(dediprog_handle, 0); |
| 653 | if (ret < 0) { |
| 654 | msg_perr("Could not claim USB device interface %i: %i %s\n", |
| 655 | 0, ret, usb_strerror()); |
| 656 | if (usb_close(dediprog_handle)) |
| 657 | msg_perr("Could not close USB device!\n"); |
| 658 | return 1; |
| 659 | } |
| 660 | dediprog_endpoint = 2; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 661 | |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 662 | if (register_shutdown(dediprog_shutdown, NULL)) |
| 663 | return 1; |
| 664 | |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 665 | if (dediprog_device_init()) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 666 | return 1; |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 667 | if (dediprog_check_devicestring()) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 668 | return 1; |
Simon Glass | a1f8068 | 2015-01-08 05:55:29 -0700 | [diff] [blame^] | 669 | if (set_target_flash(FLASH_TYPE_APPLICATION_FLASH_1)) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 670 | dediprog_set_leds(LED_ERROR); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 671 | return 1; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 672 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 673 | /* URB 11. Command Set SPI Voltage. */ |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 674 | if (dediprog_set_spi_voltage(millivolt)) { |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 675 | dediprog_set_leds(LED_ERROR); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 676 | return 1; |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 677 | } |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 678 | |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 679 | register_spi_programmer(&spi_programmer_dediprog); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 680 | |
| 681 | /* RE leftover, leave in until the driver is complete. */ |
| 682 | #if 0 |
| 683 | /* Execute RDID by hand if you want to test it. */ |
| 684 | dediprog_do_stuff(); |
| 685 | #endif |
| 686 | |
Simon Glass | e53cc1d | 2015-01-08 05:48:51 -0700 | [diff] [blame] | 687 | dediprog_set_leds(0); |
stepan | 4c06de7 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 688 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 692 | #if 0 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 693 | /* Leftovers from reverse engineering. Keep for documentation purposes until |
| 694 | * completely understood. |
| 695 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 696 | static int dediprog_do_stuff(void) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 697 | { |
| 698 | char buf[0x4]; |
| 699 | /* SPI command processing starts here. */ |
| 700 | |
| 701 | /* URB 12. Command Send SPI. */ |
| 702 | /* URB 13. Command Receive SPI. */ |
| 703 | memset(buf, 0, sizeof(buf)); |
| 704 | /* JEDEC RDID */ |
| 705 | msg_pdbg("Sending RDID\n"); |
| 706 | buf[0] = JEDEC_RDID; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 707 | if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, |
| 708 | (unsigned char *)buf, (unsigned char *)buf)) |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 709 | return 1; |
| 710 | msg_pdbg("Receiving response: "); |
| 711 | print_hex(buf, JEDEC_RDID_INSIZE); |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 712 | /* URB 14-27 are more SPI commands. */ |
| 713 | /* URB 28. Command Set SPI Voltage. */ |
| 714 | if (dediprog_set_spi_voltage(0x0)) |
| 715 | return 1; |
| 716 | /* URB 29-38. Command F, unsuccessful wait. */ |
| 717 | if (dediprog_command_f(544)) |
| 718 | return 1; |
| 719 | /* URB 39. Command Set SPI Voltage. */ |
| 720 | if (dediprog_set_spi_voltage(0x10)) |
| 721 | return 1; |
| 722 | /* URB 40. Command Set SPI Speed. */ |
| 723 | if (dediprog_set_spi_speed(0x2)) |
| 724 | return 1; |
| 725 | /* URB 41 is just URB 28. */ |
| 726 | /* URB 42,44,46,48,51,53 is just URB 8. */ |
| 727 | /* URB 43,45,47,49,52,54 is just URB 9. */ |
| 728 | /* URB 50 is just URB 6/7. */ |
| 729 | /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/ |
| 730 | /* URB 132,134 is just URB 6/7. */ |
| 731 | /* URB 133 is just URB 29-38. */ |
| 732 | /* URB 135 is just URB 8. */ |
| 733 | /* URB 136 is just URB 9. */ |
| 734 | /* URB 137 is just URB 11. */ |
| 735 | |
stepan | ff582cc | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 736 | /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */ |
| 737 | /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */ |
| 738 | /* Bulk transfer sizes for Command Start Bulk Read/Write are always |
| 739 | * 512 bytes, rest is filled with 0xff. |
| 740 | */ |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 741 | |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 742 | return 0; |
| 743 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 744 | #endif |