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