Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012-2015 Google Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 15 | * Foundation, Inc. |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include "em100.h" |
| 22 | |
| 23 | /* SPI flash related operations */ |
| 24 | |
| 25 | int get_spi_flash_id(struct em100 *em100) |
| 26 | { |
| 27 | unsigned char cmd[16]; |
| 28 | unsigned char data[512]; |
| 29 | memset(cmd, 0, 16); |
| 30 | cmd[0] = 0x30; /* Get SPI flash ID */ |
| 31 | if (!send_cmd(em100->dev, cmd)) { |
| 32 | return 0; |
| 33 | } |
| 34 | int len = get_response(em100->dev, data, 512); |
| 35 | if (len == 3) { |
| 36 | int id = (data[0] << 16) | (data[1] << 8) | data[2]; |
| 37 | return id; |
| 38 | } |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | int erase_spi_flash(struct em100 *em100) |
| 43 | { |
| 44 | unsigned char cmd[16]; |
| 45 | memset(cmd, 0, 16); |
| 46 | cmd[0] = 0x31; /* Erase SPI flash */ |
| 47 | if (!send_cmd(em100->dev, cmd)) { |
| 48 | return 0; |
| 49 | } |
| 50 | /* Specification says to wait 5s before |
| 51 | * issuing another USB command |
| 52 | */ |
| 53 | sleep(5); |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | int poll_spi_flash_status(struct em100 *em100) |
| 58 | { |
| 59 | unsigned char cmd[16]; |
| 60 | unsigned char data[1]; |
| 61 | memset(cmd, 0, 16); |
| 62 | cmd[0] = 0x32; /* Poll SPI flash status */ |
| 63 | if (!send_cmd(em100->dev, cmd)) { |
| 64 | return 0; |
| 65 | } |
| 66 | int len = get_response(em100->dev, data, 1); |
| 67 | if ((len == 1) && (data[0] == 1)) { |
| 68 | /* ready */ |
| 69 | return 1; |
| 70 | } |
| 71 | /* busy (or read unsuccessful) */ |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * read_spi_flash_page: fetch SPI flash page |
| 77 | * @param em100: initialized em100 device structure |
| 78 | * |
| 79 | * out(16 bytes): 0x33 addr addr addr .. 0 |
| 80 | * in(len + 255 bytes): 0xff ?? serno_lo serno_hi ?? ?? .. ?? |
| 81 | */ |
| 82 | int read_spi_flash_page(struct em100 *em100, int addr, unsigned char *blk) |
| 83 | { |
| 84 | unsigned char cmd[16]; |
| 85 | unsigned char data[256]; |
| 86 | memset(cmd, 0, 16); |
Stefan Reinauer | ae4dcd0 | 2015-08-28 16:26:03 -0700 | [diff] [blame] | 87 | cmd[0] = 0x33; /* read SPI flash page */ |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 88 | cmd[1] = (addr >> 16) & 0xff; |
| 89 | cmd[2] = (addr >> 8) & 0xff; |
| 90 | cmd[3] = addr & 0xff; |
| 91 | if (!send_cmd(em100->dev, cmd)) { |
| 92 | return 0; |
| 93 | } |
| 94 | int len = get_response(em100->dev, data, 256); |
| 95 | |
| 96 | if (len == 256) { |
| 97 | memcpy(blk, data, 256); |
| 98 | return 1; |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
Stefan Reinauer | 65559f1 | 2015-08-28 16:28:51 -0700 | [diff] [blame] | 103 | int write_spi_flash_page(struct em100 *em100, int address, unsigned char *data) |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 104 | { |
| 105 | int length = 256; |
| 106 | int actual; |
| 107 | int bytes_sent=0; |
| 108 | int bytes_left; |
| 109 | unsigned char cmd[16]; |
| 110 | memset(cmd, 0, 16); |
| 111 | cmd[0] = 0x34; /* host-to-em100 eeprom data */ |
Stefan Reinauer | 738812e | 2015-08-28 20:00:41 -0700 | [diff] [blame] | 112 | cmd[1] = (address >> 16) & 0xff; |
| 113 | cmd[2] = (address >> 8) & 0xff; |
| 114 | cmd[3] = address & 0xff; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 115 | |
| 116 | if (!send_cmd(em100->dev, cmd)) { |
Stefan Reinauer | 54474a6 | 2015-08-28 16:27:30 -0700 | [diff] [blame] | 117 | printf("Error: Could not initiate host-to-EM100 transfer.\n"); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | while ( bytes_sent < length) { |
| 122 | actual = 0; |
| 123 | |
| 124 | bytes_left = length - bytes_sent; |
| 125 | |
| 126 | libusb_bulk_transfer(em100->dev, 1 | LIBUSB_ENDPOINT_OUT, |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 127 | data + bytes_sent, bytes_left, &actual, |
| 128 | BULK_SEND_TIMEOUT); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 129 | bytes_sent += actual; |
| 130 | if (actual < bytes_left) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 131 | printf("Tried sending %d bytes, sent %d\n", bytes_left, |
| 132 | actual); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 133 | break; |
| 134 | } |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Stefan Reinauer | 8421b71 | 2015-08-28 20:01:34 -0700 | [diff] [blame] | 137 | if (bytes_sent != length) |
| 138 | printf ("SPI transfer failed\n"); |
| 139 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 140 | return (bytes_sent == length); |
| 141 | } |
| 142 | |
Stefan Reinauer | 40cba33 | 2015-08-28 20:03:05 -0700 | [diff] [blame] | 143 | int unlock_spi_flash(struct em100 *em100) |
| 144 | { |
| 145 | unsigned char cmd[16]; |
| 146 | memset(cmd, 0, 16); |
| 147 | cmd[0] = 0x36; /* Unlock SPI flash */ |
| 148 | if (!send_cmd(em100->dev, cmd)) { |
| 149 | return 0; |
| 150 | } |
| 151 | /* Specification says to wait 5s before |
| 152 | * issuing another USB command |
| 153 | */ |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | /* Erase SPI flash sector |
| 158 | * There are 32 sectors 64kB each |
| 159 | * |
| 160 | */ |
| 161 | int erase_spi_flash_sector(struct em100 *em100, unsigned int sector) |
| 162 | { |
| 163 | unsigned char cmd[16]; |
| 164 | |
| 165 | if (sector > 31) { |
| 166 | printf("Can't erase sector at address %x\n", sector << 16); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | memset(cmd, 0, 16); |
| 171 | cmd[0] = 0x37; /* Erase SPI flash sector */ |
| 172 | cmd[1] = sector; |
| 173 | if (!send_cmd(em100->dev, cmd)) { |
| 174 | return 0; |
| 175 | } |
| 176 | /* Specification says to wait 5s before |
| 177 | * issuing another USB command |
| 178 | */ |
| 179 | return 1; |
| 180 | |
| 181 | } |
| 182 | |
| 183 | |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 184 | /* SPI Hyper Terminal related operations */ |
| 185 | |
| 186 | /* SPI Hyper Terminal resources: |
| 187 | * |
| 188 | * FIFOs: |
| 189 | * dFIFO 64 bytes Transfer information from host to application (EM100Pro) |
| 190 | * uFIFO 512 bytes Transfer information from application to host |
| 191 | * |
| 192 | * Registers: |
| 193 | * 0 1 byte RW FIFO overflow, pause/start emulation, FIFO valid data |
| 194 | * 1 1 byte RO length of valid data in dFIFO |
| 195 | * 2 1 byte RO length of valid data in uFIFO (?? in 1 byte ??) |
| 196 | * 3 1 byte RO EM100 identification on SPI bus to differentiate from |
| 197 | * real SPI flash |
| 198 | * 4 1 byte RW uFIFO data format to indicate to the software how to |
| 199 | * read the data |
| 200 | */ |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * read_ht_register: Read HT registers |
| 204 | * @param em100: initialized em100 device structure |
| 205 | * |
| 206 | * out(2 bytes): 0x50 RegAddr .. 0 |
| 207 | * in(len + 1 byte): 0x02 val |
| 208 | */ |
| 209 | int read_ht_register(struct em100 *em100, int reg, uint8_t *val) |
| 210 | { |
| 211 | unsigned char cmd[16]; |
| 212 | unsigned char data[2]; |
| 213 | |
| 214 | memset(cmd, 0, 16); |
| 215 | cmd[0] = 0x50; /* read fpga register */ |
| 216 | cmd[1] = reg; |
| 217 | if (!send_cmd(em100->dev, cmd)) { |
| 218 | return 0; |
| 219 | } |
| 220 | int len = get_response(em100->dev, data, 2); |
| 221 | if ((len == 2) && (data[0] == 1)) { |
| 222 | *val = data[1]; |
| 223 | return 1; |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * write_ht_register: Write HT registers |
| 230 | * @param em100: initialized em100 device structure |
| 231 | * |
| 232 | * out(3 bytes): 0x51 RegAddr Val .. 0 |
| 233 | */ |
| 234 | int write_ht_register(struct em100 *em100, int reg, uint8_t val) |
| 235 | { |
| 236 | unsigned char cmd[16]; |
| 237 | memset(cmd, 0, 16); |
| 238 | cmd[0] = 0x51; /* write fpga registers */ |
| 239 | cmd[1] = reg; |
| 240 | cmd[2] = val; |
| 241 | if (!send_cmd(em100->dev, cmd)) { |
| 242 | return 0; |
| 243 | } |
| 244 | return 1; |
| 245 | } |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 246 | |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 247 | int write_dfifo(struct em100 *em100, unsigned int length, unsigned int timeout, |
| 248 | unsigned char *blk) |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 249 | { |
| 250 | int actual; |
| 251 | int bytes_sent=0; |
| 252 | int bytes_left; |
| 253 | unsigned char cmd[16]; |
| 254 | unsigned char data[512]; |
| 255 | |
| 256 | if (length > 512) { /* Really? Should be 64? */ |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 257 | printf("Error: Length of data to be written to dFIFO can't" |
| 258 | " be > 512\n"); |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | memset(cmd, 0, 16); |
| 262 | cmd[0] = 0x52; /* write dFIFO */ |
| 263 | cmd[1] = (length >> 8) & 0xff; |
| 264 | cmd[2] = length & 0xff; |
| 265 | cmd[3] = (timeout >> 8) & 0xff; |
| 266 | cmd[4] = timeout & 0xff; |
| 267 | |
| 268 | if (!send_cmd(em100->dev, cmd)) { |
| 269 | printf("Error: Could not initiate host-to-EM100 transfer.\n"); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | while ( bytes_sent < length) { |
| 274 | actual = 0; |
| 275 | |
| 276 | bytes_left = length - bytes_sent; |
| 277 | |
| 278 | libusb_bulk_transfer(em100->dev, 1 | LIBUSB_ENDPOINT_OUT, |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 279 | data + bytes_sent, bytes_left, &actual, |
| 280 | BULK_SEND_TIMEOUT); |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 281 | bytes_sent += actual; |
| 282 | if (actual < bytes_left) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 283 | printf("Tried sending %d bytes, sent %d\n", bytes_left, |
| 284 | actual); |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | |
| 288 | printf("Sent %d bytes of %d\n", bytes_sent, length); |
| 289 | } |
| 290 | |
| 291 | printf ("Transfer %s\n",bytes_sent == length ? "Succeeded" : "Failed"); |
Stefan Reinauer | d251898 | 2015-10-10 10:09:24 +0000 | [diff] [blame^] | 292 | if (bytes_sent == length) |
| 293 | printf("Warning: Sent %d bytes, expected %d\n", |
| 294 | bytes_sent, length); |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 295 | |
| 296 | int len = get_response(em100->dev, data, 512); |
| 297 | |
| 298 | if (len == 1 && data[0] == length) { |
| 299 | return 1; |
| 300 | } |
| 301 | return 0; |
| 302 | } |
| 303 | |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 304 | int read_ufifo(struct em100 *em100, unsigned int length, unsigned int timeout, |
| 305 | unsigned char *blk) |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 306 | { |
| 307 | unsigned char cmd[16]; |
| 308 | unsigned char data[512]; |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 309 | unsigned char data2[2]; |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 310 | |
| 311 | if (length > 512) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 312 | printf("Error: Length of data to be read from uFIFO can't be" |
| 313 | " > 512\n"); |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | memset(cmd, 0, 16); |
| 317 | cmd[0] = 0x53; /* read uFIFO */ |
| 318 | cmd[1] = (length >> 8) & 0xff; |
| 319 | cmd[2] = length & 0xff; |
| 320 | cmd[3] = (timeout >> 8) & 0xff; |
| 321 | cmd[4] = timeout & 0xff; |
| 322 | if (!send_cmd(em100->dev, cmd)) { |
| 323 | return 0; |
| 324 | } |
| 325 | int len = get_response(em100->dev, data, 512); |
| 326 | |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 327 | /* get second response from read ufifo command */ |
| 328 | get_response(em100->dev, data2, 2); |
| 329 | |
Stefan Reinauer | 42dfb82 | 2015-08-28 16:59:42 -0700 | [diff] [blame] | 330 | if (len == length) { |
| 331 | memcpy(blk, data, length); |
| 332 | return 1; |
| 333 | } |
| 334 | return 0; |
| 335 | } |