sh_flctl.c 23 KB

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