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