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 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <log.h> |
| 12 | #include <mapmem.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <linux/bitops.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include "nvmxip.h" |
| 17 | |
| 18 | /** |
| 19 | * nvmxip_mmio_rawread() - read from the XIP flash |
| 20 | * @address: address of the data |
| 21 | * @value: pointer to where storing the value read |
| 22 | * |
| 23 | * Read raw data from the XIP flash. |
| 24 | * |
| 25 | * Return: |
| 26 | * |
| 27 | * Always return 0. |
| 28 | */ |
| 29 | static int nvmxip_mmio_rawread(const phys_addr_t address, u64 *value) |
| 30 | { |
| 31 | *value = readq(address); |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * nvmxip_blk_read() - block device read operation |
| 37 | * @dev: the block device |
| 38 | * @blknr: first block number to read from |
| 39 | * @blkcnt: number of blocks to read |
| 40 | * @buffer: destination buffer |
| 41 | * |
| 42 | * Read data from the block storage device. |
| 43 | * |
| 44 | * Return: |
| 45 | * |
| 46 | * number of blocks read on success. Otherwise, failure |
| 47 | */ |
| 48 | static ulong nvmxip_blk_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) |
| 49 | { |
| 50 | struct nvmxip_plat *plat = dev_get_plat(dev->parent); |
| 51 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 52 | /* number of the u64 words to read */ |
| 53 | u32 qwords = (blkcnt * desc->blksz) / sizeof(u64); |
| 54 | /* physical address of the first block to read */ |
| 55 | phys_addr_t blkaddr = plat->phys_base + blknr * desc->blksz; |
| 56 | u64 *virt_blkaddr; |
| 57 | u64 *pdst = buffer; |
| 58 | uint qdata_idx; |
| 59 | |
| 60 | if (!pdst) |
| 61 | return -EINVAL; |
| 62 | |
| 63 | log_debug("[%s]: reading from blknr: %lu , blkcnt: %lu\n", dev->name, blknr, blkcnt); |
| 64 | |
| 65 | virt_blkaddr = map_sysmem(blkaddr, 0); |
| 66 | |
| 67 | /* assumption: the data is virtually contiguous */ |
| 68 | |
| 69 | for (qdata_idx = 0 ; qdata_idx < qwords ; qdata_idx++) |
| 70 | nvmxip_mmio_rawread((phys_addr_t)(virt_blkaddr + qdata_idx), pdst++); |
| 71 | |
| 72 | log_debug("[%s]: src[0]: 0x%llx , dst[0]: 0x%llx , src[-1]: 0x%llx , dst[-1]: 0x%llx\n", |
| 73 | dev->name, |
| 74 | *virt_blkaddr, |
| 75 | *(u64 *)buffer, |
| 76 | *(u64 *)((u8 *)virt_blkaddr + desc->blksz * blkcnt - sizeof(u64)), |
| 77 | *(u64 *)((u8 *)buffer + desc->blksz * blkcnt - sizeof(u64))); |
| 78 | |
| 79 | unmap_sysmem(virt_blkaddr); |
| 80 | |
| 81 | return blkcnt; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * nvmxip_blk_probe() - block storage device probe |
| 86 | * @dev: the block storage device |
| 87 | * |
| 88 | * Initialize the block storage descriptor. |
| 89 | * |
| 90 | * Return: |
| 91 | * |
| 92 | * Always return 0. |
| 93 | */ |
| 94 | static int nvmxip_blk_probe(struct udevice *dev) |
| 95 | { |
| 96 | struct nvmxip_plat *plat = dev_get_plat(dev->parent); |
| 97 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 98 | |
| 99 | desc->lba = plat->lba; |
| 100 | desc->log2blksz = plat->lba_shift; |
| 101 | desc->blksz = BIT(plat->lba_shift); |
| 102 | desc->bdev = dev; |
| 103 | |
| 104 | log_debug("[%s]: block storage layout\n lbas: %lu , log2blksz: %d, blksz: %lu\n", |
| 105 | dev->name, desc->lba, desc->log2blksz, desc->blksz); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static const struct blk_ops nvmxip_blk_ops = { |
| 111 | .read = nvmxip_blk_read, |
| 112 | }; |
| 113 | |
| 114 | U_BOOT_DRIVER(nvmxip_blk) = { |
| 115 | .name = NVMXIP_BLKDRV_NAME, |
| 116 | .id = UCLASS_BLK, |
| 117 | .probe = nvmxip_blk_probe, |
| 118 | .ops = &nvmxip_blk_ops, |
| 119 | }; |