23 #define ECC_OFFS 0x120
24 #define SPARE_OFFS 0x140
25 #define DATA_OFFS 0x200
39 nand->controller_priv = lpc3180_info;
45 "LPC3180 oscillator frequency should be between 1000 and 20000 kHz, was %i",
57 int bypass = (pll_ctrl & 0x8000) >> 15;
58 int direct = (pll_ctrl & 0x4000) >> 14;
59 int feedback = (pll_ctrl & 0x2000) >> 13;
60 int p = (1 << ((pll_ctrl & 0x1800) >> 11) * 2);
61 int n = ((pll_ctrl & 0x0600) >> 9) + 1;
62 int m = ((pll_ctrl & 0x01fe) >> 1) + 1;
63 int lock = (pll_ctrl & 0x1);
68 if (!bypass && direct)
69 return (m * fclkin) / n;
71 if (bypass && !direct)
72 return fclkin / (2 * p);
78 return m * (fclkin / n);
80 return (m / (2 * p)) * (fclkin / n);
87 uint32_t sysclk_ctrl, pwr_ctrl, hclkdiv_ctrl, hclkpll_ctrl;
98 if ((sysclk_ctrl & 1) == 0)
106 if ((pwr_ctrl & (1 << 2)) == 0)
114 if (pwr_ctrl & (1 << 10))
115 hclk = hclk_pll / (((hclkdiv_ctrl & 0x7c) >> 2) + 1);
117 hclk = hclk_pll / (1 << (hclkdiv_ctrl & 0x3));
120 LOG_DEBUG(
"LPC3180 HCLK currently clocked at %i kHz", hclk);
122 cycle = (1.0 / hclk) * 1000000.0;
136 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
141 if ((bus_width != 8) && (bus_width != 16)) {
142 LOG_ERROR(
"LPC3180 only supports 8 or 16 bit bus width, not %i", bus_width);
150 LOG_WARNING(
"LPC3180 only supports 8 bit bus width");
155 if ((address_cycles != 3) && (address_cycles != 4)) {
156 LOG_ERROR(
"LPC3180 only supports 3 or 4 address cycles, not %i", address_cycles);
160 if ((page_size != 512) && (page_size != 2048)) {
161 LOG_ERROR(
"LPC3180 only supports 512 or 2048 byte pages, not %i", page_size);
167 LOG_DEBUG(
"no LPC3180 NAND flash controller selected, using default 'mlc'");
172 uint32_t mlc_icr_value = 0x0;
174 int twp, twh, trp, treh, trhz, trbwb, tcea;
187 mlc_icr_value |= 0x8;
188 if (page_size == 2048)
189 mlc_icr_value |= 0x4;
190 if (address_cycles == 4)
191 mlc_icr_value |= 0x2;
193 mlc_icr_value |= 0x1;
199 twp = ((40 / cycle) + 1);
200 twh = ((20 / cycle) + 1);
201 trp = ((30 / cycle) + 1);
202 treh = ((15 / cycle) + 1);
203 trhz = ((30 / cycle) + 1);
204 trbwb = ((100 / cycle) + 1);
205 tcea = ((45 / cycle) + 1);
212 ((trp & 0xf) << 8) | ((treh & 0xf) << 12) | ((trhz & 0x7) << 16) |
213 ((trbwb & 0x1f) << 19) | ((tcea & 0x3) << 24));
218 int r_setup, r_hold, r_width, r_rdy;
219 int w_setup, w_hold, w_width, w_rdy;
244 r_setup = w_setup = 0;
245 r_hold = w_hold = 10 / cycle;
246 r_width = 30 / cycle;
247 w_width = 40 / cycle;
248 r_rdy = w_rdy = 100 / cycle;
252 ((r_width & 0xf) << 8) | ((r_rdy & 0xf) << 12) | ((w_setup & 0xf) << 16) |
253 ((w_hold & 0xf) << 20) | ((w_width & 0xf) << 24) | ((w_rdy & 0xf) << 28));
266 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
271 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
278 LOG_ERROR(
"LPC3180 NAND controller timed out after reset");
286 LOG_ERROR(
"LPC3180 NAND controller timed out after reset");
300 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
305 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
324 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
329 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
348 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
353 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
372 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
377 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
382 uint8_t *data8 = data;
385 uint16_t *data16 = data;
388 LOG_ERROR(
"BUG: bus_width neither 8 nor 16 bit");
398 uint8_t *data8 = data;
399 *data8 = data32 & 0xff;
401 uint16_t *data16 = data;
402 *data16 = data32 & 0xffff;
404 LOG_ERROR(
"BUG: bus_width neither 8 nor 16 bit");
423 uint8_t *page_buffer;
426 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
431 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
435 int quarter, num_quarters;
438 LOG_ERROR(
"LPC3180 MLC controller can't write OOB data only");
442 if (oob && (oob_size > 24)) {
443 LOG_ERROR(
"LPC3180 MLC controller can't write more "
444 "than 6 bytes for each quarter's OOB data");
448 if (data_size > (uint32_t)nand->
page_size) {
449 LOG_ERROR(
"data size exceeds page size");
456 page_buffer = malloc(512);
457 oob_buffer = malloc(6);
482 num_quarters = (nand->
page_size == 2048) ? 4 : 1;
484 for (quarter = 0; quarter < num_quarters; quarter++) {
485 int thisrun_data_size = (data_size > 512) ? 512 : data_size;
486 int thisrun_oob_size = (oob_size > 6) ? 6 : oob_size;
488 memset(page_buffer, 0xff, 512);
490 memcpy(page_buffer, data, thisrun_data_size);
491 data_size -= thisrun_data_size;
492 data += thisrun_data_size;
495 memset(oob_buffer, 0xff, 6);
497 memcpy(oob_buffer, oob, thisrun_oob_size);
498 oob_size -= thisrun_oob_size;
499 oob += thisrun_oob_size;
506 4, 128, page_buffer);
514 LOG_ERROR(
"timeout while waiting for completion of auto encode cycle");
563 int i = 0, target_mem_base;
564 uint8_t *ecc_flash_buffer;
586 LOG_ERROR(
"Reserve at least 0x%x physical target working area",
592 "Reserve the physical target working area at word boundary");
597 LOG_ERROR(
"no working area specified, can't read LPC internal flash");
603 page_buffer = malloc(2048);
605 page_buffer = malloc(512);
607 ecc_flash_buffer = malloc(64);
615 for (i = 0; i < nand->
page_size/0x100; i++) {
621 target_mem_base+0+i*32,
632 tmp = (target_mem_base+(1+i*2)*16)&0xfffffffc;
642 target_mem_base+12+i*32,
643 0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
648 0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
656 target_mem_base+20+i*32,
657 target_mem_base+
SPARE_OFFS+8+16*(i>>1)+(i%2)*4);
659 tmp = (target_mem_base+(2+i*2)*16)&0xfffffffc;
667 target_mem_base+28+i*32,
668 0x01 | 1<<12 | 1<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
671 }
else if (data && oob) {
682 (target_mem_base+32)&0xfffffffc);
684 (target_mem_base+32)&0xfffffffc);
693 2048 ? 512 : 128) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 |
694 1<<26 | 0<<27 | 0<<31);
698 2048 ? 512 : 128) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 |
699 1<<26 | 0<<27 | 0<<31);
701 }
else if (!data && oob)
723 target_mem_base+12+i*32,
725 2048 ? 0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
730 0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 |
731 0<<25 | 1<<26 | 0<<27 | 0<<31);
733 memset(ecc_flash_buffer, 0xff, 64);
735 memcpy(ecc_flash_buffer, oob, oob_size);
743 memset(page_buffer, 0xff, nand->
page_size == 2048 ? 2048 : 512);
744 memcpy(page_buffer, data, data_size);
753 free(ecc_flash_buffer);
762 1 | 1<<1 | 1<<6 | 2<<11 | 0<<14 | 0<<15 | 0<<16 | 0<<18);
773 (nand->
page_size == 2048 ? 0x10 : 0x04));
776 (nand->
page_size == 2048 ? 0x840 : 0x210));
781 LOG_ERROR(
"timeout while waiting for completion of DMA");
787 LOG_INFO(
"Page = 0x%" PRIx32
" was written.", page);
805 uint8_t *page_buffer;
808 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
813 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
817 uint32_t page_bytes_done = 0;
818 uint32_t oob_bytes_done = 0;
822 if (oob && (oob_size > 6)) {
823 LOG_ERROR(
"LPC3180 MLC controller can't read more than 6 bytes of OOB data");
828 if (data_size > (uint32_t)nand->
page_size) {
829 LOG_ERROR(
"data size exceeds page size");
834 page_buffer = malloc(2048);
835 oob_buffer = malloc(64);
837 page_buffer = malloc(512);
838 oob_buffer = malloc(16);
878 while (page_bytes_done < (uint32_t)nand->
page_size) {
883 LOG_ERROR(
"timeout while waiting for completion of auto decode cycle");
892 if (mlc_isr & 0x40) {
893 LOG_ERROR(
"uncorrectable error detected: 0x%2.2" PRIx32, mlc_isr);
899 LOG_WARNING(
"%i symbol error detected and corrected",
900 ((
int)(((mlc_isr & 0x30) >> 4) + 1)));
908 page_buffer + page_bytes_done);
915 oob_buffer + oob_bytes_done);
917 page_bytes_done += 512;
918 oob_bytes_done += 16;
922 memcpy(data, page_buffer, data_size);
925 memcpy(oob, oob_buffer, oob_size);
951 int retval, i, target_mem_base;
952 uint8_t *ecc_hw_buffer;
953 uint8_t *ecc_flash_buffer;
966 LOG_ERROR(
"Reserve at least 0x%x physical target working area",
972 "Reserve the physical target working area at word boundary");
977 LOG_ERROR(
"no working area specified, can't read LPC internal flash");
983 page_buffer = malloc(2048);
985 page_buffer = malloc(512);
987 ecc_hw_buffer = malloc(32);
988 ecc_flash_buffer = malloc(64);
995 for (i = 0; i < nand->
page_size/0x100; i++) {
1004 target_mem_base+4+i*32,
1011 tmp = (target_mem_base+(1+i*2)*16)&0xfffffffc;
1021 target_mem_base+12+i*32,
1022 0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1027 0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1035 target_mem_base+20+i*32,
1038 tmp = (target_mem_base+(2+i*2)*16)&0xfffffffc;
1046 target_mem_base+28+i*32,
1047 0x01 | 1<<12 | 1<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1064 target_mem_base + 12 + i * 32,
1065 (nand->
page_size == 2048 ? 0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 |
1066 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<31);
1075 1 | 1<<1 | 1<<6 | 2<<11 | 0<<14 | 0<<15 | 0<<16 | 0<<18);
1085 (nand->
page_size == 2048 ? 0x840 : 0x210));
1088 LOG_ERROR(
"timeout while waiting for completion of DMA");
1090 free(ecc_hw_buffer);
1091 free(ecc_flash_buffer);
1102 memcpy(data, page_buffer, data_size);
1104 LOG_INFO(
"Page = 0x%" PRIx32
" was read.", page);
1119 for (i = 0; i < idx; i++) {
1120 if ((0x00ffffff & *(uint32_t *)(
void *)(ecc_hw_buffer+i*8)) !=
1121 (0x00ffffff & *(uint32_t *)(
void *)(ecc_flash_buffer+8+i*16)))
1123 "ECC mismatch at 256 bytes size block= %d at page= 0x%" PRIx32,
1125 if ((0x00ffffff & *(uint32_t *)(
void *)(ecc_hw_buffer+4+i*8)) !=
1126 (0x00ffffff & *(uint32_t *)(
void *)(ecc_flash_buffer+12+i*16)))
1128 "ECC mismatch at 256 bytes size block= %d at page= 0x%" PRIx32,
1134 memcpy(oob, ecc_flash_buffer, oob_size);
1137 free(ecc_hw_buffer);
1138 free(ecc_flash_buffer);
1155 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
1169 LOG_DEBUG(
"lpc3180_controller_ready count=%d",
1180 LOG_DEBUG(
"lpc3180_controller_ready count=%d",
1198 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
1212 LOG_DEBUG(
"lpc3180_nand_ready count end=%d",
1223 LOG_DEBUG(
"lpc3180_nand_ready count end=%d",
1241 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
1245 LOG_DEBUG(
"lpc3180_tc_ready count start=%d",
1270 char *selected[] = {
1288 if (strcmp(
CMD_ARGV[1],
"mlc") == 0)
1290 else if (strcmp(
CMD_ARGV[1],
"slc") == 0) {
1305 lpc3180_info->
is_bulk ?
"%s controller selected bulk mode is available" :
1306 "%s controller selected bulk mode is not available",
1315 .handler = handle_lpc3180_select_command,
1318 "select MLC or SLC controller (default is MLC), SLC can be set to bulk mode",
1319 .usage =
"bank_id ['mlc'|'slc' ['bulk'] ]",
1327 .help =
"LPC3180 NAND flash controller commands",
1337 .nand_device_command = lpc3180_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_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
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_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static int lpc3180_init(struct nand_device *nand)
static int lpc3180_controller_ready(struct nand_device *nand, int timeout)
static int lpc3180_pll(int fclkin, uint32_t pll_ctrl)
static int lpc3180_read_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
COMMAND_HANDLER(handle_lpc3180_select_command)
static const struct command_registration lpc3180_exec_command_handlers[]
static int lpc3180_read_data(struct nand_device *nand, void *data)
static int lpc3180_command(struct nand_device *nand, uint8_t command)
static float lpc3180_cycle_time(struct nand_device *nand)
static int lpc3180_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 lpc3180_write_data(struct nand_device *nand, uint16_t data)
static int lpc3180_tc_ready(struct nand_device *nand, int timeout)
static int lpc3180_address(struct nand_device *nand, uint8_t address)
static int lpc3180_reset(struct nand_device *nand)
static int lpc3180_nand_ready(struct nand_device *nand, int timeout)
NAND_DEVICE_COMMAND_HANDLER(lpc3180_nand_device_command)
static const struct command_registration lpc3180_command_handler[]
#define ERROR_NAND_OPERATION_TIMEOUT
#define ERROR_NAND_OPERATION_FAILED
#define ERROR_NAND_OPERATION_NOT_SUPPORTED
uint32_t sw_wp_upper_bound
enum lpc3180_selected_controller selected_controller
uint32_t sw_wp_lower_bound
Interface for NAND flash controllers.
uint32_t working_area_size
target_addr_t working_area_phys
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_u16(struct target *target, target_addr_t address, uint16_t *value)
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.