fsl_elbc_nand.c 29 KB

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