mxs_nand.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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 <linux/mtd/mtd.h>
  28. #include <linux/mtd/nand.h>
  29. #include <linux/types.h>
  30. #include <common.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/arch/sys_proto.h>
  37. #include <asm/arch/dma.h>
  38. #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
  39. #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
  40. #define MXS_NAND_METADATA_SIZE 10
  41. #define MXS_NAND_COMMAND_BUFFER_SIZE 32
  42. #define MXS_NAND_BCH_TIMEOUT 10000
  43. struct mxs_nand_info {
  44. int cur_chip;
  45. uint32_t cmd_queue_len;
  46. uint32_t data_buf_size;
  47. uint8_t *cmd_buf;
  48. uint8_t *data_buf;
  49. uint8_t *oob_buf;
  50. uint8_t marking_block_bad;
  51. uint8_t raw_oob_mode;
  52. /* Functions with altered behaviour */
  53. int (*hooked_read_oob)(struct mtd_info *mtd,
  54. loff_t from, struct mtd_oob_ops *ops);
  55. int (*hooked_write_oob)(struct mtd_info *mtd,
  56. loff_t to, struct mtd_oob_ops *ops);
  57. int (*hooked_block_markbad)(struct mtd_info *mtd,
  58. loff_t ofs);
  59. /* DMA descriptors */
  60. struct mxs_dma_desc **desc;
  61. uint32_t desc_index;
  62. };
  63. struct nand_ecclayout fake_ecc_layout;
  64. /*
  65. * Cache management functions
  66. */
  67. #ifndef CONFIG_SYS_DCACHE_OFF
  68. static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
  69. {
  70. uint32_t addr = (uint32_t)info->data_buf;
  71. flush_dcache_range(addr, addr + info->data_buf_size);
  72. }
  73. static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
  74. {
  75. uint32_t addr = (uint32_t)info->data_buf;
  76. invalidate_dcache_range(addr, addr + info->data_buf_size);
  77. }
  78. static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
  79. {
  80. uint32_t addr = (uint32_t)info->cmd_buf;
  81. flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
  82. }
  83. #else
  84. static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
  85. static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
  86. static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
  87. #endif
  88. static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
  89. {
  90. struct mxs_dma_desc *desc;
  91. if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
  92. printf("MXS NAND: Too many DMA descriptors requested\n");
  93. return NULL;
  94. }
  95. desc = info->desc[info->desc_index];
  96. info->desc_index++;
  97. return desc;
  98. }
  99. static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
  100. {
  101. int i;
  102. struct mxs_dma_desc *desc;
  103. for (i = 0; i < info->desc_index; i++) {
  104. desc = info->desc[i];
  105. memset(desc, 0, sizeof(struct mxs_dma_desc));
  106. desc->address = (dma_addr_t)desc;
  107. }
  108. info->desc_index = 0;
  109. }
  110. static uint32_t mxs_nand_ecc_chunk_cnt(uint32_t page_data_size)
  111. {
  112. return page_data_size / MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
  113. }
  114. static uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength)
  115. {
  116. return ecc_strength * 13;
  117. }
  118. static uint32_t mxs_nand_aux_status_offset(void)
  119. {
  120. return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
  121. }
  122. static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size,
  123. uint32_t page_oob_size)
  124. {
  125. if (page_data_size == 2048)
  126. return 8;
  127. if (page_data_size == 4096) {
  128. if (page_oob_size == 128)
  129. return 8;
  130. if (page_oob_size == 218)
  131. return 16;
  132. }
  133. return 0;
  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 mx28_bch_regs *bch_regs = (struct mx28_bch_regs *)MXS_BCH_BASE;
  192. int timeout = MXS_NAND_BCH_TIMEOUT;
  193. int ret;
  194. ret = mx28_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 mx28_gpmi_regs *gpmi_regs =
  281. (struct mx28_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 | (4 << 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. (4 << 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 page)
  449. {
  450. struct mxs_nand_info *nand_info = nand->priv;
  451. struct mxs_dma_desc *d;
  452. uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
  453. uint32_t corrected = 0, failed = 0;
  454. uint8_t *status;
  455. int i, ret;
  456. /* Compile the DMA descriptor - wait for ready. */
  457. d = mxs_nand_get_dma_desc(nand_info);
  458. d->cmd.data =
  459. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
  460. MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
  461. (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  462. d->cmd.address = 0;
  463. d->cmd.pio_words[0] =
  464. GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
  465. GPMI_CTRL0_WORD_LENGTH |
  466. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  467. GPMI_CTRL0_ADDRESS_NAND_DATA;
  468. mxs_dma_desc_append(channel, d);
  469. /* Compile the DMA descriptor - enable the BCH block and read. */
  470. d = mxs_nand_get_dma_desc(nand_info);
  471. d->cmd.data =
  472. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
  473. MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  474. d->cmd.address = 0;
  475. d->cmd.pio_words[0] =
  476. GPMI_CTRL0_COMMAND_MODE_READ |
  477. GPMI_CTRL0_WORD_LENGTH |
  478. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  479. GPMI_CTRL0_ADDRESS_NAND_DATA |
  480. (mtd->writesize + mtd->oobsize);
  481. d->cmd.pio_words[1] = 0;
  482. d->cmd.pio_words[2] =
  483. GPMI_ECCCTRL_ENABLE_ECC |
  484. GPMI_ECCCTRL_ECC_CMD_DECODE |
  485. GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
  486. d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
  487. d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
  488. d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
  489. mxs_dma_desc_append(channel, d);
  490. /* Compile the DMA descriptor - disable the BCH block. */
  491. d = mxs_nand_get_dma_desc(nand_info);
  492. d->cmd.data =
  493. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
  494. MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
  495. (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  496. d->cmd.address = 0;
  497. d->cmd.pio_words[0] =
  498. GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
  499. GPMI_CTRL0_WORD_LENGTH |
  500. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  501. GPMI_CTRL0_ADDRESS_NAND_DATA |
  502. (mtd->writesize + mtd->oobsize);
  503. d->cmd.pio_words[1] = 0;
  504. d->cmd.pio_words[2] = 0;
  505. mxs_dma_desc_append(channel, d);
  506. /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
  507. d = mxs_nand_get_dma_desc(nand_info);
  508. d->cmd.data =
  509. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
  510. MXS_DMA_DESC_DEC_SEM;
  511. d->cmd.address = 0;
  512. mxs_dma_desc_append(channel, d);
  513. /* Execute the DMA chain. */
  514. ret = mxs_dma_go(channel);
  515. if (ret) {
  516. printf("MXS NAND: DMA read error\n");
  517. goto rtn;
  518. }
  519. ret = mxs_nand_wait_for_bch_complete();
  520. if (ret) {
  521. printf("MXS NAND: BCH read timeout\n");
  522. goto rtn;
  523. }
  524. /* Invalidate caches */
  525. mxs_nand_inval_data_buf(nand_info);
  526. /* Read DMA completed, now do the mark swapping. */
  527. mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
  528. /* Loop over status bytes, accumulating ECC status. */
  529. status = nand_info->oob_buf + mxs_nand_aux_status_offset();
  530. for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) {
  531. if (status[i] == 0x00)
  532. continue;
  533. if (status[i] == 0xff)
  534. continue;
  535. if (status[i] == 0xfe) {
  536. failed++;
  537. continue;
  538. }
  539. corrected += status[i];
  540. }
  541. /* Propagate ECC status to the owning MTD. */
  542. mtd->ecc_stats.failed += failed;
  543. mtd->ecc_stats.corrected += corrected;
  544. /*
  545. * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
  546. * details about our policy for delivering the OOB.
  547. *
  548. * We fill the caller's buffer with set bits, and then copy the block
  549. * mark to the caller's buffer. Note that, if block mark swapping was
  550. * necessary, it has already been done, so we can rely on the first
  551. * byte of the auxiliary buffer to contain the block mark.
  552. */
  553. memset(nand->oob_poi, 0xff, mtd->oobsize);
  554. nand->oob_poi[0] = nand_info->oob_buf[0];
  555. memcpy(buf, nand_info->data_buf, mtd->writesize);
  556. rtn:
  557. mxs_nand_return_dma_descs(nand_info);
  558. return ret;
  559. }
  560. /*
  561. * Write a page to NAND.
  562. */
  563. static void mxs_nand_ecc_write_page(struct mtd_info *mtd,
  564. struct nand_chip *nand, const uint8_t *buf)
  565. {
  566. struct mxs_nand_info *nand_info = nand->priv;
  567. struct mxs_dma_desc *d;
  568. uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
  569. int ret;
  570. memcpy(nand_info->data_buf, buf, mtd->writesize);
  571. memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
  572. /* Handle block mark swapping. */
  573. mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
  574. /* Compile the DMA descriptor - write data. */
  575. d = mxs_nand_get_dma_desc(nand_info);
  576. d->cmd.data =
  577. MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
  578. MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
  579. (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
  580. d->cmd.address = 0;
  581. d->cmd.pio_words[0] =
  582. GPMI_CTRL0_COMMAND_MODE_WRITE |
  583. GPMI_CTRL0_WORD_LENGTH |
  584. (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
  585. GPMI_CTRL0_ADDRESS_NAND_DATA;
  586. d->cmd.pio_words[1] = 0;
  587. d->cmd.pio_words[2] =
  588. GPMI_ECCCTRL_ENABLE_ECC |
  589. GPMI_ECCCTRL_ECC_CMD_ENCODE |
  590. GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
  591. d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
  592. d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
  593. d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
  594. mxs_dma_desc_append(channel, d);
  595. /* Flush caches */
  596. mxs_nand_flush_data_buf(nand_info);
  597. /* Execute the DMA chain. */
  598. ret = mxs_dma_go(channel);
  599. if (ret) {
  600. printf("MXS NAND: DMA write error\n");
  601. goto rtn;
  602. }
  603. ret = mxs_nand_wait_for_bch_complete();
  604. if (ret) {
  605. printf("MXS NAND: BCH write timeout\n");
  606. goto rtn;
  607. }
  608. rtn:
  609. mxs_nand_return_dma_descs(nand_info);
  610. }
  611. /*
  612. * Read OOB from NAND.
  613. *
  614. * This function is a veneer that replaces the function originally installed by
  615. * the NAND Flash MTD code.
  616. */
  617. static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
  618. struct mtd_oob_ops *ops)
  619. {
  620. struct nand_chip *chip = mtd->priv;
  621. struct mxs_nand_info *nand_info = chip->priv;
  622. int ret;
  623. if (ops->mode == MTD_OOB_RAW)
  624. nand_info->raw_oob_mode = 1;
  625. else
  626. nand_info->raw_oob_mode = 0;
  627. ret = nand_info->hooked_read_oob(mtd, from, ops);
  628. nand_info->raw_oob_mode = 0;
  629. return ret;
  630. }
  631. /*
  632. * Write OOB to NAND.
  633. *
  634. * This function is a veneer that replaces the function originally installed by
  635. * the NAND Flash MTD code.
  636. */
  637. static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
  638. struct mtd_oob_ops *ops)
  639. {
  640. struct nand_chip *chip = mtd->priv;
  641. struct mxs_nand_info *nand_info = chip->priv;
  642. int ret;
  643. if (ops->mode == MTD_OOB_RAW)
  644. nand_info->raw_oob_mode = 1;
  645. else
  646. nand_info->raw_oob_mode = 0;
  647. ret = nand_info->hooked_write_oob(mtd, to, ops);
  648. nand_info->raw_oob_mode = 0;
  649. return ret;
  650. }
  651. /*
  652. * Mark a block bad in NAND.
  653. *
  654. * This function is a veneer that replaces the function originally installed by
  655. * the NAND Flash MTD code.
  656. */
  657. static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
  658. {
  659. struct nand_chip *chip = mtd->priv;
  660. struct mxs_nand_info *nand_info = chip->priv;
  661. int ret;
  662. nand_info->marking_block_bad = 1;
  663. ret = nand_info->hooked_block_markbad(mtd, ofs);
  664. nand_info->marking_block_bad = 0;
  665. return ret;
  666. }
  667. /*
  668. * There are several places in this driver where we have to handle the OOB and
  669. * block marks. This is the function where things are the most complicated, so
  670. * this is where we try to explain it all. All the other places refer back to
  671. * here.
  672. *
  673. * These are the rules, in order of decreasing importance:
  674. *
  675. * 1) Nothing the caller does can be allowed to imperil the block mark, so all
  676. * write operations take measures to protect it.
  677. *
  678. * 2) In read operations, the first byte of the OOB we return must reflect the
  679. * true state of the block mark, no matter where that block mark appears in
  680. * the physical page.
  681. *
  682. * 3) ECC-based read operations return an OOB full of set bits (since we never
  683. * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
  684. * return).
  685. *
  686. * 4) "Raw" read operations return a direct view of the physical bytes in the
  687. * page, using the conventional definition of which bytes are data and which
  688. * are OOB. This gives the caller a way to see the actual, physical bytes
  689. * in the page, without the distortions applied by our ECC engine.
  690. *
  691. * What we do for this specific read operation depends on whether we're doing
  692. * "raw" read, or an ECC-based read.
  693. *
  694. * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
  695. * easy. When reading a page, for example, the NAND Flash MTD code calls our
  696. * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
  697. * ECC-based or raw view of the page is implicit in which function it calls
  698. * (there is a similar pair of ECC-based/raw functions for writing).
  699. *
  700. * Since MTD assumes the OOB is not covered by ECC, there is no pair of
  701. * ECC-based/raw functions for reading or or writing the OOB. The fact that the
  702. * caller wants an ECC-based or raw view of the page is not propagated down to
  703. * this driver.
  704. *
  705. * Since our OOB *is* covered by ECC, we need this information. So, we hook the
  706. * ecc.read_oob and ecc.write_oob function pointers in the owning
  707. * struct mtd_info with our own functions. These hook functions set the
  708. * raw_oob_mode field so that, when control finally arrives here, we'll know
  709. * what to do.
  710. */
  711. static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
  712. int page, int cmd)
  713. {
  714. struct mxs_nand_info *nand_info = nand->priv;
  715. /*
  716. * First, fill in the OOB buffer. If we're doing a raw read, we need to
  717. * get the bytes from the physical page. If we're not doing a raw read,
  718. * we need to fill the buffer with set bits.
  719. */
  720. if (nand_info->raw_oob_mode) {
  721. /*
  722. * If control arrives here, we're doing a "raw" read. Send the
  723. * command to read the conventional OOB and read it.
  724. */
  725. nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  726. nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
  727. } else {
  728. /*
  729. * If control arrives here, we're not doing a "raw" read. Fill
  730. * the OOB buffer with set bits and correct the block mark.
  731. */
  732. memset(nand->oob_poi, 0xff, mtd->oobsize);
  733. nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  734. mxs_nand_read_buf(mtd, nand->oob_poi, 1);
  735. }
  736. return 0;
  737. }
  738. /*
  739. * Write OOB data to NAND.
  740. */
  741. static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
  742. int page)
  743. {
  744. struct mxs_nand_info *nand_info = nand->priv;
  745. uint8_t block_mark = 0;
  746. /*
  747. * There are fundamental incompatibilities between the i.MX GPMI NFC and
  748. * the NAND Flash MTD model that make it essentially impossible to write
  749. * the out-of-band bytes.
  750. *
  751. * We permit *ONE* exception. If the *intent* of writing the OOB is to
  752. * mark a block bad, we can do that.
  753. */
  754. if (!nand_info->marking_block_bad) {
  755. printf("NXS NAND: Writing OOB isn't supported\n");
  756. return -EIO;
  757. }
  758. /* Write the block mark. */
  759. nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  760. nand->write_buf(mtd, &block_mark, 1);
  761. nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  762. /* Check if it worked. */
  763. if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
  764. return -EIO;
  765. return 0;
  766. }
  767. /*
  768. * Claims all blocks are good.
  769. *
  770. * In principle, this function is *only* called when the NAND Flash MTD system
  771. * isn't allowed to keep an in-memory bad block table, so it is forced to ask
  772. * the driver for bad block information.
  773. *
  774. * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
  775. * this function is *only* called when we take it away.
  776. *
  777. * Thus, this function is only called when we want *all* blocks to look good,
  778. * so it *always* return success.
  779. */
  780. static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  781. {
  782. return 0;
  783. }
  784. /*
  785. * Nominally, the purpose of this function is to look for or create the bad
  786. * block table. In fact, since the we call this function at the very end of
  787. * the initialization process started by nand_scan(), and we doesn't have a
  788. * more formal mechanism, we "hook" this function to continue init process.
  789. *
  790. * At this point, the physical NAND Flash chips have been identified and
  791. * counted, so we know the physical geometry. This enables us to make some
  792. * important configuration decisions.
  793. *
  794. * The return value of this function propogates directly back to this driver's
  795. * call to nand_scan(). Anything other than zero will cause this driver to
  796. * tear everything down and declare failure.
  797. */
  798. static int mxs_nand_scan_bbt(struct mtd_info *mtd)
  799. {
  800. struct nand_chip *nand = mtd->priv;
  801. struct mxs_nand_info *nand_info = nand->priv;
  802. struct mx28_bch_regs *bch_regs = (struct mx28_bch_regs *)MXS_BCH_BASE;
  803. uint32_t tmp;
  804. /* Configure BCH and set NFC geometry */
  805. mx28_reset_block(&bch_regs->hw_bch_ctrl_reg);
  806. /* Configure layout 0 */
  807. tmp = (mxs_nand_ecc_chunk_cnt(mtd->writesize) - 1)
  808. << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
  809. tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
  810. tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
  811. << BCH_FLASHLAYOUT0_ECC0_OFFSET;
  812. tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
  813. writel(tmp, &bch_regs->hw_bch_flash0layout0);
  814. tmp = (mtd->writesize + mtd->oobsize)
  815. << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
  816. tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
  817. << BCH_FLASHLAYOUT1_ECCN_OFFSET;
  818. tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
  819. writel(tmp, &bch_regs->hw_bch_flash0layout1);
  820. /* Set *all* chip selects to use layout 0 */
  821. writel(0, &bch_regs->hw_bch_layoutselect);
  822. /* Enable BCH complete interrupt */
  823. writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
  824. /* Hook some operations at the MTD level. */
  825. if (mtd->read_oob != mxs_nand_hook_read_oob) {
  826. nand_info->hooked_read_oob = mtd->read_oob;
  827. mtd->read_oob = mxs_nand_hook_read_oob;
  828. }
  829. if (mtd->write_oob != mxs_nand_hook_write_oob) {
  830. nand_info->hooked_write_oob = mtd->write_oob;
  831. mtd->write_oob = mxs_nand_hook_write_oob;
  832. }
  833. if (mtd->block_markbad != mxs_nand_hook_block_markbad) {
  834. nand_info->hooked_block_markbad = mtd->block_markbad;
  835. mtd->block_markbad = mxs_nand_hook_block_markbad;
  836. }
  837. /* We use the reference implementation for bad block management. */
  838. return nand_default_bbt(mtd);
  839. }
  840. /*
  841. * Allocate DMA buffers
  842. */
  843. int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
  844. {
  845. uint8_t *buf;
  846. const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
  847. nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
  848. /* DMA buffers */
  849. buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
  850. if (!buf) {
  851. printf("MXS NAND: Error allocating DMA buffers\n");
  852. return -ENOMEM;
  853. }
  854. memset(buf, 0, nand_info->data_buf_size);
  855. nand_info->data_buf = buf;
  856. nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
  857. /* Command buffers */
  858. nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
  859. MXS_NAND_COMMAND_BUFFER_SIZE);
  860. if (!nand_info->cmd_buf) {
  861. free(buf);
  862. printf("MXS NAND: Error allocating command buffers\n");
  863. return -ENOMEM;
  864. }
  865. memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
  866. nand_info->cmd_queue_len = 0;
  867. return 0;
  868. }
  869. /*
  870. * Initializes the NFC hardware.
  871. */
  872. int mxs_nand_init(struct mxs_nand_info *info)
  873. {
  874. struct mx28_gpmi_regs *gpmi_regs =
  875. (struct mx28_gpmi_regs *)MXS_GPMI_BASE;
  876. int i = 0;
  877. info->desc = malloc(sizeof(struct mxs_dma_desc *) *
  878. MXS_NAND_DMA_DESCRIPTOR_COUNT);
  879. if (!info->desc)
  880. goto err1;
  881. /* Allocate the DMA descriptors. */
  882. for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
  883. info->desc[i] = mxs_dma_desc_alloc();
  884. if (!info->desc[i])
  885. goto err2;
  886. }
  887. /* Init the DMA controller. */
  888. mxs_dma_init();
  889. /* Reset the GPMI block. */
  890. mx28_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
  891. /*
  892. * Choose NAND mode, set IRQ polarity, disable write protection and
  893. * select BCH ECC.
  894. */
  895. clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1,
  896. GPMI_CTRL1_GPMI_MODE,
  897. GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
  898. GPMI_CTRL1_BCH_MODE);
  899. return 0;
  900. err2:
  901. free(info->desc);
  902. err1:
  903. for (--i; i >= 0; i--)
  904. mxs_dma_desc_free(info->desc[i]);
  905. printf("MXS NAND: Unable to allocate DMA descriptors\n");
  906. return -ENOMEM;
  907. }
  908. /*!
  909. * This function is called during the driver binding process.
  910. *
  911. * @param pdev the device structure used to store device specific
  912. * information that is used by the suspend, resume and
  913. * remove functions
  914. *
  915. * @return The function always returns 0.
  916. */
  917. int board_nand_init(struct nand_chip *nand)
  918. {
  919. struct mxs_nand_info *nand_info;
  920. int err;
  921. nand_info = malloc(sizeof(struct mxs_nand_info));
  922. if (!nand_info) {
  923. printf("MXS NAND: Failed to allocate private data\n");
  924. return -ENOMEM;
  925. }
  926. memset(nand_info, 0, sizeof(struct mxs_nand_info));
  927. err = mxs_nand_alloc_buffers(nand_info);
  928. if (err)
  929. goto err1;
  930. err = mxs_nand_init(nand_info);
  931. if (err)
  932. goto err2;
  933. memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
  934. nand->priv = nand_info;
  935. nand->options |= NAND_NO_SUBPAGE_WRITE;
  936. nand->cmd_ctrl = mxs_nand_cmd_ctrl;
  937. nand->dev_ready = mxs_nand_device_ready;
  938. nand->select_chip = mxs_nand_select_chip;
  939. nand->block_bad = mxs_nand_block_bad;
  940. nand->scan_bbt = mxs_nand_scan_bbt;
  941. nand->read_byte = mxs_nand_read_byte;
  942. nand->read_buf = mxs_nand_read_buf;
  943. nand->write_buf = mxs_nand_write_buf;
  944. nand->ecc.read_page = mxs_nand_ecc_read_page;
  945. nand->ecc.write_page = mxs_nand_ecc_write_page;
  946. nand->ecc.read_oob = mxs_nand_ecc_read_oob;
  947. nand->ecc.write_oob = mxs_nand_ecc_write_oob;
  948. nand->ecc.layout = &fake_ecc_layout;
  949. nand->ecc.mode = NAND_ECC_HW;
  950. nand->ecc.bytes = 9;
  951. nand->ecc.size = 512;
  952. return 0;
  953. err2:
  954. free(nand_info->data_buf);
  955. free(nand_info->cmd_buf);
  956. err1:
  957. free(nand_info);
  958. return err;
  959. }