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