mxc_nand.c 34 KB

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