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