fsl_elbc_nand.c 21 KB

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