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