fsl_ifc_nand.c 33 KB

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