sh_flctl.c 29 KB

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