blob: e8a44024dd078b5b942bfdfd8ab0e9b5696a40df [file] [log] [blame]
Urja Rannikko22915352009-06-23 11:33:43 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com>
5 * Copyright (C) 2009 Carl-Daniel Hailfinger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000022#include <stdio.h>
Urja Rannikko22915352009-06-23 11:33:43 +000023#include <stdlib.h>
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000024#include <unistd.h>
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000025#include <string.h>
Urja Rannikko22915352009-06-23 11:33:43 +000026#include <ctype.h>
27#include <fcntl.h>
Urja Rannikko22915352009-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>
Urja Rannikko22915352009-06-23 11:33:43 +000035#include <inttypes.h>
36#include <termios.h>
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000037#include "flash.h"
38#include "programmer.h"
Urja Rannikkof3196df2009-07-21 13:02:59 +000039
40#define MSGHEADER "serprog:"
41
David Hendricks8bb20212011-06-14 01:35:36 +000042/*
43 * FIXME: This prototype was added to help reduce diffs for the shutdown
44 * registration patch, which shifted many lines of code to place
45 * serprog_shutdown() before serprog_init(). It should be removed soon.
46 */
47static int serprog_shutdown(void *data);
48
Urja Rannikkof3196df2009-07-21 13:02:59 +000049#define S_ACK 0x06
50#define S_NAK 0x15
51#define S_CMD_NOP 0x00 /* No operation */
52#define S_CMD_Q_IFACE 0x01 /* Query interface version */
53#define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */
54#define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */
55#define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */
56#define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */
57#define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */
58#define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000059#define S_CMD_Q_WRNMAXLEN 0x08 /* Query opbuf-write-N maximum length */
Urja Rannikkof3196df2009-07-21 13:02:59 +000060#define S_CMD_R_BYTE 0x09 /* Read a single byte */
61#define S_CMD_R_NBYTES 0x0A /* Read n bytes */
62#define S_CMD_O_INIT 0x0B /* Initialize operation buffer */
63#define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */
64#define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */
65#define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */
66#define S_CMD_O_EXEC 0x0F /* Execute operation buffer */
67#define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */
68#define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */
69#define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s). */
70
Urja Rannikkof3196df2009-07-21 13:02:59 +000071static uint16_t sp_device_serbuf_size = 16;
72static uint16_t sp_device_opbuf_size = 300;
73/* Bitmap of supported commands */
74static uint8_t sp_cmdmap[32];
75
Uwe Hermann4e3d0b32010-03-25 23:18:41 +000076/* sp_prev_was_write used to detect writes with contiguous addresses
Urja Rannikkof3196df2009-07-21 13:02:59 +000077 and combine them to write-n's */
78static int sp_prev_was_write = 0;
79/* sp_write_n_addr used as the starting addr of the currently
80 combined write-n operation */
81static uint32_t sp_write_n_addr;
82/* The maximum length of an write_n operation; 0 = write-n not supported */
83static uint32_t sp_max_write_n = 0;
84/* The maximum length of a read_n operation; 0 = 2^24 */
85static uint32_t sp_max_read_n = 0;
86
87/* A malloc'd buffer for combining the operation's data
88 and a counter that tells how much data is there. */
89static uint8_t *sp_write_n_buf;
90static uint32_t sp_write_n_bytes = 0;
91
92/* sp_streamed_* used for flow control checking */
93static int sp_streamed_transmit_ops = 0;
94static int sp_streamed_transmit_bytes = 0;
95
96/* sp_opbuf_usage used for counting the amount of
97 on-device operation buffer used */
98static int sp_opbuf_usage = 0;
99/* if true causes sp_docommand to automatically check
100 whether the command is supported before doing it */
101static int sp_check_avail_automatic = 0;
102
Urja Rannikkof3196df2009-07-21 13:02:59 +0000103static int sp_opensocket(char *ip, unsigned int port)
104{
105 int flag = 1;
106 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000107 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +0000108 int sock;
Sean Nelson74e8af52010-01-10 01:06:23 +0000109 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000110 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
111 if (sock < 0)
112 sp_die("Error: serprog cannot open socket");
113 hostPtr = gethostbyname(ip);
114 if (NULL == hostPtr) {
115 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
116 if (NULL == hostPtr)
117 sp_die("Error: cannot resolve");
118 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000119 sp.si.sin_family = AF_INET;
120 sp.si.sin_port = htons(port);
121 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length);
122 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000123 close(sock);
124 sp_die("Error: serprog cannot connect");
125 }
126 /* We are latency limited, and sometimes do write-write-read *
127 * (write-n) - so enable TCP_NODELAY. */
128 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
129 return sock;
130}
131
Urja Rannikkof3196df2009-07-21 13:02:59 +0000132static int sp_sync_read_timeout(int loops)
133{
134 int i;
135 unsigned char c;
136 for (i = 0; i < loops; i++) {
137 ssize_t rv;
138 rv = read(sp_fd, &c, 1);
139 if (rv == 1)
140 return c;
141 if ((rv == -1) && (errno != EAGAIN))
142 sp_die("read");
143 usleep(10 * 1000); /* 10ms units */
144 }
145 return -1;
146}
147
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000148/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000149 * always succeeded in) bring the serial protocol to known waiting-for- *
150 * command state - uses nonblocking read - rest of the driver uses *
151 * blocking read - TODO: add an alarm() timer for the rest of the app on *
152 * serial operations, though not such a big issue as the first thing to *
153 * do is synchronize (eg. check that device is alive). */
154static void sp_synchronize(void)
155{
156 int i;
157 int flags = fcntl(sp_fd, F_GETFL);
158 unsigned char buf[8];
159 flags |= O_NONBLOCK;
160 fcntl(sp_fd, F_SETFL, flags);
161 /* First sends 8 NOPs, then flushes the return data - should cause *
162 * the device serial parser to get to a sane state, unless if it *
163 * is waiting for a real long write-n. */
164 memset(buf, S_CMD_NOP, 8);
165 if (write(sp_fd, buf, 8) != 8)
166 sp_die("flush write");
167 /* A second should be enough to get all the answers to the buffer */
168 usleep(1000 * 1000);
169 sp_flush_incoming();
170
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000171 /* Then try up to 8 times to send syncnop and get the correct special *
172 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
173 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
174 * attempt, ~1s if immediate success. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000175 for (i = 0; i < 8; i++) {
176 int n;
177 unsigned char c = S_CMD_SYNCNOP;
178 if (write(sp_fd, &c, 1) != 1)
179 sp_die("sync write");
Sean Nelson74e8af52010-01-10 01:06:23 +0000180 msg_pdbg(".");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000181 fflush(stdout);
182 for (n = 0; n < 10; n++) {
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000183 c = sp_sync_read_timeout(5); /* wait up to 50ms */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000184 if (c != S_NAK)
185 continue;
186 c = sp_sync_read_timeout(2);
187 if (c != S_ACK)
188 continue;
189 c = S_CMD_SYNCNOP;
190 if (write(sp_fd, &c, 1) != 1)
191 sp_die("sync write");
192 c = sp_sync_read_timeout(50);
193 if (c != S_NAK)
194 break; /* fail */
195 c = sp_sync_read_timeout(10);
196 if (c != S_ACK)
197 break; /* fail */
198 /* Ok, synchronized; back to blocking reads and return. */
199 flags &= ~O_NONBLOCK;
200 fcntl(sp_fd, F_SETFL, flags);
Sean Nelson74e8af52010-01-10 01:06:23 +0000201 msg_pdbg("\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000202 return;
203 }
204 }
Sean Nelson74e8af52010-01-10 01:06:23 +0000205 msg_perr("Error: cannot synchronize protocol\n"
Urja Rannikkof3196df2009-07-21 13:02:59 +0000206 "- check communications and reset device?\n");
207 exit(1);
208}
209
210static int sp_check_commandavail(uint8_t command)
211{
212 int byteoffs, bitoffs;
213 byteoffs = command / 8;
214 bitoffs = command % 8;
215 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
216}
217
218static int sp_automatic_cmdcheck(uint8_t cmd)
219{
220 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000221 msg_pdbg("Warning: Automatic command availability check"
Urja Rannikkof3196df2009-07-21 13:02:59 +0000222 " failed for cmd %d - wont execute cmd\n",cmd);
223 return 1;
224 }
225 return 0;
226}
227
228static int sp_docommand(uint8_t command, uint32_t parmlen,
229 uint8_t * params, uint32_t retlen, void *retparms)
230{
231 unsigned char *sendpacket;
232 unsigned char c;
233 if (sp_automatic_cmdcheck(command))
234 return 1;
235 sendpacket = malloc(1 + parmlen);
236 if (!sendpacket)
237 sp_die("Error: cannot malloc command buffer");
238 sendpacket[0] = command;
239 memcpy(&(sendpacket[1]), params, parmlen);
240 if (write(sp_fd, sendpacket, 1 + parmlen) != (1 + parmlen)) {
241 sp_die("Error: cannot write command");
242 }
243 free(sendpacket);
244 if (read(sp_fd, &c, 1) != 1)
245 sp_die("Error: cannot read from device");
246 if (c == S_NAK) return 1;
247 if (c != S_ACK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000248 msg_perr("Error: invalid response 0x%02X from device\n",c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000249 exit(1);
250 }
251 if (retlen) {
252 int rd_bytes = 0;
253 do {
254 int r;
255 r = read(sp_fd, retparms + rd_bytes,
256 retlen - rd_bytes);
257 if (r <= 0) sp_die
258 ("Error: cannot read return parameters");
259 rd_bytes += r;
260 } while (rd_bytes != retlen);
261 }
262 return 0;
263}
264
265static void sp_flush_stream(void)
266{
267 if (sp_streamed_transmit_ops)
268 do {
269 unsigned char c;
270 if (read(sp_fd, &c, 1) != 1) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000271 sp_die("Error: cannot read from device (flushing stream)");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000272 }
273 if (c == S_NAK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000274 msg_perr("Error: NAK to a stream buffer operation\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000275 exit(1);
276 }
277 if (c != S_ACK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000278 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000279 exit(1);
280 }
281 } while (--sp_streamed_transmit_ops);
282 sp_streamed_transmit_ops = 0;
283 sp_streamed_transmit_bytes = 0;
284}
285
286static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
287{
288 uint8_t *sp;
289 if (sp_automatic_cmdcheck(cmd))
290 return 1;
291 sp = malloc(1 + parmlen);
292 if (!sp) sp_die("Error: cannot malloc command buffer");
293 sp[0] = cmd;
294 memcpy(&(sp[1]), parms, parmlen);
295 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
296 sp_flush_stream();
297 if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
298 sp_die("Error: cannot write command");
299 free(sp);
300 sp_streamed_transmit_ops += 1;
301 sp_streamed_transmit_bytes += 1 + parmlen;
302 return 0;
303}
304
305int serprog_init(void)
306{
307 uint16_t iface;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000308 unsigned char pgmname[17];
309 unsigned char rbuf[3];
310 unsigned char c;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000311 char *device;
312 char *baudport;
313 int have_device = 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000314
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000315 /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000316 device = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000317 if (device && strlen(device)) {
318 baudport = strstr(device, ":");
319 if (baudport) {
320 /* Split device from baudrate. */
321 *baudport = '\0';
322 baudport++;
323 }
324 if (!baudport || !strlen(baudport)) {
325 msg_perr("Error: No baudrate specified.\n"
326 "Use flashrom -p serprog:dev=/dev/device:baud\n");
327 free(device);
328 return 1;
329 }
330 if (strlen(device)) {
331 sp_fd = sp_openserport(device, atoi(baudport));
332 have_device++;
333 }
334 }
335 if (device && !strlen(device)) {
336 msg_perr("Error: No device specified.\n"
337 "Use flashrom -p serprog:dev=/dev/device:baud\n");
338 free(device);
339 return 1;
340 }
341 free(device);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000342
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000343 device = extract_programmer_param("ip");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000344 if (have_device && device) {
345 msg_perr("Error: Both host and device specified.\n"
346 "Please use either dev= or ip= but not both.\n");
347 free(device);
348 return 1;
349 }
350 if (device && strlen(device)) {
351 baudport = strstr(device, ":");
352 if (baudport) {
353 /* Split host from port. */
354 *baudport = '\0';
355 baudport++;
356 }
357 if (!baudport || !strlen(baudport)) {
358 msg_perr("Error: No port specified.\n"
359 "Use flashrom -p serprog:ip=ipaddr:port\n");
360 free(device);
361 return 1;
362 }
363 if (strlen(device)) {
364 sp_fd = sp_opensocket(device, atoi(baudport));
365 have_device++;
366 }
367 }
368 if (device && !strlen(device)) {
369 msg_perr("Error: No host specified.\n"
370 "Use flashrom -p serprog:ip=ipaddr:port\n");
371 free(device);
372 return 1;
373 }
374 free(device);
375
376 if (!have_device) {
377 msg_perr("Error: Neither host nor device specified.\n"
378 "Use flashrom -p serprog:dev=/dev/device:baud or "
379 "flashrom -p serprog:ip=ipaddr:port\n");
380 return 1;
381 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000382
David Hendricks8bb20212011-06-14 01:35:36 +0000383 if (register_shutdown(serprog_shutdown, NULL))
384 return 1;
385
Sean Nelson74e8af52010-01-10 01:06:23 +0000386 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000387
388 sp_check_avail_automatic = 0;
389
390 sp_synchronize();
391
Sean Nelson74e8af52010-01-10 01:06:23 +0000392 msg_pdbg(MSGHEADER "Synchronized\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000393
394 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000395 msg_perr("Error: NAK to Query Interface version\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000396 exit(1);
397 }
398
399 if (iface != 1) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000400 msg_perr("Error: Unknown interface version %d\n", iface);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000401 exit(1);
402 }
403
Sean Nelson74e8af52010-01-10 01:06:23 +0000404 msg_pdbg(MSGHEADER "Interface version ok.\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000405
406 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000407 msg_perr("Error: query command map not supported\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000408 exit(1);
409 }
410
411 sp_check_avail_automatic = 1;
412
413 /* Check for the minimum operational set of commands */
414 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000415 msg_perr("Error: Single byte read not supported\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000416 exit(1);
417 }
418 /* This could be translated to single byte reads (if missing), *
419 * but now we dont support that. */
420 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000421 msg_perr("Error: Read n bytes not supported\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000422 exit(1);
423 }
424 /* In the future one could switch to read-only mode if these *
425 * are not available. */
426 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000427 msg_perr("Error: Initialize operation buffer not supported\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000428 exit(1);
429 }
430 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000431 msg_perr("Error: Write to opbuf: write byte not supported\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000432 exit(1);
433 }
434 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000435 msg_perr("Error: Write to opbuf: delay not supported\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000436 exit(1);
437 }
438 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000439 msg_perr(
Urja Rannikkof3196df2009-07-21 13:02:59 +0000440 "Error: Execute operation buffer not supported\n");
441 exit(1);
442 }
443
444 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000445 msg_perr("Warning: NAK to query programmer name\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000446 strcpy((char *)pgmname, "(unknown)");
447 }
448 pgmname[16] = 0;
Sean Nelson74e8af52010-01-10 01:06:23 +0000449 msg_pinfo(MSGHEADER "Programmer name \"%s\"\n", pgmname);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000450
451 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000452 msg_perr("Warning: NAK to query serial buffer size\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000453 }
Sean Nelson74e8af52010-01-10 01:06:23 +0000454 msg_pdbg(MSGHEADER "serial buffer size %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000455 sp_device_serbuf_size);
456
457 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, &sp_device_opbuf_size)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000458 msg_perr("Warning: NAK to query operation buffer size\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000459 }
Sean Nelson74e8af52010-01-10 01:06:23 +0000460 msg_pdbg(MSGHEADER "operation buffer size %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000461 sp_device_opbuf_size);
462
463 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000464 msg_perr("Warning: NAK to query supported buses\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000465 c = CHIP_BUSTYPE_NONSPI; /* A reasonable default for now. */
466 }
467 buses_supported = c;
468
469 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000470 msg_perr("Error: NAK to initialize operation buffer\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000471 exit(1);
472 }
473
474 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000475 msg_pdbg(MSGHEADER "Write-n not supported");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000476 sp_max_write_n = 0;
477 } else {
478 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
479 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
480 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
Sean Nelson74e8af52010-01-10 01:06:23 +0000481 msg_pdbg(MSGHEADER "Maximum write-n length %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000482 sp_max_write_n);
483 sp_write_n_buf = malloc(sp_max_write_n);
484 if (!sp_write_n_buf) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000485 msg_perr("Error: cannot allocate memory for Write-n buffer\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000486 exit(1);
487 }
488 sp_write_n_bytes = 0;
489 }
490
491 if ((sp_check_commandavail(S_CMD_Q_RDNMAXLEN))
492 &&((sp_docommand(S_CMD_Q_RDNMAXLEN,0,NULL, 3, rbuf) == 0))) {
493 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
494 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
495 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
Sean Nelson74e8af52010-01-10 01:06:23 +0000496 msg_pdbg(MSGHEADER "Maximum read-n length %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000497 sp_max_read_n ? sp_max_read_n : (1<<24));
498 } else {
Sean Nelson74e8af52010-01-10 01:06:23 +0000499 msg_pdbg(MSGHEADER "Maximum read-n length not reported\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000500 sp_max_read_n = 0;
501 }
502
503 sp_prev_was_write = 0;
504 sp_streamed_transmit_ops = 0;
505 sp_streamed_transmit_bytes = 0;
506 sp_opbuf_usage = 0;
507 return 0;
508}
509
510/* Move an in flashrom buffer existing write-n operation to *
511 * the on-device operation buffer. */
512static void sp_pass_writen(void)
513{
514 unsigned char header[7];
Sean Nelson74e8af52010-01-10 01:06:23 +0000515 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000516 sp_write_n_bytes, sp_write_n_addr);
517 if (sp_streamed_transmit_bytes >=
518 (7 + sp_write_n_bytes + sp_device_serbuf_size))
519 sp_flush_stream();
520 /* In case it's just a single byte send it as a single write. */
521 if (sp_write_n_bytes == 1) {
522 sp_write_n_bytes = 0;
523 header[0] = (sp_write_n_addr >> 0) & 0xFF;
524 header[1] = (sp_write_n_addr >> 8) & 0xFF;
525 header[2] = (sp_write_n_addr >> 16) & 0xFF;
526 header[3] = sp_write_n_buf[0];
527 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
528 sp_opbuf_usage += 5;
529 return;
530 }
531 header[0] = S_CMD_O_WRITEN;
532 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
533 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
534 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
535 header[4] = (sp_write_n_addr >> 0) & 0xFF;
536 header[5] = (sp_write_n_addr >> 8) & 0xFF;
537 header[6] = (sp_write_n_addr >> 16) & 0xFF;
538 if (write(sp_fd, header, 7) != 7)
539 sp_die("Error: cannot write write-n command\n");
540 if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
541 sp_write_n_bytes)
542 sp_die("Error: cannot write write-n data");
543 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
544 sp_streamed_transmit_ops += 1;
545 sp_opbuf_usage += 7 + sp_write_n_bytes;
546 sp_write_n_bytes = 0;
547 sp_prev_was_write = 0;
548}
549
550static void sp_execute_opbuf_noflush(void)
551{
552 if ((sp_max_write_n) && (sp_write_n_bytes))
553 sp_pass_writen();
Peter Huewe73f8ec82011-01-24 19:15:51 +0000554 sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL);
Sean Nelson74e8af52010-01-10 01:06:23 +0000555 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000556 sp_opbuf_usage);
557 sp_opbuf_usage = 0;
558 sp_prev_was_write = 0;
559 return;
560}
561
562static void sp_execute_opbuf(void)
563{
564 sp_execute_opbuf_noflush();
565 sp_flush_stream();
566}
567
David Hendricks8bb20212011-06-14 01:35:36 +0000568static int serprog_shutdown(void *data)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000569{
Sean Nelson74e8af52010-01-10 01:06:23 +0000570 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000571 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
572 sp_execute_opbuf();
573 close(sp_fd);
574 if (sp_max_write_n)
575 free(sp_write_n_buf);
576 return 0;
577}
578
579static void sp_check_opbuf_usage(int bytes_to_be_added)
580{
581 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
582 sp_execute_opbuf();
583 /* If this happens in the mid of an page load the page load *
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000584 * will probably fail. */
Sean Nelson74e8af52010-01-10 01:06:23 +0000585 msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000586 }
587}
588
589void serprog_chip_writeb(uint8_t val, chipaddr addr)
590{
Sean Nelson74e8af52010-01-10 01:06:23 +0000591 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000592 if (sp_max_write_n) {
593 if ((sp_prev_was_write)
594 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
595 sp_write_n_buf[sp_write_n_bytes++] = val;
596 } else {
597 if ((sp_prev_was_write) && (sp_write_n_bytes))
598 sp_pass_writen();
599 sp_prev_was_write = 1;
600 sp_write_n_addr = addr;
601 sp_write_n_bytes = 1;
602 sp_write_n_buf[0] = val;
603 }
604 sp_check_opbuf_usage(7 + sp_write_n_bytes);
605 if (sp_write_n_bytes >= sp_max_write_n)
606 sp_pass_writen();
607 } else {
608 /* We will have to do single writeb ops. */
609 unsigned char writeb_parm[4];
610 sp_check_opbuf_usage(6);
611 writeb_parm[0] = (addr >> 0) & 0xFF;
612 writeb_parm[1] = (addr >> 8) & 0xFF;
613 writeb_parm[2] = (addr >> 16) & 0xFF;
614 writeb_parm[3] = val;
615 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
616 sp_opbuf_usage += 5;
617 }
618}
619
620uint8_t serprog_chip_readb(const chipaddr addr)
621{
622 unsigned char c;
623 unsigned char buf[3];
624 /* Will stream the read operation - eg. add it to the stream buffer, *
625 * then flush the buffer, then read the read answer. */
626 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
627 sp_execute_opbuf_noflush();
628 buf[0] = ((addr >> 0) & 0xFF);
629 buf[1] = ((addr >> 8) & 0xFF);
630 buf[2] = ((addr >> 16) & 0xFF);
631 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
632 sp_flush_stream();
633 if (read(sp_fd, &c, 1) != 1)
634 sp_die("readb byteread");
Sean Nelson74e8af52010-01-10 01:06:23 +0000635 msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000636 return c;
637}
638
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000639/* Local version that really does the job, doesn't care of max_read_n. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000640static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
641{
642 int rd_bytes = 0;
643 unsigned char sbuf[6];
Sean Nelson74e8af52010-01-10 01:06:23 +0000644 msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000645 /* Stream the read-n -- as above. */
646 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
647 sp_execute_opbuf_noflush();
648 sbuf[0] = ((addr >> 0) & 0xFF);
649 sbuf[1] = ((addr >> 8) & 0xFF);
650 sbuf[2] = ((addr >> 16) & 0xFF);
651 sbuf[3] = ((len >> 0) & 0xFF);
652 sbuf[4] = ((len >> 8) & 0xFF);
653 sbuf[5] = ((len >> 16) & 0xFF);
654 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
655 sp_flush_stream();
656 do {
657 int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
658 if (r <= 0)
659 sp_die("Error: cannot read read-n data");
660 rd_bytes += r;
661 } while (rd_bytes != len);
662 return;
663}
664
665/* The externally called version that makes sure that max_read_n is obeyed. */
666void serprog_chip_readn(uint8_t * buf, const chipaddr addr, size_t len)
667{
668 size_t lenm = len;
669 chipaddr addrm = addr;
670 while ((sp_max_read_n)&&(lenm > sp_max_read_n)) {
671 sp_do_read_n(&(buf[addrm-addr]),addrm,sp_max_read_n);
672 addrm += sp_max_read_n;
673 lenm -= sp_max_read_n;
674 }
675 if (lenm) sp_do_read_n(&(buf[addrm-addr]),addrm,lenm);
676}
677
678void serprog_delay(int delay)
679{
680 unsigned char buf[4];
Sean Nelson74e8af52010-01-10 01:06:23 +0000681 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000682 if ((sp_max_write_n) && (sp_write_n_bytes))
683 sp_pass_writen();
684 sp_check_opbuf_usage(5);
685 buf[0] = ((delay >> 0) & 0xFF);
686 buf[1] = ((delay >> 8) & 0xFF);
687 buf[2] = ((delay >> 16) & 0xFF);
688 buf[3] = ((delay >> 24) & 0xFF);
689 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
690 sp_opbuf_usage += 5;
691 sp_prev_was_write = 0;
692}