map_ram.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.22 2005/01/05 18: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/init.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/map.h>
  16. #include <linux/mtd/compatmac.h>
  17. static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  18. static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  19. static int mapram_erase (struct mtd_info *, struct erase_info *);
  20. static void mapram_nop (struct mtd_info *);
  21. static struct mtd_info *map_ram_probe(struct map_info *map);
  22. static struct mtd_chip_driver mapram_chipdrv = {
  23. .probe = map_ram_probe,
  24. .name = "map_ram",
  25. .module = THIS_MODULE
  26. };
  27. static struct mtd_info *map_ram_probe(struct map_info *map)
  28. {
  29. struct mtd_info *mtd;
  30. /* Check the first byte is RAM */
  31. #if 0
  32. map_write8(map, 0x55, 0);
  33. if (map_read8(map, 0) != 0x55)
  34. return NULL;
  35. map_write8(map, 0xAA, 0);
  36. if (map_read8(map, 0) != 0xAA)
  37. return NULL;
  38. /* Check the last byte is RAM */
  39. map_write8(map, 0x55, map->size-1);
  40. if (map_read8(map, map->size-1) != 0x55)
  41. return NULL;
  42. map_write8(map, 0xAA, map->size-1);
  43. if (map_read8(map, map->size-1) != 0xAA)
  44. return NULL;
  45. #endif
  46. /* OK. It seems to be RAM. */
  47. mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
  48. if (!mtd)
  49. return NULL;
  50. memset(mtd, 0, sizeof(*mtd));
  51. map->fldrv = &mapram_chipdrv;
  52. mtd->priv = map;
  53. mtd->name = map->name;
  54. mtd->type = MTD_RAM;
  55. mtd->size = map->size;
  56. mtd->erase = mapram_erase;
  57. mtd->read = mapram_read;
  58. mtd->write = mapram_write;
  59. mtd->sync = mapram_nop;
  60. mtd->flags = MTD_CAP_RAM | MTD_VOLATILE;
  61. mtd->erasesize = PAGE_SIZE;
  62. while(mtd->size & (mtd->erasesize - 1))
  63. mtd->erasesize >>= 1;
  64. __module_get(THIS_MODULE);
  65. return mtd;
  66. }
  67. static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  68. {
  69. struct map_info *map = mtd->priv;
  70. map_copy_from(map, buf, from, len);
  71. *retlen = len;
  72. return 0;
  73. }
  74. static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  75. {
  76. struct map_info *map = mtd->priv;
  77. map_copy_to(map, to, buf, len);
  78. *retlen = len;
  79. return 0;
  80. }
  81. static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
  82. {
  83. /* Yeah, it's inefficient. Who cares? It's faster than a _real_
  84. flash erase. */
  85. struct map_info *map = mtd->priv;
  86. map_word allff;
  87. unsigned long i;
  88. allff = map_word_ff(map);
  89. for (i=0; i<instr->len; i += map_bankwidth(map))
  90. map_write(map, allff, instr->addr + i);
  91. instr->state = MTD_ERASE_DONE;
  92. mtd_erase_callback(instr);
  93. return 0;
  94. }
  95. static void mapram_nop(struct mtd_info *mtd)
  96. {
  97. /* Nothing to see here */
  98. }
  99. static int __init map_ram_init(void)
  100. {
  101. register_mtd_chip_driver(&mapram_chipdrv);
  102. return 0;
  103. }
  104. static void __exit map_ram_exit(void)
  105. {
  106. unregister_mtd_chip_driver(&mapram_chipdrv);
  107. }
  108. module_init(map_ram_init);
  109. module_exit(map_ram_exit);
  110. MODULE_LICENSE("GPL");
  111. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  112. MODULE_DESCRIPTION("MTD chip driver for RAM chips");