sh_flctl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * SuperH FLCTL nand controller
  3. *
  4. * Copyright (c) 2008 Renesas Solutions Corp.
  5. * Copyright (c) 2008 Atom Create Engineering Co., Ltd.
  6. *
  7. * Based on fsl_elbc_nand.c, Copyright (c) 2006-2007 Freescale Semiconductor
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/slab.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/nand.h>
  33. #include <linux/mtd/partitions.h>
  34. #include <linux/mtd/sh_flctl.h>
  35. static struct nand_ecclayout flctl_4secc_oob_16 = {
  36. .eccbytes = 10,
  37. .eccpos = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  38. .oobfree = {
  39. {.offset = 12,
  40. . length = 4} },
  41. };
  42. static struct nand_ecclayout flctl_4secc_oob_64 = {
  43. .eccbytes = 4 * 10,
  44. .eccpos = {
  45. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  46. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  47. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  48. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 },
  49. .oobfree = {
  50. {.offset = 2, .length = 4},
  51. {.offset = 16, .length = 6},
  52. {.offset = 32, .length = 6},
  53. {.offset = 48, .length = 6} },
  54. };
  55. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  56. static struct nand_bbt_descr flctl_4secc_smallpage = {
  57. .options = NAND_BBT_SCAN2NDPAGE,
  58. .offs = 11,
  59. .len = 1,
  60. .pattern = scan_ff_pattern,
  61. };
  62. static struct nand_bbt_descr flctl_4secc_largepage = {
  63. .options = NAND_BBT_SCAN2NDPAGE,
  64. .offs = 0,
  65. .len = 2,
  66. .pattern = scan_ff_pattern,
  67. };
  68. static void empty_fifo(struct sh_flctl *flctl)
  69. {
  70. writel(flctl->flintdmacr_base | AC1CLR | AC0CLR, FLINTDMACR(flctl));
  71. writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
  72. }
  73. static void start_translation(struct sh_flctl *flctl)
  74. {
  75. writeb(TRSTRT, FLTRCR(flctl));
  76. }
  77. static void timeout_error(struct sh_flctl *flctl, const char *str)
  78. {
  79. dev_err(&flctl->pdev->dev, "Timeout occurred in %s\n", str);
  80. }
  81. static void wait_completion(struct sh_flctl *flctl)
  82. {
  83. uint32_t timeout = LOOP_TIMEOUT_MAX;
  84. while (timeout--) {
  85. if (readb(FLTRCR(flctl)) & TREND) {
  86. writeb(0x0, FLTRCR(flctl));
  87. return;
  88. }
  89. udelay(1);
  90. }
  91. timeout_error(flctl, __func__);
  92. writeb(0x0, FLTRCR(flctl));
  93. }
  94. static void set_addr(struct mtd_info *mtd, int column, int page_addr)
  95. {
  96. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  97. uint32_t addr = 0;
  98. if (column == -1) {
  99. addr = page_addr; /* ERASE1 */
  100. } else if (page_addr != -1) {
  101. /* SEQIN, READ0, etc.. */
  102. if (flctl->chip.options & NAND_BUSWIDTH_16)
  103. column >>= 1;
  104. if (flctl->page_size) {
  105. addr = column & 0x0FFF;
  106. addr |= (page_addr & 0xff) << 16;
  107. addr |= ((page_addr >> 8) & 0xff) << 24;
  108. /* big than 128MB */
  109. if (flctl->rw_ADRCNT == ADRCNT2_E) {
  110. uint32_t addr2;
  111. addr2 = (page_addr >> 16) & 0xff;
  112. writel(addr2, FLADR2(flctl));
  113. }
  114. } else {
  115. addr = column;
  116. addr |= (page_addr & 0xff) << 8;
  117. addr |= ((page_addr >> 8) & 0xff) << 16;
  118. addr |= ((page_addr >> 16) & 0xff) << 24;
  119. }
  120. }
  121. writel(addr, FLADR(flctl));
  122. }
  123. static void wait_rfifo_ready(struct sh_flctl *flctl)
  124. {
  125. uint32_t timeout = LOOP_TIMEOUT_MAX;
  126. while (timeout--) {
  127. uint32_t val;
  128. /* check FIFO */
  129. val = readl(FLDTCNTR(flctl)) >> 16;
  130. if (val & 0xFF)
  131. return;
  132. udelay(1);
  133. }
  134. timeout_error(flctl, __func__);
  135. }
  136. static void wait_wfifo_ready(struct sh_flctl *flctl)
  137. {
  138. uint32_t len, timeout = LOOP_TIMEOUT_MAX;
  139. while (timeout--) {
  140. /* check FIFO */
  141. len = (readl(FLDTCNTR(flctl)) >> 16) & 0xFF;
  142. if (len >= 4)
  143. return;
  144. udelay(1);
  145. }
  146. timeout_error(flctl, __func__);
  147. }
  148. static enum flctl_ecc_res_t wait_recfifo_ready
  149. (struct sh_flctl *flctl, int sector_number)
  150. {
  151. uint32_t timeout = LOOP_TIMEOUT_MAX;
  152. void __iomem *ecc_reg[4];
  153. int i;
  154. int state = FL_SUCCESS;
  155. uint32_t data, size;
  156. /*
  157. * First this loops checks in FLDTCNTR if we are ready to read out the
  158. * oob data. This is the case if either all went fine without errors or
  159. * if the bottom part of the loop corrected the errors or marked them as
  160. * uncorrectable and the controller is given time to push the data into
  161. * the FIFO.
  162. */
  163. while (timeout--) {
  164. /* check if all is ok and we can read out the OOB */
  165. size = readl(FLDTCNTR(flctl)) >> 24;
  166. if ((size & 0xFF) == 4)
  167. return state;
  168. /* check if a correction code has been calculated */
  169. if (!(readl(FL4ECCCR(flctl)) & _4ECCEND)) {
  170. /*
  171. * either we wait for the fifo to be filled or a
  172. * correction pattern is being generated
  173. */
  174. udelay(1);
  175. continue;
  176. }
  177. /* check for an uncorrectable error */
  178. if (readl(FL4ECCCR(flctl)) & _4ECCFA) {
  179. /* check if we face a non-empty page */
  180. for (i = 0; i < 512; i++) {
  181. if (flctl->done_buff[i] != 0xff) {
  182. state = FL_ERROR; /* can't correct */
  183. break;
  184. }
  185. }
  186. if (state == FL_SUCCESS)
  187. dev_dbg(&flctl->pdev->dev,
  188. "reading empty sector %d, ecc error ignored\n",
  189. sector_number);
  190. writel(0, FL4ECCCR(flctl));
  191. continue;
  192. }
  193. /* start error correction */
  194. ecc_reg[0] = FL4ECCRESULT0(flctl);
  195. ecc_reg[1] = FL4ECCRESULT1(flctl);
  196. ecc_reg[2] = FL4ECCRESULT2(flctl);
  197. ecc_reg[3] = FL4ECCRESULT3(flctl);
  198. for (i = 0; i < 3; i++) {
  199. uint8_t org;
  200. int index;
  201. data = readl(ecc_reg[i]);
  202. if (flctl->page_size)
  203. index = (512 * sector_number) +
  204. (data >> 16);
  205. else
  206. index = data >> 16;
  207. org = flctl->done_buff[index];
  208. flctl->done_buff[index] = org ^ (data & 0xFF);
  209. }
  210. state = FL_REPAIRABLE;
  211. writel(0, FL4ECCCR(flctl));
  212. }
  213. timeout_error(flctl, __func__);
  214. return FL_TIMEOUT; /* timeout */
  215. }
  216. static void wait_wecfifo_ready(struct sh_flctl *flctl)
  217. {
  218. uint32_t timeout = LOOP_TIMEOUT_MAX;
  219. uint32_t len;
  220. while (timeout--) {
  221. /* check FLECFIFO */
  222. len = (readl(FLDTCNTR(flctl)) >> 24) & 0xFF;
  223. if (len >= 4)
  224. return;
  225. udelay(1);
  226. }
  227. timeout_error(flctl, __func__);
  228. }
  229. static void read_datareg(struct sh_flctl *flctl, int offset)
  230. {
  231. unsigned long data;
  232. unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];
  233. wait_completion(flctl);
  234. data = readl(FLDATAR(flctl));
  235. *buf = le32_to_cpu(data);
  236. }
  237. static void read_fiforeg(struct sh_flctl *flctl, int rlen, int offset)
  238. {
  239. int i, len_4align;
  240. unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];
  241. void *fifo_addr = (void *)FLDTFIFO(flctl);
  242. len_4align = (rlen + 3) / 4;
  243. for (i = 0; i < len_4align; i++) {
  244. wait_rfifo_ready(flctl);
  245. buf[i] = readl(fifo_addr);
  246. buf[i] = be32_to_cpu(buf[i]);
  247. }
  248. }
  249. static enum flctl_ecc_res_t read_ecfiforeg
  250. (struct sh_flctl *flctl, uint8_t *buff, int sector)
  251. {
  252. int i;
  253. enum flctl_ecc_res_t res;
  254. unsigned long *ecc_buf = (unsigned long *)buff;
  255. res = wait_recfifo_ready(flctl , sector);
  256. if (res != FL_ERROR) {
  257. for (i = 0; i < 4; i++) {
  258. ecc_buf[i] = readl(FLECFIFO(flctl));
  259. ecc_buf[i] = be32_to_cpu(ecc_buf[i]);
  260. }
  261. }
  262. return res;
  263. }
  264. static void write_fiforeg(struct sh_flctl *flctl, int rlen, int offset)
  265. {
  266. int i, len_4align;
  267. unsigned long *data = (unsigned long *)&flctl->done_buff[offset];
  268. void *fifo_addr = (void *)FLDTFIFO(flctl);
  269. len_4align = (rlen + 3) / 4;
  270. for (i = 0; i < len_4align; i++) {
  271. wait_wfifo_ready(flctl);
  272. writel(cpu_to_be32(data[i]), fifo_addr);
  273. }
  274. }
  275. static void set_cmd_regs(struct mtd_info *mtd, uint32_t cmd, uint32_t flcmcdr_val)
  276. {
  277. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  278. uint32_t flcmncr_val = flctl->flcmncr_base & ~SEL_16BIT;
  279. uint32_t flcmdcr_val, addr_len_bytes = 0;
  280. /* Set SNAND bit if page size is 2048byte */
  281. if (flctl->page_size)
  282. flcmncr_val |= SNAND_E;
  283. else
  284. flcmncr_val &= ~SNAND_E;
  285. /* default FLCMDCR val */
  286. flcmdcr_val = DOCMD1_E | DOADR_E;
  287. /* Set for FLCMDCR */
  288. switch (cmd) {
  289. case NAND_CMD_ERASE1:
  290. addr_len_bytes = flctl->erase_ADRCNT;
  291. flcmdcr_val |= DOCMD2_E;
  292. break;
  293. case NAND_CMD_READ0:
  294. case NAND_CMD_READOOB:
  295. case NAND_CMD_RNDOUT:
  296. addr_len_bytes = flctl->rw_ADRCNT;
  297. flcmdcr_val |= CDSRC_E;
  298. if (flctl->chip.options & NAND_BUSWIDTH_16)
  299. flcmncr_val |= SEL_16BIT;
  300. break;
  301. case NAND_CMD_SEQIN:
  302. /* This case is that cmd is READ0 or READ1 or READ00 */
  303. flcmdcr_val &= ~DOADR_E; /* ONLY execute 1st cmd */
  304. break;
  305. case NAND_CMD_PAGEPROG:
  306. addr_len_bytes = flctl->rw_ADRCNT;
  307. flcmdcr_val |= DOCMD2_E | CDSRC_E | SELRW;
  308. if (flctl->chip.options & NAND_BUSWIDTH_16)
  309. flcmncr_val |= SEL_16BIT;
  310. break;
  311. case NAND_CMD_READID:
  312. flcmncr_val &= ~SNAND_E;
  313. flcmdcr_val |= CDSRC_E;
  314. addr_len_bytes = ADRCNT_1;
  315. break;
  316. case NAND_CMD_STATUS:
  317. case NAND_CMD_RESET:
  318. flcmncr_val &= ~SNAND_E;
  319. flcmdcr_val &= ~(DOADR_E | DOSR_E);
  320. break;
  321. default:
  322. break;
  323. }
  324. /* Set address bytes parameter */
  325. flcmdcr_val |= addr_len_bytes;
  326. /* Now actually write */
  327. writel(flcmncr_val, FLCMNCR(flctl));
  328. writel(flcmdcr_val, FLCMDCR(flctl));
  329. writel(flcmcdr_val, FLCMCDR(flctl));
  330. }
  331. static int flctl_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  332. uint8_t *buf, int oob_required, int page)
  333. {
  334. chip->read_buf(mtd, buf, mtd->writesize);
  335. return 0;
  336. }
  337. static void flctl_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  338. const uint8_t *buf, int oob_required)
  339. {
  340. chip->write_buf(mtd, buf, mtd->writesize);
  341. }
  342. static void execmd_read_page_sector(struct mtd_info *mtd, int page_addr)
  343. {
  344. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  345. int sector, page_sectors;
  346. enum flctl_ecc_res_t ecc_result;
  347. page_sectors = flctl->page_size ? 4 : 1;
  348. set_cmd_regs(mtd, NAND_CMD_READ0,
  349. (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
  350. writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE | _4ECCCORRECT,
  351. FLCMNCR(flctl));
  352. writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
  353. writel(page_addr << 2, FLADR(flctl));
  354. empty_fifo(flctl);
  355. start_translation(flctl);
  356. for (sector = 0; sector < page_sectors; sector++) {
  357. read_fiforeg(flctl, 512, 512 * sector);
  358. ecc_result = read_ecfiforeg(flctl,
  359. &flctl->done_buff[mtd->writesize + 16 * sector],
  360. sector);
  361. switch (ecc_result) {
  362. case FL_REPAIRABLE:
  363. dev_info(&flctl->pdev->dev,
  364. "applied ecc on page 0x%x", page_addr);
  365. flctl->mtd.ecc_stats.corrected++;
  366. break;
  367. case FL_ERROR:
  368. dev_warn(&flctl->pdev->dev,
  369. "page 0x%x contains corrupted data\n",
  370. page_addr);
  371. flctl->mtd.ecc_stats.failed++;
  372. break;
  373. default:
  374. ;
  375. }
  376. }
  377. wait_completion(flctl);
  378. writel(readl(FLCMNCR(flctl)) & ~(ACM_SACCES_MODE | _4ECCCORRECT),
  379. FLCMNCR(flctl));
  380. }
  381. static void execmd_read_oob(struct mtd_info *mtd, int page_addr)
  382. {
  383. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  384. int page_sectors = flctl->page_size ? 4 : 1;
  385. int i;
  386. set_cmd_regs(mtd, NAND_CMD_READ0,
  387. (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
  388. empty_fifo(flctl);
  389. for (i = 0; i < page_sectors; i++) {
  390. set_addr(mtd, (512 + 16) * i + 512 , page_addr);
  391. writel(16, FLDTCNTR(flctl));
  392. start_translation(flctl);
  393. read_fiforeg(flctl, 16, 16 * i);
  394. wait_completion(flctl);
  395. }
  396. }
  397. static void execmd_write_page_sector(struct mtd_info *mtd)
  398. {
  399. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  400. int i, page_addr = flctl->seqin_page_addr;
  401. int sector, page_sectors;
  402. page_sectors = flctl->page_size ? 4 : 1;
  403. set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
  404. (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
  405. empty_fifo(flctl);
  406. writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE, FLCMNCR(flctl));
  407. writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
  408. writel(page_addr << 2, FLADR(flctl));
  409. start_translation(flctl);
  410. for (sector = 0; sector < page_sectors; sector++) {
  411. write_fiforeg(flctl, 512, 512 * sector);
  412. for (i = 0; i < 4; i++) {
  413. wait_wecfifo_ready(flctl); /* wait for write ready */
  414. writel(0xFFFFFFFF, FLECFIFO(flctl));
  415. }
  416. }
  417. wait_completion(flctl);
  418. writel(readl(FLCMNCR(flctl)) & ~ACM_SACCES_MODE, FLCMNCR(flctl));
  419. }
  420. static void execmd_write_oob(struct mtd_info *mtd)
  421. {
  422. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  423. int page_addr = flctl->seqin_page_addr;
  424. int sector, page_sectors;
  425. page_sectors = flctl->page_size ? 4 : 1;
  426. set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
  427. (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
  428. for (sector = 0; sector < page_sectors; sector++) {
  429. empty_fifo(flctl);
  430. set_addr(mtd, sector * 528 + 512, page_addr);
  431. writel(16, FLDTCNTR(flctl)); /* set read size */
  432. start_translation(flctl);
  433. write_fiforeg(flctl, 16, 16 * sector);
  434. wait_completion(flctl);
  435. }
  436. }
  437. static void flctl_cmdfunc(struct mtd_info *mtd, unsigned int command,
  438. int column, int page_addr)
  439. {
  440. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  441. uint32_t read_cmd = 0;
  442. pm_runtime_get_sync(&flctl->pdev->dev);
  443. flctl->read_bytes = 0;
  444. if (command != NAND_CMD_PAGEPROG)
  445. flctl->index = 0;
  446. switch (command) {
  447. case NAND_CMD_READ1:
  448. case NAND_CMD_READ0:
  449. if (flctl->hwecc) {
  450. /* read page with hwecc */
  451. execmd_read_page_sector(mtd, page_addr);
  452. break;
  453. }
  454. if (flctl->page_size)
  455. set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
  456. | command);
  457. else
  458. set_cmd_regs(mtd, command, command);
  459. set_addr(mtd, 0, page_addr);
  460. flctl->read_bytes = mtd->writesize + mtd->oobsize;
  461. if (flctl->chip.options & NAND_BUSWIDTH_16)
  462. column >>= 1;
  463. flctl->index += column;
  464. goto read_normal_exit;
  465. case NAND_CMD_READOOB:
  466. if (flctl->hwecc) {
  467. /* read page with hwecc */
  468. execmd_read_oob(mtd, page_addr);
  469. break;
  470. }
  471. if (flctl->page_size) {
  472. set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
  473. | NAND_CMD_READ0);
  474. set_addr(mtd, mtd->writesize, page_addr);
  475. } else {
  476. set_cmd_regs(mtd, command, command);
  477. set_addr(mtd, 0, page_addr);
  478. }
  479. flctl->read_bytes = mtd->oobsize;
  480. goto read_normal_exit;
  481. case NAND_CMD_RNDOUT:
  482. if (flctl->hwecc)
  483. break;
  484. if (flctl->page_size)
  485. set_cmd_regs(mtd, command, (NAND_CMD_RNDOUTSTART << 8)
  486. | command);
  487. else
  488. set_cmd_regs(mtd, command, command);
  489. set_addr(mtd, column, 0);
  490. flctl->read_bytes = mtd->writesize + mtd->oobsize - column;
  491. goto read_normal_exit;
  492. case NAND_CMD_READID:
  493. set_cmd_regs(mtd, command, command);
  494. /* READID is always performed using an 8-bit bus */
  495. if (flctl->chip.options & NAND_BUSWIDTH_16)
  496. column <<= 1;
  497. set_addr(mtd, column, 0);
  498. flctl->read_bytes = 8;
  499. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  500. empty_fifo(flctl);
  501. start_translation(flctl);
  502. read_fiforeg(flctl, flctl->read_bytes, 0);
  503. wait_completion(flctl);
  504. break;
  505. case NAND_CMD_ERASE1:
  506. flctl->erase1_page_addr = page_addr;
  507. break;
  508. case NAND_CMD_ERASE2:
  509. set_cmd_regs(mtd, NAND_CMD_ERASE1,
  510. (command << 8) | NAND_CMD_ERASE1);
  511. set_addr(mtd, -1, flctl->erase1_page_addr);
  512. start_translation(flctl);
  513. wait_completion(flctl);
  514. break;
  515. case NAND_CMD_SEQIN:
  516. if (!flctl->page_size) {
  517. /* output read command */
  518. if (column >= mtd->writesize) {
  519. column -= mtd->writesize;
  520. read_cmd = NAND_CMD_READOOB;
  521. } else if (column < 256) {
  522. read_cmd = NAND_CMD_READ0;
  523. } else {
  524. column -= 256;
  525. read_cmd = NAND_CMD_READ1;
  526. }
  527. }
  528. flctl->seqin_column = column;
  529. flctl->seqin_page_addr = page_addr;
  530. flctl->seqin_read_cmd = read_cmd;
  531. break;
  532. case NAND_CMD_PAGEPROG:
  533. empty_fifo(flctl);
  534. if (!flctl->page_size) {
  535. set_cmd_regs(mtd, NAND_CMD_SEQIN,
  536. flctl->seqin_read_cmd);
  537. set_addr(mtd, -1, -1);
  538. writel(0, FLDTCNTR(flctl)); /* set 0 size */
  539. start_translation(flctl);
  540. wait_completion(flctl);
  541. }
  542. if (flctl->hwecc) {
  543. /* write page with hwecc */
  544. if (flctl->seqin_column == mtd->writesize)
  545. execmd_write_oob(mtd);
  546. else if (!flctl->seqin_column)
  547. execmd_write_page_sector(mtd);
  548. else
  549. printk(KERN_ERR "Invalid address !?\n");
  550. break;
  551. }
  552. set_cmd_regs(mtd, command, (command << 8) | NAND_CMD_SEQIN);
  553. set_addr(mtd, flctl->seqin_column, flctl->seqin_page_addr);
  554. writel(flctl->index, FLDTCNTR(flctl)); /* set write size */
  555. start_translation(flctl);
  556. write_fiforeg(flctl, flctl->index, 0);
  557. wait_completion(flctl);
  558. break;
  559. case NAND_CMD_STATUS:
  560. set_cmd_regs(mtd, command, command);
  561. set_addr(mtd, -1, -1);
  562. flctl->read_bytes = 1;
  563. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  564. start_translation(flctl);
  565. read_datareg(flctl, 0); /* read and end */
  566. break;
  567. case NAND_CMD_RESET:
  568. set_cmd_regs(mtd, command, command);
  569. set_addr(mtd, -1, -1);
  570. writel(0, FLDTCNTR(flctl)); /* set 0 size */
  571. start_translation(flctl);
  572. wait_completion(flctl);
  573. break;
  574. default:
  575. break;
  576. }
  577. goto runtime_exit;
  578. read_normal_exit:
  579. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  580. empty_fifo(flctl);
  581. start_translation(flctl);
  582. read_fiforeg(flctl, flctl->read_bytes, 0);
  583. wait_completion(flctl);
  584. runtime_exit:
  585. pm_runtime_put_sync(&flctl->pdev->dev);
  586. return;
  587. }
  588. static void flctl_select_chip(struct mtd_info *mtd, int chipnr)
  589. {
  590. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  591. int ret;
  592. switch (chipnr) {
  593. case -1:
  594. flctl->flcmncr_base &= ~CE0_ENABLE;
  595. pm_runtime_get_sync(&flctl->pdev->dev);
  596. writel(flctl->flcmncr_base, FLCMNCR(flctl));
  597. if (flctl->qos_request) {
  598. dev_pm_qos_remove_request(&flctl->pm_qos);
  599. flctl->qos_request = 0;
  600. }
  601. pm_runtime_put_sync(&flctl->pdev->dev);
  602. break;
  603. case 0:
  604. flctl->flcmncr_base |= CE0_ENABLE;
  605. if (!flctl->qos_request) {
  606. ret = dev_pm_qos_add_request(&flctl->pdev->dev,
  607. &flctl->pm_qos, 100);
  608. if (ret < 0)
  609. dev_err(&flctl->pdev->dev,
  610. "PM QoS request failed: %d\n", ret);
  611. flctl->qos_request = 1;
  612. }
  613. if (flctl->holden) {
  614. pm_runtime_get_sync(&flctl->pdev->dev);
  615. writel(HOLDEN, FLHOLDCR(flctl));
  616. pm_runtime_put_sync(&flctl->pdev->dev);
  617. }
  618. break;
  619. default:
  620. BUG();
  621. }
  622. }
  623. static void flctl_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  624. {
  625. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  626. int i, index = flctl->index;
  627. for (i = 0; i < len; i++)
  628. flctl->done_buff[index + i] = buf[i];
  629. flctl->index += len;
  630. }
  631. static uint8_t flctl_read_byte(struct mtd_info *mtd)
  632. {
  633. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  634. int index = flctl->index;
  635. uint8_t data;
  636. data = flctl->done_buff[index];
  637. flctl->index++;
  638. return data;
  639. }
  640. static uint16_t flctl_read_word(struct mtd_info *mtd)
  641. {
  642. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  643. int index = flctl->index;
  644. uint16_t data;
  645. uint16_t *buf = (uint16_t *)&flctl->done_buff[index];
  646. data = *buf;
  647. flctl->index += 2;
  648. return data;
  649. }
  650. static void flctl_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  651. {
  652. int i;
  653. for (i = 0; i < len; i++)
  654. buf[i] = flctl_read_byte(mtd);
  655. }
  656. static int flctl_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  657. {
  658. int i;
  659. for (i = 0; i < len; i++)
  660. if (buf[i] != flctl_read_byte(mtd))
  661. return -EFAULT;
  662. return 0;
  663. }
  664. static int flctl_chip_init_tail(struct mtd_info *mtd)
  665. {
  666. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  667. struct nand_chip *chip = &flctl->chip;
  668. if (mtd->writesize == 512) {
  669. flctl->page_size = 0;
  670. if (chip->chipsize > (32 << 20)) {
  671. /* big than 32MB */
  672. flctl->rw_ADRCNT = ADRCNT_4;
  673. flctl->erase_ADRCNT = ADRCNT_3;
  674. } else if (chip->chipsize > (2 << 16)) {
  675. /* big than 128KB */
  676. flctl->rw_ADRCNT = ADRCNT_3;
  677. flctl->erase_ADRCNT = ADRCNT_2;
  678. } else {
  679. flctl->rw_ADRCNT = ADRCNT_2;
  680. flctl->erase_ADRCNT = ADRCNT_1;
  681. }
  682. } else {
  683. flctl->page_size = 1;
  684. if (chip->chipsize > (128 << 20)) {
  685. /* big than 128MB */
  686. flctl->rw_ADRCNT = ADRCNT2_E;
  687. flctl->erase_ADRCNT = ADRCNT_3;
  688. } else if (chip->chipsize > (8 << 16)) {
  689. /* big than 512KB */
  690. flctl->rw_ADRCNT = ADRCNT_4;
  691. flctl->erase_ADRCNT = ADRCNT_2;
  692. } else {
  693. flctl->rw_ADRCNT = ADRCNT_3;
  694. flctl->erase_ADRCNT = ADRCNT_1;
  695. }
  696. }
  697. if (flctl->hwecc) {
  698. if (mtd->writesize == 512) {
  699. chip->ecc.layout = &flctl_4secc_oob_16;
  700. chip->badblock_pattern = &flctl_4secc_smallpage;
  701. } else {
  702. chip->ecc.layout = &flctl_4secc_oob_64;
  703. chip->badblock_pattern = &flctl_4secc_largepage;
  704. }
  705. chip->ecc.size = 512;
  706. chip->ecc.bytes = 10;
  707. chip->ecc.strength = 4;
  708. chip->ecc.read_page = flctl_read_page_hwecc;
  709. chip->ecc.write_page = flctl_write_page_hwecc;
  710. chip->ecc.mode = NAND_ECC_HW;
  711. /* 4 symbols ECC enabled */
  712. flctl->flcmncr_base |= _4ECCEN;
  713. } else {
  714. chip->ecc.mode = NAND_ECC_SOFT;
  715. }
  716. return 0;
  717. }
  718. static irqreturn_t flctl_handle_flste(int irq, void *dev_id)
  719. {
  720. struct sh_flctl *flctl = dev_id;
  721. dev_err(&flctl->pdev->dev, "flste irq: %x\n", readl(FLINTDMACR(flctl)));
  722. writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
  723. return IRQ_HANDLED;
  724. }
  725. static int __devinit flctl_probe(struct platform_device *pdev)
  726. {
  727. struct resource *res;
  728. struct sh_flctl *flctl;
  729. struct mtd_info *flctl_mtd;
  730. struct nand_chip *nand;
  731. struct sh_flctl_platform_data *pdata;
  732. int ret = -ENXIO;
  733. int irq;
  734. pdata = pdev->dev.platform_data;
  735. if (pdata == NULL) {
  736. dev_err(&pdev->dev, "no platform data defined\n");
  737. return -EINVAL;
  738. }
  739. flctl = kzalloc(sizeof(struct sh_flctl), GFP_KERNEL);
  740. if (!flctl) {
  741. dev_err(&pdev->dev, "failed to allocate driver data\n");
  742. return -ENOMEM;
  743. }
  744. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  745. if (!res) {
  746. dev_err(&pdev->dev, "failed to get I/O memory\n");
  747. goto err_iomap;
  748. }
  749. flctl->reg = ioremap(res->start, resource_size(res));
  750. if (flctl->reg == NULL) {
  751. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  752. goto err_iomap;
  753. }
  754. irq = platform_get_irq(pdev, 0);
  755. if (irq < 0) {
  756. dev_err(&pdev->dev, "failed to get flste irq data\n");
  757. goto err_flste;
  758. }
  759. ret = request_irq(irq, flctl_handle_flste, IRQF_SHARED, "flste", flctl);
  760. if (ret) {
  761. dev_err(&pdev->dev, "request interrupt failed.\n");
  762. goto err_flste;
  763. }
  764. platform_set_drvdata(pdev, flctl);
  765. flctl_mtd = &flctl->mtd;
  766. nand = &flctl->chip;
  767. flctl_mtd->priv = nand;
  768. flctl->pdev = pdev;
  769. flctl->hwecc = pdata->has_hwecc;
  770. flctl->holden = pdata->use_holden;
  771. flctl->flcmncr_base = pdata->flcmncr_val;
  772. flctl->flintdmacr_base = flctl->hwecc ? (STERINTE | ECERB) : STERINTE;
  773. /* Set address of hardware control function */
  774. /* 20 us command delay time */
  775. nand->chip_delay = 20;
  776. nand->read_byte = flctl_read_byte;
  777. nand->write_buf = flctl_write_buf;
  778. nand->read_buf = flctl_read_buf;
  779. nand->verify_buf = flctl_verify_buf;
  780. nand->select_chip = flctl_select_chip;
  781. nand->cmdfunc = flctl_cmdfunc;
  782. if (pdata->flcmncr_val & SEL_16BIT) {
  783. nand->options |= NAND_BUSWIDTH_16;
  784. nand->read_word = flctl_read_word;
  785. }
  786. pm_runtime_enable(&pdev->dev);
  787. pm_runtime_resume(&pdev->dev);
  788. ret = nand_scan_ident(flctl_mtd, 1, NULL);
  789. if (ret)
  790. goto err_chip;
  791. ret = flctl_chip_init_tail(flctl_mtd);
  792. if (ret)
  793. goto err_chip;
  794. ret = nand_scan_tail(flctl_mtd);
  795. if (ret)
  796. goto err_chip;
  797. mtd_device_register(flctl_mtd, pdata->parts, pdata->nr_parts);
  798. return 0;
  799. err_chip:
  800. pm_runtime_disable(&pdev->dev);
  801. free_irq(irq, flctl);
  802. err_flste:
  803. iounmap(flctl->reg);
  804. err_iomap:
  805. kfree(flctl);
  806. return ret;
  807. }
  808. static int __devexit flctl_remove(struct platform_device *pdev)
  809. {
  810. struct sh_flctl *flctl = platform_get_drvdata(pdev);
  811. nand_release(&flctl->mtd);
  812. pm_runtime_disable(&pdev->dev);
  813. free_irq(platform_get_irq(pdev, 0), flctl);
  814. iounmap(flctl->reg);
  815. kfree(flctl);
  816. return 0;
  817. }
  818. static struct platform_driver flctl_driver = {
  819. .remove = flctl_remove,
  820. .driver = {
  821. .name = "sh_flctl",
  822. .owner = THIS_MODULE,
  823. },
  824. };
  825. static int __init flctl_nand_init(void)
  826. {
  827. return platform_driver_probe(&flctl_driver, flctl_probe);
  828. }
  829. static void __exit flctl_nand_cleanup(void)
  830. {
  831. platform_driver_unregister(&flctl_driver);
  832. }
  833. module_init(flctl_nand_init);
  834. module_exit(flctl_nand_cleanup);
  835. MODULE_LICENSE("GPL");
  836. MODULE_AUTHOR("Yoshihiro Shimoda");
  837. MODULE_DESCRIPTION("SuperH FLCTL driver");
  838. MODULE_ALIAS("platform:sh_flctl");