socrates_nand.c 6.0 KB

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