mxs_nand.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * Freescale i.MX28 NAND flash driver
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. * on behalf of DENX Software Engineering GmbH
  6. *
  7. * Based on code from LTIB:
  8. * Freescale GPMI NFC NAND Flash Driver
  9. *
  10. * Copyright (C) 2010 Freescale Semiconductor, Inc.
  11. * Copyright (C) 2008 Embedded Alley Solutions, Inc.
  12. *
  13. * SPDX-License-Identifier: GPL-2.0+
  14. */
  15. #include <common.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/types.h>
  19. #include <malloc.h>
  20. #include <asm/errno.h>
  21. #include <asm/io.h>
  22. #include <asm/arch/clock.h>
  23. #include <asm/arch/imx-regs.h>
  24. #include <asm/imx-common/regs-bch.h>
  25. #include <asm/imx-common/regs-gpmi.h>
  26. #include <asm/arch/sys_proto.h>
  27. #include <asm/imx-common/dma.h>
  28. #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
  29. #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
  30. #if defined(CONFIG_MX6)
  31. #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 2
  32. #else
  33. #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT 0
  34. #endif
  35. #define MXS_NAND_METADATA_SIZE 10
  36. #define MXS_NAND_BITS_PER_ECC_LEVEL 13
  37. #define MXS_NAND_COMMAND_BUFFER_SIZE 32
  38. #define MXS_NAND_BCH_TIMEOUT 10000
  39. struct mxs_nand_info {
  40. int cur_chip;
  41. uint32_t cmd_queue_len;
  42. uint32_t data_buf_size;
  43. uint8_t *cmd_buf;
  44. uint8_t *data_buf;
  45. uint8_t *oob_buf;
  46. uint8_t marking_block_bad;
  47. uint8_t raw_oob_mode;
  48. /* Functions with altered behaviour */
  49. int (*hooked_read_oob)(struct mtd_info *mtd,
  50. loff_t from, struct mtd_oob_ops *ops);
  51. int (*hooked_write_oob)(struct mtd_info *mtd,
  52. loff_t to, struct mtd_oob_ops *ops);
  53. int (*hooked_block_markbad)(struct mtd_info *mtd,
  54. loff_t ofs);
  55. /* DMA descriptors */
  56. struct mxs_dma_desc **desc;
  57. uint32_t desc_index;
  58. };
  59. struct nand_ecclayout fake_ecc_layout;
  60. /*
  61. * Cache management functions
  62. */
  63. #ifndef CONFIG_SYS_DCACHE_OFF
  64. static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
  65. {
  66. uint32_t addr = (uint32_t)info->data_buf;
  67. flush_dcache_range(addr, addr + info->data_buf_size);
  68. }
  69. static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
  70. {
  71. uint32_t addr = (uint32_t)info->data_buf;
  72. invalidate_dcache_range(addr, addr + info->data_buf_size);
  73. }
  74. static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
  75. {
  76. uint32_t addr = (uint32_t)info->cmd_buf;
  77. flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
  78. }
  79. #else
  80. static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
  81. static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
  82. static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
  83. #endif
  84. static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
  85. {
  86. struct mxs_dma_desc *desc;
  87. if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
  88. printf("MXS NAND: Too many DMA descriptors requested\n");
  89. return NULL;
  90. }
  91. desc = info->desc[info->desc_index];
  92. info->desc_index++;
  93. return desc;
  94. }
  95. static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
  96. {
  97. int i;
  98. struct mxs_dma_desc *desc;
  99. for (i = 0; i < info->desc_index; i++) {
  100. desc = info->desc[i];
  101. memset(desc, 0, sizeof(struct mxs_dma_desc));
  102. desc->address = (dma_addr_t)desc;
  103. }
  104. info->desc_index = 0;
  105. }
  106. static uint32_t mxs_nand_ecc_chunk_cnt(uint32_t page_data_size)
  107. {
  108. return page_data_size / MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
  109. }
  110. static uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength)
  111. {
  112. return ecc_strength * MXS_NAND_BITS_PER_ECC_LEVEL;
  113. }
  114. static uint32_t mxs_nand_aux_status_offset(void)
  115. {
  116. return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
  117. }
  118. static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size,
  119. uint32_t page_oob_size)
  120. {
  121. int ecc_strength;
  122. /*
  123. * Determine the ECC layout with the formula:
  124. * ECC bits per chunk = (total page spare data bits) /
  125. * (bits per ECC level) / (chunks per page)
  126. * where:
  127. * total page spare data bits =
  128. * (page oob size - meta data size) * (bits per byte)
  129. */
  130. ecc_strength = ((page_oob_size - MXS_NAND_METADATA_SIZE) * 8)
  131. / (MXS_NAND_BITS_PER_ECC_LEVEL *
  132. mxs_nand_ecc_chunk_cnt(page_data_size));
  133. return round_down(ecc_strength, 2);
  134. }
  135. static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,
  136. uint32_t ecc_strength)
  137. {
  138. uint32_t chunk_data_size_in_bits;
  139. uint32_t chunk_ecc_size_in_bits;
  140. uint32_t chunk_total_size_in_bits;
  141. uint32_t block_mark_chunk_number;
  142. uint32_t block_mark_chunk_bit_offset;
  143. uint32_t block_mark_bit_offset;
  144. chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8;
  145. chunk_ecc_size_in_bits = mxs_nand_ecc_size_in_bits(ecc_strength);
  146. chunk_total_size_in_bits =
  147. chunk_data_size_in_bits + chunk_ecc_size_in_bits;
  148. /* Compute the bit offset of the block mark within the physical page. */
  149. block_mark_bit_offset = page_data_size * 8;
  150. /* Subtract the metadata bits. */
  151. block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
  152. /*
  153. * Compute the chunk number (starting at zero) in which the block mark
  154. * appears.
  155. */
  156. block_mark_chunk_number =
  157. block_mark_bit_offset / chunk_total_size_in_bits;
  158. /*
  159. * Compute the bit offset of the block mark within its chunk, and
  160. * validate it.
  161. */
  162. block_mark_chunk_bit_offset = block_mark_bit_offset -
  163. (block_mark_chunk_number * chunk_total_size_in_bits);
  164. if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
  165. return 1;
  166. /*
  167. * Now that we know the chunk number in which the block mark appears,
  168. * we can subtract all the ECC bits that appear before it.
  169. */
  170. block_mark_bit_offset -=
  171. block_mark_chunk_number * chunk_ecc_size_in_bits;
  172. return block_mark_bit_offset;
  173. }
  174. static uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
  175. {
  176. uint32_t ecc_strength;
  177. ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
  178. return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) >> 3;
  179. }
  180. static uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
  181. {
  182. uint32_t ecc_strength;
  183. ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
  184. return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) & 0x7;
  185. }
  186. /*
  187. * Wait for BCH complete IRQ and clear the IRQ
  188. */
  189. static int mxs_nand_wait_for_bch_complete(void)
  190. {
  191. struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
  192. int timeout = MXS_NAND_BCH_TIMEOUT;
  193. int ret;
  194. ret = mxs_wait_mask_set(&bch_regs->hw_bch_ctrl_reg,
  195. BCH_CTRL_COMPLETE_IRQ, timeout);
  196. writel(BCH_CTRL_COMPLETE_IRQ, &bch_regs->hw_bch_ctrl_clr);
  197. return ret;
  198. }
  199. /*
  200. * This is the function that we install in the cmd_ctrl function pointer of the
  201. * owning struct nand_chip. The only functions in the reference implementation
  202. * that use these functions pointers are cmdfunc and select_chip.
  203. *
  204. * In this driver, we implement our own select_chip, so this function will only
  205. * be called by the reference implementation's cmdfunc. For this reason, we can
  206. * ignore the chip enable bit and concentrate only on sending bytes to the NAND
  207. * Flash.
  208. */
  209. static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
  210. {
  211. struct nand_chip *nand = mtd->priv;
  212. struct mxs_nand_info *nand_info = nand->priv;
  213. struct mxs_dma_desc *d;
  214. uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
  215. int ret;
  216. /*
  217. * If this condition is true, something is _VERY_ wrong in MTD
  218. * subsystem!
  219. */
  220. if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
  221. printf("MXS NAND: Command queue too long\n");
  222. return;
  223. }
  224. /*
  225. * Every operation begins with a command byte and a series of zero or
  226. * more address bytes. These are distinguished by either the Address
  227. * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
  228. * asserted. When MTD is ready to execute the command, it will
  229. * deasert both latch enables.
  230. *
  231. * Rather than run a separate DMA operation for every single byte, we
  232. * queue them up and run a single DMA operation for the entire series
  233. * of command and data bytes.
  234. */
  235. if (ctrl & (NAND_ALE | NAND_CLE)) {
  236. if (data != NAND_CMD_NONE)
  237. nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
  238. return;
  239. }
  240. /*
  241. * If control arrives here, MTD has deasserted both the ALE and CLE,
  242. * which means it's ready to run an operation. Check if we have any
  243. * bytes to send.
  244. */
  245. if (nand_info->cmd_queue_len == 0)
  246. return;
  247. /* Compile the DMA descriptor -- a descriptor that sends command. */
  248. d = mxs_nand_get_dma_desc(nand_info);
  249. d->cmd.data =
  250. MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
  251. MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
  252. MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
  253. (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
  254. d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
  255. d->cmd.pio_words[0] =
  256. GPMI_CTRL0_COMMAND_MODE_WRITE |
  257. GPMI_CTRL0_WORD_LENGTH |
  258. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  259. GPMI_CTRL0_ADDRESS_NAND_CLE |
  260. GPMI_CTRL0_ADDRESS_INCREMENT |
  261. nand_info->cmd_queue_len;
  262. mxs_dma_desc_append(channel, d);
  263. /* Flush caches */
  264. mxs_nand_flush_cmd_buf(nand_info);
  265. /* Execute the DMA chain. */
  266. ret = mxs_dma_go(channel);
  267. if (ret)
  268. printf("MXS NAND: Error sending command\n");
  269. mxs_nand_return_dma_descs(nand_info);
  270. /* Reset the command queue. */
  271. nand_info->cmd_queue_len = 0;
  272. }
  273. /*
  274. * Test if the NAND flash is ready.
  275. */
  276. static int mxs_nand_device_ready(struct mtd_info *mtd)
  277. {
  278. struct nand_chip *chip = mtd->priv;
  279. struct mxs_nand_info *nand_info = chip->priv;
  280. struct mxs_gpmi_regs *gpmi_regs =
  281. (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
  282. uint32_t tmp;
  283. tmp = readl(&gpmi_regs->hw_gpmi_stat);
  284. tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
  285. return tmp & 1;
  286. }
  287. /*
  288. * Select the NAND chip.
  289. */
  290. static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
  291. {
  292. struct nand_chip *nand = mtd->priv;
  293. struct mxs_nand_info *nand_info = nand->priv;
  294. nand_info->cur_chip = chip;
  295. }
  296. /*
  297. * Handle block mark swapping.
  298. *
  299. * Note that, when this function is called, it doesn't know whether it's
  300. * swapping the block mark, or swapping it *back* -- but it doesn't matter
  301. * because the the operation is the same.
  302. */
  303. static void mxs_nand_swap_block_mark(struct mtd_info *mtd,
  304. uint8_t *data_buf, uint8_t *oob_buf)
  305. {
  306. uint32_t bit_offset;
  307. uint32_t buf_offset;
  308. uint32_t src;
  309. uint32_t dst;
  310. bit_offset = mxs_nand_mark_bit_offset(mtd);
  311. buf_offset = mxs_nand_mark_byte_offset(mtd);
  312. /*
  313. * Get the byte from the data area that overlays the block mark. Since
  314. * the ECC engine applies its own view to the bits in the page, the
  315. * physical block mark won't (in general) appear on a byte boundary in
  316. * the data.
  317. */
  318. src = data_buf[buf_offset] >> bit_offset;
  319. src |= data_buf[buf_offset + 1] << (8 - bit_offset);
  320. dst = oob_buf[0];
  321. oob_buf[0] = src;
  322. data_buf[buf_offset] &= ~(0xff << bit_offset);
  323. data_buf[buf_offset + 1] &= 0xff << bit_offset;
  324. data_buf[buf_offset] |= dst << bit_offset;
  325. data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
  326. }
  327. /*
  328. * Read data from NAND.
  329. */
  330. static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
  331. {
  332. struct nand_chip *nand = mtd->priv;
  333. struct mxs_nand_info *nand_info = nand->priv;
  334. struct mxs_dma_desc *d;
  335. uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
  336. int ret;
  337. if (length > NAND_MAX_PAGESIZE) {
  338. printf("MXS NAND: DMA buffer too big\n");
  339. return;
  340. }
  341. if (!buf) {
  342. printf("MXS NAND: DMA buffer is NULL\n");
  343. return;
  344. }
  345. /* Compile the DMA descriptor - a descriptor that reads data. */
  346. d = mxs_nand_get_dma_desc(nand_info);
  347. d->cmd.data =
  348. MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
  349. MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
  350. (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
  351. (length << MXS_DMA_DESC_BYTES_OFFSET);
  352. d->cmd.address = (dma_addr_t)nand_info->data_buf;
  353. d->cmd.pio_words[0] =
  354. GPMI_CTRL0_COMMAND_MODE_READ |
  355. GPMI_CTRL0_WORD_LENGTH |
  356. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  357. GPMI_CTRL0_ADDRESS_NAND_DATA |
  358. length;
  359. mxs_dma_desc_append(channel, d);
  360. /*
  361. * A DMA descriptor that waits for the command to end and the chip to
  362. * become ready.
  363. *
  364. * I think we actually should *not* be waiting for the chip to become
  365. * ready because, after all, we don't care. I think the original code
  366. * did that and no one has re-thought it yet.
  367. */
  368. d = mxs_nand_get_dma_desc(nand_info);
  369. d->cmd.data =
  370. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
  371. MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
  372. MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  373. d->cmd.address = 0;
  374. d->cmd.pio_words[0] =
  375. GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
  376. GPMI_CTRL0_WORD_LENGTH |
  377. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  378. GPMI_CTRL0_ADDRESS_NAND_DATA;
  379. mxs_dma_desc_append(channel, d);
  380. /* Execute the DMA chain. */
  381. ret = mxs_dma_go(channel);
  382. if (ret) {
  383. printf("MXS NAND: DMA read error\n");
  384. goto rtn;
  385. }
  386. /* Invalidate caches */
  387. mxs_nand_inval_data_buf(nand_info);
  388. memcpy(buf, nand_info->data_buf, length);
  389. rtn:
  390. mxs_nand_return_dma_descs(nand_info);
  391. }
  392. /*
  393. * Write data to NAND.
  394. */
  395. static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  396. int length)
  397. {
  398. struct nand_chip *nand = mtd->priv;
  399. struct mxs_nand_info *nand_info = nand->priv;
  400. struct mxs_dma_desc *d;
  401. uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
  402. int ret;
  403. if (length > NAND_MAX_PAGESIZE) {
  404. printf("MXS NAND: DMA buffer too big\n");
  405. return;
  406. }
  407. if (!buf) {
  408. printf("MXS NAND: DMA buffer is NULL\n");
  409. return;
  410. }
  411. memcpy(nand_info->data_buf, buf, length);
  412. /* Compile the DMA descriptor - a descriptor that writes data. */
  413. d = mxs_nand_get_dma_desc(nand_info);
  414. d->cmd.data =
  415. MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
  416. MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
  417. (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
  418. (length << MXS_DMA_DESC_BYTES_OFFSET);
  419. d->cmd.address = (dma_addr_t)nand_info->data_buf;
  420. d->cmd.pio_words[0] =
  421. GPMI_CTRL0_COMMAND_MODE_WRITE |
  422. GPMI_CTRL0_WORD_LENGTH |
  423. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  424. GPMI_CTRL0_ADDRESS_NAND_DATA |
  425. length;
  426. mxs_dma_desc_append(channel, d);
  427. /* Flush caches */
  428. mxs_nand_flush_data_buf(nand_info);
  429. /* Execute the DMA chain. */
  430. ret = mxs_dma_go(channel);
  431. if (ret)
  432. printf("MXS NAND: DMA write error\n");
  433. mxs_nand_return_dma_descs(nand_info);
  434. }
  435. /*
  436. * Read a single byte from NAND.
  437. */
  438. static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
  439. {
  440. uint8_t buf;
  441. mxs_nand_read_buf(mtd, &buf, 1);
  442. return buf;
  443. }
  444. /*
  445. * Read a page from NAND.
  446. */
  447. static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
  448. uint8_t *buf, int oob_required,
  449. int page)
  450. {
  451. struct mxs_nand_info *nand_info = nand->priv;
  452. struct mxs_dma_desc *d;
  453. uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
  454. uint32_t corrected = 0, failed = 0;
  455. uint8_t *status;
  456. int i, ret;
  457. /* Compile the DMA descriptor - wait for ready. */
  458. d = mxs_nand_get_dma_desc(nand_info);
  459. d->cmd.data =
  460. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
  461. MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
  462. (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  463. d->cmd.address = 0;
  464. d->cmd.pio_words[0] =
  465. GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
  466. GPMI_CTRL0_WORD_LENGTH |
  467. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  468. GPMI_CTRL0_ADDRESS_NAND_DATA;
  469. mxs_dma_desc_append(channel, d);
  470. /* Compile the DMA descriptor - enable the BCH block and read. */
  471. d = mxs_nand_get_dma_desc(nand_info);
  472. d->cmd.data =
  473. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
  474. MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  475. d->cmd.address = 0;
  476. d->cmd.pio_words[0] =
  477. GPMI_CTRL0_COMMAND_MODE_READ |
  478. GPMI_CTRL0_WORD_LENGTH |
  479. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  480. GPMI_CTRL0_ADDRESS_NAND_DATA |
  481. (mtd->writesize + mtd->oobsize);
  482. d->cmd.pio_words[1] = 0;
  483. d->cmd.pio_words[2] =
  484. GPMI_ECCCTRL_ENABLE_ECC |
  485. GPMI_ECCCTRL_ECC_CMD_DECODE |
  486. GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
  487. d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
  488. d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
  489. d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
  490. mxs_dma_desc_append(channel, d);
  491. /* Compile the DMA descriptor - disable the BCH block. */
  492. d = mxs_nand_get_dma_desc(nand_info);
  493. d->cmd.data =
  494. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
  495. MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
  496. (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  497. d->cmd.address = 0;
  498. d->cmd.pio_words[0] =
  499. GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
  500. GPMI_CTRL0_WORD_LENGTH |
  501. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  502. GPMI_CTRL0_ADDRESS_NAND_DATA |
  503. (mtd->writesize + mtd->oobsize);
  504. d->cmd.pio_words[1] = 0;
  505. d->cmd.pio_words[2] = 0;
  506. mxs_dma_desc_append(channel, d);
  507. /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
  508. d = mxs_nand_get_dma_desc(nand_info);
  509. d->cmd.data =
  510. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
  511. MXS_DMA_DESC_DEC_SEM;
  512. d->cmd.address = 0;
  513. mxs_dma_desc_append(channel, d);
  514. /* Execute the DMA chain. */
  515. ret = mxs_dma_go(channel);
  516. if (ret) {
  517. printf("MXS NAND: DMA read error\n");
  518. goto rtn;
  519. }
  520. ret = mxs_nand_wait_for_bch_complete();
  521. if (ret) {
  522. printf("MXS NAND: BCH read timeout\n");
  523. goto rtn;
  524. }
  525. /* Invalidate caches */
  526. mxs_nand_inval_data_buf(nand_info);
  527. /* Read DMA completed, now do the mark swapping. */
  528. mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
  529. /* Loop over status bytes, accumulating ECC status. */
  530. status = nand_info->oob_buf + mxs_nand_aux_status_offset();
  531. for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) {
  532. if (status[i] == 0x00)
  533. continue;
  534. if (status[i] == 0xff)
  535. continue;
  536. if (status[i] == 0xfe) {
  537. failed++;
  538. continue;
  539. }
  540. corrected += status[i];
  541. }
  542. /* Propagate ECC status to the owning MTD. */
  543. mtd->ecc_stats.failed += failed;
  544. mtd->ecc_stats.corrected += corrected;
  545. /*
  546. * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
  547. * details about our policy for delivering the OOB.
  548. *
  549. * We fill the caller's buffer with set bits, and then copy the block
  550. * mark to the caller's buffer. Note that, if block mark swapping was
  551. * necessary, it has already been done, so we can rely on the first
  552. * byte of the auxiliary buffer to contain the block mark.
  553. */
  554. memset(nand->oob_poi, 0xff, mtd->oobsize);
  555. nand->oob_poi[0] = nand_info->oob_buf[0];
  556. memcpy(buf, nand_info->data_buf, mtd->writesize);
  557. rtn:
  558. mxs_nand_return_dma_descs(nand_info);
  559. return ret;
  560. }
  561. /*
  562. * Write a page to NAND.
  563. */
  564. static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
  565. struct nand_chip *nand, const uint8_t *buf,
  566. int oob_required)
  567. {
  568. struct mxs_nand_info *nand_info = nand->priv;
  569. struct mxs_dma_desc *d;
  570. uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
  571. int ret;
  572. memcpy(nand_info->data_buf, buf, mtd->writesize);
  573. memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
  574. /* Handle block mark swapping. */
  575. mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
  576. /* Compile the DMA descriptor - write data. */
  577. d = mxs_nand_get_dma_desc(nand_info);
  578. d->cmd.data =
  579. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
  580. MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
  581. (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  582. d->cmd.address = 0;
  583. d->cmd.pio_words[0] =
  584. GPMI_CTRL0_COMMAND_MODE_WRITE |
  585. GPMI_CTRL0_WORD_LENGTH |
  586. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  587. GPMI_CTRL0_ADDRESS_NAND_DATA;
  588. d->cmd.pio_words[1] = 0;
  589. d->cmd.pio_words[2] =
  590. GPMI_ECCCTRL_ENABLE_ECC |
  591. GPMI_ECCCTRL_ECC_CMD_ENCODE |
  592. GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
  593. d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
  594. d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
  595. d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
  596. mxs_dma_desc_append(channel, d);
  597. /* Flush caches */
  598. mxs_nand_flush_data_buf(nand_info);
  599. /* Execute the DMA chain. */
  600. ret = mxs_dma_go(channel);
  601. if (ret) {
  602. printf("MXS NAND: DMA write error\n");
  603. goto rtn;
  604. }
  605. ret = mxs_nand_wait_for_bch_complete();
  606. if (ret) {
  607. printf("MXS NAND: BCH write timeout\n");
  608. goto rtn;
  609. }
  610. rtn:
  611. mxs_nand_return_dma_descs(nand_info);
  612. return 0;
  613. }
  614. /*
  615. * Read OOB from NAND.
  616. *
  617. * This function is a veneer that replaces the function originally installed by
  618. * the NAND Flash MTD code.
  619. */
  620. static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
  621. struct mtd_oob_ops *ops)
  622. {
  623. struct nand_chip *chip = mtd->priv;
  624. struct mxs_nand_info *nand_info = chip->priv;
  625. int ret;
  626. if (ops->mode == MTD_OPS_RAW)
  627. nand_info->raw_oob_mode = 1;
  628. else
  629. nand_info->raw_oob_mode = 0;
  630. ret = nand_info->hooked_read_oob(mtd, from, ops);
  631. nand_info->raw_oob_mode = 0;
  632. return ret;
  633. }
  634. /*
  635. * Write OOB to NAND.
  636. *
  637. * This function is a veneer that replaces the function originally installed by
  638. * the NAND Flash MTD code.
  639. */
  640. static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
  641. struct mtd_oob_ops *ops)
  642. {
  643. struct nand_chip *chip = mtd->priv;
  644. struct mxs_nand_info *nand_info = chip->priv;
  645. int ret;
  646. if (ops->mode == MTD_OPS_RAW)
  647. nand_info->raw_oob_mode = 1;
  648. else
  649. nand_info->raw_oob_mode = 0;
  650. ret = nand_info->hooked_write_oob(mtd, to, ops);
  651. nand_info->raw_oob_mode = 0;
  652. return ret;
  653. }
  654. /*
  655. * Mark a block bad in NAND.
  656. *
  657. * This function is a veneer that replaces the function originally installed by
  658. * the NAND Flash MTD code.
  659. */
  660. static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
  661. {
  662. struct nand_chip *chip = mtd->priv;
  663. struct mxs_nand_info *nand_info = chip->priv;
  664. int ret;
  665. nand_info->marking_block_bad = 1;
  666. ret = nand_info->hooked_block_markbad(mtd, ofs);
  667. nand_info->marking_block_bad = 0;
  668. return ret;
  669. }
  670. /*
  671. * There are several places in this driver where we have to handle the OOB and
  672. * block marks. This is the function where things are the most complicated, so
  673. * this is where we try to explain it all. All the other places refer back to
  674. * here.
  675. *
  676. * These are the rules, in order of decreasing importance:
  677. *
  678. * 1) Nothing the caller does can be allowed to imperil the block mark, so all
  679. * write operations take measures to protect it.
  680. *
  681. * 2) In read operations, the first byte of the OOB we return must reflect the
  682. * true state of the block mark, no matter where that block mark appears in
  683. * the physical page.
  684. *
  685. * 3) ECC-based read operations return an OOB full of set bits (since we never
  686. * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
  687. * return).
  688. *
  689. * 4) "Raw" read operations return a direct view of the physical bytes in the
  690. * page, using the conventional definition of which bytes are data and which
  691. * are OOB. This gives the caller a way to see the actual, physical bytes
  692. * in the page, without the distortions applied by our ECC engine.
  693. *
  694. * What we do for this specific read operation depends on whether we're doing
  695. * "raw" read, or an ECC-based read.
  696. *
  697. * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
  698. * easy. When reading a page, for example, the NAND Flash MTD code calls our
  699. * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
  700. * ECC-based or raw view of the page is implicit in which function it calls
  701. * (there is a similar pair of ECC-based/raw functions for writing).
  702. *
  703. * Since MTD assumes the OOB is not covered by ECC, there is no pair of
  704. * ECC-based/raw functions for reading or or writing the OOB. The fact that the
  705. * caller wants an ECC-based or raw view of the page is not propagated down to
  706. * this driver.
  707. *
  708. * Since our OOB *is* covered by ECC, we need this information. So, we hook the
  709. * ecc.read_oob and ecc.write_oob function pointers in the owning
  710. * struct mtd_info with our own functions. These hook functions set the
  711. * raw_oob_mode field so that, when control finally arrives here, we'll know
  712. * what to do.
  713. */
  714. static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
  715. int page)
  716. {
  717. struct mxs_nand_info *nand_info = nand->priv;
  718. /*
  719. * First, fill in the OOB buffer. If we're doing a raw read, we need to
  720. * get the bytes from the physical page. If we're not doing a raw read,
  721. * we need to fill the buffer with set bits.
  722. */
  723. if (nand_info->raw_oob_mode) {
  724. /*
  725. * If control arrives here, we're doing a "raw" read. Send the
  726. * command to read the conventional OOB and read it.
  727. */
  728. nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  729. nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
  730. } else {
  731. /*
  732. * If control arrives here, we're not doing a "raw" read. Fill
  733. * the OOB buffer with set bits and correct the block mark.
  734. */
  735. memset(nand->oob_poi, 0xff, mtd->oobsize);
  736. nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  737. mxs_nand_read_buf(mtd, nand->oob_poi, 1);
  738. }
  739. return 0;
  740. }
  741. /*
  742. * Write OOB data to NAND.
  743. */
  744. static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
  745. int page)
  746. {
  747. struct mxs_nand_info *nand_info = nand->priv;
  748. uint8_t block_mark = 0;
  749. /*
  750. * There are fundamental incompatibilities between the i.MX GPMI NFC and
  751. * the NAND Flash MTD model that make it essentially impossible to write
  752. * the out-of-band bytes.
  753. *
  754. * We permit *ONE* exception. If the *intent* of writing the OOB is to
  755. * mark a block bad, we can do that.
  756. */
  757. if (!nand_info->marking_block_bad) {
  758. printf("NXS NAND: Writing OOB isn't supported\n");
  759. return -EIO;
  760. }
  761. /* Write the block mark. */
  762. nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  763. nand->write_buf(mtd, &block_mark, 1);
  764. nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  765. /* Check if it worked. */
  766. if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
  767. return -EIO;
  768. return 0;
  769. }
  770. /*
  771. * Claims all blocks are good.
  772. *
  773. * In principle, this function is *only* called when the NAND Flash MTD system
  774. * isn't allowed to keep an in-memory bad block table, so it is forced to ask
  775. * the driver for bad block information.
  776. *
  777. * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
  778. * this function is *only* called when we take it away.
  779. *
  780. * Thus, this function is only called when we want *all* blocks to look good,
  781. * so it *always* return success.
  782. */
  783. static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  784. {
  785. return 0;
  786. }
  787. /*
  788. * Nominally, the purpose of this function is to look for or create the bad
  789. * block table. In fact, since the we call this function at the very end of
  790. * the initialization process started by nand_scan(), and we doesn't have a
  791. * more formal mechanism, we "hook" this function to continue init process.
  792. *
  793. * At this point, the physical NAND Flash chips have been identified and
  794. * counted, so we know the physical geometry. This enables us to make some
  795. * important configuration decisions.
  796. *
  797. * The return value of this function propogates directly back to this driver's
  798. * call to nand_scan(). Anything other than zero will cause this driver to
  799. * tear everything down and declare failure.
  800. */
  801. static int mxs_nand_scan_bbt(struct mtd_info *mtd)
  802. {
  803. struct nand_chip *nand = mtd->priv;
  804. struct mxs_nand_info *nand_info = nand->priv;
  805. struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
  806. uint32_t tmp;
  807. /* Configure BCH and set NFC geometry */
  808. mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
  809. /* Configure layout 0 */
  810. tmp = (mxs_nand_ecc_chunk_cnt(mtd->writesize) - 1)
  811. << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
  812. tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
  813. tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
  814. << BCH_FLASHLAYOUT0_ECC0_OFFSET;
  815. tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE
  816. >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
  817. writel(tmp, &bch_regs->hw_bch_flash0layout0);
  818. tmp = (mtd->writesize + mtd->oobsize)
  819. << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
  820. tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
  821. << BCH_FLASHLAYOUT1_ECCN_OFFSET;
  822. tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE
  823. >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
  824. writel(tmp, &bch_regs->hw_bch_flash0layout1);
  825. /* Set *all* chip selects to use layout 0 */
  826. writel(0, &bch_regs->hw_bch_layoutselect);
  827. /* Enable BCH complete interrupt */
  828. writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
  829. /* Hook some operations at the MTD level. */
  830. if (mtd->_read_oob != mxs_nand_hook_read_oob) {
  831. nand_info->hooked_read_oob = mtd->_read_oob;
  832. mtd->_read_oob = mxs_nand_hook_read_oob;
  833. }
  834. if (mtd->_write_oob != mxs_nand_hook_write_oob) {
  835. nand_info->hooked_write_oob = mtd->_write_oob;
  836. mtd->_write_oob = mxs_nand_hook_write_oob;
  837. }
  838. if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
  839. nand_info->hooked_block_markbad = mtd->_block_markbad;
  840. mtd->_block_markbad = mxs_nand_hook_block_markbad;
  841. }
  842. /* We use the reference implementation for bad block management. */
  843. return nand_default_bbt(mtd);
  844. }
  845. /*
  846. * Allocate DMA buffers
  847. */
  848. int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
  849. {
  850. uint8_t *buf;
  851. const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
  852. nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
  853. /* DMA buffers */
  854. buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
  855. if (!buf) {
  856. printf("MXS NAND: Error allocating DMA buffers\n");
  857. return -ENOMEM;
  858. }
  859. memset(buf, 0, nand_info->data_buf_size);
  860. nand_info->data_buf = buf;
  861. nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
  862. /* Command buffers */
  863. nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
  864. MXS_NAND_COMMAND_BUFFER_SIZE);
  865. if (!nand_info->cmd_buf) {
  866. free(buf);
  867. printf("MXS NAND: Error allocating command buffers\n");
  868. return -ENOMEM;
  869. }
  870. memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
  871. nand_info->cmd_queue_len = 0;
  872. return 0;
  873. }
  874. /*
  875. * Initializes the NFC hardware.
  876. */
  877. int mxs_nand_init(struct mxs_nand_info *info)
  878. {
  879. struct mxs_gpmi_regs *gpmi_regs =
  880. (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
  881. struct mxs_bch_regs *bch_regs =
  882. (struct mxs_bch_regs *)MXS_BCH_BASE;
  883. int i = 0, j;
  884. info->desc = malloc(sizeof(struct mxs_dma_desc *) *
  885. MXS_NAND_DMA_DESCRIPTOR_COUNT);
  886. if (!info->desc)
  887. goto err1;
  888. /* Allocate the DMA descriptors. */
  889. for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
  890. info->desc[i] = mxs_dma_desc_alloc();
  891. if (!info->desc[i])
  892. goto err2;
  893. }
  894. /* Init the DMA controller. */
  895. for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
  896. j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
  897. if (mxs_dma_init_channel(j))
  898. goto err3;
  899. }
  900. /* Reset the GPMI block. */
  901. mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
  902. mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
  903. /*
  904. * Choose NAND mode, set IRQ polarity, disable write protection and
  905. * select BCH ECC.
  906. */
  907. clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1,
  908. GPMI_CTRL1_GPMI_MODE,
  909. GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
  910. GPMI_CTRL1_BCH_MODE);
  911. return 0;
  912. err3:
  913. for (--j; j >= 0; j--)
  914. mxs_dma_release(j);
  915. err2:
  916. free(info->desc);
  917. err1:
  918. for (--i; i >= 0; i--)
  919. mxs_dma_desc_free(info->desc[i]);
  920. printf("MXS NAND: Unable to allocate DMA descriptors\n");
  921. return -ENOMEM;
  922. }
  923. /*!
  924. * This function is called during the driver binding process.
  925. *
  926. * @param pdev the device structure used to store device specific
  927. * information that is used by the suspend, resume and
  928. * remove functions
  929. *
  930. * @return The function always returns 0.
  931. */
  932. int board_nand_init(struct nand_chip *nand)
  933. {
  934. struct mxs_nand_info *nand_info;
  935. int err;
  936. nand_info = malloc(sizeof(struct mxs_nand_info));
  937. if (!nand_info) {
  938. printf("MXS NAND: Failed to allocate private data\n");
  939. return -ENOMEM;
  940. }
  941. memset(nand_info, 0, sizeof(struct mxs_nand_info));
  942. err = mxs_nand_alloc_buffers(nand_info);
  943. if (err)
  944. goto err1;
  945. err = mxs_nand_init(nand_info);
  946. if (err)
  947. goto err2;
  948. memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
  949. nand->priv = nand_info;
  950. nand->options |= NAND_NO_SUBPAGE_WRITE;
  951. nand->cmd_ctrl = mxs_nand_cmd_ctrl;
  952. nand->dev_ready = mxs_nand_device_ready;
  953. nand->select_chip = mxs_nand_select_chip;
  954. nand->block_bad = mxs_nand_block_bad;
  955. nand->scan_bbt = mxs_nand_scan_bbt;
  956. nand->read_byte = mxs_nand_read_byte;
  957. nand->read_buf = mxs_nand_read_buf;
  958. nand->write_buf = mxs_nand_write_buf;
  959. nand->ecc.read_page = mxs_nand_ecc_read_page;
  960. nand->ecc.write_page = mxs_nand_ecc_write_page;
  961. nand->ecc.read_oob = mxs_nand_ecc_read_oob;
  962. nand->ecc.write_oob = mxs_nand_ecc_write_oob;
  963. nand->ecc.layout = &fake_ecc_layout;
  964. nand->ecc.mode = NAND_ECC_HW;
  965. nand->ecc.bytes = 9;
  966. nand->ecc.size = 512;
  967. nand->ecc.strength = 8;
  968. return 0;
  969. err2:
  970. free(nand_info->data_buf);
  971. free(nand_info->cmd_buf);
  972. err1:
  973. free(nand_info);
  974. return err;
  975. }