fsl_elbc_nand.c 29 KB

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