sh_flctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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/completion.h>
  26. #include <linux/delay.h>
  27. #include <linux/dmaengine.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/io.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/of_mtd.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/sh_dma.h>
  37. #include <linux/slab.h>
  38. #include <linux/string.h>
  39. #include <linux/mtd/mtd.h>
  40. #include <linux/mtd/nand.h>
  41. #include <linux/mtd/partitions.h>
  42. #include <linux/mtd/sh_flctl.h>
  43. static struct nand_ecclayout flctl_4secc_oob_16 = {
  44. .eccbytes = 10,
  45. .eccpos = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  46. .oobfree = {
  47. {.offset = 12,
  48. . length = 4} },
  49. };
  50. static struct nand_ecclayout flctl_4secc_oob_64 = {
  51. .eccbytes = 4 * 10,
  52. .eccpos = {
  53. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  54. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  55. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  56. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 },
  57. .oobfree = {
  58. {.offset = 2, .length = 4},
  59. {.offset = 16, .length = 6},
  60. {.offset = 32, .length = 6},
  61. {.offset = 48, .length = 6} },
  62. };
  63. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  64. static struct nand_bbt_descr flctl_4secc_smallpage = {
  65. .options = NAND_BBT_SCAN2NDPAGE,
  66. .offs = 11,
  67. .len = 1,
  68. .pattern = scan_ff_pattern,
  69. };
  70. static struct nand_bbt_descr flctl_4secc_largepage = {
  71. .options = NAND_BBT_SCAN2NDPAGE,
  72. .offs = 0,
  73. .len = 2,
  74. .pattern = scan_ff_pattern,
  75. };
  76. static void empty_fifo(struct sh_flctl *flctl)
  77. {
  78. writel(flctl->flintdmacr_base | AC1CLR | AC0CLR, FLINTDMACR(flctl));
  79. writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
  80. }
  81. static void start_translation(struct sh_flctl *flctl)
  82. {
  83. writeb(TRSTRT, FLTRCR(flctl));
  84. }
  85. static void timeout_error(struct sh_flctl *flctl, const char *str)
  86. {
  87. dev_err(&flctl->pdev->dev, "Timeout occurred in %s\n", str);
  88. }
  89. static void wait_completion(struct sh_flctl *flctl)
  90. {
  91. uint32_t timeout = LOOP_TIMEOUT_MAX;
  92. while (timeout--) {
  93. if (readb(FLTRCR(flctl)) & TREND) {
  94. writeb(0x0, FLTRCR(flctl));
  95. return;
  96. }
  97. udelay(1);
  98. }
  99. timeout_error(flctl, __func__);
  100. writeb(0x0, FLTRCR(flctl));
  101. }
  102. static void flctl_dma_complete(void *param)
  103. {
  104. struct sh_flctl *flctl = param;
  105. complete(&flctl->dma_complete);
  106. }
  107. static void flctl_release_dma(struct sh_flctl *flctl)
  108. {
  109. if (flctl->chan_fifo0_rx) {
  110. dma_release_channel(flctl->chan_fifo0_rx);
  111. flctl->chan_fifo0_rx = NULL;
  112. }
  113. if (flctl->chan_fifo0_tx) {
  114. dma_release_channel(flctl->chan_fifo0_tx);
  115. flctl->chan_fifo0_tx = NULL;
  116. }
  117. }
  118. static void flctl_setup_dma(struct sh_flctl *flctl)
  119. {
  120. dma_cap_mask_t mask;
  121. struct dma_slave_config cfg;
  122. struct platform_device *pdev = flctl->pdev;
  123. struct sh_flctl_platform_data *pdata = dev_get_platdata(&pdev->dev);
  124. int ret;
  125. if (!pdata)
  126. return;
  127. if (pdata->slave_id_fifo0_tx <= 0 || pdata->slave_id_fifo0_rx <= 0)
  128. return;
  129. /* We can only either use DMA for both Tx and Rx or not use it at all */
  130. dma_cap_zero(mask);
  131. dma_cap_set(DMA_SLAVE, mask);
  132. flctl->chan_fifo0_tx = dma_request_channel(mask, shdma_chan_filter,
  133. (void *)(uintptr_t)pdata->slave_id_fifo0_tx);
  134. dev_dbg(&pdev->dev, "%s: TX: got channel %p\n", __func__,
  135. flctl->chan_fifo0_tx);
  136. if (!flctl->chan_fifo0_tx)
  137. return;
  138. memset(&cfg, 0, sizeof(cfg));
  139. cfg.direction = DMA_MEM_TO_DEV;
  140. cfg.dst_addr = flctl->fifo;
  141. cfg.src_addr = 0;
  142. ret = dmaengine_slave_config(flctl->chan_fifo0_tx, &cfg);
  143. if (ret < 0)
  144. goto err;
  145. flctl->chan_fifo0_rx = dma_request_channel(mask, shdma_chan_filter,
  146. (void *)(uintptr_t)pdata->slave_id_fifo0_rx);
  147. dev_dbg(&pdev->dev, "%s: RX: got channel %p\n", __func__,
  148. flctl->chan_fifo0_rx);
  149. if (!flctl->chan_fifo0_rx)
  150. goto err;
  151. cfg.direction = DMA_DEV_TO_MEM;
  152. cfg.dst_addr = 0;
  153. cfg.src_addr = flctl->fifo;
  154. ret = dmaengine_slave_config(flctl->chan_fifo0_rx, &cfg);
  155. if (ret < 0)
  156. goto err;
  157. init_completion(&flctl->dma_complete);
  158. return;
  159. err:
  160. flctl_release_dma(flctl);
  161. }
  162. static void set_addr(struct mtd_info *mtd, int column, int page_addr)
  163. {
  164. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  165. uint32_t addr = 0;
  166. if (column == -1) {
  167. addr = page_addr; /* ERASE1 */
  168. } else if (page_addr != -1) {
  169. /* SEQIN, READ0, etc.. */
  170. if (flctl->chip.options & NAND_BUSWIDTH_16)
  171. column >>= 1;
  172. if (flctl->page_size) {
  173. addr = column & 0x0FFF;
  174. addr |= (page_addr & 0xff) << 16;
  175. addr |= ((page_addr >> 8) & 0xff) << 24;
  176. /* big than 128MB */
  177. if (flctl->rw_ADRCNT == ADRCNT2_E) {
  178. uint32_t addr2;
  179. addr2 = (page_addr >> 16) & 0xff;
  180. writel(addr2, FLADR2(flctl));
  181. }
  182. } else {
  183. addr = column;
  184. addr |= (page_addr & 0xff) << 8;
  185. addr |= ((page_addr >> 8) & 0xff) << 16;
  186. addr |= ((page_addr >> 16) & 0xff) << 24;
  187. }
  188. }
  189. writel(addr, FLADR(flctl));
  190. }
  191. static void wait_rfifo_ready(struct sh_flctl *flctl)
  192. {
  193. uint32_t timeout = LOOP_TIMEOUT_MAX;
  194. while (timeout--) {
  195. uint32_t val;
  196. /* check FIFO */
  197. val = readl(FLDTCNTR(flctl)) >> 16;
  198. if (val & 0xFF)
  199. return;
  200. udelay(1);
  201. }
  202. timeout_error(flctl, __func__);
  203. }
  204. static void wait_wfifo_ready(struct sh_flctl *flctl)
  205. {
  206. uint32_t len, timeout = LOOP_TIMEOUT_MAX;
  207. while (timeout--) {
  208. /* check FIFO */
  209. len = (readl(FLDTCNTR(flctl)) >> 16) & 0xFF;
  210. if (len >= 4)
  211. return;
  212. udelay(1);
  213. }
  214. timeout_error(flctl, __func__);
  215. }
  216. static enum flctl_ecc_res_t wait_recfifo_ready
  217. (struct sh_flctl *flctl, int sector_number)
  218. {
  219. uint32_t timeout = LOOP_TIMEOUT_MAX;
  220. void __iomem *ecc_reg[4];
  221. int i;
  222. int state = FL_SUCCESS;
  223. uint32_t data, size;
  224. /*
  225. * First this loops checks in FLDTCNTR if we are ready to read out the
  226. * oob data. This is the case if either all went fine without errors or
  227. * if the bottom part of the loop corrected the errors or marked them as
  228. * uncorrectable and the controller is given time to push the data into
  229. * the FIFO.
  230. */
  231. while (timeout--) {
  232. /* check if all is ok and we can read out the OOB */
  233. size = readl(FLDTCNTR(flctl)) >> 24;
  234. if ((size & 0xFF) == 4)
  235. return state;
  236. /* check if a correction code has been calculated */
  237. if (!(readl(FL4ECCCR(flctl)) & _4ECCEND)) {
  238. /*
  239. * either we wait for the fifo to be filled or a
  240. * correction pattern is being generated
  241. */
  242. udelay(1);
  243. continue;
  244. }
  245. /* check for an uncorrectable error */
  246. if (readl(FL4ECCCR(flctl)) & _4ECCFA) {
  247. /* check if we face a non-empty page */
  248. for (i = 0; i < 512; i++) {
  249. if (flctl->done_buff[i] != 0xff) {
  250. state = FL_ERROR; /* can't correct */
  251. break;
  252. }
  253. }
  254. if (state == FL_SUCCESS)
  255. dev_dbg(&flctl->pdev->dev,
  256. "reading empty sector %d, ecc error ignored\n",
  257. sector_number);
  258. writel(0, FL4ECCCR(flctl));
  259. continue;
  260. }
  261. /* start error correction */
  262. ecc_reg[0] = FL4ECCRESULT0(flctl);
  263. ecc_reg[1] = FL4ECCRESULT1(flctl);
  264. ecc_reg[2] = FL4ECCRESULT2(flctl);
  265. ecc_reg[3] = FL4ECCRESULT3(flctl);
  266. for (i = 0; i < 3; i++) {
  267. uint8_t org;
  268. unsigned int index;
  269. data = readl(ecc_reg[i]);
  270. if (flctl->page_size)
  271. index = (512 * sector_number) +
  272. (data >> 16);
  273. else
  274. index = data >> 16;
  275. org = flctl->done_buff[index];
  276. flctl->done_buff[index] = org ^ (data & 0xFF);
  277. }
  278. state = FL_REPAIRABLE;
  279. writel(0, FL4ECCCR(flctl));
  280. }
  281. timeout_error(flctl, __func__);
  282. return FL_TIMEOUT; /* timeout */
  283. }
  284. static void wait_wecfifo_ready(struct sh_flctl *flctl)
  285. {
  286. uint32_t timeout = LOOP_TIMEOUT_MAX;
  287. uint32_t len;
  288. while (timeout--) {
  289. /* check FLECFIFO */
  290. len = (readl(FLDTCNTR(flctl)) >> 24) & 0xFF;
  291. if (len >= 4)
  292. return;
  293. udelay(1);
  294. }
  295. timeout_error(flctl, __func__);
  296. }
  297. static int flctl_dma_fifo0_transfer(struct sh_flctl *flctl, unsigned long *buf,
  298. int len, enum dma_data_direction dir)
  299. {
  300. struct dma_async_tx_descriptor *desc = NULL;
  301. struct dma_chan *chan;
  302. enum dma_transfer_direction tr_dir;
  303. dma_addr_t dma_addr;
  304. dma_cookie_t cookie = -EINVAL;
  305. uint32_t reg;
  306. int ret;
  307. if (dir == DMA_FROM_DEVICE) {
  308. chan = flctl->chan_fifo0_rx;
  309. tr_dir = DMA_DEV_TO_MEM;
  310. } else {
  311. chan = flctl->chan_fifo0_tx;
  312. tr_dir = DMA_MEM_TO_DEV;
  313. }
  314. dma_addr = dma_map_single(chan->device->dev, buf, len, dir);
  315. if (dma_addr)
  316. desc = dmaengine_prep_slave_single(chan, dma_addr, len,
  317. tr_dir, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  318. if (desc) {
  319. reg = readl(FLINTDMACR(flctl));
  320. reg |= DREQ0EN;
  321. writel(reg, FLINTDMACR(flctl));
  322. desc->callback = flctl_dma_complete;
  323. desc->callback_param = flctl;
  324. cookie = dmaengine_submit(desc);
  325. dma_async_issue_pending(chan);
  326. } else {
  327. /* DMA failed, fall back to PIO */
  328. flctl_release_dma(flctl);
  329. dev_warn(&flctl->pdev->dev,
  330. "DMA failed, falling back to PIO\n");
  331. ret = -EIO;
  332. goto out;
  333. }
  334. ret =
  335. wait_for_completion_timeout(&flctl->dma_complete,
  336. msecs_to_jiffies(3000));
  337. if (ret <= 0) {
  338. dmaengine_terminate_all(chan);
  339. dev_err(&flctl->pdev->dev, "wait_for_completion_timeout\n");
  340. }
  341. out:
  342. reg = readl(FLINTDMACR(flctl));
  343. reg &= ~DREQ0EN;
  344. writel(reg, FLINTDMACR(flctl));
  345. dma_unmap_single(chan->device->dev, dma_addr, len, dir);
  346. /* ret > 0 is success */
  347. return ret;
  348. }
  349. static void read_datareg(struct sh_flctl *flctl, int offset)
  350. {
  351. unsigned long data;
  352. unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];
  353. wait_completion(flctl);
  354. data = readl(FLDATAR(flctl));
  355. *buf = le32_to_cpu(data);
  356. }
  357. static void read_fiforeg(struct sh_flctl *flctl, int rlen, int offset)
  358. {
  359. int i, len_4align;
  360. unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];
  361. len_4align = (rlen + 3) / 4;
  362. /* initiate DMA transfer */
  363. if (flctl->chan_fifo0_rx && rlen >= 32 &&
  364. flctl_dma_fifo0_transfer(flctl, buf, rlen, DMA_DEV_TO_MEM) > 0)
  365. goto convert; /* DMA success */
  366. /* do polling transfer */
  367. for (i = 0; i < len_4align; i++) {
  368. wait_rfifo_ready(flctl);
  369. buf[i] = readl(FLDTFIFO(flctl));
  370. }
  371. convert:
  372. for (i = 0; i < len_4align; i++)
  373. buf[i] = be32_to_cpu(buf[i]);
  374. }
  375. static enum flctl_ecc_res_t read_ecfiforeg
  376. (struct sh_flctl *flctl, uint8_t *buff, int sector)
  377. {
  378. int i;
  379. enum flctl_ecc_res_t res;
  380. unsigned long *ecc_buf = (unsigned long *)buff;
  381. res = wait_recfifo_ready(flctl , sector);
  382. if (res != FL_ERROR) {
  383. for (i = 0; i < 4; i++) {
  384. ecc_buf[i] = readl(FLECFIFO(flctl));
  385. ecc_buf[i] = be32_to_cpu(ecc_buf[i]);
  386. }
  387. }
  388. return res;
  389. }
  390. static void write_fiforeg(struct sh_flctl *flctl, int rlen,
  391. unsigned int offset)
  392. {
  393. int i, len_4align;
  394. unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];
  395. len_4align = (rlen + 3) / 4;
  396. for (i = 0; i < len_4align; i++) {
  397. wait_wfifo_ready(flctl);
  398. writel(cpu_to_be32(buf[i]), FLDTFIFO(flctl));
  399. }
  400. }
  401. static void write_ec_fiforeg(struct sh_flctl *flctl, int rlen,
  402. unsigned int offset)
  403. {
  404. int i, len_4align;
  405. unsigned long *buf = (unsigned long *)&flctl->done_buff[offset];
  406. len_4align = (rlen + 3) / 4;
  407. for (i = 0; i < len_4align; i++)
  408. buf[i] = cpu_to_be32(buf[i]);
  409. /* initiate DMA transfer */
  410. if (flctl->chan_fifo0_tx && rlen >= 32 &&
  411. flctl_dma_fifo0_transfer(flctl, buf, rlen, DMA_MEM_TO_DEV) > 0)
  412. return; /* DMA success */
  413. /* do polling transfer */
  414. for (i = 0; i < len_4align; i++) {
  415. wait_wecfifo_ready(flctl);
  416. writel(buf[i], FLECFIFO(flctl));
  417. }
  418. }
  419. static void set_cmd_regs(struct mtd_info *mtd, uint32_t cmd, uint32_t flcmcdr_val)
  420. {
  421. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  422. uint32_t flcmncr_val = flctl->flcmncr_base & ~SEL_16BIT;
  423. uint32_t flcmdcr_val, addr_len_bytes = 0;
  424. /* Set SNAND bit if page size is 2048byte */
  425. if (flctl->page_size)
  426. flcmncr_val |= SNAND_E;
  427. else
  428. flcmncr_val &= ~SNAND_E;
  429. /* default FLCMDCR val */
  430. flcmdcr_val = DOCMD1_E | DOADR_E;
  431. /* Set for FLCMDCR */
  432. switch (cmd) {
  433. case NAND_CMD_ERASE1:
  434. addr_len_bytes = flctl->erase_ADRCNT;
  435. flcmdcr_val |= DOCMD2_E;
  436. break;
  437. case NAND_CMD_READ0:
  438. case NAND_CMD_READOOB:
  439. case NAND_CMD_RNDOUT:
  440. addr_len_bytes = flctl->rw_ADRCNT;
  441. flcmdcr_val |= CDSRC_E;
  442. if (flctl->chip.options & NAND_BUSWIDTH_16)
  443. flcmncr_val |= SEL_16BIT;
  444. break;
  445. case NAND_CMD_SEQIN:
  446. /* This case is that cmd is READ0 or READ1 or READ00 */
  447. flcmdcr_val &= ~DOADR_E; /* ONLY execute 1st cmd */
  448. break;
  449. case NAND_CMD_PAGEPROG:
  450. addr_len_bytes = flctl->rw_ADRCNT;
  451. flcmdcr_val |= DOCMD2_E | CDSRC_E | SELRW;
  452. if (flctl->chip.options & NAND_BUSWIDTH_16)
  453. flcmncr_val |= SEL_16BIT;
  454. break;
  455. case NAND_CMD_READID:
  456. flcmncr_val &= ~SNAND_E;
  457. flcmdcr_val |= CDSRC_E;
  458. addr_len_bytes = ADRCNT_1;
  459. break;
  460. case NAND_CMD_STATUS:
  461. case NAND_CMD_RESET:
  462. flcmncr_val &= ~SNAND_E;
  463. flcmdcr_val &= ~(DOADR_E | DOSR_E);
  464. break;
  465. default:
  466. break;
  467. }
  468. /* Set address bytes parameter */
  469. flcmdcr_val |= addr_len_bytes;
  470. /* Now actually write */
  471. writel(flcmncr_val, FLCMNCR(flctl));
  472. writel(flcmdcr_val, FLCMDCR(flctl));
  473. writel(flcmcdr_val, FLCMCDR(flctl));
  474. }
  475. static int flctl_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  476. uint8_t *buf, int oob_required, int page)
  477. {
  478. chip->read_buf(mtd, buf, mtd->writesize);
  479. if (oob_required)
  480. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  481. return 0;
  482. }
  483. static int flctl_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  484. const uint8_t *buf, int oob_required,
  485. int page)
  486. {
  487. chip->write_buf(mtd, buf, mtd->writesize);
  488. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  489. return 0;
  490. }
  491. static void execmd_read_page_sector(struct mtd_info *mtd, int page_addr)
  492. {
  493. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  494. int sector, page_sectors;
  495. enum flctl_ecc_res_t ecc_result;
  496. page_sectors = flctl->page_size ? 4 : 1;
  497. set_cmd_regs(mtd, NAND_CMD_READ0,
  498. (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
  499. writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE | _4ECCCORRECT,
  500. FLCMNCR(flctl));
  501. writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
  502. writel(page_addr << 2, FLADR(flctl));
  503. empty_fifo(flctl);
  504. start_translation(flctl);
  505. for (sector = 0; sector < page_sectors; sector++) {
  506. read_fiforeg(flctl, 512, 512 * sector);
  507. ecc_result = read_ecfiforeg(flctl,
  508. &flctl->done_buff[mtd->writesize + 16 * sector],
  509. sector);
  510. switch (ecc_result) {
  511. case FL_REPAIRABLE:
  512. dev_info(&flctl->pdev->dev,
  513. "applied ecc on page 0x%x", page_addr);
  514. flctl->mtd.ecc_stats.corrected++;
  515. break;
  516. case FL_ERROR:
  517. dev_warn(&flctl->pdev->dev,
  518. "page 0x%x contains corrupted data\n",
  519. page_addr);
  520. flctl->mtd.ecc_stats.failed++;
  521. break;
  522. default:
  523. ;
  524. }
  525. }
  526. wait_completion(flctl);
  527. writel(readl(FLCMNCR(flctl)) & ~(ACM_SACCES_MODE | _4ECCCORRECT),
  528. FLCMNCR(flctl));
  529. }
  530. static void execmd_read_oob(struct mtd_info *mtd, int page_addr)
  531. {
  532. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  533. int page_sectors = flctl->page_size ? 4 : 1;
  534. int i;
  535. set_cmd_regs(mtd, NAND_CMD_READ0,
  536. (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
  537. empty_fifo(flctl);
  538. for (i = 0; i < page_sectors; i++) {
  539. set_addr(mtd, (512 + 16) * i + 512 , page_addr);
  540. writel(16, FLDTCNTR(flctl));
  541. start_translation(flctl);
  542. read_fiforeg(flctl, 16, 16 * i);
  543. wait_completion(flctl);
  544. }
  545. }
  546. static void execmd_write_page_sector(struct mtd_info *mtd)
  547. {
  548. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  549. int page_addr = flctl->seqin_page_addr;
  550. int sector, page_sectors;
  551. page_sectors = flctl->page_size ? 4 : 1;
  552. set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
  553. (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
  554. empty_fifo(flctl);
  555. writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE, FLCMNCR(flctl));
  556. writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
  557. writel(page_addr << 2, FLADR(flctl));
  558. start_translation(flctl);
  559. for (sector = 0; sector < page_sectors; sector++) {
  560. write_fiforeg(flctl, 512, 512 * sector);
  561. write_ec_fiforeg(flctl, 16, mtd->writesize + 16 * sector);
  562. }
  563. wait_completion(flctl);
  564. writel(readl(FLCMNCR(flctl)) & ~ACM_SACCES_MODE, FLCMNCR(flctl));
  565. }
  566. static void execmd_write_oob(struct mtd_info *mtd)
  567. {
  568. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  569. int page_addr = flctl->seqin_page_addr;
  570. int sector, page_sectors;
  571. page_sectors = flctl->page_size ? 4 : 1;
  572. set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
  573. (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
  574. for (sector = 0; sector < page_sectors; sector++) {
  575. empty_fifo(flctl);
  576. set_addr(mtd, sector * 528 + 512, page_addr);
  577. writel(16, FLDTCNTR(flctl)); /* set read size */
  578. start_translation(flctl);
  579. write_fiforeg(flctl, 16, 16 * sector);
  580. wait_completion(flctl);
  581. }
  582. }
  583. static void flctl_cmdfunc(struct mtd_info *mtd, unsigned int command,
  584. int column, int page_addr)
  585. {
  586. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  587. uint32_t read_cmd = 0;
  588. pm_runtime_get_sync(&flctl->pdev->dev);
  589. flctl->read_bytes = 0;
  590. if (command != NAND_CMD_PAGEPROG)
  591. flctl->index = 0;
  592. switch (command) {
  593. case NAND_CMD_READ1:
  594. case NAND_CMD_READ0:
  595. if (flctl->hwecc) {
  596. /* read page with hwecc */
  597. execmd_read_page_sector(mtd, page_addr);
  598. break;
  599. }
  600. if (flctl->page_size)
  601. set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
  602. | command);
  603. else
  604. set_cmd_regs(mtd, command, command);
  605. set_addr(mtd, 0, page_addr);
  606. flctl->read_bytes = mtd->writesize + mtd->oobsize;
  607. if (flctl->chip.options & NAND_BUSWIDTH_16)
  608. column >>= 1;
  609. flctl->index += column;
  610. goto read_normal_exit;
  611. case NAND_CMD_READOOB:
  612. if (flctl->hwecc) {
  613. /* read page with hwecc */
  614. execmd_read_oob(mtd, page_addr);
  615. break;
  616. }
  617. if (flctl->page_size) {
  618. set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
  619. | NAND_CMD_READ0);
  620. set_addr(mtd, mtd->writesize, page_addr);
  621. } else {
  622. set_cmd_regs(mtd, command, command);
  623. set_addr(mtd, 0, page_addr);
  624. }
  625. flctl->read_bytes = mtd->oobsize;
  626. goto read_normal_exit;
  627. case NAND_CMD_RNDOUT:
  628. if (flctl->hwecc)
  629. break;
  630. if (flctl->page_size)
  631. set_cmd_regs(mtd, command, (NAND_CMD_RNDOUTSTART << 8)
  632. | command);
  633. else
  634. set_cmd_regs(mtd, command, command);
  635. set_addr(mtd, column, 0);
  636. flctl->read_bytes = mtd->writesize + mtd->oobsize - column;
  637. goto read_normal_exit;
  638. case NAND_CMD_READID:
  639. set_cmd_regs(mtd, command, command);
  640. /* READID is always performed using an 8-bit bus */
  641. if (flctl->chip.options & NAND_BUSWIDTH_16)
  642. column <<= 1;
  643. set_addr(mtd, column, 0);
  644. flctl->read_bytes = 8;
  645. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  646. empty_fifo(flctl);
  647. start_translation(flctl);
  648. read_fiforeg(flctl, flctl->read_bytes, 0);
  649. wait_completion(flctl);
  650. break;
  651. case NAND_CMD_ERASE1:
  652. flctl->erase1_page_addr = page_addr;
  653. break;
  654. case NAND_CMD_ERASE2:
  655. set_cmd_regs(mtd, NAND_CMD_ERASE1,
  656. (command << 8) | NAND_CMD_ERASE1);
  657. set_addr(mtd, -1, flctl->erase1_page_addr);
  658. start_translation(flctl);
  659. wait_completion(flctl);
  660. break;
  661. case NAND_CMD_SEQIN:
  662. if (!flctl->page_size) {
  663. /* output read command */
  664. if (column >= mtd->writesize) {
  665. column -= mtd->writesize;
  666. read_cmd = NAND_CMD_READOOB;
  667. } else if (column < 256) {
  668. read_cmd = NAND_CMD_READ0;
  669. } else {
  670. column -= 256;
  671. read_cmd = NAND_CMD_READ1;
  672. }
  673. }
  674. flctl->seqin_column = column;
  675. flctl->seqin_page_addr = page_addr;
  676. flctl->seqin_read_cmd = read_cmd;
  677. break;
  678. case NAND_CMD_PAGEPROG:
  679. empty_fifo(flctl);
  680. if (!flctl->page_size) {
  681. set_cmd_regs(mtd, NAND_CMD_SEQIN,
  682. flctl->seqin_read_cmd);
  683. set_addr(mtd, -1, -1);
  684. writel(0, FLDTCNTR(flctl)); /* set 0 size */
  685. start_translation(flctl);
  686. wait_completion(flctl);
  687. }
  688. if (flctl->hwecc) {
  689. /* write page with hwecc */
  690. if (flctl->seqin_column == mtd->writesize)
  691. execmd_write_oob(mtd);
  692. else if (!flctl->seqin_column)
  693. execmd_write_page_sector(mtd);
  694. else
  695. printk(KERN_ERR "Invalid address !?\n");
  696. break;
  697. }
  698. set_cmd_regs(mtd, command, (command << 8) | NAND_CMD_SEQIN);
  699. set_addr(mtd, flctl->seqin_column, flctl->seqin_page_addr);
  700. writel(flctl->index, FLDTCNTR(flctl)); /* set write size */
  701. start_translation(flctl);
  702. write_fiforeg(flctl, flctl->index, 0);
  703. wait_completion(flctl);
  704. break;
  705. case NAND_CMD_STATUS:
  706. set_cmd_regs(mtd, command, command);
  707. set_addr(mtd, -1, -1);
  708. flctl->read_bytes = 1;
  709. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  710. start_translation(flctl);
  711. read_datareg(flctl, 0); /* read and end */
  712. break;
  713. case NAND_CMD_RESET:
  714. set_cmd_regs(mtd, command, command);
  715. set_addr(mtd, -1, -1);
  716. writel(0, FLDTCNTR(flctl)); /* set 0 size */
  717. start_translation(flctl);
  718. wait_completion(flctl);
  719. break;
  720. default:
  721. break;
  722. }
  723. goto runtime_exit;
  724. read_normal_exit:
  725. writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
  726. empty_fifo(flctl);
  727. start_translation(flctl);
  728. read_fiforeg(flctl, flctl->read_bytes, 0);
  729. wait_completion(flctl);
  730. runtime_exit:
  731. pm_runtime_put_sync(&flctl->pdev->dev);
  732. return;
  733. }
  734. static void flctl_select_chip(struct mtd_info *mtd, int chipnr)
  735. {
  736. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  737. int ret;
  738. switch (chipnr) {
  739. case -1:
  740. flctl->flcmncr_base &= ~CE0_ENABLE;
  741. pm_runtime_get_sync(&flctl->pdev->dev);
  742. writel(flctl->flcmncr_base, FLCMNCR(flctl));
  743. if (flctl->qos_request) {
  744. dev_pm_qos_remove_request(&flctl->pm_qos);
  745. flctl->qos_request = 0;
  746. }
  747. pm_runtime_put_sync(&flctl->pdev->dev);
  748. break;
  749. case 0:
  750. flctl->flcmncr_base |= CE0_ENABLE;
  751. if (!flctl->qos_request) {
  752. ret = dev_pm_qos_add_request(&flctl->pdev->dev,
  753. &flctl->pm_qos,
  754. DEV_PM_QOS_RESUME_LATENCY,
  755. 100);
  756. if (ret < 0)
  757. dev_err(&flctl->pdev->dev,
  758. "PM QoS request failed: %d\n", ret);
  759. flctl->qos_request = 1;
  760. }
  761. if (flctl->holden) {
  762. pm_runtime_get_sync(&flctl->pdev->dev);
  763. writel(HOLDEN, FLHOLDCR(flctl));
  764. pm_runtime_put_sync(&flctl->pdev->dev);
  765. }
  766. break;
  767. default:
  768. BUG();
  769. }
  770. }
  771. static void flctl_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  772. {
  773. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  774. memcpy(&flctl->done_buff[flctl->index], buf, len);
  775. flctl->index += len;
  776. }
  777. static uint8_t flctl_read_byte(struct mtd_info *mtd)
  778. {
  779. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  780. uint8_t data;
  781. data = flctl->done_buff[flctl->index];
  782. flctl->index++;
  783. return data;
  784. }
  785. static uint16_t flctl_read_word(struct mtd_info *mtd)
  786. {
  787. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  788. uint16_t *buf = (uint16_t *)&flctl->done_buff[flctl->index];
  789. flctl->index += 2;
  790. return *buf;
  791. }
  792. static void flctl_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  793. {
  794. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  795. memcpy(buf, &flctl->done_buff[flctl->index], len);
  796. flctl->index += len;
  797. }
  798. static int flctl_chip_init_tail(struct mtd_info *mtd)
  799. {
  800. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  801. struct nand_chip *chip = &flctl->chip;
  802. if (mtd->writesize == 512) {
  803. flctl->page_size = 0;
  804. if (chip->chipsize > (32 << 20)) {
  805. /* big than 32MB */
  806. flctl->rw_ADRCNT = ADRCNT_4;
  807. flctl->erase_ADRCNT = ADRCNT_3;
  808. } else if (chip->chipsize > (2 << 16)) {
  809. /* big than 128KB */
  810. flctl->rw_ADRCNT = ADRCNT_3;
  811. flctl->erase_ADRCNT = ADRCNT_2;
  812. } else {
  813. flctl->rw_ADRCNT = ADRCNT_2;
  814. flctl->erase_ADRCNT = ADRCNT_1;
  815. }
  816. } else {
  817. flctl->page_size = 1;
  818. if (chip->chipsize > (128 << 20)) {
  819. /* big than 128MB */
  820. flctl->rw_ADRCNT = ADRCNT2_E;
  821. flctl->erase_ADRCNT = ADRCNT_3;
  822. } else if (chip->chipsize > (8 << 16)) {
  823. /* big than 512KB */
  824. flctl->rw_ADRCNT = ADRCNT_4;
  825. flctl->erase_ADRCNT = ADRCNT_2;
  826. } else {
  827. flctl->rw_ADRCNT = ADRCNT_3;
  828. flctl->erase_ADRCNT = ADRCNT_1;
  829. }
  830. }
  831. if (flctl->hwecc) {
  832. if (mtd->writesize == 512) {
  833. chip->ecc.layout = &flctl_4secc_oob_16;
  834. chip->badblock_pattern = &flctl_4secc_smallpage;
  835. } else {
  836. chip->ecc.layout = &flctl_4secc_oob_64;
  837. chip->badblock_pattern = &flctl_4secc_largepage;
  838. }
  839. chip->ecc.size = 512;
  840. chip->ecc.bytes = 10;
  841. chip->ecc.strength = 4;
  842. chip->ecc.read_page = flctl_read_page_hwecc;
  843. chip->ecc.write_page = flctl_write_page_hwecc;
  844. chip->ecc.mode = NAND_ECC_HW;
  845. /* 4 symbols ECC enabled */
  846. flctl->flcmncr_base |= _4ECCEN;
  847. } else {
  848. chip->ecc.mode = NAND_ECC_SOFT;
  849. }
  850. return 0;
  851. }
  852. static irqreturn_t flctl_handle_flste(int irq, void *dev_id)
  853. {
  854. struct sh_flctl *flctl = dev_id;
  855. dev_err(&flctl->pdev->dev, "flste irq: %x\n", readl(FLINTDMACR(flctl)));
  856. writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
  857. return IRQ_HANDLED;
  858. }
  859. struct flctl_soc_config {
  860. unsigned long flcmncr_val;
  861. unsigned has_hwecc:1;
  862. unsigned use_holden:1;
  863. };
  864. static struct flctl_soc_config flctl_sh7372_config = {
  865. .flcmncr_val = CLK_16B_12L_4H | TYPESEL_SET | SHBUSSEL,
  866. .has_hwecc = 1,
  867. .use_holden = 1,
  868. };
  869. static const struct of_device_id of_flctl_match[] = {
  870. { .compatible = "renesas,shmobile-flctl-sh7372",
  871. .data = &flctl_sh7372_config },
  872. {},
  873. };
  874. MODULE_DEVICE_TABLE(of, of_flctl_match);
  875. static struct sh_flctl_platform_data *flctl_parse_dt(struct device *dev)
  876. {
  877. const struct of_device_id *match;
  878. struct flctl_soc_config *config;
  879. struct sh_flctl_platform_data *pdata;
  880. struct device_node *dn = dev->of_node;
  881. int ret;
  882. match = of_match_device(of_flctl_match, dev);
  883. if (match)
  884. config = (struct flctl_soc_config *)match->data;
  885. else {
  886. dev_err(dev, "%s: no OF configuration attached\n", __func__);
  887. return NULL;
  888. }
  889. pdata = devm_kzalloc(dev, sizeof(struct sh_flctl_platform_data),
  890. GFP_KERNEL);
  891. if (!pdata)
  892. return NULL;
  893. /* set SoC specific options */
  894. pdata->flcmncr_val = config->flcmncr_val;
  895. pdata->has_hwecc = config->has_hwecc;
  896. pdata->use_holden = config->use_holden;
  897. /* parse user defined options */
  898. ret = of_get_nand_bus_width(dn);
  899. if (ret == 16)
  900. pdata->flcmncr_val |= SEL_16BIT;
  901. else if (ret != 8) {
  902. dev_err(dev, "%s: invalid bus width\n", __func__);
  903. return NULL;
  904. }
  905. return pdata;
  906. }
  907. static int flctl_probe(struct platform_device *pdev)
  908. {
  909. struct resource *res;
  910. struct sh_flctl *flctl;
  911. struct mtd_info *flctl_mtd;
  912. struct nand_chip *nand;
  913. struct sh_flctl_platform_data *pdata;
  914. int ret;
  915. int irq;
  916. struct mtd_part_parser_data ppdata = {};
  917. flctl = devm_kzalloc(&pdev->dev, sizeof(struct sh_flctl), GFP_KERNEL);
  918. if (!flctl)
  919. return -ENOMEM;
  920. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  921. flctl->reg = devm_ioremap_resource(&pdev->dev, res);
  922. if (IS_ERR(flctl->reg))
  923. return PTR_ERR(flctl->reg);
  924. flctl->fifo = res->start + 0x24; /* FLDTFIFO */
  925. irq = platform_get_irq(pdev, 0);
  926. if (irq < 0) {
  927. dev_err(&pdev->dev, "failed to get flste irq data\n");
  928. return -ENXIO;
  929. }
  930. ret = devm_request_irq(&pdev->dev, irq, flctl_handle_flste, IRQF_SHARED,
  931. "flste", flctl);
  932. if (ret) {
  933. dev_err(&pdev->dev, "request interrupt failed.\n");
  934. return ret;
  935. }
  936. if (pdev->dev.of_node)
  937. pdata = flctl_parse_dt(&pdev->dev);
  938. else
  939. pdata = dev_get_platdata(&pdev->dev);
  940. if (!pdata) {
  941. dev_err(&pdev->dev, "no setup data defined\n");
  942. return -EINVAL;
  943. }
  944. platform_set_drvdata(pdev, flctl);
  945. flctl_mtd = &flctl->mtd;
  946. nand = &flctl->chip;
  947. flctl_mtd->priv = nand;
  948. flctl_mtd->dev.parent = &pdev->dev;
  949. flctl->pdev = pdev;
  950. flctl->hwecc = pdata->has_hwecc;
  951. flctl->holden = pdata->use_holden;
  952. flctl->flcmncr_base = pdata->flcmncr_val;
  953. flctl->flintdmacr_base = flctl->hwecc ? (STERINTE | ECERB) : STERINTE;
  954. /* Set address of hardware control function */
  955. /* 20 us command delay time */
  956. nand->chip_delay = 20;
  957. nand->read_byte = flctl_read_byte;
  958. nand->write_buf = flctl_write_buf;
  959. nand->read_buf = flctl_read_buf;
  960. nand->select_chip = flctl_select_chip;
  961. nand->cmdfunc = flctl_cmdfunc;
  962. if (pdata->flcmncr_val & SEL_16BIT) {
  963. nand->options |= NAND_BUSWIDTH_16;
  964. nand->read_word = flctl_read_word;
  965. }
  966. pm_runtime_enable(&pdev->dev);
  967. pm_runtime_resume(&pdev->dev);
  968. flctl_setup_dma(flctl);
  969. ret = nand_scan_ident(flctl_mtd, 1, NULL);
  970. if (ret)
  971. goto err_chip;
  972. ret = flctl_chip_init_tail(flctl_mtd);
  973. if (ret)
  974. goto err_chip;
  975. ret = nand_scan_tail(flctl_mtd);
  976. if (ret)
  977. goto err_chip;
  978. ppdata.of_node = pdev->dev.of_node;
  979. ret = mtd_device_parse_register(flctl_mtd, NULL, &ppdata, pdata->parts,
  980. pdata->nr_parts);
  981. return 0;
  982. err_chip:
  983. flctl_release_dma(flctl);
  984. pm_runtime_disable(&pdev->dev);
  985. return ret;
  986. }
  987. static int flctl_remove(struct platform_device *pdev)
  988. {
  989. struct sh_flctl *flctl = platform_get_drvdata(pdev);
  990. flctl_release_dma(flctl);
  991. nand_release(&flctl->mtd);
  992. pm_runtime_disable(&pdev->dev);
  993. return 0;
  994. }
  995. static struct platform_driver flctl_driver = {
  996. .remove = flctl_remove,
  997. .driver = {
  998. .name = "sh_flctl",
  999. .of_match_table = of_match_ptr(of_flctl_match),
  1000. },
  1001. };
  1002. module_platform_driver_probe(flctl_driver, flctl_probe);
  1003. MODULE_LICENSE("GPL");
  1004. MODULE_AUTHOR("Yoshihiro Shimoda");
  1005. MODULE_DESCRIPTION("SuperH FLCTL driver");
  1006. MODULE_ALIAS("platform:sh_flctl");