blob: db98619fdf53c9b74dc3d0c3c739c4fdd974d30d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Miao Yanfcf5c042016-05-22 19:37:14 -07002/*
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
Asherah Connor5b0b43e2021-03-19 18:21:40 +11004 * (C) Copyright 2021 Asherah Connor <ashe@kivikakk.ee>
Miao Yanfcf5c042016-05-22 19:37:14 -07005 */
6
Asherah Connor5b0b43e2021-03-19 18:21:40 +11007#define LOG_CATEGORY UCLASS_QFW
8
Miao Yanfcf5c042016-05-22 19:37:14 -07009#include <common.h>
Heinrich Schuchardtc659ac72023-11-09 09:23:02 -080010#include <acpi/acpi_table.h>
Simon Glassdd4bd9a2023-01-28 15:00:24 -070011#include <bootdev.h>
12#include <bootflow.h>
13#include <bootmeth.h>
Miao Yanfcf5c042016-05-22 19:37:14 -070014#include <command.h>
15#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Miao Yanfcf5c042016-05-22 19:37:14 -070017#include <malloc.h>
Miao Yan18686592016-05-22 19:37:17 -070018#include <qfw.h>
Asherah Connor5b0b43e2021-03-19 18:21:40 +110019#include <dm.h>
20#include <misc.h>
Simon Glass0679cca2021-12-01 09:02:40 -070021#include <tables_csum.h>
Simon Glass854624c2023-07-15 21:38:50 -060022#include <asm/acpi_table.h>
Miao Yanfcf5c042016-05-22 19:37:14 -070023
Asherah Connor5b0b43e2021-03-19 18:21:40 +110024static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size,
25 void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -070026{
Asherah Connor5b0b43e2021-03-19 18:21:40 +110027 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yanfcf5c042016-05-22 19:37:14 -070028
Asherah Connor5b0b43e2021-03-19 18:21:40 +110029 debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size,
30 address);
31
32 ops->read_entry_io(qdev->dev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -070033}
34
Asherah Connor5b0b43e2021-03-19 18:21:40 +110035static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size,
36 void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -070037{
Asherah Connor5b0b43e2021-03-19 18:21:40 +110038 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yanfcf5c042016-05-22 19:37:14 -070039
Asherah Connor5b0b43e2021-03-19 18:21:40 +110040 struct qfw_dma dma = {
41 .length = cpu_to_be32(size),
42 .address = cpu_to_be64((uintptr_t)address),
43 .control = cpu_to_be32(FW_CFG_DMA_READ),
44 };
Miao Yanfcf5c042016-05-22 19:37:14 -070045
46 /*
Asherah Connor5b0b43e2021-03-19 18:21:40 +110047 * writing FW_CFG_INVALID will cause read operation to resume at last
48 * offset, otherwise read will start at offset 0
Miao Yanfcf5c042016-05-22 19:37:14 -070049 */
50 if (entry != FW_CFG_INVALID)
51 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
52
Asherah Connor5b0b43e2021-03-19 18:21:40 +110053 debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__,
Miao Yan2e82e742016-05-22 19:37:15 -070054 entry, size, address, be32_to_cpu(dma.control));
Miao Yanfcf5c042016-05-22 19:37:14 -070055
Asherah Connor5b0b43e2021-03-19 18:21:40 +110056 barrier();
57
58 ops->read_entry_dma(qdev->dev, &dma);
Miao Yanfcf5c042016-05-22 19:37:14 -070059}
60
Asherah Connor5b0b43e2021-03-19 18:21:40 +110061void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address)
Miao Yanfcf5c042016-05-22 19:37:14 -070062{
Asherah Connor5b0b43e2021-03-19 18:21:40 +110063 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
Miao Yanfcf5c042016-05-22 19:37:14 -070064
Asherah Connor5b0b43e2021-03-19 18:21:40 +110065 if (qdev->dma_present)
66 qfw_read_entry_dma(qdev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -070067 else
Asherah Connor5b0b43e2021-03-19 18:21:40 +110068 qfw_read_entry_io(qdev, entry, size, address);
Miao Yanfcf5c042016-05-22 19:37:14 -070069}
70
Asherah Connor5b0b43e2021-03-19 18:21:40 +110071int qfw_register(struct udevice *dev)
Miao Yanfcf5c042016-05-22 19:37:14 -070072{
Asherah Connor5b0b43e2021-03-19 18:21:40 +110073 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
74 u32 qemu, dma_enabled;
Miao Yanfcf5c042016-05-22 19:37:14 -070075
Asherah Connor5b0b43e2021-03-19 18:21:40 +110076 qdev->dev = dev;
77 INIT_LIST_HEAD(&qdev->fw_list);
78
79 qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu);
80 if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE)
Miao Yanfcf5c042016-05-22 19:37:14 -070081 return -ENODEV;
82
Asherah Connor5b0b43e2021-03-19 18:21:40 +110083 qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled);
84 if (dma_enabled & FW_CFG_DMA_ENABLED)
85 qdev->dma_present = true;
Miao Yanfcf5c042016-05-22 19:37:14 -070086
87 return 0;
Miao Yanfcf5c042016-05-22 19:37:14 -070088}
89
Simon Glassdd4bd9a2023-01-28 15:00:24 -070090static int qfw_post_bind(struct udevice *dev)
91{
92 int ret;
93
94 ret = bootdev_setup_for_dev(dev, "qfw_bootdev");
95 if (ret)
96 return log_msg_ret("dev", ret);
97
98 return 0;
99}
100
101static int qfw_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
102 struct bootflow *bflow)
103{
104 const struct udevice *media = dev_get_parent(dev);
105 int ret;
106
107 if (!CONFIG_IS_ENABLED(BOOTSTD))
108 return -ENOSYS;
109
110 log_debug("media=%s\n", media->name);
111 ret = bootmeth_check(bflow->method, iter);
112 if (ret)
113 return log_msg_ret("check", ret);
114
115 log_debug("iter->part=%d\n", iter->part);
116
117 /* We only support the whole device, not partitions */
118 if (iter->part)
119 return log_msg_ret("max", -ESHUTDOWN);
120
121 log_debug("reading bootflow with method: %s\n", bflow->method->name);
122 ret = bootmeth_read_bootflow(bflow->method, bflow);
123 if (ret)
124 return log_msg_ret("method", ret);
125
126 return 0;
127}
128
129static int qfw_bootdev_bind(struct udevice *dev)
130{
131 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
132
133 ucp->prio = BOOTDEVP_4_SCAN_FAST;
134
135 return 0;
136}
137
138static int qfw_bootdev_hunt(struct bootdev_hunter *info, bool show)
139{
140 int ret;
141
142 ret = uclass_probe_all(UCLASS_QFW);
143 if (ret && ret != -ENOENT)
144 return log_msg_ret("vir", ret);
145
146 return 0;
147}
148
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100149UCLASS_DRIVER(qfw) = {
150 .id = UCLASS_QFW,
151 .name = "qfw",
Simon Glassdd4bd9a2023-01-28 15:00:24 -0700152 .post_bind = qfw_post_bind,
Asherah Connor5b0b43e2021-03-19 18:21:40 +1100153 .per_device_auto = sizeof(struct qfw_dev),
154};
Simon Glassdd4bd9a2023-01-28 15:00:24 -0700155
156struct bootdev_ops qfw_bootdev_ops = {
157 .get_bootflow = qfw_get_bootflow,
158};
159
160static const struct udevice_id qfw_bootdev_ids[] = {
161 { .compatible = "u-boot,bootdev-qfw" },
162 { }
163};
164
165U_BOOT_DRIVER(qfw_bootdev) = {
166 .name = "qfw_bootdev",
167 .id = UCLASS_BOOTDEV,
168 .ops = &qfw_bootdev_ops,
169 .bind = qfw_bootdev_bind,
170 .of_match = qfw_bootdev_ids,
171};
172
173BOOTDEV_HUNTER(qfw_bootdev_hunter) = {
174 .prio = BOOTDEVP_4_SCAN_FAST,
175 .uclass = UCLASS_QFW,
176 .hunt = qfw_bootdev_hunt,
177 .drv = DM_DRIVER_REF(qfw_bootdev),
178};