fsl_ifc_nand.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 nand_chip *chip, unsigned int command,
  257. int column, int page_addr) {
  258. struct mtd_info *mtd = nand_to_mtd(chip);
  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 nand_chip *chip, int cs)
  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 nand_chip *chip, const u8 *buf, int len)
  442. {
  443. struct mtd_info *mtd = nand_to_mtd(chip);
  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 nand_chip *chip)
  464. {
  465. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  466. unsigned int offset;
  467. /*
  468. * If there are still bytes in the IFC buffer, then use the
  469. * next byte.
  470. */
  471. if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
  472. offset = ifc_nand_ctrl->index++;
  473. return ifc_in8(ifc_nand_ctrl->addr + offset);
  474. }
  475. dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
  476. return ERR_BYTE;
  477. }
  478. /*
  479. * Read two bytes from the IFC hardware buffer
  480. * read function for 16-bit buswith
  481. */
  482. static uint8_t fsl_ifc_read_byte16(struct nand_chip *chip)
  483. {
  484. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  485. uint16_t data;
  486. /*
  487. * If there are still bytes in the IFC buffer, then use the
  488. * next byte.
  489. */
  490. if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
  491. data = ifc_in16(ifc_nand_ctrl->addr + ifc_nand_ctrl->index);
  492. ifc_nand_ctrl->index += 2;
  493. return (uint8_t) data;
  494. }
  495. dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
  496. return ERR_BYTE;
  497. }
  498. /*
  499. * Read from the IFC Controller Data Buffer
  500. */
  501. static void fsl_ifc_read_buf(struct nand_chip *chip, u8 *buf, int len)
  502. {
  503. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  504. int avail;
  505. if (len < 0) {
  506. dev_err(priv->dev, "%s: len %d bytes", __func__, len);
  507. return;
  508. }
  509. avail = min((unsigned int)len,
  510. ifc_nand_ctrl->read_bytes - ifc_nand_ctrl->index);
  511. memcpy_fromio(buf, ifc_nand_ctrl->addr + ifc_nand_ctrl->index, avail);
  512. ifc_nand_ctrl->index += avail;
  513. if (len > avail)
  514. dev_err(priv->dev,
  515. "%s: beyond end of buffer (%d requested, %d available)\n",
  516. __func__, len, avail);
  517. }
  518. /*
  519. * This function is called after Program and Erase Operations to
  520. * check for success or failure.
  521. */
  522. static int fsl_ifc_wait(struct nand_chip *chip)
  523. {
  524. struct mtd_info *mtd = nand_to_mtd(chip);
  525. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  526. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  527. struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
  528. u32 nand_fsr;
  529. int status;
  530. /* Use READ_STATUS command, but wait for the device to be ready */
  531. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  532. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT),
  533. &ifc->ifc_nand.nand_fir0);
  534. ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
  535. &ifc->ifc_nand.nand_fcr0);
  536. ifc_out32(1, &ifc->ifc_nand.nand_fbcr);
  537. set_addr(mtd, 0, 0, 0);
  538. ifc_nand_ctrl->read_bytes = 1;
  539. fsl_ifc_run_command(mtd);
  540. nand_fsr = ifc_in32(&ifc->ifc_nand.nand_fsr);
  541. status = nand_fsr >> 24;
  542. /*
  543. * The chip always seems to report that it is
  544. * write-protected, even when it is not.
  545. */
  546. return status | NAND_STATUS_WP;
  547. }
  548. /*
  549. * The controller does not check for bitflips in erased pages,
  550. * therefore software must check instead.
  551. */
  552. static int check_erased_page(struct nand_chip *chip, u8 *buf)
  553. {
  554. struct mtd_info *mtd = nand_to_mtd(chip);
  555. u8 *ecc = chip->oob_poi;
  556. const int ecc_size = chip->ecc.bytes;
  557. const int pkt_size = chip->ecc.size;
  558. int i, res, bitflips = 0;
  559. struct mtd_oob_region oobregion = { };
  560. mtd_ooblayout_ecc(mtd, 0, &oobregion);
  561. ecc += oobregion.offset;
  562. for (i = 0; i < chip->ecc.steps; ++i) {
  563. res = nand_check_erased_ecc_chunk(buf, pkt_size, ecc, ecc_size,
  564. NULL, 0,
  565. chip->ecc.strength);
  566. if (res < 0)
  567. mtd->ecc_stats.failed++;
  568. else
  569. mtd->ecc_stats.corrected += res;
  570. bitflips = max(res, bitflips);
  571. buf += pkt_size;
  572. ecc += ecc_size;
  573. }
  574. return bitflips;
  575. }
  576. static int fsl_ifc_read_page(struct nand_chip *chip, uint8_t *buf,
  577. int oob_required, int page)
  578. {
  579. struct mtd_info *mtd = nand_to_mtd(chip);
  580. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  581. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  582. struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
  583. nand_read_page_op(chip, page, 0, buf, mtd->writesize);
  584. if (oob_required)
  585. fsl_ifc_read_buf(chip, chip->oob_poi, mtd->oobsize);
  586. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_ECCER) {
  587. if (!oob_required)
  588. fsl_ifc_read_buf(chip, chip->oob_poi, mtd->oobsize);
  589. return check_erased_page(chip, buf);
  590. }
  591. if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
  592. mtd->ecc_stats.failed++;
  593. return nctrl->max_bitflips;
  594. }
  595. /* ECC will be calculated automatically, and errors will be detected in
  596. * waitfunc.
  597. */
  598. static int fsl_ifc_write_page(struct nand_chip *chip, const uint8_t *buf,
  599. int oob_required, int page)
  600. {
  601. struct mtd_info *mtd = nand_to_mtd(chip);
  602. nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
  603. fsl_ifc_write_buf(chip, 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->legacy.chip_delay = %d\n", __func__,
  617. chip->legacy.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->legacy.read_byte = fsl_ifc_read_byte16;
  726. else
  727. chip->legacy.read_byte = fsl_ifc_read_byte;
  728. chip->legacy.write_buf = fsl_ifc_write_buf;
  729. chip->legacy.read_buf = fsl_ifc_read_buf;
  730. chip->select_chip = fsl_ifc_select_chip;
  731. chip->legacy.cmdfunc = fsl_ifc_cmdfunc;
  732. chip->legacy.waitfunc = fsl_ifc_wait;
  733. chip->legacy.set_features = nand_get_set_features_notsupp;
  734. chip->legacy.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->legacy.read_byte = fsl_ifc_read_byte16;
  744. chip->options |= NAND_BUSWIDTH_16;
  745. } else {
  746. chip->legacy.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");