fsl_ifc_nand.c 30 KB

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