fsl_ifc_nand.c 31 KB

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