bf5xx_nand.c 20 KB

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