bf5xx_nand.c 18 KB

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