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