sh_flctl.c 22 KB

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