mxc_nand.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX35)
  25. #include <asm/arch/imx-regs.h>
  26. #endif
  27. #include <fsl_nfc.h>
  28. #define DRIVER_NAME "mxc_nand"
  29. typedef enum {false, true} bool;
  30. struct mxc_nand_host {
  31. struct mtd_info mtd;
  32. struct nand_chip *nand;
  33. struct fsl_nfc_regs __iomem *regs;
  34. int spare_only;
  35. int status_request;
  36. int pagesize_2k;
  37. int clk_act;
  38. uint16_t col_addr;
  39. unsigned int page_addr;
  40. };
  41. static struct mxc_nand_host mxc_host;
  42. static struct mxc_nand_host *host = &mxc_host;
  43. /* Define delays in microsec for NAND device operations */
  44. #define TROP_US_DELAY 2000
  45. /* Macros to get byte and bit positions of ECC */
  46. #define COLPOS(x) ((x) >> 3)
  47. #define BITPOS(x) ((x) & 0xf)
  48. /* Define single bit Error positions in Main & Spare area */
  49. #define MAIN_SINGLEBIT_ERROR 0x4
  50. #define SPARE_SINGLEBIT_ERROR 0x1
  51. /* OOB placement block for use with hardware ecc generation */
  52. #if defined(MXC_NFC_V1)
  53. #ifndef CONFIG_SYS_NAND_LARGEPAGE
  54. static struct nand_ecclayout nand_hw_eccoob = {
  55. .eccbytes = 5,
  56. .eccpos = {6, 7, 8, 9, 10},
  57. .oobfree = { {0, 5}, {11, 5}, }
  58. };
  59. #else
  60. static struct nand_ecclayout nand_hw_eccoob2k = {
  61. .eccbytes = 20,
  62. .eccpos = {
  63. 6, 7, 8, 9, 10,
  64. 22, 23, 24, 25, 26,
  65. 38, 39, 40, 41, 42,
  66. 54, 55, 56, 57, 58,
  67. },
  68. .oobfree = { {2, 4}, {11, 11}, {27, 11}, {43, 11}, {59, 5} },
  69. };
  70. #endif
  71. #elif defined(MXC_NFC_V2_1)
  72. #ifndef CONFIG_SYS_NAND_LARGEPAGE
  73. static struct nand_ecclayout nand_hw_eccoob = {
  74. .eccbytes = 9,
  75. .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
  76. .oobfree = { {2, 5} }
  77. };
  78. #else
  79. static struct nand_ecclayout nand_hw_eccoob2k = {
  80. .eccbytes = 36,
  81. .eccpos = {
  82. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  83. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  84. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  85. 55, 56, 57, 58, 59, 60, 61, 62, 63,
  86. },
  87. .oobfree = { {2, 5}, {16, 7}, {32, 7}, {48, 7} },
  88. };
  89. #endif
  90. #endif
  91. static int is_16bit_nand(void)
  92. {
  93. #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
  94. return 1;
  95. #else
  96. return 0;
  97. #endif
  98. }
  99. static uint32_t *mxc_nand_memcpy32(uint32_t *dest, uint32_t *source, size_t size)
  100. {
  101. uint32_t *d = dest;
  102. size >>= 2;
  103. while (size--)
  104. __raw_writel(__raw_readl(source++), d++);
  105. return dest;
  106. }
  107. /*
  108. * This function polls the NANDFC to wait for the basic operation to
  109. * complete by checking the INT bit of config2 register.
  110. */
  111. static void wait_op_done(struct mxc_nand_host *host, int max_retries,
  112. uint16_t param)
  113. {
  114. uint32_t tmp;
  115. while (max_retries-- > 0) {
  116. if (readw(&host->regs->config2) & NFC_INT) {
  117. tmp = readw(&host->regs->config2);
  118. tmp &= ~NFC_INT;
  119. writew(tmp, &host->regs->config2);
  120. break;
  121. }
  122. udelay(1);
  123. }
  124. if (max_retries < 0) {
  125. MTDDEBUG(MTD_DEBUG_LEVEL0, "%s(%d): INT not set\n",
  126. __func__, param);
  127. }
  128. }
  129. /*
  130. * This function issues the specified command to the NAND device and
  131. * waits for completion.
  132. */
  133. static void send_cmd(struct mxc_nand_host *host, uint16_t cmd)
  134. {
  135. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x)\n", cmd);
  136. writew(cmd, &host->regs->flash_cmd);
  137. writew(NFC_CMD, &host->regs->config2);
  138. /* Wait for operation to complete */
  139. wait_op_done(host, TROP_US_DELAY, cmd);
  140. }
  141. /*
  142. * This function sends an address (or partial address) to the
  143. * NAND device. The address is used to select the source/destination for
  144. * a NAND command.
  145. */
  146. static void send_addr(struct mxc_nand_host *host, uint16_t addr)
  147. {
  148. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x)\n", addr);
  149. writew(addr, &host->regs->flash_addr);
  150. writew(NFC_ADDR, &host->regs->config2);
  151. /* Wait for operation to complete */
  152. wait_op_done(host, TROP_US_DELAY, addr);
  153. }
  154. /*
  155. * This function requests the NANDFC to initiate the transfer
  156. * of data currently in the NANDFC RAM buffer to the NAND device.
  157. */
  158. static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
  159. int spare_only)
  160. {
  161. if (spare_only)
  162. MTDDEBUG(MTD_DEBUG_LEVEL1, "send_prog_page (%d)\n", spare_only);
  163. if (is_mxc_nfc_21()) {
  164. int i;
  165. /*
  166. * The controller copies the 64 bytes of spare data from
  167. * the first 16 bytes of each of the 4 64 byte spare buffers.
  168. * Copy the contiguous data starting in spare_area[0] to
  169. * the four spare area buffers.
  170. */
  171. for (i = 1; i < 4; i++) {
  172. void __iomem *src = &host->regs->spare_area[0][i * 16];
  173. void __iomem *dst = &host->regs->spare_area[i][0];
  174. mxc_nand_memcpy32(dst, src, 16);
  175. }
  176. }
  177. writew(buf_id, &host->regs->buf_addr);
  178. /* Configure spare or page+spare access */
  179. if (!host->pagesize_2k) {
  180. uint16_t config1 = readw(&host->regs->config1);
  181. if (spare_only)
  182. config1 |= NFC_SP_EN;
  183. else
  184. config1 &= ~NFC_SP_EN;
  185. writew(config1, &host->regs->config1);
  186. }
  187. writew(NFC_INPUT, &host->regs->config2);
  188. /* Wait for operation to complete */
  189. wait_op_done(host, TROP_US_DELAY, spare_only);
  190. }
  191. /*
  192. * Requests NANDFC to initiate the transfer of data from the
  193. * NAND device into in the NANDFC ram buffer.
  194. */
  195. static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
  196. int spare_only)
  197. {
  198. MTDDEBUG(MTD_DEBUG_LEVEL3, "send_read_page (%d)\n", spare_only);
  199. writew(buf_id, &host->regs->buf_addr);
  200. /* Configure spare or page+spare access */
  201. if (!host->pagesize_2k) {
  202. uint32_t config1 = readw(&host->regs->config1);
  203. if (spare_only)
  204. config1 |= NFC_SP_EN;
  205. else
  206. config1 &= ~NFC_SP_EN;
  207. writew(config1, &host->regs->config1);
  208. }
  209. writew(NFC_OUTPUT, &host->regs->config2);
  210. /* Wait for operation to complete */
  211. wait_op_done(host, TROP_US_DELAY, spare_only);
  212. if (is_mxc_nfc_21()) {
  213. int i;
  214. /*
  215. * The controller copies the 64 bytes of spare data to
  216. * the first 16 bytes of each of the 4 spare buffers.
  217. * Make the data contiguous starting in spare_area[0].
  218. */
  219. for (i = 1; i < 4; i++) {
  220. void __iomem *src = &host->regs->spare_area[i][0];
  221. void __iomem *dst = &host->regs->spare_area[0][i * 16];
  222. mxc_nand_memcpy32(dst, src, 16);
  223. }
  224. }
  225. }
  226. /* Request the NANDFC to perform a read of the NAND device ID. */
  227. static void send_read_id(struct mxc_nand_host *host)
  228. {
  229. uint16_t tmp;
  230. /* NANDFC buffer 0 is used for device ID output */
  231. writew(0x0, &host->regs->buf_addr);
  232. /* Read ID into main buffer */
  233. tmp = readw(&host->regs->config1);
  234. tmp &= ~NFC_SP_EN;
  235. writew(tmp, &host->regs->config1);
  236. writew(NFC_ID, &host->regs->config2);
  237. /* Wait for operation to complete */
  238. wait_op_done(host, TROP_US_DELAY, 0);
  239. }
  240. /*
  241. * This function requests the NANDFC to perform a read of the
  242. * NAND device status and returns the current status.
  243. */
  244. static uint16_t get_dev_status(struct mxc_nand_host *host)
  245. {
  246. void __iomem *main_buf = host->regs->main_area[1];
  247. uint32_t store;
  248. uint16_t ret, tmp;
  249. /* Issue status request to NAND device */
  250. /* store the main area1 first word, later do recovery */
  251. store = readl(main_buf);
  252. /* NANDFC buffer 1 is used for device status */
  253. writew(1, &host->regs->buf_addr);
  254. /* Read status into main buffer */
  255. tmp = readw(&host->regs->config1);
  256. tmp &= ~NFC_SP_EN;
  257. writew(tmp, &host->regs->config1);
  258. writew(NFC_STATUS, &host->regs->config2);
  259. /* Wait for operation to complete */
  260. wait_op_done(host, TROP_US_DELAY, 0);
  261. /*
  262. * Status is placed in first word of main buffer
  263. * get status, then recovery area 1 data
  264. */
  265. ret = readw(main_buf);
  266. writel(store, main_buf);
  267. return ret;
  268. }
  269. /* This function is used by upper layer to checks if device is ready */
  270. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  271. {
  272. /*
  273. * NFC handles R/B internally. Therefore, this function
  274. * always returns status as ready.
  275. */
  276. return 1;
  277. }
  278. static void _mxc_nand_enable_hwecc(struct mtd_info *mtd, int on)
  279. {
  280. struct nand_chip *nand_chip = mtd->priv;
  281. struct mxc_nand_host *host = nand_chip->priv;
  282. uint16_t tmp = readw(&host->regs->config1);
  283. if (on)
  284. tmp |= NFC_ECC_EN;
  285. else
  286. tmp &= ~NFC_ECC_EN;
  287. writew(tmp, &host->regs->config1);
  288. }
  289. #ifdef CONFIG_MXC_NAND_HWECC
  290. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  291. {
  292. /*
  293. * If HW ECC is enabled, we turn it on during init. There is
  294. * no need to enable again here.
  295. */
  296. }
  297. #ifdef MXC_NFC_V2_1
  298. static int mxc_nand_read_oob_syndrome(struct mtd_info *mtd,
  299. struct nand_chip *chip,
  300. int page, int sndcmd)
  301. {
  302. struct mxc_nand_host *host = chip->priv;
  303. uint8_t *buf = chip->oob_poi;
  304. int length = mtd->oobsize;
  305. int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  306. uint8_t *bufpoi = buf;
  307. int i, toread;
  308. MTDDEBUG(MTD_DEBUG_LEVEL0,
  309. "%s: Reading OOB area of page %u to oob %p\n",
  310. __FUNCTION__, host->page_addr, buf);
  311. chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page);
  312. for (i = 0; i < chip->ecc.steps; i++) {
  313. toread = min_t(int, length, chip->ecc.prepad);
  314. if (toread) {
  315. chip->read_buf(mtd, bufpoi, toread);
  316. bufpoi += toread;
  317. length -= toread;
  318. }
  319. bufpoi += chip->ecc.bytes;
  320. host->col_addr += chip->ecc.bytes;
  321. length -= chip->ecc.bytes;
  322. toread = min_t(int, length, chip->ecc.postpad);
  323. if (toread) {
  324. chip->read_buf(mtd, bufpoi, toread);
  325. bufpoi += toread;
  326. length -= toread;
  327. }
  328. }
  329. if (length > 0)
  330. chip->read_buf(mtd, bufpoi, length);
  331. _mxc_nand_enable_hwecc(mtd, 0);
  332. chip->cmdfunc(mtd, NAND_CMD_READOOB,
  333. mtd->writesize + chip->ecc.prepad, page);
  334. bufpoi = buf + chip->ecc.prepad;
  335. length = mtd->oobsize - chip->ecc.prepad;
  336. for (i = 0; i < chip->ecc.steps; i++) {
  337. toread = min_t(int, length, chip->ecc.bytes);
  338. chip->read_buf(mtd, bufpoi, toread);
  339. bufpoi += eccpitch;
  340. length -= eccpitch;
  341. host->col_addr += chip->ecc.postpad + chip->ecc.prepad;
  342. }
  343. _mxc_nand_enable_hwecc(mtd, 1);
  344. return 1;
  345. }
  346. static int mxc_nand_read_page_raw_syndrome(struct mtd_info *mtd,
  347. struct nand_chip *chip,
  348. uint8_t *buf,
  349. int page)
  350. {
  351. struct mxc_nand_host *host = chip->priv;
  352. int eccsize = chip->ecc.size;
  353. int eccbytes = chip->ecc.bytes;
  354. int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
  355. uint8_t *oob = chip->oob_poi;
  356. int steps, size;
  357. int n;
  358. _mxc_nand_enable_hwecc(mtd, 0);
  359. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, host->page_addr);
  360. for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
  361. host->col_addr = n * eccsize;
  362. chip->read_buf(mtd, buf, eccsize);
  363. buf += eccsize;
  364. host->col_addr = mtd->writesize + n * eccpitch;
  365. if (chip->ecc.prepad) {
  366. chip->read_buf(mtd, oob, chip->ecc.prepad);
  367. oob += chip->ecc.prepad;
  368. }
  369. chip->read_buf(mtd, oob, eccbytes);
  370. oob += eccbytes;
  371. if (chip->ecc.postpad) {
  372. chip->read_buf(mtd, oob, chip->ecc.postpad);
  373. oob += chip->ecc.postpad;
  374. }
  375. }
  376. size = mtd->oobsize - (oob - chip->oob_poi);
  377. if (size)
  378. chip->read_buf(mtd, oob, size);
  379. _mxc_nand_enable_hwecc(mtd, 1);
  380. return 0;
  381. }
  382. static int mxc_nand_read_page_syndrome(struct mtd_info *mtd,
  383. struct nand_chip *chip,
  384. uint8_t *buf,
  385. int page)
  386. {
  387. struct mxc_nand_host *host = chip->priv;
  388. int n, eccsize = chip->ecc.size;
  389. int eccbytes = chip->ecc.bytes;
  390. int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
  391. int eccsteps = chip->ecc.steps;
  392. uint8_t *p = buf;
  393. uint8_t *oob = chip->oob_poi;
  394. MTDDEBUG(MTD_DEBUG_LEVEL1, "Reading page %u to buf %p oob %p\n",
  395. host->page_addr, buf, oob);
  396. /* first read the data area and the available portion of OOB */
  397. for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
  398. int stat;
  399. host->col_addr = n * eccsize;
  400. chip->read_buf(mtd, p, eccsize);
  401. host->col_addr = mtd->writesize + n * eccpitch;
  402. if (chip->ecc.prepad) {
  403. chip->read_buf(mtd, oob, chip->ecc.prepad);
  404. oob += chip->ecc.prepad;
  405. }
  406. stat = chip->ecc.correct(mtd, p, oob, NULL);
  407. if (stat < 0)
  408. mtd->ecc_stats.failed++;
  409. else
  410. mtd->ecc_stats.corrected += stat;
  411. oob += eccbytes;
  412. if (chip->ecc.postpad) {
  413. chip->read_buf(mtd, oob, chip->ecc.postpad);
  414. oob += chip->ecc.postpad;
  415. }
  416. }
  417. /* Calculate remaining oob bytes */
  418. n = mtd->oobsize - (oob - chip->oob_poi);
  419. if (n)
  420. chip->read_buf(mtd, oob, n);
  421. /* Then switch ECC off and read the OOB area to get the ECC code */
  422. _mxc_nand_enable_hwecc(mtd, 0);
  423. chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, host->page_addr);
  424. eccsteps = chip->ecc.steps;
  425. oob = chip->oob_poi + chip->ecc.prepad;
  426. for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
  427. host->col_addr = mtd->writesize +
  428. n * eccpitch +
  429. chip->ecc.prepad;
  430. chip->read_buf(mtd, oob, eccbytes);
  431. oob += eccbytes + chip->ecc.postpad;
  432. }
  433. _mxc_nand_enable_hwecc(mtd, 1);
  434. return 0;
  435. }
  436. static int mxc_nand_write_oob_syndrome(struct mtd_info *mtd,
  437. struct nand_chip *chip, int page)
  438. {
  439. struct mxc_nand_host *host = chip->priv;
  440. int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  441. int length = mtd->oobsize;
  442. int i, len, status, steps = chip->ecc.steps;
  443. const uint8_t *bufpoi = chip->oob_poi;
  444. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  445. for (i = 0; i < steps; i++) {
  446. len = min_t(int, length, eccpitch);
  447. chip->write_buf(mtd, bufpoi, len);
  448. bufpoi += len;
  449. length -= len;
  450. host->col_addr += chip->ecc.prepad + chip->ecc.postpad;
  451. }
  452. if (length > 0)
  453. chip->write_buf(mtd, bufpoi, length);
  454. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  455. status = chip->waitfunc(mtd, chip);
  456. return status & NAND_STATUS_FAIL ? -EIO : 0;
  457. }
  458. static void mxc_nand_write_page_raw_syndrome(struct mtd_info *mtd,
  459. struct nand_chip *chip,
  460. const uint8_t *buf)
  461. {
  462. struct mxc_nand_host *host = chip->priv;
  463. int eccsize = chip->ecc.size;
  464. int eccbytes = chip->ecc.bytes;
  465. int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
  466. uint8_t *oob = chip->oob_poi;
  467. int steps, size;
  468. int n;
  469. for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
  470. host->col_addr = n * eccsize;
  471. chip->write_buf(mtd, buf, eccsize);
  472. buf += eccsize;
  473. host->col_addr = mtd->writesize + n * eccpitch;
  474. if (chip->ecc.prepad) {
  475. chip->write_buf(mtd, oob, chip->ecc.prepad);
  476. oob += chip->ecc.prepad;
  477. }
  478. host->col_addr += eccbytes;
  479. oob += eccbytes;
  480. if (chip->ecc.postpad) {
  481. chip->write_buf(mtd, oob, chip->ecc.postpad);
  482. oob += chip->ecc.postpad;
  483. }
  484. }
  485. size = mtd->oobsize - (oob - chip->oob_poi);
  486. if (size)
  487. chip->write_buf(mtd, oob, size);
  488. }
  489. static void mxc_nand_write_page_syndrome(struct mtd_info *mtd,
  490. struct nand_chip *chip,
  491. const uint8_t *buf)
  492. {
  493. struct mxc_nand_host *host = chip->priv;
  494. int i, n, eccsize = chip->ecc.size;
  495. int eccbytes = chip->ecc.bytes;
  496. int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
  497. int eccsteps = chip->ecc.steps;
  498. const uint8_t *p = buf;
  499. uint8_t *oob = chip->oob_poi;
  500. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  501. for (i = n = 0;
  502. eccsteps;
  503. n++, eccsteps--, i += eccbytes, p += eccsize) {
  504. host->col_addr = n * eccsize;
  505. chip->write_buf(mtd, p, eccsize);
  506. host->col_addr = mtd->writesize + n * eccpitch;
  507. if (chip->ecc.prepad) {
  508. chip->write_buf(mtd, oob, chip->ecc.prepad);
  509. oob += chip->ecc.prepad;
  510. }
  511. chip->write_buf(mtd, oob, eccbytes);
  512. oob += eccbytes;
  513. if (chip->ecc.postpad) {
  514. chip->write_buf(mtd, oob, chip->ecc.postpad);
  515. oob += chip->ecc.postpad;
  516. }
  517. }
  518. /* Calculate remaining oob bytes */
  519. i = mtd->oobsize - (oob - chip->oob_poi);
  520. if (i)
  521. chip->write_buf(mtd, oob, i);
  522. }
  523. static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  524. u_char *read_ecc, u_char *calc_ecc)
  525. {
  526. struct nand_chip *nand_chip = mtd->priv;
  527. struct mxc_nand_host *host = nand_chip->priv;
  528. uint32_t ecc_status = readl(&host->regs->ecc_status_result);
  529. int subpages = mtd->writesize / nand_chip->subpagesize;
  530. int pg2blk_shift = nand_chip->phys_erase_shift -
  531. nand_chip->page_shift;
  532. do {
  533. if ((ecc_status & 0xf) > 4) {
  534. static int last_bad = -1;
  535. if (last_bad != host->page_addr >> pg2blk_shift) {
  536. last_bad = host->page_addr >> pg2blk_shift;
  537. printk(KERN_DEBUG
  538. "MXC_NAND: HWECC uncorrectable ECC error"
  539. " in block %u page %u subpage %d\n",
  540. last_bad, host->page_addr,
  541. mtd->writesize / nand_chip->subpagesize
  542. - subpages);
  543. }
  544. return -1;
  545. }
  546. ecc_status >>= 4;
  547. subpages--;
  548. } while (subpages > 0);
  549. return 0;
  550. }
  551. #else
  552. #define mxc_nand_read_page_syndrome NULL
  553. #define mxc_nand_read_page_raw_syndrome NULL
  554. #define mxc_nand_read_oob_syndrome NULL
  555. #define mxc_nand_write_page_syndrome NULL
  556. #define mxc_nand_write_page_raw_syndrome NULL
  557. #define mxc_nand_write_oob_syndrome NULL
  558. static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  559. u_char *read_ecc, u_char *calc_ecc)
  560. {
  561. struct nand_chip *nand_chip = mtd->priv;
  562. struct mxc_nand_host *host = nand_chip->priv;
  563. /*
  564. * 1-Bit errors are automatically corrected in HW. No need for
  565. * additional correction. 2-Bit errors cannot be corrected by
  566. * HW ECC, so we need to return failure
  567. */
  568. uint16_t ecc_status = readw(&host->regs->ecc_status_result);
  569. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  570. MTDDEBUG(MTD_DEBUG_LEVEL0,
  571. "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  572. return -1;
  573. }
  574. return 0;
  575. }
  576. #endif
  577. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  578. u_char *ecc_code)
  579. {
  580. return 0;
  581. }
  582. #endif
  583. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  584. {
  585. struct nand_chip *nand_chip = mtd->priv;
  586. struct mxc_nand_host *host = nand_chip->priv;
  587. uint8_t ret = 0;
  588. uint16_t col;
  589. uint16_t __iomem *main_buf =
  590. (uint16_t __iomem *)host->regs->main_area[0];
  591. uint16_t __iomem *spare_buf =
  592. (uint16_t __iomem *)host->regs->spare_area[0];
  593. union {
  594. uint16_t word;
  595. uint8_t bytes[2];
  596. } nfc_word;
  597. /* Check for status request */
  598. if (host->status_request)
  599. return get_dev_status(host) & 0xFF;
  600. /* Get column for 16-bit access */
  601. col = host->col_addr >> 1;
  602. /* If we are accessing the spare region */
  603. if (host->spare_only)
  604. nfc_word.word = readw(&spare_buf[col]);
  605. else
  606. nfc_word.word = readw(&main_buf[col]);
  607. /* Pick upper/lower byte of word from RAM buffer */
  608. ret = nfc_word.bytes[host->col_addr & 0x1];
  609. /* Update saved column address */
  610. if (nand_chip->options & NAND_BUSWIDTH_16)
  611. host->col_addr += 2;
  612. else
  613. host->col_addr++;
  614. return ret;
  615. }
  616. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  617. {
  618. struct nand_chip *nand_chip = mtd->priv;
  619. struct mxc_nand_host *host = nand_chip->priv;
  620. uint16_t col, ret;
  621. uint16_t __iomem *p;
  622. MTDDEBUG(MTD_DEBUG_LEVEL3,
  623. "mxc_nand_read_word(col = %d)\n", host->col_addr);
  624. col = host->col_addr;
  625. /* Adjust saved column address */
  626. if (col < mtd->writesize && host->spare_only)
  627. col += mtd->writesize;
  628. if (col < mtd->writesize) {
  629. p = (uint16_t __iomem *)(host->regs->main_area[0] +
  630. (col >> 1));
  631. } else {
  632. p = (uint16_t __iomem *)(host->regs->spare_area[0] +
  633. ((col - mtd->writesize) >> 1));
  634. }
  635. if (col & 1) {
  636. union {
  637. uint16_t word;
  638. uint8_t bytes[2];
  639. } nfc_word[3];
  640. nfc_word[0].word = readw(p);
  641. nfc_word[1].word = readw(p + 1);
  642. nfc_word[2].bytes[0] = nfc_word[0].bytes[1];
  643. nfc_word[2].bytes[1] = nfc_word[1].bytes[0];
  644. ret = nfc_word[2].word;
  645. } else {
  646. ret = readw(p);
  647. }
  648. /* Update saved column address */
  649. host->col_addr = col + 2;
  650. return ret;
  651. }
  652. /*
  653. * Write data of length len to buffer buf. The data to be
  654. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  655. * Operation by the NFC, the data is written to NAND Flash
  656. */
  657. static void mxc_nand_write_buf(struct mtd_info *mtd,
  658. const u_char *buf, int len)
  659. {
  660. struct nand_chip *nand_chip = mtd->priv;
  661. struct mxc_nand_host *host = nand_chip->priv;
  662. int n, col, i = 0;
  663. MTDDEBUG(MTD_DEBUG_LEVEL3,
  664. "mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
  665. len);
  666. col = host->col_addr;
  667. /* Adjust saved column address */
  668. if (col < mtd->writesize && host->spare_only)
  669. col += mtd->writesize;
  670. n = mtd->writesize + mtd->oobsize - col;
  671. n = min(len, n);
  672. MTDDEBUG(MTD_DEBUG_LEVEL3,
  673. "%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
  674. while (n > 0) {
  675. void __iomem *p;
  676. if (col < mtd->writesize) {
  677. p = host->regs->main_area[0] + (col & ~3);
  678. } else {
  679. p = host->regs->spare_area[0] -
  680. mtd->writesize + (col & ~3);
  681. }
  682. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s:%d: p = %p\n", __func__,
  683. __LINE__, p);
  684. if (((col | (unsigned long)&buf[i]) & 3) || n < 4) {
  685. union {
  686. uint32_t word;
  687. uint8_t bytes[4];
  688. } nfc_word;
  689. nfc_word.word = readl(p);
  690. nfc_word.bytes[col & 3] = buf[i++];
  691. n--;
  692. col++;
  693. writel(nfc_word.word, p);
  694. } else {
  695. int m = mtd->writesize - col;
  696. if (col >= mtd->writesize)
  697. m += mtd->oobsize;
  698. m = min(n, m) & ~3;
  699. MTDDEBUG(MTD_DEBUG_LEVEL3,
  700. "%s:%d: n = %d, m = %d, i = %d, col = %d\n",
  701. __func__, __LINE__, n, m, i, col);
  702. mxc_nand_memcpy32(p, (uint32_t *)&buf[i], m);
  703. col += m;
  704. i += m;
  705. n -= m;
  706. }
  707. }
  708. /* Update saved column address */
  709. host->col_addr = col;
  710. }
  711. /*
  712. * Read the data buffer from the NAND Flash. To read the data from NAND
  713. * Flash first the data output cycle is initiated by the NFC, which copies
  714. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  715. */
  716. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  717. {
  718. struct nand_chip *nand_chip = mtd->priv;
  719. struct mxc_nand_host *host = nand_chip->priv;
  720. int n, col, i = 0;
  721. MTDDEBUG(MTD_DEBUG_LEVEL3,
  722. "mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr, len);
  723. col = host->col_addr;
  724. /* Adjust saved column address */
  725. if (col < mtd->writesize && host->spare_only)
  726. col += mtd->writesize;
  727. n = mtd->writesize + mtd->oobsize - col;
  728. n = min(len, n);
  729. while (n > 0) {
  730. void __iomem *p;
  731. if (col < mtd->writesize) {
  732. p = host->regs->main_area[0] + (col & ~3);
  733. } else {
  734. p = host->regs->spare_area[0] -
  735. mtd->writesize + (col & ~3);
  736. }
  737. if (((col | (int)&buf[i]) & 3) || n < 4) {
  738. union {
  739. uint32_t word;
  740. uint8_t bytes[4];
  741. } nfc_word;
  742. nfc_word.word = readl(p);
  743. buf[i++] = nfc_word.bytes[col & 3];
  744. n--;
  745. col++;
  746. } else {
  747. int m = mtd->writesize - col;
  748. if (col >= mtd->writesize)
  749. m += mtd->oobsize;
  750. m = min(n, m) & ~3;
  751. mxc_nand_memcpy32((uint32_t *)&buf[i], p, m);
  752. col += m;
  753. i += m;
  754. n -= m;
  755. }
  756. }
  757. /* Update saved column address */
  758. host->col_addr = col;
  759. }
  760. /*
  761. * Used by the upper layer to verify the data in NAND Flash
  762. * with the data in the buf.
  763. */
  764. static int mxc_nand_verify_buf(struct mtd_info *mtd,
  765. const u_char *buf, int len)
  766. {
  767. u_char tmp[256];
  768. uint bsize;
  769. while (len) {
  770. bsize = min(len, 256);
  771. mxc_nand_read_buf(mtd, tmp, bsize);
  772. if (memcmp(buf, tmp, bsize))
  773. return 1;
  774. buf += bsize;
  775. len -= bsize;
  776. }
  777. return 0;
  778. }
  779. /*
  780. * This function is used by upper layer for select and
  781. * deselect of the NAND chip
  782. */
  783. static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
  784. {
  785. struct nand_chip *nand_chip = mtd->priv;
  786. struct mxc_nand_host *host = nand_chip->priv;
  787. switch (chip) {
  788. case -1:
  789. /* TODO: Disable the NFC clock */
  790. if (host->clk_act)
  791. host->clk_act = 0;
  792. break;
  793. case 0:
  794. /* TODO: Enable the NFC clock */
  795. if (!host->clk_act)
  796. host->clk_act = 1;
  797. break;
  798. default:
  799. break;
  800. }
  801. }
  802. /*
  803. * Used by the upper layer to write command to NAND Flash for
  804. * different operations to be carried out on NAND Flash
  805. */
  806. void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  807. int column, int page_addr)
  808. {
  809. struct nand_chip *nand_chip = mtd->priv;
  810. struct mxc_nand_host *host = nand_chip->priv;
  811. MTDDEBUG(MTD_DEBUG_LEVEL3,
  812. "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  813. command, column, page_addr);
  814. /* Reset command state information */
  815. host->status_request = false;
  816. /* Command pre-processing step */
  817. switch (command) {
  818. case NAND_CMD_STATUS:
  819. host->col_addr = 0;
  820. host->status_request = true;
  821. break;
  822. case NAND_CMD_READ0:
  823. host->page_addr = page_addr;
  824. host->col_addr = column;
  825. host->spare_only = false;
  826. break;
  827. case NAND_CMD_READOOB:
  828. host->col_addr = column;
  829. host->spare_only = true;
  830. if (host->pagesize_2k)
  831. command = NAND_CMD_READ0; /* only READ0 is valid */
  832. break;
  833. case NAND_CMD_SEQIN:
  834. if (column >= mtd->writesize) {
  835. /*
  836. * before sending SEQIN command for partial write,
  837. * we need read one page out. FSL NFC does not support
  838. * partial write. It always sends out 512+ecc+512+ecc
  839. * for large page nand flash. But for small page nand
  840. * flash, it does support SPARE ONLY operation.
  841. */
  842. if (host->pagesize_2k) {
  843. /* call ourself to read a page */
  844. mxc_nand_command(mtd, NAND_CMD_READ0, 0,
  845. page_addr);
  846. }
  847. host->col_addr = column - mtd->writesize;
  848. host->spare_only = true;
  849. /* Set program pointer to spare region */
  850. if (!host->pagesize_2k)
  851. send_cmd(host, NAND_CMD_READOOB);
  852. } else {
  853. host->spare_only = false;
  854. host->col_addr = column;
  855. /* Set program pointer to page start */
  856. if (!host->pagesize_2k)
  857. send_cmd(host, NAND_CMD_READ0);
  858. }
  859. break;
  860. case NAND_CMD_PAGEPROG:
  861. send_prog_page(host, 0, host->spare_only);
  862. if (host->pagesize_2k && is_mxc_nfc_1()) {
  863. /* data in 4 areas */
  864. send_prog_page(host, 1, host->spare_only);
  865. send_prog_page(host, 2, host->spare_only);
  866. send_prog_page(host, 3, host->spare_only);
  867. }
  868. break;
  869. }
  870. /* Write out the command to the device. */
  871. send_cmd(host, command);
  872. /* Write out column address, if necessary */
  873. if (column != -1) {
  874. /*
  875. * MXC NANDFC can only perform full page+spare or
  876. * spare-only read/write. When the upper layers perform
  877. * a read/write buffer operation, we will use the saved
  878. * column address to index into the full page.
  879. */
  880. send_addr(host, 0);
  881. if (host->pagesize_2k)
  882. /* another col addr cycle for 2k page */
  883. send_addr(host, 0);
  884. }
  885. /* Write out page address, if necessary */
  886. if (page_addr != -1) {
  887. u32 page_mask = nand_chip->pagemask;
  888. do {
  889. send_addr(host, page_addr & 0xFF);
  890. page_addr >>= 8;
  891. page_mask >>= 8;
  892. } while (page_mask);
  893. }
  894. /* Command post-processing step */
  895. switch (command) {
  896. case NAND_CMD_RESET:
  897. break;
  898. case NAND_CMD_READOOB:
  899. case NAND_CMD_READ0:
  900. if (host->pagesize_2k) {
  901. /* send read confirm command */
  902. send_cmd(host, NAND_CMD_READSTART);
  903. /* read for each AREA */
  904. send_read_page(host, 0, host->spare_only);
  905. if (is_mxc_nfc_1()) {
  906. send_read_page(host, 1, host->spare_only);
  907. send_read_page(host, 2, host->spare_only);
  908. send_read_page(host, 3, host->spare_only);
  909. }
  910. } else {
  911. send_read_page(host, 0, host->spare_only);
  912. }
  913. break;
  914. case NAND_CMD_READID:
  915. host->col_addr = 0;
  916. send_read_id(host);
  917. break;
  918. case NAND_CMD_PAGEPROG:
  919. break;
  920. case NAND_CMD_STATUS:
  921. break;
  922. case NAND_CMD_ERASE2:
  923. break;
  924. }
  925. }
  926. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  927. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  928. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  929. static struct nand_bbt_descr bbt_main_descr = {
  930. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  931. NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  932. .offs = 0,
  933. .len = 4,
  934. .veroffs = 4,
  935. .maxblocks = 4,
  936. .pattern = bbt_pattern,
  937. };
  938. static struct nand_bbt_descr bbt_mirror_descr = {
  939. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  940. NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  941. .offs = 0,
  942. .len = 4,
  943. .veroffs = 4,
  944. .maxblocks = 4,
  945. .pattern = mirror_pattern,
  946. };
  947. #endif
  948. int board_nand_init(struct nand_chip *this)
  949. {
  950. struct mtd_info *mtd;
  951. #ifdef MXC_NFC_V2_1
  952. uint16_t tmp;
  953. #endif
  954. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  955. this->options |= NAND_USE_FLASH_BBT;
  956. this->bbt_td = &bbt_main_descr;
  957. this->bbt_md = &bbt_mirror_descr;
  958. #endif
  959. /* structures must be linked */
  960. mtd = &host->mtd;
  961. mtd->priv = this;
  962. host->nand = this;
  963. /* 5 us command delay time */
  964. this->chip_delay = 5;
  965. this->priv = host;
  966. this->dev_ready = mxc_nand_dev_ready;
  967. this->cmdfunc = mxc_nand_command;
  968. this->select_chip = mxc_nand_select_chip;
  969. this->read_byte = mxc_nand_read_byte;
  970. this->read_word = mxc_nand_read_word;
  971. this->write_buf = mxc_nand_write_buf;
  972. this->read_buf = mxc_nand_read_buf;
  973. this->verify_buf = mxc_nand_verify_buf;
  974. host->regs = (struct fsl_nfc_regs __iomem *)CONFIG_MXC_NAND_REGS_BASE;
  975. host->clk_act = 1;
  976. #ifdef CONFIG_MXC_NAND_HWECC
  977. this->ecc.calculate = mxc_nand_calculate_ecc;
  978. this->ecc.hwctl = mxc_nand_enable_hwecc;
  979. this->ecc.correct = mxc_nand_correct_data;
  980. if (is_mxc_nfc_21()) {
  981. this->ecc.mode = NAND_ECC_HW_SYNDROME;
  982. this->ecc.read_page = mxc_nand_read_page_syndrome;
  983. this->ecc.read_page_raw = mxc_nand_read_page_raw_syndrome;
  984. this->ecc.read_oob = mxc_nand_read_oob_syndrome;
  985. this->ecc.write_page = mxc_nand_write_page_syndrome;
  986. this->ecc.write_page_raw = mxc_nand_write_page_raw_syndrome;
  987. this->ecc.write_oob = mxc_nand_write_oob_syndrome;
  988. this->ecc.bytes = 9;
  989. this->ecc.prepad = 7;
  990. } else {
  991. this->ecc.mode = NAND_ECC_HW;
  992. }
  993. host->pagesize_2k = 0;
  994. this->ecc.size = 512;
  995. _mxc_nand_enable_hwecc(mtd, 1);
  996. #else
  997. this->ecc.layout = &nand_soft_eccoob;
  998. this->ecc.mode = NAND_ECC_SOFT;
  999. _mxc_nand_enable_hwecc(mtd, 0);
  1000. #endif
  1001. /* Reset NAND */
  1002. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1003. /* NAND bus width determines access functions used by upper layer */
  1004. if (is_16bit_nand())
  1005. this->options |= NAND_BUSWIDTH_16;
  1006. #ifdef CONFIG_SYS_NAND_LARGEPAGE
  1007. host->pagesize_2k = 1;
  1008. this->ecc.layout = &nand_hw_eccoob2k;
  1009. #else
  1010. host->pagesize_2k = 0;
  1011. this->ecc.layout = &nand_hw_eccoob;
  1012. #endif
  1013. #ifdef MXC_NFC_V2_1
  1014. tmp = readw(&host->regs->config1);
  1015. tmp |= NFC_ONE_CYCLE;
  1016. tmp |= NFC_4_8N_ECC;
  1017. writew(tmp, &host->regs->config1);
  1018. if (host->pagesize_2k)
  1019. writew(64/2, &host->regs->spare_area_size);
  1020. else
  1021. writew(16/2, &host->regs->spare_area_size);
  1022. #endif
  1023. /*
  1024. * preset operation
  1025. * Unlock the internal RAM Buffer
  1026. */
  1027. writew(0x2, &host->regs->config);
  1028. /* Blocks to be unlocked */
  1029. writew(0x0, &host->regs->unlockstart_blkaddr);
  1030. /* Originally (Freescale LTIB 2.6.21) 0x4000 was written to the
  1031. * unlockend_blkaddr, but the magic 0x4000 does not always work
  1032. * when writing more than some 32 megabytes (on 2k page nands)
  1033. * However 0xFFFF doesn't seem to have this kind
  1034. * of limitation (tried it back and forth several times).
  1035. * The linux kernel driver sets this to 0xFFFF for the v2 controller
  1036. * only, but probably this was not tested there for v1.
  1037. * The very same limitation seems to apply to this kernel driver.
  1038. * This might be NAND chip specific and the i.MX31 datasheet is
  1039. * extremely vague about the semantics of this register.
  1040. */
  1041. writew(0xFFFF, &host->regs->unlockend_blkaddr);
  1042. /* Unlock Block Command for given address range */
  1043. writew(0x4, &host->regs->wrprot);
  1044. return 0;
  1045. }