apollo_nand.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /* linux/drivers/mtd/nand/am8218net_nand.c
  2. *
  3. *
  4. * Copyright (c) 2008 Amlogic, Inc.
  5. *
  6. * Derived from drivers/mtd/nand/s3c2410.c
  7. * Copyright (c) 2007 Ben Dooks <ben@simtec.co.uk>
  8. *
  9. * Derived from drivers/mtd/nand/cafe.c
  10. * Copyright © 2006 Red Hat, Inc.
  11. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  12. *
  13. * Changelog:
  14. *
  15. * TODO:
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/ioport.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/delay.h>
  25. #include <linux/err.h>
  26. #include <linux/slab.h>
  27. #include <linux/io.h>
  28. #include <linux/bitops.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/nand.h>
  31. #include <linux/mtd/nand_ecc.h>
  32. #include <linux/mtd/partitions.h>
  33. #include <asm/cache.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/nand.h>
  36. #include <asm/io.h>
  37. static unsigned int hardware_ecc = 1;
  38. static unsigned int nand_config=0;
  39. static unsigned char nf_bch_inv = 0;
  40. static unsigned char nf_bch_mode = 0; //NIKE only BCH9
  41. static unsigned long USER_BYTES_3_0 = 0x123456ff;
  42. //For MLC 4K page need more user byte
  43. #define BADBLOCK_FLAG_BYTE 6 //NOT USE BBT
  44. /*
  45. * Data structures for nand flash controller driver
  46. */
  47. static uint8_t scan_userbyte_pattern[] = {0xff, 0x56,0x34,0x12};
  48. static unsigned char flash_id[8];
  49. static unsigned char PageShiftTable[4] = {10, 11, 12, 13};
  50. static struct nand_bbt_descr slcpage_memorybased = {
  51. .options = 0,
  52. .offs = 0,
  53. .len = 1,
  54. .pattern = scan_userbyte_pattern
  55. };
  56. struct am8218_nand_info {
  57. /* mtd info */
  58. struct nand_hw_control controller;
  59. struct mtd_info mtd;
  60. struct nand_chip chip;
  61. /* platform info */
  62. struct am8218_nand_platform *platform;
  63. /* device info */
  64. struct device *device;
  65. unsigned char *aml_nand_dma_buf;
  66. };
  67. /*
  68. * Conversion functions
  69. */
  70. static struct am8218_nand_info *mtd_to_nand_info(struct mtd_info *mtd){
  71. return container_of(mtd, struct am8218_nand_info, mtd);
  72. }
  73. static struct am8218_nand_info *to_nand_info(struct platform_device *pdev){
  74. return platform_get_drvdata(pdev);
  75. }
  76. static struct am8218_nand_platform *to_nand_plat(struct platform_device *pdev){
  77. return pdev->dev.platform_data;
  78. }
  79. /*
  80. * struct nand_chip interface function pointers
  81. */
  82. /*
  83. * am8218_nand_hwcontrol
  84. * Issue command and address cycles to the chip
  85. */
  86. static void am8218_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl){
  87. if (cmd == NAND_CMD_NONE)
  88. return;
  89. if (ctrl & NAND_CLE)
  90. NAND_SEND_COMMAND(cmd);
  91. else
  92. NAND_SEND_ADDR(cmd);
  93. //tWB note MTD also do some like this
  94. // while (!NAND_CHECK_BUSY())
  95. // cpu_relax();
  96. }
  97. /*
  98. * am8218_nand_devready
  99. * returns 0 if the nand is busy, 1 if it is ready
  100. * note nand_erase use but
  101. * should set nandchip->delay for other func such as
  102. * nand_command_lp will latch twb
  103. *
  104. */
  105. static int am8218_nand_devready(struct mtd_info *mtd){
  106. return NAND_CHECK_BUSY();
  107. //?0:1;
  108. }
  109. /*
  110. * ECC functions
  111. *
  112. * note callback func must provide for safe
  113. * *
  114. */
  115. static void am8218_nand_enable_hwecc(struct mtd_info *mtd, int mode){
  116. return;
  117. }
  118. static int am8218_nand_calculate_ecc(struct mtd_info *mtd,const u_char *dat, u_char *ecc_code){
  119. return 0;
  120. }
  121. static int am8218_nand_correct_data(struct mtd_info *mtd, u_char *dat,u_char *read_ecc, u_char *calc_ecc){
  122. // BUG();
  123. return 0;
  124. }
  125. /*
  126. * PIO mode for buffer writing and reading
  127. */
  128. static uint8_t am8218_nand_read_byte(struct mtd_info *mtd){
  129. uint8_t val;
  130. struct nand_chip * this=mtd->priv;
  131. val=IO_READ8(this->IO_ADDR_R);
  132. return val;
  133. }
  134. static void am8218_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len){
  135. uint32_t * ptr=(uint32_t *)buf;
  136. for(;len>0;len-=4)
  137. *ptr++=NAND_GET_DATA();
  138. }
  139. static void am8218_nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len){
  140. uint32_t * ptr=(uint32_t *)buf;
  141. for(;len>0;len-=4)
  142. *ptr++=NAND_GET_DATA();
  143. }
  144. static void am8218_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len){
  145. uint32_t * ptr=(uint32_t *)buf;
  146. for(;len>0;len-=4)
  147. NAND_SEND_DATA(*ptr++);
  148. }
  149. static void am8218_nand_write_buf16(struct mtd_info *mtd,const uint8_t *buf, int len) {
  150. uint32_t * ptr=(uint32_t *)buf;
  151. for(;len>0;len-=4)
  152. NAND_SEND_DATA(*ptr++);
  153. }
  154. static int nand_id_read(unsigned char * data)
  155. {
  156. unsigned *p;
  157. unsigned i;
  158. p = (unsigned*)data;
  159. NAND_SEND_COMMAND(NAND_CMD_READID);
  160. NAND_SEND_ADDR(0x00);
  161. for (i = 0; i < 8 / sizeof(unsigned); i++)
  162. {
  163. *p++ = NAND_GET_DATA();
  164. }
  165. return 0;
  166. }
  167. static int Nand_Dma_Waiting(void){
  168. uint32_t i=0;
  169. while(READ_PERIPHS_REG(PNAND_DMA_CNTL) & (1 << 31) && i<0x1000000)
  170. i++;
  171. if(i<0x1000000)
  172. return 0;
  173. NAND_SET_DMA_CNTL(NAND_DMA_ABORT);
  174. return 1;
  175. }
  176. #ifdef CONFIG_AMLOGIC_BOARD_NIKE
  177. static void am8218_nand_reset(void){
  178. NAND_RESET_FOR_WRITE();
  179. NAND_SET_MANUAL_CONFIG(nand_config);
  180. }
  181. #endif
  182. int nf_id_valid (char *flash_id)
  183. {
  184. char manufacturers_id;
  185. manufacturers_id = flash_id[0];
  186. switch (manufacturers_id)
  187. {
  188. case NAND_MFR_SAMSUNG:
  189. case NAND_MFR_HYNIX:
  190. case NAND_MFR_TOSHIBA:
  191. case NAND_MFR_FUJITSU:
  192. case NAND_MFR_NATIONAL:
  193. case NAND_MFR_RENESAS:
  194. case NAND_MFR_STMICRO:
  195. case NAND_MFR_MICRON:
  196. case NAND_MFR_AMD:
  197. case NAND_MFR_INTEL:
  198. return 0;
  199. default:
  200. return -1;
  201. }
  202. }
  203. static int Nand_Ecc_Check(void * dest,uint32_t page_size){
  204. uint32_t j;
  205. uint32_t page;
  206. uint32_t err_count;
  207. uint32_t bch15 = nf_bch_mode;
  208. uint32_t sectors = 0;
  209. uint32_t byte;
  210. uint32_t bit;
  211. uint32_t error_cnt_reg;
  212. uint32_t allff;
  213. if(!hardware_ecc)
  214. return 0;
  215. sectors = page_size/512;
  216. if(!((sectors==1)||(sectors==4)||(sectors==8)))
  217. return 1;
  218. //flush_and_inv_dcache_all(); //FIXME
  219. //inv_dcache_all();
  220. inv_dcache_range((unsigned long)dest,((unsigned long)dest)+page_size-1);
  221. #if (defined CONFIG_AMLOGIC_BOARD_APOLLO) || (defined CONFIG_AMLOGIC_BOARD_APOLLO_H)
  222. allff=READ_PERIPHS_REG(PNAND_CONFIG_REG);
  223. if(allff&((1<<29)|(1<<30)) == ((1<<29)|(1<<30))){
  224. return 0;
  225. }
  226. #endif
  227. error_cnt_reg = READ_PERIPHS_REG(PNAND_ERR_CNT0);
  228. for( page = 0; page < sectors; page++ ) {
  229. // If page transferred and has correctable errors
  230. if( !(READ_PERIPHS_REG(PNAND_ERR_UNC) & (1 << page)) ) {
  231. // Fix errors for 512 bytes (page)
  232. err_count = (error_cnt_reg >> ((page)*4)) & 0xf;
  233. //sdram_base = *(volatile unsigned char*)dest ;
  234. // Point to the error list in the FIFO/memory
  235. if(err_count){
  236. if( bch15 ) { WRITE_PERIPHS_REG(PNAND_ERR_LOC, page*15); }
  237. else { WRITE_PERIPHS_REG(PNAND_ERR_LOC, page*9); }
  238. }
  239. for(j = 0; j < err_count; j++ ) {
  240. byte = READ_PERIPHS_REG(PNAND_ERR_LOC); // Read byte/bit location
  241. bit = byte & 0x7;
  242. //ASSERT((err_count!=0)&is_printf,err_count,nf_address,page,(byte >> 4),bit)
  243. // If the error is in the payload section
  244. if( (byte >> 4) <= 511 ) {
  245. (*(volatile unsigned char *)((volatile unsigned char *)dest + page*512 + (byte >> 4))) ^= (1 << bit);
  246. }
  247. printk(" correct error at byte %x bit %x ",byte>>4,bit);
  248. }
  249. }
  250. else {
  251. //MAYBE FOR BBT
  252. printk(" NAND Uncorrect error ,May be all oxff \n");
  253. return -1;
  254. }
  255. }
  256. return 0;
  257. }
  258. static int am8218_nand_dma_rw(uint8_t * buf, int len, int isread, int eccornot){
  259. int32_t ret=0;
  260. if(isread){
  261. inv_dcache_range((unsigned long )buf,((unsigned long )buf)+len-1);
  262. }
  263. else{
  264. flush_dcache_range((unsigned long )buf, ((unsigned long )buf)+len-1);
  265. #ifdef CONFIG_AMLOGIC_BOARD_NIKE
  266. am8218_nand_reset();
  267. #endif
  268. NAND_SET_USERBYTES_0_3(USER_BYTES_3_0);
  269. }
  270. invalidate_ahb_cache();
  271. NAND_SET_DMA_START_ADDR(buf);
  272. NAND_SET_DMA_XFER_COUNT(len);
  273. NAND_SET_DMA_CNTL((isread?NAND_DMA_DIRECTOR_READ:0)|
  274. NAND_DMA_TYPE_NORMAL |
  275. (eccornot?NAND_DMA_ECC_ENABLE:0)|
  276. (nf_bch_inv<<25)|
  277. (nf_bch_mode<<26)|
  278. NAND_DMA_BURST_PARAM(10));
  279. ret=Nand_Dma_Waiting();
  280. if(isread&&eccornot)
  281. ret=Nand_Ecc_Check(buf,len);
  282. return ret;
  283. }
  284. /*
  285. * DMA functions for buffer writing and reading
  286. */
  287. // one case not page size( PIO) , other is page size(use dma)
  288. //with ECC OR NOT
  289. static void am8218_nand_dma_read_buf(struct mtd_info *mtd,uint8_t *buf, int len){
  290. struct am8218_nand_info *info = mtd_to_nand_info(mtd);
  291. struct am8218_nand_platform *plat = info->platform;
  292. uint32_t page_size = plat->page_size; //or mtd->writesize
  293. uint32_t eccornot;
  294. dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len);
  295. if ((len < page_size)) {
  296. am8218_nand_read_buf(mtd, buf, len);
  297. }
  298. else {
  299. //page size or page+oob
  300. if(len==(mtd->writesize+mtd->oobsize))
  301. eccornot=0;
  302. else if (page_size == 512)
  303. eccornot=0;
  304. else
  305. eccornot=1;
  306. am8218_nand_dma_rw(info->aml_nand_dma_buf,len,1,eccornot);
  307. if (info->chip.state != FL_READING)
  308. printk("nand dev state error at nand_dma_read_buf %d\n", info->chip.state);
  309. memcpy(buf, info->aml_nand_dma_buf, len);
  310. }
  311. }
  312. // one case not page size( PIO) , other is page size(use dma)
  313. static void am8218_nand_dma_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len){
  314. struct am8218_nand_info *info = mtd_to_nand_info(mtd);
  315. struct am8218_nand_platform *plat = info->platform;
  316. unsigned short page_size = plat->page_size; //or mtd->writesize
  317. uint32_t eccornot;
  318. dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len);
  319. memcpy(info->aml_nand_dma_buf, buf, len);
  320. if (page_size == 512)
  321. am8218_nand_dma_rw((uint8_t * )info->aml_nand_dma_buf,len,0,0);
  322. else {
  323. if (len < page_size)
  324. am8218_nand_write_buf(mtd,info->aml_nand_dma_buf,len); //with ECC OR NOT FIXME
  325. else{
  326. if(len!=page_size)
  327. BUG();
  328. eccornot=1;
  329. am8218_nand_dma_rw((uint8_t * )info->aml_nand_dma_buf,len,0,eccornot);
  330. }
  331. }
  332. }
  333. //read+ecc correct
  334. static int am8218_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,uint8_t *buf){
  335. am8218_nand_dma_read_buf(mtd,buf,chip->ecc.size); //mtd->writesize
  336. return 0;
  337. }
  338. static void am8218_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, const uint8_t *buf){
  339. am8218_nand_dma_write_buf(mtd,buf,chip->ecc.size); //mtd->writesize
  340. }
  341. //FOr NIKE use SLC NAND, bootldr no ecc , kernel+fs use bch 9
  342. //FIXME this func is used to check a block is bad or not( by check the user buye , but the tranfer is disable bch) , so
  343. // the user byte maybe wrong ; if use __MLC__ NAND , this may happen , need more test
  344. static int am8218_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  345. int page, int sndcmd)
  346. {
  347. uint32_t pagenum=0 ;
  348. uint32_t sectors=mtd->writesize/512;
  349. uint8_t * ptr=NULL;
  350. uint32_t checkbad;
  351. //uint8_t tmp;
  352. uint8_t *oobrbuf;
  353. unsigned nand_page_size = mtd->writesize;
  354. if(0) {
  355. memset(chip->oob_poi,0xff,mtd->oobsize);
  356. }else{
  357. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  358. am8218_nand_dma_rw(chip->buffers->databuf,mtd->writesize+mtd->oobsize,1,0); //read with out ecc
  359. //oobrbuf=chip->buffers->oobrbuf;
  360. if (nand_page_size >= 512) {
  361. oobrbuf=&chip->buffers->databuf[nand_page_size];
  362. for(pagenum=0;pagenum<sectors-1;pagenum++){
  363. ptr=chip->buffers->databuf+(pagenum+1)*528;
  364. ptr=ptr-16;
  365. memcpy(oobrbuf+pagenum*16,ptr,16);
  366. }
  367. if(sectors==1)
  368. checkbad=oobrbuf[0];
  369. else
  370. checkbad=oobrbuf[0]|(oobrbuf[16]<<8)| (oobrbuf[32]<<16)|(oobrbuf[48]<<24);
  371. if(checkbad==USER_BYTES_3_0)
  372. return 0;
  373. if(checkbad==0xffffffff)
  374. return 0; //this for am_block_isbad
  375. if(checkbad!=USER_BYTES_3_0)
  376. return 1; //-1;
  377. }
  378. //tmp=chip->buffers->oobrbuf[1]
  379. //chip->buffers->oobrbuf[1]
  380. }
  381. return 0;
  382. }
  383. #define STATUS_FAILED 0x01
  384. #define STATUS_FAILED2 0x02
  385. //May be write new dat to user byte pos
  386. static int am8218_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  387. int page)
  388. {
  389. uint8_t status[4];
  390. uint8_t *oobrbuf;
  391. unsigned nand_page_size = mtd->writesize;
  392. oobrbuf=chip->oob_poi;
  393. chip->cmdfunc(mtd, NAND_CMD_SEQIN, nand_page_size, page);
  394. am8218_nand_write_buf(mtd, oobrbuf, mtd->oobsize);
  395. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  396. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  397. am8218_nand_read_buf(mtd, status, 4);
  398. if(status[0] & STATUS_FAILED)
  399. return 1;
  400. /*printk(" WRITE OOB , NOT IMPLENMENT\n");
  401. BUG();*/
  402. return 0;
  403. }
  404. static int am8218_nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  405. uint8_t *buf)
  406. {
  407. am8218_nand_read_oob(mtd,chip,-1,0);
  408. memcpy(buf,chip->buffers->databuf,mtd->writesize);
  409. // am8218_nand_dma_read_buf(mtd,buf,(mtd->writesize + mtd->oobsize));
  410. return 0;
  411. }
  412. //got yfs mtd->block_isbad -> nand_block_isbad ->here
  413. //0 for ok 1 for bad
  414. extern int nand_dev_num;
  415. static int am8218_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  416. {
  417. int32_t ret;
  418. struct nand_chip * chip=mtd->priv;
  419. uint32_t page=ofs>>chip->page_shift;
  420. struct am8218_nand_info *info = mtd_to_nand_info(mtd);
  421. struct am8218_nand_platform *plat = info->platform;
  422. unsigned short page_size = plat->page_size;
  423. if(getchip)
  424. nand_get_chip();
  425. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  426. if (page_size == 512) {
  427. ret = am8218_nand_dma_rw(chip->buffers->databuf, mtd->writesize + mtd->oobsize, 1, 0);
  428. if(ret)
  429. return 1;
  430. if( chip->buffers->databuf[mtd->writesize + BADBLOCK_FLAG_BYTE-1] != 0xff ) {
  431. printk(" NAND detect Bad block at %x \n",(uint32_t)ofs);
  432. return 1;
  433. }
  434. memset(chip->buffers->databuf,0,mtd->writesize + mtd->oobsize);
  435. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page+1);
  436. ret = am8218_nand_dma_rw(chip->buffers->databuf, mtd->writesize + mtd->oobsize, 1, 0); //second page
  437. if(ret)
  438. return 1;
  439. if( chip->buffers->databuf[mtd->writesize + BADBLOCK_FLAG_BYTE-1] != 0xff ) {
  440. printk(" NAND detect Bad block at %x \n",(uint32_t)ofs);
  441. return 1;
  442. }
  443. }
  444. else {
  445. ret=am8218_nand_dma_rw(chip->buffers->databuf,mtd->writesize ,1,1);
  446. if(ret<0){
  447. ret=am8218_nand_read_oob(mtd, chip,page, 1);
  448. if(ret!=0)
  449. printk(" NAND detect Bad block at %x \n",(uint32_t)ofs);
  450. }
  451. }
  452. if(getchip) {
  453. nand_release_chip();
  454. }
  455. return ret;
  456. }
  457. // may be call nand_default_block_markbad--->nand_do_write_oob--->chip->ecc.writeoob
  458. static int am8218_nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  459. {
  460. printk(" MARK the BAD BLOCK , NOT IMPLENMENT\n");
  461. BUG();
  462. return 0;
  463. }
  464. static int am8218_nand_hw_init(struct am8218_nand_info *info)
  465. {
  466. struct am8218_nand_platform *plat = info->platform;
  467. dev_info(info->device,"page_size=%d, data_width=%d, dma_dly=%d, twb_dly=%d\n",
  468. plat->page_size,plat->data_width,plat->dma_dly, plat->twb_dly);
  469. nand_config|=(0<<1)|(1<<6)| (1 << 0); //|(1 << 7);
  470. #if defined(ONFIG_AMLOGIC_BOARD_APOLLO) ||\
  471. defined(ONFIG_AMLOGIC_BOARD_APOLLO_H)
  472. nand_config|=(1<<7); // NAND connected to DCU1
  473. #endif
  474. #if defined(CONFIG_AMLOGIC_BOARD_APOLLO) ||\
  475. defined(CONFIG_AMLOGIC_BOARD_APOLLO_H)
  476. nand_config|=1<<11;
  477. nand_config|=2<<12;
  478. #endif
  479. NAND_SET_MANUAL_CONFIG(nand_config);
  480. if((plat->page_size==4096)||(plat->page_size==2048))
  481. nand_config|= (1<<3)|(1<< 2) ; // Large page (2k), 3 ALE
  482. else
  483. nand_config|= (0<<3)|(0<< 2); //small 512 ,2 ALE
  484. if(plat->data_width==16)
  485. nand_config|= (1 << 1); // 16-bit strang
  486. else
  487. nand_config|= (0<<1);
  488. dev_info(info->device, "Nand Config is 0x%04x\n", nand_config);
  489. NAND_SET_MANUAL_CONFIG(nand_config);
  490. return 0;
  491. }
  492. static int am8218_nand_add_partition(struct am8218_nand_info *info){
  493. struct mtd_info *mtd = &info->mtd;
  494. #ifdef CONFIG_MTD_PARTITIONS
  495. struct mtd_partition *parts = info->platform->partitions;
  496. int nr = info->platform->nr_partitions;
  497. return add_mtd_partitions(mtd, parts, nr);
  498. #else
  499. return add_mtd_device(mtd);
  500. #endif
  501. }
  502. static int am8218_nand_remove(struct platform_device *pdev){
  503. struct am8218_nand_info *info = to_nand_info(pdev);
  504. struct mtd_info *mtd = NULL;
  505. platform_set_drvdata(pdev, NULL);
  506. mtd = &info->mtd;
  507. if (mtd) {
  508. nand_release(mtd);
  509. kfree(mtd);
  510. }
  511. kfree(info);
  512. return 0;
  513. }
  514. static int am8218_nand_probe(struct platform_device *pdev){
  515. struct am8218_nand_platform *plat = to_nand_plat(pdev);
  516. struct am8218_nand_info *info = NULL;
  517. struct nand_chip *chip = NULL;
  518. struct mtd_info *mtd = NULL;
  519. unsigned char *aml_nand_dma_buf_temp = NULL;
  520. int err = 0;
  521. unsigned page_shift, maf_idx;
  522. dev_dbg(&pdev->dev, "(%p)\n", pdev);
  523. if (!plat) {
  524. dev_err(&pdev->dev, "no platform specific information\n");
  525. goto exit_error;
  526. }
  527. info = kzalloc(sizeof(*info), GFP_KERNEL);
  528. if (info == NULL) {
  529. dev_err(&pdev->dev, "no memory for flash info\n");
  530. err = -ENOMEM;
  531. goto exit_error;
  532. }
  533. platform_set_drvdata(pdev, info);
  534. spin_lock_init(&info->controller.lock);
  535. init_waitqueue_head(&info->controller.wq);
  536. info->device = &pdev->dev;
  537. info->platform = plat;
  538. chip = &info->chip;
  539. if (plat->data_width==16)
  540. chip->options |= NAND_BUSWIDTH_16;
  541. chip->options |= NAND_SKIP_BBTSCAN;
  542. chip->buffers = kmalloc(sizeof(struct nand_buffers), GFP_KERNEL);
  543. if (info->chip.buffers == NULL) {
  544. dev_err(&pdev->dev, "no memory for flash info\n");
  545. err = -ENOMEM;
  546. goto exit_error;
  547. }
  548. chip->options |= NAND_OWN_BUFFERS;
  549. chip->read_buf = (plat->data_width==16) ?
  550. am8218_nand_read_buf16 : am8218_nand_read_buf;
  551. chip->write_buf = (plat->data_width==16) ?
  552. am8218_nand_write_buf16 : am8218_nand_write_buf;
  553. chip->read_byte = am8218_nand_read_byte; //NOTE MUST PROVIDE , the defualt cause kernel OOP
  554. chip->cmd_ctrl = am8218_nand_hwcontrol;
  555. chip->dev_ready = am8218_nand_devready;
  556. chip->block_bad =am8218_nand_block_bad;
  557. chip->block_markbad =am8218_nand_block_markbad;
  558. chip->priv = &info->mtd;
  559. chip->controller = &info->controller;
  560. #if defined(CONFIG_AMLOGIC_BOARD_APOLLO_H)
  561. chip->IO_ADDR_R = (void __iomem *) (PNAND_MANUAL_DATA_REG+AHBOFFSET);
  562. chip->IO_ADDR_W = (void __iomem *) (PNAND_MANUAL_DATA_REG+AHBOFFSET);
  563. #else//for nike,aopollo
  564. chip->IO_ADDR_R = (void __iomem *) (PNAND_MANUAL_DATA_REG+PERIPHSBASE);
  565. chip->IO_ADDR_W = (void __iomem *) (PNAND_MANUAL_DATA_REG+PERIPHSBASE);
  566. #endif
  567. chip->chip_delay = 400; //FIXME
  568. /* initialise mtd info data struct */
  569. mtd = &info->mtd;
  570. mtd->priv = chip;
  571. mtd->owner = THIS_MODULE;
  572. nand_get_chip();
  573. err = am8218_nand_hw_init(info);
  574. if (err != 0)
  575. goto exit_error;
  576. nand_id_read(flash_id);
  577. if(nf_id_valid(flash_id) < 0){
  578. printk("\nInvalid ID, No NAND device found or not support\n");
  579. return -1;
  580. }
  581. else{
  582. for (maf_idx = 0; nand_flash_ids[maf_idx].id != 0x0; maf_idx++) {
  583. if (nand_flash_ids[maf_idx].id == flash_id[0])
  584. break;
  585. }
  586. }
  587. if((flash_id[1] == 0x35) || (flash_id[1] == 0x45) || (flash_id[1] == 0x55) || (flash_id[1] == 0x75)
  588. || (flash_id[1] == 0x76) || (flash_id[1] == 0x36) || (flash_id[1] == 0x46) || (flash_id[1] == 0x56)) {
  589. page_shift = 9;
  590. plat->page_size = (1 << page_shift);
  591. }
  592. else {
  593. page_shift = PageShiftTable[flash_id[3] & 0x3];
  594. plat->page_size = (1 << page_shift);
  595. }
  596. if (hardware_ecc) {
  597. if(plat->page_size==4096){
  598. chip->ecc.steps=1;
  599. chip->ecc.bytes=128;
  600. chip->ecc.size =4096;
  601. chip->badblock_pattern=&slcpage_memorybased;
  602. }
  603. else if (plat->page_size ==2048) {
  604. chip->ecc.steps=1; //for one read per page
  605. chip->ecc.bytes= 64;
  606. chip->ecc.size =2048;
  607. chip->badblock_pattern=&slcpage_memorybased; //not consider slc small page case, put this here , for mtd mem bbt
  608. }
  609. else if (plat->page_size == 512) {
  610. chip->ecc.steps=1;
  611. chip->ecc.bytes = 16;
  612. chip->ecc.size = 512;
  613. nand_config &= ~(1<<6);
  614. }
  615. NAND_SET_MANUAL_CONFIG(nand_config);
  616. aml_nand_dma_buf_temp = kzalloc(chip->ecc.size+chip->ecc.bytes+64, GFP_KERNEL);
  617. if (aml_nand_dma_buf_temp == NULL) {
  618. dev_err(&pdev->dev, "no memory for flash info\n");
  619. err = -ENOMEM;
  620. goto exit_error;
  621. }
  622. info->aml_nand_dma_buf = ((unsigned)aml_nand_dma_buf_temp+ARC_DCACHE_LINE_LEN-1)&DCACHE_LINE_MASK;
  623. chip->read_buf = am8218_nand_dma_read_buf;
  624. chip->write_buf = am8218_nand_dma_write_buf;
  625. chip->ecc.read_page = am8218_nand_read_page;
  626. chip->ecc.write_page = am8218_nand_write_page;
  627. chip->ecc.read_oob = am8218_nand_read_oob;
  628. chip->ecc.write_oob = am8218_nand_write_oob;
  629. chip->ecc.calculate = am8218_nand_calculate_ecc;
  630. chip->ecc.correct = am8218_nand_correct_data;
  631. if (plat->page_size == 512) {
  632. chip->ecc.mode = NAND_ECC_SOFT;
  633. }
  634. else {
  635. chip->ecc.mode = NAND_ECC_HW;
  636. chip->ecc.hwctl = am8218_nand_enable_hwecc;
  637. }
  638. //chip->ecc.read_page_raw=am8218_nand_read_page_raw;
  639. } else {
  640. chip->ecc.mode = NAND_ECC_SOFT;
  641. }
  642. if (nand_scan(mtd, 1)) {
  643. err = -ENXIO;
  644. goto exit_error;
  645. }
  646. if(am8218_nand_add_partition(info)!=0){
  647. err = -ENXIO;
  648. goto exit_error;
  649. }
  650. dev_dbg(&pdev->dev, "initialised ok\n");
  651. //nand_release_chip();
  652. return 0;
  653. exit_error:
  654. //changed by zhouzhi 09-8-11
  655. //because we have not add the mtd device at the error occur;
  656. //this operate should make the system broke!!
  657. //error occur at no nand device;
  658. // am8218_nand_remove(pdev);
  659. kfree(info);
  660. if (err == 0)
  661. err = -EINVAL;
  662. return err;
  663. }
  664. #define DRV_NAME "am8218-nand"
  665. #define DRV_VERSION "0.1"
  666. #define DRV_AUTHOR "pfs"
  667. #define DRV_DESC "Amlogic 8218 on-chip NAND FLash Controller Driver"
  668. /* driver device registration */
  669. static struct platform_driver am8218_nand_driver = {
  670. .probe = am8218_nand_probe,
  671. .remove = am8218_nand_remove,
  672. // .suspend = am8218_nand_suspend,
  673. // .resume = am8218_nand_resume,
  674. .driver = {
  675. .name = DRV_NAME,
  676. .owner = THIS_MODULE,
  677. },
  678. };
  679. static int __init am8218_nand_init(void)
  680. {
  681. printk(KERN_INFO "%s, Version %s (c) 2008 Amlogic Inc.\n",
  682. DRV_DESC, DRV_VERSION);
  683. return platform_driver_register(&am8218_nand_driver);
  684. }
  685. static void __exit am8218_nand_exit(void)
  686. {
  687. platform_driver_unregister(&am8218_nand_driver);
  688. }
  689. module_init(am8218_nand_init);
  690. module_exit(am8218_nand_exit);
  691. MODULE_LICENSE("GPL");
  692. MODULE_AUTHOR(DRV_AUTHOR);
  693. MODULE_DESCRIPTION(DRV_DESC);
  694. MODULE_ALIAS("platform:" DRV_NAME);