mxs_nand.c 32 KB

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