Select one of the symbols to view example projects that use it.
 
Outline
#include "config.h"
#include <helper/time_support.h>
#include <helper/bits.h>
#include <target/target.h>
#include <flash/nor/core.h>
#include <flash/nor/driver.h>
#include <flash/nor/spi.h>
#define ISPICFG
#define ISPISTS
#define ISPIADDR
#define ISPICMD
#define ISPIDAT
#define ISPISTS_BUSY
#define STATUS1_QE
#define CFG_READ
#define CFG_WRITE
#define ISPI_CTRL_BASE
ene_flash_device
eneispif_flash_bank
eneispif_flash_bank_command(struct command_invocation *, struct flash_bank *)
eneispif_read_reg(struct flash_bank *, uint32_t *, target_addr_t)
eneispif_write_reg(struct flash_bank *, target_addr_t, uint32_t)
eneispif_wait(struct flash_bank *)
eneispi_erase_sector(struct flash_bank *, int)
eneispif_erase(struct flash_bank *, unsigned int, unsigned int)
eneispif_protect(struct flash_bank *, int, unsigned int, unsigned int)
eneispif_write(struct flash_bank *, const uint8_t *, uint32_t, uint32_t)
eneispif_read_flash_id(struct flash_bank *, uint32_t *)
eneispif_probe(struct flash_bank *)
eneispif_auto_probe(struct flash_bank *)
eneispif_protect_check(struct flash_bank *)
get_eneispif_info(struct flash_bank *, struct command_invocation *)
eneispif_flash
Files
loading...
SourceVuDevelopment ToolsOpenOCDsrc/flash/nor/eneispif.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2024 ENE Technology Inc. * steven@ene.com.tw *//* ... */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <helper/time_support.h> #include <helper/bits.h> #include <target/target.h> #include <flash/nor/core.h> #include <flash/nor/driver.h> #include <flash/nor/spi.h> 6 includes #define ISPICFG 0x0000 #define ISPISTS 0x0004 #define ISPIADDR 0x0008 #define ISPICMD 0x000C #define ISPIDAT 0x0100 #define ISPISTS_BUSY BIT(0) #define STATUS1_QE BIT(1) #define CFG_READ 0x372 #define CFG_WRITE 0x371 #define ISPI_CTRL_BASE 0x50101000 10 defines /* name read qread page erase chip device_id page erase flash * _cmd _cmd _prog _cmd* _erase size size* size * _cmd _cmd *//* ... */ struct flash_device ene_flash_device = FLASH_ID("ISPI flash", 0x03, 0x00, 0x02, 0x20, 0x60, 0x00132085, 0x100, 0x1000, 0x80000); struct eneispif_flash_bank { bool probed; target_addr_t ctrl_base; uint32_t dev_id; const struct flash_device *dev; ...}; FLASH_BANK_COMMAND_HANDLER(eneispif_flash_bank_command) { struct eneispif_flash_bank *eneispif_info; LOG_DEBUG("%s", __func__); if (CMD_ARGC < 6) return ERROR_COMMAND_SYNTAX_ERROR; eneispif_info = malloc(sizeof(struct eneispif_flash_bank)); if (!eneispif_info) { LOG_ERROR("not enough memory"); return ERROR_FAIL; }if (!eneispif_info) { ... } bank->driver_priv = eneispif_info; eneispif_info->probed = false; eneispif_info->ctrl_base = ISPI_CTRL_BASE; if (CMD_ARGC >= 7) { COMMAND_PARSE_ADDRESS(CMD_ARGV[6], eneispif_info->ctrl_base); LOG_INFO("ASSUMING ISPI device at ctrl_base = " TARGET_ADDR_FMT, eneispif_info->ctrl_base); }if (CMD_ARGC >= 7) { ... } return ERROR_OK; }{ ... } static int eneispif_read_reg(struct flash_bank *bank, uint32_t *value, target_addr_t address) { struct target *target = bank->target; struct eneispif_flash_bank *eneispif_info = bank->driver_priv; int result = target_read_u32(target, eneispif_info->ctrl_base + address, value); if (result != ERROR_OK) { LOG_ERROR("%s error at " TARGET_ADDR_FMT, __func__, eneispif_info->ctrl_base + address); return result; }if (result != ERROR_OK) { ... } LOG_DEBUG("Read address " TARGET_ADDR_FMT " = 0x%" PRIx32, eneispif_info->ctrl_base + address, *value); return ERROR_OK; }{ ... } static int eneispif_write_reg(struct flash_bank *bank, target_addr_t address, uint32_t value) { struct target *target = bank->target; struct eneispif_flash_bank *eneispif_info = bank->driver_priv; LOG_DEBUG("Write address " TARGET_ADDR_FMT " = 0x%" PRIx32, eneispif_info->ctrl_base + address, value); int result = target_write_u32(target, eneispif_info->ctrl_base + address, value); if (result != ERROR_OK) { LOG_ERROR("%s error writing 0x%" PRIx32 " to " TARGET_ADDR_FMT, __func__, value, eneispif_info->ctrl_base + address); return result; }if (result != ERROR_OK) { ... } return ERROR_OK; }{ ... } static int eneispif_wait(struct flash_bank *bank) { int64_t start = timeval_ms(); while (1) { uint32_t status; if (eneispif_read_reg(bank, &status, ISPISTS) != ERROR_OK) return ERROR_FAIL; if (!(status & ISPISTS_BUSY)) break; int64_t now = timeval_ms(); if (now - start > 1000) { LOG_ERROR("Busy more than 1000ms."); return ERROR_TARGET_TIMEOUT; }if (now - start > 1000) { ... } }while (1) { ... } return ERROR_OK; }{ ... } static int eneispi_erase_sector(struct flash_bank *bank, int sector) { int retval = ERROR_OK; struct eneispif_flash_bank *eneispif_info = bank->driver_priv; uint32_t offset; uint32_t conf; retval = eneispif_read_reg(bank, &conf, ISPICFG); if (retval != ERROR_OK) return retval; offset = bank->sectors[sector].offset; retval = eneispif_write_reg(bank, ISPIADDR, offset); /* Address */ if (retval != ERROR_OK) goto done; eneispif_write_reg(bank, ISPICFG, CFG_WRITE); /* Cmmmand enable */ eneispif_write_reg(bank, ISPICMD, SPIFLASH_WRITE_ENABLE); /* Write enable */ retval = eneispif_write_reg(bank, ISPICMD, eneispif_info->dev->erase_cmd); /* Erase page */ if (retval != ERROR_OK) goto done; retval = eneispif_wait(bank); if (retval != ERROR_OK) goto done; done: eneispif_write_reg(bank, ISPICFG, conf); /* restore */ return retval; }{ ... } static int eneispif_erase(struct flash_bank *bank, unsigned int first, unsigned int last) { struct target *target = bank->target; struct eneispif_flash_bank *eneispif_info = bank->driver_priv; int retval = ERROR_OK; LOG_DEBUG("%s: from sector %u to sector %u", __func__, first, last); if (target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); return ERROR_TARGET_NOT_HALTED; }if (target->state != TARGET_HALTED) { ... } if (last < first || last >= bank->num_sectors) { LOG_ERROR("Flash sector invalid"); return ERROR_FLASH_SECTOR_INVALID; }if (last < first || last >= bank->num_sectors) { ... } if (!(eneispif_info->probed)) { LOG_ERROR("Flash bank not probed"); return ERROR_FLASH_BANK_NOT_PROBED; }if (!(eneispif_info->probed)) { ... } for (unsigned int sector = first; sector <= last; sector++) { if (bank->sectors[sector].is_protected) { LOG_ERROR("Flash sector %u protected", sector); return ERROR_FAIL; }if (bank->sectors[sector].is_protected) { ... } }for (unsigned int sector = first; sector <= last; sector++) { ... } if (eneispif_info->dev->erase_cmd == 0x00) return ERROR_FLASH_OPER_UNSUPPORTED; for (unsigned int sector = first; sector <= last; sector++) { retval = eneispi_erase_sector(bank, sector); if (retval != ERROR_OK) break; }for (unsigned int sector = first; sector <= last; sector++) { ... } return retval; }{ ... } static int eneispif_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last) { for (unsigned int sector = first; sector <= last; sector++) bank->sectors[sector].is_protected = set; return ERROR_OK; }{ ... } static int eneispif_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count) { struct target *target = bank->target; struct eneispif_flash_bank *eneispif_info = bank->driver_priv; uint32_t page_size; uint32_t conf; int retval = ERROR_OK; LOG_DEBUG("bank->size=0x%x offset=0x%08" PRIx32 " count=0x%08" PRIx32, bank->size, offset, count); if (target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); return ERROR_TARGET_NOT_HALTED; }if (target->state != TARGET_HALTED) { ... } if (offset + count > eneispif_info->dev->size_in_bytes) { LOG_WARNING("Write past end of flash. Extra data discarded."); count = eneispif_info->dev->size_in_bytes - offset; }if (offset + count > eneispif_info->dev->size_in_bytes) { ... } /* Check sector protection */ for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { /* Start offset in or before this sector? */ /* End offset in or behind this sector? */ if ((offset < (bank->sectors[sector].offset + bank->sectors[sector].size)) && ((offset + count - 1) >= bank->sectors[sector].offset) && bank->sectors[sector].is_protected) { LOG_ERROR("Flash sector %u protected", sector); return ERROR_FAIL; }if ((offset < (bank->sectors[sector].offset + bank->sectors[sector].size)) && ((offset + count - 1) >= bank->sectors[sector].offset) && bank->sectors[sector].is_protected) { ... } }for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ... } retval = eneispif_read_reg(bank, &conf, ISPICFG); if (retval != ERROR_OK) return retval; eneispif_write_reg(bank, ISPICFG, CFG_WRITE); // Cmmmand enable /* If no valid page_size, use reasonable default. */ page_size = eneispif_info->dev->pagesize ? eneispif_info->dev->pagesize : SPIFLASH_DEF_PAGESIZE; uint32_t page_offset = offset % page_size; while (count > 0) { uint32_t cur_count; /* clip block at page boundary */ if (page_offset + count > page_size) cur_count = page_size - page_offset; else cur_count = count; eneispif_write_reg(bank, ISPICMD, SPIFLASH_WRITE_ENABLE); /* Write enable */ target_write_buffer(target, eneispif_info->ctrl_base + ISPIDAT, cur_count, buffer); eneispif_write_reg(bank, ISPIADDR, offset); retval = eneispif_write_reg(bank, ISPICMD, (cur_count << 16) | eneispif_info->dev->pprog_cmd); if (retval != ERROR_OK) goto err; page_offset = 0; buffer += cur_count; offset += cur_count; count -= cur_count; retval = eneispif_wait(bank); if (retval != ERROR_OK) goto err; }while (count > 0) { ... } err: eneispif_write_reg(bank, ISPICFG, conf); /* restore */ return retval; }{ ... } /* Return ID of flash device */ /* On exit, SW mode is kept */ static int eneispif_read_flash_id(struct flash_bank *bank, uint32_t *id) { struct eneispif_flash_bank *eneispif_info = bank->driver_priv; struct target *target = bank->target; int retval; uint32_t conf, value; uint8_t buffer[4]; if (target->state != TARGET_HALTED) { LOG_ERROR("Target not halted"); return ERROR_TARGET_NOT_HALTED; }if (target->state != TARGET_HALTED) { ... } retval = eneispif_read_reg(bank, &conf, ISPICFG); if (retval != ERROR_OK) return retval; LOG_DEBUG("ISPCFG = (0x%08" PRIx32 ")", conf); /* read ID from Receive Register */ eneispif_write_reg(bank, ISPICFG, CFG_WRITE); /* Cmmmand enable */ retval = eneispif_write_reg(bank, ISPICMD, (3 << 16) | SPIFLASH_READ_ID); if (retval != ERROR_OK) goto done; retval = eneispif_wait(bank); if (retval != ERROR_OK) goto done; retval = target_read_buffer(target, eneispif_info->ctrl_base + ISPIDAT, 3, buffer); if (retval != ERROR_OK) goto done; value = (buffer[2] << 16) | (buffer[1] << 8) | buffer[0]; LOG_DEBUG("ISPDAT = (0x%08" PRIx32 ")", value); *id = value; done: eneispif_write_reg(bank, ISPICFG, conf); // restore return retval; }{ ... } static int eneispif_probe(struct flash_bank *bank) { struct eneispif_flash_bank *eneispif_info = bank->driver_priv; struct flash_sector *sectors; uint32_t id; int retval; uint32_t sectorsize; if (eneispif_info->probed) free(bank->sectors); eneispif_info->probed = false; LOG_INFO("Assuming ISPI flash at address " TARGET_ADDR_FMT " with controller at " TARGET_ADDR_FMT, bank->base, eneispif_info->ctrl_base); eneispif_write_reg(bank, ISPICFG, CFG_READ); /* RAM map enable */ retval = eneispif_read_flash_id(bank, &id); if (retval != ERROR_OK) return retval; eneispif_info->dev_id = id; eneispif_info->dev = &ene_flash_device; LOG_INFO("Found flash device \'%s\' (ID 0x%08" PRIx32 ")", eneispif_info->dev->name, eneispif_info->dev_id); /* Set correct size value */ bank->size = eneispif_info->dev->size_in_bytes; if (bank->size <= (1UL << 16)) LOG_WARNING("device needs 2-byte addresses - not implemented"); /* if no sectors, treat whole bank as single sector */ sectorsize = eneispif_info->dev->sectorsize ? eneispif_info->dev->sectorsize : eneispif_info->dev->size_in_bytes; /* create and fill sectors array */ bank->num_sectors = eneispif_info->dev->size_in_bytes / sectorsize; sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors); if (!sectors) { LOG_ERROR("not enough memory"); return ERROR_FAIL; }if (!sectors) { ... } for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { sectors[sector].offset = sector * sectorsize; sectors[sector].size = sectorsize; sectors[sector].is_erased = -1; sectors[sector].is_protected = 0; }for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ... } bank->sectors = sectors; eneispif_info->probed = true; return ERROR_OK; }{ ... } static int eneispif_auto_probe(struct flash_bank *bank) { struct eneispif_flash_bank *eneispif_info = bank->driver_priv; if (eneispif_info->probed) return ERROR_OK; return eneispif_probe(bank); }{ ... } static int eneispif_protect_check(struct flash_bank *bank) { /* Nothing to do. Protection is only handled in SW. */ return ERROR_OK; }{ ... } static int get_eneispif_info(struct flash_bank *bank, struct command_invocation *cmd) { struct eneispif_flash_bank *eneispif_info = bank->driver_priv; if (!(eneispif_info->probed)) { command_print(cmd, "ENE ISPI flash bank not probed yet."); return ERROR_OK; }if (!(eneispif_info->probed)) { ... } command_print(cmd, "ENE ISPI flash information:\n" " Device \'%s\' (ID 0x%08" PRIx32 ")", eneispif_info->dev->name, eneispif_info->dev_id); return ERROR_OK; }{ ... } const struct flash_driver eneispif_flash = { .name = "eneispif", .usage = "flash bank <name> 'eneispif' <base_address> <size> 0 0 <target> <ctrl_base>", .flash_bank_command = eneispif_flash_bank_command, .erase = eneispif_erase, .protect = eneispif_protect, .write = eneispif_write, .read = default_flash_read, .probe = eneispif_probe, .auto_probe = eneispif_auto_probe, .erase_check = default_flash_blank_check, .protect_check = eneispif_protect_check, .info = get_eneispif_info, .free_driver_priv = default_flash_free_driver_priv, ...};
Details
Show:
from
Types: Columns:
Click anywhere in the source to view detailed information here...