fsl_ifc_nand.c 31 KB

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