bf5xx_nand.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /* linux/drivers/mtd/nand/bf5xx_nand.c
  2. *
  3. * Copyright 2006-2007 Analog Devices Inc.
  4. * http://blackfin.uclinux.org/
  5. * Bryan Wu <bryan.wu@analog.com>
  6. *
  7. * Blackfin BF5xx on-chip NAND flash controler driver
  8. *
  9. * Derived from drivers/mtd/nand/s3c2410.c
  10. * Copyright (c) 2007 Ben Dooks <ben@simtec.co.uk>
  11. *
  12. * Derived from drivers/mtd/nand/cafe.c
  13. * Copyright © 2006 Red Hat, Inc.
  14. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  15. *
  16. * Changelog:
  17. * 12-Jun-2007 Bryan Wu: Initial version
  18. * 18-Jul-2007 Bryan Wu:
  19. * - ECC_HW and ECC_SW supported
  20. * - DMA supported in ECC_HW
  21. * - YAFFS tested as rootfs in both ECC_HW and ECC_SW
  22. *
  23. * TODO:
  24. * Enable JFFS2 over NAND as rootfs
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39. */
  40. #include <linux/module.h>
  41. #include <linux/types.h>
  42. #include <linux/init.h>
  43. #include <linux/kernel.h>
  44. #include <linux/string.h>
  45. #include <linux/ioport.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/delay.h>
  48. #include <linux/dma-mapping.h>
  49. #include <linux/err.h>
  50. #include <linux/slab.h>
  51. #include <linux/io.h>
  52. #include <linux/bitops.h>
  53. #include <linux/mtd/mtd.h>
  54. #include <linux/mtd/nand.h>
  55. #include <linux/mtd/nand_ecc.h>
  56. #include <linux/mtd/partitions.h>
  57. #include <asm/blackfin.h>
  58. #include <asm/dma.h>
  59. #include <asm/cacheflush.h>
  60. #include <asm/nand.h>
  61. #include <asm/portmux.h>
  62. #define DRV_NAME "bf5xx-nand"
  63. #define DRV_VERSION "1.2"
  64. #define DRV_AUTHOR "Bryan Wu <bryan.wu@analog.com>"
  65. #define DRV_DESC "BF5xx on-chip NAND FLash Controller Driver"
  66. #ifdef CONFIG_MTD_NAND_BF5XX_HWECC
  67. static int hardware_ecc = 1;
  68. #else
  69. static int hardware_ecc;
  70. #endif
  71. static unsigned short bfin_nfc_pin_req[] = {P_NAND_CE, P_NAND_RB, 0};
  72. /*
  73. * Data structures for bf5xx nand flash controller driver
  74. */
  75. /* bf5xx nand info */
  76. struct bf5xx_nand_info {
  77. /* mtd info */
  78. struct nand_hw_control controller;
  79. struct mtd_info mtd;
  80. struct nand_chip chip;
  81. /* platform info */
  82. struct bf5xx_nand_platform *platform;
  83. /* device info */
  84. struct device *device;
  85. /* DMA stuff */
  86. struct completion dma_completion;
  87. };
  88. /*
  89. * Conversion functions
  90. */
  91. static struct bf5xx_nand_info *mtd_to_nand_info(struct mtd_info *mtd)
  92. {
  93. return container_of(mtd, struct bf5xx_nand_info, mtd);
  94. }
  95. static struct bf5xx_nand_info *to_nand_info(struct platform_device *pdev)
  96. {
  97. return platform_get_drvdata(pdev);
  98. }
  99. static struct bf5xx_nand_platform *to_nand_plat(struct platform_device *pdev)
  100. {
  101. return pdev->dev.platform_data;
  102. }
  103. /*
  104. * struct nand_chip interface function pointers
  105. */
  106. /*
  107. * bf5xx_nand_hwcontrol
  108. *
  109. * Issue command and address cycles to the chip
  110. */
  111. static void bf5xx_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  112. unsigned int ctrl)
  113. {
  114. if (cmd == NAND_CMD_NONE)
  115. return;
  116. while (bfin_read_NFC_STAT() & WB_FULL)
  117. cpu_relax();
  118. if (ctrl & NAND_CLE)
  119. bfin_write_NFC_CMD(cmd);
  120. else
  121. bfin_write_NFC_ADDR(cmd);
  122. SSYNC();
  123. }
  124. /*
  125. * bf5xx_nand_devready()
  126. *
  127. * returns 0 if the nand is busy, 1 if it is ready
  128. */
  129. static int bf5xx_nand_devready(struct mtd_info *mtd)
  130. {
  131. unsigned short val = bfin_read_NFC_IRQSTAT();
  132. if ((val & NBUSYIRQ) == NBUSYIRQ)
  133. return 1;
  134. else
  135. return 0;
  136. }
  137. /*
  138. * ECC functions
  139. * These allow the bf5xx to use the controller's ECC
  140. * generator block to ECC the data as it passes through
  141. */
  142. /*
  143. * ECC error correction function
  144. */
  145. static int bf5xx_nand_correct_data_256(struct mtd_info *mtd, u_char *dat,
  146. u_char *read_ecc, u_char *calc_ecc)
  147. {
  148. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  149. u32 syndrome[5];
  150. u32 calced, stored;
  151. int i;
  152. unsigned short failing_bit, failing_byte;
  153. u_char data;
  154. calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
  155. stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
  156. syndrome[0] = (calced ^ stored);
  157. /*
  158. * syndrome 0: all zero
  159. * No error in data
  160. * No action
  161. */
  162. if (!syndrome[0] || !calced || !stored)
  163. return 0;
  164. /*
  165. * sysdrome 0: only one bit is one
  166. * ECC data was incorrect
  167. * No action
  168. */
  169. if (hweight32(syndrome[0]) == 1) {
  170. dev_err(info->device, "ECC data was incorrect!\n");
  171. return 1;
  172. }
  173. syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
  174. syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
  175. syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
  176. syndrome[4] = syndrome[2] ^ syndrome[3];
  177. for (i = 0; i < 5; i++)
  178. dev_info(info->device, "syndrome[%d] 0x%08x\n", i, syndrome[i]);
  179. dev_info(info->device,
  180. "calced[0x%08x], stored[0x%08x]\n",
  181. calced, stored);
  182. /*
  183. * sysdrome 0: exactly 11 bits are one, each parity
  184. * and parity' pair is 1 & 0 or 0 & 1.
  185. * 1-bit correctable error
  186. * Correct the error
  187. */
  188. if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
  189. dev_info(info->device,
  190. "1-bit correctable error, correct it.\n");
  191. dev_info(info->device,
  192. "syndrome[1] 0x%08x\n", syndrome[1]);
  193. failing_bit = syndrome[1] & 0x7;
  194. failing_byte = syndrome[1] >> 0x3;
  195. data = *(dat + failing_byte);
  196. data = data ^ (0x1 << failing_bit);
  197. *(dat + failing_byte) = data;
  198. return 0;
  199. }
  200. /*
  201. * sysdrome 0: random data
  202. * More than 1-bit error, non-correctable error
  203. * Discard data, mark bad block
  204. */
  205. dev_err(info->device,
  206. "More than 1-bit error, non-correctable error.\n");
  207. dev_err(info->device,
  208. "Please discard data, mark bad block\n");
  209. return 1;
  210. }
  211. static int bf5xx_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  212. u_char *read_ecc, u_char *calc_ecc)
  213. {
  214. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  215. struct bf5xx_nand_platform *plat = info->platform;
  216. unsigned short page_size = (plat->page_size ? 512 : 256);
  217. int ret;
  218. ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
  219. /* If page size is 512, correct second 256 bytes */
  220. if (page_size == 512) {
  221. dat += 256;
  222. read_ecc += 8;
  223. calc_ecc += 8;
  224. ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
  225. }
  226. return ret;
  227. }
  228. static void bf5xx_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  229. {
  230. return;
  231. }
  232. static int bf5xx_nand_calculate_ecc(struct mtd_info *mtd,
  233. const u_char *dat, u_char *ecc_code)
  234. {
  235. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  236. struct bf5xx_nand_platform *plat = info->platform;
  237. u16 page_size = (plat->page_size ? 512 : 256);
  238. u16 ecc0, ecc1;
  239. u32 code[2];
  240. u8 *p;
  241. int bytes = 3, i;
  242. /* first 4 bytes ECC code for 256 page size */
  243. ecc0 = bfin_read_NFC_ECC0();
  244. ecc1 = bfin_read_NFC_ECC1();
  245. code[0] = (ecc0 & 0x3FF) | ((ecc1 & 0x3FF) << 11);
  246. dev_dbg(info->device, "returning ecc 0x%08x\n", code[0]);
  247. /* second 4 bytes ECC code for 512 page size */
  248. if (page_size == 512) {
  249. ecc0 = bfin_read_NFC_ECC2();
  250. ecc1 = bfin_read_NFC_ECC3();
  251. code[1] = (ecc0 & 0x3FF) | ((ecc1 & 0x3FF) << 11);
  252. bytes = 6;
  253. dev_dbg(info->device, "returning ecc 0x%08x\n", code[1]);
  254. }
  255. p = (u8 *)code;
  256. for (i = 0; i < bytes; i++)
  257. ecc_code[i] = p[i];
  258. return 0;
  259. }
  260. /*
  261. * PIO mode for buffer writing and reading
  262. */
  263. static void bf5xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  264. {
  265. int i;
  266. unsigned short val;
  267. /*
  268. * Data reads are requested by first writing to NFC_DATA_RD
  269. * and then reading back from NFC_READ.
  270. */
  271. for (i = 0; i < len; i++) {
  272. while (bfin_read_NFC_STAT() & WB_FULL)
  273. cpu_relax();
  274. /* Contents do not matter */
  275. bfin_write_NFC_DATA_RD(0x0000);
  276. SSYNC();
  277. while ((bfin_read_NFC_IRQSTAT() & RD_RDY) != RD_RDY)
  278. cpu_relax();
  279. buf[i] = bfin_read_NFC_READ();
  280. val = bfin_read_NFC_IRQSTAT();
  281. val |= RD_RDY;
  282. bfin_write_NFC_IRQSTAT(val);
  283. SSYNC();
  284. }
  285. }
  286. static uint8_t bf5xx_nand_read_byte(struct mtd_info *mtd)
  287. {
  288. uint8_t val;
  289. bf5xx_nand_read_buf(mtd, &val, 1);
  290. return val;
  291. }
  292. static void bf5xx_nand_write_buf(struct mtd_info *mtd,
  293. const uint8_t *buf, int len)
  294. {
  295. int i;
  296. for (i = 0; i < len; i++) {
  297. while (bfin_read_NFC_STAT() & WB_FULL)
  298. cpu_relax();
  299. bfin_write_NFC_DATA_WR(buf[i]);
  300. SSYNC();
  301. }
  302. }
  303. static void bf5xx_nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  304. {
  305. int i;
  306. u16 *p = (u16 *) buf;
  307. len >>= 1;
  308. /*
  309. * Data reads are requested by first writing to NFC_DATA_RD
  310. * and then reading back from NFC_READ.
  311. */
  312. bfin_write_NFC_DATA_RD(0x5555);
  313. SSYNC();
  314. for (i = 0; i < len; i++)
  315. p[i] = bfin_read_NFC_READ();
  316. }
  317. static void bf5xx_nand_write_buf16(struct mtd_info *mtd,
  318. const uint8_t *buf, int len)
  319. {
  320. int i;
  321. u16 *p = (u16 *) buf;
  322. len >>= 1;
  323. for (i = 0; i < len; i++)
  324. bfin_write_NFC_DATA_WR(p[i]);
  325. SSYNC();
  326. }
  327. /*
  328. * DMA functions for buffer writing and reading
  329. */
  330. static irqreturn_t bf5xx_nand_dma_irq(int irq, void *dev_id)
  331. {
  332. struct bf5xx_nand_info *info = dev_id;
  333. clear_dma_irqstat(CH_NFC);
  334. disable_dma(CH_NFC);
  335. complete(&info->dma_completion);
  336. return IRQ_HANDLED;
  337. }
  338. static int bf5xx_nand_dma_rw(struct mtd_info *mtd,
  339. uint8_t *buf, int is_read)
  340. {
  341. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  342. struct bf5xx_nand_platform *plat = info->platform;
  343. unsigned short page_size = (plat->page_size ? 512 : 256);
  344. unsigned short val;
  345. dev_dbg(info->device, " mtd->%p, buf->%p, is_read %d\n",
  346. mtd, buf, is_read);
  347. /*
  348. * Before starting a dma transfer, be sure to invalidate/flush
  349. * the cache over the address range of your DMA buffer to
  350. * prevent cache coherency problems. Otherwise very subtle bugs
  351. * can be introduced to your driver.
  352. */
  353. if (is_read)
  354. invalidate_dcache_range((unsigned int)buf,
  355. (unsigned int)(buf + page_size));
  356. else
  357. flush_dcache_range((unsigned int)buf,
  358. (unsigned int)(buf + page_size));
  359. /*
  360. * This register must be written before each page is
  361. * transferred to generate the correct ECC register
  362. * values.
  363. */
  364. bfin_write_NFC_RST(0x1);
  365. SSYNC();
  366. disable_dma(CH_NFC);
  367. clear_dma_irqstat(CH_NFC);
  368. /* setup DMA register with Blackfin DMA API */
  369. set_dma_config(CH_NFC, 0x0);
  370. set_dma_start_addr(CH_NFC, (unsigned long) buf);
  371. set_dma_x_count(CH_NFC, (page_size >> 2));
  372. set_dma_x_modify(CH_NFC, 4);
  373. /* setup write or read operation */
  374. val = DI_EN | WDSIZE_32;
  375. if (is_read)
  376. val |= WNR;
  377. set_dma_config(CH_NFC, val);
  378. enable_dma(CH_NFC);
  379. /* Start PAGE read/write operation */
  380. if (is_read)
  381. bfin_write_NFC_PGCTL(0x1);
  382. else
  383. bfin_write_NFC_PGCTL(0x2);
  384. wait_for_completion(&info->dma_completion);
  385. return 0;
  386. }
  387. static void bf5xx_nand_dma_read_buf(struct mtd_info *mtd,
  388. uint8_t *buf, int len)
  389. {
  390. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  391. struct bf5xx_nand_platform *plat = info->platform;
  392. unsigned short page_size = (plat->page_size ? 512 : 256);
  393. dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len);
  394. if (len == page_size)
  395. bf5xx_nand_dma_rw(mtd, buf, 1);
  396. else
  397. bf5xx_nand_read_buf(mtd, buf, len);
  398. }
  399. static void bf5xx_nand_dma_write_buf(struct mtd_info *mtd,
  400. const uint8_t *buf, int len)
  401. {
  402. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  403. struct bf5xx_nand_platform *plat = info->platform;
  404. unsigned short page_size = (plat->page_size ? 512 : 256);
  405. dev_dbg(info->device, "mtd->%p, buf->%p, len %d\n", mtd, buf, len);
  406. if (len == page_size)
  407. bf5xx_nand_dma_rw(mtd, (uint8_t *)buf, 0);
  408. else
  409. bf5xx_nand_write_buf(mtd, buf, len);
  410. }
  411. /*
  412. * System initialization functions
  413. */
  414. static int bf5xx_nand_dma_init(struct bf5xx_nand_info *info)
  415. {
  416. int ret;
  417. unsigned short val;
  418. /* Do not use dma */
  419. if (!hardware_ecc)
  420. return 0;
  421. init_completion(&info->dma_completion);
  422. /* Setup DMAC1 channel mux for NFC which shared with SDH */
  423. val = bfin_read_DMAC1_PERIMUX();
  424. val &= 0xFFFE;
  425. bfin_write_DMAC1_PERIMUX(val);
  426. SSYNC();
  427. /* Request NFC DMA channel */
  428. ret = request_dma(CH_NFC, "BF5XX NFC driver");
  429. if (ret < 0) {
  430. dev_err(info->device, " unable to get DMA channel\n");
  431. return ret;
  432. }
  433. set_dma_callback(CH_NFC, (void *) bf5xx_nand_dma_irq, (void *) info);
  434. /* Turn off the DMA channel first */
  435. disable_dma(CH_NFC);
  436. return 0;
  437. }
  438. /*
  439. * BF5XX NFC hardware initialization
  440. * - pin mux setup
  441. * - clear interrupt status
  442. */
  443. static int bf5xx_nand_hw_init(struct bf5xx_nand_info *info)
  444. {
  445. int err = 0;
  446. unsigned short val;
  447. struct bf5xx_nand_platform *plat = info->platform;
  448. /* setup NFC_CTL register */
  449. dev_info(info->device,
  450. "page_size=%d, data_width=%d, wr_dly=%d, rd_dly=%d\n",
  451. (plat->page_size ? 512 : 256),
  452. (plat->data_width ? 16 : 8),
  453. plat->wr_dly, plat->rd_dly);
  454. val = (plat->page_size << NFC_PG_SIZE_OFFSET) |
  455. (plat->data_width << NFC_NWIDTH_OFFSET) |
  456. (plat->rd_dly << NFC_RDDLY_OFFSET) |
  457. (plat->rd_dly << NFC_WRDLY_OFFSET);
  458. dev_dbg(info->device, "NFC_CTL is 0x%04x\n", val);
  459. bfin_write_NFC_CTL(val);
  460. SSYNC();
  461. /* clear interrupt status */
  462. bfin_write_NFC_IRQMASK(0x0);
  463. SSYNC();
  464. val = bfin_read_NFC_IRQSTAT();
  465. bfin_write_NFC_IRQSTAT(val);
  466. SSYNC();
  467. if (peripheral_request_list(bfin_nfc_pin_req, DRV_NAME)) {
  468. printk(KERN_ERR DRV_NAME
  469. ": Requesting Peripherals failed\n");
  470. return -EFAULT;
  471. }
  472. /* DMA initialization */
  473. if (bf5xx_nand_dma_init(info))
  474. err = -ENXIO;
  475. return err;
  476. }
  477. /*
  478. * Device management interface
  479. */
  480. static int bf5xx_nand_add_partition(struct bf5xx_nand_info *info)
  481. {
  482. struct mtd_info *mtd = &info->mtd;
  483. #ifdef CONFIG_MTD_PARTITIONS
  484. struct mtd_partition *parts = info->platform->partitions;
  485. int nr = info->platform->nr_partitions;
  486. return add_mtd_partitions(mtd, parts, nr);
  487. #else
  488. return add_mtd_device(mtd);
  489. #endif
  490. }
  491. static int bf5xx_nand_remove(struct platform_device *pdev)
  492. {
  493. struct bf5xx_nand_info *info = to_nand_info(pdev);
  494. struct mtd_info *mtd = NULL;
  495. platform_set_drvdata(pdev, NULL);
  496. /* first thing we need to do is release all our mtds
  497. * and their partitions, then go through freeing the
  498. * resources used
  499. */
  500. mtd = &info->mtd;
  501. if (mtd) {
  502. nand_release(mtd);
  503. kfree(mtd);
  504. }
  505. peripheral_free_list(bfin_nfc_pin_req);
  506. /* free the common resources */
  507. kfree(info);
  508. return 0;
  509. }
  510. /*
  511. * bf5xx_nand_probe
  512. *
  513. * called by device layer when it finds a device matching
  514. * one our driver can handled. This code checks to see if
  515. * it can allocate all necessary resources then calls the
  516. * nand layer to look for devices
  517. */
  518. static int bf5xx_nand_probe(struct platform_device *pdev)
  519. {
  520. struct bf5xx_nand_platform *plat = to_nand_plat(pdev);
  521. struct bf5xx_nand_info *info = NULL;
  522. struct nand_chip *chip = NULL;
  523. struct mtd_info *mtd = NULL;
  524. int err = 0;
  525. dev_dbg(&pdev->dev, "(%p)\n", pdev);
  526. if (!plat) {
  527. dev_err(&pdev->dev, "no platform specific information\n");
  528. goto exit_error;
  529. }
  530. info = kzalloc(sizeof(*info), GFP_KERNEL);
  531. if (info == NULL) {
  532. dev_err(&pdev->dev, "no memory for flash info\n");
  533. err = -ENOMEM;
  534. goto exit_error;
  535. }
  536. platform_set_drvdata(pdev, info);
  537. spin_lock_init(&info->controller.lock);
  538. init_waitqueue_head(&info->controller.wq);
  539. info->device = &pdev->dev;
  540. info->platform = plat;
  541. /* initialise chip data struct */
  542. chip = &info->chip;
  543. if (plat->data_width)
  544. chip->options |= NAND_BUSWIDTH_16;
  545. chip->options |= NAND_CACHEPRG | NAND_SKIP_BBTSCAN;
  546. chip->read_buf = (plat->data_width) ?
  547. bf5xx_nand_read_buf16 : bf5xx_nand_read_buf;
  548. chip->write_buf = (plat->data_width) ?
  549. bf5xx_nand_write_buf16 : bf5xx_nand_write_buf;
  550. chip->read_byte = bf5xx_nand_read_byte;
  551. chip->cmd_ctrl = bf5xx_nand_hwcontrol;
  552. chip->dev_ready = bf5xx_nand_devready;
  553. chip->priv = &info->mtd;
  554. chip->controller = &info->controller;
  555. chip->IO_ADDR_R = (void __iomem *) NFC_READ;
  556. chip->IO_ADDR_W = (void __iomem *) NFC_DATA_WR;
  557. chip->chip_delay = 0;
  558. /* initialise mtd info data struct */
  559. mtd = &info->mtd;
  560. mtd->priv = chip;
  561. mtd->owner = THIS_MODULE;
  562. /* initialise the hardware */
  563. err = bf5xx_nand_hw_init(info);
  564. if (err != 0)
  565. goto exit_error;
  566. /* setup hardware ECC data struct */
  567. if (hardware_ecc) {
  568. if (plat->page_size == NFC_PG_SIZE_256) {
  569. chip->ecc.bytes = 3;
  570. chip->ecc.size = 256;
  571. } else if (plat->page_size == NFC_PG_SIZE_512) {
  572. chip->ecc.bytes = 6;
  573. chip->ecc.size = 512;
  574. }
  575. chip->read_buf = bf5xx_nand_dma_read_buf;
  576. chip->write_buf = bf5xx_nand_dma_write_buf;
  577. chip->ecc.calculate = bf5xx_nand_calculate_ecc;
  578. chip->ecc.correct = bf5xx_nand_correct_data;
  579. chip->ecc.mode = NAND_ECC_HW;
  580. chip->ecc.hwctl = bf5xx_nand_enable_hwecc;
  581. } else {
  582. chip->ecc.mode = NAND_ECC_SOFT;
  583. }
  584. /* scan hardware nand chip and setup mtd info data struct */
  585. if (nand_scan(mtd, 1)) {
  586. err = -ENXIO;
  587. goto exit_error;
  588. }
  589. /* add NAND partition */
  590. bf5xx_nand_add_partition(info);
  591. dev_dbg(&pdev->dev, "initialised ok\n");
  592. return 0;
  593. exit_error:
  594. bf5xx_nand_remove(pdev);
  595. if (err == 0)
  596. err = -EINVAL;
  597. return err;
  598. }
  599. /* PM Support */
  600. #ifdef CONFIG_PM
  601. static int bf5xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
  602. {
  603. struct bf5xx_nand_info *info = platform_get_drvdata(dev);
  604. return 0;
  605. }
  606. static int bf5xx_nand_resume(struct platform_device *dev)
  607. {
  608. struct bf5xx_nand_info *info = platform_get_drvdata(dev);
  609. if (info)
  610. bf5xx_nand_hw_init(info);
  611. return 0;
  612. }
  613. #else
  614. #define bf5xx_nand_suspend NULL
  615. #define bf5xx_nand_resume NULL
  616. #endif
  617. /* driver device registration */
  618. static struct platform_driver bf5xx_nand_driver = {
  619. .probe = bf5xx_nand_probe,
  620. .remove = bf5xx_nand_remove,
  621. .suspend = bf5xx_nand_suspend,
  622. .resume = bf5xx_nand_resume,
  623. .driver = {
  624. .name = DRV_NAME,
  625. .owner = THIS_MODULE,
  626. },
  627. };
  628. static int __init bf5xx_nand_init(void)
  629. {
  630. printk(KERN_INFO "%s, Version %s (c) 2007 Analog Devices, Inc.\n",
  631. DRV_DESC, DRV_VERSION);
  632. return platform_driver_register(&bf5xx_nand_driver);
  633. }
  634. static void __exit bf5xx_nand_exit(void)
  635. {
  636. platform_driver_unregister(&bf5xx_nand_driver);
  637. }
  638. module_init(bf5xx_nand_init);
  639. module_exit(bf5xx_nand_exit);
  640. MODULE_LICENSE("GPL");
  641. MODULE_AUTHOR(DRV_AUTHOR);
  642. MODULE_DESCRIPTION(DRV_DESC);