sh_flctl.c 21 KB

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