mxs_nand.c 32 KB

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