socrates_nand.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #ifdef CONFIG_MTD_PARTITIONS
  138. static const char *part_probes[] = { "cmdlinepart", NULL };
  139. #endif
  140. /*
  141. * Probe for the NAND device.
  142. */
  143. static int __devinit socrates_nand_probe(struct of_device *ofdev,
  144. const struct of_device_id *ofid)
  145. {
  146. struct socrates_nand_host *host;
  147. struct mtd_info *mtd;
  148. struct nand_chip *nand_chip;
  149. int res;
  150. #ifdef CONFIG_MTD_PARTITIONS
  151. struct mtd_partition *partitions = NULL;
  152. int num_partitions = 0;
  153. #endif
  154. /* Allocate memory for the device structure (and zero it) */
  155. host = kzalloc(sizeof(struct socrates_nand_host), GFP_KERNEL);
  156. if (!host) {
  157. printk(KERN_ERR
  158. "socrates_nand: failed to allocate device structure.\n");
  159. return -ENOMEM;
  160. }
  161. host->io_base = of_iomap(ofdev->node, 0);
  162. if (host->io_base == NULL) {
  163. printk(KERN_ERR "socrates_nand: ioremap failed\n");
  164. kfree(host);
  165. return -EIO;
  166. }
  167. mtd = &host->mtd;
  168. nand_chip = &host->nand_chip;
  169. host->dev = &ofdev->dev;
  170. nand_chip->priv = host; /* link the private data structures */
  171. mtd->priv = nand_chip;
  172. mtd->name = "socrates_nand";
  173. mtd->owner = THIS_MODULE;
  174. mtd->dev.parent = &ofdev->dev;
  175. /*should never be accessed directly */
  176. nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
  177. nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
  178. nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
  179. nand_chip->read_byte = socrates_nand_read_byte;
  180. nand_chip->read_word = socrates_nand_read_word;
  181. nand_chip->write_buf = socrates_nand_write_buf;
  182. nand_chip->read_buf = socrates_nand_read_buf;
  183. nand_chip->verify_buf = socrates_nand_verify_buf;
  184. nand_chip->dev_ready = socrates_nand_device_ready;
  185. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  186. /* TODO: I have no idea what real delay is. */
  187. nand_chip->chip_delay = 20; /* 20us command delay time */
  188. dev_set_drvdata(&ofdev->dev, host);
  189. /* first scan to find the device and get the page size */
  190. if (nand_scan_ident(mtd, 1)) {
  191. res = -ENXIO;
  192. goto out;
  193. }
  194. /* second phase scan */
  195. if (nand_scan_tail(mtd)) {
  196. res = -ENXIO;
  197. goto out;
  198. }
  199. #ifdef CONFIG_MTD_PARTITIONS
  200. #ifdef CONFIG_MTD_CMDLINE_PARTS
  201. num_partitions = parse_mtd_partitions(mtd, part_probes,
  202. &partitions, 0);
  203. if (num_partitions < 0) {
  204. res = num_partitions;
  205. goto release;
  206. }
  207. #endif
  208. #ifdef CONFIG_MTD_OF_PARTS
  209. if (num_partitions == 0) {
  210. num_partitions = of_mtd_parse_partitions(&ofdev->dev,
  211. ofdev->node,
  212. &partitions);
  213. if (num_partitions < 0) {
  214. res = num_partitions;
  215. goto release;
  216. }
  217. }
  218. #endif
  219. if (partitions && (num_partitions > 0))
  220. res = add_mtd_partitions(mtd, partitions, num_partitions);
  221. else
  222. #endif
  223. res = add_mtd_device(mtd);
  224. if (!res)
  225. return res;
  226. #ifdef CONFIG_MTD_PARTITIONS
  227. release:
  228. #endif
  229. nand_release(mtd);
  230. out:
  231. dev_set_drvdata(&ofdev->dev, NULL);
  232. iounmap(host->io_base);
  233. kfree(host);
  234. return res;
  235. }
  236. /*
  237. * Remove a NAND device.
  238. */
  239. static int __devexit socrates_nand_remove(struct of_device *ofdev)
  240. {
  241. struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
  242. struct mtd_info *mtd = &host->mtd;
  243. nand_release(mtd);
  244. dev_set_drvdata(&ofdev->dev, NULL);
  245. iounmap(host->io_base);
  246. kfree(host);
  247. return 0;
  248. }
  249. static struct of_device_id socrates_nand_match[] =
  250. {
  251. {
  252. .compatible = "abb,socrates-nand",
  253. },
  254. {},
  255. };
  256. MODULE_DEVICE_TABLE(of, socrates_nand_match);
  257. static struct of_platform_driver socrates_nand_driver = {
  258. .name = "socrates_nand",
  259. .match_table = socrates_nand_match,
  260. .probe = socrates_nand_probe,
  261. .remove = __devexit_p(socrates_nand_remove),
  262. };
  263. static int __init socrates_nand_init(void)
  264. {
  265. return of_register_platform_driver(&socrates_nand_driver);
  266. }
  267. static void __exit socrates_nand_exit(void)
  268. {
  269. of_unregister_platform_driver(&socrates_nand_driver);
  270. }
  271. module_init(socrates_nand_init);
  272. module_exit(socrates_nand_exit);
  273. MODULE_LICENSE("GPL");
  274. MODULE_AUTHOR("Ilya Yanok");
  275. MODULE_DESCRIPTION("NAND driver for Socrates board");