map_ram.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 unsigned long mapram_unmapped_area(struct mtd_info *, unsigned long,
  21. unsigned long, unsigned long);
  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 = kzalloc(sizeof(*mtd), GFP_KERNEL);
  48. if (!mtd)
  49. return NULL;
  50. map->fldrv = &mapram_chipdrv;
  51. mtd->priv = map;
  52. mtd->name = map->name;
  53. mtd->type = MTD_RAM;
  54. mtd->size = map->size;
  55. mtd->_erase = mapram_erase;
  56. mtd->_get_unmapped_area = mapram_unmapped_area;
  57. mtd->_read = mapram_read;
  58. mtd->_write = mapram_write;
  59. mtd->_panic_write = mapram_write;
  60. mtd->_sync = mapram_nop;
  61. mtd->flags = MTD_CAP_RAM;
  62. mtd->writesize = 1;
  63. mtd->erasesize = PAGE_SIZE;
  64. while(mtd->size & (mtd->erasesize - 1))
  65. mtd->erasesize >>= 1;
  66. __module_get(THIS_MODULE);
  67. return mtd;
  68. }
  69. /*
  70. * Allow NOMMU mmap() to directly map the device (if not NULL)
  71. * - return the address to which the offset maps
  72. * - return -ENOSYS to indicate refusal to do the mapping
  73. */
  74. static unsigned long mapram_unmapped_area(struct mtd_info *mtd,
  75. unsigned long len,
  76. unsigned long offset,
  77. unsigned long flags)
  78. {
  79. struct map_info *map = mtd->priv;
  80. return (unsigned long) map->virt + offset;
  81. }
  82. static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  83. {
  84. struct map_info *map = mtd->priv;
  85. map_copy_from(map, buf, from, len);
  86. *retlen = len;
  87. return 0;
  88. }
  89. static int mapram_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  90. {
  91. struct map_info *map = mtd->priv;
  92. map_copy_to(map, to, buf, len);
  93. *retlen = len;
  94. return 0;
  95. }
  96. static int mapram_erase (struct mtd_info *mtd, struct erase_info *instr)
  97. {
  98. /* Yeah, it's inefficient. Who cares? It's faster than a _real_
  99. flash erase. */
  100. struct map_info *map = mtd->priv;
  101. map_word allff;
  102. unsigned long i;
  103. allff = map_word_ff(map);
  104. for (i=0; i<instr->len; i += map_bankwidth(map))
  105. map_write(map, allff, instr->addr + i);
  106. instr->state = MTD_ERASE_DONE;
  107. mtd_erase_callback(instr);
  108. return 0;
  109. }
  110. static void mapram_nop(struct mtd_info *mtd)
  111. {
  112. /* Nothing to see here */
  113. }
  114. static int __init map_ram_init(void)
  115. {
  116. register_mtd_chip_driver(&mapram_chipdrv);
  117. return 0;
  118. }
  119. static void __exit map_ram_exit(void)
  120. {
  121. unregister_mtd_chip_driver(&mapram_chipdrv);
  122. }
  123. module_init(map_ram_init);
  124. module_exit(map_ram_exit);
  125. MODULE_LICENSE("GPL");
  126. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  127. MODULE_DESCRIPTION("MTD chip driver for RAM chips");