fsl_ifc_nand.c 30 KB

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