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