map_rom.c 3.1 KB

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