mxc_nand.c 34 KB

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