sh_flctl.c 27 KB

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