jtagspi: fix build on MacOS
[openocd.git] / src / flash / nor / jtagspi.c
1 /***************************************************************************
2 * Copyright (C) 2015 Robert Jordens <jordens@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "imp.h"
23 #include <jtag/jtag.h>
24 #include <flash/nor/spi.h>
25 #include <helper/time_support.h>
26
27 #define JTAGSPI_MAX_TIMEOUT 3000
28
29
30 struct jtagspi_flash_bank {
31 struct jtag_tap *tap;
32 struct flash_device dev;
33 char devname[32];
34 bool probed;
35 bool always_4byte; /* use always 4-byte address except for basic read 0x03 */
36 uint32_t ir;
37 unsigned int addr_len; /* address length in bytes */
38 };
39
40 FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
41 {
42 struct jtagspi_flash_bank *info;
43
44 if (CMD_ARGC < 7)
45 return ERROR_COMMAND_SYNTAX_ERROR;
46
47 info = malloc(sizeof(struct jtagspi_flash_bank));
48 if (!info) {
49 LOG_ERROR("no memory for flash bank info");
50 return ERROR_FAIL;
51 }
52 bank->sectors = NULL;
53 bank->driver_priv = info;
54
55 info->tap = NULL;
56 info->probed = false;
57 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], info->ir);
58
59 return ERROR_OK;
60 }
61
62 static void jtagspi_set_ir(struct flash_bank *bank)
63 {
64 struct jtagspi_flash_bank *info = bank->driver_priv;
65 struct scan_field field;
66 uint8_t buf[4] = { 0 };
67
68 LOG_DEBUG("loading jtagspi ir");
69 buf_set_u32(buf, 0, info->tap->ir_length, info->ir);
70 field.num_bits = info->tap->ir_length;
71 field.out_value = buf;
72 field.in_value = NULL;
73 jtag_add_ir_scan(info->tap, &field, TAP_IDLE);
74 }
75
76 static void flip_u8(const uint8_t *in, uint8_t *out, unsigned int len)
77 {
78 for (unsigned int i = 0; i < len; i++)
79 out[i] = flip_u32(in[i], 8);
80 }
81
82 static int jtagspi_cmd(struct flash_bank *bank, uint8_t cmd,
83 uint8_t *write_buffer, unsigned int write_len, uint8_t *data_buffer, int data_len)
84 {
85 assert(write_buffer || write_len == 0);
86 assert(data_buffer || data_len == 0);
87
88 struct scan_field fields[6];
89
90 LOG_DEBUG("cmd=0x%02x write_len=%d data_len=%d", cmd, write_len, data_len);
91
92 /* negative data_len == read operation */
93 const bool is_read = (data_len < 0);
94 if (is_read)
95 data_len = -data_len;
96
97 int n = 0;
98 const uint8_t marker = 1;
99 fields[n].num_bits = 1;
100 fields[n].out_value = &marker;
101 fields[n].in_value = NULL;
102 n++;
103
104 /* transfer length = cmd + address + read/write,
105 * -1 due to the counter implementation */
106 uint8_t xfer_bits[4];
107 h_u32_to_be(xfer_bits, ((sizeof(cmd) + write_len + data_len) * CHAR_BIT) - 1);
108 flip_u8(xfer_bits, xfer_bits, sizeof(xfer_bits));
109 fields[n].num_bits = sizeof(xfer_bits) * CHAR_BIT;
110 fields[n].out_value = xfer_bits;
111 fields[n].in_value = NULL;
112 n++;
113
114 flip_u8(&cmd, &cmd, sizeof(cmd));
115 fields[n].num_bits = sizeof(cmd) * CHAR_BIT;
116 fields[n].out_value = &cmd;
117 fields[n].in_value = NULL;
118 n++;
119
120 if (write_len) {
121 flip_u8(write_buffer, write_buffer, write_len);
122 fields[n].num_bits = write_len * CHAR_BIT;
123 fields[n].out_value = write_buffer;
124 fields[n].in_value = NULL;
125 n++;
126 }
127
128 if (data_len > 0) {
129 if (is_read) {
130 fields[n].num_bits = jtag_tap_count_enabled();
131 fields[n].out_value = NULL;
132 fields[n].in_value = NULL;
133 n++;
134
135 fields[n].out_value = NULL;
136 fields[n].in_value = data_buffer;
137 } else {
138 flip_u8(data_buffer, data_buffer, data_len);
139 fields[n].out_value = data_buffer;
140 fields[n].in_value = NULL;
141 }
142 fields[n].num_bits = data_len * CHAR_BIT;
143 n++;
144 }
145
146 jtagspi_set_ir(bank);
147 /* passing from an IR scan to SHIFT-DR clears BYPASS registers */
148 struct jtagspi_flash_bank *info = bank->driver_priv;
149 jtag_add_dr_scan(info->tap, n, fields, TAP_IDLE);
150 int retval = jtag_execute_queue();
151
152 if (is_read)
153 flip_u8(data_buffer, data_buffer, data_len);
154 return retval;
155 }
156
157 COMMAND_HANDLER(jtagspi_handle_set)
158 {
159 struct flash_bank *bank = NULL;
160 struct jtagspi_flash_bank *info = NULL;
161 struct flash_sector *sectors = NULL;
162 uint32_t temp;
163 unsigned int index = 1;
164 int retval;
165
166 LOG_DEBUG("%s", __func__);
167
168 /* there are 6 mandatory arguments:
169 * devname, size_in_bytes, pagesize, read_cmd, unused, pprog_cmd */
170 if (index + 6 > CMD_ARGC) {
171 command_print(CMD, "jtagspi: not enough arguments");
172 return ERROR_COMMAND_SYNTAX_ERROR;
173 }
174
175 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
176 if (ERROR_OK != retval)
177 return retval;
178 info = bank->driver_priv;
179
180 /* invalidate all old info */
181 if (info->probed) {
182 bank->size = 0;
183 bank->num_sectors = 0;
184 if (bank->sectors)
185 free(bank->sectors);
186 bank->sectors = NULL;
187 info->always_4byte = false;
188 info->probed = false;
189 }
190 memset(&info->dev, 0, sizeof(info->dev));
191
192 strncpy(info->devname, CMD_ARGV[index++], sizeof(info->devname) - 1);
193 info->devname[sizeof(info->devname) - 1] = '\0';
194
195 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
196 info->dev.size_in_bytes = temp;
197 if ((temp & (temp - 1)) || (temp < (1UL << 8))) {
198 command_print(CMD, "jtagspi: device size must be 2^n with n >= 8");
199 return ERROR_COMMAND_SYNTAX_ERROR;
200 }
201
202 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
203 info->dev.pagesize = temp;
204 if (info->dev.pagesize == 0)
205 info->dev.pagesize = SPIFLASH_DEF_PAGESIZE;
206 if ((temp & (temp - 1)) || (temp > info->dev.size_in_bytes)) {
207 command_print(CMD, "jtagspi: page size must be 2^n and <= device size");
208 return ERROR_COMMAND_SYNTAX_ERROR;
209 }
210
211 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.read_cmd);
212 if ((info->dev.read_cmd != 0x03) &&
213 (info->dev.read_cmd != 0x13)) {
214 command_print(CMD, "jtagspi: only 0x03/0x13 READ allowed");
215 return ERROR_COMMAND_SYNTAX_ERROR;
216 }
217
218 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.qread_cmd);
219
220 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.pprog_cmd);
221 if ((info->dev.pprog_cmd != 0x02) &&
222 (info->dev.pprog_cmd != 0x12)) {
223 command_print(CMD, "jtagspi: only 0x02/0x12 PPRG allowed");
224 return ERROR_COMMAND_SYNTAX_ERROR;
225 }
226
227 /* remaining params are optional */
228 if (index < CMD_ARGC)
229 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.chip_erase_cmd);
230 else
231 info->dev.chip_erase_cmd = 0x00;
232
233 if (index < CMD_ARGC) {
234 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[index++], temp);
235 info->dev.sectorsize = temp;
236 if ((info->dev.sectorsize > info->dev.size_in_bytes) ||
237 (info->dev.sectorsize < info->dev.pagesize) || (temp & (temp - 1))) {
238 command_print(CMD, "jtagspi: sector size must be 2^n and <= device size");
239 return ERROR_COMMAND_SYNTAX_ERROR;
240 }
241
242 if (index < CMD_ARGC)
243 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], info->dev.erase_cmd);
244 else {
245 command_print(CMD, "jtagspi: erase command missing");
246 return ERROR_COMMAND_SYNTAX_ERROR;
247 }
248 } else {
249 /* no sector size / sector erase cmd given, treat whole bank as a single sector */
250 info->dev.erase_cmd = 0x00;
251 info->dev.sectorsize = info->dev.size_in_bytes;
252 }
253
254 if (index < CMD_ARGC) {
255 command_print(CMD, "jtagspi: extra arguments");
256 return ERROR_COMMAND_SYNTAX_ERROR;
257 }
258
259 /* set correct size value */
260 bank->size = info->dev.size_in_bytes;
261
262 /* calculate address length in bytes */
263 if (bank->size <= (1UL << 8))
264 info->addr_len = 1;
265 else if (bank->size <= (1UL << 16))
266 info->addr_len = 2;
267 else if (bank->size <= (1UL << 24))
268 info->addr_len = 3;
269 else {
270 info->addr_len = 4;
271 LOG_WARNING("4-byte addresses needed, might need extra command to enable");
272 }
273
274 /* create and fill sectors array */
275 bank->num_sectors =
276 info->dev.size_in_bytes / info->dev.sectorsize;
277 sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
278 if (!sectors) {
279 LOG_ERROR("Not enough memory");
280 return ERROR_FAIL;
281 }
282
283 for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
284 sectors[sector].offset = sector * (info->dev.sectorsize);
285 sectors[sector].size = info->dev.sectorsize;
286 sectors[sector].is_erased = -1;
287 sectors[sector].is_protected = 0;
288 }
289
290 bank->sectors = sectors;
291 info->dev.name = info->devname;
292 if (info->dev.size_in_bytes / 4096)
293 LOG_INFO("flash \'%s\' id = unknown\nflash size = %" PRIu32 " kbytes",
294 info->dev.name, info->dev.size_in_bytes / 1024);
295 else
296 LOG_INFO("flash \'%s\' id = unknown\nflash size = %" PRIu32 " bytes",
297 info->dev.name, info->dev.size_in_bytes);
298 info->probed = true;
299
300 return ERROR_OK;
301 }
302
303 COMMAND_HANDLER(jtagspi_handle_cmd)
304 {
305 struct flash_bank *bank;
306 unsigned int index = 1;
307 const int max = 21;
308 uint8_t num_write, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
309 uint8_t data, *ptr;
310 char temp[4], output[(2 + max + (1 << CHAR_BIT)) * 3 + 8];
311 int retval;
312
313 LOG_DEBUG("%s", __func__);
314
315 if (CMD_ARGC < 3) {
316 command_print(CMD, "jtagspi: not enough arguments");
317 return ERROR_COMMAND_SYNTAX_ERROR;
318 }
319
320 num_write = CMD_ARGC - 2;
321 if (num_write > max) {
322 LOG_ERROR("at most %d bytes may be send", max);
323 return ERROR_COMMAND_SYNTAX_ERROR;
324 }
325
326 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
327 if (ERROR_OK != retval)
328 return retval;
329
330 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], num_read);
331
332 snprintf(output, sizeof(output), "spi: ");
333 for (ptr = &write_buffer[0] ; index < CMD_ARGC; index++) {
334 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index], data);
335 *ptr++ = data;
336 snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
337 strncat(output, temp, sizeof(output) - strlen(output) - 1);
338 }
339 strncat(output, "-> ", sizeof(output) - strlen(output) - 1);
340
341 /* process command */
342 ptr = &read_buffer[0];
343 jtagspi_cmd(bank, write_buffer[0], &write_buffer[1], num_write - 1, ptr, -num_read);
344 if (retval != ERROR_OK)
345 return retval;
346
347 for ( ; num_read > 0; num_read--) {
348 snprintf(temp, sizeof(temp), "%02" PRIx8 " ", *ptr++);
349 strncat(output, temp, sizeof(output) - strlen(output) - 1);
350 }
351 command_print(CMD, "%s", output);
352
353 return ERROR_OK;
354 }
355
356 COMMAND_HANDLER(jtagspi_handle_always_4byte)
357 {
358 struct flash_bank *bank;
359 struct jtagspi_flash_bank *jtagspi_info;
360 int retval;
361
362 LOG_DEBUG("%s", __func__);
363
364 if ((CMD_ARGC != 1) && (CMD_ARGC != 2))
365 return ERROR_COMMAND_SYNTAX_ERROR;
366
367 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
368 if (ERROR_OK != retval)
369 return retval;
370
371 jtagspi_info = bank->driver_priv;
372
373 if (CMD_ARGC == 1)
374 command_print(CMD, jtagspi_info->always_4byte ? "on" : "off");
375 else
376 COMMAND_PARSE_BOOL(CMD_ARGV[1], jtagspi_info->always_4byte, "on", "off");
377
378 return ERROR_OK;
379 }
380
381 static int jtagspi_probe(struct flash_bank *bank)
382 {
383 struct jtagspi_flash_bank *info = bank->driver_priv;
384 struct flash_sector *sectors;
385 const struct flash_device *p;
386 uint8_t in_buf[3];
387 uint32_t id, sectorsize;
388
389 if (bank->sectors) {
390 free(bank->sectors);
391 bank->sectors = NULL;
392 }
393 info->probed = false;
394
395 if (!bank->target->tap) {
396 LOG_ERROR("Target has no JTAG tap");
397 return ERROR_FAIL;
398 }
399 info->tap = bank->target->tap;
400
401 jtagspi_cmd(bank, SPIFLASH_READ_ID, NULL, 0, in_buf, -3);
402 /* the table in spi.c has the manufacturer byte (first) as the lsb */
403 id = le_to_h_u24(in_buf);
404
405 memset(&info->dev, 0, sizeof(info->dev));
406 for (p = flash_devices; p->name ; p++)
407 if (p->device_id == id) {
408 memcpy(&info->dev, p, sizeof(info->dev));
409 break;
410 }
411
412 if (!(p->name)) {
413 LOG_ERROR("Unknown flash device (ID 0x%06" PRIx32 ")", id & 0xFFFFFF);
414 return ERROR_FAIL;
415 }
416
417 LOG_INFO("Found flash device \'%s\' (ID 0x%06" PRIx32 ")",
418 info->dev.name, info->dev.device_id & 0xFFFFFF);
419
420 /* Set correct size value */
421 bank->size = info->dev.size_in_bytes;
422
423 /* calculate address length in bytes */
424 if (bank->size <= (1UL << 8))
425 info->addr_len = 1;
426 else if (bank->size <= (1UL << 16))
427 info->addr_len = 2;
428 else if (bank->size <= (1UL << 24))
429 info->addr_len = 3;
430 else {
431 info->addr_len = 4;
432 LOG_WARNING("4-byte addresses needed, might need extra command to enable");
433 }
434
435 /* if no sectors, treat whole bank as single sector */
436 sectorsize = info->dev.sectorsize ?
437 info->dev.sectorsize : info->dev.size_in_bytes;
438
439 /* create and fill sectors array */
440 bank->num_sectors = info->dev.size_in_bytes / sectorsize;
441 sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
442 if (!sectors) {
443 LOG_ERROR("not enough memory");
444 return ERROR_FAIL;
445 }
446
447 for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
448 sectors[sector].offset = sector * sectorsize;
449 sectors[sector].size = sectorsize;
450 sectors[sector].is_erased = -1;
451 sectors[sector].is_protected = 0;
452 }
453
454 bank->sectors = sectors;
455 info->probed = true;
456 return ERROR_OK;
457 }
458
459 static int jtagspi_auto_probe(struct flash_bank *bank)
460 {
461 struct jtagspi_flash_bank *info = bank->driver_priv;
462
463 if (info->probed)
464 return ERROR_OK;
465 return jtagspi_probe(bank);
466 }
467
468 static int jtagspi_read_status(struct flash_bank *bank, uint32_t *status)
469 {
470 uint8_t buf;
471 int err = jtagspi_cmd(bank, SPIFLASH_READ_STATUS, NULL, 0, &buf, -1);
472 if (err == ERROR_OK) {
473 *status = buf;
474 LOG_DEBUG("status=0x%02" PRIx32, *status);
475 }
476 return err;
477 }
478
479 static int jtagspi_wait(struct flash_bank *bank, int timeout_ms)
480 {
481 int64_t t0 = timeval_ms();
482 int64_t dt;
483
484 do {
485 dt = timeval_ms() - t0;
486
487 uint32_t status = (uint32_t)-1;
488 int retval = jtagspi_read_status(bank, &status);
489 if (retval != ERROR_OK)
490 return retval;
491
492 if ((status & SPIFLASH_BSY_BIT) == 0) {
493 LOG_DEBUG("waited %" PRId64 " ms", dt);
494 return ERROR_OK;
495 }
496 alive_sleep(1);
497 } while (dt <= timeout_ms);
498
499 LOG_ERROR("timeout, device still busy");
500 return ERROR_FAIL;
501 }
502
503 static int jtagspi_write_enable(struct flash_bank *bank)
504 {
505 jtagspi_cmd(bank, SPIFLASH_WRITE_ENABLE, NULL, 0, NULL, 0);
506
507 uint32_t status = (uint32_t)-1;
508 int retval = jtagspi_read_status(bank, &status);
509 if (retval != ERROR_OK)
510 return retval;
511
512 if ((status & SPIFLASH_WE_BIT) == 0) {
513 LOG_ERROR("Cannot enable write to flash. Status=0x%02" PRIx32, status);
514 return ERROR_FAIL;
515 }
516 return ERROR_OK;
517 }
518
519 static int jtagspi_bulk_erase(struct flash_bank *bank)
520 {
521 struct jtagspi_flash_bank *info = bank->driver_priv;
522 int retval;
523 int64_t t0 = timeval_ms();
524
525 if (info->dev.chip_erase_cmd == 0x00)
526 return ERROR_FLASH_OPER_UNSUPPORTED;
527
528 retval = jtagspi_write_enable(bank);
529 if (retval != ERROR_OK)
530 return retval;
531
532 jtagspi_cmd(bank, info->dev.chip_erase_cmd, NULL, 0, NULL, 0);
533 if (retval != ERROR_OK)
534 return retval;
535
536 retval = jtagspi_wait(bank, bank->num_sectors * JTAGSPI_MAX_TIMEOUT);
537 LOG_INFO("took %" PRId64 " ms", timeval_ms() - t0);
538 return retval;
539 }
540
541 static uint8_t *fill_addr(uint32_t addr, unsigned int addr_len, uint8_t *buffer)
542 {
543 for (buffer += addr_len; addr_len > 0; --addr_len) {
544 *--buffer = addr;
545 addr >>= 8;
546 }
547
548 return buffer;
549 }
550
551 static int jtagspi_sector_erase(struct flash_bank *bank, unsigned int sector)
552 {
553 struct jtagspi_flash_bank *info = bank->driver_priv;
554 int retval;
555 uint8_t addr[sizeof(uint32_t)];
556 int64_t t0 = timeval_ms();
557
558 retval = jtagspi_write_enable(bank);
559 if (retval != ERROR_OK)
560 return retval;
561
562 /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
563 unsigned int addr_len = info->always_4byte ? 4 : info->addr_len;
564
565 retval = jtagspi_cmd(bank, info->dev.erase_cmd, fill_addr(bank->sectors[sector].offset, addr_len, addr),
566 addr_len, NULL, 0);
567 if (retval != ERROR_OK)
568 return retval;
569
570 retval = jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT);
571 LOG_INFO("sector %u took %" PRId64 " ms", sector, timeval_ms() - t0);
572 return retval;
573 }
574
575 static int jtagspi_erase(struct flash_bank *bank, unsigned int first,
576 unsigned int last)
577 {
578 struct jtagspi_flash_bank *info = bank->driver_priv;
579 int retval = ERROR_OK;
580
581 LOG_DEBUG("erase from sector %u to sector %u", first, last);
582
583 if ((last < first) || (last >= bank->num_sectors)) {
584 LOG_ERROR("Flash sector invalid");
585 return ERROR_FLASH_SECTOR_INVALID;
586 }
587
588 if (!(info->probed)) {
589 LOG_ERROR("Flash bank not probed");
590 return ERROR_FLASH_BANK_NOT_PROBED;
591 }
592
593 for (unsigned int sector = first; sector <= last; sector++) {
594 if (bank->sectors[sector].is_protected) {
595 LOG_ERROR("Flash sector %u protected", sector);
596 return ERROR_FAIL;
597 }
598 }
599
600 if (first == 0 && last == (bank->num_sectors - 1) &&
601 info->dev.chip_erase_cmd != 0x00 &&
602 info->dev.chip_erase_cmd != info->dev.erase_cmd) {
603 LOG_DEBUG("Trying bulk erase.");
604 retval = jtagspi_bulk_erase(bank);
605 if (retval == ERROR_OK)
606 return retval;
607 else
608 LOG_WARNING("Bulk flash erase failed. Falling back to sector erase.");
609 }
610
611 if (info->dev.erase_cmd == 0x00)
612 return ERROR_FLASH_OPER_UNSUPPORTED;
613
614 for (unsigned int sector = first; sector <= last; sector++) {
615 retval = jtagspi_sector_erase(bank, sector);
616 if (retval != ERROR_OK) {
617 LOG_ERROR("Sector erase failed.");
618 break;
619 }
620 }
621
622 return retval;
623 }
624
625 static int jtagspi_protect(struct flash_bank *bank, int set, unsigned int first,
626 unsigned int last)
627 {
628 for (unsigned int sector = first; sector <= last; sector++)
629 bank->sectors[sector].is_protected = set;
630 return ERROR_OK;
631 }
632
633 static int jtagspi_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
634 {
635 struct jtagspi_flash_bank *info = bank->driver_priv;
636 uint32_t pagesize, currsize;
637 uint8_t addr[sizeof(uint32_t)];
638 int retval;
639
640 if (!(info->probed)) {
641 LOG_ERROR("Flash bank not probed.");
642 return ERROR_FLASH_BANK_NOT_PROBED;
643 }
644
645 /* if no sectorsize, use reasonable default */
646 pagesize = info->dev.sectorsize ? info->dev.sectorsize : info->dev.pagesize;
647 if (pagesize == 0)
648 pagesize = (info->dev.size_in_bytes <= SPIFLASH_DEF_PAGESIZE) ?
649 info->dev.size_in_bytes : SPIFLASH_DEF_PAGESIZE;
650
651 /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
652 unsigned int addr_len = ((info->dev.read_cmd != 0x03) && info->always_4byte) ? 4 : info->addr_len;
653
654 while (count > 0) {
655 /* length up to end of current page */
656 currsize = ((offset + pagesize) & ~(pagesize - 1)) - offset;
657 /* but no more than remaining size */
658 currsize = (count < currsize) ? count : currsize;
659
660 retval = jtagspi_cmd(bank, info->dev.read_cmd, fill_addr(offset, addr_len, addr),
661 addr_len, buffer, -currsize);
662 if (retval != ERROR_OK) {
663 LOG_ERROR("page read error");
664 return retval;
665 }
666 LOG_DEBUG("read page at 0x%08" PRIx32, offset);
667 offset += currsize;
668 buffer += currsize;
669 count -= currsize;
670 }
671 return ERROR_OK;
672 }
673
674 static int jtagspi_page_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
675 {
676 struct jtagspi_flash_bank *info = bank->driver_priv;
677 uint8_t addr[sizeof(uint32_t)];
678 int retval;
679
680 retval = jtagspi_write_enable(bank);
681 if (retval != ERROR_OK)
682 return retval;
683
684 /* ATXP032/064/128 use always 4-byte addresses except for 0x03 read */
685 unsigned int addr_len = ((info->dev.read_cmd != 0x03) && info->always_4byte) ? 4 : info->addr_len;
686
687 retval = jtagspi_cmd(bank, info->dev.pprog_cmd, fill_addr(offset, addr_len, addr),
688 addr_len, (uint8_t *) buffer, count);
689 if (retval != ERROR_OK)
690 return retval;
691 return jtagspi_wait(bank, JTAGSPI_MAX_TIMEOUT);
692 }
693
694 static int jtagspi_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
695 {
696 struct jtagspi_flash_bank *info = bank->driver_priv;
697 uint32_t pagesize, currsize;
698 int retval;
699
700 if (!(info->probed)) {
701 LOG_ERROR("Flash bank not probed.");
702 return ERROR_FLASH_BANK_NOT_PROBED;
703 }
704
705 /* if no write pagesize, use reasonable default */
706 pagesize = info->dev.pagesize ? info->dev.pagesize : SPIFLASH_DEF_PAGESIZE;
707
708 while (count > 0) {
709 /* length up to end of current page */
710 currsize = ((offset + pagesize) & ~(pagesize - 1)) - offset;
711 /* but no more than remaining size */
712 currsize = (count < currsize) ? count : currsize;
713
714 retval = jtagspi_page_write(bank, buffer, offset, currsize);
715 if (retval != ERROR_OK) {
716 LOG_ERROR("page write error");
717 return retval;
718 }
719 LOG_DEBUG("wrote page at 0x%08" PRIx32, offset);
720 offset += currsize;
721 buffer += currsize;
722 count -= currsize;
723 }
724 return ERROR_OK;
725 }
726
727 static int jtagspi_info(struct flash_bank *bank, struct command_invocation *cmd)
728 {
729 struct jtagspi_flash_bank *info = bank->driver_priv;
730
731 if (!(info->probed)) {
732 command_print_sameline(cmd, "\nJTAGSPI flash bank not probed yet\n");
733 return ERROR_OK;
734 }
735
736 command_print_sameline(cmd, "flash \'%s\', device id = 0x%06" PRIx32
737 ", flash size = %" PRIu32 " %sbytes\n(page size = %" PRIu32
738 ", read = 0x%02" PRIx8 ", qread = 0x%02" PRIx8
739 ", pprog = 0x%02" PRIx8 ", mass_erase = 0x%02" PRIx8
740 ", sector size = %" PRIu32 " %sbytes, sector_erase = 0x%02" PRIx8 ")",
741 info->dev.name, info->dev.device_id & 0xFFFFFF,
742 bank->size / 4096 ? bank->size / 1024 : bank->size,
743 bank->size / 4096 ? "k" : "", info->dev.pagesize,
744 info->dev.read_cmd, info->dev.qread_cmd,
745 info->dev.pprog_cmd, info->dev.chip_erase_cmd,
746 info->dev.sectorsize / 4096 ?
747 info->dev.sectorsize / 1024 : info->dev.sectorsize,
748 info->dev.sectorsize / 4096 ? "k" : "",
749 info->dev.erase_cmd);
750
751 return ERROR_OK;
752 }
753
754 static const struct command_registration jtagspi_exec_command_handlers[] = {
755 {
756 .name = "set",
757 .handler = jtagspi_handle_set,
758 .mode = COMMAND_EXEC,
759 .usage = "bank_id name chip_size page_size read_cmd unused pprg_cmd "
760 "[ mass_erase_cmd ] [ sector_size sector_erase_cmd ]",
761 .help = "Set device parameters if not autodetected.",
762 },
763 {
764 .name = "cmd",
765 .handler = jtagspi_handle_cmd,
766 .mode = COMMAND_EXEC,
767 .usage = "bank_id num_resp cmd_byte ...",
768 .help = "Send low-level command cmd_byte and following bytes, read num_bytes.",
769 },
770 {
771 .name = "always_4byte",
772 .handler = jtagspi_handle_always_4byte,
773 .mode = COMMAND_EXEC,
774 .usage = "bank_id [ on | off ]",
775 .help = "Use always 4-byte address except for basic 0x03.",
776 },
777
778 COMMAND_REGISTRATION_DONE
779 };
780
781 static const struct command_registration jtagspi_command_handlers[] = {
782 {
783 .name = "jtagspi",
784 .mode = COMMAND_ANY,
785 .help = "jtagspi command group",
786 .usage = "",
787 .chain = jtagspi_exec_command_handlers,
788 },
789 COMMAND_REGISTRATION_DONE
790 };
791
792 const struct flash_driver jtagspi_flash = {
793 .name = "jtagspi",
794 .commands = jtagspi_command_handlers,
795 .flash_bank_command = jtagspi_flash_bank_command,
796 .erase = jtagspi_erase,
797 .protect = jtagspi_protect,
798 .write = jtagspi_write,
799 .read = jtagspi_read,
800 .probe = jtagspi_probe,
801 .auto_probe = jtagspi_auto_probe,
802 .erase_check = default_flash_blank_check,
803 .info = jtagspi_info,
804 .free_driver_priv = default_flash_free_driver_priv,
805 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)