36 #define ECC_OFFS 0x120
37 #define SPARE_OFFS 0x140
38 #define DATA_OFFS 0x200
41 10, 11, 12, 13, 14, 15
44 40, 41, 42, 43, 44, 45,
45 46, 47, 48, 49, 50, 51,
46 52, 53, 54, 55, 56, 57,
47 58, 59, 60, 61, 62, 63
71 nand->controller_priv = lpc32xx_info;
76 LOG_WARNING(
"LPC32xx oscillator frequency should be between "
77 "1000 and 20000 kHz, was %i",
90 int bypass = (pll_ctrl & 0x8000) >> 15;
91 int direct = (pll_ctrl & 0x4000) >> 14;
92 int feedback = (pll_ctrl & 0x2000) >> 13;
93 int p = (1 << ((pll_ctrl & 0x1800) >> 11) * 2);
94 int n = ((pll_ctrl & 0x0600) >> 9) + 1;
95 int m = ((pll_ctrl & 0x01fe) >> 1) + 1;
96 int lock = (pll_ctrl & 0x1);
101 if (!bypass && direct)
102 return (m * fclkin) / n;
104 if (bypass && !direct)
105 return fclkin / (2 * p);
111 return m * (fclkin / n);
113 return (m / (2 * p)) * (fclkin / n);
120 uint32_t sysclk_ctrl, pwr_ctrl, hclkdiv_ctrl, hclkpll_ctrl;
136 if ((sysclk_ctrl & 1) == 0)
148 if ((pwr_ctrl & (1 << 2)) == 0)
153 LOG_ERROR(
"could not read HCLKPLL_CTRL");
164 if (pwr_ctrl & (1 << 10))
165 hclk = hclk_pll / (((hclkdiv_ctrl & 0x7c) >> 2) + 1);
167 hclk = hclk_pll / (1 << (hclkdiv_ctrl & 0x3));
170 LOG_DEBUG(
"LPC32xx HCLK currently clocked at %i kHz", hclk);
172 cycle = (1.0 / hclk) * 1000000.0;
187 LOG_ERROR(
"target must be halted to use LPC32xx "
188 "NAND flash controller");
193 if (bus_width != 8) {
194 LOG_ERROR(
"LPC32xx doesn't support %i", bus_width);
201 if ((address_cycles < 3) || (address_cycles > 5)) {
202 LOG_ERROR(
"LPC32xx driver doesn't support %i address cycles", address_cycles);
206 if ((page_size != 512) && (page_size != 2048)) {
207 LOG_ERROR(
"LPC32xx doesn't support page size %i", page_size);
213 LOG_DEBUG(
"no LPC32xx NAND flash controller selected, "
214 "using default 'slc'");
219 uint32_t mlc_icr_value = 0x0;
221 int twp, twh, trp, treh, trhz, trbwb, tcea;
226 LOG_ERROR(
"could not set FLASHCLK_CTRL");
246 mlc_icr_value |= 0x8;
247 if (page_size == 2048)
248 mlc_icr_value |= 0x4;
249 if (address_cycles == 4)
250 mlc_icr_value |= 0x2;
252 mlc_icr_value |= 0x1;
262 twp = ((40 / cycle) + 1);
263 twh = ((20 / cycle) + 1);
264 trp = ((30 / cycle) + 1);
265 treh = ((15 / cycle) + 1);
266 trhz = ((30 / cycle) + 1);
267 trbwb = ((100 / cycle) + 1);
268 tcea = ((45 / cycle) + 1);
282 | ((treh & 0xf) << 12)
283 | ((trhz & 0x7) << 16)
284 | ((trbwb & 0x1f) << 19)
285 | ((tcea & 0x3) << 24));
296 int r_setup, r_hold, r_width, r_rdy;
297 int w_setup, w_hold, w_width, w_rdy;
302 LOG_ERROR(
"could not set FLASHCLK_CTRL");
322 0x3e | ((bus_width == 16) ? 1 : 0));
354 r_setup = w_setup = 0;
355 r_hold = w_hold = 10 / cycle;
356 r_width = 30 / cycle;
357 w_width = 40 / cycle;
358 r_rdy = w_rdy = 100 / cycle;
363 | ((r_hold & 0xf) << 4)
364 | ((r_width & 0xf) << 8)
365 | ((r_rdy & 0xf) << 12)
366 | ((w_setup & 0xf) << 16)
367 | ((w_hold & 0xf) << 20)
368 | ((w_width & 0xf) << 24)
369 | ((w_rdy & 0xf) << 28));
386 LOG_ERROR(
"target must be halted to use "
387 "LPC32xx NAND flash controller");
392 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
403 LOG_ERROR(
"LPC32xx MLC NAND controller timed out "
416 LOG_ERROR(
"LPC32xx SLC NAND controller timed out "
432 LOG_ERROR(
"target must be halted to use "
433 "LPC32xx NAND flash controller");
438 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
466 LOG_ERROR(
"target must be halted to use "
467 "LPC32xx NAND flash controller");
472 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
500 LOG_ERROR(
"target must be halted to use "
501 "LPC32xx NAND flash controller");
506 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
534 LOG_ERROR(
"target must be halted to use LPC32xx "
535 "NAND flash controller");
540 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
545 uint8_t *data8 = data;
548 LOG_ERROR(
"BUG: bus_width neither 8 nor 16 bit");
566 uint8_t *data8 = data;
567 *data8 = data32 & 0xff;
569 LOG_ERROR(
"BUG: bus_width neither 8 nor 16 bit");
578 uint8_t *data, uint32_t data_size,
579 uint8_t *oob, uint32_t oob_size)
584 static uint8_t page_buffer[512];
585 static uint8_t oob_buffer[6];
586 int quarter, num_quarters;
618 (page >> 16) & 0xff);
655 num_quarters = (nand->
page_size == 2048) ? 4 : 1;
657 for (quarter = 0; quarter < num_quarters; quarter++) {
658 int thisrun_data_size = (data_size > 512) ? 512 : data_size;
659 int thisrun_oob_size = (oob_size > 6) ? 6 : oob_size;
661 memset(page_buffer, 0xff, 512);
663 memcpy(page_buffer, data, thisrun_data_size);
664 data_size -= thisrun_data_size;
665 data += thisrun_data_size;
668 memset(oob_buffer, 0xff, 6);
670 memcpy(oob_buffer, oob, thisrun_oob_size);
671 oob_size -= thisrun_oob_size;
672 oob += thisrun_oob_size;
678 LOG_ERROR(
"could not set MLC_ECC_ENC_REG");
683 4, 128, page_buffer);
685 LOG_ERROR(
"could not set MLC_BUF (data)");
691 LOG_ERROR(
"could not set MLC_BUF (oob)");
698 LOG_ERROR(
"could not set MLC_ECC_AUTO_ENC_REG");
704 "completion of auto encode cycle");
723 LOG_ERROR(
"write operation didn't pass, status: 0x%2.2x",
739 uint32_t i, dmasrc,
ctrl, ecc_ctrl, oob_ctrl, dmadst;
781 ctrl = (0x40 | 3 << 12 | 3 << 15 | 2 << 18 | 2 << 21 | 0 << 24
782 | 0 << 25 | 0 << 26 | 0 << 27 | 0 << 31);
796 ecc_ctrl = 0x01 | 1 << 12 | 1 << 15 | 2 << 18 | 2 << 21 | 0 << 24
797 | 0 << 25 | 0 << 26 | 1 << 27 | 0 << 31;
811 oob_ctrl = (page_size == 2048 ? 0x10 : 0x04)
812 | 3 << 12 | 3 << 15 | 2 << 18 | 2 << 21 | 0 << 24
813 | 0 << 25 | 0 << 26 | 0 << 27 | 1 << 31;
853 for (i = 0; i < page_size/0x100; i++) {
857 target_mem_base + (i*2 + 1) *
sizeof(
struct dmac_ll);
860 dmalist[(i*2) + 1].dma_src = 0x20020034;
864 target_mem_base + (i*2 + 2) *
sizeof(
struct dmac_ll);
892 LOG_ERROR(
"Could not set DMACIntTCClear");
899 LOG_ERROR(
"Could not set DMACIntErrClear");
914 1 | 1<<1 | 1<<6 | 2<<11 | 0<<14
915 | 0<<15 | 0<<16 | 0<<18);
938 LOG_ERROR(
"lpc32xx_start_slc_dma: Could not set SLC_TC");
944 LOG_ERROR(
"timeout while waiting for completion of DMA");
965 LOG_ERROR(
"Could not read DMACRawIntTCStat");
971 LOG_ERROR(
"Could not read DMACRawIntErrStat");
974 if ((tc_stat | err_stat) & 1) {
979 "DMA error, aborted");
995 for (i = 0; i < (
count * 3); i += 3) {
996 uint32_t ce =
ecc[i/3];
997 ce = ~(ce << 2) & 0xFFFFFF;
998 spare[i+2] = (uint8_t)(ce & 0xFF); ce >>= 8;
999 spare[i+1] = (uint8_t)(ce & 0xFF); ce >>= 8;
1000 spare[i] = (uint8_t)(ce & 0xFF);
1008 while (oob_size > 0) {
1009 LOG_DEBUG(
"%02x: %02x %02x %02x %02x %02x %02x %02x %02x",
addr,
1010 oob[0], oob[1], oob[2], oob[3],
1011 oob[4], oob[5], oob[6], oob[7]);
1020 uint32_t page, uint8_t *data,
1021 uint32_t data_size, uint8_t *oob,
1026 uint32_t target_mem_base;
1028 LOG_DEBUG(
"SLC write page %" PRIx32
" data=%d, oob=%d, "
1029 "data_size=%" PRIu32
", oob_size=%" PRIu32,
1030 page, !!data, !!oob, data_size, oob_size);
1032 target_mem_base = pworking_area->
address;
1038 uint32_t i, all_ff = 1;
1039 for (i = 0; i < data_size; i++)
1040 if (data[i] != 0xFF) {
1053 nll *
sizeof(
struct dmac_ll) / 4,
1056 LOG_ERROR(
"Could not write DMA descriptors to IRAM");
1081 static uint8_t fdata[2048];
1083 memcpy(fdata, data, data_size);
1088 LOG_ERROR(
"Could not write data to IRAM");
1097 LOG_ERROR(
"Could not write DMA descriptor to DMAC");
1103 tot_size += tot_size == 2048 ? 64 : 16;
1112 LOG_ERROR(
"Data DMA failed during write");
1118 static uint8_t foob[64];
1119 int foob_size = nand->
page_size == 2048 ? 64 : 16;
1120 memset(foob, 0xFF, foob_size);
1122 memcpy(foob, oob, oob_size);
1125 int ecc_count = nand->
page_size == 2048 ? 8 : 2;
1126 static uint32_t hw_ecc[8];
1128 4, ecc_count, (uint8_t *)hw_ecc);
1130 LOG_ERROR(
"Reading hw generated ECC from IRAM failed");
1134 static uint8_t
ecc[24];
1138 for (i = 0; i < ecc_count * 3; i++)
1139 foob[layout[i]] =
ecc[i];
1143 foob_size / 4, foob);
1145 LOG_ERROR(
"Writing OOB to IRAM failed");
1152 (uint8_t *)(&
dmalist[nll-1]));
1154 LOG_ERROR(
"Could not write OOB DMA descriptor to DMAC");
1164 LOG_ERROR(
"Could not set DMACIntTCClear");
1178 1 | 1<<1 | 1<<6 | 2<<11 | 0<<14
1179 | 0<<15 | 0<<16 | 0<<18);
1181 LOG_ERROR(
"Could not set DMACC0Config");
1187 "completion of DMA");
1210 uint8_t *data, uint32_t data_size,
1211 uint8_t *oob, uint32_t oob_size)
1218 LOG_ERROR(
"target must be halted to use LPC32xx "
1219 "NAND flash controller");
1224 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
1228 LOG_ERROR(
"LPC32xx MLC controller can't write "
1233 if (oob && (oob_size > 24)) {
1234 LOG_ERROR(
"LPC32xx MLC controller can't write more "
1235 "than 6 bytes for each quarter's OOB data");
1239 if (data_size > (uint32_t)nand->
page_size) {
1240 LOG_ERROR(
"data size exceeds page size");
1255 data_size, oob, oob_size);
1261 LOG_ERROR(
"Can't allocate working area in "
1262 "LPC internal RAM");
1266 data, data_size, oob, oob_size);
1274 uint8_t *data, uint32_t data_size,
1275 uint8_t *oob, uint32_t oob_size)
1278 static uint8_t page_buffer[2048];
1279 static uint8_t oob_buffer[64];
1280 uint32_t page_bytes_done = 0;
1281 uint32_t oob_bytes_done = 0;
1317 (page >> 8) & 0xff);
1325 (page >> 16) & 0xff);
1352 (page >> 8) & 0xff);
1367 while (page_bytes_done < (uint32_t)nand->
page_size) {
1371 LOG_ERROR(
"could not set MLC_ECC_AUTO_DEC_REG");
1377 "completion of auto decode cycle");
1387 if (mlc_isr & 0x8) {
1388 if (mlc_isr & 0x40) {
1389 LOG_ERROR(
"uncorrectable error detected: 0x%2.2" PRIx32, mlc_isr);
1393 LOG_WARNING(
"%i symbol error detected and corrected",
1394 ((
int)(((mlc_isr & 0x30) >> 4) + 1)));
1399 page_buffer + page_bytes_done);
1401 LOG_ERROR(
"could not read MLC_BUF (data)");
1408 oob_buffer + oob_bytes_done);
1410 LOG_ERROR(
"could not read MLC_BUF (oob)");
1415 page_bytes_done += 512;
1416 oob_bytes_done += 16;
1420 memcpy(data, page_buffer, data_size);
1423 memcpy(oob, oob_buffer, oob_size);
1430 uint32_t page, uint8_t *data,
1431 uint32_t data_size, uint8_t *oob,
1436 uint32_t target_mem_base;
1438 LOG_DEBUG(
"SLC read page %" PRIx32
" data=%" PRIu32
", oob=%" PRIu32,
1439 page, data_size, oob_size);
1441 target_mem_base = pworking_area->
address;
1449 nll *
sizeof(
struct dmac_ll) / 4,
1452 LOG_ERROR(
"Could not write DMA descriptors to IRAM");
1458 LOG_ERROR(
"lpc32xx_read_page_slc: NAND_CMD_READ0 failed");
1472 LOG_ERROR(
"lpc32xx_read_page_slc: Could not set SLC_CFG");
1480 LOG_ERROR(
"Could not write DMA descriptor to DMAC");
1486 tot_size += nand->
page_size == 2048 ? 64 : 16;
1489 LOG_ERROR(
"lpc32xx_read_page_slc: DMA read failed");
1496 4, data_size/4, data);
1498 LOG_ERROR(
"Could not read data from IRAM");
1508 LOG_ERROR(
"Could not read OOB from IRAM");
1515 static uint8_t foob[64];
1517 4, nand->
page_size == 2048 ? 16 : 4, foob);
1520 LOG_ERROR(
"Could not read OOB from IRAM");
1524 int ecc_count = nand->
page_size == 2048 ? 8 : 2;
1525 static uint32_t hw_ecc[8];
1527 ecc_count, (uint8_t *)hw_ecc);
1529 LOG_ERROR(
"Could not read hw generated ECC from IRAM");
1532 static uint8_t
ecc[24];
1535 static uint8_t fecc[24];
1538 for (i = 0; i < ecc_count * 3; i++)
1539 fecc[i] = foob[layout[i]];
1541 for (i = 0; i < ecc_count; i++) {
1545 LOG_WARNING(
"error detected and corrected: %" PRIu32
"/%d",
1553 LOG_ERROR(
"uncorrectable error detected: %" PRIu32
"/%d", page, i);
1560 uint8_t *data, uint32_t data_size,
1561 uint8_t *oob, uint32_t oob_size)
1568 LOG_ERROR(
"target must be halted to use LPC32xx "
1569 "NAND flash controller");
1574 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
1577 if (data_size > (uint32_t)nand->
page_size) {
1578 LOG_ERROR(
"data size exceeds page size");
1590 LOG_ERROR(
"Can't allocate working area in "
1591 "LPC internal RAM");
1595 data, data_size, oob, oob_size);
1609 LOG_ERROR(
"target must be halted to use LPC32xx "
1610 "NAND flash controller");
1628 LOG_DEBUG(
"lpc32xx_controller_ready count=%d",
1643 LOG_DEBUG(
"lpc32xx_controller_ready count=%d",
1662 LOG_ERROR(
"target must be halted to use LPC32xx "
1663 "NAND flash controller");
1682 LOG_DEBUG(
"lpc32xx_nand_ready count end=%d",
1697 LOG_DEBUG(
"lpc32xx_nand_ready count end=%d",
1721 LOG_ERROR(
"Could not read SLC_INT_STAT");
1738 char *selected[] = {
1757 if (strcmp(
CMD_ARGV[1],
"mlc") == 0) {
1760 }
else if (strcmp(
CMD_ARGV[1],
"slc") == 0) {
1776 .handler = handle_lpc32xx_select_command,
1778 .help =
"select MLC or SLC controller (default is MLC)",
1779 .usage =
"bank_id ['mlc'|'slc' ]",
1787 .help =
"LPC32xx NAND flash controller commands",
1797 .nand_device_command = lpc32xx_nand_device_command,
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_FLASH_OPERATION_FAILED
int nand_read_status(struct nand_device *nand, uint8_t *status)
int nand_write_finish(struct nand_device *nand)
int nand_write_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
struct nand_device * get_nand_device_by_num(int num)
int nand_page_command(struct nand_device *nand, uint32_t page, uint8_t cmd, bool oob_only)
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
static int lpc32xx_tc_ready(struct nand_device *nand, int timeout)
static int lpc32xx_make_dma_list(uint32_t target_mem_base, uint32_t page_size, int do_read)
static int lpc32xx_pll(int fclkin, uint32_t pll_ctrl)
static int lpc32xx_write_page_slc(struct nand_device *nand, struct working_area *pworking_area, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
static const struct command_registration lpc32xx_command_handler[]
static int lpc32xx_write_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
static int lpc32xx_read_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
static const int sp_ooblayout[]
static int lpc32xx_controller_ready(struct nand_device *nand, int timeout)
static int lpc32xx_address(struct nand_device *nand, uint8_t address)
static const int lp_ooblayout[]
static int lpc32xx_read_data(struct nand_device *nand, void *data)
static float lpc32xx_cycle_time(struct nand_device *nand)
static int lpc32xx_init(struct nand_device *nand)
static int lpc32xx_read_page_mlc(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
static int lpc32xx_start_slc_dma(struct nand_device *nand, uint32_t count, int do_wait)
static const struct command_registration lpc32xx_exec_command_handlers[]
static int lpc32xx_write_data(struct nand_device *nand, uint16_t data)
static int lpc32xx_reset(struct nand_device *nand)
static struct dmac_ll dmalist[(2048/256) *2+1]
COMMAND_HANDLER(handle_lpc32xx_select_command)
static void lpc32xx_dump_oob(uint8_t *oob, uint32_t oob_size)
static int lpc32xx_write_page_mlc(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
static int lpc32xx_dma_ready(struct nand_device *nand, int timeout)
static int lpc32xx_nand_ready(struct nand_device *nand, int timeout)
static uint32_t slc_ecc_copy_to_buffer(uint8_t *spare, const uint32_t *ecc, int count)
NAND_DEVICE_COMMAND_HANDLER(lpc32xx_nand_device_command)
static int lpc32xx_read_page_slc(struct nand_device *nand, struct working_area *pworking_area, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
static int lpc32xx_command(struct nand_device *nand, uint8_t command)
#define ERROR_NAND_OPERATION_TIMEOUT
#define ERROR_NAND_OPERATION_FAILED
int nand_correct_data(struct nand_device *nand, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
nand_correct_data - Detect and correct a 1 bit error for 256 byte block
#define ERROR_NAND_OPERATION_NOT_SUPPORTED
target_addr_t addr
Start address to search for the control block.
struct rtt_control ctrl
Control block.
volatile uint32_t next_lli
volatile uint32_t dma_dest
volatile uint32_t dma_src
volatile uint32_t next_ctrl
uint32_t sw_wp_upper_bound
uint32_t sw_wp_lower_bound
enum lpc32xx_selected_controller selected_controller
Interface for NAND flash controllers.
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.