fsl_ifc_nand.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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/init.h>
  25. #include <linux/kernel.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 <asm/fsl_ifc.h>
  32. #define FSL_IFC_V1_1_0 0x01010000
  33. #define ERR_BYTE 0xFF /* Value returned for read
  34. bytes when read failed */
  35. #define IFC_TIMEOUT_MSECS 500 /* Maximum number of mSecs to wait
  36. for IFC NAND Machine */
  37. struct fsl_ifc_ctrl;
  38. /* mtd information per set */
  39. struct fsl_ifc_mtd {
  40. struct mtd_info mtd;
  41. struct nand_chip chip;
  42. struct fsl_ifc_ctrl *ctrl;
  43. struct device *dev;
  44. int bank; /* Chip select bank number */
  45. unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */
  46. u8 __iomem *vbase; /* Chip select base virtual address */
  47. };
  48. /* overview of the fsl ifc controller */
  49. struct fsl_ifc_nand_ctrl {
  50. struct nand_hw_control controller;
  51. struct fsl_ifc_mtd *chips[FSL_IFC_BANK_COUNT];
  52. u8 __iomem *addr; /* Address of assigned IFC buffer */
  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 oob; /* Non zero if operating on OOB data */
  58. unsigned int eccread; /* Non zero for a full-page ECC read */
  59. unsigned int counter; /* counter for the initializations */
  60. unsigned int max_bitflips; /* Saved during READ0 cmd */
  61. };
  62. static struct fsl_ifc_nand_ctrl *ifc_nand_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_regs __iomem *ifc = ctrl->regs;
  157. int buf_num;
  158. ifc_nand_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. ifc_nand_ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2);
  164. ifc_nand_ctrl->index = column;
  165. /* for OOB data point to the second half of the buffer */
  166. if (oob)
  167. ifc_nand_ctrl->index += mtd->writesize;
  168. }
  169. static int is_blank(struct mtd_info *mtd, 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 *mainarea = (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(&mainarea[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 void 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_nand_ctrl *nctrl = ifc_nand_ctrl;
  206. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  207. u32 eccstat[4];
  208. int i;
  209. /* set the chip select for NAND Transaction */
  210. out_be32(&ifc->ifc_nand.nand_csel, priv->bank << IFC_NAND_CSEL_SHIFT);
  211. dev_vdbg(priv->dev,
  212. "%s: fir0=%08x fcr0=%08x\n",
  213. __func__,
  214. in_be32(&ifc->ifc_nand.nand_fir0),
  215. in_be32(&ifc->ifc_nand.nand_fcr0));
  216. ctrl->nand_stat = 0;
  217. /* start read/write seq */
  218. out_be32(&ifc->ifc_nand.nandseq_strt, IFC_NAND_SEQ_STRT_FIR_STRT);
  219. /* wait for command complete flag or timeout */
  220. wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
  221. IFC_TIMEOUT_MSECS * HZ/1000);
  222. /* ctrl->nand_stat will be updated from IRQ context */
  223. if (!ctrl->nand_stat)
  224. dev_err(priv->dev, "Controller is not responding\n");
  225. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_FTOER)
  226. dev_err(priv->dev, "NAND Flash Timeout Error\n");
  227. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_WPER)
  228. dev_err(priv->dev, "NAND Flash Write Protect Error\n");
  229. nctrl->max_bitflips = 0;
  230. if (nctrl->eccread) {
  231. int errors;
  232. int bufnum = nctrl->page & priv->bufnum_mask;
  233. int sector = bufnum * chip->ecc.steps;
  234. int sector_end = sector + chip->ecc.steps - 1;
  235. for (i = sector / 4; i <= sector_end / 4; i++)
  236. eccstat[i] = in_be32(&ifc->ifc_nand.nand_eccstat[i]);
  237. for (i = sector; i <= sector_end; i++) {
  238. errors = check_read_ecc(mtd, ctrl, eccstat, i);
  239. if (errors == 15) {
  240. /*
  241. * Uncorrectable error.
  242. * OK only if the whole page is blank.
  243. *
  244. * We disable ECCER reporting due to...
  245. * erratum IFC-A002770 -- so report it now if we
  246. * see an uncorrectable error in ECCSTAT.
  247. */
  248. if (!is_blank(mtd, bufnum))
  249. ctrl->nand_stat |=
  250. IFC_NAND_EVTER_STAT_ECCER;
  251. break;
  252. }
  253. mtd->ecc_stats.corrected += errors;
  254. nctrl->max_bitflips = max_t(unsigned int,
  255. nctrl->max_bitflips,
  256. errors);
  257. }
  258. nctrl->eccread = 0;
  259. }
  260. }
  261. static void fsl_ifc_do_read(struct nand_chip *chip,
  262. int oob,
  263. struct mtd_info *mtd)
  264. {
  265. struct fsl_ifc_mtd *priv = chip->priv;
  266. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  267. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  268. /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
  269. if (mtd->writesize > 512) {
  270. out_be32(&ifc->ifc_nand.nand_fir0,
  271. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  272. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  273. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  274. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
  275. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT));
  276. out_be32(&ifc->ifc_nand.nand_fir1, 0x0);
  277. out_be32(&ifc->ifc_nand.nand_fcr0,
  278. (NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
  279. (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT));
  280. } else {
  281. out_be32(&ifc->ifc_nand.nand_fir0,
  282. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  283. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  284. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  285. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT));
  286. out_be32(&ifc->ifc_nand.nand_fir1, 0x0);
  287. if (oob)
  288. out_be32(&ifc->ifc_nand.nand_fcr0,
  289. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT);
  290. else
  291. out_be32(&ifc->ifc_nand.nand_fcr0,
  292. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT);
  293. }
  294. }
  295. /* cmdfunc send commands to the IFC NAND Machine */
  296. static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  297. int column, int page_addr) {
  298. struct nand_chip *chip = mtd->priv;
  299. struct fsl_ifc_mtd *priv = chip->priv;
  300. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  301. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  302. /* clear the read buffer */
  303. ifc_nand_ctrl->read_bytes = 0;
  304. if (command != NAND_CMD_PAGEPROG)
  305. ifc_nand_ctrl->index = 0;
  306. switch (command) {
  307. /* READ0 read the entire buffer to use hardware ECC. */
  308. case NAND_CMD_READ0:
  309. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  310. set_addr(mtd, 0, page_addr, 0);
  311. ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  312. ifc_nand_ctrl->index += column;
  313. if (chip->ecc.mode == NAND_ECC_HW)
  314. ifc_nand_ctrl->eccread = 1;
  315. fsl_ifc_do_read(chip, 0, mtd);
  316. fsl_ifc_run_command(mtd);
  317. return;
  318. /* READOOB reads only the OOB because no ECC is performed. */
  319. case NAND_CMD_READOOB:
  320. out_be32(&ifc->ifc_nand.nand_fbcr, mtd->oobsize - column);
  321. set_addr(mtd, column, page_addr, 1);
  322. ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  323. fsl_ifc_do_read(chip, 1, mtd);
  324. fsl_ifc_run_command(mtd);
  325. return;
  326. case NAND_CMD_READID:
  327. case NAND_CMD_PARAM: {
  328. int timing = IFC_FIR_OP_RB;
  329. if (command == NAND_CMD_PARAM)
  330. timing = IFC_FIR_OP_RBCD;
  331. out_be32(&ifc->ifc_nand.nand_fir0,
  332. (IFC_FIR_OP_CMD0 << IFC_NAND_FIR0_OP0_SHIFT) |
  333. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  334. (timing << IFC_NAND_FIR0_OP2_SHIFT));
  335. out_be32(&ifc->ifc_nand.nand_fcr0,
  336. command << IFC_NAND_FCR0_CMD0_SHIFT);
  337. out_be32(&ifc->ifc_nand.row3, column);
  338. /*
  339. * although currently it's 8 bytes for READID, we always read
  340. * the maximum 256 bytes(for PARAM)
  341. */
  342. out_be32(&ifc->ifc_nand.nand_fbcr, 256);
  343. ifc_nand_ctrl->read_bytes = 256;
  344. set_addr(mtd, 0, 0, 0);
  345. fsl_ifc_run_command(mtd);
  346. return;
  347. }
  348. /* ERASE1 stores the block and page address */
  349. case NAND_CMD_ERASE1:
  350. set_addr(mtd, 0, page_addr, 0);
  351. return;
  352. /* ERASE2 uses the block and page address from ERASE1 */
  353. case NAND_CMD_ERASE2:
  354. out_be32(&ifc->ifc_nand.nand_fir0,
  355. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  356. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  357. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT));
  358. out_be32(&ifc->ifc_nand.nand_fcr0,
  359. (NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
  360. (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT));
  361. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  362. ifc_nand_ctrl->read_bytes = 0;
  363. fsl_ifc_run_command(mtd);
  364. return;
  365. /* SEQIN sets up the addr buffer and all registers except the length */
  366. case NAND_CMD_SEQIN: {
  367. u32 nand_fcr0;
  368. ifc_nand_ctrl->column = column;
  369. ifc_nand_ctrl->oob = 0;
  370. if (mtd->writesize > 512) {
  371. nand_fcr0 =
  372. (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
  373. (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD1_SHIFT);
  374. out_be32(&ifc->ifc_nand.nand_fir0,
  375. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  376. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  377. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  378. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) |
  379. (IFC_FIR_OP_CW1 << IFC_NAND_FIR0_OP4_SHIFT));
  380. } else {
  381. nand_fcr0 = ((NAND_CMD_PAGEPROG <<
  382. IFC_NAND_FCR0_CMD1_SHIFT) |
  383. (NAND_CMD_SEQIN <<
  384. IFC_NAND_FCR0_CMD2_SHIFT));
  385. out_be32(&ifc->ifc_nand.nand_fir0,
  386. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  387. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
  388. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  389. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
  390. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT));
  391. out_be32(&ifc->ifc_nand.nand_fir1,
  392. (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT));
  393. if (column >= mtd->writesize)
  394. nand_fcr0 |=
  395. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
  396. else
  397. nand_fcr0 |=
  398. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
  399. }
  400. if (column >= mtd->writesize) {
  401. /* OOB area --> READOOB */
  402. column -= mtd->writesize;
  403. ifc_nand_ctrl->oob = 1;
  404. }
  405. out_be32(&ifc->ifc_nand.nand_fcr0, nand_fcr0);
  406. set_addr(mtd, column, page_addr, ifc_nand_ctrl->oob);
  407. return;
  408. }
  409. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  410. case NAND_CMD_PAGEPROG: {
  411. if (ifc_nand_ctrl->oob) {
  412. out_be32(&ifc->ifc_nand.nand_fbcr,
  413. ifc_nand_ctrl->index - ifc_nand_ctrl->column);
  414. } else {
  415. out_be32(&ifc->ifc_nand.nand_fbcr, 0);
  416. }
  417. fsl_ifc_run_command(mtd);
  418. return;
  419. }
  420. case NAND_CMD_STATUS:
  421. out_be32(&ifc->ifc_nand.nand_fir0,
  422. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  423. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT));
  424. out_be32(&ifc->ifc_nand.nand_fcr0,
  425. NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT);
  426. out_be32(&ifc->ifc_nand.nand_fbcr, 1);
  427. set_addr(mtd, 0, 0, 0);
  428. ifc_nand_ctrl->read_bytes = 1;
  429. fsl_ifc_run_command(mtd);
  430. /*
  431. * The chip always seems to report that it is
  432. * write-protected, even when it is not.
  433. */
  434. setbits8(ifc_nand_ctrl->addr, NAND_STATUS_WP);
  435. return;
  436. case NAND_CMD_RESET:
  437. out_be32(&ifc->ifc_nand.nand_fir0,
  438. IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT);
  439. out_be32(&ifc->ifc_nand.nand_fcr0,
  440. NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT);
  441. fsl_ifc_run_command(mtd);
  442. return;
  443. default:
  444. dev_err(priv->dev, "%s: error, unsupported command 0x%x.\n",
  445. __func__, command);
  446. }
  447. }
  448. static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
  449. {
  450. /* The hardware does not seem to support multiple
  451. * chips per bank.
  452. */
  453. }
  454. /*
  455. * Write buf to the IFC NAND Controller Data Buffer
  456. */
  457. static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  458. {
  459. struct nand_chip *chip = mtd->priv;
  460. struct fsl_ifc_mtd *priv = chip->priv;
  461. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  462. if (len <= 0) {
  463. dev_err(priv->dev, "%s: len %d bytes", __func__, len);
  464. return;
  465. }
  466. if ((unsigned int)len > bufsize - ifc_nand_ctrl->index) {
  467. dev_err(priv->dev,
  468. "%s: beyond end of buffer (%d requested, %u available)\n",
  469. __func__, len, bufsize - ifc_nand_ctrl->index);
  470. len = bufsize - ifc_nand_ctrl->index;
  471. }
  472. memcpy_toio(&ifc_nand_ctrl->addr[ifc_nand_ctrl->index], buf, len);
  473. ifc_nand_ctrl->index += len;
  474. }
  475. /*
  476. * Read a byte from either the IFC hardware buffer
  477. * read function for 8-bit buswidth
  478. */
  479. static uint8_t fsl_ifc_read_byte(struct mtd_info *mtd)
  480. {
  481. struct nand_chip *chip = mtd->priv;
  482. struct fsl_ifc_mtd *priv = chip->priv;
  483. /*
  484. * If there are still bytes in the IFC buffer, then use the
  485. * next byte.
  486. */
  487. if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes)
  488. return in_8(&ifc_nand_ctrl->addr[ifc_nand_ctrl->index++]);
  489. dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
  490. return ERR_BYTE;
  491. }
  492. /*
  493. * Read two bytes from the IFC hardware buffer
  494. * read function for 16-bit buswith
  495. */
  496. static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
  497. {
  498. struct nand_chip *chip = mtd->priv;
  499. struct fsl_ifc_mtd *priv = chip->priv;
  500. uint16_t data;
  501. /*
  502. * If there are still bytes in the IFC buffer, then use the
  503. * next byte.
  504. */
  505. if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
  506. data = in_be16((uint16_t *)&ifc_nand_ctrl->
  507. addr[ifc_nand_ctrl->index]);
  508. ifc_nand_ctrl->index += 2;
  509. return (uint8_t) data;
  510. }
  511. dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
  512. return ERR_BYTE;
  513. }
  514. /*
  515. * Read from the IFC Controller Data Buffer
  516. */
  517. static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  518. {
  519. struct nand_chip *chip = mtd->priv;
  520. struct fsl_ifc_mtd *priv = chip->priv;
  521. int avail;
  522. if (len < 0) {
  523. dev_err(priv->dev, "%s: len %d bytes", __func__, len);
  524. return;
  525. }
  526. avail = min((unsigned int)len,
  527. ifc_nand_ctrl->read_bytes - ifc_nand_ctrl->index);
  528. memcpy_fromio(buf, &ifc_nand_ctrl->addr[ifc_nand_ctrl->index], avail);
  529. ifc_nand_ctrl->index += avail;
  530. if (len > avail)
  531. dev_err(priv->dev,
  532. "%s: beyond end of buffer (%d requested, %d available)\n",
  533. __func__, len, avail);
  534. }
  535. /*
  536. * Verify buffer against the IFC Controller Data Buffer
  537. */
  538. static int fsl_ifc_verify_buf(struct mtd_info *mtd,
  539. const u_char *buf, int len)
  540. {
  541. struct nand_chip *chip = mtd->priv;
  542. struct fsl_ifc_mtd *priv = chip->priv;
  543. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  544. struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
  545. int i;
  546. if (len < 0) {
  547. dev_err(priv->dev, "%s: write_buf of %d bytes", __func__, len);
  548. return -EINVAL;
  549. }
  550. if ((unsigned int)len > nctrl->read_bytes - nctrl->index) {
  551. dev_err(priv->dev,
  552. "%s: beyond end of buffer (%d requested, %u available)\n",
  553. __func__, len, nctrl->read_bytes - nctrl->index);
  554. nctrl->index = nctrl->read_bytes;
  555. return -EINVAL;
  556. }
  557. for (i = 0; i < len; i++)
  558. if (in_8(&nctrl->addr[nctrl->index + i]) != buf[i])
  559. break;
  560. nctrl->index += len;
  561. if (i != len)
  562. return -EIO;
  563. if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
  564. return -EIO;
  565. return 0;
  566. }
  567. /*
  568. * This function is called after Program and Erase Operations to
  569. * check for success or failure.
  570. */
  571. static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  572. {
  573. struct fsl_ifc_mtd *priv = chip->priv;
  574. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  575. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  576. u32 nand_fsr;
  577. /* Use READ_STATUS command, but wait for the device to be ready */
  578. out_be32(&ifc->ifc_nand.nand_fir0,
  579. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  580. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT));
  581. out_be32(&ifc->ifc_nand.nand_fcr0, NAND_CMD_STATUS <<
  582. IFC_NAND_FCR0_CMD0_SHIFT);
  583. out_be32(&ifc->ifc_nand.nand_fbcr, 1);
  584. set_addr(mtd, 0, 0, 0);
  585. ifc_nand_ctrl->read_bytes = 1;
  586. fsl_ifc_run_command(mtd);
  587. nand_fsr = in_be32(&ifc->ifc_nand.nand_fsr);
  588. /*
  589. * The chip always seems to report that it is
  590. * write-protected, even when it is not.
  591. */
  592. return nand_fsr | NAND_STATUS_WP;
  593. }
  594. static int fsl_ifc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  595. uint8_t *buf, int oob_required, int page)
  596. {
  597. struct fsl_ifc_mtd *priv = chip->priv;
  598. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  599. struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
  600. fsl_ifc_read_buf(mtd, buf, mtd->writesize);
  601. if (oob_required)
  602. fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  603. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_ECCER)
  604. dev_err(priv->dev, "NAND Flash ECC Uncorrectable Error\n");
  605. if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
  606. mtd->ecc_stats.failed++;
  607. return nctrl->max_bitflips;
  608. }
  609. /* ECC will be calculated automatically, and errors will be detected in
  610. * waitfunc.
  611. */
  612. static void fsl_ifc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  613. const uint8_t *buf, int oob_required)
  614. {
  615. fsl_ifc_write_buf(mtd, buf, mtd->writesize);
  616. fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  617. }
  618. static int fsl_ifc_chip_init_tail(struct mtd_info *mtd)
  619. {
  620. struct nand_chip *chip = mtd->priv;
  621. struct fsl_ifc_mtd *priv = chip->priv;
  622. dev_dbg(priv->dev, "%s: nand->numchips = %d\n", __func__,
  623. chip->numchips);
  624. dev_dbg(priv->dev, "%s: nand->chipsize = %lld\n", __func__,
  625. chip->chipsize);
  626. dev_dbg(priv->dev, "%s: nand->pagemask = %8x\n", __func__,
  627. chip->pagemask);
  628. dev_dbg(priv->dev, "%s: nand->chip_delay = %d\n", __func__,
  629. chip->chip_delay);
  630. dev_dbg(priv->dev, "%s: nand->badblockpos = %d\n", __func__,
  631. chip->badblockpos);
  632. dev_dbg(priv->dev, "%s: nand->chip_shift = %d\n", __func__,
  633. chip->chip_shift);
  634. dev_dbg(priv->dev, "%s: nand->page_shift = %d\n", __func__,
  635. chip->page_shift);
  636. dev_dbg(priv->dev, "%s: nand->phys_erase_shift = %d\n", __func__,
  637. chip->phys_erase_shift);
  638. dev_dbg(priv->dev, "%s: nand->ecclayout = %p\n", __func__,
  639. chip->ecclayout);
  640. dev_dbg(priv->dev, "%s: nand->ecc.mode = %d\n", __func__,
  641. chip->ecc.mode);
  642. dev_dbg(priv->dev, "%s: nand->ecc.steps = %d\n", __func__,
  643. chip->ecc.steps);
  644. dev_dbg(priv->dev, "%s: nand->ecc.bytes = %d\n", __func__,
  645. chip->ecc.bytes);
  646. dev_dbg(priv->dev, "%s: nand->ecc.total = %d\n", __func__,
  647. chip->ecc.total);
  648. dev_dbg(priv->dev, "%s: nand->ecc.layout = %p\n", __func__,
  649. chip->ecc.layout);
  650. dev_dbg(priv->dev, "%s: mtd->flags = %08x\n", __func__, mtd->flags);
  651. dev_dbg(priv->dev, "%s: mtd->size = %lld\n", __func__, mtd->size);
  652. dev_dbg(priv->dev, "%s: mtd->erasesize = %d\n", __func__,
  653. mtd->erasesize);
  654. dev_dbg(priv->dev, "%s: mtd->writesize = %d\n", __func__,
  655. mtd->writesize);
  656. dev_dbg(priv->dev, "%s: mtd->oobsize = %d\n", __func__,
  657. mtd->oobsize);
  658. return 0;
  659. }
  660. static void fsl_ifc_sram_init(struct fsl_ifc_mtd *priv)
  661. {
  662. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  663. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  664. uint32_t csor = 0, csor_8k = 0, csor_ext = 0;
  665. uint32_t cs = priv->bank;
  666. /* Save CSOR and CSOR_ext */
  667. csor = in_be32(&ifc->csor_cs[cs].csor);
  668. csor_ext = in_be32(&ifc->csor_cs[cs].csor_ext);
  669. /* chage PageSize 8K and SpareSize 1K*/
  670. csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
  671. out_be32(&ifc->csor_cs[cs].csor, csor_8k);
  672. out_be32(&ifc->csor_cs[cs].csor_ext, 0x0000400);
  673. /* READID */
  674. out_be32(&ifc->ifc_nand.nand_fir0,
  675. (IFC_FIR_OP_CMD0 << IFC_NAND_FIR0_OP0_SHIFT) |
  676. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  677. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT));
  678. out_be32(&ifc->ifc_nand.nand_fcr0,
  679. NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT);
  680. out_be32(&ifc->ifc_nand.row3, 0x0);
  681. out_be32(&ifc->ifc_nand.nand_fbcr, 0x0);
  682. /* Program ROW0/COL0 */
  683. out_be32(&ifc->ifc_nand.row0, 0x0);
  684. out_be32(&ifc->ifc_nand.col0, 0x0);
  685. /* set the chip select for NAND Transaction */
  686. out_be32(&ifc->ifc_nand.nand_csel, cs << IFC_NAND_CSEL_SHIFT);
  687. /* start read seq */
  688. out_be32(&ifc->ifc_nand.nandseq_strt, IFC_NAND_SEQ_STRT_FIR_STRT);
  689. /* wait for command complete flag or timeout */
  690. wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
  691. IFC_TIMEOUT_MSECS * HZ/1000);
  692. if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
  693. printk(KERN_ERR "fsl-ifc: Failed to Initialise SRAM\n");
  694. /* Restore CSOR and CSOR_ext */
  695. out_be32(&ifc->csor_cs[cs].csor, csor);
  696. out_be32(&ifc->csor_cs[cs].csor_ext, csor_ext);
  697. }
  698. static int fsl_ifc_chip_init(struct fsl_ifc_mtd *priv)
  699. {
  700. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  701. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  702. struct nand_chip *chip = &priv->chip;
  703. struct nand_ecclayout *layout;
  704. u32 csor, ver;
  705. /* Fill in fsl_ifc_mtd structure */
  706. priv->mtd.priv = chip;
  707. priv->mtd.owner = THIS_MODULE;
  708. /* fill in nand_chip structure */
  709. /* set up function call table */
  710. if ((in_be32(&ifc->cspr_cs[priv->bank].cspr)) & CSPR_PORT_SIZE_16)
  711. chip->read_byte = fsl_ifc_read_byte16;
  712. else
  713. chip->read_byte = fsl_ifc_read_byte;
  714. chip->write_buf = fsl_ifc_write_buf;
  715. chip->read_buf = fsl_ifc_read_buf;
  716. chip->verify_buf = fsl_ifc_verify_buf;
  717. chip->select_chip = fsl_ifc_select_chip;
  718. chip->cmdfunc = fsl_ifc_cmdfunc;
  719. chip->waitfunc = fsl_ifc_wait;
  720. chip->bbt_td = &bbt_main_descr;
  721. chip->bbt_md = &bbt_mirror_descr;
  722. out_be32(&ifc->ifc_nand.ncfgr, 0x0);
  723. /* set up nand options */
  724. chip->options = NAND_NO_READRDY;
  725. chip->bbt_options = NAND_BBT_USE_FLASH;
  726. if (in_be32(&ifc->cspr_cs[priv->bank].cspr) & CSPR_PORT_SIZE_16) {
  727. chip->read_byte = fsl_ifc_read_byte16;
  728. chip->options |= NAND_BUSWIDTH_16;
  729. } else {
  730. chip->read_byte = fsl_ifc_read_byte;
  731. }
  732. chip->controller = &ifc_nand_ctrl->controller;
  733. chip->priv = priv;
  734. chip->ecc.read_page = fsl_ifc_read_page;
  735. chip->ecc.write_page = fsl_ifc_write_page;
  736. csor = in_be32(&ifc->csor_cs[priv->bank].csor);
  737. /* Hardware generates ECC per 512 Bytes */
  738. chip->ecc.size = 512;
  739. chip->ecc.bytes = 8;
  740. chip->ecc.strength = 4;
  741. switch (csor & CSOR_NAND_PGS_MASK) {
  742. case CSOR_NAND_PGS_512:
  743. if (chip->options & NAND_BUSWIDTH_16) {
  744. layout = &oob_512_16bit_ecc4;
  745. } else {
  746. layout = &oob_512_8bit_ecc4;
  747. /* Avoid conflict with bad block marker */
  748. bbt_main_descr.offs = 0;
  749. bbt_mirror_descr.offs = 0;
  750. }
  751. priv->bufnum_mask = 15;
  752. break;
  753. case CSOR_NAND_PGS_2K:
  754. layout = &oob_2048_ecc4;
  755. priv->bufnum_mask = 3;
  756. break;
  757. case CSOR_NAND_PGS_4K:
  758. if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
  759. CSOR_NAND_ECC_MODE_4) {
  760. layout = &oob_4096_ecc4;
  761. } else {
  762. layout = &oob_4096_ecc8;
  763. chip->ecc.bytes = 16;
  764. }
  765. priv->bufnum_mask = 1;
  766. break;
  767. default:
  768. dev_err(priv->dev, "bad csor %#x: bad page size\n", csor);
  769. return -ENODEV;
  770. }
  771. /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
  772. if (csor & CSOR_NAND_ECC_DEC_EN) {
  773. chip->ecc.mode = NAND_ECC_HW;
  774. chip->ecc.layout = layout;
  775. } else {
  776. chip->ecc.mode = NAND_ECC_SOFT;
  777. }
  778. ver = in_be32(&ifc->ifc_rev);
  779. if (ver == FSL_IFC_V1_1_0)
  780. fsl_ifc_sram_init(priv);
  781. return 0;
  782. }
  783. static int fsl_ifc_chip_remove(struct fsl_ifc_mtd *priv)
  784. {
  785. nand_release(&priv->mtd);
  786. kfree(priv->mtd.name);
  787. if (priv->vbase)
  788. iounmap(priv->vbase);
  789. ifc_nand_ctrl->chips[priv->bank] = NULL;
  790. dev_set_drvdata(priv->dev, NULL);
  791. kfree(priv);
  792. return 0;
  793. }
  794. static int match_bank(struct fsl_ifc_regs __iomem *ifc, int bank,
  795. phys_addr_t addr)
  796. {
  797. u32 cspr = in_be32(&ifc->cspr_cs[bank].cspr);
  798. if (!(cspr & CSPR_V))
  799. return 0;
  800. if ((cspr & CSPR_MSEL) != CSPR_MSEL_NAND)
  801. return 0;
  802. return (cspr & CSPR_BA) == convert_ifc_address(addr);
  803. }
  804. static DEFINE_MUTEX(fsl_ifc_nand_mutex);
  805. static int __devinit fsl_ifc_nand_probe(struct platform_device *dev)
  806. {
  807. struct fsl_ifc_regs __iomem *ifc;
  808. struct fsl_ifc_mtd *priv;
  809. struct resource res;
  810. static const char *part_probe_types[]
  811. = { "cmdlinepart", "RedBoot", "ofpart", NULL };
  812. int ret;
  813. int bank;
  814. struct device_node *node = dev->dev.of_node;
  815. struct mtd_part_parser_data ppdata;
  816. ppdata.of_node = dev->dev.of_node;
  817. if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->regs)
  818. return -ENODEV;
  819. ifc = fsl_ifc_ctrl_dev->regs;
  820. /* get, allocate and map the memory resource */
  821. ret = of_address_to_resource(node, 0, &res);
  822. if (ret) {
  823. dev_err(&dev->dev, "%s: failed to get resource\n", __func__);
  824. return ret;
  825. }
  826. /* find which chip select it is connected to */
  827. for (bank = 0; bank < FSL_IFC_BANK_COUNT; bank++) {
  828. if (match_bank(ifc, bank, res.start))
  829. break;
  830. }
  831. if (bank >= FSL_IFC_BANK_COUNT) {
  832. dev_err(&dev->dev, "%s: address did not match any chip selects\n",
  833. __func__);
  834. return -ENODEV;
  835. }
  836. priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
  837. if (!priv)
  838. return -ENOMEM;
  839. mutex_lock(&fsl_ifc_nand_mutex);
  840. if (!fsl_ifc_ctrl_dev->nand) {
  841. ifc_nand_ctrl = kzalloc(sizeof(*ifc_nand_ctrl), GFP_KERNEL);
  842. if (!ifc_nand_ctrl) {
  843. dev_err(&dev->dev, "failed to allocate memory\n");
  844. mutex_unlock(&fsl_ifc_nand_mutex);
  845. return -ENOMEM;
  846. }
  847. ifc_nand_ctrl->read_bytes = 0;
  848. ifc_nand_ctrl->index = 0;
  849. ifc_nand_ctrl->addr = NULL;
  850. fsl_ifc_ctrl_dev->nand = ifc_nand_ctrl;
  851. spin_lock_init(&ifc_nand_ctrl->controller.lock);
  852. init_waitqueue_head(&ifc_nand_ctrl->controller.wq);
  853. } else {
  854. ifc_nand_ctrl = fsl_ifc_ctrl_dev->nand;
  855. }
  856. mutex_unlock(&fsl_ifc_nand_mutex);
  857. ifc_nand_ctrl->chips[bank] = priv;
  858. priv->bank = bank;
  859. priv->ctrl = fsl_ifc_ctrl_dev;
  860. priv->dev = &dev->dev;
  861. priv->vbase = ioremap(res.start, resource_size(&res));
  862. if (!priv->vbase) {
  863. dev_err(priv->dev, "%s: failed to map chip region\n", __func__);
  864. ret = -ENOMEM;
  865. goto err;
  866. }
  867. dev_set_drvdata(priv->dev, priv);
  868. out_be32(&ifc->ifc_nand.nand_evter_en,
  869. IFC_NAND_EVTER_EN_OPC_EN |
  870. IFC_NAND_EVTER_EN_FTOER_EN |
  871. IFC_NAND_EVTER_EN_WPER_EN);
  872. /* enable NAND Machine Interrupts */
  873. out_be32(&ifc->ifc_nand.nand_evter_intr_en,
  874. IFC_NAND_EVTER_INTR_OPCIR_EN |
  875. IFC_NAND_EVTER_INTR_FTOERIR_EN |
  876. IFC_NAND_EVTER_INTR_WPERIR_EN);
  877. priv->mtd.name = kasprintf(GFP_KERNEL, "%x.flash", (unsigned)res.start);
  878. if (!priv->mtd.name) {
  879. ret = -ENOMEM;
  880. goto err;
  881. }
  882. ret = fsl_ifc_chip_init(priv);
  883. if (ret)
  884. goto err;
  885. ret = nand_scan_ident(&priv->mtd, 1, NULL);
  886. if (ret)
  887. goto err;
  888. ret = fsl_ifc_chip_init_tail(&priv->mtd);
  889. if (ret)
  890. goto err;
  891. ret = nand_scan_tail(&priv->mtd);
  892. if (ret)
  893. goto err;
  894. /* First look for RedBoot table or partitions on the command
  895. * line, these take precedence over device tree information */
  896. mtd_device_parse_register(&priv->mtd, part_probe_types, &ppdata,
  897. NULL, 0);
  898. dev_info(priv->dev, "IFC NAND device at 0x%llx, bank %d\n",
  899. (unsigned long long)res.start, priv->bank);
  900. return 0;
  901. err:
  902. fsl_ifc_chip_remove(priv);
  903. return ret;
  904. }
  905. static int fsl_ifc_nand_remove(struct platform_device *dev)
  906. {
  907. struct fsl_ifc_mtd *priv = dev_get_drvdata(&dev->dev);
  908. fsl_ifc_chip_remove(priv);
  909. mutex_lock(&fsl_ifc_nand_mutex);
  910. ifc_nand_ctrl->counter--;
  911. if (!ifc_nand_ctrl->counter) {
  912. fsl_ifc_ctrl_dev->nand = NULL;
  913. kfree(ifc_nand_ctrl);
  914. }
  915. mutex_unlock(&fsl_ifc_nand_mutex);
  916. return 0;
  917. }
  918. static const struct of_device_id fsl_ifc_nand_match[] = {
  919. {
  920. .compatible = "fsl,ifc-nand",
  921. },
  922. {}
  923. };
  924. static struct platform_driver fsl_ifc_nand_driver = {
  925. .driver = {
  926. .name = "fsl,ifc-nand",
  927. .owner = THIS_MODULE,
  928. .of_match_table = fsl_ifc_nand_match,
  929. },
  930. .probe = fsl_ifc_nand_probe,
  931. .remove = fsl_ifc_nand_remove,
  932. };
  933. static int __init fsl_ifc_nand_init(void)
  934. {
  935. int ret;
  936. ret = platform_driver_register(&fsl_ifc_nand_driver);
  937. if (ret)
  938. printk(KERN_ERR "fsl-ifc: Failed to register platform"
  939. "driver\n");
  940. return ret;
  941. }
  942. static void __exit fsl_ifc_nand_exit(void)
  943. {
  944. platform_driver_unregister(&fsl_ifc_nand_driver);
  945. }
  946. module_init(fsl_ifc_nand_init);
  947. module_exit(fsl_ifc_nand_exit);
  948. MODULE_LICENSE("GPL");
  949. MODULE_AUTHOR("Freescale");
  950. MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver");