fsl_elbc_nand.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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_controller 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 nand_chip *chip, unsigned int command,
  274. int column, int page_addr)
  275. {
  276. struct mtd_info *mtd = nand_to_mtd(chip);
  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 nand_chip *chip, int cs)
  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 nand_chip *chip, const u8 *buf, int len)
  463. {
  464. struct mtd_info *mtd = nand_to_mtd(chip);
  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 nand_chip *chip)
  496. {
  497. struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
  498. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  499. /* If there are still bytes in the FCM, then use the next byte. */
  500. if (elbc_fcm_ctrl->index < elbc_fcm_ctrl->read_bytes)
  501. return in_8(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index++]);
  502. dev_err(priv->dev, "read_byte beyond end of buffer\n");
  503. return ERR_BYTE;
  504. }
  505. /*
  506. * Read from the FCM Controller Data Buffer
  507. */
  508. static void fsl_elbc_read_buf(struct nand_chip *chip, u8 *buf, int len)
  509. {
  510. struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
  511. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  512. int avail;
  513. if (len < 0)
  514. return;
  515. avail = min((unsigned int)len,
  516. elbc_fcm_ctrl->read_bytes - elbc_fcm_ctrl->index);
  517. memcpy_fromio(buf, &elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index], avail);
  518. elbc_fcm_ctrl->index += avail;
  519. if (len > avail)
  520. dev_err(priv->dev,
  521. "read_buf beyond end of buffer "
  522. "(%d requested, %d available)\n",
  523. len, avail);
  524. }
  525. /* This function is called after Program and Erase Operations to
  526. * check for success or failure.
  527. */
  528. static int fsl_elbc_wait(struct nand_chip *chip)
  529. {
  530. struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
  531. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  532. if (elbc_fcm_ctrl->status != LTESR_CC)
  533. return NAND_STATUS_FAIL;
  534. /* The chip always seems to report that it is
  535. * write-protected, even when it is not.
  536. */
  537. return (elbc_fcm_ctrl->mdr & 0xff) | NAND_STATUS_WP;
  538. }
  539. static int fsl_elbc_attach_chip(struct nand_chip *chip)
  540. {
  541. struct mtd_info *mtd = nand_to_mtd(chip);
  542. struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
  543. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  544. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  545. unsigned int al;
  546. /* calculate FMR Address Length field */
  547. al = 0;
  548. if (chip->pagemask & 0xffff0000)
  549. al++;
  550. if (chip->pagemask & 0xff000000)
  551. al++;
  552. priv->fmr |= al << FMR_AL_SHIFT;
  553. dev_dbg(priv->dev, "fsl_elbc_init: nand->numchips = %d\n",
  554. chip->numchips);
  555. dev_dbg(priv->dev, "fsl_elbc_init: nand->chipsize = %lld\n",
  556. chip->chipsize);
  557. dev_dbg(priv->dev, "fsl_elbc_init: nand->pagemask = %8x\n",
  558. chip->pagemask);
  559. dev_dbg(priv->dev, "fsl_elbc_init: nand->chip_delay = %d\n",
  560. chip->chip_delay);
  561. dev_dbg(priv->dev, "fsl_elbc_init: nand->badblockpos = %d\n",
  562. chip->badblockpos);
  563. dev_dbg(priv->dev, "fsl_elbc_init: nand->chip_shift = %d\n",
  564. chip->chip_shift);
  565. dev_dbg(priv->dev, "fsl_elbc_init: nand->page_shift = %d\n",
  566. chip->page_shift);
  567. dev_dbg(priv->dev, "fsl_elbc_init: nand->phys_erase_shift = %d\n",
  568. chip->phys_erase_shift);
  569. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.mode = %d\n",
  570. chip->ecc.mode);
  571. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.steps = %d\n",
  572. chip->ecc.steps);
  573. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.bytes = %d\n",
  574. chip->ecc.bytes);
  575. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.total = %d\n",
  576. chip->ecc.total);
  577. dev_dbg(priv->dev, "fsl_elbc_init: mtd->ooblayout = %p\n",
  578. mtd->ooblayout);
  579. dev_dbg(priv->dev, "fsl_elbc_init: mtd->flags = %08x\n", mtd->flags);
  580. dev_dbg(priv->dev, "fsl_elbc_init: mtd->size = %lld\n", mtd->size);
  581. dev_dbg(priv->dev, "fsl_elbc_init: mtd->erasesize = %d\n",
  582. mtd->erasesize);
  583. dev_dbg(priv->dev, "fsl_elbc_init: mtd->writesize = %d\n",
  584. mtd->writesize);
  585. dev_dbg(priv->dev, "fsl_elbc_init: mtd->oobsize = %d\n",
  586. mtd->oobsize);
  587. /* adjust Option Register and ECC to match Flash page size */
  588. if (mtd->writesize == 512) {
  589. priv->page_size = 0;
  590. clrbits32(&lbc->bank[priv->bank].or, OR_FCM_PGS);
  591. } else if (mtd->writesize == 2048) {
  592. priv->page_size = 1;
  593. setbits32(&lbc->bank[priv->bank].or, OR_FCM_PGS);
  594. } else {
  595. dev_err(priv->dev,
  596. "fsl_elbc_init: page size %d is not supported\n",
  597. mtd->writesize);
  598. return -ENOTSUPP;
  599. }
  600. return 0;
  601. }
  602. static const struct nand_controller_ops fsl_elbc_controller_ops = {
  603. .attach_chip = fsl_elbc_attach_chip,
  604. };
  605. static int fsl_elbc_read_page(struct nand_chip *chip, uint8_t *buf,
  606. int oob_required, int page)
  607. {
  608. struct mtd_info *mtd = nand_to_mtd(chip);
  609. struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
  610. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  611. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  612. nand_read_page_op(chip, page, 0, buf, mtd->writesize);
  613. if (oob_required)
  614. fsl_elbc_read_buf(chip, chip->oob_poi, mtd->oobsize);
  615. if (fsl_elbc_wait(chip) & NAND_STATUS_FAIL)
  616. mtd->ecc_stats.failed++;
  617. return elbc_fcm_ctrl->max_bitflips;
  618. }
  619. /* ECC will be calculated automatically, and errors will be detected in
  620. * waitfunc.
  621. */
  622. static int fsl_elbc_write_page(struct nand_chip *chip, const uint8_t *buf,
  623. int oob_required, int page)
  624. {
  625. struct mtd_info *mtd = nand_to_mtd(chip);
  626. nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
  627. fsl_elbc_write_buf(chip, chip->oob_poi, mtd->oobsize);
  628. return nand_prog_page_end_op(chip);
  629. }
  630. /* ECC will be calculated automatically, and errors will be detected in
  631. * waitfunc.
  632. */
  633. static int fsl_elbc_write_subpage(struct nand_chip *chip, uint32_t offset,
  634. uint32_t data_len, const uint8_t *buf,
  635. int oob_required, int page)
  636. {
  637. struct mtd_info *mtd = nand_to_mtd(chip);
  638. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  639. fsl_elbc_write_buf(chip, buf, mtd->writesize);
  640. fsl_elbc_write_buf(chip, chip->oob_poi, mtd->oobsize);
  641. return nand_prog_page_end_op(chip);
  642. }
  643. static int fsl_elbc_chip_init(struct fsl_elbc_mtd *priv)
  644. {
  645. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  646. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  647. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  648. struct nand_chip *chip = &priv->chip;
  649. struct mtd_info *mtd = nand_to_mtd(chip);
  650. dev_dbg(priv->dev, "eLBC Set Information for bank %d\n", priv->bank);
  651. /* Fill in fsl_elbc_mtd structure */
  652. mtd->dev.parent = priv->dev;
  653. nand_set_flash_node(chip, priv->dev->of_node);
  654. /* set timeout to maximum */
  655. priv->fmr = 15 << FMR_CWTO_SHIFT;
  656. if (in_be32(&lbc->bank[priv->bank].or) & OR_FCM_PGS)
  657. priv->fmr |= FMR_ECCM;
  658. /* fill in nand_chip structure */
  659. /* set up function call table */
  660. chip->legacy.read_byte = fsl_elbc_read_byte;
  661. chip->legacy.write_buf = fsl_elbc_write_buf;
  662. chip->legacy.read_buf = fsl_elbc_read_buf;
  663. chip->select_chip = fsl_elbc_select_chip;
  664. chip->legacy.cmdfunc = fsl_elbc_cmdfunc;
  665. chip->legacy.waitfunc = fsl_elbc_wait;
  666. chip->legacy.set_features = nand_get_set_features_notsupp;
  667. chip->legacy.get_features = nand_get_set_features_notsupp;
  668. chip->bbt_td = &bbt_main_descr;
  669. chip->bbt_md = &bbt_mirror_descr;
  670. /* set up nand options */
  671. chip->bbt_options = NAND_BBT_USE_FLASH;
  672. chip->controller = &elbc_fcm_ctrl->controller;
  673. nand_set_controller_data(chip, priv);
  674. chip->ecc.read_page = fsl_elbc_read_page;
  675. chip->ecc.write_page = fsl_elbc_write_page;
  676. chip->ecc.write_subpage = fsl_elbc_write_subpage;
  677. /* If CS Base Register selects full hardware ECC then use it */
  678. if ((in_be32(&lbc->bank[priv->bank].br) & BR_DECC) ==
  679. BR_DECC_CHK_GEN) {
  680. chip->ecc.mode = NAND_ECC_HW;
  681. mtd_set_ooblayout(mtd, &fsl_elbc_ooblayout_ops);
  682. chip->ecc.size = 512;
  683. chip->ecc.bytes = 3;
  684. chip->ecc.strength = 1;
  685. } else {
  686. /* otherwise fall back to default software ECC */
  687. chip->ecc.mode = NAND_ECC_SOFT;
  688. chip->ecc.algo = NAND_ECC_HAMMING;
  689. }
  690. return 0;
  691. }
  692. static int fsl_elbc_chip_remove(struct fsl_elbc_mtd *priv)
  693. {
  694. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  695. struct mtd_info *mtd = nand_to_mtd(&priv->chip);
  696. kfree(mtd->name);
  697. if (priv->vbase)
  698. iounmap(priv->vbase);
  699. elbc_fcm_ctrl->chips[priv->bank] = NULL;
  700. kfree(priv);
  701. return 0;
  702. }
  703. static DEFINE_MUTEX(fsl_elbc_nand_mutex);
  704. static int fsl_elbc_nand_probe(struct platform_device *pdev)
  705. {
  706. struct fsl_lbc_regs __iomem *lbc;
  707. struct fsl_elbc_mtd *priv;
  708. struct resource res;
  709. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl;
  710. static const char *part_probe_types[]
  711. = { "cmdlinepart", "RedBoot", "ofpart", NULL };
  712. int ret;
  713. int bank;
  714. struct device *dev;
  715. struct device_node *node = pdev->dev.of_node;
  716. struct mtd_info *mtd;
  717. if (!fsl_lbc_ctrl_dev || !fsl_lbc_ctrl_dev->regs)
  718. return -ENODEV;
  719. lbc = fsl_lbc_ctrl_dev->regs;
  720. dev = fsl_lbc_ctrl_dev->dev;
  721. /* get, allocate and map the memory resource */
  722. ret = of_address_to_resource(node, 0, &res);
  723. if (ret) {
  724. dev_err(dev, "failed to get resource\n");
  725. return ret;
  726. }
  727. /* find which chip select it is connected to */
  728. for (bank = 0; bank < MAX_BANKS; bank++)
  729. if ((in_be32(&lbc->bank[bank].br) & BR_V) &&
  730. (in_be32(&lbc->bank[bank].br) & BR_MSEL) == BR_MS_FCM &&
  731. (in_be32(&lbc->bank[bank].br) &
  732. in_be32(&lbc->bank[bank].or) & BR_BA)
  733. == fsl_lbc_addr(res.start))
  734. break;
  735. if (bank >= MAX_BANKS) {
  736. dev_err(dev, "address did not match any chip selects\n");
  737. return -ENODEV;
  738. }
  739. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  740. if (!priv)
  741. return -ENOMEM;
  742. mutex_lock(&fsl_elbc_nand_mutex);
  743. if (!fsl_lbc_ctrl_dev->nand) {
  744. elbc_fcm_ctrl = kzalloc(sizeof(*elbc_fcm_ctrl), GFP_KERNEL);
  745. if (!elbc_fcm_ctrl) {
  746. mutex_unlock(&fsl_elbc_nand_mutex);
  747. ret = -ENOMEM;
  748. goto err;
  749. }
  750. elbc_fcm_ctrl->counter++;
  751. nand_controller_init(&elbc_fcm_ctrl->controller);
  752. fsl_lbc_ctrl_dev->nand = elbc_fcm_ctrl;
  753. } else {
  754. elbc_fcm_ctrl = fsl_lbc_ctrl_dev->nand;
  755. }
  756. mutex_unlock(&fsl_elbc_nand_mutex);
  757. elbc_fcm_ctrl->chips[bank] = priv;
  758. priv->bank = bank;
  759. priv->ctrl = fsl_lbc_ctrl_dev;
  760. priv->dev = &pdev->dev;
  761. dev_set_drvdata(priv->dev, priv);
  762. priv->vbase = ioremap(res.start, resource_size(&res));
  763. if (!priv->vbase) {
  764. dev_err(dev, "failed to map chip region\n");
  765. ret = -ENOMEM;
  766. goto err;
  767. }
  768. mtd = nand_to_mtd(&priv->chip);
  769. mtd->name = kasprintf(GFP_KERNEL, "%llx.flash", (u64)res.start);
  770. if (!nand_to_mtd(&priv->chip)->name) {
  771. ret = -ENOMEM;
  772. goto err;
  773. }
  774. ret = fsl_elbc_chip_init(priv);
  775. if (ret)
  776. goto err;
  777. priv->chip.controller->ops = &fsl_elbc_controller_ops;
  778. ret = nand_scan(&priv->chip, 1);
  779. if (ret)
  780. goto err;
  781. /* First look for RedBoot table or partitions on the command
  782. * line, these take precedence over device tree information */
  783. ret = mtd_device_parse_register(mtd, part_probe_types, NULL, NULL, 0);
  784. if (ret)
  785. goto cleanup_nand;
  786. pr_info("eLBC NAND device at 0x%llx, bank %d\n",
  787. (unsigned long long)res.start, priv->bank);
  788. return 0;
  789. cleanup_nand:
  790. nand_cleanup(&priv->chip);
  791. err:
  792. fsl_elbc_chip_remove(priv);
  793. return ret;
  794. }
  795. static int fsl_elbc_nand_remove(struct platform_device *pdev)
  796. {
  797. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = fsl_lbc_ctrl_dev->nand;
  798. struct fsl_elbc_mtd *priv = dev_get_drvdata(&pdev->dev);
  799. nand_release(&priv->chip);
  800. fsl_elbc_chip_remove(priv);
  801. mutex_lock(&fsl_elbc_nand_mutex);
  802. elbc_fcm_ctrl->counter--;
  803. if (!elbc_fcm_ctrl->counter) {
  804. fsl_lbc_ctrl_dev->nand = NULL;
  805. kfree(elbc_fcm_ctrl);
  806. }
  807. mutex_unlock(&fsl_elbc_nand_mutex);
  808. return 0;
  809. }
  810. static const struct of_device_id fsl_elbc_nand_match[] = {
  811. { .compatible = "fsl,elbc-fcm-nand", },
  812. {}
  813. };
  814. MODULE_DEVICE_TABLE(of, fsl_elbc_nand_match);
  815. static struct platform_driver fsl_elbc_nand_driver = {
  816. .driver = {
  817. .name = "fsl,elbc-fcm-nand",
  818. .of_match_table = fsl_elbc_nand_match,
  819. },
  820. .probe = fsl_elbc_nand_probe,
  821. .remove = fsl_elbc_nand_remove,
  822. };
  823. module_platform_driver(fsl_elbc_nand_driver);
  824. MODULE_LICENSE("GPL");
  825. MODULE_AUTHOR("Freescale");
  826. MODULE_DESCRIPTION("Freescale Enhanced Local Bus Controller MTD NAND driver");