samsung.c 25 KB

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