fsl_elbc_nand.c 29 KB

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