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