void msg_test(void)//struct bootloader_message *msg) { unsigned char data[CFG_NAND_PAGE_SIZE]; // clear data array ... if (nand_erase_block(64) || nand_erase_block(64 + 1)) serial_puts_info("NAND erase failed !!!\n"); if (nand_program_page(data, 8192)) serial_puts_info("NAND program failed !!!\n"); serial_puts_info("msg_test finish.\n"); }
void write_file() { unsigned long len = 0; int have_begin = 0; unsigned char* buf = (unsigned char*)0x52000000; int nodata_time = 0; unsigned char c; printf("Use minicom to send file.\n"); while(1) { if(uart_getc_nowait(&buf[len]) == 0) { have_begin = 1; nodata_time = 0; len++; } else { if(have_begin) { nodata_time++; } } if(nodata_time == 1000) { break; } } printf("Have get %d bytes data\n",len); printf("Press y to program: "); c = uart_getc(); if(c == 'y' || c == 'Y') { nand_erase_block(0); nand_write_page(0,buf,len); printf("\nUpdate program successful\n"); } else { printf("\nUpdate program cancel\n"); } }
void nand_erase_test() { unsigned long addr; printf("Input the start adddress: "); scanf("%lu",&addr); nand_erase_block(addr); }
static int cmd_nand_erase(int argc, char *argv[]) { u32 start_block, block, blocks, total_blocks; int i, rval; total_blocks = flnand.blocks_per_bank * flnand.banks; if (argc != 3) { uart_putstr("nand_erase [block] [blocks]!\r\n"); uart_putstr("Total blocks: "); uart_putdec(total_blocks); uart_putstr("\r\n"); return -1; } putstr("erase nand blocks including any bad blocks...\r\n"); putstr("press enter to start!\r\n"); putstr("press any key to terminate!\r\n"); rval = uart_wait_escape(0xffffffff); if (rval == 0) return -1; strtou32(argv[1], &start_block); strtou32(argv[2], &blocks); for (i = 0, block = start_block; i < blocks; i++, block++) { if (uart_poll()) break; if (block >= total_blocks) break; rval = nand_erase_block(block); putchar('.'); if ((i & 0xf) == 0xf) { putchar(' '); putdec(i); putchar('/'); putdec(blocks); putstr(" ("); putdec(i * 100 / blocks); putstr("%)\t\t\r"); } if (rval < 0) { putstr("\r\nfailed at block "); putdec(block); putstr("\r\n"); } } putstr("\r\ndone!\r\n"); return 0; }
void nand_erase_test(void) { char buf[100]; unsigned long addr; printf("enter the start address: "); scanf("%s", buf); addr = strtoul(buf, NULL, 0); printf("erase addr = 0x%x\n\r", addr); nand_erase_block(addr); }
int set_bootloader_message(const struct bootloader_message *in) { unsigned char data[CFG_NAND_PAGE_SIZE]; memset(data, '\0', CFG_NAND_PAGE_SIZE); memcpy(data, in, sizeof(struct bootloader_message)); /* Clear MISC partition, and write bootloader_message. */ if (nand_erase_block(64) || nand_erase_block(64 + 1)) { serial_puts_info("NAND erase failed!!!\n"); return 1; } if (nand_program_page(data, PTN_MISC_OFFSET / CFG_NAND_PAGE_SIZE)) { serial_puts_info("NAND program failed !!!\n"); return 1; } serial_puts_info("set_bootloader_message finish ...\n"); return 0; }
void update_program(void) { unsigned char *buf = (unsigned char *)0x52000000; unsigned long len = 0; int have_begin = 0; int nodata_time = 0; char c; /* 读串口获得数据 */ printf("\n\ruse gtkterm to send file\n\r", len); while (1) { if (getc_nowait(&buf[len]) == 0) { have_begin = 1; nodata_time = 0; len++; } else { if (have_begin) { nodata_time++; } } if (nodata_time == 1000) { break; } } printf("have get %d bytes data\n\r", len); printf("Press Y to program the flash: \n\r"); c = getc(); if (c == 'y' || c == 'Y') { /* 烧写到nand flash block 0 */ nand_erase_block(0); nand_write(0, buf, len); printf("update program successful\n\r"); } else { printf("Cancel program!\n\r"); } }
int main() { copy_vec(); irq_init(); button_init(); uart0_init(); timer_init(); // timer4_init(); lcd_init(); lcd_clean(0xffff); adc_ts_init(); ts_init(); // tslib_calibrate(); // lcd_clean(0xffff); nand_init(); #if 1 nand_read_id(); while(1); #endif printf("\r\n\n"); printf("===============================================\r\n"); printf(" NAND FLASH PROGRAMMING \r\n"); unsigned int size = 512 * 1024; // 512KB nand_read_id(); printf("erase entire flash, waiting ... \r\n"); int i; for(i = 0; i < 2048; i++) nand_erase_block(i); printf("start program ... \r\n"); nand_write_bytes(0x0, (unsigned char *)0x31000000, size); printf("program end ... \r\n"); printf("===============================================\r\n"); while(1); return 0; }
void copy_file2nand(unsigned int ramAddr,unsigned int blockAddr,unsigned int fileSize) { //计算boot代码大小,确定需要格式化Block的块总数、需要写入的Page总数 unsigned long bootSize = fileSize; //uboot代码长度 unsigned long blockTotal,pageTotal; blockTotal = ( (bootSize + NAND_BLOCK_MASK ) & (~NAND_BLOCK_MASK) ) / NAND_BLOCK_SIZE; pageTotal = ( (bootSize + NAND_PAGE_MASK ) & (~NAND_PAGE_MASK) ) / NAND_PAGE_SIZE; printk("\r\ncode size 0x%x (%dByte)\r\n",bootSize,bootSize); printk("require block %d require page %d\r\n",blockTotal,pageTotal); //烧录进FLASH,不带坏块检测 char retErase; unsigned char *lpsrcData = (unsigned char *)ramAddr; //SDRAM 0x30000000地址的代码 unsigned int off_data,page_addr; retErase = nand_erase_block(blockAddr); // printk("retrEsdrase %d\r\n",retErase); off_data = 0; page_addr = blockAddr; while(off_data < bootSize) { nand_write_page( page_addr, (unsigned char*)(lpsrcData + off_data) , NAND_PAGE_SIZE); // nand_write_page( // page_addr, // (unsigned char*)(lpsrcData + off_data)); // printk("colum 0x%8.8x write data off %d page_addr %d\r\n",off_data,off_data,page_addr); // PrintNandFlash((unsigned char*)(lpsrcData + off_data),16); off_data += NAND_PAGE_SIZE; page_addr += NAND_PAGE_ADDR; } }
void update_program(void) { unsigned char *buf = (unsigned char *)0x52000000; unsigned long len = 0; int have_begin = 0; int nodata_time = 0; unsigned long erase_addr; char c; int i; /* 读串口获得数据 */ printf("\n\ruse V2.2.exe/gtkterm to send file\n\r", len); while (1) { if (getc_nowait(&buf[len]) == 0) { have_begin = 1; nodata_time = 0; len++; } else { if (have_begin) { nodata_time++; } } if (nodata_time == 1000) { break; } } printf("have get %d bytes data\n\r", len); printf("the first 16 bytes data: \n\r"); for (i = 0; i < 16; i++) { printf("%02x ", buf[i]); } printf("\n\r"); printf("Press Y to program the flash: \n\r"); c = getc(); if (c == 'y' || c == 'Y') { /* 烧写到nand flash block 0 */ for (erase_addr = 0; erase_addr < ((len + 0x1FFFF) & ~0x1FFFF); erase_addr += 0x20000) { nand_erase_block(erase_addr); } nand_write(0, buf, len); printf("update program successful\n\r"); } else { printf("Cancel program!\n\r"); } }
static void command_request(struct udc_ep *ep, struct udc_req *req) { if (req->status) return; char *buf = (char *)req->buf; buf[req->actual] = '\0'; char group[9], command[9]; u32 n1, n2, n3, n4; int ret; ret = sscanf(buf, "%8s %8s %8x %8x %8x %8x", group, command, &n1, &n2, &n3, &n4); if (ret < 2) goto requeue; if (strcmp(group, "buffer") == 0) { if (strcmp(command, "read") == 0) { if (ret != 4) goto requeue; /* buffer offset */ u32 offset = n1 & (BUFFER_SIZE - 1) & ~1; buffer_req.buf = (u16 *)(BUFFER_START + offset); /* read size */ buffer_req.length = min(BUFFER_START - offset, n2 & ~1); ep->ops->queue(tx_ep, &buffer_req); return; } if (strcmp(command, "write") == 0) { if (ret != 4) goto requeue; /* buffer offset */ u32 offset = n1 & (BUFFER_SIZE - 1) & ~1; buffer_req.buf = (u16 *)(BUFFER_START + offset); /* write size */ buffer_req.length = min(BUFFER_START - offset, n2 & ~1); ep->ops->queue(rx_ep, &buffer_req); return; } goto requeue; } if (strcmp(group, "nand") == 0) { if (strcmp(command, "select") == 0) { if (ret != 3) goto requeue; /* chip number */ if (n1 >= NAND_MAX_CHIPS) goto requeue; nand_select_chip(n1); goto requeue; } if (strcmp(command, "info") == 0) { if (ret != 2) goto requeue; if (!nand_chip) goto requeue; req->buf = &nand_chip->info; req->length = sizeof(struct nand_info); req->complete = command_response; tx_ep->ops->queue(tx_ep, req); return; } if (strcmp(command, "bad") == 0) { if (ret != 2) goto requeue; if (!nand_chip) goto requeue; req->buf = nand_chip->bbt; req->length = nand_chip->num_blocks / 4; req->complete = command_response; tx_ep->ops->queue(tx_ep, req); return; } if (strcmp(command, "read") == 0) { if (ret != 4) goto requeue; if (!nand_chip) goto requeue; /* block number */ if (n1 >= nand_chip->num_blocks) goto requeue; int block = n1; /* buffer offset */ u32 offset = n2 & (BUFFER_SIZE - 1) & ~3; void *mem = (void *)(BUFFER_START + offset); nand_read_block(block, mem); goto requeue; } if (strcmp(command, "erase") == 0) { if (ret != 3) goto requeue; if (!nand_chip) goto requeue; /* block number */ if (n1 >= nand_chip->num_blocks) goto requeue; int block = n1; ((u16 *)req->buf)[0] = nand_erase_block(block); req->length = 2; req->complete = command_response; tx_ep->ops->queue(tx_ep, req); return; } if (strcmp(command, "write") == 0) { if (ret != 4) goto requeue; if (!nand_chip) goto requeue; /* block number */ if (n1 >= nand_chip->num_blocks) goto requeue; int block = n1; /* buffer offset */ u32 offset = n2 & (BUFFER_SIZE - 1) & ~3; void *mem = (void *)(BUFFER_START + offset); ((u16 *)req->buf)[0] = nand_write_block(block, mem); req->length = 2; req->complete = command_response; tx_ep->ops->queue(tx_ep, req); return; } if (strcmp(command, "mark") == 0) { if (ret != 4) goto requeue; if (!nand_chip) goto requeue; /* block number */ if (n1 >= nand_chip->num_blocks) goto requeue; int block = n1; /* mark */ u8 mark = (u8)n2 & 0x3; int byte_num = block >> 2; int shift = (block & 0x3) * 2; nand_chip->bbt[byte_num] &= ~(0x3 << shift); nand_chip->bbt[byte_num] |= mark << shift; goto requeue; } goto requeue; } requeue: ep->ops->queue(ep, req); }