fsl_elbc_nand.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 <nand.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/nand.h>
  27. #include <linux/mtd/nand_ecc.h>
  28. #include <asm/io.h>
  29. #include <asm/errno.h>
  30. #ifdef VERBOSE_DEBUG
  31. #define DEBUG_ELBC
  32. #define vdbg(format, arg...) printf("DEBUG: " format, ##arg)
  33. #else
  34. #define vdbg(format, arg...) do {} while (0)
  35. #endif
  36. /* Can't use plain old DEBUG because the linux mtd
  37. * headers define it as a macro.
  38. */
  39. #ifdef DEBUG_ELBC
  40. #define dbg(format, arg...) printf("DEBUG: " format, ##arg)
  41. #else
  42. #define dbg(format, arg...) do {} while (0)
  43. #endif
  44. #define MAX_BANKS 8
  45. #define ERR_BYTE 0xFF /* Value returned for read bytes when read failed */
  46. #define FCM_TIMEOUT_MSECS 10 /* Maximum number of mSecs to wait for FCM */
  47. #define LTESR_NAND_MASK (LTESR_FCT | LTESR_PAR | LTESR_CC)
  48. struct fsl_elbc_ctrl;
  49. /* mtd information per set */
  50. struct fsl_elbc_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. case NAND_CMD_PARAM:
  284. vdbg("fsl_elbc_cmdfunc: NAND_CMD 0x%x.\n", command);
  285. out_be32(&lbc->fir, (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  286. (FIR_OP_UA << FIR_OP1_SHIFT) |
  287. (FIR_OP_RBW << FIR_OP2_SHIFT));
  288. out_be32(&lbc->fcr, command << FCR_CMD0_SHIFT);
  289. /*
  290. * although currently it's 8 bytes for READID, we always read
  291. * the maximum 256 bytes(for PARAM)
  292. */
  293. out_be32(&lbc->fbcr, 256);
  294. ctrl->read_bytes = 256;
  295. ctrl->use_mdr = 1;
  296. ctrl->mdr = column;
  297. set_addr(mtd, 0, 0, 0);
  298. fsl_elbc_run_command(mtd);
  299. return;
  300. /* ERASE1 stores the block and page address */
  301. case NAND_CMD_ERASE1:
  302. vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE1, "
  303. "page_addr: 0x%x.\n", page_addr);
  304. set_addr(mtd, 0, page_addr, 0);
  305. return;
  306. /* ERASE2 uses the block and page address from ERASE1 */
  307. case NAND_CMD_ERASE2:
  308. vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE2.\n");
  309. out_be32(&lbc->fir,
  310. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  311. (FIR_OP_PA << FIR_OP1_SHIFT) |
  312. (FIR_OP_CM1 << FIR_OP2_SHIFT));
  313. out_be32(&lbc->fcr,
  314. (NAND_CMD_ERASE1 << FCR_CMD0_SHIFT) |
  315. (NAND_CMD_ERASE2 << FCR_CMD1_SHIFT));
  316. out_be32(&lbc->fbcr, 0);
  317. ctrl->read_bytes = 0;
  318. fsl_elbc_run_command(mtd);
  319. return;
  320. /* SEQIN sets up the addr buffer and all registers except the length */
  321. case NAND_CMD_SEQIN: {
  322. u32 fcr;
  323. vdbg("fsl_elbc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, "
  324. "page_addr: 0x%x, column: 0x%x.\n",
  325. page_addr, column);
  326. ctrl->column = column;
  327. ctrl->oob = 0;
  328. if (priv->page_size) {
  329. fcr = (NAND_CMD_SEQIN << FCR_CMD0_SHIFT) |
  330. (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT);
  331. out_be32(&lbc->fir,
  332. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  333. (FIR_OP_CA << FIR_OP1_SHIFT) |
  334. (FIR_OP_PA << FIR_OP2_SHIFT) |
  335. (FIR_OP_WB << FIR_OP3_SHIFT) |
  336. (FIR_OP_CW1 << FIR_OP4_SHIFT));
  337. } else {
  338. fcr = (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT) |
  339. (NAND_CMD_SEQIN << FCR_CMD2_SHIFT);
  340. out_be32(&lbc->fir,
  341. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  342. (FIR_OP_CM2 << FIR_OP1_SHIFT) |
  343. (FIR_OP_CA << FIR_OP2_SHIFT) |
  344. (FIR_OP_PA << FIR_OP3_SHIFT) |
  345. (FIR_OP_WB << FIR_OP4_SHIFT) |
  346. (FIR_OP_CW1 << FIR_OP5_SHIFT));
  347. if (column >= mtd->writesize) {
  348. /* OOB area --> READOOB */
  349. column -= mtd->writesize;
  350. fcr |= NAND_CMD_READOOB << FCR_CMD0_SHIFT;
  351. ctrl->oob = 1;
  352. } else if (column < 256) {
  353. /* First 256 bytes --> READ0 */
  354. fcr |= NAND_CMD_READ0 << FCR_CMD0_SHIFT;
  355. } else {
  356. /* Second 256 bytes --> READ1 */
  357. fcr |= NAND_CMD_READ1 << FCR_CMD0_SHIFT;
  358. }
  359. }
  360. out_be32(&lbc->fcr, fcr);
  361. set_addr(mtd, column, page_addr, ctrl->oob);
  362. return;
  363. }
  364. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  365. case NAND_CMD_PAGEPROG: {
  366. vdbg("fsl_elbc_cmdfunc: NAND_CMD_PAGEPROG "
  367. "writing %d bytes.\n", ctrl->index);
  368. /* if the write did not start at 0 or is not a full page
  369. * then set the exact length, otherwise use a full page
  370. * write so the HW generates the ECC.
  371. */
  372. if (ctrl->oob || ctrl->column != 0 ||
  373. ctrl->index != mtd->writesize + mtd->oobsize)
  374. out_be32(&lbc->fbcr, ctrl->index);
  375. else
  376. out_be32(&lbc->fbcr, 0);
  377. fsl_elbc_run_command(mtd);
  378. return;
  379. }
  380. /* CMD_STATUS must read the status byte while CEB is active */
  381. /* Note - it does not wait for the ready line */
  382. case NAND_CMD_STATUS:
  383. out_be32(&lbc->fir,
  384. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  385. (FIR_OP_RBW << FIR_OP1_SHIFT));
  386. out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
  387. out_be32(&lbc->fbcr, 1);
  388. set_addr(mtd, 0, 0, 0);
  389. ctrl->read_bytes = 1;
  390. fsl_elbc_run_command(mtd);
  391. /* The chip always seems to report that it is
  392. * write-protected, even when it is not.
  393. */
  394. out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
  395. return;
  396. /* RESET without waiting for the ready line */
  397. case NAND_CMD_RESET:
  398. dbg("fsl_elbc_cmdfunc: NAND_CMD_RESET.\n");
  399. out_be32(&lbc->fir, FIR_OP_CM0 << FIR_OP0_SHIFT);
  400. out_be32(&lbc->fcr, NAND_CMD_RESET << FCR_CMD0_SHIFT);
  401. fsl_elbc_run_command(mtd);
  402. return;
  403. default:
  404. printf("fsl_elbc_cmdfunc: error, unsupported command 0x%x.\n",
  405. command);
  406. }
  407. }
  408. static void fsl_elbc_select_chip(struct mtd_info *mtd, int chip)
  409. {
  410. /* The hardware does not seem to support multiple
  411. * chips per bank.
  412. */
  413. }
  414. /*
  415. * Write buf to the FCM Controller Data Buffer
  416. */
  417. static void fsl_elbc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  418. {
  419. struct nand_chip *chip = mtd->priv;
  420. struct fsl_elbc_mtd *priv = chip->priv;
  421. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  422. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  423. if (len <= 0) {
  424. printf("write_buf of %d bytes", len);
  425. ctrl->status = 0;
  426. return;
  427. }
  428. if ((unsigned int)len > bufsize - ctrl->index) {
  429. printf("write_buf beyond end of buffer "
  430. "(%d requested, %u available)\n",
  431. len, bufsize - ctrl->index);
  432. len = bufsize - ctrl->index;
  433. }
  434. memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
  435. /*
  436. * This is workaround for the weird elbc hangs during nand write,
  437. * Scott Wood says: "...perhaps difference in how long it takes a
  438. * write to make it through the localbus compared to a write to IMMR
  439. * is causing problems, and sync isn't helping for some reason."
  440. * Reading back the last byte helps though.
  441. */
  442. in_8(&ctrl->addr[ctrl->index] + len - 1);
  443. ctrl->index += len;
  444. }
  445. /*
  446. * read a byte from either the FCM hardware buffer if it has any data left
  447. * otherwise issue a command to read a single byte.
  448. */
  449. static u8 fsl_elbc_read_byte(struct mtd_info *mtd)
  450. {
  451. struct nand_chip *chip = mtd->priv;
  452. struct fsl_elbc_mtd *priv = chip->priv;
  453. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  454. /* If there are still bytes in the FCM, then use the next byte. */
  455. if (ctrl->index < ctrl->read_bytes)
  456. return in_8(&ctrl->addr[ctrl->index++]);
  457. printf("read_byte beyond end of buffer\n");
  458. return ERR_BYTE;
  459. }
  460. /*
  461. * Read from the FCM Controller Data Buffer
  462. */
  463. static void fsl_elbc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  464. {
  465. struct nand_chip *chip = mtd->priv;
  466. struct fsl_elbc_mtd *priv = chip->priv;
  467. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  468. int avail;
  469. if (len < 0)
  470. return;
  471. avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
  472. memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
  473. ctrl->index += avail;
  474. if (len > avail)
  475. printf("read_buf beyond end of buffer "
  476. "(%d requested, %d available)\n",
  477. len, avail);
  478. }
  479. /*
  480. * Verify buffer against the FCM Controller Data Buffer
  481. */
  482. static int fsl_elbc_verify_buf(struct mtd_info *mtd,
  483. const u_char *buf, int len)
  484. {
  485. struct nand_chip *chip = mtd->priv;
  486. struct fsl_elbc_mtd *priv = chip->priv;
  487. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  488. int i;
  489. if (len < 0) {
  490. printf("write_buf of %d bytes", len);
  491. return -EINVAL;
  492. }
  493. if ((unsigned int)len > ctrl->read_bytes - ctrl->index) {
  494. printf("verify_buf beyond end of buffer "
  495. "(%d requested, %u available)\n",
  496. len, ctrl->read_bytes - ctrl->index);
  497. ctrl->index = ctrl->read_bytes;
  498. return -EINVAL;
  499. }
  500. for (i = 0; i < len; i++)
  501. if (in_8(&ctrl->addr[ctrl->index + i]) != buf[i])
  502. break;
  503. ctrl->index += len;
  504. return i == len && ctrl->status == LTESR_CC ? 0 : -EIO;
  505. }
  506. /* This function is called after Program and Erase Operations to
  507. * check for success or failure.
  508. */
  509. static int fsl_elbc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  510. {
  511. struct fsl_elbc_mtd *priv = chip->priv;
  512. struct fsl_elbc_ctrl *ctrl = priv->ctrl;
  513. fsl_lbc_t *lbc = ctrl->regs;
  514. if (ctrl->status != LTESR_CC)
  515. return NAND_STATUS_FAIL;
  516. /* Use READ_STATUS command, but wait for the device to be ready */
  517. ctrl->use_mdr = 0;
  518. out_be32(&lbc->fir,
  519. (FIR_OP_CW0 << FIR_OP0_SHIFT) |
  520. (FIR_OP_RBW << FIR_OP1_SHIFT));
  521. out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
  522. out_be32(&lbc->fbcr, 1);
  523. set_addr(mtd, 0, 0, 0);
  524. ctrl->read_bytes = 1;
  525. fsl_elbc_run_command(mtd);
  526. if (ctrl->status != LTESR_CC)
  527. return NAND_STATUS_FAIL;
  528. /* The chip always seems to report that it is
  529. * write-protected, even when it is not.
  530. */
  531. out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
  532. return fsl_elbc_read_byte(mtd);
  533. }
  534. static int fsl_elbc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  535. uint8_t *buf, int oob_required, int page)
  536. {
  537. fsl_elbc_read_buf(mtd, buf, mtd->writesize);
  538. fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  539. if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL)
  540. mtd->ecc_stats.failed++;
  541. return 0;
  542. }
  543. /* ECC will be calculated automatically, and errors will be detected in
  544. * waitfunc.
  545. */
  546. static int fsl_elbc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  547. const uint8_t *buf, int oob_required)
  548. {
  549. fsl_elbc_write_buf(mtd, buf, mtd->writesize);
  550. fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  551. return 0;
  552. }
  553. static struct fsl_elbc_ctrl *elbc_ctrl;
  554. static void fsl_elbc_ctrl_init(void)
  555. {
  556. elbc_ctrl = kzalloc(sizeof(*elbc_ctrl), GFP_KERNEL);
  557. if (!elbc_ctrl)
  558. return;
  559. elbc_ctrl->regs = LBC_BASE_ADDR;
  560. /* clear event registers */
  561. out_be32(&elbc_ctrl->regs->ltesr, LTESR_NAND_MASK);
  562. out_be32(&elbc_ctrl->regs->lteatr, 0);
  563. /* Enable interrupts for any detected events */
  564. out_be32(&elbc_ctrl->regs->lteir, LTESR_NAND_MASK);
  565. elbc_ctrl->read_bytes = 0;
  566. elbc_ctrl->index = 0;
  567. elbc_ctrl->addr = NULL;
  568. }
  569. static int fsl_elbc_chip_init(int devnum, u8 *addr)
  570. {
  571. struct mtd_info *mtd = &nand_info[devnum];
  572. struct nand_chip *nand;
  573. struct fsl_elbc_mtd *priv;
  574. uint32_t br = 0, or = 0;
  575. int ret;
  576. if (!elbc_ctrl) {
  577. fsl_elbc_ctrl_init();
  578. if (!elbc_ctrl)
  579. return -1;
  580. }
  581. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  582. if (!priv)
  583. return -ENOMEM;
  584. priv->ctrl = elbc_ctrl;
  585. priv->vbase = addr;
  586. /* Find which chip select it is connected to. It'd be nice
  587. * if we could pass more than one datum to the NAND driver...
  588. */
  589. for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
  590. phys_addr_t phys_addr = virt_to_phys(addr);
  591. br = in_be32(&elbc_ctrl->regs->bank[priv->bank].br);
  592. or = in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
  593. if ((br & BR_V) && (br & BR_MSEL) == BR_MS_FCM &&
  594. (br & or & BR_BA) == BR_PHYS_ADDR(phys_addr))
  595. break;
  596. }
  597. if (priv->bank >= MAX_BANKS) {
  598. printf("fsl_elbc_nand: address did not match any "
  599. "chip selects\n");
  600. return -ENODEV;
  601. }
  602. nand = &priv->chip;
  603. mtd->priv = nand;
  604. elbc_ctrl->chips[priv->bank] = priv;
  605. /* fill in nand_chip structure */
  606. /* set up function call table */
  607. nand->read_byte = fsl_elbc_read_byte;
  608. nand->write_buf = fsl_elbc_write_buf;
  609. nand->read_buf = fsl_elbc_read_buf;
  610. nand->verify_buf = fsl_elbc_verify_buf;
  611. nand->select_chip = fsl_elbc_select_chip;
  612. nand->cmdfunc = fsl_elbc_cmdfunc;
  613. nand->waitfunc = fsl_elbc_wait;
  614. /* set up nand options */
  615. nand->bbt_td = &bbt_main_descr;
  616. nand->bbt_md = &bbt_mirror_descr;
  617. /* set up nand options */
  618. nand->options = NAND_NO_SUBPAGE_WRITE;
  619. nand->bbt_options = NAND_BBT_USE_FLASH;
  620. nand->controller = &elbc_ctrl->controller;
  621. nand->priv = priv;
  622. nand->ecc.read_page = fsl_elbc_read_page;
  623. nand->ecc.write_page = fsl_elbc_write_page;
  624. priv->fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT);
  625. /* If CS Base Register selects full hardware ECC then use it */
  626. if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
  627. nand->ecc.mode = NAND_ECC_HW;
  628. nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
  629. &fsl_elbc_oob_sp_eccm1 :
  630. &fsl_elbc_oob_sp_eccm0;
  631. nand->ecc.size = 512;
  632. nand->ecc.bytes = 3;
  633. nand->ecc.steps = 1;
  634. nand->ecc.strength = 1;
  635. } else {
  636. /* otherwise fall back to default software ECC */
  637. nand->ecc.mode = NAND_ECC_SOFT;
  638. }
  639. ret = nand_scan_ident(mtd, 1, NULL);
  640. if (ret)
  641. return ret;
  642. /* Large-page-specific setup */
  643. if (mtd->writesize == 2048) {
  644. setbits_be32(&elbc_ctrl->regs->bank[priv->bank].or,
  645. OR_FCM_PGS);
  646. in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
  647. priv->page_size = 1;
  648. nand->badblock_pattern = &largepage_memorybased;
  649. /*
  650. * Hardware expects small page has ECCM0, large page has
  651. * ECCM1 when booting from NAND, and we follow that even
  652. * when not booting from NAND.
  653. */
  654. priv->fmr |= FMR_ECCM;
  655. /* adjust ecc setup if needed */
  656. if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
  657. nand->ecc.steps = 4;
  658. nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
  659. &fsl_elbc_oob_lp_eccm1 :
  660. &fsl_elbc_oob_lp_eccm0;
  661. }
  662. } else if (mtd->writesize == 512) {
  663. clrbits_be32(&elbc_ctrl->regs->bank[priv->bank].or,
  664. OR_FCM_PGS);
  665. in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
  666. } else {
  667. return -ENODEV;
  668. }
  669. ret = nand_scan_tail(mtd);
  670. if (ret)
  671. return ret;
  672. ret = nand_register(devnum);
  673. if (ret)
  674. return ret;
  675. return 0;
  676. }
  677. #ifndef CONFIG_SYS_NAND_BASE_LIST
  678. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  679. #endif
  680. static unsigned long base_address[CONFIG_SYS_MAX_NAND_DEVICE] =
  681. CONFIG_SYS_NAND_BASE_LIST;
  682. void board_nand_init(void)
  683. {
  684. int i;
  685. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  686. fsl_elbc_chip_init(i, (u8 *)base_address[i]);
  687. }