fsl_elbc_nand.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /* Freescale Enhanced Local Bus Controller NAND driver
  2. *
  3. * Copyright © 2006-2007, 2010 Freescale Semiconductor
  4. *
  5. * Authors: Nick Spence <nick.spence@freescale.com>,
  6. * Scott Wood <scottwood@freescale.com>
  7. * Jack Lan <jack.lan@freescale.com>
  8. * Roy Zang <tie-fei.zang@freescale.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/string.h>
  29. #include <linux/ioport.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/nand.h>
  36. #include <linux/mtd/nand_ecc.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <asm/io.h>
  39. #include <asm/fsl_lbc.h>
  40. #define MAX_BANKS 8
  41. #define ERR_BYTE 0xFF /* Value returned for read bytes when read failed */
  42. #define FCM_TIMEOUT_MSECS 500 /* Maximum number of mSecs to wait for FCM */
  43. /* mtd information per set */
  44. struct fsl_elbc_mtd {
  45. struct mtd_info mtd;
  46. struct nand_chip chip;
  47. struct fsl_lbc_ctrl *ctrl;
  48. struct device *dev;
  49. int bank; /* Chip select bank number */
  50. u8 __iomem *vbase; /* Chip select base virtual address */
  51. int page_size; /* NAND page size (0=512, 1=2048) */
  52. unsigned int fmr; /* FCM Flash Mode Register value */
  53. };
  54. /* Freescale eLBC FCM controller information */
  55. struct fsl_elbc_fcm_ctrl {
  56. struct nand_hw_control controller;
  57. struct fsl_elbc_mtd *chips[MAX_BANKS];
  58. u8 __iomem *addr; /* Address of assigned FCM buffer */
  59. unsigned int page; /* Last page written to / read from */
  60. unsigned int read_bytes; /* Number of bytes read during command */
  61. unsigned int column; /* Saved column from SEQIN */
  62. unsigned int index; /* Pointer to next byte to 'read' */
  63. unsigned int status; /* status read from LTESR after last op */
  64. unsigned int mdr; /* UPM/FCM Data Register value */
  65. unsigned int use_mdr; /* Non zero if the MDR is to be set */
  66. unsigned int oob; /* Non zero if operating on OOB data */
  67. unsigned int counter; /* counter for the initializations */
  68. unsigned int max_bitflips; /* Saved during READ0 cmd */
  69. };
  70. /* These map to the positions used by the FCM hardware ECC generator */
  71. /* Small Page FLASH with FMR[ECCM] = 0 */
  72. static struct nand_ecclayout fsl_elbc_oob_sp_eccm0 = {
  73. .eccbytes = 3,
  74. .eccpos = {6, 7, 8},
  75. .oobfree = { {0, 5}, {9, 7} },
  76. };
  77. /* Small Page FLASH with FMR[ECCM] = 1 */
  78. static struct nand_ecclayout fsl_elbc_oob_sp_eccm1 = {
  79. .eccbytes = 3,
  80. .eccpos = {8, 9, 10},
  81. .oobfree = { {0, 5}, {6, 2}, {11, 5} },
  82. };
  83. /* Large Page FLASH with FMR[ECCM] = 0 */
  84. static struct nand_ecclayout fsl_elbc_oob_lp_eccm0 = {
  85. .eccbytes = 12,
  86. .eccpos = {6, 7, 8, 22, 23, 24, 38, 39, 40, 54, 55, 56},
  87. .oobfree = { {1, 5}, {9, 13}, {25, 13}, {41, 13}, {57, 7} },
  88. };
  89. /* Large Page FLASH with FMR[ECCM] = 1 */
  90. static struct nand_ecclayout fsl_elbc_oob_lp_eccm1 = {
  91. .eccbytes = 12,
  92. .eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58},
  93. .oobfree = { {1, 7}, {11, 13}, {27, 13}, {43, 13}, {59, 5} },
  94. };
  95. /*
  96. * fsl_elbc_oob_lp_eccm* specify that LP NAND's OOB free area starts at offset
  97. * 1, so we have to adjust bad block pattern. This pattern should be used for
  98. * x8 chips only. So far hardware does not support x16 chips anyway.
  99. */
  100. static u8 scan_ff_pattern[] = { 0xff, };
  101. static struct nand_bbt_descr largepage_memorybased = {
  102. .options = 0,
  103. .offs = 0,
  104. .len = 1,
  105. .pattern = scan_ff_pattern,
  106. };
  107. /*
  108. * ELBC may use HW ECC, so that OOB offsets, that NAND core uses for bbt,
  109. * interfere with ECC positions, that's why we implement our own descriptors.
  110. * OOB {11, 5}, works for both SP and LP chips, with ECCM = 1 and ECCM = 0.
  111. */
  112. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  113. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  114. static struct nand_bbt_descr bbt_main_descr = {
  115. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  116. NAND_BBT_2BIT | NAND_BBT_VERSION,
  117. .offs = 11,
  118. .len = 4,
  119. .veroffs = 15,
  120. .maxblocks = 4,
  121. .pattern = bbt_pattern,
  122. };
  123. static struct nand_bbt_descr bbt_mirror_descr = {
  124. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  125. NAND_BBT_2BIT | NAND_BBT_VERSION,
  126. .offs = 11,
  127. .len = 4,
  128. .veroffs = 15,
  129. .maxblocks = 4,
  130. .pattern = mirror_pattern,
  131. };
  132. /*=================================*/
  133. /*
  134. * Set up the FCM hardware block and page address fields, and the fcm
  135. * structure addr field to point to the correct FCM buffer in memory
  136. */
  137. static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
  138. {
  139. struct nand_chip *chip = mtd->priv;
  140. struct fsl_elbc_mtd *priv = chip->priv;
  141. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  142. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  143. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  144. int buf_num;
  145. elbc_fcm_ctrl->page = page_addr;
  146. if (priv->page_size) {
  147. /*
  148. * large page size chip : FPAR[PI] save the lowest 6 bits,
  149. * FBAR[BLK] save the other bits.
  150. */
  151. out_be32(&lbc->fbar, page_addr >> 6);
  152. out_be32(&lbc->fpar,
  153. ((page_addr << FPAR_LP_PI_SHIFT) & FPAR_LP_PI) |
  154. (oob ? FPAR_LP_MS : 0) | column);
  155. buf_num = (page_addr & 1) << 2;
  156. } else {
  157. /*
  158. * small page size chip : FPAR[PI] save the lowest 5 bits,
  159. * FBAR[BLK] save the other bits.
  160. */
  161. out_be32(&lbc->fbar, page_addr >> 5);
  162. out_be32(&lbc->fpar,
  163. ((page_addr << FPAR_SP_PI_SHIFT) & FPAR_SP_PI) |
  164. (oob ? FPAR_SP_MS : 0) | column);
  165. buf_num = page_addr & 7;
  166. }
  167. elbc_fcm_ctrl->addr = priv->vbase + buf_num * 1024;
  168. elbc_fcm_ctrl->index = column;
  169. /* for OOB data point to the second half of the buffer */
  170. if (oob)
  171. elbc_fcm_ctrl->index += priv->page_size ? 2048 : 512;
  172. dev_vdbg(priv->dev, "set_addr: bank=%d, "
  173. "elbc_fcm_ctrl->addr=0x%p (0x%p), "
  174. "index %x, pes %d ps %d\n",
  175. buf_num, elbc_fcm_ctrl->addr, priv->vbase,
  176. elbc_fcm_ctrl->index,
  177. chip->phys_erase_shift, chip->page_shift);
  178. }
  179. /*
  180. * execute FCM command and wait for it to complete
  181. */
  182. static int fsl_elbc_run_command(struct mtd_info *mtd)
  183. {
  184. struct nand_chip *chip = mtd->priv;
  185. struct fsl_elbc_mtd *priv = chip->priv;
  186. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  187. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  188. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  189. /* Setup the FMR[OP] to execute without write protection */
  190. out_be32(&lbc->fmr, priv->fmr | 3);
  191. if (elbc_fcm_ctrl->use_mdr)
  192. out_be32(&lbc->mdr, elbc_fcm_ctrl->mdr);
  193. dev_vdbg(priv->dev,
  194. "fsl_elbc_run_command: fmr=%08x fir=%08x fcr=%08x\n",
  195. in_be32(&lbc->fmr), in_be32(&lbc->fir), in_be32(&lbc->fcr));
  196. dev_vdbg(priv->dev,
  197. "fsl_elbc_run_command: fbar=%08x fpar=%08x "
  198. "fbcr=%08x bank=%d\n",
  199. in_be32(&lbc->fbar), in_be32(&lbc->fpar),
  200. in_be32(&lbc->fbcr), priv->bank);
  201. ctrl->irq_status = 0;
  202. /* execute special operation */
  203. out_be32(&lbc->lsor, priv->bank);
  204. /* wait for FCM complete flag or timeout */
  205. wait_event_timeout(ctrl->irq_wait, ctrl->irq_status,
  206. FCM_TIMEOUT_MSECS * HZ/1000);
  207. elbc_fcm_ctrl->status = ctrl->irq_status;
  208. /* store mdr value in case it was needed */
  209. if (elbc_fcm_ctrl->use_mdr)
  210. elbc_fcm_ctrl->mdr = in_be32(&lbc->mdr);
  211. elbc_fcm_ctrl->use_mdr = 0;
  212. if (elbc_fcm_ctrl->status != LTESR_CC) {
  213. dev_info(priv->dev,
  214. "command failed: fir %x fcr %x status %x mdr %x\n",
  215. in_be32(&lbc->fir), in_be32(&lbc->fcr),
  216. elbc_fcm_ctrl->status, elbc_fcm_ctrl->mdr);
  217. return -EIO;
  218. }
  219. if (chip->ecc.mode != NAND_ECC_HW)
  220. return 0;
  221. elbc_fcm_ctrl->max_bitflips = 0;
  222. if (elbc_fcm_ctrl->read_bytes == mtd->writesize + mtd->oobsize) {
  223. uint32_t lteccr = in_be32(&lbc->lteccr);
  224. /*
  225. * if command was a full page read and the ELBC
  226. * has the LTECCR register, then bits 12-15 (ppc order) of
  227. * LTECCR indicates which 512 byte sub-pages had fixed errors.
  228. * bits 28-31 are uncorrectable errors, marked elsewhere.
  229. * for small page nand only 1 bit is used.
  230. * if the ELBC doesn't have the lteccr register it reads 0
  231. * FIXME: 4 bits can be corrected on NANDs with 2k pages, so
  232. * count the number of sub-pages with bitflips and update
  233. * ecc_stats.corrected accordingly.
  234. */
  235. if (lteccr & 0x000F000F)
  236. out_be32(&lbc->lteccr, 0x000F000F); /* clear lteccr */
  237. if (lteccr & 0x000F0000) {
  238. mtd->ecc_stats.corrected++;
  239. elbc_fcm_ctrl->max_bitflips = 1;
  240. }
  241. }
  242. return 0;
  243. }
  244. static void fsl_elbc_do_read(struct nand_chip *chip, int oob)
  245. {
  246. struct fsl_elbc_mtd *priv = chip->priv;
  247. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  248. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  249. if (priv->page_size) {
  250. out_be32(&lbc->fir,
  251. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  252. (FIR_OP_CA << FIR_OP1_SHIFT) |
  253. (FIR_OP_PA << FIR_OP2_SHIFT) |
  254. (FIR_OP_CM1 << FIR_OP3_SHIFT) |
  255. (FIR_OP_RBW << FIR_OP4_SHIFT));
  256. out_be32(&lbc->fcr, (NAND_CMD_READ0 << FCR_CMD0_SHIFT) |
  257. (NAND_CMD_READSTART << FCR_CMD1_SHIFT));
  258. } else {
  259. out_be32(&lbc->fir,
  260. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  261. (FIR_OP_CA << FIR_OP1_SHIFT) |
  262. (FIR_OP_PA << FIR_OP2_SHIFT) |
  263. (FIR_OP_RBW << FIR_OP3_SHIFT));
  264. if (oob)
  265. out_be32(&lbc->fcr, NAND_CMD_READOOB << FCR_CMD0_SHIFT);
  266. else
  267. out_be32(&lbc->fcr, NAND_CMD_READ0 << FCR_CMD0_SHIFT);
  268. }
  269. }
  270. /* cmdfunc send commands to the FCM */
  271. static void fsl_elbc_cmdfunc(struct mtd_info *mtd, unsigned int command,
  272. int column, int page_addr)
  273. {
  274. struct nand_chip *chip = mtd->priv;
  275. struct fsl_elbc_mtd *priv = chip->priv;
  276. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  277. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  278. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  279. elbc_fcm_ctrl->use_mdr = 0;
  280. /* clear the read buffer */
  281. elbc_fcm_ctrl->read_bytes = 0;
  282. if (command != NAND_CMD_PAGEPROG)
  283. elbc_fcm_ctrl->index = 0;
  284. switch (command) {
  285. /* READ0 and READ1 read the entire buffer to use hardware ECC. */
  286. case NAND_CMD_READ1:
  287. column += 256;
  288. /* fall-through */
  289. case NAND_CMD_READ0:
  290. dev_dbg(priv->dev,
  291. "fsl_elbc_cmdfunc: NAND_CMD_READ0, page_addr:"
  292. " 0x%x, column: 0x%x.\n", page_addr, column);
  293. out_be32(&lbc->fbcr, 0); /* read entire page to enable ECC */
  294. set_addr(mtd, 0, page_addr, 0);
  295. elbc_fcm_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  296. elbc_fcm_ctrl->index += column;
  297. fsl_elbc_do_read(chip, 0);
  298. fsl_elbc_run_command(mtd);
  299. return;
  300. /* READOOB reads only the OOB because no ECC is performed. */
  301. case NAND_CMD_READOOB:
  302. dev_vdbg(priv->dev,
  303. "fsl_elbc_cmdfunc: NAND_CMD_READOOB, page_addr:"
  304. " 0x%x, column: 0x%x.\n", page_addr, column);
  305. out_be32(&lbc->fbcr, mtd->oobsize - column);
  306. set_addr(mtd, column, page_addr, 1);
  307. elbc_fcm_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
  308. fsl_elbc_do_read(chip, 1);
  309. fsl_elbc_run_command(mtd);
  310. return;
  311. case NAND_CMD_READID:
  312. case NAND_CMD_PARAM:
  313. dev_vdbg(priv->dev, "fsl_elbc_cmdfunc: NAND_CMD %x\n", command);
  314. out_be32(&lbc->fir, (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  315. (FIR_OP_UA << FIR_OP1_SHIFT) |
  316. (FIR_OP_RBW << FIR_OP2_SHIFT));
  317. out_be32(&lbc->fcr, command << FCR_CMD0_SHIFT);
  318. /*
  319. * although currently it's 8 bytes for READID, we always read
  320. * the maximum 256 bytes(for PARAM)
  321. */
  322. out_be32(&lbc->fbcr, 256);
  323. elbc_fcm_ctrl->read_bytes = 256;
  324. elbc_fcm_ctrl->use_mdr = 1;
  325. elbc_fcm_ctrl->mdr = column;
  326. set_addr(mtd, 0, 0, 0);
  327. fsl_elbc_run_command(mtd);
  328. return;
  329. /* ERASE1 stores the block and page address */
  330. case NAND_CMD_ERASE1:
  331. dev_vdbg(priv->dev,
  332. "fsl_elbc_cmdfunc: NAND_CMD_ERASE1, "
  333. "page_addr: 0x%x.\n", page_addr);
  334. set_addr(mtd, 0, page_addr, 0);
  335. return;
  336. /* ERASE2 uses the block and page address from ERASE1 */
  337. case NAND_CMD_ERASE2:
  338. dev_vdbg(priv->dev, "fsl_elbc_cmdfunc: NAND_CMD_ERASE2.\n");
  339. out_be32(&lbc->fir,
  340. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  341. (FIR_OP_PA << FIR_OP1_SHIFT) |
  342. (FIR_OP_CM2 << FIR_OP2_SHIFT) |
  343. (FIR_OP_CW1 << FIR_OP3_SHIFT) |
  344. (FIR_OP_RS << FIR_OP4_SHIFT));
  345. out_be32(&lbc->fcr,
  346. (NAND_CMD_ERASE1 << FCR_CMD0_SHIFT) |
  347. (NAND_CMD_STATUS << FCR_CMD1_SHIFT) |
  348. (NAND_CMD_ERASE2 << FCR_CMD2_SHIFT));
  349. out_be32(&lbc->fbcr, 0);
  350. elbc_fcm_ctrl->read_bytes = 0;
  351. elbc_fcm_ctrl->use_mdr = 1;
  352. fsl_elbc_run_command(mtd);
  353. return;
  354. /* SEQIN sets up the addr buffer and all registers except the length */
  355. case NAND_CMD_SEQIN: {
  356. __be32 fcr;
  357. dev_vdbg(priv->dev,
  358. "fsl_elbc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, "
  359. "page_addr: 0x%x, column: 0x%x.\n",
  360. page_addr, column);
  361. elbc_fcm_ctrl->column = column;
  362. elbc_fcm_ctrl->use_mdr = 1;
  363. if (column >= mtd->writesize) {
  364. /* OOB area */
  365. column -= mtd->writesize;
  366. elbc_fcm_ctrl->oob = 1;
  367. } else {
  368. WARN_ON(column != 0);
  369. elbc_fcm_ctrl->oob = 0;
  370. }
  371. fcr = (NAND_CMD_STATUS << FCR_CMD1_SHIFT) |
  372. (NAND_CMD_SEQIN << FCR_CMD2_SHIFT) |
  373. (NAND_CMD_PAGEPROG << FCR_CMD3_SHIFT);
  374. if (priv->page_size) {
  375. out_be32(&lbc->fir,
  376. (FIR_OP_CM2 << FIR_OP0_SHIFT) |
  377. (FIR_OP_CA << FIR_OP1_SHIFT) |
  378. (FIR_OP_PA << FIR_OP2_SHIFT) |
  379. (FIR_OP_WB << FIR_OP3_SHIFT) |
  380. (FIR_OP_CM3 << FIR_OP4_SHIFT) |
  381. (FIR_OP_CW1 << FIR_OP5_SHIFT) |
  382. (FIR_OP_RS << FIR_OP6_SHIFT));
  383. } else {
  384. out_be32(&lbc->fir,
  385. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  386. (FIR_OP_CM2 << FIR_OP1_SHIFT) |
  387. (FIR_OP_CA << FIR_OP2_SHIFT) |
  388. (FIR_OP_PA << FIR_OP3_SHIFT) |
  389. (FIR_OP_WB << FIR_OP4_SHIFT) |
  390. (FIR_OP_CM3 << FIR_OP5_SHIFT) |
  391. (FIR_OP_CW1 << FIR_OP6_SHIFT) |
  392. (FIR_OP_RS << FIR_OP7_SHIFT));
  393. if (elbc_fcm_ctrl->oob)
  394. /* OOB area --> READOOB */
  395. fcr |= NAND_CMD_READOOB << FCR_CMD0_SHIFT;
  396. else
  397. /* First 256 bytes --> READ0 */
  398. fcr |= NAND_CMD_READ0 << FCR_CMD0_SHIFT;
  399. }
  400. out_be32(&lbc->fcr, fcr);
  401. set_addr(mtd, column, page_addr, elbc_fcm_ctrl->oob);
  402. return;
  403. }
  404. /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
  405. case NAND_CMD_PAGEPROG: {
  406. dev_vdbg(priv->dev,
  407. "fsl_elbc_cmdfunc: NAND_CMD_PAGEPROG "
  408. "writing %d bytes.\n", elbc_fcm_ctrl->index);
  409. /* if the write did not start at 0 or is not a full page
  410. * then set the exact length, otherwise use a full page
  411. * write so the HW generates the ECC.
  412. */
  413. if (elbc_fcm_ctrl->oob || elbc_fcm_ctrl->column != 0 ||
  414. elbc_fcm_ctrl->index != mtd->writesize + mtd->oobsize)
  415. out_be32(&lbc->fbcr,
  416. elbc_fcm_ctrl->index - elbc_fcm_ctrl->column);
  417. else
  418. out_be32(&lbc->fbcr, 0);
  419. fsl_elbc_run_command(mtd);
  420. return;
  421. }
  422. /* CMD_STATUS must read the status byte while CEB is active */
  423. /* Note - it does not wait for the ready line */
  424. case NAND_CMD_STATUS:
  425. out_be32(&lbc->fir,
  426. (FIR_OP_CM0 << FIR_OP0_SHIFT) |
  427. (FIR_OP_RBW << FIR_OP1_SHIFT));
  428. out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
  429. out_be32(&lbc->fbcr, 1);
  430. set_addr(mtd, 0, 0, 0);
  431. elbc_fcm_ctrl->read_bytes = 1;
  432. fsl_elbc_run_command(mtd);
  433. /* The chip always seems to report that it is
  434. * write-protected, even when it is not.
  435. */
  436. setbits8(elbc_fcm_ctrl->addr, NAND_STATUS_WP);
  437. return;
  438. /* RESET without waiting for the ready line */
  439. case NAND_CMD_RESET:
  440. dev_dbg(priv->dev, "fsl_elbc_cmdfunc: NAND_CMD_RESET.\n");
  441. out_be32(&lbc->fir, FIR_OP_CM0 << FIR_OP0_SHIFT);
  442. out_be32(&lbc->fcr, NAND_CMD_RESET << FCR_CMD0_SHIFT);
  443. fsl_elbc_run_command(mtd);
  444. return;
  445. default:
  446. dev_err(priv->dev,
  447. "fsl_elbc_cmdfunc: error, unsupported command 0x%x.\n",
  448. command);
  449. }
  450. }
  451. static void fsl_elbc_select_chip(struct mtd_info *mtd, int chip)
  452. {
  453. /* The hardware does not seem to support multiple
  454. * chips per bank.
  455. */
  456. }
  457. /*
  458. * Write buf to the FCM Controller Data Buffer
  459. */
  460. static void fsl_elbc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  461. {
  462. struct nand_chip *chip = mtd->priv;
  463. struct fsl_elbc_mtd *priv = chip->priv;
  464. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  465. unsigned int bufsize = mtd->writesize + mtd->oobsize;
  466. if (len <= 0) {
  467. dev_err(priv->dev, "write_buf of %d bytes", len);
  468. elbc_fcm_ctrl->status = 0;
  469. return;
  470. }
  471. if ((unsigned int)len > bufsize - elbc_fcm_ctrl->index) {
  472. dev_err(priv->dev,
  473. "write_buf beyond end of buffer "
  474. "(%d requested, %u available)\n",
  475. len, bufsize - elbc_fcm_ctrl->index);
  476. len = bufsize - elbc_fcm_ctrl->index;
  477. }
  478. memcpy_toio(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index], buf, len);
  479. /*
  480. * This is workaround for the weird elbc hangs during nand write,
  481. * Scott Wood says: "...perhaps difference in how long it takes a
  482. * write to make it through the localbus compared to a write to IMMR
  483. * is causing problems, and sync isn't helping for some reason."
  484. * Reading back the last byte helps though.
  485. */
  486. in_8(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index] + len - 1);
  487. elbc_fcm_ctrl->index += len;
  488. }
  489. /*
  490. * read a byte from either the FCM hardware buffer if it has any data left
  491. * otherwise issue a command to read a single byte.
  492. */
  493. static u8 fsl_elbc_read_byte(struct mtd_info *mtd)
  494. {
  495. struct nand_chip *chip = mtd->priv;
  496. struct fsl_elbc_mtd *priv = chip->priv;
  497. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  498. /* If there are still bytes in the FCM, then use the next byte. */
  499. if (elbc_fcm_ctrl->index < elbc_fcm_ctrl->read_bytes)
  500. return in_8(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index++]);
  501. dev_err(priv->dev, "read_byte beyond end of buffer\n");
  502. return ERR_BYTE;
  503. }
  504. /*
  505. * Read from the FCM Controller Data Buffer
  506. */
  507. static void fsl_elbc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  508. {
  509. struct nand_chip *chip = mtd->priv;
  510. struct fsl_elbc_mtd *priv = chip->priv;
  511. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  512. int avail;
  513. if (len < 0)
  514. return;
  515. avail = min((unsigned int)len,
  516. elbc_fcm_ctrl->read_bytes - elbc_fcm_ctrl->index);
  517. memcpy_fromio(buf, &elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index], avail);
  518. elbc_fcm_ctrl->index += avail;
  519. if (len > avail)
  520. dev_err(priv->dev,
  521. "read_buf beyond end of buffer "
  522. "(%d requested, %d available)\n",
  523. len, avail);
  524. }
  525. /*
  526. * Verify buffer against the FCM Controller Data Buffer
  527. */
  528. static int fsl_elbc_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  529. {
  530. struct nand_chip *chip = mtd->priv;
  531. struct fsl_elbc_mtd *priv = chip->priv;
  532. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  533. int i;
  534. if (len < 0) {
  535. dev_err(priv->dev, "write_buf of %d bytes", len);
  536. return -EINVAL;
  537. }
  538. if ((unsigned int)len >
  539. elbc_fcm_ctrl->read_bytes - elbc_fcm_ctrl->index) {
  540. dev_err(priv->dev,
  541. "verify_buf beyond end of buffer "
  542. "(%d requested, %u available)\n",
  543. len, elbc_fcm_ctrl->read_bytes - elbc_fcm_ctrl->index);
  544. elbc_fcm_ctrl->index = elbc_fcm_ctrl->read_bytes;
  545. return -EINVAL;
  546. }
  547. for (i = 0; i < len; i++)
  548. if (in_8(&elbc_fcm_ctrl->addr[elbc_fcm_ctrl->index + i])
  549. != buf[i])
  550. break;
  551. elbc_fcm_ctrl->index += len;
  552. return i == len && elbc_fcm_ctrl->status == LTESR_CC ? 0 : -EIO;
  553. }
  554. /* This function is called after Program and Erase Operations to
  555. * check for success or failure.
  556. */
  557. static int fsl_elbc_wait(struct mtd_info *mtd, struct nand_chip *chip)
  558. {
  559. struct fsl_elbc_mtd *priv = chip->priv;
  560. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  561. if (elbc_fcm_ctrl->status != LTESR_CC)
  562. return NAND_STATUS_FAIL;
  563. /* The chip always seems to report that it is
  564. * write-protected, even when it is not.
  565. */
  566. return (elbc_fcm_ctrl->mdr & 0xff) | NAND_STATUS_WP;
  567. }
  568. static int fsl_elbc_chip_init_tail(struct mtd_info *mtd)
  569. {
  570. struct nand_chip *chip = mtd->priv;
  571. struct fsl_elbc_mtd *priv = chip->priv;
  572. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  573. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  574. unsigned int al;
  575. /* calculate FMR Address Length field */
  576. al = 0;
  577. if (chip->pagemask & 0xffff0000)
  578. al++;
  579. if (chip->pagemask & 0xff000000)
  580. al++;
  581. priv->fmr |= al << FMR_AL_SHIFT;
  582. dev_dbg(priv->dev, "fsl_elbc_init: nand->numchips = %d\n",
  583. chip->numchips);
  584. dev_dbg(priv->dev, "fsl_elbc_init: nand->chipsize = %lld\n",
  585. chip->chipsize);
  586. dev_dbg(priv->dev, "fsl_elbc_init: nand->pagemask = %8x\n",
  587. chip->pagemask);
  588. dev_dbg(priv->dev, "fsl_elbc_init: nand->chip_delay = %d\n",
  589. chip->chip_delay);
  590. dev_dbg(priv->dev, "fsl_elbc_init: nand->badblockpos = %d\n",
  591. chip->badblockpos);
  592. dev_dbg(priv->dev, "fsl_elbc_init: nand->chip_shift = %d\n",
  593. chip->chip_shift);
  594. dev_dbg(priv->dev, "fsl_elbc_init: nand->page_shift = %d\n",
  595. chip->page_shift);
  596. dev_dbg(priv->dev, "fsl_elbc_init: nand->phys_erase_shift = %d\n",
  597. chip->phys_erase_shift);
  598. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecclayout = %p\n",
  599. chip->ecclayout);
  600. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.mode = %d\n",
  601. chip->ecc.mode);
  602. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.steps = %d\n",
  603. chip->ecc.steps);
  604. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.bytes = %d\n",
  605. chip->ecc.bytes);
  606. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.total = %d\n",
  607. chip->ecc.total);
  608. dev_dbg(priv->dev, "fsl_elbc_init: nand->ecc.layout = %p\n",
  609. chip->ecc.layout);
  610. dev_dbg(priv->dev, "fsl_elbc_init: mtd->flags = %08x\n", mtd->flags);
  611. dev_dbg(priv->dev, "fsl_elbc_init: mtd->size = %lld\n", mtd->size);
  612. dev_dbg(priv->dev, "fsl_elbc_init: mtd->erasesize = %d\n",
  613. mtd->erasesize);
  614. dev_dbg(priv->dev, "fsl_elbc_init: mtd->writesize = %d\n",
  615. mtd->writesize);
  616. dev_dbg(priv->dev, "fsl_elbc_init: mtd->oobsize = %d\n",
  617. mtd->oobsize);
  618. /* adjust Option Register and ECC to match Flash page size */
  619. if (mtd->writesize == 512) {
  620. priv->page_size = 0;
  621. clrbits32(&lbc->bank[priv->bank].or, OR_FCM_PGS);
  622. } else if (mtd->writesize == 2048) {
  623. priv->page_size = 1;
  624. setbits32(&lbc->bank[priv->bank].or, OR_FCM_PGS);
  625. /* adjust ecc setup if needed */
  626. if ((in_be32(&lbc->bank[priv->bank].br) & BR_DECC) ==
  627. BR_DECC_CHK_GEN) {
  628. chip->ecc.size = 512;
  629. chip->ecc.layout = (priv->fmr & FMR_ECCM) ?
  630. &fsl_elbc_oob_lp_eccm1 :
  631. &fsl_elbc_oob_lp_eccm0;
  632. chip->badblock_pattern = &largepage_memorybased;
  633. }
  634. } else {
  635. dev_err(priv->dev,
  636. "fsl_elbc_init: page size %d is not supported\n",
  637. mtd->writesize);
  638. return -1;
  639. }
  640. return 0;
  641. }
  642. static int fsl_elbc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  643. uint8_t *buf, int oob_required, int page)
  644. {
  645. struct fsl_elbc_mtd *priv = chip->priv;
  646. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  647. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  648. fsl_elbc_read_buf(mtd, buf, mtd->writesize);
  649. if (oob_required)
  650. fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  651. if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL)
  652. mtd->ecc_stats.failed++;
  653. return elbc_fcm_ctrl->max_bitflips;
  654. }
  655. /* ECC will be calculated automatically, and errors will be detected in
  656. * waitfunc.
  657. */
  658. static int fsl_elbc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  659. const uint8_t *buf, int oob_required)
  660. {
  661. fsl_elbc_write_buf(mtd, buf, mtd->writesize);
  662. fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  663. return 0;
  664. }
  665. static int fsl_elbc_chip_init(struct fsl_elbc_mtd *priv)
  666. {
  667. struct fsl_lbc_ctrl *ctrl = priv->ctrl;
  668. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  669. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = ctrl->nand;
  670. struct nand_chip *chip = &priv->chip;
  671. dev_dbg(priv->dev, "eLBC Set Information for bank %d\n", priv->bank);
  672. /* Fill in fsl_elbc_mtd structure */
  673. priv->mtd.priv = chip;
  674. priv->mtd.owner = THIS_MODULE;
  675. /* set timeout to maximum */
  676. priv->fmr = 15 << FMR_CWTO_SHIFT;
  677. if (in_be32(&lbc->bank[priv->bank].or) & OR_FCM_PGS)
  678. priv->fmr |= FMR_ECCM;
  679. /* fill in nand_chip structure */
  680. /* set up function call table */
  681. chip->read_byte = fsl_elbc_read_byte;
  682. chip->write_buf = fsl_elbc_write_buf;
  683. chip->read_buf = fsl_elbc_read_buf;
  684. chip->verify_buf = fsl_elbc_verify_buf;
  685. chip->select_chip = fsl_elbc_select_chip;
  686. chip->cmdfunc = fsl_elbc_cmdfunc;
  687. chip->waitfunc = fsl_elbc_wait;
  688. chip->bbt_td = &bbt_main_descr;
  689. chip->bbt_md = &bbt_mirror_descr;
  690. /* set up nand options */
  691. chip->bbt_options = NAND_BBT_USE_FLASH;
  692. chip->controller = &elbc_fcm_ctrl->controller;
  693. chip->priv = priv;
  694. chip->ecc.read_page = fsl_elbc_read_page;
  695. chip->ecc.write_page = fsl_elbc_write_page;
  696. /* If CS Base Register selects full hardware ECC then use it */
  697. if ((in_be32(&lbc->bank[priv->bank].br) & BR_DECC) ==
  698. BR_DECC_CHK_GEN) {
  699. chip->ecc.mode = NAND_ECC_HW;
  700. /* put in small page settings and adjust later if needed */
  701. chip->ecc.layout = (priv->fmr & FMR_ECCM) ?
  702. &fsl_elbc_oob_sp_eccm1 : &fsl_elbc_oob_sp_eccm0;
  703. chip->ecc.size = 512;
  704. chip->ecc.bytes = 3;
  705. chip->ecc.strength = 1;
  706. } else {
  707. /* otherwise fall back to default software ECC */
  708. chip->ecc.mode = NAND_ECC_SOFT;
  709. }
  710. return 0;
  711. }
  712. static int fsl_elbc_chip_remove(struct fsl_elbc_mtd *priv)
  713. {
  714. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = priv->ctrl->nand;
  715. nand_release(&priv->mtd);
  716. kfree(priv->mtd.name);
  717. if (priv->vbase)
  718. iounmap(priv->vbase);
  719. elbc_fcm_ctrl->chips[priv->bank] = NULL;
  720. kfree(priv);
  721. return 0;
  722. }
  723. static DEFINE_MUTEX(fsl_elbc_nand_mutex);
  724. static int __devinit fsl_elbc_nand_probe(struct platform_device *pdev)
  725. {
  726. struct fsl_lbc_regs __iomem *lbc;
  727. struct fsl_elbc_mtd *priv;
  728. struct resource res;
  729. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl;
  730. static const char *part_probe_types[]
  731. = { "cmdlinepart", "RedBoot", "ofpart", NULL };
  732. int ret;
  733. int bank;
  734. struct device *dev;
  735. struct device_node *node = pdev->dev.of_node;
  736. struct mtd_part_parser_data ppdata;
  737. ppdata.of_node = pdev->dev.of_node;
  738. if (!fsl_lbc_ctrl_dev || !fsl_lbc_ctrl_dev->regs)
  739. return -ENODEV;
  740. lbc = fsl_lbc_ctrl_dev->regs;
  741. dev = fsl_lbc_ctrl_dev->dev;
  742. /* get, allocate and map the memory resource */
  743. ret = of_address_to_resource(node, 0, &res);
  744. if (ret) {
  745. dev_err(dev, "failed to get resource\n");
  746. return ret;
  747. }
  748. /* find which chip select it is connected to */
  749. for (bank = 0; bank < MAX_BANKS; bank++)
  750. if ((in_be32(&lbc->bank[bank].br) & BR_V) &&
  751. (in_be32(&lbc->bank[bank].br) & BR_MSEL) == BR_MS_FCM &&
  752. (in_be32(&lbc->bank[bank].br) &
  753. in_be32(&lbc->bank[bank].or) & BR_BA)
  754. == fsl_lbc_addr(res.start))
  755. break;
  756. if (bank >= MAX_BANKS) {
  757. dev_err(dev, "address did not match any chip selects\n");
  758. return -ENODEV;
  759. }
  760. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  761. if (!priv)
  762. return -ENOMEM;
  763. mutex_lock(&fsl_elbc_nand_mutex);
  764. if (!fsl_lbc_ctrl_dev->nand) {
  765. elbc_fcm_ctrl = kzalloc(sizeof(*elbc_fcm_ctrl), GFP_KERNEL);
  766. if (!elbc_fcm_ctrl) {
  767. dev_err(dev, "failed to allocate memory\n");
  768. mutex_unlock(&fsl_elbc_nand_mutex);
  769. ret = -ENOMEM;
  770. goto err;
  771. }
  772. elbc_fcm_ctrl->counter++;
  773. spin_lock_init(&elbc_fcm_ctrl->controller.lock);
  774. init_waitqueue_head(&elbc_fcm_ctrl->controller.wq);
  775. fsl_lbc_ctrl_dev->nand = elbc_fcm_ctrl;
  776. } else {
  777. elbc_fcm_ctrl = fsl_lbc_ctrl_dev->nand;
  778. }
  779. mutex_unlock(&fsl_elbc_nand_mutex);
  780. elbc_fcm_ctrl->chips[bank] = priv;
  781. priv->bank = bank;
  782. priv->ctrl = fsl_lbc_ctrl_dev;
  783. priv->dev = &pdev->dev;
  784. dev_set_drvdata(priv->dev, priv);
  785. priv->vbase = ioremap(res.start, resource_size(&res));
  786. if (!priv->vbase) {
  787. dev_err(dev, "failed to map chip region\n");
  788. ret = -ENOMEM;
  789. goto err;
  790. }
  791. priv->mtd.name = kasprintf(GFP_KERNEL, "%x.flash", (unsigned)res.start);
  792. if (!priv->mtd.name) {
  793. ret = -ENOMEM;
  794. goto err;
  795. }
  796. ret = fsl_elbc_chip_init(priv);
  797. if (ret)
  798. goto err;
  799. ret = nand_scan_ident(&priv->mtd, 1, NULL);
  800. if (ret)
  801. goto err;
  802. ret = fsl_elbc_chip_init_tail(&priv->mtd);
  803. if (ret)
  804. goto err;
  805. ret = nand_scan_tail(&priv->mtd);
  806. if (ret)
  807. goto err;
  808. /* First look for RedBoot table or partitions on the command
  809. * line, these take precedence over device tree information */
  810. mtd_device_parse_register(&priv->mtd, part_probe_types, &ppdata,
  811. NULL, 0);
  812. printk(KERN_INFO "eLBC NAND device at 0x%llx, bank %d\n",
  813. (unsigned long long)res.start, priv->bank);
  814. return 0;
  815. err:
  816. fsl_elbc_chip_remove(priv);
  817. return ret;
  818. }
  819. static int fsl_elbc_nand_remove(struct platform_device *pdev)
  820. {
  821. struct fsl_elbc_fcm_ctrl *elbc_fcm_ctrl = fsl_lbc_ctrl_dev->nand;
  822. struct fsl_elbc_mtd *priv = dev_get_drvdata(&pdev->dev);
  823. fsl_elbc_chip_remove(priv);
  824. mutex_lock(&fsl_elbc_nand_mutex);
  825. elbc_fcm_ctrl->counter--;
  826. if (!elbc_fcm_ctrl->counter) {
  827. fsl_lbc_ctrl_dev->nand = NULL;
  828. kfree(elbc_fcm_ctrl);
  829. }
  830. mutex_unlock(&fsl_elbc_nand_mutex);
  831. return 0;
  832. }
  833. static const struct of_device_id fsl_elbc_nand_match[] = {
  834. { .compatible = "fsl,elbc-fcm-nand", },
  835. {}
  836. };
  837. static struct platform_driver fsl_elbc_nand_driver = {
  838. .driver = {
  839. .name = "fsl,elbc-fcm-nand",
  840. .owner = THIS_MODULE,
  841. .of_match_table = fsl_elbc_nand_match,
  842. },
  843. .probe = fsl_elbc_nand_probe,
  844. .remove = fsl_elbc_nand_remove,
  845. };
  846. module_platform_driver(fsl_elbc_nand_driver);
  847. MODULE_LICENSE("GPL");
  848. MODULE_AUTHOR("Freescale");
  849. MODULE_DESCRIPTION("Freescale Enhanced Local Bus Controller MTD NAND driver");