blob: 0e002ac25f48180d103ecbd982062a0dd3e54dd7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Miao Yan4fcd7f22016-05-22 19:37:14 -07002/*
3 * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
Asherah Connor4ffa95d2021-03-19 18:21:40 +11004 * (C) Copyright 2021 Asherah Connor <ashe@kivikakk.ee>
Miao Yan4fcd7f22016-05-22 19:37:14 -07005 */
6
Asherah Connor4ffa95d2021-03-19 18:21:40 +11007#define LOG_CATEGORY UCLASS_QFW
8
Heinrich Schuchardt2c1fd4a2023-11-09 09:23:02 -08009#include <acpi/acpi_table.h>
Simon Glass77ca5ff2023-01-28 15:00:24 -070010#include <bootdev.h>
11#include <bootflow.h>
12#include <bootmeth.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070013#include <command.h>
14#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070016#include <malloc.h>
Miao Yan92106272016-05-22 19:37:17 -070017#include <qfw.h>
Asherah Connor4ffa95d2021-03-19 18:21:40 +110018#include <dm.h>
19#include <misc.h>
Simon Glassf3425cb2021-12-01 09:02:40 -070020#include <tables_csum.h>
Simon Glass3fed4672023-07-15 21:38:50 -060021#include <asm/acpi_table.h>
Miao Yan4fcd7f22016-05-22 19:37:14 -070022
Asherah Connor4ffa95d2021-03-19 18:21:40 +110023static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size,
24 void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -070025{
Asherah Connor4ffa95d2021-03-19 18:21:40 +110026 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -070027
Asherah Connor4ffa95d2021-03-19 18:21:40 +110028 debug("%s: entry 0x%x, size %u address %p\n", __func__, entry, size,
29 address);
30
31 ops->read_entry_io(qdev->dev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -070032}
33
Asherah Connor4ffa95d2021-03-19 18:21:40 +110034static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size,
35 void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -070036{
Asherah Connor4ffa95d2021-03-19 18:21:40 +110037 struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -070038
Asherah Connor4ffa95d2021-03-19 18:21:40 +110039 struct qfw_dma dma = {
40 .length = cpu_to_be32(size),
41 .address = cpu_to_be64((uintptr_t)address),
42 .control = cpu_to_be32(FW_CFG_DMA_READ),
43 };
Miao Yan4fcd7f22016-05-22 19:37:14 -070044
45 /*
Asherah Connor4ffa95d2021-03-19 18:21:40 +110046 * writing FW_CFG_INVALID will cause read operation to resume at last
47 * offset, otherwise read will start at offset 0
Miao Yan4fcd7f22016-05-22 19:37:14 -070048 */
49 if (entry != FW_CFG_INVALID)
50 dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
51
Asherah Connor4ffa95d2021-03-19 18:21:40 +110052 debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__,
Miao Yan8a153832016-05-22 19:37:15 -070053 entry, size, address, be32_to_cpu(dma.control));
Miao Yan4fcd7f22016-05-22 19:37:14 -070054
Asherah Connor4ffa95d2021-03-19 18:21:40 +110055 barrier();
Miao Yan4fcd7f22016-05-22 19:37:14 -070056
Asherah Connor4ffa95d2021-03-19 18:21:40 +110057 ops->read_entry_dma(qdev->dev, &dma);
Miao Yan4fcd7f22016-05-22 19:37:14 -070058}
59
Asherah Connor4ffa95d2021-03-19 18:21:40 +110060void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address)
Miao Yan4fcd7f22016-05-22 19:37:14 -070061{
Asherah Connor4ffa95d2021-03-19 18:21:40 +110062 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
Miao Yan4fcd7f22016-05-22 19:37:14 -070063
Asherah Connor4ffa95d2021-03-19 18:21:40 +110064 if (qdev->dma_present)
65 qfw_read_entry_dma(qdev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -070066 else
Asherah Connor4ffa95d2021-03-19 18:21:40 +110067 qfw_read_entry_io(qdev, entry, size, address);
Miao Yan4fcd7f22016-05-22 19:37:14 -070068}
69
Asherah Connor4ffa95d2021-03-19 18:21:40 +110070int qfw_register(struct udevice *dev)
Miao Yan4fcd7f22016-05-22 19:37:14 -070071{
Asherah Connor4ffa95d2021-03-19 18:21:40 +110072 struct qfw_dev *qdev = dev_get_uclass_priv(dev);
73 u32 qemu, dma_enabled;
Miao Yan4fcd7f22016-05-22 19:37:14 -070074
Asherah Connor4ffa95d2021-03-19 18:21:40 +110075 qdev->dev = dev;
76 INIT_LIST_HEAD(&qdev->fw_list);
Miao Yan4fcd7f22016-05-22 19:37:14 -070077
Asherah Connor4ffa95d2021-03-19 18:21:40 +110078 qfw_read_entry_io(qdev, FW_CFG_SIGNATURE, 4, &qemu);
79 if (be32_to_cpu(qemu) != QEMU_FW_CFG_SIGNATURE)
80 return -ENODEV;
Miao Yan4fcd7f22016-05-22 19:37:14 -070081
Asherah Connor4ffa95d2021-03-19 18:21:40 +110082 qfw_read_entry_io(qdev, FW_CFG_ID, 1, &dma_enabled);
83 if (dma_enabled & FW_CFG_DMA_ENABLED)
84 qdev->dma_present = true;
Miao Yan4fcd7f22016-05-22 19:37:14 -070085
86 return 0;
Miao Yan4fcd7f22016-05-22 19:37:14 -070087}
88
Simon Glass77ca5ff2023-01-28 15:00:24 -070089static int qfw_post_bind(struct udevice *dev)
90{
91 int ret;
92
93 ret = bootdev_setup_for_dev(dev, "qfw_bootdev");
94 if (ret)
95 return log_msg_ret("dev", ret);
96
97 return 0;
98}
99
100static int qfw_get_bootflow(struct udevice *dev, struct bootflow_iter *iter,
101 struct bootflow *bflow)
102{
103 const struct udevice *media = dev_get_parent(dev);
104 int ret;
105
106 if (!CONFIG_IS_ENABLED(BOOTSTD))
107 return -ENOSYS;
108
109 log_debug("media=%s\n", media->name);
110 ret = bootmeth_check(bflow->method, iter);
111 if (ret)
112 return log_msg_ret("check", ret);
113
114 log_debug("iter->part=%d\n", iter->part);
115
116 /* We only support the whole device, not partitions */
117 if (iter->part)
118 return log_msg_ret("max", -ESHUTDOWN);
119
120 log_debug("reading bootflow with method: %s\n", bflow->method->name);
121 ret = bootmeth_read_bootflow(bflow->method, bflow);
122 if (ret)
123 return log_msg_ret("method", ret);
124
125 return 0;
126}
127
128static int qfw_bootdev_bind(struct udevice *dev)
129{
130 struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
131
132 ucp->prio = BOOTDEVP_4_SCAN_FAST;
133
134 return 0;
135}
136
137static int qfw_bootdev_hunt(struct bootdev_hunter *info, bool show)
138{
139 int ret;
140
141 ret = uclass_probe_all(UCLASS_QFW);
142 if (ret && ret != -ENOENT)
143 return log_msg_ret("vir", ret);
144
145 return 0;
146}
147
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100148UCLASS_DRIVER(qfw) = {
149 .id = UCLASS_QFW,
150 .name = "qfw",
Simon Glass77ca5ff2023-01-28 15:00:24 -0700151 .post_bind = qfw_post_bind,
Asherah Connor4ffa95d2021-03-19 18:21:40 +1100152 .per_device_auto = sizeof(struct qfw_dev),
153};
Simon Glass77ca5ff2023-01-28 15:00:24 -0700154
155struct bootdev_ops qfw_bootdev_ops = {
156 .get_bootflow = qfw_get_bootflow,
157};
158
159static const struct udevice_id qfw_bootdev_ids[] = {
160 { .compatible = "u-boot,bootdev-qfw" },
161 { }
162};
163
164U_BOOT_DRIVER(qfw_bootdev) = {
165 .name = "qfw_bootdev",
166 .id = UCLASS_BOOTDEV,
167 .ops = &qfw_bootdev_ops,
168 .bind = qfw_bootdev_bind,
169 .of_match = qfw_bootdev_ids,
170};
171
172BOOTDEV_HUNTER(qfw_bootdev_hunter) = {
173 .prio = BOOTDEVP_4_SCAN_FAST,
174 .uclass = UCLASS_QFW,
175 .hunt = qfw_bootdev_hunt,
176 .drv = DM_DRIVER_REF(qfw_bootdev),
177};