fsl_ifc_nand.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /* Integrated Flash Controller NAND Machine Driver
  2. *
  3. * Copyright (c) 2012 Freescale Semiconductor, Inc
  4. *
  5. * Authors: Dipen Dudhat <Dipen.Dudhat@freescale.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <nand.h>
  12. #include <linux/mtd/mtd.h>
  13. #include <linux/mtd/nand.h>
  14. #include <linux/mtd/nand_ecc.h>
  15. #include <asm/io.h>
  16. #include <asm/errno.h>
  17. #include <fsl_ifc.h>
  18. #ifndef CONFIG_SYS_FSL_IFC_BANK_COUNT
  19. #define CONFIG_SYS_FSL_IFC_BANK_COUNT 4
  20. #endif
  21. #define MAX_BANKS CONFIG_SYS_FSL_IFC_BANK_COUNT
  22. #define ERR_BYTE 0xFF /* Value returned for read bytes
  23. when read failed */
  24. struct fsl_ifc_ctrl;
  25. /* mtd information per set */
  26. struct fsl_ifc_mtd {
  27. struct nand_chip chip;
  28. struct fsl_ifc_ctrl *ctrl;
  29. struct device *dev;
  30. int bank; /* Chip select bank number */
  31. unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */
  32. u8 __iomem *vbase; /* Chip select base virtual address */
  33. };
  34. /* overview of the fsl ifc controller */
  35. struct fsl_ifc_ctrl {
  36. struct nand_hw_control controller;
  37. struct fsl_ifc_mtd *chips[MAX_BANKS];
  38. /* device info */
  39. struct fsl_ifc *regs;
  40. uint8_t __iomem *addr; /* Address of assigned IFC buffer */
  41. unsigned int cs_nand; /* On which chipsel NAND is connected */
  42. unsigned int page; /* Last page written to / read from */
  43. unsigned int read_bytes; /* Number of bytes read during command */
  44. unsigned int column; /* Saved column from SEQIN */
  45. unsigned int index; /* Pointer to next byte to 'read' */
  46. unsigned int status; /* status read from NEESR after last op */
  47. unsigned int oob; /* Non zero if operating on OOB data */
  48. unsigned int eccread; /* Non zero for a full-page ECC read */
  49. };
  50. static struct fsl_ifc_ctrl *ifc_ctrl;
  51. /* 512-byte page with 4-bit ECC, 8-bit */
  52. static struct nand_ecclayout oob_512_8bit_ecc4 = {
  53. .eccbytes = 8,
  54. .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  55. .oobfree = { {0, 5}, {6, 2} },
  56. };
  57. /* 512-byte page with 4-bit ECC, 16-bit */
  58. static struct nand_ecclayout oob_512_16bit_ecc4 = {
  59. .eccbytes = 8,
  60. .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  61. .oobfree = { {2, 6}, },
  62. };
  63. /* 2048-byte page size with 4-bit ECC */
  64. static struct nand_ecclayout oob_2048_ecc4 = {
  65. .eccbytes = 32,
  66. .eccpos = {
  67. 8, 9, 10, 11, 12, 13, 14, 15,
  68. 16, 17, 18, 19, 20, 21, 22, 23,
  69. 24, 25, 26, 27, 28, 29, 30, 31,
  70. 32, 33, 34, 35, 36, 37, 38, 39,
  71. },
  72. .oobfree = { {2, 6}, {40, 24} },
  73. };
  74. /* 4096-byte page size with 4-bit ECC */
  75. static struct nand_ecclayout oob_4096_ecc4 = {
  76. .eccbytes = 64,
  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. 40, 41, 42, 43, 44, 45, 46, 47,
  83. 48, 49, 50, 51, 52, 53, 54, 55,
  84. 56, 57, 58, 59, 60, 61, 62, 63,
  85. 64, 65, 66, 67, 68, 69, 70, 71,
  86. },
  87. .oobfree = { {2, 6}, {72, 56} },
  88. };
  89. /* 4096-byte page size with 8-bit ECC -- requires 218-byte OOB */
  90. static struct nand_ecclayout oob_4096_ecc8 = {
  91. .eccbytes = 128,
  92. .eccpos = {
  93. 8, 9, 10, 11, 12, 13, 14, 15,
  94. 16, 17, 18, 19, 20, 21, 22, 23,
  95. 24, 25, 26, 27, 28, 29, 30, 31,
  96. 32, 33, 34, 35, 36, 37, 38, 39,
  97. 40, 41, 42, 43, 44, 45, 46, 47,
  98. 48, 49, 50, 51, 52, 53, 54, 55,
  99. 56, 57, 58, 59, 60, 61, 62, 63,
  100. 64, 65, 66, 67, 68, 69, 70, 71,
  101. 72, 73, 74, 75, 76, 77, 78, 79,
  102. 80, 81, 82, 83, 84, 85, 86, 87,
  103. 88, 89, 90, 91, 92, 93, 94, 95,
  104. 96, 97, 98, 99, 100, 101, 102, 103,
  105. 104, 105, 106, 107, 108, 109, 110, 111,
  106. 112, 113, 114, 115, 116, 117, 118, 119,
  107. 120, 121, 122, 123, 124, 125, 126, 127,
  108. 128, 129, 130, 131, 132, 133, 134, 135,
  109. },
  110. .oobfree = { {2, 6}, {136, 82} },
  111. };
  112. /* 8192-byte page size with 4-bit ECC */
  113. static struct nand_ecclayout oob_8192_ecc4 = {
  114. .eccbytes = 128,
  115. .eccpos = {
  116. 8, 9, 10, 11, 12, 13, 14, 15,
  117. 16, 17, 18, 19, 20, 21, 22, 23,
  118. 24, 25, 26, 27, 28, 29, 30, 31,
  119. 32, 33, 34, 35, 36, 37, 38, 39,
  120. 40, 41, 42, 43, 44, 45, 46, 47,
  121. 48, 49, 50, 51, 52, 53, 54, 55,
  122. 56, 57, 58, 59, 60, 61, 62, 63,
  123. 64, 65, 66, 67, 68, 69, 70, 71,
  124. 72, 73, 74, 75, 76, 77, 78, 79,
  125. 80, 81, 82, 83, 84, 85, 86, 87,
  126. 88, 89, 90, 91, 92, 93, 94, 95,
  127. 96, 97, 98, 99, 100, 101, 102, 103,
  128. 104, 105, 106, 107, 108, 109, 110, 111,
  129. 112, 113, 114, 115, 116, 117, 118, 119,
  130. 120, 121, 122, 123, 124, 125, 126, 127,
  131. 128, 129, 130, 131, 132, 133, 134, 135,
  132. },
  133. .oobfree = { {2, 6}, {136, 208} },
  134. };
  135. /* 8192-byte page size with 8-bit ECC -- requires 218-byte OOB */
  136. static struct nand_ecclayout oob_8192_ecc8 = {
  137. .eccbytes = 256,
  138. .eccpos = {
  139. 8, 9, 10, 11, 12, 13, 14, 15,
  140. 16, 17, 18, 19, 20, 21, 22, 23,
  141. 24, 25, 26, 27, 28, 29, 30, 31,
  142. 32, 33, 34, 35, 36, 37, 38, 39,
  143. 40, 41, 42, 43, 44, 45, 46, 47,
  144. 48, 49, 50, 51, 52, 53, 54, 55,
  145. 56, 57, 58, 59, 60, 61, 62, 63,
  146. 64, 65, 66, 67, 68, 69, 70, 71,
  147. 72, 73, 74, 75, 76, 77, 78, 79,
  148. 80, 81, 82, 83, 84, 85, 86, 87,
  149. 88, 89, 90, 91, 92, 93, 94, 95,
  150. 96, 97, 98, 99, 100, 101, 102, 103,
  151. 104, 105, 106, 107, 108, 109, 110, 111,
  152. 112, 113, 114, 115, 116, 117, 118, 119,
  153. 120, 121, 122, 123, 124, 125, 126, 127,
  154. 128, 129, 130, 131, 132, 133, 134, 135,
  155. 136, 137, 138, 139, 140, 141, 142, 143,
  156. 144, 145, 146, 147, 148, 149, 150, 151,
  157. 152, 153, 154, 155, 156, 157, 158, 159,
  158. 160, 161, 162, 163, 164, 165, 166, 167,
  159. 168, 169, 170, 171, 172, 173, 174, 175,
  160. 176, 177, 178, 179, 180, 181, 182, 183,
  161. 184, 185, 186, 187, 188, 189, 190, 191,
  162. 192, 193, 194, 195, 196, 197, 198, 199,
  163. 200, 201, 202, 203, 204, 205, 206, 207,
  164. 208, 209, 210, 211, 212, 213, 214, 215,
  165. 216, 217, 218, 219, 220, 221, 222, 223,
  166. 224, 225, 226, 227, 228, 229, 230, 231,
  167. 232, 233, 234, 235, 236, 237, 238, 239,
  168. 240, 241, 242, 243, 244, 245, 246, 247,
  169. 248, 249, 250, 251, 252, 253, 254, 255,
  170. 256, 257, 258, 259, 260, 261, 262, 263,
  171. },
  172. .oobfree = { {2, 6}, {264, 80} },
  173. };
  174. /*
  175. * Generic flash bbt descriptors
  176. */
  177. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  178. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  179. static struct nand_bbt_descr bbt_main_descr = {
  180. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  181. NAND_BBT_2BIT | NAND_BBT_VERSION,
  182. .offs = 2, /* 0 on 8-bit small page */
  183. .len = 4,
  184. .veroffs = 6,
  185. .maxblocks = 4,
  186. .pattern = bbt_pattern,
  187. };
  188. static struct nand_bbt_descr bbt_mirror_descr = {
  189. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  190. NAND_BBT_2BIT | NAND_BBT_VERSION,
  191. .offs = 2, /* 0 on 8-bit small page */
  192. .len = 4,
  193. .veroffs = 6,
  194. .maxblocks = 4,
  195. .pattern = mirror_pattern,
  196. };
  197. /*
  198. * Set up the IFC hardware block and page address fields, and the ifc nand
  199. * structure addr field to point to the correct IFC buffer in memory
  200. */
  201. static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
  202. {
  203. struct nand_chip *chip = mtd->priv;
  204. struct fsl_ifc_mtd *priv = chip->priv;
  205. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  206. struct fsl_ifc *ifc = ctrl->regs;
  207. int buf_num;
  208. ctrl->page = page_addr;
  209. /* Program ROW0/COL0 */
  210. ifc_out32(&ifc->ifc_nand.row0, page_addr);
  211. ifc_out32(&ifc->ifc_nand.col0, (oob ? IFC_NAND_COL_MS : 0) | column);
  212. buf_num = page_addr & priv->bufnum_mask;
  213. ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2);
  214. ctrl->index = column;
  215. /* for OOB data point to the second half of the buffer */
  216. if (oob)
  217. ctrl->index += mtd->writesize;
  218. }
  219. static int is_blank(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
  220. unsigned int bufnum)
  221. {
  222. struct nand_chip *chip = mtd->priv;
  223. struct fsl_ifc_mtd *priv = chip->priv;
  224. u8 __iomem *addr = priv->vbase + bufnum * (mtd->writesize * 2);
  225. u32 __iomem *main = (u32 *)addr;
  226. u8 __iomem *oob = addr + mtd->writesize;
  227. int i;
  228. for (i = 0; i < mtd->writesize / 4; i++) {
  229. if (__raw_readl(&main[i]) != 0xffffffff)
  230. return 0;
  231. }
  232. for (i = 0; i < chip->ecc.layout->eccbytes; i++) {
  233. int pos = chip->ecc.layout->eccpos[i];
  234. if (__raw_readb(&oob[pos]) != 0xff)
  235. return 0;
  236. }
  237. return 1;
  238. }
  239. /* returns nonzero if entire page is blank */
  240. static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
  241. u32 *eccstat, unsigned int bufnum)
  242. {
  243. u32 reg = eccstat[bufnum / 4];
  244. int errors;
  245. errors = (reg >> ((3 - bufnum % 4) * 8)) & 15;
  246. return errors;
  247. }
  248. /*
  249. * execute IFC NAND command and wait for it to complete
  250. */
  251. static int fsl_ifc_run_command(struct mtd_info *mtd)
  252. {
  253. struct nand_chip *chip = mtd->priv;
  254. struct fsl_ifc_mtd *priv = chip->priv;
  255. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  256. struct fsl_ifc *ifc = ctrl->regs;
  257. u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
  258. u32 time_start;
  259. u32 eccstat[4] = {0};
  260. int i;
  261. /* set the chip select for NAND Transaction */
  262. ifc_out32(&ifc->ifc_nand.nand_csel, ifc_ctrl->cs_nand);
  263. /* start read/write seq */
  264. ifc_out32(&ifc->ifc_nand.nandseq_strt,
  265. IFC_NAND_SEQ_STRT_FIR_STRT);
  266. /* wait for NAND Machine complete flag or timeout */
  267. time_start = get_timer(0);
  268. while (get_timer(time_start) < timeo) {
  269. ctrl->status = ifc_in32(&ifc->ifc_nand.nand_evter_stat);
  270. if (ctrl->status & IFC_NAND_EVTER_STAT_OPC)
  271. break;
  272. }
  273. ifc_out32(&ifc->ifc_nand.nand_evter_stat, ctrl->status);
  274. if (ctrl->status & IFC_NAND_EVTER_STAT_FTOER)
  275. printf("%s: Flash Time Out Error\n", __func__);
  276. if (ctrl->status & IFC_NAND_EVTER_STAT_WPER)
  277. printf("%s: Write Protect Error\n", __func__);
  278. if (ctrl->eccread) {
  279. int errors;
  280. int bufnum = ctrl->page & priv->bufnum_mask;
  281. int sector = bufnum * chip->ecc.steps;
  282. int sector_end = sector + chip->ecc.steps - 1;
  283. for (i = sector / 4; i <= sector_end / 4; i++)
  284. eccstat[i] = ifc_in32(&ifc->ifc_nand.nand_eccstat[i]);
  285. for (i = sector; i <= sector_end; i++) {
  286. errors = check_read_ecc(mtd, ctrl, eccstat, i);
  287. if (errors == 15) {
  288. /*
  289. * Uncorrectable error.
  290. * OK only if the whole page is blank.
  291. *
  292. * We disable ECCER reporting due to erratum
  293. * IFC-A002770 -- so report it now if we
  294. * see an uncorrectable error in ECCSTAT.
  295. */
  296. if (!is_blank(mtd, ctrl, bufnum))
  297. ctrl->status |=
  298. IFC_NAND_EVTER_STAT_ECCER;
  299. break;
  300. }
  301. mtd->ecc_stats.corrected += errors;
  302. }
  303. ctrl->eccread = 0;
  304. }
  305. /* returns 0 on success otherwise non-zero) */
  306. return ctrl->status == IFC_NAND_EVTER_STAT_OPC ? 0 : -EIO;
  307. }
  308. static void fsl_ifc_do_read(struct nand_chip *chip,
  309. int oob,
  310. struct mtd_info *mtd)
  311. {
  312. struct fsl_ifc_mtd *priv = chip->priv;
  313. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  314. struct fsl_ifc *ifc = ctrl->regs;
  315. /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
  316. if (mtd->writesize > 512) {
  317. ifc_out32(&ifc->ifc_nand.nand_fir0,
  318. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  319. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  320. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  321. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
  322. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT));
  323. ifc_out32(&ifc->ifc_nand.nand_fir1, 0x0);
  324. ifc_out32(&ifc->ifc_nand.nand_fcr0,
  325. (NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
  326. (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT));
  327. } else {
  328. ifc_out32(&ifc->ifc_nand.nand_fir0,
  329. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  330. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  331. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  332. (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT));
  333. if (oob)
  334. ifc_out32(&ifc->ifc_nand.nand_fcr0,
  335. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT);
  336. else
  337. ifc_out32(&ifc->ifc_nand.nand_fcr0,
  338. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT);
  339. }
  340. }
  341. /* cmdfunc send commands to the IFC NAND Machine */
  342. static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  343. int column, int page_addr)
  344. {
  345. struct nand_chip *chip = mtd->priv;
  346. struct fsl_ifc_mtd *priv = chip->priv;
  347. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  348. struct fsl_ifc *ifc = ctrl->regs;
  349. /* clear the read buffer */
  350. ctrl->read_bytes = 0;
  351. if (command != NAND_CMD_PAGEPROG)
  352. ctrl->index = 0;
  353. switch (command) {
  354. /* READ0 read the entire buffer to use hardware ECC. */
  355. case NAND_CMD_READ0: {
  356. ifc_out32(&ifc->ifc_nand.nand_fbcr, 0);
  357. set_addr(mtd, 0, page_addr, 0);
  358. ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  359. ctrl->index += column;
  360. if (chip->ecc.mode == NAND_ECC_HW)
  361. ctrl->eccread = 1;
  362. fsl_ifc_do_read(chip, 0, mtd);
  363. fsl_ifc_run_command(mtd);
  364. return;
  365. }
  366. /* READOOB reads only the OOB because no ECC is performed. */
  367. case NAND_CMD_READOOB:
  368. ifc_out32(&ifc->ifc_nand.nand_fbcr, mtd->oobsize - column);
  369. set_addr(mtd, column, page_addr, 1);
  370. ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  371. fsl_ifc_do_read(chip, 1, mtd);
  372. fsl_ifc_run_command(mtd);
  373. return;
  374. /* READID must read all possible bytes while CEB is active */
  375. case NAND_CMD_READID:
  376. case NAND_CMD_PARAM: {
  377. int timing = IFC_FIR_OP_RB;
  378. if (command == NAND_CMD_PARAM)
  379. timing = IFC_FIR_OP_RBCD;
  380. ifc_out32(&ifc->ifc_nand.nand_fir0,
  381. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  382. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  383. (timing << IFC_NAND_FIR0_OP2_SHIFT));
  384. ifc_out32(&ifc->ifc_nand.nand_fcr0,
  385. command << IFC_NAND_FCR0_CMD0_SHIFT);
  386. ifc_out32(&ifc->ifc_nand.row3, column);
  387. /*
  388. * although currently it's 8 bytes for READID, we always read
  389. * the maximum 256 bytes(for PARAM)
  390. */
  391. ifc_out32(&ifc->ifc_nand.nand_fbcr, 256);
  392. ctrl->read_bytes = 256;
  393. set_addr(mtd, 0, 0, 0);
  394. fsl_ifc_run_command(mtd);
  395. return;
  396. }
  397. /* ERASE1 stores the block and page address */
  398. case NAND_CMD_ERASE1:
  399. set_addr(mtd, 0, page_addr, 0);
  400. return;
  401. /* ERASE2 uses the block and page address from ERASE1 */
  402. case NAND_CMD_ERASE2:
  403. ifc_out32(&ifc->ifc_nand.nand_fir0,
  404. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  405. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  406. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT));
  407. ifc_out32(&ifc->ifc_nand.nand_fcr0,
  408. (NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
  409. (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT));
  410. ifc_out32(&ifc->ifc_nand.nand_fbcr, 0);
  411. ctrl->read_bytes = 0;
  412. fsl_ifc_run_command(mtd);
  413. return;
  414. /* SEQIN sets up the addr buffer and all registers except the length */
  415. case NAND_CMD_SEQIN: {
  416. u32 nand_fcr0;
  417. ctrl->column = column;
  418. ctrl->oob = 0;
  419. if (mtd->writesize > 512) {
  420. nand_fcr0 =
  421. (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
  422. (NAND_CMD_STATUS << IFC_NAND_FCR0_CMD1_SHIFT) |
  423. (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD2_SHIFT);
  424. ifc_out32(&ifc->ifc_nand.nand_fir0,
  425. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  426. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
  427. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  428. (IFC_FIR_OP_WBCD <<
  429. IFC_NAND_FIR0_OP3_SHIFT) |
  430. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP4_SHIFT));
  431. ifc_out32(&ifc->ifc_nand.nand_fir1,
  432. (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT) |
  433. (IFC_FIR_OP_RDSTAT <<
  434. IFC_NAND_FIR1_OP6_SHIFT) |
  435. (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP7_SHIFT));
  436. } else {
  437. nand_fcr0 = ((NAND_CMD_PAGEPROG <<
  438. IFC_NAND_FCR0_CMD1_SHIFT) |
  439. (NAND_CMD_SEQIN <<
  440. IFC_NAND_FCR0_CMD2_SHIFT) |
  441. (NAND_CMD_STATUS <<
  442. IFC_NAND_FCR0_CMD3_SHIFT));
  443. ifc_out32(&ifc->ifc_nand.nand_fir0,
  444. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  445. (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
  446. (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
  447. (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
  448. (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT));
  449. ifc_out32(&ifc->ifc_nand.nand_fir1,
  450. (IFC_FIR_OP_CMD1 << IFC_NAND_FIR1_OP5_SHIFT) |
  451. (IFC_FIR_OP_CW3 << IFC_NAND_FIR1_OP6_SHIFT) |
  452. (IFC_FIR_OP_RDSTAT <<
  453. IFC_NAND_FIR1_OP7_SHIFT) |
  454. (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP8_SHIFT));
  455. if (column >= mtd->writesize)
  456. nand_fcr0 |=
  457. NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
  458. else
  459. nand_fcr0 |=
  460. NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
  461. }
  462. if (column >= mtd->writesize) {
  463. /* OOB area --> READOOB */
  464. column -= mtd->writesize;
  465. ctrl->oob = 1;
  466. }
  467. ifc_out32(&ifc->ifc_nand.nand_fcr0, nand_fcr0);
  468. set_addr(mtd, column, page_addr, ctrl->oob);
  469. return;
  470. }
  471. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  472. case NAND_CMD_PAGEPROG:
  473. if (ctrl->oob)
  474. ifc_out32(&ifc->ifc_nand.nand_fbcr,
  475. ctrl->index - ctrl->column);
  476. else
  477. ifc_out32(&ifc->ifc_nand.nand_fbcr, 0);
  478. fsl_ifc_run_command(mtd);
  479. return;
  480. case NAND_CMD_STATUS:
  481. ifc_out32(&ifc->ifc_nand.nand_fir0,
  482. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  483. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT));
  484. ifc_out32(&ifc->ifc_nand.nand_fcr0,
  485. NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT);
  486. ifc_out32(&ifc->ifc_nand.nand_fbcr, 1);
  487. set_addr(mtd, 0, 0, 0);
  488. ctrl->read_bytes = 1;
  489. fsl_ifc_run_command(mtd);
  490. /* Chip sometimes reporting write protect even when it's not */
  491. out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
  492. return;
  493. case NAND_CMD_RESET:
  494. ifc_out32(&ifc->ifc_nand.nand_fir0,
  495. IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT);
  496. ifc_out32(&ifc->ifc_nand.nand_fcr0,
  497. NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT);
  498. fsl_ifc_run_command(mtd);
  499. return;
  500. default:
  501. printf("%s: error, unsupported command 0x%x.\n",
  502. __func__, command);
  503. }
  504. }
  505. /*
  506. * Write buf to the IFC NAND Controller Data Buffer
  507. */
  508. static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  509. {
  510. struct nand_chip *chip = mtd->priv;
  511. struct fsl_ifc_mtd *priv = chip->priv;
  512. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  513. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  514. if (len <= 0) {
  515. printf("%s of %d bytes", __func__, len);
  516. ctrl->status = 0;
  517. return;
  518. }
  519. if ((unsigned int)len > bufsize - ctrl->index) {
  520. printf("%s beyond end of buffer "
  521. "(%d requested, %u available)\n",
  522. __func__, len, bufsize - ctrl->index);
  523. len = bufsize - ctrl->index;
  524. }
  525. memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
  526. ctrl->index += len;
  527. }
  528. /*
  529. * read a byte from either the IFC hardware buffer if it has any data left
  530. * otherwise issue a command to read a single byte.
  531. */
  532. static u8 fsl_ifc_read_byte(struct mtd_info *mtd)
  533. {
  534. struct nand_chip *chip = mtd->priv;
  535. struct fsl_ifc_mtd *priv = chip->priv;
  536. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  537. /* If there are still bytes in the IFC buffer, then use the
  538. * next byte. */
  539. if (ctrl->index < ctrl->read_bytes)
  540. return in_8(&ctrl->addr[ctrl->index++]);
  541. printf("%s beyond end of buffer\n", __func__);
  542. return ERR_BYTE;
  543. }
  544. /*
  545. * Read two bytes from the IFC hardware buffer
  546. * read function for 16-bit buswith
  547. */
  548. static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
  549. {
  550. struct nand_chip *chip = mtd->priv;
  551. struct fsl_ifc_mtd *priv = chip->priv;
  552. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  553. uint16_t data;
  554. /*
  555. * If there are still bytes in the IFC buffer, then use the
  556. * next byte.
  557. */
  558. if (ctrl->index < ctrl->read_bytes) {
  559. data = ifc_in16((uint16_t *)&ctrl->
  560. addr[ctrl->index]);
  561. ctrl->index += 2;
  562. return (uint8_t)data;
  563. }
  564. printf("%s beyond end of buffer\n", __func__);
  565. return ERR_BYTE;
  566. }
  567. /*
  568. * Read from the IFC Controller Data Buffer
  569. */
  570. static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  571. {
  572. struct nand_chip *chip = mtd->priv;
  573. struct fsl_ifc_mtd *priv = chip->priv;
  574. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  575. int avail;
  576. if (len < 0)
  577. return;
  578. avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
  579. memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
  580. ctrl->index += avail;
  581. if (len > avail)
  582. printf("%s beyond end of buffer "
  583. "(%d requested, %d available)\n",
  584. __func__, len, avail);
  585. }
  586. /* This function is called after Program and Erase Operations to
  587. * check for success or failure.
  588. */
  589. static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  590. {
  591. struct fsl_ifc_mtd *priv = chip->priv;
  592. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  593. struct fsl_ifc *ifc = ctrl->regs;
  594. u32 nand_fsr;
  595. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  596. return NAND_STATUS_FAIL;
  597. /* Use READ_STATUS command, but wait for the device to be ready */
  598. ifc_out32(&ifc->ifc_nand.nand_fir0,
  599. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  600. (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT));
  601. ifc_out32(&ifc->ifc_nand.nand_fcr0, NAND_CMD_STATUS <<
  602. IFC_NAND_FCR0_CMD0_SHIFT);
  603. ifc_out32(&ifc->ifc_nand.nand_fbcr, 1);
  604. set_addr(mtd, 0, 0, 0);
  605. ctrl->read_bytes = 1;
  606. fsl_ifc_run_command(mtd);
  607. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  608. return NAND_STATUS_FAIL;
  609. nand_fsr = ifc_in32(&ifc->ifc_nand.nand_fsr);
  610. /* Chip sometimes reporting write protect even when it's not */
  611. nand_fsr = nand_fsr | NAND_STATUS_WP;
  612. return nand_fsr;
  613. }
  614. static int fsl_ifc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  615. uint8_t *buf, int oob_required, int page)
  616. {
  617. struct fsl_ifc_mtd *priv = chip->priv;
  618. struct fsl_ifc_ctrl *ctrl = priv->ctrl;
  619. fsl_ifc_read_buf(mtd, buf, mtd->writesize);
  620. fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  621. if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
  622. mtd->ecc_stats.failed++;
  623. return 0;
  624. }
  625. /* ECC will be calculated automatically, and errors will be detected in
  626. * waitfunc.
  627. */
  628. static int fsl_ifc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  629. const uint8_t *buf, int oob_required)
  630. {
  631. fsl_ifc_write_buf(mtd, buf, mtd->writesize);
  632. fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  633. return 0;
  634. }
  635. static void fsl_ifc_ctrl_init(void)
  636. {
  637. ifc_ctrl = kzalloc(sizeof(*ifc_ctrl), GFP_KERNEL);
  638. if (!ifc_ctrl)
  639. return;
  640. ifc_ctrl->regs = IFC_BASE_ADDR;
  641. /* clear event registers */
  642. ifc_out32(&ifc_ctrl->regs->ifc_nand.nand_evter_stat, ~0U);
  643. ifc_out32(&ifc_ctrl->regs->ifc_nand.pgrdcmpl_evt_stat, ~0U);
  644. /* Enable error and event for any detected errors */
  645. ifc_out32(&ifc_ctrl->regs->ifc_nand.nand_evter_en,
  646. IFC_NAND_EVTER_EN_OPC_EN |
  647. IFC_NAND_EVTER_EN_PGRDCMPL_EN |
  648. IFC_NAND_EVTER_EN_FTOER_EN |
  649. IFC_NAND_EVTER_EN_WPER_EN);
  650. ifc_out32(&ifc_ctrl->regs->ifc_nand.ncfgr, 0x0);
  651. }
  652. static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
  653. {
  654. }
  655. static int fsl_ifc_sram_init(uint32_t ver)
  656. {
  657. struct fsl_ifc *ifc = ifc_ctrl->regs;
  658. uint32_t cs = 0, csor = 0, csor_8k = 0, csor_ext = 0;
  659. uint32_t ncfgr = 0;
  660. u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
  661. u32 time_start;
  662. if (ver > FSL_IFC_V1_1_0) {
  663. ncfgr = ifc_in32(&ifc->ifc_nand.ncfgr);
  664. ifc_out32(&ifc->ifc_nand.ncfgr, ncfgr | IFC_NAND_SRAM_INIT_EN);
  665. /* wait for SRAM_INIT bit to be clear or timeout */
  666. time_start = get_timer(0);
  667. while (get_timer(time_start) < timeo) {
  668. ifc_ctrl->status =
  669. ifc_in32(&ifc->ifc_nand.nand_evter_stat);
  670. if (!(ifc_ctrl->status & IFC_NAND_SRAM_INIT_EN))
  671. return 0;
  672. }
  673. printf("fsl-ifc: Failed to Initialise SRAM\n");
  674. return 1;
  675. }
  676. cs = ifc_ctrl->cs_nand >> IFC_NAND_CSEL_SHIFT;
  677. /* Save CSOR and CSOR_ext */
  678. csor = ifc_in32(&ifc_ctrl->regs->csor_cs[cs].csor);
  679. csor_ext = ifc_in32(&ifc_ctrl->regs->csor_cs[cs].csor_ext);
  680. /* chage PageSize 8K and SpareSize 1K*/
  681. csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
  682. ifc_out32(&ifc_ctrl->regs->csor_cs[cs].csor, csor_8k);
  683. ifc_out32(&ifc_ctrl->regs->csor_cs[cs].csor_ext, 0x0000400);
  684. /* READID */
  685. ifc_out32(&ifc->ifc_nand.nand_fir0,
  686. (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
  687. (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
  688. (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT));
  689. ifc_out32(&ifc->ifc_nand.nand_fcr0,
  690. NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT);
  691. ifc_out32(&ifc->ifc_nand.row3, 0x0);
  692. ifc_out32(&ifc->ifc_nand.nand_fbcr, 0x0);
  693. /* Program ROW0/COL0 */
  694. ifc_out32(&ifc->ifc_nand.row0, 0x0);
  695. ifc_out32(&ifc->ifc_nand.col0, 0x0);
  696. /* set the chip select for NAND Transaction */
  697. ifc_out32(&ifc->ifc_nand.nand_csel, ifc_ctrl->cs_nand);
  698. /* start read seq */
  699. ifc_out32(&ifc->ifc_nand.nandseq_strt, IFC_NAND_SEQ_STRT_FIR_STRT);
  700. time_start = get_timer(0);
  701. while (get_timer(time_start) < timeo) {
  702. ifc_ctrl->status = ifc_in32(&ifc->ifc_nand.nand_evter_stat);
  703. if (ifc_ctrl->status & IFC_NAND_EVTER_STAT_OPC)
  704. break;
  705. }
  706. if (ifc_ctrl->status != IFC_NAND_EVTER_STAT_OPC) {
  707. printf("fsl-ifc: Failed to Initialise SRAM\n");
  708. return 1;
  709. }
  710. ifc_out32(&ifc->ifc_nand.nand_evter_stat, ifc_ctrl->status);
  711. /* Restore CSOR and CSOR_ext */
  712. ifc_out32(&ifc_ctrl->regs->csor_cs[cs].csor, csor);
  713. ifc_out32(&ifc_ctrl->regs->csor_cs[cs].csor_ext, csor_ext);
  714. return 0;
  715. }
  716. static int fsl_ifc_chip_init(int devnum, u8 *addr)
  717. {
  718. struct mtd_info *mtd = &nand_info[devnum];
  719. struct nand_chip *nand;
  720. struct fsl_ifc_mtd *priv;
  721. struct nand_ecclayout *layout;
  722. uint32_t cspr = 0, csor = 0, ver = 0;
  723. int ret = 0;
  724. if (!ifc_ctrl) {
  725. fsl_ifc_ctrl_init();
  726. if (!ifc_ctrl)
  727. return -1;
  728. }
  729. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  730. if (!priv)
  731. return -ENOMEM;
  732. priv->ctrl = ifc_ctrl;
  733. priv->vbase = addr;
  734. /* Find which chip select it is connected to.
  735. */
  736. for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
  737. phys_addr_t phys_addr = virt_to_phys(addr);
  738. cspr = ifc_in32(&ifc_ctrl->regs->cspr_cs[priv->bank].cspr);
  739. csor = ifc_in32(&ifc_ctrl->regs->csor_cs[priv->bank].csor);
  740. if ((cspr & CSPR_V) && (cspr & CSPR_MSEL) == CSPR_MSEL_NAND &&
  741. (cspr & CSPR_BA) == CSPR_PHYS_ADDR(phys_addr)) {
  742. ifc_ctrl->cs_nand = priv->bank << IFC_NAND_CSEL_SHIFT;
  743. break;
  744. }
  745. }
  746. if (priv->bank >= MAX_BANKS) {
  747. printf("%s: address did not match any "
  748. "chip selects\n", __func__);
  749. kfree(priv);
  750. return -ENODEV;
  751. }
  752. nand = &priv->chip;
  753. mtd->priv = nand;
  754. ifc_ctrl->chips[priv->bank] = priv;
  755. /* fill in nand_chip structure */
  756. /* set up function call table */
  757. nand->write_buf = fsl_ifc_write_buf;
  758. nand->read_buf = fsl_ifc_read_buf;
  759. nand->select_chip = fsl_ifc_select_chip;
  760. nand->cmdfunc = fsl_ifc_cmdfunc;
  761. nand->waitfunc = fsl_ifc_wait;
  762. /* set up nand options */
  763. nand->bbt_td = &bbt_main_descr;
  764. nand->bbt_md = &bbt_mirror_descr;
  765. /* set up nand options */
  766. nand->options = NAND_NO_SUBPAGE_WRITE;
  767. nand->bbt_options = NAND_BBT_USE_FLASH;
  768. if (cspr & CSPR_PORT_SIZE_16) {
  769. nand->read_byte = fsl_ifc_read_byte16;
  770. nand->options |= NAND_BUSWIDTH_16;
  771. } else {
  772. nand->read_byte = fsl_ifc_read_byte;
  773. }
  774. nand->controller = &ifc_ctrl->controller;
  775. nand->priv = priv;
  776. nand->ecc.read_page = fsl_ifc_read_page;
  777. nand->ecc.write_page = fsl_ifc_write_page;
  778. /* Hardware generates ECC per 512 Bytes */
  779. nand->ecc.size = 512;
  780. nand->ecc.bytes = 8;
  781. switch (csor & CSOR_NAND_PGS_MASK) {
  782. case CSOR_NAND_PGS_512:
  783. if (nand->options & NAND_BUSWIDTH_16) {
  784. layout = &oob_512_16bit_ecc4;
  785. } else {
  786. layout = &oob_512_8bit_ecc4;
  787. /* Avoid conflict with bad block marker */
  788. bbt_main_descr.offs = 0;
  789. bbt_mirror_descr.offs = 0;
  790. }
  791. nand->ecc.strength = 4;
  792. priv->bufnum_mask = 15;
  793. break;
  794. case CSOR_NAND_PGS_2K:
  795. layout = &oob_2048_ecc4;
  796. nand->ecc.strength = 4;
  797. priv->bufnum_mask = 3;
  798. break;
  799. case CSOR_NAND_PGS_4K:
  800. if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
  801. CSOR_NAND_ECC_MODE_4) {
  802. layout = &oob_4096_ecc4;
  803. nand->ecc.strength = 4;
  804. } else {
  805. layout = &oob_4096_ecc8;
  806. nand->ecc.strength = 8;
  807. nand->ecc.bytes = 16;
  808. }
  809. priv->bufnum_mask = 1;
  810. break;
  811. case CSOR_NAND_PGS_8K:
  812. if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
  813. CSOR_NAND_ECC_MODE_4) {
  814. layout = &oob_8192_ecc4;
  815. nand->ecc.strength = 4;
  816. } else {
  817. layout = &oob_8192_ecc8;
  818. nand->ecc.strength = 8;
  819. nand->ecc.bytes = 16;
  820. }
  821. priv->bufnum_mask = 0;
  822. break;
  823. default:
  824. printf("ifc nand: bad csor %#x: bad page size\n", csor);
  825. return -ENODEV;
  826. }
  827. /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
  828. if (csor & CSOR_NAND_ECC_DEC_EN) {
  829. nand->ecc.mode = NAND_ECC_HW;
  830. nand->ecc.layout = layout;
  831. } else {
  832. nand->ecc.mode = NAND_ECC_SOFT;
  833. }
  834. ver = ifc_in32(&ifc_ctrl->regs->ifc_rev);
  835. if (ver >= FSL_IFC_V1_1_0)
  836. ret = fsl_ifc_sram_init(ver);
  837. if (ret)
  838. return ret;
  839. if (ver >= FSL_IFC_V2_0_0)
  840. priv->bufnum_mask = (priv->bufnum_mask * 2) + 1;
  841. ret = nand_scan_ident(mtd, 1, NULL);
  842. if (ret)
  843. return ret;
  844. ret = nand_scan_tail(mtd);
  845. if (ret)
  846. return ret;
  847. ret = nand_register(devnum);
  848. if (ret)
  849. return ret;
  850. return 0;
  851. }
  852. #ifndef CONFIG_SYS_NAND_BASE_LIST
  853. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  854. #endif
  855. static unsigned long base_address[CONFIG_SYS_MAX_NAND_DEVICE] =
  856. CONFIG_SYS_NAND_BASE_LIST;
  857. void board_nand_init(void)
  858. {
  859. int i;
  860. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  861. fsl_ifc_chip_init(i, (u8 *)base_address[i]);
  862. }