sh_flctl.c 24 KB

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