map_rom.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Common code to handle map devices which are simple ROM
  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/of.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/map.h>
  16. static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  17. static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  18. static void maprom_nop (struct mtd_info *);
  19. static struct mtd_info *map_rom_probe(struct map_info *map);
  20. static int maprom_erase (struct mtd_info *mtd, struct erase_info *info);
  21. static unsigned long maprom_unmapped_area(struct mtd_info *, unsigned long,
  22. unsigned long, unsigned long);
  23. static struct mtd_chip_driver maprom_chipdrv = {
  24. .probe = map_rom_probe,
  25. .name = "map_rom",
  26. .module = THIS_MODULE
  27. };
  28. static unsigned int default_erasesize(struct map_info *map)
  29. {
  30. const __be32 *erase_size = NULL;
  31. erase_size = of_get_property(map->device_node, "erase-size", NULL);
  32. return !erase_size ? map->size : be32_to_cpu(*erase_size);
  33. }
  34. static struct mtd_info *map_rom_probe(struct map_info *map)
  35. {
  36. struct mtd_info *mtd;
  37. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  38. if (!mtd)
  39. return NULL;
  40. map->fldrv = &maprom_chipdrv;
  41. mtd->priv = map;
  42. mtd->name = map->name;
  43. mtd->type = MTD_ROM;
  44. mtd->size = map->size;
  45. mtd->_get_unmapped_area = maprom_unmapped_area;
  46. mtd->_read = maprom_read;
  47. mtd->_write = maprom_write;
  48. mtd->_sync = maprom_nop;
  49. mtd->_erase = maprom_erase;
  50. mtd->flags = MTD_CAP_ROM;
  51. mtd->erasesize = default_erasesize(map);
  52. mtd->writesize = 1;
  53. mtd->writebufsize = 1;
  54. __module_get(THIS_MODULE);
  55. return mtd;
  56. }
  57. /*
  58. * Allow NOMMU mmap() to directly map the device (if not NULL)
  59. * - return the address to which the offset maps
  60. * - return -ENOSYS to indicate refusal to do the mapping
  61. */
  62. static unsigned long maprom_unmapped_area(struct mtd_info *mtd,
  63. unsigned long len,
  64. unsigned long offset,
  65. unsigned long flags)
  66. {
  67. struct map_info *map = mtd->priv;
  68. return (unsigned long) map->virt + offset;
  69. }
  70. static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  71. {
  72. struct map_info *map = mtd->priv;
  73. map_copy_from(map, buf, from, len);
  74. *retlen = len;
  75. return 0;
  76. }
  77. static void maprom_nop(struct mtd_info *mtd)
  78. {
  79. /* Nothing to see here */
  80. }
  81. static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  82. {
  83. return -EROFS;
  84. }
  85. static int maprom_erase (struct mtd_info *mtd, struct erase_info *info)
  86. {
  87. /* We do our best 8) */
  88. return -EROFS;
  89. }
  90. static int __init map_rom_init(void)
  91. {
  92. register_mtd_chip_driver(&maprom_chipdrv);
  93. return 0;
  94. }
  95. static void __exit map_rom_exit(void)
  96. {
  97. unregister_mtd_chip_driver(&maprom_chipdrv);
  98. }
  99. module_init(map_rom_init);
  100. module_exit(map_rom_exit);
  101. MODULE_LICENSE("GPL");
  102. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  103. MODULE_DESCRIPTION("MTD chip driver for ROM chips");