blob: e8c0d75a0ed2fd223285c04e07937544fe730d97 [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
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Urja Rannikko22915352009-06-23 11:33:43 +000016 */
17
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000018#include "platform.h"
19
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"
Stefan Tauner0d82e952012-10-27 00:34:18 +000041#include "serprog.h"
Urja Rannikkof3196df2009-07-21 13:02:59 +000042
Stefan Tauner31019d42011-10-22 21:45:27 +000043#define MSGHEADER "serprog: "
Urja Rannikkof3196df2009-07-21 13:02:59 +000044
Urja Rannikkof3196df2009-07-21 13:02:59 +000045static uint16_t sp_device_serbuf_size = 16;
46static uint16_t sp_device_opbuf_size = 300;
47/* Bitmap of supported commands */
48static uint8_t sp_cmdmap[32];
49
Uwe Hermann4e3d0b32010-03-25 23:18:41 +000050/* sp_prev_was_write used to detect writes with contiguous addresses
Urja Rannikkof3196df2009-07-21 13:02:59 +000051 and combine them to write-n's */
52static int sp_prev_was_write = 0;
53/* sp_write_n_addr used as the starting addr of the currently
54 combined write-n operation */
55static uint32_t sp_write_n_addr;
56/* The maximum length of an write_n operation; 0 = write-n not supported */
57static uint32_t sp_max_write_n = 0;
58/* The maximum length of a read_n operation; 0 = 2^24 */
59static uint32_t sp_max_read_n = 0;
60
61/* A malloc'd buffer for combining the operation's data
62 and a counter that tells how much data is there. */
63static uint8_t *sp_write_n_buf;
64static uint32_t sp_write_n_bytes = 0;
65
66/* sp_streamed_* used for flow control checking */
Nico Huber519be662018-12-23 20:03:35 +010067static unsigned int sp_streamed_transmit_ops = 0;
68static unsigned int sp_streamed_transmit_bytes = 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +000069
70/* sp_opbuf_usage used for counting the amount of
71 on-device operation buffer used */
72static int sp_opbuf_usage = 0;
73/* if true causes sp_docommand to automatically check
74 whether the command is supported before doing it */
75static int sp_check_avail_automatic = 0;
76
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000077#if ! IS_WINDOWS
Urja Rannikkof3196df2009-07-21 13:02:59 +000078static int sp_opensocket(char *ip, unsigned int port)
79{
80 int flag = 1;
81 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +000082 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +000083 int sock;
Sean Nelson74e8af52010-01-10 01:06:23 +000084 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
Urja Rannikkof3196df2009-07-21 13:02:59 +000085 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Niklas Söderlund2a95e872012-07-30 19:42:33 +000086 if (sock < 0) {
87 msg_perr("Error: serprog cannot open socket: %s\n", strerror(errno));
88 return -1;
89 }
Urja Rannikkof3196df2009-07-21 13:02:59 +000090 hostPtr = gethostbyname(ip);
91 if (NULL == hostPtr) {
92 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
Niklas Söderlund2a95e872012-07-30 19:42:33 +000093 if (NULL == hostPtr) {
Stefan Reinauerb8792872014-04-26 16:12:03 +000094 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +000095 msg_perr("Error: cannot resolve %s\n", ip);
96 return -1;
97 }
Urja Rannikkof3196df2009-07-21 13:02:59 +000098 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +000099 sp.si.sin_family = AF_INET;
100 sp.si.sin_port = htons(port);
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000101 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr_list[0], hostPtr->h_length);
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000102 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000103 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000104 msg_perr("Error: serprog cannot connect: %s\n", strerror(errno));
105 return -1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000106 }
107 /* We are latency limited, and sometimes do write-write-read *
108 * (write-n) - so enable TCP_NODELAY. */
Stefan Reinauer907c3eb2014-04-26 16:12:31 +0000109 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int))) {
110 close(sock);
111 msg_perr("Error: serprog cannot set socket options: %s\n", strerror(errno));
112 return -1;
113 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000114 return sock;
115}
Stefan Tauner52b6e9d2013-04-01 00:46:05 +0000116#endif
Urja Rannikkof3196df2009-07-21 13:02:59 +0000117
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000118/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000119 * always succeeded in) bring the serial protocol to known waiting-for- *
Stefan Taunerae3d8372013-04-01 00:45:45 +0000120 * command state - uses nonblocking I/O - rest of the driver uses *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000121 * blocking read - TODO: add an alarm() timer for the rest of the app on *
122 * serial operations, though not such a big issue as the first thing to *
123 * do is synchronize (eg. check that device is alive). */
Niklas Söderlund7145a502012-09-07 07:07:07 +0000124static int sp_synchronize(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000125{
126 int i;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000127 unsigned char buf[8];
Urja Rannikkof3196df2009-07-21 13:02:59 +0000128 /* First sends 8 NOPs, then flushes the return data - should cause *
129 * the device serial parser to get to a sane state, unless if it *
130 * is waiting for a real long write-n. */
131 memset(buf, S_CMD_NOP, 8);
Stefan Taunerae3d8372013-04-01 00:45:45 +0000132 if (serialport_write_nonblock(buf, 8, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000133 goto err_out;
134 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000135 /* A second should be enough to get all the answers to the buffer */
Stefan Tauner79587f52013-04-01 00:45:51 +0000136 internal_delay(1000 * 1000);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000137 sp_flush_incoming();
138
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000139 /* Then try up to 8 times to send syncnop and get the correct special *
140 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
141 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
142 * attempt, ~1s if immediate success. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000143 for (i = 0; i < 8; i++) {
144 int n;
145 unsigned char c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000146 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000147 goto err_out;
148 }
Sean Nelson74e8af52010-01-10 01:06:23 +0000149 msg_pdbg(".");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000150 fflush(stdout);
151 for (n = 0; n < 10; n++) {
Stefan Tauner00e16082013-04-01 00:45:38 +0000152 int ret = serialport_read_nonblock(&c, 1, 50, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000153 if (ret < 0)
154 goto err_out;
155 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000156 continue;
Stefan Tauner00e16082013-04-01 00:45:38 +0000157 ret = serialport_read_nonblock(&c, 1, 20, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000158 if (ret < 0)
159 goto err_out;
160 if (ret > 0 || c != S_ACK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000161 continue;
162 c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000163 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
164 goto err_out;
Niklas Söderlund7145a502012-09-07 07:07:07 +0000165 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000166 ret = serialport_read_nonblock(&c, 1, 500, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000167 if (ret < 0)
168 goto err_out;
169 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000170 break; /* fail */
Stefan Tauner00e16082013-04-01 00:45:38 +0000171 ret = serialport_read_nonblock(&c, 1, 100, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000172 if (ret > 0 || ret < 0)
173 goto err_out;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000174 if (c != S_ACK)
175 break; /* fail */
Sean Nelson74e8af52010-01-10 01:06:23 +0000176 msg_pdbg("\n");
Niklas Söderlund7145a502012-09-07 07:07:07 +0000177 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000178 }
179 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000180err_out:
181 msg_perr("Error: cannot synchronize protocol - check communications and reset device?\n");
182 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000183}
184
185static int sp_check_commandavail(uint8_t command)
186{
187 int byteoffs, bitoffs;
188 byteoffs = command / 8;
189 bitoffs = command % 8;
190 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
191}
192
193static int sp_automatic_cmdcheck(uint8_t cmd)
194{
195 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000196 msg_pdbg("Warning: Automatic command availability check failed "
Stefan Taunere34e3e82013-01-01 00:06:51 +0000197 "for cmd 0x%02x - won't execute cmd\n", cmd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000198 return 1;
199 }
200 return 0;
201}
202
203static int sp_docommand(uint8_t command, uint32_t parmlen,
Stefan Tauner31019d42011-10-22 21:45:27 +0000204 uint8_t *params, uint32_t retlen, void *retparms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000205{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000206 unsigned char c;
207 if (sp_automatic_cmdcheck(command))
208 return 1;
Stefan Tauner79587f52013-04-01 00:45:51 +0000209 if (serialport_write(&command, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000210 msg_perr("Error: cannot write op code: %s\n", strerror(errno));
211 return 1;
212 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000213 if (serialport_write(params, parmlen) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000214 msg_perr("Error: cannot write parameters: %s\n", strerror(errno));
215 return 1;
216 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000217 if (serialport_read(&c, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000218 msg_perr("Error: cannot read from device: %s\n", strerror(errno));
219 return 1;
220 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000221 if (c == S_NAK)
222 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000223 if (c != S_ACK) {
Stefan Tauner23e10b82016-01-23 16:16:49 +0000224 msg_perr("Error: invalid response 0x%02X from device (to command 0x%02X)\n", c, command);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000225 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000226 }
227 if (retlen) {
Stefan Tauner79587f52013-04-01 00:45:51 +0000228 if (serialport_read(retparms, retlen) != 0) {
229 msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
230 return 1;
231 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000232 }
233 return 0;
234}
235
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000236static int sp_flush_stream(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000237{
238 if (sp_streamed_transmit_ops)
239 do {
240 unsigned char c;
Stefan Tauner79587f52013-04-01 00:45:51 +0000241 if (serialport_read(&c, 1) != 0) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000242 msg_perr("Error: cannot read from device (flushing stream)");
243 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000244 }
245 if (c == S_NAK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000246 msg_perr("Error: NAK to a stream buffer operation\n");
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000247 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000248 }
249 if (c != S_ACK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000250 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000251 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000252 }
253 } while (--sp_streamed_transmit_ops);
254 sp_streamed_transmit_ops = 0;
255 sp_streamed_transmit_bytes = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000256 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000257}
258
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000259static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t *parms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000260{
261 uint8_t *sp;
262 if (sp_automatic_cmdcheck(cmd))
263 return 1;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000264
Urja Rannikkof3196df2009-07-21 13:02:59 +0000265 sp = malloc(1 + parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000266 if (!sp) {
267 msg_perr("Error: cannot malloc command buffer\n");
268 return 1;
269 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000270 sp[0] = cmd;
271 memcpy(&(sp[1]), parms, parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000272
273 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size)) {
274 if (sp_flush_stream() != 0) {
275 free(sp);
276 return 1;
277 }
278 }
279 if (serialport_write(sp, 1 + parmlen) != 0) {
280 msg_perr("Error: cannot write command\n");
281 free(sp);
282 return 1;
283 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000284 sp_streamed_transmit_ops += 1;
285 sp_streamed_transmit_bytes += 1 + parmlen;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000286
287 free(sp);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000288 return 0;
289}
290
Anastasia Klimchukd784d482021-02-15 15:02:00 +1100291/* Move an in flashrom buffer existing write-n operation to the on-device operation buffer. */
292static int sp_pass_writen(void)
293{
294 unsigned char header[7];
295 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", sp_write_n_bytes, sp_write_n_addr);
296 if (sp_streamed_transmit_bytes >= (7 + sp_write_n_bytes + sp_device_serbuf_size)) {
297 if (sp_flush_stream() != 0) {
298 return 1;
299 }
300 }
301 /* In case it's just a single byte send it as a single write. */
302 if (sp_write_n_bytes == 1) {
303 sp_write_n_bytes = 0;
304 header[0] = (sp_write_n_addr >> 0) & 0xFF;
305 header[1] = (sp_write_n_addr >> 8) & 0xFF;
306 header[2] = (sp_write_n_addr >> 16) & 0xFF;
307 header[3] = sp_write_n_buf[0];
308 if (sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header) != 0)
309 return 1;
310 sp_opbuf_usage += 5;
311 return 0;
312 }
313 header[0] = S_CMD_O_WRITEN;
314 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
315 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
316 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
317 header[4] = (sp_write_n_addr >> 0) & 0xFF;
318 header[5] = (sp_write_n_addr >> 8) & 0xFF;
319 header[6] = (sp_write_n_addr >> 16) & 0xFF;
320 if (serialport_write(header, 7) != 0) {
321 msg_perr(MSGHEADER "Error: cannot write write-n command\n");
322 return 1;
323 }
324 if (serialport_write(sp_write_n_buf, sp_write_n_bytes) != 0) {
325 msg_perr(MSGHEADER "Error: cannot write write-n data");
326 return 1;
327 }
328 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
329 sp_streamed_transmit_ops += 1;
330 sp_opbuf_usage += 7 + sp_write_n_bytes;
331 sp_write_n_bytes = 0;
332 sp_prev_was_write = 0;
333 return 0;
334}
335
336static int sp_execute_opbuf_noflush(void)
337{
338 if ((sp_max_write_n) && (sp_write_n_bytes)) {
339 if (sp_pass_writen() != 0) {
340 msg_perr("Error: could not transfer write buffer\n");
341 return 1;
342 }
343 }
344 if (sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL) != 0) {
345 msg_perr("Error: could not execute command buffer\n");
346 return 1;
347 }
348 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", sp_opbuf_usage);
349 sp_opbuf_usage = 0;
350 sp_prev_was_write = 0;
351 return 0;
352}
353
354static int sp_execute_opbuf(void)
355{
356 if (sp_execute_opbuf_noflush() != 0)
357 return 1;
358 if (sp_flush_stream() != 0)
359 return 1;
360
361 return 0;
362}
363
Edward O'Callaghane4ddc362020-04-12 17:27:53 +1000364static int serprog_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000365 unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000366 const unsigned char *writearr,
Anastasia Klimchukd784d482021-02-15 15:02:00 +1100367 unsigned char *readarr)
368{
369 unsigned char *parmbuf;
370 int ret;
371 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
372 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) {
373 if (sp_execute_opbuf() != 0) {
374 msg_perr("Error: could not execute command buffer before sending SPI commands.\n");
375 return 1;
376 }
377 }
378
379 parmbuf = malloc(writecnt + 6);
380 if (!parmbuf) {
381 msg_perr("Error: could not allocate SPI send param buffer.\n");
382 return 1;
383 }
384 parmbuf[0] = (writecnt >> 0) & 0xFF;
385 parmbuf[1] = (writecnt >> 8) & 0xFF;
386 parmbuf[2] = (writecnt >> 16) & 0xFF;
387 parmbuf[3] = (readcnt >> 0) & 0xFF;
388 parmbuf[4] = (readcnt >> 8) & 0xFF;
389 parmbuf[5] = (readcnt >> 16) & 0xFF;
390 memcpy(parmbuf + 6, writearr, writecnt);
391 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
392 readarr);
393 free(parmbuf);
394 return ret;
395}
396
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000397static struct spi_master spi_master_serprog = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100398 .features = SPI_MASTER_4BA,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000399 .max_data_read = MAX_DATA_READ_UNLIMITED,
400 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
401 .command = serprog_spi_send_command,
402 .multicommand = default_spi_send_multicommand,
Urja Rannikko731316a2017-06-15 13:32:01 +0300403 .read = default_spi_read,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000404 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000405 .write_aai = default_spi_write_aai,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000406};
407
Anastasia Klimchukd784d482021-02-15 15:02:00 +1100408static int sp_check_opbuf_usage(int bytes_to_be_added)
409{
410 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
411 /* If this happens in the middle of a page load the page load will probably fail. */
412 msg_pwarn(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
413 if (sp_execute_opbuf() != 0)
414 return 1;
415 }
416 return 0;
417}
418
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000419static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
Anastasia Klimchukd784d482021-02-15 15:02:00 +1100420 chipaddr addr)
421{
422 msg_pspew("%s\n", __func__);
423 if (sp_max_write_n) {
424 if ((sp_prev_was_write)
425 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
426 sp_write_n_buf[sp_write_n_bytes++] = val;
427 } else {
428 if ((sp_prev_was_write) && (sp_write_n_bytes))
429 sp_pass_writen();
430 sp_prev_was_write = 1;
431 sp_write_n_addr = addr;
432 sp_write_n_bytes = 1;
433 sp_write_n_buf[0] = val;
434 }
435 sp_check_opbuf_usage(7 + sp_write_n_bytes);
436 if (sp_write_n_bytes >= sp_max_write_n)
437 sp_pass_writen();
438 } else {
439 /* We will have to do single writeb ops. */
440 unsigned char writeb_parm[4];
441 sp_check_opbuf_usage(6);
442 writeb_parm[0] = (addr >> 0) & 0xFF;
443 writeb_parm[1] = (addr >> 8) & 0xFF;
444 writeb_parm[2] = (addr >> 16) & 0xFF;
445 writeb_parm[3] = val;
446 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); // FIXME: return error
447 sp_opbuf_usage += 5;
448 }
449}
450
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000451static uint8_t serprog_chip_readb(const struct flashctx *flash,
Anastasia Klimchukd784d482021-02-15 15:02:00 +1100452 const chipaddr addr)
453{
454 unsigned char c;
455 unsigned char buf[3];
456 /* Will stream the read operation - eg. add it to the stream buffer, *
457 * then flush the buffer, then read the read answer. */
458 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
459 sp_execute_opbuf_noflush();
460 buf[0] = ((addr >> 0) & 0xFF);
461 buf[1] = ((addr >> 8) & 0xFF);
462 buf[2] = ((addr >> 16) & 0xFF);
463 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); // FIXME: return error
464 sp_flush_stream(); // FIXME: return error
465 if (serialport_read(&c, 1) != 0)
466 msg_perr(MSGHEADER "readb byteread"); // FIXME: return error
467 msg_pspew("%s addr=0x%" PRIxPTR " returning 0x%02X\n", __func__, addr, c);
468 return c;
469}
470
471/* Local version that really does the job, doesn't care of max_read_n. */
472static int sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
473{
474 unsigned char sbuf[6];
475 msg_pspew("%s: addr=0x%" PRIxPTR " len=%zu\n", __func__, addr, len);
476 /* Stream the read-n -- as above. */
477 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
478 sp_execute_opbuf_noflush();
479 sbuf[0] = ((addr >> 0) & 0xFF);
480 sbuf[1] = ((addr >> 8) & 0xFF);
481 sbuf[2] = ((addr >> 16) & 0xFF);
482 sbuf[3] = ((len >> 0) & 0xFF);
483 sbuf[4] = ((len >> 8) & 0xFF);
484 sbuf[5] = ((len >> 16) & 0xFF);
485 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
486 if (sp_flush_stream() != 0)
487 return 1;
488 if (serialport_read(buf, len) != 0) {
489 msg_perr(MSGHEADER "Error: cannot read read-n data");
490 return 1;
491 }
492 return 0;
493}
494
495/* The externally called version that makes sure that max_read_n is obeyed. */
496static void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf,
497 const chipaddr addr, size_t len)
498{
499 size_t lenm = len;
500 chipaddr addrm = addr;
501 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
502 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n); // FIXME: return error
503 addrm += sp_max_read_n;
504 lenm -= sp_max_read_n;
505 }
506 if (lenm)
507 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm); // FIXME: return error
508}
509
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000510static const struct par_master par_master_serprog = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000511 .chip_readb = serprog_chip_readb,
512 .chip_readw = fallback_chip_readw,
513 .chip_readl = fallback_chip_readl,
514 .chip_readn = serprog_chip_readn,
515 .chip_writeb = serprog_chip_writeb,
516 .chip_writew = fallback_chip_writew,
517 .chip_writel = fallback_chip_writel,
518 .chip_writen = fallback_chip_writen,
519};
520
Anastasia Klimchukd784d482021-02-15 15:02:00 +1100521static int serprog_shutdown(void *data)
522{
523 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
524 if (sp_execute_opbuf() != 0)
525 msg_pwarn("Could not flush command buffer.\n");
526 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
527 uint8_t dis = 0;
528 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &dis, 0, NULL) == 0)
529 msg_pdbg(MSGHEADER "Output drivers disabled\n");
530 else
531 msg_pwarn(MSGHEADER "%s: Warning: could not disable output buffers\n", __func__);
532 }
533 /* FIXME: fix sockets on windows(?), especially closing */
534 serialport_shutdown(&sp_fd);
535 if (sp_max_write_n)
536 free(sp_write_n_buf);
537 return 0;
538}
539
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000540static enum chipbustype serprog_buses_supported = BUS_NONE;
541
Urja Rannikkof3196df2009-07-21 13:02:59 +0000542int serprog_init(void)
543{
544 uint16_t iface;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000545 unsigned char pgmname[17];
546 unsigned char rbuf[3];
547 unsigned char c;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000548 char *device;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000549 int have_device = 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000550
Stefan Tauner72587f82016-01-04 03:05:15 +0000551 /* the parameter is either of format "dev=/dev/device[:baud]" or "ip=ip:port" */
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000552 device = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000553 if (device && strlen(device)) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000554 char *baud_str = strstr(device, ":");
555 if (baud_str != NULL) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000556 /* Split device from baudrate. */
Stefan Tauner72587f82016-01-04 03:05:15 +0000557 *baud_str = '\0';
558 baud_str++;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000559 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000560 int baud;
561 /* Convert baud string to value.
562 * baud_str is either NULL (if strstr can't find the colon), points to the \0 after the colon
563 * if no characters where given after the colon, or a string to convert... */
564 if (baud_str == NULL || *baud_str == '\0') {
565 baud = -1;
566 msg_pdbg("No baudrate specified, using the hardware's defaults.\n");
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000567 } else {
Stefan Tauner72587f82016-01-04 03:05:15 +0000568 baud = atoi(baud_str); // FIXME: replace atoi with strtoul
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000569 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000570 if (strlen(device) > 0) {
571 sp_fd = sp_openserport(device, baud);
Stefan Tauneracfc4c62012-11-30 16:46:45 +0000572 if (sp_fd == SER_INV_FD) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000573 free(device);
574 return 1;
575 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000576 have_device++;
577 }
578 }
Urja Rannikko27b431b2016-01-04 03:05:23 +0000579
580#if !IS_WINDOWS
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000581 if (device && !strlen(device)) {
582 msg_perr("Error: No device specified.\n"
Stefan Tauner72587f82016-01-04 03:05:15 +0000583 "Use flashrom -p serprog:dev=/dev/device[:baud]\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000584 free(device);
585 return 1;
586 }
587 free(device);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000588
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000589 device = extract_programmer_param("ip");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000590 if (have_device && device) {
591 msg_perr("Error: Both host and device specified.\n"
592 "Please use either dev= or ip= but not both.\n");
593 free(device);
594 return 1;
595 }
596 if (device && strlen(device)) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000597 char *port = strstr(device, ":");
598 if (port != NULL) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000599 /* Split host from port. */
Stefan Tauner72587f82016-01-04 03:05:15 +0000600 *port = '\0';
601 port++;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000602 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000603 if (!port || !strlen(port)) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000604 msg_perr("Error: No port specified.\n"
605 "Use flashrom -p serprog:ip=ipaddr:port\n");
606 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000607 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000608 }
609 if (strlen(device)) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000610 sp_fd = sp_opensocket(device, atoi(port)); // FIXME: replace atoi with strtoul
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000611 if (sp_fd < 0) {
612 free(device);
613 return 1;
614 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000615 have_device++;
616 }
617 }
618 if (device && !strlen(device)) {
619 msg_perr("Error: No host specified.\n"
620 "Use flashrom -p serprog:ip=ipaddr:port\n");
621 free(device);
622 return 1;
623 }
Urja Rannikko27b431b2016-01-04 03:05:23 +0000624#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000625 free(device);
626
627 if (!have_device) {
Urja Rannikko27b431b2016-01-04 03:05:23 +0000628#if IS_WINDOWS
629 msg_perr("Error: No device specified.\n"
630 "Use flashrom -p serprog:dev=comN[:baud]\n");
631#else
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000632 msg_perr("Error: Neither host nor device specified.\n"
633 "Use flashrom -p serprog:dev=/dev/device:baud or "
634 "flashrom -p serprog:ip=ipaddr:port\n");
Urja Rannikko27b431b2016-01-04 03:05:23 +0000635#endif
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000636 return 1;
637 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000638
Sean Nelson74e8af52010-01-10 01:06:23 +0000639 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000640
641 sp_check_avail_automatic = 0;
642
Niklas Söderlund7145a502012-09-07 07:07:07 +0000643 if (sp_synchronize())
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000644 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000645
Sean Nelson74e8af52010-01-10 01:06:23 +0000646 msg_pdbg(MSGHEADER "Synchronized\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000647
648 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000649 msg_perr("Error: NAK to query interface version\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000650 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000651 }
652
653 if (iface != 1) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000654 msg_perr("Error: Unknown interface version: %d\n", iface);
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000655 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000656 }
657
Sean Nelson74e8af52010-01-10 01:06:23 +0000658 msg_pdbg(MSGHEADER "Interface version ok.\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000659
660 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000661 msg_perr("Error: query command map not supported\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000662 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000663 }
664
665 sp_check_avail_automatic = 1;
666
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000667 /* FIXME: This assumes that serprog device bustypes are always
668 * identical with flashrom bustype enums and that they all fit
669 * in a single byte.
670 */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000671 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000672 msg_pwarn("Warning: NAK to query supported buses\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000673 c = BUS_NONSPI; /* A reasonable default for now. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000674 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000675 serprog_buses_supported = c;
676
Stefan Tauner31019d42011-10-22 21:45:27 +0000677 msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
678 (c & BUS_PARALLEL) ? "on" : "off",
679 (c & BUS_LPC) ? "on" : "off",
680 (c & BUS_FWH) ? "on" : "off",
681 (c & BUS_SPI) ? "on" : "off");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000682 /* Check for the minimum operational set of commands. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000683 if (serprog_buses_supported & BUS_SPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000684 uint8_t bt = BUS_SPI;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000685 char *spispeed;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000686 if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
687 msg_perr("Error: SPI operation not supported while the "
688 "bustype is SPI\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000689 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000690 }
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000691 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000692 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000693 /* Success of any of these commands is optional. We don't need
694 the programmer to tell us its limits, but if it doesn't, we
695 will assume stuff, so it's in the programmers best interest
696 to tell us. */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000697 if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
698 uint32_t v;
699 v = ((unsigned int)(rbuf[0]) << 0);
700 v |= ((unsigned int)(rbuf[1]) << 8);
701 v |= ((unsigned int)(rbuf[2]) << 16);
702 if (v == 0)
703 v = (1 << 24) - 1; /* SPI-op maximum. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000704 spi_master_serprog.max_data_write = v;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000705 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
706 }
707 if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
708 uint32_t v;
709 v = ((unsigned int)(rbuf[0]) << 0);
710 v |= ((unsigned int)(rbuf[1]) << 8);
711 v |= ((unsigned int)(rbuf[2]) << 16);
712 if (v == 0)
713 v = (1 << 24) - 1; /* SPI-op maximum. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000714 spi_master_serprog.max_data_read = v;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000715 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
716 }
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000717 spispeed = extract_programmer_param("spispeed");
718 if (spispeed && strlen(spispeed)) {
719 uint32_t f_spi_req, f_spi;
720 uint8_t buf[4];
721 char *f_spi_suffix;
722
723 errno = 0;
724 f_spi_req = strtol(spispeed, &f_spi_suffix, 0);
725 if (errno != 0 || spispeed == f_spi_suffix) {
726 msg_perr("Error: Could not convert 'spispeed'.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000727 free(spispeed);
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000728 goto init_err_cleanup_exit;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000729 }
730 if (strlen(f_spi_suffix) == 1) {
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000731 if (!strcasecmp(f_spi_suffix, "M")) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000732 f_spi_req *= 1000000;
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000733 } else if (!strcasecmp(f_spi_suffix, "k")) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000734 f_spi_req *= 1000;
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000735 } else {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000736 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000737 free(spispeed);
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000738 goto init_err_cleanup_exit;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000739 }
740 } else if (strlen(f_spi_suffix) > 1) {
741 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000742 free(spispeed);
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000743 goto init_err_cleanup_exit;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000744 }
745
746 buf[0] = (f_spi_req >> (0 * 8)) & 0xFF;
747 buf[1] = (f_spi_req >> (1 * 8)) & 0xFF;
748 buf[2] = (f_spi_req >> (2 * 8)) & 0xFF;
749 buf[3] = (f_spi_req >> (3 * 8)) & 0xFF;
750
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000751 if (sp_check_commandavail(S_CMD_S_SPI_FREQ) == 0) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000752 msg_pwarn(MSGHEADER "Warning: Setting the SPI clock rate is not supported!\n");
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000753 } else if (sp_docommand(S_CMD_S_SPI_FREQ, 4, buf, 4, buf) == 0) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000754 f_spi = buf[0];
755 f_spi |= buf[1] << (1 * 8);
756 f_spi |= buf[2] << (2 * 8);
757 f_spi |= buf[3] << (3 * 8);
758 msg_pdbg(MSGHEADER "Requested to set SPI clock frequency to %u Hz. "
759 "It was actually set to %u Hz\n", f_spi_req, f_spi);
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000760 } else {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000761 msg_pwarn(MSGHEADER "Setting SPI clock rate to %u Hz failed!\n", f_spi_req);
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000762 }
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000763 }
764 free(spispeed);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000765 bt = serprog_buses_supported;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000766 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000767 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000768 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000769
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000770 if (serprog_buses_supported & BUS_NONSPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000771 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
772 msg_perr("Error: Initialize operation buffer "
773 "not supported\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000774 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000775 }
776
777 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
778 msg_perr("Error: Write to opbuf: "
779 "delay not supported\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000780 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000781 }
782
783 /* S_CMD_O_EXEC availability checked later. */
784
785 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
786 msg_perr("Error: Single byte read not supported\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000787 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000788 }
789 /* This could be translated to single byte reads (if missing),
790 * but now we don't support that. */
791 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
792 msg_perr("Error: Read n bytes not supported\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000793 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000794 }
795 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
796 msg_perr("Error: Write to opbuf: "
797 "write byte not supported\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000798 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000799 }
800
801 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
802 msg_pdbg(MSGHEADER "Write-n not supported");
803 sp_max_write_n = 0;
804 } else {
805 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
806 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
807 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
808 if (!sp_max_write_n) {
809 sp_max_write_n = (1 << 24);
810 }
811 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
812 sp_max_write_n);
813 sp_write_n_buf = malloc(sp_max_write_n);
814 if (!sp_write_n_buf) {
815 msg_perr("Error: cannot allocate memory for "
816 "Write-n buffer\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000817 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000818 }
819 sp_write_n_bytes = 0;
820 }
821
822 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
823 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
824 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
825 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
826 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
827 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
828 sp_max_read_n ? sp_max_read_n : (1 << 24));
829 } else {
830 msg_pdbg(MSGHEADER "Maximum read-n length "
831 "not reported\n");
832 sp_max_read_n = 0;
833 }
834
Urja Rannikkof3196df2009-07-21 13:02:59 +0000835 }
836
837 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000838 msg_pwarn("Warning: NAK to query programmer name\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000839 strcpy((char *)pgmname, "(unknown)");
840 }
841 pgmname[16] = 0;
Stefan Tauner31019d42011-10-22 21:45:27 +0000842 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000843
844 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000845 msg_pwarn("Warning: NAK to query serial buffer size\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000846 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000847 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000848 sp_device_serbuf_size);
849
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000850 if (sp_check_commandavail(S_CMD_O_INIT)) {
851 /* This would be inconsistent. */
852 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
853 msg_perr("Error: Execute operation buffer not "
854 "supported\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000855 goto init_err_cleanup_exit;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000856 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000857
858 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
859 msg_perr("Error: NAK to initialize operation buffer\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000860 goto init_err_cleanup_exit;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000861 }
862
863 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
864 &sp_device_opbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000865 msg_pwarn("Warning: NAK to query operation buffer size\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000866 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000867 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000868 sp_device_opbuf_size);
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200869 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000870
Stefan Tauner92fefc92012-10-27 00:34:23 +0000871 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
872 uint8_t en = 1;
873 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &en, 0, NULL) != 0) {
874 msg_perr("Error: could not enable output buffers\n");
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000875 goto init_err_cleanup_exit;
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000876 } else {
Stefan Tauner92fefc92012-10-27 00:34:23 +0000877 msg_pdbg(MSGHEADER "Output drivers enabled\n");
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000878 }
879 } else {
Stefan Tauner92fefc92012-10-27 00:34:23 +0000880 msg_pdbg(MSGHEADER "Warning: Programmer does not support toggling its output drivers\n");
Anastasia Klimchukfd844702021-05-24 10:07:56 +1000881 }
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000882
Urja Rannikkof3196df2009-07-21 13:02:59 +0000883 sp_prev_was_write = 0;
884 sp_streamed_transmit_ops = 0;
885 sp_streamed_transmit_bytes = 0;
886 sp_opbuf_usage = 0;
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000887
888 if (register_shutdown(serprog_shutdown, NULL))
889 goto init_err_cleanup_exit;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000890 if (serprog_buses_supported & BUS_SPI)
Nico Huber7e496852021-05-11 17:38:14 +0200891 register_spi_master(&spi_master_serprog, NULL);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000892 if (serprog_buses_supported & BUS_NONSPI)
Anastasia Klimchuk6a5db262021-05-21 09:40:58 +1000893 register_par_master(&par_master_serprog, serprog_buses_supported & BUS_NONSPI, NULL);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000894 return 0;
Anastasia Klimchuke2f10682021-05-24 09:55:03 +1000895
896init_err_cleanup_exit:
897 serprog_shutdown(NULL);
898 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000899}
900
Stefan Taunerf80419c2014-05-02 15:41:42 +0000901void serprog_delay(unsigned int usecs)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000902{
903 unsigned char buf[4];
Stefan Tauner31019d42011-10-22 21:45:27 +0000904 msg_pspew("%s usecs=%d\n", __func__, usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000905 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
Stefan Taunerd7d423b2012-10-20 09:13:16 +0000906 msg_pdbg2("serprog_delay used, but programmer doesn't support delays natively - emulating\n");
Stefan Tauner31019d42011-10-22 21:45:27 +0000907 internal_delay(usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000908 return;
909 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000910 if ((sp_max_write_n) && (sp_write_n_bytes))
911 sp_pass_writen();
912 sp_check_opbuf_usage(5);
Stefan Tauner31019d42011-10-22 21:45:27 +0000913 buf[0] = ((usecs >> 0) & 0xFF);
914 buf[1] = ((usecs >> 8) & 0xFF);
915 buf[2] = ((usecs >> 16) & 0xFF);
916 buf[3] = ((usecs >> 24) & 0xFF);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000917 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
918 sp_opbuf_usage += 5;
919 sp_prev_was_write = 0;
920}
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000921
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000922void *serprog_map(const char *descr, uintptr_t phys_addr, size_t len)
923{
924 /* Serprog transmits 24 bits only and assumes the underlying implementation handles any remaining bits
925 * correctly (usually setting them to one either in software (for FWH/LPC) or relying on the fact that
926 * the hardware observes a subset of the address bits only). Combined with the standard mapping of
927 * flashrom this creates a 16 MB-wide window just below the 4 GB boundary where serprog can operate (as
928 * needed for non-SPI chips). Below we make sure that the requested range is within this window. */
929 if ((phys_addr & 0xFF000000) == 0xFF000000) {
930 return (void*)phys_addr;
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000931 }
Elyes HAOUASec819d62019-06-09 17:50:51 +0200932 msg_pwarn(MSGHEADER "requested mapping %s is incompatible: 0x%zx bytes at 0x%0*" PRIxPTR ".\n",
933 descr, len, PRIxPTR_WIDTH, phys_addr);
934 return NULL;
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000935}