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;
97 if ((sysclk_ctrl & 1) == 0)
105 if ((pwr_ctrl & (1 << 2)) == 0)
113 if (pwr_ctrl & (1 << 10))
114 hclk = hclk_pll / (((hclkdiv_ctrl & 0x7c) >> 2) + 1);
116 hclk = hclk_pll / (1 << (hclkdiv_ctrl & 0x3));
119 LOG_DEBUG(
"LPC3180 HCLK currently clocked at %i kHz", hclk);
121 return (1.0 / hclk) * 1000000.0;
133 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
138 if ((bus_width != 8) && (bus_width != 16)) {
139 LOG_ERROR(
"LPC3180 only supports 8 or 16 bit bus width, not %i", bus_width);
147 LOG_WARNING(
"LPC3180 only supports 8 bit bus width");
152 if ((address_cycles != 3) && (address_cycles != 4)) {
153 LOG_ERROR(
"LPC3180 only supports 3 or 4 address cycles, not %i", address_cycles);
164 LOG_DEBUG(
"no LPC3180 NAND flash controller selected, using default 'mlc'");
169 uint32_t mlc_icr_value = 0x0;
171 int twp, twh, trp, treh, trhz, trbwb, tcea;
184 mlc_icr_value |= 0x8;
186 mlc_icr_value |= 0x4;
187 if (address_cycles == 4)
188 mlc_icr_value |= 0x2;
190 mlc_icr_value |= 0x1;
196 twp = ((40 / cycle) + 1);
197 twh = ((20 / cycle) + 1);
198 trp = ((30 / cycle) + 1);
199 treh = ((15 / cycle) + 1);
200 trhz = ((30 / cycle) + 1);
201 trbwb = ((100 / cycle) + 1);
202 tcea = ((45 / cycle) + 1);
209 ((trp & 0xf) << 8) | ((treh & 0xf) << 12) | ((trhz & 0x7) << 16) |
210 ((trbwb & 0x1f) << 19) | ((tcea & 0x3) << 24));
215 int r_setup, r_hold, r_width, r_rdy;
216 int w_setup, w_hold, w_width, w_rdy;
241 r_setup = w_setup = 0;
242 r_hold = w_hold = 10 / cycle;
243 r_width = 30 / cycle;
244 w_width = 40 / cycle;
245 r_rdy = w_rdy = 100 / cycle;
249 ((r_width & 0xf) << 8) | ((r_rdy & 0xf) << 12) | ((w_setup & 0xf) << 16) |
250 ((w_hold & 0xf) << 20) | ((w_width & 0xf) << 24) | ((w_rdy & 0xf) << 28));
263 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
268 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
275 LOG_ERROR(
"LPC3180 NAND controller timed out after reset");
283 LOG_ERROR(
"LPC3180 NAND controller timed out after reset");
297 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
302 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
321 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
326 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
345 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
350 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
369 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
374 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
379 uint8_t *data8 = data;
382 uint16_t *data16 = data;
385 LOG_ERROR(
"BUG: bus_width neither 8 nor 16 bit");
395 uint8_t *data8 = data;
396 *data8 = data32 & 0xff;
398 uint16_t *data16 = data;
399 *data16 = data32 & 0xffff;
401 LOG_ERROR(
"BUG: bus_width neither 8 nor 16 bit");
420 uint8_t *page_buffer;
423 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
428 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
432 int quarter, num_quarters;
435 LOG_ERROR(
"LPC3180 MLC controller can't write OOB data only");
439 if (oob && (oob_size > 24)) {
440 LOG_ERROR(
"LPC3180 MLC controller can't write more "
441 "than 6 bytes for each quarter's OOB data");
445 if (data_size > (uint32_t)nand->
page_size) {
446 LOG_ERROR(
"data size exceeds page size");
453 page_buffer = malloc(512);
454 oob_buffer = malloc(6);
479 num_quarters = (nand->
page_size == 2048) ? 4 : 1;
481 for (quarter = 0; quarter < num_quarters; quarter++) {
482 int thisrun_data_size = (data_size > 512) ? 512 : data_size;
483 int thisrun_oob_size = (oob_size > 6) ? 6 : oob_size;
485 memset(page_buffer, 0xff, 512);
487 memcpy(page_buffer, data, thisrun_data_size);
488 data_size -= thisrun_data_size;
489 data += thisrun_data_size;
492 memset(oob_buffer, 0xff, 6);
494 memcpy(oob_buffer, oob, thisrun_oob_size);
495 oob_size -= thisrun_oob_size;
496 oob += thisrun_oob_size;
503 4, 128, page_buffer);
511 LOG_ERROR(
"timeout while waiting for completion of auto encode cycle");
560 int i = 0, target_mem_base;
561 uint8_t *ecc_flash_buffer;
583 LOG_ERROR(
"Reserve at least 0x%x physical target working area",
589 "Reserve the physical target working area at word boundary");
594 LOG_ERROR(
"no working area specified, can't read LPC internal flash");
600 page_buffer = malloc(2048);
602 page_buffer = malloc(512);
604 ecc_flash_buffer = malloc(64);
612 for (i = 0; i < nand->
page_size/0x100; i++) {
618 target_mem_base+0+i*32,
629 tmp = (target_mem_base+(1+i*2)*16)&0xfffffffc;
639 target_mem_base+12+i*32,
640 0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
645 0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
653 target_mem_base+20+i*32,
654 target_mem_base+
SPARE_OFFS+8+16*(i>>1)+(i%2)*4);
656 tmp = (target_mem_base+(2+i*2)*16)&0xfffffffc;
664 target_mem_base+28+i*32,
665 0x01 | 1<<12 | 1<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
668 }
else if (data && oob) {
679 (target_mem_base+32)&0xfffffffc);
681 (target_mem_base+32)&0xfffffffc);
690 2048 ? 512 : 128) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 |
691 1<<26 | 0<<27 | 0<<31);
695 2048 ? 512 : 128) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 |
696 1<<26 | 0<<27 | 0<<31);
698 }
else if (!data && oob)
720 target_mem_base+12+i*32,
722 2048 ? 0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
727 0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 |
728 0<<25 | 1<<26 | 0<<27 | 0<<31);
730 memset(ecc_flash_buffer, 0xff, 64);
732 memcpy(ecc_flash_buffer, oob, oob_size);
740 memset(page_buffer, 0xff, nand->
page_size == 2048 ? 2048 : 512);
741 memcpy(page_buffer, data, data_size);
750 free(ecc_flash_buffer);
759 1 | 1<<1 | 1<<6 | 2<<11 | 0<<14 | 0<<15 | 0<<16 | 0<<18);
770 (nand->
page_size == 2048 ? 0x10 : 0x04));
773 (nand->
page_size == 2048 ? 0x840 : 0x210));
778 LOG_ERROR(
"timeout while waiting for completion of DMA");
784 LOG_INFO(
"Page = 0x%" PRIx32
" was written.", page);
802 uint8_t *page_buffer;
805 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
810 LOG_ERROR(
"BUG: no LPC3180 NAND flash controller selected");
814 uint32_t page_bytes_done = 0;
815 uint32_t oob_bytes_done = 0;
819 if (oob && (oob_size > 6)) {
820 LOG_ERROR(
"LPC3180 MLC controller can't read more than 6 bytes of OOB data");
825 if (data_size > (uint32_t)nand->
page_size) {
826 LOG_ERROR(
"data size exceeds page size");
831 page_buffer = malloc(2048);
832 oob_buffer = malloc(64);
834 page_buffer = malloc(512);
835 oob_buffer = malloc(16);
875 while (page_bytes_done < (uint32_t)nand->
page_size) {
880 LOG_ERROR(
"timeout while waiting for completion of auto decode cycle");
889 if (mlc_isr & 0x40) {
890 LOG_ERROR(
"uncorrectable error detected: 0x%2.2" PRIx32, mlc_isr);
896 LOG_WARNING(
"%i symbol error detected and corrected",
897 ((
int)(((mlc_isr & 0x30) >> 4) + 1)));
905 page_buffer + page_bytes_done);
912 oob_buffer + oob_bytes_done);
914 page_bytes_done += 512;
915 oob_bytes_done += 16;
919 memcpy(data, page_buffer, data_size);
922 memcpy(oob, oob_buffer, oob_size);
948 int retval, i, target_mem_base;
949 uint8_t *ecc_hw_buffer;
950 uint8_t *ecc_flash_buffer;
963 LOG_ERROR(
"Reserve at least 0x%x physical target working area",
969 "Reserve the physical target working area at word boundary");
974 LOG_ERROR(
"no working area specified, can't read LPC internal flash");
980 page_buffer = malloc(2048);
982 page_buffer = malloc(512);
984 ecc_hw_buffer = malloc(32);
985 ecc_flash_buffer = malloc(64);
992 for (i = 0; i < nand->
page_size/0x100; i++) {
1001 target_mem_base+4+i*32,
1008 tmp = (target_mem_base+(1+i*2)*16)&0xfffffffc;
1018 target_mem_base+12+i*32,
1019 0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1024 0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1032 target_mem_base+20+i*32,
1035 tmp = (target_mem_base+(2+i*2)*16)&0xfffffffc;
1043 target_mem_base+28+i*32,
1044 0x01 | 1<<12 | 1<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1061 target_mem_base + 12 + i * 32,
1062 (nand->
page_size == 2048 ? 0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 |
1063 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<31);
1072 1 | 1<<1 | 1<<6 | 2<<11 | 0<<14 | 0<<15 | 0<<16 | 0<<18);
1082 (nand->
page_size == 2048 ? 0x840 : 0x210));
1085 LOG_ERROR(
"timeout while waiting for completion of DMA");
1087 free(ecc_hw_buffer);
1088 free(ecc_flash_buffer);
1099 memcpy(data, page_buffer, data_size);
1101 LOG_INFO(
"Page = 0x%" PRIx32
" was read.", page);
1116 for (i = 0; i < idx; i++) {
1117 if ((0x00ffffff & *(uint32_t *)(
void *)(ecc_hw_buffer+i*8)) !=
1118 (0x00ffffff & *(uint32_t *)(
void *)(ecc_flash_buffer+8+i*16)))
1120 "ECC mismatch at 256 bytes size block= %d at page= 0x%" PRIx32,
1122 if ((0x00ffffff & *(uint32_t *)(
void *)(ecc_hw_buffer+4+i*8)) !=
1123 (0x00ffffff & *(uint32_t *)(
void *)(ecc_flash_buffer+12+i*16)))
1125 "ECC mismatch at 256 bytes size block= %d at page= 0x%" PRIx32,
1131 memcpy(oob, ecc_flash_buffer, oob_size);
1134 free(ecc_hw_buffer);
1135 free(ecc_flash_buffer);
1152 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
1166 LOG_DEBUG(
"lpc3180_controller_ready count=%d",
1177 LOG_DEBUG(
"lpc3180_controller_ready count=%d",
1195 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
1209 LOG_DEBUG(
"lpc3180_nand_ready count end=%d",
1220 LOG_DEBUG(
"lpc3180_nand_ready count end=%d",
1238 LOG_ERROR(
"target must be halted to use LPC3180 NAND flash controller");
1242 LOG_DEBUG(
"lpc3180_tc_ready count start=%d",
1267 char *selected[] = {
1285 if (strcmp(
CMD_ARGV[1],
"mlc") == 0)
1287 else if (strcmp(
CMD_ARGV[1],
"slc") == 0) {
1302 lpc3180_info->
is_bulk ?
"%s controller selected bulk mode is available" :
1303 "%s controller selected bulk mode is not available",
1312 .handler = handle_lpc3180_select_command,
1315 "select MLC or SLC controller (default is MLC), SLC can be set to bulk mode",
1316 .usage =
"bank_id ['mlc'|'slc' ['bulk'] ]",
1324 .help =
"LPC3180 NAND flash controller commands",
1334 .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.
uint32_t page_size
Page size.
uint32_t address
Starting address. Sector aligned.
#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.