Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 4 | * |
| 5 | * Authors: |
| 6 | * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 7 | */ |
| 8 | |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <log.h> |
| 11 | #include <mapmem.h> |
Heinrich Schuchardt | ab397a5 | 2024-07-15 15:19:50 +0200 | [diff] [blame] | 12 | #include <nvmxip.h> |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <linux/bitops.h> |
| 15 | #include <linux/errno.h> |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 16 | |
| 17 | /** |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 18 | * nvmxip_blk_read() - block device read operation |
| 19 | * @dev: the block device |
| 20 | * @blknr: first block number to read from |
| 21 | * @blkcnt: number of blocks to read |
| 22 | * @buffer: destination buffer |
| 23 | * |
| 24 | * Read data from the block storage device. |
| 25 | * |
| 26 | * Return: |
| 27 | * |
| 28 | * number of blocks read on success. Otherwise, failure |
| 29 | */ |
| 30 | static ulong nvmxip_blk_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) |
| 31 | { |
| 32 | struct nvmxip_plat *plat = dev_get_plat(dev->parent); |
| 33 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Marek Vasut | d048932 | 2023-08-23 02:18:18 +0200 | [diff] [blame] | 34 | /* number of bytes to read */ |
| 35 | u32 size = blkcnt * desc->blksz; |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 36 | /* physical address of the first block to read */ |
| 37 | phys_addr_t blkaddr = plat->phys_base + blknr * desc->blksz; |
Marek Vasut | d048932 | 2023-08-23 02:18:18 +0200 | [diff] [blame] | 38 | void *virt_blkaddr; |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 39 | uint qdata_idx; |
| 40 | |
Marek Vasut | d048932 | 2023-08-23 02:18:18 +0200 | [diff] [blame] | 41 | if (!buffer) |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 42 | return -EINVAL; |
| 43 | |
| 44 | log_debug("[%s]: reading from blknr: %lu , blkcnt: %lu\n", dev->name, blknr, blkcnt); |
| 45 | |
| 46 | virt_blkaddr = map_sysmem(blkaddr, 0); |
| 47 | |
| 48 | /* assumption: the data is virtually contiguous */ |
| 49 | |
Marek Vasut | d048932 | 2023-08-23 02:18:18 +0200 | [diff] [blame] | 50 | #if IS_ENABLED(CONFIG_PHYS_64BIT) |
| 51 | for (qdata_idx = 0 ; qdata_idx < size; qdata_idx += sizeof(u64)) |
| 52 | *(u64 *)(buffer + qdata_idx) = readq(virt_blkaddr + qdata_idx); |
| 53 | #else |
| 54 | for (qdata_idx = 0 ; qdata_idx < size; qdata_idx += sizeof(u32)) |
| 55 | *(u32 *)(buffer + qdata_idx) = readl(virt_blkaddr + qdata_idx); |
| 56 | #endif |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 57 | log_debug("[%s]: src[0]: 0x%llx , dst[0]: 0x%llx , src[-1]: 0x%llx , dst[-1]: 0x%llx\n", |
| 58 | dev->name, |
Marek Vasut | d048932 | 2023-08-23 02:18:18 +0200 | [diff] [blame] | 59 | *(u64 *)virt_blkaddr, |
Abdellatif El Khlifi | 857360c | 2023-04-17 10:11:52 +0100 | [diff] [blame] | 60 | *(u64 *)buffer, |
| 61 | *(u64 *)((u8 *)virt_blkaddr + desc->blksz * blkcnt - sizeof(u64)), |
| 62 | *(u64 *)((u8 *)buffer + desc->blksz * blkcnt - sizeof(u64))); |
| 63 | |
| 64 | unmap_sysmem(virt_blkaddr); |
| 65 | |
| 66 | return blkcnt; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * nvmxip_blk_probe() - block storage device probe |
| 71 | * @dev: the block storage device |
| 72 | * |
| 73 | * Initialize the block storage descriptor. |
| 74 | * |
| 75 | * Return: |
| 76 | * |
| 77 | * Always return 0. |
| 78 | */ |
| 79 | static int nvmxip_blk_probe(struct udevice *dev) |
| 80 | { |
| 81 | struct nvmxip_plat *plat = dev_get_plat(dev->parent); |
| 82 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 83 | |
| 84 | desc->lba = plat->lba; |
| 85 | desc->log2blksz = plat->lba_shift; |
| 86 | desc->blksz = BIT(plat->lba_shift); |
| 87 | desc->bdev = dev; |
| 88 | |
| 89 | log_debug("[%s]: block storage layout\n lbas: %lu , log2blksz: %d, blksz: %lu\n", |
| 90 | dev->name, desc->lba, desc->log2blksz, desc->blksz); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static const struct blk_ops nvmxip_blk_ops = { |
| 96 | .read = nvmxip_blk_read, |
| 97 | }; |
| 98 | |
| 99 | U_BOOT_DRIVER(nvmxip_blk) = { |
| 100 | .name = NVMXIP_BLKDRV_NAME, |
| 101 | .id = UCLASS_BLK, |
| 102 | .probe = nvmxip_blk_probe, |
| 103 | .ops = &nvmxip_blk_ops, |
| 104 | }; |