fsl_elbc_nand.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /* Freescale Enhanced Local Bus Controller NAND driver
  2. *
  3. * Copyright © 2006-2007, 2010 Freescale Semiconductor
  4. *
  5. * Authors: Nick Spence <nick.spence@freescale.com>,
  6. * Scott Wood <scottwood@freescale.com>
  7. * Jack Lan <jack.lan@freescale.com>
  8. * Roy Zang <tie-fei.zang@freescale.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/string.h>
  29. #include <linux/ioport.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/nand.h>
  36. #include <linux/mtd/nand_ecc.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <asm/io.h>
  39. #include <asm/fsl_lbc.h>
  40. #define MAX_BANKS 8
  41. #define ERR_BYTE 0xFF /* Value returned for read bytes when read failed */
  42. #define FCM_TIMEOUT_MSECS 500 /* Maximum number of mSecs to wait for FCM */
  43. /* mtd information per set */
  44. struct fsl_elbc_mtd {
  45. struct mtd_info mtd;
  46. struct nand_chip chip;
  47. struct fsl_lbc_ctrl *ctrl;
  48. struct device *dev;
  49. int bank; /* Chip select bank number */
  50. u8 __iomem *vbase; /* Chip select base virtual address */
  51. int page_size; /* NAND page size (0=512, 1=2048) */
  52. unsigned int fmr; /* FCM Flash Mode Register value */
  53. };
  54. /* Freescale eLBC FCM controller information */
  55. struct fsl_elbc_fcm_ctrl {
  56. struct nand_hw_control controller;
  57. struct fsl_elbc_mtd *chips[MAX_BANKS];
  58. u8 __iomem *addr; /* Address of assigned FCM buffer */
  59. unsigned int page; /* Last page written to / read from */
  60. unsigned int read_bytes; /* Number of bytes read during command */
  61. unsigned int column; /* Saved column from SEQIN */
  62. unsigned int index; /* Pointer to next byte to 'read' */
  63. unsigned int status; /* status read from LTESR after last op */
  64. unsigned int mdr; /* UPM/FCM Data Register value */
  65. unsigned int use_mdr; /* Non zero if the MDR is to be set */
  66. unsigned int oob; /* Non zero if operating on OOB data */
  67. unsigned int counter; /* counter for the initializations */
  68. };
  69. /* These map to the positions used by the FCM hardware ECC generator */
  70. /* Small Page FLASH with FMR[ECCM] = 0 */
  71. static struct nand_ecclayout fsl_elbc_oob_sp_eccm0 = {
  72. .eccbytes = 3,
  73. .eccpos = {6, 7, 8},
  74. .oobfree = { {0, 5}, {9, 7} },
  75. };
  76. /* Small Page FLASH with FMR[ECCM] = 1 */
  77. static struct nand_ecclayout fsl_elbc_oob_sp_eccm1 = {
  78. .eccbytes = 3,
  79. .eccpos = {8, 9, 10},
  80. .oobfree = { {0, 5}, {6, 2}, {11, 5} },
  81. };
  82. /* Large Page FLASH with FMR[ECCM] = 0 */
  83. static struct nand_ecclayout fsl_elbc_oob_lp_eccm0 = {
  84. .eccbytes = 12,
  85. .eccpos = {6, 7, 8, 22, 23, 24, 38, 39, 40, 54, 55, 56},
  86. .oobfree = { {1, 5}, {9, 13}, {25, 13}, {41, 13}, {57, 7} },
  87. };
  88. /* Large Page FLASH with FMR[ECCM] = 1 */
  89. static struct nand_ecclayout fsl_elbc_oob_lp_eccm1 = {
  90. .eccbytes = 12,
  91. .eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58},
  92. .oobfree = { {1, 7}, {11, 13}, {27, 13}, {43, 13}, {59, 5} },
  93. };
  94. /*
  95. * fsl_elbc_oob_lp_eccm* specify that LP NAND's OOB free area starts at offset
  96. * 1, so we have to adjust bad block pattern. This pattern should be used for
  97. * x8 chips only. So far hardware does not support x16 chips anyway.
  98. */
  99. static u8 scan_ff_pattern[] = { 0xff, };
  100. static struct nand_bbt_descr largepage_memorybased = {
  101. .options = 0,
  102. .offs = 0,
  103. .len = 1,
  104. .pattern = scan_ff_pattern,
  105. };
  106. /*
  107. * ELBC may use HW ECC, so that OOB offsets, that NAND core uses for bbt,
  108. * interfere with ECC positions, that's why we implement our own descriptors.
  109. * OOB {11, 5}, works for both SP and LP chips, with ECCM = 1 and ECCM = 0.
  110. */
  111. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  112. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  113. static struct nand_bbt_descr bbt_main_descr = {
  114. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  115. NAND_BBT_2BIT | NAND_BBT_VERSION,
  116. .offs = 11,
  117. .len = 4,
  118. .veroffs = 15,
  119. .maxblocks = 4,
  120. .pattern = bbt_pattern,
  121. };
  122. static struct nand_bbt_descr bbt_mirror_descr = {
  123. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  124. NAND_BBT_2BIT | NAND_BBT_VERSION,
  125. .offs = 11,
  126. .len = 4,
  127. .veroffs = 15,
  128. .maxblocks = 4,
  129. .pattern = mirror_pattern,
  130. };
  131. /*=================================*/
  132. /*
  133. * Set up the FCM hardware block and page address fields, and the fcm
  134. * structure addr field to point to the correct FCM buffer in memory
  135. */
  136. static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
  137. {
  138. struct nand_chip *chip = mtd->priv;
  139. struct fsl_elbc_mtd *priv = chip->priv;
  140. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  141. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  142. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  143. int buf_num;
  144. elbc_fcm_ctrl->page = page_addr;
  145. if (priv->page_size) {
  146. /*
  147. * large page size chip : FPAR[PI] save the lowest 6 bits,
  148. * FBAR[BLK] save the other bits.
  149. */
  150. out_be32(&lbc->fbar, page_addr >> 6);
  151. out_be32(&lbc->fpar,
  152. ((page_addr << FPAR_LP_PI_SHIFT) & FPAR_LP_PI) |
  153. (oob ? FPAR_LP_MS : 0) | column);
  154. buf_num = (page_addr & 1) << 2;
  155. } else {
  156. /*
  157. * small page size chip : FPAR[PI] save the lowest 5 bits,
  158. * FBAR[BLK] save the other bits.
  159. */
  160. out_be32(&lbc->fbar, page_addr >> 5);
  161. out_be32(&lbc->fpar,
  162. ((page_addr << FPAR_SP_PI_SHIFT) & FPAR_SP_PI) |
  163. (oob ? FPAR_SP_MS : 0) | column);
  164. buf_num = page_addr & 7;
  165. }
  166. elbc_fcm_ctrl->addr = priv->vbase + buf_num * 1024;
  167. elbc_fcm_ctrl->index = column;
  168. /* for OOB data point to the second half of the buffer */
  169. if (oob)
  170. elbc_fcm_ctrl->index += priv->page_size ? 2048 : 512;
  171. dev_vdbg(priv->dev, "set_addr: bank=%d, "
  172. "elbc_fcm_ctrl->addr=0x%p (0x%p), "
  173. "index %x, pes %d ps %d\n",
  174. buf_num, elbc_fcm_ctrl->addr, priv->vbase,
  175. elbc_fcm_ctrl->index,
  176. chip->phys_erase_shift, chip->page_shift);
  177. }
  178. /*
  179. * execute FCM command and wait for it to complete
  180. */
  181. static int fsl_elbc_run_command(struct mtd_info *mtd)
  182. {
  183. struct nand_chip *chip = mtd->priv;
  184. struct fsl_elbc_mtd *priv = chip->priv;
  185. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  186. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  187. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  188. /* Setup the FMR[OP] to execute without write protection */
  189. out_be32(&lbc->fmr, priv->fmr | 3);
  190. if (elbc_fcm_ctrl->use_mdr)
  191. out_be32(&lbc->mdr, elbc_fcm_ctrl->mdr);
  192. dev_vdbg(priv->dev,
  193. "fsl_elbc_run_command: fmr=%08x fir=%08x fcr=%08x\n",
  194. in_be32(&lbc->fmr), in_be32(&lbc->fir), in_be32(&lbc->fcr));
  195. dev_vdbg(priv->dev,
  196. "fsl_elbc_run_command: fbar=%08x fpar=%08x "
  197. "fbcr=%08x bank=%d\n",
  198. in_be32(&lbc->fbar), in_be32(&lbc->fpar),
  199. in_be32(&lbc->fbcr), priv->bank);
  200. ctrl->irq_status = 0;
  201. /* execute special operation */
  202. out_be32(&lbc->lsor, priv->bank);
  203. /* wait for FCM complete flag or timeout */
  204. wait_event_timeout(ctrl->irq_wait, ctrl->irq_status,
  205. FCM_TIMEOUT_MSECS * HZ/1000);
  206. elbc_fcm_ctrl->status = ctrl->irq_status;
  207. /* store mdr value in case it was needed */
  208. if (elbc_fcm_ctrl->use_mdr)
  209. elbc_fcm_ctrl->mdr = in_be32(&lbc->mdr);
  210. elbc_fcm_ctrl->use_mdr = 0;
  211. if (elbc_fcm_ctrl->status != LTESR_CC) {
  212. dev_info(priv->dev,
  213. "command failed: fir %x fcr %x status %x mdr %x\n",
  214. in_be32(&lbc->fir), in_be32(&lbc->fcr),
  215. elbc_fcm_ctrl->status, elbc_fcm_ctrl->mdr);
  216. return -EIO;
  217. }
  218. if (chip->ecc.mode != NAND_ECC_HW)
  219. return 0;
  220. if (elbc_fcm_ctrl->read_bytes == mtd->writesize + mtd->oobsize) {
  221. uint32_t lteccr = in_be32(&lbc->lteccr);
  222. /*
  223. * if command was a full page read and the ELBC
  224. * has the LTECCR register, then bits 12-15 (ppc order) of
  225. * LTECCR indicates which 512 byte sub-pages had fixed errors.
  226. * bits 28-31 are uncorrectable errors, marked elsewhere.
  227. * for small page nand only 1 bit is used.
  228. * if the ELBC doesn't have the lteccr register it reads 0
  229. */
  230. if (lteccr & 0x000F000F)
  231. out_be32(&lbc->lteccr, 0x000F000F); /* clear lteccr */
  232. if (lteccr & 0x000F0000)
  233. mtd->ecc_stats.corrected++;
  234. }
  235. return 0;
  236. }
  237. static void fsl_elbc_do_read(struct nand_chip *chip, int oob)
  238. {
  239. struct fsl_elbc_mtd *priv = chip->priv;
  240. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  241. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  242. if (priv->page_size) {
  243. out_be32(&lbc->fir,
  244. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  245. (FIR_OP_CA << FIR_OP1_SHIFT) |
  246. (FIR_OP_PA << FIR_OP2_SHIFT) |
  247. (FIR_OP_CM1 << FIR_OP3_SHIFT) |
  248. (FIR_OP_RBW << FIR_OP4_SHIFT));
  249. out_be32(&lbc->fcr, (NAND_CMD_READ0 << FCR_CMD0_SHIFT) |
  250. (NAND_CMD_READSTART << FCR_CMD1_SHIFT));
  251. } else {
  252. out_be32(&lbc->fir,
  253. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  254. (FIR_OP_CA << FIR_OP1_SHIFT) |
  255. (FIR_OP_PA << FIR_OP2_SHIFT) |
  256. (FIR_OP_RBW << FIR_OP3_SHIFT));
  257. if (oob)
  258. out_be32(&lbc->fcr, NAND_CMD_READOOB << FCR_CMD0_SHIFT);
  259. else
  260. out_be32(&lbc->fcr, NAND_CMD_READ0 << FCR_CMD0_SHIFT);
  261. }
  262. }
  263. /* cmdfunc send commands to the FCM */
  264. static void fsl_elbc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  265. int column, int page_addr)
  266. {
  267. struct nand_chip *chip = mtd->priv;
  268. struct fsl_elbc_mtd *priv = chip->priv;
  269. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  270. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  271. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  272. elbc_fcm_ctrl->use_mdr = 0;
  273. /* clear the read buffer */
  274. elbc_fcm_ctrl->read_bytes = 0;
  275. if (command != NAND_CMD_PAGEPROG)
  276. elbc_fcm_ctrl->index = 0;
  277. switch (command) {
  278. /* READ0 and READ1 read the entire buffer to use hardware ECC. */
  279. case NAND_CMD_READ1:
  280. column += 256;
  281. /* fall-through */
  282. case NAND_CMD_READ0:
  283. dev_dbg(priv->dev,
  284. "fsl_elbc_cmdfunc: NAND_CMD_READ0, page_addr:"
  285. " 0x%x, column: 0x%x.\n", page_addr, column);
  286. out_be32(&lbc->fbcr, 0); /* read entire page to enable ECC */
  287. set_addr(mtd, 0, page_addr, 0);
  288. elbc_fcm_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  289. elbc_fcm_ctrl->index += column;
  290. fsl_elbc_do_read(chip, 0);
  291. fsl_elbc_run_command(mtd);
  292. return;
  293. /* READOOB reads only the OOB because no ECC is performed. */
  294. case NAND_CMD_READOOB:
  295. dev_vdbg(priv->dev,
  296. "fsl_elbc_cmdfunc: NAND_CMD_READOOB, page_addr:"
  297. " 0x%x, column: 0x%x.\n", page_addr, column);
  298. out_be32(&lbc->fbcr, mtd->oobsize - column);
  299. set_addr(mtd, column, page_addr, 1);
  300. elbc_fcm_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  301. fsl_elbc_do_read(chip, 1);
  302. fsl_elbc_run_command(mtd);
  303. return;
  304. case NAND_CMD_READID:
  305. case NAND_CMD_PARAM:
  306. dev_vdbg(priv->dev, "fsl_elbc_cmdfunc: NAND_CMD %x\n", command);
  307. out_be32(&lbc->fir, (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  308. (FIR_OP_UA << FIR_OP1_SHIFT) |
  309. (FIR_OP_RBW << FIR_OP2_SHIFT));
  310. out_be32(&lbc->fcr, command << FCR_CMD0_SHIFT);
  311. /*
  312. * although currently it's 8 bytes for READID, we always read
  313. * the maximum 256 bytes(for PARAM)
  314. */
  315. out_be32(&lbc->fbcr, 256);
  316. elbc_fcm_ctrl->read_bytes = 256;
  317. elbc_fcm_ctrl->use_mdr = 1;
  318. elbc_fcm_ctrl->mdr = column;
  319. set_addr(mtd, 0, 0, 0);
  320. fsl_elbc_run_command(mtd);
  321. return;
  322. /* ERASE1 stores the block and page address */
  323. case NAND_CMD_ERASE1:
  324. dev_vdbg(priv->dev,
  325. "fsl_elbc_cmdfunc: NAND_CMD_ERASE1, "
  326. "page_addr: 0x%x.\n", page_addr);
  327. set_addr(mtd, 0, page_addr, 0);
  328. return;
  329. /* ERASE2 uses the block and page address from ERASE1 */
  330. case NAND_CMD_ERASE2:
  331. dev_vdbg(priv->dev, "fsl_elbc_cmdfunc: NAND_CMD_ERASE2.\n");
  332. out_be32(&lbc->fir,
  333. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  334. (FIR_OP_PA << FIR_OP1_SHIFT) |
  335. (FIR_OP_CM2 << FIR_OP2_SHIFT) |
  336. (FIR_OP_CW1 << FIR_OP3_SHIFT) |
  337. (FIR_OP_RS << FIR_OP4_SHIFT));
  338. out_be32(&lbc->fcr,
  339. (NAND_CMD_ERASE1 << FCR_CMD0_SHIFT) |
  340. (NAND_CMD_STATUS << FCR_CMD1_SHIFT) |
  341. (NAND_CMD_ERASE2 << FCR_CMD2_SHIFT));
  342. out_be32(&lbc->fbcr, 0);
  343. elbc_fcm_ctrl->read_bytes = 0;
  344. elbc_fcm_ctrl->use_mdr = 1;
  345. fsl_elbc_run_command(mtd);
  346. return;
  347. /* SEQIN sets up the addr buffer and all registers except the length */
  348. case NAND_CMD_SEQIN: {
  349. __be32 fcr;
  350. dev_vdbg(priv->dev,
  351. "fsl_elbc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, "
  352. "page_addr: 0x%x, column: 0x%x.\n",
  353. page_addr, column);
  354. elbc_fcm_ctrl->column = column;
  355. elbc_fcm_ctrl->use_mdr = 1;
  356. if (column >= mtd->writesize) {
  357. /* OOB area */
  358. column -= mtd->writesize;
  359. elbc_fcm_ctrl->oob = 1;
  360. } else {
  361. WARN_ON(column != 0);
  362. elbc_fcm_ctrl->oob = 0;
  363. }
  364. fcr = (NAND_CMD_STATUS << FCR_CMD1_SHIFT) |
  365. (NAND_CMD_SEQIN << FCR_CMD2_SHIFT) |
  366. (NAND_CMD_PAGEPROG << FCR_CMD3_SHIFT);
  367. if (priv->page_size) {
  368. out_be32(&lbc->fir,
  369. (FIR_OP_CM2 << FIR_OP0_SHIFT) |
  370. (FIR_OP_CA << FIR_OP1_SHIFT) |
  371. (FIR_OP_PA << FIR_OP2_SHIFT) |
  372. (FIR_OP_WB << FIR_OP3_SHIFT) |
  373. (FIR_OP_CM3 << FIR_OP4_SHIFT) |
  374. (FIR_OP_CW1 << FIR_OP5_SHIFT) |
  375. (FIR_OP_RS << FIR_OP6_SHIFT));
  376. } else {
  377. out_be32(&lbc->fir,
  378. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  379. (FIR_OP_CM2 << FIR_OP1_SHIFT) |
  380. (FIR_OP_CA << FIR_OP2_SHIFT) |
  381. (FIR_OP_PA << FIR_OP3_SHIFT) |
  382. (FIR_OP_WB << FIR_OP4_SHIFT) |
  383. (FIR_OP_CM3 << FIR_OP5_SHIFT) |
  384. (FIR_OP_CW1 << FIR_OP6_SHIFT) |
  385. (FIR_OP_RS << FIR_OP7_SHIFT));
  386. if (elbc_fcm_ctrl->oob)
  387. /* OOB area --> READOOB */
  388. fcr |= NAND_CMD_READOOB << FCR_CMD0_SHIFT;
  389. else
  390. /* First 256 bytes --> READ0 */
  391. fcr |= NAND_CMD_READ0 << FCR_CMD0_SHIFT;
  392. }
  393. out_be32(&lbc->fcr, fcr);
  394. set_addr(mtd, column, page_addr, elbc_fcm_ctrl->oob);
  395. return;
  396. }
  397. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  398. case NAND_CMD_PAGEPROG: {
  399. dev_vdbg(priv->dev,
  400. "fsl_elbc_cmdfunc: NAND_CMD_PAGEPROG "
  401. "writing %d bytes.\n", elbc_fcm_ctrl->index);
  402. /* if the write did not start at 0 or is not a full page
  403. * then set the exact length, otherwise use a full page
  404. * write so the HW generates the ECC.
  405. */
  406. if (elbc_fcm_ctrl->oob || elbc_fcm_ctrl->column != 0 ||
  407. elbc_fcm_ctrl->index != mtd->writesize + mtd->oobsize)
  408. out_be32(&lbc->fbcr,
  409. elbc_fcm_ctrl->index - elbc_fcm_ctrl->column);
  410. else
  411. out_be32(&lbc->fbcr, 0);
  412. fsl_elbc_run_command(mtd);
  413. return;
  414. }
  415. /* CMD_STATUS must read the status byte while CEB is active */
  416. /* Note - it does not wait for the ready line */
  417. case NAND_CMD_STATUS:
  418. out_be32(&lbc->fir,
  419. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  420. (FIR_OP_RBW << FIR_OP1_SHIFT));
  421. out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
  422. out_be32(&lbc->fbcr, 1);
  423. set_addr(mtd, 0, 0, 0);
  424. elbc_fcm_ctrl->read_bytes = 1;
  425. fsl_elbc_run_command(mtd);
  426. /* The chip always seems to report that it is
  427. * write-protected, even when it is not.
  428. */
  429. setbits8(elbc_fcm_ctrl->addr, NAND_STATUS_WP);
  430. return;
  431. /* RESET without waiting for the ready line */
  432. case NAND_CMD_RESET:
  433. dev_dbg(priv->dev, "fsl_elbc_cmdfunc: NAND_CMD_RESET.\n");
  434. out_be32(&lbc->fir, FIR_OP_CM0 << FIR_OP0_SHIFT);
  435. out_be32(&lbc->fcr, NAND_CMD_RESET << FCR_CMD0_SHIFT);
  436. fsl_elbc_run_command(mtd);
  437. return;
  438. default:
  439. dev_err(priv->dev,
  440. "fsl_elbc_cmdfunc: error, unsupported command 0x%x.\n",
  441. command);
  442. }
  443. }
  444. static void fsl_elbc_select_chip(struct mtd_info *mtd, int chip)
  445. {
  446. /* The hardware does not seem to support multiple
  447. * chips per bank.
  448. */
  449. }
  450. /*
  451. * Write buf to the FCM Controller Data Buffer
  452. */
  453. static void fsl_elbc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  454. {
  455. struct nand_chip *chip = mtd->priv;
  456. struct fsl_elbc_mtd *priv = chip->priv;
  457. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  458. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  459. if (len <= 0) {
  460. dev_err(priv->dev, "write_buf of %d bytes", len);
  461. elbc_fcm_ctrl->status = 0;
  462. return;
  463. }
  464. if ((unsigned int)len > bufsize - elbc_fcm_ctrl->index) {
  465. dev_err(priv->dev,
  466. "write_buf beyond end of buffer "
  467. "(%d requested, %u available)\n",
  468. len, bufsize - elbc_fcm_ctrl->index);
  469. len = bufsize - elbc_fcm_ctrl->index;
  470. }
  471. memcpy_toio(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index], buf, len);
  472. /*
  473. * This is workaround for the weird elbc hangs during nand write,
  474. * Scott Wood says: "...perhaps difference in how long it takes a
  475. * write to make it through the localbus compared to a write to IMMR
  476. * is causing problems, and sync isn't helping for some reason."
  477. * Reading back the last byte helps though.
  478. */
  479. in_8(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index] + len - 1);
  480. elbc_fcm_ctrl->index += len;
  481. }
  482. /*
  483. * read a byte from either the FCM hardware buffer if it has any data left
  484. * otherwise issue a command to read a single byte.
  485. */
  486. static u8 fsl_elbc_read_byte(struct mtd_info *mtd)
  487. {
  488. struct nand_chip *chip = mtd->priv;
  489. struct fsl_elbc_mtd *priv = chip->priv;
  490. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  491. /* If there are still bytes in the FCM, then use the next byte. */
  492. if (elbc_fcm_ctrl->index < elbc_fcm_ctrl->read_bytes)
  493. return in_8(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index++]);
  494. dev_err(priv->dev, "read_byte beyond end of buffer\n");
  495. return ERR_BYTE;
  496. }
  497. /*
  498. * Read from the FCM Controller Data Buffer
  499. */
  500. static void fsl_elbc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  501. {
  502. struct nand_chip *chip = mtd->priv;
  503. struct fsl_elbc_mtd *priv = chip->priv;
  504. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  505. int avail;
  506. if (len < 0)
  507. return;
  508. avail = min((unsigned int)len,
  509. elbc_fcm_ctrl->read_bytes - elbc_fcm_ctrl->index);
  510. memcpy_fromio(buf, &elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index], avail);
  511. elbc_fcm_ctrl->index += avail;
  512. if (len > avail)
  513. dev_err(priv->dev,
  514. "read_buf beyond end of buffer "
  515. "(%d requested, %d available)\n",
  516. len, avail);
  517. }
  518. /*
  519. * Verify buffer against the FCM Controller Data Buffer
  520. */
  521. static int fsl_elbc_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  522. {
  523. struct nand_chip *chip = mtd->priv;
  524. struct fsl_elbc_mtd *priv = chip->priv;
  525. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  526. int i;
  527. if (len < 0) {
  528. dev_err(priv->dev, "write_buf of %d bytes", len);
  529. return -EINVAL;
  530. }
  531. if ((unsigned int)len >
  532. elbc_fcm_ctrl->read_bytes - elbc_fcm_ctrl->index) {
  533. dev_err(priv->dev,
  534. "verify_buf beyond end of buffer "
  535. "(%d requested, %u available)\n",
  536. len, elbc_fcm_ctrl->read_bytes - elbc_fcm_ctrl->index);
  537. elbc_fcm_ctrl->index = elbc_fcm_ctrl->read_bytes;
  538. return -EINVAL;
  539. }
  540. for (i = 0; i < len; i++)
  541. if (in_8(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index + i])
  542. != buf[i])
  543. break;
  544. elbc_fcm_ctrl->index += len;
  545. return i == len && elbc_fcm_ctrl->status == LTESR_CC ? 0 : -EIO;
  546. }
  547. /* This function is called after Program and Erase Operations to
  548. * check for success or failure.
  549. */
  550. static int fsl_elbc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  551. {
  552. struct fsl_elbc_mtd *priv = chip->priv;
  553. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  554. if (elbc_fcm_ctrl->status != LTESR_CC)
  555. return NAND_STATUS_FAIL;
  556. /* The chip always seems to report that it is
  557. * write-protected, even when it is not.
  558. */
  559. return (elbc_fcm_ctrl->mdr & 0xff) | NAND_STATUS_WP;
  560. }
  561. static int fsl_elbc_chip_init_tail(struct mtd_info *mtd)
  562. {
  563. struct nand_chip *chip = mtd->priv;
  564. struct fsl_elbc_mtd *priv = chip->priv;
  565. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  566. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  567. unsigned int al;
  568. /* calculate FMR Address Length field */
  569. al = 0;
  570. if (chip->pagemask & 0xffff0000)
  571. al++;
  572. if (chip->pagemask & 0xff000000)
  573. al++;
  574. priv->fmr |= al << FMR_AL_SHIFT;
  575. dev_dbg(priv->dev, "fsl_elbc_init: nand->numchips = %d\n",
  576. chip->numchips);
  577. dev_dbg(priv->dev, "fsl_elbc_init: nand->chipsize = %lld\n",
  578. chip->chipsize);
  579. dev_dbg(priv->dev, "fsl_elbc_init: nand->pagemask = %8x\n",
  580. chip->pagemask);
  581. dev_dbg(priv->dev, "fsl_elbc_init: nand->chip_delay = %d\n",
  582. chip->chip_delay);
  583. dev_dbg(priv->dev, "fsl_elbc_init: nand->badblockpos = %d\n",
  584. chip->badblockpos);
  585. dev_dbg(priv->dev, "fsl_elbc_init: nand->chip_shift = %d\n",
  586. chip->chip_shift);
  587. dev_dbg(priv->dev, "fsl_elbc_init: nand->page_shift = %d\n",
  588. chip->page_shift);
  589. dev_dbg(priv->dev, "fsl_elbc_init: nand->phys_erase_shift = %d\n",
  590. chip->phys_erase_shift);
  591. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecclayout = %p\n",
  592. chip->ecclayout);
  593. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.mode = %d\n",
  594. chip->ecc.mode);
  595. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.steps = %d\n",
  596. chip->ecc.steps);
  597. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.bytes = %d\n",
  598. chip->ecc.bytes);
  599. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.total = %d\n",
  600. chip->ecc.total);
  601. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.layout = %p\n",
  602. chip->ecc.layout);
  603. dev_dbg(priv->dev, "fsl_elbc_init: mtd->flags = %08x\n", mtd->flags);
  604. dev_dbg(priv->dev, "fsl_elbc_init: mtd->size = %lld\n", mtd->size);
  605. dev_dbg(priv->dev, "fsl_elbc_init: mtd->erasesize = %d\n",
  606. mtd->erasesize);
  607. dev_dbg(priv->dev, "fsl_elbc_init: mtd->writesize = %d\n",
  608. mtd->writesize);
  609. dev_dbg(priv->dev, "fsl_elbc_init: mtd->oobsize = %d\n",
  610. mtd->oobsize);
  611. /* adjust Option Register and ECC to match Flash page size */
  612. if (mtd->writesize == 512) {
  613. priv->page_size = 0;
  614. clrbits32(&lbc->bank[priv->bank].or, OR_FCM_PGS);
  615. } else if (mtd->writesize == 2048) {
  616. priv->page_size = 1;
  617. setbits32(&lbc->bank[priv->bank].or, OR_FCM_PGS);
  618. /* adjust ecc setup if needed */
  619. if ((in_be32(&lbc->bank[priv->bank].br) & BR_DECC) ==
  620. BR_DECC_CHK_GEN) {
  621. chip->ecc.size = 512;
  622. chip->ecc.layout = (priv->fmr & FMR_ECCM) ?
  623. &fsl_elbc_oob_lp_eccm1 :
  624. &fsl_elbc_oob_lp_eccm0;
  625. chip->badblock_pattern = &largepage_memorybased;
  626. }
  627. } else {
  628. dev_err(priv->dev,
  629. "fsl_elbc_init: page size %d is not supported\n",
  630. mtd->writesize);
  631. return -1;
  632. }
  633. return 0;
  634. }
  635. static int fsl_elbc_read_page(struct mtd_info *mtd,
  636. struct nand_chip *chip,
  637. uint8_t *buf,
  638. int page)
  639. {
  640. fsl_elbc_read_buf(mtd, buf, mtd->writesize);
  641. fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  642. if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL)
  643. mtd->ecc_stats.failed++;
  644. return 0;
  645. }
  646. /* ECC will be calculated automatically, and errors will be detected in
  647. * waitfunc.
  648. */
  649. static void fsl_elbc_write_page(struct mtd_info *mtd,
  650. struct nand_chip *chip,
  651. const uint8_t *buf)
  652. {
  653. fsl_elbc_write_buf(mtd, buf, mtd->writesize);
  654. fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  655. }
  656. static int fsl_elbc_chip_init(struct fsl_elbc_mtd *priv)
  657. {
  658. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  659. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  660. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  661. struct nand_chip *chip = &priv->chip;
  662. dev_dbg(priv->dev, "eLBC Set Information for bank %d\n", priv->bank);
  663. /* Fill in fsl_elbc_mtd structure */
  664. priv->mtd.priv = chip;
  665. priv->mtd.owner = THIS_MODULE;
  666. /* set timeout to maximum */
  667. priv->fmr = 15 << FMR_CWTO_SHIFT;
  668. if (in_be32(&lbc->bank[priv->bank].or) & OR_FCM_PGS)
  669. priv->fmr |= FMR_ECCM;
  670. /* fill in nand_chip structure */
  671. /* set up function call table */
  672. chip->read_byte = fsl_elbc_read_byte;
  673. chip->write_buf = fsl_elbc_write_buf;
  674. chip->read_buf = fsl_elbc_read_buf;
  675. chip->verify_buf = fsl_elbc_verify_buf;
  676. chip->select_chip = fsl_elbc_select_chip;
  677. chip->cmdfunc = fsl_elbc_cmdfunc;
  678. chip->waitfunc = fsl_elbc_wait;
  679. chip->bbt_td = &bbt_main_descr;
  680. chip->bbt_md = &bbt_mirror_descr;
  681. /* set up nand options */
  682. chip->options = NAND_NO_READRDY | NAND_NO_AUTOINCR;
  683. chip->bbt_options = NAND_BBT_USE_FLASH;
  684. chip->controller = &elbc_fcm_ctrl->controller;
  685. chip->priv = priv;
  686. chip->ecc.read_page = fsl_elbc_read_page;
  687. chip->ecc.write_page = fsl_elbc_write_page;
  688. /* If CS Base Register selects full hardware ECC then use it */
  689. if ((in_be32(&lbc->bank[priv->bank].br) & BR_DECC) ==
  690. BR_DECC_CHK_GEN) {
  691. chip->ecc.mode = NAND_ECC_HW;
  692. /* put in small page settings and adjust later if needed */
  693. chip->ecc.layout = (priv->fmr & FMR_ECCM) ?
  694. &fsl_elbc_oob_sp_eccm1 : &fsl_elbc_oob_sp_eccm0;
  695. chip->ecc.size = 512;
  696. chip->ecc.bytes = 3;
  697. chip->ecc.strength = 1;
  698. /*
  699. * FIXME: can hardware ecc correct 4 bitflips if page size is
  700. * 2k? Then does hardware report number of corrections for this
  701. * case? If so, ecc_stats reporting needs to be fixed as well.
  702. */
  703. } else {
  704. /* otherwise fall back to default software ECC */
  705. chip->ecc.mode = NAND_ECC_SOFT;
  706. }
  707. return 0;
  708. }
  709. static int fsl_elbc_chip_remove(struct fsl_elbc_mtd *priv)
  710. {
  711. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  712. nand_release(&priv->mtd);
  713. kfree(priv->mtd.name);
  714. if (priv->vbase)
  715. iounmap(priv->vbase);
  716. elbc_fcm_ctrl->chips[priv->bank] = NULL;
  717. kfree(priv);
  718. return 0;
  719. }
  720. static DEFINE_MUTEX(fsl_elbc_nand_mutex);
  721. static int __devinit fsl_elbc_nand_probe(struct platform_device *pdev)
  722. {
  723. struct fsl_lbc_regs __iomem *lbc;
  724. struct fsl_elbc_mtd *priv;
  725. struct resource res;
  726. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl;
  727. static const char *part_probe_types[]
  728. = { "cmdlinepart", "RedBoot", "ofpart", NULL };
  729. int ret;
  730. int bank;
  731. struct device *dev;
  732. struct device_node *node = pdev->dev.of_node;
  733. struct mtd_part_parser_data ppdata;
  734. ppdata.of_node = pdev->dev.of_node;
  735. if (!fsl_lbc_ctrl_dev || !fsl_lbc_ctrl_dev->regs)
  736. return -ENODEV;
  737. lbc = fsl_lbc_ctrl_dev->regs;
  738. dev = fsl_lbc_ctrl_dev->dev;
  739. /* get, allocate and map the memory resource */
  740. ret = of_address_to_resource(node, 0, &res);
  741. if (ret) {
  742. dev_err(dev, "failed to get resource\n");
  743. return ret;
  744. }
  745. /* find which chip select it is connected to */
  746. for (bank = 0; bank < MAX_BANKS; bank++)
  747. if ((in_be32(&lbc->bank[bank].br) & BR_V) &&
  748. (in_be32(&lbc->bank[bank].br) & BR_MSEL) == BR_MS_FCM &&
  749. (in_be32(&lbc->bank[bank].br) &
  750. in_be32(&lbc->bank[bank].or) & BR_BA)
  751. == fsl_lbc_addr(res.start))
  752. break;
  753. if (bank >= MAX_BANKS) {
  754. dev_err(dev, "address did not match any chip selects\n");
  755. return -ENODEV;
  756. }
  757. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  758. if (!priv)
  759. return -ENOMEM;
  760. mutex_lock(&fsl_elbc_nand_mutex);
  761. if (!fsl_lbc_ctrl_dev->nand) {
  762. elbc_fcm_ctrl = kzalloc(sizeof(*elbc_fcm_ctrl), GFP_KERNEL);
  763. if (!elbc_fcm_ctrl) {
  764. dev_err(dev, "failed to allocate memory\n");
  765. mutex_unlock(&fsl_elbc_nand_mutex);
  766. ret = -ENOMEM;
  767. goto err;
  768. }
  769. elbc_fcm_ctrl->counter++;
  770. spin_lock_init(&elbc_fcm_ctrl->controller.lock);
  771. init_waitqueue_head(&elbc_fcm_ctrl->controller.wq);
  772. fsl_lbc_ctrl_dev->nand = elbc_fcm_ctrl;
  773. } else {
  774. elbc_fcm_ctrl = fsl_lbc_ctrl_dev->nand;
  775. }
  776. mutex_unlock(&fsl_elbc_nand_mutex);
  777. elbc_fcm_ctrl->chips[bank] = priv;
  778. priv->bank = bank;
  779. priv->ctrl = fsl_lbc_ctrl_dev;
  780. priv->dev = dev;
  781. priv->vbase = ioremap(res.start, resource_size(&res));
  782. if (!priv->vbase) {
  783. dev_err(dev, "failed to map chip region\n");
  784. ret = -ENOMEM;
  785. goto err;
  786. }
  787. priv->mtd.name = kasprintf(GFP_KERNEL, "%x.flash", (unsigned)res.start);
  788. if (!priv->mtd.name) {
  789. ret = -ENOMEM;
  790. goto err;
  791. }
  792. ret = fsl_elbc_chip_init(priv);
  793. if (ret)
  794. goto err;
  795. ret = nand_scan_ident(&priv->mtd, 1, NULL);
  796. if (ret)
  797. goto err;
  798. ret = fsl_elbc_chip_init_tail(&priv->mtd);
  799. if (ret)
  800. goto err;
  801. ret = nand_scan_tail(&priv->mtd);
  802. if (ret)
  803. goto err;
  804. /* First look for RedBoot table or partitions on the command
  805. * line, these take precedence over device tree information */
  806. mtd_device_parse_register(&priv->mtd, part_probe_types, &ppdata,
  807. NULL, 0);
  808. printk(KERN_INFO "eLBC NAND device at 0x%llx, bank %d\n",
  809. (unsigned long long)res.start, priv->bank);
  810. return 0;
  811. err:
  812. fsl_elbc_chip_remove(priv);
  813. return ret;
  814. }
  815. static int fsl_elbc_nand_remove(struct platform_device *pdev)
  816. {
  817. int i;
  818. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = fsl_lbc_ctrl_dev->nand;
  819. for (i = 0; i < MAX_BANKS; i++)
  820. if (elbc_fcm_ctrl->chips[i])
  821. fsl_elbc_chip_remove(elbc_fcm_ctrl->chips[i]);
  822. mutex_lock(&fsl_elbc_nand_mutex);
  823. elbc_fcm_ctrl->counter--;
  824. if (!elbc_fcm_ctrl->counter) {
  825. fsl_lbc_ctrl_dev->nand = NULL;
  826. kfree(elbc_fcm_ctrl);
  827. }
  828. mutex_unlock(&fsl_elbc_nand_mutex);
  829. return 0;
  830. }
  831. static const struct of_device_id fsl_elbc_nand_match[] = {
  832. { .compatible = "fsl,elbc-fcm-nand", },
  833. {}
  834. };
  835. static struct platform_driver fsl_elbc_nand_driver = {
  836. .driver = {
  837. .name = "fsl,elbc-fcm-nand",
  838. .owner = THIS_MODULE,
  839. .of_match_table = fsl_elbc_nand_match,
  840. },
  841. .probe = fsl_elbc_nand_probe,
  842. .remove = fsl_elbc_nand_remove,
  843. };
  844. module_platform_driver(fsl_elbc_nand_driver);
  845. MODULE_LICENSE("GPL");
  846. MODULE_AUTHOR("Freescale");
  847. MODULE_DESCRIPTION("Freescale Enhanced Local Bus Controller MTD NAND driver");