mxs_nand.c 32 KB

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