blob: 81ef130a0f0b7359ab2ff93ea211cc5a5a88d6c5 [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
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include <fcntl.h>
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <arpa/inet.h>
29#include <netinet/in.h>
30#include <netinet/tcp.h>
31#include <netdb.h>
32#include <sys/stat.h>
33#include <errno.h>
34#include <stdio.h>
35#include <unistd.h>
36#include <inttypes.h>
37#include <termios.h>
38#include "flash.h"
39
40char *serprog_param = NULL;
41
Urja Rannikkof3196df2009-07-21 13:02:59 +000042#define SERPROG_SUPPORT 1
43
Urja Rannikko22915352009-06-23 11:33:43 +000044#if SERPROG_SUPPORT == 1
Urja Rannikkof3196df2009-07-21 13:02:59 +000045
46#define MSGHEADER "serprog:"
47
48#define S_ACK 0x06
49#define S_NAK 0x15
50#define S_CMD_NOP 0x00 /* No operation */
51#define S_CMD_Q_IFACE 0x01 /* Query interface version */
52#define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */
53#define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */
54#define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */
55#define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */
56#define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */
57#define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */
58#define S_CMD_Q_WRNMAXLEN 0x08 /* Query opbuf-write-N maximum lenght */
59#define S_CMD_R_BYTE 0x09 /* Read a single byte */
60#define S_CMD_R_NBYTES 0x0A /* Read n bytes */
61#define S_CMD_O_INIT 0x0B /* Initialize operation buffer */
62#define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */
63#define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */
64#define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */
65#define S_CMD_O_EXEC 0x0F /* Execute operation buffer */
66#define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */
67#define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */
68#define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s). */
69
70static int sp_fd;
71
72static uint16_t sp_device_serbuf_size = 16;
73static uint16_t sp_device_opbuf_size = 300;
74/* Bitmap of supported commands */
75static uint8_t sp_cmdmap[32];
76
77/* sp_prev_was_write used to detect writes with continouous addresses
78 and combine them to write-n's */
79static int sp_prev_was_write = 0;
80/* sp_write_n_addr used as the starting addr of the currently
81 combined write-n operation */
82static uint32_t sp_write_n_addr;
83/* The maximum length of an write_n operation; 0 = write-n not supported */
84static uint32_t sp_max_write_n = 0;
85/* The maximum length of a read_n operation; 0 = 2^24 */
86static uint32_t sp_max_read_n = 0;
87
88/* A malloc'd buffer for combining the operation's data
89 and a counter that tells how much data is there. */
90static uint8_t *sp_write_n_buf;
91static uint32_t sp_write_n_bytes = 0;
92
93/* sp_streamed_* used for flow control checking */
94static int sp_streamed_transmit_ops = 0;
95static int sp_streamed_transmit_bytes = 0;
96
97/* sp_opbuf_usage used for counting the amount of
98 on-device operation buffer used */
99static int sp_opbuf_usage = 0;
100/* if true causes sp_docommand to automatically check
101 whether the command is supported before doing it */
102static int sp_check_avail_automatic = 0;
103
104static void sp_die(char *msg)
105{
106 perror(msg);
107 exit(1);
108}
109
110static int sp_opensocket(char *ip, unsigned int port)
111{
112 int flag = 1;
113 struct hostent *hostPtr = NULL;
114 struct sockaddr_in sp;
115 int sock;
116 printf_debug(MSGHEADER "IP %s port %d\n", ip, port);
117 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
118 if (sock < 0)
119 sp_die("Error: serprog cannot open socket");
120 hostPtr = gethostbyname(ip);
121 if (NULL == hostPtr) {
122 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
123 if (NULL == hostPtr)
124 sp_die("Error: cannot resolve");
125 }
126 memset(&sp, 0, sizeof(sp));
127 sp.sin_family = AF_INET;
128 sp.sin_port = htons(port);
129 (void)memcpy(&sp.sin_addr, hostPtr->h_addr, hostPtr->h_length);
130 if (connect(sock, (struct sockaddr *)&sp, sizeof(sp)) < 0) {
131 close(sock);
132 sp_die("Error: serprog cannot connect");
133 }
134 /* We are latency limited, and sometimes do write-write-read *
135 * (write-n) - so enable TCP_NODELAY. */
136 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
137 return sock;
138}
139
140struct baudentry {
141 int flag;
142 unsigned int baud;
143};
144
145/* I'd like if the C preprocessor could have directives in macros */
146#define BAUDENTRY(baud) { B##baud, baud },
147static const struct baudentry sp_baudtable[] = {
148 BAUDENTRY(9600)
149 BAUDENTRY(19200)
150 BAUDENTRY(38400)
151 BAUDENTRY(57600)
152 BAUDENTRY(115200)
153#ifdef B230400
154 BAUDENTRY(230400)
155#endif
156#ifdef B460800
157 BAUDENTRY(460800)
158#endif
159#ifdef B500000
160 BAUDENTRY(500000)
161#endif
162#ifdef B576000
163 BAUDENTRY(576000)
164#endif
165#ifdef B921600
166 BAUDENTRY(921600)
167#endif
168#ifdef B1000000
169 BAUDENTRY(1000000)
170#endif
171#ifdef B1152000
172 BAUDENTRY(1152000)
173#endif
174#ifdef B1500000
175 BAUDENTRY(1500000)
176#endif
177#ifdef B2000000
178 BAUDENTRY(2000000)
179#endif
180#ifdef B2500000
181 BAUDENTRY(2500000)
182#endif
183#ifdef B3000000
184 BAUDENTRY(3000000)
185#endif
186#ifdef B3500000
187 BAUDENTRY(3500000)
188#endif
189#ifdef B4000000
190 BAUDENTRY(4000000)
191#endif
192 {0, 0} /* Terminator */
193};
194
195static int sp_openserport(char *dev, unsigned int baud)
196{
197 struct termios options;
198 int fd, i;
199 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
200 if (fd < 0)
201 sp_die("Error: cannot open serial port");
202 fcntl(fd, F_SETFL, 0);
203 tcgetattr(fd, &options);
204 for (i = 0;; i++) {
205 if (sp_baudtable[i].baud == 0) {
206 close(fd);
207 fprintf(stderr,
208 "Error: cannot configure for baudrate %d\n",
209 baud);
210 exit(1);
211 }
212 if (sp_baudtable[i].baud == baud) {
213 cfsetispeed(&options, sp_baudtable[i].flag);
214 cfsetospeed(&options, sp_baudtable[i].flag);
215 break;
216 }
217 }
218 options.c_cflag &= ~PARENB;
219 options.c_cflag &= ~CSTOPB;
220 options.c_cflag &= ~CSIZE;
221 options.c_cflag |= CS8;
222 options.c_cflag &= ~CRTSCTS;
223 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
224 options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
225 options.c_oflag &= ~OPOST;
226 options.c_cflag |= (CLOCAL | CREAD);
227 tcsetattr(fd, TCSANOW, &options);
228 return fd;
229}
230
231static void sp_flush_incoming(void)
232{
233 int i;
234 for (i=0;i<100;i++) { /* In case the device doesnt do EAGAIN, just read 0 */
235 unsigned char flush[16];
236 ssize_t rv;
237 rv = read(sp_fd, flush, sizeof(flush));
238 if ((rv == -1) && (errno == EAGAIN))
239 break;
240 if (rv == -1)
241 sp_die("flush read");
242 }
243 return;
244}
245
246static int sp_sync_read_timeout(int loops)
247{
248 int i;
249 unsigned char c;
250 for (i = 0; i < loops; i++) {
251 ssize_t rv;
252 rv = read(sp_fd, &c, 1);
253 if (rv == 1)
254 return c;
255 if ((rv == -1) && (errno != EAGAIN))
256 sp_die("read");
257 usleep(10 * 1000); /* 10ms units */
258 }
259 return -1;
260}
261
262/* Synchronize: a bit tricky algorhytm that tries to (and in my tests has *
263 * always succeeded in) bring the serial protocol to known waiting-for- *
264 * command state - uses nonblocking read - rest of the driver uses *
265 * blocking read - TODO: add an alarm() timer for the rest of the app on *
266 * serial operations, though not such a big issue as the first thing to *
267 * do is synchronize (eg. check that device is alive). */
268static void sp_synchronize(void)
269{
270 int i;
271 int flags = fcntl(sp_fd, F_GETFL);
272 unsigned char buf[8];
273 flags |= O_NONBLOCK;
274 fcntl(sp_fd, F_SETFL, flags);
275 /* First sends 8 NOPs, then flushes the return data - should cause *
276 * the device serial parser to get to a sane state, unless if it *
277 * is waiting for a real long write-n. */
278 memset(buf, S_CMD_NOP, 8);
279 if (write(sp_fd, buf, 8) != 8)
280 sp_die("flush write");
281 /* A second should be enough to get all the answers to the buffer */
282 usleep(1000 * 1000);
283 sp_flush_incoming();
284
285 /* Then try upto 8 times to send syncnop and get the correct special *
286 * return of NAK+ACK. Timing note: upto 10 characters, 10*50ms = *
287 * upto 500ms per try, 8*0.5s = 4s; +1s (above) = upto 5s sync *
288 * attempt, ~1s if immediate success. */
289 for (i = 0; i < 8; i++) {
290 int n;
291 unsigned char c = S_CMD_SYNCNOP;
292 if (write(sp_fd, &c, 1) != 1)
293 sp_die("sync write");
294 printf_debug(".");
295 fflush(stdout);
296 for (n = 0; n < 10; n++) {
297 c = sp_sync_read_timeout(5); /* wait upto 50ms */
298 if (c != S_NAK)
299 continue;
300 c = sp_sync_read_timeout(2);
301 if (c != S_ACK)
302 continue;
303 c = S_CMD_SYNCNOP;
304 if (write(sp_fd, &c, 1) != 1)
305 sp_die("sync write");
306 c = sp_sync_read_timeout(50);
307 if (c != S_NAK)
308 break; /* fail */
309 c = sp_sync_read_timeout(10);
310 if (c != S_ACK)
311 break; /* fail */
312 /* Ok, synchronized; back to blocking reads and return. */
313 flags &= ~O_NONBLOCK;
314 fcntl(sp_fd, F_SETFL, flags);
315 printf_debug("\n");
316 return;
317 }
318 }
319 fprintf(stderr,
320 "Error: cannot synchronize protocol\n"
321 "- check communications and reset device?\n");
322 exit(1);
323}
324
325static int sp_check_commandavail(uint8_t command)
326{
327 int byteoffs, bitoffs;
328 byteoffs = command / 8;
329 bitoffs = command % 8;
330 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
331}
332
333static int sp_automatic_cmdcheck(uint8_t cmd)
334{
335 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
336 printf_debug ("Warning: Automatic command availability check"
337 " failed for cmd %d - wont execute cmd\n",cmd);
338 return 1;
339 }
340 return 0;
341}
342
343static int sp_docommand(uint8_t command, uint32_t parmlen,
344 uint8_t * params, uint32_t retlen, void *retparms)
345{
346 unsigned char *sendpacket;
347 unsigned char c;
348 if (sp_automatic_cmdcheck(command))
349 return 1;
350 sendpacket = malloc(1 + parmlen);
351 if (!sendpacket)
352 sp_die("Error: cannot malloc command buffer");
353 sendpacket[0] = command;
354 memcpy(&(sendpacket[1]), params, parmlen);
355 if (write(sp_fd, sendpacket, 1 + parmlen) != (1 + parmlen)) {
356 sp_die("Error: cannot write command");
357 }
358 free(sendpacket);
359 if (read(sp_fd, &c, 1) != 1)
360 sp_die("Error: cannot read from device");
361 if (c == S_NAK) return 1;
362 if (c != S_ACK) {
363 fprintf(stderr,
364 "Error: invalid response 0x%02X from device\n",c);
365 exit(1);
366 }
367 if (retlen) {
368 int rd_bytes = 0;
369 do {
370 int r;
371 r = read(sp_fd, retparms + rd_bytes,
372 retlen - rd_bytes);
373 if (r <= 0) sp_die
374 ("Error: cannot read return parameters");
375 rd_bytes += r;
376 } while (rd_bytes != retlen);
377 }
378 return 0;
379}
380
381static void sp_flush_stream(void)
382{
383 if (sp_streamed_transmit_ops)
384 do {
385 unsigned char c;
386 if (read(sp_fd, &c, 1) != 1) {
387 sp_die
388 ("Error: cannot read from device (flushing stream)");
389 }
390 if (c == S_NAK) {
391 fprintf(stderr,
392 "Error: NAK to a stream buffer operation\n");
393 exit(1);
394 }
395 if (c != S_ACK) {
396 fprintf(stderr,
397 "Error: Invalid reply 0x%02X from device\n",
398 c);
399 exit(1);
400 }
401 } while (--sp_streamed_transmit_ops);
402 sp_streamed_transmit_ops = 0;
403 sp_streamed_transmit_bytes = 0;
404}
405
406static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
407{
408 uint8_t *sp;
409 if (sp_automatic_cmdcheck(cmd))
410 return 1;
411 sp = malloc(1 + parmlen);
412 if (!sp) sp_die("Error: cannot malloc command buffer");
413 sp[0] = cmd;
414 memcpy(&(sp[1]), parms, parmlen);
415 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
416 sp_flush_stream();
417 if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
418 sp_die("Error: cannot write command");
419 free(sp);
420 sp_streamed_transmit_ops += 1;
421 sp_streamed_transmit_bytes += 1 + parmlen;
422 return 0;
423}
424
425int serprog_init(void)
426{
427 uint16_t iface;
428 int len;
429 unsigned char pgmname[17];
430 unsigned char rbuf[3];
431 unsigned char c;
432 char *num;
433 char *dev;
434 printf_debug("%s\n", __func__);
435 /* the parameter is either of format "/dev/device:baud" or "ip:port" */
436 if ((!serprog_param) || (!strlen(serprog_param))) {
437 nodevice:
438 fprintf(stderr,
439 "Error: No device/host given for the serial programmer driver.\n"
440 "Use flashrom -p serprog=/dev/device:baud or flashrom -p serprog=ip:port\n");
441 exit(1);
442 }
443 num = strstr(serprog_param, ":");
444 len = num - serprog_param;
445 if (!len) goto nodevice;
446 if (!num) {
447 fprintf(stderr,
448 "Error: No port or baudrate specified to serial programmer driver.\n"
449 "Use flashrom -p serprog=/dev/device:baud or flashrom -p serprog=ip:port\n");
450 exit(1);
451 }
452 len = num - serprog_param;
453 dev = malloc(len + 1);
454 if (!dev) sp_die("Error: memory allocation failure");
455 memcpy(dev, serprog_param, len);
456 dev[len] = 0;
457 num = strdup(num + 1);
458 if (!num) sp_die("Error: memory allocation failure");
459 free(serprog_param);
460 serprog_param = NULL;
461
462 if (dev[0] == '/') sp_fd = sp_openserport(dev, atoi(num));
463 else sp_fd = sp_opensocket(dev, atoi(num));
464
465 free(dev); dev = NULL;
466 free(num); num = NULL;
467
468 printf_debug(MSGHEADER "connected - attempting to synchronize\n");
469
470 sp_check_avail_automatic = 0;
471
472 sp_synchronize();
473
474 printf_debug(MSGHEADER "Synchronized\n");
475
476 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
477 fprintf(stderr, "Error: NAK to Query Interface version\n");
478 exit(1);
479 }
480
481 if (iface != 1) {
482 fprintf(stderr, "Error: Unknown interface version %d\n", iface);
483 exit(1);
484 }
485
486 printf_debug(MSGHEADER "Interface version ok.\n");
487
488 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
489 fprintf(stderr, "Error: query command map not supported\n");
490 exit(1);
491 }
492
493 sp_check_avail_automatic = 1;
494
495 /* Check for the minimum operational set of commands */
496 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
497 fprintf(stderr, "Error: Single byte read not supported\n");
498 exit(1);
499 }
500 /* This could be translated to single byte reads (if missing), *
501 * but now we dont support that. */
502 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
503 fprintf(stderr, "Error: Read n bytes not supported\n");
504 exit(1);
505 }
506 /* In the future one could switch to read-only mode if these *
507 * are not available. */
508 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
509 fprintf(stderr,
510 "Error: Initialize operation buffer not supported\n");
511 exit(1);
512 }
513 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
514 fprintf(stderr,
515 "Error: Write to opbuf: write byte not supported\n");
516 exit(1);
517 }
518 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
519 fprintf(stderr, "Error: Write to opbuf: delay not supported\n");
520 exit(1);
521 }
522 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
523 fprintf(stderr,
524 "Error: Execute operation buffer not supported\n");
525 exit(1);
526 }
527
528 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
529 fprintf(stderr, "Warning: NAK to query programmer name\n");
530 strcpy((char *)pgmname, "(unknown)");
531 }
532 pgmname[16] = 0;
533 printf(MSGHEADER "Programmer name \"%s\"\n", pgmname);
534
535 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
536 fprintf(stderr, "Warning: NAK to query serial buffer size\n");
537 }
538 printf_debug(MSGHEADER "serial buffer size %d\n",
539 sp_device_serbuf_size);
540
541 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2, &sp_device_opbuf_size)) {
542 fprintf(stderr,
543 "Warning: NAK to query operation buffer size\n");
544 }
545 printf_debug(MSGHEADER "operation buffer size %d\n",
546 sp_device_opbuf_size);
547
548 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
549 fprintf(stderr, "Warning: NAK to query supported buses\n");
550 c = CHIP_BUSTYPE_NONSPI; /* A reasonable default for now. */
551 }
552 buses_supported = c;
553
554 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
555 fprintf(stderr, "Error: NAK to initialize operation buffer\n");
556 exit(1);
557 }
558
559 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
560 printf_debug(MSGHEADER "Write-n not supported");
561 sp_max_write_n = 0;
562 } else {
563 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
564 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
565 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
566 printf_debug(MSGHEADER "Maximum write-n length %d\n",
567 sp_max_write_n);
568 sp_write_n_buf = malloc(sp_max_write_n);
569 if (!sp_write_n_buf) {
570 fprintf(stderr,
571 "Error: cannot allocate memory for Write-n buffer\n");
572 exit(1);
573 }
574 sp_write_n_bytes = 0;
575 }
576
577 if ((sp_check_commandavail(S_CMD_Q_RDNMAXLEN))
578 &&((sp_docommand(S_CMD_Q_RDNMAXLEN,0,NULL, 3, rbuf) == 0))) {
579 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
580 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
581 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
582 printf_debug(MSGHEADER "Maximum read-n length %d\n",
583 sp_max_read_n ? sp_max_read_n : (1<<24));
584 } else {
585 printf_debug(MSGHEADER "Maximum read-n length not reported\n");
586 sp_max_read_n = 0;
587 }
588
589 sp_prev_was_write = 0;
590 sp_streamed_transmit_ops = 0;
591 sp_streamed_transmit_bytes = 0;
592 sp_opbuf_usage = 0;
593 return 0;
594}
595
596/* Move an in flashrom buffer existing write-n operation to *
597 * the on-device operation buffer. */
598static void sp_pass_writen(void)
599{
600 unsigned char header[7];
601 printf_debug(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
602 sp_write_n_bytes, sp_write_n_addr);
603 if (sp_streamed_transmit_bytes >=
604 (7 + sp_write_n_bytes + sp_device_serbuf_size))
605 sp_flush_stream();
606 /* In case it's just a single byte send it as a single write. */
607 if (sp_write_n_bytes == 1) {
608 sp_write_n_bytes = 0;
609 header[0] = (sp_write_n_addr >> 0) & 0xFF;
610 header[1] = (sp_write_n_addr >> 8) & 0xFF;
611 header[2] = (sp_write_n_addr >> 16) & 0xFF;
612 header[3] = sp_write_n_buf[0];
613 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
614 sp_opbuf_usage += 5;
615 return;
616 }
617 header[0] = S_CMD_O_WRITEN;
618 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
619 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
620 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
621 header[4] = (sp_write_n_addr >> 0) & 0xFF;
622 header[5] = (sp_write_n_addr >> 8) & 0xFF;
623 header[6] = (sp_write_n_addr >> 16) & 0xFF;
624 if (write(sp_fd, header, 7) != 7)
625 sp_die("Error: cannot write write-n command\n");
626 if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
627 sp_write_n_bytes)
628 sp_die("Error: cannot write write-n data");
629 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
630 sp_streamed_transmit_ops += 1;
631 sp_opbuf_usage += 7 + sp_write_n_bytes;
632 sp_write_n_bytes = 0;
633 sp_prev_was_write = 0;
634}
635
636static void sp_execute_opbuf_noflush(void)
637{
638 if ((sp_max_write_n) && (sp_write_n_bytes))
639 sp_pass_writen();
640 sp_stream_buffer_op(S_CMD_O_EXEC, 0, 0);
641 printf_debug(MSGHEADER "Executed operation buffer of %d bytes\n",
642 sp_opbuf_usage);
643 sp_opbuf_usage = 0;
644 sp_prev_was_write = 0;
645 return;
646}
647
648static void sp_execute_opbuf(void)
649{
650 sp_execute_opbuf_noflush();
651 sp_flush_stream();
652}
653
654int serprog_shutdown(void)
655{
656 printf_debug("%s\n", __func__);
657 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
658 sp_execute_opbuf();
659 close(sp_fd);
660 if (sp_max_write_n)
661 free(sp_write_n_buf);
662 return 0;
663}
664
665static void sp_check_opbuf_usage(int bytes_to_be_added)
666{
667 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
668 sp_execute_opbuf();
669 /* If this happens in the mid of an page load the page load *
670 * will propably fail. */
671 printf_debug(MSGHEADER
672 "Warning: executed operation buffer due to size reasons\n");
673 }
674}
675
676void serprog_chip_writeb(uint8_t val, chipaddr addr)
677{
678 printf_debug("%s\n", __func__);
679 if (sp_max_write_n) {
680 if ((sp_prev_was_write)
681 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
682 sp_write_n_buf[sp_write_n_bytes++] = val;
683 } else {
684 if ((sp_prev_was_write) && (sp_write_n_bytes))
685 sp_pass_writen();
686 sp_prev_was_write = 1;
687 sp_write_n_addr = addr;
688 sp_write_n_bytes = 1;
689 sp_write_n_buf[0] = val;
690 }
691 sp_check_opbuf_usage(7 + sp_write_n_bytes);
692 if (sp_write_n_bytes >= sp_max_write_n)
693 sp_pass_writen();
694 } else {
695 /* We will have to do single writeb ops. */
696 unsigned char writeb_parm[4];
697 sp_check_opbuf_usage(6);
698 writeb_parm[0] = (addr >> 0) & 0xFF;
699 writeb_parm[1] = (addr >> 8) & 0xFF;
700 writeb_parm[2] = (addr >> 16) & 0xFF;
701 writeb_parm[3] = val;
702 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
703 sp_opbuf_usage += 5;
704 }
705}
706
707uint8_t serprog_chip_readb(const chipaddr addr)
708{
709 unsigned char c;
710 unsigned char buf[3];
711 /* Will stream the read operation - eg. add it to the stream buffer, *
712 * then flush the buffer, then read the read answer. */
713 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
714 sp_execute_opbuf_noflush();
715 buf[0] = ((addr >> 0) & 0xFF);
716 buf[1] = ((addr >> 8) & 0xFF);
717 buf[2] = ((addr >> 16) & 0xFF);
718 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
719 sp_flush_stream();
720 if (read(sp_fd, &c, 1) != 1)
721 sp_die("readb byteread");
722 printf_debug("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
723 return c;
724}
725
726/* Local version that really does the job, doesnt care of max_read_n. */
727static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
728{
729 int rd_bytes = 0;
730 unsigned char sbuf[6];
731 printf_debug("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
732 /* Stream the read-n -- as above. */
733 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
734 sp_execute_opbuf_noflush();
735 sbuf[0] = ((addr >> 0) & 0xFF);
736 sbuf[1] = ((addr >> 8) & 0xFF);
737 sbuf[2] = ((addr >> 16) & 0xFF);
738 sbuf[3] = ((len >> 0) & 0xFF);
739 sbuf[4] = ((len >> 8) & 0xFF);
740 sbuf[5] = ((len >> 16) & 0xFF);
741 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
742 sp_flush_stream();
743 do {
744 int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
745 if (r <= 0)
746 sp_die("Error: cannot read read-n data");
747 rd_bytes += r;
748 } while (rd_bytes != len);
749 return;
750}
751
752/* The externally called version that makes sure that max_read_n is obeyed. */
753void serprog_chip_readn(uint8_t * buf, const chipaddr addr, size_t len)
754{
755 size_t lenm = len;
756 chipaddr addrm = addr;
757 while ((sp_max_read_n)&&(lenm > sp_max_read_n)) {
758 sp_do_read_n(&(buf[addrm-addr]),addrm,sp_max_read_n);
759 addrm += sp_max_read_n;
760 lenm -= sp_max_read_n;
761 }
762 if (lenm) sp_do_read_n(&(buf[addrm-addr]),addrm,lenm);
763}
764
765void serprog_delay(int delay)
766{
767 unsigned char buf[4];
768 printf_debug("%s\n", __func__);
769 if ((sp_max_write_n) && (sp_write_n_bytes))
770 sp_pass_writen();
771 sp_check_opbuf_usage(5);
772 buf[0] = ((delay >> 0) & 0xFF);
773 buf[1] = ((delay >> 8) & 0xFF);
774 buf[2] = ((delay >> 16) & 0xFF);
775 buf[3] = ((delay >> 24) & 0xFF);
776 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
777 sp_opbuf_usage += 5;
778 sp_prev_was_write = 0;
779}
780
Urja Rannikko22915352009-06-23 11:33:43 +0000781#else
Urja Rannikkof3196df2009-07-21 13:02:59 +0000782
Urja Rannikko22915352009-06-23 11:33:43 +0000783int serprog_init(void)
784{
785 fprintf(stderr, "Serial programmer support was not compiled in\n");
786 exit(1);
787}
788
789int serprog_shutdown(void)
790{
791 fprintf(stderr, "Serial programmer support was not compiled in\n");
792 exit(1);
793}
794
795void serprog_chip_writeb(uint8_t val, chipaddr addr)
796{
797 fprintf(stderr, "Serial programmer support was not compiled in\n");
798 exit(1);
799}
800
801uint8_t serprog_chip_readb(const chipaddr addr)
802{
803 fprintf(stderr, "Serial programmer support was not compiled in\n");
804 exit(1);
805}
806
807void serprog_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
808{
809 fprintf(stderr, "Serial programmer support was not compiled in\n");
810 exit(1);
811}
812
813void serprog_delay(int delay)
814{
815 fprintf(stderr, "Serial programmer support was not compiled in\n");
816 exit(1);
817}
818#endif