socrates_nand.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_address.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/io.h>
  20. #define FPGA_NAND_CMD_MASK (0x7 << 28)
  21. #define FPGA_NAND_CMD_COMMAND (0x0 << 28)
  22. #define FPGA_NAND_CMD_ADDR (0x1 << 28)
  23. #define FPGA_NAND_CMD_READ (0x2 << 28)
  24. #define FPGA_NAND_CMD_WRITE (0x3 << 28)
  25. #define FPGA_NAND_BUSY (0x1 << 15)
  26. #define FPGA_NAND_ENABLE (0x1 << 31)
  27. #define FPGA_NAND_DATA_SHIFT 16
  28. struct socrates_nand_host {
  29. struct nand_chip nand_chip;
  30. struct mtd_info mtd;
  31. void __iomem *io_base;
  32. struct device *dev;
  33. };
  34. /**
  35. * socrates_nand_write_buf - write buffer to chip
  36. * @mtd: MTD device structure
  37. * @buf: data buffer
  38. * @len: number of bytes to write
  39. */
  40. static void socrates_nand_write_buf(struct mtd_info *mtd,
  41. const uint8_t *buf, int len)
  42. {
  43. int i;
  44. struct nand_chip *this = mtd->priv;
  45. struct socrates_nand_host *host = this->priv;
  46. for (i = 0; i < len; i++) {
  47. out_be32(host->io_base, FPGA_NAND_ENABLE |
  48. FPGA_NAND_CMD_WRITE |
  49. (buf[i] << FPGA_NAND_DATA_SHIFT));
  50. }
  51. }
  52. /**
  53. * socrates_nand_read_buf - read chip data into buffer
  54. * @mtd: MTD device structure
  55. * @buf: buffer to store date
  56. * @len: number of bytes to read
  57. */
  58. static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  59. {
  60. int i;
  61. struct nand_chip *this = mtd->priv;
  62. struct socrates_nand_host *host = this->priv;
  63. uint32_t val;
  64. val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ;
  65. out_be32(host->io_base, val);
  66. for (i = 0; i < len; i++) {
  67. buf[i] = (in_be32(host->io_base) >>
  68. FPGA_NAND_DATA_SHIFT) & 0xff;
  69. }
  70. }
  71. /**
  72. * socrates_nand_read_byte - read one byte from the chip
  73. * @mtd: MTD device structure
  74. */
  75. static uint8_t socrates_nand_read_byte(struct mtd_info *mtd)
  76. {
  77. uint8_t byte;
  78. socrates_nand_read_buf(mtd, &byte, sizeof(byte));
  79. return byte;
  80. }
  81. /**
  82. * socrates_nand_read_word - read one word from the chip
  83. * @mtd: MTD device structure
  84. */
  85. static uint16_t socrates_nand_read_word(struct mtd_info *mtd)
  86. {
  87. uint16_t word;
  88. socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word));
  89. return word;
  90. }
  91. /*
  92. * Hardware specific access to control-lines
  93. */
  94. static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  95. unsigned int ctrl)
  96. {
  97. struct nand_chip *nand_chip = mtd->priv;
  98. struct socrates_nand_host *host = nand_chip->priv;
  99. uint32_t val;
  100. if (cmd == NAND_CMD_NONE)
  101. return;
  102. if (ctrl & NAND_CLE)
  103. val = FPGA_NAND_CMD_COMMAND;
  104. else
  105. val = FPGA_NAND_CMD_ADDR;
  106. if (ctrl & NAND_NCE)
  107. val |= FPGA_NAND_ENABLE;
  108. val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
  109. out_be32(host->io_base, val);
  110. }
  111. /*
  112. * Read the Device Ready pin.
  113. */
  114. static int socrates_nand_device_ready(struct mtd_info *mtd)
  115. {
  116. struct nand_chip *nand_chip = mtd->priv;
  117. struct socrates_nand_host *host = nand_chip->priv;
  118. if (in_be32(host->io_base) & FPGA_NAND_BUSY)
  119. return 0; /* busy */
  120. return 1;
  121. }
  122. /*
  123. * Probe for the NAND device.
  124. */
  125. static int socrates_nand_probe(struct platform_device *ofdev)
  126. {
  127. struct socrates_nand_host *host;
  128. struct mtd_info *mtd;
  129. struct nand_chip *nand_chip;
  130. int res;
  131. struct mtd_part_parser_data ppdata;
  132. /* Allocate memory for the device structure (and zero it) */
  133. host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
  134. if (!host)
  135. return -ENOMEM;
  136. host->io_base = of_iomap(ofdev->dev.of_node, 0);
  137. if (host->io_base == NULL) {
  138. dev_err(&ofdev->dev, "ioremap failed\n");
  139. return -EIO;
  140. }
  141. mtd = &host->mtd;
  142. nand_chip = &host->nand_chip;
  143. host->dev = &ofdev->dev;
  144. nand_chip->priv = host; /* link the private data structures */
  145. mtd->priv = nand_chip;
  146. mtd->name = "socrates_nand";
  147. mtd->dev.parent = &ofdev->dev;
  148. ppdata.of_node = ofdev->dev.of_node;
  149. /*should never be accessed directly */
  150. nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
  151. nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
  152. nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
  153. nand_chip->read_byte = socrates_nand_read_byte;
  154. nand_chip->read_word = socrates_nand_read_word;
  155. nand_chip->write_buf = socrates_nand_write_buf;
  156. nand_chip->read_buf = socrates_nand_read_buf;
  157. nand_chip->dev_ready = socrates_nand_device_ready;
  158. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  159. /* TODO: I have no idea what real delay is. */
  160. nand_chip->chip_delay = 20; /* 20us command delay time */
  161. dev_set_drvdata(&ofdev->dev, host);
  162. /* first scan to find the device and get the page size */
  163. if (nand_scan_ident(mtd, 1, NULL)) {
  164. res = -ENXIO;
  165. goto out;
  166. }
  167. /* second phase scan */
  168. if (nand_scan_tail(mtd)) {
  169. res = -ENXIO;
  170. goto out;
  171. }
  172. res = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  173. if (!res)
  174. return res;
  175. nand_release(mtd);
  176. out:
  177. iounmap(host->io_base);
  178. return res;
  179. }
  180. /*
  181. * Remove a NAND device.
  182. */
  183. static int socrates_nand_remove(struct platform_device *ofdev)
  184. {
  185. struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
  186. struct mtd_info *mtd = &host->mtd;
  187. nand_release(mtd);
  188. iounmap(host->io_base);
  189. return 0;
  190. }
  191. static const struct of_device_id socrates_nand_match[] =
  192. {
  193. {
  194. .compatible = "abb,socrates-nand",
  195. },
  196. {},
  197. };
  198. MODULE_DEVICE_TABLE(of, socrates_nand_match);
  199. static struct platform_driver socrates_nand_driver = {
  200. .driver = {
  201. .name = "socrates_nand",
  202. .of_match_table = socrates_nand_match,
  203. },
  204. .probe = socrates_nand_probe,
  205. .remove = socrates_nand_remove,
  206. };
  207. module_platform_driver(socrates_nand_driver);
  208. MODULE_LICENSE("GPL");
  209. MODULE_AUTHOR("Ilya Yanok");
  210. MODULE_DESCRIPTION("NAND driver for Socrates board");