mxc_nand.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * Copyright 2004-2007 Freescale Semiconductor, Inc.
  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. #ifdef CONFIG_MX27
  134. static int is_16bit_nand(void)
  135. {
  136. struct system_control_regs *sc_regs =
  137. (struct system_control_regs *)IMX_SYSTEM_CTL_BASE;
  138. if (readl(&sc_regs->fmcr) & NF_16BIT_SEL)
  139. return 1;
  140. else
  141. return 0;
  142. }
  143. #elif defined(CONFIG_MX31)
  144. static int is_16bit_nand(void)
  145. {
  146. struct clock_control_regs *sc_regs =
  147. (struct clock_control_regs *)CCM_BASE;
  148. if (readl(&sc_regs->rcsr) & CCM_RCSR_NF16B)
  149. return 1;
  150. else
  151. return 0;
  152. }
  153. #else
  154. #warning "8/16 bit NAND autodetection not supported"
  155. static int is_16bit_nand(void)
  156. {
  157. return 0;
  158. }
  159. #endif
  160. static uint32_t *mxc_nand_memcpy32(uint32_t *dest, uint32_t *source, size_t size)
  161. {
  162. uint32_t *d = dest;
  163. size >>= 2;
  164. while (size--)
  165. __raw_writel(__raw_readl(source++), d++);
  166. return dest;
  167. }
  168. /*
  169. * This function polls the NANDFC to wait for the basic operation to
  170. * complete by checking the INT bit of config2 register.
  171. */
  172. static void wait_op_done(struct mxc_nand_host *host, int max_retries,
  173. uint16_t param)
  174. {
  175. uint32_t tmp;
  176. while (max_retries-- > 0) {
  177. if (readw(&host->regs->nfc_config2) & NFC_INT) {
  178. tmp = readw(&host->regs->nfc_config2);
  179. tmp &= ~NFC_INT;
  180. writew(tmp, &host->regs->nfc_config2);
  181. break;
  182. }
  183. udelay(1);
  184. }
  185. if (max_retries < 0) {
  186. MTDDEBUG(MTD_DEBUG_LEVEL0, "%s(%d): INT not set\n",
  187. __func__, param);
  188. }
  189. }
  190. /*
  191. * This function issues the specified command to the NAND device and
  192. * waits for completion.
  193. */
  194. static void send_cmd(struct mxc_nand_host *host, uint16_t cmd)
  195. {
  196. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x)\n", cmd);
  197. writew(cmd, &host->regs->nfc_flash_cmd);
  198. writew(NFC_CMD, &host->regs->nfc_config2);
  199. /* Wait for operation to complete */
  200. wait_op_done(host, TROP_US_DELAY, cmd);
  201. }
  202. /*
  203. * This function sends an address (or partial address) to the
  204. * NAND device. The address is used to select the source/destination for
  205. * a NAND command.
  206. */
  207. static void send_addr(struct mxc_nand_host *host, uint16_t addr)
  208. {
  209. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x)\n", addr);
  210. writew(addr, &host->regs->nfc_flash_addr);
  211. writew(NFC_ADDR, &host->regs->nfc_config2);
  212. /* Wait for operation to complete */
  213. wait_op_done(host, TROP_US_DELAY, addr);
  214. }
  215. /*
  216. * This function requests the NANDFC to initate the transfer
  217. * of data currently in the NANDFC RAM buffer to the NAND device.
  218. */
  219. static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
  220. int spare_only)
  221. {
  222. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_prog_page (%d)\n", spare_only);
  223. writew(buf_id, &host->regs->nfc_buf_addr);
  224. /* Configure spare or page+spare access */
  225. if (!host->pagesize_2k) {
  226. uint16_t config1 = readw(&host->regs->nfc_config1);
  227. if (spare_only)
  228. config1 |= NFC_SP_EN;
  229. else
  230. config1 &= ~(NFC_SP_EN);
  231. writew(config1, &host->regs->nfc_config1);
  232. }
  233. writew(NFC_INPUT, &host->regs->nfc_config2);
  234. /* Wait for operation to complete */
  235. wait_op_done(host, TROP_US_DELAY, spare_only);
  236. }
  237. /*
  238. * Requests NANDFC to initated the transfer of data from the
  239. * NAND device into in the NANDFC ram buffer.
  240. */
  241. static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
  242. int spare_only)
  243. {
  244. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_read_page (%d)\n", spare_only);
  245. writew(buf_id, &host->regs->nfc_buf_addr);
  246. /* Configure spare or page+spare access */
  247. if (!host->pagesize_2k) {
  248. uint32_t config1 = readw(&host->regs->nfc_config1);
  249. if (spare_only)
  250. config1 |= NFC_SP_EN;
  251. else
  252. config1 &= ~NFC_SP_EN;
  253. writew(config1, &host->regs->nfc_config1);
  254. }
  255. writew(NFC_OUTPUT, &host->regs->nfc_config2);
  256. /* Wait for operation to complete */
  257. wait_op_done(host, TROP_US_DELAY, spare_only);
  258. }
  259. /* Request the NANDFC to perform a read of the NAND device ID. */
  260. static void send_read_id(struct mxc_nand_host *host)
  261. {
  262. uint16_t tmp;
  263. /* NANDFC buffer 0 is used for device ID output */
  264. writew(0x0, &host->regs->nfc_buf_addr);
  265. /* Read ID into main buffer */
  266. tmp = readw(&host->regs->nfc_config1);
  267. tmp &= ~NFC_SP_EN;
  268. writew(tmp, &host->regs->nfc_config1);
  269. writew(NFC_ID, &host->regs->nfc_config2);
  270. /* Wait for operation to complete */
  271. wait_op_done(host, TROP_US_DELAY, 0);
  272. }
  273. /*
  274. * This function requests the NANDFC to perform a read of the
  275. * NAND device status and returns the current status.
  276. */
  277. static uint16_t get_dev_status(struct mxc_nand_host *host)
  278. {
  279. void __iomem *main_buf = host->regs->main_area1;
  280. uint32_t store;
  281. uint16_t ret, tmp;
  282. /* Issue status request to NAND device */
  283. /* store the main area1 first word, later do recovery */
  284. store = readl(main_buf);
  285. /* NANDFC buffer 1 is used for device status */
  286. writew(1, &host->regs->nfc_buf_addr);
  287. /* Read status into main buffer */
  288. tmp = readw(&host->regs->nfc_config1);
  289. tmp &= ~NFC_SP_EN;
  290. writew(tmp, &host->regs->nfc_config1);
  291. writew(NFC_STATUS, &host->regs->nfc_config2);
  292. /* Wait for operation to complete */
  293. wait_op_done(host, TROP_US_DELAY, 0);
  294. /*
  295. * Status is placed in first word of main buffer
  296. * get status, then recovery area 1 data
  297. */
  298. ret = readw(main_buf);
  299. writel(store, main_buf);
  300. return ret;
  301. }
  302. /* This function is used by upper layer to checks if device is ready */
  303. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  304. {
  305. /*
  306. * NFC handles R/B internally. Therefore, this function
  307. * always returns status as ready.
  308. */
  309. return 1;
  310. }
  311. #ifdef CONFIG_MXC_NAND_HWECC
  312. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  313. {
  314. /*
  315. * If HW ECC is enabled, we turn it on during init. There is
  316. * no need to enable again here.
  317. */
  318. }
  319. static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  320. u_char *read_ecc, u_char *calc_ecc)
  321. {
  322. struct nand_chip *nand_chip = mtd->priv;
  323. struct mxc_nand_host *host = nand_chip->priv;
  324. /*
  325. * 1-Bit errors are automatically corrected in HW. No need for
  326. * additional correction. 2-Bit errors cannot be corrected by
  327. * HW ECC, so we need to return failure
  328. */
  329. uint16_t ecc_status = readw(&host->regs->nfc_ecc_status_result);
  330. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  331. MTDDEBUG(MTD_DEBUG_LEVEL0,
  332. "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  333. return -1;
  334. }
  335. return 0;
  336. }
  337. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  338. u_char *ecc_code)
  339. {
  340. return 0;
  341. }
  342. #endif
  343. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  344. {
  345. struct nand_chip *nand_chip = mtd->priv;
  346. struct mxc_nand_host *host = nand_chip->priv;
  347. uint8_t ret = 0;
  348. uint16_t col;
  349. uint16_t __iomem *main_buf =
  350. (uint16_t __iomem *)host->regs->main_area0;
  351. uint16_t __iomem *spare_buf =
  352. (uint16_t __iomem *)host->regs->spare_area0;
  353. union {
  354. uint16_t word;
  355. uint8_t bytes[2];
  356. } nfc_word;
  357. /* Check for status request */
  358. if (host->status_request)
  359. return get_dev_status(host) & 0xFF;
  360. /* Get column for 16-bit access */
  361. col = host->col_addr >> 1;
  362. /* If we are accessing the spare region */
  363. if (host->spare_only)
  364. nfc_word.word = readw(&spare_buf[col]);
  365. else
  366. nfc_word.word = readw(&main_buf[col]);
  367. /* Pick upper/lower byte of word from RAM buffer */
  368. ret = nfc_word.bytes[host->col_addr & 0x1];
  369. /* Update saved column address */
  370. if (nand_chip->options & NAND_BUSWIDTH_16)
  371. host->col_addr += 2;
  372. else
  373. host->col_addr++;
  374. return ret;
  375. }
  376. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  377. {
  378. struct nand_chip *nand_chip = mtd->priv;
  379. struct mxc_nand_host *host = nand_chip->priv;
  380. uint16_t col, ret;
  381. uint16_t __iomem *p;
  382. MTDDEBUG(MTD_DEBUG_LEVEL3,
  383. "mxc_nand_read_word(col = %d)\n", host->col_addr);
  384. col = host->col_addr;
  385. /* Adjust saved column address */
  386. if (col < mtd->writesize && host->spare_only)
  387. col += mtd->writesize;
  388. if (col < mtd->writesize) {
  389. p = (uint16_t __iomem *)(host->regs->main_area0 + (col >> 1));
  390. } else {
  391. p = (uint16_t __iomem *)(host->regs->spare_area0 +
  392. ((col - mtd->writesize) >> 1));
  393. }
  394. if (col & 1) {
  395. union {
  396. uint16_t word;
  397. uint8_t bytes[2];
  398. } nfc_word[3];
  399. nfc_word[0].word = readw(p);
  400. nfc_word[1].word = readw(p + 1);
  401. nfc_word[2].bytes[0] = nfc_word[0].bytes[1];
  402. nfc_word[2].bytes[1] = nfc_word[1].bytes[0];
  403. ret = nfc_word[2].word;
  404. } else {
  405. ret = readw(p);
  406. }
  407. /* Update saved column address */
  408. host->col_addr = col + 2;
  409. return ret;
  410. }
  411. /*
  412. * Write data of length len to buffer buf. The data to be
  413. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  414. * Operation by the NFC, the data is written to NAND Flash
  415. */
  416. static void mxc_nand_write_buf(struct mtd_info *mtd,
  417. const u_char *buf, int len)
  418. {
  419. struct nand_chip *nand_chip = mtd->priv;
  420. struct mxc_nand_host *host = nand_chip->priv;
  421. int n, col, i = 0;
  422. MTDDEBUG(MTD_DEBUG_LEVEL3,
  423. "mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
  424. len);
  425. col = host->col_addr;
  426. /* Adjust saved column address */
  427. if (col < mtd->writesize && host->spare_only)
  428. col += mtd->writesize;
  429. n = mtd->writesize + mtd->oobsize - col;
  430. n = min(len, n);
  431. MTDDEBUG(MTD_DEBUG_LEVEL3,
  432. "%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
  433. while (n > 0) {
  434. void __iomem *p;
  435. if (col < mtd->writesize) {
  436. p = host->regs->main_area0 + (col & ~3);
  437. } else {
  438. p = host->regs->spare_area0 -
  439. mtd->writesize + (col & ~3);
  440. }
  441. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s:%d: p = %p\n", __func__,
  442. __LINE__, p);
  443. if (((col | (unsigned long)&buf[i]) & 3) || n < 4) {
  444. union {
  445. uint32_t word;
  446. uint8_t bytes[4];
  447. } nfc_word;
  448. nfc_word.word = readl(p);
  449. nfc_word.bytes[col & 3] = buf[i++];
  450. n--;
  451. col++;
  452. writel(nfc_word.word, p);
  453. } else {
  454. int m = mtd->writesize - col;
  455. if (col >= mtd->writesize)
  456. m += mtd->oobsize;
  457. m = min(n, m) & ~3;
  458. MTDDEBUG(MTD_DEBUG_LEVEL3,
  459. "%s:%d: n = %d, m = %d, i = %d, col = %d\n",
  460. __func__, __LINE__, n, m, i, col);
  461. mxc_nand_memcpy32(p, (uint32_t *)&buf[i], m);
  462. col += m;
  463. i += m;
  464. n -= m;
  465. }
  466. }
  467. /* Update saved column address */
  468. host->col_addr = col;
  469. }
  470. /*
  471. * Read the data buffer from the NAND Flash. To read the data from NAND
  472. * Flash first the data output cycle is initiated by the NFC, which copies
  473. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  474. */
  475. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  476. {
  477. struct nand_chip *nand_chip = mtd->priv;
  478. struct mxc_nand_host *host = nand_chip->priv;
  479. int n, col, i = 0;
  480. MTDDEBUG(MTD_DEBUG_LEVEL3,
  481. "mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr, len);
  482. col = host->col_addr;
  483. /* Adjust saved column address */
  484. if (col < mtd->writesize && host->spare_only)
  485. col += mtd->writesize;
  486. n = mtd->writesize + mtd->oobsize - col;
  487. n = min(len, n);
  488. while (n > 0) {
  489. void __iomem *p;
  490. if (col < mtd->writesize) {
  491. p = host->regs->main_area0 + (col & ~3);
  492. } else {
  493. p = host->regs->spare_area0 -
  494. mtd->writesize + (col & ~3);
  495. }
  496. if (((col | (int)&buf[i]) & 3) || n < 4) {
  497. union {
  498. uint32_t word;
  499. uint8_t bytes[4];
  500. } nfc_word;
  501. nfc_word.word = readl(p);
  502. buf[i++] = nfc_word.bytes[col & 3];
  503. n--;
  504. col++;
  505. } else {
  506. int m = mtd->writesize - col;
  507. if (col >= mtd->writesize)
  508. m += mtd->oobsize;
  509. m = min(n, m) & ~3;
  510. mxc_nand_memcpy32((uint32_t *)&buf[i], p, m);
  511. col += m;
  512. i += m;
  513. n -= m;
  514. }
  515. }
  516. /* Update saved column address */
  517. host->col_addr = col;
  518. }
  519. /*
  520. * Used by the upper layer to verify the data in NAND Flash
  521. * with the data in the buf.
  522. */
  523. static int mxc_nand_verify_buf(struct mtd_info *mtd,
  524. const u_char *buf, int len)
  525. {
  526. u_char tmp[256];
  527. uint bsize;
  528. while (len) {
  529. bsize = min(len, 256);
  530. mxc_nand_read_buf(mtd, tmp, bsize);
  531. if (memcmp(buf, tmp, bsize))
  532. return 1;
  533. buf += bsize;
  534. len -= bsize;
  535. }
  536. return 0;
  537. }
  538. /*
  539. * This function is used by upper layer for select and
  540. * deselect of the NAND chip
  541. */
  542. static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
  543. {
  544. struct nand_chip *nand_chip = mtd->priv;
  545. struct mxc_nand_host *host = nand_chip->priv;
  546. switch (chip) {
  547. case -1:
  548. /* TODO: Disable the NFC clock */
  549. if (host->clk_act)
  550. host->clk_act = 0;
  551. break;
  552. case 0:
  553. /* TODO: Enable the NFC clock */
  554. if (!host->clk_act)
  555. host->clk_act = 1;
  556. break;
  557. default:
  558. break;
  559. }
  560. }
  561. /*
  562. * Used by the upper layer to write command to NAND Flash for
  563. * different operations to be carried out on NAND Flash
  564. */
  565. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  566. int column, int page_addr)
  567. {
  568. struct nand_chip *nand_chip = mtd->priv;
  569. struct mxc_nand_host *host = nand_chip->priv;
  570. MTDDEBUG(MTD_DEBUG_LEVEL3,
  571. "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  572. command, column, page_addr);
  573. /* Reset command state information */
  574. host->status_request = false;
  575. /* Command pre-processing step */
  576. switch (command) {
  577. case NAND_CMD_STATUS:
  578. host->col_addr = 0;
  579. host->status_request = true;
  580. break;
  581. case NAND_CMD_READ0:
  582. host->col_addr = column;
  583. host->spare_only = false;
  584. break;
  585. case NAND_CMD_READOOB:
  586. host->col_addr = column;
  587. host->spare_only = true;
  588. if (host->pagesize_2k)
  589. command = NAND_CMD_READ0; /* only READ0 is valid */
  590. break;
  591. case NAND_CMD_SEQIN:
  592. if (column >= mtd->writesize) {
  593. /*
  594. * before sending SEQIN command for partial write,
  595. * we need read one page out. FSL NFC does not support
  596. * partial write. It alway send out 512+ecc+512+ecc ...
  597. * for large page nand flash. But for small page nand
  598. * flash, it does support SPARE ONLY operation.
  599. */
  600. if (host->pagesize_2k) {
  601. /* call ourself to read a page */
  602. mxc_nand_command(mtd, NAND_CMD_READ0, 0,
  603. page_addr);
  604. }
  605. host->col_addr = column - mtd->writesize;
  606. host->spare_only = true;
  607. /* Set program pointer to spare region */
  608. if (!host->pagesize_2k)
  609. send_cmd(host, NAND_CMD_READOOB);
  610. } else {
  611. host->spare_only = false;
  612. host->col_addr = column;
  613. /* Set program pointer to page start */
  614. if (!host->pagesize_2k)
  615. send_cmd(host, NAND_CMD_READ0);
  616. }
  617. break;
  618. case NAND_CMD_PAGEPROG:
  619. send_prog_page(host, 0, host->spare_only);
  620. if (host->pagesize_2k) {
  621. /* data in 4 areas datas */
  622. send_prog_page(host, 1, host->spare_only);
  623. send_prog_page(host, 2, host->spare_only);
  624. send_prog_page(host, 3, host->spare_only);
  625. }
  626. break;
  627. }
  628. /* Write out the command to the device. */
  629. send_cmd(host, command);
  630. /* Write out column address, if necessary */
  631. if (column != -1) {
  632. /*
  633. * MXC NANDFC can only perform full page+spare or
  634. * spare-only read/write. When the upper layers
  635. * layers perform a read/write buf operation,
  636. * we will used the saved column adress to index into
  637. * the full page.
  638. */
  639. send_addr(host, 0);
  640. if (host->pagesize_2k)
  641. /* another col addr cycle for 2k page */
  642. send_addr(host, 0);
  643. }
  644. /* Write out page address, if necessary */
  645. if (page_addr != -1) {
  646. /* paddr_0 - p_addr_7 */
  647. send_addr(host, (page_addr & 0xff));
  648. if (host->pagesize_2k) {
  649. send_addr(host, (page_addr >> 8) & 0xFF);
  650. if (mtd->size >= 0x10000000) {
  651. /* paddr_8 - paddr_15 */
  652. send_addr(host, (page_addr >> 8) & 0xff);
  653. send_addr(host, (page_addr >> 16) & 0xff);
  654. } else {
  655. /* paddr_8 - paddr_15 */
  656. send_addr(host, (page_addr >> 8) & 0xff);
  657. }
  658. } else {
  659. /* One more address cycle for higher density devices */
  660. if (mtd->size >= 0x4000000) {
  661. /* paddr_8 - paddr_15 */
  662. send_addr(host, (page_addr >> 8) & 0xff);
  663. send_addr(host, (page_addr >> 16) & 0xff);
  664. } else {
  665. /* paddr_8 - paddr_15 */
  666. send_addr(host, (page_addr >> 8) & 0xff);
  667. }
  668. }
  669. }
  670. /* Command post-processing step */
  671. switch (command) {
  672. case NAND_CMD_RESET:
  673. break;
  674. case NAND_CMD_READOOB:
  675. case NAND_CMD_READ0:
  676. if (host->pagesize_2k) {
  677. /* send read confirm command */
  678. send_cmd(host, NAND_CMD_READSTART);
  679. /* read for each AREA */
  680. send_read_page(host, 0, host->spare_only);
  681. send_read_page(host, 1, host->spare_only);
  682. send_read_page(host, 2, host->spare_only);
  683. send_read_page(host, 3, host->spare_only);
  684. } else {
  685. send_read_page(host, 0, host->spare_only);
  686. }
  687. break;
  688. case NAND_CMD_READID:
  689. host->col_addr = 0;
  690. send_read_id(host);
  691. break;
  692. case NAND_CMD_PAGEPROG:
  693. break;
  694. case NAND_CMD_STATUS:
  695. break;
  696. case NAND_CMD_ERASE2:
  697. break;
  698. }
  699. }
  700. int board_nand_init(struct nand_chip *this)
  701. {
  702. struct mtd_info *mtd;
  703. uint16_t tmp;
  704. int err = 0;
  705. /* structures must be linked */
  706. mtd = &host->mtd;
  707. mtd->priv = this;
  708. host->nand = this;
  709. /* 5 us command delay time */
  710. this->chip_delay = 5;
  711. this->priv = host;
  712. this->dev_ready = mxc_nand_dev_ready;
  713. this->cmdfunc = mxc_nand_command;
  714. this->select_chip = mxc_nand_select_chip;
  715. this->read_byte = mxc_nand_read_byte;
  716. this->read_word = mxc_nand_read_word;
  717. this->write_buf = mxc_nand_write_buf;
  718. this->read_buf = mxc_nand_read_buf;
  719. this->verify_buf = mxc_nand_verify_buf;
  720. host->regs = (struct nfc_regs __iomem *)CONFIG_MXC_NAND_REGS_BASE;
  721. host->clk_act = 1;
  722. #ifdef CONFIG_MXC_NAND_HWECC
  723. this->ecc.calculate = mxc_nand_calculate_ecc;
  724. this->ecc.hwctl = mxc_nand_enable_hwecc;
  725. this->ecc.correct = mxc_nand_correct_data;
  726. this->ecc.mode = NAND_ECC_HW;
  727. this->ecc.size = 512;
  728. this->ecc.bytes = 3;
  729. this->ecc.layout = &nand_hw_eccoob;
  730. tmp = readw(&host->regs->nfc_config1);
  731. tmp |= NFC_ECC_EN;
  732. writew(tmp, &host->regs->nfc_config1);
  733. #else
  734. this->ecc.layout = &nand_soft_eccoob;
  735. this->ecc.mode = NAND_ECC_SOFT;
  736. tmp = readw(&host->regs->nfc_config1);
  737. tmp &= ~NFC_ECC_EN;
  738. writew(tmp, &host->regs->nfc_config1);
  739. #endif
  740. /* Reset NAND */
  741. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  742. /*
  743. * preset operation
  744. * Unlock the internal RAM Buffer
  745. */
  746. writew(0x2, &host->regs->nfc_config);
  747. /* Blocks to be unlocked */
  748. writew(0x0, &host->regs->nfc_unlockstart_blkaddr);
  749. writew(0x4000, &host->regs->nfc_unlockend_blkaddr);
  750. /* Unlock Block Command for given address range */
  751. writew(0x4, &host->regs->nfc_wrprot);
  752. /* NAND bus width determines access funtions used by upper layer */
  753. if (is_16bit_nand())
  754. this->options |= NAND_BUSWIDTH_16;
  755. host->pagesize_2k = 0;
  756. return err;
  757. }