blob: 3525df7aaa273f90fc920f260a81e3a0b2ea776e [file] [log] [blame]
Urja Rannikko22915352009-06-23 11:33:43 +00001/*
2 * This file is part of the flashrom project.
3 *
Urja Rannikkoc93f5f12011-09-15 23:38:14 +00004 * Copyright (C) 2009, 2011 Urja Rannikko <urjaman@gmail.com>
Urja Rannikko22915352009-06-23 11:33:43 +00005 * Copyright (C) 2009 Carl-Daniel Hailfinger
Riku Viitanenddb6d922024-01-15 19:15:49 +02006 * Copyright (C) 2024 Riku Viitanen <riku.viitanen@protonmail.com>
Urja Rannikko22915352009-06-23 11:33:43 +00007 *
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 Rannikko22915352009-06-23 11:33:43 +000017 */
18
Felix Singer815eb792022-08-19 01:02:42 +020019#include <stdbool.h>
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000020#include <stdio.h>
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000021#if ! IS_WINDOWS /* stuff (presumably) needed for sockets only */
Urja Rannikko22915352009-06-23 11:33:43 +000022#include <stdlib.h>
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000023#include <unistd.h>
Urja Rannikko22915352009-06-23 11:33:43 +000024#include <fcntl.h>
Urja Rannikko22915352009-06-23 11:33:43 +000025#include <sys/socket.h>
26#include <arpa/inet.h>
27#include <netinet/in.h>
28#include <netinet/tcp.h>
29#include <netdb.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000030#endif
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000031#if IS_WINDOWS
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000032#include <conio.h>
33#else
Urja Rannikko22915352009-06-23 11:33:43 +000034#include <termios.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000035#endif
36#include <string.h>
37#include <errno.h>
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000038#include "flash.h"
39#include "programmer.h"
Urja Rannikkoc93f5f12011-09-15 23:38:14 +000040#include "chipdrivers.h"
Felix Singer100be2c2021-10-13 13:40:07 +020041
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 Viitanenddb6d922024-01-15 19:15:49 +020067#define S_CMD_S_SPI_CS 0x16 /* Set SPI chip select to use */
Urja Rannikkof3196df2009-07-21 13:02:59 +000068
Stefan Tauner31019d42011-10-22 21:45:27 +000069#define MSGHEADER "serprog: "
Urja Rannikkof3196df2009-07-21 13:02:59 +000070
David Hendricks8bb20212011-06-14 01:35:36 +000071/*
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 */
76static int serprog_shutdown(void *data);
77
Urja Rannikkof3196df2009-07-21 13:02:59 +000078static uint16_t sp_device_serbuf_size = 16;
79static uint16_t sp_device_opbuf_size = 300;
80/* Bitmap of supported commands */
81static uint8_t sp_cmdmap[32];
82
Uwe Hermann4e3d0b32010-03-25 23:18:41 +000083/* sp_prev_was_write used to detect writes with contiguous addresses
Urja Rannikkof3196df2009-07-21 13:02:59 +000084 and combine them to write-n's */
85static int sp_prev_was_write = 0;
86/* sp_write_n_addr used as the starting addr of the currently
87 combined write-n operation */
88static uint32_t sp_write_n_addr;
89/* The maximum length of an write_n operation; 0 = write-n not supported */
90static uint32_t sp_max_write_n = 0;
91/* The maximum length of a read_n operation; 0 = 2^24 */
92static 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. */
96static uint8_t *sp_write_n_buf;
97static uint32_t sp_write_n_bytes = 0;
98
99/* sp_streamed_* used for flow control checking */
Nico Huber519be662018-12-23 20:03:35 +0100100static unsigned int sp_streamed_transmit_ops = 0;
101static unsigned int sp_streamed_transmit_bytes = 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000102
103/* sp_opbuf_usage used for counting the amount of
104 on-device operation buffer used */
105static int sp_opbuf_usage = 0;
106/* if true causes sp_docommand to automatically check
107 whether the command is supported before doing it */
108static int sp_check_avail_automatic = 0;
109
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000110#if ! IS_WINDOWS
Urja Rannikkof3196df2009-07-21 13:02:59 +0000111static int sp_opensocket(char *ip, unsigned int port)
112{
113 int flag = 1;
114 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000115 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +0000116 int sock;
Sean Nelson74e8af52010-01-10 01:06:23 +0000117 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000118 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000119 if (sock < 0) {
120 msg_perr("Error: serprog cannot open socket: %s\n", strerror(errno));
121 return -1;
122 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000123 hostPtr = gethostbyname(ip);
124 if (NULL == hostPtr) {
125 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000126 if (NULL == hostPtr) {
Stefan Reinauerb8792872014-04-26 16:12:03 +0000127 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000128 msg_perr("Error: cannot resolve %s\n", ip);
129 return -1;
130 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000131 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000132 sp.si.sin_family = AF_INET;
133 sp.si.sin_port = htons(port);
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000134 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr_list[0], hostPtr->h_length);
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000135 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000136 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000137 msg_perr("Error: serprog cannot connect: %s\n", strerror(errno));
138 return -1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000139 }
140 /* We are latency limited, and sometimes do write-write-read *
141 * (write-n) - so enable TCP_NODELAY. */
Stefan Reinauer907c3eb2014-04-26 16:12:31 +0000142 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 Rannikkof3196df2009-07-21 13:02:59 +0000147 return sock;
148}
Stefan Tauner52b6e9d2013-04-01 00:46:05 +0000149#endif
Urja Rannikkof3196df2009-07-21 13:02:59 +0000150
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000151/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000152 * always succeeded in) bring the serial protocol to known waiting-for- *
Stefan Taunerae3d8372013-04-01 00:45:45 +0000153 * command state - uses nonblocking I/O - rest of the driver uses *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000154 * 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öderlund7145a502012-09-07 07:07:07 +0000157static int sp_synchronize(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000158{
159 int i;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000160 unsigned char buf[8];
Urja Rannikkof3196df2009-07-21 13:02:59 +0000161 /* 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 Taunerae3d8372013-04-01 00:45:45 +0000165 if (serialport_write_nonblock(buf, 8, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000166 goto err_out;
167 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000168 /* A second should be enough to get all the answers to the buffer */
Stefan Tauner79587f52013-04-01 00:45:51 +0000169 internal_delay(1000 * 1000);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000170 sp_flush_incoming();
171
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000172 /* 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 Rannikkof3196df2009-07-21 13:02:59 +0000176 for (i = 0; i < 8; i++) {
177 int n;
178 unsigned char c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000179 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000180 goto err_out;
181 }
Sean Nelson74e8af52010-01-10 01:06:23 +0000182 msg_pdbg(".");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000183 fflush(stdout);
184 for (n = 0; n < 10; n++) {
Stefan Tauner00e16082013-04-01 00:45:38 +0000185 int ret = serialport_read_nonblock(&c, 1, 50, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000186 if (ret < 0)
187 goto err_out;
188 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000189 continue;
Stefan Tauner00e16082013-04-01 00:45:38 +0000190 ret = serialport_read_nonblock(&c, 1, 20, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000191 if (ret < 0)
192 goto err_out;
193 if (ret > 0 || c != S_ACK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000194 continue;
195 c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000196 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
197 goto err_out;
Niklas Söderlund7145a502012-09-07 07:07:07 +0000198 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000199 ret = serialport_read_nonblock(&c, 1, 500, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000200 if (ret < 0)
201 goto err_out;
202 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000203 break; /* fail */
Stefan Tauner00e16082013-04-01 00:45:38 +0000204 ret = serialport_read_nonblock(&c, 1, 100, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000205 if (ret > 0 || ret < 0)
206 goto err_out;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000207 if (c != S_ACK)
208 break; /* fail */
Sean Nelson74e8af52010-01-10 01:06:23 +0000209 msg_pdbg("\n");
Niklas Söderlund7145a502012-09-07 07:07:07 +0000210 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000211 }
212 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000213err_out:
214 msg_perr("Error: cannot synchronize protocol - check communications and reset device?\n");
215 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000216}
217
218static 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
226static int sp_automatic_cmdcheck(uint8_t cmd)
227{
228 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000229 msg_pdbg("Warning: Automatic command availability check failed "
Stefan Taunere34e3e82013-01-01 00:06:51 +0000230 "for cmd 0x%02x - won't execute cmd\n", cmd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000231 return 1;
232 }
233 return 0;
234}
235
236static int sp_docommand(uint8_t command, uint32_t parmlen,
Stefan Tauner31019d42011-10-22 21:45:27 +0000237 uint8_t *params, uint32_t retlen, void *retparms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000238{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000239 unsigned char c;
240 if (sp_automatic_cmdcheck(command))
241 return 1;
Stefan Tauner79587f52013-04-01 00:45:51 +0000242 if (serialport_write(&command, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000243 msg_perr("Error: cannot write op code: %s\n", strerror(errno));
244 return 1;
245 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000246 if (serialport_write(params, parmlen) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000247 msg_perr("Error: cannot write parameters: %s\n", strerror(errno));
248 return 1;
249 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000250 if (serialport_read(&c, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000251 msg_perr("Error: cannot read from device: %s\n", strerror(errno));
252 return 1;
253 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000254 if (c == S_NAK)
255 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000256 if (c != S_ACK) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000257 msg_perr("Error: invalid response 0x%02X from device (to command 0x%02X)\n", c, command);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000258 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000259 }
260 if (retlen) {
Stefan Tauner79587f52013-04-01 00:45:51 +0000261 if (serialport_read(retparms, retlen) != 0) {
262 msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
263 return 1;
264 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000265 }
266 return 0;
267}
268
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000269static int sp_flush_stream(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000270{
271 if (sp_streamed_transmit_ops)
272 do {
273 unsigned char c;
Stefan Tauner79587f52013-04-01 00:45:51 +0000274 if (serialport_read(&c, 1) != 0) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000275 msg_perr("Error: cannot read from device (flushing stream)");
276 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000277 }
278 if (c == S_NAK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000279 msg_perr("Error: NAK to a stream buffer operation\n");
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000280 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000281 }
282 if (c != S_ACK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000283 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000284 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000285 }
286 } while (--sp_streamed_transmit_ops);
287 sp_streamed_transmit_ops = 0;
288 sp_streamed_transmit_bytes = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000289 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000290}
291
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000292static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t *parms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000293{
294 uint8_t *sp;
295 if (sp_automatic_cmdcheck(cmd))
296 return 1;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000297
Urja Rannikkof3196df2009-07-21 13:02:59 +0000298 sp = malloc(1 + parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000299 if (!sp) {
300 msg_perr("Error: cannot malloc command buffer\n");
301 return 1;
302 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000303 sp[0] = cmd;
aarya885917c2022-03-10 09:16:44 +0530304 if (parms)
305 memcpy(&(sp[1]), parms, parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000306
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 Rannikkof3196df2009-07-21 13:02:59 +0000318 sp_streamed_transmit_ops += 1;
319 sp_streamed_transmit_bytes += 1 + parmlen;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000320
321 free(sp);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000322 return 0;
323}
324
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000325static int serprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000326 unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000327 const unsigned char *writearr,
328 unsigned char *readarr);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000329static struct spi_master spi_master_serprog = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100330 .features = SPI_MASTER_4BA,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000331 .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 Rannikko731316a2017-06-15 13:32:01 +0300335 .read = default_spi_read,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000336 .write_256 = default_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530337 .probe_opcode = default_spi_probe_opcode,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000338};
339
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000340static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
341 chipaddr addr);
342static uint8_t serprog_chip_readb(const struct flashctx *flash,
343 const chipaddr addr);
344static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
345 const chipaddr addr, size_t len);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000346static const struct par_master par_master_serprog = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200347 .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 Hailfingereaacd2d2011-11-09 23:40:00 +0000355};
356
357static enum chipbustype serprog_buses_supported = BUS_NONE;
358
Thomas Heijligencc853d82021-05-04 15:32:17 +0200359static int serprog_init(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000360{
361 uint16_t iface;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000362 unsigned char pgmname[17];
363 unsigned char rbuf[3];
364 unsigned char c;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000365 char *device;
Felix Singer815eb792022-08-19 01:02:42 +0200366 bool have_device = false;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000367
Stefan Tauner72587f82016-01-04 03:05:15 +0000368 /* the parameter is either of format "dev=/dev/device[:baud]" or "ip=ip:port" */
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000369 device = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000370 if (device && strlen(device)) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000371 char *baud_str = strstr(device, ":");
372 if (baud_str != NULL) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000373 /* Split device from baudrate. */
Stefan Tauner72587f82016-01-04 03:05:15 +0000374 *baud_str = '\0';
375 baud_str++;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000376 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000377 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 Klimchukf02db802021-05-24 10:07:56 +1000384 } else {
Stefan Tauner72587f82016-01-04 03:05:15 +0000385 baud = atoi(baud_str); // FIXME: replace atoi with strtoul
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000386 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000387 if (strlen(device) > 0) {
388 sp_fd = sp_openserport(device, baud);
Stefan Tauneracfc4c62012-11-30 16:46:45 +0000389 if (sp_fd == SER_INV_FD) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000390 free(device);
391 return 1;
392 }
Felix Singer815eb792022-08-19 01:02:42 +0200393 have_device = true;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000394 }
395 }
Urja Rannikko27b431b2016-01-04 03:05:23 +0000396
397#if !IS_WINDOWS
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000398 if (device && !strlen(device)) {
399 msg_perr("Error: No device specified.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200400 "Use flashprog -p serprog:dev=/dev/device[:baud]\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000401 free(device);
402 return 1;
403 }
404 free(device);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000405
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000406 device = extract_programmer_param("ip");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000407 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 Tauner72587f82016-01-04 03:05:15 +0000414 char *port = strstr(device, ":");
415 if (port != NULL) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000416 /* Split host from port. */
Stefan Tauner72587f82016-01-04 03:05:15 +0000417 *port = '\0';
418 port++;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000419 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000420 if (!port || !strlen(port)) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000421 msg_perr("Error: No port specified.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200422 "Use flashprog -p serprog:ip=ipaddr:port\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000423 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000424 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000425 }
426 if (strlen(device)) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000427 sp_fd = sp_opensocket(device, atoi(port)); // FIXME: replace atoi with strtoul
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000428 if (sp_fd < 0) {
429 free(device);
430 return 1;
431 }
Felix Singer815eb792022-08-19 01:02:42 +0200432 have_device = true;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000433 }
434 }
435 if (device && !strlen(device)) {
436 msg_perr("Error: No host specified.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200437 "Use flashprog -p serprog:ip=ipaddr:port\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000438 free(device);
439 return 1;
440 }
Urja Rannikko27b431b2016-01-04 03:05:23 +0000441#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000442 free(device);
443
444 if (!have_device) {
Urja Rannikko27b431b2016-01-04 03:05:23 +0000445#if IS_WINDOWS
446 msg_perr("Error: No device specified.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200447 "Use flashprog -p serprog:dev=comN[:baud]\n");
Urja Rannikko27b431b2016-01-04 03:05:23 +0000448#else
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000449 msg_perr("Error: Neither host nor device specified.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200450 "Use flashprog -p serprog:dev=/dev/device:baud or "
451 "flashprog -p serprog:ip=ipaddr:port\n");
Urja Rannikko27b431b2016-01-04 03:05:23 +0000452#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000453 return 1;
454 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000455
Sean Nelson74e8af52010-01-10 01:06:23 +0000456 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000457
458 sp_check_avail_automatic = 0;
459
Niklas Söderlund7145a502012-09-07 07:07:07 +0000460 if (sp_synchronize())
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000461 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000462
Sean Nelson74e8af52010-01-10 01:06:23 +0000463 msg_pdbg(MSGHEADER "Synchronized\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000464
465 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000466 msg_perr("Error: NAK to query interface version\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000467 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000468 }
469
470 if (iface != 1) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000471 msg_perr("Error: Unknown interface version: %d\n", iface);
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000472 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000473 }
474
Sean Nelson74e8af52010-01-10 01:06:23 +0000475 msg_pdbg(MSGHEADER "Interface version ok.\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000476
477 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000478 msg_perr("Error: query command map not supported\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000479 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000480 }
481
482 sp_check_avail_automatic = 1;
483
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000484 /* FIXME: This assumes that serprog device bustypes are always
Nico Huberc3b02dc2023-08-12 01:13:45 +0200485 * identical with flashprog bustype enums and that they all fit
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000486 * in a single byte.
487 */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000488 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000489 msg_pwarn("Warning: NAK to query supported buses\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000490 c = BUS_NONSPI; /* A reasonable default for now. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000491 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000492 serprog_buses_supported = c;
493
Stefan Tauner31019d42011-10-22 21:45:27 +0000494 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 Rannikkoc93f5f12011-09-15 23:38:14 +0000499 /* Check for the minimum operational set of commands. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000500 if (serprog_buses_supported & BUS_SPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000501 uint8_t bt = BUS_SPI;
Riku Viitanenddb6d922024-01-15 19:15:49 +0200502 char *spispeed, *cs;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000503 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 Klimchukc6ff17b2021-05-24 09:55:03 +1000506 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000507 }
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000508 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000509 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000510 /* 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 Rannikkoc93f5f12011-09-15 23:38:14 +0000514 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 Hailfingera5bcbce2014-07-19 22:03:29 +0000521 spi_master_serprog.max_data_write = v;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000522 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 Hailfingera5bcbce2014-07-19 22:03:29 +0000531 spi_master_serprog.max_data_read = v;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000532 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
533 }
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000534 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 Taunere34e3e82013-01-01 00:06:51 +0000544 free(spispeed);
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000545 goto init_err_cleanup_exit;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000546 }
547 if (strlen(f_spi_suffix) == 1) {
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000548 if (!strcasecmp(f_spi_suffix, "M")) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000549 f_spi_req *= 1000000;
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000550 } else if (!strcasecmp(f_spi_suffix, "k")) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000551 f_spi_req *= 1000;
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000552 } else {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000553 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000554 free(spispeed);
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000555 goto init_err_cleanup_exit;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000556 }
557 } else if (strlen(f_spi_suffix) > 1) {
558 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000559 free(spispeed);
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000560 goto init_err_cleanup_exit;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000561 }
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 Klimchukf02db802021-05-24 10:07:56 +1000568 if (sp_check_commandavail(S_CMD_S_SPI_FREQ) == 0) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000569 msg_pwarn(MSGHEADER "Warning: Setting the SPI clock rate is not supported!\n");
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000570 } else if (sp_docommand(S_CMD_S_SPI_FREQ, 4, buf, 4, buf) == 0) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000571 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 Klimchukf02db802021-05-24 10:07:56 +1000577 } else {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000578 msg_pwarn(MSGHEADER "Setting SPI clock rate to %u Hz failed!\n", f_spi_req);
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000579 }
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000580 }
581 free(spispeed);
Riku Viitanenddb6d922024-01-15 19:15:49 +0200582 cs = extract_programmer_param("cs");
583 if (cs) {
584 char *endptr = NULL;
585 errno = 0;
586 unsigned long cs_num = strtoul(cs, &endptr, 0);
587 if (!*cs || errno || *endptr || cs_num > 255) {
588 msg_perr("Error: Invalid chip select requested! "
589 "Only 0-255 are valid.\n");
590 free(cs);
591 goto init_err_cleanup_exit;
592 }
593 free(cs);
594 if (!sp_check_commandavail(S_CMD_S_SPI_CS)) {
595 msg_perr("Error: Setting SPI chip select is not supported!\n");
596 goto init_err_cleanup_exit;
597 }
598 msg_pdbg(MSGHEADER "Requested to use chip select %lu.\n", cs_num);
599 uint8_t cs_num8 = cs_num;
600 if (sp_docommand(S_CMD_S_SPI_CS, 1, &cs_num8, 0, NULL)) {
601 msg_perr("Error: Chip select %u not supported "
602 "by programmer!\n", cs_num8);
603 goto init_err_cleanup_exit;
604 }
605 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000606 bt = serprog_buses_supported;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000607 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000608 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000609 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000610
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000611 if (serprog_buses_supported & BUS_NONSPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000612 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
613 msg_perr("Error: Initialize operation buffer "
614 "not supported\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000615 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000616 }
617
618 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
619 msg_perr("Error: Write to opbuf: "
620 "delay not supported\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000621 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000622 }
623
624 /* S_CMD_O_EXEC availability checked later. */
625
626 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
627 msg_perr("Error: Single byte read not supported\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000628 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000629 }
630 /* This could be translated to single byte reads (if missing),
631 * but now we don't support that. */
632 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
633 msg_perr("Error: Read n bytes not supported\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000634 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000635 }
636 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
637 msg_perr("Error: Write to opbuf: "
638 "write byte not supported\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000639 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000640 }
641
642 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
643 msg_pdbg(MSGHEADER "Write-n not supported");
644 sp_max_write_n = 0;
645 } else {
646 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
647 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
648 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
649 if (!sp_max_write_n) {
650 sp_max_write_n = (1 << 24);
651 }
652 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
653 sp_max_write_n);
654 sp_write_n_buf = malloc(sp_max_write_n);
655 if (!sp_write_n_buf) {
656 msg_perr("Error: cannot allocate memory for "
657 "Write-n buffer\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000658 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000659 }
660 sp_write_n_bytes = 0;
661 }
662
663 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
664 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
665 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
666 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
667 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
668 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
669 sp_max_read_n ? sp_max_read_n : (1 << 24));
670 } else {
671 msg_pdbg(MSGHEADER "Maximum read-n length "
672 "not reported\n");
673 sp_max_read_n = 0;
674 }
675
Urja Rannikkof3196df2009-07-21 13:02:59 +0000676 }
677
678 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000679 msg_pwarn("Warning: NAK to query programmer name\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000680 strcpy((char *)pgmname, "(unknown)");
681 }
682 pgmname[16] = 0;
Stefan Tauner31019d42011-10-22 21:45:27 +0000683 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000684
685 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000686 msg_pwarn("Warning: NAK to query serial buffer size\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000687 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000688 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000689 sp_device_serbuf_size);
690
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000691 if (sp_check_commandavail(S_CMD_O_INIT)) {
692 /* This would be inconsistent. */
693 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
694 msg_perr("Error: Execute operation buffer not "
695 "supported\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000696 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000697 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000698
699 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
700 msg_perr("Error: NAK to initialize operation buffer\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000701 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000702 }
703
704 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
705 &sp_device_opbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000706 msg_pwarn("Warning: NAK to query operation buffer size\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000707 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000708 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000709 sp_device_opbuf_size);
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200710 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000711
Stefan Tauner92fefc92012-10-27 00:34:23 +0000712 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
713 uint8_t en = 1;
714 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &en, 0, NULL) != 0) {
715 msg_perr("Error: could not enable output buffers\n");
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000716 goto init_err_cleanup_exit;
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000717 } else {
Stefan Tauner92fefc92012-10-27 00:34:23 +0000718 msg_pdbg(MSGHEADER "Output drivers enabled\n");
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000719 }
720 } else {
Stefan Tauner92fefc92012-10-27 00:34:23 +0000721 msg_pdbg(MSGHEADER "Warning: Programmer does not support toggling its output drivers\n");
Anastasia Klimchukf02db802021-05-24 10:07:56 +1000722 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000723 sp_prev_was_write = 0;
724 sp_streamed_transmit_ops = 0;
725 sp_streamed_transmit_bytes = 0;
726 sp_opbuf_usage = 0;
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000727
728 if (register_shutdown(serprog_shutdown, NULL))
729 goto init_err_cleanup_exit;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000730 if (serprog_buses_supported & BUS_SPI)
Nico Huber5e08e3e2021-05-11 17:38:14 +0200731 register_spi_master(&spi_master_serprog, NULL);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000732 if (serprog_buses_supported & BUS_NONSPI)
Anastasia Klimchukb91a2032021-05-21 09:40:58 +1000733 register_par_master(&par_master_serprog, serprog_buses_supported & BUS_NONSPI, NULL);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000734 return 0;
Anastasia Klimchukc6ff17b2021-05-24 09:55:03 +1000735
736init_err_cleanup_exit:
737 serprog_shutdown(NULL);
738 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000739}
740
Nico Huberc3b02dc2023-08-12 01:13:45 +0200741/* Move an in flashprog buffer existing write-n operation to the on-device operation buffer. */
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000742static int sp_pass_writen(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000743{
744 unsigned char header[7];
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000745 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", sp_write_n_bytes, sp_write_n_addr);
746 if (sp_streamed_transmit_bytes >= (7 + sp_write_n_bytes + sp_device_serbuf_size)) {
747 if (sp_flush_stream() != 0) {
748 return 1;
749 }
750 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000751 /* In case it's just a single byte send it as a single write. */
752 if (sp_write_n_bytes == 1) {
753 sp_write_n_bytes = 0;
754 header[0] = (sp_write_n_addr >> 0) & 0xFF;
755 header[1] = (sp_write_n_addr >> 8) & 0xFF;
756 header[2] = (sp_write_n_addr >> 16) & 0xFF;
757 header[3] = sp_write_n_buf[0];
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000758 if (sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header) != 0)
759 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000760 sp_opbuf_usage += 5;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000761 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000762 }
763 header[0] = S_CMD_O_WRITEN;
764 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
765 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
766 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
767 header[4] = (sp_write_n_addr >> 0) & 0xFF;
768 header[5] = (sp_write_n_addr >> 8) & 0xFF;
769 header[6] = (sp_write_n_addr >> 16) & 0xFF;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000770 if (serialport_write(header, 7) != 0) {
771 msg_perr(MSGHEADER "Error: cannot write write-n command\n");
772 return 1;
773 }
774 if (serialport_write(sp_write_n_buf, sp_write_n_bytes) != 0) {
775 msg_perr(MSGHEADER "Error: cannot write write-n data");
776 return 1;
777 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000778 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
779 sp_streamed_transmit_ops += 1;
780 sp_opbuf_usage += 7 + sp_write_n_bytes;
781 sp_write_n_bytes = 0;
782 sp_prev_was_write = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000783 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000784}
785
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000786static int sp_execute_opbuf_noflush(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000787{
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000788 if ((sp_max_write_n) && (sp_write_n_bytes)) {
789 if (sp_pass_writen() != 0) {
790 msg_perr("Error: could not transfer write buffer\n");
791 return 1;
792 }
793 }
794 if (sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL) != 0) {
795 msg_perr("Error: could not execute command buffer\n");
796 return 1;
797 }
798 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", sp_opbuf_usage);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000799 sp_opbuf_usage = 0;
800 sp_prev_was_write = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000801 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000802}
803
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000804static int sp_execute_opbuf(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000805{
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000806 if (sp_execute_opbuf_noflush() != 0)
807 return 1;
808 if (sp_flush_stream() != 0)
809 return 1;
810
811 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000812}
813
David Hendricks8bb20212011-06-14 01:35:36 +0000814static int serprog_shutdown(void *data)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000815{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000816 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000817 if (sp_execute_opbuf() != 0)
818 msg_pwarn("Could not flush command buffer.\n");
Stefan Tauner92fefc92012-10-27 00:34:23 +0000819 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
820 uint8_t dis = 0;
821 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &dis, 0, NULL) == 0)
822 msg_pdbg(MSGHEADER "Output drivers disabled\n");
823 else
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000824 msg_pwarn(MSGHEADER "%s: Warning: could not disable output buffers\n", __func__);
Stefan Tauner92fefc92012-10-27 00:34:23 +0000825 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000826 /* FIXME: fix sockets on windows(?), especially closing */
827 serialport_shutdown(&sp_fd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000828 if (sp_max_write_n)
829 free(sp_write_n_buf);
830 return 0;
831}
832
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000833static int sp_check_opbuf_usage(int bytes_to_be_added)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000834{
835 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000836 /* If this happens in the middle of a page load the page load will probably fail. */
837 msg_pwarn(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
838 if (sp_execute_opbuf() != 0)
839 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000840 }
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000841 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000842}
843
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000844static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
845 chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000846{
Sean Nelson74e8af52010-01-10 01:06:23 +0000847 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000848 if (sp_max_write_n) {
849 if ((sp_prev_was_write)
850 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
851 sp_write_n_buf[sp_write_n_bytes++] = val;
852 } else {
853 if ((sp_prev_was_write) && (sp_write_n_bytes))
854 sp_pass_writen();
855 sp_prev_was_write = 1;
856 sp_write_n_addr = addr;
857 sp_write_n_bytes = 1;
858 sp_write_n_buf[0] = val;
859 }
860 sp_check_opbuf_usage(7 + sp_write_n_bytes);
861 if (sp_write_n_bytes >= sp_max_write_n)
862 sp_pass_writen();
863 } else {
864 /* We will have to do single writeb ops. */
865 unsigned char writeb_parm[4];
866 sp_check_opbuf_usage(6);
867 writeb_parm[0] = (addr >> 0) & 0xFF;
868 writeb_parm[1] = (addr >> 8) & 0xFF;
869 writeb_parm[2] = (addr >> 16) & 0xFF;
870 writeb_parm[3] = val;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000871 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000872 sp_opbuf_usage += 5;
873 }
874}
875
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000876static uint8_t serprog_chip_readb(const struct flashctx *flash,
877 const chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000878{
879 unsigned char c;
880 unsigned char buf[3];
881 /* Will stream the read operation - eg. add it to the stream buffer, *
882 * then flush the buffer, then read the read answer. */
883 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
884 sp_execute_opbuf_noflush();
885 buf[0] = ((addr >> 0) & 0xFF);
886 buf[1] = ((addr >> 8) & 0xFF);
887 buf[2] = ((addr >> 16) & 0xFF);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000888 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); // FIXME: return error
889 sp_flush_stream(); // FIXME: return error
Stefan Tauner79587f52013-04-01 00:45:51 +0000890 if (serialport_read(&c, 1) != 0)
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000891 msg_perr(MSGHEADER "readb byteread"); // FIXME: return error
Stefan Taunerc2333752013-07-13 23:31:37 +0000892 msg_pspew("%s addr=0x%" PRIxPTR " returning 0x%02X\n", __func__, addr, c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000893 return c;
894}
895
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000896/* Local version that really does the job, doesn't care of max_read_n. */
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000897static int sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000898{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000899 unsigned char sbuf[6];
Stefan Tauner0554ca52013-07-25 22:54:25 +0000900 msg_pspew("%s: addr=0x%" PRIxPTR " len=%zu\n", __func__, addr, len);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000901 /* Stream the read-n -- as above. */
902 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
903 sp_execute_opbuf_noflush();
904 sbuf[0] = ((addr >> 0) & 0xFF);
905 sbuf[1] = ((addr >> 8) & 0xFF);
906 sbuf[2] = ((addr >> 16) & 0xFF);
907 sbuf[3] = ((len >> 0) & 0xFF);
908 sbuf[4] = ((len >> 8) & 0xFF);
909 sbuf[5] = ((len >> 16) & 0xFF);
910 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000911 if (sp_flush_stream() != 0)
912 return 1;
913 if (serialport_read(buf, len) != 0) {
914 msg_perr(MSGHEADER "Error: cannot read read-n data");
915 return 1;
916 }
917 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000918}
919
920/* The externally called version that makes sure that max_read_n is obeyed. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000921static void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf,
922 const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000923{
924 size_t lenm = len;
925 chipaddr addrm = addr;
Stefan Tauner31019d42011-10-22 21:45:27 +0000926 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000927 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000928 addrm += sp_max_read_n;
929 lenm -= sp_max_read_n;
930 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000931 if (lenm)
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000932 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000933}
934
Thomas Heijligencc853d82021-05-04 15:32:17 +0200935static void serprog_delay(unsigned int usecs)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000936{
937 unsigned char buf[4];
Stefan Tauner31019d42011-10-22 21:45:27 +0000938 msg_pspew("%s usecs=%d\n", __func__, usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000939 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
Stefan Taunerd7d423b2012-10-20 09:13:16 +0000940 msg_pdbg2("serprog_delay used, but programmer doesn't support delays natively - emulating\n");
Stefan Tauner31019d42011-10-22 21:45:27 +0000941 internal_delay(usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000942 return;
943 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000944 if ((sp_max_write_n) && (sp_write_n_bytes))
945 sp_pass_writen();
946 sp_check_opbuf_usage(5);
Stefan Tauner31019d42011-10-22 21:45:27 +0000947 buf[0] = ((usecs >> 0) & 0xFF);
948 buf[1] = ((usecs >> 8) & 0xFF);
949 buf[2] = ((usecs >> 16) & 0xFF);
950 buf[3] = ((usecs >> 24) & 0xFF);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000951 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
952 sp_opbuf_usage += 5;
953 sp_prev_was_write = 0;
954}
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000955
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000956static int serprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000957 unsigned int writecnt, unsigned int readcnt,
958 const unsigned char *writearr,
959 unsigned char *readarr)
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000960{
961 unsigned char *parmbuf;
962 int ret;
963 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000964 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) {
965 if (sp_execute_opbuf() != 0) {
966 msg_perr("Error: could not execute command buffer before sending SPI commands.\n");
967 return 1;
968 }
969 }
970
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000971 parmbuf = malloc(writecnt + 6);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000972 if (!parmbuf) {
973 msg_perr("Error: could not allocate SPI send param buffer.\n");
974 return 1;
975 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000976 parmbuf[0] = (writecnt >> 0) & 0xFF;
977 parmbuf[1] = (writecnt >> 8) & 0xFF;
978 parmbuf[2] = (writecnt >> 16) & 0xFF;
979 parmbuf[3] = (readcnt >> 0) & 0xFF;
980 parmbuf[4] = (readcnt >> 8) & 0xFF;
981 parmbuf[5] = (readcnt >> 16) & 0xFF;
982 memcpy(parmbuf + 6, writearr, writecnt);
983 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
984 readarr);
985 free(parmbuf);
986 return ret;
987}
988
Thomas Heijligencc853d82021-05-04 15:32:17 +0200989static void *serprog_map(const char *descr, uintptr_t phys_addr, size_t len)
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000990{
991 /* Serprog transmits 24 bits only and assumes the underlying implementation handles any remaining bits
992 * correctly (usually setting them to one either in software (for FWH/LPC) or relying on the fact that
993 * the hardware observes a subset of the address bits only). Combined with the standard mapping of
Nico Huberc3b02dc2023-08-12 01:13:45 +0200994 * flashprog this creates a 16 MB-wide window just below the 4 GB boundary where serprog can operate (as
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000995 * needed for non-SPI chips). Below we make sure that the requested range is within this window. */
996 if ((phys_addr & 0xFF000000) == 0xFF000000) {
997 return (void*)phys_addr;
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000998 }
Elyes HAOUASec819d62019-06-09 17:50:51 +0200999 msg_pwarn(MSGHEADER "requested mapping %s is incompatible: 0x%zx bytes at 0x%0*" PRIxPTR ".\n",
1000 descr, len, PRIxPTR_WIDTH, phys_addr);
1001 return NULL;
Urja Rannikko0b4ffd52015-06-29 23:24:23 +00001002}
Thomas Heijligencc853d82021-05-04 15:32:17 +02001003
1004const struct programmer_entry programmer_serprog = {
1005 .name = "serprog",
1006 .type = OTHER,
1007 /* FIXME */
1008 .devs.note = "All programmer devices speaking the serprog protocol\n",
1009 .init = serprog_init,
1010 .map_flash_region = serprog_map,
Thomas Heijligencc853d82021-05-04 15:32:17 +02001011 .delay = serprog_delay,
1012};