physmap-bt1-rom.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
  4. *
  5. * Authors:
  6. * Serge Semin <Sergey.Semin@baikalelectronics.ru>
  7. *
  8. * Baikal-T1 Physically Mapped Internal ROM driver
  9. */
  10. #include <linux/bits.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mtd/map.h>
  14. #include <linux/mtd/xip.h>
  15. #include <linux/mux/consumer.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/string.h>
  19. #include <linux/types.h>
  20. #include "physmap-bt1-rom.h"
  21. /*
  22. * Baikal-T1 SoC ROMs are only accessible by the dword-aligned instructions.
  23. * We have to take this into account when implementing the data read-methods.
  24. * Note there is no need in bothering with endianness, since both Baikal-T1
  25. * CPU and MMIO are LE.
  26. */
  27. static map_word __xipram bt1_rom_map_read(struct map_info *map,
  28. unsigned long ofs)
  29. {
  30. void __iomem *src = map->virt + ofs;
  31. unsigned int shift;
  32. map_word ret;
  33. u32 data;
  34. /* Read data within offset dword. */
  35. shift = (uintptr_t)src & 0x3;
  36. data = readl_relaxed(src - shift);
  37. if (!shift) {
  38. ret.x[0] = data;
  39. return ret;
  40. }
  41. ret.x[0] = data >> (shift * BITS_PER_BYTE);
  42. /* Read data from the next dword. */
  43. shift = 4 - shift;
  44. if (ofs + shift >= map->size)
  45. return ret;
  46. data = readl_relaxed(src + shift);
  47. ret.x[0] |= data << (shift * BITS_PER_BYTE);
  48. return ret;
  49. }
  50. static void __xipram bt1_rom_map_copy_from(struct map_info *map,
  51. void *to, unsigned long from,
  52. ssize_t len)
  53. {
  54. void __iomem *src = map->virt + from;
  55. unsigned int shift, chunk;
  56. u32 data;
  57. if (len <= 0 || from >= map->size)
  58. return;
  59. /* Make sure we don't go over the map limit. */
  60. len = min_t(ssize_t, map->size - from, len);
  61. /*
  62. * Since requested data size can be pretty big we have to implement
  63. * the copy procedure as optimal as possible. That's why it's split
  64. * up into the next three stages: unaligned head, aligned body,
  65. * unaligned tail.
  66. */
  67. shift = (uintptr_t)src & 0x3;
  68. if (shift) {
  69. chunk = min_t(ssize_t, 4 - shift, len);
  70. data = readl_relaxed(src - shift);
  71. memcpy(to, (char *)&data + shift, chunk);
  72. src += chunk;
  73. to += chunk;
  74. len -= chunk;
  75. }
  76. while (len >= 4) {
  77. data = readl_relaxed(src);
  78. memcpy(to, &data, 4);
  79. src += 4;
  80. to += 4;
  81. len -= 4;
  82. }
  83. if (len) {
  84. data = readl_relaxed(src);
  85. memcpy(to, &data, len);
  86. }
  87. }
  88. int of_flash_probe_bt1_rom(struct platform_device *pdev,
  89. struct device_node *np,
  90. struct map_info *map)
  91. {
  92. struct device *dev = &pdev->dev;
  93. /* It's supposed to be read-only MTD. */
  94. if (!of_device_is_compatible(np, "mtd-rom")) {
  95. dev_info(dev, "No mtd-rom compatible string\n");
  96. return 0;
  97. }
  98. /* Multiplatform guard. */
  99. if (!of_device_is_compatible(np, "baikal,bt1-int-rom"))
  100. return 0;
  101. /* Sanity check the device parameters retrieved from DTB. */
  102. if (map->bankwidth != 4)
  103. dev_warn(dev, "Bank width is supposed to be 32 bits wide\n");
  104. map->read = bt1_rom_map_read;
  105. map->copy_from = bt1_rom_map_copy_from;
  106. return 0;
  107. }