socrates_nand.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. * @mtd: MTD device structure
  34. * @buf: data buffer
  35. * @len: number of bytes to write
  36. */
  37. static void socrates_nand_write_buf(struct mtd_info *mtd,
  38. const uint8_t *buf, int len)
  39. {
  40. int i;
  41. struct nand_chip *this = mtd_to_nand(mtd);
  42. struct socrates_nand_host *host = nand_get_controller_data(this);
  43. for (i = 0; i < len; i++) {
  44. out_be32(host->io_base, FPGA_NAND_ENABLE |
  45. FPGA_NAND_CMD_WRITE |
  46. (buf[i] << FPGA_NAND_DATA_SHIFT));
  47. }
  48. }
  49. /**
  50. * socrates_nand_read_buf - read chip data into buffer
  51. * @mtd: MTD device structure
  52. * @buf: buffer to store date
  53. * @len: number of bytes to read
  54. */
  55. static void socrates_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  56. {
  57. int i;
  58. struct nand_chip *this = mtd_to_nand(mtd);
  59. struct socrates_nand_host *host = nand_get_controller_data(this);
  60. uint32_t val;
  61. val = FPGA_NAND_ENABLE | FPGA_NAND_CMD_READ;
  62. out_be32(host->io_base, val);
  63. for (i = 0; i < len; i++) {
  64. buf[i] = (in_be32(host->io_base) >>
  65. FPGA_NAND_DATA_SHIFT) & 0xff;
  66. }
  67. }
  68. /**
  69. * socrates_nand_read_byte - read one byte from the chip
  70. * @mtd: MTD device structure
  71. */
  72. static uint8_t socrates_nand_read_byte(struct mtd_info *mtd)
  73. {
  74. uint8_t byte;
  75. socrates_nand_read_buf(mtd, &byte, sizeof(byte));
  76. return byte;
  77. }
  78. /**
  79. * socrates_nand_read_word - read one word from the chip
  80. * @mtd: MTD device structure
  81. */
  82. static uint16_t socrates_nand_read_word(struct mtd_info *mtd)
  83. {
  84. uint16_t word;
  85. socrates_nand_read_buf(mtd, (uint8_t *)&word, sizeof(word));
  86. return word;
  87. }
  88. /*
  89. * Hardware specific access to control-lines
  90. */
  91. static void socrates_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  92. unsigned int ctrl)
  93. {
  94. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  95. struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
  96. uint32_t val;
  97. if (cmd == NAND_CMD_NONE)
  98. return;
  99. if (ctrl & NAND_CLE)
  100. val = FPGA_NAND_CMD_COMMAND;
  101. else
  102. val = FPGA_NAND_CMD_ADDR;
  103. if (ctrl & NAND_NCE)
  104. val |= FPGA_NAND_ENABLE;
  105. val |= (cmd & 0xff) << FPGA_NAND_DATA_SHIFT;
  106. out_be32(host->io_base, val);
  107. }
  108. /*
  109. * Read the Device Ready pin.
  110. */
  111. static int socrates_nand_device_ready(struct mtd_info *mtd)
  112. {
  113. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  114. struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
  115. if (in_be32(host->io_base) & FPGA_NAND_BUSY)
  116. return 0; /* busy */
  117. return 1;
  118. }
  119. /*
  120. * Probe for the NAND device.
  121. */
  122. static int socrates_nand_probe(struct platform_device *ofdev)
  123. {
  124. struct socrates_nand_host *host;
  125. struct mtd_info *mtd;
  126. struct nand_chip *nand_chip;
  127. int res;
  128. /* Allocate memory for the device structure (and zero it) */
  129. host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
  130. if (!host)
  131. return -ENOMEM;
  132. host->io_base = of_iomap(ofdev->dev.of_node, 0);
  133. if (host->io_base == NULL) {
  134. dev_err(&ofdev->dev, "ioremap failed\n");
  135. return -EIO;
  136. }
  137. nand_chip = &host->nand_chip;
  138. mtd = nand_to_mtd(nand_chip);
  139. host->dev = &ofdev->dev;
  140. /* link the private data structures */
  141. nand_set_controller_data(nand_chip, host);
  142. nand_set_flash_node(nand_chip, ofdev->dev.of_node);
  143. mtd->name = "socrates_nand";
  144. mtd->dev.parent = &ofdev->dev;
  145. /*should never be accessed directly */
  146. nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
  147. nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
  148. nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
  149. nand_chip->read_byte = socrates_nand_read_byte;
  150. nand_chip->read_word = socrates_nand_read_word;
  151. nand_chip->write_buf = socrates_nand_write_buf;
  152. nand_chip->read_buf = socrates_nand_read_buf;
  153. nand_chip->dev_ready = socrates_nand_device_ready;
  154. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  155. nand_chip->ecc.algo = NAND_ECC_HAMMING;
  156. /* TODO: I have no idea what real delay is. */
  157. nand_chip->chip_delay = 20; /* 20us command delay time */
  158. dev_set_drvdata(&ofdev->dev, host);
  159. res = nand_scan(nand_chip, 1);
  160. if (res)
  161. goto out;
  162. res = mtd_device_register(mtd, NULL, 0);
  163. if (!res)
  164. return res;
  165. nand_cleanup(nand_chip);
  166. out:
  167. iounmap(host->io_base);
  168. return res;
  169. }
  170. /*
  171. * Remove a NAND device.
  172. */
  173. static int socrates_nand_remove(struct platform_device *ofdev)
  174. {
  175. struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
  176. nand_release(&host->nand_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");