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