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