samsung.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * Samsung S3C64XX/S5PC1XX OneNAND driver
  3. *
  4. * Copyright © 2008-2010 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. * Marek Szyprowski <m.szyprowski@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Implementation:
  13. * S3C64XX and S5PC100: emulate the pseudo BufferRAM
  14. * S5PC110: use DMA
  15. */
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/onenand.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/interrupt.h>
  25. #include <asm/mach/flash.h>
  26. #include <plat/regs-onenand.h>
  27. #include <linux/io.h>
  28. enum soc_type {
  29. TYPE_S3C6400,
  30. TYPE_S3C6410,
  31. TYPE_S5PC100,
  32. TYPE_S5PC110,
  33. };
  34. #define ONENAND_ERASE_STATUS 0x00
  35. #define ONENAND_MULTI_ERASE_SET 0x01
  36. #define ONENAND_ERASE_START 0x03
  37. #define ONENAND_UNLOCK_START 0x08
  38. #define ONENAND_UNLOCK_END 0x09
  39. #define ONENAND_LOCK_START 0x0A
  40. #define ONENAND_LOCK_END 0x0B
  41. #define ONENAND_LOCK_TIGHT_START 0x0C
  42. #define ONENAND_LOCK_TIGHT_END 0x0D
  43. #define ONENAND_UNLOCK_ALL 0x0E
  44. #define ONENAND_OTP_ACCESS 0x12
  45. #define ONENAND_SPARE_ACCESS_ONLY 0x13
  46. #define ONENAND_MAIN_ACCESS_ONLY 0x14
  47. #define ONENAND_ERASE_VERIFY 0x15
  48. #define ONENAND_MAIN_SPARE_ACCESS 0x16
  49. #define ONENAND_PIPELINE_READ 0x4000
  50. #define MAP_00 (0x0)
  51. #define MAP_01 (0x1)
  52. #define MAP_10 (0x2)
  53. #define MAP_11 (0x3)
  54. #define S3C64XX_CMD_MAP_SHIFT 24
  55. #define S5PC100_CMD_MAP_SHIFT 26
  56. #define S3C6400_FBA_SHIFT 10
  57. #define S3C6400_FPA_SHIFT 4
  58. #define S3C6400_FSA_SHIFT 2
  59. #define S3C6410_FBA_SHIFT 12
  60. #define S3C6410_FPA_SHIFT 6
  61. #define S3C6410_FSA_SHIFT 4
  62. #define S5PC100_FBA_SHIFT 13
  63. #define S5PC100_FPA_SHIFT 7
  64. #define S5PC100_FSA_SHIFT 5
  65. /* S5PC110 specific definitions */
  66. #define S5PC110_DMA_SRC_ADDR 0x400
  67. #define S5PC110_DMA_SRC_CFG 0x404
  68. #define S5PC110_DMA_DST_ADDR 0x408
  69. #define S5PC110_DMA_DST_CFG 0x40C
  70. #define S5PC110_DMA_TRANS_SIZE 0x414
  71. #define S5PC110_DMA_TRANS_CMD 0x418
  72. #define S5PC110_DMA_TRANS_STATUS 0x41C
  73. #define S5PC110_DMA_TRANS_DIR 0x420
  74. #define S5PC110_INTC_DMA_CLR 0x1004
  75. #define S5PC110_INTC_ONENAND_CLR 0x1008
  76. #define S5PC110_INTC_DMA_MASK 0x1024
  77. #define S5PC110_INTC_ONENAND_MASK 0x1028
  78. #define S5PC110_INTC_DMA_PEND 0x1044
  79. #define S5PC110_INTC_ONENAND_PEND 0x1048
  80. #define S5PC110_INTC_DMA_STATUS 0x1064
  81. #define S5PC110_INTC_ONENAND_STATUS 0x1068
  82. #define S5PC110_INTC_DMA_TD (1 << 24)
  83. #define S5PC110_INTC_DMA_TE (1 << 16)
  84. #define S5PC110_DMA_CFG_SINGLE (0x0 << 16)
  85. #define S5PC110_DMA_CFG_4BURST (0x2 << 16)
  86. #define S5PC110_DMA_CFG_8BURST (0x3 << 16)
  87. #define S5PC110_DMA_CFG_16BURST (0x4 << 16)
  88. #define S5PC110_DMA_CFG_INC (0x0 << 8)
  89. #define S5PC110_DMA_CFG_CNT (0x1 << 8)
  90. #define S5PC110_DMA_CFG_8BIT (0x0 << 0)
  91. #define S5PC110_DMA_CFG_16BIT (0x1 << 0)
  92. #define S5PC110_DMA_CFG_32BIT (0x2 << 0)
  93. #define S5PC110_DMA_SRC_CFG_READ (S5PC110_DMA_CFG_16BURST | \
  94. S5PC110_DMA_CFG_INC | \
  95. S5PC110_DMA_CFG_16BIT)
  96. #define S5PC110_DMA_DST_CFG_READ (S5PC110_DMA_CFG_16BURST | \
  97. S5PC110_DMA_CFG_INC | \
  98. S5PC110_DMA_CFG_32BIT)
  99. #define S5PC110_DMA_SRC_CFG_WRITE (S5PC110_DMA_CFG_16BURST | \
  100. S5PC110_DMA_CFG_INC | \
  101. S5PC110_DMA_CFG_32BIT)
  102. #define S5PC110_DMA_DST_CFG_WRITE (S5PC110_DMA_CFG_16BURST | \
  103. S5PC110_DMA_CFG_INC | \
  104. S5PC110_DMA_CFG_16BIT)
  105. #define S5PC110_DMA_TRANS_CMD_TDC (0x1 << 18)
  106. #define S5PC110_DMA_TRANS_CMD_TEC (0x1 << 16)
  107. #define S5PC110_DMA_TRANS_CMD_TR (0x1 << 0)
  108. #define S5PC110_DMA_TRANS_STATUS_TD (0x1 << 18)
  109. #define S5PC110_DMA_TRANS_STATUS_TB (0x1 << 17)
  110. #define S5PC110_DMA_TRANS_STATUS_TE (0x1 << 16)
  111. #define S5PC110_DMA_DIR_READ 0x0
  112. #define S5PC110_DMA_DIR_WRITE 0x1
  113. struct s3c_onenand {
  114. struct mtd_info *mtd;
  115. struct platform_device *pdev;
  116. enum soc_type type;
  117. void __iomem *base;
  118. struct resource *base_res;
  119. void __iomem *ahb_addr;
  120. struct resource *ahb_res;
  121. int bootram_command;
  122. void __iomem *page_buf;
  123. void __iomem *oob_buf;
  124. unsigned int (*mem_addr)(int fba, int fpa, int fsa);
  125. unsigned int (*cmd_map)(unsigned int type, unsigned int val);
  126. void __iomem *dma_addr;
  127. struct resource *dma_res;
  128. unsigned long phys_base;
  129. struct completion complete;
  130. };
  131. #define CMD_MAP_00(dev, addr) (dev->cmd_map(MAP_00, ((addr) << 1)))
  132. #define CMD_MAP_01(dev, mem_addr) (dev->cmd_map(MAP_01, (mem_addr)))
  133. #define CMD_MAP_10(dev, mem_addr) (dev->cmd_map(MAP_10, (mem_addr)))
  134. #define CMD_MAP_11(dev, addr) (dev->cmd_map(MAP_11, ((addr) << 2)))
  135. static struct s3c_onenand *onenand;
  136. static inline int s3c_read_reg(int offset)
  137. {
  138. return readl(onenand->base + offset);
  139. }
  140. static inline void s3c_write_reg(int value, int offset)
  141. {
  142. writel(value, onenand->base + offset);
  143. }
  144. static inline int s3c_read_cmd(unsigned int cmd)
  145. {
  146. return readl(onenand->ahb_addr + cmd);
  147. }
  148. static inline void s3c_write_cmd(int value, unsigned int cmd)
  149. {
  150. writel(value, onenand->ahb_addr + cmd);
  151. }
  152. #ifdef SAMSUNG_DEBUG
  153. static void s3c_dump_reg(void)
  154. {
  155. int i;
  156. for (i = 0; i < 0x400; i += 0x40) {
  157. printk(KERN_INFO "0x%08X: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  158. (unsigned int) onenand->base + i,
  159. s3c_read_reg(i), s3c_read_reg(i + 0x10),
  160. s3c_read_reg(i + 0x20), s3c_read_reg(i + 0x30));
  161. }
  162. }
  163. #endif
  164. static unsigned int s3c64xx_cmd_map(unsigned type, unsigned val)
  165. {
  166. return (type << S3C64XX_CMD_MAP_SHIFT) | val;
  167. }
  168. static unsigned int s5pc1xx_cmd_map(unsigned type, unsigned val)
  169. {
  170. return (type << S5PC100_CMD_MAP_SHIFT) | val;
  171. }
  172. static unsigned int s3c6400_mem_addr(int fba, int fpa, int fsa)
  173. {
  174. return (fba << S3C6400_FBA_SHIFT) | (fpa << S3C6400_FPA_SHIFT) |
  175. (fsa << S3C6400_FSA_SHIFT);
  176. }
  177. static unsigned int s3c6410_mem_addr(int fba, int fpa, int fsa)
  178. {
  179. return (fba << S3C6410_FBA_SHIFT) | (fpa << S3C6410_FPA_SHIFT) |
  180. (fsa << S3C6410_FSA_SHIFT);
  181. }
  182. static unsigned int s5pc100_mem_addr(int fba, int fpa, int fsa)
  183. {
  184. return (fba << S5PC100_FBA_SHIFT) | (fpa << S5PC100_FPA_SHIFT) |
  185. (fsa << S5PC100_FSA_SHIFT);
  186. }
  187. static void s3c_onenand_reset(void)
  188. {
  189. unsigned long timeout = 0x10000;
  190. int stat;
  191. s3c_write_reg(ONENAND_MEM_RESET_COLD, MEM_RESET_OFFSET);
  192. while (1 && timeout--) {
  193. stat = s3c_read_reg(INT_ERR_STAT_OFFSET);
  194. if (stat & RST_CMP)
  195. break;
  196. }
  197. stat = s3c_read_reg(INT_ERR_STAT_OFFSET);
  198. s3c_write_reg(stat, INT_ERR_ACK_OFFSET);
  199. /* Clear interrupt */
  200. s3c_write_reg(0x0, INT_ERR_ACK_OFFSET);
  201. /* Clear the ECC status */
  202. s3c_write_reg(0x0, ECC_ERR_STAT_OFFSET);
  203. }
  204. static unsigned short s3c_onenand_readw(void __iomem *addr)
  205. {
  206. struct onenand_chip *this = onenand->mtd->priv;
  207. struct device *dev = &onenand->pdev->dev;
  208. int reg = addr - this->base;
  209. int word_addr = reg >> 1;
  210. int value;
  211. /* It's used for probing time */
  212. switch (reg) {
  213. case ONENAND_REG_MANUFACTURER_ID:
  214. return s3c_read_reg(MANUFACT_ID_OFFSET);
  215. case ONENAND_REG_DEVICE_ID:
  216. return s3c_read_reg(DEVICE_ID_OFFSET);
  217. case ONENAND_REG_VERSION_ID:
  218. return s3c_read_reg(FLASH_VER_ID_OFFSET);
  219. case ONENAND_REG_DATA_BUFFER_SIZE:
  220. return s3c_read_reg(DATA_BUF_SIZE_OFFSET);
  221. case ONENAND_REG_TECHNOLOGY:
  222. return s3c_read_reg(TECH_OFFSET);
  223. case ONENAND_REG_SYS_CFG1:
  224. return s3c_read_reg(MEM_CFG_OFFSET);
  225. /* Used at unlock all status */
  226. case ONENAND_REG_CTRL_STATUS:
  227. return 0;
  228. case ONENAND_REG_WP_STATUS:
  229. return ONENAND_WP_US;
  230. default:
  231. break;
  232. }
  233. /* BootRAM access control */
  234. if ((unsigned int) addr < ONENAND_DATARAM && onenand->bootram_command) {
  235. if (word_addr == 0)
  236. return s3c_read_reg(MANUFACT_ID_OFFSET);
  237. if (word_addr == 1)
  238. return s3c_read_reg(DEVICE_ID_OFFSET);
  239. if (word_addr == 2)
  240. return s3c_read_reg(FLASH_VER_ID_OFFSET);
  241. }
  242. value = s3c_read_cmd(CMD_MAP_11(onenand, word_addr)) & 0xffff;
  243. dev_info(dev, "%s: Illegal access at reg 0x%x, value 0x%x\n", __func__,
  244. word_addr, value);
  245. return value;
  246. }
  247. static void s3c_onenand_writew(unsigned short value, void __iomem *addr)
  248. {
  249. struct onenand_chip *this = onenand->mtd->priv;
  250. struct device *dev = &onenand->pdev->dev;
  251. unsigned int reg = addr - this->base;
  252. unsigned int word_addr = reg >> 1;
  253. /* It's used for probing time */
  254. switch (reg) {
  255. case ONENAND_REG_SYS_CFG1:
  256. s3c_write_reg(value, MEM_CFG_OFFSET);
  257. return;
  258. case ONENAND_REG_START_ADDRESS1:
  259. case ONENAND_REG_START_ADDRESS2:
  260. return;
  261. /* Lock/lock-tight/unlock/unlock_all */
  262. case ONENAND_REG_START_BLOCK_ADDRESS:
  263. return;
  264. default:
  265. break;
  266. }
  267. /* BootRAM access control */
  268. if ((unsigned int)addr < ONENAND_DATARAM) {
  269. if (value == ONENAND_CMD_READID) {
  270. onenand->bootram_command = 1;
  271. return;
  272. }
  273. if (value == ONENAND_CMD_RESET) {
  274. s3c_write_reg(ONENAND_MEM_RESET_COLD, MEM_RESET_OFFSET);
  275. onenand->bootram_command = 0;
  276. return;
  277. }
  278. }
  279. dev_info(dev, "%s: Illegal access at reg 0x%x, value 0x%x\n", __func__,
  280. word_addr, value);
  281. s3c_write_cmd(value, CMD_MAP_11(onenand, word_addr));
  282. }
  283. static int s3c_onenand_wait(struct mtd_info *mtd, int state)
  284. {
  285. struct device *dev = &onenand->pdev->dev;
  286. unsigned int flags = INT_ACT;
  287. unsigned int stat, ecc;
  288. unsigned long timeout;
  289. switch (state) {
  290. case FL_READING:
  291. flags |= BLK_RW_CMP | LOAD_CMP;
  292. break;
  293. case FL_WRITING:
  294. flags |= BLK_RW_CMP | PGM_CMP;
  295. break;
  296. case FL_ERASING:
  297. flags |= BLK_RW_CMP | ERS_CMP;
  298. break;
  299. case FL_LOCKING:
  300. flags |= BLK_RW_CMP;
  301. break;
  302. default:
  303. break;
  304. }
  305. /* The 20 msec is enough */
  306. timeout = jiffies + msecs_to_jiffies(20);
  307. while (time_before(jiffies, timeout)) {
  308. stat = s3c_read_reg(INT_ERR_STAT_OFFSET);
  309. if (stat & flags)
  310. break;
  311. if (state != FL_READING)
  312. cond_resched();
  313. }
  314. /* To get correct interrupt status in timeout case */
  315. stat = s3c_read_reg(INT_ERR_STAT_OFFSET);
  316. s3c_write_reg(stat, INT_ERR_ACK_OFFSET);
  317. /*
  318. * In the Spec. it checks the controller status first
  319. * However if you get the correct information in case of
  320. * power off recovery (POR) test, it should read ECC status first
  321. */
  322. if (stat & LOAD_CMP) {
  323. ecc = s3c_read_reg(ECC_ERR_STAT_OFFSET);
  324. if (ecc & ONENAND_ECC_4BIT_UNCORRECTABLE) {
  325. dev_info(dev, "%s: ECC error = 0x%04x\n", __func__,
  326. ecc);
  327. mtd->ecc_stats.failed++;
  328. return -EBADMSG;
  329. }
  330. }
  331. if (stat & (LOCKED_BLK | ERS_FAIL | PGM_FAIL | LD_FAIL_ECC_ERR)) {
  332. dev_info(dev, "%s: controller error = 0x%04x\n", __func__,
  333. stat);
  334. if (stat & LOCKED_BLK)
  335. dev_info(dev, "%s: it's locked error = 0x%04x\n",
  336. __func__, stat);
  337. return -EIO;
  338. }
  339. return 0;
  340. }
  341. static int s3c_onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
  342. size_t len)
  343. {
  344. struct onenand_chip *this = mtd->priv;
  345. unsigned int *m, *s;
  346. int fba, fpa, fsa = 0;
  347. unsigned int mem_addr, cmd_map_01, cmd_map_10;
  348. int i, mcount, scount;
  349. int index;
  350. fba = (int) (addr >> this->erase_shift);
  351. fpa = (int) (addr >> this->page_shift);
  352. fpa &= this->page_mask;
  353. mem_addr = onenand->mem_addr(fba, fpa, fsa);
  354. cmd_map_01 = CMD_MAP_01(onenand, mem_addr);
  355. cmd_map_10 = CMD_MAP_10(onenand, mem_addr);
  356. switch (cmd) {
  357. case ONENAND_CMD_READ:
  358. case ONENAND_CMD_READOOB:
  359. case ONENAND_CMD_BUFFERRAM:
  360. ONENAND_SET_NEXT_BUFFERRAM(this);
  361. default:
  362. break;
  363. }
  364. index = ONENAND_CURRENT_BUFFERRAM(this);
  365. /*
  366. * Emulate Two BufferRAMs and access with 4 bytes pointer
  367. */
  368. m = (unsigned int *) onenand->page_buf;
  369. s = (unsigned int *) onenand->oob_buf;
  370. if (index) {
  371. m += (this->writesize >> 2);
  372. s += (mtd->oobsize >> 2);
  373. }
  374. mcount = mtd->writesize >> 2;
  375. scount = mtd->oobsize >> 2;
  376. switch (cmd) {
  377. case ONENAND_CMD_READ:
  378. /* Main */
  379. for (i = 0; i < mcount; i++)
  380. *m++ = s3c_read_cmd(cmd_map_01);
  381. return 0;
  382. case ONENAND_CMD_READOOB:
  383. s3c_write_reg(TSRF, TRANS_SPARE_OFFSET);
  384. /* Main */
  385. for (i = 0; i < mcount; i++)
  386. *m++ = s3c_read_cmd(cmd_map_01);
  387. /* Spare */
  388. for (i = 0; i < scount; i++)
  389. *s++ = s3c_read_cmd(cmd_map_01);
  390. s3c_write_reg(0, TRANS_SPARE_OFFSET);
  391. return 0;
  392. case ONENAND_CMD_PROG:
  393. /* Main */
  394. for (i = 0; i < mcount; i++)
  395. s3c_write_cmd(*m++, cmd_map_01);
  396. return 0;
  397. case ONENAND_CMD_PROGOOB:
  398. s3c_write_reg(TSRF, TRANS_SPARE_OFFSET);
  399. /* Main - dummy write */
  400. for (i = 0; i < mcount; i++)
  401. s3c_write_cmd(0xffffffff, cmd_map_01);
  402. /* Spare */
  403. for (i = 0; i < scount; i++)
  404. s3c_write_cmd(*s++, cmd_map_01);
  405. s3c_write_reg(0, TRANS_SPARE_OFFSET);
  406. return 0;
  407. case ONENAND_CMD_UNLOCK_ALL:
  408. s3c_write_cmd(ONENAND_UNLOCK_ALL, cmd_map_10);
  409. return 0;
  410. case ONENAND_CMD_ERASE:
  411. s3c_write_cmd(ONENAND_ERASE_START, cmd_map_10);
  412. return 0;
  413. default:
  414. break;
  415. }
  416. return 0;
  417. }
  418. static unsigned char *s3c_get_bufferram(struct mtd_info *mtd, int area)
  419. {
  420. struct onenand_chip *this = mtd->priv;
  421. int index = ONENAND_CURRENT_BUFFERRAM(this);
  422. unsigned char *p;
  423. if (area == ONENAND_DATARAM) {
  424. p = (unsigned char *) onenand->page_buf;
  425. if (index == 1)
  426. p += this->writesize;
  427. } else {
  428. p = (unsigned char *) onenand->oob_buf;
  429. if (index == 1)
  430. p += mtd->oobsize;
  431. }
  432. return p;
  433. }
  434. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  435. unsigned char *buffer, int offset,
  436. size_t count)
  437. {
  438. unsigned char *p;
  439. p = s3c_get_bufferram(mtd, area);
  440. memcpy(buffer, p + offset, count);
  441. return 0;
  442. }
  443. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  444. const unsigned char *buffer, int offset,
  445. size_t count)
  446. {
  447. unsigned char *p;
  448. p = s3c_get_bufferram(mtd, area);
  449. memcpy(p + offset, buffer, count);
  450. return 0;
  451. }
  452. static int (*s5pc110_dma_ops)(void *dst, void *src, size_t count, int direction);
  453. static int s5pc110_dma_poll(void *dst, void *src, size_t count, int direction)
  454. {
  455. void __iomem *base = onenand->dma_addr;
  456. int status;
  457. unsigned long timeout;
  458. writel(src, base + S5PC110_DMA_SRC_ADDR);
  459. writel(dst, base + S5PC110_DMA_DST_ADDR);
  460. if (direction == S5PC110_DMA_DIR_READ) {
  461. writel(S5PC110_DMA_SRC_CFG_READ, base + S5PC110_DMA_SRC_CFG);
  462. writel(S5PC110_DMA_DST_CFG_READ, base + S5PC110_DMA_DST_CFG);
  463. } else {
  464. writel(S5PC110_DMA_SRC_CFG_WRITE, base + S5PC110_DMA_SRC_CFG);
  465. writel(S5PC110_DMA_DST_CFG_WRITE, base + S5PC110_DMA_DST_CFG);
  466. }
  467. writel(count, base + S5PC110_DMA_TRANS_SIZE);
  468. writel(direction, base + S5PC110_DMA_TRANS_DIR);
  469. writel(S5PC110_DMA_TRANS_CMD_TR, base + S5PC110_DMA_TRANS_CMD);
  470. /*
  471. * There's no exact timeout values at Spec.
  472. * In real case it takes under 1 msec.
  473. * So 20 msecs are enough.
  474. */
  475. timeout = jiffies + msecs_to_jiffies(20);
  476. do {
  477. status = readl(base + S5PC110_DMA_TRANS_STATUS);
  478. if (status & S5PC110_DMA_TRANS_STATUS_TE) {
  479. writel(S5PC110_DMA_TRANS_CMD_TEC,
  480. base + S5PC110_DMA_TRANS_CMD);
  481. return -EIO;
  482. }
  483. } while (!(status & S5PC110_DMA_TRANS_STATUS_TD) &&
  484. time_before(jiffies, timeout));
  485. writel(S5PC110_DMA_TRANS_CMD_TDC, base + S5PC110_DMA_TRANS_CMD);
  486. return 0;
  487. }
  488. static irqreturn_t s5pc110_onenand_irq(int irq, void *data)
  489. {
  490. void __iomem *base = onenand->dma_addr;
  491. int status, cmd = 0;
  492. status = readl(base + S5PC110_INTC_DMA_STATUS);
  493. if (likely(status & S5PC110_INTC_DMA_TD))
  494. cmd = S5PC110_DMA_TRANS_CMD_TDC;
  495. if (unlikely(status & S5PC110_INTC_DMA_TE))
  496. cmd = S5PC110_DMA_TRANS_CMD_TEC;
  497. writel(cmd, base + S5PC110_DMA_TRANS_CMD);
  498. writel(status, base + S5PC110_INTC_DMA_CLR);
  499. if (!onenand->complete.done)
  500. complete(&onenand->complete);
  501. return IRQ_HANDLED;
  502. }
  503. static int s5pc110_dma_irq(void *dst, void *src, size_t count, int direction)
  504. {
  505. void __iomem *base = onenand->dma_addr;
  506. int status;
  507. status = readl(base + S5PC110_INTC_DMA_MASK);
  508. if (status) {
  509. status &= ~(S5PC110_INTC_DMA_TD | S5PC110_INTC_DMA_TE);
  510. writel(status, base + S5PC110_INTC_DMA_MASK);
  511. }
  512. writel(src, base + S5PC110_DMA_SRC_ADDR);
  513. writel(dst, base + S5PC110_DMA_DST_ADDR);
  514. if (direction == S5PC110_DMA_DIR_READ) {
  515. writel(S5PC110_DMA_SRC_CFG_READ, base + S5PC110_DMA_SRC_CFG);
  516. writel(S5PC110_DMA_DST_CFG_READ, base + S5PC110_DMA_DST_CFG);
  517. } else {
  518. writel(S5PC110_DMA_SRC_CFG_WRITE, base + S5PC110_DMA_SRC_CFG);
  519. writel(S5PC110_DMA_DST_CFG_WRITE, base + S5PC110_DMA_DST_CFG);
  520. }
  521. writel(count, base + S5PC110_DMA_TRANS_SIZE);
  522. writel(direction, base + S5PC110_DMA_TRANS_DIR);
  523. writel(S5PC110_DMA_TRANS_CMD_TR, base + S5PC110_DMA_TRANS_CMD);
  524. wait_for_completion_timeout(&onenand->complete, msecs_to_jiffies(20));
  525. return 0;
  526. }
  527. static int s5pc110_read_bufferram(struct mtd_info *mtd, int area,
  528. unsigned char *buffer, int offset, size_t count)
  529. {
  530. struct onenand_chip *this = mtd->priv;
  531. void __iomem *p;
  532. void *buf = (void *) buffer;
  533. dma_addr_t dma_src, dma_dst;
  534. int err, ofs, page_dma = 0;
  535. struct device *dev = &onenand->pdev->dev;
  536. p = this->base + area;
  537. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  538. if (area == ONENAND_DATARAM)
  539. p += this->writesize;
  540. else
  541. p += mtd->oobsize;
  542. }
  543. if (offset & 3 || (size_t) buf & 3 ||
  544. !onenand->dma_addr || count != mtd->writesize)
  545. goto normal;
  546. /* Handle vmalloc address */
  547. if (buf >= high_memory) {
  548. struct page *page;
  549. if (((size_t) buf & PAGE_MASK) !=
  550. ((size_t) (buf + count - 1) & PAGE_MASK))
  551. goto normal;
  552. page = vmalloc_to_page(buf);
  553. if (!page)
  554. goto normal;
  555. /* Page offset */
  556. ofs = ((size_t) buf & ~PAGE_MASK);
  557. page_dma = 1;
  558. /* DMA routine */
  559. dma_src = onenand->phys_base + (p - this->base);
  560. dma_dst = dma_map_page(dev, page, ofs, count, DMA_FROM_DEVICE);
  561. } else {
  562. /* DMA routine */
  563. dma_src = onenand->phys_base + (p - this->base);
  564. dma_dst = dma_map_single(dev, buf, count, DMA_FROM_DEVICE);
  565. }
  566. if (dma_mapping_error(dev, dma_dst)) {
  567. dev_err(dev, "Couldn't map a %d byte buffer for DMA\n", count);
  568. goto normal;
  569. }
  570. err = s5pc110_dma_ops((void *) dma_dst, (void *) dma_src,
  571. count, S5PC110_DMA_DIR_READ);
  572. if (page_dma)
  573. dma_unmap_page(dev, dma_dst, count, DMA_FROM_DEVICE);
  574. else
  575. dma_unmap_single(dev, dma_dst, count, DMA_FROM_DEVICE);
  576. if (!err)
  577. return 0;
  578. normal:
  579. if (count != mtd->writesize) {
  580. /* Copy the bufferram to memory to prevent unaligned access */
  581. memcpy(this->page_buf, p, mtd->writesize);
  582. p = this->page_buf + offset;
  583. }
  584. memcpy(buffer, p, count);
  585. return 0;
  586. }
  587. static int s5pc110_chip_probe(struct mtd_info *mtd)
  588. {
  589. /* Now just return 0 */
  590. return 0;
  591. }
  592. static int s3c_onenand_bbt_wait(struct mtd_info *mtd, int state)
  593. {
  594. unsigned int flags = INT_ACT | LOAD_CMP;
  595. unsigned int stat;
  596. unsigned long timeout;
  597. /* The 20 msec is enough */
  598. timeout = jiffies + msecs_to_jiffies(20);
  599. while (time_before(jiffies, timeout)) {
  600. stat = s3c_read_reg(INT_ERR_STAT_OFFSET);
  601. if (stat & flags)
  602. break;
  603. }
  604. /* To get correct interrupt status in timeout case */
  605. stat = s3c_read_reg(INT_ERR_STAT_OFFSET);
  606. s3c_write_reg(stat, INT_ERR_ACK_OFFSET);
  607. if (stat & LD_FAIL_ECC_ERR) {
  608. s3c_onenand_reset();
  609. return ONENAND_BBT_READ_ERROR;
  610. }
  611. if (stat & LOAD_CMP) {
  612. int ecc = s3c_read_reg(ECC_ERR_STAT_OFFSET);
  613. if (ecc & ONENAND_ECC_4BIT_UNCORRECTABLE) {
  614. s3c_onenand_reset();
  615. return ONENAND_BBT_READ_ERROR;
  616. }
  617. }
  618. return 0;
  619. }
  620. static void s3c_onenand_check_lock_status(struct mtd_info *mtd)
  621. {
  622. struct onenand_chip *this = mtd->priv;
  623. struct device *dev = &onenand->pdev->dev;
  624. unsigned int block, end;
  625. int tmp;
  626. end = this->chipsize >> this->erase_shift;
  627. for (block = 0; block < end; block++) {
  628. unsigned int mem_addr = onenand->mem_addr(block, 0, 0);
  629. tmp = s3c_read_cmd(CMD_MAP_01(onenand, mem_addr));
  630. if (s3c_read_reg(INT_ERR_STAT_OFFSET) & LOCKED_BLK) {
  631. dev_err(dev, "block %d is write-protected!\n", block);
  632. s3c_write_reg(LOCKED_BLK, INT_ERR_ACK_OFFSET);
  633. }
  634. }
  635. }
  636. static void s3c_onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs,
  637. size_t len, int cmd)
  638. {
  639. struct onenand_chip *this = mtd->priv;
  640. int start, end, start_mem_addr, end_mem_addr;
  641. start = ofs >> this->erase_shift;
  642. start_mem_addr = onenand->mem_addr(start, 0, 0);
  643. end = start + (len >> this->erase_shift) - 1;
  644. end_mem_addr = onenand->mem_addr(end, 0, 0);
  645. if (cmd == ONENAND_CMD_LOCK) {
  646. s3c_write_cmd(ONENAND_LOCK_START, CMD_MAP_10(onenand,
  647. start_mem_addr));
  648. s3c_write_cmd(ONENAND_LOCK_END, CMD_MAP_10(onenand,
  649. end_mem_addr));
  650. } else {
  651. s3c_write_cmd(ONENAND_UNLOCK_START, CMD_MAP_10(onenand,
  652. start_mem_addr));
  653. s3c_write_cmd(ONENAND_UNLOCK_END, CMD_MAP_10(onenand,
  654. end_mem_addr));
  655. }
  656. this->wait(mtd, FL_LOCKING);
  657. }
  658. static void s3c_unlock_all(struct mtd_info *mtd)
  659. {
  660. struct onenand_chip *this = mtd->priv;
  661. loff_t ofs = 0;
  662. size_t len = this->chipsize;
  663. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  664. /* Write unlock command */
  665. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  666. /* No need to check return value */
  667. this->wait(mtd, FL_LOCKING);
  668. /* Workaround for all block unlock in DDP */
  669. if (!ONENAND_IS_DDP(this)) {
  670. s3c_onenand_check_lock_status(mtd);
  671. return;
  672. }
  673. /* All blocks on another chip */
  674. ofs = this->chipsize >> 1;
  675. len = this->chipsize >> 1;
  676. }
  677. s3c_onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  678. s3c_onenand_check_lock_status(mtd);
  679. }
  680. static void s3c_onenand_setup(struct mtd_info *mtd)
  681. {
  682. struct onenand_chip *this = mtd->priv;
  683. onenand->mtd = mtd;
  684. if (onenand->type == TYPE_S3C6400) {
  685. onenand->mem_addr = s3c6400_mem_addr;
  686. onenand->cmd_map = s3c64xx_cmd_map;
  687. } else if (onenand->type == TYPE_S3C6410) {
  688. onenand->mem_addr = s3c6410_mem_addr;
  689. onenand->cmd_map = s3c64xx_cmd_map;
  690. } else if (onenand->type == TYPE_S5PC100) {
  691. onenand->mem_addr = s5pc100_mem_addr;
  692. onenand->cmd_map = s5pc1xx_cmd_map;
  693. } else if (onenand->type == TYPE_S5PC110) {
  694. /* Use generic onenand functions */
  695. this->read_bufferram = s5pc110_read_bufferram;
  696. this->chip_probe = s5pc110_chip_probe;
  697. return;
  698. } else {
  699. BUG();
  700. }
  701. this->read_word = s3c_onenand_readw;
  702. this->write_word = s3c_onenand_writew;
  703. this->wait = s3c_onenand_wait;
  704. this->bbt_wait = s3c_onenand_bbt_wait;
  705. this->unlock_all = s3c_unlock_all;
  706. this->command = s3c_onenand_command;
  707. this->read_bufferram = onenand_read_bufferram;
  708. this->write_bufferram = onenand_write_bufferram;
  709. }
  710. static int s3c_onenand_probe(struct platform_device *pdev)
  711. {
  712. struct onenand_platform_data *pdata;
  713. struct onenand_chip *this;
  714. struct mtd_info *mtd;
  715. struct resource *r;
  716. int size, err;
  717. pdata = pdev->dev.platform_data;
  718. /* No need to check pdata. the platform data is optional */
  719. size = sizeof(struct mtd_info) + sizeof(struct onenand_chip);
  720. mtd = kzalloc(size, GFP_KERNEL);
  721. if (!mtd) {
  722. dev_err(&pdev->dev, "failed to allocate memory\n");
  723. return -ENOMEM;
  724. }
  725. onenand = kzalloc(sizeof(struct s3c_onenand), GFP_KERNEL);
  726. if (!onenand) {
  727. err = -ENOMEM;
  728. goto onenand_fail;
  729. }
  730. this = (struct onenand_chip *) &mtd[1];
  731. mtd->priv = this;
  732. mtd->dev.parent = &pdev->dev;
  733. mtd->owner = THIS_MODULE;
  734. onenand->pdev = pdev;
  735. onenand->type = platform_get_device_id(pdev)->driver_data;
  736. s3c_onenand_setup(mtd);
  737. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  738. if (!r) {
  739. dev_err(&pdev->dev, "no memory resource defined\n");
  740. return -ENOENT;
  741. goto ahb_resource_failed;
  742. }
  743. onenand->base_res = request_mem_region(r->start, resource_size(r),
  744. pdev->name);
  745. if (!onenand->base_res) {
  746. dev_err(&pdev->dev, "failed to request memory resource\n");
  747. err = -EBUSY;
  748. goto resource_failed;
  749. }
  750. onenand->base = ioremap(r->start, resource_size(r));
  751. if (!onenand->base) {
  752. dev_err(&pdev->dev, "failed to map memory resource\n");
  753. err = -EFAULT;
  754. goto ioremap_failed;
  755. }
  756. /* Set onenand_chip also */
  757. this->base = onenand->base;
  758. /* Use runtime badblock check */
  759. this->options |= ONENAND_SKIP_UNLOCK_CHECK;
  760. if (onenand->type != TYPE_S5PC110) {
  761. r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  762. if (!r) {
  763. dev_err(&pdev->dev, "no buffer memory resource defined\n");
  764. err = -ENOENT;
  765. goto ahb_resource_failed;
  766. }
  767. onenand->ahb_res = request_mem_region(r->start, resource_size(r),
  768. pdev->name);
  769. if (!onenand->ahb_res) {
  770. dev_err(&pdev->dev, "failed to request buffer memory resource\n");
  771. err = -EBUSY;
  772. goto ahb_resource_failed;
  773. }
  774. onenand->ahb_addr = ioremap(r->start, resource_size(r));
  775. if (!onenand->ahb_addr) {
  776. dev_err(&pdev->dev, "failed to map buffer memory resource\n");
  777. err = -EINVAL;
  778. goto ahb_ioremap_failed;
  779. }
  780. /* Allocate 4KiB BufferRAM */
  781. onenand->page_buf = kzalloc(SZ_4K, GFP_KERNEL);
  782. if (!onenand->page_buf) {
  783. err = -ENOMEM;
  784. goto page_buf_fail;
  785. }
  786. /* Allocate 128 SpareRAM */
  787. onenand->oob_buf = kzalloc(128, GFP_KERNEL);
  788. if (!onenand->oob_buf) {
  789. err = -ENOMEM;
  790. goto oob_buf_fail;
  791. }
  792. /* S3C doesn't handle subpage write */
  793. mtd->subpage_sft = 0;
  794. this->subpagesize = mtd->writesize;
  795. } else { /* S5PC110 */
  796. r = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  797. if (!r) {
  798. dev_err(&pdev->dev, "no dma memory resource defined\n");
  799. err = -ENOENT;
  800. goto dma_resource_failed;
  801. }
  802. onenand->dma_res = request_mem_region(r->start, resource_size(r),
  803. pdev->name);
  804. if (!onenand->dma_res) {
  805. dev_err(&pdev->dev, "failed to request dma memory resource\n");
  806. err = -EBUSY;
  807. goto dma_resource_failed;
  808. }
  809. onenand->dma_addr = ioremap(r->start, resource_size(r));
  810. if (!onenand->dma_addr) {
  811. dev_err(&pdev->dev, "failed to map dma memory resource\n");
  812. err = -EINVAL;
  813. goto dma_ioremap_failed;
  814. }
  815. onenand->phys_base = onenand->base_res->start;
  816. s5pc110_dma_ops = s5pc110_dma_poll;
  817. /* Interrupt support */
  818. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  819. if (r) {
  820. init_completion(&onenand->complete);
  821. s5pc110_dma_ops = s5pc110_dma_irq;
  822. err = request_irq(r->start, s5pc110_onenand_irq,
  823. IRQF_SHARED, "onenand", &onenand);
  824. if (err) {
  825. dev_err(&pdev->dev, "failed to get irq\n");
  826. goto scan_failed;
  827. }
  828. }
  829. }
  830. if (onenand_scan(mtd, 1)) {
  831. err = -EFAULT;
  832. goto scan_failed;
  833. }
  834. if (onenand->type != TYPE_S5PC110) {
  835. /* S3C doesn't handle subpage write */
  836. mtd->subpage_sft = 0;
  837. this->subpagesize = mtd->writesize;
  838. }
  839. if (s3c_read_reg(MEM_CFG_OFFSET) & ONENAND_SYS_CFG1_SYNC_READ)
  840. dev_info(&onenand->pdev->dev, "OneNAND Sync. Burst Read enabled\n");
  841. err = mtd_device_parse_register(mtd, NULL, NULL,
  842. pdata ? pdata->parts : NULL,
  843. pdata ? pdata->nr_parts : 0);
  844. platform_set_drvdata(pdev, mtd);
  845. return 0;
  846. scan_failed:
  847. if (onenand->dma_addr)
  848. iounmap(onenand->dma_addr);
  849. dma_ioremap_failed:
  850. if (onenand->dma_res)
  851. release_mem_region(onenand->dma_res->start,
  852. resource_size(onenand->dma_res));
  853. kfree(onenand->oob_buf);
  854. oob_buf_fail:
  855. kfree(onenand->page_buf);
  856. page_buf_fail:
  857. if (onenand->ahb_addr)
  858. iounmap(onenand->ahb_addr);
  859. ahb_ioremap_failed:
  860. if (onenand->ahb_res)
  861. release_mem_region(onenand->ahb_res->start,
  862. resource_size(onenand->ahb_res));
  863. dma_resource_failed:
  864. ahb_resource_failed:
  865. iounmap(onenand->base);
  866. ioremap_failed:
  867. if (onenand->base_res)
  868. release_mem_region(onenand->base_res->start,
  869. resource_size(onenand->base_res));
  870. resource_failed:
  871. kfree(onenand);
  872. onenand_fail:
  873. kfree(mtd);
  874. return err;
  875. }
  876. static int __devexit s3c_onenand_remove(struct platform_device *pdev)
  877. {
  878. struct mtd_info *mtd = platform_get_drvdata(pdev);
  879. onenand_release(mtd);
  880. if (onenand->ahb_addr)
  881. iounmap(onenand->ahb_addr);
  882. if (onenand->ahb_res)
  883. release_mem_region(onenand->ahb_res->start,
  884. resource_size(onenand->ahb_res));
  885. if (onenand->dma_addr)
  886. iounmap(onenand->dma_addr);
  887. if (onenand->dma_res)
  888. release_mem_region(onenand->dma_res->start,
  889. resource_size(onenand->dma_res));
  890. iounmap(onenand->base);
  891. release_mem_region(onenand->base_res->start,
  892. resource_size(onenand->base_res));
  893. platform_set_drvdata(pdev, NULL);
  894. kfree(onenand->oob_buf);
  895. kfree(onenand->page_buf);
  896. kfree(onenand);
  897. kfree(mtd);
  898. return 0;
  899. }
  900. static int s3c_pm_ops_suspend(struct device *dev)
  901. {
  902. struct platform_device *pdev = to_platform_device(dev);
  903. struct mtd_info *mtd = platform_get_drvdata(pdev);
  904. struct onenand_chip *this = mtd->priv;
  905. this->wait(mtd, FL_PM_SUSPENDED);
  906. return 0;
  907. }
  908. static int s3c_pm_ops_resume(struct device *dev)
  909. {
  910. struct platform_device *pdev = to_platform_device(dev);
  911. struct mtd_info *mtd = platform_get_drvdata(pdev);
  912. struct onenand_chip *this = mtd->priv;
  913. this->unlock_all(mtd);
  914. return 0;
  915. }
  916. static const struct dev_pm_ops s3c_pm_ops = {
  917. .suspend = s3c_pm_ops_suspend,
  918. .resume = s3c_pm_ops_resume,
  919. };
  920. static struct platform_device_id s3c_onenand_driver_ids[] = {
  921. {
  922. .name = "s3c6400-onenand",
  923. .driver_data = TYPE_S3C6400,
  924. }, {
  925. .name = "s3c6410-onenand",
  926. .driver_data = TYPE_S3C6410,
  927. }, {
  928. .name = "s5pc100-onenand",
  929. .driver_data = TYPE_S5PC100,
  930. }, {
  931. .name = "s5pc110-onenand",
  932. .driver_data = TYPE_S5PC110,
  933. }, { },
  934. };
  935. MODULE_DEVICE_TABLE(platform, s3c_onenand_driver_ids);
  936. static struct platform_driver s3c_onenand_driver = {
  937. .driver = {
  938. .name = "samsung-onenand",
  939. .pm = &s3c_pm_ops,
  940. },
  941. .id_table = s3c_onenand_driver_ids,
  942. .probe = s3c_onenand_probe,
  943. .remove = __devexit_p(s3c_onenand_remove),
  944. };
  945. module_platform_driver(s3c_onenand_driver);
  946. MODULE_LICENSE("GPL");
  947. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  948. MODULE_DESCRIPTION("Samsung OneNAND controller support");