fsl_ifc_nand.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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 nand_chip chip;
  40. struct fsl_ifc_ctrl *ctrl;
  41. struct device *dev;
  42. int bank; /* Chip select bank number */
  43. unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */
  44. u8 __iomem *vbase; /* Chip select base virtual address */
  45. };
  46. /* overview of the fsl ifc controller */
  47. struct fsl_ifc_nand_ctrl {
  48. struct nand_hw_control controller;
  49. struct fsl_ifc_mtd *chips[FSL_IFC_BANK_COUNT];
  50. void __iomem *addr; /* Address of assigned IFC buffer */
  51. unsigned int page; /* Last page written to / read from */
  52. unsigned int read_bytes;/* Number of bytes read during command */
  53. unsigned int column; /* Saved column from SEQIN */
  54. unsigned int index; /* Pointer to next byte to 'read' */
  55. unsigned int oob; /* Non zero if operating on OOB data */
  56. unsigned int eccread; /* Non zero for a full-page ECC read */
  57. unsigned int counter; /* counter for the initializations */
  58. unsigned int max_bitflips; /* Saved during READ0 cmd */
  59. };
  60. static struct fsl_ifc_nand_ctrl *ifc_nand_ctrl;
  61. /* 512-byte page with 4-bit ECC, 8-bit */
  62. static struct nand_ecclayout oob_512_8bit_ecc4 = {
  63. .eccbytes = 8,
  64. .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  65. .oobfree = { {0, 5}, {6, 2} },
  66. };
  67. /* 512-byte page with 4-bit ECC, 16-bit */
  68. static struct nand_ecclayout oob_512_16bit_ecc4 = {
  69. .eccbytes = 8,
  70. .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  71. .oobfree = { {2, 6}, },
  72. };
  73. /* 2048-byte page size with 4-bit ECC */
  74. static struct nand_ecclayout oob_2048_ecc4 = {
  75. .eccbytes = 32,
  76. .eccpos = {
  77. 8, 9, 10, 11, 12, 13, 14, 15,
  78. 16, 17, 18, 19, 20, 21, 22, 23,
  79. 24, 25, 26, 27, 28, 29, 30, 31,
  80. 32, 33, 34, 35, 36, 37, 38, 39,
  81. },
  82. .oobfree = { {2, 6}, {40, 24} },
  83. };
  84. /* 4096-byte page size with 4-bit ECC */
  85. static struct nand_ecclayout oob_4096_ecc4 = {
  86. .eccbytes = 64,
  87. .eccpos = {
  88. 8, 9, 10, 11, 12, 13, 14, 15,
  89. 16, 17, 18, 19, 20, 21, 22, 23,
  90. 24, 25, 26, 27, 28, 29, 30, 31,
  91. 32, 33, 34, 35, 36, 37, 38, 39,
  92. 40, 41, 42, 43, 44, 45, 46, 47,
  93. 48, 49, 50, 51, 52, 53, 54, 55,
  94. 56, 57, 58, 59, 60, 61, 62, 63,
  95. 64, 65, 66, 67, 68, 69, 70, 71,
  96. },
  97. .oobfree = { {2, 6}, {72, 56} },
  98. };
  99. /* 4096-byte page size with 8-bit ECC -- requires 218-byte OOB */
  100. static struct nand_ecclayout oob_4096_ecc8 = {
  101. .eccbytes = 128,
  102. .eccpos = {
  103. 8, 9, 10, 11, 12, 13, 14, 15,
  104. 16, 17, 18, 19, 20, 21, 22, 23,
  105. 24, 25, 26, 27, 28, 29, 30, 31,
  106. 32, 33, 34, 35, 36, 37, 38, 39,
  107. 40, 41, 42, 43, 44, 45, 46, 47,
  108. 48, 49, 50, 51, 52, 53, 54, 55,
  109. 56, 57, 58, 59, 60, 61, 62, 63,
  110. 64, 65, 66, 67, 68, 69, 70, 71,
  111. 72, 73, 74, 75, 76, 77, 78, 79,
  112. 80, 81, 82, 83, 84, 85, 86, 87,
  113. 88, 89, 90, 91, 92, 93, 94, 95,
  114. 96, 97, 98, 99, 100, 101, 102, 103,
  115. 104, 105, 106, 107, 108, 109, 110, 111,
  116. 112, 113, 114, 115, 116, 117, 118, 119,
  117. 120, 121, 122, 123, 124, 125, 126, 127,
  118. 128, 129, 130, 131, 132, 133, 134, 135,
  119. },
  120. .oobfree = { {2, 6}, {136, 82} },
  121. };
  122. /* 8192-byte page size with 4-bit ECC */
  123. static struct nand_ecclayout oob_8192_ecc4 = {
  124. .eccbytes = 128,
  125. .eccpos = {
  126. 8, 9, 10, 11, 12, 13, 14, 15,
  127. 16, 17, 18, 19, 20, 21, 22, 23,
  128. 24, 25, 26, 27, 28, 29, 30, 31,
  129. 32, 33, 34, 35, 36, 37, 38, 39,
  130. 40, 41, 42, 43, 44, 45, 46, 47,
  131. 48, 49, 50, 51, 52, 53, 54, 55,
  132. 56, 57, 58, 59, 60, 61, 62, 63,
  133. 64, 65, 66, 67, 68, 69, 70, 71,
  134. 72, 73, 74, 75, 76, 77, 78, 79,
  135. 80, 81, 82, 83, 84, 85, 86, 87,
  136. 88, 89, 90, 91, 92, 93, 94, 95,
  137. 96, 97, 98, 99, 100, 101, 102, 103,
  138. 104, 105, 106, 107, 108, 109, 110, 111,
  139. 112, 113, 114, 115, 116, 117, 118, 119,
  140. 120, 121, 122, 123, 124, 125, 126, 127,
  141. 128, 129, 130, 131, 132, 133, 134, 135,
  142. },
  143. .oobfree = { {2, 6}, {136, 208} },
  144. };
  145. /* 8192-byte page size with 8-bit ECC -- requires 218-byte OOB */
  146. static struct nand_ecclayout oob_8192_ecc8 = {
  147. .eccbytes = 256,
  148. .eccpos = {
  149. 8, 9, 10, 11, 12, 13, 14, 15,
  150. 16, 17, 18, 19, 20, 21, 22, 23,
  151. 24, 25, 26, 27, 28, 29, 30, 31,
  152. 32, 33, 34, 35, 36, 37, 38, 39,
  153. 40, 41, 42, 43, 44, 45, 46, 47,
  154. 48, 49, 50, 51, 52, 53, 54, 55,
  155. 56, 57, 58, 59, 60, 61, 62, 63,
  156. 64, 65, 66, 67, 68, 69, 70, 71,
  157. 72, 73, 74, 75, 76, 77, 78, 79,
  158. 80, 81, 82, 83, 84, 85, 86, 87,
  159. 88, 89, 90, 91, 92, 93, 94, 95,
  160. 96, 97, 98, 99, 100, 101, 102, 103,
  161. 104, 105, 106, 107, 108, 109, 110, 111,
  162. 112, 113, 114, 115, 116, 117, 118, 119,
  163. 120, 121, 122, 123, 124, 125, 126, 127,
  164. 128, 129, 130, 131, 132, 133, 134, 135,
  165. 136, 137, 138, 139, 140, 141, 142, 143,
  166. 144, 145, 146, 147, 148, 149, 150, 151,
  167. 152, 153, 154, 155, 156, 157, 158, 159,
  168. 160, 161, 162, 163, 164, 165, 166, 167,
  169. 168, 169, 170, 171, 172, 173, 174, 175,
  170. 176, 177, 178, 179, 180, 181, 182, 183,
  171. 184, 185, 186, 187, 188, 189, 190, 191,
  172. 192, 193, 194, 195, 196, 197, 198, 199,
  173. 200, 201, 202, 203, 204, 205, 206, 207,
  174. 208, 209, 210, 211, 212, 213, 214, 215,
  175. 216, 217, 218, 219, 220, 221, 222, 223,
  176. 224, 225, 226, 227, 228, 229, 230, 231,
  177. 232, 233, 234, 235, 236, 237, 238, 239,
  178. 240, 241, 242, 243, 244, 245, 246, 247,
  179. 248, 249, 250, 251, 252, 253, 254, 255,
  180. 256, 257, 258, 259, 260, 261, 262, 263,
  181. },
  182. .oobfree = { {2, 6}, {264, 80} },
  183. };
  184. /*
  185. * Generic flash bbt descriptors
  186. */
  187. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  188. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  189. static struct nand_bbt_descr bbt_main_descr = {
  190. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  191. NAND_BBT_2BIT | NAND_BBT_VERSION,
  192. .offs = 2, /* 0 on 8-bit small page */
  193. .len = 4,
  194. .veroffs = 6,
  195. .maxblocks = 4,
  196. .pattern = bbt_pattern,
  197. };
  198. static struct nand_bbt_descr bbt_mirror_descr = {
  199. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  200. NAND_BBT_2BIT | NAND_BBT_VERSION,
  201. .offs = 2, /* 0 on 8-bit small page */
  202. .len = 4,
  203. .veroffs = 6,
  204. .maxblocks = 4,
  205. .pattern = mirror_pattern,
  206. };
  207. /*
  208. * Set up the IFC hardware block and page address fields, and the ifc nand
  209. * structure addr field to point to the correct IFC buffer in memory
  210. */
  211. static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
  212. {
  213. struct nand_chip *chip = mtd_to_nand(mtd);
  214. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  215. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  216. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  217. int buf_num;
  218. ifc_nand_ctrl->page = page_addr;
  219. /* Program ROW0/COL0 */
  220. ifc_out32(page_addr, &ifc->ifc_nand.row0);
  221. ifc_out32((oob ? IFC_NAND_COL_MS : 0) | column, &ifc->ifc_nand.col0);
  222. buf_num = page_addr & priv->bufnum_mask;
  223. ifc_nand_ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2);
  224. ifc_nand_ctrl->index = column;
  225. /* for OOB data point to the second half of the buffer */
  226. if (oob)
  227. ifc_nand_ctrl->index += mtd->writesize;
  228. }
  229. static int is_blank(struct mtd_info *mtd, unsigned int bufnum)
  230. {
  231. struct nand_chip *chip = mtd_to_nand(mtd);
  232. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  233. u8 __iomem *addr = priv->vbase + bufnum * (mtd->writesize * 2);
  234. u32 __iomem *mainarea = (u32 __iomem *)addr;
  235. u8 __iomem *oob = addr + mtd->writesize;
  236. int i;
  237. for (i = 0; i < mtd->writesize / 4; i++) {
  238. if (__raw_readl(&mainarea[i]) != 0xffffffff)
  239. return 0;
  240. }
  241. for (i = 0; i < chip->ecc.layout->eccbytes; i++) {
  242. int pos = chip->ecc.layout->eccpos[i];
  243. if (__raw_readb(&oob[pos]) != 0xff)
  244. return 0;
  245. }
  246. return 1;
  247. }
  248. /* returns nonzero if entire page is blank */
  249. static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
  250. u32 *eccstat, unsigned int bufnum)
  251. {
  252. u32 reg = eccstat[bufnum / 4];
  253. int errors;
  254. errors = (reg >> ((3 - bufnum % 4) * 8)) & 15;
  255. return errors;
  256. }
  257. /*
  258. * execute IFC NAND command and wait for it to complete
  259. */
  260. static void fsl_ifc_run_command(struct mtd_info *mtd)
  261. {
  262. struct nand_chip *chip = mtd_to_nand(mtd);
  263. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  264. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  265. struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
  266. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  267. u32 eccstat[4];
  268. int i;
  269. /* set the chip select for NAND Transaction */
  270. ifc_out32(priv->bank << IFC_NAND_CSEL_SHIFT,
  271. &ifc->ifc_nand.nand_csel);
  272. dev_vdbg(priv->dev,
  273. "%s: fir0=%08x fcr0=%08x\n",
  274. __func__,
  275. ifc_in32(&ifc->ifc_nand.nand_fir0),
  276. ifc_in32(&ifc->ifc_nand.nand_fcr0));
  277. ctrl->nand_stat = 0;
  278. /* start read/write seq */
  279. ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt);
  280. /* wait for command complete flag or timeout */
  281. wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
  282. msecs_to_jiffies(IFC_TIMEOUT_MSECS));
  283. /* ctrl->nand_stat will be updated from IRQ context */
  284. if (!ctrl->nand_stat)
  285. dev_err(priv->dev, "Controller is not responding\n");
  286. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_FTOER)
  287. dev_err(priv->dev, "NAND Flash Timeout Error\n");
  288. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_WPER)
  289. dev_err(priv->dev, "NAND Flash Write Protect Error\n");
  290. nctrl->max_bitflips = 0;
  291. if (nctrl->eccread) {
  292. int errors;
  293. int bufnum = nctrl->page & priv->bufnum_mask;
  294. int sector = bufnum * chip->ecc.steps;
  295. int sector_end = sector + chip->ecc.steps - 1;
  296. for (i = sector / 4; i <= sector_end / 4; i++)
  297. eccstat[i] = ifc_in32(&ifc->ifc_nand.nand_eccstat[i]);
  298. for (i = sector; i <= sector_end; i++) {
  299. errors = check_read_ecc(mtd, ctrl, eccstat, i);
  300. if (errors == 15) {
  301. /*
  302. * Uncorrectable error.
  303. * OK only if the whole page is blank.
  304. *
  305. * We disable ECCER reporting due to...
  306. * erratum IFC-A002770 -- so report it now if we
  307. * see an uncorrectable error in ECCSTAT.
  308. */
  309. if (!is_blank(mtd, bufnum))
  310. ctrl->nand_stat |=
  311. IFC_NAND_EVTER_STAT_ECCER;
  312. break;
  313. }
  314. mtd->ecc_stats.corrected += errors;
  315. nctrl->max_bitflips = max_t(unsigned int,
  316. nctrl->max_bitflips,
  317. errors);
  318. }
  319. nctrl->eccread = 0;
  320. }
  321. }
  322. static void fsl_ifc_do_read(struct nand_chip *chip,
  323. int oob,
  324. struct mtd_info *mtd)
  325. {
  326. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  327. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  328. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  329. /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
  330. if (mtd->writesize > 512) {
  331. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  332. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  333. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  334. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
  335. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT),
  336. &ifc->ifc_nand.nand_fir0);
  337. ifc_out32(0x0, &ifc->ifc_nand.nand_fir1);
  338. ifc_out32((NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
  339. (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT),
  340. &ifc->ifc_nand.nand_fcr0);
  341. } else {
  342. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  343. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  344. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  345. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT),
  346. &ifc->ifc_nand.nand_fir0);
  347. ifc_out32(0x0, &ifc->ifc_nand.nand_fir1);
  348. if (oob)
  349. ifc_out32(NAND_CMD_READOOB <<
  350. IFC_NAND_FCR0_CMD0_SHIFT,
  351. &ifc->ifc_nand.nand_fcr0);
  352. else
  353. ifc_out32(NAND_CMD_READ0 <<
  354. IFC_NAND_FCR0_CMD0_SHIFT,
  355. &ifc->ifc_nand.nand_fcr0);
  356. }
  357. }
  358. /* cmdfunc send commands to the IFC NAND Machine */
  359. static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  360. int column, int page_addr) {
  361. struct nand_chip *chip = mtd_to_nand(mtd);
  362. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  363. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  364. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  365. /* clear the read buffer */
  366. ifc_nand_ctrl->read_bytes = 0;
  367. if (command != NAND_CMD_PAGEPROG)
  368. ifc_nand_ctrl->index = 0;
  369. switch (command) {
  370. /* READ0 read the entire buffer to use hardware ECC. */
  371. case NAND_CMD_READ0:
  372. ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
  373. set_addr(mtd, 0, page_addr, 0);
  374. ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  375. ifc_nand_ctrl->index += column;
  376. if (chip->ecc.mode == NAND_ECC_HW)
  377. ifc_nand_ctrl->eccread = 1;
  378. fsl_ifc_do_read(chip, 0, mtd);
  379. fsl_ifc_run_command(mtd);
  380. return;
  381. /* READOOB reads only the OOB because no ECC is performed. */
  382. case NAND_CMD_READOOB:
  383. ifc_out32(mtd->oobsize - column, &ifc->ifc_nand.nand_fbcr);
  384. set_addr(mtd, column, page_addr, 1);
  385. ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  386. fsl_ifc_do_read(chip, 1, mtd);
  387. fsl_ifc_run_command(mtd);
  388. return;
  389. case NAND_CMD_READID:
  390. case NAND_CMD_PARAM: {
  391. int timing = IFC_FIR_OP_RB;
  392. if (command == NAND_CMD_PARAM)
  393. timing = IFC_FIR_OP_RBCD;
  394. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  395. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  396. (timing << IFC_NAND_FIR0_OP2_SHIFT),
  397. &ifc->ifc_nand.nand_fir0);
  398. ifc_out32(command << IFC_NAND_FCR0_CMD0_SHIFT,
  399. &ifc->ifc_nand.nand_fcr0);
  400. ifc_out32(column, &ifc->ifc_nand.row3);
  401. /*
  402. * although currently it's 8 bytes for READID, we always read
  403. * the maximum 256 bytes(for PARAM)
  404. */
  405. ifc_out32(256, &ifc->ifc_nand.nand_fbcr);
  406. ifc_nand_ctrl->read_bytes = 256;
  407. set_addr(mtd, 0, 0, 0);
  408. fsl_ifc_run_command(mtd);
  409. return;
  410. }
  411. /* ERASE1 stores the block and page address */
  412. case NAND_CMD_ERASE1:
  413. set_addr(mtd, 0, page_addr, 0);
  414. return;
  415. /* ERASE2 uses the block and page address from ERASE1 */
  416. case NAND_CMD_ERASE2:
  417. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  418. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  419. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT),
  420. &ifc->ifc_nand.nand_fir0);
  421. ifc_out32((NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
  422. (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT),
  423. &ifc->ifc_nand.nand_fcr0);
  424. ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
  425. ifc_nand_ctrl->read_bytes = 0;
  426. fsl_ifc_run_command(mtd);
  427. return;
  428. /* SEQIN sets up the addr buffer and all registers except the length */
  429. case NAND_CMD_SEQIN: {
  430. u32 nand_fcr0;
  431. ifc_nand_ctrl->column = column;
  432. ifc_nand_ctrl->oob = 0;
  433. if (mtd->writesize > 512) {
  434. nand_fcr0 =
  435. (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
  436. (NAND_CMD_STATUS << IFC_NAND_FCR0_CMD1_SHIFT) |
  437. (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD2_SHIFT);
  438. ifc_out32(
  439. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  440. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  441. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  442. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) |
  443. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP4_SHIFT),
  444. &ifc->ifc_nand.nand_fir0);
  445. ifc_out32(
  446. (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT) |
  447. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP6_SHIFT) |
  448. (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP7_SHIFT),
  449. &ifc->ifc_nand.nand_fir1);
  450. } else {
  451. nand_fcr0 = ((NAND_CMD_PAGEPROG <<
  452. IFC_NAND_FCR0_CMD1_SHIFT) |
  453. (NAND_CMD_SEQIN <<
  454. IFC_NAND_FCR0_CMD2_SHIFT) |
  455. (NAND_CMD_STATUS <<
  456. IFC_NAND_FCR0_CMD3_SHIFT));
  457. ifc_out32(
  458. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  459. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
  460. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  461. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
  462. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT),
  463. &ifc->ifc_nand.nand_fir0);
  464. ifc_out32(
  465. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR1_OP5_SHIFT) |
  466. (IFC_FIR_OP_CW3 << IFC_NAND_FIR1_OP6_SHIFT) |
  467. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP7_SHIFT) |
  468. (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP8_SHIFT),
  469. &ifc->ifc_nand.nand_fir1);
  470. if (column >= mtd->writesize)
  471. nand_fcr0 |=
  472. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
  473. else
  474. nand_fcr0 |=
  475. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
  476. }
  477. if (column >= mtd->writesize) {
  478. /* OOB area --> READOOB */
  479. column -= mtd->writesize;
  480. ifc_nand_ctrl->oob = 1;
  481. }
  482. ifc_out32(nand_fcr0, &ifc->ifc_nand.nand_fcr0);
  483. set_addr(mtd, column, page_addr, ifc_nand_ctrl->oob);
  484. return;
  485. }
  486. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  487. case NAND_CMD_PAGEPROG: {
  488. if (ifc_nand_ctrl->oob) {
  489. ifc_out32(ifc_nand_ctrl->index -
  490. ifc_nand_ctrl->column,
  491. &ifc->ifc_nand.nand_fbcr);
  492. } else {
  493. ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
  494. }
  495. fsl_ifc_run_command(mtd);
  496. return;
  497. }
  498. case NAND_CMD_STATUS: {
  499. void __iomem *addr;
  500. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  501. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT),
  502. &ifc->ifc_nand.nand_fir0);
  503. ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
  504. &ifc->ifc_nand.nand_fcr0);
  505. ifc_out32(1, &ifc->ifc_nand.nand_fbcr);
  506. set_addr(mtd, 0, 0, 0);
  507. ifc_nand_ctrl->read_bytes = 1;
  508. fsl_ifc_run_command(mtd);
  509. /*
  510. * The chip always seems to report that it is
  511. * write-protected, even when it is not.
  512. */
  513. addr = ifc_nand_ctrl->addr;
  514. if (chip->options & NAND_BUSWIDTH_16)
  515. ifc_out16(ifc_in16(addr) | (NAND_STATUS_WP), addr);
  516. else
  517. ifc_out8(ifc_in8(addr) | (NAND_STATUS_WP), addr);
  518. return;
  519. }
  520. case NAND_CMD_RESET:
  521. ifc_out32(IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT,
  522. &ifc->ifc_nand.nand_fir0);
  523. ifc_out32(NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT,
  524. &ifc->ifc_nand.nand_fcr0);
  525. fsl_ifc_run_command(mtd);
  526. return;
  527. default:
  528. dev_err(priv->dev, "%s: error, unsupported command 0x%x.\n",
  529. __func__, command);
  530. }
  531. }
  532. static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
  533. {
  534. /* The hardware does not seem to support multiple
  535. * chips per bank.
  536. */
  537. }
  538. /*
  539. * Write buf to the IFC NAND Controller Data Buffer
  540. */
  541. static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  542. {
  543. struct nand_chip *chip = mtd_to_nand(mtd);
  544. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  545. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  546. if (len <= 0) {
  547. dev_err(priv->dev, "%s: len %d bytes", __func__, len);
  548. return;
  549. }
  550. if ((unsigned int)len > bufsize - ifc_nand_ctrl->index) {
  551. dev_err(priv->dev,
  552. "%s: beyond end of buffer (%d requested, %u available)\n",
  553. __func__, len, bufsize - ifc_nand_ctrl->index);
  554. len = bufsize - ifc_nand_ctrl->index;
  555. }
  556. memcpy_toio(ifc_nand_ctrl->addr + ifc_nand_ctrl->index, buf, len);
  557. ifc_nand_ctrl->index += len;
  558. }
  559. /*
  560. * Read a byte from either the IFC hardware buffer
  561. * read function for 8-bit buswidth
  562. */
  563. static uint8_t fsl_ifc_read_byte(struct mtd_info *mtd)
  564. {
  565. struct nand_chip *chip = mtd_to_nand(mtd);
  566. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  567. unsigned int offset;
  568. /*
  569. * If there are still bytes in the IFC buffer, then use the
  570. * next byte.
  571. */
  572. if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
  573. offset = ifc_nand_ctrl->index++;
  574. return ifc_in8(ifc_nand_ctrl->addr + offset);
  575. }
  576. dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
  577. return ERR_BYTE;
  578. }
  579. /*
  580. * Read two bytes from the IFC hardware buffer
  581. * read function for 16-bit buswith
  582. */
  583. static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
  584. {
  585. struct nand_chip *chip = mtd_to_nand(mtd);
  586. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  587. uint16_t data;
  588. /*
  589. * If there are still bytes in the IFC buffer, then use the
  590. * next byte.
  591. */
  592. if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
  593. data = ifc_in16(ifc_nand_ctrl->addr + ifc_nand_ctrl->index);
  594. ifc_nand_ctrl->index += 2;
  595. return (uint8_t) data;
  596. }
  597. dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
  598. return ERR_BYTE;
  599. }
  600. /*
  601. * Read from the IFC Controller Data Buffer
  602. */
  603. static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  604. {
  605. struct nand_chip *chip = mtd_to_nand(mtd);
  606. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  607. int avail;
  608. if (len < 0) {
  609. dev_err(priv->dev, "%s: len %d bytes", __func__, len);
  610. return;
  611. }
  612. avail = min((unsigned int)len,
  613. ifc_nand_ctrl->read_bytes - ifc_nand_ctrl->index);
  614. memcpy_fromio(buf, ifc_nand_ctrl->addr + ifc_nand_ctrl->index, avail);
  615. ifc_nand_ctrl->index += avail;
  616. if (len > avail)
  617. dev_err(priv->dev,
  618. "%s: beyond end of buffer (%d requested, %d available)\n",
  619. __func__, len, avail);
  620. }
  621. /*
  622. * This function is called after Program and Erase Operations to
  623. * check for success or failure.
  624. */
  625. static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  626. {
  627. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  628. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  629. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  630. u32 nand_fsr;
  631. /* Use READ_STATUS command, but wait for the device to be ready */
  632. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  633. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT),
  634. &ifc->ifc_nand.nand_fir0);
  635. ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
  636. &ifc->ifc_nand.nand_fcr0);
  637. ifc_out32(1, &ifc->ifc_nand.nand_fbcr);
  638. set_addr(mtd, 0, 0, 0);
  639. ifc_nand_ctrl->read_bytes = 1;
  640. fsl_ifc_run_command(mtd);
  641. nand_fsr = ifc_in32(&ifc->ifc_nand.nand_fsr);
  642. /*
  643. * The chip always seems to report that it is
  644. * write-protected, even when it is not.
  645. */
  646. return nand_fsr | NAND_STATUS_WP;
  647. }
  648. static int fsl_ifc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  649. uint8_t *buf, int oob_required, int page)
  650. {
  651. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  652. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  653. struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
  654. fsl_ifc_read_buf(mtd, buf, mtd->writesize);
  655. if (oob_required)
  656. fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  657. if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_ECCER)
  658. dev_err(priv->dev, "NAND Flash ECC Uncorrectable Error\n");
  659. if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
  660. mtd->ecc_stats.failed++;
  661. return nctrl->max_bitflips;
  662. }
  663. /* ECC will be calculated automatically, and errors will be detected in
  664. * waitfunc.
  665. */
  666. static int fsl_ifc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  667. const uint8_t *buf, int oob_required, int page)
  668. {
  669. fsl_ifc_write_buf(mtd, buf, mtd->writesize);
  670. fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  671. return 0;
  672. }
  673. static int fsl_ifc_chip_init_tail(struct mtd_info *mtd)
  674. {
  675. struct nand_chip *chip = mtd_to_nand(mtd);
  676. struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
  677. dev_dbg(priv->dev, "%s: nand->numchips = %d\n", __func__,
  678. chip->numchips);
  679. dev_dbg(priv->dev, "%s: nand->chipsize = %lld\n", __func__,
  680. chip->chipsize);
  681. dev_dbg(priv->dev, "%s: nand->pagemask = %8x\n", __func__,
  682. chip->pagemask);
  683. dev_dbg(priv->dev, "%s: nand->chip_delay = %d\n", __func__,
  684. chip->chip_delay);
  685. dev_dbg(priv->dev, "%s: nand->badblockpos = %d\n", __func__,
  686. chip->badblockpos);
  687. dev_dbg(priv->dev, "%s: nand->chip_shift = %d\n", __func__,
  688. chip->chip_shift);
  689. dev_dbg(priv->dev, "%s: nand->page_shift = %d\n", __func__,
  690. chip->page_shift);
  691. dev_dbg(priv->dev, "%s: nand->phys_erase_shift = %d\n", __func__,
  692. chip->phys_erase_shift);
  693. dev_dbg(priv->dev, "%s: nand->ecc.mode = %d\n", __func__,
  694. chip->ecc.mode);
  695. dev_dbg(priv->dev, "%s: nand->ecc.steps = %d\n", __func__,
  696. chip->ecc.steps);
  697. dev_dbg(priv->dev, "%s: nand->ecc.bytes = %d\n", __func__,
  698. chip->ecc.bytes);
  699. dev_dbg(priv->dev, "%s: nand->ecc.total = %d\n", __func__,
  700. chip->ecc.total);
  701. dev_dbg(priv->dev, "%s: nand->ecc.layout = %p\n", __func__,
  702. chip->ecc.layout);
  703. dev_dbg(priv->dev, "%s: mtd->flags = %08x\n", __func__, mtd->flags);
  704. dev_dbg(priv->dev, "%s: mtd->size = %lld\n", __func__, mtd->size);
  705. dev_dbg(priv->dev, "%s: mtd->erasesize = %d\n", __func__,
  706. mtd->erasesize);
  707. dev_dbg(priv->dev, "%s: mtd->writesize = %d\n", __func__,
  708. mtd->writesize);
  709. dev_dbg(priv->dev, "%s: mtd->oobsize = %d\n", __func__,
  710. mtd->oobsize);
  711. return 0;
  712. }
  713. static void fsl_ifc_sram_init(struct fsl_ifc_mtd *priv)
  714. {
  715. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  716. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  717. uint32_t csor = 0, csor_8k = 0, csor_ext = 0;
  718. uint32_t cs = priv->bank;
  719. /* Save CSOR and CSOR_ext */
  720. csor = ifc_in32(&ifc->csor_cs[cs].csor);
  721. csor_ext = ifc_in32(&ifc->csor_cs[cs].csor_ext);
  722. /* chage PageSize 8K and SpareSize 1K*/
  723. csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
  724. ifc_out32(csor_8k, &ifc->csor_cs[cs].csor);
  725. ifc_out32(0x0000400, &ifc->csor_cs[cs].csor_ext);
  726. /* READID */
  727. ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  728. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  729. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT),
  730. &ifc->ifc_nand.nand_fir0);
  731. ifc_out32(NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT,
  732. &ifc->ifc_nand.nand_fcr0);
  733. ifc_out32(0x0, &ifc->ifc_nand.row3);
  734. ifc_out32(0x0, &ifc->ifc_nand.nand_fbcr);
  735. /* Program ROW0/COL0 */
  736. ifc_out32(0x0, &ifc->ifc_nand.row0);
  737. ifc_out32(0x0, &ifc->ifc_nand.col0);
  738. /* set the chip select for NAND Transaction */
  739. ifc_out32(cs << IFC_NAND_CSEL_SHIFT, &ifc->ifc_nand.nand_csel);
  740. /* start read seq */
  741. ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt);
  742. /* wait for command complete flag or timeout */
  743. wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
  744. msecs_to_jiffies(IFC_TIMEOUT_MSECS));
  745. if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
  746. printk(KERN_ERR "fsl-ifc: Failed to Initialise SRAM\n");
  747. /* Restore CSOR and CSOR_ext */
  748. ifc_out32(csor, &ifc->csor_cs[cs].csor);
  749. ifc_out32(csor_ext, &ifc->csor_cs[cs].csor_ext);
  750. }
  751. static int fsl_ifc_chip_init(struct fsl_ifc_mtd *priv)
  752. {
  753. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  754. struct fsl_ifc_regs __iomem *ifc = ctrl->regs;
  755. struct nand_chip *chip = &priv->chip;
  756. struct mtd_info *mtd = nand_to_mtd(&priv->chip);
  757. struct nand_ecclayout *layout;
  758. u32 csor;
  759. /* Fill in fsl_ifc_mtd structure */
  760. mtd->dev.parent = priv->dev;
  761. nand_set_flash_node(chip, priv->dev->of_node);
  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. nand_set_controller_data(chip, 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. struct mtd_info *mtd = nand_to_mtd(&priv->chip);
  850. nand_release(mtd);
  851. kfree(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 = ifc_in32(&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_info *mtd;
  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. mtd = nand_to_mtd(&priv->chip);
  939. mtd->name = kasprintf(GFP_KERNEL, "%llx.flash", (u64)res.start);
  940. if (!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(mtd, 1, NULL);
  948. if (ret)
  949. goto err;
  950. ret = fsl_ifc_chip_init_tail(mtd);
  951. if (ret)
  952. goto err;
  953. ret = nand_scan_tail(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(mtd, part_probe_types, NULL, 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. MODULE_DEVICE_TABLE(of, fsl_ifc_nand_match);
  986. static struct platform_driver fsl_ifc_nand_driver = {
  987. .driver = {
  988. .name = "fsl,ifc-nand",
  989. .of_match_table = fsl_ifc_nand_match,
  990. },
  991. .probe = fsl_ifc_nand_probe,
  992. .remove = fsl_ifc_nand_remove,
  993. };
  994. module_platform_driver(fsl_ifc_nand_driver);
  995. MODULE_LICENSE("GPL");
  996. MODULE_AUTHOR("Freescale");
  997. MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver");