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