socrates_nand.c 5.8 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. 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_to_nand(mtd);
  44. struct socrates_nand_host *host = nand_get_controller_data(this);
  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_to_nand(mtd);
  61. struct socrates_nand_host *host = nand_get_controller_data(this);
  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_to_nand(mtd);
  97. struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
  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_to_nand(mtd);
  116. struct socrates_nand_host *host = nand_get_controller_data(nand_chip);
  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. /* Allocate memory for the device structure (and zero it) */
  131. host = devm_kzalloc(&ofdev->dev, sizeof(*host), GFP_KERNEL);
  132. if (!host)
  133. return -ENOMEM;
  134. host->io_base = of_iomap(ofdev->dev.of_node, 0);
  135. if (host->io_base == NULL) {
  136. dev_err(&ofdev->dev, "ioremap failed\n");
  137. return -EIO;
  138. }
  139. nand_chip = &host->nand_chip;
  140. mtd = nand_to_mtd(nand_chip);
  141. host->dev = &ofdev->dev;
  142. /* link the private data structures */
  143. nand_set_controller_data(nand_chip, host);
  144. nand_set_flash_node(nand_chip, ofdev->dev.of_node);
  145. mtd->name = "socrates_nand";
  146. mtd->dev.parent = &ofdev->dev;
  147. /*should never be accessed directly */
  148. nand_chip->IO_ADDR_R = (void *)0xdeadbeef;
  149. nand_chip->IO_ADDR_W = (void *)0xdeadbeef;
  150. nand_chip->cmd_ctrl = socrates_nand_cmd_ctrl;
  151. nand_chip->read_byte = socrates_nand_read_byte;
  152. nand_chip->read_word = socrates_nand_read_word;
  153. nand_chip->write_buf = socrates_nand_write_buf;
  154. nand_chip->read_buf = socrates_nand_read_buf;
  155. nand_chip->dev_ready = socrates_nand_device_ready;
  156. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  157. nand_chip->ecc.algo = NAND_ECC_HAMMING;
  158. /* TODO: I have no idea what real delay is. */
  159. nand_chip->chip_delay = 20; /* 20us command delay time */
  160. dev_set_drvdata(&ofdev->dev, host);
  161. /* first scan to find the device and get the page size */
  162. if (nand_scan_ident(mtd, 1, NULL)) {
  163. res = -ENXIO;
  164. goto out;
  165. }
  166. /* second phase scan */
  167. if (nand_scan_tail(mtd)) {
  168. res = -ENXIO;
  169. goto out;
  170. }
  171. res = mtd_device_register(mtd, NULL, 0);
  172. if (!res)
  173. return res;
  174. nand_release(mtd);
  175. out:
  176. iounmap(host->io_base);
  177. return res;
  178. }
  179. /*
  180. * Remove a NAND device.
  181. */
  182. static int socrates_nand_remove(struct platform_device *ofdev)
  183. {
  184. struct socrates_nand_host *host = dev_get_drvdata(&ofdev->dev);
  185. struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
  186. nand_release(mtd);
  187. iounmap(host->io_base);
  188. return 0;
  189. }
  190. static const struct of_device_id socrates_nand_match[] =
  191. {
  192. {
  193. .compatible = "abb,socrates-nand",
  194. },
  195. {},
  196. };
  197. MODULE_DEVICE_TABLE(of, socrates_nand_match);
  198. static struct platform_driver socrates_nand_driver = {
  199. .driver = {
  200. .name = "socrates_nand",
  201. .of_match_table = socrates_nand_match,
  202. },
  203. .probe = socrates_nand_probe,
  204. .remove = socrates_nand_remove,
  205. };
  206. module_platform_driver(socrates_nand_driver);
  207. MODULE_LICENSE("GPL");
  208. MODULE_AUTHOR("Ilya Yanok");
  209. MODULE_DESCRIPTION("NAND driver for Socrates board");