socrates_nand.c 5.0 KB

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