map_ram.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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;
  61. mtd->writesize = 1;
  62. mtd->erasesize = PAGE_SIZE;
  63. while(mtd->size & (mtd->erasesize - 1))
  64. mtd->erasesize >>= 1;
  65. __module_get(THIS_MODULE);
  66. return mtd;
  67. }
  68. static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  69. {
  70. struct map_info *map = mtd->priv;
  71. map_copy_from(map, buf, from, len);
  72. *retlen = len;
  73. return 0;
  74. }
  75. static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  76. {
  77. struct map_info *map = mtd->priv;
  78. map_copy_to(map, to, buf, len);
  79. *retlen = len;
  80. return 0;
  81. }
  82. static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
  83. {
  84. /* Yeah, it's inefficient. Who cares? It's faster than a _real_
  85. flash erase. */
  86. struct map_info *map = mtd->priv;
  87. map_word allff;
  88. unsigned long i;
  89. allff = map_word_ff(map);
  90. for (i=0; i<instr->len; i += map_bankwidth(map))
  91. map_write(map, allff, instr->addr + i);
  92. instr->state = MTD_ERASE_DONE;
  93. mtd_erase_callback(instr);
  94. return 0;
  95. }
  96. static void mapram_nop(struct mtd_info *mtd)
  97. {
  98. /* Nothing to see here */
  99. }
  100. static int __init map_ram_init(void)
  101. {
  102. register_mtd_chip_driver(&mapram_chipdrv);
  103. return 0;
  104. }
  105. static void __exit map_ram_exit(void)
  106. {
  107. unregister_mtd_chip_driver(&mapram_chipdrv);
  108. }
  109. module_init(map_ram_init);
  110. module_exit(map_ram_exit);
  111. MODULE_LICENSE("GPL");
  112. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  113. MODULE_DESCRIPTION("MTD chip driver for RAM chips");