sh_flctl.c 30 KB

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