mxc_nand.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
  4. * Copyright 2009 Ilya Yanok, <yanok@emcraft.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include <common.h>
  21. #include <nand.h>
  22. #include <linux/err.h>
  23. #include <asm/io.h>
  24. #ifdef CONFIG_MX27
  25. #include <asm/arch/imx-regs.h>
  26. #endif
  27. #define DRIVER_NAME "mxc_nand"
  28. struct nfc_regs {
  29. /* NFC RAM BUFFER Main area 0 */
  30. uint8_t main_area0[0x200];
  31. uint8_t main_area1[0x200];
  32. uint8_t main_area2[0x200];
  33. uint8_t main_area3[0x200];
  34. /* SPARE BUFFER Spare area 0 */
  35. uint8_t spare_area0[0x10];
  36. uint8_t spare_area1[0x10];
  37. uint8_t spare_area2[0x10];
  38. uint8_t spare_area3[0x10];
  39. uint8_t pad[0x5c0];
  40. /* NFC registers */
  41. uint16_t nfc_buf_size;
  42. uint16_t reserved;
  43. uint16_t nfc_buf_addr;
  44. uint16_t nfc_flash_addr;
  45. uint16_t nfc_flash_cmd;
  46. uint16_t nfc_config;
  47. uint16_t nfc_ecc_status_result;
  48. uint16_t nfc_rsltmain_area;
  49. uint16_t nfc_rsltspare_area;
  50. uint16_t nfc_wrprot;
  51. uint16_t nfc_unlockstart_blkaddr;
  52. uint16_t nfc_unlockend_blkaddr;
  53. uint16_t nfc_nf_wrprst;
  54. uint16_t nfc_config1;
  55. uint16_t nfc_config2;
  56. };
  57. /*
  58. * Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register
  59. * for Command operation
  60. */
  61. #define NFC_CMD 0x1
  62. /*
  63. * Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register
  64. * for Address operation
  65. */
  66. #define NFC_ADDR 0x2
  67. /*
  68. * Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register
  69. * for Input operation
  70. */
  71. #define NFC_INPUT 0x4
  72. /*
  73. * Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register
  74. * for Data Output operation
  75. */
  76. #define NFC_OUTPUT 0x8
  77. /*
  78. * Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register
  79. * for Read ID operation
  80. */
  81. #define NFC_ID 0x10
  82. /*
  83. * Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register
  84. * for Read Status operation
  85. */
  86. #define NFC_STATUS 0x20
  87. /*
  88. * Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read
  89. * Status operation
  90. */
  91. #define NFC_INT 0x8000
  92. #define NFC_SP_EN (1 << 2)
  93. #define NFC_ECC_EN (1 << 3)
  94. #define NFC_BIG (1 << 5)
  95. #define NFC_RST (1 << 6)
  96. #define NFC_CE (1 << 7)
  97. #define NFC_ONE_CYCLE (1 << 8)
  98. typedef enum {false, true} bool;
  99. struct mxc_nand_host {
  100. struct mtd_info mtd;
  101. struct nand_chip *nand;
  102. struct nfc_regs __iomem *regs;
  103. int spare_only;
  104. int status_request;
  105. int pagesize_2k;
  106. int clk_act;
  107. uint16_t col_addr;
  108. };
  109. static struct mxc_nand_host mxc_host;
  110. static struct mxc_nand_host *host = &mxc_host;
  111. /* Define delays in microsec for NAND device operations */
  112. #define TROP_US_DELAY 2000
  113. /* Macros to get byte and bit positions of ECC */
  114. #define COLPOS(x) ((x) >> 3)
  115. #define BITPOS(x) ((x) & 0xf)
  116. /* Define single bit Error positions in Main & Spare area */
  117. #define MAIN_SINGLEBIT_ERROR 0x4
  118. #define SPARE_SINGLEBIT_ERROR 0x1
  119. /* OOB placement block for use with hardware ecc generation */
  120. #ifdef CONFIG_MXC_NAND_HWECC
  121. static struct nand_ecclayout nand_hw_eccoob = {
  122. .eccbytes = 5,
  123. .eccpos = {6, 7, 8, 9, 10},
  124. .oobfree = {{0, 5}, {11, 5}, }
  125. };
  126. #else
  127. static struct nand_ecclayout nand_soft_eccoob = {
  128. .eccbytes = 6,
  129. .eccpos = {6, 7, 8, 9, 10, 11},
  130. .oobfree = {{0, 5}, {12, 4}, }
  131. };
  132. #endif
  133. static uint32_t *mxc_nand_memcpy32(uint32_t *dest, uint32_t *source, size_t size)
  134. {
  135. uint32_t *d = dest;
  136. size >>= 2;
  137. while (size--)
  138. __raw_writel(__raw_readl(source++), d++);
  139. return dest;
  140. }
  141. /*
  142. * This function polls the NANDFC to wait for the basic operation to
  143. * complete by checking the INT bit of config2 register.
  144. */
  145. static void wait_op_done(struct mxc_nand_host *host, int max_retries,
  146. uint16_t param)
  147. {
  148. uint32_t tmp;
  149. while (max_retries-- > 0) {
  150. if (readw(&host->regs->nfc_config2) & NFC_INT) {
  151. tmp = readw(&host->regs->nfc_config2);
  152. tmp &= ~NFC_INT;
  153. writew(tmp, &host->regs->nfc_config2);
  154. break;
  155. }
  156. udelay(1);
  157. }
  158. if (max_retries < 0) {
  159. MTDDEBUG(MTD_DEBUG_LEVEL0, "%s(%d): INT not set\n",
  160. __func__, param);
  161. }
  162. }
  163. /*
  164. * This function issues the specified command to the NAND device and
  165. * waits for completion.
  166. */
  167. static void send_cmd(struct mxc_nand_host *host, uint16_t cmd)
  168. {
  169. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x)\n", cmd);
  170. writew(cmd, &host->regs->nfc_flash_cmd);
  171. writew(NFC_CMD, &host->regs->nfc_config2);
  172. /* Wait for operation to complete */
  173. wait_op_done(host, TROP_US_DELAY, cmd);
  174. }
  175. /*
  176. * This function sends an address (or partial address) to the
  177. * NAND device. The address is used to select the source/destination for
  178. * a NAND command.
  179. */
  180. static void send_addr(struct mxc_nand_host *host, uint16_t addr)
  181. {
  182. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x)\n", addr);
  183. writew(addr, &host->regs->nfc_flash_addr);
  184. writew(NFC_ADDR, &host->regs->nfc_config2);
  185. /* Wait for operation to complete */
  186. wait_op_done(host, TROP_US_DELAY, addr);
  187. }
  188. /*
  189. * This function requests the NANDFC to initate the transfer
  190. * of data currently in the NANDFC RAM buffer to the NAND device.
  191. */
  192. static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
  193. int spare_only)
  194. {
  195. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_prog_page (%d)\n", spare_only);
  196. writew(buf_id, &host->regs->nfc_buf_addr);
  197. /* Configure spare or page+spare access */
  198. if (!host->pagesize_2k) {
  199. uint16_t config1 = readw(&host->regs->nfc_config1);
  200. if (spare_only)
  201. config1 |= NFC_SP_EN;
  202. else
  203. config1 &= ~(NFC_SP_EN);
  204. writew(config1, &host->regs->nfc_config1);
  205. }
  206. writew(NFC_INPUT, &host->regs->nfc_config2);
  207. /* Wait for operation to complete */
  208. wait_op_done(host, TROP_US_DELAY, spare_only);
  209. }
  210. /*
  211. * Requests NANDFC to initated the transfer of data from the
  212. * NAND device into in the NANDFC ram buffer.
  213. */
  214. static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
  215. int spare_only)
  216. {
  217. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_read_page (%d)\n", spare_only);
  218. writew(buf_id, &host->regs->nfc_buf_addr);
  219. /* Configure spare or page+spare access */
  220. if (!host->pagesize_2k) {
  221. uint32_t config1 = readw(&host->regs->nfc_config1);
  222. if (spare_only)
  223. config1 |= NFC_SP_EN;
  224. else
  225. config1 &= ~NFC_SP_EN;
  226. writew(config1, &host->regs->nfc_config1);
  227. }
  228. writew(NFC_OUTPUT, &host->regs->nfc_config2);
  229. /* Wait for operation to complete */
  230. wait_op_done(host, TROP_US_DELAY, spare_only);
  231. }
  232. /* Request the NANDFC to perform a read of the NAND device ID. */
  233. static void send_read_id(struct mxc_nand_host *host)
  234. {
  235. uint16_t tmp;
  236. /* NANDFC buffer 0 is used for device ID output */
  237. writew(0x0, &host->regs->nfc_buf_addr);
  238. /* Read ID into main buffer */
  239. tmp = readw(&host->regs->nfc_config1);
  240. tmp &= ~NFC_SP_EN;
  241. writew(tmp, &host->regs->nfc_config1);
  242. writew(NFC_ID, &host->regs->nfc_config2);
  243. /* Wait for operation to complete */
  244. wait_op_done(host, TROP_US_DELAY, 0);
  245. }
  246. /*
  247. * This function requests the NANDFC to perform a read of the
  248. * NAND device status and returns the current status.
  249. */
  250. static uint16_t get_dev_status(struct mxc_nand_host *host)
  251. {
  252. void __iomem *main_buf = host->regs->main_area1;
  253. uint32_t store;
  254. uint16_t ret, tmp;
  255. /* Issue status request to NAND device */
  256. /* store the main area1 first word, later do recovery */
  257. store = readl(main_buf);
  258. /* NANDFC buffer 1 is used for device status */
  259. writew(1, &host->regs->nfc_buf_addr);
  260. /* Read status into main buffer */
  261. tmp = readw(&host->regs->nfc_config1);
  262. tmp &= ~NFC_SP_EN;
  263. writew(tmp, &host->regs->nfc_config1);
  264. writew(NFC_STATUS, &host->regs->nfc_config2);
  265. /* Wait for operation to complete */
  266. wait_op_done(host, TROP_US_DELAY, 0);
  267. /*
  268. * Status is placed in first word of main buffer
  269. * get status, then recovery area 1 data
  270. */
  271. ret = readw(main_buf);
  272. writel(store, main_buf);
  273. return ret;
  274. }
  275. /* This function is used by upper layer to checks if device is ready */
  276. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  277. {
  278. /*
  279. * NFC handles R/B internally. Therefore, this function
  280. * always returns status as ready.
  281. */
  282. return 1;
  283. }
  284. #ifdef CONFIG_MXC_NAND_HWECC
  285. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  286. {
  287. /*
  288. * If HW ECC is enabled, we turn it on during init. There is
  289. * no need to enable again here.
  290. */
  291. }
  292. static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  293. u_char *read_ecc, u_char *calc_ecc)
  294. {
  295. struct nand_chip *nand_chip = mtd->priv;
  296. struct mxc_nand_host *host = nand_chip->priv;
  297. /*
  298. * 1-Bit errors are automatically corrected in HW. No need for
  299. * additional correction. 2-Bit errors cannot be corrected by
  300. * HW ECC, so we need to return failure
  301. */
  302. uint16_t ecc_status = readw(&host->regs->nfc_ecc_status_result);
  303. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  304. MTDDEBUG(MTD_DEBUG_LEVEL0,
  305. "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  306. return -1;
  307. }
  308. return 0;
  309. }
  310. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  311. u_char *ecc_code)
  312. {
  313. return 0;
  314. }
  315. #endif
  316. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  317. {
  318. struct nand_chip *nand_chip = mtd->priv;
  319. struct mxc_nand_host *host = nand_chip->priv;
  320. uint8_t ret = 0;
  321. uint16_t col;
  322. uint16_t __iomem *main_buf =
  323. (uint16_t __iomem *)host->regs->main_area0;
  324. uint16_t __iomem *spare_buf =
  325. (uint16_t __iomem *)host->regs->spare_area0;
  326. union {
  327. uint16_t word;
  328. uint8_t bytes[2];
  329. } nfc_word;
  330. /* Check for status request */
  331. if (host->status_request)
  332. return get_dev_status(host) & 0xFF;
  333. /* Get column for 16-bit access */
  334. col = host->col_addr >> 1;
  335. /* If we are accessing the spare region */
  336. if (host->spare_only)
  337. nfc_word.word = readw(&spare_buf[col]);
  338. else
  339. nfc_word.word = readw(&main_buf[col]);
  340. /* Pick upper/lower byte of word from RAM buffer */
  341. ret = nfc_word.bytes[host->col_addr & 0x1];
  342. /* Update saved column address */
  343. if (nand_chip->options & NAND_BUSWIDTH_16)
  344. host->col_addr += 2;
  345. else
  346. host->col_addr++;
  347. return ret;
  348. }
  349. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  350. {
  351. struct nand_chip *nand_chip = mtd->priv;
  352. struct mxc_nand_host *host = nand_chip->priv;
  353. uint16_t col, ret;
  354. uint16_t __iomem *p;
  355. MTDDEBUG(MTD_DEBUG_LEVEL3,
  356. "mxc_nand_read_word(col = %d)\n", host->col_addr);
  357. col = host->col_addr;
  358. /* Adjust saved column address */
  359. if (col < mtd->writesize && host->spare_only)
  360. col += mtd->writesize;
  361. if (col < mtd->writesize) {
  362. p = (uint16_t __iomem *)(host->regs->main_area0 + (col >> 1));
  363. } else {
  364. p = (uint16_t __iomem *)(host->regs->spare_area0 +
  365. ((col - mtd->writesize) >> 1));
  366. }
  367. if (col & 1) {
  368. union {
  369. uint16_t word;
  370. uint8_t bytes[2];
  371. } nfc_word[3];
  372. nfc_word[0].word = readw(p);
  373. nfc_word[1].word = readw(p + 1);
  374. nfc_word[2].bytes[0] = nfc_word[0].bytes[1];
  375. nfc_word[2].bytes[1] = nfc_word[1].bytes[0];
  376. ret = nfc_word[2].word;
  377. } else {
  378. ret = readw(p);
  379. }
  380. /* Update saved column address */
  381. host->col_addr = col + 2;
  382. return ret;
  383. }
  384. /*
  385. * Write data of length len to buffer buf. The data to be
  386. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  387. * Operation by the NFC, the data is written to NAND Flash
  388. */
  389. static void mxc_nand_write_buf(struct mtd_info *mtd,
  390. const u_char *buf, int len)
  391. {
  392. struct nand_chip *nand_chip = mtd->priv;
  393. struct mxc_nand_host *host = nand_chip->priv;
  394. int n, col, i = 0;
  395. MTDDEBUG(MTD_DEBUG_LEVEL3,
  396. "mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
  397. len);
  398. col = host->col_addr;
  399. /* Adjust saved column address */
  400. if (col < mtd->writesize && host->spare_only)
  401. col += mtd->writesize;
  402. n = mtd->writesize + mtd->oobsize - col;
  403. n = min(len, n);
  404. MTDDEBUG(MTD_DEBUG_LEVEL3,
  405. "%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
  406. while (n > 0) {
  407. void __iomem *p;
  408. if (col < mtd->writesize) {
  409. p = host->regs->main_area0 + (col & ~3);
  410. } else {
  411. p = host->regs->spare_area0 -
  412. mtd->writesize + (col & ~3);
  413. }
  414. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s:%d: p = %p\n", __func__,
  415. __LINE__, p);
  416. if (((col | (unsigned long)&buf[i]) & 3) || n < 4) {
  417. union {
  418. uint32_t word;
  419. uint8_t bytes[4];
  420. } nfc_word;
  421. nfc_word.word = readl(p);
  422. nfc_word.bytes[col & 3] = buf[i++];
  423. n--;
  424. col++;
  425. writel(nfc_word.word, p);
  426. } else {
  427. int m = mtd->writesize - col;
  428. if (col >= mtd->writesize)
  429. m += mtd->oobsize;
  430. m = min(n, m) & ~3;
  431. MTDDEBUG(MTD_DEBUG_LEVEL3,
  432. "%s:%d: n = %d, m = %d, i = %d, col = %d\n",
  433. __func__, __LINE__, n, m, i, col);
  434. mxc_nand_memcpy32(p, (uint32_t *)&buf[i], m);
  435. col += m;
  436. i += m;
  437. n -= m;
  438. }
  439. }
  440. /* Update saved column address */
  441. host->col_addr = col;
  442. }
  443. /*
  444. * Read the data buffer from the NAND Flash. To read the data from NAND
  445. * Flash first the data output cycle is initiated by the NFC, which copies
  446. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  447. */
  448. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  449. {
  450. struct nand_chip *nand_chip = mtd->priv;
  451. struct mxc_nand_host *host = nand_chip->priv;
  452. int n, col, i = 0;
  453. MTDDEBUG(MTD_DEBUG_LEVEL3,
  454. "mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr, len);
  455. col = host->col_addr;
  456. /* Adjust saved column address */
  457. if (col < mtd->writesize && host->spare_only)
  458. col += mtd->writesize;
  459. n = mtd->writesize + mtd->oobsize - col;
  460. n = min(len, n);
  461. while (n > 0) {
  462. void __iomem *p;
  463. if (col < mtd->writesize) {
  464. p = host->regs->main_area0 + (col & ~3);
  465. } else {
  466. p = host->regs->spare_area0 -
  467. mtd->writesize + (col & ~3);
  468. }
  469. if (((col | (int)&buf[i]) & 3) || n < 4) {
  470. union {
  471. uint32_t word;
  472. uint8_t bytes[4];
  473. } nfc_word;
  474. nfc_word.word = readl(p);
  475. buf[i++] = nfc_word.bytes[col & 3];
  476. n--;
  477. col++;
  478. } else {
  479. int m = mtd->writesize - col;
  480. if (col >= mtd->writesize)
  481. m += mtd->oobsize;
  482. m = min(n, m) & ~3;
  483. mxc_nand_memcpy32((uint32_t *)&buf[i], p, m);
  484. col += m;
  485. i += m;
  486. n -= m;
  487. }
  488. }
  489. /* Update saved column address */
  490. host->col_addr = col;
  491. }
  492. /*
  493. * Used by the upper layer to verify the data in NAND Flash
  494. * with the data in the buf.
  495. */
  496. static int mxc_nand_verify_buf(struct mtd_info *mtd,
  497. const u_char *buf, int len)
  498. {
  499. u_char tmp[256];
  500. uint bsize;
  501. while (len) {
  502. bsize = min(len, 256);
  503. mxc_nand_read_buf(mtd, tmp, bsize);
  504. if (memcmp(buf, tmp, bsize))
  505. return 1;
  506. buf += bsize;
  507. len -= bsize;
  508. }
  509. return 0;
  510. }
  511. /*
  512. * This function is used by upper layer for select and
  513. * deselect of the NAND chip
  514. */
  515. static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
  516. {
  517. struct nand_chip *nand_chip = mtd->priv;
  518. struct mxc_nand_host *host = nand_chip->priv;
  519. switch (chip) {
  520. case -1:
  521. /* TODO: Disable the NFC clock */
  522. if (host->clk_act)
  523. host->clk_act = 0;
  524. break;
  525. case 0:
  526. /* TODO: Enable the NFC clock */
  527. if (!host->clk_act)
  528. host->clk_act = 1;
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. /*
  535. * Used by the upper layer to write command to NAND Flash for
  536. * different operations to be carried out on NAND Flash
  537. */
  538. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  539. int column, int page_addr)
  540. {
  541. struct nand_chip *nand_chip = mtd->priv;
  542. struct mxc_nand_host *host = nand_chip->priv;
  543. MTDDEBUG(MTD_DEBUG_LEVEL3,
  544. "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  545. command, column, page_addr);
  546. /* Reset command state information */
  547. host->status_request = false;
  548. /* Command pre-processing step */
  549. switch (command) {
  550. case NAND_CMD_STATUS:
  551. host->col_addr = 0;
  552. host->status_request = true;
  553. break;
  554. case NAND_CMD_READ0:
  555. host->col_addr = column;
  556. host->spare_only = false;
  557. break;
  558. case NAND_CMD_READOOB:
  559. host->col_addr = column;
  560. host->spare_only = true;
  561. if (host->pagesize_2k)
  562. command = NAND_CMD_READ0; /* only READ0 is valid */
  563. break;
  564. case NAND_CMD_SEQIN:
  565. if (column >= mtd->writesize) {
  566. /*
  567. * before sending SEQIN command for partial write,
  568. * we need read one page out. FSL NFC does not support
  569. * partial write. It alway send out 512+ecc+512+ecc ...
  570. * for large page nand flash. But for small page nand
  571. * flash, it does support SPARE ONLY operation.
  572. */
  573. if (host->pagesize_2k) {
  574. /* call ourself to read a page */
  575. mxc_nand_command(mtd, NAND_CMD_READ0, 0,
  576. page_addr);
  577. }
  578. host->col_addr = column - mtd->writesize;
  579. host->spare_only = true;
  580. /* Set program pointer to spare region */
  581. if (!host->pagesize_2k)
  582. send_cmd(host, NAND_CMD_READOOB);
  583. } else {
  584. host->spare_only = false;
  585. host->col_addr = column;
  586. /* Set program pointer to page start */
  587. if (!host->pagesize_2k)
  588. send_cmd(host, NAND_CMD_READ0);
  589. }
  590. break;
  591. case NAND_CMD_PAGEPROG:
  592. send_prog_page(host, 0, host->spare_only);
  593. if (host->pagesize_2k) {
  594. /* data in 4 areas datas */
  595. send_prog_page(host, 1, host->spare_only);
  596. send_prog_page(host, 2, host->spare_only);
  597. send_prog_page(host, 3, host->spare_only);
  598. }
  599. break;
  600. }
  601. /* Write out the command to the device. */
  602. send_cmd(host, command);
  603. /* Write out column address, if necessary */
  604. if (column != -1) {
  605. /*
  606. * MXC NANDFC can only perform full page+spare or
  607. * spare-only read/write. When the upper layers
  608. * layers perform a read/write buf operation,
  609. * we will used the saved column adress to index into
  610. * the full page.
  611. */
  612. send_addr(host, 0);
  613. if (host->pagesize_2k)
  614. /* another col addr cycle for 2k page */
  615. send_addr(host, 0);
  616. }
  617. /* Write out page address, if necessary */
  618. if (page_addr != -1) {
  619. /* paddr_0 - p_addr_7 */
  620. send_addr(host, (page_addr & 0xff));
  621. if (host->pagesize_2k) {
  622. send_addr(host, (page_addr >> 8) & 0xFF);
  623. if (mtd->size >= 0x10000000) {
  624. /* paddr_8 - paddr_15 */
  625. send_addr(host, (page_addr >> 8) & 0xff);
  626. send_addr(host, (page_addr >> 16) & 0xff);
  627. } else {
  628. /* paddr_8 - paddr_15 */
  629. send_addr(host, (page_addr >> 8) & 0xff);
  630. }
  631. } else {
  632. /* One more address cycle for higher density devices */
  633. if (mtd->size >= 0x4000000) {
  634. /* paddr_8 - paddr_15 */
  635. send_addr(host, (page_addr >> 8) & 0xff);
  636. send_addr(host, (page_addr >> 16) & 0xff);
  637. } else {
  638. /* paddr_8 - paddr_15 */
  639. send_addr(host, (page_addr >> 8) & 0xff);
  640. }
  641. }
  642. }
  643. /* Command post-processing step */
  644. switch (command) {
  645. case NAND_CMD_RESET:
  646. break;
  647. case NAND_CMD_READOOB:
  648. case NAND_CMD_READ0:
  649. if (host->pagesize_2k) {
  650. /* send read confirm command */
  651. send_cmd(host, NAND_CMD_READSTART);
  652. /* read for each AREA */
  653. send_read_page(host, 0, host->spare_only);
  654. send_read_page(host, 1, host->spare_only);
  655. send_read_page(host, 2, host->spare_only);
  656. send_read_page(host, 3, host->spare_only);
  657. } else {
  658. send_read_page(host, 0, host->spare_only);
  659. }
  660. break;
  661. case NAND_CMD_READID:
  662. host->col_addr = 0;
  663. send_read_id(host);
  664. break;
  665. case NAND_CMD_PAGEPROG:
  666. break;
  667. case NAND_CMD_STATUS:
  668. break;
  669. case NAND_CMD_ERASE2:
  670. break;
  671. }
  672. }
  673. int board_nand_init(struct nand_chip *this)
  674. {
  675. struct system_control_regs *sc_regs =
  676. (struct system_control_regs *)IMX_SYSTEM_CTL_BASE;
  677. struct mtd_info *mtd;
  678. uint16_t tmp;
  679. int err = 0;
  680. /* structures must be linked */
  681. mtd = &host->mtd;
  682. mtd->priv = this;
  683. host->nand = this;
  684. /* 5 us command delay time */
  685. this->chip_delay = 5;
  686. this->priv = host;
  687. this->dev_ready = mxc_nand_dev_ready;
  688. this->cmdfunc = mxc_nand_command;
  689. this->select_chip = mxc_nand_select_chip;
  690. this->read_byte = mxc_nand_read_byte;
  691. this->read_word = mxc_nand_read_word;
  692. this->write_buf = mxc_nand_write_buf;
  693. this->read_buf = mxc_nand_read_buf;
  694. this->verify_buf = mxc_nand_verify_buf;
  695. host->regs = (struct nfc_regs __iomem *)CONFIG_MXC_NAND_REGS_BASE;
  696. host->clk_act = 1;
  697. #ifdef CONFIG_MXC_NAND_HWECC
  698. this->ecc.calculate = mxc_nand_calculate_ecc;
  699. this->ecc.hwctl = mxc_nand_enable_hwecc;
  700. this->ecc.correct = mxc_nand_correct_data;
  701. this->ecc.mode = NAND_ECC_HW;
  702. this->ecc.size = 512;
  703. this->ecc.bytes = 3;
  704. this->ecc.layout = &nand_hw_eccoob;
  705. tmp = readw(&host->regs->nfc_config1);
  706. tmp |= NFC_ECC_EN;
  707. writew(tmp, &host->regs->nfc_config1);
  708. #else
  709. this->ecc.layout = &nand_soft_eccoob;
  710. this->ecc.mode = NAND_ECC_SOFT;
  711. tmp = readw(&host->regs->nfc_config1);
  712. tmp &= ~NFC_ECC_EN;
  713. writew(tmp, &host->regs->nfc_config1);
  714. #endif
  715. /* Reset NAND */
  716. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  717. /*
  718. * preset operation
  719. * Unlock the internal RAM Buffer
  720. */
  721. writew(0x2, &host->regs->nfc_config);
  722. /* Blocks to be unlocked */
  723. writew(0x0, &host->regs->nfc_unlockstart_blkaddr);
  724. writew(0x4000, &host->regs->nfc_unlockend_blkaddr);
  725. /* Unlock Block Command for given address range */
  726. writew(0x4, &host->regs->nfc_wrprot);
  727. /* NAND bus width determines access funtions used by upper layer */
  728. if (readl(&sc_regs->fmcr) & NF_16BIT_SEL)
  729. this->options |= NAND_BUSWIDTH_16;
  730. host->pagesize_2k = 0;
  731. return err;
  732. }