fsl_elbc_nand.c 30 KB

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