8ecd27ae8d099af5f1b2672fb3c54c64e048b5c6
[openocd.git] / src / flash / nor / esirisc_flash.c
1 /***************************************************************************
2 * Copyright (C) 2018 by Square, Inc. *
3 * Steven Stallion <stallion@squareup.com> *
4 * James Zhao <hjz@squareup.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <flash/common.h>
25 #include <flash/nor/imp.h>
26 #include <helper/command.h>
27 #include <helper/log.h>
28 #include <helper/time_support.h>
29 #include <helper/types.h>
30 #include <target/esirisc.h>
31 #include <target/target.h>
32
33 /* eSi-TSMC Flash Registers */
34 #define CONTROL 0x00 /* Control Register */
35 #define TIMING0 0x04 /* Timing Register 0 */
36 #define TIMING1 0x08 /* Timing Register 1 */
37 #define TIMING2 0x0c /* Timing Register 2 */
38 #define UNLOCK1 0x18 /* Unlock 1 */
39 #define UNLOCK2 0x1c /* Unlock 2 */
40 #define ADDRESS 0x20 /* Erase/Program Address */
41 #define PB_DATA 0x24 /* Program Buffer Data */
42 #define PB_INDEX 0x28 /* Program Buffer Index */
43 #define STATUS 0x2c /* Status Register */
44 #define REDUN_0 0x30 /* Redundant Address 0 */
45 #define REDUN_1 0x34 /* Redundant Address 1 */
46
47 /* Control Fields */
48 #define CONTROL_SLM (1<<0) /* Sleep Mode */
49 #define CONTROL_WP (1<<1) /* Register Write Protect */
50 #define CONTROL_E (1<<3) /* Erase */
51 #define CONTROL_EP (1<<4) /* Erase Page */
52 #define CONTROL_P (1<<5) /* Program Flash */
53 #define CONTROL_ERC (1<<6) /* Erase Reference Cell */
54 #define CONTROL_R (1<<7) /* Recall Trim Code */
55 #define CONTROL_AP (1<<8) /* Auto-Program */
56
57 /* Timing Fields */
58 #define TIMING0_R(x) (((x) << 0) & 0x3f) /* Read Wait States */
59 #define TIMING0_F(x) (((x) << 16) & 0xffff0000) /* Tnvh Clock Cycles */
60 #define TIMING1_E(x) (((x) << 0) & 0xffffff) /* Tme/Terase/Tre Clock Cycles */
61 #define TIMING2_P(x) (((x) << 0) & 0xffff) /* Tprog Clock Cycles */
62 #define TIMING2_H(x) (((x) << 16) & 0xff0000) /* Clock Cycles in 100ns */
63 #define TIMING2_T(x) (((x) << 24) & 0xf000000) /* Clock Cycles in 10ns */
64
65 /* Status Fields */
66 #define STATUS_BUSY (1<<0) /* Busy (Erase/Program) */
67 #define STATUS_WER (1<<1) /* Write Protect Error */
68 #define STATUS_DR (1<<2) /* Disable Redundancy */
69 #define STATUS_DIS (1<<3) /* Discharged */
70 #define STATUS_BO (1<<4) /* Brown Out */
71
72 /* Redundant Address Fields */
73 #define REDUN_R (1<<0) /* Used */
74 #define REDUN_P(x) (((x) << 12) & 0x7f000) /* Redundant Page Address */
75
76 /*
77 * The eSi-TSMC Flash manual provides two sets of timings based on the
78 * underlying flash process. By default, 90nm is assumed.
79 */
80 #if 0 /* 55nm */
81 #define TNVH 5000 /* 5us */
82 #define TME 80000000 /* 80ms */
83 #define TERASE 160000000 /* 160ms */
84 #define TRE 100000000 /* 100ms */
85 #define TPROG 8000 /* 8us */
86 #else /* 90nm */
87 #define TNVH 5000 /* 5us */
88 #define TME 20000000 /* 20ms */
89 #define TERASE 40000000 /* 40ms */
90 #define TRE 40000000 /* 40ms */
91 #define TPROG 40000 /* 40us */
92 #endif
93
94 #define CONTROL_TIMEOUT 5000 /* 5s */
95 #define PAGE_SIZE 4096
96 #define PB_MAX 32
97
98 #define NUM_NS_PER_S 1000000000ULL
99
100 struct esirisc_flash_bank {
101 bool probed;
102 uint32_t cfg;
103 uint32_t clock;
104 uint32_t wait_states;
105 };
106
107 static const struct command_registration esirisc_flash_command_handlers[];
108
109 FLASH_BANK_COMMAND_HANDLER(esirisc_flash_bank_command)
110 {
111 struct esirisc_flash_bank *esirisc_info;
112 struct command *esirisc_cmd;
113
114 if (CMD_ARGC < 9)
115 return ERROR_COMMAND_SYNTAX_ERROR;
116
117 esirisc_info = calloc(1, sizeof(struct esirisc_flash_bank));
118
119 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], esirisc_info->cfg);
120 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[7], esirisc_info->clock);
121 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[8], esirisc_info->wait_states);
122
123 bank->driver_priv = esirisc_info;
124
125 /* register commands using existing esirisc context */
126 esirisc_cmd = command_find_in_context(CMD_CTX, "esirisc");
127 register_commands(CMD_CTX, esirisc_cmd, esirisc_flash_command_handlers);
128
129 return ERROR_OK;
130 }
131
132 /*
133 * Register writes are ignored if the control.WP flag is set; the
134 * following sequence is required to modify this flag even when
135 * protection is disabled.
136 */
137 static int esirisc_flash_unlock(struct flash_bank *bank)
138 {
139 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
140 struct target *target = bank->target;
141
142 target_write_u32(target, esirisc_info->cfg + UNLOCK1, 0x7123);
143 target_write_u32(target, esirisc_info->cfg + UNLOCK2, 0x812a);
144 target_write_u32(target, esirisc_info->cfg + UNLOCK1, 0xbee1);
145
146 return ERROR_OK;
147 }
148
149 static int esirisc_flash_disable_protect(struct flash_bank *bank)
150 {
151 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
152 struct target *target = bank->target;
153 uint32_t control;
154
155 target_read_u32(target, esirisc_info->cfg + CONTROL, &control);
156 if (!(control & CONTROL_WP))
157 return ERROR_OK;
158
159 esirisc_flash_unlock(bank);
160
161 control &= ~CONTROL_WP;
162
163 target_write_u32(target, esirisc_info->cfg + CONTROL, control);
164
165 return ERROR_OK;
166 }
167
168 static int esirisc_flash_enable_protect(struct flash_bank *bank)
169 {
170 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
171 struct target *target = bank->target;
172 uint32_t control;
173
174 target_read_u32(target, esirisc_info->cfg + CONTROL, &control);
175 if (control & CONTROL_WP)
176 return ERROR_OK;
177
178 esirisc_flash_unlock(bank);
179
180 control |= CONTROL_WP;
181
182 target_write_u32(target, esirisc_info->cfg + CONTROL, control);
183
184 return ERROR_OK;
185 }
186
187 static int esirisc_flash_check_status(struct flash_bank *bank)
188 {
189 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
190 struct target *target = bank->target;
191 uint32_t status;
192
193 target_read_u32(target, esirisc_info->cfg + STATUS, &status);
194 if (status & STATUS_WER) {
195 LOG_ERROR("%s: bad status: 0x%" PRIx32, bank->name, status);
196 return ERROR_FLASH_OPERATION_FAILED;
197 }
198
199 return ERROR_OK;
200 }
201
202 static int esirisc_flash_clear_status(struct flash_bank *bank)
203 {
204 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
205 struct target *target = bank->target;
206
207 target_write_u32(target, esirisc_info->cfg + STATUS, STATUS_WER);
208
209 return ERROR_OK;
210 }
211
212 static int esirisc_flash_wait(struct flash_bank *bank, int ms)
213 {
214 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
215 struct target *target = bank->target;
216 uint32_t status;
217 int64_t t;
218
219 t = timeval_ms();
220 for (;;) {
221 target_read_u32(target, esirisc_info->cfg + STATUS, &status);
222 if (!(status & STATUS_BUSY))
223 return ERROR_OK;
224
225 if ((timeval_ms() - t) > ms)
226 return ERROR_TARGET_TIMEOUT;
227
228 keep_alive();
229 }
230 }
231
232 static int esirisc_flash_control(struct flash_bank *bank, uint32_t control)
233 {
234 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
235 struct target *target = bank->target;
236
237 esirisc_flash_clear_status(bank);
238
239 target_write_u32(target, esirisc_info->cfg + CONTROL, control);
240
241 int retval = esirisc_flash_wait(bank, CONTROL_TIMEOUT);
242 if (retval != ERROR_OK) {
243 LOG_ERROR("%s: control timed out: 0x%" PRIx32, bank->name, control);
244 return retval;
245 }
246
247 return esirisc_flash_check_status(bank);
248 }
249
250 static int esirisc_flash_recall(struct flash_bank *bank)
251 {
252 return esirisc_flash_control(bank, CONTROL_R);
253 }
254
255 static int esirisc_flash_erase(struct flash_bank *bank, int first, int last)
256 {
257 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
258 struct target *target = bank->target;
259 int retval = ERROR_OK;
260
261 if (target->state != TARGET_HALTED)
262 return ERROR_TARGET_NOT_HALTED;
263
264 esirisc_flash_disable_protect(bank);
265
266 for (int page = first; page < last; ++page) {
267 uint32_t address = page * PAGE_SIZE;
268
269 target_write_u32(target, esirisc_info->cfg + ADDRESS, address);
270
271 retval = esirisc_flash_control(bank, CONTROL_EP);
272 if (retval != ERROR_OK) {
273 LOG_ERROR("%s: failed to erase address: 0x%" PRIx32, bank->name, address);
274 break;
275 }
276 }
277
278 esirisc_flash_enable_protect(bank);
279
280 return retval;
281 }
282
283 static int esirisc_flash_mass_erase(struct flash_bank *bank)
284 {
285 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
286 struct target *target = bank->target;
287 int retval;
288
289 if (target->state != TARGET_HALTED)
290 return ERROR_TARGET_NOT_HALTED;
291
292 esirisc_flash_disable_protect(bank);
293
294 target_write_u32(target, esirisc_info->cfg + ADDRESS, 0);
295
296 retval = esirisc_flash_control(bank, CONTROL_E);
297 if (retval != ERROR_OK)
298 LOG_ERROR("%s: failed to mass erase", bank->name);
299
300 esirisc_flash_enable_protect(bank);
301
302 return retval;
303 }
304
305 /*
306 * Per TSMC, the reference cell should be erased once per sample. This
307 * is typically done during wafer sort, however we include support for
308 * those that may need to calibrate flash at a later time.
309 */
310 static int esirisc_flash_ref_erase(struct flash_bank *bank)
311 {
312 struct target *target = bank->target;
313 int retval;
314
315 if (target->state != TARGET_HALTED)
316 return ERROR_TARGET_NOT_HALTED;
317
318 esirisc_flash_disable_protect(bank);
319
320 retval = esirisc_flash_control(bank, CONTROL_ERC);
321 if (retval != ERROR_OK)
322 LOG_ERROR("%s: failed to erase reference cell", bank->name);
323
324 esirisc_flash_enable_protect(bank);
325
326 return retval;
327 }
328
329 static int esirisc_flash_protect(struct flash_bank *bank, int set, int first, int last)
330 {
331 struct target *target = bank->target;
332
333 if (target->state != TARGET_HALTED)
334 return ERROR_TARGET_NOT_HALTED;
335
336 if (set)
337 esirisc_flash_enable_protect(bank);
338 else
339 esirisc_flash_disable_protect(bank);
340
341 return ERROR_OK;
342 }
343
344 static int esirisc_flash_fill_pb(struct flash_bank *bank,
345 const uint8_t *buffer, uint32_t count)
346 {
347 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
348 struct target *target = bank->target;
349 struct esirisc_common *esirisc = target_to_esirisc(target);
350
351 /*
352 * The pb_index register is auto-incremented when pb_data is written
353 * and should be cleared before each operation.
354 */
355 target_write_u32(target, esirisc_info->cfg + PB_INDEX, 0);
356
357 /*
358 * The width of the pb_data register depends on the underlying
359 * target; writing one byte at a time incurs a significant
360 * performance penalty and should be avoided.
361 */
362 while (count > 0) {
363 uint32_t max_bytes = DIV_ROUND_UP(esirisc->num_bits, 8);
364 uint32_t num_bytes = MIN(count, max_bytes);
365
366 target_write_buffer(target, esirisc_info->cfg + PB_DATA, num_bytes, buffer);
367
368 buffer += num_bytes;
369 count -= num_bytes;
370 }
371
372 return ERROR_OK;
373 }
374
375 static int esirisc_flash_write(struct flash_bank *bank,
376 const uint8_t *buffer, uint32_t offset, uint32_t count)
377 {
378 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
379 struct target *target = bank->target;
380 int retval = ERROR_OK;
381
382 if (target->state != TARGET_HALTED)
383 return ERROR_TARGET_NOT_HALTED;
384
385 esirisc_flash_disable_protect(bank);
386
387 /*
388 * The address register is auto-incremented based on the contents of
389 * the pb_index register after each operation completes. It can be
390 * set once provided pb_index is cleared before each operation.
391 */
392 target_write_u32(target, esirisc_info->cfg + ADDRESS, offset);
393
394 /*
395 * Care must be taken when filling the program buffer; a maximum of
396 * 32 bytes may be written at a time and may not cross a 32-byte
397 * boundary based on the current offset.
398 */
399 while (count > 0) {
400 uint32_t max_bytes = PB_MAX - (offset & 0x1f);
401 uint32_t num_bytes = MIN(count, max_bytes);
402
403 esirisc_flash_fill_pb(bank, buffer, num_bytes);
404
405 retval = esirisc_flash_control(bank, CONTROL_P);
406 if (retval != ERROR_OK) {
407 LOG_ERROR("%s: failed to program address: 0x%" PRIx32, bank->name, offset);
408 break;
409 }
410
411 buffer += num_bytes;
412 offset += num_bytes;
413 count -= num_bytes;
414 }
415
416 esirisc_flash_enable_protect(bank);
417
418 return retval;
419 }
420
421 static uint32_t esirisc_flash_num_cycles(struct flash_bank *bank, uint64_t ns)
422 {
423 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
424
425 /* apply scaling factor to avoid truncation */
426 uint64_t hz = (uint64_t)esirisc_info->clock * 1000;
427 uint64_t num_cycles = ((hz / NUM_NS_PER_S) * ns) / 1000;
428
429 if (hz % NUM_NS_PER_S > 0)
430 num_cycles++;
431
432 return num_cycles;
433 }
434
435 static int esirisc_flash_init(struct flash_bank *bank)
436 {
437 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
438 struct target *target = bank->target;
439 uint32_t value;
440 int retval;
441
442 esirisc_flash_disable_protect(bank);
443
444 /* initialize timing registers */
445 value = TIMING0_F(esirisc_flash_num_cycles(bank, TNVH))
446 | TIMING0_R(esirisc_info->wait_states);
447
448 LOG_DEBUG("TIMING0: 0x%" PRIx32, value);
449 target_write_u32(target, esirisc_info->cfg + TIMING0, value);
450
451 value = TIMING1_E(esirisc_flash_num_cycles(bank, TERASE));
452
453 LOG_DEBUG("TIMING1: 0x%" PRIx32, value);
454 target_write_u32(target, esirisc_info->cfg + TIMING1, value);
455
456 value = TIMING2_T(esirisc_flash_num_cycles(bank, 10))
457 | TIMING2_H(esirisc_flash_num_cycles(bank, 100))
458 | TIMING2_P(esirisc_flash_num_cycles(bank, TPROG));
459
460 LOG_DEBUG("TIMING2: 0x%" PRIx32, value);
461 target_write_u32(target, esirisc_info->cfg + TIMING2, value);
462
463 /* recall trim code */
464 retval = esirisc_flash_recall(bank);
465 if (retval != ERROR_OK)
466 LOG_ERROR("%s: failed to recall trim code", bank->name);
467
468 esirisc_flash_enable_protect(bank);
469
470 return retval;
471 }
472
473 static int esirisc_flash_probe(struct flash_bank *bank)
474 {
475 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
476 struct target *target = bank->target;
477 int retval;
478
479 if (target->state != TARGET_HALTED)
480 return ERROR_TARGET_NOT_HALTED;
481
482 bank->num_sectors = bank->size / PAGE_SIZE;
483 bank->sectors = alloc_block_array(0, PAGE_SIZE, bank->num_sectors);
484
485 /*
486 * Register write protection is enforced using a single protection
487 * block for the entire bank. This is as good as it gets.
488 */
489 bank->num_prot_blocks = 1;
490 bank->prot_blocks = alloc_block_array(0, bank->size, bank->num_prot_blocks);
491
492 retval = esirisc_flash_init(bank);
493 if (retval != ERROR_OK) {
494 LOG_ERROR("%s: failed to initialize bank", bank->name);
495 return retval;
496 }
497
498 esirisc_info->probed = true;
499
500 return ERROR_OK;
501 }
502
503 static int esirisc_flash_auto_probe(struct flash_bank *bank)
504 {
505 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
506
507 if (esirisc_info->probed)
508 return ERROR_OK;
509
510 return esirisc_flash_probe(bank);
511 }
512
513 static int esirisc_flash_protect_check(struct flash_bank *bank)
514 {
515 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
516 struct target *target = bank->target;
517 uint32_t control;
518
519 if (target->state != TARGET_HALTED)
520 return ERROR_TARGET_NOT_HALTED;
521
522 target_read_u32(target, esirisc_info->cfg + CONTROL, &control);
523
524 /* single protection block (also see: esirisc_flash_probe()) */
525 bank->prot_blocks[0].is_protected = !!(control & CONTROL_WP);
526
527 return ERROR_OK;
528 }
529
530 static int esirisc_flash_info(struct flash_bank *bank, char *buf, int buf_size)
531 {
532 struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
533
534 snprintf(buf, buf_size,
535 "%4s cfg at 0x%" PRIx32 ", clock %" PRId32 ", wait_states %" PRId32,
536 "", /* align with first line */
537 esirisc_info->cfg,
538 esirisc_info->clock,
539 esirisc_info->wait_states);
540
541 return ERROR_OK;
542 }
543
544 COMMAND_HANDLER(handle_esirisc_flash_mass_erase_command)
545 {
546 struct flash_bank *bank;
547 int retval;
548
549 if (CMD_ARGC < 1)
550 return ERROR_COMMAND_SYNTAX_ERROR;
551
552 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
553 if (retval != ERROR_OK)
554 return retval;
555
556 retval = esirisc_flash_mass_erase(bank);
557
558 command_print(CMD_CTX, "mass erase %s",
559 (retval == ERROR_OK) ? "successful" : "failed");
560
561 return retval;
562 }
563
564 COMMAND_HANDLER(handle_esirisc_flash_ref_erase_command)
565 {
566 struct flash_bank *bank;
567 int retval;
568
569 if (CMD_ARGC < 1)
570 return ERROR_COMMAND_SYNTAX_ERROR;
571
572 retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
573 if (retval != ERROR_OK)
574 return retval;
575
576 retval = esirisc_flash_ref_erase(bank);
577
578 command_print(CMD_CTX, "erase reference cell %s",
579 (retval == ERROR_OK) ? "successful" : "failed");
580
581 return retval;
582 }
583
584 static const struct command_registration esirisc_flash_exec_command_handlers[] = {
585 {
586 .name = "mass_erase",
587 .handler = handle_esirisc_flash_mass_erase_command,
588 .mode = COMMAND_EXEC,
589 .help = "erase all pages in data memory",
590 .usage = "bank_id",
591 },
592 {
593 .name = "ref_erase",
594 .handler = handle_esirisc_flash_ref_erase_command,
595 .mode = COMMAND_EXEC,
596 .help = "erase reference cell (uncommon)",
597 .usage = "bank_id",
598 },
599 COMMAND_REGISTRATION_DONE
600 };
601
602 static const struct command_registration esirisc_flash_command_handlers[] = {
603 {
604 .name = "flash",
605 .mode = COMMAND_EXEC,
606 .help = "eSi-TSMC Flash command group",
607 .usage = "",
608 .chain = esirisc_flash_exec_command_handlers,
609 },
610 COMMAND_REGISTRATION_DONE
611 };
612
613 struct flash_driver esirisc_flash = {
614 .name = "esirisc",
615 .usage = "flash bank bank_id 'esirisc' base_address size_bytes 0 0 target "
616 "cfg_address clock_hz wait_states",
617 .flash_bank_command = esirisc_flash_bank_command,
618 .erase = esirisc_flash_erase,
619 .protect = esirisc_flash_protect,
620 .write = esirisc_flash_write,
621 .read = default_flash_read,
622 .probe = esirisc_flash_probe,
623 .auto_probe = esirisc_flash_auto_probe,
624 .erase_check = default_flash_blank_check,
625 .protect_check = esirisc_flash_protect_check,
626 .info = esirisc_flash_info,
627 .free_driver_priv = default_flash_free_driver_priv,
628 };

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)