fsl_ifc_nand.c 33 KB

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