Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009, 2011 Urja Rannikko <urjaman@gmail.com> |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009 Carl-Daniel Hailfinger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 25 | #include <string.h> |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 26 | #include <ctype.h> |
| 27 | #include <fcntl.h> |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 28 | #include <sys/socket.h> |
| 29 | #include <arpa/inet.h> |
| 30 | #include <netinet/in.h> |
| 31 | #include <netinet/tcp.h> |
| 32 | #include <netdb.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <errno.h> |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 35 | #include <inttypes.h> |
| 36 | #include <termios.h> |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 37 | #include "flash.h" |
| 38 | #include "programmer.h" |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 39 | #include "chipdrivers.h" |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 40 | |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 41 | #define MSGHEADER "serprog: " |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 42 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 43 | /* |
| 44 | * FIXME: This prototype was added to help reduce diffs for the shutdown |
| 45 | * registration patch, which shifted many lines of code to place |
| 46 | * serprog_shutdown() before serprog_init(). It should be removed soon. |
| 47 | */ |
| 48 | static int serprog_shutdown(void *data); |
| 49 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 50 | #define S_ACK 0x06 |
| 51 | #define S_NAK 0x15 |
| 52 | #define S_CMD_NOP 0x00 /* No operation */ |
| 53 | #define S_CMD_Q_IFACE 0x01 /* Query interface version */ |
| 54 | #define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */ |
| 55 | #define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */ |
| 56 | #define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */ |
| 57 | #define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */ |
| 58 | #define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */ |
| 59 | #define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */ |
Cristian Măgherușan-Stanciu | 9932c7b | 2011-07-07 19:56:58 +0000 | [diff] [blame] | 60 | #define S_CMD_Q_WRNMAXLEN 0x08 /* Query opbuf-write-N maximum length */ |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 61 | #define S_CMD_R_BYTE 0x09 /* Read a single byte */ |
| 62 | #define S_CMD_R_NBYTES 0x0A /* Read n bytes */ |
| 63 | #define S_CMD_O_INIT 0x0B /* Initialize operation buffer */ |
| 64 | #define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */ |
| 65 | #define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */ |
| 66 | #define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */ |
| 67 | #define S_CMD_O_EXEC 0x0F /* Execute operation buffer */ |
| 68 | #define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */ |
| 69 | #define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */ |
| 70 | #define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s). */ |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 71 | #define S_CMD_O_SPIOP 0x13 /* Perform SPI operation. */ |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 72 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 73 | static uint16_t sp_device_serbuf_size = 16; |
| 74 | static uint16_t sp_device_opbuf_size = 300; |
| 75 | /* Bitmap of supported commands */ |
| 76 | static uint8_t sp_cmdmap[32]; |
| 77 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 78 | /* sp_prev_was_write used to detect writes with contiguous addresses |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 79 | and combine them to write-n's */ |
| 80 | static int sp_prev_was_write = 0; |
| 81 | /* sp_write_n_addr used as the starting addr of the currently |
| 82 | combined write-n operation */ |
| 83 | static uint32_t sp_write_n_addr; |
| 84 | /* The maximum length of an write_n operation; 0 = write-n not supported */ |
| 85 | static uint32_t sp_max_write_n = 0; |
| 86 | /* The maximum length of a read_n operation; 0 = 2^24 */ |
| 87 | static uint32_t sp_max_read_n = 0; |
| 88 | |
| 89 | /* A malloc'd buffer for combining the operation's data |
| 90 | and a counter that tells how much data is there. */ |
| 91 | static uint8_t *sp_write_n_buf; |
| 92 | static uint32_t sp_write_n_bytes = 0; |
| 93 | |
| 94 | /* sp_streamed_* used for flow control checking */ |
| 95 | static int sp_streamed_transmit_ops = 0; |
| 96 | static int sp_streamed_transmit_bytes = 0; |
| 97 | |
| 98 | /* sp_opbuf_usage used for counting the amount of |
| 99 | on-device operation buffer used */ |
| 100 | static int sp_opbuf_usage = 0; |
| 101 | /* if true causes sp_docommand to automatically check |
| 102 | whether the command is supported before doing it */ |
| 103 | static int sp_check_avail_automatic = 0; |
| 104 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 105 | static int sp_opensocket(char *ip, unsigned int port) |
| 106 | { |
| 107 | int flag = 1; |
| 108 | struct hostent *hostPtr = NULL; |
Carl-Daniel Hailfinger | 6d12560 | 2009-09-05 01:10:23 +0000 | [diff] [blame] | 109 | union { struct sockaddr_in si; struct sockaddr s; } sp = {}; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 110 | int sock; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 111 | msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 112 | sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 113 | if (sock < 0) |
| 114 | sp_die("Error: serprog cannot open socket"); |
| 115 | hostPtr = gethostbyname(ip); |
| 116 | if (NULL == hostPtr) { |
| 117 | hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET); |
| 118 | if (NULL == hostPtr) |
| 119 | sp_die("Error: cannot resolve"); |
| 120 | } |
Carl-Daniel Hailfinger | 6d12560 | 2009-09-05 01:10:23 +0000 | [diff] [blame] | 121 | sp.si.sin_family = AF_INET; |
| 122 | sp.si.sin_port = htons(port); |
| 123 | (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length); |
| 124 | if (connect(sock, &sp.s, sizeof(sp.si)) < 0) { |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 125 | close(sock); |
| 126 | sp_die("Error: serprog cannot connect"); |
| 127 | } |
| 128 | /* We are latency limited, and sometimes do write-write-read * |
| 129 | * (write-n) - so enable TCP_NODELAY. */ |
| 130 | setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)); |
| 131 | return sock; |
| 132 | } |
| 133 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 134 | static int sp_sync_read_timeout(int loops) |
| 135 | { |
| 136 | int i; |
| 137 | unsigned char c; |
| 138 | for (i = 0; i < loops; i++) { |
| 139 | ssize_t rv; |
| 140 | rv = read(sp_fd, &c, 1); |
| 141 | if (rv == 1) |
| 142 | return c; |
| 143 | if ((rv == -1) && (errno != EAGAIN)) |
| 144 | sp_die("read"); |
| 145 | usleep(10 * 1000); /* 10ms units */ |
| 146 | } |
| 147 | return -1; |
| 148 | } |
| 149 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 150 | /* Synchronize: a bit tricky algorithm that tries to (and in my tests has * |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 151 | * always succeeded in) bring the serial protocol to known waiting-for- * |
| 152 | * command state - uses nonblocking read - rest of the driver uses * |
| 153 | * blocking read - TODO: add an alarm() timer for the rest of the app on * |
| 154 | * serial operations, though not such a big issue as the first thing to * |
| 155 | * do is synchronize (eg. check that device is alive). */ |
| 156 | static void sp_synchronize(void) |
| 157 | { |
| 158 | int i; |
| 159 | int flags = fcntl(sp_fd, F_GETFL); |
| 160 | unsigned char buf[8]; |
| 161 | flags |= O_NONBLOCK; |
| 162 | fcntl(sp_fd, F_SETFL, flags); |
| 163 | /* First sends 8 NOPs, then flushes the return data - should cause * |
| 164 | * the device serial parser to get to a sane state, unless if it * |
| 165 | * is waiting for a real long write-n. */ |
| 166 | memset(buf, S_CMD_NOP, 8); |
| 167 | if (write(sp_fd, buf, 8) != 8) |
| 168 | sp_die("flush write"); |
| 169 | /* A second should be enough to get all the answers to the buffer */ |
| 170 | usleep(1000 * 1000); |
| 171 | sp_flush_incoming(); |
| 172 | |
Cristian Măgherușan-Stanciu | 9932c7b | 2011-07-07 19:56:58 +0000 | [diff] [blame] | 173 | /* Then try up to 8 times to send syncnop and get the correct special * |
| 174 | * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = * |
| 175 | * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync * |
| 176 | * attempt, ~1s if immediate success. */ |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 177 | for (i = 0; i < 8; i++) { |
| 178 | int n; |
| 179 | unsigned char c = S_CMD_SYNCNOP; |
| 180 | if (write(sp_fd, &c, 1) != 1) |
| 181 | sp_die("sync write"); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 182 | msg_pdbg("."); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 183 | fflush(stdout); |
| 184 | for (n = 0; n < 10; n++) { |
Cristian Măgherușan-Stanciu | 9932c7b | 2011-07-07 19:56:58 +0000 | [diff] [blame] | 185 | c = sp_sync_read_timeout(5); /* wait up to 50ms */ |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 186 | if (c != S_NAK) |
| 187 | continue; |
| 188 | c = sp_sync_read_timeout(2); |
| 189 | if (c != S_ACK) |
| 190 | continue; |
| 191 | c = S_CMD_SYNCNOP; |
| 192 | if (write(sp_fd, &c, 1) != 1) |
| 193 | sp_die("sync write"); |
| 194 | c = sp_sync_read_timeout(50); |
| 195 | if (c != S_NAK) |
| 196 | break; /* fail */ |
| 197 | c = sp_sync_read_timeout(10); |
| 198 | if (c != S_ACK) |
| 199 | break; /* fail */ |
| 200 | /* Ok, synchronized; back to blocking reads and return. */ |
| 201 | flags &= ~O_NONBLOCK; |
| 202 | fcntl(sp_fd, F_SETFL, flags); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 203 | msg_pdbg("\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | } |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 207 | msg_perr("Error: cannot synchronize protocol " |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 208 | "- check communications and reset device?\n"); |
| 209 | exit(1); |
| 210 | } |
| 211 | |
| 212 | static int sp_check_commandavail(uint8_t command) |
| 213 | { |
| 214 | int byteoffs, bitoffs; |
| 215 | byteoffs = command / 8; |
| 216 | bitoffs = command % 8; |
| 217 | return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0; |
| 218 | } |
| 219 | |
| 220 | static int sp_automatic_cmdcheck(uint8_t cmd) |
| 221 | { |
| 222 | if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) { |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 223 | msg_pdbg("Warning: Automatic command availability check failed " |
| 224 | "for cmd 0x%x - won't execute cmd\n", cmd); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int sp_docommand(uint8_t command, uint32_t parmlen, |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 231 | uint8_t *params, uint32_t retlen, void *retparms) |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 232 | { |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 233 | unsigned char c; |
| 234 | if (sp_automatic_cmdcheck(command)) |
| 235 | return 1; |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 236 | if (write(sp_fd, &command, 1) != 1) |
| 237 | sp_die("Error: cannot write op code"); |
| 238 | if (write(sp_fd, params, parmlen) != (parmlen)) |
| 239 | sp_die("Error: cannot write parameters"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 240 | if (read(sp_fd, &c, 1) != 1) |
| 241 | sp_die("Error: cannot read from device"); |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 242 | if (c == S_NAK) |
| 243 | return 1; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 244 | if (c != S_ACK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 245 | msg_perr("Error: invalid response 0x%02X from device\n",c); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 246 | exit(1); |
| 247 | } |
| 248 | if (retlen) { |
| 249 | int rd_bytes = 0; |
| 250 | do { |
| 251 | int r; |
| 252 | r = read(sp_fd, retparms + rd_bytes, |
| 253 | retlen - rd_bytes); |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 254 | if (r <= 0) |
| 255 | sp_die("Error: cannot read return parameters"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 256 | rd_bytes += r; |
| 257 | } while (rd_bytes != retlen); |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static void sp_flush_stream(void) |
| 263 | { |
| 264 | if (sp_streamed_transmit_ops) |
| 265 | do { |
| 266 | unsigned char c; |
| 267 | if (read(sp_fd, &c, 1) != 1) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 268 | sp_die("Error: cannot read from device (flushing stream)"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 269 | } |
| 270 | if (c == S_NAK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 271 | msg_perr("Error: NAK to a stream buffer operation\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 272 | exit(1); |
| 273 | } |
| 274 | if (c != S_ACK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 275 | msg_perr("Error: Invalid reply 0x%02X from device\n", c); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 276 | exit(1); |
| 277 | } |
| 278 | } while (--sp_streamed_transmit_ops); |
| 279 | sp_streamed_transmit_ops = 0; |
| 280 | sp_streamed_transmit_bytes = 0; |
| 281 | } |
| 282 | |
| 283 | static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms) |
| 284 | { |
| 285 | uint8_t *sp; |
| 286 | if (sp_automatic_cmdcheck(cmd)) |
| 287 | return 1; |
| 288 | sp = malloc(1 + parmlen); |
| 289 | if (!sp) sp_die("Error: cannot malloc command buffer"); |
| 290 | sp[0] = cmd; |
| 291 | memcpy(&(sp[1]), parms, parmlen); |
| 292 | if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size)) |
| 293 | sp_flush_stream(); |
| 294 | if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen)) |
| 295 | sp_die("Error: cannot write command"); |
| 296 | free(sp); |
| 297 | sp_streamed_transmit_ops += 1; |
| 298 | sp_streamed_transmit_bytes += 1 + parmlen; |
| 299 | return 0; |
| 300 | } |
| 301 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 302 | static int serprog_spi_send_command(struct flashctx *flash, |
| 303 | unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 304 | const unsigned char *writearr, |
| 305 | unsigned char *readarr); |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 306 | static int serprog_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 307 | unsigned int start, unsigned int len); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 308 | static struct spi_programmer spi_programmer_serprog = { |
| 309 | .type = SPI_CONTROLLER_SERPROG, |
| 310 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 311 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
| 312 | .command = serprog_spi_send_command, |
| 313 | .multicommand = default_spi_send_multicommand, |
| 314 | .read = serprog_spi_read, |
| 315 | .write_256 = default_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 316 | .write_aai = default_spi_write_aai, |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 319 | static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val, |
| 320 | chipaddr addr); |
| 321 | static uint8_t serprog_chip_readb(const struct flashctx *flash, |
| 322 | const chipaddr addr); |
| 323 | static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf, |
| 324 | const chipaddr addr, size_t len); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 325 | static const struct par_programmer par_programmer_serprog = { |
| 326 | .chip_readb = serprog_chip_readb, |
| 327 | .chip_readw = fallback_chip_readw, |
| 328 | .chip_readl = fallback_chip_readl, |
| 329 | .chip_readn = serprog_chip_readn, |
| 330 | .chip_writeb = serprog_chip_writeb, |
| 331 | .chip_writew = fallback_chip_writew, |
| 332 | .chip_writel = fallback_chip_writel, |
| 333 | .chip_writen = fallback_chip_writen, |
| 334 | }; |
| 335 | |
| 336 | static enum chipbustype serprog_buses_supported = BUS_NONE; |
| 337 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 338 | int serprog_init(void) |
| 339 | { |
| 340 | uint16_t iface; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 341 | unsigned char pgmname[17]; |
| 342 | unsigned char rbuf[3]; |
| 343 | unsigned char c; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 344 | char *device; |
| 345 | char *baudport; |
| 346 | int have_device = 0; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 347 | |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 348 | /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */ |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 349 | device = extract_programmer_param("dev"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 350 | if (device && strlen(device)) { |
| 351 | baudport = strstr(device, ":"); |
| 352 | if (baudport) { |
| 353 | /* Split device from baudrate. */ |
| 354 | *baudport = '\0'; |
| 355 | baudport++; |
| 356 | } |
| 357 | if (!baudport || !strlen(baudport)) { |
| 358 | msg_perr("Error: No baudrate specified.\n" |
| 359 | "Use flashrom -p serprog:dev=/dev/device:baud\n"); |
| 360 | free(device); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 361 | return 1; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 362 | } |
| 363 | if (strlen(device)) { |
| 364 | sp_fd = sp_openserport(device, atoi(baudport)); |
| 365 | have_device++; |
| 366 | } |
| 367 | } |
| 368 | if (device && !strlen(device)) { |
| 369 | msg_perr("Error: No device specified.\n" |
| 370 | "Use flashrom -p serprog:dev=/dev/device:baud\n"); |
| 371 | free(device); |
| 372 | return 1; |
| 373 | } |
| 374 | free(device); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 375 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 376 | device = extract_programmer_param("ip"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 377 | if (have_device && device) { |
| 378 | msg_perr("Error: Both host and device specified.\n" |
| 379 | "Please use either dev= or ip= but not both.\n"); |
| 380 | free(device); |
| 381 | return 1; |
| 382 | } |
| 383 | if (device && strlen(device)) { |
| 384 | baudport = strstr(device, ":"); |
| 385 | if (baudport) { |
| 386 | /* Split host from port. */ |
| 387 | *baudport = '\0'; |
| 388 | baudport++; |
| 389 | } |
| 390 | if (!baudport || !strlen(baudport)) { |
| 391 | msg_perr("Error: No port specified.\n" |
| 392 | "Use flashrom -p serprog:ip=ipaddr:port\n"); |
| 393 | free(device); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 394 | return 1; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 395 | } |
| 396 | if (strlen(device)) { |
| 397 | sp_fd = sp_opensocket(device, atoi(baudport)); |
| 398 | have_device++; |
| 399 | } |
| 400 | } |
| 401 | if (device && !strlen(device)) { |
| 402 | msg_perr("Error: No host specified.\n" |
| 403 | "Use flashrom -p serprog:ip=ipaddr:port\n"); |
| 404 | free(device); |
| 405 | return 1; |
| 406 | } |
| 407 | free(device); |
| 408 | |
| 409 | if (!have_device) { |
| 410 | msg_perr("Error: Neither host nor device specified.\n" |
| 411 | "Use flashrom -p serprog:dev=/dev/device:baud or " |
| 412 | "flashrom -p serprog:ip=ipaddr:port\n"); |
| 413 | return 1; |
| 414 | } |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 415 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 416 | if (register_shutdown(serprog_shutdown, NULL)) |
| 417 | return 1; |
| 418 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 419 | msg_pdbg(MSGHEADER "connected - attempting to synchronize\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 420 | |
| 421 | sp_check_avail_automatic = 0; |
| 422 | |
| 423 | sp_synchronize(); |
| 424 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 425 | msg_pdbg(MSGHEADER "Synchronized\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 426 | |
| 427 | if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) { |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 428 | msg_perr("Error: NAK to query interface version\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 429 | return 1; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if (iface != 1) { |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 433 | msg_perr("Error: Unknown interface version: %d\n", iface); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 434 | return 1; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 437 | msg_pdbg(MSGHEADER "Interface version ok.\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 438 | |
| 439 | if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 440 | msg_perr("Error: query command map not supported\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 441 | return 1; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | sp_check_avail_automatic = 1; |
| 445 | |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 446 | /* FIXME: This assumes that serprog device bustypes are always |
| 447 | * identical with flashrom bustype enums and that they all fit |
| 448 | * in a single byte. |
| 449 | */ |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 450 | if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) { |
| 451 | msg_perr("Warning: NAK to query supported buses\n"); |
| 452 | c = BUS_NONSPI; /* A reasonable default for now. */ |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 453 | } |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 454 | serprog_buses_supported = c; |
| 455 | |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 456 | msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n", |
| 457 | (c & BUS_PARALLEL) ? "on" : "off", |
| 458 | (c & BUS_LPC) ? "on" : "off", |
| 459 | (c & BUS_FWH) ? "on" : "off", |
| 460 | (c & BUS_SPI) ? "on" : "off"); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 461 | /* Check for the minimum operational set of commands. */ |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 462 | if (serprog_buses_supported & BUS_SPI) { |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 463 | uint8_t bt = BUS_SPI; |
| 464 | if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) { |
| 465 | msg_perr("Error: SPI operation not supported while the " |
| 466 | "bustype is SPI\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 467 | return 1; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 468 | } |
| 469 | /* Success of any of these commands is optional. We don't need |
| 470 | the programmer to tell us its limits, but if it doesn't, we |
| 471 | will assume stuff, so it's in the programmers best interest |
| 472 | to tell us. */ |
| 473 | sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL); |
| 474 | if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { |
| 475 | uint32_t v; |
| 476 | v = ((unsigned int)(rbuf[0]) << 0); |
| 477 | v |= ((unsigned int)(rbuf[1]) << 8); |
| 478 | v |= ((unsigned int)(rbuf[2]) << 16); |
| 479 | if (v == 0) |
| 480 | v = (1 << 24) - 1; /* SPI-op maximum. */ |
| 481 | spi_programmer_serprog.max_data_write = v; |
| 482 | msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v); |
| 483 | } |
| 484 | if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) { |
| 485 | uint32_t v; |
| 486 | v = ((unsigned int)(rbuf[0]) << 0); |
| 487 | v |= ((unsigned int)(rbuf[1]) << 8); |
| 488 | v |= ((unsigned int)(rbuf[2]) << 16); |
| 489 | if (v == 0) |
| 490 | v = (1 << 24) - 1; /* SPI-op maximum. */ |
| 491 | spi_programmer_serprog.max_data_read = v; |
| 492 | msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v); |
| 493 | } |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 494 | bt = serprog_buses_supported; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 495 | sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 496 | } |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 497 | |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 498 | if (serprog_buses_supported & BUS_NONSPI) { |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 499 | if (sp_check_commandavail(S_CMD_O_INIT) == 0) { |
| 500 | msg_perr("Error: Initialize operation buffer " |
| 501 | "not supported\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 502 | return 1; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (sp_check_commandavail(S_CMD_O_DELAY) == 0) { |
| 506 | msg_perr("Error: Write to opbuf: " |
| 507 | "delay not supported\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 508 | return 1; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* S_CMD_O_EXEC availability checked later. */ |
| 512 | |
| 513 | if (sp_check_commandavail(S_CMD_R_BYTE) == 0) { |
| 514 | msg_perr("Error: Single byte read not supported\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 515 | return 1; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 516 | } |
| 517 | /* This could be translated to single byte reads (if missing), |
| 518 | * but now we don't support that. */ |
| 519 | if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) { |
| 520 | msg_perr("Error: Read n bytes not supported\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 521 | return 1; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 522 | } |
| 523 | if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) { |
| 524 | msg_perr("Error: Write to opbuf: " |
| 525 | "write byte not supported\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 526 | return 1; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { |
| 530 | msg_pdbg(MSGHEADER "Write-n not supported"); |
| 531 | sp_max_write_n = 0; |
| 532 | } else { |
| 533 | sp_max_write_n = ((unsigned int)(rbuf[0]) << 0); |
| 534 | sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8); |
| 535 | sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16); |
| 536 | if (!sp_max_write_n) { |
| 537 | sp_max_write_n = (1 << 24); |
| 538 | } |
| 539 | msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", |
| 540 | sp_max_write_n); |
| 541 | sp_write_n_buf = malloc(sp_max_write_n); |
| 542 | if (!sp_write_n_buf) { |
| 543 | msg_perr("Error: cannot allocate memory for " |
| 544 | "Write-n buffer\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 545 | return 1; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 546 | } |
| 547 | sp_write_n_bytes = 0; |
| 548 | } |
| 549 | |
| 550 | if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) && |
| 551 | (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) { |
| 552 | sp_max_read_n = ((unsigned int)(rbuf[0]) << 0); |
| 553 | sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8); |
| 554 | sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16); |
| 555 | msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", |
| 556 | sp_max_read_n ? sp_max_read_n : (1 << 24)); |
| 557 | } else { |
| 558 | msg_pdbg(MSGHEADER "Maximum read-n length " |
| 559 | "not reported\n"); |
| 560 | sp_max_read_n = 0; |
| 561 | } |
| 562 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 566 | msg_perr("Warning: NAK to query programmer name\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 567 | strcpy((char *)pgmname, "(unknown)"); |
| 568 | } |
| 569 | pgmname[16] = 0; |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 570 | msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 571 | |
| 572 | if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 573 | msg_perr("Warning: NAK to query serial buffer size\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 574 | } |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 575 | msg_pdbg(MSGHEADER "Serial buffer size is %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 576 | sp_device_serbuf_size); |
| 577 | |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 578 | if (sp_check_commandavail(S_CMD_O_INIT)) { |
| 579 | /* This would be inconsistent. */ |
| 580 | if (sp_check_commandavail(S_CMD_O_EXEC) == 0) { |
| 581 | msg_perr("Error: Execute operation buffer not " |
| 582 | "supported\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 583 | return 1; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 584 | } |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 585 | |
| 586 | if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) { |
| 587 | msg_perr("Error: NAK to initialize operation buffer\n"); |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 588 | return 1; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, |
| 592 | &sp_device_opbuf_size)) { |
| 593 | msg_perr("Warning: NAK to query operation buffer " |
| 594 | "size\n"); |
| 595 | } |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 596 | msg_pdbg(MSGHEADER "operation buffer size is %d\n", |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 597 | sp_device_opbuf_size); |
| 598 | } |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 599 | |
| 600 | sp_prev_was_write = 0; |
| 601 | sp_streamed_transmit_ops = 0; |
| 602 | sp_streamed_transmit_bytes = 0; |
| 603 | sp_opbuf_usage = 0; |
Carl-Daniel Hailfinger | eaacd2d | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 604 | if (serprog_buses_supported & BUS_SPI) |
| 605 | register_spi_programmer(&spi_programmer_serprog); |
| 606 | if (serprog_buses_supported & BUS_NONSPI) |
| 607 | register_par_programmer(&par_programmer_serprog, |
| 608 | serprog_buses_supported & BUS_NONSPI); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | /* Move an in flashrom buffer existing write-n operation to * |
| 613 | * the on-device operation buffer. */ |
| 614 | static void sp_pass_writen(void) |
| 615 | { |
| 616 | unsigned char header[7]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 617 | msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 618 | sp_write_n_bytes, sp_write_n_addr); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 619 | if (sp_streamed_transmit_bytes >= |
| 620 | (7 + sp_write_n_bytes + sp_device_serbuf_size)) |
| 621 | sp_flush_stream(); |
| 622 | /* In case it's just a single byte send it as a single write. */ |
| 623 | if (sp_write_n_bytes == 1) { |
| 624 | sp_write_n_bytes = 0; |
| 625 | header[0] = (sp_write_n_addr >> 0) & 0xFF; |
| 626 | header[1] = (sp_write_n_addr >> 8) & 0xFF; |
| 627 | header[2] = (sp_write_n_addr >> 16) & 0xFF; |
| 628 | header[3] = sp_write_n_buf[0]; |
| 629 | sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header); |
| 630 | sp_opbuf_usage += 5; |
| 631 | return; |
| 632 | } |
| 633 | header[0] = S_CMD_O_WRITEN; |
| 634 | header[1] = (sp_write_n_bytes >> 0) & 0xFF; |
| 635 | header[2] = (sp_write_n_bytes >> 8) & 0xFF; |
| 636 | header[3] = (sp_write_n_bytes >> 16) & 0xFF; |
| 637 | header[4] = (sp_write_n_addr >> 0) & 0xFF; |
| 638 | header[5] = (sp_write_n_addr >> 8) & 0xFF; |
| 639 | header[6] = (sp_write_n_addr >> 16) & 0xFF; |
| 640 | if (write(sp_fd, header, 7) != 7) |
| 641 | sp_die("Error: cannot write write-n command\n"); |
| 642 | if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) != |
| 643 | sp_write_n_bytes) |
| 644 | sp_die("Error: cannot write write-n data"); |
| 645 | sp_streamed_transmit_bytes += 7 + sp_write_n_bytes; |
| 646 | sp_streamed_transmit_ops += 1; |
| 647 | sp_opbuf_usage += 7 + sp_write_n_bytes; |
| 648 | sp_write_n_bytes = 0; |
| 649 | sp_prev_was_write = 0; |
| 650 | } |
| 651 | |
| 652 | static void sp_execute_opbuf_noflush(void) |
| 653 | { |
| 654 | if ((sp_max_write_n) && (sp_write_n_bytes)) |
| 655 | sp_pass_writen(); |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 656 | sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 657 | msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 658 | sp_opbuf_usage); |
| 659 | sp_opbuf_usage = 0; |
| 660 | sp_prev_was_write = 0; |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | static void sp_execute_opbuf(void) |
| 665 | { |
| 666 | sp_execute_opbuf_noflush(); |
| 667 | sp_flush_stream(); |
| 668 | } |
| 669 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 670 | static int serprog_shutdown(void *data) |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 671 | { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 672 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 673 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 674 | sp_execute_opbuf(); |
| 675 | close(sp_fd); |
| 676 | if (sp_max_write_n) |
| 677 | free(sp_write_n_buf); |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | static void sp_check_opbuf_usage(int bytes_to_be_added) |
| 682 | { |
| 683 | if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) { |
| 684 | sp_execute_opbuf(); |
| 685 | /* If this happens in the mid of an page load the page load * |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 686 | * will probably fail. */ |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 687 | msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 691 | static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val, |
| 692 | chipaddr addr) |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 693 | { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 694 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 695 | if (sp_max_write_n) { |
| 696 | if ((sp_prev_was_write) |
| 697 | && (addr == (sp_write_n_addr + sp_write_n_bytes))) { |
| 698 | sp_write_n_buf[sp_write_n_bytes++] = val; |
| 699 | } else { |
| 700 | if ((sp_prev_was_write) && (sp_write_n_bytes)) |
| 701 | sp_pass_writen(); |
| 702 | sp_prev_was_write = 1; |
| 703 | sp_write_n_addr = addr; |
| 704 | sp_write_n_bytes = 1; |
| 705 | sp_write_n_buf[0] = val; |
| 706 | } |
| 707 | sp_check_opbuf_usage(7 + sp_write_n_bytes); |
| 708 | if (sp_write_n_bytes >= sp_max_write_n) |
| 709 | sp_pass_writen(); |
| 710 | } else { |
| 711 | /* We will have to do single writeb ops. */ |
| 712 | unsigned char writeb_parm[4]; |
| 713 | sp_check_opbuf_usage(6); |
| 714 | writeb_parm[0] = (addr >> 0) & 0xFF; |
| 715 | writeb_parm[1] = (addr >> 8) & 0xFF; |
| 716 | writeb_parm[2] = (addr >> 16) & 0xFF; |
| 717 | writeb_parm[3] = val; |
| 718 | sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); |
| 719 | sp_opbuf_usage += 5; |
| 720 | } |
| 721 | } |
| 722 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 723 | static uint8_t serprog_chip_readb(const struct flashctx *flash, |
| 724 | const chipaddr addr) |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 725 | { |
| 726 | unsigned char c; |
| 727 | unsigned char buf[3]; |
| 728 | /* Will stream the read operation - eg. add it to the stream buffer, * |
| 729 | * then flush the buffer, then read the read answer. */ |
| 730 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 731 | sp_execute_opbuf_noflush(); |
| 732 | buf[0] = ((addr >> 0) & 0xFF); |
| 733 | buf[1] = ((addr >> 8) & 0xFF); |
| 734 | buf[2] = ((addr >> 16) & 0xFF); |
| 735 | sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); |
| 736 | sp_flush_stream(); |
| 737 | if (read(sp_fd, &c, 1) != 1) |
| 738 | sp_die("readb byteread"); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 739 | msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 740 | return c; |
| 741 | } |
| 742 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 743 | /* Local version that really does the job, doesn't care of max_read_n. */ |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 744 | static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len) |
| 745 | { |
| 746 | int rd_bytes = 0; |
| 747 | unsigned char sbuf[6]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 748 | msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 749 | /* Stream the read-n -- as above. */ |
| 750 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 751 | sp_execute_opbuf_noflush(); |
| 752 | sbuf[0] = ((addr >> 0) & 0xFF); |
| 753 | sbuf[1] = ((addr >> 8) & 0xFF); |
| 754 | sbuf[2] = ((addr >> 16) & 0xFF); |
| 755 | sbuf[3] = ((len >> 0) & 0xFF); |
| 756 | sbuf[4] = ((len >> 8) & 0xFF); |
| 757 | sbuf[5] = ((len >> 16) & 0xFF); |
| 758 | sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf); |
| 759 | sp_flush_stream(); |
| 760 | do { |
| 761 | int r = read(sp_fd, buf + rd_bytes, len - rd_bytes); |
| 762 | if (r <= 0) |
| 763 | sp_die("Error: cannot read read-n data"); |
| 764 | rd_bytes += r; |
| 765 | } while (rd_bytes != len); |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | /* The externally called version that makes sure that max_read_n is obeyed. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 770 | static void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf, |
| 771 | const chipaddr addr, size_t len) |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 772 | { |
| 773 | size_t lenm = len; |
| 774 | chipaddr addrm = addr; |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 775 | while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) { |
| 776 | sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 777 | addrm += sp_max_read_n; |
| 778 | lenm -= sp_max_read_n; |
| 779 | } |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 780 | if (lenm) |
| 781 | sp_do_read_n(&(buf[addrm-addr]), addrm, lenm); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 784 | void serprog_delay(int usecs) |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 785 | { |
| 786 | unsigned char buf[4]; |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 787 | msg_pspew("%s usecs=%d\n", __func__, usecs); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 788 | if (!sp_check_commandavail(S_CMD_O_DELAY)) { |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 789 | msg_pdbg("Note: serprog_delay used, but the programmer doesn't " |
| 790 | "support delay\n"); |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 791 | internal_delay(usecs); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 792 | return; |
| 793 | } |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 794 | if ((sp_max_write_n) && (sp_write_n_bytes)) |
| 795 | sp_pass_writen(); |
| 796 | sp_check_opbuf_usage(5); |
Stefan Tauner | 31019d4 | 2011-10-22 21:45:27 +0000 | [diff] [blame] | 797 | buf[0] = ((usecs >> 0) & 0xFF); |
| 798 | buf[1] = ((usecs >> 8) & 0xFF); |
| 799 | buf[2] = ((usecs >> 16) & 0xFF); |
| 800 | buf[3] = ((usecs >> 24) & 0xFF); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 801 | sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf); |
| 802 | sp_opbuf_usage += 5; |
| 803 | sp_prev_was_write = 0; |
| 804 | } |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 805 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 806 | static int serprog_spi_send_command(struct flashctx *flash, |
| 807 | unsigned int writecnt, unsigned int readcnt, |
| 808 | const unsigned char *writearr, |
| 809 | unsigned char *readarr) |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 810 | { |
| 811 | unsigned char *parmbuf; |
| 812 | int ret; |
| 813 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
| 814 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 815 | sp_execute_opbuf(); |
| 816 | parmbuf = malloc(writecnt + 6); |
| 817 | if (!parmbuf) |
| 818 | sp_die("Error: cannot malloc SPI send param buffer"); |
| 819 | parmbuf[0] = (writecnt >> 0) & 0xFF; |
| 820 | parmbuf[1] = (writecnt >> 8) & 0xFF; |
| 821 | parmbuf[2] = (writecnt >> 16) & 0xFF; |
| 822 | parmbuf[3] = (readcnt >> 0) & 0xFF; |
| 823 | parmbuf[4] = (readcnt >> 8) & 0xFF; |
| 824 | parmbuf[5] = (readcnt >> 16) & 0xFF; |
| 825 | memcpy(parmbuf + 6, writearr, writecnt); |
| 826 | ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt, |
| 827 | readarr); |
| 828 | free(parmbuf); |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | /* FIXME: This function is optimized so that it does not split each transaction |
| 833 | * into chip page_size long blocks unnecessarily like spi_read_chunked. This has |
| 834 | * the advantage that it is much faster for most chips, but breaks those with |
| 835 | * non-contiguous address space (like AT45DB161D). When spi_read_chunked is |
| 836 | * fixed this method can be removed. */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 837 | static int serprog_spi_read(struct flashctx *flash, uint8_t *buf, |
| 838 | unsigned int start, unsigned int len) |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 839 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 840 | unsigned int i, cur_len; |
| 841 | const unsigned int max_read = spi_programmer_serprog.max_data_read; |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 842 | for (i = 0; i < len; i += cur_len) { |
| 843 | int ret; |
| 844 | cur_len = min(max_read, (len - i)); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 845 | ret = spi_nbyte_read(flash, start + i, buf + i, cur_len); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 846 | if (ret) |
| 847 | return ret; |
| 848 | } |
| 849 | return 0; |
| 850 | } |