lpc32xx_nand_mlc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * LPC32xx MLC NAND flash controller driver
  3. *
  4. * (C) Copyright 2014 3ADEV <http://3adev.com>
  5. * Written by Albert ARIBAUD <albert.aribaud@3adev.fr>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. *
  9. * NOTE:
  10. *
  11. * The MLC NAND flash controller provides hardware Reed-Solomon ECC
  12. * covering in- and out-of-band data together. Therefore, in- and out-
  13. * of-band data must be written together in order to have a valid ECC.
  14. *
  15. * Consequently, pages with meaningful in-band data are written with
  16. * blank (all-ones) out-of-band data and a valid ECC, and any later
  17. * out-of-band data write will void the ECC.
  18. *
  19. * Therefore, code which reads such late-written out-of-band data
  20. * should not rely on the ECC validity.
  21. */
  22. #include <common.h>
  23. #include <nand.h>
  24. #include <linux/errno.h>
  25. #include <asm/io.h>
  26. #include <nand.h>
  27. #include <asm/arch/clk.h>
  28. #include <asm/arch/sys_proto.h>
  29. /*
  30. * MLC NAND controller registers.
  31. */
  32. struct lpc32xx_nand_mlc_registers {
  33. u8 buff[32768]; /* controller's serial data buffer */
  34. u8 data[32768]; /* NAND's raw data buffer */
  35. u32 cmd;
  36. u32 addr;
  37. u32 ecc_enc_reg;
  38. u32 ecc_dec_reg;
  39. u32 ecc_auto_enc_reg;
  40. u32 ecc_auto_dec_reg;
  41. u32 rpr;
  42. u32 wpr;
  43. u32 rubp;
  44. u32 robp;
  45. u32 sw_wp_add_low;
  46. u32 sw_wp_add_hig;
  47. u32 icr;
  48. u32 time_reg;
  49. u32 irq_mr;
  50. u32 irq_sr;
  51. u32 lock_pr;
  52. u32 isr;
  53. u32 ceh;
  54. };
  55. /* LOCK_PR register defines */
  56. #define LOCK_PR_UNLOCK_KEY 0x0000A25E /* Magic unlock value */
  57. /* ICR defines */
  58. #define ICR_LARGE_BLOCKS 0x00000004 /* configure for 2KB blocks */
  59. #define ICR_ADDR4 0x00000002 /* configure for 4-word addrs */
  60. /* CEH defines */
  61. #define CEH_NORMAL_CE 0x00000001 /* do not force CE ON */
  62. /* ISR register defines */
  63. #define ISR_NAND_READY 0x00000001
  64. #define ISR_CONTROLLER_READY 0x00000002
  65. #define ISR_ECC_READY 0x00000004
  66. #define ISR_DECODER_ERRORS(s) ((((s) >> 4) & 3)+1)
  67. #define ISR_DECODER_FAILURE 0x00000040
  68. #define ISR_DECODER_ERROR 0x00000008
  69. /* time-out for NAND chip / controller loops, in us */
  70. #define LPC32X_NAND_TIMEOUT 5000
  71. /*
  72. * There is a single instance of the NAND MLC controller
  73. */
  74. static struct lpc32xx_nand_mlc_registers __iomem *lpc32xx_nand_mlc_registers
  75. = (struct lpc32xx_nand_mlc_registers __iomem *)MLC_NAND_BASE;
  76. #define clkdiv(v, w, o) (((1+(clk/v)) & w) << o)
  77. /**
  78. * OOB data in each small page are 6 'free' then 10 ECC bytes.
  79. * To make things easier, when reading large pages, the four pages'
  80. * 'free' OOB bytes are grouped in the first 24 bytes of the OOB buffer,
  81. * while the the four ECC bytes are groupe in its last 40 bytes.
  82. *
  83. * The struct below represents how free vs ecc oob bytes are stored
  84. * in the buffer.
  85. *
  86. * Note: the OOB bytes contain the bad block marker at offsets 0 and 1.
  87. */
  88. struct lpc32xx_oob {
  89. struct {
  90. uint8_t free_oob_bytes[6];
  91. } free[4];
  92. struct {
  93. uint8_t ecc_oob_bytes[10];
  94. } ecc[4];
  95. };
  96. /*
  97. * Initialize the controller
  98. */
  99. static void lpc32xx_nand_init(void)
  100. {
  101. unsigned int clk;
  102. /* Configure controller for no software write protection, x8 bus
  103. width, large block device, and 4 address words */
  104. /* unlock controller registers with magic key */
  105. writel(LOCK_PR_UNLOCK_KEY,
  106. &lpc32xx_nand_mlc_registers->lock_pr);
  107. /* enable large blocks and large NANDs */
  108. writel(ICR_LARGE_BLOCKS | ICR_ADDR4,
  109. &lpc32xx_nand_mlc_registers->icr);
  110. /* Make sure MLC interrupts are disabled */
  111. writel(0, &lpc32xx_nand_mlc_registers->irq_mr);
  112. /* Normal chip enable operation */
  113. writel(CEH_NORMAL_CE,
  114. &lpc32xx_nand_mlc_registers->ceh);
  115. /* Setup NAND timing */
  116. clk = get_hclk_clk_rate();
  117. writel(
  118. clkdiv(CONFIG_LPC32XX_NAND_MLC_TCEA_DELAY, 0x03, 24) |
  119. clkdiv(CONFIG_LPC32XX_NAND_MLC_BUSY_DELAY, 0x1F, 19) |
  120. clkdiv(CONFIG_LPC32XX_NAND_MLC_NAND_TA, 0x07, 16) |
  121. clkdiv(CONFIG_LPC32XX_NAND_MLC_RD_HIGH, 0x0F, 12) |
  122. clkdiv(CONFIG_LPC32XX_NAND_MLC_RD_LOW, 0x0F, 8) |
  123. clkdiv(CONFIG_LPC32XX_NAND_MLC_WR_HIGH, 0x0F, 4) |
  124. clkdiv(CONFIG_LPC32XX_NAND_MLC_WR_LOW, 0x0F, 0),
  125. &lpc32xx_nand_mlc_registers->time_reg);
  126. }
  127. #if !defined(CONFIG_SPL_BUILD)
  128. /**
  129. * lpc32xx_cmd_ctrl - write command to either cmd or data register
  130. */
  131. static void lpc32xx_cmd_ctrl(struct mtd_info *mtd, int cmd,
  132. unsigned int ctrl)
  133. {
  134. if (cmd == NAND_CMD_NONE)
  135. return;
  136. if (ctrl & NAND_CLE)
  137. writeb(cmd & 0Xff, &lpc32xx_nand_mlc_registers->cmd);
  138. else if (ctrl & NAND_ALE)
  139. writeb(cmd & 0Xff, &lpc32xx_nand_mlc_registers->addr);
  140. }
  141. /**
  142. * lpc32xx_read_byte - read a byte from the NAND
  143. * @mtd: MTD device structure
  144. */
  145. static uint8_t lpc32xx_read_byte(struct mtd_info *mtd)
  146. {
  147. return readb(&lpc32xx_nand_mlc_registers->data);
  148. }
  149. /**
  150. * lpc32xx_dev_ready - test if NAND device (actually controller) is ready
  151. * @mtd: MTD device structure
  152. * @mode: mode to set the ECC HW to.
  153. */
  154. static int lpc32xx_dev_ready(struct mtd_info *mtd)
  155. {
  156. /* means *controller* ready for us */
  157. int status = readl(&lpc32xx_nand_mlc_registers->isr);
  158. return status & ISR_CONTROLLER_READY;
  159. }
  160. /**
  161. * ECC layout -- this is needed whatever ECC mode we are using.
  162. * In a 2KB (4*512B) page, R/S codes occupy 40 (4*10) bytes.
  163. * To make U-Boot's life easier, we pack 'useable' OOB at the
  164. * front and R/S ECC at the back.
  165. */
  166. static struct nand_ecclayout lpc32xx_largepage_ecclayout = {
  167. .eccbytes = 40,
  168. .eccpos = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
  169. 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  170. 44, 45, 46, 47, 48, 48, 50, 51, 52, 53,
  171. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  172. },
  173. .oobfree = {
  174. /* bytes 0 and 1 are used for the bad block marker */
  175. {
  176. .offset = 2,
  177. .length = 22
  178. },
  179. }
  180. };
  181. /**
  182. * lpc32xx_read_page_hwecc - read in- and out-of-band data with ECC
  183. * @mtd: mtd info structure
  184. * @chip: nand chip info structure
  185. * @buf: buffer to store read data
  186. * @oob_required: caller requires OOB data read to chip->oob_poi
  187. * @page: page number to read
  188. *
  189. * Use large block Auto Decode Read Mode(1) as described in User Manual
  190. * section 8.6.2.1.
  191. *
  192. * The initial Read Mode and Read Start commands are sent by the caller.
  193. *
  194. * ECC will be false if out-of-band data has been updated since in-band
  195. * data was initially written.
  196. */
  197. static int lpc32xx_read_page_hwecc(struct mtd_info *mtd,
  198. struct nand_chip *chip, uint8_t *buf, int oob_required,
  199. int page)
  200. {
  201. unsigned int i, status, timeout, err, max_bitflips = 0;
  202. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  203. /* go through all four small pages */
  204. for (i = 0; i < 4; i++) {
  205. /* start auto decode (reads 528 NAND bytes) */
  206. writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
  207. /* wait for controller to return to ready state */
  208. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  209. status = readl(&lpc32xx_nand_mlc_registers->isr);
  210. if (status & ISR_CONTROLLER_READY)
  211. break;
  212. udelay(1);
  213. }
  214. /* if decoder failed, return failure */
  215. if (status & ISR_DECODER_FAILURE)
  216. return -1;
  217. /* keep count of maximum bitflips performed */
  218. if (status & ISR_DECODER_ERROR) {
  219. err = ISR_DECODER_ERRORS(status);
  220. if (err > max_bitflips)
  221. max_bitflips = err;
  222. }
  223. /* copy first 512 bytes into buffer */
  224. memcpy(buf+512*i, lpc32xx_nand_mlc_registers->buff, 512);
  225. /* copy next 6 bytes at front of OOB buffer */
  226. memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
  227. /* copy last 10 bytes (R/S ECC) at back of OOB buffer */
  228. memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->buff, 10);
  229. }
  230. return max_bitflips;
  231. }
  232. /**
  233. * lpc32xx_read_page_raw - read raw (in-band, out-of-band and ECC) data
  234. * @mtd: mtd info structure
  235. * @chip: nand chip info structure
  236. * @buf: buffer to store read data
  237. * @oob_required: caller requires OOB data read to chip->oob_poi
  238. * @page: page number to read
  239. *
  240. * Read NAND directly; can read pages with invalid ECC.
  241. */
  242. static int lpc32xx_read_page_raw(struct mtd_info *mtd,
  243. struct nand_chip *chip, uint8_t *buf, int oob_required,
  244. int page)
  245. {
  246. unsigned int i, status, timeout;
  247. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  248. /* when we get here we've already had the Read Mode(1) */
  249. /* go through all four small pages */
  250. for (i = 0; i < 4; i++) {
  251. /* wait for NAND to return to ready state */
  252. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  253. status = readl(&lpc32xx_nand_mlc_registers->isr);
  254. if (status & ISR_NAND_READY)
  255. break;
  256. udelay(1);
  257. }
  258. /* if NAND stalled, return failure */
  259. if (!(status & ISR_NAND_READY))
  260. return -1;
  261. /* copy first 512 bytes into buffer */
  262. memcpy(buf+512*i, lpc32xx_nand_mlc_registers->data, 512);
  263. /* copy next 6 bytes at front of OOB buffer */
  264. memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->data, 6);
  265. /* copy last 10 bytes (R/S ECC) at back of OOB buffer */
  266. memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->data, 10);
  267. }
  268. return 0;
  269. }
  270. /**
  271. * lpc32xx_read_oob - read out-of-band data
  272. * @mtd: mtd info structure
  273. * @chip: nand chip info structure
  274. * @page: page number to read
  275. *
  276. * Read out-of-band data. User Manual section 8.6.4 suggests using Read
  277. * Mode(3) which the controller will turn into a Read Mode(1) internally
  278. * but nand_base.c will turn Mode(3) into Mode(0), so let's use Mode(0)
  279. * directly.
  280. *
  281. * ECC covers in- and out-of-band data and was written when out-of-band
  282. * data was blank. Therefore, if the out-of-band being read here is not
  283. * blank, then the ECC will be false and the read will return bitflips,
  284. * even in case of ECC failure where we will return 5 bitflips. The
  285. * caller should be prepared to handle this.
  286. */
  287. static int lpc32xx_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  288. int page)
  289. {
  290. unsigned int i, status, timeout, err, max_bitflips = 0;
  291. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  292. /* No command was sent before calling read_oob() so send one */
  293. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  294. /* go through all four small pages */
  295. for (i = 0; i < 4; i++) {
  296. /* start auto decode (reads 528 NAND bytes) */
  297. writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
  298. /* wait for controller to return to ready state */
  299. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  300. status = readl(&lpc32xx_nand_mlc_registers->isr);
  301. if (status & ISR_CONTROLLER_READY)
  302. break;
  303. udelay(1);
  304. }
  305. /* if decoder failure, count 'one too many' bitflips */
  306. if (status & ISR_DECODER_FAILURE)
  307. max_bitflips = 5;
  308. /* keep count of maximum bitflips performed */
  309. if (status & ISR_DECODER_ERROR) {
  310. err = ISR_DECODER_ERRORS(status);
  311. if (err > max_bitflips)
  312. max_bitflips = err;
  313. }
  314. /* set read pointer to OOB area */
  315. writel(0, &lpc32xx_nand_mlc_registers->robp);
  316. /* copy next 6 bytes at front of OOB buffer */
  317. memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
  318. /* copy next 10 bytes (R/S ECC) at back of OOB buffer */
  319. memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->buff, 10);
  320. }
  321. return max_bitflips;
  322. }
  323. /**
  324. * lpc32xx_write_page_hwecc - write in- and out-of-band data with ECC
  325. * @mtd: mtd info structure
  326. * @chip: nand chip info structure
  327. * @buf: data buffer
  328. * @oob_required: must write chip->oob_poi to OOB
  329. *
  330. * Use large block Auto Encode as per User Manual section 8.6.4.
  331. *
  332. * The initial Write Serial Input and final Auto Program commands are
  333. * sent by the caller.
  334. */
  335. static int lpc32xx_write_page_hwecc(struct mtd_info *mtd,
  336. struct nand_chip *chip, const uint8_t *buf, int oob_required,
  337. int page)
  338. {
  339. unsigned int i, status, timeout;
  340. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  341. /* when we get here we've already had the SEQIN */
  342. for (i = 0; i < 4; i++) {
  343. /* start encode (expects 518 writes to buff) */
  344. writel(0, &lpc32xx_nand_mlc_registers->ecc_enc_reg);
  345. /* copy first 512 bytes from buffer */
  346. memcpy(&lpc32xx_nand_mlc_registers->buff, buf+512*i, 512);
  347. /* copy next 6 bytes from OOB buffer -- excluding ECC */
  348. memcpy(&lpc32xx_nand_mlc_registers->buff, &oob->free[i], 6);
  349. /* wait for ECC to return to ready state */
  350. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  351. status = readl(&lpc32xx_nand_mlc_registers->isr);
  352. if (status & ISR_ECC_READY)
  353. break;
  354. udelay(1);
  355. }
  356. /* if ECC stalled, return failure */
  357. if (!(status & ISR_ECC_READY))
  358. return -1;
  359. /* Trigger auto encode (writes 528 bytes to NAND) */
  360. writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_enc_reg);
  361. /* wait for controller to return to ready state */
  362. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  363. status = readl(&lpc32xx_nand_mlc_registers->isr);
  364. if (status & ISR_CONTROLLER_READY)
  365. break;
  366. udelay(1);
  367. }
  368. /* if controller stalled, return error */
  369. if (!(status & ISR_CONTROLLER_READY))
  370. return -1;
  371. }
  372. return 0;
  373. }
  374. /**
  375. * lpc32xx_write_page_raw - write raw (in-band, out-of-band and ECC) data
  376. * @mtd: mtd info structure
  377. * @chip: nand chip info structure
  378. * @buf: buffer to store read data
  379. * @oob_required: caller requires OOB data read to chip->oob_poi
  380. * @page: page number to read
  381. *
  382. * Use large block write but without encode.
  383. *
  384. * The initial Write Serial Input and final Auto Program commands are
  385. * sent by the caller.
  386. *
  387. * This function will write the full out-of-band data, including the
  388. * ECC area. Therefore, it can write pages with valid *or* invalid ECC.
  389. */
  390. static int lpc32xx_write_page_raw(struct mtd_info *mtd,
  391. struct nand_chip *chip, const uint8_t *buf, int oob_required,
  392. int page)
  393. {
  394. unsigned int i;
  395. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  396. /* when we get here we've already had the Read Mode(1) */
  397. for (i = 0; i < 4; i++) {
  398. /* copy first 512 bytes from buffer */
  399. memcpy(lpc32xx_nand_mlc_registers->buff, buf+512*i, 512);
  400. /* copy next 6 bytes into OOB buffer -- excluding ECC */
  401. memcpy(lpc32xx_nand_mlc_registers->buff, &oob->free[i], 6);
  402. /* copy next 10 bytes into OOB buffer -- that is 'ECC' */
  403. memcpy(lpc32xx_nand_mlc_registers->buff, &oob->ecc[i], 10);
  404. }
  405. return 0;
  406. }
  407. /**
  408. * lpc32xx_write_oob - write out-of-band data
  409. * @mtd: mtd info structure
  410. * @chip: nand chip info structure
  411. * @page: page number to read
  412. *
  413. * Since ECC covers in- and out-of-band data, writing out-of-band data
  414. * with ECC will render the page ECC wrong -- or, if the page was blank,
  415. * then it will produce a good ECC but a later in-band data write will
  416. * render it wrong.
  417. *
  418. * Therefore, do not compute or write any ECC, and always return success.
  419. *
  420. * This implies that we do four writes, since non-ECC out-of-band data
  421. * are not contiguous in a large page.
  422. */
  423. static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  424. int page)
  425. {
  426. /* update oob on all 4 subpages in sequence */
  427. unsigned int i, status, timeout;
  428. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  429. for (i = 0; i < 4; i++) {
  430. /* start data input */
  431. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x200+0x210*i, page);
  432. /* copy 6 non-ECC out-of-band bytes directly into NAND */
  433. memcpy(lpc32xx_nand_mlc_registers->data, &oob->free[i], 6);
  434. /* program page */
  435. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  436. /* wait for NAND to return to ready state */
  437. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  438. status = readl(&lpc32xx_nand_mlc_registers->isr);
  439. if (status & ISR_NAND_READY)
  440. break;
  441. udelay(1);
  442. }
  443. /* if NAND stalled, return error */
  444. if (!(status & ISR_NAND_READY))
  445. return -1;
  446. }
  447. return 0;
  448. }
  449. /**
  450. * lpc32xx_waitfunc - wait until a command is done
  451. * @mtd: MTD device structure
  452. * @chip: NAND chip structure
  453. *
  454. * Wait for controller and FLASH to both be ready.
  455. */
  456. static int lpc32xx_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  457. {
  458. int status;
  459. unsigned int timeout;
  460. /* wait until both controller and NAND are ready */
  461. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  462. status = readl(&lpc32xx_nand_mlc_registers->isr);
  463. if ((status & (ISR_CONTROLLER_READY || ISR_NAND_READY))
  464. == (ISR_CONTROLLER_READY || ISR_NAND_READY))
  465. break;
  466. udelay(1);
  467. }
  468. /* if controller or NAND stalled, return error */
  469. if ((status & (ISR_CONTROLLER_READY || ISR_NAND_READY))
  470. != (ISR_CONTROLLER_READY || ISR_NAND_READY))
  471. return -1;
  472. /* write NAND status command */
  473. writel(NAND_CMD_STATUS, &lpc32xx_nand_mlc_registers->cmd);
  474. /* read back status and return it */
  475. return readb(&lpc32xx_nand_mlc_registers->data);
  476. }
  477. /*
  478. * We are self-initializing, so we need our own chip struct
  479. */
  480. static struct nand_chip lpc32xx_chip;
  481. /*
  482. * Initialize the controller
  483. */
  484. void board_nand_init(void)
  485. {
  486. struct mtd_info *mtd = nand_to_mtd(&lpc32xx_chip);
  487. int ret;
  488. /* Set all BOARDSPECIFIC (actually core-specific) fields */
  489. lpc32xx_chip.IO_ADDR_R = &lpc32xx_nand_mlc_registers->buff;
  490. lpc32xx_chip.IO_ADDR_W = &lpc32xx_nand_mlc_registers->buff;
  491. lpc32xx_chip.cmd_ctrl = lpc32xx_cmd_ctrl;
  492. /* do not set init_size: nand_base.c will read sizes from chip */
  493. lpc32xx_chip.dev_ready = lpc32xx_dev_ready;
  494. /* do not set setup_read_retry: this is NAND-chip-specific */
  495. /* do not set chip_delay: we have dev_ready defined. */
  496. lpc32xx_chip.options |= NAND_NO_SUBPAGE_WRITE;
  497. /* Set needed ECC fields */
  498. lpc32xx_chip.ecc.mode = NAND_ECC_HW;
  499. lpc32xx_chip.ecc.layout = &lpc32xx_largepage_ecclayout;
  500. lpc32xx_chip.ecc.size = 512;
  501. lpc32xx_chip.ecc.bytes = 10;
  502. lpc32xx_chip.ecc.strength = 4;
  503. lpc32xx_chip.ecc.read_page = lpc32xx_read_page_hwecc;
  504. lpc32xx_chip.ecc.read_page_raw = lpc32xx_read_page_raw;
  505. lpc32xx_chip.ecc.write_page = lpc32xx_write_page_hwecc;
  506. lpc32xx_chip.ecc.write_page_raw = lpc32xx_write_page_raw;
  507. lpc32xx_chip.ecc.read_oob = lpc32xx_read_oob;
  508. lpc32xx_chip.ecc.write_oob = lpc32xx_write_oob;
  509. lpc32xx_chip.waitfunc = lpc32xx_waitfunc;
  510. lpc32xx_chip.read_byte = lpc32xx_read_byte; /* FIXME: NEEDED? */
  511. /* BBT options: read from last two pages */
  512. lpc32xx_chip.bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_LASTBLOCK
  513. | NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE
  514. | NAND_BBT_WRITE;
  515. /* Initialize NAND interface */
  516. lpc32xx_nand_init();
  517. /* identify chip */
  518. ret = nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_CHIPS, NULL);
  519. if (ret) {
  520. pr_err("nand_scan_ident returned %i", ret);
  521. return;
  522. }
  523. /* finish scanning the chip */
  524. ret = nand_scan_tail(mtd);
  525. if (ret) {
  526. pr_err("nand_scan_tail returned %i", ret);
  527. return;
  528. }
  529. /* chip is good, register it */
  530. ret = nand_register(0, mtd);
  531. if (ret)
  532. pr_err("nand_register returned %i", ret);
  533. }
  534. #else /* defined(CONFIG_SPL_BUILD) */
  535. void nand_init(void)
  536. {
  537. /* enable NAND controller */
  538. lpc32xx_mlc_nand_init();
  539. /* initialize NAND controller */
  540. lpc32xx_nand_init();
  541. }
  542. void nand_deselect(void)
  543. {
  544. /* nothing to do, but SPL requires this function */
  545. }
  546. static int read_single_page(uint8_t *dest, int page,
  547. struct lpc32xx_oob *oob)
  548. {
  549. int status, i, timeout, err, max_bitflips = 0;
  550. /* enter read mode */
  551. writel(NAND_CMD_READ0, &lpc32xx_nand_mlc_registers->cmd);
  552. /* send column (lsb then MSB) and page (lsb to MSB) */
  553. writel(0, &lpc32xx_nand_mlc_registers->addr);
  554. writel(0, &lpc32xx_nand_mlc_registers->addr);
  555. writel(page & 0xff, &lpc32xx_nand_mlc_registers->addr);
  556. writel((page>>8) & 0xff, &lpc32xx_nand_mlc_registers->addr);
  557. writel((page>>16) & 0xff, &lpc32xx_nand_mlc_registers->addr);
  558. /* start reading */
  559. writel(NAND_CMD_READSTART, &lpc32xx_nand_mlc_registers->cmd);
  560. /* large page auto decode read */
  561. for (i = 0; i < 4; i++) {
  562. /* start auto decode (reads 528 NAND bytes) */
  563. writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
  564. /* wait for controller to return to ready state */
  565. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  566. status = readl(&lpc32xx_nand_mlc_registers->isr);
  567. if (status & ISR_CONTROLLER_READY)
  568. break;
  569. udelay(1);
  570. }
  571. /* if controller stalled, return error */
  572. if (!(status & ISR_CONTROLLER_READY))
  573. return -1;
  574. /* if decoder failure, return error */
  575. if (status & ISR_DECODER_FAILURE)
  576. return -1;
  577. /* keep count of maximum bitflips performed */
  578. if (status & ISR_DECODER_ERROR) {
  579. err = ISR_DECODER_ERRORS(status);
  580. if (err > max_bitflips)
  581. max_bitflips = err;
  582. }
  583. /* copy first 512 bytes into buffer */
  584. memcpy(dest+i*512, lpc32xx_nand_mlc_registers->buff, 512);
  585. /* copy next 6 bytes bytes into OOB buffer */
  586. memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
  587. }
  588. return max_bitflips;
  589. }
  590. /*
  591. * Load U-Boot signed image.
  592. * This loads an image from NAND, skipping bad blocks.
  593. * A block is declared bad if at least one of its readable pages has
  594. * a bad block marker in its OOB at position 0.
  595. * If all pages ion a block are unreadable, the block is considered
  596. * bad (i.e., assumed not to be part of the image) and skipped.
  597. *
  598. * IMPORTANT NOTE:
  599. *
  600. * If the first block of the image is fully unreadable, it will be
  601. * ignored and skipped as if it had been marked bad. If it was not
  602. * actually marked bad at the time of writing the image, the resulting
  603. * image loaded will lack a header and magic number. It could thus be
  604. * considered as a raw, headerless, image and SPL might erroneously
  605. * jump into it.
  606. *
  607. * In order to avoid this risk, LPC32XX-based boards which use this
  608. * driver MUST define CONFIG_SPL_PANIC_ON_RAW_IMAGE.
  609. */
  610. #define BYTES_PER_PAGE 2048
  611. #define PAGES_PER_BLOCK 64
  612. #define BYTES_PER_BLOCK (BYTES_PER_PAGE * PAGES_PER_BLOCK)
  613. #define PAGES_PER_CHIP_MAX 524288
  614. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  615. {
  616. int bytes_left = size;
  617. int pages_left = DIV_ROUND_UP(size, BYTES_PER_PAGE);
  618. int blocks_left = DIV_ROUND_UP(size, BYTES_PER_BLOCK);
  619. int block = 0;
  620. int page = offs / BYTES_PER_PAGE;
  621. /* perform reads block by block */
  622. while (blocks_left) {
  623. /* compute first page number to read */
  624. void *block_page_dst = dst;
  625. /* read at most one block, possibly less */
  626. int block_bytes_left = bytes_left;
  627. if (block_bytes_left > BYTES_PER_BLOCK)
  628. block_bytes_left = BYTES_PER_BLOCK;
  629. /* keep track of good, failed, and "bad" pages */
  630. int block_pages_good = 0;
  631. int block_pages_bad = 0;
  632. int block_pages_err = 0;
  633. /* we shall read a full block of pages, maybe less */
  634. int block_pages_left = pages_left;
  635. if (block_pages_left > PAGES_PER_BLOCK)
  636. block_pages_left = PAGES_PER_BLOCK;
  637. int block_pages = block_pages_left;
  638. int block_page = page;
  639. /* while pages are left and the block is not known as bad */
  640. while ((block_pages > 0) && (block_pages_bad == 0)) {
  641. /* we will read OOB, too, for bad block markers */
  642. struct lpc32xx_oob oob;
  643. /* read page */
  644. int res = read_single_page(block_page_dst, block_page,
  645. &oob);
  646. /* count readable pages */
  647. if (res >= 0) {
  648. /* this page is good */
  649. block_pages_good++;
  650. /* this page is bad */
  651. if ((oob.free[0].free_oob_bytes[0] != 0xff)
  652. | (oob.free[0].free_oob_bytes[1] != 0xff))
  653. block_pages_bad++;
  654. } else
  655. /* count errors */
  656. block_pages_err++;
  657. /* we're done with this page */
  658. block_page++;
  659. block_page_dst += BYTES_PER_PAGE;
  660. if (block_pages)
  661. block_pages--;
  662. }
  663. /* a fully unreadable block is considered bad */
  664. if (block_pages_good == 0)
  665. block_pages_bad = block_pages_err;
  666. /* errors are fatal only in good blocks */
  667. if ((block_pages_err > 0) && (block_pages_bad == 0))
  668. return -1;
  669. /* we keep reads only of good blocks */
  670. if (block_pages_bad == 0) {
  671. dst += block_bytes_left;
  672. bytes_left -= block_bytes_left;
  673. pages_left -= block_pages_left;
  674. blocks_left--;
  675. }
  676. /* good or bad, we're done with this block */
  677. block++;
  678. page += PAGES_PER_BLOCK;
  679. }
  680. /* report success */
  681. return 0;
  682. }
  683. #endif /* CONFIG_SPL_BUILD */