map_ram.c 3.1 KB

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