map_rom.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 int maprom_point (struct mtd_info *mtd, loff_t from, size_t len,
  22. size_t *retlen, void **virt, resource_size_t *phys);
  23. static int maprom_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
  24. static struct mtd_chip_driver maprom_chipdrv = {
  25. .probe = map_rom_probe,
  26. .name = "map_rom",
  27. .module = THIS_MODULE
  28. };
  29. static unsigned int default_erasesize(struct map_info *map)
  30. {
  31. const __be32 *erase_size = NULL;
  32. erase_size = of_get_property(map->device_node, "erase-size", NULL);
  33. return !erase_size ? map->size : be32_to_cpu(*erase_size);
  34. }
  35. static struct mtd_info *map_rom_probe(struct map_info *map)
  36. {
  37. struct mtd_info *mtd;
  38. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  39. if (!mtd)
  40. return NULL;
  41. map->fldrv = &maprom_chipdrv;
  42. mtd->priv = map;
  43. mtd->name = map->name;
  44. mtd->type = MTD_ROM;
  45. mtd->size = map->size;
  46. mtd->_point = maprom_point;
  47. mtd->_unpoint = maprom_unpoint;
  48. mtd->_read = maprom_read;
  49. mtd->_write = maprom_write;
  50. mtd->_sync = maprom_nop;
  51. mtd->_erase = maprom_erase;
  52. mtd->flags = MTD_CAP_ROM;
  53. mtd->erasesize = default_erasesize(map);
  54. mtd->writesize = 1;
  55. mtd->writebufsize = 1;
  56. __module_get(THIS_MODULE);
  57. return mtd;
  58. }
  59. static int maprom_point(struct mtd_info *mtd, loff_t from, size_t len,
  60. size_t *retlen, void **virt, resource_size_t *phys)
  61. {
  62. struct map_info *map = mtd->priv;
  63. if (!map->virt)
  64. return -EINVAL;
  65. *virt = map->virt + from;
  66. if (phys)
  67. *phys = map->phys + from;
  68. *retlen = len;
  69. return 0;
  70. }
  71. static int maprom_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  72. {
  73. return 0;
  74. }
  75. static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  76. {
  77. struct map_info *map = mtd->priv;
  78. map_copy_from(map, buf, from, len);
  79. *retlen = len;
  80. return 0;
  81. }
  82. static void maprom_nop(struct mtd_info *mtd)
  83. {
  84. /* Nothing to see here */
  85. }
  86. static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  87. {
  88. return -EROFS;
  89. }
  90. static int maprom_erase (struct mtd_info *mtd, struct erase_info *info)
  91. {
  92. /* We do our best 8) */
  93. return -EROFS;
  94. }
  95. static int __init map_rom_init(void)
  96. {
  97. register_mtd_chip_driver(&maprom_chipdrv);
  98. return 0;
  99. }
  100. static void __exit map_rom_exit(void)
  101. {
  102. unregister_mtd_chip_driver(&maprom_chipdrv);
  103. }
  104. module_init(map_rom_init);
  105. module_exit(map_rom_exit);
  106. MODULE_LICENSE("GPL");
  107. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  108. MODULE_DESCRIPTION("MTD chip driver for ROM chips");