bf5xx_nand.c 19 KB

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