sh_flctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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,
  755. DEV_PM_QOS_LATENCY,
  756. 100);
  757. if (ret < 0)
  758. dev_err(&flctl->pdev->dev,
  759. "PM QoS request failed: %d\n", ret);
  760. flctl->qos_request = 1;
  761. }
  762. if (flctl->holden) {
  763. pm_runtime_get_sync(&flctl->pdev->dev);
  764. writel(HOLDEN, FLHOLDCR(flctl));
  765. pm_runtime_put_sync(&flctl->pdev->dev);
  766. }
  767. break;
  768. default:
  769. BUG();
  770. }
  771. }
  772. static void flctl_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  773. {
  774. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  775. memcpy(&flctl->done_buff[flctl->index], buf, len);
  776. flctl->index += len;
  777. }
  778. static uint8_t flctl_read_byte(struct mtd_info *mtd)
  779. {
  780. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  781. uint8_t data;
  782. data = flctl->done_buff[flctl->index];
  783. flctl->index++;
  784. return data;
  785. }
  786. static uint16_t flctl_read_word(struct mtd_info *mtd)
  787. {
  788. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  789. uint16_t *buf = (uint16_t *)&flctl->done_buff[flctl->index];
  790. flctl->index += 2;
  791. return *buf;
  792. }
  793. static void flctl_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  794. {
  795. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  796. memcpy(buf, &flctl->done_buff[flctl->index], len);
  797. flctl->index += len;
  798. }
  799. static int flctl_chip_init_tail(struct mtd_info *mtd)
  800. {
  801. struct sh_flctl *flctl = mtd_to_flctl(mtd);
  802. struct nand_chip *chip = &flctl->chip;
  803. if (mtd->writesize == 512) {
  804. flctl->page_size = 0;
  805. if (chip->chipsize > (32 << 20)) {
  806. /* big than 32MB */
  807. flctl->rw_ADRCNT = ADRCNT_4;
  808. flctl->erase_ADRCNT = ADRCNT_3;
  809. } else if (chip->chipsize > (2 << 16)) {
  810. /* big than 128KB */
  811. flctl->rw_ADRCNT = ADRCNT_3;
  812. flctl->erase_ADRCNT = ADRCNT_2;
  813. } else {
  814. flctl->rw_ADRCNT = ADRCNT_2;
  815. flctl->erase_ADRCNT = ADRCNT_1;
  816. }
  817. } else {
  818. flctl->page_size = 1;
  819. if (chip->chipsize > (128 << 20)) {
  820. /* big than 128MB */
  821. flctl->rw_ADRCNT = ADRCNT2_E;
  822. flctl->erase_ADRCNT = ADRCNT_3;
  823. } else if (chip->chipsize > (8 << 16)) {
  824. /* big than 512KB */
  825. flctl->rw_ADRCNT = ADRCNT_4;
  826. flctl->erase_ADRCNT = ADRCNT_2;
  827. } else {
  828. flctl->rw_ADRCNT = ADRCNT_3;
  829. flctl->erase_ADRCNT = ADRCNT_1;
  830. }
  831. }
  832. if (flctl->hwecc) {
  833. if (mtd->writesize == 512) {
  834. chip->ecc.layout = &flctl_4secc_oob_16;
  835. chip->badblock_pattern = &flctl_4secc_smallpage;
  836. } else {
  837. chip->ecc.layout = &flctl_4secc_oob_64;
  838. chip->badblock_pattern = &flctl_4secc_largepage;
  839. }
  840. chip->ecc.size = 512;
  841. chip->ecc.bytes = 10;
  842. chip->ecc.strength = 4;
  843. chip->ecc.read_page = flctl_read_page_hwecc;
  844. chip->ecc.write_page = flctl_write_page_hwecc;
  845. chip->ecc.mode = NAND_ECC_HW;
  846. /* 4 symbols ECC enabled */
  847. flctl->flcmncr_base |= _4ECCEN;
  848. } else {
  849. chip->ecc.mode = NAND_ECC_SOFT;
  850. }
  851. return 0;
  852. }
  853. static irqreturn_t flctl_handle_flste(int irq, void *dev_id)
  854. {
  855. struct sh_flctl *flctl = dev_id;
  856. dev_err(&flctl->pdev->dev, "flste irq: %x\n", readl(FLINTDMACR(flctl)));
  857. writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
  858. return IRQ_HANDLED;
  859. }
  860. #ifdef CONFIG_OF
  861. struct flctl_soc_config {
  862. unsigned long flcmncr_val;
  863. unsigned has_hwecc:1;
  864. unsigned use_holden:1;
  865. };
  866. static struct flctl_soc_config flctl_sh7372_config = {
  867. .flcmncr_val = CLK_16B_12L_4H | TYPESEL_SET | SHBUSSEL,
  868. .has_hwecc = 1,
  869. .use_holden = 1,
  870. };
  871. static const struct of_device_id of_flctl_match[] = {
  872. { .compatible = "renesas,shmobile-flctl-sh7372",
  873. .data = &flctl_sh7372_config },
  874. {},
  875. };
  876. MODULE_DEVICE_TABLE(of, of_flctl_match);
  877. static struct sh_flctl_platform_data *flctl_parse_dt(struct device *dev)
  878. {
  879. const struct of_device_id *match;
  880. struct flctl_soc_config *config;
  881. struct sh_flctl_platform_data *pdata;
  882. struct device_node *dn = dev->of_node;
  883. int ret;
  884. match = of_match_device(of_flctl_match, dev);
  885. if (match)
  886. config = (struct flctl_soc_config *)match->data;
  887. else {
  888. dev_err(dev, "%s: no OF configuration attached\n", __func__);
  889. return NULL;
  890. }
  891. pdata = devm_kzalloc(dev, sizeof(struct sh_flctl_platform_data),
  892. GFP_KERNEL);
  893. if (!pdata) {
  894. dev_err(dev, "%s: failed to allocate config data\n", __func__);
  895. return NULL;
  896. }
  897. /* set SoC specific options */
  898. pdata->flcmncr_val = config->flcmncr_val;
  899. pdata->has_hwecc = config->has_hwecc;
  900. pdata->use_holden = config->use_holden;
  901. /* parse user defined options */
  902. ret = of_get_nand_bus_width(dn);
  903. if (ret == 16)
  904. pdata->flcmncr_val |= SEL_16BIT;
  905. else if (ret != 8) {
  906. dev_err(dev, "%s: invalid bus width\n", __func__);
  907. return NULL;
  908. }
  909. return pdata;
  910. }
  911. #else /* CONFIG_OF */
  912. #define of_flctl_match NULL
  913. static struct sh_flctl_platform_data *flctl_parse_dt(struct device *dev)
  914. {
  915. return NULL;
  916. }
  917. #endif /* CONFIG_OF */
  918. static int flctl_probe(struct platform_device *pdev)
  919. {
  920. struct resource *res;
  921. struct sh_flctl *flctl;
  922. struct mtd_info *flctl_mtd;
  923. struct nand_chip *nand;
  924. struct sh_flctl_platform_data *pdata;
  925. int ret = -ENXIO;
  926. int irq;
  927. struct mtd_part_parser_data ppdata = {};
  928. flctl = kzalloc(sizeof(struct sh_flctl), GFP_KERNEL);
  929. if (!flctl) {
  930. dev_err(&pdev->dev, "failed to allocate driver data\n");
  931. return -ENOMEM;
  932. }
  933. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  934. if (!res) {
  935. dev_err(&pdev->dev, "failed to get I/O memory\n");
  936. goto err_iomap;
  937. }
  938. flctl->reg = ioremap(res->start, resource_size(res));
  939. if (flctl->reg == NULL) {
  940. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  941. goto err_iomap;
  942. }
  943. irq = platform_get_irq(pdev, 0);
  944. if (irq < 0) {
  945. dev_err(&pdev->dev, "failed to get flste irq data\n");
  946. goto err_flste;
  947. }
  948. ret = request_irq(irq, flctl_handle_flste, IRQF_SHARED, "flste", flctl);
  949. if (ret) {
  950. dev_err(&pdev->dev, "request interrupt failed.\n");
  951. goto err_flste;
  952. }
  953. if (pdev->dev.of_node)
  954. pdata = flctl_parse_dt(&pdev->dev);
  955. else
  956. pdata = pdev->dev.platform_data;
  957. if (!pdata) {
  958. dev_err(&pdev->dev, "no setup data defined\n");
  959. ret = -EINVAL;
  960. goto err_pdata;
  961. }
  962. platform_set_drvdata(pdev, flctl);
  963. flctl_mtd = &flctl->mtd;
  964. nand = &flctl->chip;
  965. flctl_mtd->priv = nand;
  966. flctl->pdev = pdev;
  967. flctl->hwecc = pdata->has_hwecc;
  968. flctl->holden = pdata->use_holden;
  969. flctl->flcmncr_base = pdata->flcmncr_val;
  970. flctl->flintdmacr_base = flctl->hwecc ? (STERINTE | ECERB) : STERINTE;
  971. /* Set address of hardware control function */
  972. /* 20 us command delay time */
  973. nand->chip_delay = 20;
  974. nand->read_byte = flctl_read_byte;
  975. nand->write_buf = flctl_write_buf;
  976. nand->read_buf = flctl_read_buf;
  977. nand->select_chip = flctl_select_chip;
  978. nand->cmdfunc = flctl_cmdfunc;
  979. if (pdata->flcmncr_val & SEL_16BIT) {
  980. nand->options |= NAND_BUSWIDTH_16;
  981. nand->read_word = flctl_read_word;
  982. }
  983. pm_runtime_enable(&pdev->dev);
  984. pm_runtime_resume(&pdev->dev);
  985. flctl_setup_dma(flctl);
  986. ret = nand_scan_ident(flctl_mtd, 1, NULL);
  987. if (ret)
  988. goto err_chip;
  989. ret = flctl_chip_init_tail(flctl_mtd);
  990. if (ret)
  991. goto err_chip;
  992. ret = nand_scan_tail(flctl_mtd);
  993. if (ret)
  994. goto err_chip;
  995. ppdata.of_node = pdev->dev.of_node;
  996. ret = mtd_device_parse_register(flctl_mtd, NULL, &ppdata, pdata->parts,
  997. pdata->nr_parts);
  998. return 0;
  999. err_chip:
  1000. flctl_release_dma(flctl);
  1001. pm_runtime_disable(&pdev->dev);
  1002. err_pdata:
  1003. free_irq(irq, flctl);
  1004. err_flste:
  1005. iounmap(flctl->reg);
  1006. err_iomap:
  1007. kfree(flctl);
  1008. return ret;
  1009. }
  1010. static int flctl_remove(struct platform_device *pdev)
  1011. {
  1012. struct sh_flctl *flctl = platform_get_drvdata(pdev);
  1013. flctl_release_dma(flctl);
  1014. nand_release(&flctl->mtd);
  1015. pm_runtime_disable(&pdev->dev);
  1016. free_irq(platform_get_irq(pdev, 0), flctl);
  1017. iounmap(flctl->reg);
  1018. kfree(flctl);
  1019. return 0;
  1020. }
  1021. static struct platform_driver flctl_driver = {
  1022. .remove = flctl_remove,
  1023. .driver = {
  1024. .name = "sh_flctl",
  1025. .owner = THIS_MODULE,
  1026. .of_match_table = of_flctl_match,
  1027. },
  1028. };
  1029. static int __init flctl_nand_init(void)
  1030. {
  1031. return platform_driver_probe(&flctl_driver, flctl_probe);
  1032. }
  1033. static void __exit flctl_nand_cleanup(void)
  1034. {
  1035. platform_driver_unregister(&flctl_driver);
  1036. }
  1037. module_init(flctl_nand_init);
  1038. module_exit(flctl_nand_cleanup);
  1039. MODULE_LICENSE("GPL");
  1040. MODULE_AUTHOR("Yoshihiro Shimoda");
  1041. MODULE_DESCRIPTION("SuperH FLCTL driver");
  1042. MODULE_ALIAS("platform:sh_flctl");