fsl_elbc_nand.c 28 KB

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