mxs_nand.c 32 KB

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