socrates_nand.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * Hardware specific access to control-lines
  92. */
  93. static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  94. unsigned int ctrl)
  95. {
  96. struct nand_chip *nand_chip = mtd->priv;
  97. struct socrates_nand_host *host = nand_chip->priv;
  98. uint32_t val;
  99. if (cmd == NAND_CMD_NONE)
  100. return;
  101. if (ctrl & NAND_CLE)
  102. val = FPGA_NAND_CMD_COMMAND;
  103. else
  104. val = FPGA_NAND_CMD_ADDR;
  105. if (ctrl & NAND_NCE)
  106. val |= FPGA_NAND_ENABLE;
  107. val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
  108. out_be32(host->io_base, val);
  109. }
  110. /*
  111. * Read the Device Ready pin.
  112. */
  113. static int socrates_nand_device_ready(struct mtd_info *mtd)
  114. {
  115. struct nand_chip *nand_chip = mtd->priv;
  116. struct socrates_nand_host *host = nand_chip->priv;
  117. if (in_be32(host->io_base) & FPGA_NAND_BUSY)
  118. return 0; /* busy */
  119. return 1;
  120. }
  121. /*
  122. * Probe for the NAND device.
  123. */
  124. static int socrates_nand_probe(struct platform_device *ofdev)
  125. {
  126. struct socrates_nand_host *host;
  127. struct mtd_info *mtd;
  128. struct nand_chip *nand_chip;
  129. int res;
  130. struct mtd_part_parser_data ppdata;
  131. /* Allocate memory for the device structure (and zero it) */
  132. host = kzalloc(sizeof(struct socrates_nand_host), GFP_KERNEL);
  133. if (!host) {
  134. printk(KERN_ERR
  135. "socrates_nand: failed to allocate device structure.\n");
  136. return -ENOMEM;
  137. }
  138. host->io_base = of_iomap(ofdev->dev.of_node, 0);
  139. if (host->io_base == NULL) {
  140. printk(KERN_ERR "socrates_nand: ioremap failed\n");
  141. kfree(host);
  142. return -EIO;
  143. }
  144. mtd = &host->mtd;
  145. nand_chip = &host->nand_chip;
  146. host->dev = &ofdev->dev;
  147. nand_chip->priv = host; /* link the private data structures */
  148. mtd->priv = nand_chip;
  149. mtd->name = "socrates_nand";
  150. mtd->owner = THIS_MODULE;
  151. mtd->dev.parent = &ofdev->dev;
  152. ppdata.of_node = ofdev->dev.of_node;
  153. /*should never be accessed directly */
  154. nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
  155. nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
  156. nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
  157. nand_chip->read_byte = socrates_nand_read_byte;
  158. nand_chip->read_word = socrates_nand_read_word;
  159. nand_chip->write_buf = socrates_nand_write_buf;
  160. nand_chip->read_buf = socrates_nand_read_buf;
  161. nand_chip->dev_ready = socrates_nand_device_ready;
  162. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  163. /* TODO: I have no idea what real delay is. */
  164. nand_chip->chip_delay = 20; /* 20us command delay time */
  165. dev_set_drvdata(&ofdev->dev, host);
  166. /* first scan to find the device and get the page size */
  167. if (nand_scan_ident(mtd, 1, NULL)) {
  168. res = -ENXIO;
  169. goto out;
  170. }
  171. /* second phase scan */
  172. if (nand_scan_tail(mtd)) {
  173. res = -ENXIO;
  174. goto out;
  175. }
  176. res = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  177. if (!res)
  178. return res;
  179. nand_release(mtd);
  180. out:
  181. dev_set_drvdata(&ofdev->dev, NULL);
  182. iounmap(host->io_base);
  183. kfree(host);
  184. return res;
  185. }
  186. /*
  187. * Remove a NAND device.
  188. */
  189. static int socrates_nand_remove(struct platform_device *ofdev)
  190. {
  191. struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
  192. struct mtd_info *mtd = &host->mtd;
  193. nand_release(mtd);
  194. dev_set_drvdata(&ofdev->dev, NULL);
  195. iounmap(host->io_base);
  196. kfree(host);
  197. return 0;
  198. }
  199. static const struct of_device_id socrates_nand_match[] =
  200. {
  201. {
  202. .compatible = "abb,socrates-nand",
  203. },
  204. {},
  205. };
  206. MODULE_DEVICE_TABLE(of, socrates_nand_match);
  207. static struct platform_driver socrates_nand_driver = {
  208. .driver = {
  209. .name = "socrates_nand",
  210. .owner = THIS_MODULE,
  211. .of_match_table = socrates_nand_match,
  212. },
  213. .probe = socrates_nand_probe,
  214. .remove = socrates_nand_remove,
  215. };
  216. module_platform_driver(socrates_nand_driver);
  217. MODULE_LICENSE("GPL");
  218. MODULE_AUTHOR("Ilya Yanok");
  219. MODULE_DESCRIPTION("NAND driver for Socrates board");