bf5xx_nand.c 20 KB

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