sh_flctl.c 24 KB

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