blob: 237db7d93add3fb6683a1f85610ee5b21ce433a1 [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
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000022#include <stdio.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000023#ifndef _WIN32 /* stuff (presumably) needed for sockets only */
Urja Rannikko22915352009-06-23 11:33:43 +000024#include <stdlib.h>
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000025#include <unistd.h>
Urja Rannikko22915352009-06-23 11:33:43 +000026#include <fcntl.h>
Urja Rannikko22915352009-06-23 11:33:43 +000027#include <sys/socket.h>
28#include <arpa/inet.h>
29#include <netinet/in.h>
30#include <netinet/tcp.h>
31#include <netdb.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000032#endif
33#ifdef _WIN32
34#include <conio.h>
35#else
Urja Rannikko22915352009-06-23 11:33:43 +000036#include <termios.h>
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000037#endif
38#include <string.h>
39#include <errno.h>
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000040#include "flash.h"
41#include "programmer.h"
Urja Rannikkoc93f5f12011-09-15 23:38:14 +000042#include "chipdrivers.h"
Stefan Tauner0d82e952012-10-27 00:34:18 +000043#include "serprog.h"
Urja Rannikkof3196df2009-07-21 13:02:59 +000044
Stefan Tauner31019d42011-10-22 21:45:27 +000045#define MSGHEADER "serprog: "
Urja Rannikkof3196df2009-07-21 13:02:59 +000046
David Hendricks8bb20212011-06-14 01:35:36 +000047/*
48 * FIXME: This prototype was added to help reduce diffs for the shutdown
49 * registration patch, which shifted many lines of code to place
50 * serprog_shutdown() before serprog_init(). It should be removed soon.
51 */
52static int serprog_shutdown(void *data);
53
Urja Rannikkof3196df2009-07-21 13:02:59 +000054static uint16_t sp_device_serbuf_size = 16;
55static uint16_t sp_device_opbuf_size = 300;
56/* Bitmap of supported commands */
57static uint8_t sp_cmdmap[32];
58
Uwe Hermann4e3d0b32010-03-25 23:18:41 +000059/* sp_prev_was_write used to detect writes with contiguous addresses
Urja Rannikkof3196df2009-07-21 13:02:59 +000060 and combine them to write-n's */
61static int sp_prev_was_write = 0;
62/* sp_write_n_addr used as the starting addr of the currently
63 combined write-n operation */
64static uint32_t sp_write_n_addr;
65/* The maximum length of an write_n operation; 0 = write-n not supported */
66static uint32_t sp_max_write_n = 0;
67/* The maximum length of a read_n operation; 0 = 2^24 */
68static uint32_t sp_max_read_n = 0;
69
70/* A malloc'd buffer for combining the operation's data
71 and a counter that tells how much data is there. */
72static uint8_t *sp_write_n_buf;
73static uint32_t sp_write_n_bytes = 0;
74
75/* sp_streamed_* used for flow control checking */
76static int sp_streamed_transmit_ops = 0;
77static int sp_streamed_transmit_bytes = 0;
78
79/* sp_opbuf_usage used for counting the amount of
80 on-device operation buffer used */
81static int sp_opbuf_usage = 0;
82/* if true causes sp_docommand to automatically check
83 whether the command is supported before doing it */
84static int sp_check_avail_automatic = 0;
85
Stefan Tauner52b6e9d2013-04-01 00:46:05 +000086#ifndef WIN32
Urja Rannikkof3196df2009-07-21 13:02:59 +000087static int sp_opensocket(char *ip, unsigned int port)
88{
89 int flag = 1;
90 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +000091 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +000092 int sock;
Sean Nelson74e8af52010-01-10 01:06:23 +000093 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
Urja Rannikkof3196df2009-07-21 13:02:59 +000094 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Niklas Söderlund2a95e872012-07-30 19:42:33 +000095 if (sock < 0) {
96 msg_perr("Error: serprog cannot open socket: %s\n", strerror(errno));
97 return -1;
98 }
Urja Rannikkof3196df2009-07-21 13:02:59 +000099 hostPtr = gethostbyname(ip);
100 if (NULL == hostPtr) {
101 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000102 if (NULL == hostPtr) {
103 msg_perr("Error: cannot resolve %s\n", ip);
104 return -1;
105 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000106 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000107 sp.si.sin_family = AF_INET;
108 sp.si.sin_port = htons(port);
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000109 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr_list[0], hostPtr->h_length);
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000110 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000111 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000112 msg_perr("Error: serprog cannot connect: %s\n", strerror(errno));
113 return -1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000114 }
115 /* We are latency limited, and sometimes do write-write-read *
116 * (write-n) - so enable TCP_NODELAY. */
117 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
118 return sock;
119}
Stefan Tauner52b6e9d2013-04-01 00:46:05 +0000120#endif
Urja Rannikkof3196df2009-07-21 13:02:59 +0000121
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000122/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000123 * always succeeded in) bring the serial protocol to known waiting-for- *
Stefan Taunerae3d8372013-04-01 00:45:45 +0000124 * command state - uses nonblocking I/O - rest of the driver uses *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000125 * blocking read - TODO: add an alarm() timer for the rest of the app on *
126 * serial operations, though not such a big issue as the first thing to *
127 * do is synchronize (eg. check that device is alive). */
Niklas Söderlund7145a502012-09-07 07:07:07 +0000128static int sp_synchronize(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000129{
130 int i;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000131 unsigned char buf[8];
Urja Rannikkof3196df2009-07-21 13:02:59 +0000132 /* First sends 8 NOPs, then flushes the return data - should cause *
133 * the device serial parser to get to a sane state, unless if it *
134 * is waiting for a real long write-n. */
135 memset(buf, S_CMD_NOP, 8);
Stefan Taunerae3d8372013-04-01 00:45:45 +0000136 if (serialport_write_nonblock(buf, 8, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000137 goto err_out;
138 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000139 /* A second should be enough to get all the answers to the buffer */
Stefan Tauner79587f52013-04-01 00:45:51 +0000140 internal_delay(1000 * 1000);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000141 sp_flush_incoming();
142
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000143 /* Then try up to 8 times to send syncnop and get the correct special *
144 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
145 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
146 * attempt, ~1s if immediate success. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000147 for (i = 0; i < 8; i++) {
148 int n;
149 unsigned char c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000150 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
Niklas Söderlund7145a502012-09-07 07:07:07 +0000151 goto err_out;
152 }
Sean Nelson74e8af52010-01-10 01:06:23 +0000153 msg_pdbg(".");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000154 fflush(stdout);
155 for (n = 0; n < 10; n++) {
Stefan Tauner00e16082013-04-01 00:45:38 +0000156 int ret = serialport_read_nonblock(&c, 1, 50, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000157 if (ret < 0)
158 goto err_out;
159 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000160 continue;
Stefan Tauner00e16082013-04-01 00:45:38 +0000161 ret = serialport_read_nonblock(&c, 1, 20, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000162 if (ret < 0)
163 goto err_out;
164 if (ret > 0 || c != S_ACK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000165 continue;
166 c = S_CMD_SYNCNOP;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000167 if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) {
168 goto err_out;
Niklas Söderlund7145a502012-09-07 07:07:07 +0000169 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000170 ret = serialport_read_nonblock(&c, 1, 500, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000171 if (ret < 0)
172 goto err_out;
173 if (ret > 0 || c != S_NAK)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000174 break; /* fail */
Stefan Tauner00e16082013-04-01 00:45:38 +0000175 ret = serialport_read_nonblock(&c, 1, 100, NULL);
Niklas Söderlund7145a502012-09-07 07:07:07 +0000176 if (ret > 0 || ret < 0)
177 goto err_out;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000178 if (c != S_ACK)
179 break; /* fail */
Sean Nelson74e8af52010-01-10 01:06:23 +0000180 msg_pdbg("\n");
Niklas Söderlund7145a502012-09-07 07:07:07 +0000181 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000182 }
183 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000184err_out:
185 msg_perr("Error: cannot synchronize protocol - check communications and reset device?\n");
186 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000187}
188
189static int sp_check_commandavail(uint8_t command)
190{
191 int byteoffs, bitoffs;
192 byteoffs = command / 8;
193 bitoffs = command % 8;
194 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
195}
196
197static int sp_automatic_cmdcheck(uint8_t cmd)
198{
199 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000200 msg_pdbg("Warning: Automatic command availability check failed "
Stefan Taunere34e3e82013-01-01 00:06:51 +0000201 "for cmd 0x%02x - won't execute cmd\n", cmd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000202 return 1;
203 }
204 return 0;
205}
206
207static int sp_docommand(uint8_t command, uint32_t parmlen,
Stefan Tauner31019d42011-10-22 21:45:27 +0000208 uint8_t *params, uint32_t retlen, void *retparms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000209{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000210 unsigned char c;
211 if (sp_automatic_cmdcheck(command))
212 return 1;
Stefan Tauner79587f52013-04-01 00:45:51 +0000213 if (serialport_write(&command, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000214 msg_perr("Error: cannot write op code: %s\n", strerror(errno));
215 return 1;
216 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000217 if (serialport_write(params, parmlen) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000218 msg_perr("Error: cannot write parameters: %s\n", strerror(errno));
219 return 1;
220 }
Stefan Tauner79587f52013-04-01 00:45:51 +0000221 if (serialport_read(&c, 1) != 0) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000222 msg_perr("Error: cannot read from device: %s\n", strerror(errno));
223 return 1;
224 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000225 if (c == S_NAK)
226 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000227 if (c != S_ACK) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000228 msg_perr("Error: invalid response 0x%02X from device\n", c);
229 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000230 }
231 if (retlen) {
Stefan Tauner79587f52013-04-01 00:45:51 +0000232 if (serialport_read(retparms, retlen) != 0) {
233 msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
234 return 1;
235 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000236 }
237 return 0;
238}
239
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000240static int sp_flush_stream(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000241{
242 if (sp_streamed_transmit_ops)
243 do {
244 unsigned char c;
Stefan Tauner79587f52013-04-01 00:45:51 +0000245 if (serialport_read(&c, 1) != 0) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000246 msg_perr("Error: cannot read from device (flushing stream)");
247 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000248 }
249 if (c == S_NAK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000250 msg_perr("Error: NAK to a stream buffer operation\n");
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000251 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000252 }
253 if (c != S_ACK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000254 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000255 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000256 }
257 } while (--sp_streamed_transmit_ops);
258 sp_streamed_transmit_ops = 0;
259 sp_streamed_transmit_bytes = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000260 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000261}
262
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000263static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t *parms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000264{
265 uint8_t *sp;
266 if (sp_automatic_cmdcheck(cmd))
267 return 1;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000268
Urja Rannikkof3196df2009-07-21 13:02:59 +0000269 sp = malloc(1 + parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000270 if (!sp) {
271 msg_perr("Error: cannot malloc command buffer\n");
272 return 1;
273 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000274 sp[0] = cmd;
275 memcpy(&(sp[1]), parms, parmlen);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000276
277 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size)) {
278 if (sp_flush_stream() != 0) {
279 free(sp);
280 return 1;
281 }
282 }
283 if (serialport_write(sp, 1 + parmlen) != 0) {
284 msg_perr("Error: cannot write command\n");
285 free(sp);
286 return 1;
287 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000288 sp_streamed_transmit_ops += 1;
289 sp_streamed_transmit_bytes += 1 + parmlen;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000290
291 free(sp);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000292 return 0;
293}
294
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000295static int serprog_spi_send_command(struct flashctx *flash,
296 unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000297 const unsigned char *writearr,
298 unsigned char *readarr);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000299static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000300 unsigned int start, unsigned int len);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000301static struct spi_programmer spi_programmer_serprog = {
302 .type = SPI_CONTROLLER_SERPROG,
303 .max_data_read = MAX_DATA_READ_UNLIMITED,
304 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
305 .command = serprog_spi_send_command,
306 .multicommand = default_spi_send_multicommand,
307 .read = serprog_spi_read,
308 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000309 .write_aai = default_spi_write_aai,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000310};
311
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000312static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
313 chipaddr addr);
314static uint8_t serprog_chip_readb(const struct flashctx *flash,
315 const chipaddr addr);
316static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
317 const chipaddr addr, size_t len);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000318static const struct par_programmer par_programmer_serprog = {
319 .chip_readb = serprog_chip_readb,
320 .chip_readw = fallback_chip_readw,
321 .chip_readl = fallback_chip_readl,
322 .chip_readn = serprog_chip_readn,
323 .chip_writeb = serprog_chip_writeb,
324 .chip_writew = fallback_chip_writew,
325 .chip_writel = fallback_chip_writel,
326 .chip_writen = fallback_chip_writen,
327};
328
329static enum chipbustype serprog_buses_supported = BUS_NONE;
330
Urja Rannikkof3196df2009-07-21 13:02:59 +0000331int serprog_init(void)
332{
333 uint16_t iface;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000334 unsigned char pgmname[17];
335 unsigned char rbuf[3];
336 unsigned char c;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000337 char *device;
338 char *baudport;
339 int have_device = 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000340
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000341 /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000342 device = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000343 if (device && strlen(device)) {
344 baudport = strstr(device, ":");
345 if (baudport) {
346 /* Split device from baudrate. */
347 *baudport = '\0';
348 baudport++;
349 }
350 if (!baudport || !strlen(baudport)) {
351 msg_perr("Error: No baudrate specified.\n"
352 "Use flashrom -p serprog:dev=/dev/device:baud\n");
353 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000354 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000355 }
356 if (strlen(device)) {
357 sp_fd = sp_openserport(device, atoi(baudport));
Stefan Tauneracfc4c62012-11-30 16:46:45 +0000358 if (sp_fd == SER_INV_FD) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000359 free(device);
360 return 1;
361 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000362 have_device++;
363 }
364 }
365 if (device && !strlen(device)) {
366 msg_perr("Error: No device specified.\n"
367 "Use flashrom -p serprog:dev=/dev/device:baud\n");
368 free(device);
369 return 1;
370 }
371 free(device);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000372
Stefan Tauner52b6e9d2013-04-01 00:46:05 +0000373#ifndef _WIN32
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000374 device = extract_programmer_param("ip");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000375 if (have_device && device) {
376 msg_perr("Error: Both host and device specified.\n"
377 "Please use either dev= or ip= but not both.\n");
378 free(device);
379 return 1;
380 }
381 if (device && strlen(device)) {
382 baudport = strstr(device, ":");
383 if (baudport) {
384 /* Split host from port. */
385 *baudport = '\0';
386 baudport++;
387 }
388 if (!baudport || !strlen(baudport)) {
389 msg_perr("Error: No port specified.\n"
390 "Use flashrom -p serprog:ip=ipaddr:port\n");
391 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000392 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000393 }
394 if (strlen(device)) {
395 sp_fd = sp_opensocket(device, atoi(baudport));
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000396 if (sp_fd < 0) {
397 free(device);
398 return 1;
399 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000400 have_device++;
401 }
402 }
403 if (device && !strlen(device)) {
404 msg_perr("Error: No host specified.\n"
405 "Use flashrom -p serprog:ip=ipaddr:port\n");
406 free(device);
407 return 1;
408 }
409 free(device);
410
411 if (!have_device) {
412 msg_perr("Error: Neither host nor device specified.\n"
413 "Use flashrom -p serprog:dev=/dev/device:baud or "
414 "flashrom -p serprog:ip=ipaddr:port\n");
415 return 1;
416 }
Stefan Tauner52b6e9d2013-04-01 00:46:05 +0000417#endif
Urja Rannikkof3196df2009-07-21 13:02:59 +0000418
David Hendricks8bb20212011-06-14 01:35:36 +0000419 if (register_shutdown(serprog_shutdown, NULL))
420 return 1;
421
Sean Nelson74e8af52010-01-10 01:06:23 +0000422 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000423
424 sp_check_avail_automatic = 0;
425
Niklas Söderlund7145a502012-09-07 07:07:07 +0000426 if (sp_synchronize())
427 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000428
Sean Nelson74e8af52010-01-10 01:06:23 +0000429 msg_pdbg(MSGHEADER "Synchronized\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000430
431 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000432 msg_perr("Error: NAK to query interface version\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000433 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000434 }
435
436 if (iface != 1) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000437 msg_perr("Error: Unknown interface version: %d\n", iface);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000438 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000439 }
440
Sean Nelson74e8af52010-01-10 01:06:23 +0000441 msg_pdbg(MSGHEADER "Interface version ok.\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000442
443 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000444 msg_perr("Error: query command map not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000445 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000446 }
447
448 sp_check_avail_automatic = 1;
449
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000450 /* FIXME: This assumes that serprog device bustypes are always
451 * identical with flashrom bustype enums and that they all fit
452 * in a single byte.
453 */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000454 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000455 msg_pwarn("Warning: NAK to query supported buses\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000456 c = BUS_NONSPI; /* A reasonable default for now. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000457 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000458 serprog_buses_supported = c;
459
Stefan Tauner31019d42011-10-22 21:45:27 +0000460 msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
461 (c & BUS_PARALLEL) ? "on" : "off",
462 (c & BUS_LPC) ? "on" : "off",
463 (c & BUS_FWH) ? "on" : "off",
464 (c & BUS_SPI) ? "on" : "off");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000465 /* Check for the minimum operational set of commands. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000466 if (serprog_buses_supported & BUS_SPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000467 uint8_t bt = BUS_SPI;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000468 char *spispeed;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000469 if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
470 msg_perr("Error: SPI operation not supported while the "
471 "bustype is SPI\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000472 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000473 }
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000474 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
475 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000476 /* Success of any of these commands is optional. We don't need
477 the programmer to tell us its limits, but if it doesn't, we
478 will assume stuff, so it's in the programmers best interest
479 to tell us. */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000480 if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
481 uint32_t v;
482 v = ((unsigned int)(rbuf[0]) << 0);
483 v |= ((unsigned int)(rbuf[1]) << 8);
484 v |= ((unsigned int)(rbuf[2]) << 16);
485 if (v == 0)
486 v = (1 << 24) - 1; /* SPI-op maximum. */
487 spi_programmer_serprog.max_data_write = v;
488 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
489 }
490 if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
491 uint32_t v;
492 v = ((unsigned int)(rbuf[0]) << 0);
493 v |= ((unsigned int)(rbuf[1]) << 8);
494 v |= ((unsigned int)(rbuf[2]) << 16);
495 if (v == 0)
496 v = (1 << 24) - 1; /* SPI-op maximum. */
497 spi_programmer_serprog.max_data_read = v;
498 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
499 }
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000500 spispeed = extract_programmer_param("spispeed");
501 if (spispeed && strlen(spispeed)) {
502 uint32_t f_spi_req, f_spi;
503 uint8_t buf[4];
504 char *f_spi_suffix;
505
506 errno = 0;
507 f_spi_req = strtol(spispeed, &f_spi_suffix, 0);
508 if (errno != 0 || spispeed == f_spi_suffix) {
509 msg_perr("Error: Could not convert 'spispeed'.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000510 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000511 return 1;
512 }
513 if (strlen(f_spi_suffix) == 1) {
514 if (!strcasecmp(f_spi_suffix, "M"))
515 f_spi_req *= 1000000;
516 else if (!strcasecmp(f_spi_suffix, "k"))
517 f_spi_req *= 1000;
518 else {
519 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000520 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000521 return 1;
522 }
523 } else if (strlen(f_spi_suffix) > 1) {
524 msg_perr("Error: Garbage following 'spispeed' value.\n");
Stefan Taunere34e3e82013-01-01 00:06:51 +0000525 free(spispeed);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000526 return 1;
527 }
528
529 buf[0] = (f_spi_req >> (0 * 8)) & 0xFF;
530 buf[1] = (f_spi_req >> (1 * 8)) & 0xFF;
531 buf[2] = (f_spi_req >> (2 * 8)) & 0xFF;
532 buf[3] = (f_spi_req >> (3 * 8)) & 0xFF;
533
534 if (sp_check_commandavail(S_CMD_S_SPI_FREQ) == 0)
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000535 msg_pwarn(MSGHEADER "Warning: Setting the SPI clock rate is not supported!\n");
536 else if (sp_docommand(S_CMD_S_SPI_FREQ, 4, buf, 4, buf) == 0) {
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000537 f_spi = buf[0];
538 f_spi |= buf[1] << (1 * 8);
539 f_spi |= buf[2] << (2 * 8);
540 f_spi |= buf[3] << (3 * 8);
541 msg_pdbg(MSGHEADER "Requested to set SPI clock frequency to %u Hz. "
542 "It was actually set to %u Hz\n", f_spi_req, f_spi);
543 } else
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000544 msg_pwarn(MSGHEADER "Setting SPI clock rate to %u Hz failed!\n", f_spi_req);
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000545 }
546 free(spispeed);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000547 bt = serprog_buses_supported;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000548 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
549 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000550 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000551
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000552 if (serprog_buses_supported & BUS_NONSPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000553 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
554 msg_perr("Error: Initialize operation buffer "
555 "not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000556 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000557 }
558
559 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
560 msg_perr("Error: Write to opbuf: "
561 "delay not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000562 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000563 }
564
565 /* S_CMD_O_EXEC availability checked later. */
566
567 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
568 msg_perr("Error: Single byte read not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000569 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000570 }
571 /* This could be translated to single byte reads (if missing),
572 * but now we don't support that. */
573 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
574 msg_perr("Error: Read n bytes not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000575 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000576 }
577 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
578 msg_perr("Error: Write to opbuf: "
579 "write byte not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000580 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000581 }
582
583 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
584 msg_pdbg(MSGHEADER "Write-n not supported");
585 sp_max_write_n = 0;
586 } else {
587 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
588 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
589 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
590 if (!sp_max_write_n) {
591 sp_max_write_n = (1 << 24);
592 }
593 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
594 sp_max_write_n);
595 sp_write_n_buf = malloc(sp_max_write_n);
596 if (!sp_write_n_buf) {
597 msg_perr("Error: cannot allocate memory for "
598 "Write-n buffer\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000599 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000600 }
601 sp_write_n_bytes = 0;
602 }
603
604 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
605 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
606 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
607 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
608 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
609 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
610 sp_max_read_n ? sp_max_read_n : (1 << 24));
611 } else {
612 msg_pdbg(MSGHEADER "Maximum read-n length "
613 "not reported\n");
614 sp_max_read_n = 0;
615 }
616
Urja Rannikkof3196df2009-07-21 13:02:59 +0000617 }
618
619 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000620 msg_pwarn("Warning: NAK to query programmer name\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000621 strcpy((char *)pgmname, "(unknown)");
622 }
623 pgmname[16] = 0;
Stefan Tauner31019d42011-10-22 21:45:27 +0000624 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000625
626 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000627 msg_pwarn("Warning: NAK to query serial buffer size\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000628 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000629 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000630 sp_device_serbuf_size);
631
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000632 if (sp_check_commandavail(S_CMD_O_INIT)) {
633 /* This would be inconsistent. */
634 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
635 msg_perr("Error: Execute operation buffer not "
636 "supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000637 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000638 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000639
640 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
641 msg_perr("Error: NAK to initialize operation buffer\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000642 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000643 }
644
645 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
646 &sp_device_opbuf_size)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000647 msg_pwarn("Warning: NAK to query operation buffer size\n");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000648 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000649 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000650 sp_device_opbuf_size);
651 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000652
Stefan Tauner92fefc92012-10-27 00:34:23 +0000653 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
654 uint8_t en = 1;
655 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &en, 0, NULL) != 0) {
656 msg_perr("Error: could not enable output buffers\n");
657 return 1;
658 } else
659 msg_pdbg(MSGHEADER "Output drivers enabled\n");
660 } else
661 msg_pdbg(MSGHEADER "Warning: Programmer does not support toggling its output drivers\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000662 sp_prev_was_write = 0;
663 sp_streamed_transmit_ops = 0;
664 sp_streamed_transmit_bytes = 0;
665 sp_opbuf_usage = 0;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000666 if (serprog_buses_supported & BUS_SPI)
667 register_spi_programmer(&spi_programmer_serprog);
668 if (serprog_buses_supported & BUS_NONSPI)
669 register_par_programmer(&par_programmer_serprog,
670 serprog_buses_supported & BUS_NONSPI);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000671 return 0;
672}
673
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000674/* Move an in flashrom buffer existing write-n operation to the on-device operation buffer. */
675static int sp_pass_writen(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000676{
677 unsigned char header[7];
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000678 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n", sp_write_n_bytes, sp_write_n_addr);
679 if (sp_streamed_transmit_bytes >= (7 + sp_write_n_bytes + sp_device_serbuf_size)) {
680 if (sp_flush_stream() != 0) {
681 return 1;
682 }
683 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000684 /* In case it's just a single byte send it as a single write. */
685 if (sp_write_n_bytes == 1) {
686 sp_write_n_bytes = 0;
687 header[0] = (sp_write_n_addr >> 0) & 0xFF;
688 header[1] = (sp_write_n_addr >> 8) & 0xFF;
689 header[2] = (sp_write_n_addr >> 16) & 0xFF;
690 header[3] = sp_write_n_buf[0];
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000691 if (sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header) != 0)
692 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000693 sp_opbuf_usage += 5;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000694 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000695 }
696 header[0] = S_CMD_O_WRITEN;
697 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
698 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
699 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
700 header[4] = (sp_write_n_addr >> 0) & 0xFF;
701 header[5] = (sp_write_n_addr >> 8) & 0xFF;
702 header[6] = (sp_write_n_addr >> 16) & 0xFF;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000703 if (serialport_write(header, 7) != 0) {
704 msg_perr(MSGHEADER "Error: cannot write write-n command\n");
705 return 1;
706 }
707 if (serialport_write(sp_write_n_buf, sp_write_n_bytes) != 0) {
708 msg_perr(MSGHEADER "Error: cannot write write-n data");
709 return 1;
710 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000711 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
712 sp_streamed_transmit_ops += 1;
713 sp_opbuf_usage += 7 + sp_write_n_bytes;
714 sp_write_n_bytes = 0;
715 sp_prev_was_write = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000716 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000717}
718
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000719static int sp_execute_opbuf_noflush(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000720{
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000721 if ((sp_max_write_n) && (sp_write_n_bytes)) {
722 if (sp_pass_writen() != 0) {
723 msg_perr("Error: could not transfer write buffer\n");
724 return 1;
725 }
726 }
727 if (sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL) != 0) {
728 msg_perr("Error: could not execute command buffer\n");
729 return 1;
730 }
731 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n", sp_opbuf_usage);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000732 sp_opbuf_usage = 0;
733 sp_prev_was_write = 0;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000734 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000735}
736
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000737static int sp_execute_opbuf(void)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000738{
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000739 if (sp_execute_opbuf_noflush() != 0)
740 return 1;
741 if (sp_flush_stream() != 0)
742 return 1;
743
744 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000745}
746
David Hendricks8bb20212011-06-14 01:35:36 +0000747static int serprog_shutdown(void *data)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000748{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000749 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000750 if (sp_execute_opbuf() != 0)
751 msg_pwarn("Could not flush command buffer.\n");
Stefan Tauner92fefc92012-10-27 00:34:23 +0000752 if (sp_check_commandavail(S_CMD_S_PIN_STATE)) {
753 uint8_t dis = 0;
754 if (sp_docommand(S_CMD_S_PIN_STATE, 1, &dis, 0, NULL) == 0)
755 msg_pdbg(MSGHEADER "Output drivers disabled\n");
756 else
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000757 msg_pwarn(MSGHEADER "%s: Warning: could not disable output buffers\n", __func__);
Stefan Tauner92fefc92012-10-27 00:34:23 +0000758 }
Niklas Söderlund7145a502012-09-07 07:07:07 +0000759 /* FIXME: fix sockets on windows(?), especially closing */
760 serialport_shutdown(&sp_fd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000761 if (sp_max_write_n)
762 free(sp_write_n_buf);
763 return 0;
764}
765
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000766static int sp_check_opbuf_usage(int bytes_to_be_added)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000767{
768 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000769 /* If this happens in the middle of a page load the page load will probably fail. */
770 msg_pwarn(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
771 if (sp_execute_opbuf() != 0)
772 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000773 }
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000774 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000775}
776
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000777static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
778 chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000779{
Sean Nelson74e8af52010-01-10 01:06:23 +0000780 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000781 if (sp_max_write_n) {
782 if ((sp_prev_was_write)
783 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
784 sp_write_n_buf[sp_write_n_bytes++] = val;
785 } else {
786 if ((sp_prev_was_write) && (sp_write_n_bytes))
787 sp_pass_writen();
788 sp_prev_was_write = 1;
789 sp_write_n_addr = addr;
790 sp_write_n_bytes = 1;
791 sp_write_n_buf[0] = val;
792 }
793 sp_check_opbuf_usage(7 + sp_write_n_bytes);
794 if (sp_write_n_bytes >= sp_max_write_n)
795 sp_pass_writen();
796 } else {
797 /* We will have to do single writeb ops. */
798 unsigned char writeb_parm[4];
799 sp_check_opbuf_usage(6);
800 writeb_parm[0] = (addr >> 0) & 0xFF;
801 writeb_parm[1] = (addr >> 8) & 0xFF;
802 writeb_parm[2] = (addr >> 16) & 0xFF;
803 writeb_parm[3] = val;
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000804 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000805 sp_opbuf_usage += 5;
806 }
807}
808
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000809static uint8_t serprog_chip_readb(const struct flashctx *flash,
810 const chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000811{
812 unsigned char c;
813 unsigned char buf[3];
814 /* Will stream the read operation - eg. add it to the stream buffer, *
815 * then flush the buffer, then read the read answer. */
816 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
817 sp_execute_opbuf_noflush();
818 buf[0] = ((addr >> 0) & 0xFF);
819 buf[1] = ((addr >> 8) & 0xFF);
820 buf[2] = ((addr >> 16) & 0xFF);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000821 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf); // FIXME: return error
822 sp_flush_stream(); // FIXME: return error
Stefan Tauner79587f52013-04-01 00:45:51 +0000823 if (serialport_read(&c, 1) != 0)
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000824 msg_perr(MSGHEADER "readb byteread"); // FIXME: return error
Stefan Taunerc2333752013-07-13 23:31:37 +0000825 msg_pspew("%s addr=0x%" PRIxPTR " returning 0x%02X\n", __func__, addr, c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000826 return c;
827}
828
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000829/* Local version that really does the job, doesn't care of max_read_n. */
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000830static int sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000831{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000832 unsigned char sbuf[6];
Stefan Tauner0554ca52013-07-25 22:54:25 +0000833 msg_pspew("%s: addr=0x%" PRIxPTR " len=%zu\n", __func__, addr, len);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000834 /* Stream the read-n -- as above. */
835 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
836 sp_execute_opbuf_noflush();
837 sbuf[0] = ((addr >> 0) & 0xFF);
838 sbuf[1] = ((addr >> 8) & 0xFF);
839 sbuf[2] = ((addr >> 16) & 0xFF);
840 sbuf[3] = ((len >> 0) & 0xFF);
841 sbuf[4] = ((len >> 8) & 0xFF);
842 sbuf[5] = ((len >> 16) & 0xFF);
843 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000844 if (sp_flush_stream() != 0)
845 return 1;
846 if (serialport_read(buf, len) != 0) {
847 msg_perr(MSGHEADER "Error: cannot read read-n data");
848 return 1;
849 }
850 return 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000851}
852
853/* The externally called version that makes sure that max_read_n is obeyed. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000854static void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf,
855 const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000856{
857 size_t lenm = len;
858 chipaddr addrm = addr;
Stefan Tauner31019d42011-10-22 21:45:27 +0000859 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000860 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000861 addrm += sp_max_read_n;
862 lenm -= sp_max_read_n;
863 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000864 if (lenm)
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000865 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm); // FIXME: return error
Urja Rannikkof3196df2009-07-21 13:02:59 +0000866}
867
Stefan Tauner31019d42011-10-22 21:45:27 +0000868void serprog_delay(int usecs)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000869{
870 unsigned char buf[4];
Stefan Tauner31019d42011-10-22 21:45:27 +0000871 msg_pspew("%s usecs=%d\n", __func__, usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000872 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
Stefan Taunerd7d423b2012-10-20 09:13:16 +0000873 msg_pdbg2("serprog_delay used, but programmer doesn't support delays natively - emulating\n");
Stefan Tauner31019d42011-10-22 21:45:27 +0000874 internal_delay(usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000875 return;
876 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000877 if ((sp_max_write_n) && (sp_write_n_bytes))
878 sp_pass_writen();
879 sp_check_opbuf_usage(5);
Stefan Tauner31019d42011-10-22 21:45:27 +0000880 buf[0] = ((usecs >> 0) & 0xFF);
881 buf[1] = ((usecs >> 8) & 0xFF);
882 buf[2] = ((usecs >> 16) & 0xFF);
883 buf[3] = ((usecs >> 24) & 0xFF);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000884 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
885 sp_opbuf_usage += 5;
886 sp_prev_was_write = 0;
887}
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000888
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000889static int serprog_spi_send_command(struct flashctx *flash,
890 unsigned int writecnt, unsigned int readcnt,
891 const unsigned char *writearr,
892 unsigned char *readarr)
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000893{
894 unsigned char *parmbuf;
895 int ret;
896 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000897 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes)) {
898 if (sp_execute_opbuf() != 0) {
899 msg_perr("Error: could not execute command buffer before sending SPI commands.\n");
900 return 1;
901 }
902 }
903
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000904 parmbuf = malloc(writecnt + 6);
Stefan Taunerbdead0d2013-08-24 02:10:18 +0000905 if (!parmbuf) {
906 msg_perr("Error: could not allocate SPI send param buffer.\n");
907 return 1;
908 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000909 parmbuf[0] = (writecnt >> 0) & 0xFF;
910 parmbuf[1] = (writecnt >> 8) & 0xFF;
911 parmbuf[2] = (writecnt >> 16) & 0xFF;
912 parmbuf[3] = (readcnt >> 0) & 0xFF;
913 parmbuf[4] = (readcnt >> 8) & 0xFF;
914 parmbuf[5] = (readcnt >> 16) & 0xFF;
915 memcpy(parmbuf + 6, writearr, writecnt);
916 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
917 readarr);
918 free(parmbuf);
919 return ret;
920}
921
922/* FIXME: This function is optimized so that it does not split each transaction
923 * into chip page_size long blocks unnecessarily like spi_read_chunked. This has
924 * the advantage that it is much faster for most chips, but breaks those with
Aidan Thorntondb4e87d2013-08-27 18:01:53 +0000925 * non-continuous reads. When spi_read_chunked is fixed this method can be removed. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000926static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
927 unsigned int start, unsigned int len)
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000928{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000929 unsigned int i, cur_len;
930 const unsigned int max_read = spi_programmer_serprog.max_data_read;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000931 for (i = 0; i < len; i += cur_len) {
932 int ret;
933 cur_len = min(max_read, (len - i));
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000934 ret = spi_nbyte_read(flash, start + i, buf + i, cur_len);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000935 if (ret)
936 return ret;
937 }
938 return 0;
939}