sh_flctl.c 21 KB

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