sh_flctl.c 20 KB

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