physmap-bt1-rom.c 3.0 KB

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