sh_flctl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. chip->read_buf(mtd, buf, mtd->writesize);
  307. return 0;
  308. }
  309. static void flctl_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  310. const uint8_t *buf, int oob_required)
  311. {
  312. chip->write_buf(mtd, buf, mtd->writesize);
  313. }
  314. static void execmd_read_page_sector(struct mtd_info *mtd, int page_addr)
  315. {
  316. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  317. int sector, page_sectors;
  318. if (flctl->page_size)
  319. page_sectors = 4;
  320. else
  321. page_sectors = 1;
  322. writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE | _4ECCCORRECT,
  323. FLCMNCR(flctl));
  324. set_cmd_regs(mtd, NAND_CMD_READ0,
  325. (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
  326. for (sector = 0; sector < page_sectors; sector++) {
  327. int ret;
  328. empty_fifo(flctl);
  329. writel(readl(FLCMDCR(flctl)) | 1, FLCMDCR(flctl));
  330. writel(page_addr << 2 | sector, FLADR(flctl));
  331. start_translation(flctl);
  332. read_fiforeg(flctl, 512, 512 * sector);
  333. ret = read_ecfiforeg(flctl,
  334. &flctl->done_buff[mtd->writesize + 16 * sector],
  335. sector);
  336. if (ret)
  337. flctl->hwecc_cant_correct[sector] = 1;
  338. writel(0x0, FL4ECCCR(flctl));
  339. wait_completion(flctl);
  340. }
  341. writel(readl(FLCMNCR(flctl)) & ~(ACM_SACCES_MODE | _4ECCCORRECT),
  342. FLCMNCR(flctl));
  343. }
  344. static void execmd_read_oob(struct mtd_info *mtd, int page_addr)
  345. {
  346. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  347. int page_sectors = flctl->page_size ? 4 : 1;
  348. int i;
  349. set_cmd_regs(mtd, NAND_CMD_READ0,
  350. (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
  351. empty_fifo(flctl);
  352. for (i = 0; i < page_sectors; i++) {
  353. set_addr(mtd, (512 + 16) * i + 512 , page_addr);
  354. writel(16, FLDTCNTR(flctl));
  355. start_translation(flctl);
  356. read_fiforeg(flctl, 16, 16 * i);
  357. wait_completion(flctl);
  358. }
  359. }
  360. static void execmd_write_page_sector(struct mtd_info *mtd)
  361. {
  362. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  363. int i, page_addr = flctl->seqin_page_addr;
  364. int sector, page_sectors;
  365. if (flctl->page_size)
  366. page_sectors = 4;
  367. else
  368. page_sectors = 1;
  369. writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE, FLCMNCR(flctl));
  370. set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
  371. (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
  372. for (sector = 0; sector < page_sectors; sector++) {
  373. empty_fifo(flctl);
  374. writel(readl(FLCMDCR(flctl)) | 1, FLCMDCR(flctl));
  375. writel(page_addr << 2 | sector, FLADR(flctl));
  376. start_translation(flctl);
  377. write_fiforeg(flctl, 512, 512 * sector);
  378. for (i = 0; i < 4; i++) {
  379. wait_wecfifo_ready(flctl); /* wait for write ready */
  380. writel(0xFFFFFFFF, FLECFIFO(flctl));
  381. }
  382. wait_completion(flctl);
  383. }
  384. writel(readl(FLCMNCR(flctl)) & ~ACM_SACCES_MODE, FLCMNCR(flctl));
  385. }
  386. static void execmd_write_oob(struct mtd_info *mtd)
  387. {
  388. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  389. int page_addr = flctl->seqin_page_addr;
  390. int sector, page_sectors;
  391. page_sectors = flctl->page_size ? 4 : 1;
  392. set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
  393. (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
  394. for (sector = 0; sector < page_sectors; sector++) {
  395. empty_fifo(flctl);
  396. set_addr(mtd, sector * 528 + 512, page_addr);
  397. writel(16, FLDTCNTR(flctl)); /* set read size */
  398. start_translation(flctl);
  399. write_fiforeg(flctl, 16, 16 * sector);
  400. wait_completion(flctl);
  401. }
  402. }
  403. static void flctl_cmdfunc(struct mtd_info *mtd, unsigned int command,
  404. int column, int page_addr)
  405. {
  406. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  407. uint32_t read_cmd = 0;
  408. pm_runtime_get_sync(&flctl->pdev->dev);
  409. flctl->read_bytes = 0;
  410. if (command != NAND_CMD_PAGEPROG)
  411. flctl->index = 0;
  412. switch (command) {
  413. case NAND_CMD_READ1:
  414. case NAND_CMD_READ0:
  415. if (flctl->hwecc) {
  416. /* read page with hwecc */
  417. execmd_read_page_sector(mtd, page_addr);
  418. break;
  419. }
  420. if (flctl->page_size)
  421. set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
  422. | command);
  423. else
  424. set_cmd_regs(mtd, command, command);
  425. set_addr(mtd, 0, page_addr);
  426. flctl->read_bytes = mtd->writesize + mtd->oobsize;
  427. if (flctl->chip.options & NAND_BUSWIDTH_16)
  428. column >>= 1;
  429. flctl->index += column;
  430. goto read_normal_exit;
  431. case NAND_CMD_READOOB:
  432. if (flctl->hwecc) {
  433. /* read page with hwecc */
  434. execmd_read_oob(mtd, page_addr);
  435. break;
  436. }
  437. if (flctl->page_size) {
  438. set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
  439. | NAND_CMD_READ0);
  440. set_addr(mtd, mtd->writesize, page_addr);
  441. } else {
  442. set_cmd_regs(mtd, command, command);
  443. set_addr(mtd, 0, page_addr);
  444. }
  445. flctl->read_bytes = mtd->oobsize;
  446. goto read_normal_exit;
  447. case NAND_CMD_RNDOUT:
  448. if (flctl->hwecc)
  449. break;
  450. if (flctl->page_size)
  451. set_cmd_regs(mtd, command, (NAND_CMD_RNDOUTSTART << 8)
  452. | command);
  453. else
  454. set_cmd_regs(mtd, command, command);
  455. set_addr(mtd, column, 0);
  456. flctl->read_bytes = mtd->writesize + mtd->oobsize - column;
  457. goto read_normal_exit;
  458. case NAND_CMD_READID:
  459. set_cmd_regs(mtd, command, command);
  460. /* READID is always performed using an 8-bit bus */
  461. if (flctl->chip.options & NAND_BUSWIDTH_16)
  462. column <<= 1;
  463. set_addr(mtd, column, 0);
  464. flctl->read_bytes = 8;
  465. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  466. empty_fifo(flctl);
  467. start_translation(flctl);
  468. read_fiforeg(flctl, flctl->read_bytes, 0);
  469. wait_completion(flctl);
  470. break;
  471. case NAND_CMD_ERASE1:
  472. flctl->erase1_page_addr = page_addr;
  473. break;
  474. case NAND_CMD_ERASE2:
  475. set_cmd_regs(mtd, NAND_CMD_ERASE1,
  476. (command << 8) | NAND_CMD_ERASE1);
  477. set_addr(mtd, -1, flctl->erase1_page_addr);
  478. start_translation(flctl);
  479. wait_completion(flctl);
  480. break;
  481. case NAND_CMD_SEQIN:
  482. if (!flctl->page_size) {
  483. /* output read command */
  484. if (column >= mtd->writesize) {
  485. column -= mtd->writesize;
  486. read_cmd = NAND_CMD_READOOB;
  487. } else if (column < 256) {
  488. read_cmd = NAND_CMD_READ0;
  489. } else {
  490. column -= 256;
  491. read_cmd = NAND_CMD_READ1;
  492. }
  493. }
  494. flctl->seqin_column = column;
  495. flctl->seqin_page_addr = page_addr;
  496. flctl->seqin_read_cmd = read_cmd;
  497. break;
  498. case NAND_CMD_PAGEPROG:
  499. empty_fifo(flctl);
  500. if (!flctl->page_size) {
  501. set_cmd_regs(mtd, NAND_CMD_SEQIN,
  502. flctl->seqin_read_cmd);
  503. set_addr(mtd, -1, -1);
  504. writel(0, FLDTCNTR(flctl)); /* set 0 size */
  505. start_translation(flctl);
  506. wait_completion(flctl);
  507. }
  508. if (flctl->hwecc) {
  509. /* write page with hwecc */
  510. if (flctl->seqin_column == mtd->writesize)
  511. execmd_write_oob(mtd);
  512. else if (!flctl->seqin_column)
  513. execmd_write_page_sector(mtd);
  514. else
  515. printk(KERN_ERR "Invalid address !?\n");
  516. break;
  517. }
  518. set_cmd_regs(mtd, command, (command << 8) | NAND_CMD_SEQIN);
  519. set_addr(mtd, flctl->seqin_column, flctl->seqin_page_addr);
  520. writel(flctl->index, FLDTCNTR(flctl)); /* set write size */
  521. start_translation(flctl);
  522. write_fiforeg(flctl, flctl->index, 0);
  523. wait_completion(flctl);
  524. break;
  525. case NAND_CMD_STATUS:
  526. set_cmd_regs(mtd, command, command);
  527. set_addr(mtd, -1, -1);
  528. flctl->read_bytes = 1;
  529. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  530. start_translation(flctl);
  531. read_datareg(flctl, 0); /* read and end */
  532. break;
  533. case NAND_CMD_RESET:
  534. set_cmd_regs(mtd, command, command);
  535. set_addr(mtd, -1, -1);
  536. writel(0, FLDTCNTR(flctl)); /* set 0 size */
  537. start_translation(flctl);
  538. wait_completion(flctl);
  539. break;
  540. default:
  541. break;
  542. }
  543. goto runtime_exit;
  544. read_normal_exit:
  545. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  546. empty_fifo(flctl);
  547. start_translation(flctl);
  548. read_fiforeg(flctl, flctl->read_bytes, 0);
  549. wait_completion(flctl);
  550. runtime_exit:
  551. pm_runtime_put_sync(&flctl->pdev->dev);
  552. return;
  553. }
  554. static void flctl_select_chip(struct mtd_info *mtd, int chipnr)
  555. {
  556. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  557. int ret;
  558. switch (chipnr) {
  559. case -1:
  560. flctl->flcmncr_base &= ~CE0_ENABLE;
  561. pm_runtime_get_sync(&flctl->pdev->dev);
  562. writel(flctl->flcmncr_base, FLCMNCR(flctl));
  563. if (flctl->qos_request) {
  564. dev_pm_qos_remove_request(&flctl->pm_qos);
  565. flctl->qos_request = 0;
  566. }
  567. pm_runtime_put_sync(&flctl->pdev->dev);
  568. break;
  569. case 0:
  570. flctl->flcmncr_base |= CE0_ENABLE;
  571. if (!flctl->qos_request) {
  572. ret = dev_pm_qos_add_request(&flctl->pdev->dev,
  573. &flctl->pm_qos, 100);
  574. if (ret < 0)
  575. dev_err(&flctl->pdev->dev,
  576. "PM QoS request failed: %d\n", ret);
  577. flctl->qos_request = 1;
  578. }
  579. if (flctl->holden) {
  580. pm_runtime_get_sync(&flctl->pdev->dev);
  581. writel(HOLDEN, FLHOLDCR(flctl));
  582. pm_runtime_put_sync(&flctl->pdev->dev);
  583. }
  584. break;
  585. default:
  586. BUG();
  587. }
  588. }
  589. static void flctl_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  590. {
  591. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  592. int i, index = flctl->index;
  593. for (i = 0; i < len; i++)
  594. flctl->done_buff[index + i] = buf[i];
  595. flctl->index += len;
  596. }
  597. static uint8_t flctl_read_byte(struct mtd_info *mtd)
  598. {
  599. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  600. int index = flctl->index;
  601. uint8_t data;
  602. data = flctl->done_buff[index];
  603. flctl->index++;
  604. return data;
  605. }
  606. static uint16_t flctl_read_word(struct mtd_info *mtd)
  607. {
  608. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  609. int index = flctl->index;
  610. uint16_t data;
  611. uint16_t *buf = (uint16_t *)&flctl->done_buff[index];
  612. data = *buf;
  613. flctl->index += 2;
  614. return data;
  615. }
  616. static void flctl_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  617. {
  618. int i;
  619. for (i = 0; i < len; i++)
  620. buf[i] = flctl_read_byte(mtd);
  621. }
  622. static int flctl_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  623. {
  624. int i;
  625. for (i = 0; i < len; i++)
  626. if (buf[i] != flctl_read_byte(mtd))
  627. return -EFAULT;
  628. return 0;
  629. }
  630. static int flctl_chip_init_tail(struct mtd_info *mtd)
  631. {
  632. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  633. struct nand_chip *chip = &flctl->chip;
  634. if (mtd->writesize == 512) {
  635. flctl->page_size = 0;
  636. if (chip->chipsize > (32 << 20)) {
  637. /* big than 32MB */
  638. flctl->rw_ADRCNT = ADRCNT_4;
  639. flctl->erase_ADRCNT = ADRCNT_3;
  640. } else if (chip->chipsize > (2 << 16)) {
  641. /* big than 128KB */
  642. flctl->rw_ADRCNT = ADRCNT_3;
  643. flctl->erase_ADRCNT = ADRCNT_2;
  644. } else {
  645. flctl->rw_ADRCNT = ADRCNT_2;
  646. flctl->erase_ADRCNT = ADRCNT_1;
  647. }
  648. } else {
  649. flctl->page_size = 1;
  650. if (chip->chipsize > (128 << 20)) {
  651. /* big than 128MB */
  652. flctl->rw_ADRCNT = ADRCNT2_E;
  653. flctl->erase_ADRCNT = ADRCNT_3;
  654. } else if (chip->chipsize > (8 << 16)) {
  655. /* big than 512KB */
  656. flctl->rw_ADRCNT = ADRCNT_4;
  657. flctl->erase_ADRCNT = ADRCNT_2;
  658. } else {
  659. flctl->rw_ADRCNT = ADRCNT_3;
  660. flctl->erase_ADRCNT = ADRCNT_1;
  661. }
  662. }
  663. if (flctl->hwecc) {
  664. if (mtd->writesize == 512) {
  665. chip->ecc.layout = &flctl_4secc_oob_16;
  666. chip->badblock_pattern = &flctl_4secc_smallpage;
  667. } else {
  668. chip->ecc.layout = &flctl_4secc_oob_64;
  669. chip->badblock_pattern = &flctl_4secc_largepage;
  670. }
  671. chip->ecc.size = 512;
  672. chip->ecc.bytes = 10;
  673. chip->ecc.strength = 4;
  674. chip->ecc.read_page = flctl_read_page_hwecc;
  675. chip->ecc.write_page = flctl_write_page_hwecc;
  676. chip->ecc.mode = NAND_ECC_HW;
  677. /* 4 symbols ECC enabled */
  678. flctl->flcmncr_base |= _4ECCEN;
  679. } else {
  680. chip->ecc.mode = NAND_ECC_SOFT;
  681. }
  682. return 0;
  683. }
  684. static irqreturn_t flctl_handle_flste(int irq, void *dev_id)
  685. {
  686. struct sh_flctl *flctl = dev_id;
  687. dev_err(&flctl->pdev->dev, "flste irq: %x\n", readl(FLINTDMACR(flctl)));
  688. writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
  689. return IRQ_HANDLED;
  690. }
  691. static int __devinit flctl_probe(struct platform_device *pdev)
  692. {
  693. struct resource *res;
  694. struct sh_flctl *flctl;
  695. struct mtd_info *flctl_mtd;
  696. struct nand_chip *nand;
  697. struct sh_flctl_platform_data *pdata;
  698. int ret = -ENXIO;
  699. int irq;
  700. pdata = pdev->dev.platform_data;
  701. if (pdata == NULL) {
  702. dev_err(&pdev->dev, "no platform data defined\n");
  703. return -EINVAL;
  704. }
  705. flctl = kzalloc(sizeof(struct sh_flctl), GFP_KERNEL);
  706. if (!flctl) {
  707. dev_err(&pdev->dev, "failed to allocate driver data\n");
  708. return -ENOMEM;
  709. }
  710. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  711. if (!res) {
  712. dev_err(&pdev->dev, "failed to get I/O memory\n");
  713. goto err_iomap;
  714. }
  715. flctl->reg = ioremap(res->start, resource_size(res));
  716. if (flctl->reg == NULL) {
  717. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  718. goto err_iomap;
  719. }
  720. irq = platform_get_irq(pdev, 0);
  721. if (irq < 0) {
  722. dev_err(&pdev->dev, "failed to get flste irq data\n");
  723. goto err_flste;
  724. }
  725. ret = request_irq(irq, flctl_handle_flste, IRQF_SHARED, "flste", flctl);
  726. if (ret) {
  727. dev_err(&pdev->dev, "request interrupt failed.\n");
  728. goto err_flste;
  729. }
  730. platform_set_drvdata(pdev, flctl);
  731. flctl_mtd = &flctl->mtd;
  732. nand = &flctl->chip;
  733. flctl_mtd->priv = nand;
  734. flctl->pdev = pdev;
  735. flctl->hwecc = pdata->has_hwecc;
  736. flctl->holden = pdata->use_holden;
  737. flctl->flcmncr_base = pdata->flcmncr_val;
  738. flctl->flintdmacr_base = flctl->hwecc ? (STERINTE | ECERB) : STERINTE;
  739. /* Set address of hardware control function */
  740. /* 20 us command delay time */
  741. nand->chip_delay = 20;
  742. nand->read_byte = flctl_read_byte;
  743. nand->write_buf = flctl_write_buf;
  744. nand->read_buf = flctl_read_buf;
  745. nand->verify_buf = flctl_verify_buf;
  746. nand->select_chip = flctl_select_chip;
  747. nand->cmdfunc = flctl_cmdfunc;
  748. if (pdata->flcmncr_val & SEL_16BIT) {
  749. nand->options |= NAND_BUSWIDTH_16;
  750. nand->read_word = flctl_read_word;
  751. }
  752. pm_runtime_enable(&pdev->dev);
  753. pm_runtime_resume(&pdev->dev);
  754. ret = nand_scan_ident(flctl_mtd, 1, NULL);
  755. if (ret)
  756. goto err_chip;
  757. ret = flctl_chip_init_tail(flctl_mtd);
  758. if (ret)
  759. goto err_chip;
  760. ret = nand_scan_tail(flctl_mtd);
  761. if (ret)
  762. goto err_chip;
  763. mtd_device_register(flctl_mtd, pdata->parts, pdata->nr_parts);
  764. return 0;
  765. err_chip:
  766. pm_runtime_disable(&pdev->dev);
  767. free_irq(irq, flctl);
  768. err_flste:
  769. iounmap(flctl->reg);
  770. err_iomap:
  771. kfree(flctl);
  772. return ret;
  773. }
  774. static int __devexit flctl_remove(struct platform_device *pdev)
  775. {
  776. struct sh_flctl *flctl = platform_get_drvdata(pdev);
  777. nand_release(&flctl->mtd);
  778. pm_runtime_disable(&pdev->dev);
  779. free_irq(platform_get_irq(pdev, 0), flctl);
  780. iounmap(flctl->reg);
  781. kfree(flctl);
  782. return 0;
  783. }
  784. static struct platform_driver flctl_driver = {
  785. .remove = flctl_remove,
  786. .driver = {
  787. .name = "sh_flctl",
  788. .owner = THIS_MODULE,
  789. },
  790. };
  791. static int __init flctl_nand_init(void)
  792. {
  793. return platform_driver_probe(&flctl_driver, flctl_probe);
  794. }
  795. static void __exit flctl_nand_cleanup(void)
  796. {
  797. platform_driver_unregister(&flctl_driver);
  798. }
  799. module_init(flctl_nand_init);
  800. module_exit(flctl_nand_cleanup);
  801. MODULE_LICENSE("GPL");
  802. MODULE_AUTHOR("Yoshihiro Shimoda");
  803. MODULE_DESCRIPTION("SuperH FLCTL driver");
  804. MODULE_ALIAS("platform:sh_flctl");