mpc5121_nfc.c 15 KB

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