OpenOCD
artery.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Copyright (C) 2023 by Marc Schink <dev@zapb.de>
5  */
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "imp.h"
12 #include <helper/align.h>
13 #include <helper/binarybuffer.h>
14 #include <helper/time_support.h>
15 #include <helper/bits.h>
16 #include <target/cortex_m.h>
17 
18 #include "artery.h"
19 
20 // Flash timeout values in milliseconds.
21 #define FLASH_MASS_ERASE_TIMEOUT 2400
22 #define FLASH_ERASE_TIMEOUT 500
23 #define FLASH_WRITE_TIMEOUT 5
24 #define HICK_STABLE_TIMEOUT 1000
25 
26 /*
27  * Flash memory register assignment for the following device series:
28  * - AT32F403A / AT32F407
29  * - AT32F413
30  * - AT32F415
31  * - AT32F421
32  * - AT32F423
33  * - AT32F425
34  * - AT32WB415
35  */
37  [ARTERY_FLASH_REG_PSR] = 0x00,
38  [ARTERY_FLASH_REG_UNLOCK] = 0x04,
40  [ARTERY_FLASH_REG_STS] = 0x0c,
41  [ARTERY_FLASH_REG_CTRL] = 0x10,
42  [ARTERY_FLASH_REG_ADDR] = 0x14,
43  [ARTERY_FLASH_REG_USD] = 0x1c,
44  [ARTERY_FLASH_REG_EPPS0] = 0x20,
45  // [ARTERY_FLASH_REG_EPPS1] not available.
46 };
47 
48 // Flash memory register assignment for the AT32F435 / AT32F437 series.
50  [ARTERY_FLASH_REG_PSR] = 0x00,
51  [ARTERY_FLASH_REG_UNLOCK] = 0x04,
53  [ARTERY_FLASH_REG_STS] = 0x0c,
54  [ARTERY_FLASH_REG_CTRL] = 0x10,
55  [ARTERY_FLASH_REG_ADDR] = 0x14,
56  [ARTERY_FLASH_REG_USD] = 0x1c,
57  [ARTERY_FLASH_REG_EPPS0] = 0x20,
58  [ARTERY_FLASH_REG_EPPS1] = 0x2c,
59 };
60 
61 /*
62  * User system data (USD) offsets for the following device series:
63  * - AT32F415
64  * - AT32F421
65  * - AT32F423
66  * - AT32F425
67  * - AT32WB415
68  */
69 static const uint32_t usd_offsets_f4xx_wb415[ARTERY_USD_INDEX_NUM] = {
70  [ARTERY_USD_FAP_INDEX] = 0x00,
71  [ARTERY_USD_SSB_INDEX] = 0x02,
72  [ARTERY_USD_DATA_INDEX] = 0x04,
73  [ARTERY_USD_EPP_INDEX] = 0x08,
74  // [ARTERY_USD_EPP_EXT_INDEX] not available.
76 };
77 
78 // User system data (USD) offsets for the AT32F403A / AT32F407 / AT32F413 series.
80  [ARTERY_USD_FAP_INDEX] = 0x00,
81  [ARTERY_USD_SSB_INDEX] = 0x02,
82  [ARTERY_USD_DATA_INDEX] = 0x04,
83  [ARTERY_USD_EPP_INDEX] = 0x08,
84  // [ARTERY_USD_EPP_EXT_INDEX] not available.
86 };
87 
88 // User system data (USD) offsets for the AT32F435 / AT32F437 series.
89 static const uint32_t usd_offsets_f435_f437[ARTERY_USD_INDEX_NUM] = {
90  [ARTERY_USD_FAP_INDEX] = 0x00,
91  [ARTERY_USD_SSB_INDEX] = 0x02,
92  [ARTERY_USD_DATA_INDEX] = 0x04,
93  [ARTERY_USD_EPP_INDEX] = 0x08,
94  [ARTERY_USD_EPP_EXT_INDEX] = 0x14,
96 };
97 
98 static const struct artery_series_info artery_series[] = {
100  .has_fap_high_level = false,
101  .has_epp_ext = false,
102  .flash_regs_base = 0x40022000,
103  .flash_regs = flash_regs_f4xx_wb415,
104  .crm_base = 0x40021000,
105  .usd_base = 0x1FFFF800,
106  .usd_offsets = usd_offsets_f403a_f407_f413,
107  },
108  [ARTERY_SERIES_F413] = {
109  .has_fap_high_level = false,
110  .has_epp_ext = false,
111  .flash_regs_base = 0x40022000,
112  .flash_regs = flash_regs_f4xx_wb415,
113  .crm_base = 0x40021000,
114  .usd_base = 0x1FFFF800,
115  .usd_offsets = usd_offsets_f403a_f407_f413,
116  },
117  [ARTERY_SERIES_F415] = {
118  .has_fap_high_level = true,
119  .has_epp_ext = false,
120  .flash_regs_base = 0x40022000,
121  .flash_regs = flash_regs_f4xx_wb415,
122  .crm_base = 0x40021000,
123  .usd_base = 0x1FFFF800,
124  .usd_offsets = usd_offsets_f4xx_wb415,
125  },
126  [ARTERY_SERIES_F421] = {
127  .has_fap_high_level = true,
128  .has_epp_ext = false,
129  .flash_regs_base = 0x40022000,
130  .flash_regs = flash_regs_f4xx_wb415,
131  .crm_base = 0x40021000,
132  .usd_base = 0x1FFFF800,
133  .usd_offsets = usd_offsets_f4xx_wb415,
134  },
135  [ARTERY_SERIES_F423] = {
136  .has_fap_high_level = true,
137  .has_epp_ext = false,
138  .flash_regs_base = 0x40023C00,
139  .flash_regs = flash_regs_f4xx_wb415,
140  .crm_base = 0x40023800,
141  .usd_base = 0x1FFFF800,
142  .usd_offsets = usd_offsets_f4xx_wb415,
143  },
144  [ARTERY_SERIES_F425] = {
145  .has_fap_high_level = true,
146  .has_epp_ext = false,
147  .flash_regs_base = 0x40022000,
148  .flash_regs = flash_regs_f4xx_wb415,
149  .crm_base = 0x40021000,
150  .usd_base = 0x1FFFF800,
151  .usd_offsets = usd_offsets_f4xx_wb415,
152  },
154  .has_fap_high_level = false,
155  .has_epp_ext = true,
156  .flash_regs_base = 0x40023C00,
157  .flash_regs = flash_regs_f435_f437,
158  .crm_base = 0x40023800,
159  .usd_base = 0x1FFFC000,
160  .usd_offsets = usd_offsets_f435_f437,
161  },
162  [ARTERY_SERIES_WB415] = {
163  .has_fap_high_level = true,
164  .has_epp_ext = false,
165  .flash_regs_base = 0x40022000,
166  .flash_regs = flash_regs_f4xx_wb415,
167  .crm_base = 0x40021000,
168  .usd_base = 0x1FFFF800,
169  .usd_offsets = usd_offsets_f4xx_wb415,
170  },
171 };
172 
173 static const struct artery_part_info artery_parts[] = {
174  {
175  .pid = 0x70050240,
176  .name = "AT32F403AVCT7",
177  .series = ARTERY_SERIES_F403A_F407,
178  .flash_size = 256,
179  .page_size = 2048,
180  .usd_size = 48,
181  .usd_data_size = 8,
182  },
183  {
184  .pid = 0x70050241,
185  .name = "AT32F403ARCT7",
186  .series = ARTERY_SERIES_F403A_F407,
187  .flash_size = 256,
188  .page_size = 2048,
189  .usd_size = 48,
190  .usd_data_size = 8,
191  },
192  {
193  .pid = 0x70050242,
194  .name = "AT32F403ACCT7",
195  .series = ARTERY_SERIES_F403A_F407,
196  .flash_size = 256,
197  .page_size = 2048,
198  .usd_size = 48,
199  .usd_data_size = 8,
200  },
201  {
202  .pid = 0x70050243,
203  .name = "AT32F403ACCU7",
204  .series = ARTERY_SERIES_F403A_F407,
205  .flash_size = 256,
206  .page_size = 2048,
207  .usd_size = 48,
208  .usd_data_size = 8,
209  },
210  {
211  .pid = 0x70050249,
212  .name = "AT32F407VCT7",
213  .series = ARTERY_SERIES_F403A_F407,
214  .flash_size = 256,
215  .page_size = 2048,
216  .usd_size = 48,
217  .usd_data_size = 8,
218  },
219  {
220  .pid = 0x7005024a,
221  .name = "AT32F407RCT7",
222  .series = ARTERY_SERIES_F403A_F407,
223  .flash_size = 256,
224  .page_size = 2048,
225  .usd_size = 48,
226  .usd_data_size = 8,
227  },
228  {
229  .pid = 0x700502cd,
230  .name = "AT32F403AVET7",
231  .series = ARTERY_SERIES_F403A_F407,
232  .flash_size = 512,
233  .page_size = 2048,
234  .usd_size = 48,
235  .usd_data_size = 8,
236  },
237  {
238  .pid = 0x700502ce,
239  .name = "AT32F403ARET7",
240  .series = ARTERY_SERIES_F403A_F407,
241  .flash_size = 512,
242  .page_size = 2048,
243  .usd_size = 48,
244  .usd_data_size = 8,
245  },
246  {
247  .pid = 0x700502cf,
248  .name = "AT32F403ACET7",
249  .series = ARTERY_SERIES_F403A_F407,
250  .flash_size = 512,
251  .page_size = 2048,
252  .usd_size = 48,
253  .usd_data_size = 8,
254  },
255  {
256  .pid = 0x700502d0,
257  .name = "AT32F403ACEU7",
258  .series = ARTERY_SERIES_F403A_F407,
259  .flash_size = 512,
260  .page_size = 2048,
261  .usd_size = 48,
262  .usd_data_size = 8,
263  },
264  {
265  .pid = 0x700502d1,
266  .name = "AT32F407VET7",
267  .series = ARTERY_SERIES_F403A_F407,
268  .flash_size = 512,
269  .page_size = 2048,
270  .usd_size = 48,
271  .usd_data_size = 8,
272  },
273  {
274  .pid = 0x700502d2,
275  .name = "AT32F407RET7",
276  .series = ARTERY_SERIES_F403A_F407,
277  .flash_size = 512,
278  .page_size = 2048,
279  .usd_size = 48,
280  .usd_data_size = 8,
281  },
282  {
283  .pid = 0x70050254,
284  .name = "AT32F407AVCT7",
285  .series = ARTERY_SERIES_F403A_F407,
286  .flash_size = 256,
287  .page_size = 2048,
288  .usd_size = 48,
289  .usd_data_size = 8,
290  },
291  {
292  .pid = 0x70030240,
293  .name = "AT32F413RCT7",
294  .series = ARTERY_SERIES_F413,
295  .flash_size = 256,
296  .page_size = 2048,
297  .usd_size = 48,
298  .usd_data_size = 8,
299  },
300  {
301  .pid = 0x700301c1,
302  .name = "AT32F413RBT7",
303  .series = ARTERY_SERIES_F413,
304  .flash_size = 128,
305  .page_size = 1024,
306  .usd_size = 48,
307  .usd_data_size = 8,
308  },
309  {
310  .pid = 0x70030242,
311  .name = "AT32F413CCT7",
312  .series = ARTERY_SERIES_F413,
313  .flash_size = 256,
314  .page_size = 2048,
315  .usd_size = 48,
316  .usd_data_size = 8,
317  },
318  {
319  .pid = 0x700301c3,
320  .name = "AT32F413CBT7",
321  .series = ARTERY_SERIES_F413,
322  .flash_size = 128,
323  .page_size = 1024,
324  .usd_size = 48,
325  .usd_data_size = 8,
326  },
327  {
328  .pid = 0x70030244,
329  .name = "AT32F413KCU7-4",
330  .series = ARTERY_SERIES_F413,
331  .flash_size = 256,
332  .page_size = 2048,
333  .usd_size = 48,
334  .usd_data_size = 8,
335  },
336  {
337  .pid = 0x700301c5,
338  .name = "AT32F413KBU7-4",
339  .series = ARTERY_SERIES_F413,
340  .flash_size = 128,
341  .page_size = 1024,
342  .usd_size = 48,
343  .usd_data_size = 8,
344  },
345  {
346  .pid = 0x70030106,
347  .name = "AT32F413C8T7",
348  .series = ARTERY_SERIES_F413,
349  .flash_size = 64,
350  .page_size = 1024,
351  .usd_size = 48,
352  .usd_data_size = 8,
353  },
354  {
355  .pid = 0x70030247,
356  .name = "AT32F413CCU7",
357  .series = ARTERY_SERIES_F413,
358  .flash_size = 256,
359  .page_size = 2048,
360  .usd_size = 48,
361  .usd_data_size = 8,
362  },
363  {
364  .pid = 0x700301ca,
365  .name = "AT32F413CBU7",
366  .series = ARTERY_SERIES_F413,
367  .flash_size = 128,
368  .page_size = 1024,
369  .usd_size = 48,
370  .usd_data_size = 8,
371  },
372  {
373  .pid = 0x70030240,
374  .name = "AT32F415RCT7",
375  .series = ARTERY_SERIES_F415,
376  .flash_size = 256,
377  .page_size = 2048,
378  .usd_size = 1024,
379  .usd_data_size = 506,
380  },
381  {
382  .pid = 0x70030241,
383  .name = "AT32F415CCT7",
384  .series = ARTERY_SERIES_F415,
385  .flash_size = 256,
386  .page_size = 2048,
387  .usd_size = 1024,
388  .usd_data_size = 506,
389  },
390  {
391  .pid = 0x70030242,
392  .name = "AT32F415KCU7-4",
393  .series = ARTERY_SERIES_F415,
394  .flash_size = 256,
395  .page_size = 2048,
396  .usd_size = 1024,
397  .usd_data_size = 506,
398  },
399  {
400  .pid = 0x70030243,
401  .name = "AT32F415RCT7-7",
402  .series = ARTERY_SERIES_F415,
403  .flash_size = 256,
404  .page_size = 2048,
405  .usd_size = 1024,
406  .usd_data_size = 506,
407  },
408  {
409  .pid = 0x700301c4,
410  .name = "AT32F415RBT7",
411  .series = ARTERY_SERIES_F415,
412  .flash_size = 128,
413  .page_size = 1024,
414  .usd_size = 1024,
415  .usd_data_size = 506,
416  },
417  {
418  .pid = 0x700301c5,
419  .name = "AT32F415CBT7",
420  .series = ARTERY_SERIES_F415,
421  .flash_size = 128,
422  .page_size = 1024,
423  .usd_size = 1024,
424  .usd_data_size = 506,
425  },
426  {
427  .pid = 0x700301c6,
428  .name = "AT32F415KBU7-4",
429  .series = ARTERY_SERIES_F415,
430  .flash_size = 128,
431  .page_size = 1024,
432  .usd_size = 1024,
433  .usd_data_size = 506,
434  },
435  {
436  .pid = 0x700301c7,
437  .name = "AT32F415RBT7-7",
438  .series = ARTERY_SERIES_F415,
439  .flash_size = 128,
440  .page_size = 1024,
441  .usd_size = 1024,
442  .usd_data_size = 506,
443  },
444  {
445  .pid = 0x70030108,
446  .name = "AT32F415R8T7",
447  .series = ARTERY_SERIES_F415,
448  .flash_size = 64,
449  .page_size = 1024,
450  .usd_size = 1024,
451  .usd_data_size = 506,
452  },
453  {
454  .pid = 0x70030109,
455  .name = "AT32F415C8T7",
456  .series = ARTERY_SERIES_F415,
457  .flash_size = 64,
458  .page_size = 1024,
459  .usd_size = 1024,
460  .usd_data_size = 506,
461  },
462  {
463  .pid = 0x7003010a,
464  .name = "AT32F415K8U7-4",
465  .series = ARTERY_SERIES_F415,
466  .flash_size = 64,
467  .page_size = 1024,
468  .usd_size = 1024,
469  .usd_data_size = 506,
470  },
471  {
472  .pid = 0x7003024c,
473  .name = "AT32F415CCU7",
474  .series = ARTERY_SERIES_F415,
475  .flash_size = 256,
476  .page_size = 2048,
477  .usd_size = 1024,
478  .usd_data_size = 506,
479  },
480  {
481  .pid = 0x700301cd,
482  .name = "AT32F415CBU7",
483  .series = ARTERY_SERIES_F415,
484  .flash_size = 128,
485  .page_size = 1024,
486  .usd_size = 1024,
487  .usd_data_size = 506,
488  },
489  {
490  .pid = 0x50020100,
491  .name = "AT32F421C8T7",
492  .series = ARTERY_SERIES_F421,
493  .flash_size = 64,
494  .page_size = 1024,
495  .usd_size = 512,
496  .usd_data_size = 250,
497  },
498  {
499  .pid = 0x50020101,
500  .name = "AT32F421K8T7",
501  .series = ARTERY_SERIES_F421,
502  .flash_size = 64,
503  .page_size = 1024,
504  .usd_size = 512,
505  .usd_data_size = 250,
506  },
507  {
508  .pid = 0x50020102,
509  .name = "AT32F421K8U7",
510  .series = ARTERY_SERIES_F421,
511  .flash_size = 64,
512  .page_size = 1024,
513  .usd_size = 512,
514  .usd_data_size = 250,
515  },
516  {
517  .pid = 0x50020103,
518  .name = "AT32F421K8U7-4",
519  .series = ARTERY_SERIES_F421,
520  .flash_size = 64,
521  .page_size = 1024,
522  .usd_size = 512,
523  .usd_data_size = 250,
524  },
525  {
526  .pid = 0x50020104,
527  .name = "AT32F421F8U7",
528  .series = ARTERY_SERIES_F421,
529  .flash_size = 64,
530  .page_size = 1024,
531  .usd_size = 512,
532  .usd_data_size = 250,
533  },
534  {
535  .pid = 0x50020105,
536  .name = "AT32F421F8P7",
537  .series = ARTERY_SERIES_F421,
538  .flash_size = 64,
539  .page_size = 1024,
540  .usd_size = 512,
541  .usd_data_size = 250,
542  },
543  {
544  .pid = 0x50020086,
545  .name = "AT32F421C6T7",
546  .series = ARTERY_SERIES_F421,
547  .flash_size = 32,
548  .page_size = 1024,
549  .usd_size = 512,
550  .usd_data_size = 250,
551  },
552  {
553  .pid = 0x50020087,
554  .name = "AT32F421K6T7",
555  .series = ARTERY_SERIES_F421,
556  .flash_size = 32,
557  .page_size = 1024,
558  .usd_size = 512,
559  .usd_data_size = 250,
560  },
561  {
562  .pid = 0x50020088,
563  .name = "AT32F421K6U7",
564  .series = ARTERY_SERIES_F421,
565  .flash_size = 32,
566  .page_size = 1024,
567  .usd_size = 512,
568  .usd_data_size = 250,
569  },
570  {
571  .pid = 0x50020089,
572  .name = "AT32F421K6U7-4",
573  .series = ARTERY_SERIES_F421,
574  .flash_size = 32,
575  .page_size = 1024,
576  .usd_size = 512,
577  .usd_data_size = 250,
578  },
579  {
580  .pid = 0x5002008a,
581  .name = "AT32F421F6U7",
582  .series = ARTERY_SERIES_F421,
583  .flash_size = 32,
584  .page_size = 1024,
585  .usd_size = 512,
586  .usd_data_size = 250,
587  },
588  {
589  .pid = 0x5002008b,
590  .name = "AT32F421F6P7",
591  .series = ARTERY_SERIES_F421,
592  .flash_size = 32,
593  .page_size = 1024,
594  .usd_size = 512,
595  .usd_data_size = 250,
596  },
597  {
598  .pid = 0x5001000c,
599  .name = "AT32F421C4T7",
600  .series = ARTERY_SERIES_F421,
601  .flash_size = 16,
602  .page_size = 1024,
603  .usd_size = 512,
604  .usd_data_size = 250,
605  },
606  {
607  .pid = 0x5001000d,
608  .name = "AT32F421K4T7",
609  .series = ARTERY_SERIES_F421,
610  .flash_size = 16,
611  .page_size = 1024,
612  .usd_size = 512,
613  .usd_data_size = 250,
614  },
615  {
616  .pid = 0x5001000e,
617  .name = "AT32F421K4U7",
618  .series = ARTERY_SERIES_F421,
619  .flash_size = 16,
620  .page_size = 1024,
621  .usd_size = 512,
622  .usd_data_size = 250,
623  },
624  {
625  .pid = 0x5001000f,
626  .name = "AT32F421K4U7-4",
627  .series = ARTERY_SERIES_F421,
628  .flash_size = 16,
629  .page_size = 1024,
630  .usd_size = 512,
631  .usd_data_size = 250,
632  },
633  {
634  .pid = 0x50010010,
635  .name = "AT32F421F4U7",
636  .series = ARTERY_SERIES_F421,
637  .flash_size = 16,
638  .page_size = 1024,
639  .usd_size = 512,
640  .usd_data_size = 250,
641  },
642  {
643  .pid = 0x50010011,
644  .name = "AT32F421F4P7",
645  .series = ARTERY_SERIES_F421,
646  .flash_size = 16,
647  .page_size = 1024,
648  .usd_size = 512,
649  .usd_data_size = 250,
650  },
651  {
652  .pid = 0x50020112,
653  .name = "AT32F421G8U7",
654  .series = ARTERY_SERIES_F421,
655  .flash_size = 64,
656  .page_size = 1024,
657  .usd_size = 512,
658  .usd_data_size = 250,
659  },
660  {
661  .pid = 0x50020093,
662  .name = "AT32F421G6U7",
663  .series = ARTERY_SERIES_F421,
664  .flash_size = 32,
665  .page_size = 1024,
666  .usd_size = 512,
667  .usd_data_size = 250,
668  },
669  {
670  .pid = 0x50010014,
671  .name = "AT32F421G4U7",
672  .series = ARTERY_SERIES_F421,
673  .flash_size = 16,
674  .page_size = 1024,
675  .usd_size = 512,
676  .usd_data_size = 250,
677  },
678  {
679  .pid = 0x700a3240,
680  .name = "AT32F423VCT7",
681  .series = ARTERY_SERIES_F423,
682  .flash_size = 256,
683  .page_size = 2048,
684  .usd_size = 512,
685  .usd_data_size = 250,
686  },
687  {
688  .pid = 0x700a21c1,
689  .name = "AT32F423VBT7",
690  .series = ARTERY_SERIES_F423,
691  .flash_size = 128,
692  .page_size = 1024,
693  .usd_size = 512,
694  .usd_data_size = 250,
695  },
696  {
697  .pid = 0x70032102,
698  .name = "AT32F423V8T7",
699  .series = ARTERY_SERIES_F423,
700  .flash_size = 64,
701  .page_size = 1024,
702  .usd_size = 512,
703  .usd_data_size = 250,
704  },
705  {
706  .pid = 0x700a3243,
707  .name = "AT32F423RCT7",
708  .series = ARTERY_SERIES_F423,
709  .flash_size = 256,
710  .page_size = 2048,
711  .usd_size = 512,
712  .usd_data_size = 250,
713  },
714  {
715  .pid = 0x700a21c4,
716  .name = "AT32F423RBT7",
717  .series = ARTERY_SERIES_F423,
718  .flash_size = 128,
719  .page_size = 1024,
720  .usd_size = 512,
721  .usd_data_size = 250,
722  },
723  {
724  .pid = 0x70032105,
725  .name = "AT32F423R8T7",
726  .series = ARTERY_SERIES_F423,
727  .flash_size = 64,
728  .page_size = 1024,
729  .usd_size = 512,
730  .usd_data_size = 250,
731  },
732  {
733  .pid = 0x700a3246,
734  .name = "AT32F423RCT7-7",
735  .series = ARTERY_SERIES_F423,
736  .flash_size = 256,
737  .page_size = 2048,
738  .usd_size = 512,
739  .usd_data_size = 250,
740  },
741  {
742  .pid = 0x700a21c7,
743  .name = "AT32F423RBT7-7",
744  .series = ARTERY_SERIES_F423,
745  .flash_size = 128,
746  .page_size = 1024,
747  .usd_size = 512,
748  .usd_data_size = 250,
749  },
750  {
751  .pid = 0x70032108,
752  .name = "AT32F423R8T7-7",
753  .series = ARTERY_SERIES_F423,
754  .flash_size = 64,
755  .page_size = 1024,
756  .usd_size = 512,
757  .usd_data_size = 250,
758  },
759  {
760  .pid = 0x700a3249,
761  .name = "AT32F423CCT7",
762  .series = ARTERY_SERIES_F423,
763  .flash_size = 256,
764  .page_size = 2048,
765  .usd_size = 512,
766  .usd_data_size = 250,
767  },
768  {
769  .pid = 0x700a21ca,
770  .name = "AT32F423CBT7",
771  .series = ARTERY_SERIES_F423,
772  .flash_size = 128,
773  .page_size = 1024,
774  .usd_size = 512,
775  .usd_data_size = 250,
776  },
777  {
778  .pid = 0x7003210b,
779  .name = "AT32F423C8T7",
780  .series = ARTERY_SERIES_F423,
781  .flash_size = 64,
782  .page_size = 1024,
783  .usd_size = 512,
784  .usd_data_size = 250,
785  },
786  {
787  .pid = 0x700a324c,
788  .name = "AT32F423CCU7",
789  .series = ARTERY_SERIES_F423,
790  .flash_size = 256,
791  .page_size = 2048,
792  .usd_size = 512,
793  .usd_data_size = 250,
794  },
795  {
796  .pid = 0x700a21cd,
797  .name = "AT32F423CBU7",
798  .series = ARTERY_SERIES_F423,
799  .flash_size = 128,
800  .page_size = 1024,
801  .usd_size = 512,
802  .usd_data_size = 250,
803  },
804  {
805  .pid = 0x7003210e,
806  .name = "AT32F423C8U7",
807  .series = ARTERY_SERIES_F423,
808  .flash_size = 64,
809  .page_size = 1024,
810  .usd_size = 512,
811  .usd_data_size = 250,
812  },
813  {
814  .pid = 0x700a3250,
815  .name = "AT32F423TCU7",
816  .series = ARTERY_SERIES_F423,
817  .flash_size = 256,
818  .page_size = 2048,
819  .usd_size = 512,
820  .usd_data_size = 250,
821  },
822  {
823  .pid = 0x700a21d1,
824  .name = "AT32F423TBU7",
825  .series = ARTERY_SERIES_F423,
826  .flash_size = 128,
827  .page_size = 1024,
828  .usd_size = 512,
829  .usd_data_size = 250,
830  },
831  {
832  .pid = 0x70032112,
833  .name = "AT32F423T8U7",
834  .series = ARTERY_SERIES_F423,
835  .flash_size = 64,
836  .page_size = 1024,
837  .usd_size = 512,
838  .usd_data_size = 250,
839  },
840  {
841  .pid = 0x700a3253,
842  .name = "AT32F423KCU7-4",
843  .series = ARTERY_SERIES_F423,
844  .flash_size = 256,
845  .page_size = 2048,
846  .usd_size = 512,
847  .usd_data_size = 250,
848  },
849  {
850  .pid = 0x700a21d4,
851  .name = "AT32F423KBU7-4",
852  .series = ARTERY_SERIES_F423,
853  .flash_size = 128,
854  .page_size = 1024,
855  .usd_size = 512,
856  .usd_data_size = 250,
857  },
858  {
859  .pid = 0x70032115,
860  .name = "AT32F423K8U7-4",
861  .series = ARTERY_SERIES_F423,
862  .flash_size = 64,
863  .page_size = 1024,
864  .usd_size = 512,
865  .usd_data_size = 250,
866  },
867  {
868  .pid = 0x50092100,
869  .name = "AT32F425R8T7",
870  .series = ARTERY_SERIES_F425,
871  .flash_size = 64,
872  .page_size = 1024,
873  .usd_size = 512,
874  .usd_data_size = 250,
875  },
876  {
877  .pid = 0x50092081,
878  .name = "AT32F425R6T7",
879  .series = ARTERY_SERIES_F425,
880  .flash_size = 32,
881  .page_size = 1024,
882  .usd_size = 512,
883  .usd_data_size = 250,
884  },
885  {
886  .pid = 0x50092103,
887  .name = "AT32F425R8T7-7",
888  .series = ARTERY_SERIES_F425,
889  .flash_size = 64,
890  .page_size = 1024,
891  .usd_size = 512,
892  .usd_data_size = 250,
893  },
894  {
895  .pid = 0x50092084,
896  .name = "AT32F425R6T7-7",
897  .series = ARTERY_SERIES_F425,
898  .flash_size = 32,
899  .page_size = 1024,
900  .usd_size = 512,
901  .usd_data_size = 250,
902  },
903  {
904  .pid = 0x50092106,
905  .name = "AT32F425C8T7",
906  .series = ARTERY_SERIES_F425,
907  .flash_size = 64,
908  .page_size = 1024,
909  .usd_size = 512,
910  .usd_data_size = 250,
911  },
912  {
913  .pid = 0x50092087,
914  .name = "AT32F425C6T7",
915  .series = ARTERY_SERIES_F425,
916  .flash_size = 32,
917  .page_size = 1024,
918  .usd_size = 512,
919  .usd_data_size = 250,
920  },
921  {
922  .pid = 0x50092109,
923  .name = "AT32F425C8U7",
924  .series = ARTERY_SERIES_F425,
925  .flash_size = 64,
926  .page_size = 1024,
927  .usd_size = 512,
928  .usd_data_size = 250,
929  },
930  {
931  .pid = 0x5009208a,
932  .name = "AT32F425C6U7",
933  .series = ARTERY_SERIES_F425,
934  .flash_size = 32,
935  .page_size = 1024,
936  .usd_size = 512,
937  .usd_data_size = 250,
938  },
939  {
940  .pid = 0x5009210c,
941  .name = "AT32F425K8T7",
942  .series = ARTERY_SERIES_F425,
943  .flash_size = 64,
944  .page_size = 1024,
945  .usd_size = 512,
946  .usd_data_size = 250,
947  },
948  {
949  .pid = 0x5009208d,
950  .name = "AT32F425K6T7",
951  .series = ARTERY_SERIES_F425,
952  .flash_size = 32,
953  .page_size = 1024,
954  .usd_size = 512,
955  .usd_data_size = 250,
956  },
957  {
958  .pid = 0x5009210f,
959  .name = "AT32F425K8U7-4",
960  .series = ARTERY_SERIES_F425,
961  .flash_size = 64,
962  .page_size = 1024,
963  .usd_size = 512,
964  .usd_data_size = 250,
965  },
966  {
967  .pid = 0x50092090,
968  .name = "AT32F425K6U7-4",
969  .series = ARTERY_SERIES_F425,
970  .flash_size = 32,
971  .page_size = 1024,
972  .usd_size = 512,
973  .usd_data_size = 250,
974  },
975  {
976  .pid = 0x50092112,
977  .name = "AT32F425F8P7",
978  .series = ARTERY_SERIES_F425,
979  .flash_size = 64,
980  .page_size = 1024,
981  .usd_size = 512,
982  .usd_data_size = 250,
983  },
984  {
985  .pid = 0x50092093,
986  .name = "AT32F425F6P7",
987  .series = ARTERY_SERIES_F425,
988  .flash_size = 32,
989  .page_size = 1024,
990  .usd_size = 512,
991  .usd_data_size = 250,
992  },
993  {
994  .pid = 0x70084598,
995  .name = "AT32F435ZDT7",
996  .series = ARTERY_SERIES_F435_F437,
997  .flash_size = 448,
998  .page_size = 4096,
999  .usd_size = 4096,
1000  .usd_data_size = 2012,
1001  },
1002  {
1003  .pid = 0x70083242,
1004  .name = "AT32F435ZCT7",
1005  .series = ARTERY_SERIES_F435_F437,
1006  .flash_size = 256,
1007  .page_size = 2048,
1008  .usd_size = 512,
1009  .usd_data_size = 220,
1010  },
1011  {
1012  .pid = 0x70084599,
1013  .name = "AT32F435VDT7",
1014  .series = ARTERY_SERIES_F435_F437,
1015  .flash_size = 448,
1016  .page_size = 4096,
1017  .usd_size = 4096,
1018  .usd_data_size = 2012,
1019  },
1020  {
1021  .pid = 0x70083245,
1022  .name = "AT32F435VCT7",
1023  .series = ARTERY_SERIES_F435_F437,
1024  .flash_size = 256,
1025  .page_size = 2048,
1026  .usd_size = 512,
1027  .usd_data_size = 220,
1028  },
1029  {
1030  .pid = 0x7008459a,
1031  .name = "AT32F435RDT7",
1032  .series = ARTERY_SERIES_F435_F437,
1033  .flash_size = 448,
1034  .page_size = 4096,
1035  .usd_size = 4096,
1036  .usd_data_size = 2012,
1037  },
1038  {
1039  .pid = 0x70083248,
1040  .name = "AT32F435RCT7",
1041  .series = ARTERY_SERIES_F435_F437,
1042  .flash_size = 256,
1043  .page_size = 2048,
1044  .usd_size = 512,
1045  .usd_data_size = 220,
1046  },
1047  {
1048  .pid = 0x7008459b,
1049  .name = "AT32F435CDT7",
1050  .series = ARTERY_SERIES_F435_F437,
1051  .flash_size = 448,
1052  .page_size = 4096,
1053  .usd_size = 4096,
1054  .usd_data_size = 2012,
1055  },
1056  {
1057  .pid = 0x7008324b,
1058  .name = "AT32F435CCT7",
1059  .series = ARTERY_SERIES_F435_F437,
1060  .flash_size = 256,
1061  .page_size = 2048,
1062  .usd_size = 512,
1063  .usd_data_size = 220,
1064  },
1065  {
1066  .pid = 0x7008459c,
1067  .name = "AT32F435CDU7",
1068  .series = ARTERY_SERIES_F435_F437,
1069  .flash_size = 448,
1070  .page_size = 4096,
1071  .usd_size = 4096,
1072  .usd_data_size = 2012,
1073  },
1074  {
1075  .pid = 0x7008324e,
1076  .name = "AT32F435CCU7",
1077  .series = ARTERY_SERIES_F435_F437,
1078  .flash_size = 256,
1079  .page_size = 2048,
1080  .usd_size = 512,
1081  .usd_data_size = 220,
1082  },
1083  {
1084  .pid = 0x7008459d,
1085  .name = "AT32F437ZDT7",
1086  .series = ARTERY_SERIES_F435_F437,
1087  .flash_size = 448,
1088  .page_size = 4096,
1089  .usd_size = 4096,
1090  .usd_data_size = 2012,
1091  },
1092  {
1093  .pid = 0x70083251,
1094  .name = "AT32F437ZCT7",
1095  .series = ARTERY_SERIES_F435_F437,
1096  .flash_size = 256,
1097  .page_size = 2048,
1098  .usd_size = 512,
1099  .usd_data_size = 220,
1100  },
1101  {
1102  .pid = 0x7008459e,
1103  .name = "AT32F437VDT7",
1104  .series = ARTERY_SERIES_F435_F437,
1105  .flash_size = 448,
1106  .page_size = 4096,
1107  .usd_size = 4096,
1108  .usd_data_size = 2012,
1109  },
1110  {
1111  .pid = 0x70083254,
1112  .name = "AT32F437VCT7",
1113  .series = ARTERY_SERIES_F435_F437,
1114  .flash_size = 256,
1115  .page_size = 2048,
1116  .usd_size = 512,
1117  .usd_data_size = 220,
1118  },
1119  {
1120  .pid = 0x7008459f,
1121  .name = "AT32F437RDT7",
1122  .series = ARTERY_SERIES_F435_F437,
1123  .flash_size = 448,
1124  .page_size = 4096,
1125  .usd_size = 4096,
1126  .usd_data_size = 2012,
1127  },
1128  {
1129  .pid = 0x70083257,
1130  .name = "AT32F437RCT7",
1131  .series = ARTERY_SERIES_F435_F437,
1132  .flash_size = 256,
1133  .page_size = 2048,
1134  .usd_size = 512,
1135  .usd_data_size = 220,
1136  },
1137  {
1138  .pid = 0x70030250,
1139  .name = "AT32WB415CCU7-7",
1140  .series = ARTERY_SERIES_WB415,
1141  .flash_size = 256,
1142  .page_size = 2048,
1143  .usd_size = 1024,
1144  .usd_data_size = 506,
1145  },
1146 };
1147 
1148 /* flash bank artery <base> <size> 0 0 <target#> */
1149 FLASH_BANK_COMMAND_HANDLER(artery_flash_bank_command)
1150 {
1151  if (CMD_ARGC < 6)
1153 
1154  struct artery_flash_bank *artery_info = calloc(1,
1155  sizeof(struct artery_flash_bank));
1156 
1157  if (!artery_info)
1158  return ERROR_FAIL;
1159 
1160  bank->driver_priv = artery_info;
1161  artery_info->probed = false;
1162 
1163  return ERROR_OK;
1164 }
1165 
1167  enum artery_flash_reg_index reg, uint32_t *value)
1168 {
1169  const struct artery_flash_bank *artery_info = bank->driver_priv;
1170  const struct artery_part_info *part_info = artery_info->part_info;
1171  const struct artery_series_info *series_info = &artery_series[part_info->series];
1172  uint32_t reg_addr = series_info->flash_regs_base + series_info->flash_regs[reg];
1173 
1174  return target_read_u32(bank->target, reg_addr, value);
1175 }
1176 
1178  enum artery_flash_reg_index reg, uint32_t value)
1179 {
1180  const struct artery_flash_bank *artery_info = bank->driver_priv;
1181  const struct artery_part_info *part_info = artery_info->part_info;
1182  const struct artery_series_info *series_info = &artery_series[part_info->series];
1183  uint32_t reg_addr = series_info->flash_regs_base + series_info->flash_regs[reg];
1184 
1185  return target_write_u32(bank->target, reg_addr, value);
1186 }
1187 
1188 static int artery_wait_flash_busy(struct flash_bank *bank, unsigned int timeout)
1189 {
1190  const int64_t start_time = timeval_ms();
1191 
1192  while (true) {
1193  uint32_t status;
1195  &status);
1196 
1197  if (retval != ERROR_OK)
1198  return retval;
1199 
1200  if (!(status & FLASH_STS_OBF))
1201  break;
1202 
1203  if ((timeval_ms() - start_time) > timeout) {
1204  LOG_ERROR("Timed out waiting for flash");
1205  return ERROR_FAIL;
1206  }
1207 
1208  keep_alive();
1209  }
1210 
1211  return ERROR_OK;
1212 }
1213 
1215 {
1216  const struct artery_flash_bank *artery_info = bank->driver_priv;
1217  const struct artery_part_info *part_info = artery_info->part_info;
1218  const struct artery_series_info *series_info = &artery_series[part_info->series];
1219  uint32_t crm_base = series_info->crm_base;
1220  struct target *target = bank->target;
1221 
1222  uint32_t crm_ctrl;
1223  int ret = target_read_u32(target, crm_base + CRM_REG_CTRL, &crm_ctrl);
1224 
1225  if (ret != ERROR_OK)
1226  return ret;
1227 
1228  // High speed internal clock (HICK) is already enabled and ready.
1229  if (crm_ctrl & CRM_CTRL_HICKSTBL)
1230  return ERROR_OK;
1231 
1232  crm_ctrl |= CRM_CTRL_HICKEN;
1233  ret = target_write_u32(target, crm_base + CRM_REG_CTRL, crm_ctrl);
1234 
1235  if (ret != ERROR_OK)
1236  return ret;
1237 
1238  const int64_t start_time = timeval_ms();
1239 
1240  while (true) {
1241  ret = target_read_u32(target, crm_base + CRM_REG_CTRL, &crm_ctrl);
1242 
1243  if (ret != ERROR_OK)
1244  return ret;
1245 
1246  if (crm_ctrl & CRM_CTRL_HICKSTBL)
1247  break;
1248 
1249  if ((timeval_ms() - start_time) > HICK_STABLE_TIMEOUT) {
1250  LOG_ERROR("Timed out waiting for flash");
1251  return ERROR_FAIL;
1252  }
1253 
1254  keep_alive();
1255  }
1256 
1257  return ERROR_OK;
1258 }
1259 
1260 static int artery_usd_unlock(struct flash_bank *bank)
1261 {
1262  uint32_t ctrl;
1264 
1265  if (retval != ERROR_OK)
1266  return retval;
1267 
1268  if (ctrl & FLASH_CTRL_USDULKS)
1269  return ERROR_OK;
1270 
1272 
1273  if (retval != ERROR_OK)
1274  return retval;
1275 
1277 
1278  if (retval != ERROR_OK)
1279  return retval;
1280 
1282 
1283  if (retval != ERROR_OK)
1284  return retval;
1285 
1286  if (!(ctrl & FLASH_CTRL_USDULKS)) {
1287  LOG_ERROR("Failed to unlock user system data");
1288  return ERROR_FAIL;
1289  }
1290 
1291  return ERROR_OK;
1292 }
1293 
1294 static int artery_usd_lock(struct flash_bank *bank)
1295 {
1296  uint32_t ctrl;
1297 
1299 
1300  if (retval != ERROR_OK)
1301  return retval;
1302 
1303  if (!(ctrl & FLASH_CTRL_USDULKS))
1304  return ERROR_OK;
1305 
1308 
1309  if (retval != ERROR_OK)
1310  return retval;
1311 
1313 
1314  if (retval != ERROR_OK)
1315  return retval;
1316 
1317  if (ctrl & FLASH_CTRL_USDULKS) {
1318  LOG_ERROR("Failed to lock user system data");
1319  return ERROR_FAIL;
1320  }
1321 
1322  return ERROR_OK;
1323 }
1324 
1325 // Initialize the device for flash memory operations.
1326 static int artery_init_flash(struct flash_bank *bank)
1327 {
1328  /*
1329  * The internal high speed clock (HICK) must be enabled before any flash
1330  * operation is performed.
1331  */
1332  int retval = artery_enable_hiclk(bank);
1333 
1334  if (retval != ERROR_OK) {
1335  LOG_ERROR("Failed to enable HICLK");
1336  return retval;
1337  }
1338 
1339  uint32_t ctrl;
1341 
1342  if (retval != ERROR_OK)
1343  return retval;
1344 
1345  if (!(ctrl & FLASH_CTRL_OPLK))
1346  return ERROR_OK;
1347 
1349 
1350  if (retval != ERROR_OK)
1351  return retval;
1352 
1354 
1355  if (retval != ERROR_OK)
1356  return retval;
1357 
1359 
1360  if (retval != ERROR_OK)
1361  return retval;
1362 
1363  if (ctrl & FLASH_CTRL_OPLK) {
1364  LOG_ERROR("Failed to initialize flash memory");
1365  return ERROR_FAIL;
1366  }
1367 
1368  return artery_usd_unlock(bank);
1369 }
1370 
1371 // Deinitialize the flash memory controller.
1373 {
1374  int retval = artery_usd_lock(bank);
1375 
1376  if (retval != ERROR_OK)
1377  return retval;
1378 
1379  uint32_t ctrl;
1381 
1382  if (retval != ERROR_OK)
1383  return retval;
1384 
1385  if (ctrl & FLASH_CTRL_OPLK)
1386  return ERROR_OK;
1387 
1388  ctrl |= FLASH_CTRL_OPLK;
1390 
1391  if (retval != ERROR_OK)
1392  return retval;
1393 
1395 
1396  if (retval != ERROR_OK)
1397  return retval;
1398 
1399  if (!(ctrl & FLASH_CTRL_OPLK)) {
1400  LOG_ERROR("Failed to lock flash");
1401  return ERROR_FAIL;
1402  }
1403 
1404  return ERROR_OK;
1405 }
1406 
1407 static int artery_read_protection(struct flash_bank *bank, uint64_t *protection)
1408 {
1409  uint32_t epps0;
1411  &epps0);
1412 
1413  if (retval != ERROR_OK)
1414  return retval;
1415 
1416  const struct artery_flash_bank *artery_info = bank->driver_priv;
1417  const struct artery_part_info *part_info = artery_info->part_info;
1418 
1419  uint64_t prot = epps0;
1420 
1421  if (artery_series[part_info->series].has_epp_ext) {
1422  uint32_t epps1;
1424  &epps1);
1425 
1426  if (retval != ERROR_OK)
1427  return retval;
1428 
1429  prot |= (((uint64_t)epps1) << 32);
1430  }
1431 
1432  *protection = prot;
1433 
1434  return ERROR_OK;
1435 }
1436 
1438 {
1439  uint64_t prot;
1440  int retval = artery_read_protection(bank, &prot);
1441 
1442  if (retval != ERROR_OK) {
1443  LOG_ERROR("Failed to read flash protection settings");
1444  return retval;
1445  }
1446 
1447  for (unsigned int i = 0; i < bank->num_prot_blocks; i++) {
1448  const bool protected = !(prot & (UINT64_C(1) << i));
1449  bank->prot_blocks[i].is_protected = protected ? 1 : 0;
1450  }
1451 
1452  return ERROR_OK;
1453 }
1454 
1455 static int artery_erase(struct flash_bank *bank, unsigned int first,
1456  unsigned int last)
1457 {
1458  struct target *target = bank->target;
1459 
1460  if (target->state != TARGET_HALTED) {
1461  LOG_ERROR("Target not halted");
1462  return ERROR_TARGET_NOT_HALTED;
1463  }
1464 
1465  int retval = artery_init_flash(bank);
1466 
1467  if (retval != ERROR_OK) {
1468  LOG_ERROR("Failed to initialize flash controller");
1469  return retval;
1470  }
1471 
1472  // Clear the EPPERR bit, otherwise we may read an invalid value later on.
1475 
1476  if (retval != ERROR_OK) {
1477  LOG_ERROR("Failed to write FLASH_STS register");
1478  goto flash_deinit;
1479  }
1480 
1482 
1483  if (retval != ERROR_OK)
1484  goto flash_deinit;
1485 
1486  for (unsigned int i = first; i <= last; i++) {
1488  bank->base + bank->sectors[i].offset);
1489 
1490  if (retval != ERROR_OK) {
1491  LOG_ERROR("Failed to write FLASH_ADDR register");
1492  goto flash_deinit;
1493  }
1494 
1497 
1498  if (retval != ERROR_OK) {
1499  LOG_ERROR("Failed to write FLASH_CTRL register");
1500  goto flash_deinit;
1501  }
1502 
1504 
1505  if (retval != ERROR_OK)
1506  goto flash_deinit;
1507 
1508  uint32_t sts;
1510 
1511  if (retval != ERROR_OK) {
1512  LOG_ERROR("Failed to read FLASH_STS register");
1513  goto flash_deinit;
1514  }
1515 
1516  if (sts & FLASH_STS_EPPERR) {
1517  LOG_ERROR("Sector %u is write protected", i);
1518  retval = ERROR_FLASH_PROTECTED;
1519  goto flash_deinit;
1520  }
1521  }
1522 
1523  int retval_deinit;
1524 flash_deinit:
1525  retval_deinit = artery_deinit_flash(bank);
1526 
1527  if (retval_deinit != ERROR_OK)
1528  return retval_deinit;
1529 
1530  return retval;
1531 }
1532 
1533 static int artery_usd_init(struct flash_bank *bank, uint8_t **buffer)
1534 {
1535  const struct artery_flash_bank *artery_info = bank->driver_priv;
1536  const struct artery_part_info *part_info = artery_info->part_info;
1537  uint32_t usd_size = part_info->usd_size;
1538 
1539  *buffer = malloc(usd_size);
1540 
1541  if (!*buffer) {
1542  LOG_ERROR("Failed to allocate USD buffer");
1543  return ERROR_FAIL;
1544  }
1545 
1546  memset(*buffer, 0xff, usd_size);
1547 
1548  return ERROR_OK;
1549 }
1550 
1551 static int artery_usd_read(struct flash_bank *bank, uint8_t *buffer)
1552 {
1553  const struct artery_flash_bank *artery_info = bank->driver_priv;
1554  const struct artery_part_info *part_info = artery_info->part_info;
1555  const struct artery_series_info *series_info = &artery_series[part_info->series];
1556 
1557  return target_read_buffer(bank->target, series_info->usd_base,
1558  part_info->usd_size, buffer);
1559 }
1560 
1561 static uint8_t artery_usd_read_buffer(const uint8_t *buffer, uint32_t base,
1562  uint32_t offset)
1563 {
1564  return buffer[base + (offset * 2)];
1565 }
1566 
1567 static int artery_usd_load(const struct artery_part_info *part_info,
1568  const uint8_t *buffer, struct artery_usd *usd)
1569 {
1570  const uint32_t *usd_regs = artery_series[part_info->series].usd_offsets;
1571 
1572  uint8_t fap_level = artery_usd_read_buffer(buffer,
1573  usd_regs[ARTERY_USD_FAP_INDEX], 0);
1574 
1575  switch (fap_level) {
1577  case ARTERY_FAP_LEVEL_HIGH:
1578  usd->fap_level = fap_level;
1579  break;
1580  default:
1582  }
1583 
1584  usd->ssb = artery_usd_read_buffer(buffer, usd_regs[ARTERY_USD_SSB_INDEX], 0);
1585  usd->protection = 0;
1586 
1587  for (unsigned int i = 0; i < 4; i++) {
1588  const uint8_t prot = artery_usd_read_buffer(buffer,
1589  usd_regs[ARTERY_USD_EPP_INDEX], i);
1590  usd->protection |= (prot << (i * 8));
1591  }
1592 
1593  if (artery_series[part_info->series].has_epp_ext) {
1594  usd->protection_ext = 0;
1595 
1596  for (unsigned int i = 0; i < 4; i++) {
1597  const uint8_t prot = artery_usd_read_buffer(buffer,
1598  usd_regs[ARTERY_USD_EPP_INDEX], i);
1599  usd->protection_ext |= (prot << (i * 8));
1600  }
1601  }
1602 
1603  // All devices have at least two bytes of user data.
1605  usd_regs[ARTERY_USD_DATA_INDEX], 0);
1607  usd_regs[ARTERY_USD_DATA_INDEX], 1);
1608 
1609  for (unsigned int i = 0; i < part_info->usd_data_size - 2; i++) {
1610  usd->data[i + 2] = artery_usd_read_buffer(buffer,
1611  usd_regs[ARTERY_USD_DATA_EXT_INDEX], i);
1612  }
1613 
1614  return ERROR_OK;
1615 }
1616 
1617 static void artery_usd_write_buffer(uint8_t *buffer, uint32_t base,
1618  uint32_t offset, uint8_t data)
1619 {
1620  buffer[base + (offset * 2)] = data;
1621  buffer[base + (offset * 2) + 1] = ~data;
1622 }
1623 
1624 static void artery_usd_update(const struct artery_part_info *part_info,
1625  uint8_t *buffer, const struct artery_usd *usd)
1626 {
1627  const uint32_t *usd_regs = artery_series[part_info->series].usd_offsets;
1628 
1630  usd->fap_level);
1632  usd->ssb);
1633 
1634  for (unsigned int i = 0; i < 4; i++) {
1635  const uint8_t prot = usd->protection >> (i * 8);
1637  }
1638 
1639  if (artery_series[part_info->series].has_epp_ext) {
1640  for (unsigned int i = 0; i < 4; i++) {
1641  const uint8_t prot = usd->protection_ext >> (i * 8);
1643  i, prot);
1644  }
1645  }
1646 
1648  usd->data[0]);
1650  usd->data[1]);
1651 
1652  for (unsigned int i = 0; i < part_info->usd_data_size - 2; i++) {
1654  usd->data[i + 2]);
1655  }
1656 }
1657 
1658 static int artery_usd_write(struct flash_bank *bank, const uint8_t *buffer)
1659 {
1660  struct target *target = bank->target;
1661 
1663 
1664  if (retval != ERROR_OK)
1665  return retval;
1666 
1667  // Clear the PRGMERR bit, otherwise we may read an invalid value later on.
1670 
1671  if (retval != ERROR_OK) {
1672  LOG_ERROR("Failed to write FLASH_STS register");
1673  return retval;
1674  }
1675 
1676  // Set the USDULKS bit to avoid locking the USD area.
1679 
1680  if (retval != ERROR_OK) {
1681  LOG_ERROR("Failed to write FLASH_CTRL register");
1682  return retval;
1683  }
1684 
1685  const struct artery_flash_bank *artery_info = bank->driver_priv;
1686  const struct artery_part_info *part_info = artery_info->part_info;
1687 
1688  const target_addr_t usd_base = artery_series[part_info->series].usd_base;
1689  const uint32_t usd_size = part_info->usd_size;
1690 
1691  unsigned int bytes_written = 0;
1692 
1694 
1695  if (retval != ERROR_OK)
1696  return retval;
1697 
1698  while (bytes_written < usd_size) {
1699  uint32_t tmp;
1700  memcpy(&tmp, buffer + bytes_written, sizeof(tmp));
1701 
1702  if (tmp != 0xffffffff) {
1703  retval = target_write_u32(target, usd_base + bytes_written, tmp);
1704 
1705  if (retval != ERROR_OK) {
1706  LOG_ERROR("Failed to write user system data");
1707  return retval;
1708  }
1709 
1711 
1712  if (retval != ERROR_OK)
1713  return retval;
1714  }
1715 
1716  bytes_written += 4;
1717  }
1718 
1719  uint32_t sts;
1721 
1722  if (retval != ERROR_OK) {
1723  LOG_ERROR("Failed to read FLASH_STS register");
1724  return retval;
1725  }
1726 
1727  if (sts & FLASH_STS_PRGMERR) {
1728  LOG_ERROR("Failed to program user system data");
1730  }
1731 
1732  return ERROR_OK;
1733 }
1734 
1735 static int artery_usd_erase(struct flash_bank *bank)
1736 {
1738 
1739  if (retval != ERROR_OK)
1740  return retval;
1741 
1742  // Set the USDULKS bit to avoid locking the USD area.
1745 
1746  if (retval != ERROR_OK) {
1747  LOG_ERROR("Failed to write FLASH_CTRL register");
1748  return retval;
1749  }
1750 
1752 
1753  if (retval != ERROR_OK)
1754  return retval;
1755 
1756  uint32_t sts;
1758 
1759  if (retval != ERROR_OK) {
1760  LOG_ERROR("Failed to read FLASH_STS register");
1761  return retval;
1762  }
1763 
1765 
1766  if (retval != ERROR_OK) {
1767  LOG_ERROR("Failed to read FLASH_CTRL register");
1768  return retval;
1769  }
1770 
1771  return ERROR_OK;
1772 }
1773 
1774 static int artery_get_fap(struct flash_bank *bank,
1775  enum artery_fap_level *fap_level)
1776 {
1777  uint32_t usd;
1779  &usd);
1780 
1781  if (retval != ERROR_OK)
1782  return retval;
1783 
1784  const struct artery_flash_bank *artery_info = bank->driver_priv;
1785  const struct artery_part_info *part_info = artery_info->part_info;
1786  const struct artery_series_info *series_info = &artery_series[part_info->series];
1787 
1788  const bool fap_high = series_info->has_fap_high_level && (usd & FLASH_USD_FAP_HL);
1789  const bool fap_low = usd & FLASH_USD_FAP;
1790 
1791  if (fap_high && fap_low)
1792  *fap_level = ARTERY_FAP_LEVEL_HIGH;
1793  else if (fap_low)
1794  *fap_level = ARTERY_FAP_LEVEL_LOW;
1795  else
1796  *fap_level = ARTERY_FAP_LEVEL_DISABLED;
1797 
1798  return ERROR_OK;
1799 }
1800 
1801 static int artery_protect(struct flash_bank *bank, int set, unsigned int first,
1802  unsigned int last)
1803 {
1804  struct target *target = bank->target;
1805 
1806  if (target->state != TARGET_HALTED) {
1807  LOG_ERROR("Target not halted");
1808  return ERROR_TARGET_NOT_HALTED;
1809  }
1810 
1811  enum artery_fap_level fap_level;
1812  int retval = artery_get_fap(bank, &fap_level);
1813 
1814  if (retval != ERROR_OK) {
1815  LOG_ERROR("Failed to read FAP level");
1816  return retval;
1817  }
1818 
1819  if (fap_level != ARTERY_FAP_LEVEL_DISABLED) {
1820  LOG_ERROR("Protection cannot be modified when FAP is active");
1821  return ERROR_FAIL;
1822  }
1823 
1824  uint8_t *usd_buffer;
1825  retval = artery_usd_init(bank, &usd_buffer);
1826 
1827  if (retval != ERROR_OK)
1828  return retval;
1829 
1830  retval = artery_usd_read(bank, usd_buffer);
1831 
1832  if (retval != ERROR_OK) {
1833  LOG_ERROR("Failed to read user system data");
1834  return retval;
1835  }
1836 
1837  const struct artery_flash_bank *artery_info = bank->driver_priv;
1838  const struct artery_part_info *part_info = artery_info->part_info;
1839 
1840  struct artery_usd usd;
1841  retval = artery_usd_load(part_info, usd_buffer, &usd);
1842 
1843  if (retval != ERROR_OK) {
1844  LOG_ERROR("Failed to load user system data");
1845  free(usd_buffer);
1846  return retval;
1847  }
1848 
1849  for (unsigned int i = first; i <= MIN(31, last); i++) {
1850  if (bank->prot_blocks[i].is_protected == set)
1851  continue;
1852 
1853  if (set)
1854  usd.protection &= ~BIT(i);
1855  else
1856  usd.protection |= BIT(i);
1857  }
1858 
1859  for (unsigned int i = 32; i <= last; i++) {
1860  if (bank->prot_blocks[i].is_protected == set)
1861  continue;
1862 
1863  if (set)
1864  usd.protection_ext &= ~BIT(i - 32);
1865  else
1866  usd.protection_ext |= BIT(i - 32);
1867  }
1868 
1869  artery_usd_update(part_info, usd_buffer, &usd);
1870  retval = artery_init_flash(bank);
1871 
1872  if (retval != ERROR_OK) {
1873  LOG_ERROR("Failed to initialize flash controller");
1874  free(usd_buffer);
1875  return retval;
1876  }
1877 
1878  retval = artery_usd_erase(bank);
1879 
1880  if (retval != ERROR_OK) {
1881  LOG_ERROR("Failed to erase user system data");
1882  free(usd_buffer);
1883  goto flash_deinit;
1884  }
1885 
1886  retval = artery_usd_write(bank, usd_buffer);
1887 
1888  free(usd_buffer);
1889 
1890  if (retval != ERROR_OK) {
1891  LOG_ERROR("Failed to write user system data");
1892  goto flash_deinit;
1893  }
1894 
1895  int retval_deinit;
1896 flash_deinit:
1897  retval_deinit = artery_deinit_flash(bank);
1898 
1899  if (retval_deinit != ERROR_OK)
1900  return retval_deinit;
1901 
1902  return retval;
1903 }
1904 
1906  const uint8_t *buffer, uint32_t offset, uint32_t count)
1907 {
1908  struct target *target = bank->target;
1909 
1911 
1912  if (retval != ERROR_OK)
1913  return retval;
1914 
1917 
1918  if (retval != ERROR_OK) {
1919  LOG_ERROR("failed to write ctrl register");
1920  return retval;
1921  }
1922 
1923  const uint32_t block_size = 4;
1924 
1925  uint32_t bytes_written = 0;
1926  target_addr_t address = bank->base + offset;
1927 
1928  for (uint32_t i = 0; i < count / block_size; i++) {
1929  retval = target_write_memory(target, address, block_size, 1,
1930  buffer + bytes_written);
1931 
1932  if (retval != ERROR_OK)
1933  return retval;
1934 
1936 
1937  if (retval != ERROR_OK)
1938  return retval;
1939 
1940  address += block_size;
1941  bytes_written += block_size;
1942  }
1943 
1944  return ERROR_OK;
1945 }
1946 
1947 static int artery_write(struct flash_bank *bank, const uint8_t *buffer,
1948  uint32_t offset, uint32_t count)
1949 {
1950  struct target *target = bank->target;
1951 
1952  if (target->state != TARGET_HALTED) {
1953  LOG_ERROR("Target not halted");
1954  return ERROR_TARGET_NOT_HALTED;
1955  }
1956 
1957  int retval = artery_init_flash(bank);
1958  if (retval != ERROR_OK) {
1959  LOG_ERROR("Failed to initialize flash controller");
1960  return retval;
1961  }
1962 
1964 
1965  if (retval != ERROR_OK) {
1966  LOG_ERROR("Failed to write flash memory");
1967  goto flash_deinit;
1968  }
1969 
1970  int retval_deinit;
1971 flash_deinit:
1972  retval_deinit = artery_deinit_flash(bank);
1973 
1974  if (retval != ERROR_OK)
1975  return retval;
1976 
1977  return retval_deinit;
1978 }
1979 
1980 static int artery_probe(struct flash_bank *bank)
1981 {
1982  struct target *target = bank->target;
1983 
1984  if (!target_was_examined(target)) {
1985  LOG_ERROR("Target not examined yet");
1987  }
1988 
1989  const struct cortex_m_common *cortex_m = target_to_cortex_m_safe(target);
1990 
1991  if (!cortex_m) {
1992  LOG_ERROR("Target is not a Cortex-M device");
1993  return ERROR_TARGET_INVALID;
1994  }
1995 
1996  struct artery_flash_bank *artery_info = bank->driver_priv;
1997 
1998  artery_info->probed = false;
1999 
2000  int retval = target_read_u32(target, DEBUG_IDCODE, &artery_info->idcode);
2001 
2002  if (retval != ERROR_OK)
2003  return retval;
2004 
2005  const uint32_t pid = artery_info->idcode;
2006  const bool has_fpu = cortex_m->armv7m.fp_feature != FP_NONE;
2007  bool check_device_series = false;
2008  enum artery_series device_series;
2009 
2010  /*
2011  * The following PIDs are used for AT32F413 and AT32F415 devices. In order
2012  * to distinguish between the series, we use the presence of the FPU. Note
2013  * that we do not rely on the unqiue device ID (UID) which also encodes the
2014  * device series. The reason is that the UID registers are not accessible
2015  * when the flash access protection (FAP) is active.
2016  */
2017  switch (pid) {
2018  case 0x700301C5:
2019  case 0x70030240:
2020  case 0x70030242:
2021  check_device_series = true;
2022  device_series = has_fpu ? ARTERY_SERIES_F413 : ARTERY_SERIES_F415;
2023  break;
2024  default:
2025  break;
2026  }
2027 
2028  artery_info->part_info = NULL;
2029 
2030  for (size_t i = 0; i < ARRAY_SIZE(artery_parts); i++) {
2031  if (check_device_series && artery_parts[i].series != device_series)
2032  continue;
2033 
2034  if (artery_parts[i].pid == pid) {
2035  artery_info->part_info = &artery_parts[i];
2036  break;
2037  }
2038  }
2039 
2040  if (!artery_info->part_info) {
2041  LOG_ERROR("Cannot identify target as an Artery device");
2042  return ERROR_FAIL;
2043  }
2044 
2045  const struct artery_part_info *part_info = artery_info->part_info;
2046 
2047  LOG_INFO("Device ID = 0x%08" PRIx32 " (%s)", artery_info->idcode,
2048  part_info->name);
2049  LOG_INFO("Flash size = %d KiB", part_info->flash_size);
2050 
2051  free(bank->sectors);
2052 
2053  bank->base = FLASH_BASE;
2054  bank->size = part_info->flash_size * 1024;
2055 
2056  const unsigned int num_pages = (bank->size) / part_info->page_size;
2057 
2058  // Ensure that the flash infrastructure uses an alignment of 4 bytes.
2059  bank->write_start_alignment = 4;
2060  bank->write_end_alignment = 4;
2061 
2062  bank->num_sectors = num_pages;
2063  bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
2064 
2065  if (!bank->sectors) {
2066  LOG_ERROR("Failed to allocate bank sectors");
2067  return ERROR_FAIL;
2068  }
2069 
2070  for (unsigned int i = 0; i < bank->num_sectors; i++) {
2071  bank->sectors[i].offset = i * part_info->page_size;
2072  bank->sectors[i].size = part_info->page_size;
2073  bank->sectors[i].is_erased = -1;
2074  bank->sectors[i].is_protected = 0;
2075  }
2076 
2077  free(bank->prot_blocks);
2078 
2079  /*
2080  * Flash erase/program protection (EPPx) registers configuration for each
2081  * device series.
2082  *
2083  * - AT32F403A / AT32F407
2084  * - AT32F413
2085  *
2086  * Each bit represents a sector of 4 KiB. The last bit represents the
2087  * entire remaining flash memory and extension area.
2088  *
2089  * - AT32F415
2090  * - AT32WB415
2091  *
2092  * Each bit represents a sector of 2 KiB. The last bit represents the
2093  * entire remaining flash memory and extension area.
2094  *
2095  * - AT32F421
2096  * - AT32F423
2097  * - AT32F425
2098  *
2099  * Each bit represents a sector of 4 KiB. Some bits may not used
2100  * depending on the flash memory size. The last bit represents only the
2101  * flash memory extension area.
2102  *
2103  * - AT32F435 / AT32F437
2104  *
2105  * This device series has an additional erase/program protection (EPP)
2106  * register.
2107  *
2108  * The first 32 bits represent a flash sector of 4 KiB per bit.
2109  *
2110  * The additional 32 bits represent a sector of 128 KiB each. The
2111  * second last bit covers the remaining flash memory. The last bit is
2112  * always reserved.
2113  *
2114  */
2115  if (part_info->series == ARTERY_SERIES_F435_F437) {
2116  // See description above.
2117  const unsigned int num_prot_blocks_1 = 32;
2118  const unsigned int num_prot_blocks_2 = MIN(31, DIV_ROUND_UP(part_info->flash_size - 128, 128));
2119  const unsigned int num_prot_blocks = num_prot_blocks_1 + num_prot_blocks_2;
2120  bank->num_prot_blocks = num_prot_blocks;
2121  bank->prot_blocks = malloc(sizeof(struct flash_sector) * num_prot_blocks);
2122 
2123  if (!bank->prot_blocks) {
2124  LOG_ERROR("Failed to allocate protection blocks");
2125  return ERROR_FAIL;
2126  }
2127 
2128  const uint32_t prot_block_size = 4096;
2129 
2130  unsigned int i;
2131  uint32_t block_offset = 0;
2132 
2133  for (i = 0; i < 32; i++) {
2134  bank->prot_blocks[i].offset = block_offset;
2135  bank->prot_blocks[i].size = prot_block_size;
2136  bank->prot_blocks[i].is_erased = -1;
2137  bank->prot_blocks[i].is_protected = -1;
2138 
2139  block_offset += prot_block_size;
2140  }
2141 
2142  const uint32_t prot_block_size_2 = 128 * 1024;
2143 
2144  for (; i < (num_prot_blocks - 1); i++) {
2145  bank->prot_blocks[i].offset = block_offset;
2146  bank->prot_blocks[i].size = prot_block_size_2;
2147  bank->prot_blocks[i].is_erased = -1;
2148  bank->prot_blocks[i].is_protected = -1;
2149 
2150  block_offset += prot_block_size_2;
2151  }
2152 
2153  bank->prot_blocks[i].offset = block_offset;
2154  bank->prot_blocks[i].size = bank->size - block_offset;
2155  bank->prot_blocks[i].is_erased = -1;
2156  bank->prot_blocks[i].is_protected = -1;
2157  } else {
2158  uint32_t prot_block_size;
2159 
2160  switch (part_info->series) {
2162  case ARTERY_SERIES_F413:
2163  case ARTERY_SERIES_F421:
2164  case ARTERY_SERIES_F423:
2165  case ARTERY_SERIES_F425:
2166  prot_block_size = 4096;
2167  break;
2168  case ARTERY_SERIES_F415:
2169  case ARTERY_SERIES_WB415:
2170  prot_block_size = 2048;
2171  break;
2172  default:
2173  LOG_ERROR("Unknown Artery device series");
2174  return ERROR_FAIL;
2175  }
2176 
2177  const unsigned int num_prot_blocks = MIN(bank->size / prot_block_size, 32);
2178  bank->num_prot_blocks = num_prot_blocks;
2179  bank->prot_blocks = malloc(sizeof(struct flash_sector) * num_prot_blocks);
2180 
2181  if (!bank->prot_blocks) {
2182  LOG_ERROR("Failed to allocate protection blocks");
2183  return ERROR_FAIL;
2184  }
2185 
2186  unsigned int i;
2187 
2188  for (i = 0; i < (num_prot_blocks - 1); i++) {
2189  bank->prot_blocks[i].offset = i * prot_block_size;
2190  bank->prot_blocks[i].size = prot_block_size;
2191  bank->prot_blocks[i].is_erased = -1;
2192  bank->prot_blocks[i].is_protected = -1;
2193  }
2194 
2195  bank->prot_blocks[i].offset = i * prot_block_size;
2196  bank->prot_blocks[i].size = (bank->size - (i * prot_block_size));
2197  bank->prot_blocks[i].is_erased = -1;
2198  bank->prot_blocks[i].is_protected = -1;
2199  }
2200 
2201  artery_info->probed = true;
2202 
2203  return ERROR_OK;
2204 }
2205 
2206 static int artery_auto_probe(struct flash_bank *bank)
2207 {
2208  const struct artery_flash_bank *artery_info = bank->driver_priv;
2209 
2210  if (artery_info->probed)
2211  return ERROR_OK;
2212 
2213  return artery_probe(bank);
2214 }
2215 
2216 static int artery_info(struct flash_bank *bank, struct command_invocation *cmd)
2217 {
2218  const struct artery_flash_bank *artery_info = bank->driver_priv;
2219  const struct artery_part_info *part_info = artery_info->part_info;
2220 
2221  if (!part_info) {
2222  command_print_sameline(cmd, "Cannot identify target device");
2223  return ERROR_OK;
2224  }
2225 
2226  command_print_sameline(cmd, "%s - %u KiB flash", part_info->name,
2227  part_info->flash_size);
2228 
2229  return ERROR_OK;
2230 }
2231 
2232 static int artery_mass_erase(struct flash_bank *bank)
2233 {
2234  int retval = artery_init_flash(bank);
2235 
2236  if (retval != ERROR_OK) {
2237  LOG_ERROR("Failed to initialize flash controller");
2238  return retval;
2239  }
2240 
2242 
2243  if (retval != ERROR_OK)
2244  goto flash_deinit;
2245 
2246  // Clear the EPPERR bit, otherwise we may read an invalid value later on.
2249 
2250  if (retval != ERROR_OK) {
2251  LOG_ERROR("Failed to write FLASH_CTRL register");
2252  goto flash_deinit;
2253  }
2254 
2257 
2258  if (retval != ERROR_OK) {
2259  LOG_ERROR("Failed to write FLASH_CTRL register");
2260  goto flash_deinit;
2261  }
2262 
2264 
2265  if (retval != ERROR_OK)
2266  goto flash_deinit;
2267 
2268  uint32_t sts;
2270 
2271  if (retval != ERROR_OK) {
2272  LOG_ERROR("Failed to read FLASH_STS register");
2273  goto flash_deinit;
2274  }
2275 
2276  if (sts & FLASH_STS_EPPERR) {
2277  LOG_ERROR("Mass erase operation failed");
2278  goto flash_deinit;
2279  }
2280 
2281  int retval_deinit;
2282 flash_deinit:
2283  retval_deinit = artery_deinit_flash(bank);
2284 
2285  if (retval != ERROR_OK)
2286  return retval;
2287 
2288  return retval_deinit;
2289 }
2290 
2291 COMMAND_HANDLER(artery_handle_fap_enable_command)
2292 {
2293  if (CMD_ARGC != 1)
2295 
2296  struct flash_bank *bank;
2297  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2298 
2299  if (retval != ERROR_OK)
2300  return retval;
2301 
2302  enum artery_fap_level fap_level;
2303  retval = artery_get_fap(bank, &fap_level);
2304 
2305  if (retval != ERROR_OK) {
2306  command_print(CMD, "failed to read FAP state");
2307  return retval;
2308  }
2309 
2310  if (fap_level != ARTERY_FAP_LEVEL_DISABLED) {
2311  command_print(CMD, "flash access protection is already enabled");
2312  return ERROR_FAIL;
2313  }
2314 
2315  uint8_t *usd_buffer;
2316  retval = artery_usd_init(bank, &usd_buffer);
2317 
2318  if (retval != ERROR_OK)
2319  return retval;
2320 
2321  retval = artery_usd_read(bank, usd_buffer);
2322 
2323  if (retval != ERROR_OK) {
2324  command_print(CMD, "failed to read user system data");
2325  return retval;
2326  }
2327 
2328  const struct artery_flash_bank *artery_info = bank->driver_priv;
2329  const struct artery_part_info *part_info = artery_info->part_info;
2330 
2331  struct artery_usd usd;
2332  retval = artery_usd_load(part_info, usd_buffer, &usd);
2333 
2334  if (retval != ERROR_OK) {
2335  command_print(CMD, "failed to load user system data");
2336  return retval;
2337  }
2338 
2340  artery_usd_update(part_info, usd_buffer, &usd);
2341 
2342  retval = artery_init_flash(bank);
2343  if (retval != ERROR_OK) {
2344  LOG_ERROR("Failed to initialize flash controller");
2345  goto flash_deinit;
2346  }
2347 
2348  retval = artery_usd_erase(bank);
2349 
2350  if (retval != ERROR_OK) {
2351  free(usd_buffer);
2352  command_print(CMD, "failed to erase user system data");
2353  goto flash_deinit;
2354  }
2355 
2356  retval = artery_usd_write(bank, usd_buffer);
2357  free(usd_buffer);
2358 
2359  if (retval != ERROR_OK) {
2360  command_print(CMD, "failed to write user system data");
2361  goto flash_deinit;
2362  }
2363 
2364  int retval_deinit;
2365 flash_deinit:
2366  retval_deinit = artery_deinit_flash(bank);
2367 
2368  if (retval_deinit != ERROR_OK)
2369  return retval_deinit;
2370 
2371  return retval;
2372 }
2373 
2374 /*
2375  * We use a dedicated operation to perform the FAP unlock operation that only
2376  * writes the corresponding FAP byte(s) instead of the entire user system
2377  * data (USD) area. The reason is that directly after the FAP byte(s) are
2378  * written, a device reset is performed and all other writes to the USD area
2379  * would fail and generate errors.
2380 */
2382 {
2383  int retval = artery_init_flash(bank);
2384 
2385  if (retval != ERROR_OK) {
2386  LOG_ERROR("Failed to initialize flash controller");
2387  return retval;
2388  }
2389 
2390  retval = artery_usd_erase(bank);
2391 
2392  if (retval != ERROR_OK) {
2393  LOG_ERROR("Failed to erase user system data");
2394  goto flash_deinit;
2395  }
2396 
2398 
2399  if (retval != ERROR_OK)
2400  goto flash_deinit;
2401 
2402  // Clear the PRGMERR bit, otherwise we may read an invalid value later on.
2405 
2406  if (retval != ERROR_OK) {
2407  LOG_ERROR("Failed to write FLASH_STS register");
2408  goto flash_deinit;
2409  }
2410 
2411  // Set the USDULKS bit to avoid locking the USD area.
2414 
2415  if (retval != ERROR_OK) {
2416  LOG_ERROR("Failed to write FLASH_CTRL register");
2417  goto flash_deinit;
2418  }
2419 
2421 
2422  if (retval != ERROR_OK)
2423  goto flash_deinit;
2424 
2425  uint16_t buffer;
2426 
2427  const struct artery_flash_bank *artery_info = bank->driver_priv;
2428  const struct artery_part_info *part_info = artery_info->part_info;
2429  const uint32_t *usd_regs = artery_series[part_info->series].usd_offsets;
2430 
2431  artery_usd_write_buffer((uint8_t *)&buffer, usd_regs[ARTERY_USD_FAP_INDEX], 0,
2433 
2434  const target_addr_t usd_base = artery_series[part_info->series].usd_base;
2435  retval = target_write_u16(bank->target, usd_base, buffer);
2436 
2437  if (retval != ERROR_OK) {
2438  LOG_ERROR("Failed to write FAP level");
2439  goto flash_deinit;
2440  }
2441 
2442  /*
2443  * Note that we do not need to deinitialize the flash memory because the
2444  * device performed a reset anyway.
2445  */
2446 
2447  return ERROR_OK;
2448 
2449  int retval_deinit;
2450 flash_deinit:
2451  retval_deinit = artery_deinit_flash(bank);
2452 
2453  if (retval_deinit != ERROR_OK)
2454  return retval_deinit;
2455 
2456  return retval;
2457 }
2458 
2459 COMMAND_HANDLER(artery_handle_fap_disable_command)
2460 {
2461  if (CMD_ARGC != 1)
2463 
2464  struct flash_bank *bank;
2465  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2466 
2467  if (retval != ERROR_OK)
2468  return retval;
2469 
2470  enum artery_fap_level fap_level;
2471  retval = artery_get_fap(bank, &fap_level);
2472 
2473  if (retval != ERROR_OK) {
2474  command_print(CMD, "failed to read FAP state");
2475  return retval;
2476  }
2477 
2478  if (fap_level == ARTERY_FAP_LEVEL_DISABLED) {
2479  command_print(CMD, "flash access protection is not enabled");
2480  return ERROR_FAIL;
2481  }
2482 
2483  retval = artery_disable_fap(bank);
2484 
2485  if (retval != ERROR_OK) {
2486  command_print(CMD, "failed to disable flash access protection");
2487  return retval;
2488  }
2489 
2490  return ERROR_OK;
2491 }
2492 
2493 COMMAND_HANDLER(artery_handle_fap_state_command)
2494 {
2495  if (CMD_ARGC != 1)
2497 
2498  struct flash_bank *bank;
2499  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2500 
2501  if (retval != ERROR_OK)
2502  return retval;
2503 
2504  enum artery_fap_level fap_level;
2505  retval = artery_get_fap(bank, &fap_level);
2506 
2507  if (retval != ERROR_OK) {
2508  command_print(CMD, "failed to read FAP level");
2509  return retval;
2510  }
2511 
2512  const bool fap_enabled = fap_level != ARTERY_FAP_LEVEL_DISABLED;
2513  command_print(CMD, "%u", fap_enabled);
2514 
2515  return ERROR_OK;
2516 }
2517 
2518 COMMAND_HANDLER(artery_handle_mass_erase_command)
2519 {
2520  if (CMD_ARGC != 1)
2522 
2523  struct flash_bank *bank;
2524  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2525 
2526  if (retval != ERROR_OK)
2527  return retval;
2528 
2529  retval = artery_mass_erase(bank);
2530 
2531  if (retval != ERROR_OK) {
2532  command_print(CMD, "Mass erase failed");
2533  return retval;
2534  }
2535 
2536  return ERROR_OK;
2537 }
2538 
2539 static const struct command_registration artery_fap_command_handlers[] = {
2540  {
2541  .name = "enable",
2542  .handler = artery_handle_fap_enable_command,
2543  .mode = COMMAND_EXEC,
2544  .usage = "<bank_id>",
2545  .help = "Enable flash access protection (FAP)",
2546  },
2547  {
2548  .name = "disable",
2549  .handler = artery_handle_fap_disable_command,
2550  .mode = COMMAND_EXEC,
2551  .usage = "<bank_id>",
2552  .help = "Disable flash access protection (FAP)",
2553  },
2554  {
2555  .name = "state",
2556  .handler = artery_handle_fap_state_command,
2557  .mode = COMMAND_EXEC,
2558  .usage = "<bank_id>",
2559  .help = "Get the flash access protection (FAP) state",
2560  },
2561 
2563 };
2564 
2565 static const struct command_registration artery_exec_command_handlers[] = {
2566  {
2567  .name = "fap",
2568  .mode = COMMAND_ANY,
2569  .help = "flash access protection (FAP) command group",
2570  .usage = "",
2571  .chain = artery_fap_command_handlers,
2572  },
2573  {
2574  .name = "mass_erase",
2575  .handler = artery_handle_mass_erase_command,
2576  .mode = COMMAND_EXEC,
2577  .usage = "<bank_id>",
2578  .help = "Erase entire flash memory",
2579  },
2581 };
2582 
2583 static const struct command_registration artery_command_handlers[] = {
2584  {
2585  .name = "artery",
2586  .mode = COMMAND_ANY,
2587  .help = "artery flash command group",
2588  .usage = "",
2590  },
2592 };
2593 
2594 const struct flash_driver artery_flash = {
2595  .name = "artery",
2596  .commands = artery_command_handlers,
2597  .flash_bank_command = artery_flash_bank_command,
2598  .erase = artery_erase,
2599  .protect = artery_protect,
2600  .write = artery_write,
2601  .read = default_flash_read,
2602  .probe = artery_probe,
2603  .auto_probe = artery_auto_probe,
2604  .erase_check = default_flash_blank_check,
2605  .protect_check = artery_protect_check,
2606  .info = artery_info,
2607  .free_driver_priv = default_flash_free_driver_priv,
2608 };
@ FP_NONE
Definition: armv7m.h:214
static int artery_usd_write(struct flash_bank *bank, const uint8_t *buffer)
Definition: artery.c:1658
#define HICK_STABLE_TIMEOUT
Definition: artery.c:24
#define FLASH_ERASE_TIMEOUT
Definition: artery.c:22
const struct flash_driver artery_flash
Definition: artery.c:2594
static const struct command_registration artery_command_handlers[]
Definition: artery.c:2583
static int artery_usd_init(struct flash_bank *bank, uint8_t **buffer)
Definition: artery.c:1533
static void artery_usd_write_buffer(uint8_t *buffer, uint32_t base, uint32_t offset, uint8_t data)
Definition: artery.c:1617
static int artery_disable_fap(struct flash_bank *bank)
Definition: artery.c:2381
static int artery_probe(struct flash_bank *bank)
Definition: artery.c:1980
static int artery_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: artery.c:1455
static int artery_mass_erase(struct flash_bank *bank)
Definition: artery.c:2232
static const struct artery_part_info artery_parts[]
Definition: artery.c:173
static int artery_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: artery.c:2216
FLASH_BANK_COMMAND_HANDLER(artery_flash_bank_command)
Definition: artery.c:1149
static int artery_usd_read(struct flash_bank *bank, uint8_t *buffer)
Definition: artery.c:1551
static int artery_usd_unlock(struct flash_bank *bank)
Definition: artery.c:1260
static int artery_auto_probe(struct flash_bank *bank)
Definition: artery.c:2206
static int artery_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: artery.c:1801
static const struct command_registration artery_exec_command_handlers[]
Definition: artery.c:2565
static int artery_enable_hiclk(struct flash_bank *bank)
Definition: artery.c:1214
static uint8_t artery_usd_read_buffer(const uint8_t *buffer, uint32_t base, uint32_t offset)
Definition: artery.c:1561
static int artery_usd_lock(struct flash_bank *bank)
Definition: artery.c:1294
static const uint32_t flash_regs_f4xx_wb415[ARTERY_FLASH_REG_INDEX_NUM]
Definition: artery.c:36
static int artery_usd_erase(struct flash_bank *bank)
Definition: artery.c:1735
static int artery_write_flash_register(struct flash_bank *bank, enum artery_flash_reg_index reg, uint32_t value)
Definition: artery.c:1177
COMMAND_HANDLER(artery_handle_fap_enable_command)
Definition: artery.c:2291
static int artery_wait_flash_busy(struct flash_bank *bank, unsigned int timeout)
Definition: artery.c:1188
static int artery_read_protection(struct flash_bank *bank, uint64_t *protection)
Definition: artery.c:1407
static const uint32_t flash_regs_f435_f437[ARTERY_FLASH_REG_INDEX_NUM]
Definition: artery.c:49
static const uint32_t usd_offsets_f4xx_wb415[ARTERY_USD_INDEX_NUM]
Definition: artery.c:69
static int artery_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: artery.c:1947
static const uint32_t usd_offsets_f403a_f407_f413[ARTERY_USD_INDEX_NUM]
Definition: artery.c:79
static int artery_read_flash_register(struct flash_bank *bank, enum artery_flash_reg_index reg, uint32_t *value)
Definition: artery.c:1166
static int artery_usd_load(const struct artery_part_info *part_info, const uint8_t *buffer, struct artery_usd *usd)
Definition: artery.c:1567
static const uint32_t usd_offsets_f435_f437[ARTERY_USD_INDEX_NUM]
Definition: artery.c:89
static int artery_deinit_flash(struct flash_bank *bank)
Definition: artery.c:1372
static int artery_init_flash(struct flash_bank *bank)
Definition: artery.c:1326
static void artery_usd_update(const struct artery_part_info *part_info, uint8_t *buffer, const struct artery_usd *usd)
Definition: artery.c:1624
static const struct command_registration artery_fap_command_handlers[]
Definition: artery.c:2539
static int artery_protect_check(struct flash_bank *bank)
Definition: artery.c:1437
#define FLASH_WRITE_TIMEOUT
Definition: artery.c:23
#define FLASH_MASS_ERASE_TIMEOUT
Definition: artery.c:21
static int artery_write_without_loader(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: artery.c:1905
static int artery_get_fap(struct flash_bank *bank, enum artery_fap_level *fap_level)
Definition: artery.c:1774
artery_flash_reg_index
Definition: artery.h:25
@ ARTERY_FLASH_REG_USD
Definition: artery.h:32
@ ARTERY_FLASH_REG_USD_UNLOCK
Definition: artery.h:28
@ ARTERY_FLASH_REG_PSR
Definition: artery.h:26
@ ARTERY_FLASH_REG_ADDR
Definition: artery.h:31
@ ARTERY_FLASH_REG_INDEX_NUM
Definition: artery.h:35
@ ARTERY_FLASH_REG_EPPS1
Definition: artery.h:34
@ ARTERY_FLASH_REG_CTRL
Definition: artery.h:30
@ ARTERY_FLASH_REG_UNLOCK
Definition: artery.h:27
@ ARTERY_FLASH_REG_STS
Definition: artery.h:29
@ ARTERY_FLASH_REG_EPPS0
Definition: artery.h:33
#define CRM_REG_CTRL
Definition: artery.h:94
#define DEBUG_IDCODE
Definition: artery.h:10
#define FLASH_BASE
Definition: artery.h:12
#define KEY2
Definition: artery.h:126
#define FLASH_CTRL_SECERS
Definition: artery.h:107
artery_fap_level
Definition: artery.h:48
@ ARTERY_FAP_LEVEL_DISABLED
Definition: artery.h:49
@ ARTERY_FAP_LEVEL_LOW
Definition: artery.h:50
@ ARTERY_FAP_LEVEL_HIGH
Definition: artery.h:51
#define FLASH_CTRL_ERSTR
Definition: artery.h:103
#define CRM_CTRL_HICKSTBL
Definition: artery.h:97
#define FLASH_CTRL_OPLK
Definition: artery.h:102
#define FLASH_USD_FAP_HL
Definition: artery.h:118
#define FLASH_STS_OBF
Definition: artery.h:111
#define FLASH_STS_EPPERR
Definition: artery.h:113
#define FLASH_USD_FAP
Definition: artery.h:117
#define FLASH_CTRL_USDULKS
Definition: artery.h:101
#define FLASH_CTRL_USDERS
Definition: artery.h:104
#define FLASH_STS_PRGMERR
Definition: artery.h:112
#define FLASH_CTRL_BANKERS
Definition: artery.h:106
@ ARTERY_USD_FAP_INDEX
Definition: artery.h:39
@ ARTERY_USD_EPP_EXT_INDEX
Definition: artery.h:43
@ ARTERY_USD_INDEX_NUM
Definition: artery.h:45
@ ARTERY_USD_DATA_INDEX
Definition: artery.h:41
@ ARTERY_USD_DATA_EXT_INDEX
Definition: artery.h:44
@ ARTERY_USD_EPP_INDEX
Definition: artery.h:42
@ ARTERY_USD_SSB_INDEX
Definition: artery.h:40
artery_series
Definition: artery.h:14
@ ARTERY_SERIES_F413
Definition: artery.h:16
@ ARTERY_SERIES_F415
Definition: artery.h:17
@ ARTERY_SERIES_F425
Definition: artery.h:20
@ ARTERY_SERIES_F403A_F407
Definition: artery.h:15
@ ARTERY_SERIES_F423
Definition: artery.h:19
@ ARTERY_SERIES_WB415
Definition: artery.h:22
@ ARTERY_SERIES_F421
Definition: artery.h:18
@ ARTERY_SERIES_F435_F437
Definition: artery.h:21
#define FLASH_CTRL_FPRGM
Definition: artery.h:108
#define KEY1
Definition: artery.h:125
#define CRM_CTRL_HICKEN
Definition: artery.h:98
#define FLASH_CTRL_USDPRGM
Definition: artery.h:105
Support functions to access arbitrary bits in a byte array.
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:352
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:375
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:118
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static struct cortex_m_common * target_to_cortex_m_safe(struct target *target)
Definition: cortex_m.h:314
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_PROTECTED
Definition: flash/common.h:37
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
void keep_alive(void)
Definition: log.c:427
#define ERROR_FAIL
Definition: log.h:174
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_INFO(expr ...)
Definition: log.h:127
#define ERROR_OK
Definition: log.h:168
uint8_t protection
Definition: qn908x.c:1
#define MIN(a, b)
Definition: replacements.h:22
struct rtt_control ctrl
Control block.
Definition: rtt/rtt.c:25
#define BIT(nr)
Definition: stm32l4x.h:18
int fp_feature
Definition: armv7m.h:236
const char * name
Definition: artery.h:56
uint32_t usd_data_size
Definition: artery.h:65
uint32_t page_size
Definition: artery.h:61
enum artery_series series
Definition: artery.h:57
uint32_t usd_size
Definition: artery.h:63
uint32_t flash_size
Definition: artery.h:59
uint32_t pid
Definition: artery.h:55
uint32_t flash_regs_base
Definition: artery.h:77
uint32_t usd_base
Definition: artery.h:80
bool has_fap_high_level
Definition: artery.h:75
uint32_t crm_base
Definition: artery.h:79
const uint32_t * flash_regs
Definition: artery.h:78
uint8_t data[ARTERY_USD_DATA_MAX_SIZE]
Definition: artery.h:91
uint32_t protection_ext
Definition: artery.h:90
uint32_t protection
Definition: artery.h:89
enum artery_fap_level fap_level
Definition: artery.h:87
uint8_t ssb
Definition: artery.h:88
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:234
struct armv7m_common armv7m
Definition: cortex_m.h:229
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
Definition: register.h:111
Definition: target.h:119
enum target_state state
Definition: target.h:160
Definition: psoc6.c:83
int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
Definition: target.c:2671
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2416
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1275
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2650
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2559
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:786
static bool target_was_examined(const struct target *target)
Definition: target.h:432
#define ERROR_TARGET_INVALID
Definition: target.h:783
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:793
int64_t timeval_ms(void)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
uint64_t target_addr_t
Definition: types.h:335
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22