sh_flctl.c 29 KB

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