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 | |
| 41 | #define MSGHEADER "serprog:" |
| 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 | } |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 207 | msg_perr("Error: cannot synchronize protocol\n" |
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)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 223 | msg_pdbg("Warning: Automatic command availability check" |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 224 | " failed for cmd %d - wont execute cmd\n",cmd); |
| 225 | return 1; |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int sp_docommand(uint8_t command, uint32_t parmlen, |
| 231 | uint8_t * params, uint32_t retlen, void *retparms) |
| 232 | { |
| 233 | unsigned char *sendpacket; |
| 234 | unsigned char c; |
| 235 | if (sp_automatic_cmdcheck(command)) |
| 236 | return 1; |
| 237 | sendpacket = malloc(1 + parmlen); |
| 238 | if (!sendpacket) |
| 239 | sp_die("Error: cannot malloc command buffer"); |
| 240 | sendpacket[0] = command; |
| 241 | memcpy(&(sendpacket[1]), params, parmlen); |
| 242 | if (write(sp_fd, sendpacket, 1 + parmlen) != (1 + parmlen)) { |
| 243 | sp_die("Error: cannot write command"); |
| 244 | } |
| 245 | free(sendpacket); |
| 246 | if (read(sp_fd, &c, 1) != 1) |
| 247 | sp_die("Error: cannot read from device"); |
| 248 | if (c == S_NAK) return 1; |
| 249 | if (c != S_ACK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 250 | msg_perr("Error: invalid response 0x%02X from device\n",c); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 251 | exit(1); |
| 252 | } |
| 253 | if (retlen) { |
| 254 | int rd_bytes = 0; |
| 255 | do { |
| 256 | int r; |
| 257 | r = read(sp_fd, retparms + rd_bytes, |
| 258 | retlen - rd_bytes); |
| 259 | if (r <= 0) sp_die |
| 260 | ("Error: cannot read return parameters"); |
| 261 | rd_bytes += r; |
| 262 | } while (rd_bytes != retlen); |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static void sp_flush_stream(void) |
| 268 | { |
| 269 | if (sp_streamed_transmit_ops) |
| 270 | do { |
| 271 | unsigned char c; |
| 272 | if (read(sp_fd, &c, 1) != 1) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 273 | sp_die("Error: cannot read from device (flushing stream)"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 274 | } |
| 275 | if (c == S_NAK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 276 | msg_perr("Error: NAK to a stream buffer operation\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 277 | exit(1); |
| 278 | } |
| 279 | if (c != S_ACK) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 280 | msg_perr("Error: Invalid reply 0x%02X from device\n", c); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 281 | exit(1); |
| 282 | } |
| 283 | } while (--sp_streamed_transmit_ops); |
| 284 | sp_streamed_transmit_ops = 0; |
| 285 | sp_streamed_transmit_bytes = 0; |
| 286 | } |
| 287 | |
| 288 | static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms) |
| 289 | { |
| 290 | uint8_t *sp; |
| 291 | if (sp_automatic_cmdcheck(cmd)) |
| 292 | return 1; |
| 293 | sp = malloc(1 + parmlen); |
| 294 | if (!sp) sp_die("Error: cannot malloc command buffer"); |
| 295 | sp[0] = cmd; |
| 296 | memcpy(&(sp[1]), parms, parmlen); |
| 297 | if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size)) |
| 298 | sp_flush_stream(); |
| 299 | if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen)) |
| 300 | sp_die("Error: cannot write command"); |
| 301 | free(sp); |
| 302 | sp_streamed_transmit_ops += 1; |
| 303 | sp_streamed_transmit_bytes += 1 + parmlen; |
| 304 | return 0; |
| 305 | } |
| 306 | |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 307 | static struct spi_programmer spi_programmer_serprog = { |
| 308 | .type = SPI_CONTROLLER_SERPROG, |
| 309 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 310 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
| 311 | .command = serprog_spi_send_command, |
| 312 | .multicommand = default_spi_send_multicommand, |
| 313 | .read = serprog_spi_read, |
| 314 | .write_256 = default_spi_write_256, |
| 315 | }; |
| 316 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 317 | int serprog_init(void) |
| 318 | { |
| 319 | uint16_t iface; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 320 | unsigned char pgmname[17]; |
| 321 | unsigned char rbuf[3]; |
| 322 | unsigned char c; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 323 | char *device; |
| 324 | char *baudport; |
| 325 | int have_device = 0; |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 326 | |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 327 | /* 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] | 328 | device = extract_programmer_param("dev"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 329 | if (device && strlen(device)) { |
| 330 | baudport = strstr(device, ":"); |
| 331 | if (baudport) { |
| 332 | /* Split device from baudrate. */ |
| 333 | *baudport = '\0'; |
| 334 | baudport++; |
| 335 | } |
| 336 | if (!baudport || !strlen(baudport)) { |
| 337 | msg_perr("Error: No baudrate specified.\n" |
| 338 | "Use flashrom -p serprog:dev=/dev/device:baud\n"); |
| 339 | free(device); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 340 | return 1; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 341 | } |
| 342 | if (strlen(device)) { |
| 343 | sp_fd = sp_openserport(device, atoi(baudport)); |
| 344 | have_device++; |
| 345 | } |
| 346 | } |
| 347 | if (device && !strlen(device)) { |
| 348 | msg_perr("Error: No device specified.\n" |
| 349 | "Use flashrom -p serprog:dev=/dev/device:baud\n"); |
| 350 | free(device); |
| 351 | return 1; |
| 352 | } |
| 353 | free(device); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 354 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 355 | device = extract_programmer_param("ip"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 356 | if (have_device && device) { |
| 357 | msg_perr("Error: Both host and device specified.\n" |
| 358 | "Please use either dev= or ip= but not both.\n"); |
| 359 | free(device); |
| 360 | return 1; |
| 361 | } |
| 362 | if (device && strlen(device)) { |
| 363 | baudport = strstr(device, ":"); |
| 364 | if (baudport) { |
| 365 | /* Split host from port. */ |
| 366 | *baudport = '\0'; |
| 367 | baudport++; |
| 368 | } |
| 369 | if (!baudport || !strlen(baudport)) { |
| 370 | msg_perr("Error: No port specified.\n" |
| 371 | "Use flashrom -p serprog:ip=ipaddr:port\n"); |
| 372 | free(device); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 373 | return 1; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 374 | } |
| 375 | if (strlen(device)) { |
| 376 | sp_fd = sp_opensocket(device, atoi(baudport)); |
| 377 | have_device++; |
| 378 | } |
| 379 | } |
| 380 | if (device && !strlen(device)) { |
| 381 | msg_perr("Error: No host specified.\n" |
| 382 | "Use flashrom -p serprog:ip=ipaddr:port\n"); |
| 383 | free(device); |
| 384 | return 1; |
| 385 | } |
| 386 | free(device); |
| 387 | |
| 388 | if (!have_device) { |
| 389 | msg_perr("Error: Neither host nor device specified.\n" |
| 390 | "Use flashrom -p serprog:dev=/dev/device:baud or " |
| 391 | "flashrom -p serprog:ip=ipaddr:port\n"); |
| 392 | return 1; |
| 393 | } |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 394 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 395 | if (register_shutdown(serprog_shutdown, NULL)) |
| 396 | return 1; |
| 397 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 398 | msg_pdbg(MSGHEADER "connected - attempting to synchronize\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 399 | |
| 400 | sp_check_avail_automatic = 0; |
| 401 | |
| 402 | sp_synchronize(); |
| 403 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 404 | msg_pdbg(MSGHEADER "Synchronized\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 405 | |
| 406 | if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 407 | msg_perr("Error: NAK to Query Interface version\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 408 | exit(1); |
| 409 | } |
| 410 | |
| 411 | if (iface != 1) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 412 | msg_perr("Error: Unknown interface version %d\n", iface); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 413 | exit(1); |
| 414 | } |
| 415 | |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 416 | msg_pdbg(MSGHEADER "Interface version ok.\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 417 | |
| 418 | if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 419 | msg_perr("Error: query command map not supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 420 | exit(1); |
| 421 | } |
| 422 | |
| 423 | sp_check_avail_automatic = 1; |
| 424 | |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 425 | |
| 426 | if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) { |
| 427 | msg_perr("Warning: NAK to query supported buses\n"); |
| 428 | c = BUS_NONSPI; /* A reasonable default for now. */ |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 429 | } |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 430 | buses_supported = c; |
| 431 | /* Check for the minimum operational set of commands. */ |
| 432 | if (buses_supported & BUS_SPI) { |
| 433 | uint8_t bt = BUS_SPI; |
| 434 | if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) { |
| 435 | msg_perr("Error: SPI operation not supported while the " |
| 436 | "bustype is SPI\n"); |
| 437 | exit(1); |
| 438 | } |
| 439 | /* Success of any of these commands is optional. We don't need |
| 440 | the programmer to tell us its limits, but if it doesn't, we |
| 441 | will assume stuff, so it's in the programmers best interest |
| 442 | to tell us. */ |
| 443 | sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL); |
| 444 | if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { |
| 445 | uint32_t v; |
| 446 | v = ((unsigned int)(rbuf[0]) << 0); |
| 447 | v |= ((unsigned int)(rbuf[1]) << 8); |
| 448 | v |= ((unsigned int)(rbuf[2]) << 16); |
| 449 | if (v == 0) |
| 450 | v = (1 << 24) - 1; /* SPI-op maximum. */ |
| 451 | spi_programmer_serprog.max_data_write = v; |
| 452 | msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v); |
| 453 | } |
| 454 | if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) { |
| 455 | uint32_t v; |
| 456 | v = ((unsigned int)(rbuf[0]) << 0); |
| 457 | v |= ((unsigned int)(rbuf[1]) << 8); |
| 458 | v |= ((unsigned int)(rbuf[2]) << 16); |
| 459 | if (v == 0) |
| 460 | v = (1 << 24) - 1; /* SPI-op maximum. */ |
| 461 | spi_programmer_serprog.max_data_read = v; |
| 462 | msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v); |
| 463 | } |
| 464 | bt = buses_supported; |
| 465 | sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL); |
| 466 | register_spi_programmer(&spi_programmer_serprog); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 467 | } |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 468 | |
| 469 | if (buses_supported & BUS_NONSPI) { |
| 470 | if (sp_check_commandavail(S_CMD_O_INIT) == 0) { |
| 471 | msg_perr("Error: Initialize operation buffer " |
| 472 | "not supported\n"); |
| 473 | exit(1); |
| 474 | } |
| 475 | |
| 476 | if (sp_check_commandavail(S_CMD_O_DELAY) == 0) { |
| 477 | msg_perr("Error: Write to opbuf: " |
| 478 | "delay not supported\n"); |
| 479 | exit(1); |
| 480 | } |
| 481 | |
| 482 | /* S_CMD_O_EXEC availability checked later. */ |
| 483 | |
| 484 | if (sp_check_commandavail(S_CMD_R_BYTE) == 0) { |
| 485 | msg_perr("Error: Single byte read not supported\n"); |
| 486 | exit(1); |
| 487 | } |
| 488 | /* This could be translated to single byte reads (if missing), |
| 489 | * but now we don't support that. */ |
| 490 | if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) { |
| 491 | msg_perr("Error: Read n bytes not supported\n"); |
| 492 | exit(1); |
| 493 | } |
| 494 | if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) { |
| 495 | msg_perr("Error: Write to opbuf: " |
| 496 | "write byte not supported\n"); |
| 497 | exit(1); |
| 498 | } |
| 499 | |
| 500 | if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) { |
| 501 | msg_pdbg(MSGHEADER "Write-n not supported"); |
| 502 | sp_max_write_n = 0; |
| 503 | } else { |
| 504 | sp_max_write_n = ((unsigned int)(rbuf[0]) << 0); |
| 505 | sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8); |
| 506 | sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16); |
| 507 | if (!sp_max_write_n) { |
| 508 | sp_max_write_n = (1 << 24); |
| 509 | } |
| 510 | msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", |
| 511 | sp_max_write_n); |
| 512 | sp_write_n_buf = malloc(sp_max_write_n); |
| 513 | if (!sp_write_n_buf) { |
| 514 | msg_perr("Error: cannot allocate memory for " |
| 515 | "Write-n buffer\n"); |
| 516 | exit(1); |
| 517 | } |
| 518 | sp_write_n_bytes = 0; |
| 519 | } |
| 520 | |
| 521 | if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) && |
| 522 | (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) { |
| 523 | sp_max_read_n = ((unsigned int)(rbuf[0]) << 0); |
| 524 | sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8); |
| 525 | sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16); |
| 526 | msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", |
| 527 | sp_max_read_n ? sp_max_read_n : (1 << 24)); |
| 528 | } else { |
| 529 | msg_pdbg(MSGHEADER "Maximum read-n length " |
| 530 | "not reported\n"); |
| 531 | sp_max_read_n = 0; |
| 532 | } |
| 533 | |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 537 | msg_perr("Warning: NAK to query programmer name\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 538 | strcpy((char *)pgmname, "(unknown)"); |
| 539 | } |
| 540 | pgmname[16] = 0; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 541 | msg_pinfo(MSGHEADER "Programmer name \"%s\"\n", pgmname); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 542 | |
| 543 | 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] | 544 | msg_perr("Warning: NAK to query serial buffer size\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 545 | } |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 546 | msg_pdbg(MSGHEADER "serial buffer size %d\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 547 | sp_device_serbuf_size); |
| 548 | |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 549 | if (sp_check_commandavail(S_CMD_O_INIT)) { |
| 550 | /* This would be inconsistent. */ |
| 551 | if (sp_check_commandavail(S_CMD_O_EXEC) == 0) { |
| 552 | msg_perr("Error: Execute operation buffer not " |
| 553 | "supported\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 554 | exit(1); |
| 555 | } |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 556 | |
| 557 | if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) { |
| 558 | msg_perr("Error: NAK to initialize operation buffer\n"); |
| 559 | exit(1); |
| 560 | } |
| 561 | |
| 562 | if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, |
| 563 | &sp_device_opbuf_size)) { |
| 564 | msg_perr("Warning: NAK to query operation buffer " |
| 565 | "size\n"); |
| 566 | } |
| 567 | msg_pdbg(MSGHEADER "operation buffer size %d\n", |
| 568 | sp_device_opbuf_size); |
| 569 | } |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 570 | |
| 571 | sp_prev_was_write = 0; |
| 572 | sp_streamed_transmit_ops = 0; |
| 573 | sp_streamed_transmit_bytes = 0; |
| 574 | sp_opbuf_usage = 0; |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /* Move an in flashrom buffer existing write-n operation to * |
| 579 | * the on-device operation buffer. */ |
| 580 | static void sp_pass_writen(void) |
| 581 | { |
| 582 | unsigned char header[7]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 583 | msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 584 | sp_write_n_bytes, sp_write_n_addr); |
| 585 | if (sp_streamed_transmit_bytes >= |
| 586 | (7 + sp_write_n_bytes + sp_device_serbuf_size)) |
| 587 | sp_flush_stream(); |
| 588 | /* In case it's just a single byte send it as a single write. */ |
| 589 | if (sp_write_n_bytes == 1) { |
| 590 | sp_write_n_bytes = 0; |
| 591 | header[0] = (sp_write_n_addr >> 0) & 0xFF; |
| 592 | header[1] = (sp_write_n_addr >> 8) & 0xFF; |
| 593 | header[2] = (sp_write_n_addr >> 16) & 0xFF; |
| 594 | header[3] = sp_write_n_buf[0]; |
| 595 | sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header); |
| 596 | sp_opbuf_usage += 5; |
| 597 | return; |
| 598 | } |
| 599 | header[0] = S_CMD_O_WRITEN; |
| 600 | header[1] = (sp_write_n_bytes >> 0) & 0xFF; |
| 601 | header[2] = (sp_write_n_bytes >> 8) & 0xFF; |
| 602 | header[3] = (sp_write_n_bytes >> 16) & 0xFF; |
| 603 | header[4] = (sp_write_n_addr >> 0) & 0xFF; |
| 604 | header[5] = (sp_write_n_addr >> 8) & 0xFF; |
| 605 | header[6] = (sp_write_n_addr >> 16) & 0xFF; |
| 606 | if (write(sp_fd, header, 7) != 7) |
| 607 | sp_die("Error: cannot write write-n command\n"); |
| 608 | if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) != |
| 609 | sp_write_n_bytes) |
| 610 | sp_die("Error: cannot write write-n data"); |
| 611 | sp_streamed_transmit_bytes += 7 + sp_write_n_bytes; |
| 612 | sp_streamed_transmit_ops += 1; |
| 613 | sp_opbuf_usage += 7 + sp_write_n_bytes; |
| 614 | sp_write_n_bytes = 0; |
| 615 | sp_prev_was_write = 0; |
| 616 | } |
| 617 | |
| 618 | static void sp_execute_opbuf_noflush(void) |
| 619 | { |
| 620 | if ((sp_max_write_n) && (sp_write_n_bytes)) |
| 621 | sp_pass_writen(); |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 622 | sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 623 | msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 624 | sp_opbuf_usage); |
| 625 | sp_opbuf_usage = 0; |
| 626 | sp_prev_was_write = 0; |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | static void sp_execute_opbuf(void) |
| 631 | { |
| 632 | sp_execute_opbuf_noflush(); |
| 633 | sp_flush_stream(); |
| 634 | } |
| 635 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 636 | static int serprog_shutdown(void *data) |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 637 | { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 638 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 639 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 640 | sp_execute_opbuf(); |
| 641 | close(sp_fd); |
| 642 | if (sp_max_write_n) |
| 643 | free(sp_write_n_buf); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static void sp_check_opbuf_usage(int bytes_to_be_added) |
| 648 | { |
| 649 | if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) { |
| 650 | sp_execute_opbuf(); |
| 651 | /* 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] | 652 | * will probably fail. */ |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 653 | msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n"); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | void serprog_chip_writeb(uint8_t val, chipaddr addr) |
| 658 | { |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 659 | msg_pspew("%s\n", __func__); |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 660 | if (sp_max_write_n) { |
| 661 | if ((sp_prev_was_write) |
| 662 | && (addr == (sp_write_n_addr + sp_write_n_bytes))) { |
| 663 | sp_write_n_buf[sp_write_n_bytes++] = val; |
| 664 | } else { |
| 665 | if ((sp_prev_was_write) && (sp_write_n_bytes)) |
| 666 | sp_pass_writen(); |
| 667 | sp_prev_was_write = 1; |
| 668 | sp_write_n_addr = addr; |
| 669 | sp_write_n_bytes = 1; |
| 670 | sp_write_n_buf[0] = val; |
| 671 | } |
| 672 | sp_check_opbuf_usage(7 + sp_write_n_bytes); |
| 673 | if (sp_write_n_bytes >= sp_max_write_n) |
| 674 | sp_pass_writen(); |
| 675 | } else { |
| 676 | /* We will have to do single writeb ops. */ |
| 677 | unsigned char writeb_parm[4]; |
| 678 | sp_check_opbuf_usage(6); |
| 679 | writeb_parm[0] = (addr >> 0) & 0xFF; |
| 680 | writeb_parm[1] = (addr >> 8) & 0xFF; |
| 681 | writeb_parm[2] = (addr >> 16) & 0xFF; |
| 682 | writeb_parm[3] = val; |
| 683 | sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); |
| 684 | sp_opbuf_usage += 5; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | uint8_t serprog_chip_readb(const chipaddr addr) |
| 689 | { |
| 690 | unsigned char c; |
| 691 | unsigned char buf[3]; |
| 692 | /* Will stream the read operation - eg. add it to the stream buffer, * |
| 693 | * then flush the buffer, then read the read answer. */ |
| 694 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 695 | sp_execute_opbuf_noflush(); |
| 696 | buf[0] = ((addr >> 0) & 0xFF); |
| 697 | buf[1] = ((addr >> 8) & 0xFF); |
| 698 | buf[2] = ((addr >> 16) & 0xFF); |
| 699 | sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); |
| 700 | sp_flush_stream(); |
| 701 | if (read(sp_fd, &c, 1) != 1) |
| 702 | sp_die("readb byteread"); |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 703 | 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] | 704 | return c; |
| 705 | } |
| 706 | |
Uwe Hermann | 4e3d0b3 | 2010-03-25 23:18:41 +0000 | [diff] [blame] | 707 | /* 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] | 708 | static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len) |
| 709 | { |
| 710 | int rd_bytes = 0; |
| 711 | unsigned char sbuf[6]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 712 | 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] | 713 | /* Stream the read-n -- as above. */ |
| 714 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 715 | sp_execute_opbuf_noflush(); |
| 716 | sbuf[0] = ((addr >> 0) & 0xFF); |
| 717 | sbuf[1] = ((addr >> 8) & 0xFF); |
| 718 | sbuf[2] = ((addr >> 16) & 0xFF); |
| 719 | sbuf[3] = ((len >> 0) & 0xFF); |
| 720 | sbuf[4] = ((len >> 8) & 0xFF); |
| 721 | sbuf[5] = ((len >> 16) & 0xFF); |
| 722 | sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf); |
| 723 | sp_flush_stream(); |
| 724 | do { |
| 725 | int r = read(sp_fd, buf + rd_bytes, len - rd_bytes); |
| 726 | if (r <= 0) |
| 727 | sp_die("Error: cannot read read-n data"); |
| 728 | rd_bytes += r; |
| 729 | } while (rd_bytes != len); |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | /* The externally called version that makes sure that max_read_n is obeyed. */ |
| 734 | void serprog_chip_readn(uint8_t * buf, const chipaddr addr, size_t len) |
| 735 | { |
| 736 | size_t lenm = len; |
| 737 | chipaddr addrm = addr; |
| 738 | while ((sp_max_read_n)&&(lenm > sp_max_read_n)) { |
| 739 | sp_do_read_n(&(buf[addrm-addr]),addrm,sp_max_read_n); |
| 740 | addrm += sp_max_read_n; |
| 741 | lenm -= sp_max_read_n; |
| 742 | } |
| 743 | if (lenm) sp_do_read_n(&(buf[addrm-addr]),addrm,lenm); |
| 744 | } |
| 745 | |
| 746 | void serprog_delay(int delay) |
| 747 | { |
| 748 | unsigned char buf[4]; |
Sean Nelson | 74e8af5 | 2010-01-10 01:06:23 +0000 | [diff] [blame] | 749 | msg_pspew("%s\n", __func__); |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 750 | if (!sp_check_commandavail(S_CMD_O_DELAY)) { |
| 751 | internal_delay(delay); |
| 752 | msg_pdbg("Note: serprog_delay used, but the programmer doesn't " |
| 753 | "support delay\n"); |
| 754 | return; |
| 755 | } |
Urja Rannikko | f3196df | 2009-07-21 13:02:59 +0000 | [diff] [blame] | 756 | if ((sp_max_write_n) && (sp_write_n_bytes)) |
| 757 | sp_pass_writen(); |
| 758 | sp_check_opbuf_usage(5); |
| 759 | buf[0] = ((delay >> 0) & 0xFF); |
| 760 | buf[1] = ((delay >> 8) & 0xFF); |
| 761 | buf[2] = ((delay >> 16) & 0xFF); |
| 762 | buf[3] = ((delay >> 24) & 0xFF); |
| 763 | sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf); |
| 764 | sp_opbuf_usage += 5; |
| 765 | sp_prev_was_write = 0; |
| 766 | } |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 767 | |
| 768 | int serprog_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 769 | const unsigned char *writearr, |
| 770 | unsigned char *readarr) |
| 771 | { |
| 772 | unsigned char *parmbuf; |
| 773 | int ret; |
| 774 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
| 775 | if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) |
| 776 | sp_execute_opbuf(); |
| 777 | parmbuf = malloc(writecnt + 6); |
| 778 | if (!parmbuf) |
| 779 | sp_die("Error: cannot malloc SPI send param buffer"); |
| 780 | parmbuf[0] = (writecnt >> 0) & 0xFF; |
| 781 | parmbuf[1] = (writecnt >> 8) & 0xFF; |
| 782 | parmbuf[2] = (writecnt >> 16) & 0xFF; |
| 783 | parmbuf[3] = (readcnt >> 0) & 0xFF; |
| 784 | parmbuf[4] = (readcnt >> 8) & 0xFF; |
| 785 | parmbuf[5] = (readcnt >> 16) & 0xFF; |
| 786 | memcpy(parmbuf + 6, writearr, writecnt); |
| 787 | ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt, |
| 788 | readarr); |
| 789 | free(parmbuf); |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | /* FIXME: This function is optimized so that it does not split each transaction |
| 794 | * into chip page_size long blocks unnecessarily like spi_read_chunked. This has |
| 795 | * the advantage that it is much faster for most chips, but breaks those with |
| 796 | * non-contiguous address space (like AT45DB161D). When spi_read_chunked is |
| 797 | * fixed this method can be removed. */ |
Stefan Tauner | 8c35745 | 2011-09-18 22:42:18 +0000 | [diff] [blame^] | 798 | int serprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
Urja Rannikko | c93f5f1 | 2011-09-15 23:38:14 +0000 | [diff] [blame] | 799 | { |
| 800 | int i; |
| 801 | int cur_len; |
| 802 | const int max_read = spi_programmer_serprog.max_data_read; |
| 803 | for (i = 0; i < len; i += cur_len) { |
| 804 | int ret; |
| 805 | cur_len = min(max_read, (len - i)); |
| 806 | ret = spi_nbyte_read(start + i, buf + i, cur_len); |
| 807 | if (ret) |
| 808 | return ret; |
| 809 | } |
| 810 | return 0; |
| 811 | } |