socrates_nand.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2008 Ilya Yanok, Emcraft Systems
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/module.h>
  7. #include <linux/mtd/mtd.h>
  8. #include <linux/mtd/rawnand.h>
  9. #include <linux/mtd/partitions.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/io.h>
  13. #define FPGA_NAND_CMD_MASK (0x7 << 28)
  14. #define FPGA_NAND_CMD_COMMAND (0x0 << 28)
  15. #define FPGA_NAND_CMD_ADDR (0x1 << 28)
  16. #define FPGA_NAND_CMD_READ (0x2 << 28)
  17. #define FPGA_NAND_CMD_WRITE (0x3 << 28)
  18. #define FPGA_NAND_BUSY (0x1 << 15)
  19. #define FPGA_NAND_ENABLE (0x1 << 31)
  20. #define FPGA_NAND_DATA_SHIFT 16
  21. struct socrates_nand_host {
  22. struct nand_controller controller;
  23. struct nand_chip nand_chip;
  24. void __iomem *io_base;
  25. struct device *dev;
  26. };
  27. /**
  28. * socrates_nand_write_buf - write buffer to chip
  29. * @this: NAND chip object
  30. * @buf: data buffer
  31. * @len: number of bytes to write
  32. */
  33. static void socrates_nand_write_buf(struct nand_chip *this, const uint8_t *buf,
  34. int len)
  35. {
  36. int i;
  37. struct socrates_nand_host *host = nand_get_controller_data(this);
  38. for (i = 0; i < len; i++) {
  39. out_be32(host->io_base, FPGA_NAND_ENABLE |
  40. FPGA_NAND_CMD_WRITE |
  41. (buf[i] << FPGA_NAND_DATA_SHIFT));
  42. }
  43. }
  44. /**
  45. * socrates_nand_read_buf - read chip data into buffer
  46. * @this: NAND chip object
  47. * @buf: buffer to store date
  48. * @len: number of bytes to read
  49. */
  50. static void socrates_nand_read_buf(struct nand_chip *this, uint8_t *buf,
  51. int len)
  52. {
  53. int i;
  54. struct socrates_nand_host *host = nand_get_controller_data(this);
  55. uint32_t val;
  56. val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ;
  57. out_be32(host->io_base, val);
  58. for (i = 0; i < len; i++) {
  59. buf[i] = (in_be32(host->io_base) >>
  60. FPGA_NAND_DATA_SHIFT) & 0xff;
  61. }
  62. }
  63. /**
  64. * socrates_nand_read_byte - read one byte from the chip
  65. * @mtd: MTD device structure
  66. */
  67. static uint8_t socrates_nand_read_byte(struct nand_chip *this)
  68. {
  69. uint8_t byte;
  70. socrates_nand_read_buf(this, &byte, sizeof(byte));
  71. return byte;
  72. }
  73. /*
  74. * Hardware specific access to control-lines
  75. */
  76. static void socrates_nand_cmd_ctrl(struct nand_chip *nand_chip, int cmd,
  77. unsigned int ctrl)
  78. {
  79. struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
  80. uint32_t val;
  81. if (cmd == NAND_CMD_NONE)
  82. return;
  83. if (ctrl & NAND_CLE)
  84. val = FPGA_NAND_CMD_COMMAND;
  85. else
  86. val = FPGA_NAND_CMD_ADDR;
  87. if (ctrl & NAND_NCE)
  88. val |= FPGA_NAND_ENABLE;
  89. val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
  90. out_be32(host->io_base, val);
  91. }
  92. /*
  93. * Read the Device Ready pin.
  94. */
  95. static int socrates_nand_device_ready(struct nand_chip *nand_chip)
  96. {
  97. struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
  98. if (in_be32(host->io_base) & FPGA_NAND_BUSY)
  99. return 0; /* busy */
  100. return 1;
  101. }
  102. static int socrates_attach_chip(struct nand_chip *chip)
  103. {
  104. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
  105. chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
  106. chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  107. return 0;
  108. }
  109. static const struct nand_controller_ops socrates_ops = {
  110. .attach_chip = socrates_attach_chip,
  111. };
  112. /*
  113. * Probe for the NAND device.
  114. */
  115. static int socrates_nand_probe(struct platform_device *ofdev)
  116. {
  117. struct socrates_nand_host *host;
  118. struct mtd_info *mtd;
  119. struct nand_chip *nand_chip;
  120. int res;
  121. /* Allocate memory for the device structure (and zero it) */
  122. host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
  123. if (!host)
  124. return -ENOMEM;
  125. host->io_base = of_iomap(ofdev->dev.of_node, 0);
  126. if (host->io_base == NULL) {
  127. dev_err(&ofdev->dev, "ioremap failed\n");
  128. return -EIO;
  129. }
  130. nand_chip = &host->nand_chip;
  131. mtd = nand_to_mtd(nand_chip);
  132. host->dev = &ofdev->dev;
  133. nand_controller_init(&host->controller);
  134. host->controller.ops = &socrates_ops;
  135. nand_chip->controller = &host->controller;
  136. /* link the private data structures */
  137. nand_set_controller_data(nand_chip, host);
  138. nand_set_flash_node(nand_chip, ofdev->dev.of_node);
  139. mtd->name = "socrates_nand";
  140. mtd->dev.parent = &ofdev->dev;
  141. nand_chip->legacy.cmd_ctrl = socrates_nand_cmd_ctrl;
  142. nand_chip->legacy.read_byte = socrates_nand_read_byte;
  143. nand_chip->legacy.write_buf = socrates_nand_write_buf;
  144. nand_chip->legacy.read_buf = socrates_nand_read_buf;
  145. nand_chip->legacy.dev_ready = socrates_nand_device_ready;
  146. /* TODO: I have no idea what real delay is. */
  147. nand_chip->legacy.chip_delay = 20; /* 20us command delay time */
  148. /*
  149. * This driver assumes that the default ECC engine should be TYPE_SOFT.
  150. * Set ->engine_type before registering the NAND devices in order to
  151. * provide a driver specific default value.
  152. */
  153. nand_chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  154. dev_set_drvdata(&ofdev->dev, host);
  155. res = nand_scan(nand_chip, 1);
  156. if (res)
  157. goto out;
  158. res = mtd_device_register(mtd, NULL, 0);
  159. if (!res)
  160. return res;
  161. nand_cleanup(nand_chip);
  162. out:
  163. iounmap(host->io_base);
  164. return res;
  165. }
  166. /*
  167. * Remove a NAND device.
  168. */
  169. static int socrates_nand_remove(struct platform_device *ofdev)
  170. {
  171. struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
  172. struct nand_chip *chip = &host->nand_chip;
  173. int ret;
  174. ret = mtd_device_unregister(nand_to_mtd(chip));
  175. WARN_ON(ret);
  176. nand_cleanup(chip);
  177. iounmap(host->io_base);
  178. return 0;
  179. }
  180. static const struct of_device_id socrates_nand_match[] =
  181. {
  182. {
  183. .compatible = "abb,socrates-nand",
  184. },
  185. {},
  186. };
  187. MODULE_DEVICE_TABLE(of, socrates_nand_match);
  188. static struct platform_driver socrates_nand_driver = {
  189. .driver = {
  190. .name = "socrates_nand",
  191. .of_match_table = socrates_nand_match,
  192. },
  193. .probe = socrates_nand_probe,
  194. .remove = socrates_nand_remove,
  195. };
  196. module_platform_driver(socrates_nand_driver);
  197. MODULE_LICENSE("GPL");
  198. MODULE_AUTHOR("Ilya Yanok");
  199. MODULE_DESCRIPTION("NAND driver for Socrates board");