map_ram.c 3.7 KB

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