excite_nandflash.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/kernel.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/nand.h>
  32. #include <linux/mtd/nand_ecc.h>
  33. #include <linux/mtd/partitions.h>
  34. #include <asm/io.h>
  35. #include <asm/rm9k-ocd.h>
  36. #include <excite_nandflash.h>
  37. #define EXCITE_NANDFLASH_VERSION "0.1"
  38. /* I/O register offsets */
  39. #define EXCITE_NANDFLASH_DATA_BYTE 0x00
  40. #define EXCITE_NANDFLASH_STATUS_BYTE 0x0c
  41. #define EXCITE_NANDFLASH_ADDR_BYTE 0x10
  42. #define EXCITE_NANDFLASH_CMD_BYTE 0x14
  43. /* prefix for debug output */
  44. static const char module_id[] = "excite_nandflash";
  45. /*
  46. * partition definition
  47. */
  48. static const struct mtd_partition partition_info[] = {
  49. {
  50. .name = "eXcite RootFS",
  51. .offset = 0,
  52. .size = MTDPART_SIZ_FULL
  53. }
  54. };
  55. static inline const struct resource *
  56. excite_nand_get_resource(struct platform_device *d, unsigned long flags,
  57. const char *basename)
  58. {
  59. char buf[80];
  60. if (snprintf(buf, sizeof buf, "%s_%u", basename, d->id) >= sizeof buf)
  61. return NULL;
  62. return platform_get_resource_byname(d, flags, buf);
  63. }
  64. static inline void __iomem *
  65. excite_nand_map_regs(struct platform_device *d, const char *basename)
  66. {
  67. void *result = NULL;
  68. const struct resource *const r =
  69. excite_nand_get_resource(d, IORESOURCE_MEM, basename);
  70. if (r)
  71. result = ioremap_nocache(r->start, r->end + 1 - r->start);
  72. return result;
  73. }
  74. /* controller and mtd information */
  75. struct excite_nand_drvdata {
  76. struct mtd_info board_mtd;
  77. struct nand_chip board_chip;
  78. void __iomem *regs;
  79. void __iomem *tgt;
  80. };
  81. /* Control function */
  82. static void excite_nand_control(struct mtd_info *mtd, int cmd,
  83. unsigned int ctrl)
  84. {
  85. struct excite_nand_drvdata * const d =
  86. container_of(mtd, struct excite_nand_drvdata, board_mtd);
  87. switch (ctrl) {
  88. case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
  89. d->tgt = d->regs + EXCITE_NANDFLASH_CMD_BYTE;
  90. break;
  91. case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
  92. d->tgt = d->regs + EXCITE_NANDFLASH_ADDR_BYTE;
  93. break;
  94. case NAND_CTRL_CHANGE | NAND_NCE:
  95. d->tgt = d->regs + EXCITE_NANDFLASH_DATA_BYTE;
  96. break;
  97. }
  98. if (cmd != NAND_CMD_NONE)
  99. __raw_writeb(cmd, d->tgt);
  100. }
  101. /* Return 0 if flash is busy, 1 if ready */
  102. static int excite_nand_devready(struct mtd_info *mtd)
  103. {
  104. struct excite_nand_drvdata * const drvdata =
  105. container_of(mtd, struct excite_nand_drvdata, board_mtd);
  106. return __raw_readb(drvdata->regs + EXCITE_NANDFLASH_STATUS_BYTE);
  107. }
  108. /*
  109. * Called by device layer to remove the driver.
  110. * The binding to the mtd and all allocated
  111. * resources are released.
  112. */
  113. static int __exit excite_nand_remove(struct device *dev)
  114. {
  115. struct excite_nand_drvdata * const this = dev_get_drvdata(dev);
  116. dev_set_drvdata(dev, NULL);
  117. if (unlikely(!this)) {
  118. printk(KERN_ERR "%s: called %s without private data!!",
  119. module_id, __func__);
  120. return -EINVAL;
  121. }
  122. /* first thing we need to do is release our mtd
  123. * then go through freeing the resource used
  124. */
  125. nand_release(&this->board_mtd);
  126. /* free the common resources */
  127. iounmap(this->regs);
  128. kfree(this);
  129. DEBUG(MTD_DEBUG_LEVEL1, "%s: removed\n", module_id);
  130. return 0;
  131. }
  132. /*
  133. * Called by device layer when it finds a device matching
  134. * one our driver can handle. This code checks to see if
  135. * it can allocate all necessary resources then calls the
  136. * nand layer to look for devices.
  137. */
  138. static int __init excite_nand_probe(struct device *dev)
  139. {
  140. struct platform_device * const pdev = to_platform_device(dev);
  141. struct excite_nand_drvdata *drvdata; /* private driver data */
  142. struct nand_chip *board_chip; /* private flash chip data */
  143. struct mtd_info *board_mtd; /* mtd info for this board */
  144. int scan_res;
  145. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  146. if (unlikely(!drvdata)) {
  147. printk(KERN_ERR "%s: no memory for drvdata\n",
  148. module_id);
  149. return -ENOMEM;
  150. }
  151. /* bind private data into driver */
  152. dev_set_drvdata(dev, drvdata);
  153. /* allocate and map the resource */
  154. drvdata->regs =
  155. excite_nand_map_regs(pdev, EXCITE_NANDFLASH_RESOURCE_REGS);
  156. if (unlikely(!drvdata->regs)) {
  157. printk(KERN_ERR "%s: cannot reserve register region\n",
  158. module_id);
  159. kfree(drvdata);
  160. return -ENXIO;
  161. }
  162. drvdata->tgt = drvdata->regs + EXCITE_NANDFLASH_DATA_BYTE;
  163. /* initialise our chip */
  164. board_chip = &drvdata->board_chip;
  165. board_chip->IO_ADDR_R = board_chip->IO_ADDR_W =
  166. drvdata->regs + EXCITE_NANDFLASH_DATA_BYTE;
  167. board_chip->cmd_ctrl = excite_nand_control;
  168. board_chip->dev_ready = excite_nand_devready;
  169. board_chip->chip_delay = 25;
  170. board_chip->ecc.mode = NAND_ECC_SOFT;
  171. /* link chip to mtd */
  172. board_mtd = &drvdata->board_mtd;
  173. board_mtd->priv = board_chip;
  174. DEBUG(MTD_DEBUG_LEVEL2, "%s: device scan\n", module_id);
  175. scan_res = nand_scan(&drvdata->board_mtd, 1);
  176. if (likely(!scan_res)) {
  177. DEBUG(MTD_DEBUG_LEVEL2, "%s: register partitions\n", module_id);
  178. add_mtd_partitions(&drvdata->board_mtd, partition_info,
  179. sizeof partition_info / sizeof partition_info[0]);
  180. } else {
  181. iounmap(drvdata->regs);
  182. kfree(drvdata);
  183. printk(KERN_ERR "%s: device scan failed\n", module_id);
  184. return -EIO;
  185. }
  186. return 0;
  187. }
  188. static struct device_driver excite_nand_driver = {
  189. .name = "excite_nand",
  190. .bus = &platform_bus_type,
  191. .probe = excite_nand_probe,
  192. .remove = __exit_p(excite_nand_remove)
  193. };
  194. static int __init excite_nand_init(void)
  195. {
  196. pr_info("Basler eXcite nand flash driver Version "
  197. EXCITE_NANDFLASH_VERSION "\n");
  198. return driver_register(&excite_nand_driver);
  199. }
  200. static void __exit excite_nand_exit(void)
  201. {
  202. driver_unregister(&excite_nand_driver);
  203. }
  204. module_init(excite_nand_init);
  205. module_exit(excite_nand_exit);
  206. MODULE_AUTHOR("Thomas Koeller <thomas.koeller@baslerweb.com>");
  207. MODULE_DESCRIPTION("Basler eXcite NAND-Flash driver");
  208. MODULE_LICENSE("GPL");
  209. MODULE_VERSION(EXCITE_NANDFLASH_VERSION)