fsl_elbc_nand.c 29 KB

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