blob: a8352a81db64dce90d67f97867a2f65277f6d8b3 [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 *
hailfinger37b4fbf2009-06-23 11:33:43 +000017 */
18
hailfinger4f45a4f2009-08-12 13:32:56 +000019#include <stdio.h>
hailfinger37b4fbf2009-06-23 11:33:43 +000020#include <stdlib.h>
hailfinger4f45a4f2009-08-12 13:32:56 +000021#include <unistd.h>
hailfinger4f45a4f2009-08-12 13:32:56 +000022#include <string.h>
hailfinger37b4fbf2009-06-23 11:33:43 +000023#include <ctype.h>
24#include <fcntl.h>
hailfinger37b4fbf2009-06-23 11:33:43 +000025#include <sys/socket.h>
26#include <arpa/inet.h>
27#include <netinet/in.h>
28#include <netinet/tcp.h>
29#include <netdb.h>
30#include <sys/stat.h>
31#include <errno.h>
hailfinger37b4fbf2009-06-23 11:33:43 +000032#include <inttypes.h>
33#include <termios.h>
hailfinger428f6852010-07-27 22:41:39 +000034#include "flash.h"
35#include "programmer.h"
stefanct69965b62011-09-15 23:38:14 +000036#include "chipdrivers.h"
Patrick Georgib9fe7f12017-04-11 20:35:19 +020037#include "serprog.h"
hailfingerbacbc8b2009-07-21 13:02:59 +000038
stefanctd9ac2212011-10-22 21:45:27 +000039#define MSGHEADER "serprog: "
hailfingerbacbc8b2009-07-21 13:02:59 +000040
dhendrix0ffc2eb2011-06-14 01:35:36 +000041/*
42 * FIXME: This prototype was added to help reduce diffs for the shutdown
43 * registration patch, which shifted many lines of code to place
44 * serprog_shutdown() before serprog_init(). It should be removed soon.
45 */
David Hendricks93784b42016-08-09 17:00:38 -070046static int serprog_shutdown(void *data);
dhendrix0ffc2eb2011-06-14 01:35:36 +000047
hailfingerbacbc8b2009-07-21 13:02:59 +000048static uint16_t sp_device_serbuf_size = 16;
49static uint16_t sp_device_opbuf_size = 300;
50/* Bitmap of supported commands */
51static uint8_t sp_cmdmap[32];
52
uwe3a3ab2f2010-03-25 23:18:41 +000053/* sp_prev_was_write used to detect writes with contiguous addresses
hailfingerbacbc8b2009-07-21 13:02:59 +000054 and combine them to write-n's */
55static int sp_prev_was_write = 0;
56/* sp_write_n_addr used as the starting addr of the currently
57 combined write-n operation */
58static uint32_t sp_write_n_addr;
59/* The maximum length of an write_n operation; 0 = write-n not supported */
60static uint32_t sp_max_write_n = 0;
61/* The maximum length of a read_n operation; 0 = 2^24 */
62static uint32_t sp_max_read_n = 0;
63
64/* A malloc'd buffer for combining the operation's data
65 and a counter that tells how much data is there. */
66static uint8_t *sp_write_n_buf;
67static uint32_t sp_write_n_bytes = 0;
68
69/* sp_streamed_* used for flow control checking */
70static int sp_streamed_transmit_ops = 0;
71static int sp_streamed_transmit_bytes = 0;
72
73/* sp_opbuf_usage used for counting the amount of
74 on-device operation buffer used */
75static int sp_opbuf_usage = 0;
76/* if true causes sp_docommand to automatically check
77 whether the command is supported before doing it */
78static int sp_check_avail_automatic = 0;
79
hailfingerbacbc8b2009-07-21 13:02:59 +000080static int sp_opensocket(char *ip, unsigned int port)
81{
82 int flag = 1;
83 struct hostent *hostPtr = NULL;
hailfinger3e454f32009-09-05 01:10:23 +000084 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
hailfingerbacbc8b2009-07-21 13:02:59 +000085 int sock;
snelson0afd28b2010-01-10 01:06:23 +000086 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
hailfingerbacbc8b2009-07-21 13:02:59 +000087 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
88 if (sock < 0)
89 sp_die("Error: serprog cannot open socket");
90 hostPtr = gethostbyname(ip);
91 if (NULL == hostPtr) {
92 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
93 if (NULL == hostPtr)
94 sp_die("Error: cannot resolve");
95 }
hailfinger3e454f32009-09-05 01:10:23 +000096 sp.si.sin_family = AF_INET;
97 sp.si.sin_port = htons(port);
98 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length);
99 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
hailfingerbacbc8b2009-07-21 13:02:59 +0000100 close(sock);
101 sp_die("Error: serprog cannot connect");
102 }
103 /* We are latency limited, and sometimes do write-write-read *
104 * (write-n) - so enable TCP_NODELAY. */
105 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
106 return sock;
107}
108
hailfingerbacbc8b2009-07-21 13:02:59 +0000109static int sp_sync_read_timeout(int loops)
110{
111 int i;
112 unsigned char c;
113 for (i = 0; i < loops; i++) {
114 ssize_t rv;
115 rv = read(sp_fd, &c, 1);
116 if (rv == 1)
117 return c;
118 if ((rv == -1) && (errno != EAGAIN))
119 sp_die("read");
120 usleep(10 * 1000); /* 10ms units */
121 }
122 return -1;
123}
124
uwe3a3ab2f2010-03-25 23:18:41 +0000125/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
hailfingerbacbc8b2009-07-21 13:02:59 +0000126 * always succeeded in) bring the serial protocol to known waiting-for- *
127 * command state - uses nonblocking read - rest of the driver uses *
128 * blocking read - TODO: add an alarm() timer for the rest of the app on *
129 * serial operations, though not such a big issue as the first thing to *
130 * do is synchronize (eg. check that device is alive). */
131static void sp_synchronize(void)
132{
133 int i;
134 int flags = fcntl(sp_fd, F_GETFL);
135 unsigned char buf[8];
136 flags |= O_NONBLOCK;
137 fcntl(sp_fd, F_SETFL, flags);
138 /* First sends 8 NOPs, then flushes the return data - should cause *
139 * the device serial parser to get to a sane state, unless if it *
140 * is waiting for a real long write-n. */
141 memset(buf, S_CMD_NOP, 8);
142 if (write(sp_fd, buf, 8) != 8)
143 sp_die("flush write");
144 /* A second should be enough to get all the answers to the buffer */
145 usleep(1000 * 1000);
146 sp_flush_incoming();
147
stefanct371e7e82011-07-07 19:56:58 +0000148 /* Then try up to 8 times to send syncnop and get the correct special *
149 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
150 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
151 * attempt, ~1s if immediate success. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000152 for (i = 0; i < 8; i++) {
153 int n;
154 unsigned char c = S_CMD_SYNCNOP;
155 if (write(sp_fd, &c, 1) != 1)
156 sp_die("sync write");
snelson0afd28b2010-01-10 01:06:23 +0000157 msg_pdbg(".");
hailfingerbacbc8b2009-07-21 13:02:59 +0000158 fflush(stdout);
159 for (n = 0; n < 10; n++) {
stefanct371e7e82011-07-07 19:56:58 +0000160 c = sp_sync_read_timeout(5); /* wait up to 50ms */
hailfingerbacbc8b2009-07-21 13:02:59 +0000161 if (c != S_NAK)
162 continue;
163 c = sp_sync_read_timeout(2);
164 if (c != S_ACK)
165 continue;
166 c = S_CMD_SYNCNOP;
167 if (write(sp_fd, &c, 1) != 1)
168 sp_die("sync write");
169 c = sp_sync_read_timeout(50);
170 if (c != S_NAK)
171 break; /* fail */
172 c = sp_sync_read_timeout(10);
173 if (c != S_ACK)
174 break; /* fail */
175 /* Ok, synchronized; back to blocking reads and return. */
176 flags &= ~O_NONBLOCK;
177 fcntl(sp_fd, F_SETFL, flags);
snelson0afd28b2010-01-10 01:06:23 +0000178 msg_pdbg("\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000179 return;
180 }
181 }
stefanctd9ac2212011-10-22 21:45:27 +0000182 msg_perr("Error: cannot synchronize protocol "
hailfingerbacbc8b2009-07-21 13:02:59 +0000183 "- check communications and reset device?\n");
184 exit(1);
185}
186
187static int sp_check_commandavail(uint8_t command)
188{
189 int byteoffs, bitoffs;
190 byteoffs = command / 8;
191 bitoffs = command % 8;
192 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
193}
194
195static int sp_automatic_cmdcheck(uint8_t cmd)
196{
197 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
stefanctd9ac2212011-10-22 21:45:27 +0000198 msg_pdbg("Warning: Automatic command availability check failed "
199 "for cmd 0x%x - won't execute cmd\n", cmd);
hailfingerbacbc8b2009-07-21 13:02:59 +0000200 return 1;
201 }
202 return 0;
203}
204
205static int sp_docommand(uint8_t command, uint32_t parmlen,
stefanctd9ac2212011-10-22 21:45:27 +0000206 uint8_t *params, uint32_t retlen, void *retparms)
hailfingerbacbc8b2009-07-21 13:02:59 +0000207{
hailfingerbacbc8b2009-07-21 13:02:59 +0000208 unsigned char c;
209 if (sp_automatic_cmdcheck(command))
210 return 1;
stefanctd9ac2212011-10-22 21:45:27 +0000211 if (write(sp_fd, &command, 1) != 1)
212 sp_die("Error: cannot write op code");
213 if (write(sp_fd, params, parmlen) != (parmlen))
214 sp_die("Error: cannot write parameters");
hailfingerbacbc8b2009-07-21 13:02:59 +0000215 if (read(sp_fd, &c, 1) != 1)
216 sp_die("Error: cannot read from device");
stefanctd9ac2212011-10-22 21:45:27 +0000217 if (c == S_NAK)
218 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000219 if (c != S_ACK) {
snelson0afd28b2010-01-10 01:06:23 +0000220 msg_perr("Error: invalid response 0x%02X from device\n",c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000221 exit(1);
222 }
223 if (retlen) {
224 int rd_bytes = 0;
225 do {
226 int r;
227 r = read(sp_fd, retparms + rd_bytes,
228 retlen - rd_bytes);
stefanctd9ac2212011-10-22 21:45:27 +0000229 if (r <= 0)
230 sp_die("Error: cannot read return parameters");
hailfingerbacbc8b2009-07-21 13:02:59 +0000231 rd_bytes += r;
232 } while (rd_bytes != retlen);
233 }
234 return 0;
235}
236
237static void sp_flush_stream(void)
238{
239 if (sp_streamed_transmit_ops)
240 do {
241 unsigned char c;
242 if (read(sp_fd, &c, 1) != 1) {
snelson0afd28b2010-01-10 01:06:23 +0000243 sp_die("Error: cannot read from device (flushing stream)");
hailfingerbacbc8b2009-07-21 13:02:59 +0000244 }
245 if (c == S_NAK) {
snelson0afd28b2010-01-10 01:06:23 +0000246 msg_perr("Error: NAK to a stream buffer operation\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000247 exit(1);
248 }
249 if (c != S_ACK) {
snelson0afd28b2010-01-10 01:06:23 +0000250 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000251 exit(1);
252 }
253 } while (--sp_streamed_transmit_ops);
254 sp_streamed_transmit_ops = 0;
255 sp_streamed_transmit_bytes = 0;
256}
257
258static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
259{
260 uint8_t *sp;
261 if (sp_automatic_cmdcheck(cmd))
262 return 1;
263 sp = malloc(1 + parmlen);
264 if (!sp) sp_die("Error: cannot malloc command buffer");
265 sp[0] = cmd;
266 memcpy(&(sp[1]), parms, parmlen);
267 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
268 sp_flush_stream();
269 if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
270 sp_die("Error: cannot write command");
271 free(sp);
272 sp_streamed_transmit_ops += 1;
273 sp_streamed_transmit_bytes += 1 + parmlen;
274 return 0;
275}
276
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700277static int serprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
hailfinger76bb7e92011-11-09 23:40:00 +0000278 const unsigned char *writearr,
279 unsigned char *readarr);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700280static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000281 unsigned int start, unsigned int len);
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100282static struct spi_master spi_master_serprog = {
stefanct69965b62011-09-15 23:38:14 +0000283 .type = SPI_CONTROLLER_SERPROG,
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000284 .features = SPI_MASTER_4BA,
stefanct69965b62011-09-15 23:38:14 +0000285 .max_data_read = MAX_DATA_READ_UNLIMITED,
286 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
287 .command = serprog_spi_send_command,
288 .multicommand = default_spi_send_multicommand,
289 .read = serprog_spi_read,
290 .write_256 = default_spi_write_256,
291};
292
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700293static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
294 chipaddr addr);
295static uint8_t serprog_chip_readb(const struct flashctx *flash,
296 const chipaddr addr);
297static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
298 const chipaddr addr, size_t len);
299
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100300static const struct par_master par_master_serprog = {
hailfinger76bb7e92011-11-09 23:40:00 +0000301 .chip_readb = serprog_chip_readb,
302 .chip_readw = fallback_chip_readw,
303 .chip_readl = fallback_chip_readl,
304 .chip_readn = serprog_chip_readn,
305 .chip_writeb = serprog_chip_writeb,
306 .chip_writew = fallback_chip_writew,
307 .chip_writel = fallback_chip_writel,
308 .chip_writen = fallback_chip_writen,
309};
310
311static enum chipbustype serprog_buses_supported = BUS_NONE;
312
David Hendricksac1d25c2016-08-09 17:00:58 -0700313int serprog_init(void)
hailfingerbacbc8b2009-07-21 13:02:59 +0000314{
315 uint16_t iface;
hailfingerbacbc8b2009-07-21 13:02:59 +0000316 unsigned char pgmname[17];
317 unsigned char rbuf[3];
318 unsigned char c;
hailfinger1ef766d2010-07-06 09:55:48 +0000319 char *device;
320 char *baudport;
321 int have_device = 0;
hailfingerbacbc8b2009-07-21 13:02:59 +0000322
hailfinger1ef766d2010-07-06 09:55:48 +0000323 /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */
hailfingerddeb4ac2010-07-08 10:13:37 +0000324 device = extract_programmer_param("dev");
hailfinger1ef766d2010-07-06 09:55:48 +0000325 if (device && strlen(device)) {
326 baudport = strstr(device, ":");
327 if (baudport) {
328 /* Split device from baudrate. */
329 *baudport = '\0';
330 baudport++;
331 }
332 if (!baudport || !strlen(baudport)) {
333 msg_perr("Error: No baudrate specified.\n"
334 "Use flashrom -p serprog:dev=/dev/device:baud\n");
335 free(device);
stefanct69965b62011-09-15 23:38:14 +0000336 return 1;
hailfinger1ef766d2010-07-06 09:55:48 +0000337 }
338 if (strlen(device)) {
339 sp_fd = sp_openserport(device, atoi(baudport));
340 have_device++;
341 }
342 }
343 if (device && !strlen(device)) {
344 msg_perr("Error: No device specified.\n"
345 "Use flashrom -p serprog:dev=/dev/device:baud\n");
346 free(device);
347 return 1;
348 }
349 free(device);
hailfingerbacbc8b2009-07-21 13:02:59 +0000350
hailfingerddeb4ac2010-07-08 10:13:37 +0000351 device = extract_programmer_param("ip");
hailfinger1ef766d2010-07-06 09:55:48 +0000352 if (have_device && device) {
353 msg_perr("Error: Both host and device specified.\n"
354 "Please use either dev= or ip= but not both.\n");
355 free(device);
356 return 1;
357 }
358 if (device && strlen(device)) {
359 baudport = strstr(device, ":");
360 if (baudport) {
361 /* Split host from port. */
362 *baudport = '\0';
363 baudport++;
364 }
365 if (!baudport || !strlen(baudport)) {
366 msg_perr("Error: No port specified.\n"
367 "Use flashrom -p serprog:ip=ipaddr:port\n");
368 free(device);
stefanct69965b62011-09-15 23:38:14 +0000369 return 1;
hailfinger1ef766d2010-07-06 09:55:48 +0000370 }
371 if (strlen(device)) {
372 sp_fd = sp_opensocket(device, atoi(baudport));
373 have_device++;
374 }
375 }
376 if (device && !strlen(device)) {
377 msg_perr("Error: No host specified.\n"
378 "Use flashrom -p serprog:ip=ipaddr:port\n");
379 free(device);
380 return 1;
381 }
382 free(device);
383
384 if (!have_device) {
385 msg_perr("Error: Neither host nor device specified.\n"
386 "Use flashrom -p serprog:dev=/dev/device:baud or "
387 "flashrom -p serprog:ip=ipaddr:port\n");
388 return 1;
389 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000390
dhendrix0ffc2eb2011-06-14 01:35:36 +0000391 if (register_shutdown(serprog_shutdown, NULL))
392 return 1;
393
snelson0afd28b2010-01-10 01:06:23 +0000394 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000395
396 sp_check_avail_automatic = 0;
397
398 sp_synchronize();
399
snelson0afd28b2010-01-10 01:06:23 +0000400 msg_pdbg(MSGHEADER "Synchronized\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000401
402 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
stefanctd9ac2212011-10-22 21:45:27 +0000403 msg_perr("Error: NAK to query interface version\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000404 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000405 }
406
407 if (iface != 1) {
stefanctd9ac2212011-10-22 21:45:27 +0000408 msg_perr("Error: Unknown interface version: %d\n", iface);
hailfinger76bb7e92011-11-09 23:40:00 +0000409 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000410 }
411
snelson0afd28b2010-01-10 01:06:23 +0000412 msg_pdbg(MSGHEADER "Interface version ok.\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000413
414 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
snelson0afd28b2010-01-10 01:06:23 +0000415 msg_perr("Error: query command map not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000416 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000417 }
418
419 sp_check_avail_automatic = 1;
420
hailfinger76bb7e92011-11-09 23:40:00 +0000421 /* FIXME: This assumes that serprog device bustypes are always
422 * identical with flashrom bustype enums and that they all fit
423 * in a single byte.
424 */
stefanct69965b62011-09-15 23:38:14 +0000425 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
426 msg_perr("Warning: NAK to query supported buses\n");
427 c = BUS_NONSPI; /* A reasonable default for now. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000428 }
hailfinger76bb7e92011-11-09 23:40:00 +0000429 serprog_buses_supported = c;
430
stefanctd9ac2212011-10-22 21:45:27 +0000431 msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
432 (c & BUS_PARALLEL) ? "on" : "off",
433 (c & BUS_LPC) ? "on" : "off",
434 (c & BUS_FWH) ? "on" : "off",
435 (c & BUS_SPI) ? "on" : "off");
stefanct69965b62011-09-15 23:38:14 +0000436 /* Check for the minimum operational set of commands. */
hailfinger76bb7e92011-11-09 23:40:00 +0000437 if (serprog_buses_supported & BUS_SPI) {
stefanct69965b62011-09-15 23:38:14 +0000438 uint8_t bt = BUS_SPI;
439 if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
440 msg_perr("Error: SPI operation not supported while the "
441 "bustype is SPI\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000442 return 1;
stefanct69965b62011-09-15 23:38:14 +0000443 }
444 /* Success of any of these commands is optional. We don't need
445 the programmer to tell us its limits, but if it doesn't, we
446 will assume stuff, so it's in the programmers best interest
447 to tell us. */
448 sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
449 if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
450 uint32_t v;
451 v = ((unsigned int)(rbuf[0]) << 0);
452 v |= ((unsigned int)(rbuf[1]) << 8);
453 v |= ((unsigned int)(rbuf[2]) << 16);
454 if (v == 0)
455 v = (1 << 24) - 1; /* SPI-op maximum. */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100456 spi_master_serprog.max_data_write = v;
stefanct69965b62011-09-15 23:38:14 +0000457 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
458 }
459 if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
460 uint32_t v;
461 v = ((unsigned int)(rbuf[0]) << 0);
462 v |= ((unsigned int)(rbuf[1]) << 8);
463 v |= ((unsigned int)(rbuf[2]) << 16);
464 if (v == 0)
465 v = (1 << 24) - 1; /* SPI-op maximum. */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100466 spi_master_serprog.max_data_read = v;
stefanct69965b62011-09-15 23:38:14 +0000467 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
468 }
hailfinger76bb7e92011-11-09 23:40:00 +0000469 bt = serprog_buses_supported;
stefanct69965b62011-09-15 23:38:14 +0000470 sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
hailfingerbacbc8b2009-07-21 13:02:59 +0000471 }
stefanct69965b62011-09-15 23:38:14 +0000472
hailfinger76bb7e92011-11-09 23:40:00 +0000473 if (serprog_buses_supported & BUS_NONSPI) {
stefanct69965b62011-09-15 23:38:14 +0000474 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
475 msg_perr("Error: Initialize operation buffer "
476 "not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000477 return 1;
stefanct69965b62011-09-15 23:38:14 +0000478 }
479
480 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
481 msg_perr("Error: Write to opbuf: "
482 "delay not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000483 return 1;
stefanct69965b62011-09-15 23:38:14 +0000484 }
485
486 /* S_CMD_O_EXEC availability checked later. */
487
488 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
489 msg_perr("Error: Single byte read not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000490 return 1;
stefanct69965b62011-09-15 23:38:14 +0000491 }
492 /* This could be translated to single byte reads (if missing),
493 * but now we don't support that. */
494 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
495 msg_perr("Error: Read n bytes not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000496 return 1;
stefanct69965b62011-09-15 23:38:14 +0000497 }
498 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
499 msg_perr("Error: Write to opbuf: "
500 "write byte not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000501 return 1;
stefanct69965b62011-09-15 23:38:14 +0000502 }
503
504 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
505 msg_pdbg(MSGHEADER "Write-n not supported");
506 sp_max_write_n = 0;
507 } else {
508 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
509 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
510 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
511 if (!sp_max_write_n) {
512 sp_max_write_n = (1 << 24);
513 }
514 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
515 sp_max_write_n);
516 sp_write_n_buf = malloc(sp_max_write_n);
517 if (!sp_write_n_buf) {
518 msg_perr("Error: cannot allocate memory for "
519 "Write-n buffer\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000520 return 1;
stefanct69965b62011-09-15 23:38:14 +0000521 }
522 sp_write_n_bytes = 0;
523 }
524
525 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
526 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
527 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
528 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
529 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
530 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
531 sp_max_read_n ? sp_max_read_n : (1 << 24));
532 } else {
533 msg_pdbg(MSGHEADER "Maximum read-n length "
534 "not reported\n");
535 sp_max_read_n = 0;
536 }
537
hailfingerbacbc8b2009-07-21 13:02:59 +0000538 }
539
540 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
snelson0afd28b2010-01-10 01:06:23 +0000541 msg_perr("Warning: NAK to query programmer name\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000542 strcpy((char *)pgmname, "(unknown)");
543 }
544 pgmname[16] = 0;
stefanctd9ac2212011-10-22 21:45:27 +0000545 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
hailfingerbacbc8b2009-07-21 13:02:59 +0000546
547 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
snelson0afd28b2010-01-10 01:06:23 +0000548 msg_perr("Warning: NAK to query serial buffer size\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000549 }
stefanctd9ac2212011-10-22 21:45:27 +0000550 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
hailfingerbacbc8b2009-07-21 13:02:59 +0000551 sp_device_serbuf_size);
552
stefanct69965b62011-09-15 23:38:14 +0000553 if (sp_check_commandavail(S_CMD_O_INIT)) {
554 /* This would be inconsistent. */
555 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
556 msg_perr("Error: Execute operation buffer not "
557 "supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000558 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000559 }
stefanct69965b62011-09-15 23:38:14 +0000560
561 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
562 msg_perr("Error: NAK to initialize operation buffer\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000563 return 1;
stefanct69965b62011-09-15 23:38:14 +0000564 }
565
566 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
567 &sp_device_opbuf_size)) {
568 msg_perr("Warning: NAK to query operation buffer "
569 "size\n");
570 }
stefanctd9ac2212011-10-22 21:45:27 +0000571 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
stefanct69965b62011-09-15 23:38:14 +0000572 sp_device_opbuf_size);
573 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000574
575 sp_prev_was_write = 0;
576 sp_streamed_transmit_ops = 0;
577 sp_streamed_transmit_bytes = 0;
578 sp_opbuf_usage = 0;
hailfinger76bb7e92011-11-09 23:40:00 +0000579 if (serprog_buses_supported & BUS_SPI)
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100580 register_spi_master(&spi_master_serprog);
hailfinger76bb7e92011-11-09 23:40:00 +0000581 if (serprog_buses_supported & BUS_NONSPI)
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100582 register_par_master(&par_master_serprog,
hailfinger76bb7e92011-11-09 23:40:00 +0000583 serprog_buses_supported & BUS_NONSPI);
hailfingerbacbc8b2009-07-21 13:02:59 +0000584 return 0;
585}
586
587/* Move an in flashrom buffer existing write-n operation to *
588 * the on-device operation buffer. */
589static void sp_pass_writen(void)
590{
591 unsigned char header[7];
snelson0afd28b2010-01-10 01:06:23 +0000592 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
stefanctd9ac2212011-10-22 21:45:27 +0000593 sp_write_n_bytes, sp_write_n_addr);
hailfingerbacbc8b2009-07-21 13:02:59 +0000594 if (sp_streamed_transmit_bytes >=
595 (7 + sp_write_n_bytes + sp_device_serbuf_size))
596 sp_flush_stream();
597 /* In case it's just a single byte send it as a single write. */
598 if (sp_write_n_bytes == 1) {
599 sp_write_n_bytes = 0;
600 header[0] = (sp_write_n_addr >> 0) & 0xFF;
601 header[1] = (sp_write_n_addr >> 8) & 0xFF;
602 header[2] = (sp_write_n_addr >> 16) & 0xFF;
603 header[3] = sp_write_n_buf[0];
604 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
605 sp_opbuf_usage += 5;
606 return;
607 }
608 header[0] = S_CMD_O_WRITEN;
609 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
610 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
611 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
612 header[4] = (sp_write_n_addr >> 0) & 0xFF;
613 header[5] = (sp_write_n_addr >> 8) & 0xFF;
614 header[6] = (sp_write_n_addr >> 16) & 0xFF;
615 if (write(sp_fd, header, 7) != 7)
616 sp_die("Error: cannot write write-n command\n");
617 if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
618 sp_write_n_bytes)
619 sp_die("Error: cannot write write-n data");
620 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
621 sp_streamed_transmit_ops += 1;
622 sp_opbuf_usage += 7 + sp_write_n_bytes;
623 sp_write_n_bytes = 0;
624 sp_prev_was_write = 0;
625}
626
627static void sp_execute_opbuf_noflush(void)
628{
629 if ((sp_max_write_n) && (sp_write_n_bytes))
630 sp_pass_writen();
stepand0d220f2011-01-24 19:15:51 +0000631 sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL);
snelson0afd28b2010-01-10 01:06:23 +0000632 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n",
hailfingerbacbc8b2009-07-21 13:02:59 +0000633 sp_opbuf_usage);
634 sp_opbuf_usage = 0;
635 sp_prev_was_write = 0;
636 return;
637}
638
639static void sp_execute_opbuf(void)
640{
641 sp_execute_opbuf_noflush();
642 sp_flush_stream();
643}
644
David Hendricks93784b42016-08-09 17:00:38 -0700645static int serprog_shutdown(void *data)
hailfingerbacbc8b2009-07-21 13:02:59 +0000646{
snelson0afd28b2010-01-10 01:06:23 +0000647 msg_pspew("%s\n", __func__);
hailfingerbacbc8b2009-07-21 13:02:59 +0000648 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
649 sp_execute_opbuf();
650 close(sp_fd);
651 if (sp_max_write_n)
652 free(sp_write_n_buf);
653 return 0;
654}
655
656static void sp_check_opbuf_usage(int bytes_to_be_added)
657{
658 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
659 sp_execute_opbuf();
660 /* If this happens in the mid of an page load the page load *
uwe3a3ab2f2010-03-25 23:18:41 +0000661 * will probably fail. */
snelson0afd28b2010-01-10 01:06:23 +0000662 msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000663 }
664}
665
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700666void serprog_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
hailfingerbacbc8b2009-07-21 13:02:59 +0000667{
snelson0afd28b2010-01-10 01:06:23 +0000668 msg_pspew("%s\n", __func__);
hailfingerbacbc8b2009-07-21 13:02:59 +0000669 if (sp_max_write_n) {
670 if ((sp_prev_was_write)
671 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
672 sp_write_n_buf[sp_write_n_bytes++] = val;
673 } else {
674 if ((sp_prev_was_write) && (sp_write_n_bytes))
675 sp_pass_writen();
676 sp_prev_was_write = 1;
677 sp_write_n_addr = addr;
678 sp_write_n_bytes = 1;
679 sp_write_n_buf[0] = val;
680 }
681 sp_check_opbuf_usage(7 + sp_write_n_bytes);
682 if (sp_write_n_bytes >= sp_max_write_n)
683 sp_pass_writen();
684 } else {
685 /* We will have to do single writeb ops. */
686 unsigned char writeb_parm[4];
687 sp_check_opbuf_usage(6);
688 writeb_parm[0] = (addr >> 0) & 0xFF;
689 writeb_parm[1] = (addr >> 8) & 0xFF;
690 writeb_parm[2] = (addr >> 16) & 0xFF;
691 writeb_parm[3] = val;
692 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
693 sp_opbuf_usage += 5;
694 }
695}
696
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700697uint8_t serprog_chip_readb(const struct flashctx *flash, const chipaddr addr)
hailfingerbacbc8b2009-07-21 13:02:59 +0000698{
699 unsigned char c;
700 unsigned char buf[3];
701 /* Will stream the read operation - eg. add it to the stream buffer, *
702 * then flush the buffer, then read the read answer. */
703 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
704 sp_execute_opbuf_noflush();
705 buf[0] = ((addr >> 0) & 0xFF);
706 buf[1] = ((addr >> 8) & 0xFF);
707 buf[2] = ((addr >> 16) & 0xFF);
708 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
709 sp_flush_stream();
710 if (read(sp_fd, &c, 1) != 1)
711 sp_die("readb byteread");
snelson0afd28b2010-01-10 01:06:23 +0000712 msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000713 return c;
714}
715
uwe3a3ab2f2010-03-25 23:18:41 +0000716/* Local version that really does the job, doesn't care of max_read_n. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000717static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
718{
719 int rd_bytes = 0;
720 unsigned char sbuf[6];
snelson0afd28b2010-01-10 01:06:23 +0000721 msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
hailfingerbacbc8b2009-07-21 13:02:59 +0000722 /* Stream the read-n -- as above. */
723 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
724 sp_execute_opbuf_noflush();
725 sbuf[0] = ((addr >> 0) & 0xFF);
726 sbuf[1] = ((addr >> 8) & 0xFF);
727 sbuf[2] = ((addr >> 16) & 0xFF);
728 sbuf[3] = ((len >> 0) & 0xFF);
729 sbuf[4] = ((len >> 8) & 0xFF);
730 sbuf[5] = ((len >> 16) & 0xFF);
731 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
732 sp_flush_stream();
733 do {
734 int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
735 if (r <= 0)
736 sp_die("Error: cannot read read-n data");
737 rd_bytes += r;
738 } while (rd_bytes != len);
739 return;
740}
741
742/* The externally called version that makes sure that max_read_n is obeyed. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700743void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf, const chipaddr addr, size_t len)
hailfingerbacbc8b2009-07-21 13:02:59 +0000744{
745 size_t lenm = len;
746 chipaddr addrm = addr;
stefanctd9ac2212011-10-22 21:45:27 +0000747 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
748 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n);
hailfingerbacbc8b2009-07-21 13:02:59 +0000749 addrm += sp_max_read_n;
750 lenm -= sp_max_read_n;
751 }
stefanctd9ac2212011-10-22 21:45:27 +0000752 if (lenm)
753 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm);
hailfingerbacbc8b2009-07-21 13:02:59 +0000754}
755
stefanctd9ac2212011-10-22 21:45:27 +0000756void serprog_delay(int usecs)
hailfingerbacbc8b2009-07-21 13:02:59 +0000757{
758 unsigned char buf[4];
stefanctd9ac2212011-10-22 21:45:27 +0000759 msg_pspew("%s usecs=%d\n", __func__, usecs);
stefanct69965b62011-09-15 23:38:14 +0000760 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
stefanct69965b62011-09-15 23:38:14 +0000761 msg_pdbg("Note: serprog_delay used, but the programmer doesn't "
762 "support delay\n");
stefanctd9ac2212011-10-22 21:45:27 +0000763 internal_delay(usecs);
stefanct69965b62011-09-15 23:38:14 +0000764 return;
765 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000766 if ((sp_max_write_n) && (sp_write_n_bytes))
767 sp_pass_writen();
768 sp_check_opbuf_usage(5);
stefanctd9ac2212011-10-22 21:45:27 +0000769 buf[0] = ((usecs >> 0) & 0xFF);
770 buf[1] = ((usecs >> 8) & 0xFF);
771 buf[2] = ((usecs >> 16) & 0xFF);
772 buf[3] = ((usecs >> 24) & 0xFF);
hailfingerbacbc8b2009-07-21 13:02:59 +0000773 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
774 sp_opbuf_usage += 5;
775 sp_prev_was_write = 0;
776}
stefanct69965b62011-09-15 23:38:14 +0000777
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700778static int serprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
stefanct69965b62011-09-15 23:38:14 +0000779 const unsigned char *writearr,
780 unsigned char *readarr)
781{
782 unsigned char *parmbuf;
783 int ret;
784 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
785 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
786 sp_execute_opbuf();
787 parmbuf = malloc(writecnt + 6);
788 if (!parmbuf)
789 sp_die("Error: cannot malloc SPI send param buffer");
790 parmbuf[0] = (writecnt >> 0) & 0xFF;
791 parmbuf[1] = (writecnt >> 8) & 0xFF;
792 parmbuf[2] = (writecnt >> 16) & 0xFF;
793 parmbuf[3] = (readcnt >> 0) & 0xFF;
794 parmbuf[4] = (readcnt >> 8) & 0xFF;
795 parmbuf[5] = (readcnt >> 16) & 0xFF;
796 memcpy(parmbuf + 6, writearr, writecnt);
797 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
798 readarr);
799 free(parmbuf);
800 return ret;
801}
802
803/* FIXME: This function is optimized so that it does not split each transaction
804 * into chip page_size long blocks unnecessarily like spi_read_chunked. This has
805 * the advantage that it is much faster for most chips, but breaks those with
806 * non-contiguous address space (like AT45DB161D). When spi_read_chunked is
807 * fixed this method can be removed. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700808static int serprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
stefanct69965b62011-09-15 23:38:14 +0000809{
stefanctc5eb8a92011-11-23 09:13:48 +0000810 unsigned int i, cur_len;
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100811 const unsigned int max_read = spi_master_serprog.max_data_read;
stefanct69965b62011-09-15 23:38:14 +0000812 for (i = 0; i < len; i += cur_len) {
813 int ret;
814 cur_len = min(max_read, (len - i));
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700815 ret = spi_nbyte_read(flash, start + i, buf + i, cur_len);
stefanct69965b62011-09-15 23:38:14 +0000816 if (ret)
817 return ret;
818 }
819 return 0;
820}