sh_flctl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. if (oob_required)
  346. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  347. return 0;
  348. }
  349. static int flctl_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  350. const uint8_t *buf, int oob_required)
  351. {
  352. chip->write_buf(mtd, buf, mtd->writesize);
  353. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  354. return 0;
  355. }
  356. static void execmd_read_page_sector(struct mtd_info *mtd, int page_addr)
  357. {
  358. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  359. int sector, page_sectors;
  360. enum flctl_ecc_res_t ecc_result;
  361. page_sectors = flctl->page_size ? 4 : 1;
  362. set_cmd_regs(mtd, NAND_CMD_READ0,
  363. (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
  364. writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE | _4ECCCORRECT,
  365. FLCMNCR(flctl));
  366. writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
  367. writel(page_addr << 2, FLADR(flctl));
  368. empty_fifo(flctl);
  369. start_translation(flctl);
  370. for (sector = 0; sector < page_sectors; sector++) {
  371. read_fiforeg(flctl, 512, 512 * sector);
  372. ecc_result = read_ecfiforeg(flctl,
  373. &flctl->done_buff[mtd->writesize + 16 * sector],
  374. sector);
  375. switch (ecc_result) {
  376. case FL_REPAIRABLE:
  377. dev_info(&flctl->pdev->dev,
  378. "applied ecc on page 0x%x", page_addr);
  379. flctl->mtd.ecc_stats.corrected++;
  380. break;
  381. case FL_ERROR:
  382. dev_warn(&flctl->pdev->dev,
  383. "page 0x%x contains corrupted data\n",
  384. page_addr);
  385. flctl->mtd.ecc_stats.failed++;
  386. break;
  387. default:
  388. ;
  389. }
  390. }
  391. wait_completion(flctl);
  392. writel(readl(FLCMNCR(flctl)) & ~(ACM_SACCES_MODE | _4ECCCORRECT),
  393. FLCMNCR(flctl));
  394. }
  395. static void execmd_read_oob(struct mtd_info *mtd, int page_addr)
  396. {
  397. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  398. int page_sectors = flctl->page_size ? 4 : 1;
  399. int i;
  400. set_cmd_regs(mtd, NAND_CMD_READ0,
  401. (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
  402. empty_fifo(flctl);
  403. for (i = 0; i < page_sectors; i++) {
  404. set_addr(mtd, (512 + 16) * i + 512 , page_addr);
  405. writel(16, FLDTCNTR(flctl));
  406. start_translation(flctl);
  407. read_fiforeg(flctl, 16, 16 * i);
  408. wait_completion(flctl);
  409. }
  410. }
  411. static void execmd_write_page_sector(struct mtd_info *mtd)
  412. {
  413. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  414. int page_addr = flctl->seqin_page_addr;
  415. int sector, page_sectors;
  416. page_sectors = flctl->page_size ? 4 : 1;
  417. set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
  418. (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
  419. empty_fifo(flctl);
  420. writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE, FLCMNCR(flctl));
  421. writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
  422. writel(page_addr << 2, FLADR(flctl));
  423. start_translation(flctl);
  424. for (sector = 0; sector < page_sectors; sector++) {
  425. write_fiforeg(flctl, 512, 512 * sector);
  426. write_ec_fiforeg(flctl, 16, mtd->writesize + 16 * sector);
  427. }
  428. wait_completion(flctl);
  429. writel(readl(FLCMNCR(flctl)) & ~ACM_SACCES_MODE, FLCMNCR(flctl));
  430. }
  431. static void execmd_write_oob(struct mtd_info *mtd)
  432. {
  433. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  434. int page_addr = flctl->seqin_page_addr;
  435. int sector, page_sectors;
  436. page_sectors = flctl->page_size ? 4 : 1;
  437. set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
  438. (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
  439. for (sector = 0; sector < page_sectors; sector++) {
  440. empty_fifo(flctl);
  441. set_addr(mtd, sector * 528 + 512, page_addr);
  442. writel(16, FLDTCNTR(flctl)); /* set read size */
  443. start_translation(flctl);
  444. write_fiforeg(flctl, 16, 16 * sector);
  445. wait_completion(flctl);
  446. }
  447. }
  448. static void flctl_cmdfunc(struct mtd_info *mtd, unsigned int command,
  449. int column, int page_addr)
  450. {
  451. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  452. uint32_t read_cmd = 0;
  453. pm_runtime_get_sync(&flctl->pdev->dev);
  454. flctl->read_bytes = 0;
  455. if (command != NAND_CMD_PAGEPROG)
  456. flctl->index = 0;
  457. switch (command) {
  458. case NAND_CMD_READ1:
  459. case NAND_CMD_READ0:
  460. if (flctl->hwecc) {
  461. /* read page with hwecc */
  462. execmd_read_page_sector(mtd, page_addr);
  463. break;
  464. }
  465. if (flctl->page_size)
  466. set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
  467. | command);
  468. else
  469. set_cmd_regs(mtd, command, command);
  470. set_addr(mtd, 0, page_addr);
  471. flctl->read_bytes = mtd->writesize + mtd->oobsize;
  472. if (flctl->chip.options & NAND_BUSWIDTH_16)
  473. column >>= 1;
  474. flctl->index += column;
  475. goto read_normal_exit;
  476. case NAND_CMD_READOOB:
  477. if (flctl->hwecc) {
  478. /* read page with hwecc */
  479. execmd_read_oob(mtd, page_addr);
  480. break;
  481. }
  482. if (flctl->page_size) {
  483. set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
  484. | NAND_CMD_READ0);
  485. set_addr(mtd, mtd->writesize, page_addr);
  486. } else {
  487. set_cmd_regs(mtd, command, command);
  488. set_addr(mtd, 0, page_addr);
  489. }
  490. flctl->read_bytes = mtd->oobsize;
  491. goto read_normal_exit;
  492. case NAND_CMD_RNDOUT:
  493. if (flctl->hwecc)
  494. break;
  495. if (flctl->page_size)
  496. set_cmd_regs(mtd, command, (NAND_CMD_RNDOUTSTART << 8)
  497. | command);
  498. else
  499. set_cmd_regs(mtd, command, command);
  500. set_addr(mtd, column, 0);
  501. flctl->read_bytes = mtd->writesize + mtd->oobsize - column;
  502. goto read_normal_exit;
  503. case NAND_CMD_READID:
  504. set_cmd_regs(mtd, command, command);
  505. /* READID is always performed using an 8-bit bus */
  506. if (flctl->chip.options & NAND_BUSWIDTH_16)
  507. column <<= 1;
  508. set_addr(mtd, column, 0);
  509. flctl->read_bytes = 8;
  510. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  511. empty_fifo(flctl);
  512. start_translation(flctl);
  513. read_fiforeg(flctl, flctl->read_bytes, 0);
  514. wait_completion(flctl);
  515. break;
  516. case NAND_CMD_ERASE1:
  517. flctl->erase1_page_addr = page_addr;
  518. break;
  519. case NAND_CMD_ERASE2:
  520. set_cmd_regs(mtd, NAND_CMD_ERASE1,
  521. (command << 8) | NAND_CMD_ERASE1);
  522. set_addr(mtd, -1, flctl->erase1_page_addr);
  523. start_translation(flctl);
  524. wait_completion(flctl);
  525. break;
  526. case NAND_CMD_SEQIN:
  527. if (!flctl->page_size) {
  528. /* output read command */
  529. if (column >= mtd->writesize) {
  530. column -= mtd->writesize;
  531. read_cmd = NAND_CMD_READOOB;
  532. } else if (column < 256) {
  533. read_cmd = NAND_CMD_READ0;
  534. } else {
  535. column -= 256;
  536. read_cmd = NAND_CMD_READ1;
  537. }
  538. }
  539. flctl->seqin_column = column;
  540. flctl->seqin_page_addr = page_addr;
  541. flctl->seqin_read_cmd = read_cmd;
  542. break;
  543. case NAND_CMD_PAGEPROG:
  544. empty_fifo(flctl);
  545. if (!flctl->page_size) {
  546. set_cmd_regs(mtd, NAND_CMD_SEQIN,
  547. flctl->seqin_read_cmd);
  548. set_addr(mtd, -1, -1);
  549. writel(0, FLDTCNTR(flctl)); /* set 0 size */
  550. start_translation(flctl);
  551. wait_completion(flctl);
  552. }
  553. if (flctl->hwecc) {
  554. /* write page with hwecc */
  555. if (flctl->seqin_column == mtd->writesize)
  556. execmd_write_oob(mtd);
  557. else if (!flctl->seqin_column)
  558. execmd_write_page_sector(mtd);
  559. else
  560. printk(KERN_ERR "Invalid address !?\n");
  561. break;
  562. }
  563. set_cmd_regs(mtd, command, (command << 8) | NAND_CMD_SEQIN);
  564. set_addr(mtd, flctl->seqin_column, flctl->seqin_page_addr);
  565. writel(flctl->index, FLDTCNTR(flctl)); /* set write size */
  566. start_translation(flctl);
  567. write_fiforeg(flctl, flctl->index, 0);
  568. wait_completion(flctl);
  569. break;
  570. case NAND_CMD_STATUS:
  571. set_cmd_regs(mtd, command, command);
  572. set_addr(mtd, -1, -1);
  573. flctl->read_bytes = 1;
  574. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  575. start_translation(flctl);
  576. read_datareg(flctl, 0); /* read and end */
  577. break;
  578. case NAND_CMD_RESET:
  579. set_cmd_regs(mtd, command, command);
  580. set_addr(mtd, -1, -1);
  581. writel(0, FLDTCNTR(flctl)); /* set 0 size */
  582. start_translation(flctl);
  583. wait_completion(flctl);
  584. break;
  585. default:
  586. break;
  587. }
  588. goto runtime_exit;
  589. read_normal_exit:
  590. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  591. empty_fifo(flctl);
  592. start_translation(flctl);
  593. read_fiforeg(flctl, flctl->read_bytes, 0);
  594. wait_completion(flctl);
  595. runtime_exit:
  596. pm_runtime_put_sync(&flctl->pdev->dev);
  597. return;
  598. }
  599. static void flctl_select_chip(struct mtd_info *mtd, int chipnr)
  600. {
  601. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  602. int ret;
  603. switch (chipnr) {
  604. case -1:
  605. flctl->flcmncr_base &= ~CE0_ENABLE;
  606. pm_runtime_get_sync(&flctl->pdev->dev);
  607. writel(flctl->flcmncr_base, FLCMNCR(flctl));
  608. if (flctl->qos_request) {
  609. dev_pm_qos_remove_request(&flctl->pm_qos);
  610. flctl->qos_request = 0;
  611. }
  612. pm_runtime_put_sync(&flctl->pdev->dev);
  613. break;
  614. case 0:
  615. flctl->flcmncr_base |= CE0_ENABLE;
  616. if (!flctl->qos_request) {
  617. ret = dev_pm_qos_add_request(&flctl->pdev->dev,
  618. &flctl->pm_qos, 100);
  619. if (ret < 0)
  620. dev_err(&flctl->pdev->dev,
  621. "PM QoS request failed: %d\n", ret);
  622. flctl->qos_request = 1;
  623. }
  624. if (flctl->holden) {
  625. pm_runtime_get_sync(&flctl->pdev->dev);
  626. writel(HOLDEN, FLHOLDCR(flctl));
  627. pm_runtime_put_sync(&flctl->pdev->dev);
  628. }
  629. break;
  630. default:
  631. BUG();
  632. }
  633. }
  634. static void flctl_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  635. {
  636. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  637. int index = flctl->index;
  638. memcpy(&flctl->done_buff[index], buf, len);
  639. flctl->index += len;
  640. }
  641. static uint8_t flctl_read_byte(struct mtd_info *mtd)
  642. {
  643. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  644. int index = flctl->index;
  645. uint8_t data;
  646. data = flctl->done_buff[index];
  647. flctl->index++;
  648. return data;
  649. }
  650. static uint16_t flctl_read_word(struct mtd_info *mtd)
  651. {
  652. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  653. int index = flctl->index;
  654. uint16_t data;
  655. uint16_t *buf = (uint16_t *)&flctl->done_buff[index];
  656. data = *buf;
  657. flctl->index += 2;
  658. return data;
  659. }
  660. static void flctl_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  661. {
  662. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  663. int index = flctl->index;
  664. memcpy(buf, &flctl->done_buff[index], len);
  665. flctl->index += len;
  666. }
  667. static int flctl_chip_init_tail(struct mtd_info *mtd)
  668. {
  669. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  670. struct nand_chip *chip = &flctl->chip;
  671. if (mtd->writesize == 512) {
  672. flctl->page_size = 0;
  673. if (chip->chipsize > (32 << 20)) {
  674. /* big than 32MB */
  675. flctl->rw_ADRCNT = ADRCNT_4;
  676. flctl->erase_ADRCNT = ADRCNT_3;
  677. } else if (chip->chipsize > (2 << 16)) {
  678. /* big than 128KB */
  679. flctl->rw_ADRCNT = ADRCNT_3;
  680. flctl->erase_ADRCNT = ADRCNT_2;
  681. } else {
  682. flctl->rw_ADRCNT = ADRCNT_2;
  683. flctl->erase_ADRCNT = ADRCNT_1;
  684. }
  685. } else {
  686. flctl->page_size = 1;
  687. if (chip->chipsize > (128 << 20)) {
  688. /* big than 128MB */
  689. flctl->rw_ADRCNT = ADRCNT2_E;
  690. flctl->erase_ADRCNT = ADRCNT_3;
  691. } else if (chip->chipsize > (8 << 16)) {
  692. /* big than 512KB */
  693. flctl->rw_ADRCNT = ADRCNT_4;
  694. flctl->erase_ADRCNT = ADRCNT_2;
  695. } else {
  696. flctl->rw_ADRCNT = ADRCNT_3;
  697. flctl->erase_ADRCNT = ADRCNT_1;
  698. }
  699. }
  700. if (flctl->hwecc) {
  701. if (mtd->writesize == 512) {
  702. chip->ecc.layout = &flctl_4secc_oob_16;
  703. chip->badblock_pattern = &flctl_4secc_smallpage;
  704. } else {
  705. chip->ecc.layout = &flctl_4secc_oob_64;
  706. chip->badblock_pattern = &flctl_4secc_largepage;
  707. }
  708. chip->ecc.size = 512;
  709. chip->ecc.bytes = 10;
  710. chip->ecc.strength = 4;
  711. chip->ecc.read_page = flctl_read_page_hwecc;
  712. chip->ecc.write_page = flctl_write_page_hwecc;
  713. chip->ecc.mode = NAND_ECC_HW;
  714. /* 4 symbols ECC enabled */
  715. flctl->flcmncr_base |= _4ECCEN;
  716. } else {
  717. chip->ecc.mode = NAND_ECC_SOFT;
  718. }
  719. return 0;
  720. }
  721. static irqreturn_t flctl_handle_flste(int irq, void *dev_id)
  722. {
  723. struct sh_flctl *flctl = dev_id;
  724. dev_err(&flctl->pdev->dev, "flste irq: %x\n", readl(FLINTDMACR(flctl)));
  725. writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
  726. return IRQ_HANDLED;
  727. }
  728. static int __devinit flctl_probe(struct platform_device *pdev)
  729. {
  730. struct resource *res;
  731. struct sh_flctl *flctl;
  732. struct mtd_info *flctl_mtd;
  733. struct nand_chip *nand;
  734. struct sh_flctl_platform_data *pdata;
  735. int ret = -ENXIO;
  736. int irq;
  737. pdata = pdev->dev.platform_data;
  738. if (pdata == NULL) {
  739. dev_err(&pdev->dev, "no platform data defined\n");
  740. return -EINVAL;
  741. }
  742. flctl = kzalloc(sizeof(struct sh_flctl), GFP_KERNEL);
  743. if (!flctl) {
  744. dev_err(&pdev->dev, "failed to allocate driver data\n");
  745. return -ENOMEM;
  746. }
  747. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  748. if (!res) {
  749. dev_err(&pdev->dev, "failed to get I/O memory\n");
  750. goto err_iomap;
  751. }
  752. flctl->reg = ioremap(res->start, resource_size(res));
  753. if (flctl->reg == NULL) {
  754. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  755. goto err_iomap;
  756. }
  757. irq = platform_get_irq(pdev, 0);
  758. if (irq < 0) {
  759. dev_err(&pdev->dev, "failed to get flste irq data\n");
  760. goto err_flste;
  761. }
  762. ret = request_irq(irq, flctl_handle_flste, IRQF_SHARED, "flste", flctl);
  763. if (ret) {
  764. dev_err(&pdev->dev, "request interrupt failed.\n");
  765. goto err_flste;
  766. }
  767. platform_set_drvdata(pdev, flctl);
  768. flctl_mtd = &flctl->mtd;
  769. nand = &flctl->chip;
  770. flctl_mtd->priv = nand;
  771. flctl->pdev = pdev;
  772. flctl->hwecc = pdata->has_hwecc;
  773. flctl->holden = pdata->use_holden;
  774. flctl->flcmncr_base = pdata->flcmncr_val;
  775. flctl->flintdmacr_base = flctl->hwecc ? (STERINTE | ECERB) : STERINTE;
  776. /* Set address of hardware control function */
  777. /* 20 us command delay time */
  778. nand->chip_delay = 20;
  779. nand->read_byte = flctl_read_byte;
  780. nand->write_buf = flctl_write_buf;
  781. nand->read_buf = flctl_read_buf;
  782. nand->select_chip = flctl_select_chip;
  783. nand->cmdfunc = flctl_cmdfunc;
  784. if (pdata->flcmncr_val & SEL_16BIT) {
  785. nand->options |= NAND_BUSWIDTH_16;
  786. nand->read_word = flctl_read_word;
  787. }
  788. pm_runtime_enable(&pdev->dev);
  789. pm_runtime_resume(&pdev->dev);
  790. ret = nand_scan_ident(flctl_mtd, 1, NULL);
  791. if (ret)
  792. goto err_chip;
  793. ret = flctl_chip_init_tail(flctl_mtd);
  794. if (ret)
  795. goto err_chip;
  796. ret = nand_scan_tail(flctl_mtd);
  797. if (ret)
  798. goto err_chip;
  799. mtd_device_register(flctl_mtd, pdata->parts, pdata->nr_parts);
  800. return 0;
  801. err_chip:
  802. pm_runtime_disable(&pdev->dev);
  803. free_irq(irq, flctl);
  804. err_flste:
  805. iounmap(flctl->reg);
  806. err_iomap:
  807. kfree(flctl);
  808. return ret;
  809. }
  810. static int __devexit flctl_remove(struct platform_device *pdev)
  811. {
  812. struct sh_flctl *flctl = platform_get_drvdata(pdev);
  813. nand_release(&flctl->mtd);
  814. pm_runtime_disable(&pdev->dev);
  815. free_irq(platform_get_irq(pdev, 0), flctl);
  816. iounmap(flctl->reg);
  817. kfree(flctl);
  818. return 0;
  819. }
  820. static struct platform_driver flctl_driver = {
  821. .remove = flctl_remove,
  822. .driver = {
  823. .name = "sh_flctl",
  824. .owner = THIS_MODULE,
  825. },
  826. };
  827. static int __init flctl_nand_init(void)
  828. {
  829. return platform_driver_probe(&flctl_driver, flctl_probe);
  830. }
  831. static void __exit flctl_nand_cleanup(void)
  832. {
  833. platform_driver_unregister(&flctl_driver);
  834. }
  835. module_init(flctl_nand_init);
  836. module_exit(flctl_nand_cleanup);
  837. MODULE_LICENSE("GPL");
  838. MODULE_AUTHOR("Yoshihiro Shimoda");
  839. MODULE_DESCRIPTION("SuperH FLCTL driver");
  840. MODULE_ALIAS("platform:sh_flctl");