sh_flctl.c 23 KB

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