sh_flctl.c 21 KB

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