mpc5121_nfc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Copyright 2004-2008 Freescale Semiconductor, Inc.
  3. * Copyright 2009 Semihalf.
  4. * (C) Copyright 2009 Stefan Roese <sr@denx.de>
  5. *
  6. * Based on original driver from Freescale Semiconductor
  7. * written by John Rigby <jrigby@freescale.com> on basis
  8. * of drivers/mtd/nand/mxc_nand.c. Reworked and extended
  9. * Piotr Ziecik <kosmo@semihalf.com>.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. * MA 02110-1301, USA.
  24. */
  25. #include <common.h>
  26. #include <malloc.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/nand.h>
  29. #include <linux/mtd/nand_ecc.h>
  30. #include <linux/mtd/compat.h>
  31. #include <asm/errno.h>
  32. #include <asm/io.h>
  33. #include <asm/processor.h>
  34. #include <nand.h>
  35. #define DRV_NAME "mpc5121_nfc"
  36. /* Timeouts */
  37. #define NFC_RESET_TIMEOUT 1000 /* 1 ms */
  38. #define NFC_TIMEOUT 2000 /* 2000 us */
  39. /* Addresses for NFC MAIN RAM BUFFER areas */
  40. #define NFC_MAIN_AREA(n) ((n) * 0x200)
  41. /* Addresses for NFC SPARE BUFFER areas */
  42. #define NFC_SPARE_BUFFERS 8
  43. #define NFC_SPARE_LEN 0x40
  44. #define NFC_SPARE_AREA(n) (0x1000 + ((n) * NFC_SPARE_LEN))
  45. /* MPC5121 NFC registers */
  46. #define NFC_BUF_ADDR 0x1E04
  47. #define NFC_FLASH_ADDR 0x1E06
  48. #define NFC_FLASH_CMD 0x1E08
  49. #define NFC_CONFIG 0x1E0A
  50. #define NFC_ECC_STATUS1 0x1E0C
  51. #define NFC_ECC_STATUS2 0x1E0E
  52. #define NFC_SPAS 0x1E10
  53. #define NFC_WRPROT 0x1E12
  54. #define NFC_NF_WRPRST 0x1E18
  55. #define NFC_CONFIG1 0x1E1A
  56. #define NFC_CONFIG2 0x1E1C
  57. #define NFC_UNLOCKSTART_BLK0 0x1E20
  58. #define NFC_UNLOCKEND_BLK0 0x1E22
  59. #define NFC_UNLOCKSTART_BLK1 0x1E24
  60. #define NFC_UNLOCKEND_BLK1 0x1E26
  61. #define NFC_UNLOCKSTART_BLK2 0x1E28
  62. #define NFC_UNLOCKEND_BLK2 0x1E2A
  63. #define NFC_UNLOCKSTART_BLK3 0x1E2C
  64. #define NFC_UNLOCKEND_BLK3 0x1E2E
  65. /* Bit Definitions: NFC_BUF_ADDR */
  66. #define NFC_RBA_MASK (7 << 0)
  67. #define NFC_ACTIVE_CS_SHIFT 5
  68. #define NFC_ACTIVE_CS_MASK (3 << NFC_ACTIVE_CS_SHIFT)
  69. /* Bit Definitions: NFC_CONFIG */
  70. #define NFC_BLS_UNLOCKED (1 << 1)
  71. /* Bit Definitions: NFC_CONFIG1 */
  72. #define NFC_ECC_4BIT (1 << 0)
  73. #define NFC_FULL_PAGE_DMA (1 << 1)
  74. #define NFC_SPARE_ONLY (1 << 2)
  75. #define NFC_ECC_ENABLE (1 << 3)
  76. #define NFC_INT_MASK (1 << 4)
  77. #define NFC_BIG_ENDIAN (1 << 5)
  78. #define NFC_RESET (1 << 6)
  79. #define NFC_CE (1 << 7)
  80. #define NFC_ONE_CYCLE (1 << 8)
  81. #define NFC_PPB_32 (0 << 9)
  82. #define NFC_PPB_64 (1 << 9)
  83. #define NFC_PPB_128 (2 << 9)
  84. #define NFC_PPB_256 (3 << 9)
  85. #define NFC_PPB_MASK (3 << 9)
  86. #define NFC_FULL_PAGE_INT (1 << 11)
  87. /* Bit Definitions: NFC_CONFIG2 */
  88. #define NFC_COMMAND (1 << 0)
  89. #define NFC_ADDRESS (1 << 1)
  90. #define NFC_INPUT (1 << 2)
  91. #define NFC_OUTPUT (1 << 3)
  92. #define NFC_ID (1 << 4)
  93. #define NFC_STATUS (1 << 5)
  94. #define NFC_CMD_FAIL (1 << 15)
  95. #define NFC_INT (1 << 15)
  96. /* Bit Definitions: NFC_WRPROT */
  97. #define NFC_WPC_LOCK_TIGHT (1 << 0)
  98. #define NFC_WPC_LOCK (1 << 1)
  99. #define NFC_WPC_UNLOCK (1 << 2)
  100. struct mpc5121_nfc_prv {
  101. struct mtd_info mtd;
  102. struct nand_chip chip;
  103. int irq;
  104. void __iomem *regs;
  105. struct clk *clk;
  106. uint column;
  107. int spareonly;
  108. int chipsel;
  109. };
  110. int mpc5121_nfc_chip = 0;
  111. static void mpc5121_nfc_done(struct mtd_info *mtd);
  112. /* Read NFC register */
  113. static inline u16 nfc_read(struct mtd_info *mtd, uint reg)
  114. {
  115. struct nand_chip *chip = mtd->priv;
  116. struct mpc5121_nfc_prv *prv = chip->priv;
  117. return in_be16(prv->regs + reg);
  118. }
  119. /* Write NFC register */
  120. static inline void nfc_write(struct mtd_info *mtd, uint reg, u16 val)
  121. {
  122. struct nand_chip *chip = mtd->priv;
  123. struct mpc5121_nfc_prv *prv = chip->priv;
  124. out_be16(prv->regs + reg, val);
  125. }
  126. /* Set bits in NFC register */
  127. static inline void nfc_set(struct mtd_info *mtd, uint reg, u16 bits)
  128. {
  129. nfc_write(mtd, reg, nfc_read(mtd, reg) | bits);
  130. }
  131. /* Clear bits in NFC register */
  132. static inline void nfc_clear(struct mtd_info *mtd, uint reg, u16 bits)
  133. {
  134. nfc_write(mtd, reg, nfc_read(mtd, reg) & ~bits);
  135. }
  136. /* Invoke address cycle */
  137. static inline void mpc5121_nfc_send_addr(struct mtd_info *mtd, u16 addr)
  138. {
  139. nfc_write(mtd, NFC_FLASH_ADDR, addr);
  140. nfc_write(mtd, NFC_CONFIG2, NFC_ADDRESS);
  141. mpc5121_nfc_done(mtd);
  142. }
  143. /* Invoke command cycle */
  144. static inline void mpc5121_nfc_send_cmd(struct mtd_info *mtd, u16 cmd)
  145. {
  146. nfc_write(mtd, NFC_FLASH_CMD, cmd);
  147. nfc_write(mtd, NFC_CONFIG2, NFC_COMMAND);
  148. mpc5121_nfc_done(mtd);
  149. }
  150. /* Send data from NFC buffers to NAND flash */
  151. static inline void mpc5121_nfc_send_prog_page(struct mtd_info *mtd)
  152. {
  153. nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
  154. nfc_write(mtd, NFC_CONFIG2, NFC_INPUT);
  155. mpc5121_nfc_done(mtd);
  156. }
  157. /* Receive data from NAND flash */
  158. static inline void mpc5121_nfc_send_read_page(struct mtd_info *mtd)
  159. {
  160. nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
  161. nfc_write(mtd, NFC_CONFIG2, NFC_OUTPUT);
  162. mpc5121_nfc_done(mtd);
  163. }
  164. /* Receive ID from NAND flash */
  165. static inline void mpc5121_nfc_send_read_id(struct mtd_info *mtd)
  166. {
  167. nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
  168. nfc_write(mtd, NFC_CONFIG2, NFC_ID);
  169. mpc5121_nfc_done(mtd);
  170. }
  171. /* Receive status from NAND flash */
  172. static inline void mpc5121_nfc_send_read_status(struct mtd_info *mtd)
  173. {
  174. nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
  175. nfc_write(mtd, NFC_CONFIG2, NFC_STATUS);
  176. mpc5121_nfc_done(mtd);
  177. }
  178. static void mpc5121_nfc_done(struct mtd_info *mtd)
  179. {
  180. int max_retries = NFC_TIMEOUT;
  181. while (1) {
  182. max_retries--;
  183. if (nfc_read(mtd, NFC_CONFIG2) & NFC_INT)
  184. break;
  185. udelay(1);
  186. }
  187. if (max_retries <= 0)
  188. printk(KERN_WARNING DRV_NAME
  189. ": Timeout while waiting for completion.\n");
  190. }
  191. /* Do address cycle(s) */
  192. static void mpc5121_nfc_addr_cycle(struct mtd_info *mtd, int column, int page)
  193. {
  194. struct nand_chip *chip = mtd->priv;
  195. u32 pagemask = chip->pagemask;
  196. if (column != -1) {
  197. mpc5121_nfc_send_addr(mtd, column);
  198. if (mtd->writesize > 512)
  199. mpc5121_nfc_send_addr(mtd, column >> 8);
  200. }
  201. if (page != -1) {
  202. do {
  203. mpc5121_nfc_send_addr(mtd, page & 0xFF);
  204. page >>= 8;
  205. pagemask >>= 8;
  206. } while (pagemask);
  207. }
  208. }
  209. /* Control chip select signals */
  210. /*
  211. * Selecting the active device:
  212. *
  213. * This is different than the linux version. Switching between chips
  214. * is done via board_nand_select_device(). The Linux select_chip
  215. * function used here in U-Boot has only 2 valid chip numbers:
  216. * 0 select
  217. * -1 deselect
  218. */
  219. /*
  220. * Implement it as a weak default, so that boards with a specific
  221. * chip-select routine can use their own function.
  222. */
  223. void __mpc5121_nfc_select_chip(struct mtd_info *mtd, int chip)
  224. {
  225. if (chip < 0) {
  226. nfc_clear(mtd, NFC_CONFIG1, NFC_CE);
  227. return;
  228. }
  229. nfc_clear(mtd, NFC_BUF_ADDR, NFC_ACTIVE_CS_MASK);
  230. nfc_set(mtd, NFC_BUF_ADDR, (chip << NFC_ACTIVE_CS_SHIFT) &
  231. NFC_ACTIVE_CS_MASK);
  232. nfc_set(mtd, NFC_CONFIG1, NFC_CE);
  233. }
  234. void mpc5121_nfc_select_chip(struct mtd_info *mtd, int chip)
  235. __attribute__((weak, alias("__mpc5121_nfc_select_chip")));
  236. void board_nand_select_device(struct nand_chip *nand, int chip)
  237. {
  238. /*
  239. * Only save this chip number in global variable here. This
  240. * will be used later in mpc5121_nfc_select_chip().
  241. */
  242. mpc5121_nfc_chip = chip;
  243. }
  244. /* Read NAND Ready/Busy signal */
  245. static int mpc5121_nfc_dev_ready(struct mtd_info *mtd)
  246. {
  247. /*
  248. * NFC handles ready/busy signal internally. Therefore, this function
  249. * always returns status as ready.
  250. */
  251. return 1;
  252. }
  253. /* Write command to NAND flash */
  254. static void mpc5121_nfc_command(struct mtd_info *mtd, unsigned command,
  255. int column, int page)
  256. {
  257. struct nand_chip *chip = mtd->priv;
  258. struct mpc5121_nfc_prv *prv = chip->priv;
  259. prv->column = (column >= 0) ? column : 0;
  260. prv->spareonly = 0;
  261. switch (command) {
  262. case NAND_CMD_PAGEPROG:
  263. mpc5121_nfc_send_prog_page(mtd);
  264. break;
  265. /*
  266. * NFC does not support sub-page reads and writes,
  267. * so emulate them using full page transfers.
  268. */
  269. case NAND_CMD_READ0:
  270. column = 0;
  271. break;
  272. case NAND_CMD_READ1:
  273. prv->column += 256;
  274. command = NAND_CMD_READ0;
  275. column = 0;
  276. break;
  277. case NAND_CMD_READOOB:
  278. prv->spareonly = 1;
  279. command = NAND_CMD_READ0;
  280. column = 0;
  281. break;
  282. case NAND_CMD_SEQIN:
  283. mpc5121_nfc_command(mtd, NAND_CMD_READ0, column, page);
  284. column = 0;
  285. break;
  286. case NAND_CMD_ERASE1:
  287. case NAND_CMD_ERASE2:
  288. case NAND_CMD_READID:
  289. case NAND_CMD_STATUS:
  290. break;
  291. default:
  292. return;
  293. }
  294. mpc5121_nfc_send_cmd(mtd, command);
  295. mpc5121_nfc_addr_cycle(mtd, column, page);
  296. switch (command) {
  297. case NAND_CMD_READ0:
  298. if (mtd->writesize > 512)
  299. mpc5121_nfc_send_cmd(mtd, NAND_CMD_READSTART);
  300. mpc5121_nfc_send_read_page(mtd);
  301. break;
  302. case NAND_CMD_READID:
  303. mpc5121_nfc_send_read_id(mtd);
  304. break;
  305. case NAND_CMD_STATUS:
  306. mpc5121_nfc_send_read_status(mtd);
  307. if (chip->options & NAND_BUSWIDTH_16)
  308. prv->column = 1;
  309. else
  310. prv->column = 0;
  311. break;
  312. }
  313. }
  314. /* Copy data from/to NFC spare buffers. */
  315. static void mpc5121_nfc_copy_spare(struct mtd_info *mtd, uint offset,
  316. u8 * buffer, uint size, int wr)
  317. {
  318. struct nand_chip *nand = mtd->priv;
  319. struct mpc5121_nfc_prv *prv = nand->priv;
  320. uint o, s, sbsize, blksize;
  321. /*
  322. * NAND spare area is available through NFC spare buffers.
  323. * The NFC divides spare area into (page_size / 512) chunks.
  324. * Each chunk is placed into separate spare memory area, using
  325. * first (spare_size / num_of_chunks) bytes of the buffer.
  326. *
  327. * For NAND device in which the spare area is not divided fully
  328. * by the number of chunks, number of used bytes in each spare
  329. * buffer is rounded down to the nearest even number of bytes,
  330. * and all remaining bytes are added to the last used spare area.
  331. *
  332. * For more information read section 26.6.10 of MPC5121e
  333. * Microcontroller Reference Manual, Rev. 3.
  334. */
  335. /* Calculate number of valid bytes in each spare buffer */
  336. sbsize = (mtd->oobsize / (mtd->writesize / 512)) & ~1;
  337. while (size) {
  338. /* Calculate spare buffer number */
  339. s = offset / sbsize;
  340. if (s > NFC_SPARE_BUFFERS - 1)
  341. s = NFC_SPARE_BUFFERS - 1;
  342. /*
  343. * Calculate offset to requested data block in selected spare
  344. * buffer and its size.
  345. */
  346. o = offset - (s * sbsize);
  347. blksize = min(sbsize - o, size);
  348. if (wr)
  349. memcpy_toio(prv->regs + NFC_SPARE_AREA(s) + o,
  350. buffer, blksize);
  351. else
  352. memcpy_fromio(buffer,
  353. prv->regs + NFC_SPARE_AREA(s) + o,
  354. blksize);
  355. buffer += blksize;
  356. offset += blksize;
  357. size -= blksize;
  358. };
  359. }
  360. /* Copy data from/to NFC main and spare buffers */
  361. static void mpc5121_nfc_buf_copy(struct mtd_info *mtd, u_char * buf, int len,
  362. int wr)
  363. {
  364. struct nand_chip *chip = mtd->priv;
  365. struct mpc5121_nfc_prv *prv = chip->priv;
  366. uint c = prv->column;
  367. uint l;
  368. /* Handle spare area access */
  369. if (prv->spareonly || c >= mtd->writesize) {
  370. /* Calculate offset from beginning of spare area */
  371. if (c >= mtd->writesize)
  372. c -= mtd->writesize;
  373. prv->column += len;
  374. mpc5121_nfc_copy_spare(mtd, c, buf, len, wr);
  375. return;
  376. }
  377. /*
  378. * Handle main area access - limit copy length to prevent
  379. * crossing main/spare boundary.
  380. */
  381. l = min((uint) len, mtd->writesize - c);
  382. prv->column += l;
  383. if (wr)
  384. memcpy_toio(prv->regs + NFC_MAIN_AREA(0) + c, buf, l);
  385. else
  386. memcpy_fromio(buf, prv->regs + NFC_MAIN_AREA(0) + c, l);
  387. /* Handle crossing main/spare boundary */
  388. if (l != len) {
  389. buf += l;
  390. len -= l;
  391. mpc5121_nfc_buf_copy(mtd, buf, len, wr);
  392. }
  393. }
  394. /* Read data from NFC buffers */
  395. static void mpc5121_nfc_read_buf(struct mtd_info *mtd, u_char * buf, int len)
  396. {
  397. mpc5121_nfc_buf_copy(mtd, buf, len, 0);
  398. }
  399. /* Write data to NFC buffers */
  400. static void mpc5121_nfc_write_buf(struct mtd_info *mtd,
  401. const u_char * buf, int len)
  402. {
  403. mpc5121_nfc_buf_copy(mtd, (u_char *) buf, len, 1);
  404. }
  405. /* Compare buffer with NAND flash */
  406. static int mpc5121_nfc_verify_buf(struct mtd_info *mtd,
  407. const u_char * buf, int len)
  408. {
  409. u_char tmp[256];
  410. uint bsize;
  411. while (len) {
  412. bsize = min(len, 256);
  413. mpc5121_nfc_read_buf(mtd, tmp, bsize);
  414. if (memcmp(buf, tmp, bsize))
  415. return 1;
  416. buf += bsize;
  417. len -= bsize;
  418. }
  419. return 0;
  420. }
  421. /* Read byte from NFC buffers */
  422. static u8 mpc5121_nfc_read_byte(struct mtd_info *mtd)
  423. {
  424. u8 tmp;
  425. mpc5121_nfc_read_buf(mtd, &tmp, sizeof(tmp));
  426. return tmp;
  427. }
  428. /* Read word from NFC buffers */
  429. static u16 mpc5121_nfc_read_word(struct mtd_info *mtd)
  430. {
  431. u16 tmp;
  432. mpc5121_nfc_read_buf(mtd, (u_char *) & tmp, sizeof(tmp));
  433. return tmp;
  434. }
  435. /*
  436. * Read NFC configuration from Reset Config Word
  437. *
  438. * NFC is configured during reset in basis of information stored
  439. * in Reset Config Word. There is no other way to set NAND block
  440. * size, spare size and bus width.
  441. */
  442. static int mpc5121_nfc_read_hw_config(struct mtd_info *mtd)
  443. {
  444. immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  445. struct nand_chip *chip = mtd->priv;
  446. uint rcw_pagesize = 0;
  447. uint rcw_sparesize = 0;
  448. uint rcw_width;
  449. uint rcwh;
  450. uint romloc, ps;
  451. rcwh = in_be32(&(im->reset.rcwh));
  452. /* Bit 6: NFC bus width */
  453. rcw_width = ((rcwh >> 6) & 0x1) ? 2 : 1;
  454. /* Bit 7: NFC Page/Spare size */
  455. ps = (rcwh >> 7) & 0x1;
  456. /* Bits [22:21]: ROM Location */
  457. romloc = (rcwh >> 21) & 0x3;
  458. /* Decode RCW bits */
  459. switch ((ps << 2) | romloc) {
  460. case 0x00:
  461. case 0x01:
  462. rcw_pagesize = 512;
  463. rcw_sparesize = 16;
  464. break;
  465. case 0x02:
  466. case 0x03:
  467. rcw_pagesize = 4096;
  468. rcw_sparesize = 128;
  469. break;
  470. case 0x04:
  471. case 0x05:
  472. rcw_pagesize = 2048;
  473. rcw_sparesize = 64;
  474. break;
  475. case 0x06:
  476. case 0x07:
  477. rcw_pagesize = 4096;
  478. rcw_sparesize = 218;
  479. break;
  480. }
  481. mtd->writesize = rcw_pagesize;
  482. mtd->oobsize = rcw_sparesize;
  483. if (rcw_width == 2)
  484. chip->options |= NAND_BUSWIDTH_16;
  485. debug(KERN_NOTICE DRV_NAME ": Configured for "
  486. "%u-bit NAND, page size %u with %u spare.\n",
  487. rcw_width * 8, rcw_pagesize, rcw_sparesize);
  488. return 0;
  489. }
  490. int board_nand_init(struct nand_chip *chip)
  491. {
  492. struct mpc5121_nfc_prv *prv;
  493. struct mtd_info *mtd;
  494. int resettime = 0;
  495. int retval = 0;
  496. int rev;
  497. static int chip_nr = 0;
  498. /*
  499. * Check SoC revision. This driver supports only NFC
  500. * in MPC5121 revision 2.
  501. */
  502. rev = (mfspr(SPRN_SVR) >> 4) & 0xF;
  503. if (rev != 2) {
  504. printk(KERN_ERR DRV_NAME
  505. ": SoC revision %u is not supported!\n", rev);
  506. return -ENXIO;
  507. }
  508. prv = malloc(sizeof(*prv));
  509. if (!prv) {
  510. printk(KERN_ERR DRV_NAME ": Memory exhausted!\n");
  511. return -ENOMEM;
  512. }
  513. mtd = &nand_info[chip_nr++];
  514. mtd->priv = chip;
  515. chip->priv = prv;
  516. /* Read NFC configuration from Reset Config Word */
  517. retval = mpc5121_nfc_read_hw_config(mtd);
  518. if (retval) {
  519. printk(KERN_ERR DRV_NAME ": Unable to read NFC config!\n");
  520. return retval;
  521. }
  522. prv->regs = (void __iomem *)CONFIG_SYS_NAND_BASE;
  523. chip->dev_ready = mpc5121_nfc_dev_ready;
  524. chip->cmdfunc = mpc5121_nfc_command;
  525. chip->read_byte = mpc5121_nfc_read_byte;
  526. chip->read_word = mpc5121_nfc_read_word;
  527. chip->read_buf = mpc5121_nfc_read_buf;
  528. chip->write_buf = mpc5121_nfc_write_buf;
  529. chip->verify_buf = mpc5121_nfc_verify_buf;
  530. chip->select_chip = mpc5121_nfc_select_chip;
  531. chip->options = NAND_NO_AUTOINCR | NAND_USE_FLASH_BBT;
  532. chip->ecc.mode = NAND_ECC_SOFT;
  533. /* Reset NAND Flash controller */
  534. nfc_set(mtd, NFC_CONFIG1, NFC_RESET);
  535. while (nfc_read(mtd, NFC_CONFIG1) & NFC_RESET) {
  536. if (resettime++ >= NFC_RESET_TIMEOUT) {
  537. printk(KERN_ERR DRV_NAME
  538. ": Timeout while resetting NFC!\n");
  539. retval = -EINVAL;
  540. goto error;
  541. }
  542. udelay(1);
  543. }
  544. /* Enable write to NFC memory */
  545. nfc_write(mtd, NFC_CONFIG, NFC_BLS_UNLOCKED);
  546. /* Enable write to all NAND pages */
  547. nfc_write(mtd, NFC_UNLOCKSTART_BLK0, 0x0000);
  548. nfc_write(mtd, NFC_UNLOCKEND_BLK0, 0xFFFF);
  549. nfc_write(mtd, NFC_WRPROT, NFC_WPC_UNLOCK);
  550. /*
  551. * Setup NFC:
  552. * - Big Endian transfers,
  553. * - Interrupt after full page read/write.
  554. */
  555. nfc_write(mtd, NFC_CONFIG1, NFC_BIG_ENDIAN | NFC_INT_MASK |
  556. NFC_FULL_PAGE_INT);
  557. /* Set spare area size */
  558. nfc_write(mtd, NFC_SPAS, mtd->oobsize >> 1);
  559. /* Detect NAND chips */
  560. if (nand_scan(mtd, 1)) {
  561. printk(KERN_ERR DRV_NAME ": NAND Flash not found !\n");
  562. retval = -ENXIO;
  563. goto error;
  564. }
  565. /* Set erase block size */
  566. switch (mtd->erasesize / mtd->writesize) {
  567. case 32:
  568. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_32);
  569. break;
  570. case 64:
  571. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_64);
  572. break;
  573. case 128:
  574. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_128);
  575. break;
  576. case 256:
  577. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_256);
  578. break;
  579. default:
  580. printk(KERN_ERR DRV_NAME ": Unsupported NAND flash!\n");
  581. retval = -ENXIO;
  582. goto error;
  583. }
  584. return 0;
  585. error:
  586. return retval;
  587. }