2 * Copyright (C) 2012 Spreadtrum Communications Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
20 #include <asm/errno.h>
23 #include <asm/arch/sci_types.h>
24 #include <asm/arch/pinmap.h>
25 #include <asm/arch/bits.h>
28 #include <linux/mtd/nand.h>
30 #include <asm/arch-sc8830/sprd_nfc_reg_v2.h>
31 //#include <asm/arch/sc8810_reg_base.h>
32 #include <asm/arch/regs_ana.h>
33 #include <asm/arch/analog_reg_v3.h>
34 //#include <asm/arch/sc8810_reg_ahb.h>
35 //#include <asm/arch/sc8810_reg_global.h>
36 #include <asm/arch/gpio_drvapi.h>
37 #include <asm/arch/regs_global.h>
38 //#include <asm/arch/regs_cpc.h>
39 //#include <asm/arch/pin_reg_v3.h>
41 #ifdef CONFIG_NAND_SPL
45 int cnt = 200 * (x); \
46 for (i=0; i<cnt; i++);\
50 #define mdelay(_ms) udelay((_ms)*1000)
54 #if defined(CONFIG_NAND_SPL) || !defined(NAND_DBG)
55 #define DPRINT(arg...) do{}while(0)
59 #define ASSERT(cond) { assert(cond); }
62 #define NFC_MC_ICMD_ID (0xCD)
63 #define NFC_MC_ADDR_ID (0x0A)
64 #define NFC_MC_WRB0_ID (0xB0)
65 #define NFC_MC_WRB1_ID (0xB1)
66 #define NFC_MC_MRDT_ID (0xD0)
67 #define NFC_MC_MWDT_ID (0xD1)
68 #define NFC_MC_SRDT_ID (0xD2)
69 #define NFC_MC_SWDT_ID (0xD3)
70 #define NFC_MC_IDST_ID (0xDD)
71 #define NFC_MC_CSEN_ID (0xCE)
72 #define NFC_MC_NOP_ID (0xF0)
73 #define NFC_MC_DONE_ID (0xFF)
74 #define NFC_MAX_CHIP 1
75 #define NFC_TIMEOUT_VAL 0x1000000
77 #define NAND_MC_CMD(x) (uint16_t)(((x & 0xff) << 8) | NFC_MC_ICMD_ID)
78 #define NAND_MC_ADDR(x) (uint16_t)(((x & 0xff) << 8) | (NFC_MC_ADDR_ID << 4))
79 #define NAND_MC_WRB0(x) (uint16_t)(NFC_MC_WRB0_ID)
80 #define NAND_MC_MRDT (uint16_t)(NFC_MC_MRDT_ID)
81 #define NAND_MC_MWDT (uint16_t)(NFC_MC_MWDT_ID)
82 #define NAND_MC_SRDT (uint16_t)(NFC_MC_SRDT_ID)
83 #define NAND_MC_SWDT (uint16_t)(NFC_MC_SWDT_ID)
84 #define NAND_MC_IDST(x) (uint16_t)((NFC_MC_IDST_ID) | ((x -1) << 8))
85 #define NAND_MC_NOP(x) (uint16_t)(((x & 0xff) << 8) | NFC_MC_NOP_ID)
88 #define NAND_MC_BUFFER_SIZE (24)
90 static int mtderasesize = 0;
91 static int mtdwritesize = 0;
92 static int mtdoobsize = 0;
94 #endif //DOLPHIN_UBOOT end
98 #include <linux/kernel.h>
99 #include <linux/module.h>
100 #include <linux/interrupt.h>
101 #include <linux/dma-mapping.h>
102 #include <linux/platform_device.h>
103 #include <linux/delay.h>
104 #include <linux/mtd/mtd.h>
105 #include <linux/mtd/nand.h>
106 #include <linux/mtd/partitions.h>
107 #include <linux/io.h>
108 #include <linux/irq.h>
109 #include <linux/slab.h>
110 #include <linux/spinlock.h>
111 #include <mach/globalregs.h>
112 #include <mach/pinmap.h>
114 #include "dolphin_nand.h"
115 #define DPRINT printk
117 #endif //DOLPHIN_KERNEL
121 #define SPRD_ASSERT(cond) { if (!(cond)) while(1); }
122 #define STATIC_FUNC static
124 #include "sprd_nand_param.h"
127 /* 2 bit correct, sc8810 support 1, 2, 4, 8, 12,14, 24 */
128 #define CONFIG_SYS_NAND_ECC_MODE (2)
129 //#define CONFIG_SYS_NAND_ECC_MODE 8
130 /* Number of ECC bytes per OOB - S3C6400 calculates 4 bytes ECC in 1-bit mode */
131 #define CONFIG_SYS_NAND_ECCBYTES (4)
132 //#define CONFIG_SYS_NAND_ECCBYTES 14
134 #define NAND_MC_BUFFER_SIZE (24)
135 #define CONFIG_SYS_NAND_ECCSIZE (512)
136 #define CONFIG_SYS_NAND_5_ADDR_CYCLE 5
137 #define SPRD_NAND_CLOCK (153)
139 #define IRQ_TIMEOUT 100//unit:ms,IRQ timeout value
140 #define DRIVER_NAME "sc8830_nand"
143 enum NAND_ERR_CORRECT_S {
150 enum NAND_HANDLE_STATUS_S {
156 static enum NAND_HANDLE_STATUS_S handle_status = NAND_HANDLE_DONE;
157 static enum NAND_ERR_CORRECT_S ret_irq_en = NAND_NO_ERROR;
159 STATIC_FUNC void nfc_enable_interrupt(void);
160 STATIC_FUNC void nfc_disable_interrupt(void);
161 STATIC_FUNC void nfc_clear_interrupt(void);
164 //#define NAND_IRQ_EN
166 #include <linux/completion.h>
167 #include <mach/irqs.h>
169 static struct completion nfc_op_completion;
170 STATIC_FUNC void nfc_wait_op_done(void);
174 struct sprd_dolphin_nand_info {
175 struct mtd_info *mtd;
176 struct nand_chip *nand;
182 #ifdef DOLPHIN_KERNEL
183 struct platform_device *pdev;
186 struct sprd_nand_param *param;
187 uint32_t chip; //chip index
188 uint32_t v_mbuf; //virtual main buffer address
189 uint32_t p_mbuf; //phy main buffer address
190 uint32_t v_oob; // virtual oob buffer address
191 uint32_t p_oob; //phy oob buffer address
192 uint32_t page; //page address
193 uint16_t column; //column address
195 uint16_t m_size; //main part size per sector
196 uint16_t s_size; //oob size per sector
197 uint8_t a_cycles;//address cycles, 3, 4, 5
198 uint8_t sct_pg; //sector per page
201 uint8_t ecc_mode;//0-1bit, 1-2bit, 2-4bit, 3-8bit,4-12bit,5-16bit,6-24bit
202 uint8_t ecc_pos; // ecc postion
203 uint8_t wp_en; //write protect enable
205 uint16_t page_per_bl;//page per block
208 uint8_t ins_num;//instruction number
209 uint32_t ins[NAND_MC_BUFFER_SIZE >> 1];
213 #define mtd_to_dolphin(m) (&g_dolphin)
216 static struct nand_ecclayout sprd_dolphin_nand_oob_default = {
223 struct sprd_dolphin_nand_info g_dolphin = {0};
224 //save the data read by read_byte and read_word api interface functon
225 static __attribute__((aligned(4))) uint8_t s_oob_data[NAND_MAX_OOBSIZE];
226 //static __attribute__((aligned(4))) uint8_t s_oob_data[8];
227 //static __attribute__((aligned(4))) uint8_t s_id_status[8];
229 STATIC_FUNC int sprd_dolphin_nand_read_id(struct sprd_dolphin_nand_info *dolphin, uint32_t *buf);
230 STATIC_FUNC int sprd_dolphin_nand_reset(struct sprd_dolphin_nand_info *dolphin);
231 STATIC_FUNC int sprd_dolphin_nand_wait_finish(struct sprd_dolphin_nand_info *dolphin);
233 STATIC_FUNC uint32_t sprd_dolphin_reg_read(uint32_t addr)
237 STATIC_FUNC void sprd_dolphin_reg_write(uint32_t addr, uint32_t val)
241 STATIC_FUNC void sprd_dolphin_reg_or(uint32_t addr, uint32_t val)
243 sprd_dolphin_reg_write(addr, sprd_dolphin_reg_read(addr) | val);
245 STATIC_FUNC void sprd_dolphin_reg_and(uint32_t addr, uint32_t mask)
247 sprd_dolphin_reg_write(addr, sprd_dolphin_reg_read(addr) & mask);
249 STATIC_FUNC void sprd_dolphin_nand_int_clr(uint32_t bit_clear)
251 sprd_dolphin_reg_write(NFC_INT_REG, bit_clear);
254 STATIC_FUNC void nfc_clear_interrupt(void)
258 value = ( INT_STSMCH_CLR | INT_WP_CLR | INT_TO_CLR | INT_DONE_CLR);
259 sprd_dolphin_reg_or(NFC_INT_REG, value); /* clear all interrupt status */
261 //value = NFC_CMD_CLR;
262 //sprd_dolphin_reg_or(NFC_START_REG, value); /* clear all interrupt status */
265 STATIC_FUNC void nfc_enable_interrupt(void)
269 value = (INT_TO_EN | INT_DONE_EN);
270 sprd_dolphin_reg_or(NFC_INT_REG, value); /* clear all interrupt status */
273 STATIC_FUNC void nfc_disable_interrupt(void)
277 value = ~(INT_TO_EN | INT_DONE_EN);
278 sprd_dolphin_reg_and(NFC_INT_REG, value); /* clear all interrupt status */
281 unsigned int ecc_mode_convert(uint32_t mode)
320 STATIC_FUNC void dolphin_set_timing_config(struct sprd_nand_timing *timing , uint32_t nfc_clk_MHz) {
321 u32 reg_val, temp_val;
325 /* get acs value : 0ns */
326 reg_val |= ((2 & 0x1F) << NFC_ACS_OFFSET);
328 /* get ace value + 6ns read delay time, and rwl added */
329 temp_val = (timing->ace_ns + 6) * nfc_clk_MHz / 1000;
330 if (((timing->ace_ns * nfc_clk_MHz) % 1000) != 0) {
333 reg_val |= ((temp_val & 0x1F) << NFC_ACE_OFFSET);
335 /* get rws value : 20 ns */
336 temp_val = 20 * nfc_clk_MHz / 1000;
337 if (((timing->ace_ns * nfc_clk_MHz) % 1000) != 0) {
340 reg_val |= ((temp_val & 0x3F) << NFC_RWS_OFFSET);
342 /* get rws value : 0 ns */
343 reg_val |= ((2 & 0x1F) << NFC_RWE_OFFSET);
346 temp_val = (timing->rwh_ns + 6) * nfc_clk_MHz / 1000;
347 if (((timing->ace_ns * nfc_clk_MHz) % 1000) != 0) {
350 reg_val |= ((temp_val & 0x1F) << NFC_RWH_OFFSET);
352 /* get rwl value, 6 is read delay time*/
353 temp_val = (timing->rwl_ns + 6) * nfc_clk_MHz / 1000;
354 if (((timing->ace_ns * nfc_clk_MHz) % 1000) != 0) {
357 reg_val |= (temp_val & 0x3F);
359 DPRINT("%s nand timing val: 0x%x\n\r", __func__, reg_val);
361 sprd_dolphin_reg_write(NFC_TIMING_REG, reg_val);
365 #ifdef CONFIG_NAND_SPL
366 struct sprd_dolphin_boot_header_info {
368 uint32_t sct_size; //
369 uint32_t acycle; // 3, 4, 5
370 uint32_t bus_width; //0 ,1
371 uint32_t spare_size; //spare part sise for one sector
372 uint32_t ecc_mode; //0--1bit, 1--2bit,2--4bit,3--8bit,4--12bit, 5--16bit, 6--24bit
373 uint32_t ecc_pos; // ecc postion at spare part
374 uint32_t sct_per_page; //sector per page
377 uint32_t magic_num; //0xaa55a5a5
378 uint32_t ecc_value[27];
380 void boad_nand_param_init(struct sprd_dolphin_nand_info *dolphin, struct nand_chip *chip, uint8 *id)
388 /* The 4th id byte is the important one */
390 writesize = 1024 << (extid & 0x3);
393 oobsize = (8 << (extid & 0x01)) * (writesize >> 9);
395 /* Calc blocksize. Blocksize is multiples of 64KiB */
396 erasesize = (64 * 1024) << (extid & 0x03);
398 /* Get buswidth information */
399 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
400 dolphin->write_size = writesize;
401 dolphin->m_size =CONFIG_SYS_NAND_ECCSIZE;
402 dolphin->sct_pg = writesize / CONFIG_SYS_NAND_ECCSIZE;
403 dolphin->s_size = oobsize / dolphin->sct_pg;
404 dolphin->ecc_mode = ecc_mode_convert(CONFIG_SYS_NAND_ECC_MODE);
405 dolphin->ecc_pos = dolphin->s_size - ((14 * CONFIG_SYS_NAND_ECC_MODE + 7) / 8);
406 dolphin->info_pos = dolphin->ecc_pos - 1;
407 dolphin->info_size = 1;
408 dolphin->page_per_bl = erasesize / dolphin->write_size;
409 dolphin->a_cycles = CONFIG_SYS_NAND_5_ADDR_CYCLE;
410 if(NAND_BUSWIDTH_16 == busw)
412 chip->options |= NAND_BUSWIDTH_16;
416 chip->options &= ~NAND_BUSWIDTH_16;
421 * because the dolphin firmware use the nand identify process
422 * and the data at the header of nand_spl is the nand param used at nand read and write,
423 * so in nand_spl, don't need read the id or use the onfi spec to calculate the nand param,
424 * just use the param at the nand_spl header instead of
426 void nand_hardware_config(struct mtd_info *mtd, struct nand_chip *chip)
428 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
429 struct sprd_nand_param* param;
430 struct sprd_nand_oob* oob;
431 struct sprd_nand_timing* timing;
434 sprd_dolphin_nand_reset(dolphin);
436 sprd_dolphin_nand_read_id(dolphin, (uint32_t *)s_oob_data);
437 boad_nand_param_init(dolphin, dolphin->nand, s_oob_data);
440 param = SprdGetNandParam(id);
443 dolphin->param = param;
445 timing = ¶m->sTiming;
447 dolphin_set_timing_config(timing, SPRD_NAND_CLOCK);
449 //save the param config
450 dolphin->write_size = param->nPageSize;
451 dolphin->page_per_bl = param->nBlkSize / param->nPageSize;
452 dolphin->m_size = param->nSecSize;
453 dolphin->sct_pg = (param->nPageSize / param->nSecSize);
454 dolphin->oob_size = param->nSpareSize;
455 dolphin->a_cycles = param->nCycles;
457 dolphin->s_size = oob->nOOBSize;
458 dolphin->info_pos = oob->nInfoPos;
459 dolphin->info_size = oob->nInfoSize;
460 dolphin->ecc_pos = oob->nEccPos;
461 dolphin->ecc_mode = ecc_mode_convert(oob->nEccBits);
465 chip->ecc.bytes = (oob->nEccBits * 14 + 7) / 8;
467 chip->eccbitmode = oob->nEccBits;
471 chip->options |= NAND_BUSWIDTH_16;
475 chip->options &= ~NAND_BUSWIDTH_16;
478 mtd->writesize = dolphin->write_size;
479 mtd->oobsize = dolphin->s_size * dolphin->sct_pg;
480 mtd->erasesize = dolphin->page_per_bl * dolphin->write_size;
485 STATIC_FUNC void sprd_dolphin_nand_ecc_layout_gen(struct sprd_nand_oob *oob, uint8_t sector_num, struct nand_ecclayout *layout)
490 uint32_t used_len ; //one sector ecc data size(byte)
491 uint32_t eccbytes = 0; //one page ecc data size(byte)
492 uint32_t oobfree_len = 0;
494 used_len = (14 * oob->nEccBits + 7) / 8 + oob->nInfoSize;
495 if(sector_num > ARRAY_SIZE(layout->oobfree))
499 for(sct = 0; sct < sector_num; sct++)
501 //offset = (oob_size * sct) + ecc_pos;
502 //for(i = 0; i < ecc_len; i++)
503 offset = (oob->nOOBSize * sct) + oob->nEccPos;
504 for(i = 0; i < used_len; i++)
506 layout->eccpos[eccbytes++] = offset + i;
508 layout->oobfree[sct].offset = oob->nOOBSize * sct;
509 layout->oobfree[sct].length = oob->nOOBSize - used_len ;
510 oobfree_len += oob->nOOBSize - used_len;
512 //for bad mark postion
513 layout->oobfree[0].offset = 2;
514 layout->oobfree[0].length = oob->nOOBSize - used_len - 2;
516 layout->eccbytes = used_len * sector_num;
519 void nand_hardware_config(struct mtd_info *mtd, struct nand_chip *chip, uint8_t id[5])
521 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
522 struct sprd_nand_param* param;
523 struct sprd_nand_oob* oob;
524 struct sprd_nand_timing* timing;
526 param = SprdGetNandParam(id);
529 dolphin->param = param;
531 timing = ¶m->sTiming;
533 dolphin_set_timing_config(timing, SPRD_NAND_CLOCK);
535 //save the param config
536 dolphin->write_size = param->nPageSize;
537 dolphin->page_per_bl = param->nBlkSize / param->nPageSize;
538 dolphin->m_size = param->nSecSize;
539 dolphin->sct_pg = (param->nPageSize / param->nSecSize);
540 dolphin->oob_size = param->nSpareSize;
541 dolphin->a_cycles = param->nCycles;
543 dolphin->s_size = oob->nOOBSize;
544 dolphin->info_pos = oob->nInfoPos;
545 dolphin->info_size = oob->nInfoSize;
546 dolphin->ecc_pos = oob->nEccPos;
547 dolphin->ecc_mode = ecc_mode_convert(oob->nEccBits);
551 chip->ecc.bytes = (oob->nEccBits * 14 + 7) / 8;
553 chip->eccbitmode = oob->nEccBits;
557 chip->options |= NAND_BUSWIDTH_16;
561 chip->options &= ~NAND_BUSWIDTH_16;
564 /* Calculate the address shift from the page size */
565 chip->page_shift = ffs(mtd->writesize) - 1;
566 /* Convert chipsize to number of pages per chip -1. */
567 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
568 sprd_dolphin_nand_ecc_layout_gen(oob, dolphin->sct_pg, &sprd_dolphin_nand_oob_default);
569 chip->ecc.layout = &sprd_dolphin_nand_oob_default;
571 mtd->writesize = dolphin->write_size;
572 mtd->oobsize = dolphin->oob_size;
573 mtd->erasesize = dolphin->page_per_bl * dolphin->write_size;
577 struct sprd_nand_oob oob_tmp;
579 //save the param config
580 steps = mtd->writesize / CONFIG_SYS_NAND_ECCSIZE;
581 dolphin->ecc_mode = ecc_mode_convert(CONFIG_SYS_NAND_ECC_MODE);
582 dolphin->m_size = CONFIG_SYS_NAND_ECCSIZE;
583 dolphin->s_size = mtd->oobsize / steps;
584 dolphin->a_cycles = mtd->writesize / CONFIG_SYS_NAND_ECCSIZE;
585 dolphin->sct_pg = steps;
586 dolphin->info_pos = dolphin->s_size - CONFIG_SYS_NAND_ECCBYTES - 1;
587 dolphin->info_size = 1;
588 dolphin->write_size = mtd->writesize;
589 dolphin->page_per_bl = mtd->erasesize / mtd->writesize;
590 dolphin->oob_size = mtd->oobsize;
591 dolphin->ecc_pos = dolphin->s_size - CONFIG_SYS_NAND_ECCBYTES;
594 oob_tmp.nOOBSize = dolphin->s_size;
595 oob_tmp.nInfoPos = dolphin->info_pos;
596 oob_tmp.nInfoSize = dolphin->info_size;
597 oob_tmp.nEccPos = dolphin->ecc_pos;
598 oob_tmp.nEccBits = CONFIG_SYS_NAND_ECC_MODE;
599 sprd_dolphin_nand_ecc_layout_gen(&oob_tmp, dolphin->sct_pg, &sprd_dolphin_nand_oob_default);
601 chip->ecc.layout = &sprd_dolphin_nand_oob_default;
603 chip->eccbitmode = CONFIG_SYS_NAND_ECC_MODE;
605 if(chip->chipsize > (128 << 20)) {
606 dolphin->a_cycles = 5;
609 dolphin->a_cycles = 4;
614 #endif //end CONFIG_NAND_SPL
619 #ifndef CONFIG_NAND_SPL
625 uint8_t dir; //if dir is 0, read dadta from NFC buffer, if 1, write data to NFC buffer
628 } sprd_dolphin_nand_data_param;
630 STATIC_FUNC unsigned int sprd_dolphin_data_trans(sprd_dolphin_nand_data_param *param)
635 cfg0 = NFC_ONLY_MST_MODE | MAIN_SPAR_APT | NFC_WPN;
640 if(param->m_sct != 0)
642 cfg0 |= (param->m_sct - 1) << SECTOR_NUM_OFFSET;
644 cfg1 |= (param->m_size - 1);
645 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, (uint32_t)param->m_buf);
647 if(param->s_sct != 0)
650 cfg1 |= (param->s_size - 1) << SPAR_SIZE_OFFSET;
651 cfg2 |= (param->s_sct - 1) << SPAR_SECTOR_NUM_OFFSET;
652 sprd_dolphin_reg_write(NFC_SPAR_ADDR_REG, (uint32_t)param->s_buf);
654 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
655 sprd_dolphin_reg_write(NFC_CFG1_REG, cfg1);
656 sprd_dolphin_reg_write(NFC_CFG2_REG, cfg2);
657 sprd_dolphin_nand_int_clr(INT_STSMCH_CLR | INT_WP_CLR | INT_TO_CLR | INT_DONE_CLR);//clear all interrupt
658 sprd_dolphin_reg_write(NFC_START_REG, NFC_START);
659 sprd_dolphin_nand_wait_finish(&g_dolphin);
662 void sprd_ecc_ctrl(struct sprd_ecc_param *param, uint32_t dir)
667 cfg0 = NFC_ONLY_ECC_MODE | MAIN_SPAR_APT;
672 cfg1 |=(param->sinfo_size - 1) << SPAR_INFO_SIZE_OFFSET;
673 cfg1 |=(param->sp_size - 1) << SPAR_SIZE_OFFSET;
674 cfg1 |= (param->m_size - 1);
676 cfg2 |= (param->sinfo_pos)<< SPAR_INFO_POS_OFFSET;
677 cfg2 |= ecc_mode_convert(param->mode) << ECC_MODE_OFFSET;
678 cfg2 |= param->ecc_pos;
679 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
680 sprd_dolphin_reg_write(NFC_CFG1_REG, cfg1);
681 sprd_dolphin_reg_write(NFC_CFG2_REG, cfg2);
682 sprd_dolphin_nand_int_clr(INT_STSMCH_CLR | INT_WP_CLR | INT_TO_CLR | INT_DONE_CLR);//clear all interrupt
683 sprd_dolphin_reg_write(NFC_START_REG, NFC_START);
684 sprd_dolphin_nand_wait_finish(&g_dolphin);
687 unsigned int sprd_ecc_encode(struct sprd_ecc_param *param)
689 struct sprd_dolphin_nand_info *dolphin;
690 sprd_dolphin_nand_data_param d_param;
692 dolphin = &g_dolphin;
693 memset(&d_param, 0, sizeof(d_param));
695 d_param.m_buf = param->p_mbuf;
696 d_param.s_buf = param->p_sbuf;
697 d_param.m_sct = param->ecc_num;
698 d_param.s_sct = param->ecc_num;
700 d_param.m_size = param->m_size;
701 d_param.s_size = param->sp_size;
703 Dcache_CleanRegion((unsigned int)d_param.m_buf, d_param.m_sct*d_param.m_size);
704 Dcache_CleanRegion((unsigned int)d_param.s_buf, d_param.s_sct*d_param.s_size);
706 sprd_dolphin_data_trans(&d_param);
707 sprd_ecc_ctrl(param, 1);
711 Dcache_InvalRegion((unsigned int)d_param.m_buf , d_param.m_sct*d_param.m_size);
712 Dcache_InvalRegion((unsigned int)d_param.s_buf , d_param.s_sct*d_param.s_size);
714 sprd_dolphin_data_trans(&d_param); //read the ecc value from nfc buffer
717 #endif //CONFIG_NAND_SPL end
718 #endif //DOLPHIN_UBOOT END
722 //add one macro instruction to nand controller
723 STATIC_FUNC void sprd_dolphin_nand_ins_init(struct sprd_dolphin_nand_info *dolphin)
725 dolphin->ins_num = 0;
727 STATIC_FUNC void sprd_dolphin_nand_ins_add(uint16_t ins, struct sprd_dolphin_nand_info *dolphin)
729 uint16_t *buf = (uint16_t *)dolphin->ins;
730 if(dolphin->ins_num >= NAND_MC_BUFFER_SIZE)
734 *(buf + dolphin->ins_num) = ins;
738 STATIC_FUNC void sprd_dolphin_nand_ins_exec(struct sprd_dolphin_nand_info *dolphin)
743 for(i = 0; i < ((dolphin->ins_num + 1) >> 1); i++)
745 sprd_dolphin_reg_write(NFC_INST0_REG + (i << 2), dolphin->ins[i]);
747 cfg0 = sprd_dolphin_reg_read(NFC_CFG0_REG);
764 sprd_dolphin_nand_int_clr(INT_STSMCH_CLR | INT_WP_CLR | INT_TO_CLR | INT_DONE_CLR);//clear all interrupt
765 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
766 sprd_dolphin_reg_write(NFC_START_REG, NFC_START);
769 STATIC_FUNC int sprd_dolphin_nand_wait_finish(struct sprd_dolphin_nand_info *dolphin)
772 unsigned int counter = 0;
773 while((counter < NFC_TIMEOUT_VAL/*time out*/))
775 value = sprd_dolphin_reg_read(NFC_INT_REG);
776 if(value & INT_DONE_RAW)
782 sprd_dolphin_reg_write(NFC_INT_REG, 0xf00); //clear all interrupt status
783 if(counter >= NFC_TIMEOUT_VAL)
791 STATIC_FUNC void sprd_dolphin_nand_wp_en(struct sprd_dolphin_nand_info *dolphin, int en)
802 STATIC_FUNC void sprd_dolphin_select_chip(struct mtd_info *mtd, int chip)
804 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
805 if(chip < 0) { //for release caller
808 //DPRINT("sprd_dolphin_select_chip, %x\r\n", chip);
809 dolphin->chip = chip;
810 #ifdef CONFIG_NAND_SPL
811 nand_hardware_config(mtd,dolphin->nand);
815 STATIC_FUNC void sprd_dolphin_nand_read_status(struct sprd_dolphin_nand_info *dolphin)
818 //DPRINT("%s enter\n", __func__);
820 sprd_dolphin_nand_ins_init(dolphin);
821 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_STATUS), dolphin);
822 sprd_dolphin_nand_ins_add(NAND_MC_NOP(10), dolphin);
823 sprd_dolphin_nand_ins_add(NAND_MC_IDST(1), dolphin);
824 sprd_dolphin_nand_ins_add(NFC_MC_DONE_ID, dolphin);
825 sprd_dolphin_reg_write(NFC_CFG0_REG, NFC_ONLY_NAND_MODE);
826 sprd_dolphin_nand_ins_exec(dolphin);
827 sprd_dolphin_nand_wait_finish(dolphin);
828 buf = (uint32_t *)s_oob_data;
829 *buf = sprd_dolphin_reg_read(NFC_STATUS0_REG);
830 dolphin->buf_head = 0;
831 dolphin->_buf_tail = 1;
833 //DPRINT("%s leave\n", __func__);
835 STATIC_FUNC int sprd_dolphin_nand_read_id(struct sprd_dolphin_nand_info *dolphin, uint32_t *buf)
837 //DPRINT("%s enter\n", __func__);
839 sprd_dolphin_nand_ins_init(dolphin);
840 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_READID), dolphin);
841 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(0), dolphin);
842 sprd_dolphin_nand_ins_add(NAND_MC_NOP(10), dolphin);
843 sprd_dolphin_nand_ins_add(NAND_MC_IDST(8), dolphin);
844 sprd_dolphin_nand_ins_add(NFC_MC_DONE_ID, dolphin);
846 sprd_dolphin_reg_write(NFC_CFG0_REG, NFC_ONLY_NAND_MODE);
847 sprd_dolphin_nand_ins_exec(dolphin);
848 if (sprd_dolphin_nand_wait_finish(dolphin) != 0)
852 *buf = sprd_dolphin_reg_read(NFC_STATUS0_REG);
853 *(buf + 1) = sprd_dolphin_reg_read(NFC_STATUS1_REG);
854 dolphin->buf_head = 0;
855 dolphin->_buf_tail = 8;
857 //DPRINT("%s leave\n", __func__);
861 STATIC_FUNC int sprd_dolphin_nand_reset(struct sprd_dolphin_nand_info *dolphin)
863 //DPRINT("%s enter\n", __func__);
865 sprd_dolphin_nand_ins_init(dolphin);
866 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_RESET), dolphin);
867 sprd_dolphin_nand_ins_add(NFC_MC_WRB0_ID, dolphin); //wait rb
868 sprd_dolphin_nand_ins_add(NFC_MC_DONE_ID, dolphin);
870 sprd_dolphin_reg_write(NFC_CFG0_REG, NFC_ONLY_NAND_MODE);
871 sprd_dolphin_nand_ins_exec(dolphin);
872 if (sprd_dolphin_nand_wait_finish(dolphin) != 0)
877 //DPRINT("%s leave\n", __func__);
881 STATIC_FUNC u32 sprd_dolphin_get_decode_sts(u32 index)
886 reg_addr = NFC_STATUS0_REG + (index & 0xfffffffc);
887 shift = (index & 0x3) << 3;
888 err = sprd_dolphin_reg_read(reg_addr);
890 if((err & ECC_ALL_FF))
892 err &= ERR_ERR_NUM0_MASK;
904 STATIC_FUNC int sprd_dolphin_nand_read_lp(struct mtd_info *mtd,uint8_t *mbuf, uint8_t *sbuf,uint32_t raw)
906 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
907 struct nand_chip *chip = dolphin->nand;
915 int size = 0 ,sct_size;
919 page_addr = dolphin->page;
922 column = mtd->writesize;
928 if(chip->options & NAND_BUSWIDTH_16)
932 //DPRINT("sprd_dolphin_nand_read_lp,page_addr = %x,column = %x\r\n",page_addr, column);
933 sprd_dolphin_nand_ins_init(dolphin);
934 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_READ0), dolphin);
935 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(column & 0xff), dolphin);
937 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(column & 0xff), dolphin);
939 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
941 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
943 if (5 == dolphin->a_cycles)// five address cycles
946 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
948 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_READSTART), dolphin);
950 sprd_dolphin_nand_ins_add(NFC_MC_WRB0_ID, dolphin); //wait rb
953 sprd_dolphin_nand_ins_add(NAND_MC_SRDT, dolphin);
954 //switch to main part
955 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_RNDOUT), dolphin);
956 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(0), dolphin);
957 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(0), dolphin);
958 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_RNDOUTSTART), dolphin);
959 sprd_dolphin_nand_ins_add(NAND_MC_MRDT, dolphin);
963 sprd_dolphin_nand_ins_add(NAND_MC_MRDT, dolphin);
965 sprd_dolphin_nand_ins_add(NFC_MC_DONE_ID, dolphin);
967 cfg0 = NFC_AUTO_MODE | MAIN_SPAR_APT | ((dolphin->sct_pg - 1)<< SECTOR_NUM_OFFSET);
968 if((!raw) && mbuf && sbuf)
970 cfg0 |= ECC_EN | DETECT_ALL_FF;
972 if(chip->options & NAND_BUSWIDTH_16)
976 cfg1 = (dolphin->info_size) << SPAR_INFO_SIZE_OFFSET;
977 cfg2 = (dolphin->ecc_mode << 12) | (dolphin->info_pos << SPAR_INFO_POS_OFFSET) | ((dolphin->sct_pg - 1) << SPAR_SECTOR_NUM_OFFSET) | dolphin->ecc_pos;
979 #ifndef CONFIG_NAND_SPL
982 Dcache_CleanRegion((unsigned int)mbuf, dolphin->m_size*dolphin->sct_pg);
983 Dcache_InvalRegion((unsigned int)mbuf, dolphin->m_size*dolphin->sct_pg);
988 Dcache_CleanRegion((unsigned int)sbuf, dolphin->s_size*dolphin->sct_pg);
989 Dcache_InvalRegion((unsigned int)sbuf, dolphin->s_size*dolphin->sct_pg);
995 cfg1 |= (dolphin->m_size - 1) | ((dolphin->s_size - 1)<< SPAR_SIZE_OFFSET);
996 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, (uint32_t)mbuf);
997 sprd_dolphin_reg_write(NFC_SPAR_ADDR_REG, (uint32_t)sbuf);
998 cfg0 |= MAIN_USE | SPAR_USE;
1004 cfg1 |= (dolphin->m_size - 1);
1005 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, (uint32_t)mbuf);
1009 cfg1 |= (dolphin->s_size - 1);
1010 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, (uint32_t)sbuf);
1014 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
1015 sprd_dolphin_reg_write(NFC_CFG1_REG, cfg1);
1016 sprd_dolphin_reg_write(NFC_CFG2_REG, cfg2);
1018 sprd_dolphin_nand_ins_exec(dolphin);
1019 sprd_dolphin_nand_wait_finish(dolphin);
1021 for(i = 0; i < dolphin->sct_pg; i++) {
1022 err = sprd_dolphin_get_decode_sts(i);
1023 if(err == ERR_ERR_NUM0_MASK) {
1026 size = dolphin->s_size * (i+1);
1027 sct_size = dolphin->s_size;
1029 if(sbuf[--size] != 0xFF){
1030 DPRINT("%s spare area bif flip 0x%x\n",__func__, sbuf[size]);
1031 for(num=0; num<8; num++){
1032 if(!(sbuf[size]>>num & 0x1)){
1034 if(ecc_bit > chip->eccbitmode){
1036 DPRINT("%s spare area ecc check err\n",__func__);
1042 mtd->ecc_stats.failed++;
1049 size = dolphin->m_size*(i+1) ;
1050 sct_size = dolphin->m_size ;
1052 if(mbuf[--size] != 0xFF){
1053 DPRINT("%s main area bif flip 0x%x\n",__func__, mbuf[size]);
1054 for(num = 0 ; num < 8 ; num++){
1055 if(!(mbuf[size]>>num & 0x1) ){
1058 if(ecc_bit > chip->eccbitmode){
1060 DPRINT("%s main+spare area ecc check err\n",__func__);
1066 mtd->ecc_stats.failed++;
1074 mtd->ecc_stats.corrected += err;
1083 STATIC_FUNC int sprd_dolphin_nand_write_lp(struct mtd_info *mtd,const uint8_t *mbuf, uint8_t *sbuf,uint32_t raw)
1085 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1086 struct nand_chip *chip = dolphin->nand;
1092 page_addr = dolphin->page;
1097 column = mtd->writesize;
1099 if(chip->options & NAND_BUSWIDTH_16)
1104 sprd_dolphin_nand_ins_init(dolphin);
1105 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_SEQIN), dolphin);
1106 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(column & 0xff), dolphin);
1108 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(column & 0xff), dolphin);
1109 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1111 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1113 if (5 == dolphin->a_cycles)// five address cycles
1116 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1119 sprd_dolphin_nand_ins_add(NAND_MC_MWDT, dolphin);
1122 sprd_dolphin_nand_ins_add(NAND_MC_SWDT, dolphin);
1124 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_PAGEPROG), dolphin);
1125 sprd_dolphin_nand_ins_add(NFC_MC_WRB0_ID, dolphin); //wait rb
1127 sprd_dolphin_nand_ins_add(NFC_MC_DONE_ID, dolphin);
1129 cfg0 = NFC_AUTO_MODE | NFC_RW | NFC_WPN | MAIN_SPAR_APT | ((dolphin->sct_pg - 1)<< SECTOR_NUM_OFFSET);
1130 if((!raw) && mbuf && sbuf)
1134 if(chip->options & NAND_BUSWIDTH_16)
1138 cfg1 = ((dolphin->info_size) << SPAR_INFO_SIZE_OFFSET);
1139 cfg2 = (dolphin->ecc_mode << 12) | (dolphin->info_pos << SPAR_INFO_POS_OFFSET) | ((dolphin->sct_pg - 1) << SPAR_SECTOR_NUM_OFFSET) | dolphin->ecc_pos;
1141 #ifndef CONFIG_NAND_SPL
1144 Dcache_CleanRegion((unsigned int)mbuf, dolphin->m_size*dolphin->sct_pg);
1145 Dcache_InvalRegion((unsigned int)mbuf, dolphin->m_size*dolphin->sct_pg);
1150 Dcache_CleanRegion((unsigned int)sbuf, dolphin->s_size*dolphin->sct_pg);
1151 Dcache_InvalRegion((unsigned int)sbuf, dolphin->s_size*dolphin->sct_pg);
1157 cfg0 |= MAIN_USE | SPAR_USE;
1158 cfg1 |= (dolphin->m_size - 1) | ((dolphin->s_size - 1) << SPAR_SIZE_OFFSET);
1159 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, (uint32_t)mbuf);
1160 sprd_dolphin_reg_write(NFC_SPAR_ADDR_REG, (uint32_t)sbuf);
1167 cfg1 |= dolphin->m_size - 1;
1168 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, (uint32_t)mbuf);
1172 cfg1 |= dolphin->s_size - 1;
1173 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, (uint32_t)sbuf);
1176 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
1177 sprd_dolphin_reg_write(NFC_CFG1_REG, cfg1);
1178 sprd_dolphin_reg_write(NFC_CFG2_REG, cfg2);
1179 sprd_dolphin_nand_ins_exec(dolphin);
1180 sprd_dolphin_nand_wait_finish(dolphin);
1187 #ifdef DOLPHIN_KERNEL
1190 STATIC_FUNC void sprd_dolphin_nand_ins_exec_irq(struct sprd_dolphin_nand_info *dolphin)
1196 //DPRINT("%s enter\n", __func__);
1198 for(i = 0; i < ((dolphin->ins_num + 1) >> 1); i++)
1200 sprd_dolphin_reg_write(NFC_INST0_REG + (i << 2), dolphin->ins[i]);
1202 cfg0 = sprd_dolphin_reg_read(NFC_CFG0_REG);
1219 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
1221 nfc_clear_interrupt();
1222 nfc_enable_interrupt();
1224 sprd_dolphin_reg_write(NFC_START_REG, NFC_START);
1226 //DPRINT("%s leave\n", __func__);
1229 STATIC_FUNC int sprd_dolphin_nand_wait_finish_irq(struct sprd_dolphin_nand_info *dolphin)
1232 unsigned int counter = 0;
1234 //DPRINT("%s enter\n", __func__);
1237 if(handle_status==NAND_HANDLE_DONE)
1239 ret_irq_en=NAND_NO_ERROR;
1241 else if(handle_status==NAND_HANDLE_TIMEOUT)
1243 ret_irq_en=NAND_ERR_NEED_RETRY;
1245 else if(handle_status==NAND_HANDLE_ERR)
1247 ret_irq_en=NAND_ERR_NEED_RETRY;
1250 //DPRINT("%s leave\n", __func__);
1255 STATIC_FUNC void nfc_wait_op_done(void)
1257 if (!wait_for_completion_timeout(&nfc_op_completion, msecs_to_jiffies(IRQ_TIMEOUT)))
1259 handle_status=NAND_HANDLE_ERR;
1260 DPRINT(KERN_ERR "%s, wait irq timeout\n", __func__);
1264 STATIC_FUNC irqreturn_t nfc_irq_handler(int irq, void *dev_id)
1268 //DPRINT("%s enter\n", __func__); /*diable irq*/
1269 nfc_disable_interrupt();
1271 value = sprd_dolphin_reg_read(NFC_INT_REG);
1272 //DPRINT("%s, NFC_INT_REG:0x%x\n", __func__, value);
1273 /*record handle status*/
1274 if(value & INT_TO_STS)
1277 DPRINT(KERN_ALERT "%s, timeout occur NFC_INT_REG:0x%x\n", __func__, value);
1278 handle_status=NAND_HANDLE_TIMEOUT;
1280 else if(value & INT_DONE_STS)
1282 handle_status=NAND_HANDLE_DONE;
1285 /*clear irq status*/
1286 //value = (INT_DONE_CLR | INT_TO_CLR);
1287 //sprd_dolphin_reg_or(NFC_INT_REG, value); /* clear all interrupt status */
1289 //value = NFC_CMD_CLR;
1290 //sprd_dolphin_reg_or(NFC_START_REG, value); /* clear all interrupt status */
1292 nfc_clear_interrupt();
1294 complete(&nfc_op_completion);
1296 //DPRINT("%s leave\n", __func__);
1304 STATIC_FUNC int sprd_dolphin_nand_read_lp(struct mtd_info *mtd,uint8_t *mbuf, uint8_t *sbuf,uint32_t raw)
1306 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1307 struct nand_chip *chip = dolphin->nand;
1315 int size = 0 ,sct_size;
1319 page_addr = dolphin->page;
1321 //DPRINT("%s enter\n", __func__);
1324 column = mtd->writesize;
1330 if(chip->options & NAND_BUSWIDTH_16)
1334 //DPRINT("sprd_dolphin_nand_read_lp,page_addr = %x,column = %x\r\n",page_addr, column);
1336 sprd_dolphin_nand_ins_init(dolphin);
1337 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_READ0), dolphin);
1338 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(column & 0xff), dolphin);
1340 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(column & 0xff), dolphin);
1342 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1344 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1346 if (5 == dolphin->a_cycles)// five address cycles
1349 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1351 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_READSTART), dolphin);
1353 sprd_dolphin_nand_ins_add(NFC_MC_WRB0_ID, dolphin); //wait rb
1356 sprd_dolphin_nand_ins_add(NAND_MC_SRDT, dolphin);
1357 //switch to main part
1358 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_RNDOUT), dolphin);
1359 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(0), dolphin);
1360 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(0), dolphin);
1361 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_RNDOUTSTART), dolphin);
1362 sprd_dolphin_nand_ins_add(NAND_MC_MRDT, dolphin);
1366 sprd_dolphin_nand_ins_add(NAND_MC_MRDT, dolphin);
1368 sprd_dolphin_nand_ins_add(NFC_MC_DONE_ID, dolphin);
1370 cfg0 = NFC_AUTO_MODE | MAIN_SPAR_APT | ((dolphin->sct_pg - 1)<< SECTOR_NUM_OFFSET);
1371 if((!raw) && mbuf && sbuf)
1373 cfg0 |= ECC_EN | DETECT_ALL_FF;
1375 if(chip->options & NAND_BUSWIDTH_16)
1379 cfg1 = (dolphin->info_size) << SPAR_INFO_SIZE_OFFSET;
1380 cfg2 = (dolphin->ecc_mode << 12) | (dolphin->info_pos << SPAR_INFO_POS_OFFSET) | ((dolphin->sct_pg - 1) << SPAR_SECTOR_NUM_OFFSET) | dolphin->ecc_pos;
1384 cfg1 |= (dolphin->m_size - 1) | ((dolphin->s_size - 1)<< SPAR_SIZE_OFFSET);
1385 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, dolphin->p_mbuf);
1386 sprd_dolphin_reg_write(NFC_SPAR_ADDR_REG, dolphin->p_oob);
1387 cfg0 |= MAIN_USE | SPAR_USE;
1393 cfg1 |= (dolphin->m_size - 1);
1394 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, dolphin->p_mbuf);
1398 cfg1 |= (dolphin->s_size - 1);
1399 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, dolphin->p_oob);
1403 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
1404 sprd_dolphin_reg_write(NFC_CFG1_REG, cfg1);
1405 sprd_dolphin_reg_write(NFC_CFG2_REG, cfg2);
1409 sprd_dolphin_nand_ins_exec_irq(dolphin);
1410 sprd_dolphin_nand_wait_finish_irq(dolphin);
1412 sprd_dolphin_nand_ins_exec(dolphin);
1413 sprd_dolphin_nand_wait_finish(dolphin);
1418 for(i = 0; i < dolphin->sct_pg; i++) {
1419 err = sprd_dolphin_get_decode_sts(i);
1420 if(err == ERR_ERR_NUM0_MASK) {
1423 size = dolphin->s_size * (i+1);
1424 sct_size = dolphin->s_size;
1426 if(sbuf[--size] != 0xFF){
1427 DPRINT("%s spare area bif flip 0x%x\n",__func__,sbuf[size]);
1428 for(num=0; num<8; num++){
1429 if(!(sbuf[size]>>num & 0x1)){
1431 if(ecc_bit> chip->ecc.strength){
1433 DPRINT("%s spare area ecc check error!\n",__func__);
1439 mtd->ecc_stats.failed++;
1446 size = dolphin->m_size*(i+1) ;
1447 sct_size = dolphin->m_size ;
1449 if(mbuf[--size] != 0xFF){
1450 DPRINT("%s main area bif flip 0x%x\n",__func__,mbuf[size]);
1451 for(num = 0 ; num < 8 ; num++){
1452 if(!(mbuf[size]>>num & 0x1) ){
1454 if(ecc_bit > chip->ecc.strength){
1456 DPRINT("%s main+spare area ecc check error!\n",__func__);
1463 mtd->ecc_stats.failed++;
1471 mtd->ecc_stats.corrected += err;
1477 memcpy(mbuf, (const void *)dolphin->v_mbuf, dolphin->write_size);
1480 memcpy(sbuf, (const void *)dolphin->v_oob, dolphin->oob_size);
1488 STATIC_FUNC int sprd_dolphin_nand_write_lp(struct mtd_info *mtd,const uint8_t *mbuf, uint8_t *sbuf,uint32_t raw)
1490 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1491 struct nand_chip *chip = dolphin->nand;
1497 page_addr = dolphin->page;
1499 //DPRINT("%s, page addr is %lx\n", __func__, page_addr);
1505 column = mtd->writesize;
1507 if(chip->options & NAND_BUSWIDTH_16)
1513 memcpy((void *)dolphin->v_mbuf, (const void *)mbuf, dolphin->write_size);
1516 memcpy((void *)dolphin->v_oob, (const void *)sbuf, dolphin->oob_size);
1519 sprd_dolphin_nand_ins_init(dolphin);
1520 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_SEQIN), dolphin);
1521 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(column & 0xff), dolphin);
1523 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(column & 0xff), dolphin);
1524 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1526 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1528 if (5 == dolphin->a_cycles)// five address cycles
1531 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1534 sprd_dolphin_nand_ins_add(NAND_MC_MWDT, dolphin);
1537 sprd_dolphin_nand_ins_add(NAND_MC_SWDT, dolphin);
1539 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_PAGEPROG), dolphin);
1540 sprd_dolphin_nand_ins_add(NFC_MC_WRB0_ID, dolphin); //wait rb
1542 sprd_dolphin_nand_ins_add(NFC_MC_DONE_ID, dolphin);
1544 cfg0 = NFC_AUTO_MODE | NFC_RW | NFC_WPN | MAIN_SPAR_APT | ((dolphin->sct_pg - 1)<< SECTOR_NUM_OFFSET);
1545 if((!raw) && mbuf && sbuf)
1549 if(chip->options & NAND_BUSWIDTH_16)
1553 cfg1 = ((dolphin->info_size) << SPAR_INFO_SIZE_OFFSET);
1554 cfg2 = (dolphin->ecc_mode << 12) | (dolphin->info_pos << SPAR_INFO_POS_OFFSET) | ((dolphin->sct_pg - 1) << SPAR_SECTOR_NUM_OFFSET) | dolphin->ecc_pos;
1558 cfg0 |= MAIN_USE | SPAR_USE;
1559 cfg1 |= (dolphin->m_size - 1) | ((dolphin->s_size - 1) << SPAR_SIZE_OFFSET);
1560 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, dolphin->p_mbuf);
1561 sprd_dolphin_reg_write(NFC_SPAR_ADDR_REG, dolphin->p_oob);
1568 cfg1 |= dolphin->m_size - 1;
1569 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, dolphin->p_mbuf);
1573 cfg1 |= dolphin->s_size - 1;
1574 sprd_dolphin_reg_write(NFC_MAIN_ADDR_REG, dolphin->p_oob);
1577 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
1578 sprd_dolphin_reg_write(NFC_CFG1_REG, cfg1);
1579 sprd_dolphin_reg_write(NFC_CFG2_REG, cfg2);
1583 sprd_dolphin_nand_ins_exec_irq(dolphin);
1584 sprd_dolphin_nand_wait_finish_irq(dolphin);
1586 sprd_dolphin_nand_ins_exec(dolphin);
1587 sprd_dolphin_nand_wait_finish(dolphin);
1596 STATIC_FUNC int sprd_dolphin_nand_read_sp(struct mtd_info *mtd,uint8_t *mbuf, uint8_t *sbuf,uint32_t raw)
1600 STATIC_FUNC int sprd_dolphin_nand_write_sp(struct mtd_info *mtd,const uint8_t *mbuf, uint8_t *sbuf,uint32_t raw)
1604 STATIC_FUNC void sprd_dolphin_erase(struct mtd_info *mtd, int page_addr)
1606 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1609 //DPRINT("%s, page addr is %x\r\n", __func__ , page_addr);
1611 sprd_dolphin_nand_ins_init(dolphin);
1612 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_ERASE1), dolphin);
1613 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1615 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1616 if((5 == dolphin->a_cycles) || ((4 == dolphin->a_cycles) && (512 == dolphin->write_size)))
1619 sprd_dolphin_nand_ins_add(NAND_MC_ADDR(page_addr & 0xff), dolphin);
1621 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_ERASE2), dolphin);
1622 sprd_dolphin_nand_ins_add(NFC_MC_WRB0_ID, dolphin); //wait rb
1624 sprd_dolphin_nand_ins_add(NFC_MC_DONE_ID, dolphin);
1625 cfg0 = NFC_WPN | NFC_ONLY_NAND_MODE;
1626 sprd_dolphin_reg_write(NFC_CFG0_REG, cfg0);
1630 sprd_dolphin_nand_ins_exec_irq(dolphin);
1631 sprd_dolphin_nand_wait_finish_irq(dolphin);
1633 sprd_dolphin_nand_ins_exec(dolphin);
1634 sprd_dolphin_nand_wait_finish(dolphin);
1637 //DPRINT("%s leave\n", __func__);
1639 STATIC_FUNC uint8_t sprd_dolphin_read_byte(struct mtd_info *mtd)
1642 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1643 if(dolphin->buf_head < dolphin->_buf_tail)
1645 ch = s_oob_data[dolphin->buf_head ++];
1649 STATIC_FUNC uint16_t sprd_dolphin_read_word(struct mtd_info *mtd)
1651 uint16_t data = 0xffff;
1652 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1653 if(dolphin->buf_head < (dolphin->_buf_tail - 1))
1655 data = s_oob_data[dolphin->buf_head ++];
1656 data |= ((uint16_t)s_oob_data[dolphin->buf_head ++]) << 8;
1661 STATIC_FUNC int sprd_dolphin_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
1666 STATIC_FUNC int sprd_dolphin_ecc_calculate(struct mtd_info *mtd, const uint8_t *data,
1672 STATIC_FUNC int sprd_dolphin_ecc_correct(struct mtd_info *mtd, uint8_t *data,
1673 uint8_t *read_ecc, uint8_t *calc_ecc)
1678 static int sprd_dolphin_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1679 uint8_t *buf, int page)
1681 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1682 dolphin->page = page;
1683 if(512 == mtd->writesize)
1685 sprd_dolphin_nand_read_sp(mtd, buf, chip->oob_poi, 0);
1689 sprd_dolphin_nand_read_lp(mtd, buf, chip->oob_poi, 0);
1693 static int sprd_dolphin_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1694 uint8_t *buf, int page)
1696 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1697 dolphin->page = page;
1698 if(512 == mtd->writesize)
1700 sprd_dolphin_nand_read_sp(mtd, buf, chip->oob_poi, 1);
1704 sprd_dolphin_nand_read_lp(mtd, buf, chip->oob_poi, 1);
1708 STATIC_FUNC int sprd_dolphin_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1709 int page, int sndcmd)
1711 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1712 dolphin->page = page;
1713 if(512 == mtd->writesize)
1715 sprd_dolphin_nand_read_sp(mtd, 0, chip->oob_poi, 1);
1719 sprd_dolphin_nand_read_lp(mtd, 0, chip->oob_poi, 1);
1723 STATIC_FUNC void sprd_dolphin_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1726 if(512 == mtd->writesize)
1728 sprd_dolphin_nand_write_sp(mtd, buf, chip->oob_poi, 0);
1732 sprd_dolphin_nand_write_lp(mtd, buf, chip->oob_poi, 0);
1736 STATIC_FUNC void sprd_dolphin_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1739 if(512 == mtd->writesize)
1741 sprd_dolphin_nand_write_sp(mtd, buf, chip->oob_poi, 1);
1745 sprd_dolphin_nand_write_lp(mtd, buf, chip->oob_poi, 1);
1748 STATIC_FUNC int sprd_dolphin_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1751 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1752 dolphin->page = page;
1753 if(512 == mtd->writesize)
1755 sprd_dolphin_nand_write_sp(mtd, 0, chip->oob_poi, 1);
1759 sprd_dolphin_nand_write_lp(mtd, 0, chip->oob_poi, 1);
1766 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
1767 * @mtd: MTD device structure
1768 * @ofs: offset from device start
1769 * @getchip: 0, if the chip is already selected
1771 * Check, if the block is bad.
1773 STATIC_FUNC int sprd_dolphin_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
1775 int page, chipnr, res = 0;
1776 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1777 struct nand_chip *chip = mtd->priv;
1781 page = (int)((long)ofs >> chip->page_shift) & chip->pagemask;
1784 chipnr = (int)((long)ofs >> chip->chip_shift);
1785 /* Select the NAND device */
1786 chip->select_chip(mtd, chipnr);
1789 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1790 if(512 == dolphin->write_size) {
1791 sprd_dolphin_nand_read_sp(mtd, 0, s_oob_data, 1);
1794 sprd_dolphin_nand_read_lp(mtd, 0, s_oob_data, 1);
1796 dolphin->buf_head = 0;
1797 dolphin->_buf_tail = mtd->oobsize;
1798 buf = (uint16_t *)(s_oob_data + chip->badblockpos);
1800 if (chip->options & NAND_BUSWIDTH_16) {
1802 if ((bad & 0xFF) != 0xff) {
1806 bad = *(buf) & 0xff;
1814 STATIC_FUNC void sprd_dolphin_nand_cmdfunc(struct mtd_info *mtd, unsigned int command,
1815 int column, int page_addr)
1817 struct sprd_dolphin_nand_info *dolphin = mtd_to_dolphin(mtd);
1818 /* Emulate NAND_CMD_READOOB */
1819 if (command == NAND_CMD_READOOB) {
1820 column += mtd->writesize;
1821 command = NAND_CMD_READ0;
1824 * program and erase have their own busy handlers
1825 * status, sequential in, and deplete1 need no delay
1828 case NAND_CMD_STATUS:
1829 sprd_dolphin_nand_read_status(dolphin);
1831 case NAND_CMD_READID:
1832 sprd_dolphin_nand_read_id(dolphin, (uint32_t *)s_oob_data);
1834 case NAND_CMD_RESET:
1835 sprd_dolphin_nand_reset(dolphin);
1837 case NAND_CMD_ERASE1:
1838 sprd_dolphin_erase(mtd, page_addr);
1840 case NAND_CMD_READ0:
1841 case NAND_CMD_SEQIN:
1842 dolphin->column = column;
1843 dolphin->page = page_addr;
1848 STATIC_FUNC void sprd_dolphin_nand_hwecc_ctl(struct mtd_info *mtd, int mode)
1850 return; //do nothing
1855 #ifdef DOLPHIN_UBOOT
1856 #define DOLPHIN_AHB_BASE SPRD_AHB_PHYS
1857 #define DOLPHIN_PIN_BASE SPRD_PIN_PHYS
1858 #define DOLPHIN_AHB_RST (DOLPHIN_AHB_BASE + 0x0004)
1859 #define DOLPHIN_NANC_CLK_CFG (DOLPHIN_AHB_BASE + 0x0060)
1861 #define DOLPHIN_ADISLAVE_BASE SPRD_ADISLAVE_PHYS
1862 #define DOLPHIN_ANA_CTL_GLB_BASE (DOLPHIN_ADISLAVE_BASE + 0x8800)
1864 #define DOLPHIN_NFC_REG_BASE SPRD_NFC_PHYS
1865 #define DOLPHIN_NFC_TIMING_REG (DOLPHIN_NFC_REG_BASE + 0x14)
1866 #define DOLPHIN_NFC_TIMEOUT_REG (DOLPHIN_NFC_REG_BASE + 0x34)
1869 #ifdef DOLPHIN_KERNEL
1870 #define DOLPHIN_AHB_BASE SPRD_AHB_BASE
1871 #define DOLPHIN_PIN_BASE SPRD_PIN_BASE
1872 #define DOLPHIN_AHB_RST (DOLPHIN_AHB_BASE + 0x0004)
1873 #define DOLPHIN_NANC_CLK_CFG (DOLPHIN_AHB_BASE + 0x0060)
1875 #define DOLPHIN_ADISLAVE_BASE SPRD_ADISLAVE_BASE
1876 #define DOLPHIN_ANA_CTL_GLB_BASE (DOLPHIN_ADISLAVE_BASE + 0x8800)
1878 #define DOLPHIN_NFC_REG_BASE SPRD_NFC_BASE
1879 #define DOLPHIN_NFC_TIMING_REG (DOLPHIN_NFC_REG_BASE + 0x14)
1880 #define DOLPHIN_NFC_TIMEOUT_REG (DOLPHIN_NFC_REG_BASE + 0x34)
1884 STATIC_FUNC void sprd_dolphin_nand_hw_init(struct sprd_dolphin_nand_info *dolphin)
1889 sprd_dolphin_reg_and(DOLPHIN_NANC_CLK_CFG, ~(BIT(1) | BIT(0)));
1890 sprd_dolphin_reg_or(DOLPHIN_NANC_CLK_CFG, BIT(0));
1892 sprd_dolphin_reg_or(DOLPHIN_AHB_BASE, BIT(19) | BIT(18) | BIT(17));
1894 sprd_dolphin_reg_or(DOLPHIN_AHB_RST,BIT(20));
1896 sprd_dolphin_reg_and(DOLPHIN_AHB_RST, ~(BIT(20)));
1898 val = (3) | (4 << NFC_RWH_OFFSET) | (3 << NFC_RWE_OFFSET) | (3 << NFC_RWS_OFFSET) | (3 << NFC_ACE_OFFSET) | (3 << NFC_ACS_OFFSET);
1899 sprd_dolphin_reg_write(DOLPHIN_NFC_TIMING_REG, val);
1900 sprd_dolphin_reg_write(DOLPHIN_NFC_TIMEOUT_REG, 0xffffffff);
1901 sprd_dolphin_reg_write(DOLPHIN_NFC_REG_BASE + 0x118, 3);
1904 sprd_dolphin_reg_or(DOLPHIN_PIN_BASE + REG_PIN_NFWPN, BIT(7) | BIT(8) | BIT(9));
1905 sprd_dolphin_reg_and(DOLPHIN_PIN_BASE + REG_PIN_NFWPN, ~(BIT(4) | BIT(5)));
1906 sprd_dolphin_reg_or(DOLPHIN_PIN_BASE + REG_PIN_NFRB, BIT(7) | BIT(8) | BIT(9));
1907 sprd_dolphin_reg_and(DOLPHIN_PIN_BASE + REG_PIN_NFRB, ~(BIT(4) | BIT(5)));
1908 for(i = 0; i < 22; ++i)
1910 sprd_dolphin_reg_or(DOLPHIN_PIN_BASE + REG_PIN_NFCLE + (i << 2), BIT(7) | BIT(8) | BIT(9));
1911 sprd_dolphin_reg_and(DOLPHIN_PIN_BASE + REG_PIN_NFCLE + (i << 2), ~(BIT(4) | BIT(5)));
1914 i = sprd_dolphin_reg_read(DOLPHIN_ANA_CTL_GLB_BASE+0x3c);
1917 sprd_dolphin_reg_write(DOLPHIN_ANA_CTL_GLB_BASE + 0x3c, i);
1919 i = sprd_dolphin_reg_read(DOLPHIN_ANA_CTL_GLB_BASE+0x20);
1922 sprd_dolphin_reg_write(DOLPHIN_ANA_CTL_GLB_BASE + 0x20, i);
1924 //close write protect
1925 sprd_dolphin_nand_wp_en(dolphin, 0);
1929 int board_nand_init(struct nand_chip *chip)
1931 DPRINT("board_nand_init\r\n");
1933 sprd_dolphin_nand_hw_init(&g_dolphin);
1935 chip->select_chip = sprd_dolphin_select_chip;
1936 chip->cmdfunc = sprd_dolphin_nand_cmdfunc;
1937 chip->read_byte = sprd_dolphin_read_byte;
1938 chip->read_word = sprd_dolphin_read_word;
1939 chip->waitfunc = sprd_dolphin_waitfunc;
1940 chip->ecc.mode = NAND_ECC_HW;
1941 chip->ecc.calculate = sprd_dolphin_ecc_calculate;
1942 chip->ecc.hwctl = sprd_dolphin_nand_hwecc_ctl;
1943 chip->ecc.correct = sprd_dolphin_ecc_correct;
1944 chip->ecc.read_page = sprd_dolphin_read_page;
1945 chip->ecc.read_page_raw = sprd_dolphin_read_page_raw;
1946 chip->ecc.write_page = sprd_dolphin_write_page;
1947 chip->ecc.write_page_raw = sprd_dolphin_write_page_raw;
1948 chip->ecc.read_oob = sprd_dolphin_read_oob;
1949 chip->ecc.write_oob = sprd_dolphin_write_oob;
1950 chip->erase_cmd = sprd_dolphin_erase;
1952 chip->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
1953 g_dolphin.ecc_mode = ecc_mode_convert(CONFIG_SYS_NAND_ECC_MODE);
1954 g_dolphin.nand = chip;
1956 #ifdef DOLPHIN_KERNEL
1957 chip->ecc.strength = CONFIG_SYS_NAND_ECC_MODE;
1960 #ifdef DOLPHIN_UBOOT
1961 chip->eccbitmode = CONFIG_SYS_NAND_ECC_MODE;
1964 //dolphin_set_timing_config(&g_dolphin, SPRD_NAND_CLOCK); /* 153 is current clock 153MHz */
1966 chip->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
1968 chip->chip_delay = 20;
1969 chip->priv = &g_dolphin;
1971 //DPRINT("v2 board eccbitmode %d\n", chip->eccbitmode);
1973 chip->options = NAND_BUSWIDTH_16;
1978 #ifdef DOLPHIN_UBOOT
1979 #ifndef CONFIG_NAND_SPL
1980 void McuReadNandType(unsigned char *array)
1986 static unsigned long nfc_read_status(void)
1988 unsigned long status = 0;
1990 sprd_dolphin_nand_read_status(&g_dolphin);
1991 status = s_oob_data[0];
1996 #ifndef CONFIG_NAND_SPL
1997 static int sprd_scan_one_block(int blk, int erasesize, int writesize)
2002 int oobsize = mtdoobsize;
2003 int column, page_addr;
2005 page_addr = blk * (erasesize / writesize);
2006 for (ii = 0; ii < 2; ii ++) {
2007 DPRINT("please debug here : %s %d\n", __FUNCTION__, __LINE__);
2008 sprd_dolphin_nand_ins_init(&g_dolphin);
2009 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_READ0), &g_dolphin);
2010 sprd_dolphin_nand_ins_add(NAND_MC_CMD(NAND_CMD_READSTART), &g_dolphin);
2011 if ((s_oob_data[0] != 0xff) || (s_oob_data[1] != 0xff))
2013 } //for (ii = 0; ii < 2; ii ++)
2015 if ((s_oob_data[0] == 0xff) && (s_oob_data[1] == 0xff))
2016 status = 0; //good block
2018 status = 1; //bad block
2023 static unsigned long nand_ctl_erase_block(int blk, int erasesize, int writesize)
2028 page_addr = blk * (erasesize / writesize);
2029 sprd_dolphin_erase(&g_dolphin, page_addr);
2030 status = nfc_read_status();
2037 #ifndef CONFIG_NAND_SPL
2038 void nand_scan_patition(int blocks, int erasesize, int writesize)
2045 for (blk = 0; blk < blocks; blk ++) {
2046 ret = sprd_scan_one_block(blk, erasesize, writesize);
2048 DPRINT("\n%d is bad, scrub to erase it, ", blk);
2049 ret = nand_ctl_erase_block(blk, erasesize, writesize);
2050 DPRINT("0x%02x\n", ret);
2052 ret = nand_ctl_erase_block(blk, erasesize, writesize);
2053 DPRINT("erasing block : %d %d % \r", blk, (blk * 100 ) / blocks);
2057 int nand_scan_block(int block, int erasesize, int writesize){
2059 ret = nand_ctl_erase_block(block, erasesize, writesize);
2065 #endif //DOLPHIN_UBOOT end
2069 #ifdef DOLPHIN_KERNEL
2070 extern int parse_mtd_partitions(struct mtd_info *master, const char **types,
2071 struct mtd_partition **pparts,
2072 struct mtd_part_parser_data *data);
2074 static struct mtd_info *sprd_mtd = NULL;
2075 #ifdef CONFIG_MTD_CMDLINE_PARTS
2076 const char *part_probes[] = { "cmdlinepart", NULL };
2079 STATIC_FUNC int sprd_nand_dma_init(struct sprd_dolphin_nand_info *dolphin)
2081 dma_addr_t phys_addr = 0;
2083 virt_ptr = dma_alloc_coherent(NULL, dolphin->write_size, &phys_addr, GFP_KERNEL);
2084 if (virt_ptr == NULL) {
2085 DPRINT(KERN_ERR "NAND - Failed to allocate memory for DMA main buffer\n");
2088 dolphin->v_mbuf = (u32)virt_ptr;
2089 dolphin->p_mbuf = (u32)phys_addr;
2091 virt_ptr = dma_alloc_coherent(NULL, dolphin->oob_size, &phys_addr, GFP_KERNEL);
2092 if (virt_ptr == NULL) {
2093 DPRINT(KERN_ERR "NAND - Failed to allocate memory for DMA oob buffer\n");
2094 dma_free_coherent(NULL, dolphin->write_size, (void *)dolphin->v_mbuf, (dma_addr_t)dolphin->p_mbuf);
2097 dolphin->v_oob = (u32)virt_ptr;
2098 dolphin->p_oob = (u32)phys_addr;
2101 STATIC_FUNC void sprd_nand_dma_deinit(struct sprd_dolphin_nand_info *dolphin)
2103 dma_free_coherent(NULL, dolphin->write_size, (void *)dolphin->v_mbuf, (dma_addr_t)dolphin->p_mbuf);
2104 dma_free_coherent(NULL, dolphin->write_size, (void *)dolphin->v_oob, (dma_addr_t)dolphin->p_oob);
2106 STATIC_FUNC int sprd_nand_probe(struct platform_device *pdev)
2108 struct nand_chip *this;
2109 struct resource *regs = NULL;
2110 struct mtd_partition *partitions = NULL;
2111 int num_partitions = 0;
2117 init_completion(&nfc_op_completion);
2118 err = request_irq(IRQ_NFC_INT, nfc_irq_handler, 0, DRIVER_NAME, NULL);
2120 DPRINT(KERN_ERR "request_irq error\n");
2123 DPRINT(KERN_ALERT "request_irq ok\n");
2127 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2129 dev_err(&pdev->dev,"resources unusable\n");
2133 memset(&g_dolphin, 0 , sizeof(g_dolphin));
2135 platform_set_drvdata(pdev, &g_dolphin);
2136 g_dolphin.pdev = pdev;
2138 sprd_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
2139 this = (struct nand_chip *)(&sprd_mtd[1]);
2140 memset((char *)sprd_mtd, 0, sizeof(struct mtd_info));
2141 memset((char *)this, 0, sizeof(struct nand_chip));
2143 sprd_mtd->priv = this;
2145 this->options |= NAND_BUSWIDTH_16;
2146 //this->options |= NAND_NO_READRDY;
2148 board_nand_init(this);
2151 if (sprd_dolphin_nand_reset(&g_dolphin) != 0)
2154 DPRINT(KERN_ERR "nand reset failed!!!!!!!!!!!!!\n");
2159 if (sprd_dolphin_nand_read_id(&g_dolphin, (uint32_t *)s_oob_data) != 0)
2162 DPRINT(KERN_ERR "nand read id failed, no nand device!!!!!!!!!!!!!\n");
2164 DPRINT(KERN_ALERT "nand read id ok, nand exists!!!!!!!!!!!!!\n");
2166 //nand_scan(sprd_mtd, 1);
2167 /* first scan to find the device and get the page size */
2168 if (nand_scan_ident(sprd_mtd, 1, NULL)) {
2172 sprd_dolphin_nand_read_id(&g_dolphin, (uint32_t *)s_oob_data);
2173 nand_hardware_config(sprd_mtd, this, s_oob_data);
2174 if(sprd_nand_dma_init(&g_dolphin) != 0) {
2178 //this->IO_ADDR_R = g_dolphin.v_mbuf;
2179 //this->IO_ADDR_W = g_dolphin.v_mbuf;
2181 /* second phase scan */
2182 if (nand_scan_tail(sprd_mtd)) {
2187 sprd_mtd->name = "sprd-nand";
2188 num_partitions = parse_mtd_partitions(sprd_mtd, part_probes, &partitions, 0);
2190 if ((!partitions) || (num_partitions == 0)) {
2191 DPRINT(KERN_ALERT "No parititions defined, or unsupported device.\n");
2195 #ifdef CONFIG_MTD_CMDLINE_PARTS
2196 mtd_device_register(sprd_mtd, partitions, num_partitions);
2201 nand_release(sprd_mtd);
2202 sprd_nand_dma_deinit(&g_dolphin);
2204 sprd_dolphin_reg_and(DOLPHIN_AHB_BASE,~(BIT(19) | BIT(18) | BIT(17)));
2209 STATIC_FUNC int sprd_nand_remove(struct platform_device *pdev)
2211 platform_set_drvdata(pdev, NULL);
2212 nand_release(sprd_mtd);
2213 sprd_nand_dma_deinit(&g_dolphin);
2219 STATIC_FUNC int sprd_nand_suspend(struct platform_device *dev, pm_message_t pm)
2225 STATIC_FUNC int sprd_nand_resume(struct platform_device *dev)
2227 sprd_dolphin_nand_hw_init(&g_dolphin);
2231 #define sprd_nand_suspend NULL
2232 #define sprd_nand_resume NULL
2235 static struct platform_driver sprd_nand_driver = {
2236 .probe = sprd_nand_probe,
2237 .remove = sprd_nand_remove,
2238 .suspend = sprd_nand_suspend,
2239 .resume = sprd_nand_resume,
2241 .name = "sprd-nand",
2242 .owner = THIS_MODULE,
2246 STATIC_FUNC int __init sprd_nand_init(void)
2248 return platform_driver_register(&sprd_nand_driver);
2251 STATIC_FUNC void __exit sprd_nand_exit(void)
2253 platform_driver_unregister(&sprd_nand_driver);
2256 module_init(sprd_nand_init);
2257 module_exit(sprd_nand_exit);
2259 MODULE_LICENSE("GPL");
2260 MODULE_AUTHOR("giya.li@spreadtrum.com");
2261 MODULE_DESCRIPTION("SPRD dolphin MTD NAND driver");
2262 MODULE_ALIAS("platform:sprd-nand");