Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com> |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 4 | * (C) Copyright 2021 Asherah Connor <ashe@kivikakk.ee> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_QFW |
| 8 | |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 9 | #include <common.h> |
Heinrich Schuchardt | c659ac7 | 2023-11-09 09:23:02 -0800 | [diff] [blame] | 10 | #include <acpi/acpi_table.h> |
Simon Glass | dd4bd9a | 2023-01-28 15:00:24 -0700 | [diff] [blame] | 11 | #include <bootdev.h> |
| 12 | #include <bootflow.h> |
| 13 | #include <bootmeth.h> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 14 | #include <command.h> |
| 15 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 17 | #include <malloc.h> |
Miao Yan | 1868659 | 2016-05-22 19:37:17 -0700 | [diff] [blame] | 18 | #include <qfw.h> |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 19 | #include <dm.h> |
| 20 | #include <misc.h> |
Simon Glass | 0679cca | 2021-12-01 09:02:40 -0700 | [diff] [blame] | 21 | #include <tables_csum.h> |
Simon Glass | 854624c | 2023-07-15 21:38:50 -0600 | [diff] [blame] | 22 | #include <asm/acpi_table.h> |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 23 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 24 | static void qfw_read_entry_io(struct qfw_dev *qdev, u16 entry, u32 size, |
| 25 | void *address) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 26 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 27 | struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 28 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 29 | 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 Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 35 | static void qfw_read_entry_dma(struct qfw_dev *qdev, u16 entry, u32 size, |
| 36 | void *address) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 37 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 38 | struct dm_qfw_ops *ops = dm_qfw_get_ops(qdev->dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 39 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 40 | 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 Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 47 | * writing FW_CFG_INVALID will cause read operation to resume at last |
| 48 | * offset, otherwise read will start at offset 0 |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 49 | */ |
| 50 | if (entry != FW_CFG_INVALID) |
| 51 | dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16)); |
| 52 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 53 | debug("%s: entry 0x%x, size %u address %p, control 0x%x\n", __func__, |
Miao Yan | 2e82e74 | 2016-05-22 19:37:15 -0700 | [diff] [blame] | 54 | entry, size, address, be32_to_cpu(dma.control)); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 55 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 56 | barrier(); |
| 57 | |
| 58 | ops->read_entry_dma(qdev->dev, &dma); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 61 | void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 62 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 63 | struct qfw_dev *qdev = dev_get_uclass_priv(dev); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 64 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 65 | if (qdev->dma_present) |
| 66 | qfw_read_entry_dma(qdev, entry, size, address); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 67 | else |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 68 | qfw_read_entry_io(qdev, entry, size, address); |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 71 | int qfw_register(struct udevice *dev) |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 72 | { |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 73 | struct qfw_dev *qdev = dev_get_uclass_priv(dev); |
| 74 | u32 qemu, dma_enabled; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 75 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 76 | 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 Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 81 | return -ENODEV; |
| 82 | |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 83 | 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 Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 86 | |
| 87 | return 0; |
Miao Yan | fcf5c04 | 2016-05-22 19:37:14 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Simon Glass | dd4bd9a | 2023-01-28 15:00:24 -0700 | [diff] [blame] | 90 | static 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 | |
| 101 | static 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 | |
| 129 | static 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 | |
| 138 | static 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 Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 149 | UCLASS_DRIVER(qfw) = { |
| 150 | .id = UCLASS_QFW, |
| 151 | .name = "qfw", |
Simon Glass | dd4bd9a | 2023-01-28 15:00:24 -0700 | [diff] [blame] | 152 | .post_bind = qfw_post_bind, |
Asherah Connor | 5b0b43e | 2021-03-19 18:21:40 +1100 | [diff] [blame] | 153 | .per_device_auto = sizeof(struct qfw_dev), |
| 154 | }; |
Simon Glass | dd4bd9a | 2023-01-28 15:00:24 -0700 | [diff] [blame] | 155 | |
| 156 | struct bootdev_ops qfw_bootdev_ops = { |
| 157 | .get_bootflow = qfw_get_bootflow, |
| 158 | }; |
| 159 | |
| 160 | static const struct udevice_id qfw_bootdev_ids[] = { |
| 161 | { .compatible = "u-boot,bootdev-qfw" }, |
| 162 | { } |
| 163 | }; |
| 164 | |
| 165 | U_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 | |
| 173 | BOOTDEV_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 | }; |