fsl_ifc_nand.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /* Integrated Flash Controller NAND Machine Driver
  2. *
  3. * Copyright (c) 2012 Freescale Semiconductor, Inc
  4. *
  5. * Authors: Dipen Dudhat <Dipen.Dudhat@freescale.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <malloc.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/nand.h>
  25. #include <linux/mtd/nand_ecc.h>
  26. #include <asm/io.h>
  27. #include <asm/errno.h>
  28. #include <asm/fsl_ifc.h>
  29. #define MAX_BANKS 4
  30. #define ERR_BYTE 0xFF /* Value returned for read bytes
  31. when read failed */
  32. #define IFC_TIMEOUT_MSECS 10 /* Maximum number of mSecs to wait for IFC
  33. NAND Machine */
  34. struct fsl_ifc_ctrl;
  35. /* mtd information per set */
  36. struct fsl_ifc_mtd {
  37. struct mtd_info mtd;
  38. struct nand_chip chip;
  39. struct fsl_ifc_ctrl *ctrl;
  40. struct device *dev;
  41. int bank; /* Chip select bank number */
  42. unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */
  43. u8 __iomem *vbase; /* Chip select base virtual address */
  44. };
  45. /* overview of the fsl ifc controller */
  46. struct fsl_ifc_ctrl {
  47. struct nand_hw_control controller;
  48. struct fsl_ifc_mtd *chips[MAX_BANKS];
  49. /* device info */
  50. struct fsl_ifc *regs;
  51. uint8_t __iomem *addr; /* Address of assigned IFC buffer */
  52. unsigned int cs_nand; /* On which chipsel NAND is connected */
  53. unsigned int page; /* Last page written to / read from */
  54. unsigned int read_bytes; /* Number of bytes read during command */
  55. unsigned int column; /* Saved column from SEQIN */
  56. unsigned int index; /* Pointer to next byte to 'read' */
  57. unsigned int status; /* status read from NEESR after last op */
  58. unsigned int oob; /* Non zero if operating on OOB data */
  59. unsigned int eccread; /* Non zero for a full-page ECC read */
  60. };
  61. static struct fsl_ifc_ctrl *ifc_ctrl;
  62. /* 512-byte page with 4-bit ECC, 8-bit */
  63. static struct nand_ecclayout oob_512_8bit_ecc4 = {
  64. .eccbytes = 8,
  65. .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  66. .oobfree = { {0, 5}, {6, 2} },
  67. };
  68. /* 512-byte page with 4-bit ECC, 16-bit */
  69. static struct nand_ecclayout oob_512_16bit_ecc4 = {
  70. .eccbytes = 8,
  71. .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  72. .oobfree = { {2, 6}, },
  73. };
  74. /* 2048-byte page size with 4-bit ECC */
  75. static struct nand_ecclayout oob_2048_ecc4 = {
  76. .eccbytes = 32,
  77. .eccpos = {
  78. 8, 9, 10, 11, 12, 13, 14, 15,
  79. 16, 17, 18, 19, 20, 21, 22, 23,
  80. 24, 25, 26, 27, 28, 29, 30, 31,
  81. 32, 33, 34, 35, 36, 37, 38, 39,
  82. },
  83. .oobfree = { {2, 6}, {40, 24} },
  84. };
  85. /* 4096-byte page size with 4-bit ECC */
  86. static struct nand_ecclayout oob_4096_ecc4 = {
  87. .eccbytes = 64,
  88. .eccpos = {
  89. 8, 9, 10, 11, 12, 13, 14, 15,
  90. 16, 17, 18, 19, 20, 21, 22, 23,
  91. 24, 25, 26, 27, 28, 29, 30, 31,
  92. 32, 33, 34, 35, 36, 37, 38, 39,
  93. 40, 41, 42, 43, 44, 45, 46, 47,
  94. 48, 49, 50, 51, 52, 53, 54, 55,
  95. 56, 57, 58, 59, 60, 61, 62, 63,
  96. 64, 65, 66, 67, 68, 69, 70, 71,
  97. },
  98. .oobfree = { {2, 6}, {72, 56} },
  99. };
  100. /* 4096-byte page size with 8-bit ECC -- requires 218-byte OOB */
  101. static struct nand_ecclayout oob_4096_ecc8 = {
  102. .eccbytes = 128,
  103. .eccpos = {
  104. 8, 9, 10, 11, 12, 13, 14, 15,
  105. 16, 17, 18, 19, 20, 21, 22, 23,
  106. 24, 25, 26, 27, 28, 29, 30, 31,
  107. 32, 33, 34, 35, 36, 37, 38, 39,
  108. 40, 41, 42, 43, 44, 45, 46, 47,
  109. 48, 49, 50, 51, 52, 53, 54, 55,
  110. 56, 57, 58, 59, 60, 61, 62, 63,
  111. 64, 65, 66, 67, 68, 69, 70, 71,
  112. 72, 73, 74, 75, 76, 77, 78, 79,
  113. 80, 81, 82, 83, 84, 85, 86, 87,
  114. 88, 89, 90, 91, 92, 93, 94, 95,
  115. 96, 97, 98, 99, 100, 101, 102, 103,
  116. 104, 105, 106, 107, 108, 109, 110, 111,
  117. 112, 113, 114, 115, 116, 117, 118, 119,
  118. 120, 121, 122, 123, 124, 125, 126, 127,
  119. 128, 129, 130, 131, 132, 133, 134, 135,
  120. },
  121. .oobfree = { {2, 6}, {136, 82} },
  122. };
  123. /*
  124. * Generic flash bbt descriptors
  125. */
  126. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  127. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  128. static struct nand_bbt_descr bbt_main_descr = {
  129. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  130. NAND_BBT_2BIT | NAND_BBT_VERSION,
  131. .offs = 2, /* 0 on 8-bit small page */
  132. .len = 4,
  133. .veroffs = 6,
  134. .maxblocks = 4,
  135. .pattern = bbt_pattern,
  136. };
  137. static struct nand_bbt_descr bbt_mirror_descr = {
  138. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  139. NAND_BBT_2BIT | NAND_BBT_VERSION,
  140. .offs = 2, /* 0 on 8-bit small page */
  141. .len = 4,
  142. .veroffs = 6,
  143. .maxblocks = 4,
  144. .pattern = mirror_pattern,
  145. };
  146. /*
  147. * Set up the IFC hardware block and page address fields, and the ifc nand
  148. * structure addr field to point to the correct IFC buffer in memory
  149. */
  150. static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
  151. {
  152. struct nand_chip *chip = mtd->priv;
  153. struct fsl_ifc_mtd *priv = chip->priv;
  154. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  155. struct fsl_ifc *ifc = ctrl->regs;
  156. int buf_num;
  157. ctrl->page = page_addr;
  158. /* Program ROW0/COL0 */
  159. out_be32(&ifc->ifc_nand.row0, page_addr);
  160. out_be32(&ifc->ifc_nand.col0, (oob ? IFC_NAND_COL_MS : 0) | column);
  161. buf_num = page_addr & priv->bufnum_mask;
  162. ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2);
  163. ctrl->index = column;
  164. /* for OOB data point to the second half of the buffer */
  165. if (oob)
  166. ctrl->index += mtd->writesize;
  167. }
  168. static int is_blank(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
  169. unsigned int bufnum)
  170. {
  171. struct nand_chip *chip = mtd->priv;
  172. struct fsl_ifc_mtd *priv = chip->priv;
  173. u8 __iomem *addr = priv->vbase + bufnum * (mtd->writesize * 2);
  174. u32 __iomem *main = (u32 *)addr;
  175. u8 __iomem *oob = addr + mtd->writesize;
  176. int i;
  177. for (i = 0; i < mtd->writesize / 4; i++) {
  178. if (__raw_readl(&main[i]) != 0xffffffff)
  179. return 0;
  180. }
  181. for (i = 0; i < chip->ecc.layout->eccbytes; i++) {
  182. int pos = chip->ecc.layout->eccpos[i];
  183. if (__raw_readb(&oob[pos]) != 0xff)
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. /* returns nonzero if entire page is blank */
  189. static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
  190. u32 *eccstat, unsigned int bufnum)
  191. {
  192. u32 reg = eccstat[bufnum / 4];
  193. int errors;
  194. errors = (reg >> ((3 - bufnum % 4) * 8)) & 15;
  195. return errors;
  196. }
  197. /*
  198. * execute IFC NAND command and wait for it to complete
  199. */
  200. static int fsl_ifc_run_command(struct mtd_info *mtd)
  201. {
  202. struct nand_chip *chip = mtd->priv;
  203. struct fsl_ifc_mtd *priv = chip->priv;
  204. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  205. struct fsl_ifc *ifc = ctrl->regs;
  206. long long end_tick;
  207. u32 eccstat[4];
  208. int i;
  209. /* set the chip select for NAND Transaction */
  210. out_be32(&ifc->ifc_nand.nand_csel, ifc_ctrl->cs_nand);
  211. /* start read/write seq */
  212. out_be32(&ifc->ifc_nand.nandseq_strt,
  213. IFC_NAND_SEQ_STRT_FIR_STRT);
  214. /* wait for NAND Machine complete flag or timeout */
  215. end_tick = usec2ticks(IFC_TIMEOUT_MSECS * 1000) + get_ticks();
  216. while (end_tick > get_ticks()) {
  217. ctrl->status = in_be32(&ifc->ifc_nand.nand_evter_stat);
  218. if (ctrl->status & IFC_NAND_EVTER_STAT_OPC)
  219. break;
  220. }
  221. out_be32(&ifc->ifc_nand.nand_evter_stat, ctrl->status);
  222. if (ctrl->status & IFC_NAND_EVTER_STAT_FTOER)
  223. printf("%s: Flash Time Out Error\n", __func__);
  224. if (ctrl->status & IFC_NAND_EVTER_STAT_WPER)
  225. printf("%s: Write Protect Error\n", __func__);
  226. if (ctrl->eccread) {
  227. int errors;
  228. int bufnum = ctrl->page & priv->bufnum_mask;
  229. int sector = bufnum * chip->ecc.steps;
  230. int sector_end = sector + chip->ecc.steps - 1;
  231. for (i = sector / 4; i <= sector_end / 4; i++)
  232. eccstat[i] = in_be32(&ifc->ifc_nand.nand_eccstat[i]);
  233. for (i = sector; i <= sector_end; i++) {
  234. errors = check_read_ecc(mtd, ctrl, eccstat, i);
  235. if (errors == 15) {
  236. /*
  237. * Uncorrectable error.
  238. * OK only if the whole page is blank.
  239. *
  240. * We disable ECCER reporting due to erratum
  241. * IFC-A002770 -- so report it now if we
  242. * see an uncorrectable error in ECCSTAT.
  243. */
  244. if (!is_blank(mtd, ctrl, bufnum))
  245. ctrl->status |=
  246. IFC_NAND_EVTER_STAT_ECCER;
  247. break;
  248. }
  249. mtd->ecc_stats.corrected += errors;
  250. }
  251. ctrl->eccread = 0;
  252. }
  253. /* returns 0 on success otherwise non-zero) */
  254. return ctrl->status == IFC_NAND_EVTER_STAT_OPC ? 0 : -EIO;
  255. }
  256. static void fsl_ifc_do_read(struct nand_chip *chip,
  257. int oob,
  258. struct mtd_info *mtd)
  259. {
  260. struct fsl_ifc_mtd *priv = chip->priv;
  261. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  262. struct fsl_ifc *ifc = ctrl->regs;
  263. /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
  264. if (mtd->writesize > 512) {
  265. out_be32(&ifc->ifc_nand.nand_fir0,
  266. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  267. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  268. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  269. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
  270. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT));
  271. out_be32(&ifc->ifc_nand.nand_fir1, 0x0);
  272. out_be32(&ifc->ifc_nand.nand_fcr0,
  273. (NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
  274. (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT));
  275. } else {
  276. out_be32(&ifc->ifc_nand.nand_fir0,
  277. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  278. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  279. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  280. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT));
  281. if (oob)
  282. out_be32(&ifc->ifc_nand.nand_fcr0,
  283. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT);
  284. else
  285. out_be32(&ifc->ifc_nand.nand_fcr0,
  286. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT);
  287. }
  288. }
  289. /* cmdfunc send commands to the IFC NAND Machine */
  290. static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  291. int column, int page_addr)
  292. {
  293. struct nand_chip *chip = mtd->priv;
  294. struct fsl_ifc_mtd *priv = chip->priv;
  295. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  296. struct fsl_ifc *ifc = ctrl->regs;
  297. /* clear the read buffer */
  298. ctrl->read_bytes = 0;
  299. if (command != NAND_CMD_PAGEPROG)
  300. ctrl->index = 0;
  301. switch (command) {
  302. /* READ0 read the entire buffer to use hardware ECC. */
  303. case NAND_CMD_READ0: {
  304. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  305. set_addr(mtd, 0, page_addr, 0);
  306. ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  307. ctrl->index += column;
  308. if (chip->ecc.mode == NAND_ECC_HW)
  309. ctrl->eccread = 1;
  310. fsl_ifc_do_read(chip, 0, mtd);
  311. fsl_ifc_run_command(mtd);
  312. return;
  313. }
  314. /* READOOB reads only the OOB because no ECC is performed. */
  315. case NAND_CMD_READOOB:
  316. out_be32(&ifc->ifc_nand.nand_fbcr, mtd->oobsize - column);
  317. set_addr(mtd, column, page_addr, 1);
  318. ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  319. fsl_ifc_do_read(chip, 1, mtd);
  320. fsl_ifc_run_command(mtd);
  321. return;
  322. /* READID must read all possible bytes while CEB is active */
  323. case NAND_CMD_READID:
  324. out_be32(&ifc->ifc_nand.nand_fir0,
  325. (IFC_FIR_OP_CMD0 << IFC_NAND_FIR0_OP0_SHIFT) |
  326. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  327. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT));
  328. out_be32(&ifc->ifc_nand.nand_fcr0,
  329. NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT);
  330. /* 4 bytes for manuf, device and exts */
  331. out_be32(&ifc->ifc_nand.nand_fbcr, 4);
  332. ctrl->read_bytes = 4;
  333. set_addr(mtd, 0, 0, 0);
  334. fsl_ifc_run_command(mtd);
  335. return;
  336. /* ERASE1 stores the block and page address */
  337. case NAND_CMD_ERASE1:
  338. set_addr(mtd, 0, page_addr, 0);
  339. return;
  340. /* ERASE2 uses the block and page address from ERASE1 */
  341. case NAND_CMD_ERASE2:
  342. out_be32(&ifc->ifc_nand.nand_fir0,
  343. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  344. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  345. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT));
  346. out_be32(&ifc->ifc_nand.nand_fcr0,
  347. (NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
  348. (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT));
  349. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  350. ctrl->read_bytes = 0;
  351. fsl_ifc_run_command(mtd);
  352. return;
  353. /* SEQIN sets up the addr buffer and all registers except the length */
  354. case NAND_CMD_SEQIN: {
  355. u32 nand_fcr0;
  356. ctrl->column = column;
  357. ctrl->oob = 0;
  358. if (mtd->writesize > 512) {
  359. nand_fcr0 =
  360. (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
  361. (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD1_SHIFT);
  362. out_be32(&ifc->ifc_nand.nand_fir0,
  363. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  364. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  365. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  366. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) |
  367. (IFC_FIR_OP_CW1 << IFC_NAND_FIR0_OP4_SHIFT));
  368. out_be32(&ifc->ifc_nand.nand_fir1, 0);
  369. } else {
  370. nand_fcr0 = ((NAND_CMD_PAGEPROG <<
  371. IFC_NAND_FCR0_CMD1_SHIFT) |
  372. (NAND_CMD_SEQIN <<
  373. IFC_NAND_FCR0_CMD2_SHIFT));
  374. out_be32(&ifc->ifc_nand.nand_fir0,
  375. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  376. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
  377. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  378. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
  379. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT));
  380. out_be32(&ifc->ifc_nand.nand_fir1,
  381. (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT));
  382. if (column >= mtd->writesize)
  383. nand_fcr0 |=
  384. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
  385. else
  386. nand_fcr0 |=
  387. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
  388. }
  389. if (column >= mtd->writesize) {
  390. /* OOB area --> READOOB */
  391. column -= mtd->writesize;
  392. ctrl->oob = 1;
  393. }
  394. out_be32(&ifc->ifc_nand.nand_fcr0, nand_fcr0);
  395. set_addr(mtd, column, page_addr, ctrl->oob);
  396. return;
  397. }
  398. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  399. case NAND_CMD_PAGEPROG:
  400. if (ctrl->oob)
  401. out_be32(&ifc->ifc_nand.nand_fbcr,
  402. ctrl->index - ctrl->column);
  403. else
  404. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  405. fsl_ifc_run_command(mtd);
  406. return;
  407. case NAND_CMD_STATUS:
  408. out_be32(&ifc->ifc_nand.nand_fir0,
  409. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  410. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT));
  411. out_be32(&ifc->ifc_nand.nand_fcr0,
  412. NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT);
  413. out_be32(&ifc->ifc_nand.nand_fbcr, 1);
  414. set_addr(mtd, 0, 0, 0);
  415. ctrl->read_bytes = 1;
  416. fsl_ifc_run_command(mtd);
  417. /* Chip sometimes reporting write protect even when it's not */
  418. out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
  419. return;
  420. case NAND_CMD_RESET:
  421. out_be32(&ifc->ifc_nand.nand_fir0,
  422. IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT);
  423. out_be32(&ifc->ifc_nand.nand_fcr0,
  424. NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT);
  425. fsl_ifc_run_command(mtd);
  426. return;
  427. default:
  428. printf("%s: error, unsupported command 0x%x.\n",
  429. __func__, command);
  430. }
  431. }
  432. /*
  433. * Write buf to the IFC NAND Controller Data Buffer
  434. */
  435. static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  436. {
  437. struct nand_chip *chip = mtd->priv;
  438. struct fsl_ifc_mtd *priv = chip->priv;
  439. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  440. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  441. if (len <= 0) {
  442. printf("%s of %d bytes", __func__, len);
  443. ctrl->status = 0;
  444. return;
  445. }
  446. if ((unsigned int)len > bufsize - ctrl->index) {
  447. printf("%s beyond end of buffer "
  448. "(%d requested, %u available)\n",
  449. __func__, len, bufsize - ctrl->index);
  450. len = bufsize - ctrl->index;
  451. }
  452. memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
  453. ctrl->index += len;
  454. }
  455. /*
  456. * read a byte from either the IFC hardware buffer if it has any data left
  457. * otherwise issue a command to read a single byte.
  458. */
  459. static u8 fsl_ifc_read_byte(struct mtd_info *mtd)
  460. {
  461. struct nand_chip *chip = mtd->priv;
  462. struct fsl_ifc_mtd *priv = chip->priv;
  463. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  464. /* If there are still bytes in the IFC buffer, then use the
  465. * next byte. */
  466. if (ctrl->index < ctrl->read_bytes)
  467. return in_8(&ctrl->addr[ctrl->index++]);
  468. printf("%s beyond end of buffer\n", __func__);
  469. return ERR_BYTE;
  470. }
  471. /*
  472. * Read two bytes from the IFC hardware buffer
  473. * read function for 16-bit buswith
  474. */
  475. static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
  476. {
  477. struct nand_chip *chip = mtd->priv;
  478. struct fsl_ifc_mtd *priv = chip->priv;
  479. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  480. uint16_t data;
  481. /*
  482. * If there are still bytes in the IFC buffer, then use the
  483. * next byte.
  484. */
  485. if (ctrl->index < ctrl->read_bytes) {
  486. data = in_be16((uint16_t *)&ctrl->
  487. addr[ctrl->index]);
  488. ctrl->index += 2;
  489. return (uint8_t)data;
  490. }
  491. printf("%s beyond end of buffer\n", __func__);
  492. return ERR_BYTE;
  493. }
  494. /*
  495. * Read from the IFC Controller Data Buffer
  496. */
  497. static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  498. {
  499. struct nand_chip *chip = mtd->priv;
  500. struct fsl_ifc_mtd *priv = chip->priv;
  501. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  502. int avail;
  503. if (len < 0)
  504. return;
  505. avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
  506. memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
  507. ctrl->index += avail;
  508. if (len > avail)
  509. printf("%s beyond end of buffer "
  510. "(%d requested, %d available)\n",
  511. __func__, len, avail);
  512. }
  513. /*
  514. * Verify buffer against the IFC Controller Data Buffer
  515. */
  516. static int fsl_ifc_verify_buf(struct mtd_info *mtd,
  517. const u_char *buf, int len)
  518. {
  519. struct nand_chip *chip = mtd->priv;
  520. struct fsl_ifc_mtd *priv = chip->priv;
  521. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  522. int i;
  523. if (len < 0) {
  524. printf("%s of %d bytes", __func__, len);
  525. return -EINVAL;
  526. }
  527. if ((unsigned int)len > ctrl->read_bytes - ctrl->index) {
  528. printf("%s beyond end of buffer "
  529. "(%d requested, %u available)\n",
  530. __func__, len, ctrl->read_bytes - ctrl->index);
  531. ctrl->index = ctrl->read_bytes;
  532. return -EINVAL;
  533. }
  534. for (i = 0; i < len; i++)
  535. if (in_8(&ctrl->addr[ctrl->index + i]) != buf[i])
  536. break;
  537. ctrl->index += len;
  538. return i == len && ctrl->status == IFC_NAND_EVTER_STAT_OPC ? 0 : -EIO;
  539. }
  540. /* This function is called after Program and Erase Operations to
  541. * check for success or failure.
  542. */
  543. static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  544. {
  545. struct fsl_ifc_mtd *priv = chip->priv;
  546. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  547. struct fsl_ifc *ifc = ctrl->regs;
  548. u32 nand_fsr;
  549. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  550. return NAND_STATUS_FAIL;
  551. /* Use READ_STATUS command, but wait for the device to be ready */
  552. out_be32(&ifc->ifc_nand.nand_fir0,
  553. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  554. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT));
  555. out_be32(&ifc->ifc_nand.nand_fcr0, NAND_CMD_STATUS <<
  556. IFC_NAND_FCR0_CMD0_SHIFT);
  557. out_be32(&ifc->ifc_nand.nand_fbcr, 1);
  558. set_addr(mtd, 0, 0, 0);
  559. ctrl->read_bytes = 1;
  560. fsl_ifc_run_command(mtd);
  561. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  562. return NAND_STATUS_FAIL;
  563. nand_fsr = in_be32(&ifc->ifc_nand.nand_fsr);
  564. /* Chip sometimes reporting write protect even when it's not */
  565. nand_fsr = nand_fsr | NAND_STATUS_WP;
  566. return nand_fsr;
  567. }
  568. static int fsl_ifc_read_page(struct mtd_info *mtd,
  569. struct nand_chip *chip,
  570. uint8_t *buf, int page)
  571. {
  572. struct fsl_ifc_mtd *priv = chip->priv;
  573. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  574. fsl_ifc_read_buf(mtd, buf, mtd->writesize);
  575. fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  576. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  577. mtd->ecc_stats.failed++;
  578. return 0;
  579. }
  580. /* ECC will be calculated automatically, and errors will be detected in
  581. * waitfunc.
  582. */
  583. static void fsl_ifc_write_page(struct mtd_info *mtd,
  584. struct nand_chip *chip,
  585. const uint8_t *buf)
  586. {
  587. fsl_ifc_write_buf(mtd, buf, mtd->writesize);
  588. fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  589. }
  590. static void fsl_ifc_ctrl_init(void)
  591. {
  592. ifc_ctrl = kzalloc(sizeof(*ifc_ctrl), GFP_KERNEL);
  593. if (!ifc_ctrl)
  594. return;
  595. ifc_ctrl->regs = IFC_BASE_ADDR;
  596. /* clear event registers */
  597. out_be32(&ifc_ctrl->regs->ifc_nand.nand_evter_stat, ~0U);
  598. out_be32(&ifc_ctrl->regs->ifc_nand.pgrdcmpl_evt_stat, ~0U);
  599. /* Enable error and event for any detected errors */
  600. out_be32(&ifc_ctrl->regs->ifc_nand.nand_evter_en,
  601. IFC_NAND_EVTER_EN_OPC_EN |
  602. IFC_NAND_EVTER_EN_PGRDCMPL_EN |
  603. IFC_NAND_EVTER_EN_FTOER_EN |
  604. IFC_NAND_EVTER_EN_WPER_EN);
  605. out_be32(&ifc_ctrl->regs->ifc_nand.ncfgr, 0x0);
  606. }
  607. static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
  608. {
  609. }
  610. int board_nand_init(struct nand_chip *nand)
  611. {
  612. struct fsl_ifc_mtd *priv;
  613. struct nand_ecclayout *layout;
  614. uint32_t cspr = 0, csor = 0;
  615. if (!ifc_ctrl) {
  616. fsl_ifc_ctrl_init();
  617. if (!ifc_ctrl)
  618. return -1;
  619. }
  620. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  621. if (!priv)
  622. return -ENOMEM;
  623. priv->ctrl = ifc_ctrl;
  624. priv->vbase = nand->IO_ADDR_R;
  625. /* Find which chip select it is connected to.
  626. */
  627. for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
  628. phys_addr_t base_addr = virt_to_phys(nand->IO_ADDR_R);
  629. cspr = in_be32(&ifc_ctrl->regs->cspr_cs[priv->bank].cspr);
  630. csor = in_be32(&ifc_ctrl->regs->csor_cs[priv->bank].csor);
  631. if ((cspr & CSPR_V) && (cspr & CSPR_MSEL) == CSPR_MSEL_NAND &&
  632. (cspr & CSPR_BA) == CSPR_PHYS_ADDR(base_addr)) {
  633. ifc_ctrl->cs_nand = priv->bank << IFC_NAND_CSEL_SHIFT;
  634. break;
  635. }
  636. }
  637. if (priv->bank >= MAX_BANKS) {
  638. printf("%s: address did not match any "
  639. "chip selects\n", __func__);
  640. return -ENODEV;
  641. }
  642. ifc_ctrl->chips[priv->bank] = priv;
  643. /* fill in nand_chip structure */
  644. /* set up function call table */
  645. nand->write_buf = fsl_ifc_write_buf;
  646. nand->read_buf = fsl_ifc_read_buf;
  647. nand->verify_buf = fsl_ifc_verify_buf;
  648. nand->select_chip = fsl_ifc_select_chip;
  649. nand->cmdfunc = fsl_ifc_cmdfunc;
  650. nand->waitfunc = fsl_ifc_wait;
  651. /* set up nand options */
  652. nand->bbt_td = &bbt_main_descr;
  653. nand->bbt_md = &bbt_mirror_descr;
  654. /* set up nand options */
  655. nand->options = NAND_NO_READRDY | NAND_NO_AUTOINCR |
  656. NAND_USE_FLASH_BBT;
  657. if (cspr & CSPR_PORT_SIZE_16) {
  658. nand->read_byte = fsl_ifc_read_byte16;
  659. nand->options |= NAND_BUSWIDTH_16;
  660. } else {
  661. nand->read_byte = fsl_ifc_read_byte;
  662. }
  663. nand->controller = &ifc_ctrl->controller;
  664. nand->priv = priv;
  665. nand->ecc.read_page = fsl_ifc_read_page;
  666. nand->ecc.write_page = fsl_ifc_write_page;
  667. /* Hardware generates ECC per 512 Bytes */
  668. nand->ecc.size = 512;
  669. nand->ecc.bytes = 8;
  670. switch (csor & CSOR_NAND_PGS_MASK) {
  671. case CSOR_NAND_PGS_512:
  672. if (nand->options & NAND_BUSWIDTH_16) {
  673. layout = &oob_512_16bit_ecc4;
  674. } else {
  675. layout = &oob_512_8bit_ecc4;
  676. /* Avoid conflict with bad block marker */
  677. bbt_main_descr.offs = 0;
  678. bbt_mirror_descr.offs = 0;
  679. }
  680. priv->bufnum_mask = 15;
  681. break;
  682. case CSOR_NAND_PGS_2K:
  683. layout = &oob_2048_ecc4;
  684. priv->bufnum_mask = 3;
  685. break;
  686. case CSOR_NAND_PGS_4K:
  687. if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
  688. CSOR_NAND_ECC_MODE_4) {
  689. layout = &oob_4096_ecc4;
  690. } else {
  691. layout = &oob_4096_ecc8;
  692. nand->ecc.bytes = 16;
  693. }
  694. priv->bufnum_mask = 1;
  695. break;
  696. default:
  697. printf("ifc nand: bad csor %#x: bad page size\n", csor);
  698. return -ENODEV;
  699. }
  700. /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
  701. if (csor & CSOR_NAND_ECC_DEC_EN) {
  702. nand->ecc.mode = NAND_ECC_HW;
  703. nand->ecc.layout = layout;
  704. } else {
  705. nand->ecc.mode = NAND_ECC_SOFT;
  706. }
  707. return 0;
  708. }