mpc5121_nfc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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/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. case NAND_CMD_RESET:
  291. break;
  292. default:
  293. return;
  294. }
  295. mpc5121_nfc_send_cmd(mtd, command);
  296. mpc5121_nfc_addr_cycle(mtd, column, page);
  297. switch (command) {
  298. case NAND_CMD_READ0:
  299. if (mtd->writesize > 512)
  300. mpc5121_nfc_send_cmd(mtd, NAND_CMD_READSTART);
  301. mpc5121_nfc_send_read_page(mtd);
  302. break;
  303. case NAND_CMD_READID:
  304. mpc5121_nfc_send_read_id(mtd);
  305. break;
  306. case NAND_CMD_STATUS:
  307. mpc5121_nfc_send_read_status(mtd);
  308. if (chip->options & NAND_BUSWIDTH_16)
  309. prv->column = 1;
  310. else
  311. prv->column = 0;
  312. break;
  313. }
  314. }
  315. /* Copy data from/to NFC spare buffers. */
  316. static void mpc5121_nfc_copy_spare(struct mtd_info *mtd, uint offset,
  317. u8 * buffer, uint size, int wr)
  318. {
  319. struct nand_chip *nand = mtd->priv;
  320. struct mpc5121_nfc_prv *prv = nand->priv;
  321. uint o, s, sbsize, blksize;
  322. /*
  323. * NAND spare area is available through NFC spare buffers.
  324. * The NFC divides spare area into (page_size / 512) chunks.
  325. * Each chunk is placed into separate spare memory area, using
  326. * first (spare_size / num_of_chunks) bytes of the buffer.
  327. *
  328. * For NAND device in which the spare area is not divided fully
  329. * by the number of chunks, number of used bytes in each spare
  330. * buffer is rounded down to the nearest even number of bytes,
  331. * and all remaining bytes are added to the last used spare area.
  332. *
  333. * For more information read section 26.6.10 of MPC5121e
  334. * Microcontroller Reference Manual, Rev. 3.
  335. */
  336. /* Calculate number of valid bytes in each spare buffer */
  337. sbsize = (mtd->oobsize / (mtd->writesize / 512)) & ~1;
  338. while (size) {
  339. /* Calculate spare buffer number */
  340. s = offset / sbsize;
  341. if (s > NFC_SPARE_BUFFERS - 1)
  342. s = NFC_SPARE_BUFFERS - 1;
  343. /*
  344. * Calculate offset to requested data block in selected spare
  345. * buffer and its size.
  346. */
  347. o = offset - (s * sbsize);
  348. blksize = min(sbsize - o, size);
  349. if (wr)
  350. memcpy_toio(prv->regs + NFC_SPARE_AREA(s) + o,
  351. buffer, blksize);
  352. else
  353. memcpy_fromio(buffer,
  354. prv->regs + NFC_SPARE_AREA(s) + o,
  355. blksize);
  356. buffer += blksize;
  357. offset += blksize;
  358. size -= blksize;
  359. };
  360. }
  361. /* Copy data from/to NFC main and spare buffers */
  362. static void mpc5121_nfc_buf_copy(struct mtd_info *mtd, u_char * buf, int len,
  363. int wr)
  364. {
  365. struct nand_chip *chip = mtd->priv;
  366. struct mpc5121_nfc_prv *prv = chip->priv;
  367. uint c = prv->column;
  368. uint l;
  369. /* Handle spare area access */
  370. if (prv->spareonly || c >= mtd->writesize) {
  371. /* Calculate offset from beginning of spare area */
  372. if (c >= mtd->writesize)
  373. c -= mtd->writesize;
  374. prv->column += len;
  375. mpc5121_nfc_copy_spare(mtd, c, buf, len, wr);
  376. return;
  377. }
  378. /*
  379. * Handle main area access - limit copy length to prevent
  380. * crossing main/spare boundary.
  381. */
  382. l = min((uint) len, mtd->writesize - c);
  383. prv->column += l;
  384. if (wr)
  385. memcpy_toio(prv->regs + NFC_MAIN_AREA(0) + c, buf, l);
  386. else
  387. memcpy_fromio(buf, prv->regs + NFC_MAIN_AREA(0) + c, l);
  388. /* Handle crossing main/spare boundary */
  389. if (l != len) {
  390. buf += l;
  391. len -= l;
  392. mpc5121_nfc_buf_copy(mtd, buf, len, wr);
  393. }
  394. }
  395. /* Read data from NFC buffers */
  396. static void mpc5121_nfc_read_buf(struct mtd_info *mtd, u_char * buf, int len)
  397. {
  398. mpc5121_nfc_buf_copy(mtd, buf, len, 0);
  399. }
  400. /* Write data to NFC buffers */
  401. static void mpc5121_nfc_write_buf(struct mtd_info *mtd,
  402. const u_char * buf, int len)
  403. {
  404. mpc5121_nfc_buf_copy(mtd, (u_char *) buf, len, 1);
  405. }
  406. /* Compare buffer with NAND flash */
  407. static int mpc5121_nfc_verify_buf(struct mtd_info *mtd,
  408. const u_char * buf, int len)
  409. {
  410. u_char tmp[256];
  411. uint bsize;
  412. while (len) {
  413. bsize = min(len, 256);
  414. mpc5121_nfc_read_buf(mtd, tmp, bsize);
  415. if (memcmp(buf, tmp, bsize))
  416. return 1;
  417. buf += bsize;
  418. len -= bsize;
  419. }
  420. return 0;
  421. }
  422. /* Read byte from NFC buffers */
  423. static u8 mpc5121_nfc_read_byte(struct mtd_info *mtd)
  424. {
  425. u8 tmp;
  426. mpc5121_nfc_read_buf(mtd, &tmp, sizeof(tmp));
  427. return tmp;
  428. }
  429. /* Read word from NFC buffers */
  430. static u16 mpc5121_nfc_read_word(struct mtd_info *mtd)
  431. {
  432. u16 tmp;
  433. mpc5121_nfc_read_buf(mtd, (u_char *) & tmp, sizeof(tmp));
  434. return tmp;
  435. }
  436. /*
  437. * Read NFC configuration from Reset Config Word
  438. *
  439. * NFC is configured during reset in basis of information stored
  440. * in Reset Config Word. There is no other way to set NAND block
  441. * size, spare size and bus width.
  442. */
  443. static int mpc5121_nfc_read_hw_config(struct mtd_info *mtd)
  444. {
  445. immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  446. struct nand_chip *chip = mtd->priv;
  447. uint rcw_pagesize = 0;
  448. uint rcw_sparesize = 0;
  449. uint rcw_width;
  450. uint rcwh;
  451. uint romloc, ps;
  452. rcwh = in_be32(&(im->reset.rcwh));
  453. /* Bit 6: NFC bus width */
  454. rcw_width = ((rcwh >> 6) & 0x1) ? 2 : 1;
  455. /* Bit 7: NFC Page/Spare size */
  456. ps = (rcwh >> 7) & 0x1;
  457. /* Bits [22:21]: ROM Location */
  458. romloc = (rcwh >> 21) & 0x3;
  459. /* Decode RCW bits */
  460. switch ((ps << 2) | romloc) {
  461. case 0x00:
  462. case 0x01:
  463. rcw_pagesize = 512;
  464. rcw_sparesize = 16;
  465. break;
  466. case 0x02:
  467. case 0x03:
  468. rcw_pagesize = 4096;
  469. rcw_sparesize = 128;
  470. break;
  471. case 0x04:
  472. case 0x05:
  473. rcw_pagesize = 2048;
  474. rcw_sparesize = 64;
  475. break;
  476. case 0x06:
  477. case 0x07:
  478. rcw_pagesize = 4096;
  479. rcw_sparesize = 218;
  480. break;
  481. }
  482. mtd->writesize = rcw_pagesize;
  483. mtd->oobsize = rcw_sparesize;
  484. if (rcw_width == 2)
  485. chip->options |= NAND_BUSWIDTH_16;
  486. debug(KERN_NOTICE DRV_NAME ": Configured for "
  487. "%u-bit NAND, page size %u with %u spare.\n",
  488. rcw_width * 8, rcw_pagesize, rcw_sparesize);
  489. return 0;
  490. }
  491. int board_nand_init(struct nand_chip *chip)
  492. {
  493. struct mpc5121_nfc_prv *prv;
  494. struct mtd_info *mtd;
  495. int resettime = 0;
  496. int retval = 0;
  497. int rev;
  498. static int chip_nr = 0;
  499. /*
  500. * Check SoC revision. This driver supports only NFC
  501. * in MPC5121 revision 2.
  502. */
  503. rev = (mfspr(SPRN_SVR) >> 4) & 0xF;
  504. if (rev != 2) {
  505. printk(KERN_ERR DRV_NAME
  506. ": SoC revision %u is not supported!\n", rev);
  507. return -ENXIO;
  508. }
  509. prv = malloc(sizeof(*prv));
  510. if (!prv) {
  511. printk(KERN_ERR DRV_NAME ": Memory exhausted!\n");
  512. return -ENOMEM;
  513. }
  514. mtd = &nand_info[chip_nr++];
  515. mtd->priv = chip;
  516. chip->priv = prv;
  517. /* Read NFC configuration from Reset Config Word */
  518. retval = mpc5121_nfc_read_hw_config(mtd);
  519. if (retval) {
  520. printk(KERN_ERR DRV_NAME ": Unable to read NFC config!\n");
  521. return retval;
  522. }
  523. prv->regs = (void __iomem *)CONFIG_SYS_NAND_BASE;
  524. chip->dev_ready = mpc5121_nfc_dev_ready;
  525. chip->cmdfunc = mpc5121_nfc_command;
  526. chip->read_byte = mpc5121_nfc_read_byte;
  527. chip->read_word = mpc5121_nfc_read_word;
  528. chip->read_buf = mpc5121_nfc_read_buf;
  529. chip->write_buf = mpc5121_nfc_write_buf;
  530. chip->verify_buf = mpc5121_nfc_verify_buf;
  531. chip->select_chip = mpc5121_nfc_select_chip;
  532. chip->bbt_options = NAND_BBT_USE_FLASH;
  533. chip->ecc.mode = NAND_ECC_SOFT;
  534. /* Reset NAND Flash controller */
  535. nfc_set(mtd, NFC_CONFIG1, NFC_RESET);
  536. while (nfc_read(mtd, NFC_CONFIG1) & NFC_RESET) {
  537. if (resettime++ >= NFC_RESET_TIMEOUT) {
  538. printk(KERN_ERR DRV_NAME
  539. ": Timeout while resetting NFC!\n");
  540. retval = -EINVAL;
  541. goto error;
  542. }
  543. udelay(1);
  544. }
  545. /* Enable write to NFC memory */
  546. nfc_write(mtd, NFC_CONFIG, NFC_BLS_UNLOCKED);
  547. /* Enable write to all NAND pages */
  548. nfc_write(mtd, NFC_UNLOCKSTART_BLK0, 0x0000);
  549. nfc_write(mtd, NFC_UNLOCKEND_BLK0, 0xFFFF);
  550. nfc_write(mtd, NFC_WRPROT, NFC_WPC_UNLOCK);
  551. /*
  552. * Setup NFC:
  553. * - Big Endian transfers,
  554. * - Interrupt after full page read/write.
  555. */
  556. nfc_write(mtd, NFC_CONFIG1, NFC_BIG_ENDIAN | NFC_INT_MASK |
  557. NFC_FULL_PAGE_INT);
  558. /* Set spare area size */
  559. nfc_write(mtd, NFC_SPAS, mtd->oobsize >> 1);
  560. /* Detect NAND chips */
  561. if (nand_scan(mtd, 1)) {
  562. printk(KERN_ERR DRV_NAME ": NAND Flash not found !\n");
  563. retval = -ENXIO;
  564. goto error;
  565. }
  566. /* Set erase block size */
  567. switch (mtd->erasesize / mtd->writesize) {
  568. case 32:
  569. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_32);
  570. break;
  571. case 64:
  572. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_64);
  573. break;
  574. case 128:
  575. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_128);
  576. break;
  577. case 256:
  578. nfc_set(mtd, NFC_CONFIG1, NFC_PPB_256);
  579. break;
  580. default:
  581. printk(KERN_ERR DRV_NAME ": Unsupported NAND flash!\n");
  582. retval = -ENXIO;
  583. goto error;
  584. }
  585. return 0;
  586. error:
  587. return retval;
  588. }