nuc900_nand.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 mtd_info mtd;
  49. struct nand_chip chip;
  50. void __iomem *reg;
  51. struct clk *clk;
  52. spinlock_t lock;
  53. };
  54. static const struct mtd_partition partitions[] = {
  55. {
  56. .name = "NAND FS 0",
  57. .offset = 0,
  58. .size = 8 * 1024 * 1024
  59. },
  60. {
  61. .name = "NAND FS 1",
  62. .offset = MTDPART_OFS_APPEND,
  63. .size = MTDPART_SIZ_FULL
  64. }
  65. };
  66. static unsigned char nuc900_nand_read_byte(struct mtd_info *mtd)
  67. {
  68. unsigned char ret;
  69. struct nuc900_nand *nand;
  70. nand = container_of(mtd, struct nuc900_nand, mtd);
  71. ret = (unsigned char)read_data_reg(nand);
  72. return ret;
  73. }
  74. static void nuc900_nand_read_buf(struct mtd_info *mtd,
  75. unsigned char *buf, int len)
  76. {
  77. int i;
  78. struct nuc900_nand *nand;
  79. nand = container_of(mtd, struct nuc900_nand, mtd);
  80. for (i = 0; i < len; i++)
  81. buf[i] = (unsigned char)read_data_reg(nand);
  82. }
  83. static void nuc900_nand_write_buf(struct mtd_info *mtd,
  84. const unsigned char *buf, int len)
  85. {
  86. int i;
  87. struct nuc900_nand *nand;
  88. nand = container_of(mtd, struct nuc900_nand, 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(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;
  104. int ready;
  105. nand = container_of(mtd, struct nuc900_nand, mtd);
  106. ready = (nuc900_check_rb(nand)) ? 1 : 0;
  107. return ready;
  108. }
  109. static void nuc900_nand_command_lp(struct mtd_info *mtd, unsigned int command,
  110. int column, int page_addr)
  111. {
  112. register struct nand_chip *chip = mtd->priv;
  113. struct nuc900_nand *nand;
  114. nand = container_of(mtd, struct nuc900_nand, mtd);
  115. if (command == NAND_CMD_READOOB) {
  116. column += mtd->writesize;
  117. command = NAND_CMD_READ0;
  118. }
  119. write_cmd_reg(nand, command & 0xff);
  120. if (column != -1 || page_addr != -1) {
  121. if (column != -1) {
  122. if (chip->options & NAND_BUSWIDTH_16 &&
  123. !nand_opcode_8bits(command))
  124. column >>= 1;
  125. write_addr_reg(nand, column);
  126. write_addr_reg(nand, column >> 8 | ENDADDR);
  127. }
  128. if (page_addr != -1) {
  129. write_addr_reg(nand, page_addr);
  130. if (chip->chipsize > (128 << 20)) {
  131. write_addr_reg(nand, page_addr >> 8);
  132. write_addr_reg(nand, page_addr >> 16 | ENDADDR);
  133. } else {
  134. write_addr_reg(nand, page_addr >> 8 | ENDADDR);
  135. }
  136. }
  137. }
  138. switch (command) {
  139. case NAND_CMD_CACHEDPROG:
  140. case NAND_CMD_PAGEPROG:
  141. case NAND_CMD_ERASE1:
  142. case NAND_CMD_ERASE2:
  143. case NAND_CMD_SEQIN:
  144. case NAND_CMD_RNDIN:
  145. case NAND_CMD_STATUS:
  146. return;
  147. case NAND_CMD_RESET:
  148. if (chip->dev_ready)
  149. break;
  150. udelay(chip->chip_delay);
  151. write_cmd_reg(nand, NAND_CMD_STATUS);
  152. write_cmd_reg(nand, command);
  153. while (!nuc900_check_rb(nand))
  154. ;
  155. return;
  156. case NAND_CMD_RNDOUT:
  157. write_cmd_reg(nand, NAND_CMD_RNDOUTSTART);
  158. return;
  159. case NAND_CMD_READ0:
  160. write_cmd_reg(nand, NAND_CMD_READSTART);
  161. default:
  162. if (!chip->dev_ready) {
  163. udelay(chip->chip_delay);
  164. return;
  165. }
  166. }
  167. /* Apply this short delay always to ensure that we do wait tWB in
  168. * any case on any machine. */
  169. ndelay(100);
  170. while (!chip->dev_ready(mtd))
  171. ;
  172. }
  173. static void nuc900_nand_enable(struct nuc900_nand *nand)
  174. {
  175. unsigned int val;
  176. spin_lock(&nand->lock);
  177. __raw_writel(RESET_FMI, (nand->reg + REG_FMICSR));
  178. val = __raw_readl(nand->reg + REG_FMICSR);
  179. if (!(val & NAND_EN))
  180. __raw_writel(val | NAND_EN, nand->reg + REG_FMICSR);
  181. val = __raw_readl(nand->reg + REG_SMCSR);
  182. val &= ~(SWRST|PSIZE|DMARWEN|BUSWID|ECC4EN|NANDCS);
  183. val |= WP;
  184. __raw_writel(val, nand->reg + REG_SMCSR);
  185. spin_unlock(&nand->lock);
  186. }
  187. static int nuc900_nand_probe(struct platform_device *pdev)
  188. {
  189. struct nuc900_nand *nuc900_nand;
  190. struct nand_chip *chip;
  191. struct resource *res;
  192. nuc900_nand = devm_kzalloc(&pdev->dev, sizeof(struct nuc900_nand),
  193. GFP_KERNEL);
  194. if (!nuc900_nand)
  195. return -ENOMEM;
  196. chip = &(nuc900_nand->chip);
  197. nuc900_nand->mtd.priv = chip;
  198. nuc900_nand->mtd.dev.parent = &pdev->dev;
  199. spin_lock_init(&nuc900_nand->lock);
  200. nuc900_nand->clk = devm_clk_get(&pdev->dev, NULL);
  201. if (IS_ERR(nuc900_nand->clk))
  202. return -ENOENT;
  203. clk_enable(nuc900_nand->clk);
  204. chip->cmdfunc = nuc900_nand_command_lp;
  205. chip->dev_ready = nuc900_nand_devready;
  206. chip->read_byte = nuc900_nand_read_byte;
  207. chip->write_buf = nuc900_nand_write_buf;
  208. chip->read_buf = nuc900_nand_read_buf;
  209. chip->chip_delay = 50;
  210. chip->options = 0;
  211. chip->ecc.mode = NAND_ECC_SOFT;
  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(&(nuc900_nand->mtd), 1))
  218. return -ENXIO;
  219. mtd_device_register(&(nuc900_nand->mtd), partitions,
  220. ARRAY_SIZE(partitions));
  221. platform_set_drvdata(pdev, nuc900_nand);
  222. return 0;
  223. }
  224. static int nuc900_nand_remove(struct platform_device *pdev)
  225. {
  226. struct nuc900_nand *nuc900_nand = platform_get_drvdata(pdev);
  227. nand_release(&nuc900_nand->mtd);
  228. clk_disable(nuc900_nand->clk);
  229. return 0;
  230. }
  231. static struct platform_driver nuc900_nand_driver = {
  232. .probe = nuc900_nand_probe,
  233. .remove = nuc900_nand_remove,
  234. .driver = {
  235. .name = "nuc900-fmi",
  236. },
  237. };
  238. module_platform_driver(nuc900_nand_driver);
  239. MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
  240. MODULE_DESCRIPTION("w90p910/NUC9xx nand driver!");
  241. MODULE_LICENSE("GPL");
  242. MODULE_ALIAS("platform:nuc900-fmi");