bf5xx_nand.c 19 KB

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