Hi,

I am trying to get openocd to work with the freescale TWRK60512 board
(cortex-m4, 256k flash, 256k flexNVM, the config needed for this is very
similar to the existing target/k40.cfg + board/kwikstik.cfg, but I run
into a problem when trying to flash RedBoot. I have opened a ticket #46
for this issue.

After some debugging, it seems the problem is that the driver uses the
NVM-specific instructions to write into flash, which does not work for
the normal program flash segment that occupies the first 256k on the
this board. The attached patch addresses this by checking whether the
destination address is in the pflash or nvm area, and in the former case
falls back to the slow longword write mechanism; with this change I am
able to flash RedBoot on the above board.

The patch also fixes the check for the bank size sanity; currently the
driver assumes that the bank size should be the size of the program
flash (i.e., over all size - nvm), but since the driver writes into nvm
as well, this is not correct, and makes it impossible to configure the
bank on the above board without getting a warning about the bank size
mismatch.

See what you think,

kind regards,

Tomas
>From bd8ee5007090395c50fc1560b690ee2ff07ea8bd Mon Sep 17 00:00:00 2001
From: Tomas Frydrych <[email protected]>
Date: Fri, 11 Nov 2011 19:11:09 +0000
Subject: [PATCH] kinetis flash: use longword write when writing into pflash


Signed-off-by: Tomas Frydrych <[email protected]>
---
 src/flash/nor/kinetis.c |   87 +++++++++++++++++++++++++++++++++-------------
 1 files changed, 62 insertions(+), 25 deletions(-)

diff --git a/src/flash/nor/kinetis.c b/src/flash/nor/kinetis.c
index ecc60be..cb811a1 100644
--- a/src/flash/nor/kinetis.c
+++ b/src/flash/nor/kinetis.c
@@ -1,7 +1,10 @@
 /***************************************************************************
- *   Copyright (C) 2011 by Mathias Kuester                                  *
+ *   Copyright (C) 2011 by Mathias Kuester                                 *
  *   [email protected]                                                     *
  *                                                                         *
+ *   Copyright (C) 2011 sleep(5) ltd                                       *
+ *   [email protected]                                                   *
+ *                                                                         *
  *   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     *
@@ -24,6 +27,11 @@
 #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)
 {
@@ -60,6 +68,8 @@ static int kinetis_update_bank_info(struct flash_bank *bank)
 
 FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
 {
+	struct kinetis_flash_bank *bank_info;
+
 	if (CMD_ARGC < 6) {
 		LOG_ERROR("incomplete flash_bank kinetis configuration %d",
 			  CMD_ARGC);
@@ -68,6 +78,12 @@ FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
 
 	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;
 }
 
@@ -264,9 +280,11 @@ 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;
+	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");
@@ -279,31 +297,50 @@ static int kinetis_write(struct flash_bank *bank, uint8_t * buffer,
 		return result;
 	}
 
-	/* make flex ram available */
-	w0 = (0x81 << 24) | 0x00ff0000;
+	if (offset >= kbank->nvm_start)
+		nvm = 1;
 
-	result = kinetis_ftfl_command(bank, w0, w1, w2);
-
-	if (result != ERROR_OK) {
-		return ERROR_FLASH_OPERATION_FAILED;
+	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;
 	}
 
-	/* check if ram ready */
-	result = target_read_memory(bank->target, 0x40020001, 1, 1, buf);
+	if (nvm) {
+		LOG_DEBUG("flash write into NVM @%08X", offset);
 
-	if (result != ERROR_OK) {
-		return result;
-	}
+		/* make flex ram available */
+		w0 = (0x81 << 24) | 0x00ff0000;
 
-	if (!(buf[0] & (1 << 1))) {
-		/* fallback to longword write */
-		fallback = 1;
+		result = kinetis_ftfl_command(bank, w0, w1, w2);
 
-		LOG_WARNING
-		    ("ram not ready, fallback to slow longword write (FCNFG: %02X)",
-		     buf[0]);
+		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)) {
@@ -364,7 +401,7 @@ static int kinetis_probe(struct flash_bank *bank)
 	int result, i;
 	uint8_t buf[4];
 	uint32_t sim_sdid, sim_fcfg1, sim_fcfg2, offset = 0;
-	uint32_t nvm_size, pf_size, flash_size, ee_size;
+	uint32_t nvm_size, pf_size, ee_size;
 
 	if (bank->target->state != TARGET_HALTED) {
 		LOG_ERROR("Target not halted");
@@ -425,9 +462,6 @@ static int kinetis_probe(struct flash_bank *bank)
 		break;
 	}
 
-	/* pf_size is the total size */
-	flash_size = pf_size - nvm_size;
-
 	switch ((sim_fcfg1 >> 16) & 0x0f) {
 	case 0x02:
 		ee_size = 4 * 1024;
@@ -458,11 +492,14 @@ static int kinetis_probe(struct flash_bank *bank)
 		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 (flash_size != bank->size) {
-		LOG_WARNING("flash size is different %d != %d", flash_size,
+	if (pf_size != bank->size) {
+		LOG_WARNING("flash size is different %d != %d", pf_size,
 			    bank->size);
 	}
 
-- 
1.7.7.1

------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
OpenOCD-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openocd-devel

Reply via email to