aboutsummaryrefslogtreecommitdiff
path: root/src/flash/nor/kinetis.c
blob: 0cfbd7adaa033ac923f61ce2dea7474a667bf904 (plain)
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/***************************************************************************
 *   Copyright (C) 2011 by Mathias Kuester                                 *
 *   kesmtp@freenet.de                                                     *
 *                                                                         *
 *   Copyright (C) 2011 sleep(5) ltd                                       *
 *   tomas@sleepfive.com                                                   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "imp.h"
#include "helper/binarybuffer.h"

struct kinetis_flash_bank {
	uint32_t nvm_start;
};

static int kinetis_get_master_bank(struct flash_bank *bank,
				   struct flash_bank **master_bank)
{
	*master_bank = get_flash_bank_by_name_noprobe(bank->name);
	if (*master_bank == NULL) {
		LOG_ERROR("master flash bank '%s' does not exist",
			  (char *)bank->driver_priv);
		return ERROR_FLASH_OPERATION_FAILED;
	}

	return ERROR_OK;
}

static int kinetis_update_bank_info(struct flash_bank *bank)
{
	int result;
	struct flash_bank *master_bank;

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	/* update the info we do not have */
	bank->size = master_bank->size;
	bank->chip_width = master_bank->chip_width;
	bank->bus_width = master_bank->bus_width;
	bank->num_sectors = master_bank->num_sectors;
	bank->sectors = master_bank->sectors;

	return ERROR_OK;
}

FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
{
	struct kinetis_flash_bank *bank_info;

	if (CMD_ARGC < 6) {
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	LOG_INFO("add flash_bank kinetis %s", bank->name);

	bank_info = malloc(sizeof(struct kinetis_flash_bank));

	memset(bank_info, 0, sizeof(struct kinetis_flash_bank));

	bank->driver_priv = bank_info;

	return ERROR_OK;
}

static int kinetis_protect(struct flash_bank *bank, int set, int first,
			   int last)
{
	int result;
	struct flash_bank *master_bank;

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	LOG_WARNING("kinetis_protect not supported yet");

	if (bank->target->state != TARGET_HALTED) {
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	return ERROR_OK;
}

static int kinetis_protect_check(struct flash_bank *bank)
{
	int result;
	struct flash_bank *master_bank;
	uint8_t buffer[4];
	uint32_t fprot, psize, psec;
	int i, b;

	if (bank->target->state != TARGET_HALTED) {
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	/* read protection register FTFL_FPROT */
	result = target_read_memory(bank->target, 0x40020010, 1, 4, buffer);

	if (result != ERROR_OK) {
		return result;
	}

	fprot = target_buffer_get_u32(bank->target, buffer);

	/* every bit protect 1/32 of the full flash */
	psize = bank->size / 32;
	psec = 0;
	b = 0;

	for (i = 0; i < bank->num_sectors; i++) {
		if ((fprot >> b) & 1)
			bank->sectors[i].is_protected = 0;
		else
			bank->sectors[i].is_protected = 1;

		psec += bank->sectors[i].size;

		if (psec >= psize) {
			psec = 0;
			b++;
		}
	}

	return ERROR_OK;
}

static int kinetis_ftfl_command(struct flash_bank *bank, uint32_t w0,
				uint32_t w1, uint32_t w2)
{
	uint8_t buffer[12];
	int result, i;

	/* wait for done */
	for (i = 0; i < 50; i++) {
		result =
		    target_read_memory(bank->target, 0x40020000, 1, 1, buffer);

		if (result != ERROR_OK) {
			return result;
		}

		if (buffer[0] & 0x80)
			break;

		buffer[0] = 0x00;
	}

	if (buffer[0] != 0x80) {
		/* reset error flags */
		buffer[0] = 0x30;
		result =
		    target_write_memory(bank->target, 0x40020000, 1, 1, buffer);
		if (result != ERROR_OK) {
			return result;
		}
	}

	target_buffer_set_u32(bank->target, buffer, w0);
	target_buffer_set_u32(bank->target, buffer + 4, w1);
	target_buffer_set_u32(bank->target, buffer + 8, w2);

	result = target_write_memory(bank->target, 0x40020004, 4, 3, buffer);

	if (result != ERROR_OK) {
		return result;
	}

	/* start command */
	buffer[0] = 0x80;
	result = target_write_memory(bank->target, 0x40020000, 1, 1, buffer);
	if (result != ERROR_OK) {
		return result;
	}

	/* wait for done */
	for (i = 0; i < 50; i++) {
		result =
		    target_read_memory(bank->target, 0x40020000, 1, 1, buffer);

		if (result != ERROR_OK) {
			return result;
		}

		if (buffer[0] & 0x80)
			break;

		buffer[0] = 0x00;
	}

	if (buffer[0] != 0x80) {
		LOG_ERROR
		    ("ftfl command failed FSTAT: %02X W0: %08X W1: %08X W2: %08X",
		     buffer[0], w0, w1, w2);

		return ERROR_FLASH_OPERATION_FAILED;
	}

	return ERROR_OK;
}

static int kinetis_erase(struct flash_bank *bank, int first, int last)
{
	struct flash_bank *master_bank;
	int result, i;
	uint32_t w0 = 0, w1 = 0, w2 = 0;

	if (bank->target->state != TARGET_HALTED) {
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	if ((first > bank->num_sectors) || (last > bank->num_sectors)) {
		return ERROR_FLASH_OPERATION_FAILED;
	}

	for (i = first; i <= last; i++) {
		/* set command and sector address */
		w0 = (0x09 << 24) | bank->sectors[i].offset;

		result = kinetis_ftfl_command(bank, w0, w1, w2);

		if (result != ERROR_OK) {
			LOG_WARNING("erase sector %d failed", i);
			return ERROR_FLASH_OPERATION_FAILED;
		}

		bank->sectors[i].is_erased = 1;
	}

	if (first == 0) {
		LOG_WARNING
		    ("flash configuration field erased, please reset the device");
	}

	return ERROR_OK;
}

static int kinetis_write(struct flash_bank *bank, uint8_t * buffer,
			 uint32_t offset, uint32_t count)
{
	struct flash_bank *master_bank;
	unsigned int i, result, fallback = 0, nvm = 0;
	uint8_t buf[8];
	uint32_t wc, w0 = 0, w1 = 0, w2 = 0;
	struct kinetis_flash_bank * kbank = (struct kinetis_flash_bank *)
		bank->driver_priv;

	if (bank->target->state != TARGET_HALTED) {
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	if (offset >= kbank->nvm_start)
		nvm = 1;

	if (!nvm && (offset + count) > kbank->nvm_start) {
		/* we could flash this in two goes, but if the segment
		   spans across the pflash/nvm boundary, something is probably
		   not right.
		*/
		LOG_ERROR("Segment spans NVM boundary");
		return ERROR_FLASH_DST_OUT_OF_BANK;
	}

	if (nvm) {
		LOG_DEBUG("flash write into NVM @%08X", offset);

		/* make flex ram available */
		w0 = (0x81 << 24) | 0x00ff0000;

		result = kinetis_ftfl_command(bank, w0, w1, w2);

		if (result != ERROR_OK)
			return ERROR_FLASH_OPERATION_FAILED;

		/* check if ram ready */
		result = target_read_memory(bank->target, 0x40020001, 1, 1, buf);

		if (result != ERROR_OK)
			return result;

		if (!(buf[0] & (1 << 1))) {
			/* fallback to longword write */
			fallback = 1;

		       LOG_WARNING("ram not ready, fallback to slow longword write (FCNFG: %02X)",
				   buf[0]);
		}
	} else {
		LOG_DEBUG("flash write into PFLASH @08%X", offset);
		fallback = 1;
	}


	/* program section command */
	if (fallback == 0) {
		for (i = 0; i < count; i += (2 * 1024)) {
			wc = 512;

			if ((count - i) < (2 * 1024)) {
				wc = count - i;
				wc /= 4;
			}

			LOG_DEBUG("write section @ %08X with length %d",
				  offset + i, wc * 4);

			/* write data to flexram */
			result =
			    target_write_memory(bank->target, 0x14000000, 4, wc,
						buffer + i);

			if (result != ERROR_OK) {
				LOG_ERROR("target_write_memory failed");

				return result;
			}

			/* execute section command */
			w0 = (0x0b << 24) | (offset + i);
			w1 = (256 << 16);

			result = kinetis_ftfl_command(bank, w0, w1, w2);

			if (result != ERROR_OK) {
				return ERROR_FLASH_OPERATION_FAILED;
			}
		}
	}
	/* program longword command */
	else {
		for (i = 0; i < count; i += 4) {
			LOG_DEBUG("write longword @ %08X", offset + i);

			w0 = (0x06 << 24) | (offset + i);
			w1 = buf_get_u32(buffer + offset + i, 0, 32);

			result = kinetis_ftfl_command(bank, w0, w1, w2);

			if (result != ERROR_OK) {
				return ERROR_FLASH_OPERATION_FAILED;
			}
		}
	}

	return ERROR_OK;
}

static int kinetis_probe(struct flash_bank *bank)
{
	struct flash_bank *master_bank;
	int result, i;
	uint8_t buf[4];
	uint32_t sim_sdid, sim_fcfg1, sim_fcfg2, offset = 0;
	uint32_t nvm_size, pf_size, ee_size;

	if (bank->target->state != TARGET_HALTED) {
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	result = target_read_memory(bank->target, 0x40048024, 1, 4, buf);
	if (result != ERROR_OK) {
		return result;
	}
	sim_sdid = target_buffer_get_u32(bank->target, buf);
	result = target_read_memory(bank->target, 0x4004804c, 1, 4, buf);
	if (result != ERROR_OK) {
		return result;
	}
	sim_fcfg1 = target_buffer_get_u32(bank->target, buf);
	result = target_read_memory(bank->target, 0x40048050, 1, 4, buf);
	if (result != ERROR_OK) {
		return result;
	}
	sim_fcfg2 = target_buffer_get_u32(bank->target, buf);

	LOG_DEBUG("SDID: %08X FCFG1: %08X FCFG2: %08X", sim_sdid, sim_fcfg1,
		  sim_fcfg2);

	switch ((sim_fcfg1 >> 28) & 0x0f) {
	case 0x07:
		nvm_size = 128 * 1024;
		break;
	case 0x09:
	case 0x0f:
		nvm_size = 256 * 1024;
		break;
	default:
		nvm_size = 0;
		break;
	}

	switch ((sim_fcfg1 >> 24) & 0x0f) {
	case 0x07:
		pf_size = 128 * 1024;
		break;
	case 0x09:
		pf_size = 256 * 1024;
		break;
	case 0x0b:
	case 0x0f:
		pf_size = 512 * 1024;
		break;
	default:
		pf_size = 0;
		break;
	}

	switch ((sim_fcfg1 >> 16) & 0x0f) {
	case 0x02:
		ee_size = 4 * 1024;
		break;
	case 0x03:
		ee_size = 2 * 1024;
		break;
	case 0x04:
		ee_size = 1 * 1024;
		break;
	case 0x05:
		ee_size = 512;
		break;
	case 0x06:
		ee_size = 256;
		break;
	case 0x07:
		ee_size = 128;
		break;
	case 0x08:
		ee_size = 64;
		break;
	case 0x09:
		ee_size = 32;
		break;
	default:
		ee_size = 0;
		break;
	}

	((struct kinetis_flash_bank *) bank->driver_priv)->nvm_start =
		pf_size - nvm_size;

	LOG_DEBUG("NVM: %d PF: %d EE: %d BL1: %d", nvm_size, pf_size, ee_size,
		  (sim_fcfg2 >> 23) & 1);

	if (pf_size != bank->size) {
		LOG_WARNING("flash size is different %d != %d", pf_size,
			    bank->size);
	}

	bank->num_sectors = bank->size / (2 * 1024);
	assert(bank->num_sectors > 0);
	bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);

	for (i = 0; i < bank->num_sectors; i++) {
		bank->sectors[i].offset = offset;
		bank->sectors[i].size = 2 * 1024;
		offset += bank->sectors[i].size;
		bank->sectors[i].is_erased = -1;
		bank->sectors[i].is_protected = 1;
	}

	/* update the info we do not have */
	return kinetis_update_bank_info(bank);
}

static int kinetis_auto_probe(struct flash_bank *bank)
{
	return kinetis_probe(bank);
}

static int kinetis_info(struct flash_bank *bank, char *buf, int buf_size)
{
	int result;
	struct flash_bank *master_bank;

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	snprintf(buf, buf_size,
		 "%s driver for flash bank %s at 0x%8.8" PRIx32 "",
		 bank->driver->name, master_bank->name, master_bank->base);

	return ERROR_OK;
}

static int kinetis_blank_check(struct flash_bank *bank)
{
	int result;
	struct flash_bank *master_bank;

	LOG_WARNING("kinetis_blank_check not supported yet");

	if (bank->target->state != TARGET_HALTED) {
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	return ERROR_OK;
}

static int kinetis_flash_read(struct flash_bank *bank,
			      uint8_t * buffer, uint32_t offset, uint32_t count)
{
	int result;
	struct flash_bank *master_bank;

	LOG_WARNING("kinetis_flash_read not supported yet");

	if (bank->target->state != TARGET_HALTED) {
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	result = kinetis_get_master_bank(bank, &master_bank);

	if (result != ERROR_OK) {
		return result;
	}

	return ERROR_OK;
}

struct flash_driver kinetis_flash = {
	.name = "kinetis",
	.flash_bank_command = kinetis_flash_bank_command,
	.erase = kinetis_erase,
	.protect = kinetis_protect,
	.write = kinetis_write,
	.read = kinetis_flash_read,
	.probe = kinetis_probe,
	.auto_probe = kinetis_auto_probe,
	.erase_check = kinetis_blank_check,
	.protect_check = kinetis_protect_check,
	.info = kinetis_info,
};