sh_flctl.c 24 KB

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