blob: a2a3fe01f7d7757974420fdf2aff125c27be7764 [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000022#include "platform.h"
23
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000024#include <stdio.h>
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000025#if ! IS_WINDOWS /* stuff (presumably) needed for sockets only */
Urja Rannikko22915352009-06-23 11:33:43 +000026#include <stdlib.h>
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000027#include <unistd.h>
Urja Rannikko22915352009-06-23 11:33:43 +000028#include <fcntl.h>
Urja Rannikko22915352009-06-23 11:33:43 +000029#include <sys/socket.h>
30#include <arpa/inet.h>
31#include <netinet/in.h>
32#include <netinet/tcp.h>
33#include <netdb.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000034#endif
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000035#if IS_WINDOWS
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000036#include <conio.h>
37#else
Urja Rannikko22915352009-06-23 11:33:43 +000038#include <termios.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000039#endif
40#include <string.h>
41#include <errno.h>
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000042#include "flash.h"
43#include "programmer.h"
Urja Rannikkoc93f5f12011-09-15 23:38:14 +000044#include "chipdrivers.h"
Stefan Tauner0d82e952012-10-27 00:34:18 +000045#include "serprog.h"
Urja Rannikkof3196df2009-07-21 13:02:59 +000046
Stefan Tauner31019d42011-10-22 21:45:27 +000047#define MSGHEADER "serprog: "
Urja Rannikkof3196df2009-07-21 13:02:59 +000048
David Hendricks8bb20212011-06-14 01:35:36 +000049/*
50 * FIXME: This prototype was added to help reduce diffs for the shutdown
51 * registration patch, which shifted many lines of code to place
52 * serprog_shutdown() before serprog_init(). It should be removed soon.
53 */
54static int serprog_shutdown(void *data);
55
Urja Rannikkof3196df2009-07-21 13:02:59 +000056static uint16_t sp_device_serbuf_size = 16;
57static uint16_t sp_device_opbuf_size = 300;
58/* Bitmap of supported commands */
59static uint8_t sp_cmdmap[32];
60
Uwe Hermann4e3d0b32010-03-25 23:18:41 +000061/* sp_prev_was_write used to detect writes with contiguous addresses
Urja Rannikkof3196df2009-07-21 13:02:59 +000062 and combine them to write-n's */
63static int sp_prev_was_write = 0;
64/* sp_write_n_addr used as the starting addr of the currently
65 combined write-n operation */
66static uint32_t sp_write_n_addr;
67/* The maximum length of an write_n operation; 0 = write-n not supported */
68static uint32_t sp_max_write_n = 0;
69/* The maximum length of a read_n operation; 0 = 2^24 */
70static uint32_t sp_max_read_n = 0;
71
72/* A malloc'd buffer for combining the operation's data
73 and a counter that tells how much data is there. */
74static uint8_t *sp_write_n_buf;
75static uint32_t sp_write_n_bytes = 0;
76
77/* sp_streamed_* used for flow control checking */
78static int sp_streamed_transmit_ops = 0;
79static int sp_streamed_transmit_bytes = 0;
80
81/* sp_opbuf_usage used for counting the amount of
82 on-device operation buffer used */
83static int sp_opbuf_usage = 0;
84/* if true causes sp_docommand to automatically check
85 whether the command is supported before doing it */
86static int sp_check_avail_automatic = 0;
87
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000088#if ! IS_WINDOWS
Urja Rannikkof3196df2009-07-21 13:02:59 +000089static int sp_opensocket(char *ip, unsigned int port)
90{
91 int flag = 1;
92 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +000093 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +000094 int sock;
Sean Nelson74e8af52010-01-10 01:06:23 +000095 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
Urja Rannikkof3196df2009-07-21 13:02:59 +000096 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Niklas Söderlund2a95e872012-07-30 19:42:33 +000097 if (sock < 0) {
98 msg_perr("Error: serprog cannot open socket: %s\n", strerror(errno));
99 return -1;
100 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000101 hostPtr = gethostbyname(ip);
102 if (NULL == hostPtr) {
103 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000104 if (NULL == hostPtr) {
Stefan Reinauerb8792872014-04-26 16:12:03 +0000105 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000106 msg_perr("Error: cannot resolve %s\n", ip);
107 return -1;
108 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000109 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000110 sp.si.sin_family = AF_INET;
111 sp.si.sin_port = htons(port);
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000112 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr_list[0], hostPtr->h_length);
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000113 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000114 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000115 msg_perr("Error: serprog cannot connect: %s\n", strerror(errno));
116 return -1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000117 }
118 /* We are latency limited, and sometimes do write-write-read *
119 * (write-n) - so enable TCP_NODELAY. */
Stefan Reinauer907c3eb2014-04-26 16:12:31 +0000120 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int))) {
121 close(sock);
122 msg_perr("Error: serprog cannot set socket options: %s\n", strerror(errno));
123 return -1;
124 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000125 return sock;
126}
Stefan Tauner52b6e9d2013-04-01 00:46:05 +0000127#endif
Urja Rannikkof3196df2009-07-21 13:02:59 +0000128
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000129/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000130 * always succeeded in) bring the serial protocol to known waiting-for- *
Stefan Taunerae3d8372013-04-01 00:45:45 +0000131 * command state - uses nonblocking I/O - rest of the driver uses *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000132 * blocking read - TODO: add an alarm() timer for the rest of the app on *
133 * serial operations, though not such a big issue as the first thing to *
134 * do is synchronize (eg. check that device is alive). */
Niklas Söderlund7145a502012-09-07 07:07:07 +0000135static int sp_synchronize(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000136{
137 int i;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000138 unsigned char buf[8];
Urja Rannikkof3196df2009-07-21 13:02:59 +0000139 /* First sends 8 NOPs, then flushes the return data - should cause *
140 * the device serial parser to get to a sane state, unless if it *
141 * is waiting for a real long write-n. */
142 memset(buf, S_CMD_NOP, 8);
Stefan Taunerae3d8372013-04-01 00:45:45 +0000143 if (serialport_write_nonblock(buf, 8, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000144 goto err_out;
145 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000146 /* A second should be enough to get all the answers to the buffer */
Stefan Tauner79587f52013-04-01 00:45:51 +0000147 internal_delay(1000 * 1000);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000148 sp_flush_incoming();
149
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000150 /* Then try up to 8 times to send syncnop and get the correct special *
151 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
152 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
153 * attempt, ~1s if immediate success. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000154 for (i = 0; i < 8; i++) {
155 int n;
156 unsigned char c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000157 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000158 goto err_out;
159 }
Sean Nelson74e8af52010-01-10 01:06:23 +0000160 msg_pdbg(".");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000161 fflush(stdout);
162 for (n = 0; n < 10; n++) {
Stefan Tauner00e16082013-04-01 00:45:38 +0000163 int ret = serialport_read_nonblock(&c, 1, 50, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000164 if (ret < 0)
165 goto err_out;
166 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000167 continue;
Stefan Tauner00e16082013-04-01 00:45:38 +0000168 ret = serialport_read_nonblock(&c, 1, 20, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000169 if (ret < 0)
170 goto err_out;
171 if (ret > 0 || c != S_ACK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000172 continue;
173 c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000174 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
175 goto err_out;
Niklas Söderlund7145a502012-09-07 07:07:07 +0000176 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000177 ret = serialport_read_nonblock(&c, 1, 500, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000178 if (ret < 0)
179 goto err_out;
180 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000181 break; /* fail */
Stefan Tauner00e16082013-04-01 00:45:38 +0000182 ret = serialport_read_nonblock(&c, 1, 100, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000183 if (ret > 0 || ret < 0)
184 goto err_out;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000185 if (c != S_ACK)
186 break; /* fail */
Sean Nelson74e8af52010-01-10 01:06:23 +0000187 msg_pdbg("\n");
Niklas Söderlund7145a502012-09-07 07:07:07 +0000188 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000189 }
190 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000191err_out:
192 msg_perr("Error: cannot synchronize protocol - check communications and reset device?\n");
193 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000194}
195
196static int sp_check_commandavail(uint8_t command)
197{
198 int byteoffs, bitoffs;
199 byteoffs = command / 8;
200 bitoffs = command % 8;
201 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
202}
203
204static int sp_automatic_cmdcheck(uint8_t cmd)
205{
206 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000207 msg_pdbg("Warning: Automatic command availability check failed "
Stefan Taunere34e3e82013-01-01 00:06:51 +0000208 "for cmd 0x%02x - won't execute cmd\n", cmd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000209 return 1;
210 }
211 return 0;
212}
213
214static int sp_docommand(uint8_t command, uint32_t parmlen,
Stefan Tauner31019d42011-10-22 21:45:27 +0000215 uint8_t *params, uint32_t retlen, void *retparms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000216{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000217 unsigned char c;
218 if (sp_automatic_cmdcheck(command))
219 return 1;
Stefan Tauner79587f52013-04-01 00:45:51 +0000220 if (serialport_write(&command, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000221 msg_perr("Error: cannot write op code: %s\n", strerror(errno));
222 return 1;
223 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000224 if (serialport_write(params, parmlen) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000225 msg_perr("Error: cannot write parameters: %s\n", strerror(errno));
226 return 1;
227 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000228 if (serialport_read(&c, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000229 msg_perr("Error: cannot read from device: %s\n", strerror(errno));
230 return 1;
231 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000232 if (c == S_NAK)
233 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000234 if (c != S_ACK) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000235 msg_perr("Error: invalid response 0x%02X from device\n", c);
236 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000237 }
238 if (retlen) {
Stefan Tauner79587f52013-04-01 00:45:51 +0000239 if (serialport_read(retparms, retlen) != 0) {
240 msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
241 return 1;
242 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000243 }
244 return 0;
245}
246
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000247static int sp_flush_stream(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000248{
249 if (sp_streamed_transmit_ops)
250 do {
251 unsigned char c;
Stefan Tauner79587f52013-04-01 00:45:51 +0000252 if (serialport_read(&c, 1) != 0) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000253 msg_perr("Error: cannot read from device (flushing stream)");
254 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000255 }
256 if (c == S_NAK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000257 msg_perr("Error: NAK to a stream buffer operation\n");
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000258 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000259 }
260 if (c != S_ACK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000261 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000262 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000263 }
264 } while (--sp_streamed_transmit_ops);
265 sp_streamed_transmit_ops = 0;
266 sp_streamed_transmit_bytes = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000267 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000268}
269
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000270static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t *parms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000271{
272 uint8_t *sp;
273 if (sp_automatic_cmdcheck(cmd))
274 return 1;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000275
Urja Rannikkof3196df2009-07-21 13:02:59 +0000276 sp = malloc(1 + parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000277 if (!sp) {
278 msg_perr("Error: cannot malloc command buffer\n");
279 return 1;
280 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000281 sp[0] = cmd;
282 memcpy(&(sp[1]), parms, parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000283
284 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size)) {
285 if (sp_flush_stream() != 0) {
286 free(sp);
287 return 1;
288 }
289 }
290 if (serialport_write(sp, 1 + parmlen) != 0) {
291 msg_perr("Error: cannot write command\n");
292 free(sp);
293 return 1;
294 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000295 sp_streamed_transmit_ops += 1;
296 sp_streamed_transmit_bytes += 1 + parmlen;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000297
298 free(sp);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000299 return 0;
300}
301
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000302static int serprog_spi_send_command(struct flashctx *flash,
303 unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000304 const unsigned char *writearr,
305 unsigned char *readarr);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000306static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000307 unsigned int start, unsigned int len);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000308static struct spi_master spi_master_serprog = {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000309 .type = SPI_CONTROLLER_SERPROG,
310 .max_data_read = MAX_DATA_READ_UNLIMITED,
311 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
312 .command = serprog_spi_send_command,
313 .multicommand = default_spi_send_multicommand,
314 .read = serprog_spi_read,
315 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000316 .write_aai = default_spi_write_aai,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000317};
318
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000319static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
320 chipaddr addr);
321static uint8_t serprog_chip_readb(const struct flashctx *flash,
322 const chipaddr addr);
323static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
324 const chipaddr addr, size_t len);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000325static const struct par_master par_master_serprog = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000326 .chip_readb = serprog_chip_readb,
327 .chip_readw = fallback_chip_readw,
328 .chip_readl = fallback_chip_readl,
329 .chip_readn = serprog_chip_readn,
330 .chip_writeb = serprog_chip_writeb,
331 .chip_writew = fallback_chip_writew,
332 .chip_writel = fallback_chip_writel,
333 .chip_writen = fallback_chip_writen,
334};
335
336static enum chipbustype serprog_buses_supported = BUS_NONE;
337
Urja Rannikkof3196df2009-07-21 13:02:59 +0000338int serprog_init(void)
339{
340 uint16_t iface;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000341 unsigned char pgmname[17];
342 unsigned char rbuf[3];
343 unsigned char c;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000344 char *device;
345 char *baudport;
346 int have_device = 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000347
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000348 /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000349 device = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000350 if (device && strlen(device)) {
351 baudport = strstr(device, ":");
352 if (baudport) {
353 /* Split device from baudrate. */
354 *baudport = '\0';
355 baudport++;
356 }
357 if (!baudport || !strlen(baudport)) {
358 msg_perr("Error: No baudrate specified.\n"
359 "Use flashrom -p serprog:dev=/dev/device:baud\n");
360 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000361 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000362 }
363 if (strlen(device)) {
364 sp_fd = sp_openserport(device, atoi(baudport));
Stefan Tauneracfc4c62012-11-30 16:46:45 +0000365 if (sp_fd == SER_INV_FD) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000366 free(device);
367 return 1;
368 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000369 have_device++;
370 }
371 }
372 if (device && !strlen(device)) {
373 msg_perr("Error: No device specified.\n"
374 "Use flashrom -p serprog:dev=/dev/device:baud\n");
375 free(device);
376 return 1;
377 }
378 free(device);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000379
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000380#if !IS_WINDOWS
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000381 device = extract_programmer_param("ip");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000382 if (have_device && device) {
383 msg_perr("Error: Both host and device specified.\n"
384 "Please use either dev= or ip= but not both.\n");
385 free(device);
386 return 1;
387 }
388 if (device && strlen(device)) {
389 baudport = strstr(device, ":");
390 if (baudport) {
391 /* Split host from port. */
392 *baudport = '\0';
393 baudport++;
394 }
395 if (!baudport || !strlen(baudport)) {
396 msg_perr("Error: No port specified.\n"
397 "Use flashrom -p serprog:ip=ipaddr:port\n");
398 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000399 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000400 }
401 if (strlen(device)) {
402 sp_fd = sp_opensocket(device, atoi(baudport));
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000403 if (sp_fd < 0) {
404 free(device);
405 return 1;
406 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000407 have_device++;
408 }
409 }
410 if (device && !strlen(device)) {
411 msg_perr("Error: No host specified.\n"
412 "Use flashrom -p serprog:ip=ipaddr:port\n");
413 free(device);
414 return 1;
415 }
416 free(device);
417
418 if (!have_device) {
419 msg_perr("Error: Neither host nor device specified.\n"
420 "Use flashrom -p serprog:dev=/dev/device:baud or "
421 "flashrom -p serprog:ip=ipaddr:port\n");
422 return 1;
423 }
Stefan Tauner52b6e9d2013-04-01 00:46:05 +0000424#endif
Urja Rannikkof3196df2009-07-21 13:02:59 +0000425
David Hendricks8bb20212011-06-14 01:35:36 +0000426 if (register_shutdown(serprog_shutdown, NULL))
427 return 1;
428
Sean Nelson74e8af52010-01-10 01:06:23 +0000429 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000430
431 sp_check_avail_automatic = 0;
432
Niklas Söderlund7145a502012-09-07 07:07:07 +0000433 if (sp_synchronize())
434 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000435
Sean Nelson74e8af52010-01-10 01:06:23 +0000436 msg_pdbg(MSGHEADER "Synchronized\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000437
438 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000439 msg_perr("Error: NAK to query interface version\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000440 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000441 }
442
443 if (iface != 1) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000444 msg_perr("Error: Unknown interface version: %d\n", iface);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000445 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000446 }
447
Sean Nelson74e8af52010-01-10 01:06:23 +0000448 msg_pdbg(MSGHEADER "Interface version ok.\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000449
450 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000451 msg_perr("Error: query command map not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000452 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000453 }
454
455 sp_check_avail_automatic = 1;
456
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000457 /* FIXME: This assumes that serprog device bustypes are always
458 * identical with flashrom bustype enums and that they all fit
459 * in a single byte.
460 */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000461 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000462 msg_pwarn("Warning: NAK to query supported buses\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000463 c = BUS_NONSPI; /* A reasonable default for now. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000464 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000465 serprog_buses_supported = c;
466
Stefan Tauner31019d42011-10-22 21:45:27 +0000467 msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
468 (c & BUS_PARALLEL) ? "on" : "off",
469 (c & BUS_LPC) ? "on" : "off",
470 (c & BUS_FWH) ? "on" : "off",
471 (c & BUS_SPI) ? "on" : "off");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000472 /* Check for the minimum operational set of commands. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000473 if (serprog_buses_supported & BUS_SPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000474 uint8_t bt = BUS_SPI;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000475 char *spispeed;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000476 if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
477 msg_perr("Error: SPI operation not supported while the "
478 "bustype is SPI\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000479 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000480 }
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000481 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
482 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000483 /* Success of any of these commands is optional. We don't need
484 the programmer to tell us its limits, but if it doesn't, we
485 will assume stuff, so it's in the programmers best interest
486 to tell us. */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000487 if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
488 uint32_t v;
489 v = ((unsigned int)(rbuf[0]) << 0);
490 v |= ((unsigned int)(rbuf[1]) << 8);
491 v |= ((unsigned int)(rbuf[2]) << 16);
492 if (v == 0)
493 v = (1 << 24) - 1; /* SPI-op maximum. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000494 spi_master_serprog.max_data_write = v;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000495 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
496 }
497 if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
498 uint32_t v;
499 v = ((unsigned int)(rbuf[0]) << 0);
500 v |= ((unsigned int)(rbuf[1]) << 8);
501 v |= ((unsigned int)(rbuf[2]) << 16);
502 if (v == 0)
503 v = (1 << 24) - 1; /* SPI-op maximum. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000504 spi_master_serprog.max_data_read = v;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000505 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
506 }
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000507 spispeed = extract_programmer_param("spispeed");
508 if (spispeed && strlen(spispeed)) {
509 uint32_t f_spi_req, f_spi;
510 uint8_t buf[4];
511 char *f_spi_suffix;
512
513 errno = 0;
514 f_spi_req = strtol(spispeed, &f_spi_suffix, 0);
515 if (errno != 0 || spispeed == f_spi_suffix) {
516 msg_perr("Error: Could not convert 'spispeed'.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000517 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000518 return 1;
519 }
520 if (strlen(f_spi_suffix) == 1) {
521 if (!strcasecmp(f_spi_suffix, "M"))
522 f_spi_req *= 1000000;
523 else if (!strcasecmp(f_spi_suffix, "k"))
524 f_spi_req *= 1000;
525 else {
526 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000527 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000528 return 1;
529 }
530 } else if (strlen(f_spi_suffix) > 1) {
531 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000532 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000533 return 1;
534 }
535
536 buf[0] = (f_spi_req >> (0 * 8)) & 0xFF;
537 buf[1] = (f_spi_req >> (1 * 8)) & 0xFF;
538 buf[2] = (f_spi_req >> (2 * 8)) & 0xFF;
539 buf[3] = (f_spi_req >> (3 * 8)) & 0xFF;
540
541 if (sp_check_commandavail(S_CMD_S_SPI_FREQ) == 0)
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000542 msg_pwarn(MSGHEADER "Warning: Setting the SPI clock rate is not supported!\n");
543 else if (sp_docommand(S_CMD_S_SPI_FREQ, 4, buf, 4, buf) == 0) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000544 f_spi = buf[0];
545 f_spi |= buf[1] << (1 * 8);
546 f_spi |= buf[2] << (2 * 8);
547 f_spi |= buf[3] << (3 * 8);
548 msg_pdbg(MSGHEADER "Requested to set SPI clock frequency to %u Hz. "
549 "It was actually set to %u Hz\n", f_spi_req, f_spi);
550 } else
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000551 msg_pwarn(MSGHEADER "Setting SPI clock rate to %u Hz failed!\n", f_spi_req);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000552 }
553 free(spispeed);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000554 bt = serprog_buses_supported;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000555 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
556 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000557 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000558
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000559 if (serprog_buses_supported & BUS_NONSPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000560 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
561 msg_perr("Error: Initialize operation buffer "
562 "not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000563 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000564 }
565
566 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
567 msg_perr("Error: Write to opbuf: "
568 "delay not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000569 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000570 }
571
572 /* S_CMD_O_EXEC availability checked later. */
573
574 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
575 msg_perr("Error: Single byte read not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000576 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000577 }
578 /* This could be translated to single byte reads (if missing),
579 * but now we don't support that. */
580 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
581 msg_perr("Error: Read n bytes not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000582 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000583 }
584 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
585 msg_perr("Error: Write to opbuf: "
586 "write byte not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000587 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000588 }
589
590 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
591 msg_pdbg(MSGHEADER "Write-n not supported");
592 sp_max_write_n = 0;
593 } else {
594 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
595 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
596 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
597 if (!sp_max_write_n) {
598 sp_max_write_n = (1 << 24);
599 }
600 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
601 sp_max_write_n);
602 sp_write_n_buf = malloc(sp_max_write_n);
603 if (!sp_write_n_buf) {
604 msg_perr("Error: cannot allocate memory for "
605 "Write-n buffer\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000606 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000607 }
608 sp_write_n_bytes = 0;
609 }
610
611 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
612 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
613 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
614 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
615 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
616 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
617 sp_max_read_n ? sp_max_read_n : (1 << 24));
618 } else {
619 msg_pdbg(MSGHEADER "Maximum read-n length "
620 "not reported\n");
621 sp_max_read_n = 0;
622 }
623
Urja Rannikkof3196df2009-07-21 13:02:59 +0000624 }
625
626 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000627 msg_pwarn("Warning: NAK to query programmer name\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000628 strcpy((char *)pgmname, "(unknown)");
629 }
630 pgmname[16] = 0;
Stefan Tauner31019d42011-10-22 21:45:27 +0000631 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000632
633 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000634 msg_pwarn("Warning: NAK to query serial buffer size\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000635 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000636 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000637 sp_device_serbuf_size);
638
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000639 if (sp_check_commandavail(S_CMD_O_INIT)) {
640 /* This would be inconsistent. */
641 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
642 msg_perr("Error: Execute operation buffer not "
643 "supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000644 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000645 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000646
647 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
648 msg_perr("Error: NAK to initialize operation buffer\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000649 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000650 }
651
652 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
653 &sp_device_opbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000654 msg_pwarn("Warning: NAK to query operation buffer size\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000655 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000656 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000657 sp_device_opbuf_size);
658 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000659
Stefan Tauner92fefc92012-10-27 00:34:23 +0000660 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
661 uint8_t en = 1;
662 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &en, 0, NULL) != 0) {
663 msg_perr("Error: could not enable output buffers\n");
664 return 1;
665 } else
666 msg_pdbg(MSGHEADER "Output drivers enabled\n");
667 } else
668 msg_pdbg(MSGHEADER "Warning: Programmer does not support toggling its output drivers\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000669 sp_prev_was_write = 0;
670 sp_streamed_transmit_ops = 0;
671 sp_streamed_transmit_bytes = 0;
672 sp_opbuf_usage = 0;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000673 if (serprog_buses_supported & BUS_SPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000674 register_spi_master(&spi_master_serprog);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000675 if (serprog_buses_supported & BUS_NONSPI)
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000676 register_par_master(&par_master_serprog, serprog_buses_supported & BUS_NONSPI);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000677 return 0;
678}
679
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000680/* Move an in flashrom buffer existing write-n operation to the on-device operation buffer. */
681static int sp_pass_writen(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000682{
683 unsigned char header[7];
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000684 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", sp_write_n_bytes, sp_write_n_addr);
685 if (sp_streamed_transmit_bytes >= (7 + sp_write_n_bytes + sp_device_serbuf_size)) {
686 if (sp_flush_stream() != 0) {
687 return 1;
688 }
689 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000690 /* In case it's just a single byte send it as a single write. */
691 if (sp_write_n_bytes == 1) {
692 sp_write_n_bytes = 0;
693 header[0] = (sp_write_n_addr >> 0) & 0xFF;
694 header[1] = (sp_write_n_addr >> 8) & 0xFF;
695 header[2] = (sp_write_n_addr >> 16) & 0xFF;
696 header[3] = sp_write_n_buf[0];
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000697 if (sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header) != 0)
698 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000699 sp_opbuf_usage += 5;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000700 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000701 }
702 header[0] = S_CMD_O_WRITEN;
703 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
704 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
705 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
706 header[4] = (sp_write_n_addr >> 0) & 0xFF;
707 header[5] = (sp_write_n_addr >> 8) & 0xFF;
708 header[6] = (sp_write_n_addr >> 16) & 0xFF;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000709 if (serialport_write(header, 7) != 0) {
710 msg_perr(MSGHEADER "Error: cannot write write-n command\n");
711 return 1;
712 }
713 if (serialport_write(sp_write_n_buf, sp_write_n_bytes) != 0) {
714 msg_perr(MSGHEADER "Error: cannot write write-n data");
715 return 1;
716 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000717 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
718 sp_streamed_transmit_ops += 1;
719 sp_opbuf_usage += 7 + sp_write_n_bytes;
720 sp_write_n_bytes = 0;
721 sp_prev_was_write = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000722 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000723}
724
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000725static int sp_execute_opbuf_noflush(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000726{
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000727 if ((sp_max_write_n) && (sp_write_n_bytes)) {
728 if (sp_pass_writen() != 0) {
729 msg_perr("Error: could not transfer write buffer\n");
730 return 1;
731 }
732 }
733 if (sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL) != 0) {
734 msg_perr("Error: could not execute command buffer\n");
735 return 1;
736 }
737 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", sp_opbuf_usage);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000738 sp_opbuf_usage = 0;
739 sp_prev_was_write = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000740 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000741}
742
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000743static int sp_execute_opbuf(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000744{
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000745 if (sp_execute_opbuf_noflush() != 0)
746 return 1;
747 if (sp_flush_stream() != 0)
748 return 1;
749
750 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000751}
752
David Hendricks8bb20212011-06-14 01:35:36 +0000753static int serprog_shutdown(void *data)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000754{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000755 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000756 if (sp_execute_opbuf() != 0)
757 msg_pwarn("Could not flush command buffer.\n");
Stefan Tauner92fefc92012-10-27 00:34:23 +0000758 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
759 uint8_t dis = 0;
760 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &dis, 0, NULL) == 0)
761 msg_pdbg(MSGHEADER "Output drivers disabled\n");
762 else
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000763 msg_pwarn(MSGHEADER "%s: Warning: could not disable output buffers\n", __func__);
Stefan Tauner92fefc92012-10-27 00:34:23 +0000764 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000765 /* FIXME: fix sockets on windows(?), especially closing */
766 serialport_shutdown(&sp_fd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000767 if (sp_max_write_n)
768 free(sp_write_n_buf);
769 return 0;
770}
771
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000772static int sp_check_opbuf_usage(int bytes_to_be_added)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000773{
774 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000775 /* If this happens in the middle of a page load the page load will probably fail. */
776 msg_pwarn(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
777 if (sp_execute_opbuf() != 0)
778 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000779 }
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000780 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000781}
782
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000783static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
784 chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000785{
Sean Nelson74e8af52010-01-10 01:06:23 +0000786 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000787 if (sp_max_write_n) {
788 if ((sp_prev_was_write)
789 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
790 sp_write_n_buf[sp_write_n_bytes++] = val;
791 } else {
792 if ((sp_prev_was_write) && (sp_write_n_bytes))
793 sp_pass_writen();
794 sp_prev_was_write = 1;
795 sp_write_n_addr = addr;
796 sp_write_n_bytes = 1;
797 sp_write_n_buf[0] = val;
798 }
799 sp_check_opbuf_usage(7 + sp_write_n_bytes);
800 if (sp_write_n_bytes >= sp_max_write_n)
801 sp_pass_writen();
802 } else {
803 /* We will have to do single writeb ops. */
804 unsigned char writeb_parm[4];
805 sp_check_opbuf_usage(6);
806 writeb_parm[0] = (addr >> 0) & 0xFF;
807 writeb_parm[1] = (addr >> 8) & 0xFF;
808 writeb_parm[2] = (addr >> 16) & 0xFF;
809 writeb_parm[3] = val;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000810 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000811 sp_opbuf_usage += 5;
812 }
813}
814
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000815static uint8_t serprog_chip_readb(const struct flashctx *flash,
816 const chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000817{
818 unsigned char c;
819 unsigned char buf[3];
820 /* Will stream the read operation - eg. add it to the stream buffer, *
821 * then flush the buffer, then read the read answer. */
822 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
823 sp_execute_opbuf_noflush();
824 buf[0] = ((addr >> 0) & 0xFF);
825 buf[1] = ((addr >> 8) & 0xFF);
826 buf[2] = ((addr >> 16) & 0xFF);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000827 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); // FIXME: return error
828 sp_flush_stream(); // FIXME: return error
Stefan Tauner79587f52013-04-01 00:45:51 +0000829 if (serialport_read(&c, 1) != 0)
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000830 msg_perr(MSGHEADER "readb byteread"); // FIXME: return error
Stefan Taunerc2333752013-07-13 23:31:37 +0000831 msg_pspew("%s addr=0x%" PRIxPTR " returning 0x%02X\n", __func__, addr, c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000832 return c;
833}
834
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000835/* Local version that really does the job, doesn't care of max_read_n. */
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000836static int sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000837{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000838 unsigned char sbuf[6];
Stefan Tauner0554ca52013-07-25 22:54:25 +0000839 msg_pspew("%s: addr=0x%" PRIxPTR " len=%zu\n", __func__, addr, len);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000840 /* Stream the read-n -- as above. */
841 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
842 sp_execute_opbuf_noflush();
843 sbuf[0] = ((addr >> 0) & 0xFF);
844 sbuf[1] = ((addr >> 8) & 0xFF);
845 sbuf[2] = ((addr >> 16) & 0xFF);
846 sbuf[3] = ((len >> 0) & 0xFF);
847 sbuf[4] = ((len >> 8) & 0xFF);
848 sbuf[5] = ((len >> 16) & 0xFF);
849 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000850 if (sp_flush_stream() != 0)
851 return 1;
852 if (serialport_read(buf, len) != 0) {
853 msg_perr(MSGHEADER "Error: cannot read read-n data");
854 return 1;
855 }
856 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000857}
858
859/* The externally called version that makes sure that max_read_n is obeyed. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000860static void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf,
861 const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000862{
863 size_t lenm = len;
864 chipaddr addrm = addr;
Stefan Tauner31019d42011-10-22 21:45:27 +0000865 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000866 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000867 addrm += sp_max_read_n;
868 lenm -= sp_max_read_n;
869 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000870 if (lenm)
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000871 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000872}
873
Stefan Taunerf80419c2014-05-02 15:41:42 +0000874void serprog_delay(unsigned int usecs)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000875{
876 unsigned char buf[4];
Stefan Tauner31019d42011-10-22 21:45:27 +0000877 msg_pspew("%s usecs=%d\n", __func__, usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000878 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
Stefan Taunerd7d423b2012-10-20 09:13:16 +0000879 msg_pdbg2("serprog_delay used, but programmer doesn't support delays natively - emulating\n");
Stefan Tauner31019d42011-10-22 21:45:27 +0000880 internal_delay(usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000881 return;
882 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000883 if ((sp_max_write_n) && (sp_write_n_bytes))
884 sp_pass_writen();
885 sp_check_opbuf_usage(5);
Stefan Tauner31019d42011-10-22 21:45:27 +0000886 buf[0] = ((usecs >> 0) & 0xFF);
887 buf[1] = ((usecs >> 8) & 0xFF);
888 buf[2] = ((usecs >> 16) & 0xFF);
889 buf[3] = ((usecs >> 24) & 0xFF);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000890 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
891 sp_opbuf_usage += 5;
892 sp_prev_was_write = 0;
893}
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000894
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000895static int serprog_spi_send_command(struct flashctx *flash,
896 unsigned int writecnt, unsigned int readcnt,
897 const unsigned char *writearr,
898 unsigned char *readarr)
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000899{
900 unsigned char *parmbuf;
901 int ret;
902 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000903 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) {
904 if (sp_execute_opbuf() != 0) {
905 msg_perr("Error: could not execute command buffer before sending SPI commands.\n");
906 return 1;
907 }
908 }
909
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000910 parmbuf = malloc(writecnt + 6);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000911 if (!parmbuf) {
912 msg_perr("Error: could not allocate SPI send param buffer.\n");
913 return 1;
914 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000915 parmbuf[0] = (writecnt >> 0) & 0xFF;
916 parmbuf[1] = (writecnt >> 8) & 0xFF;
917 parmbuf[2] = (writecnt >> 16) & 0xFF;
918 parmbuf[3] = (readcnt >> 0) & 0xFF;
919 parmbuf[4] = (readcnt >> 8) & 0xFF;
920 parmbuf[5] = (readcnt >> 16) & 0xFF;
921 memcpy(parmbuf + 6, writearr, writecnt);
922 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
923 readarr);
924 free(parmbuf);
925 return ret;
926}
927
928/* FIXME: This function is optimized so that it does not split each transaction
929 * into chip page_size long blocks unnecessarily like spi_read_chunked. This has
930 * the advantage that it is much faster for most chips, but breaks those with
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000931 * non-continuous reads. When spi_read_chunked is fixed this method can be removed. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000932static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
933 unsigned int start, unsigned int len)
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000934{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000935 unsigned int i, cur_len;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000936 const unsigned int max_read = spi_master_serprog.max_data_read;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000937 for (i = 0; i < len; i += cur_len) {
938 int ret;
939 cur_len = min(max_read, (len - i));
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000940 ret = spi_nbyte_read(flash, start + i, buf + i, cur_len);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000941 if (ret)
942 return ret;
943 }
944 return 0;
945}
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000946
947void *serprog_map(const char *descr, uintptr_t phys_addr, size_t len)
948{
949 /* Serprog transmits 24 bits only and assumes the underlying implementation handles any remaining bits
950 * correctly (usually setting them to one either in software (for FWH/LPC) or relying on the fact that
951 * the hardware observes a subset of the address bits only). Combined with the standard mapping of
952 * flashrom this creates a 16 MB-wide window just below the 4 GB boundary where serprog can operate (as
953 * needed for non-SPI chips). Below we make sure that the requested range is within this window. */
954 if ((phys_addr & 0xFF000000) == 0xFF000000) {
955 return (void*)phys_addr;
956 } else {
957 msg_pwarn(MSGHEADER "requested mapping %s is incompatible: 0x%zx bytes at 0x%0*" PRIxPTR ".\n",
958 descr, len, PRIxPTR_WIDTH, phys_addr);
959 return NULL;
960 }
961}