fsl_elbc_nand.c 22 KB

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