-
Notifications
You must be signed in to change notification settings - Fork 4
/
nucX1.c
executable file
·504 lines (429 loc) · 13.8 KB
/
nucX1.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/***************************************************************************
* Copyright (C) 2011 by James K. Larson *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
// This flash driver takes it's inspiration and structure from the stm32x
// flash driver in the OpenOCD package.
// 7/16/11 -- Initial dummy version works - now add functions. -jkl
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "imp.h"
/* nucX1 register locations */
#define NUCX1_SYS_BASE 0x50000000
#define NUCX1_SYS_WRPROT 0x50000100
#define NUCX1_SYSCLK_BASE 0x50000200
#define NUCX1_SYSCLK_PWRCON 0x50000200
#define NUCX1_SYSCLK_CLKSEL0 0x50000210
#define NUCX1_SYSCLK_CLKDIV 0x50000218
#define NUCX1_SYSCLK_AHBCLK 0x50000204
#define NUCX1_FLASH_BASE 0x5000C000
#define NUCX1_FLASH_ISPCON 0x5000C000
#define NUCX1_FLASH_ISPCMD 0x5000C00C
#define NUCX1_FLASH_ISPADR 0x5000C004
//#define NUCX1_FLASH_ISPDAT 0x5000C00?
#define NUCX1_FLASH_ISPTRG 0x5000C010
/* Command register bits */
#define PWRCON_OSC22M (1 << 2)
#define PWRCON_XTL12M (1 << 0)
#define AHBCLK_ISP_EN (1 << 2)
#define ISPCON_ISPEN (1 << 0)
#define ISPCON_APUEN (1 << 3)
#define ISPCON_ISPFF (1 << 6)
#define ISPCMD_FCTRL (0x2)
//#define ISPCMD_FCEN (1 << 4)
#define ISPCMD_FOEN (1 << 5)
// The above three terms combine to make the erase command
#define ISPCMD_ERASE (ISPCMD_FCTRL | ISPCMD_FOEN)
#define ISPTRG_ISPGO (1 << 0)
/* access unlock keys */
#define KEY1 0x59
#define KEY2 0x16
#define KEY3 0x88
#define LOCK 0x00
// Private bank information for nucX1.
struct nucX1_flash_bank
{
struct working_area *write_algorithm; // this will be used later
int probed;
};
// This is the function called in the config file.
FLASH_BANK_COMMAND_HANDLER(nucX1_flash_bank_command)
{
struct nucX1_flash_bank *nucX1_info;
if (CMD_ARGC < 6)
{
LOG_WARNING("incomplete flash_bank nucX1 configuration");
return ERROR_FLASH_BANK_INVALID;
}
nucX1_info = malloc(sizeof(struct nucX1_flash_bank));
bank->driver_priv = nucX1_info;
nucX1_info->write_algorithm = NULL;
nucX1_info->probed = 0;
return ERROR_OK;
}
// Protection checking - examines the lock bit.
static int nucX1_protect_check(struct flash_bank *bank)
{
struct target *target = bank->target;
//struct stm32x_flash_bank *nucX1_info = bank->driver_priv;
uint32_t protected;
int s;
int set;
if (target->state != TARGET_HALTED)
{
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}
// Check to see if Nuc is unlocked or not
int retval = target_read_u32(target, NUCX1_SYS_WRPROT, &protected);
if (retval != ERROR_OK)
return retval;
LOG_INFO("protected = 0x%08" PRIx32 "", protected);
if (protected == 0){ // means protected
set = 1;
}
else {
set = 0;
}
for (s = 0; s < bank->num_sectors; s++)
bank->sectors[s].is_protected = set;
return ERROR_OK;
}
// The erase routine - active development is here.
// As of 7/31/11, this does not work.
static int nucX1_erase(struct flash_bank *bank, int first, int last)
{
struct target *target = bank->target;
int i, timeout;
//uint32_t protected;
uint32_t protected, clockSelection, status, dummy;
if (bank->target->state != TARGET_HALTED)
{
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}
LOG_INFO("NucX1: Sector Erase begins.");
// Check to see if Nuc is unlocked or not
int retval = target_read_u32(target, NUCX1_SYS_WRPROT, &protected);
if (retval != ERROR_OK)
return retval;
LOG_INFO("protected = 0x%08" PRIx32 "", protected);
if (protected == 0){ // means protected - so unlock it
/* unlock flash registers */
retval = target_write_u32(target, NUCX1_SYS_WRPROT, KEY1);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, NUCX1_SYS_WRPROT, KEY2);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, NUCX1_SYS_WRPROT, KEY3);
if (retval != ERROR_OK)
return retval;
}
// Check that unlock worked
retval = target_read_u32(target, NUCX1_SYS_WRPROT, &protected);
if (retval != ERROR_OK)
return retval;
LOG_INFO("protected = 0x%08" PRIx32 "", protected);
if (protected == 1){ // means unprotected
LOG_INFO("protection removed\n");
} else {
LOG_INFO("still protected!!\n");
}
// select the internal clock and enable programming
retval = target_read_u32(target, NUCX1_SYSCLK_PWRCON, &clockSelection);
if (retval != ERROR_OK)
return retval;
LOG_INFO("clock selection = 0x%08" PRIx32 "", clockSelection);
if ((clockSelection & PWRCON_XTL12M ) == 0){ // internal 22MHz not selected - select it
retval = target_write_u32(target, NUCX1_SYSCLK_PWRCON, PWRCON_XTL12M);
if (retval != ERROR_OK)
return retval;
// Delay to allow settling
alive_sleep(5); // can use busy sleep for short times - but what's short??
LOG_INFO("12MHz clock is now selected\n");
}
else {
LOG_INFO("12MHz clock already selected\n");
}
retval = target_write_u32(target, NUCX1_SYSCLK_CLKSEL0, 0x00); // try all 0s - may be wrong?
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, NUCX1_SYSCLK_CLKDIV, 0x00); // all 0s are OK.
if (retval != ERROR_OK)
return retval;
// most of these, I need to read the value, then set or clear the bit, then write it back out
retval = target_write_u32(target, NUCX1_SYSCLK_AHBCLK, AHBCLK_ISP_EN); // should be the only bit set
if (retval != ERROR_OK)
return retval;
retval = target_read_u32(target, NUCX1_FLASH_ISPCON, &dummy);
if (retval != ERROR_OK)
return retval;
LOG_INFO("ISPCON = 0x%08" PRIx32 "", dummy);
//dummy = dummy | ISPCON_ISPEN | ISPCON_APUEN;
dummy = dummy | ISPCON_ISPEN ;
LOG_INFO("ISPCON becomes 0x%08" PRIx32 "", dummy);
retval = target_write_u32(target, NUCX1_FLASH_ISPCON, dummy);
if (retval != ERROR_OK)
return retval;
/*
retval = target_write_u32(target, NUCX1_FLASH_ISPCON, ISPCON_ISPEN);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, NUCX1_FLASH_ISPCON, ISPCON_APUEN); // should these OR in??
if (retval != ERROR_OK)
return retval;
*/
LOG_INFO("ISPCMD gets 0x%08" PRIx32 "", ISPCMD_ERASE);
retval = target_write_u32(target, NUCX1_FLASH_ISPCMD, ISPCMD_ERASE); // This is the whole command
if (retval != ERROR_OK)
return retval;
for (i = first; i <= last; i++)
{
LOG_INFO("erasing sector %d at addresss 0x%" PRIx32 "\n",i,bank->base + bank->sectors[i].offset);
retval = target_write_u32(target, NUCX1_FLASH_ISPADR, bank->base + bank->sectors[i].offset); // need size here??
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, NUCX1_FLASH_ISPTRG, ISPTRG_ISPGO); // This is the only bit available
if (retval != ERROR_OK)
return retval;
//wait for busy to clear - check the GO flag
timeout = 100;
for (;;)
{
retval = target_read_u32(target, NUCX1_FLASH_ISPTRG, &status);
if (retval != ERROR_OK)
return retval;
LOG_INFO("status: 0x%" PRIx32 "", status);
if (status == 0)
break;
if (timeout-- <= 0)
{
LOG_INFO("timed out waiting for flash");
return ERROR_FAIL;
}
busy_sleep(1); // can use busy sleep for short times.
}
// check for failure
retval = target_read_u32(target, NUCX1_FLASH_ISPCON, &status);
if (retval != ERROR_OK)
return retval;
if ((status & ISPCON_ISPFF) != 0){
LOG_DEBUG("failure: 0x%" PRIx32 "", status);
// if bit is set, then must write to it to clear it.
retval = target_write_u32(target, NUCX1_FLASH_ISPCON, ISPCON_ISPFF);
if (retval != ERROR_OK)
return retval;
} else {
LOG_INFO ("erased OK\n");
bank->sectors[i].is_erased = 1;
}
}
// done, so restore the protection
retval = target_write_u32(target, NUCX1_SYS_WRPROT, LOCK);
if (retval != ERROR_OK)
return retval;
LOG_INFO("Erase done\n" );
return ERROR_OK;
}
// The write routine stub.
static int nucX1_write_block(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
{
int retval = ERROR_OK;
LOG_INFO("Novoton NUC: FLASH Write ...");
return retval;
}
// The probe routine for the nuc. Only recognizes the nuc120 right now.
static int nucX1_probe(struct flash_bank *bank)
{
struct target *target = bank->target;
struct nucX1_flash_bank *nucX1_info = bank->driver_priv;
int i;
uint16_t num_pages;
uint32_t device_id;
int page_size;
uint32_t base_address = 0x00000000;
nucX1_info->probed = 0;
// don't know for sure if this is required??
if (bank->target->state != TARGET_HALTED)
{
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}
/* read nucX1 device id register */
int retval = target_read_u32(target, 0x50000000, &device_id);
if (retval != ERROR_OK)
return retval;
LOG_INFO("device id = 0x%08" PRIx32 "", device_id);
if ((device_id ) == 0x00012000)
{
/* medium density - we have 1k pages
* 4 pages for a protection area */
page_size = 512; // This may be better thought of as "sectors"
num_pages = 256; // 128K
LOG_INFO("Nuc 120 Type");
//nucX1_info->ppage_size = 4;
}
else if ((device_id ) != 0x00000000) // probably a nuc of some sort
{
page_size = 512; // this is the standard size
//nucX1_info->ppage_size = 4;
num_pages = 64; // 32K (Arbitrary)
LOG_WARNING("Undefined NUC type??");
}
else
{
LOG_WARNING("Cannot identify target as a nuc family.");
return ERROR_FAIL;
}
if (bank->sectors)
{
free(bank->sectors);
bank->sectors = NULL;
}
bank->base = base_address;
bank->size = (num_pages * page_size);
bank->num_sectors = num_pages;
bank->sectors = malloc(sizeof(struct flash_sector) * num_pages);
for (i = 0; i < num_pages; i++)
{
bank->sectors[i].offset = i * page_size;
bank->sectors[i].size = page_size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 1;
//LOG_INFO("sector id = %x", i);
}
nucX1_info->probed = 1;
LOG_INFO("Novoton NUC: Probed ...");
return ERROR_OK;
}
// Standard approach to autoprobing.
static int nucX1_auto_probe(struct flash_bank *bank)
{
struct nucX1_flash_bank *nucX1_info = bank->driver_priv;
if (nucX1_info->probed)
return ERROR_OK;
return nucX1_probe(bank);
}
// Info doesn't really add much, but works correctly.
static int nucX1_info(struct flash_bank *bank, char *buf, int buf_size)
{
struct target *target = bank->target;
uint32_t device_id;
int printed;
/* read nucX1 device id register */
int retval = target_read_u32(target, 0x50000000, &device_id);
if (retval != ERROR_OK)
return retval;
if ((device_id) == 0x00012000)
{
LOG_INFO("nuc120USB (Medium Density)");
}
else if ((device_id) != 0x00000000)
{
LOG_INFO("nuc device likely - add to driver");
}
else
{
LOG_INFO("Cannot identify target as a nuc1xx");
return ERROR_FAIL;
}
return ERROR_OK;
}
/*
// The nuc120 doesn't support mass erase, so this will probably be removed soon.
// The structure is left for now until I am sure I don't want to add any custom
// commands.
static int nucX1_mass_erase(struct flash_bank *bank)
{
struct target *target = bank->target;
int retval = ERROR_OK;
//uint32_t u32DummyRead;
//int32_t flashflag;
//uint32_t ucDQ6_read1, ucDQ6_read2, ucDQ5_read;
if (target->state != TARGET_HALTED)
{
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}
LOG_INFO("Novoton NUC: Chip Erase ... (may take several seconds)");
return retval;
}
COMMAND_HANDLER(nucX1_handle_mass_erase_command)
{
int i; // for erasing sectors
if (CMD_ARGC < 1)
{
command_print(CMD_CTX, "nucX1 mass_erase <bank>");
return ERROR_OK;
}
struct flash_bank *bank;
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
if (ERROR_OK != retval)
return retval;
retval = nucX1_mass_erase(bank);
if (retval == ERROR_OK)
{
// set all sectors as erased
for (i = 0; i < bank->num_sectors; i++)
{
bank->sectors[i].is_erased = 1;
}
command_print(CMD_CTX, "nucX1 mass erase complete");
}
else
{
command_print(CMD_CTX, "nucX1 mass erase failed");
}
return retval;
}
static const struct command_registration nucX1_exec_command_handlers[] = {
{
.name = "mass_erase",
.handler = nucX1_handle_mass_erase_command,
.mode = COMMAND_EXEC,
.usage = "bank_id",
.help = "Erase entire Flash device.",
},
COMMAND_REGISTRATION_DONE
};
static const struct command_registration nucX1_command_handlers[] = {
{
.name = "nucX1",
.mode = COMMAND_ANY,
.help = "nucX1 Flash command group",
.chain = nucX1_exec_command_handlers,
},
COMMAND_REGISTRATION_DONE
};
*/
struct flash_driver nucX1_flash = {
.name = "nucX1",
//.commands = nucX1_command_handlers,
.commands = NULL,
.flash_bank_command = nucX1_flash_bank_command,
.erase = nucX1_erase,
.write = nucX1_write_block,
.read = default_flash_read,
.probe = nucX1_probe,
.auto_probe = nucX1_auto_probe,
.erase_check = default_flash_mem_blank_check,
.protect_check = nucX1_protect_check,
.info = nucX1_info,
};