blob: 3d855296f515f915d0f61eb8f8b951a7e835449b [file] [log] [blame]
hailfinger37b4fbf2009-06-23 11:33:43 +00001/*
2 * This file is part of the flashrom project.
3 *
stefanct69965b62011-09-15 23:38:14 +00004 * Copyright (C) 2009, 2011 Urja Rannikko <urjaman@gmail.com>
hailfinger37b4fbf2009-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
hailfinger4f45a4f2009-08-12 13:32:56 +000022#include <stdio.h>
hailfinger37b4fbf2009-06-23 11:33:43 +000023#include <stdlib.h>
hailfinger4f45a4f2009-08-12 13:32:56 +000024#include <unistd.h>
hailfinger4f45a4f2009-08-12 13:32:56 +000025#include <string.h>
hailfinger37b4fbf2009-06-23 11:33:43 +000026#include <ctype.h>
27#include <fcntl.h>
hailfinger37b4fbf2009-06-23 11:33:43 +000028#include <sys/socket.h>
29#include <arpa/inet.h>
30#include <netinet/in.h>
31#include <netinet/tcp.h>
32#include <netdb.h>
33#include <sys/stat.h>
34#include <errno.h>
hailfinger37b4fbf2009-06-23 11:33:43 +000035#include <inttypes.h>
36#include <termios.h>
hailfinger428f6852010-07-27 22:41:39 +000037#include "flash.h"
38#include "programmer.h"
stefanct69965b62011-09-15 23:38:14 +000039#include "chipdrivers.h"
hailfingerbacbc8b2009-07-21 13:02:59 +000040
stefanctd9ac2212011-10-22 21:45:27 +000041#define MSGHEADER "serprog: "
hailfingerbacbc8b2009-07-21 13:02:59 +000042
dhendrix0ffc2eb2011-06-14 01:35:36 +000043/*
44 * FIXME: This prototype was added to help reduce diffs for the shutdown
45 * registration patch, which shifted many lines of code to place
46 * serprog_shutdown() before serprog_init(). It should be removed soon.
47 */
David Hendricks93784b42016-08-09 17:00:38 -070048static int serprog_shutdown(void *data);
dhendrix0ffc2eb2011-06-14 01:35:36 +000049
hailfingerbacbc8b2009-07-21 13:02:59 +000050#define S_ACK 0x06
51#define S_NAK 0x15
52#define S_CMD_NOP 0x00 /* No operation */
53#define S_CMD_Q_IFACE 0x01 /* Query interface version */
54#define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */
55#define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */
56#define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */
57#define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */
58#define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */
59#define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */
stefanct371e7e82011-07-07 19:56:58 +000060#define S_CMD_Q_WRNMAXLEN 0x08 /* Query opbuf-write-N maximum length */
hailfingerbacbc8b2009-07-21 13:02:59 +000061#define S_CMD_R_BYTE 0x09 /* Read a single byte */
62#define S_CMD_R_NBYTES 0x0A /* Read n bytes */
63#define S_CMD_O_INIT 0x0B /* Initialize operation buffer */
64#define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */
65#define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */
66#define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */
67#define S_CMD_O_EXEC 0x0F /* Execute operation buffer */
68#define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */
69#define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */
70#define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s). */
stefanct69965b62011-09-15 23:38:14 +000071#define S_CMD_O_SPIOP 0x13 /* Perform SPI operation. */
hailfingerbacbc8b2009-07-21 13:02:59 +000072
hailfingerbacbc8b2009-07-21 13:02:59 +000073static uint16_t sp_device_serbuf_size = 16;
74static uint16_t sp_device_opbuf_size = 300;
75/* Bitmap of supported commands */
76static uint8_t sp_cmdmap[32];
77
uwe3a3ab2f2010-03-25 23:18:41 +000078/* sp_prev_was_write used to detect writes with contiguous addresses
hailfingerbacbc8b2009-07-21 13:02:59 +000079 and combine them to write-n's */
80static int sp_prev_was_write = 0;
81/* sp_write_n_addr used as the starting addr of the currently
82 combined write-n operation */
83static uint32_t sp_write_n_addr;
84/* The maximum length of an write_n operation; 0 = write-n not supported */
85static uint32_t sp_max_write_n = 0;
86/* The maximum length of a read_n operation; 0 = 2^24 */
87static uint32_t sp_max_read_n = 0;
88
89/* A malloc'd buffer for combining the operation's data
90 and a counter that tells how much data is there. */
91static uint8_t *sp_write_n_buf;
92static uint32_t sp_write_n_bytes = 0;
93
94/* sp_streamed_* used for flow control checking */
95static int sp_streamed_transmit_ops = 0;
96static int sp_streamed_transmit_bytes = 0;
97
98/* sp_opbuf_usage used for counting the amount of
99 on-device operation buffer used */
100static int sp_opbuf_usage = 0;
101/* if true causes sp_docommand to automatically check
102 whether the command is supported before doing it */
103static int sp_check_avail_automatic = 0;
104
hailfingerbacbc8b2009-07-21 13:02:59 +0000105static int sp_opensocket(char *ip, unsigned int port)
106{
107 int flag = 1;
108 struct hostent *hostPtr = NULL;
hailfinger3e454f32009-09-05 01:10:23 +0000109 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
hailfingerbacbc8b2009-07-21 13:02:59 +0000110 int sock;
snelson0afd28b2010-01-10 01:06:23 +0000111 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
hailfingerbacbc8b2009-07-21 13:02:59 +0000112 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
113 if (sock < 0)
114 sp_die("Error: serprog cannot open socket");
115 hostPtr = gethostbyname(ip);
116 if (NULL == hostPtr) {
117 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
118 if (NULL == hostPtr)
119 sp_die("Error: cannot resolve");
120 }
hailfinger3e454f32009-09-05 01:10:23 +0000121 sp.si.sin_family = AF_INET;
122 sp.si.sin_port = htons(port);
123 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length);
124 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
hailfingerbacbc8b2009-07-21 13:02:59 +0000125 close(sock);
126 sp_die("Error: serprog cannot connect");
127 }
128 /* We are latency limited, and sometimes do write-write-read *
129 * (write-n) - so enable TCP_NODELAY. */
130 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
131 return sock;
132}
133
hailfingerbacbc8b2009-07-21 13:02:59 +0000134static int sp_sync_read_timeout(int loops)
135{
136 int i;
137 unsigned char c;
138 for (i = 0; i < loops; i++) {
139 ssize_t rv;
140 rv = read(sp_fd, &c, 1);
141 if (rv == 1)
142 return c;
143 if ((rv == -1) && (errno != EAGAIN))
144 sp_die("read");
145 usleep(10 * 1000); /* 10ms units */
146 }
147 return -1;
148}
149
uwe3a3ab2f2010-03-25 23:18:41 +0000150/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
hailfingerbacbc8b2009-07-21 13:02:59 +0000151 * always succeeded in) bring the serial protocol to known waiting-for- *
152 * command state - uses nonblocking read - rest of the driver uses *
153 * blocking read - TODO: add an alarm() timer for the rest of the app on *
154 * serial operations, though not such a big issue as the first thing to *
155 * do is synchronize (eg. check that device is alive). */
156static void sp_synchronize(void)
157{
158 int i;
159 int flags = fcntl(sp_fd, F_GETFL);
160 unsigned char buf[8];
161 flags |= O_NONBLOCK;
162 fcntl(sp_fd, F_SETFL, flags);
163 /* First sends 8 NOPs, then flushes the return data - should cause *
164 * the device serial parser to get to a sane state, unless if it *
165 * is waiting for a real long write-n. */
166 memset(buf, S_CMD_NOP, 8);
167 if (write(sp_fd, buf, 8) != 8)
168 sp_die("flush write");
169 /* A second should be enough to get all the answers to the buffer */
170 usleep(1000 * 1000);
171 sp_flush_incoming();
172
stefanct371e7e82011-07-07 19:56:58 +0000173 /* Then try up to 8 times to send syncnop and get the correct special *
174 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
175 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
176 * attempt, ~1s if immediate success. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000177 for (i = 0; i < 8; i++) {
178 int n;
179 unsigned char c = S_CMD_SYNCNOP;
180 if (write(sp_fd, &c, 1) != 1)
181 sp_die("sync write");
snelson0afd28b2010-01-10 01:06:23 +0000182 msg_pdbg(".");
hailfingerbacbc8b2009-07-21 13:02:59 +0000183 fflush(stdout);
184 for (n = 0; n < 10; n++) {
stefanct371e7e82011-07-07 19:56:58 +0000185 c = sp_sync_read_timeout(5); /* wait up to 50ms */
hailfingerbacbc8b2009-07-21 13:02:59 +0000186 if (c != S_NAK)
187 continue;
188 c = sp_sync_read_timeout(2);
189 if (c != S_ACK)
190 continue;
191 c = S_CMD_SYNCNOP;
192 if (write(sp_fd, &c, 1) != 1)
193 sp_die("sync write");
194 c = sp_sync_read_timeout(50);
195 if (c != S_NAK)
196 break; /* fail */
197 c = sp_sync_read_timeout(10);
198 if (c != S_ACK)
199 break; /* fail */
200 /* Ok, synchronized; back to blocking reads and return. */
201 flags &= ~O_NONBLOCK;
202 fcntl(sp_fd, F_SETFL, flags);
snelson0afd28b2010-01-10 01:06:23 +0000203 msg_pdbg("\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000204 return;
205 }
206 }
stefanctd9ac2212011-10-22 21:45:27 +0000207 msg_perr("Error: cannot synchronize protocol "
hailfingerbacbc8b2009-07-21 13:02:59 +0000208 "- check communications and reset device?\n");
209 exit(1);
210}
211
212static int sp_check_commandavail(uint8_t command)
213{
214 int byteoffs, bitoffs;
215 byteoffs = command / 8;
216 bitoffs = command % 8;
217 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
218}
219
220static int sp_automatic_cmdcheck(uint8_t cmd)
221{
222 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
stefanctd9ac2212011-10-22 21:45:27 +0000223 msg_pdbg("Warning: Automatic command availability check failed "
224 "for cmd 0x%x - won't execute cmd\n", cmd);
hailfingerbacbc8b2009-07-21 13:02:59 +0000225 return 1;
226 }
227 return 0;
228}
229
230static int sp_docommand(uint8_t command, uint32_t parmlen,
stefanctd9ac2212011-10-22 21:45:27 +0000231 uint8_t *params, uint32_t retlen, void *retparms)
hailfingerbacbc8b2009-07-21 13:02:59 +0000232{
hailfingerbacbc8b2009-07-21 13:02:59 +0000233 unsigned char c;
234 if (sp_automatic_cmdcheck(command))
235 return 1;
stefanctd9ac2212011-10-22 21:45:27 +0000236 if (write(sp_fd, &command, 1) != 1)
237 sp_die("Error: cannot write op code");
238 if (write(sp_fd, params, parmlen) != (parmlen))
239 sp_die("Error: cannot write parameters");
hailfingerbacbc8b2009-07-21 13:02:59 +0000240 if (read(sp_fd, &c, 1) != 1)
241 sp_die("Error: cannot read from device");
stefanctd9ac2212011-10-22 21:45:27 +0000242 if (c == S_NAK)
243 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000244 if (c != S_ACK) {
snelson0afd28b2010-01-10 01:06:23 +0000245 msg_perr("Error: invalid response 0x%02X from device\n",c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000246 exit(1);
247 }
248 if (retlen) {
249 int rd_bytes = 0;
250 do {
251 int r;
252 r = read(sp_fd, retparms + rd_bytes,
253 retlen - rd_bytes);
stefanctd9ac2212011-10-22 21:45:27 +0000254 if (r <= 0)
255 sp_die("Error: cannot read return parameters");
hailfingerbacbc8b2009-07-21 13:02:59 +0000256 rd_bytes += r;
257 } while (rd_bytes != retlen);
258 }
259 return 0;
260}
261
262static void sp_flush_stream(void)
263{
264 if (sp_streamed_transmit_ops)
265 do {
266 unsigned char c;
267 if (read(sp_fd, &c, 1) != 1) {
snelson0afd28b2010-01-10 01:06:23 +0000268 sp_die("Error: cannot read from device (flushing stream)");
hailfingerbacbc8b2009-07-21 13:02:59 +0000269 }
270 if (c == S_NAK) {
snelson0afd28b2010-01-10 01:06:23 +0000271 msg_perr("Error: NAK to a stream buffer operation\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000272 exit(1);
273 }
274 if (c != S_ACK) {
snelson0afd28b2010-01-10 01:06:23 +0000275 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000276 exit(1);
277 }
278 } while (--sp_streamed_transmit_ops);
279 sp_streamed_transmit_ops = 0;
280 sp_streamed_transmit_bytes = 0;
281}
282
283static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
284{
285 uint8_t *sp;
286 if (sp_automatic_cmdcheck(cmd))
287 return 1;
288 sp = malloc(1 + parmlen);
289 if (!sp) sp_die("Error: cannot malloc command buffer");
290 sp[0] = cmd;
291 memcpy(&(sp[1]), parms, parmlen);
292 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
293 sp_flush_stream();
294 if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
295 sp_die("Error: cannot write command");
296 free(sp);
297 sp_streamed_transmit_ops += 1;
298 sp_streamed_transmit_bytes += 1 + parmlen;
299 return 0;
300}
301
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700302static int serprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
hailfinger76bb7e92011-11-09 23:40:00 +0000303 const unsigned char *writearr,
304 unsigned char *readarr);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700305static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000306 unsigned int start, unsigned int len);
stefanct69965b62011-09-15 23:38:14 +0000307static struct spi_programmer spi_programmer_serprog = {
308 .type = SPI_CONTROLLER_SERPROG,
309 .max_data_read = MAX_DATA_READ_UNLIMITED,
310 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
311 .command = serprog_spi_send_command,
312 .multicommand = default_spi_send_multicommand,
313 .read = serprog_spi_read,
314 .write_256 = default_spi_write_256,
315};
316
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700317static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
318 chipaddr addr);
319static uint8_t serprog_chip_readb(const struct flashctx *flash,
320 const chipaddr addr);
321static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
322 const chipaddr addr, size_t len);
323
hailfinger76bb7e92011-11-09 23:40:00 +0000324static const struct par_programmer par_programmer_serprog = {
325 .chip_readb = serprog_chip_readb,
326 .chip_readw = fallback_chip_readw,
327 .chip_readl = fallback_chip_readl,
328 .chip_readn = serprog_chip_readn,
329 .chip_writeb = serprog_chip_writeb,
330 .chip_writew = fallback_chip_writew,
331 .chip_writel = fallback_chip_writel,
332 .chip_writen = fallback_chip_writen,
333};
334
335static enum chipbustype serprog_buses_supported = BUS_NONE;
336
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700337int serprog_init(struct flashctx *flash)
hailfingerbacbc8b2009-07-21 13:02:59 +0000338{
339 uint16_t iface;
hailfingerbacbc8b2009-07-21 13:02:59 +0000340 unsigned char pgmname[17];
341 unsigned char rbuf[3];
342 unsigned char c;
hailfinger1ef766d2010-07-06 09:55:48 +0000343 char *device;
344 char *baudport;
345 int have_device = 0;
hailfingerbacbc8b2009-07-21 13:02:59 +0000346
hailfinger1ef766d2010-07-06 09:55:48 +0000347 /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */
hailfingerddeb4ac2010-07-08 10:13:37 +0000348 device = extract_programmer_param("dev");
hailfinger1ef766d2010-07-06 09:55:48 +0000349 if (device && strlen(device)) {
350 baudport = strstr(device, ":");
351 if (baudport) {
352 /* Split device from baudrate. */
353 *baudport = '\0';
354 baudport++;
355 }
356 if (!baudport || !strlen(baudport)) {
357 msg_perr("Error: No baudrate specified.\n"
358 "Use flashrom -p serprog:dev=/dev/device:baud\n");
359 free(device);
stefanct69965b62011-09-15 23:38:14 +0000360 return 1;
hailfinger1ef766d2010-07-06 09:55:48 +0000361 }
362 if (strlen(device)) {
363 sp_fd = sp_openserport(device, atoi(baudport));
364 have_device++;
365 }
366 }
367 if (device && !strlen(device)) {
368 msg_perr("Error: No device specified.\n"
369 "Use flashrom -p serprog:dev=/dev/device:baud\n");
370 free(device);
371 return 1;
372 }
373 free(device);
hailfingerbacbc8b2009-07-21 13:02:59 +0000374
hailfingerddeb4ac2010-07-08 10:13:37 +0000375 device = extract_programmer_param("ip");
hailfinger1ef766d2010-07-06 09:55:48 +0000376 if (have_device && device) {
377 msg_perr("Error: Both host and device specified.\n"
378 "Please use either dev= or ip= but not both.\n");
379 free(device);
380 return 1;
381 }
382 if (device && strlen(device)) {
383 baudport = strstr(device, ":");
384 if (baudport) {
385 /* Split host from port. */
386 *baudport = '\0';
387 baudport++;
388 }
389 if (!baudport || !strlen(baudport)) {
390 msg_perr("Error: No port specified.\n"
391 "Use flashrom -p serprog:ip=ipaddr:port\n");
392 free(device);
stefanct69965b62011-09-15 23:38:14 +0000393 return 1;
hailfinger1ef766d2010-07-06 09:55:48 +0000394 }
395 if (strlen(device)) {
396 sp_fd = sp_opensocket(device, atoi(baudport));
397 have_device++;
398 }
399 }
400 if (device && !strlen(device)) {
401 msg_perr("Error: No host specified.\n"
402 "Use flashrom -p serprog:ip=ipaddr:port\n");
403 free(device);
404 return 1;
405 }
406 free(device);
407
408 if (!have_device) {
409 msg_perr("Error: Neither host nor device specified.\n"
410 "Use flashrom -p serprog:dev=/dev/device:baud or "
411 "flashrom -p serprog:ip=ipaddr:port\n");
412 return 1;
413 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000414
dhendrix0ffc2eb2011-06-14 01:35:36 +0000415 if (register_shutdown(serprog_shutdown, NULL))
416 return 1;
417
snelson0afd28b2010-01-10 01:06:23 +0000418 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000419
420 sp_check_avail_automatic = 0;
421
422 sp_synchronize();
423
snelson0afd28b2010-01-10 01:06:23 +0000424 msg_pdbg(MSGHEADER "Synchronized\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000425
426 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
stefanctd9ac2212011-10-22 21:45:27 +0000427 msg_perr("Error: NAK to query interface version\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000428 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000429 }
430
431 if (iface != 1) {
stefanctd9ac2212011-10-22 21:45:27 +0000432 msg_perr("Error: Unknown interface version: %d\n", iface);
hailfinger76bb7e92011-11-09 23:40:00 +0000433 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000434 }
435
snelson0afd28b2010-01-10 01:06:23 +0000436 msg_pdbg(MSGHEADER "Interface version ok.\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000437
438 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
snelson0afd28b2010-01-10 01:06:23 +0000439 msg_perr("Error: query command map not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000440 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000441 }
442
443 sp_check_avail_automatic = 1;
444
hailfinger76bb7e92011-11-09 23:40:00 +0000445 /* FIXME: This assumes that serprog device bustypes are always
446 * identical with flashrom bustype enums and that they all fit
447 * in a single byte.
448 */
stefanct69965b62011-09-15 23:38:14 +0000449 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
450 msg_perr("Warning: NAK to query supported buses\n");
451 c = BUS_NONSPI; /* A reasonable default for now. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000452 }
hailfinger76bb7e92011-11-09 23:40:00 +0000453 serprog_buses_supported = c;
454
stefanctd9ac2212011-10-22 21:45:27 +0000455 msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
456 (c & BUS_PARALLEL) ? "on" : "off",
457 (c & BUS_LPC) ? "on" : "off",
458 (c & BUS_FWH) ? "on" : "off",
459 (c & BUS_SPI) ? "on" : "off");
stefanct69965b62011-09-15 23:38:14 +0000460 /* Check for the minimum operational set of commands. */
hailfinger76bb7e92011-11-09 23:40:00 +0000461 if (serprog_buses_supported & BUS_SPI) {
stefanct69965b62011-09-15 23:38:14 +0000462 uint8_t bt = BUS_SPI;
463 if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
464 msg_perr("Error: SPI operation not supported while the "
465 "bustype is SPI\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000466 return 1;
stefanct69965b62011-09-15 23:38:14 +0000467 }
468 /* Success of any of these commands is optional. We don't need
469 the programmer to tell us its limits, but if it doesn't, we
470 will assume stuff, so it's in the programmers best interest
471 to tell us. */
472 sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
473 if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
474 uint32_t v;
475 v = ((unsigned int)(rbuf[0]) << 0);
476 v |= ((unsigned int)(rbuf[1]) << 8);
477 v |= ((unsigned int)(rbuf[2]) << 16);
478 if (v == 0)
479 v = (1 << 24) - 1; /* SPI-op maximum. */
480 spi_programmer_serprog.max_data_write = v;
481 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
482 }
483 if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
484 uint32_t v;
485 v = ((unsigned int)(rbuf[0]) << 0);
486 v |= ((unsigned int)(rbuf[1]) << 8);
487 v |= ((unsigned int)(rbuf[2]) << 16);
488 if (v == 0)
489 v = (1 << 24) - 1; /* SPI-op maximum. */
490 spi_programmer_serprog.max_data_read = v;
491 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
492 }
hailfinger76bb7e92011-11-09 23:40:00 +0000493 bt = serprog_buses_supported;
stefanct69965b62011-09-15 23:38:14 +0000494 sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
hailfingerbacbc8b2009-07-21 13:02:59 +0000495 }
stefanct69965b62011-09-15 23:38:14 +0000496
hailfinger76bb7e92011-11-09 23:40:00 +0000497 if (serprog_buses_supported & BUS_NONSPI) {
stefanct69965b62011-09-15 23:38:14 +0000498 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
499 msg_perr("Error: Initialize operation buffer "
500 "not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000501 return 1;
stefanct69965b62011-09-15 23:38:14 +0000502 }
503
504 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
505 msg_perr("Error: Write to opbuf: "
506 "delay not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000507 return 1;
stefanct69965b62011-09-15 23:38:14 +0000508 }
509
510 /* S_CMD_O_EXEC availability checked later. */
511
512 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
513 msg_perr("Error: Single byte read not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000514 return 1;
stefanct69965b62011-09-15 23:38:14 +0000515 }
516 /* This could be translated to single byte reads (if missing),
517 * but now we don't support that. */
518 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
519 msg_perr("Error: Read n bytes not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000520 return 1;
stefanct69965b62011-09-15 23:38:14 +0000521 }
522 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
523 msg_perr("Error: Write to opbuf: "
524 "write byte not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000525 return 1;
stefanct69965b62011-09-15 23:38:14 +0000526 }
527
528 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
529 msg_pdbg(MSGHEADER "Write-n not supported");
530 sp_max_write_n = 0;
531 } else {
532 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
533 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
534 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
535 if (!sp_max_write_n) {
536 sp_max_write_n = (1 << 24);
537 }
538 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
539 sp_max_write_n);
540 sp_write_n_buf = malloc(sp_max_write_n);
541 if (!sp_write_n_buf) {
542 msg_perr("Error: cannot allocate memory for "
543 "Write-n buffer\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000544 return 1;
stefanct69965b62011-09-15 23:38:14 +0000545 }
546 sp_write_n_bytes = 0;
547 }
548
549 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
550 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
551 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
552 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
553 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
554 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
555 sp_max_read_n ? sp_max_read_n : (1 << 24));
556 } else {
557 msg_pdbg(MSGHEADER "Maximum read-n length "
558 "not reported\n");
559 sp_max_read_n = 0;
560 }
561
hailfingerbacbc8b2009-07-21 13:02:59 +0000562 }
563
564 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
snelson0afd28b2010-01-10 01:06:23 +0000565 msg_perr("Warning: NAK to query programmer name\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000566 strcpy((char *)pgmname, "(unknown)");
567 }
568 pgmname[16] = 0;
stefanctd9ac2212011-10-22 21:45:27 +0000569 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
hailfingerbacbc8b2009-07-21 13:02:59 +0000570
571 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
snelson0afd28b2010-01-10 01:06:23 +0000572 msg_perr("Warning: NAK to query serial buffer size\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000573 }
stefanctd9ac2212011-10-22 21:45:27 +0000574 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
hailfingerbacbc8b2009-07-21 13:02:59 +0000575 sp_device_serbuf_size);
576
stefanct69965b62011-09-15 23:38:14 +0000577 if (sp_check_commandavail(S_CMD_O_INIT)) {
578 /* This would be inconsistent. */
579 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
580 msg_perr("Error: Execute operation buffer not "
581 "supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000582 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000583 }
stefanct69965b62011-09-15 23:38:14 +0000584
585 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
586 msg_perr("Error: NAK to initialize operation buffer\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000587 return 1;
stefanct69965b62011-09-15 23:38:14 +0000588 }
589
590 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
591 &sp_device_opbuf_size)) {
592 msg_perr("Warning: NAK to query operation buffer "
593 "size\n");
594 }
stefanctd9ac2212011-10-22 21:45:27 +0000595 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
stefanct69965b62011-09-15 23:38:14 +0000596 sp_device_opbuf_size);
597 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000598
599 sp_prev_was_write = 0;
600 sp_streamed_transmit_ops = 0;
601 sp_streamed_transmit_bytes = 0;
602 sp_opbuf_usage = 0;
hailfinger76bb7e92011-11-09 23:40:00 +0000603 if (serprog_buses_supported & BUS_SPI)
604 register_spi_programmer(&spi_programmer_serprog);
605 if (serprog_buses_supported & BUS_NONSPI)
606 register_par_programmer(&par_programmer_serprog,
607 serprog_buses_supported & BUS_NONSPI);
hailfingerbacbc8b2009-07-21 13:02:59 +0000608 return 0;
609}
610
611/* Move an in flashrom buffer existing write-n operation to *
612 * the on-device operation buffer. */
613static void sp_pass_writen(void)
614{
615 unsigned char header[7];
snelson0afd28b2010-01-10 01:06:23 +0000616 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
stefanctd9ac2212011-10-22 21:45:27 +0000617 sp_write_n_bytes, sp_write_n_addr);
hailfingerbacbc8b2009-07-21 13:02:59 +0000618 if (sp_streamed_transmit_bytes >=
619 (7 + sp_write_n_bytes + sp_device_serbuf_size))
620 sp_flush_stream();
621 /* In case it's just a single byte send it as a single write. */
622 if (sp_write_n_bytes == 1) {
623 sp_write_n_bytes = 0;
624 header[0] = (sp_write_n_addr >> 0) & 0xFF;
625 header[1] = (sp_write_n_addr >> 8) & 0xFF;
626 header[2] = (sp_write_n_addr >> 16) & 0xFF;
627 header[3] = sp_write_n_buf[0];
628 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
629 sp_opbuf_usage += 5;
630 return;
631 }
632 header[0] = S_CMD_O_WRITEN;
633 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
634 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
635 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
636 header[4] = (sp_write_n_addr >> 0) & 0xFF;
637 header[5] = (sp_write_n_addr >> 8) & 0xFF;
638 header[6] = (sp_write_n_addr >> 16) & 0xFF;
639 if (write(sp_fd, header, 7) != 7)
640 sp_die("Error: cannot write write-n command\n");
641 if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
642 sp_write_n_bytes)
643 sp_die("Error: cannot write write-n data");
644 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
645 sp_streamed_transmit_ops += 1;
646 sp_opbuf_usage += 7 + sp_write_n_bytes;
647 sp_write_n_bytes = 0;
648 sp_prev_was_write = 0;
649}
650
651static void sp_execute_opbuf_noflush(void)
652{
653 if ((sp_max_write_n) && (sp_write_n_bytes))
654 sp_pass_writen();
stepand0d220f2011-01-24 19:15:51 +0000655 sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL);
snelson0afd28b2010-01-10 01:06:23 +0000656 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n",
hailfingerbacbc8b2009-07-21 13:02:59 +0000657 sp_opbuf_usage);
658 sp_opbuf_usage = 0;
659 sp_prev_was_write = 0;
660 return;
661}
662
663static void sp_execute_opbuf(void)
664{
665 sp_execute_opbuf_noflush();
666 sp_flush_stream();
667}
668
David Hendricks93784b42016-08-09 17:00:38 -0700669static int serprog_shutdown(void *data)
hailfingerbacbc8b2009-07-21 13:02:59 +0000670{
snelson0afd28b2010-01-10 01:06:23 +0000671 msg_pspew("%s\n", __func__);
hailfingerbacbc8b2009-07-21 13:02:59 +0000672 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
673 sp_execute_opbuf();
674 close(sp_fd);
675 if (sp_max_write_n)
676 free(sp_write_n_buf);
677 return 0;
678}
679
680static void sp_check_opbuf_usage(int bytes_to_be_added)
681{
682 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
683 sp_execute_opbuf();
684 /* If this happens in the mid of an page load the page load *
uwe3a3ab2f2010-03-25 23:18:41 +0000685 * will probably fail. */
snelson0afd28b2010-01-10 01:06:23 +0000686 msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000687 }
688}
689
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700690void serprog_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
hailfingerbacbc8b2009-07-21 13:02:59 +0000691{
snelson0afd28b2010-01-10 01:06:23 +0000692 msg_pspew("%s\n", __func__);
hailfingerbacbc8b2009-07-21 13:02:59 +0000693 if (sp_max_write_n) {
694 if ((sp_prev_was_write)
695 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
696 sp_write_n_buf[sp_write_n_bytes++] = val;
697 } else {
698 if ((sp_prev_was_write) && (sp_write_n_bytes))
699 sp_pass_writen();
700 sp_prev_was_write = 1;
701 sp_write_n_addr = addr;
702 sp_write_n_bytes = 1;
703 sp_write_n_buf[0] = val;
704 }
705 sp_check_opbuf_usage(7 + sp_write_n_bytes);
706 if (sp_write_n_bytes >= sp_max_write_n)
707 sp_pass_writen();
708 } else {
709 /* We will have to do single writeb ops. */
710 unsigned char writeb_parm[4];
711 sp_check_opbuf_usage(6);
712 writeb_parm[0] = (addr >> 0) & 0xFF;
713 writeb_parm[1] = (addr >> 8) & 0xFF;
714 writeb_parm[2] = (addr >> 16) & 0xFF;
715 writeb_parm[3] = val;
716 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
717 sp_opbuf_usage += 5;
718 }
719}
720
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700721uint8_t serprog_chip_readb(const struct flashctx *flash, const chipaddr addr)
hailfingerbacbc8b2009-07-21 13:02:59 +0000722{
723 unsigned char c;
724 unsigned char buf[3];
725 /* Will stream the read operation - eg. add it to the stream buffer, *
726 * then flush the buffer, then read the read answer. */
727 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
728 sp_execute_opbuf_noflush();
729 buf[0] = ((addr >> 0) & 0xFF);
730 buf[1] = ((addr >> 8) & 0xFF);
731 buf[2] = ((addr >> 16) & 0xFF);
732 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
733 sp_flush_stream();
734 if (read(sp_fd, &c, 1) != 1)
735 sp_die("readb byteread");
snelson0afd28b2010-01-10 01:06:23 +0000736 msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000737 return c;
738}
739
uwe3a3ab2f2010-03-25 23:18:41 +0000740/* Local version that really does the job, doesn't care of max_read_n. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000741static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
742{
743 int rd_bytes = 0;
744 unsigned char sbuf[6];
snelson0afd28b2010-01-10 01:06:23 +0000745 msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
hailfingerbacbc8b2009-07-21 13:02:59 +0000746 /* Stream the read-n -- as above. */
747 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
748 sp_execute_opbuf_noflush();
749 sbuf[0] = ((addr >> 0) & 0xFF);
750 sbuf[1] = ((addr >> 8) & 0xFF);
751 sbuf[2] = ((addr >> 16) & 0xFF);
752 sbuf[3] = ((len >> 0) & 0xFF);
753 sbuf[4] = ((len >> 8) & 0xFF);
754 sbuf[5] = ((len >> 16) & 0xFF);
755 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
756 sp_flush_stream();
757 do {
758 int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
759 if (r <= 0)
760 sp_die("Error: cannot read read-n data");
761 rd_bytes += r;
762 } while (rd_bytes != len);
763 return;
764}
765
766/* The externally called version that makes sure that max_read_n is obeyed. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700767void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf, const chipaddr addr, size_t len)
hailfingerbacbc8b2009-07-21 13:02:59 +0000768{
769 size_t lenm = len;
770 chipaddr addrm = addr;
stefanctd9ac2212011-10-22 21:45:27 +0000771 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
772 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n);
hailfingerbacbc8b2009-07-21 13:02:59 +0000773 addrm += sp_max_read_n;
774 lenm -= sp_max_read_n;
775 }
stefanctd9ac2212011-10-22 21:45:27 +0000776 if (lenm)
777 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm);
hailfingerbacbc8b2009-07-21 13:02:59 +0000778}
779
stefanctd9ac2212011-10-22 21:45:27 +0000780void serprog_delay(int usecs)
hailfingerbacbc8b2009-07-21 13:02:59 +0000781{
782 unsigned char buf[4];
stefanctd9ac2212011-10-22 21:45:27 +0000783 msg_pspew("%s usecs=%d\n", __func__, usecs);
stefanct69965b62011-09-15 23:38:14 +0000784 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
stefanct69965b62011-09-15 23:38:14 +0000785 msg_pdbg("Note: serprog_delay used, but the programmer doesn't "
786 "support delay\n");
stefanctd9ac2212011-10-22 21:45:27 +0000787 internal_delay(usecs);
stefanct69965b62011-09-15 23:38:14 +0000788 return;
789 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000790 if ((sp_max_write_n) && (sp_write_n_bytes))
791 sp_pass_writen();
792 sp_check_opbuf_usage(5);
stefanctd9ac2212011-10-22 21:45:27 +0000793 buf[0] = ((usecs >> 0) & 0xFF);
794 buf[1] = ((usecs >> 8) & 0xFF);
795 buf[2] = ((usecs >> 16) & 0xFF);
796 buf[3] = ((usecs >> 24) & 0xFF);
hailfingerbacbc8b2009-07-21 13:02:59 +0000797 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
798 sp_opbuf_usage += 5;
799 sp_prev_was_write = 0;
800}
stefanct69965b62011-09-15 23:38:14 +0000801
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700802static int serprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
stefanct69965b62011-09-15 23:38:14 +0000803 const unsigned char *writearr,
804 unsigned char *readarr)
805{
806 unsigned char *parmbuf;
807 int ret;
808 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
809 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
810 sp_execute_opbuf();
811 parmbuf = malloc(writecnt + 6);
812 if (!parmbuf)
813 sp_die("Error: cannot malloc SPI send param buffer");
814 parmbuf[0] = (writecnt >> 0) & 0xFF;
815 parmbuf[1] = (writecnt >> 8) & 0xFF;
816 parmbuf[2] = (writecnt >> 16) & 0xFF;
817 parmbuf[3] = (readcnt >> 0) & 0xFF;
818 parmbuf[4] = (readcnt >> 8) & 0xFF;
819 parmbuf[5] = (readcnt >> 16) & 0xFF;
820 memcpy(parmbuf + 6, writearr, writecnt);
821 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
822 readarr);
823 free(parmbuf);
824 return ret;
825}
826
827/* FIXME: This function is optimized so that it does not split each transaction
828 * into chip page_size long blocks unnecessarily like spi_read_chunked. This has
829 * the advantage that it is much faster for most chips, but breaks those with
830 * non-contiguous address space (like AT45DB161D). When spi_read_chunked is
831 * fixed this method can be removed. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700832static int serprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
stefanct69965b62011-09-15 23:38:14 +0000833{
stefanctc5eb8a92011-11-23 09:13:48 +0000834 unsigned int i, cur_len;
835 const unsigned int max_read = spi_programmer_serprog.max_data_read;
stefanct69965b62011-09-15 23:38:14 +0000836 for (i = 0; i < len; i += cur_len) {
837 int ret;
838 cur_len = min(max_read, (len - i));
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700839 ret = spi_nbyte_read(flash, start + i, buf + i, cur_len);
stefanct69965b62011-09-15 23:38:14 +0000840 if (ret)
841 return ret;
842 }
843 return 0;
844}