excite_flashtest.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2005 by Basler Vision Technologies AG
  3. * Author: Thies Moeller <thies.moeller@baslerweb.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/string.h>
  24. #include <linux/ioport.h>
  25. #include <linux/device.h>
  26. #include <linux/delay.h>
  27. #include <linux/err.h>
  28. #include <linux/kernel.h>
  29. #include <excite.h>
  30. #include <asm/io.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/nand.h>
  33. #include <linux/mtd/nand_ecc.h>
  34. #include <linux/mtd/partitions.h>
  35. #include <asm/rm9k-ocd.h> // for ocd_write
  36. #include <linux/workqueue.h> // for queue
  37. #include "excite_nandflash.h"
  38. #include "nandflash.h"
  39. #define PFX "excite flashtest: "
  40. typedef void __iomem *io_reg_t;
  41. #define io_readb(__a__) __raw_readb((__a__))
  42. #define io_writeb(__v__, __a__) __raw_writeb((__v__), (__a__))
  43. static inline const struct resource *excite_nandflash_get_resource(
  44. struct platform_device *d, unsigned long flags, const char *basename)
  45. {
  46. const char fmt[] = "%s_%u";
  47. char buf[80];
  48. if (unlikely(snprintf(buf, sizeof buf, fmt, basename, d->id) >= sizeof buf))
  49. return NULL;
  50. return platform_get_resource_byname(d, flags, buf);
  51. }
  52. static inline io_reg_t
  53. excite_nandflash_map_regs(struct platform_device *d, const char *basename)
  54. {
  55. void *result = NULL;
  56. const struct resource *const r =
  57. excite_nandflash_get_resource(d, IORESOURCE_MEM, basename);
  58. if (r)
  59. result = ioremap_nocache(r->start, r->end + 1 - r->start);
  60. return result;
  61. }
  62. /* controller and mtd information */
  63. struct excite_nandflash_drvdata {
  64. struct mtd_info board_mtd;
  65. struct nand_chip board_chip;
  66. io_reg_t regs;
  67. };
  68. /* command and control functions */
  69. static void excite_nandflash_hwcontrol(struct mtd_info *mtd, int cmd)
  70. {
  71. struct nand_chip *this = mtd->priv;
  72. io_reg_t regs = container_of(mtd,struct excite_nandflash_drvdata,board_mtd)->regs;
  73. switch (cmd) {
  74. /* Select the command latch */
  75. case NAND_CTL_SETCLE: this->IO_ADDR_W = regs + EXCITE_NANDFLASH_CMD;
  76. break;
  77. /* Deselect the command latch */
  78. case NAND_CTL_CLRCLE: this->IO_ADDR_W = regs + EXCITE_NANDFLASH_DATA;
  79. break;
  80. /* Select the address latch */
  81. case NAND_CTL_SETALE: this->IO_ADDR_W = regs + EXCITE_NANDFLASH_ADDR;
  82. break;
  83. /* Deselect the address latch */
  84. case NAND_CTL_CLRALE: this->IO_ADDR_W = regs + EXCITE_NANDFLASH_DATA;
  85. break;
  86. /* Select the chip -- not used */
  87. case NAND_CTL_SETNCE:
  88. break;
  89. /* Deselect the chip -- not used */
  90. case NAND_CTL_CLRNCE:
  91. break;
  92. }
  93. this->IO_ADDR_R = this->IO_ADDR_W;
  94. }
  95. /* excite_nandflash_devready()
  96. *
  97. * returns 0 if the nand is busy, 1 if it is ready
  98. */
  99. static int excite_nandflash_devready(struct mtd_info *mtd)
  100. {
  101. struct excite_nandflash_drvdata *drvdata =
  102. container_of(mtd, struct excite_nandflash_drvdata, board_mtd);
  103. return io_readb(drvdata->regs + EXCITE_NANDFLASH_STATUS);
  104. }
  105. /* device management functions */
  106. /* excite_nandflash_remove
  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 excite_nandflash_remove(struct device *dev)
  113. {
  114. struct excite_nandflash_drvdata *this = dev_get_drvdata(dev);
  115. pr_info(PFX "remove");
  116. dev_set_drvdata(dev, NULL);
  117. if (this == NULL) {
  118. pr_debug(PFX "call remove without private data!!");
  119. return 0;
  120. }
  121. /* free the common resources */
  122. if (this->regs != NULL) {
  123. iounmap(this->regs);
  124. this->regs = NULL;
  125. }
  126. kfree(this);
  127. return 0;
  128. }
  129. static int elapsed;
  130. void my_workqueue_handler(void *arg)
  131. {
  132. elapsed = 1;
  133. }
  134. DECLARE_WORK(sigElapsed, my_workqueue_handler, 0);
  135. /* excite_nandflash_probe
  136. *
  137. * called by device layer when it finds a device matching
  138. * one our driver can handled. This code checks to see if
  139. * it can allocate all necessary resources then calls the
  140. * nand layer to look for devices
  141. */
  142. static int excite_nandflash_probe(struct device *dev)
  143. {
  144. struct platform_device *pdev = to_platform_device(dev);
  145. struct excite_nandflash_drvdata *drvdata; /* private driver data */
  146. struct nand_chip *board_chip; /* private flash chip data */
  147. struct mtd_info *board_mtd; /* mtd info for this board */
  148. int err = 0;
  149. int count = 0;
  150. struct timeval tv,endtv;
  151. unsigned int dt;
  152. pr_info(PFX "probe dev: (%p)\n", dev);
  153. pr_info(PFX "adjust LB timing\n");
  154. ocd_writel(0x00000330, LDP2);
  155. drvdata = kmalloc(sizeof(*drvdata), GFP_KERNEL);
  156. if (unlikely(!drvdata)) {
  157. printk(KERN_ERR PFX "no memory for drvdata\n");
  158. err = -ENOMEM;
  159. goto mem_error;
  160. }
  161. /* Initialize structures */
  162. memset(drvdata, 0, sizeof(*drvdata));
  163. /* bind private data into driver */
  164. dev_set_drvdata(dev, drvdata);
  165. /* allocate and map the resource */
  166. drvdata->regs =
  167. excite_nandflash_map_regs(pdev, EXCITE_NANDFLASH_RESOURCE_REGS);
  168. if (unlikely(!drvdata->regs)) {
  169. printk(KERN_ERR PFX "cannot reserve register region\n");
  170. err = -ENXIO;
  171. goto io_error;
  172. }
  173. /* initialise our chip */
  174. board_chip = &drvdata->board_chip;
  175. board_chip->IO_ADDR_R = drvdata->regs + EXCITE_NANDFLASH_DATA;
  176. board_chip->IO_ADDR_W = drvdata->regs + EXCITE_NANDFLASH_DATA;
  177. board_chip->hwcontrol = excite_nandflash_hwcontrol;
  178. board_chip->dev_ready = excite_nandflash_devready;
  179. board_chip->chip_delay = 25;
  180. #if 0
  181. /* TODO: speedup the initial scan */
  182. board_chip->options = NAND_USE_FLASH_BBT;
  183. #endif
  184. board_chip->eccmode = NAND_ECC_SOFT;
  185. /* link chip to mtd */
  186. board_mtd = &drvdata->board_mtd;
  187. board_mtd->priv = board_chip;
  188. pr_info(PFX "FlashTest\n");
  189. elapsed = 0;
  190. /* schedule_delayed_work(&sigElapsed, 1*HZ);
  191. while (!elapsed) {
  192. io_readb(drvdata->regs + EXCITE_NANDFLASH_STATUS);
  193. count++;
  194. }
  195. pr_info(PFX "reads in 1 sec --> %d\n",count);
  196. */
  197. do_gettimeofday(&tv);
  198. for (count = 0 ; count < 1000000; count ++) {
  199. io_readb(drvdata->regs + EXCITE_NANDFLASH_STATUS);
  200. }
  201. do_gettimeofday(&endtv);
  202. dt = (endtv.tv_sec - tv.tv_sec) * 1000000 + endtv.tv_usec - tv.tv_usec;
  203. pr_info(PFX "%8d us timeval\n",dt);
  204. pr_info(PFX "EndFlashTest\n");
  205. /* return with error to unload everything
  206. */
  207. io_error:
  208. iounmap(drvdata->regs);
  209. mem_error:
  210. kfree(drvdata);
  211. if (err == 0)
  212. err = -EINVAL;
  213. return err;
  214. }
  215. static struct device_driver excite_nandflash_driver = {
  216. .name = "excite_nand",
  217. .bus = &platform_bus_type,
  218. .probe = excite_nandflash_probe,
  219. .remove = excite_nandflash_remove,
  220. };
  221. static int __init excite_nandflash_init(void)
  222. {
  223. pr_info(PFX "register Driver (Rev: $Revision:$)\n");
  224. return driver_register(&excite_nandflash_driver);
  225. }
  226. static void __exit excite_nandflash_exit(void)
  227. {
  228. driver_unregister(&excite_nandflash_driver);
  229. pr_info(PFX "Driver unregistered");
  230. }
  231. module_init(excite_nandflash_init);
  232. module_exit(excite_nandflash_exit);
  233. MODULE_AUTHOR("Thies Moeller <thies.moeller@baslerweb.com>");
  234. MODULE_DESCRIPTION("Basler eXcite NAND-Flash driver");
  235. MODULE_LICENSE("GPL");