nuc900_nand.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright © 2009 Nuvoton technology corporation.
  3. *
  4. * Wan ZongShun <mcuos.com@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation;version 2 of the License.
  9. *
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/delay.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/nand.h>
  21. #include <linux/mtd/partitions.h>
  22. #define REG_FMICSR 0x00
  23. #define REG_SMCSR 0xa0
  24. #define REG_SMISR 0xac
  25. #define REG_SMCMD 0xb0
  26. #define REG_SMADDR 0xb4
  27. #define REG_SMDATA 0xb8
  28. #define RESET_FMI 0x01
  29. #define NAND_EN 0x08
  30. #define READYBUSY (0x01 << 18)
  31. #define SWRST 0x01
  32. #define PSIZE (0x01 << 3)
  33. #define DMARWEN (0x03 << 1)
  34. #define BUSWID (0x01 << 4)
  35. #define ECC4EN (0x01 << 5)
  36. #define WP (0x01 << 24)
  37. #define NANDCS (0x01 << 25)
  38. #define ENDADDR (0x01 << 31)
  39. #define read_data_reg(dev) \
  40. __raw_readl((dev)->reg + REG_SMDATA)
  41. #define write_data_reg(dev, val) \
  42. __raw_writel((val), (dev)->reg + REG_SMDATA)
  43. #define write_cmd_reg(dev, val) \
  44. __raw_writel((val), (dev)->reg + REG_SMCMD)
  45. #define write_addr_reg(dev, val) \
  46. __raw_writel((val), (dev)->reg + REG_SMADDR)
  47. struct nuc900_nand {
  48. struct nand_chip chip;
  49. void __iomem *reg;
  50. struct clk *clk;
  51. spinlock_t lock;
  52. };
  53. static inline struct nuc900_nand *mtd_to_nuc900(struct mtd_info *mtd)
  54. {
  55. return container_of(mtd_to_nand(mtd), struct nuc900_nand, chip);
  56. }
  57. static const struct mtd_partition partitions[] = {
  58. {
  59. .name = "NAND FS 0",
  60. .offset = 0,
  61. .size = 8 * 1024 * 1024
  62. },
  63. {
  64. .name = "NAND FS 1",
  65. .offset = MTDPART_OFS_APPEND,
  66. .size = MTDPART_SIZ_FULL
  67. }
  68. };
  69. static unsigned char nuc900_nand_read_byte(struct mtd_info *mtd)
  70. {
  71. unsigned char ret;
  72. struct nuc900_nand *nand = mtd_to_nuc900(mtd);
  73. ret = (unsigned char)read_data_reg(nand);
  74. return ret;
  75. }
  76. static void nuc900_nand_read_buf(struct mtd_info *mtd,
  77. unsigned char *buf, int len)
  78. {
  79. int i;
  80. struct nuc900_nand *nand = mtd_to_nuc900(mtd);
  81. for (i = 0; i < len; i++)
  82. buf[i] = (unsigned char)read_data_reg(nand);
  83. }
  84. static void nuc900_nand_write_buf(struct mtd_info *mtd,
  85. const unsigned char *buf, int len)
  86. {
  87. int i;
  88. struct nuc900_nand *nand = mtd_to_nuc900(mtd);
  89. for (i = 0; i < len; i++)
  90. write_data_reg(nand, buf[i]);
  91. }
  92. static int nuc900_check_rb(struct nuc900_nand *nand)
  93. {
  94. unsigned int val;
  95. spin_lock(&nand->lock);
  96. val = __raw_readl(nand->reg + REG_SMISR);
  97. val &= READYBUSY;
  98. spin_unlock(&nand->lock);
  99. return val;
  100. }
  101. static int nuc900_nand_devready(struct mtd_info *mtd)
  102. {
  103. struct nuc900_nand *nand = mtd_to_nuc900(mtd);
  104. int ready;
  105. ready = (nuc900_check_rb(nand)) ? 1 : 0;
  106. return ready;
  107. }
  108. static void nuc900_nand_command_lp(struct mtd_info *mtd, unsigned int command,
  109. int column, int page_addr)
  110. {
  111. register struct nand_chip *chip = mtd_to_nand(mtd);
  112. struct nuc900_nand *nand = mtd_to_nuc900(mtd);
  113. if (command == NAND_CMD_READOOB) {
  114. column += mtd->writesize;
  115. command = NAND_CMD_READ0;
  116. }
  117. write_cmd_reg(nand, command & 0xff);
  118. if (column != -1 || page_addr != -1) {
  119. if (column != -1) {
  120. if (chip->options & NAND_BUSWIDTH_16 &&
  121. !nand_opcode_8bits(command))
  122. column >>= 1;
  123. write_addr_reg(nand, column);
  124. write_addr_reg(nand, column >> 8 | ENDADDR);
  125. }
  126. if (page_addr != -1) {
  127. write_addr_reg(nand, page_addr);
  128. if (chip->chipsize > (128 << 20)) {
  129. write_addr_reg(nand, page_addr >> 8);
  130. write_addr_reg(nand, page_addr >> 16 | ENDADDR);
  131. } else {
  132. write_addr_reg(nand, page_addr >> 8 | ENDADDR);
  133. }
  134. }
  135. }
  136. switch (command) {
  137. case NAND_CMD_CACHEDPROG:
  138. case NAND_CMD_PAGEPROG:
  139. case NAND_CMD_ERASE1:
  140. case NAND_CMD_ERASE2:
  141. case NAND_CMD_SEQIN:
  142. case NAND_CMD_RNDIN:
  143. case NAND_CMD_STATUS:
  144. return;
  145. case NAND_CMD_RESET:
  146. if (chip->dev_ready)
  147. break;
  148. udelay(chip->chip_delay);
  149. write_cmd_reg(nand, NAND_CMD_STATUS);
  150. write_cmd_reg(nand, command);
  151. while (!nuc900_check_rb(nand))
  152. ;
  153. return;
  154. case NAND_CMD_RNDOUT:
  155. write_cmd_reg(nand, NAND_CMD_RNDOUTSTART);
  156. return;
  157. case NAND_CMD_READ0:
  158. write_cmd_reg(nand, NAND_CMD_READSTART);
  159. default:
  160. if (!chip->dev_ready) {
  161. udelay(chip->chip_delay);
  162. return;
  163. }
  164. }
  165. /* Apply this short delay always to ensure that we do wait tWB in
  166. * any case on any machine. */
  167. ndelay(100);
  168. while (!chip->dev_ready(mtd))
  169. ;
  170. }
  171. static void nuc900_nand_enable(struct nuc900_nand *nand)
  172. {
  173. unsigned int val;
  174. spin_lock(&nand->lock);
  175. __raw_writel(RESET_FMI, (nand->reg + REG_FMICSR));
  176. val = __raw_readl(nand->reg + REG_FMICSR);
  177. if (!(val & NAND_EN))
  178. __raw_writel(val | NAND_EN, nand->reg + REG_FMICSR);
  179. val = __raw_readl(nand->reg + REG_SMCSR);
  180. val &= ~(SWRST|PSIZE|DMARWEN|BUSWID|ECC4EN|NANDCS);
  181. val |= WP;
  182. __raw_writel(val, nand->reg + REG_SMCSR);
  183. spin_unlock(&nand->lock);
  184. }
  185. static int nuc900_nand_probe(struct platform_device *pdev)
  186. {
  187. struct nuc900_nand *nuc900_nand;
  188. struct nand_chip *chip;
  189. struct mtd_info *mtd;
  190. struct resource *res;
  191. nuc900_nand = devm_kzalloc(&pdev->dev, sizeof(struct nuc900_nand),
  192. GFP_KERNEL);
  193. if (!nuc900_nand)
  194. return -ENOMEM;
  195. chip = &(nuc900_nand->chip);
  196. mtd = nand_to_mtd(chip);
  197. mtd->dev.parent = &pdev->dev;
  198. spin_lock_init(&nuc900_nand->lock);
  199. nuc900_nand->clk = devm_clk_get(&pdev->dev, NULL);
  200. if (IS_ERR(nuc900_nand->clk))
  201. return -ENOENT;
  202. clk_enable(nuc900_nand->clk);
  203. chip->cmdfunc = nuc900_nand_command_lp;
  204. chip->dev_ready = nuc900_nand_devready;
  205. chip->read_byte = nuc900_nand_read_byte;
  206. chip->write_buf = nuc900_nand_write_buf;
  207. chip->read_buf = nuc900_nand_read_buf;
  208. chip->chip_delay = 50;
  209. chip->options = 0;
  210. chip->ecc.mode = NAND_ECC_SOFT;
  211. chip->ecc.algo = NAND_ECC_HAMMING;
  212. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  213. nuc900_nand->reg = devm_ioremap_resource(&pdev->dev, res);
  214. if (IS_ERR(nuc900_nand->reg))
  215. return PTR_ERR(nuc900_nand->reg);
  216. nuc900_nand_enable(nuc900_nand);
  217. if (nand_scan(mtd, 1))
  218. return -ENXIO;
  219. mtd_device_register(mtd, partitions, ARRAY_SIZE(partitions));
  220. platform_set_drvdata(pdev, nuc900_nand);
  221. return 0;
  222. }
  223. static int nuc900_nand_remove(struct platform_device *pdev)
  224. {
  225. struct nuc900_nand *nuc900_nand = platform_get_drvdata(pdev);
  226. nand_release(nand_to_mtd(&nuc900_nand->chip));
  227. clk_disable(nuc900_nand->clk);
  228. return 0;
  229. }
  230. static struct platform_driver nuc900_nand_driver = {
  231. .probe = nuc900_nand_probe,
  232. .remove = nuc900_nand_remove,
  233. .driver = {
  234. .name = "nuc900-fmi",
  235. },
  236. };
  237. module_platform_driver(nuc900_nand_driver);
  238. MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
  239. MODULE_DESCRIPTION("w90p910/NUC9xx nand driver!");
  240. MODULE_LICENSE("GPL");
  241. MODULE_ALIAS("platform:nuc900-fmi");