fsl_elbc_nand.c 21 KB

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