fsl_ifc_nand.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. /* OOB area --> READOOB */
  381. column -= mtd->writesize;
  382. nand_fcr0 |= NAND_CMD_READOOB <<
  383. IFC_NAND_FCR0_CMD0_SHIFT;
  384. ctrl->oob = 1;
  385. } else if (column < 256) {
  386. /* First 256 bytes --> READ0 */
  387. nand_fcr0 |= NAND_CMD_READ0 << FCR_CMD0_SHIFT;
  388. } else {
  389. /* Second 256 bytes --> READ1 */
  390. nand_fcr0 |= NAND_CMD_READ1 << FCR_CMD0_SHIFT;
  391. }
  392. }
  393. out_be32(&ifc->ifc_nand.nand_fcr0, nand_fcr0);
  394. set_addr(mtd, column, page_addr, ctrl->oob);
  395. return;
  396. }
  397. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  398. case NAND_CMD_PAGEPROG:
  399. if (ctrl->oob)
  400. out_be32(&ifc->ifc_nand.nand_fbcr, ctrl->index);
  401. else
  402. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  403. fsl_ifc_run_command(mtd);
  404. return;
  405. case NAND_CMD_STATUS:
  406. out_be32(&ifc->ifc_nand.nand_fir0,
  407. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  408. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT));
  409. out_be32(&ifc->ifc_nand.nand_fcr0,
  410. NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT);
  411. out_be32(&ifc->ifc_nand.nand_fbcr, 1);
  412. set_addr(mtd, 0, 0, 0);
  413. ctrl->read_bytes = 1;
  414. fsl_ifc_run_command(mtd);
  415. /* Chip sometimes reporting write protect even when it's not */
  416. out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
  417. return;
  418. case NAND_CMD_RESET:
  419. out_be32(&ifc->ifc_nand.nand_fir0,
  420. IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT);
  421. out_be32(&ifc->ifc_nand.nand_fcr0,
  422. NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT);
  423. fsl_ifc_run_command(mtd);
  424. return;
  425. default:
  426. printf("%s: error, unsupported command 0x%x.\n",
  427. __func__, command);
  428. }
  429. }
  430. /*
  431. * Write buf to the IFC NAND Controller Data Buffer
  432. */
  433. static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  434. {
  435. struct nand_chip *chip = mtd->priv;
  436. struct fsl_ifc_mtd *priv = chip->priv;
  437. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  438. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  439. if (len <= 0) {
  440. printf("%s of %d bytes", __func__, len);
  441. ctrl->status = 0;
  442. return;
  443. }
  444. if ((unsigned int)len > bufsize - ctrl->index) {
  445. printf("%s beyond end of buffer "
  446. "(%d requested, %u available)\n",
  447. __func__, len, bufsize - ctrl->index);
  448. len = bufsize - ctrl->index;
  449. }
  450. memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
  451. ctrl->index += len;
  452. }
  453. /*
  454. * read a byte from either the IFC hardware buffer if it has any data left
  455. * otherwise issue a command to read a single byte.
  456. */
  457. static u8 fsl_ifc_read_byte(struct mtd_info *mtd)
  458. {
  459. struct nand_chip *chip = mtd->priv;
  460. struct fsl_ifc_mtd *priv = chip->priv;
  461. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  462. /* If there are still bytes in the IFC buffer, then use the
  463. * next byte. */
  464. if (ctrl->index < ctrl->read_bytes)
  465. return in_8(&ctrl->addr[ctrl->index++]);
  466. printf("%s beyond end of buffer\n", __func__);
  467. return ERR_BYTE;
  468. }
  469. /*
  470. * Read two bytes from the IFC hardware buffer
  471. * read function for 16-bit buswith
  472. */
  473. static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
  474. {
  475. struct nand_chip *chip = mtd->priv;
  476. struct fsl_ifc_mtd *priv = chip->priv;
  477. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  478. uint16_t data;
  479. /*
  480. * If there are still bytes in the IFC buffer, then use the
  481. * next byte.
  482. */
  483. if (ctrl->index < ctrl->read_bytes) {
  484. data = in_be16((uint16_t *)&ctrl->
  485. addr[ctrl->index]);
  486. ctrl->index += 2;
  487. return (uint8_t)data;
  488. }
  489. printf("%s beyond end of buffer\n", __func__);
  490. return ERR_BYTE;
  491. }
  492. /*
  493. * Read from the IFC Controller Data Buffer
  494. */
  495. static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  496. {
  497. struct nand_chip *chip = mtd->priv;
  498. struct fsl_ifc_mtd *priv = chip->priv;
  499. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  500. int avail;
  501. if (len < 0)
  502. return;
  503. avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
  504. memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
  505. ctrl->index += avail;
  506. if (len > avail)
  507. printf("%s beyond end of buffer "
  508. "(%d requested, %d available)\n",
  509. __func__, len, avail);
  510. }
  511. /*
  512. * Verify buffer against the IFC Controller Data Buffer
  513. */
  514. static int fsl_ifc_verify_buf(struct mtd_info *mtd,
  515. const u_char *buf, int len)
  516. {
  517. struct nand_chip *chip = mtd->priv;
  518. struct fsl_ifc_mtd *priv = chip->priv;
  519. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  520. int i;
  521. if (len < 0) {
  522. printf("%s of %d bytes", __func__, len);
  523. return -EINVAL;
  524. }
  525. if ((unsigned int)len > ctrl->read_bytes - ctrl->index) {
  526. printf("%s beyond end of buffer "
  527. "(%d requested, %u available)\n",
  528. __func__, len, ctrl->read_bytes - ctrl->index);
  529. ctrl->index = ctrl->read_bytes;
  530. return -EINVAL;
  531. }
  532. for (i = 0; i < len; i++)
  533. if (in_8(&ctrl->addr[ctrl->index + i]) != buf[i])
  534. break;
  535. ctrl->index += len;
  536. return i == len && ctrl->status == IFC_NAND_EVTER_STAT_OPC ? 0 : -EIO;
  537. }
  538. /* This function is called after Program and Erase Operations to
  539. * check for success or failure.
  540. */
  541. static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  542. {
  543. struct fsl_ifc_mtd *priv = chip->priv;
  544. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  545. struct fsl_ifc *ifc = ctrl->regs;
  546. u32 nand_fsr;
  547. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  548. return NAND_STATUS_FAIL;
  549. /* Use READ_STATUS command, but wait for the device to be ready */
  550. out_be32(&ifc->ifc_nand.nand_fir0,
  551. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  552. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT));
  553. out_be32(&ifc->ifc_nand.nand_fcr0, NAND_CMD_STATUS <<
  554. IFC_NAND_FCR0_CMD0_SHIFT);
  555. out_be32(&ifc->ifc_nand.nand_fbcr, 1);
  556. set_addr(mtd, 0, 0, 0);
  557. ctrl->read_bytes = 1;
  558. fsl_ifc_run_command(mtd);
  559. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  560. return NAND_STATUS_FAIL;
  561. nand_fsr = in_be32(&ifc->ifc_nand.nand_fsr);
  562. /* Chip sometimes reporting write protect even when it's not */
  563. nand_fsr = nand_fsr | NAND_STATUS_WP;
  564. return nand_fsr;
  565. }
  566. static int fsl_ifc_read_page(struct mtd_info *mtd,
  567. struct nand_chip *chip,
  568. uint8_t *buf, int page)
  569. {
  570. struct fsl_ifc_mtd *priv = chip->priv;
  571. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  572. fsl_ifc_read_buf(mtd, buf, mtd->writesize);
  573. fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  574. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  575. mtd->ecc_stats.failed++;
  576. return 0;
  577. }
  578. /* ECC will be calculated automatically, and errors will be detected in
  579. * waitfunc.
  580. */
  581. static void fsl_ifc_write_page(struct mtd_info *mtd,
  582. struct nand_chip *chip,
  583. const uint8_t *buf)
  584. {
  585. fsl_ifc_write_buf(mtd, buf, mtd->writesize);
  586. fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  587. }
  588. static void fsl_ifc_ctrl_init(void)
  589. {
  590. ifc_ctrl = kzalloc(sizeof(*ifc_ctrl), GFP_KERNEL);
  591. if (!ifc_ctrl)
  592. return;
  593. ifc_ctrl->regs = IFC_BASE_ADDR;
  594. /* clear event registers */
  595. out_be32(&ifc_ctrl->regs->ifc_nand.nand_evter_stat, ~0U);
  596. out_be32(&ifc_ctrl->regs->ifc_nand.pgrdcmpl_evt_stat, ~0U);
  597. /* Enable error and event for any detected errors */
  598. out_be32(&ifc_ctrl->regs->ifc_nand.nand_evter_en,
  599. IFC_NAND_EVTER_EN_OPC_EN |
  600. IFC_NAND_EVTER_EN_PGRDCMPL_EN |
  601. IFC_NAND_EVTER_EN_FTOER_EN |
  602. IFC_NAND_EVTER_EN_WPER_EN);
  603. out_be32(&ifc_ctrl->regs->ifc_nand.ncfgr, 0x0);
  604. }
  605. static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
  606. {
  607. }
  608. int board_nand_init(struct nand_chip *nand)
  609. {
  610. struct fsl_ifc_mtd *priv;
  611. struct nand_ecclayout *layout;
  612. uint32_t cspr = 0, csor = 0;
  613. if (!ifc_ctrl) {
  614. fsl_ifc_ctrl_init();
  615. if (!ifc_ctrl)
  616. return -1;
  617. }
  618. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  619. if (!priv)
  620. return -ENOMEM;
  621. priv->ctrl = ifc_ctrl;
  622. priv->vbase = nand->IO_ADDR_R;
  623. /* Find which chip select it is connected to.
  624. */
  625. for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
  626. phys_addr_t base_addr = virt_to_phys(nand->IO_ADDR_R);
  627. cspr = in_be32(&ifc_ctrl->regs->cspr_cs[priv->bank].cspr);
  628. csor = in_be32(&ifc_ctrl->regs->csor_cs[priv->bank].csor);
  629. if ((cspr & CSPR_V) && (cspr & CSPR_MSEL) == CSPR_MSEL_NAND &&
  630. (cspr & CSPR_BA) == CSPR_PHYS_ADDR(base_addr)) {
  631. ifc_ctrl->cs_nand = priv->bank << IFC_NAND_CSEL_SHIFT;
  632. break;
  633. }
  634. }
  635. if (priv->bank >= MAX_BANKS) {
  636. printf("%s: address did not match any "
  637. "chip selects\n", __func__);
  638. return -ENODEV;
  639. }
  640. ifc_ctrl->chips[priv->bank] = priv;
  641. /* fill in nand_chip structure */
  642. /* set up function call table */
  643. nand->write_buf = fsl_ifc_write_buf;
  644. nand->read_buf = fsl_ifc_read_buf;
  645. nand->verify_buf = fsl_ifc_verify_buf;
  646. nand->select_chip = fsl_ifc_select_chip;
  647. nand->cmdfunc = fsl_ifc_cmdfunc;
  648. nand->waitfunc = fsl_ifc_wait;
  649. /* set up nand options */
  650. nand->bbt_td = &bbt_main_descr;
  651. nand->bbt_md = &bbt_mirror_descr;
  652. /* set up nand options */
  653. nand->options = NAND_NO_READRDY | NAND_NO_AUTOINCR |
  654. NAND_USE_FLASH_BBT;
  655. if (cspr & CSPR_PORT_SIZE_16) {
  656. nand->read_byte = fsl_ifc_read_byte16;
  657. nand->options |= NAND_BUSWIDTH_16;
  658. } else {
  659. nand->read_byte = fsl_ifc_read_byte;
  660. }
  661. nand->controller = &ifc_ctrl->controller;
  662. nand->priv = priv;
  663. nand->ecc.read_page = fsl_ifc_read_page;
  664. nand->ecc.write_page = fsl_ifc_write_page;
  665. /* Hardware generates ECC per 512 Bytes */
  666. nand->ecc.size = 512;
  667. nand->ecc.bytes = 8;
  668. switch (csor & CSOR_NAND_PGS_MASK) {
  669. case CSOR_NAND_PGS_512:
  670. if (nand->options & NAND_BUSWIDTH_16) {
  671. layout = &oob_512_16bit_ecc4;
  672. } else {
  673. layout = &oob_512_8bit_ecc4;
  674. /* Avoid conflict with bad block marker */
  675. bbt_main_descr.offs = 0;
  676. bbt_mirror_descr.offs = 0;
  677. }
  678. priv->bufnum_mask = 15;
  679. break;
  680. case CSOR_NAND_PGS_2K:
  681. layout = &oob_2048_ecc4;
  682. priv->bufnum_mask = 3;
  683. break;
  684. case CSOR_NAND_PGS_4K:
  685. if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
  686. CSOR_NAND_ECC_MODE_4) {
  687. layout = &oob_4096_ecc4;
  688. } else {
  689. layout = &oob_4096_ecc8;
  690. nand->ecc.bytes = 16;
  691. }
  692. priv->bufnum_mask = 1;
  693. break;
  694. default:
  695. printf("ifc nand: bad csor %#x: bad page size\n", csor);
  696. return -ENODEV;
  697. }
  698. /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
  699. if (csor & CSOR_NAND_ECC_DEC_EN) {
  700. nand->ecc.mode = NAND_ECC_HW;
  701. nand->ecc.layout = layout;
  702. } else {
  703. nand->ecc.mode = NAND_ECC_SOFT;
  704. }
  705. return 0;
  706. }