excite_nandflash.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2005 - 2007 by Basler Vision Technologies AG
  3. * Author: Thomas Koeller <thomas.koeller.qbaslerweb.com>
  4. * Original code by Thies Moeller <thies.moeller@baslerweb.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/string.h>
  25. #include <linux/ioport.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/delay.h>
  28. #include <linux/err.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/nand.h>
  31. #include <linux/mtd/nand_ecc.h>
  32. #include <linux/mtd/partitions.h>
  33. #include <asm/io.h>
  34. #include <asm/rm9k-ocd.h>
  35. #include <excite_nandflash.h>
  36. #define EXCITE_NANDFLASH_VERSION "0.1"
  37. /* I/O register offsets */
  38. #define EXCITE_NANDFLASH_DATA_BYTE 0x00
  39. #define EXCITE_NANDFLASH_STATUS_BYTE 0x0c
  40. #define EXCITE_NANDFLASH_ADDR_BYTE 0x10
  41. #define EXCITE_NANDFLASH_CMD_BYTE 0x14
  42. /* prefix for debug output */
  43. static const char module_id[] = "excite_nandflash";
  44. /*
  45. * partition definition
  46. */
  47. static const struct mtd_partition partition_info[] = {
  48. {
  49. .name = "eXcite RootFS",
  50. .offset = 0,
  51. .size = MTDPART_SIZ_FULL
  52. }
  53. };
  54. static inline const struct resource *
  55. excite_nand_get_resource(struct platform_device *d, unsigned long flags,
  56. const char *basename)
  57. {
  58. char buf[80];
  59. if (snprintf(buf, sizeof buf, "%s_%u", basename, d->id) >= sizeof buf)
  60. return NULL;
  61. return platform_get_resource_byname(d, flags, buf);
  62. }
  63. static inline void __iomem *
  64. excite_nand_map_regs(struct platform_device *d, const char *basename)
  65. {
  66. void *result = NULL;
  67. const struct resource *const r =
  68. excite_nand_get_resource(d, IORESOURCE_MEM, basename);
  69. if (r)
  70. result = ioremap_nocache(r->start, r->end + 1 - r->start);
  71. return result;
  72. }
  73. /* controller and mtd information */
  74. struct excite_nand_drvdata {
  75. struct mtd_info board_mtd;
  76. struct nand_chip board_chip;
  77. void __iomem *regs;
  78. void __iomem *tgt;
  79. };
  80. /* Control function */
  81. static void excite_nand_control(struct mtd_info *mtd, int cmd,
  82. unsigned int ctrl)
  83. {
  84. struct excite_nand_drvdata * const d =
  85. container_of(mtd, struct excite_nand_drvdata, board_mtd);
  86. switch (ctrl) {
  87. case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
  88. d->tgt = d->regs + EXCITE_NANDFLASH_CMD_BYTE;
  89. break;
  90. case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
  91. d->tgt = d->regs + EXCITE_NANDFLASH_ADDR_BYTE;
  92. break;
  93. case NAND_CTRL_CHANGE | NAND_NCE:
  94. d->tgt = d->regs + EXCITE_NANDFLASH_DATA_BYTE;
  95. break;
  96. }
  97. if (cmd != NAND_CMD_NONE)
  98. __raw_writeb(cmd, d->tgt);
  99. }
  100. /* Return 0 if flash is busy, 1 if ready */
  101. static int excite_nand_devready(struct mtd_info *mtd)
  102. {
  103. struct excite_nand_drvdata * const drvdata =
  104. container_of(mtd, struct excite_nand_drvdata, board_mtd);
  105. return __raw_readb(drvdata->regs + EXCITE_NANDFLASH_STATUS_BYTE);
  106. }
  107. /*
  108. * Called by device layer to remove the driver.
  109. * The binding to the mtd and all allocated
  110. * resources are released.
  111. */
  112. static int __exit excite_nand_remove(struct device *dev)
  113. {
  114. struct excite_nand_drvdata * const this = dev_get_drvdata(dev);
  115. dev_set_drvdata(dev, NULL);
  116. if (unlikely(!this)) {
  117. printk(KERN_ERR "%s: called %s without private data!!",
  118. module_id, __func__);
  119. return -EINVAL;
  120. }
  121. /* first thing we need to do is release our mtd
  122. * then go through freeing the resource used
  123. */
  124. nand_release(&this->board_mtd);
  125. /* free the common resources */
  126. iounmap(this->regs);
  127. kfree(this);
  128. DEBUG(MTD_DEBUG_LEVEL1, "%s: removed\n", module_id);
  129. return 0;
  130. }
  131. /*
  132. * Called by device layer when it finds a device matching
  133. * one our driver can handle. This code checks to see if
  134. * it can allocate all necessary resources then calls the
  135. * nand layer to look for devices.
  136. */
  137. static int __init excite_nand_probe(struct device *dev)
  138. {
  139. struct platform_device * const pdev = to_platform_device(dev);
  140. struct excite_nand_drvdata *drvdata; /* private driver data */
  141. struct nand_chip *board_chip; /* private flash chip data */
  142. struct mtd_info *board_mtd; /* mtd info for this board */
  143. int scan_res;
  144. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  145. if (unlikely(!drvdata)) {
  146. printk(KERN_ERR "%s: no memory for drvdata\n",
  147. module_id);
  148. return -ENOMEM;
  149. }
  150. /* bind private data into driver */
  151. dev_set_drvdata(dev, drvdata);
  152. /* allocate and map the resource */
  153. drvdata->regs =
  154. excite_nand_map_regs(pdev, EXCITE_NANDFLASH_RESOURCE_REGS);
  155. if (unlikely(!drvdata->regs)) {
  156. printk(KERN_ERR "%s: cannot reserve register region\n",
  157. module_id);
  158. kfree(drvdata);
  159. return -ENXIO;
  160. }
  161. drvdata->tgt = drvdata->regs + EXCITE_NANDFLASH_DATA_BYTE;
  162. /* initialise our chip */
  163. board_chip = &drvdata->board_chip;
  164. board_chip->IO_ADDR_R = board_chip->IO_ADDR_W =
  165. drvdata->regs + EXCITE_NANDFLASH_DATA_BYTE;
  166. board_chip->cmd_ctrl = excite_nand_control;
  167. board_chip->dev_ready = excite_nand_devready;
  168. board_chip->chip_delay = 25;
  169. board_chip->ecc.mode = NAND_ECC_SOFT;
  170. /* link chip to mtd */
  171. board_mtd = &drvdata->board_mtd;
  172. board_mtd->priv = board_chip;
  173. DEBUG(MTD_DEBUG_LEVEL2, "%s: device scan\n", module_id);
  174. scan_res = nand_scan(&drvdata->board_mtd, 1);
  175. if (likely(!scan_res)) {
  176. DEBUG(MTD_DEBUG_LEVEL2, "%s: register partitions\n", module_id);
  177. add_mtd_partitions(&drvdata->board_mtd, partition_info,
  178. ARRAY_SIZE(partition_info));
  179. } else {
  180. iounmap(drvdata->regs);
  181. kfree(drvdata);
  182. printk(KERN_ERR "%s: device scan failed\n", module_id);
  183. return -EIO;
  184. }
  185. return 0;
  186. }
  187. static struct device_driver excite_nand_driver = {
  188. .name = "excite_nand",
  189. .bus = &platform_bus_type,
  190. .probe = excite_nand_probe,
  191. .remove = __exit_p(excite_nand_remove)
  192. };
  193. static int __init excite_nand_init(void)
  194. {
  195. pr_info("Basler eXcite nand flash driver Version "
  196. EXCITE_NANDFLASH_VERSION "\n");
  197. return driver_register(&excite_nand_driver);
  198. }
  199. static void __exit excite_nand_exit(void)
  200. {
  201. driver_unregister(&excite_nand_driver);
  202. }
  203. module_init(excite_nand_init);
  204. module_exit(excite_nand_exit);
  205. MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
  206. MODULE_DESCRIPTION("Basler eXcite NAND-Flash driver");
  207. MODULE_LICENSE("GPL");
  208. MODULE_VERSION(EXCITE_NANDFLASH_VERSION)