sh_flctl.c 28 KB

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