samsung.c 28 KB

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