OpenOCD
mspm0.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2023-2025 Texas Instruments Incorporated - https://www.ti.com/
5  *
6  * NOR flash driver for MSPM0L and MSPM0G class of uC from Texas Instruments.
7  *
8  * See:
9  * https://www.ti.com/microcontrollers-mcus-processors/arm-based-microcontrollers/arm-cortex-m0-mcus/overview.html
10  ***************************************************************************/
11 
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 
16 #include "imp.h"
17 #include <helper/bits.h>
18 #include <helper/time_support.h>
19 
20 /* MSPM0 Region memory map */
21 #define MSPM0_FLASH_BASE_NONMAIN 0x41C00000
22 #define MSPM0_FLASH_END_NONMAIN 0x41C00400
23 #define MSPM0_FLASH_BASE_MAIN 0x0
24 #define MSPM0_FLASH_BASE_DATA 0x41D00000
25 
26 /* MSPM0 FACTORYREGION registers */
27 #define MSPM0_FACTORYREGION 0x41C40000
28 #define MSPM0_TRACEID (MSPM0_FACTORYREGION + 0x000)
29 #define MSPM0_DID (MSPM0_FACTORYREGION + 0x004)
30 #define MSPM0_USERID (MSPM0_FACTORYREGION + 0x008)
31 #define MSPM0_SRAMFLASH (MSPM0_FACTORYREGION + 0x018)
32 
33 /* MSPM0 FCTL registers */
34 #define FLASH_CONTROL_BASE 0x400CD000
35 #define FCTL_REG_DESC (FLASH_CONTROL_BASE + 0x10FC)
36 #define FCTL_REG_CMDEXEC (FLASH_CONTROL_BASE + 0x1100)
37 #define FCTL_REG_CMDTYPE (FLASH_CONTROL_BASE + 0x1104)
38 #define FCTL_REG_CMDADDR (FLASH_CONTROL_BASE + 0x1120)
39 #define FCTL_REG_CMDBYTEN (FLASH_CONTROL_BASE + 0x1124)
40 #define FCTL_REG_CMDDATA0 (FLASH_CONTROL_BASE + 0x1130)
41 #define FCTL_REG_CMDWEPROTA (FLASH_CONTROL_BASE + 0x11D0)
42 #define FCTL_REG_CMDWEPROTB (FLASH_CONTROL_BASE + 0x11D4)
43 #define FCTL_REG_CMDWEPROTNM (FLASH_CONTROL_BASE + 0x1210)
44 #define FCTL_REG_STATCMD (FLASH_CONTROL_BASE + 0x13D0)
45 
46 /* FCTL_STATCMD[CMDDONE] Bits */
47 #define FCTL_STATCMD_CMDDONE_MASK 0x00000001
48 #define FCTL_STATCMD_CMDDONE_STATDONE 0x00000001
49 
50 /* FCTL_STATCMD[CMDPASS] Bits */
51 #define FCTL_STATCMD_CMDPASS_MASK 0x00000002
52 #define FCTL_STATCMD_CMDPASS_STATPASS 0x00000002
53 
54 /*
55  * FCTL_CMDEXEC Bits
56  * FCTL_CMDEXEC[VAL] Bits
57  */
58 #define FCTL_CMDEXEC_VAL_EXECUTE 0x00000001
59 
60 /* FCTL_CMDTYPE[COMMAND] Bits */
61 #define FCTL_CMDTYPE_COMMAND_PROGRAM 0x00000001
62 #define FCTL_CMDTYPE_COMMAND_ERASE 0x00000002
63 
64 /* FCTL_CMDTYPE[SIZE] Bits */
65 #define FCTL_CMDTYPE_SIZE_ONEWORD 0x00000000
66 #define FCTL_CMDTYPE_SIZE_SECTOR 0x00000040
67 
68 /* FCTL_FEATURE_VER_B minimum */
69 #define FCTL_FEATURE_VER_B 0xA
70 
71 #define MSPM0_MAX_PROTREGS 3
72 
73 #define MSPM0_FLASH_TIMEOUT_MS 8000
74 #define ERR_STRING_MAX 255
75 
76 /* SYSCTL BASE */
77 #define SYSCTL_BASE 0x400AF000
78 #define SYSCTL_SECCFG_SECSTATUS (SYSCTL_BASE + 0x00003048)
79 
80 /* TI manufacturer ID */
81 #define TI_MANUFACTURER_ID 0x17
82 
83 /* Defines for probe status */
84 #define MSPM0_NO_ID_FOUND 0
85 #define MSPM0_DEV_ID_FOUND 1
86 #define MSPM0_DEV_PART_ID_FOUND 2
87 
89  /* chip id register */
90  uint32_t did;
91  /* Device Unique ID register */
92  uint32_t traceid;
93  unsigned char version;
94 
95  const char *name;
96 
97  /* Decoded flash information */
98  unsigned int data_flash_size_kb;
99  unsigned int main_flash_size_kb;
100  unsigned int main_flash_num_banks;
101  unsigned int sector_size;
102  /* Decoded SRAM information */
103  unsigned int sram_size_kb;
104 
105  /* Flash word size: 64 bit = 8, 128bit = 16 bytes */
106  unsigned char flash_word_size_bytes;
107 
108  /* Protection register stuff */
109  unsigned int protect_reg_base;
110  unsigned int protect_reg_count;
111 
112  /* Flashctl version: A - CMDWEPROTA/B, B- CMDWEPROTB */
113  unsigned char flash_version;
114 };
115 
117  const char *part_name;
118  unsigned short part;
119  unsigned char variant;
120 };
121 
123  const char *family_name;
124  unsigned short part_num;
125  unsigned char part_count;
126  const struct mspm0_part_info *part_info;
127 };
128 
129 /* https://www.ti.com/lit/ds/symlink/mspm0l1346.pdf Table 8-13 and so on */
130 static const struct mspm0_part_info mspm0l_parts[] = {
131  /* MSPM0L110x */
132  { "MSPM0L1105TDGS20R", 0x51DB, 0x16 },
133  { "MSPM0L1105TDGS28R", 0x51DB, 0x83 },
134  { "MSPM0L1105TDYYR", 0x51DB, 0x54 },
135  { "MSPM0L1105TRGER", 0x51DB, 0x86 },
136  { "MSPM0L1105TRHBR", 0x51DB, 0x68 },
137  { "MSPM0L1106TDGS20R", 0x5552, 0x4B },
138  { "MSPM0L1106TDGS28R", 0x5552, 0x98 },
139  { "MSPM0L1106TDYYR", 0x5552, 0x9D },
140  { "MSPM0L1106TRGER", 0x5552, 0x90 },
141  { "MSPM0L1106TRHBR", 0x5552, 0x53 },
142  /* MSPM0L130x (covers MSPM0L134x) */
143  { "MSPM0L1303SRGER", 0xef0, 0x17 },
144  { "MSPM0L1303TRGER", 0xef0, 0xe2 },
145  { "MSPM0L1304QDGS20R", 0xd717, 0x91 },
146  { "MSPM0L1304QDGS28R", 0xd717, 0xb6 },
147  { "MSPM0L1304QDYYR", 0xd717, 0xa0 },
148  { "MSPM0L1304QRHBR", 0xd717, 0xa9 },
149  { "MSPM0L1304SDGS20R", 0xd717, 0xfa },
150  { "MSPM0L1304SDGS28R", 0xd717, 0x73 },
151  { "MSPM0L1304SDYYR", 0xd717, 0xb7 },
152  { "MSPM0L1304SRGER", 0xd717, 0x26 },
153  { "MSPM0L1304SRHBR", 0xd717, 0xe4 },
154  { "MSPM0L1304TDGS20R", 0xd717, 0x33 },
155  { "MSPM0L1304TDGS28R", 0xd717, 0xa8 },
156  { "MSPM0L1304TDYYR", 0xd717, 0xf9 },
157  { "MSPM0L1304TRGER", 0xd717, 0xb7 },
158  { "MSPM0L1304TRHBR", 0xd717, 0x5a },
159  { "MSPM0L1305QDGS20R", 0x4d03, 0xb7 },
160  { "MSPM0L1305QDGS28R", 0x4d03, 0x74 },
161  { "MSPM0L1305QDYYR", 0x4d03, 0xec },
162  { "MSPM0L1305QRHBR", 0x4d03, 0x78 },
163  { "MSPM0L1305SDGS20R", 0x4d03, 0xc7 },
164  { "MSPM0L1305SDGS28R", 0x4d03, 0x64 },
165  { "MSPM0L1305SDYYR", 0x4d03, 0x91 },
166  { "MSPM0L1305SRGER", 0x4d03, 0x73 },
167  { "MSPM0L1305SRHBR", 0x4d03, 0x2d },
168  { "MSPM0L1305TDGS20R", 0x4d03, 0xa0 },
169  { "MSPM0L1305TDGS28R", 0x4d03, 0xfb },
170  { "MSPM0L1305TDYYR", 0x4d03, 0xde },
171  { "MSPM0L1305TRGER", 0x4d03, 0xea },
172  { "MSPM0L1305TRHBR", 0x4d03, 0x85 },
173  { "MSPM0L1306QDGS20R", 0xbb70, 0x59 },
174  { "MSPM0L1306QDGS28R", 0xbb70, 0xf7 },
175  { "MSPM0L1306QDYYR", 0xbb70, 0x9f },
176  { "MSPM0L1306QRHBR", 0xbb70, 0xc2 },
177  { "MSPM0L1306SDGS20R", 0xbb70, 0xf4 },
178  { "MSPM0L1306SDGS28R", 0xbb70, 0x5 },
179  { "MSPM0L1306SDYYR", 0xbb70, 0xe },
180  { "MSPM0L1306SRGER", 0xbb70, 0x7f },
181  { "MSPM0L1306SRHBR", 0xbb70, 0x3c },
182  { "MSPM0L1306TDGS20R", 0xbb70, 0xa },
183  { "MSPM0L1306TDGS28R", 0xbb70, 0x63 },
184  { "MSPM0L1306TDYYR", 0xbb70, 0x35 },
185  { "MSPM0L1306TRGER", 0xbb70, 0xaa },
186  { "MSPM0L1306TRHBR", 0xbb70, 0x52 },
187  { "MSPM0L1343TDGS20R", 0xb231, 0x2e },
188  { "MSPM0L1344TDGS20R", 0x40b0, 0xd0 },
189  { "MSPM0L1345TDGS28R", 0x98b4, 0x74 },
190  { "MSPM0L1346TDGS28R", 0xf2b5, 0xef },
191 };
192 
193 /* https://www.ti.com/lit/ds/symlink/mspm0g3506.pdf Table 8-22 */
194 static const struct mspm0_part_info mspm0g_parts_bb88[] = {
195  /* MSPM0G110x */
196  { "MSPM0G1105TPTR", 0x8934, 0xD },
197  { "MSPM0G1105TRGZR", 0x8934, 0xFE },
198  { "MSPM0G1106TPMR", 0x477B, 0xD4 },
199  { "MSPM0G1106TPTR", 0x477B, 0x71 },
200  { "MSPM0G1106TRGZR", 0x477B, 0xBB },
201  { "MSPM0G1106TRHBR", 0x477B, 0x01 },
202  { "MSPM0G1106TYCJR", 0x477B, 0x09 },
203  { "MSPM0G1107TDGS28R", 0x807B, 0x82 },
204  { "MSPM0G1107TPMR", 0x807B, 0xB3 },
205  { "MSPM0G1107TPTR", 0x807B, 0x32 },
206  { "MSPM0G1107TRGER", 0x807B, 0x79 },
207  { "MSPM0G1107TRGZR", 0x807B, 0x20 },
208  { "MSPM0G1107TRHBR", 0x807B, 0xBC },
209  { "MSPM0G1107TYCJR", 0x807B, 0x7A },
210  /* MSPM0G150x */
211  { "MSPM0G1505SDGS28R", 0x13C4, 0x73 },
212  { "MSPM0G1505SPMR", 0x13C4, 0x53 },
213  { "MSPM0G1505SPTR", 0x13C4, 0x3E },
214  { "MSPM0G1505SRGER", 0x13C4, 0x47 },
215  { "MSPM0G1505SRGZR", 0x13C4, 0x34 },
216  { "MSPM0G1505SRHBR", 0x13C4, 0x30 },
217  { "MSPM0G1506SDGS28R", 0x5AE0, 0x3A },
218  { "MSPM0G1506SPMR", 0x5AE0, 0xF6 },
219  { "MSPM0G1506SRGER", 0x5AE0, 0x67 },
220  { "MSPM0G1506SRGZR", 0x5AE0, 0x75 },
221  { "MSPM0G1506SRHBR", 0x5AE0, 0x57 },
222  { "MSPM0G1506SRPTR", 0x5AE0, 0x36 },
223  { "MSPM0G1506SYCJR", 0x5AE0, 0x9E },
224  { "MSPM0G1507SDGS28R", 0x2655, 0x6D },
225  { "MSPM0G1507SPMR", 0x2655, 0x97 },
226  { "MSPM0G1507SPTR", 0x2655, 0x2E },
227  { "MSPM0G1507SRGER", 0x2655, 0x83 },
228  { "MSPM0G1507SRGZR", 0x2655, 0xD3 },
229  { "MSPM0G1507SRHBR", 0x2655, 0x4D },
230  { "MSPM0G1507SYCJR", 0x2655, 0x65 },
231  /* MSPM0G310x */
232  { "MSPM0G3105SDGS20R", 0x4749, 0x21 },
233  { "MSPM0G3105SDGS28R", 0x4749, 0xDD },
234  { "MSPM0G3105SRHBR", 0x4749, 0xBE },
235  { "MSPM0G3106SDGS20R", 0x54C7, 0xD2 },
236  { "MSPM0G3106SDGS28R", 0x54C7, 0xB9 },
237  { "MSPM0G3106SRHBR", 0x54C7, 0x67 },
238  { "MSPM0G3107SDGS20R", 0xAB39, 0x5C },
239  { "MSPM0G3107SDGS28R", 0xAB39, 0xCC },
240  { "MSPM0G3107SRHBR", 0xAB39, 0xB7 },
241  /* MSPM0G350x */
242  { "MSPM0G3505SDGS28R", 0xc504, 0x8e },
243  { "MSPM0G3505SPMR", 0xc504, 0x1d },
244  { "MSPM0G3505SPTR", 0xc504, 0x93 },
245  { "MSPM0G3505SRGZR", 0xc504, 0xc7 },
246  { "MSPM0G3505SRHBR", 0xc504, 0xe7 },
247  { "MSPM0G3506SDGS28R", 0x151f, 0x8 },
248  { "MSPM0G3506SPMR", 0x151f, 0xd4 },
249  { "MSPM0G3506SPTR", 0x151f, 0x39 },
250  { "MSPM0G3506SRGZR", 0x151f, 0xfe },
251  { "MSPM0G3506SRHBR", 0x151f, 0xb5 },
252  { "MSPM0G3507SDGS28R", 0xae2d, 0xca },
253  { "MSPM0G3507SPMR", 0xae2d, 0xc7 },
254  { "MSPM0G3507SPTR", 0xae2d, 0x3f },
255  { "MSPM0G3507SRGZR", 0xae2d, 0xf7 },
256  { "MSPM0G3507SRHBR", 0xae2d, 0x4c },
257  /* MSPM0G310x-Q1 */
258  { "M0G3105QDGS20RQ1", 0x1349, 0xFB},
259  { "M0G3105QDGS28RQ1", 0x1349, 0x1B},
260  { "M0G3105QDGS32RQ1", 0x1349, 0x08},
261  { "M0G3105QPMRQ1", 0x1349, 0xD0},
262  { "M0G3105QPTRQ1", 0x1349, 0xEF},
263  { "M0G3105QRGZRQ1", 0x1349, 0x70},
264  { "M0G3105QRHBRQ1", 0x1349, 0x01},
265  { "M0G3106QDGS20RQ1", 0x94AD, 0x6F},
266  { "M0G3106QDGS28RQ1", 0x94AD, 0x03},
267  { "M0G3106QDGS32RQ1", 0x94AD, 0x8D},
268  { "M0G3106QPMRQ1", 0x54C7, 0x08},
269  { "M0G3106QPTRQ1", 0x54C7, 0x3F},
270  { "M0G3106QRGZRQ1", 0x94AD, 0xE6},
271  { "M0G3106QRHBRQ1", 0x94AD, 0x20},
272  { "M0G3107QDGS20RQ1", 0x4e2f, 0xfd},
273  { "M0G3107QDGS28RQ1", 0x4e2f, 0x67},
274  { "M0G3107QDGS28RQ1", 0x4e2f, 0xd5},
275  { "M0G3107QPMRQ1", 0x4e2f, 0x51 },
276  { "M0G3107QPTRQ1", 0x4e2f, 0xc7},
277  { "M0G3107QRGZRQ1", 0x4e2f, 0x8a },
278  { "M0G3107QRHBRQ1", 0x4e2f, 0x9a},
279  /* MSPM0G350x-Q1 */
280  { "M0G3505QDGS28RQ1", 0x704E, 0x4C },
281  { "M0G3505QDGS32RQ1", 0x704E, 0x7F },
282  { "M0G3505QPMRQ1", 0x704E, 0x7B },
283  { "M0G3505QPTRQ1", 0x704E, 0x9C },
284  { "M0G3505QRGZRQ1", 0x704E, 0xC9 },
285  { "M0G3505QRHBRQ1", 0x704E, 0x26 },
286  { "M0G3506QDGS28RQ1", 0xEE12, 0x71 },
287  { "M0G3506QDGS32RQ1", 0xEE12, 0x6C },
288  { "M0G3506QPMRQ1", 0xEE12, 0x7B },
289  { "M0G3506QPTRQ1", 0xEE12, 0x5A },
290  { "M0G3506QRGZRQ1", 0xEE12, 0xD2 },
291  { "M0G3506QRHBRQ1", 0xEE12, 0xFC },
292  { "M0G3507QDGS28RQ1", 0x34E0, 0xEA },
293  { "M0G3507QDGS32RQ1", 0x34E0, 0xF6 },
294  { "M0G3507QPMRQ1", 0x34E0, 0x26 },
295  { "M0G3507QRGZRQ1", 0x34E0, 0xC5 },
296  { "M0G3507QRHBRQ1", 0x34E0, 0xAC },
297  { "M0G3507QSPTRQ1", 0x34E0, 0xE3 },
298 };
299 
300 /* https://www.ti.com/lit/gpn/mspm0g1518 Table 8-21 and so on */
301 static const struct mspm0_part_info mspm0g_parts_bba9[] = {
302  /* MSPM0Gx51x */
303  { "MSPM0G1518SPMR", 0x2120, 0x13 },
304  { "MSPM0G1518SPNR", 0x2120, 0x16 },
305  { "MSPM0G1518SPTR", 0x2120, 0x12 },
306  { "MSPM0G1518SPZR", 0x2120, 0x18 },
307  { "MSPM0G1518SRGZR", 0x2120, 0x11 },
308  { "MSPM0G1518SRHBR", 0x2120, 0x10 },
309  { "MSPM0G1518SZAWR", 0x2120, 0x19 },
310  { "MSPM0G1519SPMR", 0x2407, 0x13 },
311  { "MSPM0G1519SPNR", 0x2407, 0x16 },
312  { "MSPM0G1519SPTR", 0x2407, 0x12 },
313  { "MSPM0G1519SPZR", 0x2407, 0x18 },
314  { "MSPM0G1519SRGZR", 0x2407, 0x11 },
315  { "MSPM0G1519SRHBR", 0x2407, 0x10 },
316  { "MSPM0G1519SZAWR", 0x2407, 0x19 },
317  { "MSPM0G3518SPMR", 0x1205, 0x13 },
318  { "MSPM0G3518SPNR", 0x1205, 0x15 },
319  { "MSPM0G3518SPTR", 0x1205, 0x12 },
320  { "MSPM0G3518SPZR", 0x1205, 0x16 },
321  { "MSPM0G3518SRGZR", 0x1205, 0x11 },
322  { "MSPM0G3518SRHBR", 0x1205, 0x10 },
323  { "MSPM0G3518SZAWR", 0x1205, 0x19 },
324  { "MSPM0G3519SPMR", 0x1508, 0x13 },
325  { "MSPM0G3519SPNR", 0x1508, 0x15 },
326  { "MSPM0G3519SPTR", 0x1508, 0x12 },
327  { "MSPM0G3519SPZR", 0x1508, 0x16 },
328  { "MSPM0G3519SRGZR", 0x1508, 0x11 },
329  { "MSPM0G3519SRHBR", 0x1508, 0x10 },
330  { "MSPM0G3519SZAWR", 0x1508, 0x19 },
331  /* MSPM0G351x-Q1 */
332  { "M0G3518QPMRQ1", 0x4009, 0x13 },
333  { "M0G3518QPNRQ1", 0x4009, 0x14 },
334  { "M0G3518QPTRQ1", 0x4009, 0x12 },
335  { "M0G3518QPZRQ1", 0x4009, 0x15 },
336  { "M0G3518QRGZRQ1", 0x4009, 0x11 },
337  { "M0G3518QRHBRQ1", 0x4009, 0x10 },
338  { "M0G3519QPMRQ1", 0x3512, 0x13 },
339  { "M0G3519AQPMRQ1", 0x3512, 0x16 },
340  { "M0G3519QPNRQ1", 0x3512, 0x14 },
341  { "M0G3519QPTRQ1", 0x3512, 0x12 },
342  { "M0G3519QPZRQ1", 0x3512, 0x15 },
343  { "M0G3519QRGZRQ1", 0x3512, 0x11 },
344  { "M0G3519QRHBRQ1", 0x3512, 0x10 },
345  /* MSPM0G352x-Q1 */
346  { "M0G3529QPMRQ1", 0xF8D1, 0x13 },
347 };
348 
349 /* https://www.ti.com/lit/gpn/mspm0g5187 Table 8-24*/
350 static const struct mspm0_part_info mspm0g_parts_bbbc[] = {
351  /* MSPM0G5187 */
352  { "MSPM0G5187S28YCJR", 0x5610, 0x18 },
353  { "MSPM0G5187SDGS20R", 0x5610, 0x16 },
354  { "MSPM0G5187SPMR", 0x5610, 0x10 },
355  { "MSPM0G5187SPTR", 0x5610, 0x11 },
356  { "MSPM0G5187SRHBR", 0x5610, 0x13 },
357  { "MSPM0G5187SRGER", 0x5610, 0x14 },
358  { "MSPM0G5187SRGZR", 0x5610, 0x12 },
359  { "MSPM0G5187SRUYR", 0x5610, 0x17 },
360 };
361 
362 /* https://www.ti.com/lit/gpn/mspm0c1104 Table 8-12 and so on */
363 static const struct mspm0_part_info mspm0c_parts[] = {
364  { "MSPS003F4SPW20R", 0x57b3, 0x70},
365  { "MSPM0C1104SDGS20R", 0x57b3, 0x71},
366  { "MSPM0C1104SRUKR", 0x57b3, 0x73},
367  { "MSPM0C1104SDYYR", 0x57b3, 0x75},
368  { "MSPM0C1104SDDFR", 0x57b3, 0x77},
369  { "MSPM0C1104SDSGR", 0x57b3, 0x79},
370 };
371 
372 /* https://www.ti.com/lit/gpn/MSPM0L2228 Table 8-16 and so on */
373 static const struct mspm0_part_info mspm0lx22x_parts[] = {
374  { "MSPM0L1227SRGER", 0x7C32, 0xF1},
375  { "MSPM0L1227SPTR", 0x7C32, 0xC9},
376  { "MSPM0L1227SPMR", 0x7C32, 0x1C},
377  { "MSPM0L1227SPNAR", 0x7C32, 0x91},
378  { "MSPM0L1227SPNR", 0x7C32, 0x39},
379  { "MSPM0L1228SRGER", 0x33F7, 0x13},
380  { "MSPM0L1228SRHBR", 0x33F7, 0x3A},
381  { "MSPM0L1228SRGZR", 0x33F7, 0xBC},
382  { "MSPM0L1228SPTR", 0x33F7, 0xF8},
383  { "MSPM0L1228SPMR", 0x33F7, 0xCE},
384  { "MSPM0L1228SPNAR", 0x33F7, 0x59},
385  { "MSPM0L1228SPNR", 0x33F7, 0x7},
386  { "MSPM0L2227SRGZR", 0x5E8F, 0x90},
387  { "MSPM0L2227SPTR", 0x5E8F, 0xA},
388  { "MSPM0L2227SPMR", 0x5E8F, 0x6D},
389  { "MSPM0L2227SPNAR", 0x5E8F, 0x24},
390  { "MSPM0L2227SPNR", 0x5E8F, 0x68},
391  { "MSPM0L2228SRGZR", 0x2C38, 0xB8},
392  { "MSPM0L2228SPTR", 0x2C38, 0x25},
393  { "MSPM0L2228SPMR", 0x2C38, 0x6E},
394  { "MSPM0L2228SPNAR", 0x2C38, 0x63},
395  { "MSPM0L2228SPNR", 0x2C38, 0x3C},
396 };
397 
398 static const struct mspm0_family_info mspm0_finf[] = {
399  { "MSPM0L", 0xbb82, ARRAY_SIZE(mspm0l_parts), mspm0l_parts },
400  { "MSPM0Lx22x", 0xbb9f, ARRAY_SIZE(mspm0lx22x_parts), mspm0lx22x_parts },
401  { "MSPM0G", 0xbb88, ARRAY_SIZE(mspm0g_parts_bb88), mspm0g_parts_bb88 },
402  { "MSPM0G", 0xbba9, ARRAY_SIZE(mspm0g_parts_bba9), mspm0g_parts_bba9 },
403  { "MSPM0G", 0xbbbc, ARRAY_SIZE(mspm0g_parts_bbbc), mspm0g_parts_bbbc },
404  { "MSPM0C", 0xbba1, ARRAY_SIZE(mspm0c_parts), mspm0c_parts },
405 };
406 
407 /*
408  * OpenOCD command interface
409  */
410 
411 /*
412  * flash_bank mspm0 <base> <size> 0 0 <target#>
413  */
414 FLASH_BANK_COMMAND_HANDLER(mspm0_flash_bank_command)
415 {
416  struct mspm0_flash_bank *mspm0_info;
417 
418  switch (bank->base) {
422  break;
423  default:
424  LOG_ERROR("Invalid bank address " TARGET_ADDR_FMT, bank->base);
425  return ERROR_FAIL;
426  }
427 
428  mspm0_info = calloc(1, sizeof(struct mspm0_flash_bank));
429  if (!mspm0_info) {
430  LOG_ERROR("%s: Out of memory for mspm0_info!", __func__);
431  return ERROR_FAIL;
432  }
433 
434  bank->driver_priv = mspm0_info;
435 
436  mspm0_info->sector_size = 0x400;
437 
438  return ERROR_OK;
439 }
440 
441 /*
442  * Chip identification and status
443  */
445 {
446  struct mspm0_flash_bank *mspm0_info = bank->driver_priv;
447 
448  if (mspm0_info->did == 0)
450 
452  "\nTI MSPM0 information: Chip is "
453  "%s rev %d Device Unique ID: 0x%" PRIu32 "\n",
454  mspm0_info->name, mspm0_info->version,
455  mspm0_info->traceid);
457  "main flash: %uKiB in %u bank(s), sram: %uKiB, data flash: %uKiB",
458  mspm0_info->main_flash_size_kb,
459  mspm0_info->main_flash_num_banks, mspm0_info->sram_size_kb,
460  mspm0_info->data_flash_size_kb);
461 
462  return ERROR_OK;
463 }
464 
465 /* Extract a bitfield helper */
466 static unsigned int mspm0_extract_val(unsigned int var, unsigned char hi, unsigned char lo)
467 {
468  return (var & GENMASK(hi, lo)) >> lo;
469 }
470 
472 {
473  struct mspm0_flash_bank *mspm0_info = bank->driver_priv;
474  struct target *target = bank->target;
475  const struct mspm0_family_info *minfo = NULL;
476 
477  /* Read and parse chip identification and flash version register */
478  uint32_t did;
479  int retval = target_read_u32(target, MSPM0_DID, &did);
480  if (retval != ERROR_OK) {
481  LOG_ERROR("Failed to read device ID");
482  return retval;
483  }
484  retval = target_read_u32(target, MSPM0_TRACEID, &mspm0_info->traceid);
485  if (retval != ERROR_OK) {
486  LOG_ERROR("Failed to read trace ID");
487  return retval;
488  }
489  uint32_t userid;
490  retval = target_read_u32(target, MSPM0_USERID, &userid);
491  if (retval != ERROR_OK) {
492  LOG_ERROR("Failed to read user ID");
493  return retval;
494  }
495  uint32_t flashram;
496  retval = target_read_u32(target, MSPM0_SRAMFLASH, &flashram);
497  if (retval != ERROR_OK) {
498  LOG_ERROR("Failed to read sramflash register");
499  return retval;
500  }
501  uint32_t flashdesc;
502  retval = target_read_u32(target, FCTL_REG_DESC, &flashdesc);
503  if (retval != ERROR_OK) {
504  LOG_ERROR("Failed to read flashctl description register");
505  return retval;
506  }
507 
508  unsigned char version = mspm0_extract_val(did, 31, 28);
509  unsigned short pnum = mspm0_extract_val(did, 27, 12);
510  unsigned char variant = mspm0_extract_val(userid, 23, 16);
511  unsigned short part = mspm0_extract_val(userid, 15, 0);
512  unsigned short manufacturer = mspm0_extract_val(did, 11, 1);
513 
514  /*
515  * Valid DIE and manufacturer ID?
516  * Check the ALWAYS_1 bit to be 1 and manufacturer to be 0x17. All MSPM0
517  * devices within the Device ID field of the factory constants will
518  * always read 0x17 as it is TI's JEDEC bank and company code. If 1
519  * and 0x17 is not read from their respective registers then it truly
520  * is not a MSPM0 device so we will return an error instead of
521  * going any further.
522  */
523  if (!(did & BIT(0)) || !(manufacturer & TI_MANUFACTURER_ID)) {
524  LOG_WARNING("Unknown Device ID[0x%" PRIx32 "], cannot identify target",
525  did);
526  LOG_DEBUG("did 0x%" PRIx32 ", traceid 0x%" PRIx32 ", userid 0x%" PRIx32
527  ", flashram 0x%" PRIx32, did, mspm0_info->traceid, userid,
528  flashram);
530  }
531 
532  /* Initialize master index selector and probe status*/
533  unsigned char minfo_idx = 0xff;
534  unsigned char probe_status = MSPM0_NO_ID_FOUND;
535 
536  /* Check if we at least know the family of devices */
537  for (unsigned int i = 0; i < ARRAY_SIZE(mspm0_finf); i++) {
538  if (mspm0_finf[i].part_num == pnum) {
539  minfo_idx = i;
540  minfo = &mspm0_finf[i];
541  probe_status = MSPM0_DEV_ID_FOUND;
542  break;
543  }
544  }
545 
546  /* Initialize part index selector*/
547  unsigned char pinfo_idx = 0xff;
548 
549  /*
550  * If we can identify the part number then we will attempt to identify
551  * the specific chip. Otherwise, if we do not know the part number then
552  * it would be useless to identify the specific chip.
553  */
554  if (probe_status == MSPM0_DEV_ID_FOUND) {
555  /* Can we specifically identify the chip */
556  for (unsigned int i = 0; i < minfo->part_count; i++) {
557  if (minfo->part_info[i].part == part
558  && minfo->part_info[i].variant == variant) {
559  pinfo_idx = i;
560  probe_status = MSPM0_DEV_PART_ID_FOUND;
561  break;
562  }
563  }
564  }
565 
566  /*
567  * We will check the status of our probe within this switch-case statement
568  * using these three scenarios.
569  *
570  * 1) Device, part, and variant ID is unknown.
571  * 2) Device ID is known but the part/variant ID is unknown.
572  * 3) Device ID and part/variant ID is known
573  *
574  * For scenario 1, we allow the user to continue because if the
575  * manufacturer matches TI's JEDEC value and ALWAYS_1 from the device ID
576  * field is correct then the assumption the user is using an MSPM0 device
577  * can be made.
578  */
579  switch (probe_status) {
580  case MSPM0_NO_ID_FOUND:
581  mspm0_info->name = "mspm0x";
582  LOG_INFO("Unidentified PART[0x%x]/variant[0x%x"
583  "], unknown DeviceID[0x%x"
584  "]. Attempting to proceed as %s.", part, variant, pnum,
585  mspm0_info->name);
586  break;
587  case MSPM0_DEV_ID_FOUND:
588  mspm0_info->name = mspm0_finf[minfo_idx].family_name;
589  LOG_INFO("Unidentified PART[0x%x]/variant[0x%x"
590  "], known DeviceID[0x%x"
591  "]. Attempting to proceed as %s.", part, variant, pnum,
592  mspm0_info->name);
593  break;
595  default:
596  mspm0_info->name = mspm0_finf[minfo_idx].part_info[pinfo_idx].part_name;
597  LOG_DEBUG("Part: %s detected", mspm0_info->name);
598  break;
599  }
600 
601  mspm0_info->did = did;
602  mspm0_info->version = version;
603  mspm0_info->data_flash_size_kb = mspm0_extract_val(flashram, 31, 26);
604  mspm0_info->main_flash_size_kb = mspm0_extract_val(flashram, 11, 0);
605  mspm0_info->main_flash_num_banks = mspm0_extract_val(flashram, 13, 12) + 1;
606  mspm0_info->sram_size_kb = mspm0_extract_val(flashram, 25, 16);
607  mspm0_info->flash_version = mspm0_extract_val(flashdesc, 15, 12);
608 
609  /*
610  * Hardcode flash_word_size unless we find some other pattern
611  * See section 7.7 (Foot note mentions the flash word size).
612  * almost all values seem to be 8 bytes, but if there are variance,
613  * then we should update mspm0_part_info structure with this info.
614  */
615  mspm0_info->flash_word_size_bytes = 8;
616 
617  LOG_DEBUG("Detected: main flash: %uKb in %u banks, sram: %uKb, data flash: %uKb",
618  mspm0_info->main_flash_size_kb, mspm0_info->main_flash_num_banks,
619  mspm0_info->sram_size_kb, mspm0_info->data_flash_size_kb);
620 
621  return ERROR_OK;
622 }
623 
624 /*
625  * Decode error values
626  */
627 static const struct {
628  const unsigned char bit_offset;
629  const char *fail_string;
631  { 2, "CMDINPROGRESS" },
632  { 4, "FAILWEPROT" },
633  { 5, "FAILVERIFY" },
634  { 6, "FAILILLADDR" },
635  { 7, "FAILMODE" },
636  { 12, "FAILMISC" },
637 };
638 
639 static const char *mspm0_fctl_translate_ret_err(unsigned int return_code)
640 {
641  for (unsigned int i = 0; i < ARRAY_SIZE(mspm0_fctl_fail_decode_strings); i++) {
642  if (return_code & BIT(mspm0_fctl_fail_decode_strings[i].bit_offset))
644  }
645 
646  /* If unknown error notify the user*/
647  return "FAILUNKNOWN";
648 }
649 
650 static int mspm0_fctl_get_sector_reg(struct flash_bank *bank, unsigned int addr,
651  unsigned int *reg, unsigned int *sector_mask)
652 {
653  struct mspm0_flash_bank *mspm0_info = bank->driver_priv;
654  struct target *target = bank->target;
655  int ret = ERROR_OK;
656  unsigned int sector_num = (addr >> 10);
657  unsigned int sector_in_bank = sector_num;
658  unsigned int phys_sector_num = sector_num;
659  uint32_t sysctl_sec_status;
660  unsigned int exec_upper_bank;
661 
662  /*
663  * If the device has dual banks we will need to check if it is configured
664  * to execute from the upper bank. In the scenario that we are executing
665  * from upper bank then we will need to protect it using CMDWEPROTA rather
666  * than CMDWEPROTB. We also need to take into account what sector
667  * we're using when going between banks.
668  */
669  if (mspm0_info->main_flash_num_banks > 1 &&
670  bank->base == MSPM0_FLASH_BASE_MAIN) {
671  ret = target_read_u32(target, SYSCTL_SECCFG_SECSTATUS, &sysctl_sec_status);
672  if (ret != ERROR_OK)
673  return ret;
674  exec_upper_bank = mspm0_extract_val(sysctl_sec_status, 12, 12);
675  if (exec_upper_bank) {
676  if (sector_num > (mspm0_info->main_flash_size_kb / 2)) {
677  phys_sector_num =
678  sector_num - (mspm0_info->main_flash_size_kb / 2);
679  } else {
680  phys_sector_num =
681  sector_num + (mspm0_info->main_flash_size_kb / 2);
682  }
683  }
684  sector_in_bank =
685  sector_num % (mspm0_info->main_flash_size_kb /
686  mspm0_info->main_flash_num_banks);
687  }
688 
689  /*
690  * NOTE: MSPM0 devices of version A will use CMDWEPROTA and CMDWEPROTB
691  * for MAIN flash. CMDWEPROTC is included in the TRM/DATASHEET but for
692  * all practical purposes, it is considered reserved. If the flash
693  * version on the device is version B, then we will only use
694  * CMDWEPROTB for MAIN and DATA flash if the device has it.
695  */
696  switch (bank->base) {
699  if (mspm0_info->flash_version < FCTL_FEATURE_VER_B) {
700  /* Use CMDWEPROTA */
701  if (phys_sector_num < 32) {
702  *sector_mask = BIT(phys_sector_num);
704  }
705 
706  /* Use CMDWEPROTB */
707  if (phys_sector_num >= 32 && sector_in_bank < 256) {
708  /* Dual bank system */
709  if (mspm0_info->main_flash_num_banks > 1)
710  *sector_mask = BIT(sector_in_bank / 8);
711  else /* Single bank system */
712  *sector_mask = BIT((sector_in_bank - 32) / 8);
714  }
715  } else {
716  *sector_mask = BIT((sector_in_bank / 8) % 32);
718  }
719  break;
721  *sector_mask = BIT(sector_num % 32);
723  break;
724  default:
725  /*
726  * Not expected to reach here due to check in mspm0_address_check()
727  * but adding it as another layer of safety.
728  */
730  break;
731  }
732 
733  if (ret != ERROR_OK)
734  LOG_ERROR("Unable to map sector protect reg for address 0x%08x", addr);
735 
736  return ret;
737 }
738 
739 static int mspm0_address_check(struct flash_bank *bank, unsigned int addr)
740 {
741  struct mspm0_flash_bank *mspm0_info = bank->driver_priv;
742  unsigned int flash_main_size = mspm0_info->main_flash_size_kb * 1024;
743  unsigned int flash_data_size = mspm0_info->data_flash_size_kb * 1024;
744  int ret = ERROR_FLASH_SECTOR_INVALID;
745 
746  /*
747  * Before unprotecting any memory lets make sure that the address and
748  * bank given is a known bank and whether or not the address falls under
749  * the proper bank.
750  */
751  switch (bank->base) {
753  if (addr <= (MSPM0_FLASH_BASE_MAIN + flash_main_size))
754  ret = ERROR_OK;
755  break;
758  ret = ERROR_OK;
759  break;
761  if (addr >= MSPM0_FLASH_BASE_DATA &&
762  addr <= (MSPM0_FLASH_BASE_DATA + flash_data_size))
763  ret = ERROR_OK;
764  break;
765  default:
767  break;
768  }
769 
770  return ret;
771 }
772 
773 static int mspm0_fctl_unprotect_sector(struct flash_bank *bank, unsigned int addr)
774 {
775  struct target *target = bank->target;
776  unsigned int reg = 0x0;
777  uint32_t sector_mask = 0x0;
778  int ret;
779 
780  ret = mspm0_address_check(bank, addr);
781  switch (ret) {
783  LOG_ERROR("Unable to map sector protect reg for address 0x%08x", addr);
784  break;
786  LOG_ERROR("Unable to determine which bank to use 0x%08x", addr);
787  break;
788  default:
789  mspm0_fctl_get_sector_reg(bank, addr, &reg, &sector_mask);
790  ret = target_write_u32(target, reg, ~sector_mask);
791  break;
792  }
793 
794  return ret;
795 }
796 
798  uint32_t addr,
799  uint32_t cmd,
800  uint32_t byte_en)
801 {
802  struct target *target = bank->target;
803 
804  /*
805  * Configure the flash operation within the CMDTYPE register, byte_en
806  * bits if needed, and then set the address where the flash operation
807  * will execute.
808  */
810  if (retval != ERROR_OK)
811  return retval;
812  if (byte_en != 0) {
813  retval = target_write_u32(target, FCTL_REG_CMDBYTEN, byte_en);
814  if (retval != ERROR_OK)
815  return retval;
816  }
817 
819 }
820 
822 {
823  struct target *target = bank->target;
824  uint32_t return_code = 0;
825  int64_t start_ms;
826  int64_t elapsed_ms;
827 
828  start_ms = timeval_ms();
829  while ((return_code & FCTL_STATCMD_CMDDONE_MASK) != FCTL_STATCMD_CMDDONE_STATDONE) {
830  int retval = target_read_u32(target, FCTL_REG_STATCMD, &return_code);
831  if (retval != ERROR_OK)
832  return retval;
833 
834  elapsed_ms = timeval_ms() - start_ms;
835  if (elapsed_ms > MSPM0_FLASH_TIMEOUT_MS)
836  break;
837 
838  keep_alive();
839  }
840 
842  LOG_ERROR("Flash command failed: %s", mspm0_fctl_translate_ret_err(return_code));
843  return ERROR_FAIL;
844  }
845 
846  return ERROR_OK;
847 }
848 
849 static int mspm0_fctl_sector_erase(struct flash_bank *bank, uint32_t addr)
850 {
851  struct target *target = bank->target;
852 
853  /*
854  * TRM Says:
855  * Note that the CMDWEPROTx registers are reset to a protected state
856  * at the end of all program and erase operations. These registers
857  * must be re-configured by software before a new operation is
858  * initiated.
859  *
860  * This means that as we start erasing sector by sector, the protection
861  * registers are reset and need to be unprotected *again* for the next
862  * erase operation. Unfortunately, this means that we cannot do a unitary
863  * unprotect operation independent of flash erase operation
864  */
865  int retval = mspm0_fctl_unprotect_sector(bank, addr);
866  if (retval != ERROR_OK) {
867  LOG_ERROR("Unprotecting sector of memory at address 0x%08" PRIx32
868  " failed", addr);
869  return retval;
870  }
871 
872  /* Actual erase operation */
873  retval = mspm0_fctl_cfg_command(bank, addr,
875  if (retval != ERROR_OK)
876  return retval;
878  if (retval != ERROR_OK)
879  return retval;
880 
882 }
883 
885 {
886  struct mspm0_flash_bank *mspm0_info = bank->driver_priv;
887 
888  if (mspm0_info->did == 0)
890 
891  /*
892  * TRM Says:
893  * Note that the CMDWEPROTx registers are reset to a protected state
894  * at the end of all program and erase operations. These registers
895  * must be re-configured by software before a new operation is
896  * initiated.
897  *
898  * This means that when any flash operation is performed at a block level,
899  * the block is locked back again. This prevents usage where we can set a
900  * protection level once at the flash level and then do erase / write
901  * operation without touching the protection register (since it is
902  * reset by hardware automatically). In effect, we cannot use the hardware
903  * defined protection scheme in openOCD.
904  *
905  * To deal with this protection scheme, the CMDWEPROTx register that
906  * correlates to the sector is modified at the time of operation and as far
907  * openOCD is concerned, the flash operates as completely un-protected
908  * flash.
909  */
910  for (unsigned int i = 0; i < bank->num_sectors; i++)
911  bank->sectors[i].is_protected = 0;
912 
913  return ERROR_OK;
914 }
915 
916 static int mspm0_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
917 {
918  struct target *target = bank->target;
919  struct mspm0_flash_bank *mspm0_info = bank->driver_priv;
920  int retval = ERROR_OK;
921  uint32_t protect_reg_cache[MSPM0_MAX_PROTREGS];
922 
923  if (bank->target->state != TARGET_HALTED) {
924  LOG_ERROR("Please halt target for erasing flash");
926  }
927 
928  if (mspm0_info->did == 0)
930 
931  /* Pick a copy of the current protection config for later restoration */
932  for (unsigned int i = 0; i < mspm0_info->protect_reg_count; i++) {
933  retval = target_read_u32(target,
934  mspm0_info->protect_reg_base + (i * 4),
935  &protect_reg_cache[i]);
936  if (retval != ERROR_OK) {
937  LOG_ERROR("Failed saving flashctl protection status");
938  return retval;
939  }
940  }
941 
942  switch (bank->base) {
944  for (unsigned int csa = first; csa <= last; csa++) {
945  unsigned int addr = csa * mspm0_info->sector_size;
946  retval = mspm0_fctl_sector_erase(bank, addr);
947  if (retval != ERROR_OK)
948  LOG_ERROR("Sector erase on MAIN failed at address 0x%08x "
949  "(sector: %u)", addr, csa);
950  }
951  break;
954  if (retval != ERROR_OK)
955  LOG_ERROR("Sector erase on NONMAIN failed");
956  break;
958  for (unsigned int csa = first; csa <= last; csa++) {
959  unsigned int addr = (MSPM0_FLASH_BASE_DATA +
960  (csa * mspm0_info->sector_size));
961  retval = mspm0_fctl_sector_erase(bank, addr);
962  if (retval != ERROR_OK)
963  LOG_ERROR("Sector erase on DATA bank failed at address 0x%08x "
964  "(sector: %u)", addr, csa);
965  }
966  break;
967  default:
968  LOG_ERROR("Invalid memory region access");
969  retval = ERROR_FLASH_BANK_INVALID;
970  break;
971  }
972 
973  /* If there were any issues in our checks, return the error */
974  if (retval != ERROR_OK)
975  return retval;
976 
977  /*
978  * TRM Says:
979  * Note that the CMDWEPROTx registers are reset to a protected state
980  * at the end of all program and erase operations. These registers
981  * must be re-configured by software before a new operation is
982  * initiated
983  * Let us just Dump the protection registers back to the system.
984  * That way we retain the protection status as requested by the user
985  */
986  for (unsigned int i = 0; i < mspm0_info->protect_reg_count; i++) {
987  retval = target_write_u32(target, mspm0_info->protect_reg_base + (i * 4),
988  protect_reg_cache[i]);
989  if (retval != ERROR_OK) {
990  LOG_ERROR("Failed re-applying protection status of flashctl");
991  return retval;
992  }
993  }
994 
995  return retval;
996 }
997 
998 static int mspm0_write(struct flash_bank *bank, const unsigned char *buffer,
999  unsigned int offset, unsigned int count)
1000 {
1001  struct target *target = bank->target;
1002  struct mspm0_flash_bank *mspm0_info = bank->driver_priv;
1003  uint32_t protect_reg_cache[MSPM0_MAX_PROTREGS];
1004  int retval;
1005 
1006  /*
1007  * XXX: TRM Says:
1008  * The number of program operations applied to a given word line must be
1009  * monitored to ensure that the maximum word line program limit before
1010  * erase is not violated.
1011  *
1012  * There is no reasonable way we can maintain that state in OpenOCD. So,
1013  * Let the manufacturing path figure this out.
1014  */
1015 
1016  if (bank->target->state != TARGET_HALTED) {
1017  LOG_ERROR("Please halt target for programming flash");
1018  return ERROR_TARGET_NOT_HALTED;
1019  }
1020 
1021  if (mspm0_info->did == 0)
1023 
1024  /*
1025  * Pick a copy of the current protection config for later restoration
1026  * We need to restore these regs after every write, so instead of trying
1027  * to figure things out on the fly, we just context save and restore
1028  */
1029  for (unsigned int i = 0; i < mspm0_info->protect_reg_count; i++) {
1030  retval = target_read_u32(target,
1031  mspm0_info->protect_reg_base + (i * 4),
1032  &protect_reg_cache[i]);
1033  if (retval != ERROR_OK) {
1034  LOG_ERROR("Failed saving flashctl protection status");
1035  return retval;
1036  }
1037  }
1038 
1039  /* Add proper memory offset for bank being written to */
1040  unsigned int addr = bank->base + offset;
1041 
1042  while (count) {
1043  unsigned int num_bytes_to_write;
1044  uint32_t bytes_en;
1045 
1046  /*
1047  * If count is not 64 bit aligned, we will do byte wise op to keep things simple
1048  * Usually this might mean we need to additional write ops towards
1049  * trailing edge, but that is a tiny penalty for image downloads.
1050  * NOTE: we are going to assume the device does not support multi-word
1051  * programming - there does not seem to be discoverability!
1052  */
1053  if (count < mspm0_info->flash_word_size_bytes)
1054  num_bytes_to_write = count;
1055  else
1056  num_bytes_to_write = mspm0_info->flash_word_size_bytes;
1057 
1058  /* Data bytes to write */
1059  bytes_en = (1 << num_bytes_to_write) - 1;
1060  /* ECC chunks to write */
1061  switch (mspm0_info->flash_word_size_bytes) {
1062  case 8:
1063  bytes_en |= BIT(8);
1064  break;
1065  case 16:
1066  bytes_en |= BIT(16);
1067  bytes_en |= (num_bytes_to_write > 8) ? BIT(17) : 0;
1068  break;
1069  default:
1070  LOG_ERROR("Invalid flash_word_size_bytes %d",
1071  mspm0_info->flash_word_size_bytes);
1072  return ERROR_FAIL;
1073  }
1074 
1075  retval = mspm0_fctl_cfg_command(bank, addr,
1077  bytes_en);
1078  if (retval != ERROR_OK)
1079  return retval;
1080 
1082  if (retval != ERROR_OK)
1083  return retval;
1084 
1085  retval = target_write_buffer(target, FCTL_REG_CMDDATA0, num_bytes_to_write, buffer);
1086  if (retval != ERROR_OK)
1087  return retval;
1088 
1089  addr += num_bytes_to_write;
1090  buffer += num_bytes_to_write;
1091  count -= num_bytes_to_write;
1092 
1094  if (retval != ERROR_OK)
1095  return retval;
1096 
1097  retval = mspm0_fctl_wait_cmd_ok(bank);
1098  if (retval != ERROR_OK)
1099  return retval;
1100  }
1101 
1102  /*
1103  * TRM Says:
1104  * Note that the CMDWEPROTx registers are reset to a protected state
1105  * at the end of all program and erase operations. These registers
1106  * must be re-configured by software before a new operation is
1107  * initiated
1108  * Let us just Dump the protection registers back to the system.
1109  * That way we retain the protection status as requested by the user
1110  */
1111  for (unsigned int i = 0; i < mspm0_info->protect_reg_count; i++) {
1112  retval = target_write_u32(target,
1113  mspm0_info->protect_reg_base + (i * 4),
1114  protect_reg_cache[i]);
1115  if (retval != ERROR_OK) {
1116  LOG_ERROR("Failed re-applying protection status of flashctl");
1117  return retval;
1118  }
1119  }
1120 
1121  return ERROR_OK;
1122 }
1123 
1124 static int mspm0_probe(struct flash_bank *bank)
1125 {
1126  struct mspm0_flash_bank *mspm0_info = bank->driver_priv;
1127 
1128  /*
1129  * If this is a mspm0 chip, it has flash; probe() is just
1130  * to figure out how much is present. Only do it once.
1131  */
1132  if (mspm0_info->did != 0)
1133  return ERROR_OK;
1134 
1135  /*
1136  * mspm0_read_part_info() already handled error checking and
1137  * reporting. Note that it doesn't write, so we don't care about
1138  * whether the target is halted or not.
1139  */
1140  int retval = mspm0_read_part_info(bank);
1141  if (retval != ERROR_OK)
1142  return retval;
1143 
1144  if (bank->sectors) {
1145  free(bank->sectors);
1146  bank->sectors = NULL;
1147  }
1148 
1149  bank->write_start_alignment = 4;
1150  bank->write_end_alignment = 4;
1151 
1152  switch (bank->base) {
1154  bank->size = 1024;
1155  bank->num_sectors = 0x1;
1156  mspm0_info->protect_reg_base = FCTL_REG_CMDWEPROTNM;
1157  mspm0_info->protect_reg_count = 1;
1158  break;
1159  case MSPM0_FLASH_BASE_MAIN:
1160  bank->size = (mspm0_info->main_flash_size_kb * 1024);
1161  bank->num_sectors = bank->size / mspm0_info->sector_size;
1162  /*
1163  * If the feature version bit read from the FCTL_REG_DESC is
1164  * greater than or equal to 0xA then it means that the device
1165  * will exclusively use CMDWEPROTB ONLY for MAIN memory protection
1166  */
1167  if (mspm0_info->flash_version >= FCTL_FEATURE_VER_B) {
1168  mspm0_info->protect_reg_base = FCTL_REG_CMDWEPROTB;
1169  mspm0_info->protect_reg_count = 1;
1170  } else {
1171  mspm0_info->protect_reg_base = FCTL_REG_CMDWEPROTA;
1172  mspm0_info->protect_reg_count = 3;
1173  }
1174  break;
1175  case MSPM0_FLASH_BASE_DATA:
1176  if (!mspm0_info->data_flash_size_kb) {
1177  LOG_INFO("Data region NOT available!");
1178  bank->size = 0x0;
1179  bank->num_sectors = 0x0;
1180  return ERROR_OK;
1181  }
1182  /*
1183  * Any MSPM0 device containing data bank will have a flashctl
1184  * feature version of 0xA or higher. Since data bank is treated
1185  * like MAIN memory, it will also exclusively use CMDWEPROTB for
1186  * protection.
1187  */
1188  bank->size = (mspm0_info->data_flash_size_kb * 1024);
1189  bank->num_sectors = bank->size / mspm0_info->sector_size;
1190  mspm0_info->protect_reg_base = FCTL_REG_CMDWEPROTB;
1191  mspm0_info->protect_reg_count = 1;
1192  break;
1193  default:
1194  LOG_ERROR("Invalid bank address " TARGET_ADDR_FMT,
1195  bank->base);
1196  return ERROR_FAIL;
1197  }
1198 
1199  bank->sectors = calloc(bank->num_sectors, sizeof(struct flash_sector));
1200  if (!bank->sectors) {
1201  LOG_ERROR("Out of memory for sectors!");
1202  return ERROR_FAIL;
1203  }
1204  for (unsigned int i = 0; i < bank->num_sectors; i++) {
1205  bank->sectors[i].offset = i * mspm0_info->sector_size;
1206  bank->sectors[i].size = mspm0_info->sector_size;
1207  bank->sectors[i].is_erased = -1;
1208  }
1209 
1210  return ERROR_OK;
1211 }
1212 
1213 const struct flash_driver mspm0_flash = {
1214  .name = "mspm0",
1215  .flash_bank_command = mspm0_flash_bank_command,
1216  .erase = mspm0_erase,
1217  .protect = NULL,
1218  .write = mspm0_write,
1219  .read = default_flash_read,
1220  .probe = mspm0_probe,
1221  .auto_probe = mspm0_probe,
1222  .erase_check = default_flash_blank_check,
1223  .protect_check = mspm0_protect_check,
1224  .info = get_mspm0_info,
1225  .free_driver_priv = default_flash_free_driver_priv,
1226 };
#define GENMASK(h, l)
Definition: bits.h:24
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_BANK_INVALID
Definition: flash/common.h:28
#define ERROR_FLASH_SECTOR_INVALID
Definition: flash/common.h:29
#define ERROR_FLASH_BANK_NOT_PROBED
Definition: flash/common.h:35
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
#define ERROR_FLASH_DST_OUT_OF_BANK
Definition: flash/common.h:31
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:433
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#define FCTL_CMDEXEC_VAL_EXECUTE
Definition: mspm0.c:58
static int mspm0_fctl_wait_cmd_ok(struct flash_bank *bank)
Definition: mspm0.c:821
#define MSPM0_DEV_PART_ID_FOUND
Definition: mspm0.c:86
static const struct mspm0_part_info mspm0g_parts_bb88[]
Definition: mspm0.c:194
#define FCTL_CMDTYPE_SIZE_SECTOR
Definition: mspm0.c:66
#define FCTL_REG_STATCMD
Definition: mspm0.c:44
#define MSPM0_FLASH_TIMEOUT_MS
Definition: mspm0.c:73
#define FCTL_STATCMD_CMDDONE_MASK
Definition: mspm0.c:47
static const char * mspm0_fctl_translate_ret_err(unsigned int return_code)
Definition: mspm0.c:639
#define MSPM0_DID
Definition: mspm0.c:29
#define MSPM0_FLASH_BASE_MAIN
Definition: mspm0.c:23
#define FCTL_REG_CMDTYPE
Definition: mspm0.c:37
static int get_mspm0_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: mspm0.c:444
static int mspm0_write(struct flash_bank *bank, const unsigned char *buffer, unsigned int offset, unsigned int count)
Definition: mspm0.c:998
static const struct mspm0_part_info mspm0lx22x_parts[]
Definition: mspm0.c:373
const struct flash_driver mspm0_flash
Definition: mspm0.c:1213
static unsigned int mspm0_extract_val(unsigned int var, unsigned char hi, unsigned char lo)
Definition: mspm0.c:466
#define FCTL_REG_DESC
Definition: mspm0.c:35
#define MSPM0_MAX_PROTREGS
Definition: mspm0.c:71
#define FCTL_REG_CMDBYTEN
Definition: mspm0.c:39
#define MSPM0_USERID
Definition: mspm0.c:30
#define FCTL_STATCMD_CMDPASS_MASK
Definition: mspm0.c:51
static int mspm0_fctl_get_sector_reg(struct flash_bank *bank, unsigned int addr, unsigned int *reg, unsigned int *sector_mask)
Definition: mspm0.c:650
static const struct mspm0_family_info mspm0_finf[]
Definition: mspm0.c:398
#define MSPM0_FLASH_END_NONMAIN
Definition: mspm0.c:22
static const struct mspm0_part_info mspm0l_parts[]
Definition: mspm0.c:130
const unsigned char bit_offset
Definition: mspm0.c:628
static int mspm0_fctl_unprotect_sector(struct flash_bank *bank, unsigned int addr)
Definition: mspm0.c:773
#define MSPM0_TRACEID
Definition: mspm0.c:28
static int mspm0_fctl_cfg_command(struct flash_bank *bank, uint32_t addr, uint32_t cmd, uint32_t byte_en)
Definition: mspm0.c:797
static int mspm0_protect_check(struct flash_bank *bank)
Definition: mspm0.c:884
#define FCTL_REG_CMDADDR
Definition: mspm0.c:38
static const struct mspm0_part_info mspm0g_parts_bba9[]
Definition: mspm0.c:301
#define FCTL_REG_CMDWEPROTB
Definition: mspm0.c:42
#define FCTL_CMDTYPE_COMMAND_ERASE
Definition: mspm0.c:62
#define FCTL_REG_CMDEXEC
Definition: mspm0.c:36
FLASH_BANK_COMMAND_HANDLER(mspm0_flash_bank_command)
Definition: mspm0.c:414
static const struct mspm0_part_info mspm0c_parts[]
Definition: mspm0.c:363
#define MSPM0_NO_ID_FOUND
Definition: mspm0.c:84
#define TI_MANUFACTURER_ID
Definition: mspm0.c:81
#define MSPM0_FLASH_BASE_DATA
Definition: mspm0.c:24
const char * fail_string
Definition: mspm0.c:629
#define FCTL_FEATURE_VER_B
Definition: mspm0.c:69
static const struct mspm0_part_info mspm0g_parts_bbbc[]
Definition: mspm0.c:350
#define FCTL_STATCMD_CMDPASS_STATPASS
Definition: mspm0.c:52
#define FCTL_STATCMD_CMDDONE_STATDONE
Definition: mspm0.c:48
static const struct @12 mspm0_fctl_fail_decode_strings[]
static int mspm0_probe(struct flash_bank *bank)
Definition: mspm0.c:1124
#define MSPM0_SRAMFLASH
Definition: mspm0.c:31
#define FCTL_REG_CMDWEPROTA
Definition: mspm0.c:41
#define MSPM0_FLASH_BASE_NONMAIN
Definition: mspm0.c:21
#define MSPM0_DEV_ID_FOUND
Definition: mspm0.c:85
static int mspm0_read_part_info(struct flash_bank *bank)
Definition: mspm0.c:471
#define SYSCTL_SECCFG_SECSTATUS
Definition: mspm0.c:78
#define FCTL_CMDTYPE_SIZE_ONEWORD
Definition: mspm0.c:65
static int mspm0_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: mspm0.c:916
static int mspm0_address_check(struct flash_bank *bank, unsigned int addr)
Definition: mspm0.c:739
#define FCTL_CMDTYPE_COMMAND_PROGRAM
Definition: mspm0.c:61
#define FCTL_REG_CMDWEPROTNM
Definition: mspm0.c:43
static int mspm0_fctl_sector_erase(struct flash_bank *bank, uint32_t addr)
Definition: mspm0.c:849
#define FCTL_REG_CMDDATA0
Definition: mspm0.c:40
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
#define BIT(nr)
Definition: stm32l4x.h:18
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
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
const char * family_name
Definition: mspm0.c:123
unsigned short part_num
Definition: mspm0.c:124
const struct mspm0_part_info * part_info
Definition: mspm0.c:126
unsigned char part_count
Definition: mspm0.c:125
unsigned int main_flash_num_banks
Definition: mspm0.c:100
unsigned int protect_reg_base
Definition: mspm0.c:109
unsigned int data_flash_size_kb
Definition: mspm0.c:98
uint32_t traceid
Definition: mspm0.c:92
unsigned int protect_reg_count
Definition: mspm0.c:110
unsigned int main_flash_size_kb
Definition: mspm0.c:99
uint32_t did
Definition: mspm0.c:90
unsigned char flash_version
Definition: mspm0.c:113
unsigned int sram_size_kb
Definition: mspm0.c:103
unsigned int sector_size
Definition: mspm0.c:101
unsigned char version
Definition: mspm0.c:93
const char * name
Definition: mspm0.c:95
unsigned char flash_word_size_bytes
Definition: mspm0.c:106
unsigned char variant
Definition: mspm0.c:119
unsigned short part
Definition: mspm0.c:118
const char * part_name
Definition: mspm0.c:117
Definition: register.h:111
Definition: target.h:119
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2359
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2625
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2551
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:795
@ TARGET_HALTED
Definition: target.h:58
int64_t timeval_ms(void)
#define TARGET_ADDR_FMT
Definition: types.h:286
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22