fsl_ifc_nand.c 33 KB

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