map_ram.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. static int mapram_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  16. static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  17. static int mapram_erase (struct mtd_info *, struct erase_info *);
  18. static void mapram_nop (struct mtd_info *);
  19. static struct mtd_info *map_ram_probe(struct map_info *map);
  20. static int mapram_point (struct mtd_info *mtd, loff_t from, size_t len,
  21. size_t *retlen, void **virt, resource_size_t *phys);
  22. static int mapram_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
  23. static struct mtd_chip_driver mapram_chipdrv = {
  24. .probe = map_ram_probe,
  25. .name = "map_ram",
  26. .module = THIS_MODULE
  27. };
  28. static struct mtd_info *map_ram_probe(struct map_info *map)
  29. {
  30. struct mtd_info *mtd;
  31. /* Check the first byte is RAM */
  32. #if 0
  33. map_write8(map, 0x55, 0);
  34. if (map_read8(map, 0) != 0x55)
  35. return NULL;
  36. map_write8(map, 0xAA, 0);
  37. if (map_read8(map, 0) != 0xAA)
  38. return NULL;
  39. /* Check the last byte is RAM */
  40. map_write8(map, 0x55, map->size-1);
  41. if (map_read8(map, map->size-1) != 0x55)
  42. return NULL;
  43. map_write8(map, 0xAA, map->size-1);
  44. if (map_read8(map, map->size-1) != 0xAA)
  45. return NULL;
  46. #endif
  47. /* OK. It seems to be RAM. */
  48. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  49. if (!mtd)
  50. return NULL;
  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->_panic_write = mapram_write;
  60. mtd->_point = mapram_point;
  61. mtd->_sync = mapram_nop;
  62. mtd->_unpoint = mapram_unpoint;
  63. mtd->flags = MTD_CAP_RAM;
  64. mtd->writesize = 1;
  65. mtd->erasesize = PAGE_SIZE;
  66. while(mtd->size & (mtd->erasesize - 1))
  67. mtd->erasesize >>= 1;
  68. __module_get(THIS_MODULE);
  69. return mtd;
  70. }
  71. static int mapram_point(struct mtd_info *mtd, loff_t from, size_t len,
  72. size_t *retlen, void **virt, resource_size_t *phys)
  73. {
  74. struct map_info *map = mtd->priv;
  75. if (!map->virt)
  76. return -EINVAL;
  77. *virt = map->virt + from;
  78. if (phys)
  79. *phys = map->phys + from;
  80. *retlen = len;
  81. return 0;
  82. }
  83. static int mapram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  84. {
  85. return 0;
  86. }
  87. static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  88. {
  89. struct map_info *map = mtd->priv;
  90. map_copy_from(map, buf, from, len);
  91. *retlen = len;
  92. return 0;
  93. }
  94. static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  95. {
  96. struct map_info *map = mtd->priv;
  97. map_copy_to(map, to, buf, len);
  98. *retlen = len;
  99. return 0;
  100. }
  101. static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
  102. {
  103. /* Yeah, it's inefficient. Who cares? It's faster than a _real_
  104. flash erase. */
  105. struct map_info *map = mtd->priv;
  106. map_word allff;
  107. unsigned long i;
  108. allff = map_word_ff(map);
  109. for (i=0; i<instr->len; i += map_bankwidth(map))
  110. map_write(map, allff, instr->addr + i);
  111. return 0;
  112. }
  113. static void mapram_nop(struct mtd_info *mtd)
  114. {
  115. /* Nothing to see here */
  116. }
  117. static int __init map_ram_init(void)
  118. {
  119. register_mtd_chip_driver(&mapram_chipdrv);
  120. return 0;
  121. }
  122. static void __exit map_ram_exit(void)
  123. {
  124. unregister_mtd_chip_driver(&mapram_chipdrv);
  125. }
  126. module_init(map_ram_init);
  127. module_exit(map_ram_exit);
  128. MODULE_LICENSE("GPL");
  129. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  130. MODULE_DESCRIPTION("MTD chip driver for RAM chips");