bf5xx_nand.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 nand_chip chip;
  133. /* platform info */
  134. struct bf5xx_nand_platform *platform;
  135. /* device info */
  136. struct device *device;
  137. /* DMA stuff */
  138. struct completion dma_completion;
  139. };
  140. /*
  141. * Conversion functions
  142. */
  143. static struct bf5xx_nand_info *mtd_to_nand_info(struct mtd_info *mtd)
  144. {
  145. return container_of(mtd_to_nand(mtd), struct bf5xx_nand_info,
  146. chip);
  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 -EBADMSG;
  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 1;
  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 -EBADMSG;
  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_to_nand(mtd);
  268. int ret, bitflips = 0;
  269. ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
  270. if (ret < 0)
  271. return ret;
  272. bitflips = ret;
  273. /* If ecc size is 512, correct second 256 bytes */
  274. if (chip->ecc.size == 512) {
  275. dat += 256;
  276. read_ecc += 3;
  277. calc_ecc += 3;
  278. ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
  279. if (ret < 0)
  280. return ret;
  281. bitflips += ret;
  282. }
  283. return bitflips;
  284. }
  285. static void bf5xx_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  286. {
  287. return;
  288. }
  289. static int bf5xx_nand_calculate_ecc(struct mtd_info *mtd,
  290. const u_char *dat, u_char *ecc_code)
  291. {
  292. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  293. struct nand_chip *chip = mtd_to_nand(mtd);
  294. u16 ecc0, ecc1;
  295. u32 code[2];
  296. u8 *p;
  297. /* first 3 bytes ECC code for 256 page size */
  298. ecc0 = bfin_read_NFC_ECC0();
  299. ecc1 = bfin_read_NFC_ECC1();
  300. code[0] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
  301. dev_dbg(info->device, "returning ecc 0x%08x\n", code[0]);
  302. p = (u8 *) code;
  303. memcpy(ecc_code, p, 3);
  304. /* second 3 bytes ECC code for 512 ecc size */
  305. if (chip->ecc.size == 512) {
  306. ecc0 = bfin_read_NFC_ECC2();
  307. ecc1 = bfin_read_NFC_ECC3();
  308. code[1] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
  309. /* second 3 bytes in ecc_code for second 256
  310. * bytes of 512 page size
  311. */
  312. p = (u8 *) (code + 1);
  313. memcpy((ecc_code + 3), p, 3);
  314. dev_dbg(info->device, "returning ecc 0x%08x\n", code[1]);
  315. }
  316. return 0;
  317. }
  318. /*
  319. * PIO mode for buffer writing and reading
  320. */
  321. static void bf5xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  322. {
  323. int i;
  324. unsigned short val;
  325. /*
  326. * Data reads are requested by first writing to NFC_DATA_RD
  327. * and then reading back from NFC_READ.
  328. */
  329. for (i = 0; i < len; i++) {
  330. while (bfin_read_NFC_STAT() & WB_FULL)
  331. cpu_relax();
  332. /* Contents do not matter */
  333. bfin_write_NFC_DATA_RD(0x0000);
  334. SSYNC();
  335. while ((bfin_read_NFC_IRQSTAT() & RD_RDY) != RD_RDY)
  336. cpu_relax();
  337. buf[i] = bfin_read_NFC_READ();
  338. val = bfin_read_NFC_IRQSTAT();
  339. val |= RD_RDY;
  340. bfin_write_NFC_IRQSTAT(val);
  341. SSYNC();
  342. }
  343. }
  344. static uint8_t bf5xx_nand_read_byte(struct mtd_info *mtd)
  345. {
  346. uint8_t val;
  347. bf5xx_nand_read_buf(mtd, &val, 1);
  348. return val;
  349. }
  350. static void bf5xx_nand_write_buf(struct mtd_info *mtd,
  351. const uint8_t *buf, int len)
  352. {
  353. int i;
  354. for (i = 0; i < len; i++) {
  355. while (bfin_read_NFC_STAT() & WB_FULL)
  356. cpu_relax();
  357. bfin_write_NFC_DATA_WR(buf[i]);
  358. SSYNC();
  359. }
  360. }
  361. static void bf5xx_nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  362. {
  363. int i;
  364. u16 *p = (u16 *) buf;
  365. len >>= 1;
  366. /*
  367. * Data reads are requested by first writing to NFC_DATA_RD
  368. * and then reading back from NFC_READ.
  369. */
  370. bfin_write_NFC_DATA_RD(0x5555);
  371. SSYNC();
  372. for (i = 0; i < len; i++)
  373. p[i] = bfin_read_NFC_READ();
  374. }
  375. static void bf5xx_nand_write_buf16(struct mtd_info *mtd,
  376. const uint8_t *buf, int len)
  377. {
  378. int i;
  379. u16 *p = (u16 *) buf;
  380. len >>= 1;
  381. for (i = 0; i < len; i++)
  382. bfin_write_NFC_DATA_WR(p[i]);
  383. SSYNC();
  384. }
  385. /*
  386. * DMA functions for buffer writing and reading
  387. */
  388. static irqreturn_t bf5xx_nand_dma_irq(int irq, void *dev_id)
  389. {
  390. struct bf5xx_nand_info *info = dev_id;
  391. clear_dma_irqstat(CH_NFC);
  392. disable_dma(CH_NFC);
  393. complete(&info->dma_completion);
  394. return IRQ_HANDLED;
  395. }
  396. static void bf5xx_nand_dma_rw(struct mtd_info *mtd,
  397. uint8_t *buf, int is_read)
  398. {
  399. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  400. struct nand_chip *chip = mtd_to_nand(mtd);
  401. unsigned short val;
  402. dev_dbg(info->device, " mtd->%p, buf->%p, is_read %d\n",
  403. mtd, buf, is_read);
  404. /*
  405. * Before starting a dma transfer, be sure to invalidate/flush
  406. * the cache over the address range of your DMA buffer to
  407. * prevent cache coherency problems. Otherwise very subtle bugs
  408. * can be introduced to your driver.
  409. */
  410. if (is_read)
  411. invalidate_dcache_range((unsigned int)buf,
  412. (unsigned int)(buf + chip->ecc.size));
  413. else
  414. flush_dcache_range((unsigned int)buf,
  415. (unsigned int)(buf + chip->ecc.size));
  416. /*
  417. * This register must be written before each page is
  418. * transferred to generate the correct ECC register
  419. * values.
  420. */
  421. bfin_write_NFC_RST(ECC_RST);
  422. SSYNC();
  423. while (bfin_read_NFC_RST() & ECC_RST)
  424. cpu_relax();
  425. disable_dma(CH_NFC);
  426. clear_dma_irqstat(CH_NFC);
  427. /* setup DMA register with Blackfin DMA API */
  428. set_dma_config(CH_NFC, 0x0);
  429. set_dma_start_addr(CH_NFC, (unsigned long) buf);
  430. /* The DMAs have different size on BF52x and BF54x */
  431. #ifdef CONFIG_BF52x
  432. set_dma_x_count(CH_NFC, (chip->ecc.size >> 1));
  433. set_dma_x_modify(CH_NFC, 2);
  434. val = DI_EN | WDSIZE_16;
  435. #endif
  436. #ifdef CONFIG_BF54x
  437. set_dma_x_count(CH_NFC, (chip->ecc.size >> 2));
  438. set_dma_x_modify(CH_NFC, 4);
  439. val = DI_EN | WDSIZE_32;
  440. #endif
  441. /* setup write or read operation */
  442. if (is_read)
  443. val |= WNR;
  444. set_dma_config(CH_NFC, val);
  445. enable_dma(CH_NFC);
  446. /* Start PAGE read/write operation */
  447. if (is_read)
  448. bfin_write_NFC_PGCTL(PG_RD_START);
  449. else
  450. bfin_write_NFC_PGCTL(PG_WR_START);
  451. wait_for_completion(&info->dma_completion);
  452. }
  453. static void bf5xx_nand_dma_read_buf(struct mtd_info *mtd,
  454. uint8_t *buf, int len)
  455. {
  456. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  457. struct nand_chip *chip = mtd_to_nand(mtd);
  458. dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len);
  459. if (len == chip->ecc.size)
  460. bf5xx_nand_dma_rw(mtd, buf, 1);
  461. else
  462. bf5xx_nand_read_buf(mtd, buf, len);
  463. }
  464. static void bf5xx_nand_dma_write_buf(struct mtd_info *mtd,
  465. const uint8_t *buf, int len)
  466. {
  467. struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
  468. struct nand_chip *chip = mtd_to_nand(mtd);
  469. dev_dbg(info->device, "mtd->%p, buf->%p, len %d\n", mtd, buf, len);
  470. if (len == chip->ecc.size)
  471. bf5xx_nand_dma_rw(mtd, (uint8_t *)buf, 0);
  472. else
  473. bf5xx_nand_write_buf(mtd, buf, len);
  474. }
  475. static int bf5xx_nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  476. uint8_t *buf, int oob_required, int page)
  477. {
  478. bf5xx_nand_read_buf(mtd, buf, mtd->writesize);
  479. bf5xx_nand_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  480. return 0;
  481. }
  482. static int bf5xx_nand_write_page_raw(struct mtd_info *mtd,
  483. struct nand_chip *chip, const uint8_t *buf, int oob_required,
  484. int page)
  485. {
  486. bf5xx_nand_write_buf(mtd, buf, mtd->writesize);
  487. bf5xx_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  488. return 0;
  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 bf5xx_nand_add_partition(struct bf5xx_nand_info *info)
  559. {
  560. struct mtd_info *mtd = nand_to_mtd(&info->chip);
  561. struct mtd_partition *parts = info->platform->partitions;
  562. int nr = info->platform->nr_partitions;
  563. return mtd_device_register(mtd, parts, nr);
  564. }
  565. static int bf5xx_nand_remove(struct platform_device *pdev)
  566. {
  567. struct bf5xx_nand_info *info = to_nand_info(pdev);
  568. /* first thing we need to do is release all our mtds
  569. * and their partitions, then go through freeing the
  570. * resources used
  571. */
  572. nand_release(nand_to_mtd(&info->chip));
  573. peripheral_free_list(bfin_nfc_pin_req);
  574. bf5xx_nand_dma_remove(info);
  575. return 0;
  576. }
  577. static int bf5xx_nand_scan(struct mtd_info *mtd)
  578. {
  579. struct nand_chip *chip = mtd_to_nand(mtd);
  580. int ret;
  581. ret = nand_scan_ident(mtd, 1, NULL);
  582. if (ret)
  583. return ret;
  584. if (hardware_ecc) {
  585. /*
  586. * for nand with page size > 512B, think it as several sections with 512B
  587. */
  588. if (likely(mtd->writesize >= 512)) {
  589. chip->ecc.size = 512;
  590. chip->ecc.bytes = 6;
  591. chip->ecc.strength = 2;
  592. } else {
  593. chip->ecc.size = 256;
  594. chip->ecc.bytes = 3;
  595. chip->ecc.strength = 1;
  596. bfin_write_NFC_CTL(bfin_read_NFC_CTL() & ~(1 << NFC_PG_SIZE_OFFSET));
  597. SSYNC();
  598. }
  599. }
  600. return nand_scan_tail(mtd);
  601. }
  602. /*
  603. * bf5xx_nand_probe
  604. *
  605. * called by device layer when it finds a device matching
  606. * one our driver can handled. This code checks to see if
  607. * it can allocate all necessary resources then calls the
  608. * nand layer to look for devices
  609. */
  610. static int bf5xx_nand_probe(struct platform_device *pdev)
  611. {
  612. struct bf5xx_nand_platform *plat = to_nand_plat(pdev);
  613. struct bf5xx_nand_info *info = NULL;
  614. struct nand_chip *chip = NULL;
  615. struct mtd_info *mtd = NULL;
  616. int err = 0;
  617. dev_dbg(&pdev->dev, "(%p)\n", pdev);
  618. if (!plat) {
  619. dev_err(&pdev->dev, "no platform specific information\n");
  620. return -EINVAL;
  621. }
  622. if (peripheral_request_list(bfin_nfc_pin_req, DRV_NAME)) {
  623. dev_err(&pdev->dev, "requesting Peripherals failed\n");
  624. return -EFAULT;
  625. }
  626. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  627. if (info == NULL) {
  628. err = -ENOMEM;
  629. goto out_err;
  630. }
  631. platform_set_drvdata(pdev, info);
  632. spin_lock_init(&info->controller.lock);
  633. init_waitqueue_head(&info->controller.wq);
  634. info->device = &pdev->dev;
  635. info->platform = plat;
  636. /* initialise chip data struct */
  637. chip = &info->chip;
  638. mtd = nand_to_mtd(&info->chip);
  639. if (plat->data_width)
  640. chip->options |= NAND_BUSWIDTH_16;
  641. chip->options |= NAND_CACHEPRG | NAND_SKIP_BBTSCAN;
  642. chip->read_buf = (plat->data_width) ?
  643. bf5xx_nand_read_buf16 : bf5xx_nand_read_buf;
  644. chip->write_buf = (plat->data_width) ?
  645. bf5xx_nand_write_buf16 : bf5xx_nand_write_buf;
  646. chip->read_byte = bf5xx_nand_read_byte;
  647. chip->cmd_ctrl = bf5xx_nand_hwcontrol;
  648. chip->dev_ready = bf5xx_nand_devready;
  649. nand_set_controller_data(chip, mtd);
  650. chip->controller = &info->controller;
  651. chip->IO_ADDR_R = (void __iomem *) NFC_READ;
  652. chip->IO_ADDR_W = (void __iomem *) NFC_DATA_WR;
  653. chip->chip_delay = 0;
  654. /* initialise mtd info data struct */
  655. mtd->dev.parent = &pdev->dev;
  656. /* initialise the hardware */
  657. err = bf5xx_nand_hw_init(info);
  658. if (err)
  659. goto out_err;
  660. /* setup hardware ECC data struct */
  661. if (hardware_ecc) {
  662. #ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
  663. chip->ecc.layout = &bootrom_ecclayout;
  664. #endif
  665. chip->read_buf = bf5xx_nand_dma_read_buf;
  666. chip->write_buf = bf5xx_nand_dma_write_buf;
  667. chip->ecc.calculate = bf5xx_nand_calculate_ecc;
  668. chip->ecc.correct = bf5xx_nand_correct_data;
  669. chip->ecc.mode = NAND_ECC_HW;
  670. chip->ecc.hwctl = bf5xx_nand_enable_hwecc;
  671. chip->ecc.read_page_raw = bf5xx_nand_read_page_raw;
  672. chip->ecc.write_page_raw = bf5xx_nand_write_page_raw;
  673. } else {
  674. chip->ecc.mode = NAND_ECC_SOFT;
  675. }
  676. /* scan hardware nand chip and setup mtd info data struct */
  677. if (bf5xx_nand_scan(mtd)) {
  678. err = -ENXIO;
  679. goto out_err_nand_scan;
  680. }
  681. #ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
  682. chip->badblockpos = 63;
  683. #endif
  684. /* add NAND partition */
  685. bf5xx_nand_add_partition(info);
  686. dev_dbg(&pdev->dev, "initialised ok\n");
  687. return 0;
  688. out_err_nand_scan:
  689. bf5xx_nand_dma_remove(info);
  690. out_err:
  691. peripheral_free_list(bfin_nfc_pin_req);
  692. return err;
  693. }
  694. /* driver device registration */
  695. static struct platform_driver bf5xx_nand_driver = {
  696. .probe = bf5xx_nand_probe,
  697. .remove = bf5xx_nand_remove,
  698. .driver = {
  699. .name = DRV_NAME,
  700. },
  701. };
  702. module_platform_driver(bf5xx_nand_driver);
  703. MODULE_LICENSE("GPL");
  704. MODULE_AUTHOR(DRV_AUTHOR);
  705. MODULE_DESCRIPTION(DRV_DESC);
  706. MODULE_ALIAS("platform:" DRV_NAME);