Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
0004  *
0005  * Authors:
0006  *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
0007  *
0008  * Baikal-T1 Physically Mapped Internal ROM driver
0009  */
0010 #include <linux/bits.h>
0011 #include <linux/device.h>
0012 #include <linux/kernel.h>
0013 #include <linux/mtd/map.h>
0014 #include <linux/mtd/xip.h>
0015 #include <linux/mux/consumer.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/string.h>
0020 #include <linux/types.h>
0021 
0022 #include "physmap-bt1-rom.h"
0023 
0024 /*
0025  * Baikal-T1 SoC ROMs are only accessible by the dword-aligned instructions.
0026  * We have to take this into account when implementing the data read-methods.
0027  * Note there is no need in bothering with endianness, since both Baikal-T1
0028  * CPU and MMIO are LE.
0029  */
0030 static map_word __xipram bt1_rom_map_read(struct map_info *map,
0031                       unsigned long ofs)
0032 {
0033     void __iomem *src = map->virt + ofs;
0034     unsigned int shift;
0035     map_word ret;
0036     u32 data;
0037 
0038     /* Read data within offset dword. */
0039     shift = (uintptr_t)src & 0x3;
0040     data = readl_relaxed(src - shift);
0041     if (!shift) {
0042         ret.x[0] = data;
0043         return ret;
0044     }
0045     ret.x[0] = data >> (shift * BITS_PER_BYTE);
0046 
0047     /* Read data from the next dword. */
0048     shift = 4 - shift;
0049     if (ofs + shift >= map->size)
0050         return ret;
0051 
0052     data = readl_relaxed(src + shift);
0053     ret.x[0] |= data << (shift * BITS_PER_BYTE);
0054 
0055     return ret;
0056 }
0057 
0058 static void __xipram bt1_rom_map_copy_from(struct map_info *map,
0059                        void *to, unsigned long from,
0060                        ssize_t len)
0061 {
0062     void __iomem *src = map->virt + from;
0063     unsigned int shift, chunk;
0064     u32 data;
0065 
0066     if (len <= 0 || from >= map->size)
0067         return;
0068 
0069     /* Make sure we don't go over the map limit. */
0070     len = min_t(ssize_t, map->size - from, len);
0071 
0072     /*
0073      * Since requested data size can be pretty big we have to implement
0074      * the copy procedure as optimal as possible. That's why it's split
0075      * up into the next three stages: unaligned head, aligned body,
0076      * unaligned tail.
0077      */
0078     shift = (uintptr_t)src & 0x3;
0079     if (shift) {
0080         chunk = min_t(ssize_t, 4 - shift, len);
0081         data = readl_relaxed(src - shift);
0082         memcpy(to, (char *)&data + shift, chunk);
0083         src += chunk;
0084         to += chunk;
0085         len -= chunk;
0086     }
0087 
0088     while (len >= 4) {
0089         data = readl_relaxed(src);
0090         memcpy(to, &data, 4);
0091         src += 4;
0092         to += 4;
0093         len -= 4;
0094     }
0095 
0096     if (len) {
0097         data = readl_relaxed(src);
0098         memcpy(to, &data, len);
0099     }
0100 }
0101 
0102 int of_flash_probe_bt1_rom(struct platform_device *pdev,
0103                struct device_node *np,
0104                struct map_info *map)
0105 {
0106     struct device *dev = &pdev->dev;
0107 
0108     /* It's supposed to be read-only MTD. */
0109     if (!of_device_is_compatible(np, "mtd-rom")) {
0110         dev_info(dev, "No mtd-rom compatible string\n");
0111         return 0;
0112     }
0113 
0114     /* Multiplatform guard. */
0115     if (!of_device_is_compatible(np, "baikal,bt1-int-rom"))
0116         return 0;
0117 
0118     /* Sanity check the device parameters retrieved from DTB. */
0119     if (map->bankwidth != 4)
0120         dev_warn(dev, "Bank width is supposed to be 32 bits wide\n");
0121 
0122     map->read = bt1_rom_map_read;
0123     map->copy_from = bt1_rom_map_copy_from;
0124 
0125     return 0;
0126 }