blob: 65442452010f25387edde9592c81b782815dd8d9 [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>
25#include "flash.h"
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000026#include <string.h>
Urja Rannikko22915352009-06-23 11:33:43 +000027#include <ctype.h>
28#include <fcntl.h>
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <arpa/inet.h>
32#include <netinet/in.h>
33#include <netinet/tcp.h>
34#include <netdb.h>
35#include <sys/stat.h>
36#include <errno.h>
Urja Rannikko22915352009-06-23 11:33:43 +000037#include <inttypes.h>
38#include <termios.h>
Urja Rannikkof3196df2009-07-21 13:02:59 +000039
40#define MSGHEADER "serprog:"
41
42#define S_ACK 0x06
43#define S_NAK 0x15
44#define S_CMD_NOP 0x00 /* No operation */
45#define S_CMD_Q_IFACE 0x01 /* Query interface version */
46#define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */
47#define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */
48#define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */
49#define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */
50#define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */
51#define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */
52#define S_CMD_Q_WRNMAXLEN 0x08 /* Query opbuf-write-N maximum lenght */
53#define S_CMD_R_BYTE 0x09 /* Read a single byte */
54#define S_CMD_R_NBYTES 0x0A /* Read n bytes */
55#define S_CMD_O_INIT 0x0B /* Initialize operation buffer */
56#define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */
57#define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */
58#define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */
59#define S_CMD_O_EXEC 0x0F /* Execute operation buffer */
60#define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */
61#define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */
62#define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s). */
63
Urja Rannikkof3196df2009-07-21 13:02:59 +000064static uint16_t sp_device_serbuf_size = 16;
65static uint16_t sp_device_opbuf_size = 300;
66/* Bitmap of supported commands */
67static uint8_t sp_cmdmap[32];
68
69/* sp_prev_was_write used to detect writes with continouous addresses
70 and combine them to write-n's */
71static int sp_prev_was_write = 0;
72/* sp_write_n_addr used as the starting addr of the currently
73 combined write-n operation */
74static uint32_t sp_write_n_addr;
75/* The maximum length of an write_n operation; 0 = write-n not supported */
76static uint32_t sp_max_write_n = 0;
77/* The maximum length of a read_n operation; 0 = 2^24 */
78static uint32_t sp_max_read_n = 0;
79
80/* A malloc'd buffer for combining the operation's data
81 and a counter that tells how much data is there. */
82static uint8_t *sp_write_n_buf;
83static uint32_t sp_write_n_bytes = 0;
84
85/* sp_streamed_* used for flow control checking */
86static int sp_streamed_transmit_ops = 0;
87static int sp_streamed_transmit_bytes = 0;
88
89/* sp_opbuf_usage used for counting the amount of
90 on-device operation buffer used */
91static int sp_opbuf_usage = 0;
92/* if true causes sp_docommand to automatically check
93 whether the command is supported before doing it */
94static int sp_check_avail_automatic = 0;
95
Urja Rannikkof3196df2009-07-21 13:02:59 +000096static int sp_opensocket(char *ip, unsigned int port)
97{
98 int flag = 1;
99 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000100 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +0000101 int sock;
102 printf_debug(MSGHEADER "IP %s port %d\n", ip, port);
103 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
104 if (sock < 0)
105 sp_die("Error: serprog cannot open socket");
106 hostPtr = gethostbyname(ip);
107 if (NULL == hostPtr) {
108 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
109 if (NULL == hostPtr)
110 sp_die("Error: cannot resolve");
111 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000112 sp.si.sin_family = AF_INET;
113 sp.si.sin_port = htons(port);
114 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length);
115 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000116 close(sock);
117 sp_die("Error: serprog cannot connect");
118 }
119 /* We are latency limited, and sometimes do write-write-read *
120 * (write-n) - so enable TCP_NODELAY. */
121 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
122 return sock;
123}
124
Urja Rannikkof3196df2009-07-21 13:02:59 +0000125static int sp_sync_read_timeout(int loops)
126{
127 int i;
128 unsigned char c;
129 for (i = 0; i < loops; i++) {
130 ssize_t rv;
131 rv = read(sp_fd, &c, 1);
132 if (rv == 1)
133 return c;
134 if ((rv == -1) && (errno != EAGAIN))
135 sp_die("read");
136 usleep(10 * 1000); /* 10ms units */
137 }
138 return -1;
139}
140
141/* Synchronize: a bit tricky algorhytm that tries to (and in my tests has *
142 * always succeeded in) bring the serial protocol to known waiting-for- *
143 * command state - uses nonblocking read - rest of the driver uses *
144 * blocking read - TODO: add an alarm() timer for the rest of the app on *
145 * serial operations, though not such a big issue as the first thing to *
146 * do is synchronize (eg. check that device is alive). */
147static void sp_synchronize(void)
148{
149 int i;
150 int flags = fcntl(sp_fd, F_GETFL);
151 unsigned char buf[8];
152 flags |= O_NONBLOCK;
153 fcntl(sp_fd, F_SETFL, flags);
154 /* First sends 8 NOPs, then flushes the return data - should cause *
155 * the device serial parser to get to a sane state, unless if it *
156 * is waiting for a real long write-n. */
157 memset(buf, S_CMD_NOP, 8);
158 if (write(sp_fd, buf, 8) != 8)
159 sp_die("flush write");
160 /* A second should be enough to get all the answers to the buffer */
161 usleep(1000 * 1000);
162 sp_flush_incoming();
163
164 /* Then try upto 8 times to send syncnop and get the correct special *
165 * return of NAK+ACK. Timing note: upto 10 characters, 10*50ms = *
166 * upto 500ms per try, 8*0.5s = 4s; +1s (above) = upto 5s sync *
167 * attempt, ~1s if immediate success. */
168 for (i = 0; i < 8; i++) {
169 int n;
170 unsigned char c = S_CMD_SYNCNOP;
171 if (write(sp_fd, &c, 1) != 1)
172 sp_die("sync write");
173 printf_debug(".");
174 fflush(stdout);
175 for (n = 0; n < 10; n++) {
176 c = sp_sync_read_timeout(5); /* wait upto 50ms */
177 if (c != S_NAK)
178 continue;
179 c = sp_sync_read_timeout(2);
180 if (c != S_ACK)
181 continue;
182 c = S_CMD_SYNCNOP;
183 if (write(sp_fd, &c, 1) != 1)
184 sp_die("sync write");
185 c = sp_sync_read_timeout(50);
186 if (c != S_NAK)
187 break; /* fail */
188 c = sp_sync_read_timeout(10);
189 if (c != S_ACK)
190 break; /* fail */
191 /* Ok, synchronized; back to blocking reads and return. */
192 flags &= ~O_NONBLOCK;
193 fcntl(sp_fd, F_SETFL, flags);
194 printf_debug("\n");
195 return;
196 }
197 }
198 fprintf(stderr,
199 "Error: cannot synchronize protocol\n"
200 "- check communications and reset device?\n");
201 exit(1);
202}
203
204static int sp_check_commandavail(uint8_t command)
205{
206 int byteoffs, bitoffs;
207 byteoffs = command / 8;
208 bitoffs = command % 8;
209 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
210}
211
212static int sp_automatic_cmdcheck(uint8_t cmd)
213{
214 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
215 printf_debug ("Warning: Automatic command availability check"
216 " failed for cmd %d - wont execute cmd\n",cmd);
217 return 1;
218 }
219 return 0;
220}
221
222static int sp_docommand(uint8_t command, uint32_t parmlen,
223 uint8_t * params, uint32_t retlen, void *retparms)
224{
225 unsigned char *sendpacket;
226 unsigned char c;
227 if (sp_automatic_cmdcheck(command))
228 return 1;
229 sendpacket = malloc(1 + parmlen);
230 if (!sendpacket)
231 sp_die("Error: cannot malloc command buffer");
232 sendpacket[0] = command;
233 memcpy(&(sendpacket[1]), params, parmlen);
234 if (write(sp_fd, sendpacket, 1 + parmlen) != (1 + parmlen)) {
235 sp_die("Error: cannot write command");
236 }
237 free(sendpacket);
238 if (read(sp_fd, &c, 1) != 1)
239 sp_die("Error: cannot read from device");
240 if (c == S_NAK) return 1;
241 if (c != S_ACK) {
242 fprintf(stderr,
243 "Error: invalid response 0x%02X from device\n",c);
244 exit(1);
245 }
246 if (retlen) {
247 int rd_bytes = 0;
248 do {
249 int r;
250 r = read(sp_fd, retparms + rd_bytes,
251 retlen - rd_bytes);
252 if (r <= 0) sp_die
253 ("Error: cannot read return parameters");
254 rd_bytes += r;
255 } while (rd_bytes != retlen);
256 }
257 return 0;
258}
259
260static void sp_flush_stream(void)
261{
262 if (sp_streamed_transmit_ops)
263 do {
264 unsigned char c;
265 if (read(sp_fd, &c, 1) != 1) {
266 sp_die
267 ("Error: cannot read from device (flushing stream)");
268 }
269 if (c == S_NAK) {
270 fprintf(stderr,
271 "Error: NAK to a stream buffer operation\n");
272 exit(1);
273 }
274 if (c != S_ACK) {
275 fprintf(stderr,
276 "Error: Invalid reply 0x%02X from device\n",
277 c);
278 exit(1);
279 }
280 } while (--sp_streamed_transmit_ops);
281 sp_streamed_transmit_ops = 0;
282 sp_streamed_transmit_bytes = 0;
283}
284
285static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
286{
287 uint8_t *sp;
288 if (sp_automatic_cmdcheck(cmd))
289 return 1;
290 sp = malloc(1 + parmlen);
291 if (!sp) sp_die("Error: cannot malloc command buffer");
292 sp[0] = cmd;
293 memcpy(&(sp[1]), parms, parmlen);
294 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
295 sp_flush_stream();
296 if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
297 sp_die("Error: cannot write command");
298 free(sp);
299 sp_streamed_transmit_ops += 1;
300 sp_streamed_transmit_bytes += 1 + parmlen;
301 return 0;
302}
303
304int serprog_init(void)
305{
306 uint16_t iface;
307 int len;
308 unsigned char pgmname[17];
309 unsigned char rbuf[3];
310 unsigned char c;
311 char *num;
312 char *dev;
313 printf_debug("%s\n", __func__);
314 /* the parameter is either of format "/dev/device:baud" or "ip:port" */
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000315 if ((!programmer_param) || (!strlen(programmer_param))) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000316 nodevice:
317 fprintf(stderr,
318 "Error: No device/host given for the serial programmer driver.\n"
319 "Use flashrom -p serprog=/dev/device:baud or flashrom -p serprog=ip:port\n");
320 exit(1);
321 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000322 num = strstr(programmer_param, ":");
323 len = num - programmer_param;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000324 if (!len) goto nodevice;
325 if (!num) {
326 fprintf(stderr,
327 "Error: No port or baudrate specified to serial programmer driver.\n"
328 "Use flashrom -p serprog=/dev/device:baud or flashrom -p serprog=ip:port\n");
329 exit(1);
330 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000331 len = num - programmer_param;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000332 dev = malloc(len + 1);
333 if (!dev) sp_die("Error: memory allocation failure");
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000334 memcpy(dev, programmer_param, len);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000335 dev[len] = 0;
336 num = strdup(num + 1);
337 if (!num) sp_die("Error: memory allocation failure");
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000338 free(programmer_param);
339 programmer_param = NULL;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000340
341 if (dev[0] == '/') sp_fd = sp_openserport(dev, atoi(num));
342 else sp_fd = sp_opensocket(dev, atoi(num));
343
344 free(dev); dev = NULL;
345 free(num); num = NULL;
346
347 printf_debug(MSGHEADER "connected - attempting to synchronize\n");
348
349 sp_check_avail_automatic = 0;
350
351 sp_synchronize();
352
353 printf_debug(MSGHEADER "Synchronized\n");
354
355 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
356 fprintf(stderr, "Error: NAK to Query Interface version\n");
357 exit(1);
358 }
359
360 if (iface != 1) {
361 fprintf(stderr, "Error: Unknown interface version %d\n", iface);
362 exit(1);
363 }
364
365 printf_debug(MSGHEADER "Interface version ok.\n");
366
367 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
368 fprintf(stderr, "Error: query command map not supported\n");
369 exit(1);
370 }
371
372 sp_check_avail_automatic = 1;
373
374 /* Check for the minimum operational set of commands */
375 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
376 fprintf(stderr, "Error: Single byte read not supported\n");
377 exit(1);
378 }
379 /* This could be translated to single byte reads (if missing), *
380 * but now we dont support that. */
381 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
382 fprintf(stderr, "Error: Read n bytes not supported\n");
383 exit(1);
384 }
385 /* In the future one could switch to read-only mode if these *
386 * are not available. */
387 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
388 fprintf(stderr,
389 "Error: Initialize operation buffer not supported\n");
390 exit(1);
391 }
392 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
393 fprintf(stderr,
394 "Error: Write to opbuf: write byte not supported\n");
395 exit(1);
396 }
397 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
398 fprintf(stderr, "Error: Write to opbuf: delay not supported\n");
399 exit(1);
400 }
401 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
402 fprintf(stderr,
403 "Error: Execute operation buffer not supported\n");
404 exit(1);
405 }
406
407 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
408 fprintf(stderr, "Warning: NAK to query programmer name\n");
409 strcpy((char *)pgmname, "(unknown)");
410 }
411 pgmname[16] = 0;
412 printf(MSGHEADER "Programmer name \"%s\"\n", pgmname);
413
414 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
415 fprintf(stderr, "Warning: NAK to query serial buffer size\n");
416 }
417 printf_debug(MSGHEADER "serial buffer size %d\n",
418 sp_device_serbuf_size);
419
420 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, &sp_device_opbuf_size)) {
421 fprintf(stderr,
422 "Warning: NAK to query operation buffer size\n");
423 }
424 printf_debug(MSGHEADER "operation buffer size %d\n",
425 sp_device_opbuf_size);
426
427 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
428 fprintf(stderr, "Warning: NAK to query supported buses\n");
429 c = CHIP_BUSTYPE_NONSPI; /* A reasonable default for now. */
430 }
431 buses_supported = c;
432
433 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
434 fprintf(stderr, "Error: NAK to initialize operation buffer\n");
435 exit(1);
436 }
437
438 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
439 printf_debug(MSGHEADER "Write-n not supported");
440 sp_max_write_n = 0;
441 } else {
442 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
443 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
444 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
445 printf_debug(MSGHEADER "Maximum write-n length %d\n",
446 sp_max_write_n);
447 sp_write_n_buf = malloc(sp_max_write_n);
448 if (!sp_write_n_buf) {
449 fprintf(stderr,
450 "Error: cannot allocate memory for Write-n buffer\n");
451 exit(1);
452 }
453 sp_write_n_bytes = 0;
454 }
455
456 if ((sp_check_commandavail(S_CMD_Q_RDNMAXLEN))
457 &&((sp_docommand(S_CMD_Q_RDNMAXLEN,0,NULL, 3, rbuf) == 0))) {
458 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
459 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
460 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
461 printf_debug(MSGHEADER "Maximum read-n length %d\n",
462 sp_max_read_n ? sp_max_read_n : (1<<24));
463 } else {
464 printf_debug(MSGHEADER "Maximum read-n length not reported\n");
465 sp_max_read_n = 0;
466 }
467
468 sp_prev_was_write = 0;
469 sp_streamed_transmit_ops = 0;
470 sp_streamed_transmit_bytes = 0;
471 sp_opbuf_usage = 0;
472 return 0;
473}
474
475/* Move an in flashrom buffer existing write-n operation to *
476 * the on-device operation buffer. */
477static void sp_pass_writen(void)
478{
479 unsigned char header[7];
480 printf_debug(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
481 sp_write_n_bytes, sp_write_n_addr);
482 if (sp_streamed_transmit_bytes >=
483 (7 + sp_write_n_bytes + sp_device_serbuf_size))
484 sp_flush_stream();
485 /* In case it's just a single byte send it as a single write. */
486 if (sp_write_n_bytes == 1) {
487 sp_write_n_bytes = 0;
488 header[0] = (sp_write_n_addr >> 0) & 0xFF;
489 header[1] = (sp_write_n_addr >> 8) & 0xFF;
490 header[2] = (sp_write_n_addr >> 16) & 0xFF;
491 header[3] = sp_write_n_buf[0];
492 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
493 sp_opbuf_usage += 5;
494 return;
495 }
496 header[0] = S_CMD_O_WRITEN;
497 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
498 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
499 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
500 header[4] = (sp_write_n_addr >> 0) & 0xFF;
501 header[5] = (sp_write_n_addr >> 8) & 0xFF;
502 header[6] = (sp_write_n_addr >> 16) & 0xFF;
503 if (write(sp_fd, header, 7) != 7)
504 sp_die("Error: cannot write write-n command\n");
505 if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
506 sp_write_n_bytes)
507 sp_die("Error: cannot write write-n data");
508 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
509 sp_streamed_transmit_ops += 1;
510 sp_opbuf_usage += 7 + sp_write_n_bytes;
511 sp_write_n_bytes = 0;
512 sp_prev_was_write = 0;
513}
514
515static void sp_execute_opbuf_noflush(void)
516{
517 if ((sp_max_write_n) && (sp_write_n_bytes))
518 sp_pass_writen();
519 sp_stream_buffer_op(S_CMD_O_EXEC, 0, 0);
520 printf_debug(MSGHEADER "Executed operation buffer of %d bytes\n",
521 sp_opbuf_usage);
522 sp_opbuf_usage = 0;
523 sp_prev_was_write = 0;
524 return;
525}
526
527static void sp_execute_opbuf(void)
528{
529 sp_execute_opbuf_noflush();
530 sp_flush_stream();
531}
532
533int serprog_shutdown(void)
534{
535 printf_debug("%s\n", __func__);
536 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
537 sp_execute_opbuf();
538 close(sp_fd);
539 if (sp_max_write_n)
540 free(sp_write_n_buf);
541 return 0;
542}
543
544static void sp_check_opbuf_usage(int bytes_to_be_added)
545{
546 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
547 sp_execute_opbuf();
548 /* If this happens in the mid of an page load the page load *
549 * will propably fail. */
550 printf_debug(MSGHEADER
551 "Warning: executed operation buffer due to size reasons\n");
552 }
553}
554
555void serprog_chip_writeb(uint8_t val, chipaddr addr)
556{
557 printf_debug("%s\n", __func__);
558 if (sp_max_write_n) {
559 if ((sp_prev_was_write)
560 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
561 sp_write_n_buf[sp_write_n_bytes++] = val;
562 } else {
563 if ((sp_prev_was_write) && (sp_write_n_bytes))
564 sp_pass_writen();
565 sp_prev_was_write = 1;
566 sp_write_n_addr = addr;
567 sp_write_n_bytes = 1;
568 sp_write_n_buf[0] = val;
569 }
570 sp_check_opbuf_usage(7 + sp_write_n_bytes);
571 if (sp_write_n_bytes >= sp_max_write_n)
572 sp_pass_writen();
573 } else {
574 /* We will have to do single writeb ops. */
575 unsigned char writeb_parm[4];
576 sp_check_opbuf_usage(6);
577 writeb_parm[0] = (addr >> 0) & 0xFF;
578 writeb_parm[1] = (addr >> 8) & 0xFF;
579 writeb_parm[2] = (addr >> 16) & 0xFF;
580 writeb_parm[3] = val;
581 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
582 sp_opbuf_usage += 5;
583 }
584}
585
586uint8_t serprog_chip_readb(const chipaddr addr)
587{
588 unsigned char c;
589 unsigned char buf[3];
590 /* Will stream the read operation - eg. add it to the stream buffer, *
591 * then flush the buffer, then read the read answer. */
592 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
593 sp_execute_opbuf_noflush();
594 buf[0] = ((addr >> 0) & 0xFF);
595 buf[1] = ((addr >> 8) & 0xFF);
596 buf[2] = ((addr >> 16) & 0xFF);
597 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
598 sp_flush_stream();
599 if (read(sp_fd, &c, 1) != 1)
600 sp_die("readb byteread");
601 printf_debug("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
602 return c;
603}
604
605/* Local version that really does the job, doesnt care of max_read_n. */
606static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
607{
608 int rd_bytes = 0;
609 unsigned char sbuf[6];
610 printf_debug("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
611 /* Stream the read-n -- as above. */
612 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
613 sp_execute_opbuf_noflush();
614 sbuf[0] = ((addr >> 0) & 0xFF);
615 sbuf[1] = ((addr >> 8) & 0xFF);
616 sbuf[2] = ((addr >> 16) & 0xFF);
617 sbuf[3] = ((len >> 0) & 0xFF);
618 sbuf[4] = ((len >> 8) & 0xFF);
619 sbuf[5] = ((len >> 16) & 0xFF);
620 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
621 sp_flush_stream();
622 do {
623 int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
624 if (r <= 0)
625 sp_die("Error: cannot read read-n data");
626 rd_bytes += r;
627 } while (rd_bytes != len);
628 return;
629}
630
631/* The externally called version that makes sure that max_read_n is obeyed. */
632void serprog_chip_readn(uint8_t * buf, const chipaddr addr, size_t len)
633{
634 size_t lenm = len;
635 chipaddr addrm = addr;
636 while ((sp_max_read_n)&&(lenm > sp_max_read_n)) {
637 sp_do_read_n(&(buf[addrm-addr]),addrm,sp_max_read_n);
638 addrm += sp_max_read_n;
639 lenm -= sp_max_read_n;
640 }
641 if (lenm) sp_do_read_n(&(buf[addrm-addr]),addrm,lenm);
642}
643
644void serprog_delay(int delay)
645{
646 unsigned char buf[4];
647 printf_debug("%s\n", __func__);
648 if ((sp_max_write_n) && (sp_write_n_bytes))
649 sp_pass_writen();
650 sp_check_opbuf_usage(5);
651 buf[0] = ((delay >> 0) & 0xFF);
652 buf[1] = ((delay >> 8) & 0xFF);
653 buf[2] = ((delay >> 16) & 0xFF);
654 buf[3] = ((delay >> 24) & 0xFF);
655 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
656 sp_opbuf_usage += 5;
657 sp_prev_was_write = 0;
658}