blob: 229938db3806b0e0ad6ac0baf442b962c62936bb [file] [log] [blame]
Abdellatif El Khlifi857360c2023-04-17 10:11:52 +01001// 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 Khlifi857360c2023-04-17 10:11:52 +01009#include <dm.h>
10#include <log.h>
11#include <mapmem.h>
12#include <asm/io.h>
13#include <linux/bitops.h>
14#include <linux/errno.h>
15#include "nvmxip.h"
16
17/**
Abdellatif El Khlifi857360c2023-04-17 10:11:52 +010018 * 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 */
30static 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 Vasutd0489322023-08-23 02:18:18 +020034 /* number of bytes to read */
35 u32 size = blkcnt * desc->blksz;
Abdellatif El Khlifi857360c2023-04-17 10:11:52 +010036 /* physical address of the first block to read */
37 phys_addr_t blkaddr = plat->phys_base + blknr * desc->blksz;
Marek Vasutd0489322023-08-23 02:18:18 +020038 void *virt_blkaddr;
Abdellatif El Khlifi857360c2023-04-17 10:11:52 +010039 uint qdata_idx;
40
Marek Vasutd0489322023-08-23 02:18:18 +020041 if (!buffer)
Abdellatif El Khlifi857360c2023-04-17 10:11:52 +010042 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 Vasutd0489322023-08-23 02:18:18 +020050#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 Khlifi857360c2023-04-17 10:11:52 +010057 log_debug("[%s]: src[0]: 0x%llx , dst[0]: 0x%llx , src[-1]: 0x%llx , dst[-1]: 0x%llx\n",
58 dev->name,
Marek Vasutd0489322023-08-23 02:18:18 +020059 *(u64 *)virt_blkaddr,
Abdellatif El Khlifi857360c2023-04-17 10:11:52 +010060 *(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 */
79static 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
95static const struct blk_ops nvmxip_blk_ops = {
96 .read = nvmxip_blk_read,
97};
98
99U_BOOT_DRIVER(nvmxip_blk) = {
100 .name = NVMXIP_BLKDRV_NAME,
101 .id = UCLASS_BLK,
102 .probe = nvmxip_blk_probe,
103 .ops = &nvmxip_blk_ops,
104};