sh_flctl.c 20 KB

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