bf5xx_nand.c 21 KB

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