excite_nandflash.c 6.6 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/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 platform_device *dev)
  113. {
  114. struct excite_nand_drvdata * const this = platform_get_drvdata(dev);
  115. platform_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 platform_device *pdev)
  138. {
  139. struct excite_nand_drvdata *drvdata; /* private driver data */
  140. struct nand_chip *board_chip; /* private flash chip data */
  141. struct mtd_info *board_mtd; /* mtd info for this board */
  142. int scan_res;
  143. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  144. if (unlikely(!drvdata)) {
  145. printk(KERN_ERR "%s: no memory for drvdata\n",
  146. module_id);
  147. return -ENOMEM;
  148. }
  149. /* bind private data into driver */
  150. platform_set_drvdata(pdev, drvdata);
  151. /* allocate and map the resource */
  152. drvdata->regs =
  153. excite_nand_map_regs(pdev, EXCITE_NANDFLASH_RESOURCE_REGS);
  154. if (unlikely(!drvdata->regs)) {
  155. printk(KERN_ERR "%s: cannot reserve register region\n",
  156. module_id);
  157. kfree(drvdata);
  158. return -ENXIO;
  159. }
  160. drvdata->tgt = drvdata->regs + EXCITE_NANDFLASH_DATA_BYTE;
  161. /* initialise our chip */
  162. board_chip = &drvdata->board_chip;
  163. board_chip->IO_ADDR_R = board_chip->IO_ADDR_W =
  164. drvdata->regs + EXCITE_NANDFLASH_DATA_BYTE;
  165. board_chip->cmd_ctrl = excite_nand_control;
  166. board_chip->dev_ready = excite_nand_devready;
  167. board_chip->chip_delay = 25;
  168. board_chip->ecc.mode = NAND_ECC_SOFT;
  169. /* link chip to mtd */
  170. board_mtd = &drvdata->board_mtd;
  171. board_mtd->priv = board_chip;
  172. DEBUG(MTD_DEBUG_LEVEL2, "%s: device scan\n", module_id);
  173. scan_res = nand_scan(&drvdata->board_mtd, 1);
  174. if (likely(!scan_res)) {
  175. DEBUG(MTD_DEBUG_LEVEL2, "%s: register partitions\n", module_id);
  176. add_mtd_partitions(&drvdata->board_mtd, partition_info,
  177. ARRAY_SIZE(partition_info));
  178. } else {
  179. iounmap(drvdata->regs);
  180. kfree(drvdata);
  181. printk(KERN_ERR "%s: device scan failed\n", module_id);
  182. return -EIO;
  183. }
  184. return 0;
  185. }
  186. static struct platform_driver excite_nand_driver = {
  187. .driver = {
  188. .name = "excite_nand",
  189. .owner = THIS_MODULE,
  190. },
  191. .probe = excite_nand_probe,
  192. .remove = __devexit_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 platform_driver_register(&excite_nand_driver);
  199. }
  200. static void __exit excite_nand_exit(void)
  201. {
  202. platform_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)