fsl_elbc_nand.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* Freescale Enhanced Local Bus Controller FCM NAND driver
  2. *
  3. * Copyright (c) 2006-2008 Freescale Semiconductor
  4. *
  5. * Authors: Nick Spence <nick.spence@freescale.com>,
  6. * Scott Wood <scottwood@freescale.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <malloc.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/nand.h>
  26. #include <linux/mtd/nand_ecc.h>
  27. #include <asm/io.h>
  28. #include <asm/errno.h>
  29. #ifdef VERBOSE_DEBUG
  30. #define DEBUG_ELBC
  31. #define vdbg(format, arg...) printf("DEBUG: " format, ##arg)
  32. #else
  33. #define vdbg(format, arg...) do {} while (0)
  34. #endif
  35. /* Can't use plain old DEBUG because the linux mtd
  36. * headers define it as a macro.
  37. */
  38. #ifdef DEBUG_ELBC
  39. #define dbg(format, arg...) printf("DEBUG: " format, ##arg)
  40. #else
  41. #define dbg(format, arg...) do {} while (0)
  42. #endif
  43. #define MAX_BANKS 8
  44. #define ERR_BYTE 0xFF /* Value returned for read bytes when read failed */
  45. #define FCM_TIMEOUT_MSECS 10 /* Maximum number of mSecs to wait for FCM */
  46. #define LTESR_NAND_MASK (LTESR_FCT | LTESR_PAR | LTESR_CC)
  47. struct fsl_elbc_ctrl;
  48. /* mtd information per set */
  49. struct fsl_elbc_mtd {
  50. struct mtd_info mtd;
  51. struct nand_chip chip;
  52. struct fsl_elbc_ctrl *ctrl;
  53. struct device *dev;
  54. int bank; /* Chip select bank number */
  55. u8 __iomem *vbase; /* Chip select base virtual address */
  56. int page_size; /* NAND page size (0=512, 1=2048) */
  57. unsigned int fmr; /* FCM Flash Mode Register value */
  58. };
  59. /* overview of the fsl elbc controller */
  60. struct fsl_elbc_ctrl {
  61. struct nand_hw_control controller;
  62. struct fsl_elbc_mtd *chips[MAX_BANKS];
  63. /* device info */
  64. lbus83xx_t *regs;
  65. u8 __iomem *addr; /* Address of assigned FCM buffer */
  66. unsigned int page; /* Last page written to / read from */
  67. unsigned int read_bytes; /* Number of bytes read during command */
  68. unsigned int column; /* Saved column from SEQIN */
  69. unsigned int index; /* Pointer to next byte to 'read' */
  70. unsigned int status; /* status read from LTESR after last op */
  71. unsigned int mdr; /* UPM/FCM Data Register value */
  72. unsigned int use_mdr; /* Non zero if the MDR is to be set */
  73. unsigned int oob; /* Non zero if operating on OOB data */
  74. uint8_t *oob_poi; /* Place to write ECC after read back */
  75. };
  76. /* These map to the positions used by the FCM hardware ECC generator */
  77. /* Small Page FLASH with FMR[ECCM] = 0 */
  78. static struct nand_ecclayout fsl_elbc_oob_sp_eccm0 = {
  79. .eccbytes = 3,
  80. .eccpos = {6, 7, 8},
  81. .oobfree = { {0, 5}, {9, 7} },
  82. .oobavail = 12,
  83. };
  84. /* Small Page FLASH with FMR[ECCM] = 1 */
  85. static struct nand_ecclayout fsl_elbc_oob_sp_eccm1 = {
  86. .eccbytes = 3,
  87. .eccpos = {8, 9, 10},
  88. .oobfree = { {0, 5}, {6, 2}, {11, 5} },
  89. .oobavail = 12,
  90. };
  91. /* Large Page FLASH with FMR[ECCM] = 0 */
  92. static struct nand_ecclayout fsl_elbc_oob_lp_eccm0 = {
  93. .eccbytes = 12,
  94. .eccpos = {6, 7, 8, 22, 23, 24, 38, 39, 40, 54, 55, 56},
  95. .oobfree = { {1, 5}, {9, 13}, {25, 13}, {41, 13}, {57, 7} },
  96. .oobavail = 48,
  97. };
  98. /* Large Page FLASH with FMR[ECCM] = 1 */
  99. static struct nand_ecclayout fsl_elbc_oob_lp_eccm1 = {
  100. .eccbytes = 12,
  101. .eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58},
  102. .oobfree = { {1, 7}, {11, 13}, {27, 13}, {43, 13}, {59, 5} },
  103. .oobavail = 48,
  104. };
  105. /*
  106. * fsl_elbc_oob_lp_eccm* specify that LP NAND's OOB free area starts at offset
  107. * 1, so we have to adjust bad block pattern. This pattern should be used for
  108. * x8 chips only. So far hardware does not support x16 chips anyway.
  109. */
  110. static u8 scan_ff_pattern[] = { 0xff, };
  111. static struct nand_bbt_descr largepage_memorybased = {
  112. .options = 0,
  113. .offs = 0,
  114. .len = 1,
  115. .pattern = scan_ff_pattern,
  116. };
  117. /*=================================*/
  118. /*
  119. * Set up the FCM hardware block and page address fields, and the fcm
  120. * structure addr field to point to the correct FCM buffer in memory
  121. */
  122. static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
  123. {
  124. struct nand_chip *chip = mtd->priv;
  125. struct fsl_elbc_mtd *priv = chip->priv;
  126. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  127. lbus83xx_t *lbc = ctrl->regs;
  128. int buf_num;
  129. ctrl->page = page_addr;
  130. if (priv->page_size) {
  131. out_be32(&lbc->fbar, page_addr >> 6);
  132. out_be32(&lbc->fpar,
  133. ((page_addr << FPAR_LP_PI_SHIFT) & FPAR_LP_PI) |
  134. (oob ? FPAR_LP_MS : 0) | column);
  135. buf_num = (page_addr & 1) << 2;
  136. } else {
  137. out_be32(&lbc->fbar, page_addr >> 5);
  138. out_be32(&lbc->fpar,
  139. ((page_addr << FPAR_SP_PI_SHIFT) & FPAR_SP_PI) |
  140. (oob ? FPAR_SP_MS : 0) | column);
  141. buf_num = page_addr & 7;
  142. }
  143. ctrl->addr = priv->vbase + buf_num * 1024;
  144. ctrl->index = column;
  145. /* for OOB data point to the second half of the buffer */
  146. if (oob)
  147. ctrl->index += priv->page_size ? 2048 : 512;
  148. vdbg("set_addr: bank=%d, ctrl->addr=0x%p (0x%p), "
  149. "index %x, pes %d ps %d\n",
  150. buf_num, ctrl->addr, priv->vbase, ctrl->index,
  151. chip->phys_erase_shift, chip->page_shift);
  152. }
  153. /*
  154. * execute FCM command and wait for it to complete
  155. */
  156. static int fsl_elbc_run_command(struct mtd_info *mtd)
  157. {
  158. struct nand_chip *chip = mtd->priv;
  159. struct fsl_elbc_mtd *priv = chip->priv;
  160. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  161. lbus83xx_t *lbc = ctrl->regs;
  162. long long end_tick;
  163. u32 ltesr;
  164. /* Setup the FMR[OP] to execute without write protection */
  165. out_be32(&lbc->fmr, priv->fmr | 3);
  166. if (ctrl->use_mdr)
  167. out_be32(&lbc->mdr, ctrl->mdr);
  168. vdbg("fsl_elbc_run_command: fmr=%08x fir=%08x fcr=%08x\n",
  169. in_be32(&lbc->fmr), in_be32(&lbc->fir), in_be32(&lbc->fcr));
  170. vdbg("fsl_elbc_run_command: fbar=%08x fpar=%08x "
  171. "fbcr=%08x bank=%d\n",
  172. in_be32(&lbc->fbar), in_be32(&lbc->fpar),
  173. in_be32(&lbc->fbcr), priv->bank);
  174. /* execute special operation */
  175. out_be32(&lbc->lsor, priv->bank);
  176. /* wait for FCM complete flag or timeout */
  177. end_tick = usec2ticks(FCM_TIMEOUT_MSECS * 1000) + get_ticks();
  178. ltesr = 0;
  179. while (end_tick > get_ticks()) {
  180. ltesr = in_be32(&lbc->ltesr);
  181. if (ltesr & LTESR_CC)
  182. break;
  183. }
  184. ctrl->status = ltesr & LTESR_NAND_MASK;
  185. out_be32(&lbc->ltesr, ctrl->status);
  186. out_be32(&lbc->lteatr, 0);
  187. /* store mdr value in case it was needed */
  188. if (ctrl->use_mdr)
  189. ctrl->mdr = in_be32(&lbc->mdr);
  190. ctrl->use_mdr = 0;
  191. vdbg("fsl_elbc_run_command: stat=%08x mdr=%08x fmr=%08x\n",
  192. ctrl->status, ctrl->mdr, in_be32(&lbc->fmr));
  193. /* returns 0 on success otherwise non-zero) */
  194. return ctrl->status == LTESR_CC ? 0 : -EIO;
  195. }
  196. static void fsl_elbc_do_read(struct nand_chip *chip, int oob)
  197. {
  198. struct fsl_elbc_mtd *priv = chip->priv;
  199. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  200. lbus83xx_t *lbc = ctrl->regs;
  201. if (priv->page_size) {
  202. out_be32(&lbc->fir,
  203. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  204. (FIR_OP_CA << FIR_OP1_SHIFT) |
  205. (FIR_OP_PA << FIR_OP2_SHIFT) |
  206. (FIR_OP_CW1 << FIR_OP3_SHIFT) |
  207. (FIR_OP_RBW << FIR_OP4_SHIFT));
  208. out_be32(&lbc->fcr, (NAND_CMD_READ0 << FCR_CMD0_SHIFT) |
  209. (NAND_CMD_READSTART << FCR_CMD1_SHIFT));
  210. } else {
  211. out_be32(&lbc->fir,
  212. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  213. (FIR_OP_CA << FIR_OP1_SHIFT) |
  214. (FIR_OP_PA << FIR_OP2_SHIFT) |
  215. (FIR_OP_RBW << FIR_OP3_SHIFT));
  216. if (oob)
  217. out_be32(&lbc->fcr,
  218. NAND_CMD_READOOB << FCR_CMD0_SHIFT);
  219. else
  220. out_be32(&lbc->fcr, NAND_CMD_READ0 << FCR_CMD0_SHIFT);
  221. }
  222. }
  223. /* cmdfunc send commands to the FCM */
  224. static void fsl_elbc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  225. int column, int page_addr)
  226. {
  227. struct nand_chip *chip = mtd->priv;
  228. struct fsl_elbc_mtd *priv = chip->priv;
  229. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  230. lbus83xx_t *lbc = ctrl->regs;
  231. ctrl->use_mdr = 0;
  232. /* clear the read buffer */
  233. ctrl->read_bytes = 0;
  234. if (command != NAND_CMD_PAGEPROG)
  235. ctrl->index = 0;
  236. switch (command) {
  237. /* READ0 and READ1 read the entire buffer to use hardware ECC. */
  238. case NAND_CMD_READ1:
  239. column += 256;
  240. /* fall-through */
  241. case NAND_CMD_READ0:
  242. vdbg("fsl_elbc_cmdfunc: NAND_CMD_READ0, page_addr:"
  243. " 0x%x, column: 0x%x.\n", page_addr, column);
  244. out_be32(&lbc->fbcr, 0); /* read entire page to enable ECC */
  245. set_addr(mtd, 0, page_addr, 0);
  246. ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  247. ctrl->index += column;
  248. fsl_elbc_do_read(chip, 0);
  249. fsl_elbc_run_command(mtd);
  250. return;
  251. /* READOOB reads only the OOB because no ECC is performed. */
  252. case NAND_CMD_READOOB:
  253. vdbg("fsl_elbc_cmdfunc: NAND_CMD_READOOB, page_addr:"
  254. " 0x%x, column: 0x%x.\n", page_addr, column);
  255. out_be32(&lbc->fbcr, mtd->oobsize - column);
  256. set_addr(mtd, column, page_addr, 1);
  257. ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  258. fsl_elbc_do_read(chip, 1);
  259. fsl_elbc_run_command(mtd);
  260. return;
  261. /* READID must read all 5 possible bytes while CEB is active */
  262. case NAND_CMD_READID:
  263. vdbg("fsl_elbc_cmdfunc: NAND_CMD_READID.\n");
  264. out_be32(&lbc->fir, (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  265. (FIR_OP_UA << FIR_OP1_SHIFT) |
  266. (FIR_OP_RBW << FIR_OP2_SHIFT));
  267. out_be32(&lbc->fcr, NAND_CMD_READID << FCR_CMD0_SHIFT);
  268. /* 5 bytes for manuf, device and exts */
  269. out_be32(&lbc->fbcr, 5);
  270. ctrl->read_bytes = 5;
  271. ctrl->use_mdr = 1;
  272. ctrl->mdr = 0;
  273. set_addr(mtd, 0, 0, 0);
  274. fsl_elbc_run_command(mtd);
  275. return;
  276. /* ERASE1 stores the block and page address */
  277. case NAND_CMD_ERASE1:
  278. vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE1, "
  279. "page_addr: 0x%x.\n", page_addr);
  280. set_addr(mtd, 0, page_addr, 0);
  281. return;
  282. /* ERASE2 uses the block and page address from ERASE1 */
  283. case NAND_CMD_ERASE2:
  284. vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE2.\n");
  285. out_be32(&lbc->fir,
  286. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  287. (FIR_OP_PA << FIR_OP1_SHIFT) |
  288. (FIR_OP_CM1 << FIR_OP2_SHIFT));
  289. out_be32(&lbc->fcr,
  290. (NAND_CMD_ERASE1 << FCR_CMD0_SHIFT) |
  291. (NAND_CMD_ERASE2 << FCR_CMD1_SHIFT));
  292. out_be32(&lbc->fbcr, 0);
  293. ctrl->read_bytes = 0;
  294. fsl_elbc_run_command(mtd);
  295. return;
  296. /* SEQIN sets up the addr buffer and all registers except the length */
  297. case NAND_CMD_SEQIN: {
  298. u32 fcr;
  299. vdbg("fsl_elbc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, "
  300. "page_addr: 0x%x, column: 0x%x.\n",
  301. page_addr, column);
  302. ctrl->column = column;
  303. ctrl->oob = 0;
  304. if (priv->page_size) {
  305. fcr = (NAND_CMD_SEQIN << FCR_CMD0_SHIFT) |
  306. (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT);
  307. out_be32(&lbc->fir,
  308. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  309. (FIR_OP_CA << FIR_OP1_SHIFT) |
  310. (FIR_OP_PA << FIR_OP2_SHIFT) |
  311. (FIR_OP_WB << FIR_OP3_SHIFT) |
  312. (FIR_OP_CW1 << FIR_OP4_SHIFT));
  313. } else {
  314. fcr = (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT) |
  315. (NAND_CMD_SEQIN << FCR_CMD2_SHIFT);
  316. out_be32(&lbc->fir,
  317. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  318. (FIR_OP_CM2 << FIR_OP1_SHIFT) |
  319. (FIR_OP_CA << FIR_OP2_SHIFT) |
  320. (FIR_OP_PA << FIR_OP3_SHIFT) |
  321. (FIR_OP_WB << FIR_OP4_SHIFT) |
  322. (FIR_OP_CW1 << FIR_OP5_SHIFT));
  323. if (column >= mtd->writesize) {
  324. /* OOB area --> READOOB */
  325. column -= mtd->writesize;
  326. fcr |= NAND_CMD_READOOB << FCR_CMD0_SHIFT;
  327. ctrl->oob = 1;
  328. } else if (column < 256) {
  329. /* First 256 bytes --> READ0 */
  330. fcr |= NAND_CMD_READ0 << FCR_CMD0_SHIFT;
  331. } else {
  332. /* Second 256 bytes --> READ1 */
  333. fcr |= NAND_CMD_READ1 << FCR_CMD0_SHIFT;
  334. }
  335. }
  336. out_be32(&lbc->fcr, fcr);
  337. set_addr(mtd, column, page_addr, ctrl->oob);
  338. return;
  339. }
  340. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  341. case NAND_CMD_PAGEPROG: {
  342. int full_page;
  343. vdbg("fsl_elbc_cmdfunc: NAND_CMD_PAGEPROG "
  344. "writing %d bytes.\n", ctrl->index);
  345. /* if the write did not start at 0 or is not a full page
  346. * then set the exact length, otherwise use a full page
  347. * write so the HW generates the ECC.
  348. */
  349. if (ctrl->oob || ctrl->column != 0 ||
  350. ctrl->index != mtd->writesize + mtd->oobsize) {
  351. out_be32(&lbc->fbcr, ctrl->index);
  352. full_page = 0;
  353. } else {
  354. out_be32(&lbc->fbcr, 0);
  355. full_page = 1;
  356. }
  357. fsl_elbc_run_command(mtd);
  358. /* Read back the page in order to fill in the ECC for the
  359. * caller. Is this really needed?
  360. */
  361. if (full_page && ctrl->oob_poi) {
  362. out_be32(&lbc->fbcr, 3);
  363. set_addr(mtd, 6, page_addr, 1);
  364. ctrl->read_bytes = mtd->writesize + 9;
  365. fsl_elbc_do_read(chip, 1);
  366. fsl_elbc_run_command(mtd);
  367. memcpy_fromio(ctrl->oob_poi + 6,
  368. &ctrl->addr[ctrl->index], 3);
  369. ctrl->index += 3;
  370. }
  371. ctrl->oob_poi = NULL;
  372. return;
  373. }
  374. /* CMD_STATUS must read the status byte while CEB is active */
  375. /* Note - it does not wait for the ready line */
  376. case NAND_CMD_STATUS:
  377. out_be32(&lbc->fir,
  378. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  379. (FIR_OP_RBW << FIR_OP1_SHIFT));
  380. out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
  381. out_be32(&lbc->fbcr, 1);
  382. set_addr(mtd, 0, 0, 0);
  383. ctrl->read_bytes = 1;
  384. fsl_elbc_run_command(mtd);
  385. /* The chip always seems to report that it is
  386. * write-protected, even when it is not.
  387. */
  388. out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
  389. return;
  390. /* RESET without waiting for the ready line */
  391. case NAND_CMD_RESET:
  392. dbg("fsl_elbc_cmdfunc: NAND_CMD_RESET.\n");
  393. out_be32(&lbc->fir, FIR_OP_CM0 << FIR_OP0_SHIFT);
  394. out_be32(&lbc->fcr, NAND_CMD_RESET << FCR_CMD0_SHIFT);
  395. fsl_elbc_run_command(mtd);
  396. return;
  397. default:
  398. printf("fsl_elbc_cmdfunc: error, unsupported command 0x%x.\n",
  399. command);
  400. }
  401. }
  402. static void fsl_elbc_select_chip(struct mtd_info *mtd, int chip)
  403. {
  404. /* The hardware does not seem to support multiple
  405. * chips per bank.
  406. */
  407. }
  408. /*
  409. * Write buf to the FCM Controller Data Buffer
  410. */
  411. static void fsl_elbc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  412. {
  413. struct nand_chip *chip = mtd->priv;
  414. struct fsl_elbc_mtd *priv = chip->priv;
  415. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  416. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  417. if (len <= 0) {
  418. printf("write_buf of %d bytes", len);
  419. ctrl->status = 0;
  420. return;
  421. }
  422. if ((unsigned int)len > bufsize - ctrl->index) {
  423. printf("write_buf beyond end of buffer "
  424. "(%d requested, %u available)\n",
  425. len, bufsize - ctrl->index);
  426. len = bufsize - ctrl->index;
  427. }
  428. memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
  429. /*
  430. * This is workaround for the weird elbc hangs during nand write,
  431. * Scott Wood says: "...perhaps difference in how long it takes a
  432. * write to make it through the localbus compared to a write to IMMR
  433. * is causing problems, and sync isn't helping for some reason."
  434. * Reading back the last byte helps though.
  435. */
  436. in_8(&ctrl->addr[ctrl->index] + len - 1);
  437. ctrl->index += len;
  438. }
  439. /*
  440. * read a byte from either the FCM hardware buffer if it has any data left
  441. * otherwise issue a command to read a single byte.
  442. */
  443. static u8 fsl_elbc_read_byte(struct mtd_info *mtd)
  444. {
  445. struct nand_chip *chip = mtd->priv;
  446. struct fsl_elbc_mtd *priv = chip->priv;
  447. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  448. /* If there are still bytes in the FCM, then use the next byte. */
  449. if (ctrl->index < ctrl->read_bytes)
  450. return in_8(&ctrl->addr[ctrl->index++]);
  451. printf("read_byte beyond end of buffer\n");
  452. return ERR_BYTE;
  453. }
  454. /*
  455. * Read from the FCM Controller Data Buffer
  456. */
  457. static void fsl_elbc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  458. {
  459. struct nand_chip *chip = mtd->priv;
  460. struct fsl_elbc_mtd *priv = chip->priv;
  461. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  462. int avail;
  463. if (len < 0)
  464. return;
  465. avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
  466. memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
  467. ctrl->index += avail;
  468. if (len > avail)
  469. printf("read_buf beyond end of buffer "
  470. "(%d requested, %d available)\n",
  471. len, avail);
  472. }
  473. /*
  474. * Verify buffer against the FCM Controller Data Buffer
  475. */
  476. static int fsl_elbc_verify_buf(struct mtd_info *mtd,
  477. const u_char *buf, int len)
  478. {
  479. struct nand_chip *chip = mtd->priv;
  480. struct fsl_elbc_mtd *priv = chip->priv;
  481. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  482. int i;
  483. if (len < 0) {
  484. printf("write_buf of %d bytes", len);
  485. return -EINVAL;
  486. }
  487. if ((unsigned int)len > ctrl->read_bytes - ctrl->index) {
  488. printf("verify_buf beyond end of buffer "
  489. "(%d requested, %u available)\n",
  490. len, ctrl->read_bytes - ctrl->index);
  491. ctrl->index = ctrl->read_bytes;
  492. return -EINVAL;
  493. }
  494. for (i = 0; i < len; i++)
  495. if (in_8(&ctrl->addr[ctrl->index + i]) != buf[i])
  496. break;
  497. ctrl->index += len;
  498. return i == len && ctrl->status == LTESR_CC ? 0 : -EIO;
  499. }
  500. /* This function is called after Program and Erase Operations to
  501. * check for success or failure.
  502. */
  503. static int fsl_elbc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  504. {
  505. struct fsl_elbc_mtd *priv = chip->priv;
  506. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  507. lbus83xx_t *lbc = ctrl->regs;
  508. if (ctrl->status != LTESR_CC)
  509. return NAND_STATUS_FAIL;
  510. /* Use READ_STATUS command, but wait for the device to be ready */
  511. ctrl->use_mdr = 0;
  512. out_be32(&lbc->fir,
  513. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  514. (FIR_OP_RBW << FIR_OP1_SHIFT));
  515. out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
  516. out_be32(&lbc->fbcr, 1);
  517. set_addr(mtd, 0, 0, 0);
  518. ctrl->read_bytes = 1;
  519. fsl_elbc_run_command(mtd);
  520. if (ctrl->status != LTESR_CC)
  521. return NAND_STATUS_FAIL;
  522. /* The chip always seems to report that it is
  523. * write-protected, even when it is not.
  524. */
  525. out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
  526. return fsl_elbc_read_byte(mtd);
  527. }
  528. static int fsl_elbc_read_page(struct mtd_info *mtd,
  529. struct nand_chip *chip,
  530. uint8_t *buf)
  531. {
  532. fsl_elbc_read_buf(mtd, buf, mtd->writesize);
  533. fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  534. if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL)
  535. mtd->ecc_stats.failed++;
  536. return 0;
  537. }
  538. /* ECC will be calculated automatically, and errors will be detected in
  539. * waitfunc.
  540. */
  541. static void fsl_elbc_write_page(struct mtd_info *mtd,
  542. struct nand_chip *chip,
  543. const uint8_t *buf)
  544. {
  545. struct fsl_elbc_mtd *priv = chip->priv;
  546. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  547. fsl_elbc_write_buf(mtd, buf, mtd->writesize);
  548. fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  549. ctrl->oob_poi = chip->oob_poi;
  550. }
  551. static struct fsl_elbc_ctrl *elbc_ctrl;
  552. static void fsl_elbc_ctrl_init(void)
  553. {
  554. immap_t *im = (immap_t *)CFG_IMMR;
  555. elbc_ctrl = kzalloc(sizeof(*elbc_ctrl), GFP_KERNEL);
  556. if (!elbc_ctrl)
  557. return;
  558. elbc_ctrl->regs = &im->lbus;
  559. /* clear event registers */
  560. out_be32(&elbc_ctrl->regs->ltesr, LTESR_NAND_MASK);
  561. out_be32(&elbc_ctrl->regs->lteatr, 0);
  562. /* Enable interrupts for any detected events */
  563. out_be32(&elbc_ctrl->regs->lteir, LTESR_NAND_MASK);
  564. elbc_ctrl->read_bytes = 0;
  565. elbc_ctrl->index = 0;
  566. elbc_ctrl->addr = NULL;
  567. }
  568. int board_nand_init(struct nand_chip *nand)
  569. {
  570. struct fsl_elbc_mtd *priv;
  571. uint32_t br, or;
  572. if (!elbc_ctrl) {
  573. fsl_elbc_ctrl_init();
  574. if (!elbc_ctrl)
  575. return -1;
  576. }
  577. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  578. if (!priv)
  579. return -ENOMEM;
  580. priv->ctrl = elbc_ctrl;
  581. priv->vbase = nand->IO_ADDR_R;
  582. /* Find which chip select it is connected to. It'd be nice
  583. * if we could pass more than one datum to the NAND driver...
  584. */
  585. for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
  586. br = in_be32(&elbc_ctrl->regs->bank[priv->bank].br);
  587. or = in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
  588. if ((br & BR_V) && (br & BR_MSEL) == BR_MS_FCM &&
  589. (br & or & BR_BA) == (phys_addr_t)nand->IO_ADDR_R)
  590. break;
  591. }
  592. if (priv->bank >= MAX_BANKS) {
  593. printf("fsl_elbc_nand: address did not match any "
  594. "chip selects\n");
  595. return -ENODEV;
  596. }
  597. elbc_ctrl->chips[priv->bank] = priv;
  598. /* fill in nand_chip structure */
  599. /* set up function call table */
  600. nand->read_byte = fsl_elbc_read_byte;
  601. nand->write_buf = fsl_elbc_write_buf;
  602. nand->read_buf = fsl_elbc_read_buf;
  603. nand->verify_buf = fsl_elbc_verify_buf;
  604. nand->select_chip = fsl_elbc_select_chip;
  605. nand->cmdfunc = fsl_elbc_cmdfunc;
  606. nand->waitfunc = fsl_elbc_wait;
  607. /* set up nand options */
  608. nand->options = NAND_NO_READRDY | NAND_NO_AUTOINCR;
  609. nand->controller = &elbc_ctrl->controller;
  610. nand->priv = priv;
  611. nand->ecc.read_page = fsl_elbc_read_page;
  612. nand->ecc.write_page = fsl_elbc_write_page;
  613. /* If CS Base Register selects full hardware ECC then use it */
  614. if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
  615. nand->ecc.mode = NAND_ECC_HW;
  616. nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
  617. &fsl_elbc_oob_sp_eccm1 :
  618. &fsl_elbc_oob_sp_eccm0;
  619. nand->ecc.size = 512;
  620. nand->ecc.bytes = 3;
  621. nand->ecc.steps = 1;
  622. } else {
  623. /* otherwise fall back to default software ECC */
  624. nand->ecc.mode = NAND_ECC_SOFT;
  625. }
  626. priv->fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT);
  627. /* Large-page-specific setup */
  628. if (or & OR_FCM_PGS) {
  629. priv->page_size = 1;
  630. nand->badblock_pattern = &largepage_memorybased;
  631. /* adjust ecc setup if needed */
  632. if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
  633. nand->ecc.steps = 4;
  634. nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
  635. &fsl_elbc_oob_lp_eccm1 :
  636. &fsl_elbc_oob_lp_eccm0;
  637. }
  638. }
  639. return 0;
  640. }