mxc_nand.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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 (is_mxc_nfc_1())
  1051. this->ecc.strength = 1;
  1052. else
  1053. this->ecc.strength = 4;
  1054. host->pagesize_2k = 0;
  1055. this->ecc.size = 512;
  1056. _mxc_nand_enable_hwecc(mtd, 1);
  1057. #else
  1058. this->ecc.layout = &nand_soft_eccoob;
  1059. this->ecc.mode = NAND_ECC_SOFT;
  1060. _mxc_nand_enable_hwecc(mtd, 0);
  1061. #endif
  1062. /* Reset NAND */
  1063. this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1064. /* NAND bus width determines access functions used by upper layer */
  1065. if (is_16bit_nand())
  1066. this->options |= NAND_BUSWIDTH_16;
  1067. #ifdef CONFIG_SYS_NAND_LARGEPAGE
  1068. host->pagesize_2k = 1;
  1069. this->ecc.layout = &nand_hw_eccoob2k;
  1070. #else
  1071. host->pagesize_2k = 0;
  1072. this->ecc.layout = &nand_hw_eccoob;
  1073. #endif
  1074. #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
  1075. #ifdef MXC_NFC_V2_1
  1076. tmp = readnfc(&host->regs->config1);
  1077. tmp |= NFC_V2_CONFIG1_ONE_CYCLE;
  1078. tmp |= NFC_V2_CONFIG1_ECC_MODE_4;
  1079. writenfc(tmp, &host->regs->config1);
  1080. if (host->pagesize_2k)
  1081. writenfc(64/2, &host->regs->spare_area_size);
  1082. else
  1083. writenfc(16/2, &host->regs->spare_area_size);
  1084. #endif
  1085. /*
  1086. * preset operation
  1087. * Unlock the internal RAM Buffer
  1088. */
  1089. writenfc(0x2, &host->regs->config);
  1090. /* Blocks to be unlocked */
  1091. writenfc(0x0, &host->regs->unlockstart_blkaddr);
  1092. /* Originally (Freescale LTIB 2.6.21) 0x4000 was written to the
  1093. * unlockend_blkaddr, but the magic 0x4000 does not always work
  1094. * when writing more than some 32 megabytes (on 2k page nands)
  1095. * However 0xFFFF doesn't seem to have this kind
  1096. * of limitation (tried it back and forth several times).
  1097. * The linux kernel driver sets this to 0xFFFF for the v2 controller
  1098. * only, but probably this was not tested there for v1.
  1099. * The very same limitation seems to apply to this kernel driver.
  1100. * This might be NAND chip specific and the i.MX31 datasheet is
  1101. * extremely vague about the semantics of this register.
  1102. */
  1103. writenfc(0xFFFF, &host->regs->unlockend_blkaddr);
  1104. /* Unlock Block Command for given address range */
  1105. writenfc(0x4, &host->regs->wrprot);
  1106. #elif defined(MXC_NFC_V3_2)
  1107. writenfc(NFC_V3_CONFIG1_RBA(0), &host->regs->config1);
  1108. writenfc(NFC_V3_IPC_CREQ, &host->ip_regs->ipc);
  1109. /* Unlock the internal RAM Buffer */
  1110. writenfc(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
  1111. &host->ip_regs->wrprot);
  1112. /* Blocks to be unlocked */
  1113. for (tmp = 0; tmp < CONFIG_SYS_NAND_MAX_CHIPS; tmp++)
  1114. writenfc(0x0 | 0xFFFF << 16,
  1115. &host->ip_regs->wrprot_unlock_blkaddr[tmp]);
  1116. writenfc(0, &host->ip_regs->ipc);
  1117. tmp = readnfc(&host->ip_regs->config2);
  1118. tmp &= ~(NFC_V3_CONFIG2_SPAS_MASK | NFC_V3_CONFIG2_EDC_MASK |
  1119. NFC_V3_CONFIG2_ECC_MODE_8 | NFC_V3_CONFIG2_PS_MASK);
  1120. tmp |= NFC_V3_CONFIG2_ONE_CYCLE;
  1121. if (host->pagesize_2k) {
  1122. tmp |= NFC_V3_CONFIG2_SPAS(64/2);
  1123. tmp |= NFC_V3_CONFIG2_PS_2048;
  1124. } else {
  1125. tmp |= NFC_V3_CONFIG2_SPAS(16/2);
  1126. tmp |= NFC_V3_CONFIG2_PS_512;
  1127. }
  1128. writenfc(tmp, &host->ip_regs->config2);
  1129. tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
  1130. NFC_V3_CONFIG3_NO_SDMA |
  1131. NFC_V3_CONFIG3_RBB_MODE |
  1132. NFC_V3_CONFIG3_SBB(6) | /* Reset default */
  1133. NFC_V3_CONFIG3_ADD_OP(0);
  1134. if (!(this->options & NAND_BUSWIDTH_16))
  1135. tmp |= NFC_V3_CONFIG3_FW8;
  1136. writenfc(tmp, &host->ip_regs->config3);
  1137. writenfc(0, &host->ip_regs->delay_line);
  1138. #endif
  1139. return 0;
  1140. }