fsl_ifc_nand.c 23 KB

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