map_ram.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Common code to handle map devices which are simple RAM
  3.  * (C) 2000 Red Hat. GPL'd.
  4.  * $Id: map_ram.c,v 1.14 2001/10/02 15:05:12 dwmw2 Exp $
  5.  */
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <asm/io.h>
  10. #include <asm/byteorder.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/mtd/map.h>
  14. static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  15. static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  16. static int mapram_erase (struct mtd_info *, struct erase_info *);
  17. static void mapram_nop (struct mtd_info *);
  18. static struct mtd_info *map_ram_probe(struct map_info *map);
  19. static struct mtd_chip_driver mapram_chipdrv = {
  20. probe: map_ram_probe,
  21. name: "map_ram",
  22. module: THIS_MODULE
  23. };
  24. static struct mtd_info *map_ram_probe(struct map_info *map)
  25. {
  26. struct mtd_info *mtd;
  27. /* Check the first byte is RAM */
  28. #if 0
  29. map->write8(map, 0x55, 0);
  30. if (map->read8(map, 0) != 0x55)
  31. return NULL;
  32. map->write8(map, 0xAA, 0);
  33. if (map->read8(map, 0) != 0xAA)
  34. return NULL;
  35. /* Check the last byte is RAM */
  36. map->write8(map, 0x55, map->size-1);
  37. if (map->read8(map, map->size-1) != 0x55)
  38. return NULL;
  39. map->write8(map, 0xAA, map->size-1);
  40. if (map->read8(map, map->size-1) != 0xAA)
  41. return NULL;
  42. #endif
  43. /* OK. It seems to be RAM. */
  44. mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
  45. if (!mtd)
  46. return NULL;
  47. memset(mtd, 0, sizeof(*mtd));
  48. map->fldrv = &mapram_chipdrv;
  49. mtd->priv = map;
  50. mtd->name = map->name;
  51. mtd->type = MTD_RAM;
  52. mtd->size = map->size;
  53. mtd->erase = mapram_erase;
  54. mtd->read = mapram_read;
  55. mtd->write = mapram_write;
  56. mtd->sync = mapram_nop;
  57. mtd->flags = MTD_CAP_RAM | MTD_VOLATILE;
  58. mtd->erasesize = PAGE_SIZE;
  59.   while(mtd->size & (mtd->erasesize - 1))
  60. mtd->erasesize >>= 1;
  61. MOD_INC_USE_COUNT;
  62. return mtd;
  63. }
  64. static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  65. {
  66. struct map_info *map = (struct map_info *)mtd->priv;
  67. map->copy_from(map, buf, from, len);
  68. *retlen = len;
  69. return 0;
  70. }
  71. static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  72. {
  73. struct map_info *map = (struct map_info *)mtd->priv;
  74. map->copy_to(map, to, buf, len);
  75. *retlen = len;
  76. return 0;
  77. }
  78. static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
  79. {
  80. /* Yeah, it's inefficient. Who cares? It's faster than a _real_
  81.    flash erase. */
  82. struct map_info *map = (struct map_info *)mtd->priv;
  83. unsigned long i;
  84. for (i=0; i<instr->len; i++)
  85. map->write8(map, 0xFF, instr->addr + i);
  86. if (instr->callback)
  87. instr->callback(instr);
  88. return 0;
  89. }
  90. static void mapram_nop(struct mtd_info *mtd)
  91. {
  92. /* Nothing to see here */
  93. }
  94. int __init map_ram_init(void)
  95. {
  96. register_mtd_chip_driver(&mapram_chipdrv);
  97. return 0;
  98. }
  99. static void __exit map_ram_exit(void)
  100. {
  101. unregister_mtd_chip_driver(&mapram_chipdrv);
  102. }
  103. module_init(map_ram_init);
  104. module_exit(map_ram_exit);
  105. MODULE_LICENSE("GPL");
  106. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  107. MODULE_DESCRIPTION("MTD chip driver for RAM chips");