fsl_ifc_nand.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. * Freescale Integrated Flash Controller NAND driver
  3. *
  4. * Copyright 2011-2012 Freescale Semiconductor, Inc
  5. *
  6. * Author: Dipen Dudhat <Dipen.Dudhat@freescale.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/of_address.h>
  26. #include <linux/slab.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/nand.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/mtd/nand_ecc.h>
  31. #include <linux/fsl_ifc.h>
  32. #define ERR_BYTE 0xFF /* Value returned for read
  33. bytes when read failed */
  34. #define IFC_TIMEOUT_MSECS 500 /* Maximum number of mSecs to wait
  35. for IFC NAND Machine */
  36. struct fsl_ifc_ctrl;
  37. /* mtd information per set */
  38. struct fsl_ifc_mtd {
  39. struct mtd_info mtd;
  40. struct nand_chip chip;
  41. struct fsl_ifc_ctrl *ctrl;
  42. struct device *dev;
  43. int bank; /* Chip select bank number */
  44. unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */
  45. u8 __iomem *vbase; /* Chip select base virtual address */
  46. };
  47. /* overview of the fsl ifc controller */
  48. struct fsl_ifc_nand_ctrl {
  49. struct nand_hw_control controller;
  50. struct fsl_ifc_mtd *chips[FSL_IFC_BANK_COUNT];
  51. void __iomem *addr; /* Address of assigned IFC buffer */
  52. unsigned int page; /* Last page written to / read from */
  53. unsigned int read_bytes;/* Number of bytes read during command */
  54. unsigned int column; /* Saved column from SEQIN */
  55. unsigned int index; /* Pointer to next byte to 'read' */
  56. unsigned int oob; /* Non zero if operating on OOB data */
  57. unsigned int eccread; /* Non zero for a full-page ECC read */
  58. unsigned int counter; /* counter for the initializations */
  59. unsigned int max_bitflips; /* Saved during READ0 cmd */
  60. };
  61. static struct fsl_ifc_nand_ctrl *ifc_nand_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. /* 8192-byte page size with 4-bit ECC */
  124. static struct nand_ecclayout oob_8192_ecc4 = {
  125. .eccbytes = 128,
  126. .eccpos = {
  127. 8, 9, 10, 11, 12, 13, 14, 15,
  128. 16, 17, 18, 19, 20, 21, 22, 23,
  129. 24, 25, 26, 27, 28, 29, 30, 31,
  130. 32, 33, 34, 35, 36, 37, 38, 39,
  131. 40, 41, 42, 43, 44, 45, 46, 47,
  132. 48, 49, 50, 51, 52, 53, 54, 55,
  133. 56, 57, 58, 59, 60, 61, 62, 63,
  134. 64, 65, 66, 67, 68, 69, 70, 71,
  135. 72, 73, 74, 75, 76, 77, 78, 79,
  136. 80, 81, 82, 83, 84, 85, 86, 87,
  137. 88, 89, 90, 91, 92, 93, 94, 95,
  138. 96, 97, 98, 99, 100, 101, 102, 103,
  139. 104, 105, 106, 107, 108, 109, 110, 111,
  140. 112, 113, 114, 115, 116, 117, 118, 119,
  141. 120, 121, 122, 123, 124, 125, 126, 127,
  142. 128, 129, 130, 131, 132, 133, 134, 135,
  143. },
  144. .oobfree = { {2, 6}, {136, 208} },
  145. };
  146. /* 8192-byte page size with 8-bit ECC -- requires 218-byte OOB */
  147. static struct nand_ecclayout oob_8192_ecc8 = {
  148. .eccbytes = 256,
  149. .eccpos = {
  150. 8, 9, 10, 11, 12, 13, 14, 15,
  151. 16, 17, 18, 19, 20, 21, 22, 23,
  152. 24, 25, 26, 27, 28, 29, 30, 31,
  153. 32, 33, 34, 35, 36, 37, 38, 39,
  154. 40, 41, 42, 43, 44, 45, 46, 47,
  155. 48, 49, 50, 51, 52, 53, 54, 55,
  156. 56, 57, 58, 59, 60, 61, 62, 63,
  157. 64, 65, 66, 67, 68, 69, 70, 71,
  158. 72, 73, 74, 75, 76, 77, 78, 79,
  159. 80, 81, 82, 83, 84, 85, 86, 87,
  160. 88, 89, 90, 91, 92, 93, 94, 95,
  161. 96, 97, 98, 99, 100, 101, 102, 103,
  162. 104, 105, 106, 107, 108, 109, 110, 111,
  163. 112, 113, 114, 115, 116, 117, 118, 119,
  164. 120, 121, 122, 123, 124, 125, 126, 127,
  165. 128, 129, 130, 131, 132, 133, 134, 135,
  166. 136, 137, 138, 139, 140, 141, 142, 143,
  167. 144, 145, 146, 147, 148, 149, 150, 151,
  168. 152, 153, 154, 155, 156, 157, 158, 159,
  169. 160, 161, 162, 163, 164, 165, 166, 167,
  170. 168, 169, 170, 171, 172, 173, 174, 175,
  171. 176, 177, 178, 179, 180, 181, 182, 183,
  172. 184, 185, 186, 187, 188, 189, 190, 191,
  173. 192, 193, 194, 195, 196, 197, 198, 199,
  174. 200, 201, 202, 203, 204, 205, 206, 207,
  175. 208, 209, 210, 211, 212, 213, 214, 215,
  176. 216, 217, 218, 219, 220, 221, 222, 223,
  177. 224, 225, 226, 227, 228, 229, 230, 231,
  178. 232, 233, 234, 235, 236, 237, 238, 239,
  179. 240, 241, 242, 243, 244, 245, 246, 247,
  180. 248, 249, 250, 251, 252, 253, 254, 255,
  181. 256, 257, 258, 259, 260, 261, 262, 263,
  182. },
  183. .oobfree = { {2, 6}, {264, 80} },
  184. };
  185. /*
  186. * Generic flash bbt descriptors
  187. */
  188. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  189. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  190. static struct nand_bbt_descr bbt_main_descr = {
  191. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  192. NAND_BBT_2BIT | NAND_BBT_VERSION,
  193. .offs = 2, /* 0 on 8-bit small page */
  194. .len = 4,
  195. .veroffs = 6,
  196. .maxblocks = 4,
  197. .pattern = bbt_pattern,
  198. };
  199. static struct nand_bbt_descr bbt_mirror_descr = {
  200. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  201. NAND_BBT_2BIT | NAND_BBT_VERSION,
  202. .offs = 2, /* 0 on 8-bit small page */
  203. .len = 4,
  204. .veroffs = 6,
  205. .maxblocks = 4,
  206. .pattern = mirror_pattern,
  207. };
  208. /*
  209. * Set up the IFC hardware block and page address fields, and the ifc nand
  210. * structure addr field to point to the correct IFC buffer in memory
  211. */
  212. static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
  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_regs __iomem *ifc = ctrl->regs;
  218. int buf_num;
  219. ifc_nand_ctrl->page = page_addr;
  220. /* Program ROW0/COL0 */
  221. ifc_out32(page_addr, &ifc->ifc_nand.row0);
  222. ifc_out32((oob ? IFC_NAND_COL_MS : 0) | column, &ifc->ifc_nand.col0);
  223. buf_num = page_addr & priv->bufnum_mask;
  224. ifc_nand_ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2);
  225. ifc_nand_ctrl->index = column;
  226. /* for OOB data point to the second half of the buffer */
  227. if (oob)
  228. ifc_nand_ctrl->index += mtd->writesize;
  229. }
  230. static int is_blank(struct mtd_info *mtd, unsigned int bufnum)
  231. {
  232. struct nand_chip *chip = mtd->priv;
  233. struct fsl_ifc_mtd *priv = chip->priv;
  234. u8 __iomem *addr = priv->vbase + bufnum * (mtd->writesize * 2);
  235. u32 __iomem *mainarea = (u32 __iomem *)addr;
  236. u8 __iomem *oob = addr + mtd->writesize;
  237. int i;
  238. for (i = 0; i < mtd->writesize / 4; i++) {
  239. if (__raw_readl(&mainarea[i]) != 0xffffffff)
  240. return 0;
  241. }
  242. for (i = 0; i < chip->ecc.layout->eccbytes; i++) {
  243. int pos = chip->ecc.layout->eccpos[i];
  244. if (__raw_readb(&oob[pos]) != 0xff)
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. /* returns nonzero if entire page is blank */
  250. static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
  251. u32 *eccstat, unsigned int bufnum)
  252. {
  253. u32 reg = eccstat[bufnum / 4];
  254. int errors;
  255. errors = (reg >> ((3 - bufnum % 4) * 8)) & 15;
  256. return errors;
  257. }
  258. /*
  259. * execute IFC NAND command and wait for it to complete
  260. */
  261. static void fsl_ifc_run_command(struct mtd_info *mtd)
  262. {
  263. struct nand_chip *chip = mtd->priv;
  264. struct fsl_ifc_mtd *priv = chip->priv;
  265. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  266. struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
  267. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  268. u32 eccstat[4];
  269. int i;
  270. /* set the chip select for NAND Transaction */
  271. ifc_out32(priv->bank << IFC_NAND_CSEL_SHIFT,
  272. &ifc->ifc_nand.nand_csel);
  273. dev_vdbg(priv->dev,
  274. "%s: fir0=%08x fcr0=%08x\n",
  275. __func__,
  276. ifc_in32(&ifc->ifc_nand.nand_fir0),
  277. ifc_in32(&ifc->ifc_nand.nand_fcr0));
  278. ctrl->nand_stat = 0;
  279. /* start read/write seq */
  280. ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt);
  281. /* wait for command complete flag or timeout */
  282. wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
  283. msecs_to_jiffies(IFC_TIMEOUT_MSECS));
  284. /* ctrl->nand_stat will be updated from IRQ context */
  285. if (!ctrl->nand_stat)
  286. dev_err(priv->dev, "Controller is not responding\n");
  287. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_FTOER)
  288. dev_err(priv->dev, "NAND Flash Timeout Error\n");
  289. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_WPER)
  290. dev_err(priv->dev, "NAND Flash Write Protect Error\n");
  291. nctrl->max_bitflips = 0;
  292. if (nctrl->eccread) {
  293. int errors;
  294. int bufnum = nctrl->page & priv->bufnum_mask;
  295. int sector = bufnum * chip->ecc.steps;
  296. int sector_end = sector + chip->ecc.steps - 1;
  297. for (i = sector / 4; i <= sector_end / 4; i++)
  298. eccstat[i] = ifc_in32(&ifc->ifc_nand.nand_eccstat[i]);
  299. for (i = sector; i <= sector_end; i++) {
  300. errors = check_read_ecc(mtd, ctrl, eccstat, i);
  301. if (errors == 15) {
  302. /*
  303. * Uncorrectable error.
  304. * OK only if the whole page is blank.
  305. *
  306. * We disable ECCER reporting due to...
  307. * erratum IFC-A002770 -- so report it now if we
  308. * see an uncorrectable error in ECCSTAT.
  309. */
  310. if (!is_blank(mtd, bufnum))
  311. ctrl->nand_stat |=
  312. IFC_NAND_EVTER_STAT_ECCER;
  313. break;
  314. }
  315. mtd->ecc_stats.corrected += errors;
  316. nctrl->max_bitflips = max_t(unsigned int,
  317. nctrl->max_bitflips,
  318. errors);
  319. }
  320. nctrl->eccread = 0;
  321. }
  322. }
  323. static void fsl_ifc_do_read(struct nand_chip *chip,
  324. int oob,
  325. struct mtd_info *mtd)
  326. {
  327. struct fsl_ifc_mtd *priv = chip->priv;
  328. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  329. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  330. /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
  331. if (mtd->writesize > 512) {
  332. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  333. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  334. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  335. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
  336. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT),
  337. &ifc->ifc_nand.nand_fir0);
  338. ifc_out32(0x0, &ifc->ifc_nand.nand_fir1);
  339. ifc_out32((NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
  340. (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT),
  341. &ifc->ifc_nand.nand_fcr0);
  342. } else {
  343. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  344. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  345. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  346. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT),
  347. &ifc->ifc_nand.nand_fir0);
  348. ifc_out32(0x0, &ifc->ifc_nand.nand_fir1);
  349. if (oob)
  350. ifc_out32(NAND_CMD_READOOB <<
  351. IFC_NAND_FCR0_CMD0_SHIFT,
  352. &ifc->ifc_nand.nand_fcr0);
  353. else
  354. ifc_out32(NAND_CMD_READ0 <<
  355. IFC_NAND_FCR0_CMD0_SHIFT,
  356. &ifc->ifc_nand.nand_fcr0);
  357. }
  358. }
  359. /* cmdfunc send commands to the IFC NAND Machine */
  360. static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  361. int column, int page_addr) {
  362. struct nand_chip *chip = mtd->priv;
  363. struct fsl_ifc_mtd *priv = chip->priv;
  364. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  365. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  366. /* clear the read buffer */
  367. ifc_nand_ctrl->read_bytes = 0;
  368. if (command != NAND_CMD_PAGEPROG)
  369. ifc_nand_ctrl->index = 0;
  370. switch (command) {
  371. /* READ0 read the entire buffer to use hardware ECC. */
  372. case NAND_CMD_READ0:
  373. ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
  374. set_addr(mtd, 0, page_addr, 0);
  375. ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  376. ifc_nand_ctrl->index += column;
  377. if (chip->ecc.mode == NAND_ECC_HW)
  378. ifc_nand_ctrl->eccread = 1;
  379. fsl_ifc_do_read(chip, 0, mtd);
  380. fsl_ifc_run_command(mtd);
  381. return;
  382. /* READOOB reads only the OOB because no ECC is performed. */
  383. case NAND_CMD_READOOB:
  384. ifc_out32(mtd->oobsize - column, &ifc->ifc_nand.nand_fbcr);
  385. set_addr(mtd, column, page_addr, 1);
  386. ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  387. fsl_ifc_do_read(chip, 1, mtd);
  388. fsl_ifc_run_command(mtd);
  389. return;
  390. case NAND_CMD_READID:
  391. case NAND_CMD_PARAM: {
  392. /*
  393. * For READID, read 8 bytes that are currently used.
  394. * For PARAM, read all 3 copies of 256-bytes pages.
  395. */
  396. int len = 8;
  397. int timing = IFC_FIR_OP_RB;
  398. if (command == NAND_CMD_PARAM) {
  399. timing = IFC_FIR_OP_RBCD;
  400. len = 256 * 3;
  401. }
  402. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  403. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  404. (timing << IFC_NAND_FIR0_OP2_SHIFT),
  405. &ifc->ifc_nand.nand_fir0);
  406. ifc_out32(command << IFC_NAND_FCR0_CMD0_SHIFT,
  407. &ifc->ifc_nand.nand_fcr0);
  408. ifc_out32(column, &ifc->ifc_nand.row3);
  409. ifc_out32(len, &ifc->ifc_nand.nand_fbcr);
  410. ifc_nand_ctrl->read_bytes = len;
  411. set_addr(mtd, 0, 0, 0);
  412. fsl_ifc_run_command(mtd);
  413. return;
  414. }
  415. /* ERASE1 stores the block and page address */
  416. case NAND_CMD_ERASE1:
  417. set_addr(mtd, 0, page_addr, 0);
  418. return;
  419. /* ERASE2 uses the block and page address from ERASE1 */
  420. case NAND_CMD_ERASE2:
  421. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  422. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  423. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT),
  424. &ifc->ifc_nand.nand_fir0);
  425. ifc_out32((NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
  426. (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT),
  427. &ifc->ifc_nand.nand_fcr0);
  428. ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
  429. ifc_nand_ctrl->read_bytes = 0;
  430. fsl_ifc_run_command(mtd);
  431. return;
  432. /* SEQIN sets up the addr buffer and all registers except the length */
  433. case NAND_CMD_SEQIN: {
  434. u32 nand_fcr0;
  435. ifc_nand_ctrl->column = column;
  436. ifc_nand_ctrl->oob = 0;
  437. if (mtd->writesize > 512) {
  438. nand_fcr0 =
  439. (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
  440. (NAND_CMD_STATUS << IFC_NAND_FCR0_CMD1_SHIFT) |
  441. (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD2_SHIFT);
  442. ifc_out32(
  443. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  444. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  445. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  446. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) |
  447. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP4_SHIFT),
  448. &ifc->ifc_nand.nand_fir0);
  449. ifc_out32(
  450. (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT) |
  451. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP6_SHIFT) |
  452. (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP7_SHIFT),
  453. &ifc->ifc_nand.nand_fir1);
  454. } else {
  455. nand_fcr0 = ((NAND_CMD_PAGEPROG <<
  456. IFC_NAND_FCR0_CMD1_SHIFT) |
  457. (NAND_CMD_SEQIN <<
  458. IFC_NAND_FCR0_CMD2_SHIFT) |
  459. (NAND_CMD_STATUS <<
  460. IFC_NAND_FCR0_CMD3_SHIFT));
  461. ifc_out32(
  462. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  463. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
  464. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  465. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
  466. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT),
  467. &ifc->ifc_nand.nand_fir0);
  468. ifc_out32(
  469. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR1_OP5_SHIFT) |
  470. (IFC_FIR_OP_CW3 << IFC_NAND_FIR1_OP6_SHIFT) |
  471. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP7_SHIFT) |
  472. (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP8_SHIFT),
  473. &ifc->ifc_nand.nand_fir1);
  474. if (column >= mtd->writesize)
  475. nand_fcr0 |=
  476. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
  477. else
  478. nand_fcr0 |=
  479. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
  480. }
  481. if (column >= mtd->writesize) {
  482. /* OOB area --> READOOB */
  483. column -= mtd->writesize;
  484. ifc_nand_ctrl->oob = 1;
  485. }
  486. ifc_out32(nand_fcr0, &ifc->ifc_nand.nand_fcr0);
  487. set_addr(mtd, column, page_addr, ifc_nand_ctrl->oob);
  488. return;
  489. }
  490. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  491. case NAND_CMD_PAGEPROG: {
  492. if (ifc_nand_ctrl->oob) {
  493. ifc_out32(ifc_nand_ctrl->index -
  494. ifc_nand_ctrl->column,
  495. &ifc->ifc_nand.nand_fbcr);
  496. } else {
  497. ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
  498. }
  499. fsl_ifc_run_command(mtd);
  500. return;
  501. }
  502. case NAND_CMD_STATUS: {
  503. void __iomem *addr;
  504. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  505. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT),
  506. &ifc->ifc_nand.nand_fir0);
  507. ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
  508. &ifc->ifc_nand.nand_fcr0);
  509. ifc_out32(1, &ifc->ifc_nand.nand_fbcr);
  510. set_addr(mtd, 0, 0, 0);
  511. ifc_nand_ctrl->read_bytes = 1;
  512. fsl_ifc_run_command(mtd);
  513. /*
  514. * The chip always seems to report that it is
  515. * write-protected, even when it is not.
  516. */
  517. addr = ifc_nand_ctrl->addr;
  518. if (chip->options & NAND_BUSWIDTH_16)
  519. ifc_out16(ifc_in16(addr) | (NAND_STATUS_WP), addr);
  520. else
  521. ifc_out8(ifc_in8(addr) | (NAND_STATUS_WP), addr);
  522. return;
  523. }
  524. case NAND_CMD_RESET:
  525. ifc_out32(IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT,
  526. &ifc->ifc_nand.nand_fir0);
  527. ifc_out32(NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT,
  528. &ifc->ifc_nand.nand_fcr0);
  529. fsl_ifc_run_command(mtd);
  530. return;
  531. default:
  532. dev_err(priv->dev, "%s: error, unsupported command 0x%x.\n",
  533. __func__, command);
  534. }
  535. }
  536. static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
  537. {
  538. /* The hardware does not seem to support multiple
  539. * chips per bank.
  540. */
  541. }
  542. /*
  543. * Write buf to the IFC NAND Controller Data Buffer
  544. */
  545. static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  546. {
  547. struct nand_chip *chip = mtd->priv;
  548. struct fsl_ifc_mtd *priv = chip->priv;
  549. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  550. if (len <= 0) {
  551. dev_err(priv->dev, "%s: len %d bytes", __func__, len);
  552. return;
  553. }
  554. if ((unsigned int)len > bufsize - ifc_nand_ctrl->index) {
  555. dev_err(priv->dev,
  556. "%s: beyond end of buffer (%d requested, %u available)\n",
  557. __func__, len, bufsize - ifc_nand_ctrl->index);
  558. len = bufsize - ifc_nand_ctrl->index;
  559. }
  560. memcpy_toio(ifc_nand_ctrl->addr + ifc_nand_ctrl->index, buf, len);
  561. ifc_nand_ctrl->index += len;
  562. }
  563. /*
  564. * Read a byte from either the IFC hardware buffer
  565. * read function for 8-bit buswidth
  566. */
  567. static uint8_t fsl_ifc_read_byte(struct mtd_info *mtd)
  568. {
  569. struct nand_chip *chip = mtd->priv;
  570. struct fsl_ifc_mtd *priv = chip->priv;
  571. unsigned int offset;
  572. /*
  573. * If there are still bytes in the IFC buffer, then use the
  574. * next byte.
  575. */
  576. if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
  577. offset = ifc_nand_ctrl->index++;
  578. return ifc_in8(ifc_nand_ctrl->addr + offset);
  579. }
  580. dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
  581. return ERR_BYTE;
  582. }
  583. /*
  584. * Read two bytes from the IFC hardware buffer
  585. * read function for 16-bit buswith
  586. */
  587. static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
  588. {
  589. struct nand_chip *chip = mtd->priv;
  590. struct fsl_ifc_mtd *priv = chip->priv;
  591. uint16_t data;
  592. /*
  593. * If there are still bytes in the IFC buffer, then use the
  594. * next byte.
  595. */
  596. if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
  597. data = ifc_in16(ifc_nand_ctrl->addr + ifc_nand_ctrl->index);
  598. ifc_nand_ctrl->index += 2;
  599. return (uint8_t) data;
  600. }
  601. dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
  602. return ERR_BYTE;
  603. }
  604. /*
  605. * Read from the IFC Controller Data Buffer
  606. */
  607. static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  608. {
  609. struct nand_chip *chip = mtd->priv;
  610. struct fsl_ifc_mtd *priv = chip->priv;
  611. int avail;
  612. if (len < 0) {
  613. dev_err(priv->dev, "%s: len %d bytes", __func__, len);
  614. return;
  615. }
  616. avail = min((unsigned int)len,
  617. ifc_nand_ctrl->read_bytes - ifc_nand_ctrl->index);
  618. memcpy_fromio(buf, ifc_nand_ctrl->addr + ifc_nand_ctrl->index, avail);
  619. ifc_nand_ctrl->index += avail;
  620. if (len > avail)
  621. dev_err(priv->dev,
  622. "%s: beyond end of buffer (%d requested, %d available)\n",
  623. __func__, len, avail);
  624. }
  625. /*
  626. * This function is called after Program and Erase Operations to
  627. * check for success or failure.
  628. */
  629. static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  630. {
  631. struct fsl_ifc_mtd *priv = chip->priv;
  632. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  633. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  634. u32 nand_fsr;
  635. int status;
  636. /* Use READ_STATUS command, but wait for the device to be ready */
  637. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  638. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT),
  639. &ifc->ifc_nand.nand_fir0);
  640. ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
  641. &ifc->ifc_nand.nand_fcr0);
  642. ifc_out32(1, &ifc->ifc_nand.nand_fbcr);
  643. set_addr(mtd, 0, 0, 0);
  644. ifc_nand_ctrl->read_bytes = 1;
  645. fsl_ifc_run_command(mtd);
  646. nand_fsr = ifc_in32(&ifc->ifc_nand.nand_fsr);
  647. status = nand_fsr >> 24;
  648. /*
  649. * The chip always seems to report that it is
  650. * write-protected, even when it is not.
  651. */
  652. return status | NAND_STATUS_WP;
  653. }
  654. static int fsl_ifc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  655. uint8_t *buf, int oob_required, int page)
  656. {
  657. struct fsl_ifc_mtd *priv = chip->priv;
  658. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  659. struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
  660. fsl_ifc_read_buf(mtd, buf, mtd->writesize);
  661. if (oob_required)
  662. fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  663. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_ECCER)
  664. dev_err(priv->dev, "NAND Flash ECC Uncorrectable Error\n");
  665. if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
  666. mtd->ecc_stats.failed++;
  667. return nctrl->max_bitflips;
  668. }
  669. /* ECC will be calculated automatically, and errors will be detected in
  670. * waitfunc.
  671. */
  672. static int fsl_ifc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  673. const uint8_t *buf, int oob_required, int page)
  674. {
  675. fsl_ifc_write_buf(mtd, buf, mtd->writesize);
  676. fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  677. return 0;
  678. }
  679. static int fsl_ifc_chip_init_tail(struct mtd_info *mtd)
  680. {
  681. struct nand_chip *chip = mtd->priv;
  682. struct fsl_ifc_mtd *priv = chip->priv;
  683. dev_dbg(priv->dev, "%s: nand->numchips = %d\n", __func__,
  684. chip->numchips);
  685. dev_dbg(priv->dev, "%s: nand->chipsize = %lld\n", __func__,
  686. chip->chipsize);
  687. dev_dbg(priv->dev, "%s: nand->pagemask = %8x\n", __func__,
  688. chip->pagemask);
  689. dev_dbg(priv->dev, "%s: nand->chip_delay = %d\n", __func__,
  690. chip->chip_delay);
  691. dev_dbg(priv->dev, "%s: nand->badblockpos = %d\n", __func__,
  692. chip->badblockpos);
  693. dev_dbg(priv->dev, "%s: nand->chip_shift = %d\n", __func__,
  694. chip->chip_shift);
  695. dev_dbg(priv->dev, "%s: nand->page_shift = %d\n", __func__,
  696. chip->page_shift);
  697. dev_dbg(priv->dev, "%s: nand->phys_erase_shift = %d\n", __func__,
  698. chip->phys_erase_shift);
  699. dev_dbg(priv->dev, "%s: nand->ecc.mode = %d\n", __func__,
  700. chip->ecc.mode);
  701. dev_dbg(priv->dev, "%s: nand->ecc.steps = %d\n", __func__,
  702. chip->ecc.steps);
  703. dev_dbg(priv->dev, "%s: nand->ecc.bytes = %d\n", __func__,
  704. chip->ecc.bytes);
  705. dev_dbg(priv->dev, "%s: nand->ecc.total = %d\n", __func__,
  706. chip->ecc.total);
  707. dev_dbg(priv->dev, "%s: nand->ecc.layout = %p\n", __func__,
  708. chip->ecc.layout);
  709. dev_dbg(priv->dev, "%s: mtd->flags = %08x\n", __func__, mtd->flags);
  710. dev_dbg(priv->dev, "%s: mtd->size = %lld\n", __func__, mtd->size);
  711. dev_dbg(priv->dev, "%s: mtd->erasesize = %d\n", __func__,
  712. mtd->erasesize);
  713. dev_dbg(priv->dev, "%s: mtd->writesize = %d\n", __func__,
  714. mtd->writesize);
  715. dev_dbg(priv->dev, "%s: mtd->oobsize = %d\n", __func__,
  716. mtd->oobsize);
  717. return 0;
  718. }
  719. static void fsl_ifc_sram_init(struct fsl_ifc_mtd *priv)
  720. {
  721. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  722. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  723. uint32_t csor = 0, csor_8k = 0, csor_ext = 0;
  724. uint32_t cs = priv->bank;
  725. /* Save CSOR and CSOR_ext */
  726. csor = ifc_in32(&ifc->csor_cs[cs].csor);
  727. csor_ext = ifc_in32(&ifc->csor_cs[cs].csor_ext);
  728. /* chage PageSize 8K and SpareSize 1K*/
  729. csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
  730. ifc_out32(csor_8k, &ifc->csor_cs[cs].csor);
  731. ifc_out32(0x0000400, &ifc->csor_cs[cs].csor_ext);
  732. /* READID */
  733. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  734. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  735. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT),
  736. &ifc->ifc_nand.nand_fir0);
  737. ifc_out32(NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT,
  738. &ifc->ifc_nand.nand_fcr0);
  739. ifc_out32(0x0, &ifc->ifc_nand.row3);
  740. ifc_out32(0x0, &ifc->ifc_nand.nand_fbcr);
  741. /* Program ROW0/COL0 */
  742. ifc_out32(0x0, &ifc->ifc_nand.row0);
  743. ifc_out32(0x0, &ifc->ifc_nand.col0);
  744. /* set the chip select for NAND Transaction */
  745. ifc_out32(cs << IFC_NAND_CSEL_SHIFT, &ifc->ifc_nand.nand_csel);
  746. /* start read seq */
  747. ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt);
  748. /* wait for command complete flag or timeout */
  749. wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
  750. msecs_to_jiffies(IFC_TIMEOUT_MSECS));
  751. if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
  752. printk(KERN_ERR "fsl-ifc: Failed to Initialise SRAM\n");
  753. /* Restore CSOR and CSOR_ext */
  754. ifc_out32(csor, &ifc->csor_cs[cs].csor);
  755. ifc_out32(csor_ext, &ifc->csor_cs[cs].csor_ext);
  756. }
  757. static int fsl_ifc_chip_init(struct fsl_ifc_mtd *priv)
  758. {
  759. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  760. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  761. struct nand_chip *chip = &priv->chip;
  762. struct nand_ecclayout *layout;
  763. u32 csor;
  764. /* Fill in fsl_ifc_mtd structure */
  765. priv->mtd.priv = chip;
  766. priv->mtd.dev.parent = priv->dev;
  767. /* fill in nand_chip structure */
  768. /* set up function call table */
  769. if ((ifc_in32(&ifc->cspr_cs[priv->bank].cspr)) & CSPR_PORT_SIZE_16)
  770. chip->read_byte = fsl_ifc_read_byte16;
  771. else
  772. chip->read_byte = fsl_ifc_read_byte;
  773. chip->write_buf = fsl_ifc_write_buf;
  774. chip->read_buf = fsl_ifc_read_buf;
  775. chip->select_chip = fsl_ifc_select_chip;
  776. chip->cmdfunc = fsl_ifc_cmdfunc;
  777. chip->waitfunc = fsl_ifc_wait;
  778. chip->bbt_td = &bbt_main_descr;
  779. chip->bbt_md = &bbt_mirror_descr;
  780. ifc_out32(0x0, &ifc->ifc_nand.ncfgr);
  781. /* set up nand options */
  782. chip->bbt_options = NAND_BBT_USE_FLASH;
  783. chip->options = NAND_NO_SUBPAGE_WRITE;
  784. if (ifc_in32(&ifc->cspr_cs[priv->bank].cspr) & CSPR_PORT_SIZE_16) {
  785. chip->read_byte = fsl_ifc_read_byte16;
  786. chip->options |= NAND_BUSWIDTH_16;
  787. } else {
  788. chip->read_byte = fsl_ifc_read_byte;
  789. }
  790. chip->controller = &ifc_nand_ctrl->controller;
  791. chip->priv = priv;
  792. chip->ecc.read_page = fsl_ifc_read_page;
  793. chip->ecc.write_page = fsl_ifc_write_page;
  794. csor = ifc_in32(&ifc->csor_cs[priv->bank].csor);
  795. /* Hardware generates ECC per 512 Bytes */
  796. chip->ecc.size = 512;
  797. chip->ecc.bytes = 8;
  798. chip->ecc.strength = 4;
  799. switch (csor & CSOR_NAND_PGS_MASK) {
  800. case CSOR_NAND_PGS_512:
  801. if (chip->options & NAND_BUSWIDTH_16) {
  802. layout = &oob_512_16bit_ecc4;
  803. } else {
  804. layout = &oob_512_8bit_ecc4;
  805. /* Avoid conflict with bad block marker */
  806. bbt_main_descr.offs = 0;
  807. bbt_mirror_descr.offs = 0;
  808. }
  809. priv->bufnum_mask = 15;
  810. break;
  811. case CSOR_NAND_PGS_2K:
  812. layout = &oob_2048_ecc4;
  813. priv->bufnum_mask = 3;
  814. break;
  815. case CSOR_NAND_PGS_4K:
  816. if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
  817. CSOR_NAND_ECC_MODE_4) {
  818. layout = &oob_4096_ecc4;
  819. } else {
  820. layout = &oob_4096_ecc8;
  821. chip->ecc.bytes = 16;
  822. chip->ecc.strength = 8;
  823. }
  824. priv->bufnum_mask = 1;
  825. break;
  826. case CSOR_NAND_PGS_8K:
  827. if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
  828. CSOR_NAND_ECC_MODE_4) {
  829. layout = &oob_8192_ecc4;
  830. } else {
  831. layout = &oob_8192_ecc8;
  832. chip->ecc.bytes = 16;
  833. chip->ecc.strength = 8;
  834. }
  835. priv->bufnum_mask = 0;
  836. break;
  837. default:
  838. dev_err(priv->dev, "bad csor %#x: bad page size\n", csor);
  839. return -ENODEV;
  840. }
  841. /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
  842. if (csor & CSOR_NAND_ECC_DEC_EN) {
  843. chip->ecc.mode = NAND_ECC_HW;
  844. chip->ecc.layout = layout;
  845. } else {
  846. chip->ecc.mode = NAND_ECC_SOFT;
  847. }
  848. if (ctrl->version == FSL_IFC_VERSION_1_1_0)
  849. fsl_ifc_sram_init(priv);
  850. return 0;
  851. }
  852. static int fsl_ifc_chip_remove(struct fsl_ifc_mtd *priv)
  853. {
  854. nand_release(&priv->mtd);
  855. kfree(priv->mtd.name);
  856. if (priv->vbase)
  857. iounmap(priv->vbase);
  858. ifc_nand_ctrl->chips[priv->bank] = NULL;
  859. return 0;
  860. }
  861. static int match_bank(struct fsl_ifc_regs __iomem *ifc, int bank,
  862. phys_addr_t addr)
  863. {
  864. u32 cspr = ifc_in32(&ifc->cspr_cs[bank].cspr);
  865. if (!(cspr & CSPR_V))
  866. return 0;
  867. if ((cspr & CSPR_MSEL) != CSPR_MSEL_NAND)
  868. return 0;
  869. return (cspr & CSPR_BA) == convert_ifc_address(addr);
  870. }
  871. static DEFINE_MUTEX(fsl_ifc_nand_mutex);
  872. static int fsl_ifc_nand_probe(struct platform_device *dev)
  873. {
  874. struct fsl_ifc_regs __iomem *ifc;
  875. struct fsl_ifc_mtd *priv;
  876. struct resource res;
  877. static const char *part_probe_types[]
  878. = { "cmdlinepart", "RedBoot", "ofpart", NULL };
  879. int ret;
  880. int bank;
  881. struct device_node *node = dev->dev.of_node;
  882. struct mtd_part_parser_data ppdata;
  883. ppdata.of_node = dev->dev.of_node;
  884. if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->regs)
  885. return -ENODEV;
  886. ifc = fsl_ifc_ctrl_dev->regs;
  887. /* get, allocate and map the memory resource */
  888. ret = of_address_to_resource(node, 0, &res);
  889. if (ret) {
  890. dev_err(&dev->dev, "%s: failed to get resource\n", __func__);
  891. return ret;
  892. }
  893. /* find which chip select it is connected to */
  894. for (bank = 0; bank < fsl_ifc_ctrl_dev->banks; bank++) {
  895. if (match_bank(ifc, bank, res.start))
  896. break;
  897. }
  898. if (bank >= fsl_ifc_ctrl_dev->banks) {
  899. dev_err(&dev->dev, "%s: address did not match any chip selects\n",
  900. __func__);
  901. return -ENODEV;
  902. }
  903. priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
  904. if (!priv)
  905. return -ENOMEM;
  906. mutex_lock(&fsl_ifc_nand_mutex);
  907. if (!fsl_ifc_ctrl_dev->nand) {
  908. ifc_nand_ctrl = kzalloc(sizeof(*ifc_nand_ctrl), GFP_KERNEL);
  909. if (!ifc_nand_ctrl) {
  910. mutex_unlock(&fsl_ifc_nand_mutex);
  911. return -ENOMEM;
  912. }
  913. ifc_nand_ctrl->read_bytes = 0;
  914. ifc_nand_ctrl->index = 0;
  915. ifc_nand_ctrl->addr = NULL;
  916. fsl_ifc_ctrl_dev->nand = ifc_nand_ctrl;
  917. spin_lock_init(&ifc_nand_ctrl->controller.lock);
  918. init_waitqueue_head(&ifc_nand_ctrl->controller.wq);
  919. } else {
  920. ifc_nand_ctrl = fsl_ifc_ctrl_dev->nand;
  921. }
  922. mutex_unlock(&fsl_ifc_nand_mutex);
  923. ifc_nand_ctrl->chips[bank] = priv;
  924. priv->bank = bank;
  925. priv->ctrl = fsl_ifc_ctrl_dev;
  926. priv->dev = &dev->dev;
  927. priv->vbase = ioremap(res.start, resource_size(&res));
  928. if (!priv->vbase) {
  929. dev_err(priv->dev, "%s: failed to map chip region\n", __func__);
  930. ret = -ENOMEM;
  931. goto err;
  932. }
  933. dev_set_drvdata(priv->dev, priv);
  934. ifc_out32(IFC_NAND_EVTER_EN_OPC_EN |
  935. IFC_NAND_EVTER_EN_FTOER_EN |
  936. IFC_NAND_EVTER_EN_WPER_EN,
  937. &ifc->ifc_nand.nand_evter_en);
  938. /* enable NAND Machine Interrupts */
  939. ifc_out32(IFC_NAND_EVTER_INTR_OPCIR_EN |
  940. IFC_NAND_EVTER_INTR_FTOERIR_EN |
  941. IFC_NAND_EVTER_INTR_WPERIR_EN,
  942. &ifc->ifc_nand.nand_evter_intr_en);
  943. priv->mtd.name = kasprintf(GFP_KERNEL, "%llx.flash", (u64)res.start);
  944. if (!priv->mtd.name) {
  945. ret = -ENOMEM;
  946. goto err;
  947. }
  948. ret = fsl_ifc_chip_init(priv);
  949. if (ret)
  950. goto err;
  951. ret = nand_scan_ident(&priv->mtd, 1, NULL);
  952. if (ret)
  953. goto err;
  954. ret = fsl_ifc_chip_init_tail(&priv->mtd);
  955. if (ret)
  956. goto err;
  957. ret = nand_scan_tail(&priv->mtd);
  958. if (ret)
  959. goto err;
  960. /* First look for RedBoot table or partitions on the command
  961. * line, these take precedence over device tree information */
  962. mtd_device_parse_register(&priv->mtd, part_probe_types, &ppdata,
  963. NULL, 0);
  964. dev_info(priv->dev, "IFC NAND device at 0x%llx, bank %d\n",
  965. (unsigned long long)res.start, priv->bank);
  966. return 0;
  967. err:
  968. fsl_ifc_chip_remove(priv);
  969. return ret;
  970. }
  971. static int fsl_ifc_nand_remove(struct platform_device *dev)
  972. {
  973. struct fsl_ifc_mtd *priv = dev_get_drvdata(&dev->dev);
  974. fsl_ifc_chip_remove(priv);
  975. mutex_lock(&fsl_ifc_nand_mutex);
  976. ifc_nand_ctrl->counter--;
  977. if (!ifc_nand_ctrl->counter) {
  978. fsl_ifc_ctrl_dev->nand = NULL;
  979. kfree(ifc_nand_ctrl);
  980. }
  981. mutex_unlock(&fsl_ifc_nand_mutex);
  982. return 0;
  983. }
  984. static const struct of_device_id fsl_ifc_nand_match[] = {
  985. {
  986. .compatible = "fsl,ifc-nand",
  987. },
  988. {}
  989. };
  990. MODULE_DEVICE_TABLE(of, fsl_ifc_nand_match);
  991. static struct platform_driver fsl_ifc_nand_driver = {
  992. .driver = {
  993. .name = "fsl,ifc-nand",
  994. .of_match_table = fsl_ifc_nand_match,
  995. },
  996. .probe = fsl_ifc_nand_probe,
  997. .remove = fsl_ifc_nand_remove,
  998. };
  999. module_platform_driver(fsl_ifc_nand_driver);
  1000. MODULE_LICENSE("GPL");
  1001. MODULE_AUTHOR("Freescale");
  1002. MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver");