fsl_ifc_nand.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /* Integrated Flash Controller NAND Machine Driver
  2. *
  3. * Copyright (c) 2011 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 = (reg >> ((3 - bufnum % 4) * 8)) & 15;
  194. if (errors == 15) { /* uncorrectable */
  195. /* Blank pages fail hw ECC checks */
  196. if (is_blank(mtd, ctrl, bufnum))
  197. return 1;
  198. /*
  199. * We disable ECCER reporting in hardware due to
  200. * erratum IFC-A002770 -- so report it now if we
  201. * see an uncorrectable error in ECCSTAT.
  202. */
  203. ctrl->status |= IFC_NAND_EVTER_STAT_ECCER;
  204. } else if (errors > 0) {
  205. mtd->ecc_stats.corrected += errors;
  206. }
  207. return 0;
  208. }
  209. /*
  210. * execute IFC NAND command and wait for it to complete
  211. */
  212. static int fsl_ifc_run_command(struct mtd_info *mtd)
  213. {
  214. struct nand_chip *chip = mtd->priv;
  215. struct fsl_ifc_mtd *priv = chip->priv;
  216. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  217. struct fsl_ifc *ifc = ctrl->regs;
  218. long long end_tick;
  219. u32 eccstat[4];
  220. int i;
  221. /* set the chip select for NAND Transaction */
  222. out_be32(&ifc->ifc_nand.nand_csel, ifc_ctrl->cs_nand);
  223. /* start read/write seq */
  224. out_be32(&ifc->ifc_nand.nandseq_strt,
  225. IFC_NAND_SEQ_STRT_FIR_STRT);
  226. /* wait for NAND Machine complete flag or timeout */
  227. end_tick = usec2ticks(IFC_TIMEOUT_MSECS * 1000) + get_ticks();
  228. while (end_tick > get_ticks()) {
  229. ctrl->status = in_be32(&ifc->ifc_nand.nand_evter_stat);
  230. if (ctrl->status & IFC_NAND_EVTER_STAT_OPC)
  231. break;
  232. }
  233. out_be32(&ifc->ifc_nand.nand_evter_stat, ctrl->status);
  234. if (ctrl->status & IFC_NAND_EVTER_STAT_FTOER)
  235. printf("%s: Flash Time Out Error\n", __func__);
  236. if (ctrl->status & IFC_NAND_EVTER_STAT_WPER)
  237. printf("%s: Write Protect Error\n", __func__);
  238. if (ctrl->eccread) {
  239. int bufperpage = mtd->writesize / 512;
  240. int bufnum = (ctrl->page & priv->bufnum_mask) * bufperpage;
  241. int bufnum_end = bufnum + bufperpage - 1;
  242. for (i = bufnum / 4; i <= bufnum_end / 4; i++)
  243. eccstat[i] = in_be32(&ifc->ifc_nand.nand_eccstat[i]);
  244. for (i = bufnum; i <= bufnum_end; i++) {
  245. if (check_read_ecc(mtd, ctrl, eccstat, i))
  246. break;
  247. }
  248. ctrl->eccread = 0;
  249. }
  250. /* returns 0 on success otherwise non-zero) */
  251. return ctrl->status == IFC_NAND_EVTER_STAT_OPC ? 0 : -EIO;
  252. }
  253. static void fsl_ifc_do_read(struct nand_chip *chip,
  254. int oob,
  255. struct mtd_info *mtd)
  256. {
  257. struct fsl_ifc_mtd *priv = chip->priv;
  258. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  259. struct fsl_ifc *ifc = ctrl->regs;
  260. /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
  261. if (mtd->writesize > 512) {
  262. out_be32(&ifc->ifc_nand.nand_fir0,
  263. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  264. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  265. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  266. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
  267. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT));
  268. out_be32(&ifc->ifc_nand.nand_fir1, 0x0);
  269. out_be32(&ifc->ifc_nand.nand_fcr0,
  270. (NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
  271. (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT));
  272. } else {
  273. out_be32(&ifc->ifc_nand.nand_fir0,
  274. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  275. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  276. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  277. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT));
  278. if (oob)
  279. out_be32(&ifc->ifc_nand.nand_fcr0,
  280. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT);
  281. else
  282. out_be32(&ifc->ifc_nand.nand_fcr0,
  283. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT);
  284. }
  285. }
  286. /* cmdfunc send commands to the IFC NAND Machine */
  287. static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  288. int column, int page_addr)
  289. {
  290. struct nand_chip *chip = mtd->priv;
  291. struct fsl_ifc_mtd *priv = chip->priv;
  292. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  293. struct fsl_ifc *ifc = ctrl->regs;
  294. /* clear the read buffer */
  295. ctrl->read_bytes = 0;
  296. if (command != NAND_CMD_PAGEPROG)
  297. ctrl->index = 0;
  298. switch (command) {
  299. /* READ0 read the entire buffer to use hardware ECC. */
  300. case NAND_CMD_READ0: {
  301. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  302. set_addr(mtd, 0, page_addr, 0);
  303. ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  304. ctrl->index += column;
  305. if (chip->ecc.mode == NAND_ECC_HW)
  306. ctrl->eccread = 1;
  307. fsl_ifc_do_read(chip, 0, mtd);
  308. fsl_ifc_run_command(mtd);
  309. return;
  310. }
  311. /* READOOB reads only the OOB because no ECC is performed. */
  312. case NAND_CMD_READOOB:
  313. out_be32(&ifc->ifc_nand.nand_fbcr, mtd->oobsize - column);
  314. set_addr(mtd, column, page_addr, 1);
  315. ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  316. fsl_ifc_do_read(chip, 1, mtd);
  317. fsl_ifc_run_command(mtd);
  318. return;
  319. /* READID must read all possible bytes while CEB is active */
  320. case NAND_CMD_READID:
  321. out_be32(&ifc->ifc_nand.nand_fir0,
  322. (IFC_FIR_OP_CMD0 << IFC_NAND_FIR0_OP0_SHIFT) |
  323. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  324. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT));
  325. out_be32(&ifc->ifc_nand.nand_fcr0,
  326. NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT);
  327. /* 4 bytes for manuf, device and exts */
  328. out_be32(&ifc->ifc_nand.nand_fbcr, 4);
  329. ctrl->read_bytes = 4;
  330. set_addr(mtd, 0, 0, 0);
  331. fsl_ifc_run_command(mtd);
  332. return;
  333. /* ERASE1 stores the block and page address */
  334. case NAND_CMD_ERASE1:
  335. set_addr(mtd, 0, page_addr, 0);
  336. return;
  337. /* ERASE2 uses the block and page address from ERASE1 */
  338. case NAND_CMD_ERASE2:
  339. out_be32(&ifc->ifc_nand.nand_fir0,
  340. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  341. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  342. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT));
  343. out_be32(&ifc->ifc_nand.nand_fcr0,
  344. (NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
  345. (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT));
  346. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  347. ctrl->read_bytes = 0;
  348. fsl_ifc_run_command(mtd);
  349. return;
  350. /* SEQIN sets up the addr buffer and all registers except the length */
  351. case NAND_CMD_SEQIN: {
  352. u32 nand_fcr0;
  353. ctrl->column = column;
  354. ctrl->oob = 0;
  355. if (mtd->writesize > 512) {
  356. nand_fcr0 =
  357. (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
  358. (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD1_SHIFT);
  359. out_be32(&ifc->ifc_nand.nand_fir0,
  360. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  361. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  362. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  363. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) |
  364. (IFC_FIR_OP_CW1 << IFC_NAND_FIR0_OP4_SHIFT));
  365. out_be32(&ifc->ifc_nand.nand_fir1, 0);
  366. } else {
  367. nand_fcr0 = ((NAND_CMD_PAGEPROG <<
  368. IFC_NAND_FCR0_CMD1_SHIFT) |
  369. (NAND_CMD_SEQIN <<
  370. IFC_NAND_FCR0_CMD2_SHIFT));
  371. out_be32(&ifc->ifc_nand.nand_fir0,
  372. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  373. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
  374. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  375. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
  376. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT));
  377. out_be32(&ifc->ifc_nand.nand_fir1,
  378. (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT));
  379. if (column >= mtd->writesize)
  380. nand_fcr0 |=
  381. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
  382. else
  383. nand_fcr0 |=
  384. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
  385. }
  386. if (column >= mtd->writesize) {
  387. /* OOB area --> READOOB */
  388. column -= mtd->writesize;
  389. ctrl->oob = 1;
  390. }
  391. out_be32(&ifc->ifc_nand.nand_fcr0, nand_fcr0);
  392. set_addr(mtd, column, page_addr, ctrl->oob);
  393. return;
  394. }
  395. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  396. case NAND_CMD_PAGEPROG:
  397. if (ctrl->oob)
  398. out_be32(&ifc->ifc_nand.nand_fbcr,
  399. ctrl->index - ctrl->column);
  400. else
  401. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  402. fsl_ifc_run_command(mtd);
  403. return;
  404. case NAND_CMD_STATUS:
  405. out_be32(&ifc->ifc_nand.nand_fir0,
  406. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  407. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT));
  408. out_be32(&ifc->ifc_nand.nand_fcr0,
  409. NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT);
  410. out_be32(&ifc->ifc_nand.nand_fbcr, 1);
  411. set_addr(mtd, 0, 0, 0);
  412. ctrl->read_bytes = 1;
  413. fsl_ifc_run_command(mtd);
  414. /* Chip sometimes reporting write protect even when it's not */
  415. out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
  416. return;
  417. case NAND_CMD_RESET:
  418. out_be32(&ifc->ifc_nand.nand_fir0,
  419. IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT);
  420. out_be32(&ifc->ifc_nand.nand_fcr0,
  421. NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT);
  422. fsl_ifc_run_command(mtd);
  423. return;
  424. default:
  425. printf("%s: error, unsupported command 0x%x.\n",
  426. __func__, command);
  427. }
  428. }
  429. /*
  430. * Write buf to the IFC NAND Controller Data Buffer
  431. */
  432. static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  433. {
  434. struct nand_chip *chip = mtd->priv;
  435. struct fsl_ifc_mtd *priv = chip->priv;
  436. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  437. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  438. if (len <= 0) {
  439. printf("%s of %d bytes", __func__, len);
  440. ctrl->status = 0;
  441. return;
  442. }
  443. if ((unsigned int)len > bufsize - ctrl->index) {
  444. printf("%s beyond end of buffer "
  445. "(%d requested, %u available)\n",
  446. __func__, len, bufsize - ctrl->index);
  447. len = bufsize - ctrl->index;
  448. }
  449. memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
  450. ctrl->index += len;
  451. }
  452. /*
  453. * read a byte from either the IFC hardware buffer if it has any data left
  454. * otherwise issue a command to read a single byte.
  455. */
  456. static u8 fsl_ifc_read_byte(struct mtd_info *mtd)
  457. {
  458. struct nand_chip *chip = mtd->priv;
  459. struct fsl_ifc_mtd *priv = chip->priv;
  460. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  461. /* If there are still bytes in the IFC buffer, then use the
  462. * next byte. */
  463. if (ctrl->index < ctrl->read_bytes)
  464. return in_8(&ctrl->addr[ctrl->index++]);
  465. printf("%s beyond end of buffer\n", __func__);
  466. return ERR_BYTE;
  467. }
  468. /*
  469. * Read two bytes from the IFC hardware buffer
  470. * read function for 16-bit buswith
  471. */
  472. static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
  473. {
  474. struct nand_chip *chip = mtd->priv;
  475. struct fsl_ifc_mtd *priv = chip->priv;
  476. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  477. uint16_t data;
  478. /*
  479. * If there are still bytes in the IFC buffer, then use the
  480. * next byte.
  481. */
  482. if (ctrl->index < ctrl->read_bytes) {
  483. data = in_be16((uint16_t *)&ctrl->
  484. addr[ctrl->index]);
  485. ctrl->index += 2;
  486. return (uint8_t)data;
  487. }
  488. printf("%s beyond end of buffer\n", __func__);
  489. return ERR_BYTE;
  490. }
  491. /*
  492. * Read from the IFC Controller Data Buffer
  493. */
  494. static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  495. {
  496. struct nand_chip *chip = mtd->priv;
  497. struct fsl_ifc_mtd *priv = chip->priv;
  498. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  499. int avail;
  500. if (len < 0)
  501. return;
  502. avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
  503. memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
  504. ctrl->index += avail;
  505. if (len > avail)
  506. printf("%s beyond end of buffer "
  507. "(%d requested, %d available)\n",
  508. __func__, len, avail);
  509. }
  510. /*
  511. * Verify buffer against the IFC Controller Data Buffer
  512. */
  513. static int fsl_ifc_verify_buf(struct mtd_info *mtd,
  514. const u_char *buf, int len)
  515. {
  516. struct nand_chip *chip = mtd->priv;
  517. struct fsl_ifc_mtd *priv = chip->priv;
  518. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  519. int i;
  520. if (len < 0) {
  521. printf("%s of %d bytes", __func__, len);
  522. return -EINVAL;
  523. }
  524. if ((unsigned int)len > ctrl->read_bytes - ctrl->index) {
  525. printf("%s beyond end of buffer "
  526. "(%d requested, %u available)\n",
  527. __func__, len, ctrl->read_bytes - ctrl->index);
  528. ctrl->index = ctrl->read_bytes;
  529. return -EINVAL;
  530. }
  531. for (i = 0; i < len; i++)
  532. if (in_8(&ctrl->addr[ctrl->index + i]) != buf[i])
  533. break;
  534. ctrl->index += len;
  535. return i == len && ctrl->status == IFC_NAND_EVTER_STAT_OPC ? 0 : -EIO;
  536. }
  537. /* This function is called after Program and Erase Operations to
  538. * check for success or failure.
  539. */
  540. static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  541. {
  542. struct fsl_ifc_mtd *priv = chip->priv;
  543. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  544. struct fsl_ifc *ifc = ctrl->regs;
  545. u32 nand_fsr;
  546. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  547. return NAND_STATUS_FAIL;
  548. /* Use READ_STATUS command, but wait for the device to be ready */
  549. out_be32(&ifc->ifc_nand.nand_fir0,
  550. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  551. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT));
  552. out_be32(&ifc->ifc_nand.nand_fcr0, NAND_CMD_STATUS <<
  553. IFC_NAND_FCR0_CMD0_SHIFT);
  554. out_be32(&ifc->ifc_nand.nand_fbcr, 1);
  555. set_addr(mtd, 0, 0, 0);
  556. ctrl->read_bytes = 1;
  557. fsl_ifc_run_command(mtd);
  558. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  559. return NAND_STATUS_FAIL;
  560. nand_fsr = in_be32(&ifc->ifc_nand.nand_fsr);
  561. /* Chip sometimes reporting write protect even when it's not */
  562. nand_fsr = nand_fsr | NAND_STATUS_WP;
  563. return nand_fsr;
  564. }
  565. static int fsl_ifc_read_page(struct mtd_info *mtd,
  566. struct nand_chip *chip,
  567. uint8_t *buf, int page)
  568. {
  569. struct fsl_ifc_mtd *priv = chip->priv;
  570. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  571. fsl_ifc_read_buf(mtd, buf, mtd->writesize);
  572. fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  573. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  574. mtd->ecc_stats.failed++;
  575. return 0;
  576. }
  577. /* ECC will be calculated automatically, and errors will be detected in
  578. * waitfunc.
  579. */
  580. static void fsl_ifc_write_page(struct mtd_info *mtd,
  581. struct nand_chip *chip,
  582. const uint8_t *buf)
  583. {
  584. fsl_ifc_write_buf(mtd, buf, mtd->writesize);
  585. fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  586. }
  587. static void fsl_ifc_ctrl_init(void)
  588. {
  589. ifc_ctrl = kzalloc(sizeof(*ifc_ctrl), GFP_KERNEL);
  590. if (!ifc_ctrl)
  591. return;
  592. ifc_ctrl->regs = IFC_BASE_ADDR;
  593. /* clear event registers */
  594. out_be32(&ifc_ctrl->regs->ifc_nand.nand_evter_stat, ~0U);
  595. out_be32(&ifc_ctrl->regs->ifc_nand.pgrdcmpl_evt_stat, ~0U);
  596. /* Enable error and event for any detected errors */
  597. out_be32(&ifc_ctrl->regs->ifc_nand.nand_evter_en,
  598. IFC_NAND_EVTER_EN_OPC_EN |
  599. IFC_NAND_EVTER_EN_PGRDCMPL_EN |
  600. IFC_NAND_EVTER_EN_FTOER_EN |
  601. IFC_NAND_EVTER_EN_WPER_EN);
  602. out_be32(&ifc_ctrl->regs->ifc_nand.ncfgr, 0x0);
  603. }
  604. static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
  605. {
  606. }
  607. int board_nand_init(struct nand_chip *nand)
  608. {
  609. struct fsl_ifc_mtd *priv;
  610. struct nand_ecclayout *layout;
  611. uint32_t cspr = 0, csor = 0;
  612. if (!ifc_ctrl) {
  613. fsl_ifc_ctrl_init();
  614. if (!ifc_ctrl)
  615. return -1;
  616. }
  617. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  618. if (!priv)
  619. return -ENOMEM;
  620. priv->ctrl = ifc_ctrl;
  621. priv->vbase = nand->IO_ADDR_R;
  622. /* Find which chip select it is connected to.
  623. */
  624. for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
  625. phys_addr_t base_addr = virt_to_phys(nand->IO_ADDR_R);
  626. cspr = in_be32(&ifc_ctrl->regs->cspr_cs[priv->bank].cspr);
  627. csor = in_be32(&ifc_ctrl->regs->csor_cs[priv->bank].csor);
  628. if ((cspr & CSPR_V) && (cspr & CSPR_MSEL) == CSPR_MSEL_NAND &&
  629. (cspr & CSPR_BA) == CSPR_PHYS_ADDR(base_addr)) {
  630. ifc_ctrl->cs_nand = priv->bank << IFC_NAND_CSEL_SHIFT;
  631. break;
  632. }
  633. }
  634. if (priv->bank >= MAX_BANKS) {
  635. printf("%s: address did not match any "
  636. "chip selects\n", __func__);
  637. return -ENODEV;
  638. }
  639. ifc_ctrl->chips[priv->bank] = priv;
  640. /* fill in nand_chip structure */
  641. /* set up function call table */
  642. nand->write_buf = fsl_ifc_write_buf;
  643. nand->read_buf = fsl_ifc_read_buf;
  644. nand->verify_buf = fsl_ifc_verify_buf;
  645. nand->select_chip = fsl_ifc_select_chip;
  646. nand->cmdfunc = fsl_ifc_cmdfunc;
  647. nand->waitfunc = fsl_ifc_wait;
  648. /* set up nand options */
  649. nand->bbt_td = &bbt_main_descr;
  650. nand->bbt_md = &bbt_mirror_descr;
  651. /* set up nand options */
  652. nand->options = NAND_NO_READRDY | NAND_NO_AUTOINCR |
  653. NAND_USE_FLASH_BBT;
  654. if (cspr & CSPR_PORT_SIZE_16) {
  655. nand->read_byte = fsl_ifc_read_byte16;
  656. nand->options |= NAND_BUSWIDTH_16;
  657. } else {
  658. nand->read_byte = fsl_ifc_read_byte;
  659. }
  660. nand->controller = &ifc_ctrl->controller;
  661. nand->priv = priv;
  662. nand->ecc.read_page = fsl_ifc_read_page;
  663. nand->ecc.write_page = fsl_ifc_write_page;
  664. /* Hardware generates ECC per 512 Bytes */
  665. nand->ecc.size = 512;
  666. nand->ecc.bytes = 8;
  667. switch (csor & CSOR_NAND_PGS_MASK) {
  668. case CSOR_NAND_PGS_512:
  669. if (nand->options & NAND_BUSWIDTH_16) {
  670. layout = &oob_512_16bit_ecc4;
  671. } else {
  672. layout = &oob_512_8bit_ecc4;
  673. /* Avoid conflict with bad block marker */
  674. bbt_main_descr.offs = 0;
  675. bbt_mirror_descr.offs = 0;
  676. }
  677. priv->bufnum_mask = 15;
  678. break;
  679. case CSOR_NAND_PGS_2K:
  680. layout = &oob_2048_ecc4;
  681. priv->bufnum_mask = 3;
  682. break;
  683. case CSOR_NAND_PGS_4K:
  684. if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
  685. CSOR_NAND_ECC_MODE_4) {
  686. layout = &oob_4096_ecc4;
  687. } else {
  688. layout = &oob_4096_ecc8;
  689. nand->ecc.bytes = 16;
  690. }
  691. priv->bufnum_mask = 1;
  692. break;
  693. default:
  694. printf("ifc nand: bad csor %#x: bad page size\n", csor);
  695. return -ENODEV;
  696. }
  697. /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
  698. if (csor & CSOR_NAND_ECC_DEC_EN) {
  699. nand->ecc.mode = NAND_ECC_HW;
  700. nand->ecc.layout = layout;
  701. } else {
  702. nand->ecc.mode = NAND_ECC_SOFT;
  703. }
  704. return 0;
  705. }