mxc_nand.c 22 KB

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