fsl_ifc_nand.c 25 KB

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