mxs_nand.c 32 KB

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