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;
135 if ((sysclk_ctrl & 1) == 0)
147 if ((pwr_ctrl & (1 << 2)) == 0)
152 LOG_ERROR(
"could not read HCLKPLL_CTRL");
163 if (pwr_ctrl & (1 << 10))
164 hclk = hclk_pll / (((hclkdiv_ctrl & 0x7c) >> 2) + 1);
166 hclk = hclk_pll / (1 << (hclkdiv_ctrl & 0x3));
169 LOG_DEBUG(
"LPC32xx HCLK currently clocked at %i kHz", hclk);
171 return (1.0 / hclk) * 1000000.0;
184 LOG_ERROR(
"target must be halted to use LPC32xx "
185 "NAND flash controller");
190 if (bus_width != 8) {
191 LOG_ERROR(
"LPC32xx doesn't support %i", bus_width);
198 if ((address_cycles < 3) || (address_cycles > 5)) {
199 LOG_ERROR(
"LPC32xx driver doesn't support %i address cycles", address_cycles);
210 LOG_DEBUG(
"no LPC32xx NAND flash controller selected, "
211 "using default 'slc'");
216 uint32_t mlc_icr_value = 0x0;
218 int twp, twh, trp, treh, trhz, trbwb, tcea;
223 LOG_ERROR(
"could not set FLASHCLK_CTRL");
243 mlc_icr_value |= 0x8;
245 mlc_icr_value |= 0x4;
246 if (address_cycles == 4)
247 mlc_icr_value |= 0x2;
249 mlc_icr_value |= 0x1;
259 twp = ((40 / cycle) + 1);
260 twh = ((20 / cycle) + 1);
261 trp = ((30 / cycle) + 1);
262 treh = ((15 / cycle) + 1);
263 trhz = ((30 / cycle) + 1);
264 trbwb = ((100 / cycle) + 1);
265 tcea = ((45 / cycle) + 1);
279 | ((treh & 0xf) << 12)
280 | ((trhz & 0x7) << 16)
281 | ((trbwb & 0x1f) << 19)
282 | ((tcea & 0x3) << 24));
293 int r_setup, r_hold, r_width, r_rdy;
294 int w_setup, w_hold, w_width, w_rdy;
299 LOG_ERROR(
"could not set FLASHCLK_CTRL");
319 0x3e | ((bus_width == 16) ? 1 : 0));
351 r_setup = w_setup = 0;
352 r_hold = w_hold = 10 / cycle;
353 r_width = 30 / cycle;
354 w_width = 40 / cycle;
355 r_rdy = w_rdy = 100 / cycle;
360 | ((r_hold & 0xf) << 4)
361 | ((r_width & 0xf) << 8)
362 | ((r_rdy & 0xf) << 12)
363 | ((w_setup & 0xf) << 16)
364 | ((w_hold & 0xf) << 20)
365 | ((w_width & 0xf) << 24)
366 | ((w_rdy & 0xf) << 28));
383 LOG_ERROR(
"target must be halted to use "
384 "LPC32xx NAND flash controller");
389 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
400 LOG_ERROR(
"LPC32xx MLC NAND controller timed out "
413 LOG_ERROR(
"LPC32xx SLC NAND controller timed out "
429 LOG_ERROR(
"target must be halted to use "
430 "LPC32xx NAND flash controller");
435 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
463 LOG_ERROR(
"target must be halted to use "
464 "LPC32xx NAND flash controller");
469 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
497 LOG_ERROR(
"target must be halted to use "
498 "LPC32xx NAND flash controller");
503 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
531 LOG_ERROR(
"target must be halted to use LPC32xx "
532 "NAND flash controller");
537 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
542 uint8_t *data8 = data;
545 LOG_ERROR(
"BUG: bus_width neither 8 nor 16 bit");
563 uint8_t *data8 = data;
564 *data8 = data32 & 0xff;
566 LOG_ERROR(
"BUG: bus_width neither 8 nor 16 bit");
575 uint8_t *data, uint32_t data_size,
576 uint8_t *oob, uint32_t oob_size)
581 static uint8_t page_buffer[512];
582 static uint8_t oob_buffer[6];
583 int quarter, num_quarters;
615 (page >> 16) & 0xff);
652 num_quarters = (nand->
page_size == 2048) ? 4 : 1;
654 for (quarter = 0; quarter < num_quarters; quarter++) {
655 int thisrun_data_size = (data_size > 512) ? 512 : data_size;
656 int thisrun_oob_size = (oob_size > 6) ? 6 : oob_size;
658 memset(page_buffer, 0xff, 512);
660 memcpy(page_buffer, data, thisrun_data_size);
661 data_size -= thisrun_data_size;
662 data += thisrun_data_size;
665 memset(oob_buffer, 0xff, 6);
667 memcpy(oob_buffer, oob, thisrun_oob_size);
668 oob_size -= thisrun_oob_size;
669 oob += thisrun_oob_size;
675 LOG_ERROR(
"could not set MLC_ECC_ENC_REG");
680 4, 128, page_buffer);
682 LOG_ERROR(
"could not set MLC_BUF (data)");
688 LOG_ERROR(
"could not set MLC_BUF (oob)");
695 LOG_ERROR(
"could not set MLC_ECC_AUTO_ENC_REG");
701 "completion of auto encode cycle");
720 LOG_ERROR(
"write operation didn't pass, status: 0x%2.2x",
736 uint32_t i, dmasrc,
ctrl, ecc_ctrl, oob_ctrl, dmadst;
778 ctrl = (0x40 | 3 << 12 | 3 << 15 | 2 << 18 | 2 << 21 | 0 << 24
779 | 0 << 25 | 0 << 26 | 0 << 27 | 0 << 31);
793 ecc_ctrl = 0x01 | 1 << 12 | 1 << 15 | 2 << 18 | 2 << 21 | 0 << 24
794 | 0 << 25 | 0 << 26 | 1 << 27 | 0 << 31;
808 oob_ctrl = (
page_size == 2048 ? 0x10 : 0x04)
809 | 3 << 12 | 3 << 15 | 2 << 18 | 2 << 21 | 0 << 24
810 | 0 << 25 | 0 << 26 | 0 << 27 | 1 << 31;
854 target_mem_base + (i*2 + 1) *
sizeof(
struct dmac_ll);
857 dmalist[(i*2) + 1].dma_src = 0x20020034;
861 target_mem_base + (i*2 + 2) *
sizeof(
struct dmac_ll);
889 LOG_ERROR(
"Could not set DMACIntTCClear");
896 LOG_ERROR(
"Could not set DMACIntErrClear");
911 1 | 1<<1 | 1<<6 | 2<<11 | 0<<14
912 | 0<<15 | 0<<16 | 0<<18);
935 LOG_ERROR(
"lpc32xx_start_slc_dma: Could not set SLC_TC");
941 LOG_ERROR(
"timeout while waiting for completion of DMA");
962 LOG_ERROR(
"Could not read DMACRawIntTCStat");
968 LOG_ERROR(
"Could not read DMACRawIntErrStat");
971 if ((tc_stat | err_stat) & 1) {
976 "DMA error, aborted");
992 for (i = 0; i < (
count * 3); i += 3) {
993 uint32_t ce =
ecc[i/3];
994 ce = ~(ce << 2) & 0xFFFFFF;
995 spare[i+2] = (uint8_t)(ce & 0xFF); ce >>= 8;
996 spare[i+1] = (uint8_t)(ce & 0xFF); ce >>= 8;
997 spare[i] = (uint8_t)(ce & 0xFF);
1005 while (oob_size > 0) {
1006 LOG_DEBUG(
"%02x: %02x %02x %02x %02x %02x %02x %02x %02x",
addr,
1007 oob[0], oob[1], oob[2], oob[3],
1008 oob[4], oob[5], oob[6], oob[7]);
1017 uint32_t page, uint8_t *data,
1018 uint32_t data_size, uint8_t *oob,
1023 uint32_t target_mem_base;
1025 LOG_DEBUG(
"SLC write page %" PRIx32
" data=%d, oob=%d, "
1026 "data_size=%" PRIu32
", oob_size=%" PRIu32,
1027 page, !!data, !!oob, data_size, oob_size);
1029 target_mem_base = pworking_area->
address;
1035 uint32_t i, all_ff = 1;
1036 for (i = 0; i < data_size; i++)
1037 if (data[i] != 0xFF) {
1050 nll *
sizeof(
struct dmac_ll) / 4,
1053 LOG_ERROR(
"Could not write DMA descriptors to IRAM");
1078 static uint8_t fdata[2048];
1080 memcpy(fdata, data, data_size);
1085 LOG_ERROR(
"Could not write data to IRAM");
1094 LOG_ERROR(
"Could not write DMA descriptor to DMAC");
1100 tot_size += tot_size == 2048 ? 64 : 16;
1109 LOG_ERROR(
"Data DMA failed during write");
1115 static uint8_t foob[64];
1116 int foob_size = nand->
page_size == 2048 ? 64 : 16;
1117 memset(foob, 0xFF, foob_size);
1119 memcpy(foob, oob, oob_size);
1122 int ecc_count = nand->
page_size == 2048 ? 8 : 2;
1123 static uint32_t hw_ecc[8];
1125 4, ecc_count, (uint8_t *)hw_ecc);
1127 LOG_ERROR(
"Reading hw generated ECC from IRAM failed");
1131 static uint8_t
ecc[24];
1135 for (i = 0; i < ecc_count * 3; i++)
1136 foob[layout[i]] =
ecc[i];
1140 foob_size / 4, foob);
1142 LOG_ERROR(
"Writing OOB to IRAM failed");
1149 (uint8_t *)(&
dmalist[nll-1]));
1151 LOG_ERROR(
"Could not write OOB DMA descriptor to DMAC");
1161 LOG_ERROR(
"Could not set DMACIntTCClear");
1175 1 | 1<<1 | 1<<6 | 2<<11 | 0<<14
1176 | 0<<15 | 0<<16 | 0<<18);
1178 LOG_ERROR(
"Could not set DMACC0Config");
1184 "completion of DMA");
1207 uint8_t *data, uint32_t data_size,
1208 uint8_t *oob, uint32_t oob_size)
1215 LOG_ERROR(
"target must be halted to use LPC32xx "
1216 "NAND flash controller");
1221 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
1225 LOG_ERROR(
"LPC32xx MLC controller can't write "
1230 if (oob && (oob_size > 24)) {
1231 LOG_ERROR(
"LPC32xx MLC controller can't write more "
1232 "than 6 bytes for each quarter's OOB data");
1236 if (data_size > (uint32_t)nand->
page_size) {
1237 LOG_ERROR(
"data size exceeds page size");
1252 data_size, oob, oob_size);
1258 LOG_ERROR(
"Can't allocate working area in "
1259 "LPC internal RAM");
1263 data, data_size, oob, oob_size);
1271 uint8_t *data, uint32_t data_size,
1272 uint8_t *oob, uint32_t oob_size)
1275 static uint8_t page_buffer[2048];
1276 static uint8_t oob_buffer[64];
1277 uint32_t page_bytes_done = 0;
1278 uint32_t oob_bytes_done = 0;
1314 (page >> 8) & 0xff);
1322 (page >> 16) & 0xff);
1349 (page >> 8) & 0xff);
1364 while (page_bytes_done < (uint32_t)nand->
page_size) {
1368 LOG_ERROR(
"could not set MLC_ECC_AUTO_DEC_REG");
1374 "completion of auto decode cycle");
1384 if (mlc_isr & 0x8) {
1385 if (mlc_isr & 0x40) {
1386 LOG_ERROR(
"uncorrectable error detected: 0x%2.2" PRIx32, mlc_isr);
1390 LOG_WARNING(
"%i symbol error detected and corrected",
1391 ((
int)(((mlc_isr & 0x30) >> 4) + 1)));
1396 page_buffer + page_bytes_done);
1398 LOG_ERROR(
"could not read MLC_BUF (data)");
1405 oob_buffer + oob_bytes_done);
1407 LOG_ERROR(
"could not read MLC_BUF (oob)");
1412 page_bytes_done += 512;
1413 oob_bytes_done += 16;
1417 memcpy(data, page_buffer, data_size);
1420 memcpy(oob, oob_buffer, oob_size);
1427 uint32_t page, uint8_t *data,
1428 uint32_t data_size, uint8_t *oob,
1433 uint32_t target_mem_base;
1435 LOG_DEBUG(
"SLC read page %" PRIx32
" data=%" PRIu32
", oob=%" PRIu32,
1436 page, data_size, oob_size);
1438 target_mem_base = pworking_area->
address;
1446 nll *
sizeof(
struct dmac_ll) / 4,
1449 LOG_ERROR(
"Could not write DMA descriptors to IRAM");
1455 LOG_ERROR(
"lpc32xx_read_page_slc: NAND_CMD_READ0 failed");
1469 LOG_ERROR(
"lpc32xx_read_page_slc: Could not set SLC_CFG");
1477 LOG_ERROR(
"Could not write DMA descriptor to DMAC");
1483 tot_size += nand->
page_size == 2048 ? 64 : 16;
1486 LOG_ERROR(
"lpc32xx_read_page_slc: DMA read failed");
1493 4, data_size/4, data);
1495 LOG_ERROR(
"Could not read data from IRAM");
1505 LOG_ERROR(
"Could not read OOB from IRAM");
1512 static uint8_t foob[64];
1514 4, nand->
page_size == 2048 ? 16 : 4, foob);
1517 LOG_ERROR(
"Could not read OOB from IRAM");
1521 int ecc_count = nand->
page_size == 2048 ? 8 : 2;
1522 static uint32_t hw_ecc[8];
1524 ecc_count, (uint8_t *)hw_ecc);
1526 LOG_ERROR(
"Could not read hw generated ECC from IRAM");
1529 static uint8_t
ecc[24];
1532 static uint8_t fecc[24];
1535 for (i = 0; i < ecc_count * 3; i++)
1536 fecc[i] = foob[layout[i]];
1538 for (i = 0; i < ecc_count; i++) {
1542 LOG_WARNING(
"error detected and corrected: %" PRIu32
"/%d",
1550 LOG_ERROR(
"uncorrectable error detected: %" PRIu32
"/%d", page, i);
1557 uint8_t *data, uint32_t data_size,
1558 uint8_t *oob, uint32_t oob_size)
1565 LOG_ERROR(
"target must be halted to use LPC32xx "
1566 "NAND flash controller");
1571 LOG_ERROR(
"BUG: no LPC32xx NAND flash controller selected");
1574 if (data_size > (uint32_t)nand->
page_size) {
1575 LOG_ERROR(
"data size exceeds page size");
1587 LOG_ERROR(
"Can't allocate working area in "
1588 "LPC internal RAM");
1592 data, data_size, oob, oob_size);
1606 LOG_ERROR(
"target must be halted to use LPC32xx "
1607 "NAND flash controller");
1625 LOG_DEBUG(
"lpc32xx_controller_ready count=%d",
1640 LOG_DEBUG(
"lpc32xx_controller_ready count=%d",
1659 LOG_ERROR(
"target must be halted to use LPC32xx "
1660 "NAND flash controller");
1679 LOG_DEBUG(
"lpc32xx_nand_ready count end=%d",
1694 LOG_DEBUG(
"lpc32xx_nand_ready count end=%d",
1718 LOG_ERROR(
"Could not read SLC_INT_STAT");
1735 char *selected[] = {
1754 if (strcmp(
CMD_ARGV[1],
"mlc") == 0) {
1757 }
else if (strcmp(
CMD_ARGV[1],
"slc") == 0) {
1773 .handler = handle_lpc32xx_select_command,
1775 .help =
"select MLC or SLC controller (default is MLC)",
1776 .usage =
"bank_id ['mlc'|'slc' ]",
1784 .help =
"LPC32xx NAND flash controller commands",
1794 .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.
uint32_t page_size
Page size.
uint32_t address
Starting address. Sector aligned.
#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.