lpc32xx_nand_mlc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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 <asm/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. {
  338. unsigned int i, status, timeout;
  339. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  340. /* when we get here we've already had the SEQIN */
  341. for (i = 0; i < 4; i++) {
  342. /* start encode (expects 518 writes to buff) */
  343. writel(0, &lpc32xx_nand_mlc_registers->ecc_enc_reg);
  344. /* copy first 512 bytes from buffer */
  345. memcpy(&lpc32xx_nand_mlc_registers->buff, buf+512*i, 512);
  346. /* copy next 6 bytes from OOB buffer -- excluding ECC */
  347. memcpy(&lpc32xx_nand_mlc_registers->buff, &oob->free[i], 6);
  348. /* wait for ECC to return to ready state */
  349. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  350. status = readl(&lpc32xx_nand_mlc_registers->isr);
  351. if (status & ISR_ECC_READY)
  352. break;
  353. udelay(1);
  354. }
  355. /* if ECC stalled, return failure */
  356. if (!(status & ISR_ECC_READY))
  357. return -1;
  358. /* Trigger auto encode (writes 528 bytes to NAND) */
  359. writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_enc_reg);
  360. /* wait for controller to return to ready state */
  361. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  362. status = readl(&lpc32xx_nand_mlc_registers->isr);
  363. if (status & ISR_CONTROLLER_READY)
  364. break;
  365. udelay(1);
  366. }
  367. /* if controller stalled, return error */
  368. if (!(status & ISR_CONTROLLER_READY))
  369. return -1;
  370. }
  371. return 0;
  372. }
  373. /**
  374. * lpc32xx_write_page_raw - write raw (in-band, out-of-band and ECC) data
  375. * @mtd: mtd info structure
  376. * @chip: nand chip info structure
  377. * @buf: buffer to store read data
  378. * @oob_required: caller requires OOB data read to chip->oob_poi
  379. * @page: page number to read
  380. *
  381. * Use large block write but without encode.
  382. *
  383. * The initial Write Serial Input and final Auto Program commands are
  384. * sent by the caller.
  385. *
  386. * This function will write the full out-of-band data, including the
  387. * ECC area. Therefore, it can write pages with valid *or* invalid ECC.
  388. */
  389. static int lpc32xx_write_page_raw(struct mtd_info *mtd,
  390. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  391. {
  392. unsigned int i;
  393. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  394. /* when we get here we've already had the Read Mode(1) */
  395. for (i = 0; i < 4; i++) {
  396. /* copy first 512 bytes from buffer */
  397. memcpy(lpc32xx_nand_mlc_registers->buff, buf+512*i, 512);
  398. /* copy next 6 bytes into OOB buffer -- excluding ECC */
  399. memcpy(lpc32xx_nand_mlc_registers->buff, &oob->free[i], 6);
  400. /* copy next 10 bytes into OOB buffer -- that is 'ECC' */
  401. memcpy(lpc32xx_nand_mlc_registers->buff, &oob->ecc[i], 10);
  402. }
  403. return 0;
  404. }
  405. /**
  406. * lpc32xx_write_oob - write out-of-band data
  407. * @mtd: mtd info structure
  408. * @chip: nand chip info structure
  409. * @page: page number to read
  410. *
  411. * Since ECC covers in- and out-of-band data, writing out-of-band data
  412. * with ECC will render the page ECC wrong -- or, if the page was blank,
  413. * then it will produce a good ECC but a later in-band data write will
  414. * render it wrong.
  415. *
  416. * Therefore, do not compute or write any ECC, and always return success.
  417. *
  418. * This implies that we do four writes, since non-ECC out-of-band data
  419. * are not contiguous in a large page.
  420. */
  421. static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  422. int page)
  423. {
  424. /* update oob on all 4 subpages in sequence */
  425. unsigned int i, status, timeout;
  426. struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
  427. for (i = 0; i < 4; i++) {
  428. /* start data input */
  429. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x200+0x210*i, page);
  430. /* copy 6 non-ECC out-of-band bytes directly into NAND */
  431. memcpy(lpc32xx_nand_mlc_registers->data, &oob->free[i], 6);
  432. /* program page */
  433. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  434. /* wait for NAND to return to ready state */
  435. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  436. status = readl(&lpc32xx_nand_mlc_registers->isr);
  437. if (status & ISR_NAND_READY)
  438. break;
  439. udelay(1);
  440. }
  441. /* if NAND stalled, return error */
  442. if (!(status & ISR_NAND_READY))
  443. return -1;
  444. }
  445. return 0;
  446. }
  447. /**
  448. * lpc32xx_waitfunc - wait until a command is done
  449. * @mtd: MTD device structure
  450. * @chip: NAND chip structure
  451. *
  452. * Wait for controller and FLASH to both be ready.
  453. */
  454. static int lpc32xx_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
  455. {
  456. int status;
  457. unsigned int timeout;
  458. /* wait until both controller and NAND are ready */
  459. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  460. status = readl(&lpc32xx_nand_mlc_registers->isr);
  461. if ((status & (ISR_CONTROLLER_READY || ISR_NAND_READY))
  462. == (ISR_CONTROLLER_READY || ISR_NAND_READY))
  463. break;
  464. udelay(1);
  465. }
  466. /* if controller or NAND stalled, return error */
  467. if ((status & (ISR_CONTROLLER_READY || ISR_NAND_READY))
  468. != (ISR_CONTROLLER_READY || ISR_NAND_READY))
  469. return -1;
  470. /* write NAND status command */
  471. writel(NAND_CMD_STATUS, &lpc32xx_nand_mlc_registers->cmd);
  472. /* read back status and return it */
  473. return readb(&lpc32xx_nand_mlc_registers->data);
  474. }
  475. /*
  476. * We are self-initializing, so we need our own chip struct
  477. */
  478. static struct nand_chip lpc32xx_chip;
  479. /*
  480. * Initialize the controller
  481. */
  482. void board_nand_init(void)
  483. {
  484. /* we have only one device anyway */
  485. struct mtd_info *mtd = &nand_info[0];
  486. /* chip is struct nand_chip, and is now provided by the driver. */
  487. mtd->priv = &lpc32xx_chip;
  488. /* to store return status in case we need to print it */
  489. int ret;
  490. /* Set all BOARDSPECIFIC (actually core-specific) fields */
  491. lpc32xx_chip.IO_ADDR_R = &lpc32xx_nand_mlc_registers->buff;
  492. lpc32xx_chip.IO_ADDR_W = &lpc32xx_nand_mlc_registers->buff;
  493. lpc32xx_chip.cmd_ctrl = lpc32xx_cmd_ctrl;
  494. /* do not set init_size: nand_base.c will read sizes from chip */
  495. lpc32xx_chip.dev_ready = lpc32xx_dev_ready;
  496. /* do not set setup_read_retry: this is NAND-chip-specific */
  497. /* do not set chip_delay: we have dev_ready defined. */
  498. lpc32xx_chip.options |= NAND_NO_SUBPAGE_WRITE;
  499. /* Set needed ECC fields */
  500. lpc32xx_chip.ecc.mode = NAND_ECC_HW;
  501. lpc32xx_chip.ecc.layout = &lpc32xx_largepage_ecclayout;
  502. lpc32xx_chip.ecc.size = 512;
  503. lpc32xx_chip.ecc.bytes = 10;
  504. lpc32xx_chip.ecc.strength = 4;
  505. lpc32xx_chip.ecc.read_page = lpc32xx_read_page_hwecc;
  506. lpc32xx_chip.ecc.read_page_raw = lpc32xx_read_page_raw;
  507. lpc32xx_chip.ecc.write_page = lpc32xx_write_page_hwecc;
  508. lpc32xx_chip.ecc.write_page_raw = lpc32xx_write_page_raw;
  509. lpc32xx_chip.ecc.read_oob = lpc32xx_read_oob;
  510. lpc32xx_chip.ecc.write_oob = lpc32xx_write_oob;
  511. lpc32xx_chip.waitfunc = lpc32xx_waitfunc;
  512. lpc32xx_chip.read_byte = lpc32xx_read_byte; /* FIXME: NEEDED? */
  513. /* BBT options: read from last two pages */
  514. lpc32xx_chip.bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_LASTBLOCK
  515. | NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE
  516. | NAND_BBT_WRITE;
  517. /* Initialize NAND interface */
  518. lpc32xx_nand_init();
  519. /* identify chip */
  520. ret = nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_CHIPS, NULL);
  521. if (ret) {
  522. error("nand_scan_ident returned %i", ret);
  523. return;
  524. }
  525. /* finish scanning the chip */
  526. ret = nand_scan_tail(mtd);
  527. if (ret) {
  528. error("nand_scan_tail returned %i", ret);
  529. return;
  530. }
  531. /* chip is good, register it */
  532. ret = nand_register(0);
  533. if (ret)
  534. error("nand_register returned %i", ret);
  535. }
  536. #else /* defined(CONFIG_SPL_BUILD) */
  537. void nand_init(void)
  538. {
  539. /* enable NAND controller */
  540. lpc32xx_mlc_nand_init();
  541. /* initialize NAND controller */
  542. lpc32xx_nand_init();
  543. }
  544. void nand_deselect(void)
  545. {
  546. /* nothing to do, but SPL requires this function */
  547. }
  548. static int read_single_page(uint8_t *dest, int page,
  549. struct lpc32xx_oob *oob)
  550. {
  551. int status, i, timeout, err, max_bitflips = 0;
  552. /* enter read mode */
  553. writel(NAND_CMD_READ0, &lpc32xx_nand_mlc_registers->cmd);
  554. /* send column (lsb then MSB) and page (lsb to MSB) */
  555. writel(0, &lpc32xx_nand_mlc_registers->addr);
  556. writel(0, &lpc32xx_nand_mlc_registers->addr);
  557. writel(page & 0xff, &lpc32xx_nand_mlc_registers->addr);
  558. writel((page>>8) & 0xff, &lpc32xx_nand_mlc_registers->addr);
  559. writel((page>>16) & 0xff, &lpc32xx_nand_mlc_registers->addr);
  560. /* start reading */
  561. writel(NAND_CMD_READSTART, &lpc32xx_nand_mlc_registers->cmd);
  562. /* large page auto decode read */
  563. for (i = 0; i < 4; i++) {
  564. /* start auto decode (reads 528 NAND bytes) */
  565. writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
  566. /* wait for controller to return to ready state */
  567. for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
  568. status = readl(&lpc32xx_nand_mlc_registers->isr);
  569. if (status & ISR_CONTROLLER_READY)
  570. break;
  571. udelay(1);
  572. }
  573. /* if controller stalled, return error */
  574. if (!(status & ISR_CONTROLLER_READY))
  575. return -1;
  576. /* if decoder failure, return error */
  577. if (status & ISR_DECODER_FAILURE)
  578. return -1;
  579. /* keep count of maximum bitflips performed */
  580. if (status & ISR_DECODER_ERROR) {
  581. err = ISR_DECODER_ERRORS(status);
  582. if (err > max_bitflips)
  583. max_bitflips = err;
  584. }
  585. /* copy first 512 bytes into buffer */
  586. memcpy(dest+i*512, lpc32xx_nand_mlc_registers->buff, 512);
  587. /* copy next 6 bytes bytes into OOB buffer */
  588. memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
  589. }
  590. return max_bitflips;
  591. }
  592. /*
  593. * Load U-Boot signed image.
  594. * This loads an image from NAND, skipping bad blocks.
  595. * A block is declared bad if at least one of its readable pages has
  596. * a bad block marker in its OOB at position 0.
  597. * If all pages ion a block are unreadable, the block is considered
  598. * bad (i.e., assumed not to be part of the image) and skipped.
  599. *
  600. * IMPORTANT NOTE:
  601. *
  602. * If the first block of the image is fully unreadable, it will be
  603. * ignored and skipped as if it had been marked bad. If it was not
  604. * actually marked bad at the time of writing the image, the resulting
  605. * image loaded will lack a header and magic number. It could thus be
  606. * considered as a raw, headerless, image and SPL might erroneously
  607. * jump into it.
  608. *
  609. * In order to avoid this risk, LPC32XX-based boards which use this
  610. * driver MUST define CONFIG_SPL_PANIC_ON_RAW_IMAGE.
  611. */
  612. #define BYTES_PER_PAGE 2048
  613. #define PAGES_PER_BLOCK 64
  614. #define BYTES_PER_BLOCK (BYTES_PER_PAGE * PAGES_PER_BLOCK)
  615. #define PAGES_PER_CHIP_MAX 524288
  616. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  617. {
  618. int bytes_left = size;
  619. int pages_left = DIV_ROUND_UP(size, BYTES_PER_PAGE);
  620. int blocks_left = DIV_ROUND_UP(size, BYTES_PER_BLOCK);
  621. int block = 0;
  622. int page = offs / BYTES_PER_PAGE;
  623. /* perform reads block by block */
  624. while (blocks_left) {
  625. /* compute first page number to read */
  626. void *block_page_dst = dst;
  627. /* read at most one block, possibly less */
  628. int block_bytes_left = bytes_left;
  629. if (block_bytes_left > BYTES_PER_BLOCK)
  630. block_bytes_left = BYTES_PER_BLOCK;
  631. /* keep track of good, failed, and "bad" pages */
  632. int block_pages_good = 0;
  633. int block_pages_bad = 0;
  634. int block_pages_err = 0;
  635. /* we shall read a full block of pages, maybe less */
  636. int block_pages_left = pages_left;
  637. if (block_pages_left > PAGES_PER_BLOCK)
  638. block_pages_left = PAGES_PER_BLOCK;
  639. int block_pages = block_pages_left;
  640. int block_page = page;
  641. /* while pages are left and the block is not known as bad */
  642. while ((block_pages > 0) && (block_pages_bad == 0)) {
  643. /* we will read OOB, too, for bad block markers */
  644. struct lpc32xx_oob oob;
  645. /* read page */
  646. int res = read_single_page(block_page_dst, block_page,
  647. &oob);
  648. /* count readable pages */
  649. if (res >= 0) {
  650. /* this page is good */
  651. block_pages_good++;
  652. /* this page is bad */
  653. if ((oob.free[0].free_oob_bytes[0] != 0xff)
  654. | (oob.free[0].free_oob_bytes[1] != 0xff))
  655. block_pages_bad++;
  656. } else
  657. /* count errors */
  658. block_pages_err++;
  659. /* we're done with this page */
  660. block_page++;
  661. block_page_dst += BYTES_PER_PAGE;
  662. if (block_pages)
  663. block_pages--;
  664. }
  665. /* a fully unreadable block is considered bad */
  666. if (block_pages_good == 0)
  667. block_pages_bad = block_pages_err;
  668. /* errors are fatal only in good blocks */
  669. if ((block_pages_err > 0) && (block_pages_bad == 0))
  670. return -1;
  671. /* we keep reads only of good blocks */
  672. if (block_pages_bad == 0) {
  673. dst += block_bytes_left;
  674. bytes_left -= block_bytes_left;
  675. pages_left -= block_pages_left;
  676. blocks_left--;
  677. }
  678. /* good or bad, we're done with this block */
  679. block++;
  680. page += PAGES_PER_BLOCK;
  681. }
  682. /* report success */
  683. return 0;
  684. }
  685. #endif /* CONFIG_SPL_BUILD */