fsl_ifc_nand.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. ifc_out32(page_addr, &ifc->ifc_nand.row0);
  222. ifc_out32((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. ifc_out32(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. ifc_in32(&ifc->ifc_nand.nand_fir0),
  277. ifc_in32(&ifc->ifc_nand.nand_fcr0));
  278. ctrl->nand_stat = 0;
  279. /* start read/write seq */
  280. ifc_out32(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] = ifc_in32(&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. ifc_out32((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. ifc_out32(0x0, &ifc->ifc_nand.nand_fir1);
  339. ifc_out32((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. ifc_out32((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. ifc_out32(0x0, &ifc->ifc_nand.nand_fir1);
  349. if (oob)
  350. ifc_out32(NAND_CMD_READOOB <<
  351. IFC_NAND_FCR0_CMD0_SHIFT,
  352. &ifc->ifc_nand.nand_fcr0);
  353. else
  354. ifc_out32(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. ifc_out32(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. ifc_out32(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. ifc_out32((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. ifc_out32(command << IFC_NAND_FCR0_CMD0_SHIFT,
  400. &ifc->ifc_nand.nand_fcr0);
  401. ifc_out32(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. ifc_out32(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. ifc_out32((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. ifc_out32((NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
  423. (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT),
  424. &ifc->ifc_nand.nand_fcr0);
  425. ifc_out32(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. ifc_out32(
  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. ifc_out32(
  447. (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT) |
  448. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP6_SHIFT) |
  449. (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP7_SHIFT),
  450. &ifc->ifc_nand.nand_fir1);
  451. } else {
  452. nand_fcr0 = ((NAND_CMD_PAGEPROG <<
  453. IFC_NAND_FCR0_CMD1_SHIFT) |
  454. (NAND_CMD_SEQIN <<
  455. IFC_NAND_FCR0_CMD2_SHIFT) |
  456. (NAND_CMD_STATUS <<
  457. IFC_NAND_FCR0_CMD3_SHIFT));
  458. ifc_out32(
  459. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  460. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
  461. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  462. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
  463. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT),
  464. &ifc->ifc_nand.nand_fir0);
  465. ifc_out32(
  466. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR1_OP5_SHIFT) |
  467. (IFC_FIR_OP_CW3 << IFC_NAND_FIR1_OP6_SHIFT) |
  468. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP7_SHIFT) |
  469. (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP8_SHIFT),
  470. &ifc->ifc_nand.nand_fir1);
  471. if (column >= mtd->writesize)
  472. nand_fcr0 |=
  473. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
  474. else
  475. nand_fcr0 |=
  476. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
  477. }
  478. if (column >= mtd->writesize) {
  479. /* OOB area --> READOOB */
  480. column -= mtd->writesize;
  481. ifc_nand_ctrl->oob = 1;
  482. }
  483. ifc_out32(nand_fcr0, &ifc->ifc_nand.nand_fcr0);
  484. set_addr(mtd, column, page_addr, ifc_nand_ctrl->oob);
  485. return;
  486. }
  487. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  488. case NAND_CMD_PAGEPROG: {
  489. if (ifc_nand_ctrl->oob) {
  490. ifc_out32(ifc_nand_ctrl->index -
  491. ifc_nand_ctrl->column,
  492. &ifc->ifc_nand.nand_fbcr);
  493. } else {
  494. ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
  495. }
  496. fsl_ifc_run_command(mtd);
  497. return;
  498. }
  499. case NAND_CMD_STATUS: {
  500. void __iomem *addr;
  501. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  502. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT),
  503. &ifc->ifc_nand.nand_fir0);
  504. ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
  505. &ifc->ifc_nand.nand_fcr0);
  506. ifc_out32(1, &ifc->ifc_nand.nand_fbcr);
  507. set_addr(mtd, 0, 0, 0);
  508. ifc_nand_ctrl->read_bytes = 1;
  509. fsl_ifc_run_command(mtd);
  510. /*
  511. * The chip always seems to report that it is
  512. * write-protected, even when it is not.
  513. */
  514. addr = ifc_nand_ctrl->addr;
  515. if (chip->options & NAND_BUSWIDTH_16)
  516. ifc_out16(ifc_in16(addr) | (NAND_STATUS_WP), addr);
  517. else
  518. ifc_out8(ifc_in8(addr) | (NAND_STATUS_WP), addr);
  519. return;
  520. }
  521. case NAND_CMD_RESET:
  522. ifc_out32(IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT,
  523. &ifc->ifc_nand.nand_fir0);
  524. ifc_out32(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 ifc_in8(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 = ifc_in16(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. ifc_out32((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. ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
  637. &ifc->ifc_nand.nand_fcr0);
  638. ifc_out32(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 = ifc_in32(&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 = ifc_in32(&ifc->csor_cs[cs].csor);
  722. csor_ext = ifc_in32(&ifc->csor_cs[cs].csor_ext);
  723. /* chage PageSize 8K and SpareSize 1K*/
  724. csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
  725. ifc_out32(csor_8k, &ifc->csor_cs[cs].csor);
  726. ifc_out32(0x0000400, &ifc->csor_cs[cs].csor_ext);
  727. /* READID */
  728. ifc_out32((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. ifc_out32(NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT,
  733. &ifc->ifc_nand.nand_fcr0);
  734. ifc_out32(0x0, &ifc->ifc_nand.row3);
  735. ifc_out32(0x0, &ifc->ifc_nand.nand_fbcr);
  736. /* Program ROW0/COL0 */
  737. ifc_out32(0x0, &ifc->ifc_nand.row0);
  738. ifc_out32(0x0, &ifc->ifc_nand.col0);
  739. /* set the chip select for NAND Transaction */
  740. ifc_out32(cs << IFC_NAND_CSEL_SHIFT, &ifc->ifc_nand.nand_csel);
  741. /* start read seq */
  742. ifc_out32(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. msecs_to_jiffies(IFC_TIMEOUT_MSECS));
  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. ifc_out32(csor, &ifc->csor_cs[cs].csor);
  750. ifc_out32(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;
  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 ((ifc_in32(&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. ifc_out32(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 (ifc_in32(&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 = ifc_in32(&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. if (ctrl->version == FSL_IFC_VERSION_1_1_0)
  844. fsl_ifc_sram_init(priv);
  845. return 0;
  846. }
  847. static int fsl_ifc_chip_remove(struct fsl_ifc_mtd *priv)
  848. {
  849. nand_release(&priv->mtd);
  850. kfree(priv->mtd.name);
  851. if (priv->vbase)
  852. iounmap(priv->vbase);
  853. ifc_nand_ctrl->chips[priv->bank] = NULL;
  854. return 0;
  855. }
  856. static int match_bank(struct fsl_ifc_regs __iomem *ifc, int bank,
  857. phys_addr_t addr)
  858. {
  859. u32 cspr = ifc_in32(&ifc->cspr_cs[bank].cspr);
  860. if (!(cspr & CSPR_V))
  861. return 0;
  862. if ((cspr & CSPR_MSEL) != CSPR_MSEL_NAND)
  863. return 0;
  864. return (cspr & CSPR_BA) == convert_ifc_address(addr);
  865. }
  866. static DEFINE_MUTEX(fsl_ifc_nand_mutex);
  867. static int fsl_ifc_nand_probe(struct platform_device *dev)
  868. {
  869. struct fsl_ifc_regs __iomem *ifc;
  870. struct fsl_ifc_mtd *priv;
  871. struct resource res;
  872. static const char *part_probe_types[]
  873. = { "cmdlinepart", "RedBoot", "ofpart", NULL };
  874. int ret;
  875. int bank;
  876. struct device_node *node = dev->dev.of_node;
  877. struct mtd_part_parser_data ppdata;
  878. ppdata.of_node = dev->dev.of_node;
  879. if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->regs)
  880. return -ENODEV;
  881. ifc = fsl_ifc_ctrl_dev->regs;
  882. /* get, allocate and map the memory resource */
  883. ret = of_address_to_resource(node, 0, &res);
  884. if (ret) {
  885. dev_err(&dev->dev, "%s: failed to get resource\n", __func__);
  886. return ret;
  887. }
  888. /* find which chip select it is connected to */
  889. for (bank = 0; bank < fsl_ifc_ctrl_dev->banks; bank++) {
  890. if (match_bank(ifc, bank, res.start))
  891. break;
  892. }
  893. if (bank >= fsl_ifc_ctrl_dev->banks) {
  894. dev_err(&dev->dev, "%s: address did not match any chip selects\n",
  895. __func__);
  896. return -ENODEV;
  897. }
  898. priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
  899. if (!priv)
  900. return -ENOMEM;
  901. mutex_lock(&fsl_ifc_nand_mutex);
  902. if (!fsl_ifc_ctrl_dev->nand) {
  903. ifc_nand_ctrl = kzalloc(sizeof(*ifc_nand_ctrl), GFP_KERNEL);
  904. if (!ifc_nand_ctrl) {
  905. mutex_unlock(&fsl_ifc_nand_mutex);
  906. return -ENOMEM;
  907. }
  908. ifc_nand_ctrl->read_bytes = 0;
  909. ifc_nand_ctrl->index = 0;
  910. ifc_nand_ctrl->addr = NULL;
  911. fsl_ifc_ctrl_dev->nand = ifc_nand_ctrl;
  912. spin_lock_init(&ifc_nand_ctrl->controller.lock);
  913. init_waitqueue_head(&ifc_nand_ctrl->controller.wq);
  914. } else {
  915. ifc_nand_ctrl = fsl_ifc_ctrl_dev->nand;
  916. }
  917. mutex_unlock(&fsl_ifc_nand_mutex);
  918. ifc_nand_ctrl->chips[bank] = priv;
  919. priv->bank = bank;
  920. priv->ctrl = fsl_ifc_ctrl_dev;
  921. priv->dev = &dev->dev;
  922. priv->vbase = ioremap(res.start, resource_size(&res));
  923. if (!priv->vbase) {
  924. dev_err(priv->dev, "%s: failed to map chip region\n", __func__);
  925. ret = -ENOMEM;
  926. goto err;
  927. }
  928. dev_set_drvdata(priv->dev, priv);
  929. ifc_out32(IFC_NAND_EVTER_EN_OPC_EN |
  930. IFC_NAND_EVTER_EN_FTOER_EN |
  931. IFC_NAND_EVTER_EN_WPER_EN,
  932. &ifc->ifc_nand.nand_evter_en);
  933. /* enable NAND Machine Interrupts */
  934. ifc_out32(IFC_NAND_EVTER_INTR_OPCIR_EN |
  935. IFC_NAND_EVTER_INTR_FTOERIR_EN |
  936. IFC_NAND_EVTER_INTR_WPERIR_EN,
  937. &ifc->ifc_nand.nand_evter_intr_en);
  938. priv->mtd.name = kasprintf(GFP_KERNEL, "%llx.flash", (u64)res.start);
  939. if (!priv->mtd.name) {
  940. ret = -ENOMEM;
  941. goto err;
  942. }
  943. ret = fsl_ifc_chip_init(priv);
  944. if (ret)
  945. goto err;
  946. ret = nand_scan_ident(&priv->mtd, 1, NULL);
  947. if (ret)
  948. goto err;
  949. ret = fsl_ifc_chip_init_tail(&priv->mtd);
  950. if (ret)
  951. goto err;
  952. ret = nand_scan_tail(&priv->mtd);
  953. if (ret)
  954. goto err;
  955. /* First look for RedBoot table or partitions on the command
  956. * line, these take precedence over device tree information */
  957. mtd_device_parse_register(&priv->mtd, part_probe_types, &ppdata,
  958. NULL, 0);
  959. dev_info(priv->dev, "IFC NAND device at 0x%llx, bank %d\n",
  960. (unsigned long long)res.start, priv->bank);
  961. return 0;
  962. err:
  963. fsl_ifc_chip_remove(priv);
  964. return ret;
  965. }
  966. static int fsl_ifc_nand_remove(struct platform_device *dev)
  967. {
  968. struct fsl_ifc_mtd *priv = dev_get_drvdata(&dev->dev);
  969. fsl_ifc_chip_remove(priv);
  970. mutex_lock(&fsl_ifc_nand_mutex);
  971. ifc_nand_ctrl->counter--;
  972. if (!ifc_nand_ctrl->counter) {
  973. fsl_ifc_ctrl_dev->nand = NULL;
  974. kfree(ifc_nand_ctrl);
  975. }
  976. mutex_unlock(&fsl_ifc_nand_mutex);
  977. return 0;
  978. }
  979. static const struct of_device_id fsl_ifc_nand_match[] = {
  980. {
  981. .compatible = "fsl,ifc-nand",
  982. },
  983. {}
  984. };
  985. static struct platform_driver fsl_ifc_nand_driver = {
  986. .driver = {
  987. .name = "fsl,ifc-nand",
  988. .of_match_table = fsl_ifc_nand_match,
  989. },
  990. .probe = fsl_ifc_nand_probe,
  991. .remove = fsl_ifc_nand_remove,
  992. };
  993. module_platform_driver(fsl_ifc_nand_driver);
  994. MODULE_LICENSE("GPL");
  995. MODULE_AUTHOR("Freescale");
  996. MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver");