blob: e418c3e208c8c37e7dd41dc17ad6e55f00683244 [file] [log] [blame]
Urja Rannikko22915352009-06-23 11:33:43 +00001/*
2 * This file is part of the flashrom project.
3 *
Urja Rannikkoc93f5f12011-09-15 23:38:14 +00004 * Copyright (C) 2009, 2011 Urja Rannikko <urjaman@gmail.com>
Urja Rannikko22915352009-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
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 Rannikkoc93f5f12011-09-15 23:38:14 +000039#include "chipdrivers.h"
Urja Rannikkof3196df2009-07-21 13:02:59 +000040
Stefan Tauner31019d42011-10-22 21:45:27 +000041#define MSGHEADER "serprog: "
Urja Rannikkof3196df2009-07-21 13:02:59 +000042
David Hendricks8bb20212011-06-14 01:35:36 +000043/*
44 * FIXME: This prototype was added to help reduce diffs for the shutdown
45 * registration patch, which shifted many lines of code to place
46 * serprog_shutdown() before serprog_init(). It should be removed soon.
47 */
48static int serprog_shutdown(void *data);
49
Urja Rannikkof3196df2009-07-21 13:02:59 +000050#define S_ACK 0x06
51#define S_NAK 0x15
52#define S_CMD_NOP 0x00 /* No operation */
53#define S_CMD_Q_IFACE 0x01 /* Query interface version */
54#define S_CMD_Q_CMDMAP 0x02 /* Query supported commands bitmap */
55#define S_CMD_Q_PGMNAME 0x03 /* Query programmer name */
56#define S_CMD_Q_SERBUF 0x04 /* Query Serial Buffer Size */
57#define S_CMD_Q_BUSTYPE 0x05 /* Query supported bustypes */
58#define S_CMD_Q_CHIPSIZE 0x06 /* Query supported chipsize (2^n format) */
59#define S_CMD_Q_OPBUF 0x07 /* Query operation buffer size */
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000060#define S_CMD_Q_WRNMAXLEN 0x08 /* Query opbuf-write-N maximum length */
Urja Rannikkof3196df2009-07-21 13:02:59 +000061#define S_CMD_R_BYTE 0x09 /* Read a single byte */
62#define S_CMD_R_NBYTES 0x0A /* Read n bytes */
63#define S_CMD_O_INIT 0x0B /* Initialize operation buffer */
64#define S_CMD_O_WRITEB 0x0C /* Write opbuf: Write byte with address */
65#define S_CMD_O_WRITEN 0x0D /* Write to opbuf: Write-N */
66#define S_CMD_O_DELAY 0x0E /* Write opbuf: udelay */
67#define S_CMD_O_EXEC 0x0F /* Execute operation buffer */
68#define S_CMD_SYNCNOP 0x10 /* Special no-operation that returns NAK+ACK */
69#define S_CMD_Q_RDNMAXLEN 0x11 /* Query read-n maximum length */
70#define S_CMD_S_BUSTYPE 0x12 /* Set used bustype(s). */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +000071#define S_CMD_O_SPIOP 0x13 /* Perform SPI operation. */
Stefan Taunerb98f6eb2012-08-13 16:33:04 +000072#define S_CMD_S_SPI_FREQ 0x14 /* Set SPI clock frequency */
Urja Rannikkof3196df2009-07-21 13:02:59 +000073
Urja Rannikkof3196df2009-07-21 13:02:59 +000074static uint16_t sp_device_serbuf_size = 16;
75static uint16_t sp_device_opbuf_size = 300;
76/* Bitmap of supported commands */
77static uint8_t sp_cmdmap[32];
78
Uwe Hermann4e3d0b32010-03-25 23:18:41 +000079/* sp_prev_was_write used to detect writes with contiguous addresses
Urja Rannikkof3196df2009-07-21 13:02:59 +000080 and combine them to write-n's */
81static int sp_prev_was_write = 0;
82/* sp_write_n_addr used as the starting addr of the currently
83 combined write-n operation */
84static uint32_t sp_write_n_addr;
85/* The maximum length of an write_n operation; 0 = write-n not supported */
86static uint32_t sp_max_write_n = 0;
87/* The maximum length of a read_n operation; 0 = 2^24 */
88static uint32_t sp_max_read_n = 0;
89
90/* A malloc'd buffer for combining the operation's data
91 and a counter that tells how much data is there. */
92static uint8_t *sp_write_n_buf;
93static uint32_t sp_write_n_bytes = 0;
94
95/* sp_streamed_* used for flow control checking */
96static int sp_streamed_transmit_ops = 0;
97static int sp_streamed_transmit_bytes = 0;
98
99/* sp_opbuf_usage used for counting the amount of
100 on-device operation buffer used */
101static int sp_opbuf_usage = 0;
102/* if true causes sp_docommand to automatically check
103 whether the command is supported before doing it */
104static int sp_check_avail_automatic = 0;
105
Urja Rannikkof3196df2009-07-21 13:02:59 +0000106static int sp_opensocket(char *ip, unsigned int port)
107{
108 int flag = 1;
109 struct hostent *hostPtr = NULL;
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000110 union { struct sockaddr_in si; struct sockaddr s; } sp = {};
Urja Rannikkof3196df2009-07-21 13:02:59 +0000111 int sock;
Sean Nelson74e8af52010-01-10 01:06:23 +0000112 msg_pdbg(MSGHEADER "IP %s port %d\n", ip, port);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000113 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000114 if (sock < 0) {
115 msg_perr("Error: serprog cannot open socket: %s\n", strerror(errno));
116 return -1;
117 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000118 hostPtr = gethostbyname(ip);
119 if (NULL == hostPtr) {
120 hostPtr = gethostbyaddr(ip, strlen(ip), AF_INET);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000121 if (NULL == hostPtr) {
122 msg_perr("Error: cannot resolve %s\n", ip);
123 return -1;
124 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000125 }
Carl-Daniel Hailfinger6d125602009-09-05 01:10:23 +0000126 sp.si.sin_family = AF_INET;
127 sp.si.sin_port = htons(port);
128 (void)memcpy(&sp.si.sin_addr, hostPtr->h_addr, hostPtr->h_length);
129 if (connect(sock, &sp.s, sizeof(sp.si)) < 0) {
Urja Rannikkof3196df2009-07-21 13:02:59 +0000130 close(sock);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000131 msg_perr("Error: serprog cannot connect: %s\n", strerror(errno));
132 return -1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000133 }
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
Urja Rannikkof3196df2009-07-21 13:02:59 +0000140static int sp_sync_read_timeout(int loops)
141{
142 int i;
143 unsigned char c;
144 for (i = 0; i < loops; i++) {
145 ssize_t rv;
146 rv = read(sp_fd, &c, 1);
147 if (rv == 1)
148 return c;
149 if ((rv == -1) && (errno != EAGAIN))
150 sp_die("read");
151 usleep(10 * 1000); /* 10ms units */
152 }
153 return -1;
154}
155
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000156/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
Urja Rannikkof3196df2009-07-21 13:02:59 +0000157 * always succeeded in) bring the serial protocol to known waiting-for- *
158 * command state - uses nonblocking read - rest of the driver uses *
159 * blocking read - TODO: add an alarm() timer for the rest of the app on *
160 * serial operations, though not such a big issue as the first thing to *
161 * do is synchronize (eg. check that device is alive). */
162static void sp_synchronize(void)
163{
164 int i;
165 int flags = fcntl(sp_fd, F_GETFL);
166 unsigned char buf[8];
167 flags |= O_NONBLOCK;
168 fcntl(sp_fd, F_SETFL, flags);
169 /* First sends 8 NOPs, then flushes the return data - should cause *
170 * the device serial parser to get to a sane state, unless if it *
171 * is waiting for a real long write-n. */
172 memset(buf, S_CMD_NOP, 8);
173 if (write(sp_fd, buf, 8) != 8)
174 sp_die("flush write");
175 /* A second should be enough to get all the answers to the buffer */
176 usleep(1000 * 1000);
177 sp_flush_incoming();
178
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000179 /* Then try up to 8 times to send syncnop and get the correct special *
180 * return of NAK+ACK. Timing note: up to 10 characters, 10*50ms = *
181 * up to 500ms per try, 8*0.5s = 4s; +1s (above) = up to 5s sync *
182 * attempt, ~1s if immediate success. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000183 for (i = 0; i < 8; i++) {
184 int n;
185 unsigned char c = S_CMD_SYNCNOP;
186 if (write(sp_fd, &c, 1) != 1)
187 sp_die("sync write");
Sean Nelson74e8af52010-01-10 01:06:23 +0000188 msg_pdbg(".");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000189 fflush(stdout);
190 for (n = 0; n < 10; n++) {
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000191 c = sp_sync_read_timeout(5); /* wait up to 50ms */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000192 if (c != S_NAK)
193 continue;
194 c = sp_sync_read_timeout(2);
195 if (c != S_ACK)
196 continue;
197 c = S_CMD_SYNCNOP;
198 if (write(sp_fd, &c, 1) != 1)
199 sp_die("sync write");
200 c = sp_sync_read_timeout(50);
201 if (c != S_NAK)
202 break; /* fail */
203 c = sp_sync_read_timeout(10);
204 if (c != S_ACK)
205 break; /* fail */
206 /* Ok, synchronized; back to blocking reads and return. */
207 flags &= ~O_NONBLOCK;
208 fcntl(sp_fd, F_SETFL, flags);
Sean Nelson74e8af52010-01-10 01:06:23 +0000209 msg_pdbg("\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000210 return;
211 }
212 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000213 msg_perr("Error: cannot synchronize protocol "
Urja Rannikkof3196df2009-07-21 13:02:59 +0000214 "- check communications and reset device?\n");
215 exit(1);
216}
217
218static int sp_check_commandavail(uint8_t command)
219{
220 int byteoffs, bitoffs;
221 byteoffs = command / 8;
222 bitoffs = command % 8;
223 return (sp_cmdmap[byteoffs] & (1 << bitoffs)) ? 1 : 0;
224}
225
226static int sp_automatic_cmdcheck(uint8_t cmd)
227{
228 if ((sp_check_avail_automatic) && (sp_check_commandavail(cmd) == 0)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000229 msg_pdbg("Warning: Automatic command availability check failed "
230 "for cmd 0x%x - won't execute cmd\n", cmd);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000231 return 1;
232 }
233 return 0;
234}
235
236static int sp_docommand(uint8_t command, uint32_t parmlen,
Stefan Tauner31019d42011-10-22 21:45:27 +0000237 uint8_t *params, uint32_t retlen, void *retparms)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000238{
Urja Rannikkof3196df2009-07-21 13:02:59 +0000239 unsigned char c;
240 if (sp_automatic_cmdcheck(command))
241 return 1;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000242 if (write(sp_fd, &command, 1) != 1) {
243 msg_perr("Error: cannot write op code: %s\n", strerror(errno));
244 return 1;
245 }
246 if (write(sp_fd, params, parmlen) != (parmlen)) {
247 msg_perr("Error: cannot write parameters: %s\n", strerror(errno));
248 return 1;
249 }
250 if (read(sp_fd, &c, 1) != 1) {
251 msg_perr("Error: cannot read from device: %s\n", strerror(errno));
252 return 1;
253 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000254 if (c == S_NAK)
255 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000256 if (c != S_ACK) {
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000257 msg_perr("Error: invalid response 0x%02X from device\n", c);
258 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000259 }
260 if (retlen) {
261 int rd_bytes = 0;
262 do {
263 int r;
264 r = read(sp_fd, retparms + rd_bytes,
265 retlen - rd_bytes);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000266 if (r <= 0) {
267 msg_perr("Error: cannot read return parameters: %s\n", strerror(errno));
268 return 1;
269 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000270 rd_bytes += r;
271 } while (rd_bytes != retlen);
272 }
273 return 0;
274}
275
276static void sp_flush_stream(void)
277{
278 if (sp_streamed_transmit_ops)
279 do {
280 unsigned char c;
281 if (read(sp_fd, &c, 1) != 1) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000282 sp_die("Error: cannot read from device (flushing stream)");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000283 }
284 if (c == S_NAK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000285 msg_perr("Error: NAK to a stream buffer operation\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000286 exit(1);
287 }
288 if (c != S_ACK) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000289 msg_perr("Error: Invalid reply 0x%02X from device\n", c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000290 exit(1);
291 }
292 } while (--sp_streamed_transmit_ops);
293 sp_streamed_transmit_ops = 0;
294 sp_streamed_transmit_bytes = 0;
295}
296
297static int sp_stream_buffer_op(uint8_t cmd, uint32_t parmlen, uint8_t * parms)
298{
299 uint8_t *sp;
300 if (sp_automatic_cmdcheck(cmd))
301 return 1;
302 sp = malloc(1 + parmlen);
303 if (!sp) sp_die("Error: cannot malloc command buffer");
304 sp[0] = cmd;
305 memcpy(&(sp[1]), parms, parmlen);
306 if (sp_streamed_transmit_bytes >= (1 + parmlen + sp_device_serbuf_size))
307 sp_flush_stream();
308 if (write(sp_fd, sp, 1 + parmlen) != (1 + parmlen))
309 sp_die("Error: cannot write command");
310 free(sp);
311 sp_streamed_transmit_ops += 1;
312 sp_streamed_transmit_bytes += 1 + parmlen;
313 return 0;
314}
315
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000316static int serprog_spi_send_command(struct flashctx *flash,
317 unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000318 const unsigned char *writearr,
319 unsigned char *readarr);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000320static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000321 unsigned int start, unsigned int len);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000322static struct spi_programmer spi_programmer_serprog = {
323 .type = SPI_CONTROLLER_SERPROG,
324 .max_data_read = MAX_DATA_READ_UNLIMITED,
325 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
326 .command = serprog_spi_send_command,
327 .multicommand = default_spi_send_multicommand,
328 .read = serprog_spi_read,
329 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000330 .write_aai = default_spi_write_aai,
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000331};
332
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000333static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
334 chipaddr addr);
335static uint8_t serprog_chip_readb(const struct flashctx *flash,
336 const chipaddr addr);
337static void serprog_chip_readn(const struct flashctx *flash, uint8_t *buf,
338 const chipaddr addr, size_t len);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000339static const struct par_programmer par_programmer_serprog = {
340 .chip_readb = serprog_chip_readb,
341 .chip_readw = fallback_chip_readw,
342 .chip_readl = fallback_chip_readl,
343 .chip_readn = serprog_chip_readn,
344 .chip_writeb = serprog_chip_writeb,
345 .chip_writew = fallback_chip_writew,
346 .chip_writel = fallback_chip_writel,
347 .chip_writen = fallback_chip_writen,
348};
349
350static enum chipbustype serprog_buses_supported = BUS_NONE;
351
Urja Rannikkof3196df2009-07-21 13:02:59 +0000352int serprog_init(void)
353{
354 uint16_t iface;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000355 unsigned char pgmname[17];
356 unsigned char rbuf[3];
357 unsigned char c;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000358 char *device;
359 char *baudport;
360 int have_device = 0;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000361
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000362 /* the parameter is either of format "dev=/dev/device:baud" or "ip=ip:port" */
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000363 device = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000364 if (device && strlen(device)) {
365 baudport = strstr(device, ":");
366 if (baudport) {
367 /* Split device from baudrate. */
368 *baudport = '\0';
369 baudport++;
370 }
371 if (!baudport || !strlen(baudport)) {
372 msg_perr("Error: No baudrate specified.\n"
373 "Use flashrom -p serprog:dev=/dev/device:baud\n");
374 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000375 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000376 }
377 if (strlen(device)) {
378 sp_fd = sp_openserport(device, atoi(baudport));
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000379 if (sp_fd < 0) {
380 free(device);
381 return 1;
382 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000383 have_device++;
384 }
385 }
386 if (device && !strlen(device)) {
387 msg_perr("Error: No device specified.\n"
388 "Use flashrom -p serprog:dev=/dev/device:baud\n");
389 free(device);
390 return 1;
391 }
392 free(device);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000393
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000394 device = extract_programmer_param("ip");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000395 if (have_device && device) {
396 msg_perr("Error: Both host and device specified.\n"
397 "Please use either dev= or ip= but not both.\n");
398 free(device);
399 return 1;
400 }
401 if (device && strlen(device)) {
402 baudport = strstr(device, ":");
403 if (baudport) {
404 /* Split host from port. */
405 *baudport = '\0';
406 baudport++;
407 }
408 if (!baudport || !strlen(baudport)) {
409 msg_perr("Error: No port specified.\n"
410 "Use flashrom -p serprog:ip=ipaddr:port\n");
411 free(device);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000412 return 1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000413 }
414 if (strlen(device)) {
415 sp_fd = sp_opensocket(device, atoi(baudport));
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000416 if (sp_fd < 0) {
417 free(device);
418 return 1;
419 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000420 have_device++;
421 }
422 }
423 if (device && !strlen(device)) {
424 msg_perr("Error: No host specified.\n"
425 "Use flashrom -p serprog:ip=ipaddr:port\n");
426 free(device);
427 return 1;
428 }
429 free(device);
430
431 if (!have_device) {
432 msg_perr("Error: Neither host nor device specified.\n"
433 "Use flashrom -p serprog:dev=/dev/device:baud or "
434 "flashrom -p serprog:ip=ipaddr:port\n");
435 return 1;
436 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000437
David Hendricks8bb20212011-06-14 01:35:36 +0000438 if (register_shutdown(serprog_shutdown, NULL))
439 return 1;
440
Sean Nelson74e8af52010-01-10 01:06:23 +0000441 msg_pdbg(MSGHEADER "connected - attempting to synchronize\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000442
443 sp_check_avail_automatic = 0;
444
445 sp_synchronize();
446
Sean Nelson74e8af52010-01-10 01:06:23 +0000447 msg_pdbg(MSGHEADER "Synchronized\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000448
449 if (sp_docommand(S_CMD_Q_IFACE, 0, NULL, 2, &iface)) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000450 msg_perr("Error: NAK to query interface version\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000451 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000452 }
453
454 if (iface != 1) {
Stefan Tauner31019d42011-10-22 21:45:27 +0000455 msg_perr("Error: Unknown interface version: %d\n", iface);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000456 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000457 }
458
Sean Nelson74e8af52010-01-10 01:06:23 +0000459 msg_pdbg(MSGHEADER "Interface version ok.\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000460
461 if (sp_docommand(S_CMD_Q_CMDMAP, 0, NULL, 32, sp_cmdmap)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000462 msg_perr("Error: query command map not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000463 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000464 }
465
466 sp_check_avail_automatic = 1;
467
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000468 /* FIXME: This assumes that serprog device bustypes are always
469 * identical with flashrom bustype enums and that they all fit
470 * in a single byte.
471 */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000472 if (sp_docommand(S_CMD_Q_BUSTYPE, 0, NULL, 1, &c)) {
473 msg_perr("Warning: NAK to query supported buses\n");
474 c = BUS_NONSPI; /* A reasonable default for now. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000475 }
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000476 serprog_buses_supported = c;
477
Stefan Tauner31019d42011-10-22 21:45:27 +0000478 msg_pdbg(MSGHEADER "Bus support: parallel=%s, LPC=%s, FWH=%s, SPI=%s\n",
479 (c & BUS_PARALLEL) ? "on" : "off",
480 (c & BUS_LPC) ? "on" : "off",
481 (c & BUS_FWH) ? "on" : "off",
482 (c & BUS_SPI) ? "on" : "off");
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000483 /* Check for the minimum operational set of commands. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000484 if (serprog_buses_supported & BUS_SPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000485 uint8_t bt = BUS_SPI;
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000486 char *spispeed;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000487 if (sp_check_commandavail(S_CMD_O_SPIOP) == 0) {
488 msg_perr("Error: SPI operation not supported while the "
489 "bustype is SPI\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000490 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000491 }
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000492 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
493 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000494 /* Success of any of these commands is optional. We don't need
495 the programmer to tell us its limits, but if it doesn't, we
496 will assume stuff, so it's in the programmers best interest
497 to tell us. */
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000498 if (!sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
499 uint32_t v;
500 v = ((unsigned int)(rbuf[0]) << 0);
501 v |= ((unsigned int)(rbuf[1]) << 8);
502 v |= ((unsigned int)(rbuf[2]) << 16);
503 if (v == 0)
504 v = (1 << 24) - 1; /* SPI-op maximum. */
505 spi_programmer_serprog.max_data_write = v;
506 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n", v);
507 }
508 if (!sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf)) {
509 uint32_t v;
510 v = ((unsigned int)(rbuf[0]) << 0);
511 v |= ((unsigned int)(rbuf[1]) << 8);
512 v |= ((unsigned int)(rbuf[2]) << 16);
513 if (v == 0)
514 v = (1 << 24) - 1; /* SPI-op maximum. */
515 spi_programmer_serprog.max_data_read = v;
516 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n", v);
517 }
Stefan Taunerb98f6eb2012-08-13 16:33:04 +0000518 spispeed = extract_programmer_param("spispeed");
519 if (spispeed && strlen(spispeed)) {
520 uint32_t f_spi_req, f_spi;
521 uint8_t buf[4];
522 char *f_spi_suffix;
523
524 errno = 0;
525 f_spi_req = strtol(spispeed, &f_spi_suffix, 0);
526 if (errno != 0 || spispeed == f_spi_suffix) {
527 msg_perr("Error: Could not convert 'spispeed'.\n");
528 return 1;
529 }
530 if (strlen(f_spi_suffix) == 1) {
531 if (!strcasecmp(f_spi_suffix, "M"))
532 f_spi_req *= 1000000;
533 else if (!strcasecmp(f_spi_suffix, "k"))
534 f_spi_req *= 1000;
535 else {
536 msg_perr("Error: Garbage following 'spispeed' value.\n");
537 return 1;
538 }
539 } else if (strlen(f_spi_suffix) > 1) {
540 msg_perr("Error: Garbage following 'spispeed' value.\n");
541 return 1;
542 }
543
544 buf[0] = (f_spi_req >> (0 * 8)) & 0xFF;
545 buf[1] = (f_spi_req >> (1 * 8)) & 0xFF;
546 buf[2] = (f_spi_req >> (2 * 8)) & 0xFF;
547 buf[3] = (f_spi_req >> (3 * 8)) & 0xFF;
548
549 if (sp_check_commandavail(S_CMD_S_SPI_FREQ) == 0)
550 msg_perr(MSGHEADER "Warning: Setting the SPI clock rate is not supported!\n");
551 else if (sp_docommand(S_CMD_S_SPI_FREQ, 4, buf, 4, buf)
552 == 0) {
553 f_spi = buf[0];
554 f_spi |= buf[1] << (1 * 8);
555 f_spi |= buf[2] << (2 * 8);
556 f_spi |= buf[3] << (3 * 8);
557 msg_pdbg(MSGHEADER "Requested to set SPI clock frequency to %u Hz. "
558 "It was actually set to %u Hz\n", f_spi_req, f_spi);
559 } else
560 msg_pdbg(MSGHEADER "Setting SPI clock rate to %u Hz failed!\n", f_spi_req);
561 }
562 free(spispeed);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000563 bt = serprog_buses_supported;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000564 if (sp_docommand(S_CMD_S_BUSTYPE, 1, &bt, 0, NULL))
565 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000566 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000567
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000568 if (serprog_buses_supported & BUS_NONSPI) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000569 if (sp_check_commandavail(S_CMD_O_INIT) == 0) {
570 msg_perr("Error: Initialize operation buffer "
571 "not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000572 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000573 }
574
575 if (sp_check_commandavail(S_CMD_O_DELAY) == 0) {
576 msg_perr("Error: Write to opbuf: "
577 "delay not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000578 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000579 }
580
581 /* S_CMD_O_EXEC availability checked later. */
582
583 if (sp_check_commandavail(S_CMD_R_BYTE) == 0) {
584 msg_perr("Error: Single byte read not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000585 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000586 }
587 /* This could be translated to single byte reads (if missing),
588 * but now we don't support that. */
589 if (sp_check_commandavail(S_CMD_R_NBYTES) == 0) {
590 msg_perr("Error: Read n bytes not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000591 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000592 }
593 if (sp_check_commandavail(S_CMD_O_WRITEB) == 0) {
594 msg_perr("Error: Write to opbuf: "
595 "write byte not supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000596 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000597 }
598
599 if (sp_docommand(S_CMD_Q_WRNMAXLEN, 0, NULL, 3, rbuf)) {
600 msg_pdbg(MSGHEADER "Write-n not supported");
601 sp_max_write_n = 0;
602 } else {
603 sp_max_write_n = ((unsigned int)(rbuf[0]) << 0);
604 sp_max_write_n |= ((unsigned int)(rbuf[1]) << 8);
605 sp_max_write_n |= ((unsigned int)(rbuf[2]) << 16);
606 if (!sp_max_write_n) {
607 sp_max_write_n = (1 << 24);
608 }
609 msg_pdbg(MSGHEADER "Maximum write-n length is %d\n",
610 sp_max_write_n);
611 sp_write_n_buf = malloc(sp_max_write_n);
612 if (!sp_write_n_buf) {
613 msg_perr("Error: cannot allocate memory for "
614 "Write-n buffer\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000615 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000616 }
617 sp_write_n_bytes = 0;
618 }
619
620 if (sp_check_commandavail(S_CMD_Q_RDNMAXLEN) &&
621 (sp_docommand(S_CMD_Q_RDNMAXLEN, 0, NULL, 3, rbuf) == 0)) {
622 sp_max_read_n = ((unsigned int)(rbuf[0]) << 0);
623 sp_max_read_n |= ((unsigned int)(rbuf[1]) << 8);
624 sp_max_read_n |= ((unsigned int)(rbuf[2]) << 16);
625 msg_pdbg(MSGHEADER "Maximum read-n length is %d\n",
626 sp_max_read_n ? sp_max_read_n : (1 << 24));
627 } else {
628 msg_pdbg(MSGHEADER "Maximum read-n length "
629 "not reported\n");
630 sp_max_read_n = 0;
631 }
632
Urja Rannikkof3196df2009-07-21 13:02:59 +0000633 }
634
635 if (sp_docommand(S_CMD_Q_PGMNAME, 0, NULL, 16, pgmname)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000636 msg_perr("Warning: NAK to query programmer name\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000637 strcpy((char *)pgmname, "(unknown)");
638 }
639 pgmname[16] = 0;
Stefan Tauner31019d42011-10-22 21:45:27 +0000640 msg_pinfo(MSGHEADER "Programmer name is \"%s\"\n", pgmname);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000641
642 if (sp_docommand(S_CMD_Q_SERBUF, 0, NULL, 2, &sp_device_serbuf_size)) {
Sean Nelson74e8af52010-01-10 01:06:23 +0000643 msg_perr("Warning: NAK to query serial buffer size\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000644 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000645 msg_pdbg(MSGHEADER "Serial buffer size is %d\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000646 sp_device_serbuf_size);
647
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000648 if (sp_check_commandavail(S_CMD_O_INIT)) {
649 /* This would be inconsistent. */
650 if (sp_check_commandavail(S_CMD_O_EXEC) == 0) {
651 msg_perr("Error: Execute operation buffer not "
652 "supported\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000653 return 1;
Urja Rannikkof3196df2009-07-21 13:02:59 +0000654 }
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000655
656 if (sp_docommand(S_CMD_O_INIT, 0, NULL, 0, NULL)) {
657 msg_perr("Error: NAK to initialize operation buffer\n");
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000658 return 1;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000659 }
660
661 if (sp_docommand(S_CMD_Q_OPBUF, 0, NULL, 2,
662 &sp_device_opbuf_size)) {
663 msg_perr("Warning: NAK to query operation buffer "
664 "size\n");
665 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000666 msg_pdbg(MSGHEADER "operation buffer size is %d\n",
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000667 sp_device_opbuf_size);
668 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000669
670 sp_prev_was_write = 0;
671 sp_streamed_transmit_ops = 0;
672 sp_streamed_transmit_bytes = 0;
673 sp_opbuf_usage = 0;
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000674 if (serprog_buses_supported & BUS_SPI)
675 register_spi_programmer(&spi_programmer_serprog);
676 if (serprog_buses_supported & BUS_NONSPI)
677 register_par_programmer(&par_programmer_serprog,
678 serprog_buses_supported & BUS_NONSPI);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000679 return 0;
680}
681
682/* Move an in flashrom buffer existing write-n operation to *
683 * the on-device operation buffer. */
684static void sp_pass_writen(void)
685{
686 unsigned char header[7];
Sean Nelson74e8af52010-01-10 01:06:23 +0000687 msg_pspew(MSGHEADER "Passing write-n bytes=%d addr=0x%x\n",
Stefan Tauner31019d42011-10-22 21:45:27 +0000688 sp_write_n_bytes, sp_write_n_addr);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000689 if (sp_streamed_transmit_bytes >=
690 (7 + sp_write_n_bytes + sp_device_serbuf_size))
691 sp_flush_stream();
692 /* In case it's just a single byte send it as a single write. */
693 if (sp_write_n_bytes == 1) {
694 sp_write_n_bytes = 0;
695 header[0] = (sp_write_n_addr >> 0) & 0xFF;
696 header[1] = (sp_write_n_addr >> 8) & 0xFF;
697 header[2] = (sp_write_n_addr >> 16) & 0xFF;
698 header[3] = sp_write_n_buf[0];
699 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, header);
700 sp_opbuf_usage += 5;
701 return;
702 }
703 header[0] = S_CMD_O_WRITEN;
704 header[1] = (sp_write_n_bytes >> 0) & 0xFF;
705 header[2] = (sp_write_n_bytes >> 8) & 0xFF;
706 header[3] = (sp_write_n_bytes >> 16) & 0xFF;
707 header[4] = (sp_write_n_addr >> 0) & 0xFF;
708 header[5] = (sp_write_n_addr >> 8) & 0xFF;
709 header[6] = (sp_write_n_addr >> 16) & 0xFF;
710 if (write(sp_fd, header, 7) != 7)
711 sp_die("Error: cannot write write-n command\n");
712 if (write(sp_fd, sp_write_n_buf, sp_write_n_bytes) !=
713 sp_write_n_bytes)
714 sp_die("Error: cannot write write-n data");
715 sp_streamed_transmit_bytes += 7 + sp_write_n_bytes;
716 sp_streamed_transmit_ops += 1;
717 sp_opbuf_usage += 7 + sp_write_n_bytes;
718 sp_write_n_bytes = 0;
719 sp_prev_was_write = 0;
720}
721
722static void sp_execute_opbuf_noflush(void)
723{
724 if ((sp_max_write_n) && (sp_write_n_bytes))
725 sp_pass_writen();
Peter Huewe73f8ec82011-01-24 19:15:51 +0000726 sp_stream_buffer_op(S_CMD_O_EXEC, 0, NULL);
Sean Nelson74e8af52010-01-10 01:06:23 +0000727 msg_pspew(MSGHEADER "Executed operation buffer of %d bytes\n",
Urja Rannikkof3196df2009-07-21 13:02:59 +0000728 sp_opbuf_usage);
729 sp_opbuf_usage = 0;
730 sp_prev_was_write = 0;
731 return;
732}
733
734static void sp_execute_opbuf(void)
735{
736 sp_execute_opbuf_noflush();
737 sp_flush_stream();
738}
739
David Hendricks8bb20212011-06-14 01:35:36 +0000740static int serprog_shutdown(void *data)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000741{
Sean Nelson74e8af52010-01-10 01:06:23 +0000742 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000743 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
744 sp_execute_opbuf();
745 close(sp_fd);
746 if (sp_max_write_n)
747 free(sp_write_n_buf);
748 return 0;
749}
750
751static void sp_check_opbuf_usage(int bytes_to_be_added)
752{
753 if (sp_device_opbuf_size <= (sp_opbuf_usage + bytes_to_be_added)) {
754 sp_execute_opbuf();
755 /* If this happens in the mid of an page load the page load *
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000756 * will probably fail. */
Sean Nelson74e8af52010-01-10 01:06:23 +0000757 msg_pdbg(MSGHEADER "Warning: executed operation buffer due to size reasons\n");
Urja Rannikkof3196df2009-07-21 13:02:59 +0000758 }
759}
760
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000761static void serprog_chip_writeb(const struct flashctx *flash, uint8_t val,
762 chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000763{
Sean Nelson74e8af52010-01-10 01:06:23 +0000764 msg_pspew("%s\n", __func__);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000765 if (sp_max_write_n) {
766 if ((sp_prev_was_write)
767 && (addr == (sp_write_n_addr + sp_write_n_bytes))) {
768 sp_write_n_buf[sp_write_n_bytes++] = val;
769 } else {
770 if ((sp_prev_was_write) && (sp_write_n_bytes))
771 sp_pass_writen();
772 sp_prev_was_write = 1;
773 sp_write_n_addr = addr;
774 sp_write_n_bytes = 1;
775 sp_write_n_buf[0] = val;
776 }
777 sp_check_opbuf_usage(7 + sp_write_n_bytes);
778 if (sp_write_n_bytes >= sp_max_write_n)
779 sp_pass_writen();
780 } else {
781 /* We will have to do single writeb ops. */
782 unsigned char writeb_parm[4];
783 sp_check_opbuf_usage(6);
784 writeb_parm[0] = (addr >> 0) & 0xFF;
785 writeb_parm[1] = (addr >> 8) & 0xFF;
786 writeb_parm[2] = (addr >> 16) & 0xFF;
787 writeb_parm[3] = val;
788 sp_stream_buffer_op(S_CMD_O_WRITEB, 4, writeb_parm);
789 sp_opbuf_usage += 5;
790 }
791}
792
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000793static uint8_t serprog_chip_readb(const struct flashctx *flash,
794 const chipaddr addr)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000795{
796 unsigned char c;
797 unsigned char buf[3];
798 /* Will stream the read operation - eg. add it to the stream buffer, *
799 * then flush the buffer, then read the read answer. */
800 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
801 sp_execute_opbuf_noflush();
802 buf[0] = ((addr >> 0) & 0xFF);
803 buf[1] = ((addr >> 8) & 0xFF);
804 buf[2] = ((addr >> 16) & 0xFF);
805 sp_stream_buffer_op(S_CMD_R_BYTE, 3, buf);
806 sp_flush_stream();
807 if (read(sp_fd, &c, 1) != 1)
808 sp_die("readb byteread");
Sean Nelson74e8af52010-01-10 01:06:23 +0000809 msg_pspew("%s addr=0x%lx returning 0x%02X\n", __func__, addr, c);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000810 return c;
811}
812
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000813/* Local version that really does the job, doesn't care of max_read_n. */
Urja Rannikkof3196df2009-07-21 13:02:59 +0000814static void sp_do_read_n(uint8_t * buf, const chipaddr addr, size_t len)
815{
816 int rd_bytes = 0;
817 unsigned char sbuf[6];
Sean Nelson74e8af52010-01-10 01:06:23 +0000818 msg_pspew("%s: addr=0x%lx len=%lu\n", __func__, addr, (unsigned long)len);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000819 /* Stream the read-n -- as above. */
820 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
821 sp_execute_opbuf_noflush();
822 sbuf[0] = ((addr >> 0) & 0xFF);
823 sbuf[1] = ((addr >> 8) & 0xFF);
824 sbuf[2] = ((addr >> 16) & 0xFF);
825 sbuf[3] = ((len >> 0) & 0xFF);
826 sbuf[4] = ((len >> 8) & 0xFF);
827 sbuf[5] = ((len >> 16) & 0xFF);
828 sp_stream_buffer_op(S_CMD_R_NBYTES, 6, sbuf);
829 sp_flush_stream();
830 do {
831 int r = read(sp_fd, buf + rd_bytes, len - rd_bytes);
832 if (r <= 0)
833 sp_die("Error: cannot read read-n data");
834 rd_bytes += r;
835 } while (rd_bytes != len);
836 return;
837}
838
839/* The externally called version that makes sure that max_read_n is obeyed. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000840static void serprog_chip_readn(const struct flashctx *flash, uint8_t * buf,
841 const chipaddr addr, size_t len)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000842{
843 size_t lenm = len;
844 chipaddr addrm = addr;
Stefan Tauner31019d42011-10-22 21:45:27 +0000845 while ((sp_max_read_n != 0) && (lenm > sp_max_read_n)) {
846 sp_do_read_n(&(buf[addrm-addr]), addrm, sp_max_read_n);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000847 addrm += sp_max_read_n;
848 lenm -= sp_max_read_n;
849 }
Stefan Tauner31019d42011-10-22 21:45:27 +0000850 if (lenm)
851 sp_do_read_n(&(buf[addrm-addr]), addrm, lenm);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000852}
853
Stefan Tauner31019d42011-10-22 21:45:27 +0000854void serprog_delay(int usecs)
Urja Rannikkof3196df2009-07-21 13:02:59 +0000855{
856 unsigned char buf[4];
Stefan Tauner31019d42011-10-22 21:45:27 +0000857 msg_pspew("%s usecs=%d\n", __func__, usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000858 if (!sp_check_commandavail(S_CMD_O_DELAY)) {
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000859 msg_pdbg("Note: serprog_delay used, but the programmer doesn't "
860 "support delay\n");
Stefan Tauner31019d42011-10-22 21:45:27 +0000861 internal_delay(usecs);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000862 return;
863 }
Urja Rannikkof3196df2009-07-21 13:02:59 +0000864 if ((sp_max_write_n) && (sp_write_n_bytes))
865 sp_pass_writen();
866 sp_check_opbuf_usage(5);
Stefan Tauner31019d42011-10-22 21:45:27 +0000867 buf[0] = ((usecs >> 0) & 0xFF);
868 buf[1] = ((usecs >> 8) & 0xFF);
869 buf[2] = ((usecs >> 16) & 0xFF);
870 buf[3] = ((usecs >> 24) & 0xFF);
Urja Rannikkof3196df2009-07-21 13:02:59 +0000871 sp_stream_buffer_op(S_CMD_O_DELAY, 4, buf);
872 sp_opbuf_usage += 5;
873 sp_prev_was_write = 0;
874}
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000875
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000876static int serprog_spi_send_command(struct flashctx *flash,
877 unsigned int writecnt, unsigned int readcnt,
878 const unsigned char *writearr,
879 unsigned char *readarr)
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000880{
881 unsigned char *parmbuf;
882 int ret;
883 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
884 if ((sp_opbuf_usage) || (sp_max_write_n && sp_write_n_bytes))
885 sp_execute_opbuf();
886 parmbuf = malloc(writecnt + 6);
887 if (!parmbuf)
888 sp_die("Error: cannot malloc SPI send param buffer");
889 parmbuf[0] = (writecnt >> 0) & 0xFF;
890 parmbuf[1] = (writecnt >> 8) & 0xFF;
891 parmbuf[2] = (writecnt >> 16) & 0xFF;
892 parmbuf[3] = (readcnt >> 0) & 0xFF;
893 parmbuf[4] = (readcnt >> 8) & 0xFF;
894 parmbuf[5] = (readcnt >> 16) & 0xFF;
895 memcpy(parmbuf + 6, writearr, writecnt);
896 ret = sp_docommand(S_CMD_O_SPIOP, writecnt + 6, parmbuf, readcnt,
897 readarr);
898 free(parmbuf);
899 return ret;
900}
901
902/* FIXME: This function is optimized so that it does not split each transaction
903 * into chip page_size long blocks unnecessarily like spi_read_chunked. This has
904 * the advantage that it is much faster for most chips, but breaks those with
905 * non-contiguous address space (like AT45DB161D). When spi_read_chunked is
906 * fixed this method can be removed. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000907static int serprog_spi_read(struct flashctx *flash, uint8_t *buf,
908 unsigned int start, unsigned int len)
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000909{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000910 unsigned int i, cur_len;
911 const unsigned int max_read = spi_programmer_serprog.max_data_read;
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000912 for (i = 0; i < len; i += cur_len) {
913 int ret;
914 cur_len = min(max_read, (len - i));
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000915 ret = spi_nbyte_read(flash, start + i, buf + i, cur_len);
Urja Rannikkoc93f5f12011-09-15 23:38:14 +0000916 if (ret)
917 return ret;
918 }
919 return 0;
920}