blob: ed4e21a2f3ed19517a7d18849d4fcc4b197905ff [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"
Patrick Georgib9fe7f12017-04-11 20:35:19 +020040#include "serprog.h"
hailfingerbacbc8b2009-07-21 13:02:59 +000041
stefanctd9ac2212011-10-22 21:45:27 +000042#define MSGHEADER "serprog: "
hailfingerbacbc8b2009-07-21 13:02:59 +000043
dhendrix0ffc2eb2011-06-14 01:35:36 +000044/*
45 * FIXME: This prototype was added to help reduce diffs for the shutdown
46 * registration patch, which shifted many lines of code to place
47 * serprog_shutdown() before serprog_init(). It should be removed soon.
48 */
David Hendricks93784b42016-08-09 17:00:38 -070049static int serprog_shutdown(void *data);
dhendrix0ffc2eb2011-06-14 01:35:36 +000050
hailfingerbacbc8b2009-07-21 13:02:59 +000051static uint16_t sp_device_serbuf_size = 16;
52static uint16_t sp_device_opbuf_size = 300;
53/* Bitmap of supported commands */
54static uint8_t sp_cmdmap[32];
55
uwe3a3ab2f2010-03-25 23:18:41 +000056/* sp_prev_was_write used to detect writes with contiguous addresses
hailfingerbacbc8b2009-07-21 13:02:59 +000057 and combine them to write-n's */
58static int sp_prev_was_write = 0;
59/* sp_write_n_addr used as the starting addr of the currently
60 combined write-n operation */
61static uint32_t sp_write_n_addr;
62/* The maximum length of an write_n operation; 0 = write-n not supported */
63static uint32_t sp_max_write_n = 0;
64/* The maximum length of a read_n operation; 0 = 2^24 */
65static uint32_t sp_max_read_n = 0;
66
67/* A malloc'd buffer for combining the operation's data
68 and a counter that tells how much data is there. */
69static uint8_t *sp_write_n_buf;
70static uint32_t sp_write_n_bytes = 0;
71
72/* sp_streamed_* used for flow control checking */
73static int sp_streamed_transmit_ops = 0;
74static int sp_streamed_transmit_bytes = 0;
75
76/* sp_opbuf_usage used for counting the amount of
77 on-device operation buffer used */
78static int sp_opbuf_usage = 0;
79/* if true causes sp_docommand to automatically check
80 whether the command is supported before doing it */
81static int sp_check_avail_automatic = 0;
82
hailfingerbacbc8b2009-07-21 13:02:59 +000083static int sp_opensocket(char *ip, unsigned int port)
84{
85 int flag = 1;
86 struct hostent *hostPtr = NULL;
hailfinger3e454f32009-09-05 01:10:23 +000087 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
hailfingerbacbc8b2009-07-21 13:02:59 +000088 int sock;
snelson0afd28b2010-01-10 01:06:23 +000089 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
hailfingerbacbc8b2009-07-21 13:02:59 +000090 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
91 if (sock < 0)
92 sp_die("Error: serprog cannot open socket");
93 hostPtr = gethostbyname(ip);
94 if (NULL == hostPtr) {
95 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
96 if (NULL == hostPtr)
97 sp_die("Error: cannot resolve");
98 }
hailfinger3e454f32009-09-05 01:10:23 +000099 sp.si.sin_family = AF_INET;
100 sp.si.sin_port = htons(port);
101 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length);
102 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
hailfingerbacbc8b2009-07-21 13:02:59 +0000103 close(sock);
104 sp_die("Error: serprog cannot connect");
105 }
106 /* We are latency limited, and sometimes do write-write-read *
107 * (write-n) - so enable TCP_NODELAY. */
108 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
109 return sock;
110}
111
hailfingerbacbc8b2009-07-21 13:02:59 +0000112static int sp_sync_read_timeout(int loops)
113{
114 int i;
115 unsigned char c;
116 for (i = 0; i < loops; i++) {
117 ssize_t rv;
118 rv = read(sp_fd, &c, 1);
119 if (rv == 1)
120 return c;
121 if ((rv == -1) && (errno != EAGAIN))
122 sp_die("read");
123 usleep(10 * 1000); /* 10ms units */
124 }
125 return -1;
126}
127
uwe3a3ab2f2010-03-25 23:18:41 +0000128/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
hailfingerbacbc8b2009-07-21 13:02:59 +0000129 * always succeeded in) bring the serial protocol to known waiting-for- *
130 * command state - uses nonblocking read - rest of the driver uses *
131 * blocking read - TODO: add an alarm() timer for the rest of the app on *
132 * serial operations, though not such a big issue as the first thing to *
133 * do is synchronize (eg. check that device is alive). */
134static void sp_synchronize(void)
135{
136 int i;
137 int flags = fcntl(sp_fd, F_GETFL);
138 unsigned char buf[8];
139 flags |= O_NONBLOCK;
140 fcntl(sp_fd, F_SETFL, flags);
141 /* First sends 8 NOPs, then flushes the return data - should cause *
142 * the device serial parser to get to a sane state, unless if it *
143 * is waiting for a real long write-n. */
144 memset(buf, S_CMD_NOP, 8);
145 if (write(sp_fd, buf, 8) != 8)
146 sp_die("flush write");
147 /* A second should be enough to get all the answers to the buffer */
148 usleep(1000 * 1000);
149 sp_flush_incoming();
150
stefanct371e7e82011-07-07 19:56:58 +0000151 /* Then try up to 8 times to send syncnop and get the correct special *
152 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
153 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
154 * attempt, ~1s if immediate success. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000155 for (i = 0; i < 8; i++) {
156 int n;
157 unsigned char c = S_CMD_SYNCNOP;
158 if (write(sp_fd, &c, 1) != 1)
159 sp_die("sync write");
snelson0afd28b2010-01-10 01:06:23 +0000160 msg_pdbg(".");
hailfingerbacbc8b2009-07-21 13:02:59 +0000161 fflush(stdout);
162 for (n = 0; n < 10; n++) {
stefanct371e7e82011-07-07 19:56:58 +0000163 c = sp_sync_read_timeout(5); /* wait up to 50ms */
hailfingerbacbc8b2009-07-21 13:02:59 +0000164 if (c != S_NAK)
165 continue;
166 c = sp_sync_read_timeout(2);
167 if (c != S_ACK)
168 continue;
169 c = S_CMD_SYNCNOP;
170 if (write(sp_fd, &c, 1) != 1)
171 sp_die("sync write");
172 c = sp_sync_read_timeout(50);
173 if (c != S_NAK)
174 break; /* fail */
175 c = sp_sync_read_timeout(10);
176 if (c != S_ACK)
177 break; /* fail */
178 /* Ok, synchronized; back to blocking reads and return. */
179 flags &= ~O_NONBLOCK;
180 fcntl(sp_fd, F_SETFL, flags);
snelson0afd28b2010-01-10 01:06:23 +0000181 msg_pdbg("\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000182 return;
183 }
184 }
stefanctd9ac2212011-10-22 21:45:27 +0000185 msg_perr("Error: cannot synchronize protocol "
hailfingerbacbc8b2009-07-21 13:02:59 +0000186 "- check communications and reset device?\n");
187 exit(1);
188}
189
190static int sp_check_commandavail(uint8_t command)
191{
192 int byteoffs, bitoffs;
193 byteoffs = command / 8;
194 bitoffs = command % 8;
195 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
196}
197
198static int sp_automatic_cmdcheck(uint8_t cmd)
199{
200 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
stefanctd9ac2212011-10-22 21:45:27 +0000201 msg_pdbg("Warning: Automatic command availability check failed "
202 "for cmd 0x%x - won't execute cmd\n", cmd);
hailfingerbacbc8b2009-07-21 13:02:59 +0000203 return 1;
204 }
205 return 0;
206}
207
208static int sp_docommand(uint8_t command, uint32_t parmlen,
stefanctd9ac2212011-10-22 21:45:27 +0000209 uint8_t *params, uint32_t retlen, void *retparms)
hailfingerbacbc8b2009-07-21 13:02:59 +0000210{
hailfingerbacbc8b2009-07-21 13:02:59 +0000211 unsigned char c;
212 if (sp_automatic_cmdcheck(command))
213 return 1;
stefanctd9ac2212011-10-22 21:45:27 +0000214 if (write(sp_fd, &command, 1) != 1)
215 sp_die("Error: cannot write op code");
216 if (write(sp_fd, params, parmlen) != (parmlen))
217 sp_die("Error: cannot write parameters");
hailfingerbacbc8b2009-07-21 13:02:59 +0000218 if (read(sp_fd, &c, 1) != 1)
219 sp_die("Error: cannot read from device");
stefanctd9ac2212011-10-22 21:45:27 +0000220 if (c == S_NAK)
221 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000222 if (c != S_ACK) {
snelson0afd28b2010-01-10 01:06:23 +0000223 msg_perr("Error: invalid response 0x%02X from device\n",c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000224 exit(1);
225 }
226 if (retlen) {
227 int rd_bytes = 0;
228 do {
229 int r;
230 r = read(sp_fd, retparms + rd_bytes,
231 retlen - rd_bytes);
stefanctd9ac2212011-10-22 21:45:27 +0000232 if (r <= 0)
233 sp_die("Error: cannot read return parameters");
hailfingerbacbc8b2009-07-21 13:02:59 +0000234 rd_bytes += r;
235 } while (rd_bytes != retlen);
236 }
237 return 0;
238}
239
240static void sp_flush_stream(void)
241{
242 if (sp_streamed_transmit_ops)
243 do {
244 unsigned char c;
245 if (read(sp_fd, &c, 1) != 1) {
snelson0afd28b2010-01-10 01:06:23 +0000246 sp_die("Error: cannot read from device (flushing stream)");
hailfingerbacbc8b2009-07-21 13:02:59 +0000247 }
248 if (c == S_NAK) {
snelson0afd28b2010-01-10 01:06:23 +0000249 msg_perr("Error: NAK to a stream buffer operation\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000250 exit(1);
251 }
252 if (c != S_ACK) {
snelson0afd28b2010-01-10 01:06:23 +0000253 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000254 exit(1);
255 }
256 } while (--sp_streamed_transmit_ops);
257 sp_streamed_transmit_ops = 0;
258 sp_streamed_transmit_bytes = 0;
259}
260
261static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
262{
263 uint8_t *sp;
264 if (sp_automatic_cmdcheck(cmd))
265 return 1;
266 sp = malloc(1 + parmlen);
267 if (!sp) sp_die("Error: cannot malloc command buffer");
268 sp[0] = cmd;
269 memcpy(&(sp[1]), parms, parmlen);
270 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
271 sp_flush_stream();
272 if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
273 sp_die("Error: cannot write command");
274 free(sp);
275 sp_streamed_transmit_ops += 1;
276 sp_streamed_transmit_bytes += 1 + parmlen;
277 return 0;
278}
279
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700280static int serprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
hailfinger76bb7e92011-11-09 23:40:00 +0000281 const unsigned char *writearr,
282 unsigned char *readarr);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700283static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000284 unsigned int start, unsigned int len);
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100285static struct spi_master spi_master_serprog = {
stefanct69965b62011-09-15 23:38:14 +0000286 .type = SPI_CONTROLLER_SERPROG,
287 .max_data_read = MAX_DATA_READ_UNLIMITED,
288 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
289 .command = serprog_spi_send_command,
290 .multicommand = default_spi_send_multicommand,
291 .read = serprog_spi_read,
292 .write_256 = default_spi_write_256,
293};
294
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700295static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
296 chipaddr addr);
297static uint8_t serprog_chip_readb(const struct flashctx *flash,
298 const chipaddr addr);
299static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
300 const chipaddr addr, size_t len);
301
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100302static const struct par_master par_master_serprog = {
hailfinger76bb7e92011-11-09 23:40:00 +0000303 .chip_readb = serprog_chip_readb,
304 .chip_readw = fallback_chip_readw,
305 .chip_readl = fallback_chip_readl,
306 .chip_readn = serprog_chip_readn,
307 .chip_writeb = serprog_chip_writeb,
308 .chip_writew = fallback_chip_writew,
309 .chip_writel = fallback_chip_writel,
310 .chip_writen = fallback_chip_writen,
311};
312
313static enum chipbustype serprog_buses_supported = BUS_NONE;
314
David Hendricksac1d25c2016-08-09 17:00:58 -0700315int serprog_init(void)
hailfingerbacbc8b2009-07-21 13:02:59 +0000316{
317 uint16_t iface;
hailfingerbacbc8b2009-07-21 13:02:59 +0000318 unsigned char pgmname[17];
319 unsigned char rbuf[3];
320 unsigned char c;
hailfinger1ef766d2010-07-06 09:55:48 +0000321 char *device;
322 char *baudport;
323 int have_device = 0;
hailfingerbacbc8b2009-07-21 13:02:59 +0000324
hailfinger1ef766d2010-07-06 09:55:48 +0000325 /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */
hailfingerddeb4ac2010-07-08 10:13:37 +0000326 device = extract_programmer_param("dev");
hailfinger1ef766d2010-07-06 09:55:48 +0000327 if (device && strlen(device)) {
328 baudport = strstr(device, ":");
329 if (baudport) {
330 /* Split device from baudrate. */
331 *baudport = '\0';
332 baudport++;
333 }
334 if (!baudport || !strlen(baudport)) {
335 msg_perr("Error: No baudrate specified.\n"
336 "Use flashrom -p serprog:dev=/dev/device:baud\n");
337 free(device);
stefanct69965b62011-09-15 23:38:14 +0000338 return 1;
hailfinger1ef766d2010-07-06 09:55:48 +0000339 }
340 if (strlen(device)) {
341 sp_fd = sp_openserport(device, atoi(baudport));
342 have_device++;
343 }
344 }
345 if (device && !strlen(device)) {
346 msg_perr("Error: No device specified.\n"
347 "Use flashrom -p serprog:dev=/dev/device:baud\n");
348 free(device);
349 return 1;
350 }
351 free(device);
hailfingerbacbc8b2009-07-21 13:02:59 +0000352
hailfingerddeb4ac2010-07-08 10:13:37 +0000353 device = extract_programmer_param("ip");
hailfinger1ef766d2010-07-06 09:55:48 +0000354 if (have_device && device) {
355 msg_perr("Error: Both host and device specified.\n"
356 "Please use either dev= or ip= but not both.\n");
357 free(device);
358 return 1;
359 }
360 if (device && strlen(device)) {
361 baudport = strstr(device, ":");
362 if (baudport) {
363 /* Split host from port. */
364 *baudport = '\0';
365 baudport++;
366 }
367 if (!baudport || !strlen(baudport)) {
368 msg_perr("Error: No port specified.\n"
369 "Use flashrom -p serprog:ip=ipaddr:port\n");
370 free(device);
stefanct69965b62011-09-15 23:38:14 +0000371 return 1;
hailfinger1ef766d2010-07-06 09:55:48 +0000372 }
373 if (strlen(device)) {
374 sp_fd = sp_opensocket(device, atoi(baudport));
375 have_device++;
376 }
377 }
378 if (device && !strlen(device)) {
379 msg_perr("Error: No host specified.\n"
380 "Use flashrom -p serprog:ip=ipaddr:port\n");
381 free(device);
382 return 1;
383 }
384 free(device);
385
386 if (!have_device) {
387 msg_perr("Error: Neither host nor device specified.\n"
388 "Use flashrom -p serprog:dev=/dev/device:baud or "
389 "flashrom -p serprog:ip=ipaddr:port\n");
390 return 1;
391 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000392
dhendrix0ffc2eb2011-06-14 01:35:36 +0000393 if (register_shutdown(serprog_shutdown, NULL))
394 return 1;
395
snelson0afd28b2010-01-10 01:06:23 +0000396 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000397
398 sp_check_avail_automatic = 0;
399
400 sp_synchronize();
401
snelson0afd28b2010-01-10 01:06:23 +0000402 msg_pdbg(MSGHEADER "Synchronized\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000403
404 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
stefanctd9ac2212011-10-22 21:45:27 +0000405 msg_perr("Error: NAK to query interface version\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000406 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000407 }
408
409 if (iface != 1) {
stefanctd9ac2212011-10-22 21:45:27 +0000410 msg_perr("Error: Unknown interface version: %d\n", iface);
hailfinger76bb7e92011-11-09 23:40:00 +0000411 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000412 }
413
snelson0afd28b2010-01-10 01:06:23 +0000414 msg_pdbg(MSGHEADER "Interface version ok.\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000415
416 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
snelson0afd28b2010-01-10 01:06:23 +0000417 msg_perr("Error: query command map not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000418 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000419 }
420
421 sp_check_avail_automatic = 1;
422
hailfinger76bb7e92011-11-09 23:40:00 +0000423 /* FIXME: This assumes that serprog device bustypes are always
424 * identical with flashrom bustype enums and that they all fit
425 * in a single byte.
426 */
stefanct69965b62011-09-15 23:38:14 +0000427 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
428 msg_perr("Warning: NAK to query supported buses\n");
429 c = BUS_NONSPI; /* A reasonable default for now. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000430 }
hailfinger76bb7e92011-11-09 23:40:00 +0000431 serprog_buses_supported = c;
432
stefanctd9ac2212011-10-22 21:45:27 +0000433 msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
434 (c & BUS_PARALLEL) ? "on" : "off",
435 (c & BUS_LPC) ? "on" : "off",
436 (c & BUS_FWH) ? "on" : "off",
437 (c & BUS_SPI) ? "on" : "off");
stefanct69965b62011-09-15 23:38:14 +0000438 /* Check for the minimum operational set of commands. */
hailfinger76bb7e92011-11-09 23:40:00 +0000439 if (serprog_buses_supported & BUS_SPI) {
stefanct69965b62011-09-15 23:38:14 +0000440 uint8_t bt = BUS_SPI;
441 if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
442 msg_perr("Error: SPI operation not supported while the "
443 "bustype is SPI\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000444 return 1;
stefanct69965b62011-09-15 23:38:14 +0000445 }
446 /* Success of any of these commands is optional. We don't need
447 the programmer to tell us its limits, but if it doesn't, we
448 will assume stuff, so it's in the programmers best interest
449 to tell us. */
450 sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
451 if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
452 uint32_t v;
453 v = ((unsigned int)(rbuf[0]) << 0);
454 v |= ((unsigned int)(rbuf[1]) << 8);
455 v |= ((unsigned int)(rbuf[2]) << 16);
456 if (v == 0)
457 v = (1 << 24) - 1; /* SPI-op maximum. */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100458 spi_master_serprog.max_data_write = v;
stefanct69965b62011-09-15 23:38:14 +0000459 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
460 }
461 if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
462 uint32_t v;
463 v = ((unsigned int)(rbuf[0]) << 0);
464 v |= ((unsigned int)(rbuf[1]) << 8);
465 v |= ((unsigned int)(rbuf[2]) << 16);
466 if (v == 0)
467 v = (1 << 24) - 1; /* SPI-op maximum. */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100468 spi_master_serprog.max_data_read = v;
stefanct69965b62011-09-15 23:38:14 +0000469 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
470 }
hailfinger76bb7e92011-11-09 23:40:00 +0000471 bt = serprog_buses_supported;
stefanct69965b62011-09-15 23:38:14 +0000472 sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL);
hailfingerbacbc8b2009-07-21 13:02:59 +0000473 }
stefanct69965b62011-09-15 23:38:14 +0000474
hailfinger76bb7e92011-11-09 23:40:00 +0000475 if (serprog_buses_supported & BUS_NONSPI) {
stefanct69965b62011-09-15 23:38:14 +0000476 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
477 msg_perr("Error: Initialize operation buffer "
478 "not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000479 return 1;
stefanct69965b62011-09-15 23:38:14 +0000480 }
481
482 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
483 msg_perr("Error: Write to opbuf: "
484 "delay not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000485 return 1;
stefanct69965b62011-09-15 23:38:14 +0000486 }
487
488 /* S_CMD_O_EXEC availability checked later. */
489
490 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
491 msg_perr("Error: Single byte read not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000492 return 1;
stefanct69965b62011-09-15 23:38:14 +0000493 }
494 /* This could be translated to single byte reads (if missing),
495 * but now we don't support that. */
496 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
497 msg_perr("Error: Read n bytes not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000498 return 1;
stefanct69965b62011-09-15 23:38:14 +0000499 }
500 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
501 msg_perr("Error: Write to opbuf: "
502 "write byte not supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000503 return 1;
stefanct69965b62011-09-15 23:38:14 +0000504 }
505
506 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
507 msg_pdbg(MSGHEADER "Write-n not supported");
508 sp_max_write_n = 0;
509 } else {
510 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
511 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
512 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
513 if (!sp_max_write_n) {
514 sp_max_write_n = (1 << 24);
515 }
516 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
517 sp_max_write_n);
518 sp_write_n_buf = malloc(sp_max_write_n);
519 if (!sp_write_n_buf) {
520 msg_perr("Error: cannot allocate memory for "
521 "Write-n buffer\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000522 return 1;
stefanct69965b62011-09-15 23:38:14 +0000523 }
524 sp_write_n_bytes = 0;
525 }
526
527 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
528 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
529 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
530 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
531 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
532 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
533 sp_max_read_n ? sp_max_read_n : (1 << 24));
534 } else {
535 msg_pdbg(MSGHEADER "Maximum read-n length "
536 "not reported\n");
537 sp_max_read_n = 0;
538 }
539
hailfingerbacbc8b2009-07-21 13:02:59 +0000540 }
541
542 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
snelson0afd28b2010-01-10 01:06:23 +0000543 msg_perr("Warning: NAK to query programmer name\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000544 strcpy((char *)pgmname, "(unknown)");
545 }
546 pgmname[16] = 0;
stefanctd9ac2212011-10-22 21:45:27 +0000547 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
hailfingerbacbc8b2009-07-21 13:02:59 +0000548
549 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
snelson0afd28b2010-01-10 01:06:23 +0000550 msg_perr("Warning: NAK to query serial buffer size\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000551 }
stefanctd9ac2212011-10-22 21:45:27 +0000552 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
hailfingerbacbc8b2009-07-21 13:02:59 +0000553 sp_device_serbuf_size);
554
stefanct69965b62011-09-15 23:38:14 +0000555 if (sp_check_commandavail(S_CMD_O_INIT)) {
556 /* This would be inconsistent. */
557 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
558 msg_perr("Error: Execute operation buffer not "
559 "supported\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000560 return 1;
hailfingerbacbc8b2009-07-21 13:02:59 +0000561 }
stefanct69965b62011-09-15 23:38:14 +0000562
563 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
564 msg_perr("Error: NAK to initialize operation buffer\n");
hailfinger76bb7e92011-11-09 23:40:00 +0000565 return 1;
stefanct69965b62011-09-15 23:38:14 +0000566 }
567
568 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
569 &sp_device_opbuf_size)) {
570 msg_perr("Warning: NAK to query operation buffer "
571 "size\n");
572 }
stefanctd9ac2212011-10-22 21:45:27 +0000573 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
stefanct69965b62011-09-15 23:38:14 +0000574 sp_device_opbuf_size);
575 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000576
577 sp_prev_was_write = 0;
578 sp_streamed_transmit_ops = 0;
579 sp_streamed_transmit_bytes = 0;
580 sp_opbuf_usage = 0;
hailfinger76bb7e92011-11-09 23:40:00 +0000581 if (serprog_buses_supported & BUS_SPI)
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100582 register_spi_master(&spi_master_serprog);
hailfinger76bb7e92011-11-09 23:40:00 +0000583 if (serprog_buses_supported & BUS_NONSPI)
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100584 register_par_master(&par_master_serprog,
hailfinger76bb7e92011-11-09 23:40:00 +0000585 serprog_buses_supported & BUS_NONSPI);
hailfingerbacbc8b2009-07-21 13:02:59 +0000586 return 0;
587}
588
589/* Move an in flashrom buffer existing write-n operation to *
590 * the on-device operation buffer. */
591static void sp_pass_writen(void)
592{
593 unsigned char header[7];
snelson0afd28b2010-01-10 01:06:23 +0000594 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
stefanctd9ac2212011-10-22 21:45:27 +0000595 sp_write_n_bytes, sp_write_n_addr);
hailfingerbacbc8b2009-07-21 13:02:59 +0000596 if (sp_streamed_transmit_bytes >=
597 (7 + sp_write_n_bytes + sp_device_serbuf_size))
598 sp_flush_stream();
599 /* In case it's just a single byte send it as a single write. */
600 if (sp_write_n_bytes == 1) {
601 sp_write_n_bytes = 0;
602 header[0] = (sp_write_n_addr >> 0) & 0xFF;
603 header[1] = (sp_write_n_addr >> 8) & 0xFF;
604 header[2] = (sp_write_n_addr >> 16) & 0xFF;
605 header[3] = sp_write_n_buf[0];
606 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
607 sp_opbuf_usage += 5;
608 return;
609 }
610 header[0] = S_CMD_O_WRITEN;
611 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
612 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
613 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
614 header[4] = (sp_write_n_addr >> 0) & 0xFF;
615 header[5] = (sp_write_n_addr >> 8) & 0xFF;
616 header[6] = (sp_write_n_addr >> 16) & 0xFF;
617 if (write(sp_fd, header, 7) != 7)
618 sp_die("Error: cannot write write-n command\n");
619 if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
620 sp_write_n_bytes)
621 sp_die("Error: cannot write write-n data");
622 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
623 sp_streamed_transmit_ops += 1;
624 sp_opbuf_usage += 7 + sp_write_n_bytes;
625 sp_write_n_bytes = 0;
626 sp_prev_was_write = 0;
627}
628
629static void sp_execute_opbuf_noflush(void)
630{
631 if ((sp_max_write_n) && (sp_write_n_bytes))
632 sp_pass_writen();
stepand0d220f2011-01-24 19:15:51 +0000633 sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL);
snelson0afd28b2010-01-10 01:06:23 +0000634 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n",
hailfingerbacbc8b2009-07-21 13:02:59 +0000635 sp_opbuf_usage);
636 sp_opbuf_usage = 0;
637 sp_prev_was_write = 0;
638 return;
639}
640
641static void sp_execute_opbuf(void)
642{
643 sp_execute_opbuf_noflush();
644 sp_flush_stream();
645}
646
David Hendricks93784b42016-08-09 17:00:38 -0700647static int serprog_shutdown(void *data)
hailfingerbacbc8b2009-07-21 13:02:59 +0000648{
snelson0afd28b2010-01-10 01:06:23 +0000649 msg_pspew("%s\n", __func__);
hailfingerbacbc8b2009-07-21 13:02:59 +0000650 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
651 sp_execute_opbuf();
652 close(sp_fd);
653 if (sp_max_write_n)
654 free(sp_write_n_buf);
655 return 0;
656}
657
658static void sp_check_opbuf_usage(int bytes_to_be_added)
659{
660 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
661 sp_execute_opbuf();
662 /* If this happens in the mid of an page load the page load *
uwe3a3ab2f2010-03-25 23:18:41 +0000663 * will probably fail. */
snelson0afd28b2010-01-10 01:06:23 +0000664 msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
hailfingerbacbc8b2009-07-21 13:02:59 +0000665 }
666}
667
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700668void serprog_chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
hailfingerbacbc8b2009-07-21 13:02:59 +0000669{
snelson0afd28b2010-01-10 01:06:23 +0000670 msg_pspew("%s\n", __func__);
hailfingerbacbc8b2009-07-21 13:02:59 +0000671 if (sp_max_write_n) {
672 if ((sp_prev_was_write)
673 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
674 sp_write_n_buf[sp_write_n_bytes++] = val;
675 } else {
676 if ((sp_prev_was_write) && (sp_write_n_bytes))
677 sp_pass_writen();
678 sp_prev_was_write = 1;
679 sp_write_n_addr = addr;
680 sp_write_n_bytes = 1;
681 sp_write_n_buf[0] = val;
682 }
683 sp_check_opbuf_usage(7 + sp_write_n_bytes);
684 if (sp_write_n_bytes >= sp_max_write_n)
685 sp_pass_writen();
686 } else {
687 /* We will have to do single writeb ops. */
688 unsigned char writeb_parm[4];
689 sp_check_opbuf_usage(6);
690 writeb_parm[0] = (addr >> 0) & 0xFF;
691 writeb_parm[1] = (addr >> 8) & 0xFF;
692 writeb_parm[2] = (addr >> 16) & 0xFF;
693 writeb_parm[3] = val;
694 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
695 sp_opbuf_usage += 5;
696 }
697}
698
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700699uint8_t serprog_chip_readb(const struct flashctx *flash, const chipaddr addr)
hailfingerbacbc8b2009-07-21 13:02:59 +0000700{
701 unsigned char c;
702 unsigned char buf[3];
703 /* Will stream the read operation - eg. add it to the stream buffer, *
704 * then flush the buffer, then read the read answer. */
705 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
706 sp_execute_opbuf_noflush();
707 buf[0] = ((addr >> 0) & 0xFF);
708 buf[1] = ((addr >> 8) & 0xFF);
709 buf[2] = ((addr >> 16) & 0xFF);
710 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
711 sp_flush_stream();
712 if (read(sp_fd, &c, 1) != 1)
713 sp_die("readb byteread");
snelson0afd28b2010-01-10 01:06:23 +0000714 msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
hailfingerbacbc8b2009-07-21 13:02:59 +0000715 return c;
716}
717
uwe3a3ab2f2010-03-25 23:18:41 +0000718/* Local version that really does the job, doesn't care of max_read_n. */
hailfingerbacbc8b2009-07-21 13:02:59 +0000719static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
720{
721 int rd_bytes = 0;
722 unsigned char sbuf[6];
snelson0afd28b2010-01-10 01:06:23 +0000723 msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
hailfingerbacbc8b2009-07-21 13:02:59 +0000724 /* Stream the read-n -- as above. */
725 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
726 sp_execute_opbuf_noflush();
727 sbuf[0] = ((addr >> 0) & 0xFF);
728 sbuf[1] = ((addr >> 8) & 0xFF);
729 sbuf[2] = ((addr >> 16) & 0xFF);
730 sbuf[3] = ((len >> 0) & 0xFF);
731 sbuf[4] = ((len >> 8) & 0xFF);
732 sbuf[5] = ((len >> 16) & 0xFF);
733 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
734 sp_flush_stream();
735 do {
736 int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
737 if (r <= 0)
738 sp_die("Error: cannot read read-n data");
739 rd_bytes += r;
740 } while (rd_bytes != len);
741 return;
742}
743
744/* The externally called version that makes sure that max_read_n is obeyed. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700745void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf, const chipaddr addr, size_t len)
hailfingerbacbc8b2009-07-21 13:02:59 +0000746{
747 size_t lenm = len;
748 chipaddr addrm = addr;
stefanctd9ac2212011-10-22 21:45:27 +0000749 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
750 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n);
hailfingerbacbc8b2009-07-21 13:02:59 +0000751 addrm += sp_max_read_n;
752 lenm -= sp_max_read_n;
753 }
stefanctd9ac2212011-10-22 21:45:27 +0000754 if (lenm)
755 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm);
hailfingerbacbc8b2009-07-21 13:02:59 +0000756}
757
stefanctd9ac2212011-10-22 21:45:27 +0000758void serprog_delay(int usecs)
hailfingerbacbc8b2009-07-21 13:02:59 +0000759{
760 unsigned char buf[4];
stefanctd9ac2212011-10-22 21:45:27 +0000761 msg_pspew("%s usecs=%d\n", __func__, usecs);
stefanct69965b62011-09-15 23:38:14 +0000762 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
stefanct69965b62011-09-15 23:38:14 +0000763 msg_pdbg("Note: serprog_delay used, but the programmer doesn't "
764 "support delay\n");
stefanctd9ac2212011-10-22 21:45:27 +0000765 internal_delay(usecs);
stefanct69965b62011-09-15 23:38:14 +0000766 return;
767 }
hailfingerbacbc8b2009-07-21 13:02:59 +0000768 if ((sp_max_write_n) && (sp_write_n_bytes))
769 sp_pass_writen();
770 sp_check_opbuf_usage(5);
stefanctd9ac2212011-10-22 21:45:27 +0000771 buf[0] = ((usecs >> 0) & 0xFF);
772 buf[1] = ((usecs >> 8) & 0xFF);
773 buf[2] = ((usecs >> 16) & 0xFF);
774 buf[3] = ((usecs >> 24) & 0xFF);
hailfingerbacbc8b2009-07-21 13:02:59 +0000775 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
776 sp_opbuf_usage += 5;
777 sp_prev_was_write = 0;
778}
stefanct69965b62011-09-15 23:38:14 +0000779
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700780static int serprog_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
stefanct69965b62011-09-15 23:38:14 +0000781 const unsigned char *writearr,
782 unsigned char *readarr)
783{
784 unsigned char *parmbuf;
785 int ret;
786 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
787 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
788 sp_execute_opbuf();
789 parmbuf = malloc(writecnt + 6);
790 if (!parmbuf)
791 sp_die("Error: cannot malloc SPI send param buffer");
792 parmbuf[0] = (writecnt >> 0) & 0xFF;
793 parmbuf[1] = (writecnt >> 8) & 0xFF;
794 parmbuf[2] = (writecnt >> 16) & 0xFF;
795 parmbuf[3] = (readcnt >> 0) & 0xFF;
796 parmbuf[4] = (readcnt >> 8) & 0xFF;
797 parmbuf[5] = (readcnt >> 16) & 0xFF;
798 memcpy(parmbuf + 6, writearr, writecnt);
799 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
800 readarr);
801 free(parmbuf);
802 return ret;
803}
804
805/* FIXME: This function is optimized so that it does not split each transaction
806 * into chip page_size long blocks unnecessarily like spi_read_chunked. This has
807 * the advantage that it is much faster for most chips, but breaks those with
808 * non-contiguous address space (like AT45DB161D). When spi_read_chunked is
809 * fixed this method can be removed. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700810static int serprog_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
stefanct69965b62011-09-15 23:38:14 +0000811{
stefanctc5eb8a92011-11-23 09:13:48 +0000812 unsigned int i, cur_len;
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100813 const unsigned int max_read = spi_master_serprog.max_data_read;
stefanct69965b62011-09-15 23:38:14 +0000814 for (i = 0; i < len; i += cur_len) {
815 int ret;
816 cur_len = min(max_read, (len - i));
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700817 ret = spi_nbyte_read(flash, start + i, buf + i, cur_len);
stefanct69965b62011-09-15 23:38:14 +0000818 if (ret)
819 return ret;
820 }
821 return 0;
822}