socrates_nand.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * drivers/mtd/nand/socrates_nand.c
  3. *
  4. * Copyright © 2008 Ilya Yanok, Emcraft Systems
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/nand.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/io.h>
  19. #define FPGA_NAND_CMD_MASK (0x7 << 28)
  20. #define FPGA_NAND_CMD_COMMAND (0x0 << 28)
  21. #define FPGA_NAND_CMD_ADDR (0x1 << 28)
  22. #define FPGA_NAND_CMD_READ (0x2 << 28)
  23. #define FPGA_NAND_CMD_WRITE (0x3 << 28)
  24. #define FPGA_NAND_BUSY (0x1 << 15)
  25. #define FPGA_NAND_ENABLE (0x1 << 31)
  26. #define FPGA_NAND_DATA_SHIFT 16
  27. struct socrates_nand_host {
  28. struct nand_chip nand_chip;
  29. struct mtd_info mtd;
  30. void __iomem *io_base;
  31. struct device *dev;
  32. };
  33. /**
  34. * socrates_nand_write_buf - write buffer to chip
  35. * @mtd: MTD device structure
  36. * @buf: data buffer
  37. * @len: number of bytes to write
  38. */
  39. static void socrates_nand_write_buf(struct mtd_info *mtd,
  40. const uint8_t *buf, int len)
  41. {
  42. int i;
  43. struct nand_chip *this = mtd->priv;
  44. struct socrates_nand_host *host = this->priv;
  45. for (i = 0; i < len; i++) {
  46. out_be32(host->io_base, FPGA_NAND_ENABLE |
  47. FPGA_NAND_CMD_WRITE |
  48. (buf[i] << FPGA_NAND_DATA_SHIFT));
  49. }
  50. }
  51. /**
  52. * socrates_nand_read_buf - read chip data into buffer
  53. * @mtd: MTD device structure
  54. * @buf: buffer to store date
  55. * @len: number of bytes to read
  56. */
  57. static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  58. {
  59. int i;
  60. struct nand_chip *this = mtd->priv;
  61. struct socrates_nand_host *host = this->priv;
  62. uint32_t val;
  63. val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ;
  64. out_be32(host->io_base, val);
  65. for (i = 0; i < len; i++) {
  66. buf[i] = (in_be32(host->io_base) >>
  67. FPGA_NAND_DATA_SHIFT) & 0xff;
  68. }
  69. }
  70. /**
  71. * socrates_nand_read_byte - read one byte from the chip
  72. * @mtd: MTD device structure
  73. */
  74. static uint8_t socrates_nand_read_byte(struct mtd_info *mtd)
  75. {
  76. uint8_t byte;
  77. socrates_nand_read_buf(mtd, &byte, sizeof(byte));
  78. return byte;
  79. }
  80. /**
  81. * socrates_nand_read_word - read one word from the chip
  82. * @mtd: MTD device structure
  83. */
  84. static uint16_t socrates_nand_read_word(struct mtd_info *mtd)
  85. {
  86. uint16_t word;
  87. socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word));
  88. return word;
  89. }
  90. /**
  91. * socrates_nand_verify_buf - Verify chip data against buffer
  92. * @mtd: MTD device structure
  93. * @buf: buffer containing the data to compare
  94. * @len: number of bytes to compare
  95. */
  96. static int socrates_nand_verify_buf(struct mtd_info *mtd, const u8 *buf,
  97. int len)
  98. {
  99. int i;
  100. for (i = 0; i < len; i++) {
  101. if (buf[i] != socrates_nand_read_byte(mtd))
  102. return -EFAULT;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * Hardware specific access to control-lines
  108. */
  109. static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  110. unsigned int ctrl)
  111. {
  112. struct nand_chip *nand_chip = mtd->priv;
  113. struct socrates_nand_host *host = nand_chip->priv;
  114. uint32_t val;
  115. if (cmd == NAND_CMD_NONE)
  116. return;
  117. if (ctrl & NAND_CLE)
  118. val = FPGA_NAND_CMD_COMMAND;
  119. else
  120. val = FPGA_NAND_CMD_ADDR;
  121. if (ctrl & NAND_NCE)
  122. val |= FPGA_NAND_ENABLE;
  123. val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
  124. out_be32(host->io_base, val);
  125. }
  126. /*
  127. * Read the Device Ready pin.
  128. */
  129. static int socrates_nand_device_ready(struct mtd_info *mtd)
  130. {
  131. struct nand_chip *nand_chip = mtd->priv;
  132. struct socrates_nand_host *host = nand_chip->priv;
  133. if (in_be32(host->io_base) & FPGA_NAND_BUSY)
  134. return 0; /* busy */
  135. return 1;
  136. }
  137. static const char *part_probes[] = { "cmdlinepart", NULL };
  138. /*
  139. * Probe for the NAND device.
  140. */
  141. static int __devinit socrates_nand_probe(struct platform_device *ofdev)
  142. {
  143. struct socrates_nand_host *host;
  144. struct mtd_info *mtd;
  145. struct nand_chip *nand_chip;
  146. int res;
  147. struct mtd_partition *partitions = NULL;
  148. int num_partitions = 0;
  149. /* Allocate memory for the device structure (and zero it) */
  150. host = kzalloc(sizeof(struct socrates_nand_host), GFP_KERNEL);
  151. if (!host) {
  152. printk(KERN_ERR
  153. "socrates_nand: failed to allocate device structure.\n");
  154. return -ENOMEM;
  155. }
  156. host->io_base = of_iomap(ofdev->dev.of_node, 0);
  157. if (host->io_base == NULL) {
  158. printk(KERN_ERR "socrates_nand: ioremap failed\n");
  159. kfree(host);
  160. return -EIO;
  161. }
  162. mtd = &host->mtd;
  163. nand_chip = &host->nand_chip;
  164. host->dev = &ofdev->dev;
  165. nand_chip->priv = host; /* link the private data structures */
  166. mtd->priv = nand_chip;
  167. mtd->name = "socrates_nand";
  168. mtd->owner = THIS_MODULE;
  169. mtd->dev.parent = &ofdev->dev;
  170. /*should never be accessed directly */
  171. nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
  172. nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
  173. nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
  174. nand_chip->read_byte = socrates_nand_read_byte;
  175. nand_chip->read_word = socrates_nand_read_word;
  176. nand_chip->write_buf = socrates_nand_write_buf;
  177. nand_chip->read_buf = socrates_nand_read_buf;
  178. nand_chip->verify_buf = socrates_nand_verify_buf;
  179. nand_chip->dev_ready = socrates_nand_device_ready;
  180. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  181. /* TODO: I have no idea what real delay is. */
  182. nand_chip->chip_delay = 20; /* 20us command delay time */
  183. dev_set_drvdata(&ofdev->dev, host);
  184. /* first scan to find the device and get the page size */
  185. if (nand_scan_ident(mtd, 1, NULL)) {
  186. res = -ENXIO;
  187. goto out;
  188. }
  189. /* second phase scan */
  190. if (nand_scan_tail(mtd)) {
  191. res = -ENXIO;
  192. goto out;
  193. }
  194. #ifdef CONFIG_MTD_CMDLINE_PARTS
  195. num_partitions = parse_mtd_partitions(mtd, part_probes,
  196. &partitions, 0);
  197. if (num_partitions < 0) {
  198. res = num_partitions;
  199. goto release;
  200. }
  201. #endif
  202. if (num_partitions == 0) {
  203. num_partitions = of_mtd_parse_partitions(&ofdev->dev,
  204. ofdev->dev.of_node,
  205. &partitions);
  206. if (num_partitions < 0) {
  207. res = num_partitions;
  208. goto release;
  209. }
  210. }
  211. res = mtd_device_register(mtd, partitions, num_partitions);
  212. if (!res)
  213. return res;
  214. release:
  215. nand_release(mtd);
  216. out:
  217. dev_set_drvdata(&ofdev->dev, NULL);
  218. iounmap(host->io_base);
  219. kfree(host);
  220. return res;
  221. }
  222. /*
  223. * Remove a NAND device.
  224. */
  225. static int __devexit socrates_nand_remove(struct platform_device *ofdev)
  226. {
  227. struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
  228. struct mtd_info *mtd = &host->mtd;
  229. nand_release(mtd);
  230. dev_set_drvdata(&ofdev->dev, NULL);
  231. iounmap(host->io_base);
  232. kfree(host);
  233. return 0;
  234. }
  235. static const struct of_device_id socrates_nand_match[] =
  236. {
  237. {
  238. .compatible = "abb,socrates-nand",
  239. },
  240. {},
  241. };
  242. MODULE_DEVICE_TABLE(of, socrates_nand_match);
  243. static struct platform_driver socrates_nand_driver = {
  244. .driver = {
  245. .name = "socrates_nand",
  246. .owner = THIS_MODULE,
  247. .of_match_table = socrates_nand_match,
  248. },
  249. .probe = socrates_nand_probe,
  250. .remove = __devexit_p(socrates_nand_remove),
  251. };
  252. static int __init socrates_nand_init(void)
  253. {
  254. return platform_driver_register(&socrates_nand_driver);
  255. }
  256. static void __exit socrates_nand_exit(void)
  257. {
  258. platform_driver_unregister(&socrates_nand_driver);
  259. }
  260. module_init(socrates_nand_init);
  261. module_exit(socrates_nand_exit);
  262. MODULE_LICENSE("GPL");
  263. MODULE_AUTHOR("Ilya Yanok");
  264. MODULE_DESCRIPTION("NAND driver for Socrates board");