mxc_nand.c 33 KB

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