2008-04-21 03:18:19 -05:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2008 by *
|
|
|
|
|
* Karl RobinSod <karl.robinsod@gmail.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., *
|
2013-06-02 14:32:36 -05:00
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
|
2008-04-21 03:18:19 -05:00
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
* There are some things to notice
|
|
|
|
|
*
|
|
|
|
|
* You need to unprotect flash sectors each time you connect the OpenOCD
|
|
|
|
|
* Dumping 1MB takes about 60 Seconds
|
|
|
|
|
* Full erase (sectors 0-22 inclusive) takes 2-4 seconds
|
|
|
|
|
* Writing 1MB takes 88 seconds
|
|
|
|
|
*
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-12-04 16:06:20 -06:00
|
|
|
|
#include "imp.h"
|
2009-12-03 06:14:25 -06:00
|
|
|
|
#include <helper/binarybuffer.h>
|
2008-04-21 03:18:19 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define LOAD_TIMER_ERASE 0
|
|
|
|
|
#define LOAD_TIMER_WRITE 1
|
2008-04-21 03:18:19 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define FLASH_PAGE_SIZE 512
|
2008-04-21 03:18:19 -05:00
|
|
|
|
|
|
|
|
|
/* LPC288X control registers */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define DBGU_CIDR 0x8000507C
|
2008-04-21 03:18:19 -05:00
|
|
|
|
/* LPC288X flash registers */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define F_CTRL 0x80102000 /* Flash control register R/W 0x5 */
|
|
|
|
|
#define F_STAT 0x80102004 /* Flash status register RO 0x45 */
|
|
|
|
|
#define F_PROG_TIME 0x80102008 /* Flash program time register R/W 0 */
|
|
|
|
|
#define F_WAIT 0x80102010 /* Flash read wait state register R/W 0xC004 */
|
|
|
|
|
#define F_CLK_TIME 0x8010201C /* Flash clock divider for 66 kHz generation R/W 0
|
|
|
|
|
**/
|
|
|
|
|
#define F_INTEN_CLR 0x80102FD8 /* Clear interrupt enable bits WO - */
|
|
|
|
|
#define F_INTEN_SET 0x80102FDC /* Set interrupt enable bits WO - */
|
|
|
|
|
#define F_INT_STAT 0x80102FE0 /* Interrupt status bits RO 0 */
|
|
|
|
|
#define F_INTEN 0x80102FE4 /* Interrupt enable bits RO 0 */
|
|
|
|
|
#define F_INT_CLR 0x80102FE8 /* Clear interrupt status bits WO */
|
|
|
|
|
#define F_INT_SET 0x80102FEC /* Set interrupt status bits WO - */
|
|
|
|
|
#define FLASH_PD 0x80005030 /* Allows turning off the Flash memory for power
|
|
|
|
|
*savings. R/W 1*/
|
|
|
|
|
#define FLASH_INIT 0x80005034 /* Monitors Flash readiness, such as recovery from
|
|
|
|
|
*Power Down mode. R/W -*/
|
2008-04-21 03:18:19 -05:00
|
|
|
|
|
|
|
|
|
/* F_CTRL bits */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define FC_CS 0x0001
|
|
|
|
|
#define FC_FUNC 0x0002
|
|
|
|
|
#define FC_WEN 0x0004
|
|
|
|
|
#define FC_RD_LATCH 0x0020
|
|
|
|
|
#define FC_PROTECT 0x0080
|
|
|
|
|
#define FC_SET_DATA 0x0400
|
|
|
|
|
#define FC_RSSL 0x0800
|
|
|
|
|
#define FC_PROG_REQ 0x1000
|
|
|
|
|
#define FC_CLR_BUF 0x4000
|
|
|
|
|
#define FC_LOAD_REQ 0x8000
|
2008-04-21 03:18:19 -05:00
|
|
|
|
/* F_STAT bits */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define FS_DONE 0x0001
|
|
|
|
|
#define FS_PROGGNT 0x0002
|
|
|
|
|
#define FS_RDY 0x0004
|
|
|
|
|
#define FS_ERR 0x0020
|
2008-04-21 03:18:19 -05:00
|
|
|
|
/* F_PROG_TIME */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define FPT_TIME_MASK 0x7FFF
|
2008-04-21 03:18:19 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define FPT_ENABLE 0x8000
|
2008-04-21 03:18:19 -05:00
|
|
|
|
/* F_WAIT */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
#define FW_WAIT_STATES_MASK 0x00FF
|
|
|
|
|
#define FW_SET_MASK 0xC000
|
2008-04-21 03:18:19 -05:00
|
|
|
|
|
|
|
|
|
/* F_CLK_TIME */
|
|
|
|
|
#define FCT_CLK_DIV_MASK 0x0FFF
|
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
struct lpc288x_flash_bank {
|
2010-11-17 07:00:25 -06:00
|
|
|
|
uint32_t working_area;
|
|
|
|
|
uint32_t working_area_size;
|
|
|
|
|
|
|
|
|
|
/* chip id register */
|
|
|
|
|
uint32_t cidr;
|
2012-01-31 11:55:03 -06:00
|
|
|
|
const char *target_name;
|
2010-11-17 07:00:25 -06:00
|
|
|
|
uint32_t cclk;
|
|
|
|
|
|
|
|
|
|
uint32_t sector_size_break;
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static uint32_t lpc288x_wait_status_busy(struct flash_bank *bank, int timeout);
|
2009-11-13 12:11:13 -06:00
|
|
|
|
static void lpc288x_load_timer(int erase, struct target *target);
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static void lpc288x_set_flash_clk(struct flash_bank *bank);
|
|
|
|
|
static uint32_t lpc288x_system_ready(struct flash_bank *bank);
|
2008-04-21 03:18:19 -05:00
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static uint32_t lpc288x_wait_status_busy(struct flash_bank *bank, int timeout)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2009-06-18 02:10:25 -05:00
|
|
|
|
uint32_t status;
|
2009-11-13 12:11:13 -06:00
|
|
|
|
struct target *target = bank->target;
|
2012-01-31 11:55:03 -06:00
|
|
|
|
do {
|
2008-08-19 11:40:35 -05:00
|
|
|
|
alive_sleep(1);
|
2008-07-18 06:38:23 -05:00
|
|
|
|
timeout--;
|
|
|
|
|
target_read_u32(target, F_STAT, &status);
|
2009-06-23 17:37:49 -05:00
|
|
|
|
} while (((status & FS_DONE) == 0) && timeout);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if (timeout == 0) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
LOG_DEBUG("Timedout!");
|
|
|
|
|
return ERROR_FLASH_OPERATION_FAILED;
|
|
|
|
|
}
|
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Read device id register and fill in driver info structure */
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static int lpc288x_read_part_info(struct flash_bank *bank)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2009-11-13 09:38:05 -06:00
|
|
|
|
struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
|
2009-11-13 12:11:13 -06:00
|
|
|
|
struct target *target = bank->target;
|
2009-06-18 02:10:25 -05:00
|
|
|
|
uint32_t cidr;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
int i = 0;
|
2009-06-18 02:10:25 -05:00
|
|
|
|
uint32_t offset;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if (lpc288x_info->cidr == 0x0102100A)
|
2012-01-31 11:55:03 -06:00
|
|
|
|
return ERROR_OK;/* already probed, multiple probes may cause memory leak, not
|
|
|
|
|
*allowed */
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* Read and parse chip identification register */
|
|
|
|
|
target_read_u32(target, DBGU_CIDR, &cidr);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if (cidr != 0x0102100A) {
|
|
|
|
|
LOG_WARNING("Cannot identify target as an LPC288X (%08" PRIx32 ")", cidr);
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_FLASH_OPERATION_FAILED;
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
lpc288x_info->cidr = cidr;
|
|
|
|
|
lpc288x_info->sector_size_break = 0x000F0000;
|
|
|
|
|
lpc288x_info->target_name = "LPC288x";
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* setup the sector info... */
|
|
|
|
|
offset = bank->base;
|
|
|
|
|
bank->num_sectors = 23;
|
2009-11-13 09:37:54 -06:00
|
|
|
|
bank->sectors = malloc(sizeof(struct flash_sector) * 23);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
for (i = 0; i < 15; i++) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
bank->sectors[i].offset = offset;
|
|
|
|
|
bank->sectors[i].size = 64 * 1024;
|
|
|
|
|
offset += bank->sectors[i].size;
|
|
|
|
|
bank->sectors[i].is_erased = -1;
|
|
|
|
|
bank->sectors[i].is_protected = 1;
|
|
|
|
|
}
|
2012-01-31 11:55:03 -06:00
|
|
|
|
for (i = 15; i < 23; i++) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
bank->sectors[i].offset = offset;
|
|
|
|
|
bank->sectors[i].size = 8 * 1024;
|
|
|
|
|
offset += bank->sectors[i].size;
|
|
|
|
|
bank->sectors[i].is_erased = -1;
|
|
|
|
|
bank->sectors[i].is_protected = 1;
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static int lpc288x_protect_check(struct flash_bank *bank)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2008-12-13 06:44:39 -06:00
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* flash_bank LPC288x 0 0 0 0 <target#> <cclk> */
|
2009-11-10 03:41:30 -06:00
|
|
|
|
FLASH_BANK_COMMAND_HANDLER(lpc288x_flash_bank_command)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2009-11-13 09:38:05 -06:00
|
|
|
|
struct lpc288x_flash_bank *lpc288x_info;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2009-11-15 06:57:12 -06:00
|
|
|
|
if (CMD_ARGC < 6)
|
2011-12-16 00:48:39 -06:00
|
|
|
|
return ERROR_COMMAND_SYNTAX_ERROR;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2009-11-13 09:38:05 -06:00
|
|
|
|
lpc288x_info = malloc(sizeof(struct lpc288x_flash_bank));
|
2008-07-18 06:38:23 -05:00
|
|
|
|
bank->driver_priv = lpc288x_info;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* part wasn't probed for info yet */
|
|
|
|
|
lpc288x_info->cidr = 0;
|
2009-11-15 10:15:59 -06:00
|
|
|
|
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], lpc288x_info->cclk);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-17 14:54:25 -05:00
|
|
|
|
/* The frequency is the AHB clock frequency divided by (CLK_DIV ×3) + 1.
|
|
|
|
|
* This must be programmed such that the Flash Programming clock frequency is 66 kHz ± 20%.
|
2008-07-18 06:38:23 -05:00
|
|
|
|
* AHB = 12 MHz ?
|
|
|
|
|
* 12000000/66000 = 182
|
|
|
|
|
* CLK_DIV = 60 ? */
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static void lpc288x_set_flash_clk(struct flash_bank *bank)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2009-06-18 02:10:25 -05:00
|
|
|
|
uint32_t clk_time;
|
2009-11-13 09:38:05 -06:00
|
|
|
|
struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
|
2008-07-18 06:38:23 -05:00
|
|
|
|
clk_time = (lpc288x_info->cclk / 66000) / 3;
|
|
|
|
|
target_write_u32(bank->target, F_CTRL, FC_CS | FC_WEN);
|
|
|
|
|
target_write_u32(bank->target, F_CLK_TIME, clk_time);
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* AHB tcyc (in ns) 83 ns
|
|
|
|
|
* LOAD_TIMER_ERASE FPT_TIME = ((400,000,000 / AHB tcyc (in ns)) - 2) / 512
|
2009-05-31 22:05:26 -05:00
|
|
|
|
* = 9412 (9500) (AN10548 9375)
|
2008-07-18 06:38:23 -05:00
|
|
|
|
* LOAD_TIMER_WRITE FPT_TIME = ((1,000,000 / AHB tcyc (in ns)) - 2) / 512
|
2009-05-31 22:05:26 -05:00
|
|
|
|
* = 23 (75) (AN10548 72 - is this wrong?)
|
2008-07-18 06:38:23 -05:00
|
|
|
|
* TODO: Sort out timing calcs ;) */
|
2009-11-13 12:11:13 -06:00
|
|
|
|
static void lpc288x_load_timer(int erase, struct target *target)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if (erase == LOAD_TIMER_ERASE)
|
|
|
|
|
target_write_u32(target, F_PROG_TIME, FPT_ENABLE | 9500);
|
|
|
|
|
else
|
|
|
|
|
target_write_u32(target, F_PROG_TIME, FPT_ENABLE | 75);
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static uint32_t lpc288x_system_ready(struct flash_bank *bank)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2009-11-13 09:38:05 -06:00
|
|
|
|
struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if (lpc288x_info->cidr == 0)
|
|
|
|
|
return ERROR_FLASH_BANK_NOT_PROBED;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if (bank->target->state != TARGET_HALTED) {
|
2008-08-17 14:40:17 -05:00
|
|
|
|
LOG_ERROR("Target not halted");
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_TARGET_NOT_HALTED;
|
|
|
|
|
}
|
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static int lpc288x_erase_check(struct flash_bank *bank)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2009-06-18 02:10:25 -05:00
|
|
|
|
uint32_t status = lpc288x_system_ready(bank); /* probed? halted? */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if (status != ERROR_OK) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
LOG_INFO("Processor not halted/not probed");
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static int lpc288x_erase(struct flash_bank *bank, int first, int last)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2009-06-18 02:10:25 -05:00
|
|
|
|
uint32_t status;
|
2008-07-18 06:38:23 -05:00
|
|
|
|
int sector;
|
2009-11-13 12:11:13 -06:00
|
|
|
|
struct target *target = bank->target;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
status = lpc288x_system_ready(bank); /* probed? halted? */
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if (status != ERROR_OK)
|
|
|
|
|
return status;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if ((first < 0) || (last < first) || (last >= bank->num_sectors)) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
LOG_INFO("Bad sector range");
|
|
|
|
|
return ERROR_FLASH_SECTOR_INVALID;
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* Configure the flash controller timing */
|
|
|
|
|
lpc288x_set_flash_clk(bank);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
for (sector = first; sector <= last; sector++) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
|
|
|
|
|
return ERROR_FLASH_OPERATION_FAILED;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
lpc288x_load_timer(LOAD_TIMER_ERASE, target);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
target_write_u32(target, bank->sectors[sector].offset, 0x00);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_CS);
|
|
|
|
|
}
|
|
|
|
|
if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
|
|
|
|
|
return ERROR_FLASH_OPERATION_FAILED;
|
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static int lpc288x_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2009-06-18 02:06:25 -05:00
|
|
|
|
uint8_t page_buffer[FLASH_PAGE_SIZE];
|
2012-01-31 11:55:03 -06:00
|
|
|
|
uint32_t status, source_offset, dest_offset;
|
2009-11-13 12:11:13 -06:00
|
|
|
|
struct target *target = bank->target;
|
2009-06-18 02:10:25 -05:00
|
|
|
|
uint32_t bytes_remaining = count;
|
|
|
|
|
uint32_t first_sector, last_sector, sector, page;
|
2009-04-19 03:16:58 -05:00
|
|
|
|
int i;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* probed? halted? */
|
|
|
|
|
status = lpc288x_system_ready(bank);
|
|
|
|
|
if (status != ERROR_OK)
|
|
|
|
|
return status;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* Initialise search indices */
|
|
|
|
|
first_sector = last_sector = 0xffffffff;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* validate the write range... */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
for (i = 0; i < bank->num_sectors; i++) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if ((offset >= bank->sectors[i].offset) &&
|
2012-01-31 11:55:03 -06:00
|
|
|
|
(offset < (bank->sectors[i].offset + bank->sectors[i].size)) &&
|
|
|
|
|
(first_sector == 0xffffffff)) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
first_sector = i;
|
|
|
|
|
/* all writes must start on a sector boundary... */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if (offset % bank->sectors[i].size) {
|
|
|
|
|
LOG_INFO(
|
|
|
|
|
"offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "",
|
|
|
|
|
offset,
|
|
|
|
|
bank->sectors[i].size);
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (((offset + count) > bank->sectors[i].offset) &&
|
2012-01-31 11:55:03 -06:00
|
|
|
|
((offset + count) <= (bank->sectors[i].offset + bank->sectors[i].size)) &&
|
|
|
|
|
(last_sector == 0xffffffff))
|
2008-07-18 06:38:23 -05:00
|
|
|
|
last_sector = i;
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* Range check... */
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if (first_sector == 0xffffffff || last_sector == 0xffffffff) {
|
2009-06-20 22:20:46 -05:00
|
|
|
|
LOG_INFO("Range check failed %" PRIx32 " %" PRIx32 "", offset, count);
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_FLASH_DST_OUT_OF_BANK;
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* Configure the flash controller timing */
|
|
|
|
|
lpc288x_set_flash_clk(bank);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* initialise the offsets */
|
|
|
|
|
source_offset = 0;
|
|
|
|
|
dest_offset = 0;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
for (sector = first_sector; sector <= last_sector; sector++) {
|
|
|
|
|
for (page = 0; page < bank->sectors[sector].size / FLASH_PAGE_SIZE; page++) {
|
|
|
|
|
if (bytes_remaining == 0) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
count = 0;
|
|
|
|
|
memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
|
2012-01-31 11:55:03 -06:00
|
|
|
|
} else if (bytes_remaining < FLASH_PAGE_SIZE) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
count = bytes_remaining;
|
|
|
|
|
memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
|
|
|
|
|
memcpy(page_buffer, &buffer[source_offset], count);
|
2012-01-31 11:55:03 -06:00
|
|
|
|
} else {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
count = FLASH_PAGE_SIZE;
|
|
|
|
|
memcpy(page_buffer, &buffer[source_offset], count);
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* Wait for flash to become ready */
|
|
|
|
|
if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
|
|
|
|
|
return ERROR_FLASH_OPERATION_FAILED;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* fill flash data latches with 1's */
|
|
|
|
|
target_write_u32(target, F_CTRL, FC_CS | FC_SET_DATA | FC_WEN | FC_FUNC);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
target_write_u32(target, F_CTRL, FC_CS | FC_WEN | FC_FUNC);
|
2013-10-04 17:24:28 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if (target_write_buffer(target, offset + dest_offset, FLASH_PAGE_SIZE,
|
|
|
|
|
page_buffer) != ERROR_OK) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
LOG_INFO("Write to flash buffer failed");
|
|
|
|
|
return ERROR_FLASH_OPERATION_FAILED;
|
|
|
|
|
}
|
2013-10-04 17:24:28 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
dest_offset += FLASH_PAGE_SIZE;
|
|
|
|
|
source_offset += count;
|
|
|
|
|
bytes_remaining -= count;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
lpc288x_load_timer(LOAD_TIMER_WRITE, target);
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_FUNC |
|
|
|
|
|
FC_CS);
|
2008-07-18 06:38:23 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static int lpc288x_probe(struct flash_bank *bank)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* we only deal with LPC2888 so flash config is fixed */
|
2009-11-13 09:38:05 -06:00
|
|
|
|
struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
|
2008-07-18 06:38:23 -05:00
|
|
|
|
int retval;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if (lpc288x_info->cidr != 0)
|
2012-01-31 11:55:03 -06:00
|
|
|
|
return ERROR_OK;/* already probed */
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
if (bank->target->state != TARGET_HALTED) {
|
2008-08-17 14:40:17 -05:00
|
|
|
|
LOG_ERROR("Target not halted");
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_TARGET_NOT_HALTED;
|
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
retval = lpc288x_read_part_info(bank);
|
|
|
|
|
if (retval != ERROR_OK)
|
|
|
|
|
return retval;
|
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 13:32:28 -06:00
|
|
|
|
static int lpc288x_protect(struct flash_bank *bank, int set, int first, int last)
|
2008-04-21 03:18:19 -05:00
|
|
|
|
{
|
2008-07-18 06:38:23 -05:00
|
|
|
|
int lockregion, status;
|
2009-06-18 02:10:25 -05:00
|
|
|
|
uint32_t value;
|
2009-11-13 12:11:13 -06:00
|
|
|
|
struct target *target = bank->target;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* probed? halted? */
|
2009-05-31 22:05:26 -05:00
|
|
|
|
status = lpc288x_system_ready(bank);
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if (status != ERROR_OK)
|
|
|
|
|
return status;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
if ((first < 0) || (last < first) || (last >= bank->num_sectors))
|
|
|
|
|
return ERROR_FLASH_SECTOR_INVALID;
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* Configure the flash controller timing */
|
2009-05-31 22:05:26 -05:00
|
|
|
|
lpc288x_set_flash_clk(bank);
|
|
|
|
|
|
2012-01-31 11:55:03 -06:00
|
|
|
|
for (lockregion = first; lockregion <= last; lockregion++) {
|
|
|
|
|
if (set) {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* write an odd value to base addy to protect... */
|
|
|
|
|
value = 0x01;
|
2012-01-31 11:55:03 -06:00
|
|
|
|
} else {
|
2008-07-18 06:38:23 -05:00
|
|
|
|
/* write an even value to base addy to unprotect... */
|
|
|
|
|
value = 0x00;
|
|
|
|
|
}
|
|
|
|
|
target_write_u32(target, bank->sectors[lockregion].offset, value);
|
2012-01-31 11:55:03 -06:00
|
|
|
|
target_write_u32(target, F_CTRL, FC_LOAD_REQ | FC_PROTECT | FC_WEN | FC_FUNC |
|
|
|
|
|
FC_CS);
|
2008-07-18 06:38:23 -05:00
|
|
|
|
}
|
2009-05-31 22:05:26 -05:00
|
|
|
|
|
2008-07-18 06:38:23 -05:00
|
|
|
|
return ERROR_OK;
|
2008-04-21 03:18:19 -05:00
|
|
|
|
}
|
2009-11-09 11:20:33 -06:00
|
|
|
|
|
2009-11-13 09:38:01 -06:00
|
|
|
|
struct flash_driver lpc288x_flash = {
|
2010-01-29 15:52:08 -06:00
|
|
|
|
.name = "lpc288x",
|
|
|
|
|
.flash_bank_command = lpc288x_flash_bank_command,
|
|
|
|
|
.erase = lpc288x_erase,
|
|
|
|
|
.protect = lpc288x_protect,
|
|
|
|
|
.write = lpc288x_write,
|
2010-05-10 22:16:33 -05:00
|
|
|
|
.read = default_flash_read,
|
2010-01-29 15:52:08 -06:00
|
|
|
|
.probe = lpc288x_probe,
|
|
|
|
|
.auto_probe = lpc288x_probe,
|
|
|
|
|
.erase_check = lpc288x_erase_check,
|
|
|
|
|
.protect_check = lpc288x_protect_check,
|
|
|
|
|
};
|