fsl_elbc_nand.c 28 KB

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