bf5xx_nand.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /* linux/drivers/mtd/nand/bf5xx_nand.c
  2. *
  3. * Copyright 2006-2008 Analog Devices Inc.
  4. * http://blackfin.uclinux.org/
  5. * Bryan Wu <bryan.wu@analog.com>
  6. *
  7. * Blackfin BF5xx on-chip NAND flash controller 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 const 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. /* first 4 bytes ECC code for 256 page size */
  257. ecc0 = bfin_read_NFC_ECC0();
  258. ecc1 = bfin_read_NFC_ECC1();
  259. code[0] = (ecc0 & 0x3FF) | ((ecc1 & 0x3FF) << 11);
  260. dev_dbg(info->device, "returning ecc 0x%08x\n", code[0]);
  261. /* first 3 bytes in ecc_code for 256 page size */
  262. p = (u8 *) code;
  263. memcpy(ecc_code, p, 3);
  264. /* second 4 bytes ECC code for 512 page size */
  265. if (page_size == 512) {
  266. ecc0 = bfin_read_NFC_ECC2();
  267. ecc1 = bfin_read_NFC_ECC3();
  268. code[1] = (ecc0 & 0x3FF) | ((ecc1 & 0x3FF) << 11);
  269. /* second 3 bytes in ecc_code for second 256
  270. * bytes of 512 page size
  271. */
  272. p = (u8 *) (code + 1);
  273. memcpy((ecc_code + 3), p, 3);
  274. dev_dbg(info->device, "returning ecc 0x%08x\n", code[1]);
  275. }
  276. return 0;
  277. }
  278. /*
  279. * PIO mode for buffer writing and reading
  280. */
  281. static void bf5xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  282. {
  283. int i;
  284. unsigned short val;
  285. /*
  286. * Data reads are requested by first writing to NFC_DATA_RD
  287. * and then reading back from NFC_READ.
  288. */
  289. for (i = 0; i < len; i++) {
  290. while (bfin_read_NFC_STAT() & WB_FULL)
  291. cpu_relax();
  292. /* Contents do not matter */
  293. bfin_write_NFC_DATA_RD(0x0000);
  294. SSYNC();
  295. while ((bfin_read_NFC_IRQSTAT() & RD_RDY) != RD_RDY)
  296. cpu_relax();
  297. buf[i] = bfin_read_NFC_READ();
  298. val = bfin_read_NFC_IRQSTAT();
  299. val |= RD_RDY;
  300. bfin_write_NFC_IRQSTAT(val);
  301. SSYNC();
  302. }
  303. }
  304. static uint8_t bf5xx_nand_read_byte(struct mtd_info *mtd)
  305. {
  306. uint8_t val;
  307. bf5xx_nand_read_buf(mtd, &val, 1);
  308. return val;
  309. }
  310. static void bf5xx_nand_write_buf(struct mtd_info *mtd,
  311. const uint8_t *buf, int len)
  312. {
  313. int i;
  314. for (i = 0; i < len; i++) {
  315. while (bfin_read_NFC_STAT() & WB_FULL)
  316. cpu_relax();
  317. bfin_write_NFC_DATA_WR(buf[i]);
  318. SSYNC();
  319. }
  320. }
  321. static void bf5xx_nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  322. {
  323. int i;
  324. u16 *p = (u16 *) buf;
  325. len >>= 1;
  326. /*
  327. * Data reads are requested by first writing to NFC_DATA_RD
  328. * and then reading back from NFC_READ.
  329. */
  330. bfin_write_NFC_DATA_RD(0x5555);
  331. SSYNC();
  332. for (i = 0; i < len; i++)
  333. p[i] = bfin_read_NFC_READ();
  334. }
  335. static void bf5xx_nand_write_buf16(struct mtd_info *mtd,
  336. const uint8_t *buf, int len)
  337. {
  338. int i;
  339. u16 *p = (u16 *) buf;
  340. len >>= 1;
  341. for (i = 0; i < len; i++)
  342. bfin_write_NFC_DATA_WR(p[i]);
  343. SSYNC();
  344. }
  345. /*
  346. * DMA functions for buffer writing and reading
  347. */
  348. static irqreturn_t bf5xx_nand_dma_irq(int irq, void *dev_id)
  349. {
  350. struct bf5xx_nand_info *info = dev_id;
  351. clear_dma_irqstat(CH_NFC);
  352. disable_dma(CH_NFC);
  353. complete(&info->dma_completion);
  354. return IRQ_HANDLED;
  355. }
  356. static int bf5xx_nand_dma_rw(struct mtd_info *mtd,
  357. uint8_t *buf, int is_read)
  358. {
  359. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  360. struct bf5xx_nand_platform *plat = info->platform;
  361. unsigned short page_size = (plat->page_size ? 512 : 256);
  362. unsigned short val;
  363. dev_dbg(info->device, " mtd->%p, buf->%p, is_read %d\n",
  364. mtd, buf, is_read);
  365. /*
  366. * Before starting a dma transfer, be sure to invalidate/flush
  367. * the cache over the address range of your DMA buffer to
  368. * prevent cache coherency problems. Otherwise very subtle bugs
  369. * can be introduced to your driver.
  370. */
  371. if (is_read)
  372. invalidate_dcache_range((unsigned int)buf,
  373. (unsigned int)(buf + page_size));
  374. else
  375. flush_dcache_range((unsigned int)buf,
  376. (unsigned int)(buf + page_size));
  377. /*
  378. * This register must be written before each page is
  379. * transferred to generate the correct ECC register
  380. * values.
  381. */
  382. bfin_write_NFC_RST(0x1);
  383. SSYNC();
  384. disable_dma(CH_NFC);
  385. clear_dma_irqstat(CH_NFC);
  386. /* setup DMA register with Blackfin DMA API */
  387. set_dma_config(CH_NFC, 0x0);
  388. set_dma_start_addr(CH_NFC, (unsigned long) buf);
  389. set_dma_x_count(CH_NFC, (page_size >> 2));
  390. set_dma_x_modify(CH_NFC, 4);
  391. /* setup write or read operation */
  392. val = DI_EN | WDSIZE_32;
  393. if (is_read)
  394. val |= WNR;
  395. set_dma_config(CH_NFC, val);
  396. enable_dma(CH_NFC);
  397. /* Start PAGE read/write operation */
  398. if (is_read)
  399. bfin_write_NFC_PGCTL(0x1);
  400. else
  401. bfin_write_NFC_PGCTL(0x2);
  402. wait_for_completion(&info->dma_completion);
  403. return 0;
  404. }
  405. static void bf5xx_nand_dma_read_buf(struct mtd_info *mtd,
  406. uint8_t *buf, int len)
  407. {
  408. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  409. struct bf5xx_nand_platform *plat = info->platform;
  410. unsigned short page_size = (plat->page_size ? 512 : 256);
  411. dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len);
  412. if (len == page_size)
  413. bf5xx_nand_dma_rw(mtd, buf, 1);
  414. else
  415. bf5xx_nand_read_buf(mtd, buf, len);
  416. }
  417. static void bf5xx_nand_dma_write_buf(struct mtd_info *mtd,
  418. const uint8_t *buf, int len)
  419. {
  420. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  421. struct bf5xx_nand_platform *plat = info->platform;
  422. unsigned short page_size = (plat->page_size ? 512 : 256);
  423. dev_dbg(info->device, "mtd->%p, buf->%p, len %d\n", mtd, buf, len);
  424. if (len == page_size)
  425. bf5xx_nand_dma_rw(mtd, (uint8_t *)buf, 0);
  426. else
  427. bf5xx_nand_write_buf(mtd, buf, len);
  428. }
  429. /*
  430. * System initialization functions
  431. */
  432. static int bf5xx_nand_dma_init(struct bf5xx_nand_info *info)
  433. {
  434. int ret;
  435. unsigned short val;
  436. /* Do not use dma */
  437. if (!hardware_ecc)
  438. return 0;
  439. init_completion(&info->dma_completion);
  440. #ifdef CONFIG_BF54x
  441. /* Setup DMAC1 channel mux for NFC which shared with SDH */
  442. val = bfin_read_DMAC1_PERIMUX();
  443. val &= 0xFFFE;
  444. bfin_write_DMAC1_PERIMUX(val);
  445. SSYNC();
  446. #endif
  447. /* Request NFC DMA channel */
  448. ret = request_dma(CH_NFC, "BF5XX NFC driver");
  449. if (ret < 0) {
  450. dev_err(info->device, " unable to get DMA channel\n");
  451. return ret;
  452. }
  453. set_dma_callback(CH_NFC, (void *) bf5xx_nand_dma_irq, (void *) info);
  454. /* Turn off the DMA channel first */
  455. disable_dma(CH_NFC);
  456. return 0;
  457. }
  458. /*
  459. * BF5XX NFC hardware initialization
  460. * - pin mux setup
  461. * - clear interrupt status
  462. */
  463. static int bf5xx_nand_hw_init(struct bf5xx_nand_info *info)
  464. {
  465. int err = 0;
  466. unsigned short val;
  467. struct bf5xx_nand_platform *plat = info->platform;
  468. /* setup NFC_CTL register */
  469. dev_info(info->device,
  470. "page_size=%d, data_width=%d, wr_dly=%d, rd_dly=%d\n",
  471. (plat->page_size ? 512 : 256),
  472. (plat->data_width ? 16 : 8),
  473. plat->wr_dly, plat->rd_dly);
  474. val = (plat->page_size << NFC_PG_SIZE_OFFSET) |
  475. (plat->data_width << NFC_NWIDTH_OFFSET) |
  476. (plat->rd_dly << NFC_RDDLY_OFFSET) |
  477. (plat->rd_dly << NFC_WRDLY_OFFSET);
  478. dev_dbg(info->device, "NFC_CTL is 0x%04x\n", val);
  479. bfin_write_NFC_CTL(val);
  480. SSYNC();
  481. /* clear interrupt status */
  482. bfin_write_NFC_IRQMASK(0x0);
  483. SSYNC();
  484. val = bfin_read_NFC_IRQSTAT();
  485. bfin_write_NFC_IRQSTAT(val);
  486. SSYNC();
  487. /* DMA initialization */
  488. if (bf5xx_nand_dma_init(info))
  489. err = -ENXIO;
  490. return err;
  491. }
  492. /*
  493. * Device management interface
  494. */
  495. static int bf5xx_nand_add_partition(struct bf5xx_nand_info *info)
  496. {
  497. struct mtd_info *mtd = &info->mtd;
  498. #ifdef CONFIG_MTD_PARTITIONS
  499. struct mtd_partition *parts = info->platform->partitions;
  500. int nr = info->platform->nr_partitions;
  501. return add_mtd_partitions(mtd, parts, nr);
  502. #else
  503. return add_mtd_device(mtd);
  504. #endif
  505. }
  506. static int bf5xx_nand_remove(struct platform_device *pdev)
  507. {
  508. struct bf5xx_nand_info *info = to_nand_info(pdev);
  509. struct mtd_info *mtd = NULL;
  510. platform_set_drvdata(pdev, NULL);
  511. /* first thing we need to do is release all our mtds
  512. * and their partitions, then go through freeing the
  513. * resources used
  514. */
  515. mtd = &info->mtd;
  516. if (mtd) {
  517. nand_release(mtd);
  518. kfree(mtd);
  519. }
  520. peripheral_free_list(bfin_nfc_pin_req);
  521. /* free the common resources */
  522. kfree(info);
  523. return 0;
  524. }
  525. /*
  526. * bf5xx_nand_probe
  527. *
  528. * called by device layer when it finds a device matching
  529. * one our driver can handled. This code checks to see if
  530. * it can allocate all necessary resources then calls the
  531. * nand layer to look for devices
  532. */
  533. static int bf5xx_nand_probe(struct platform_device *pdev)
  534. {
  535. struct bf5xx_nand_platform *plat = to_nand_plat(pdev);
  536. struct bf5xx_nand_info *info = NULL;
  537. struct nand_chip *chip = NULL;
  538. struct mtd_info *mtd = NULL;
  539. int err = 0;
  540. dev_dbg(&pdev->dev, "(%p)\n", pdev);
  541. if (peripheral_request_list(bfin_nfc_pin_req, DRV_NAME)) {
  542. printk(KERN_ERR DRV_NAME
  543. ": Requesting Peripherals failed\n");
  544. return -EFAULT;
  545. }
  546. if (!plat) {
  547. dev_err(&pdev->dev, "no platform specific information\n");
  548. goto exit_error;
  549. }
  550. info = kzalloc(sizeof(*info), GFP_KERNEL);
  551. if (info == NULL) {
  552. dev_err(&pdev->dev, "no memory for flash info\n");
  553. err = -ENOMEM;
  554. goto exit_error;
  555. }
  556. platform_set_drvdata(pdev, info);
  557. spin_lock_init(&info->controller.lock);
  558. init_waitqueue_head(&info->controller.wq);
  559. info->device = &pdev->dev;
  560. info->platform = plat;
  561. /* initialise chip data struct */
  562. chip = &info->chip;
  563. if (plat->data_width)
  564. chip->options |= NAND_BUSWIDTH_16;
  565. chip->options |= NAND_CACHEPRG | NAND_SKIP_BBTSCAN;
  566. chip->read_buf = (plat->data_width) ?
  567. bf5xx_nand_read_buf16 : bf5xx_nand_read_buf;
  568. chip->write_buf = (plat->data_width) ?
  569. bf5xx_nand_write_buf16 : bf5xx_nand_write_buf;
  570. chip->read_byte = bf5xx_nand_read_byte;
  571. chip->cmd_ctrl = bf5xx_nand_hwcontrol;
  572. chip->dev_ready = bf5xx_nand_devready;
  573. chip->priv = &info->mtd;
  574. chip->controller = &info->controller;
  575. chip->IO_ADDR_R = (void __iomem *) NFC_READ;
  576. chip->IO_ADDR_W = (void __iomem *) NFC_DATA_WR;
  577. chip->chip_delay = 0;
  578. /* initialise mtd info data struct */
  579. mtd = &info->mtd;
  580. mtd->priv = chip;
  581. mtd->owner = THIS_MODULE;
  582. /* initialise the hardware */
  583. err = bf5xx_nand_hw_init(info);
  584. if (err != 0)
  585. goto exit_error;
  586. /* setup hardware ECC data struct */
  587. if (hardware_ecc) {
  588. if (plat->page_size == NFC_PG_SIZE_256) {
  589. chip->ecc.bytes = 3;
  590. chip->ecc.size = 256;
  591. } else if (plat->page_size == NFC_PG_SIZE_512) {
  592. chip->ecc.bytes = 6;
  593. chip->ecc.size = 512;
  594. }
  595. chip->read_buf = bf5xx_nand_dma_read_buf;
  596. chip->write_buf = bf5xx_nand_dma_write_buf;
  597. chip->ecc.calculate = bf5xx_nand_calculate_ecc;
  598. chip->ecc.correct = bf5xx_nand_correct_data;
  599. chip->ecc.mode = NAND_ECC_HW;
  600. chip->ecc.hwctl = bf5xx_nand_enable_hwecc;
  601. } else {
  602. chip->ecc.mode = NAND_ECC_SOFT;
  603. }
  604. /* scan hardware nand chip and setup mtd info data struct */
  605. if (nand_scan(mtd, 1)) {
  606. err = -ENXIO;
  607. goto exit_error;
  608. }
  609. /* add NAND partition */
  610. bf5xx_nand_add_partition(info);
  611. dev_dbg(&pdev->dev, "initialised ok\n");
  612. return 0;
  613. exit_error:
  614. bf5xx_nand_remove(pdev);
  615. if (err == 0)
  616. err = -EINVAL;
  617. return err;
  618. }
  619. /* PM Support */
  620. #ifdef CONFIG_PM
  621. static int bf5xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
  622. {
  623. struct bf5xx_nand_info *info = platform_get_drvdata(dev);
  624. return 0;
  625. }
  626. static int bf5xx_nand_resume(struct platform_device *dev)
  627. {
  628. struct bf5xx_nand_info *info = platform_get_drvdata(dev);
  629. return 0;
  630. }
  631. #else
  632. #define bf5xx_nand_suspend NULL
  633. #define bf5xx_nand_resume NULL
  634. #endif
  635. /* driver device registration */
  636. static struct platform_driver bf5xx_nand_driver = {
  637. .probe = bf5xx_nand_probe,
  638. .remove = bf5xx_nand_remove,
  639. .suspend = bf5xx_nand_suspend,
  640. .resume = bf5xx_nand_resume,
  641. .driver = {
  642. .name = DRV_NAME,
  643. .owner = THIS_MODULE,
  644. },
  645. };
  646. static int __init bf5xx_nand_init(void)
  647. {
  648. printk(KERN_INFO "%s, Version %s (c) 2007 Analog Devices, Inc.\n",
  649. DRV_DESC, DRV_VERSION);
  650. return platform_driver_register(&bf5xx_nand_driver);
  651. }
  652. static void __exit bf5xx_nand_exit(void)
  653. {
  654. platform_driver_unregister(&bf5xx_nand_driver);
  655. }
  656. module_init(bf5xx_nand_init);
  657. module_exit(bf5xx_nand_exit);
  658. MODULE_LICENSE("GPL");
  659. MODULE_AUTHOR(DRV_AUTHOR);
  660. MODULE_DESCRIPTION(DRV_DESC);
  661. MODULE_ALIAS("platform:" DRV_NAME);