fsl_ifc_nand.c 29 KB

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