bf5xx_nand.c 20 KB

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