fsl_elbc_nand.c 30 KB

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