mb/google/nissa/var/rull: Configure Acoustic noise mitigation
[coreboot2.git] / src / soc / amd / common / block / uart / uart.c
blobca678fd59b4ab659a73e250104def4bde6fcb045
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <acpi/acpigen.h>
4 #include <amdblocks/aoac.h>
5 #include <amdblocks/gpio.h>
6 #include <amdblocks/uart.h>
7 #include <device/device.h>
9 uintptr_t get_uart_base(unsigned int idx)
11 size_t num_ctrlrs;
12 const struct soc_uart_ctrlr_info *ctrlr = soc_get_uart_ctrlr_info(&num_ctrlrs);
14 if (idx >= num_ctrlrs)
15 return 0;
17 return ctrlr[idx].base;
20 static enum cb_err get_uart_idx(uintptr_t base, const struct soc_uart_ctrlr_info *ctrlr,
21 size_t num_ctrlrs, unsigned int *idx)
23 unsigned int i;
24 for (i = 0; i < num_ctrlrs; i++) {
25 if (base == ctrlr[i].base) {
26 *idx = i;
27 return CB_SUCCESS;
30 return CB_ERR;
33 static enum cb_err get_uart_aoac_device(uintptr_t base, unsigned int *aoac_dev)
35 unsigned int idx;
36 size_t num_ctrlrs;
37 const struct soc_uart_ctrlr_info *ctrlr = soc_get_uart_ctrlr_info(&num_ctrlrs);
39 if (get_uart_idx(base, ctrlr, num_ctrlrs, &idx) == CB_ERR)
40 return CB_ERR;
42 *aoac_dev = ctrlr[idx].aoac_device;
43 return CB_SUCCESS;
46 void set_uart_config(unsigned int idx)
48 size_t num_ctrlrs;
49 const struct soc_uart_ctrlr_info *ctrlr = soc_get_uart_ctrlr_info(&num_ctrlrs);
51 if (idx >= num_ctrlrs)
52 return;
54 gpio_configure_pads(ctrlr[idx].mux, 2);
57 #if CONFIG(HAVE_ACPI_TABLES)
58 static const char *uart_acpi_name(const struct device *dev)
60 unsigned int idx;
61 size_t num_ctrlrs;
62 const struct soc_uart_ctrlr_info *ctrlr = soc_get_uart_ctrlr_info(&num_ctrlrs);
64 if (get_uart_idx(dev->path.mmio.addr, ctrlr, num_ctrlrs, &idx) == CB_SUCCESS)
65 return ctrlr[idx].acpi_name;
66 else
67 return NULL;
70 /* This gets called for enabled devices only. */
71 static void uart_inject_ssdt(const struct device *dev)
73 acpigen_write_scope(acpi_device_path(dev));
74 acpigen_write_store_int_to_namestr(acpi_device_status(dev), "STAT");
75 acpigen_pop_len(); /* Scope */
77 #endif
79 /* Even though this is called enable, it gets called for both enabled and disabled devices. */
80 static void uart_enable(struct device *dev)
82 unsigned int dev_id;
84 if (get_uart_aoac_device(dev->path.mmio.addr, &dev_id) == CB_ERR) {
85 printk(BIOS_ERR, "%s: Unknown device: %s\n", __func__, dev_path(dev));
86 return;
89 if (dev->enabled) {
90 power_on_aoac_device(dev_id);
91 wait_for_aoac_enabled(dev_id);
92 } else {
93 power_off_aoac_device(dev_id);
97 static void uart_read_resources(struct device *dev)
99 mmio_range(dev, 0, dev->path.mmio.addr, 4 * KiB);
102 struct device_operations amd_uart_mmio_ops = {
103 .read_resources = uart_read_resources,
104 .set_resources = noop_set_resources,
105 .scan_bus = scan_static_bus,
106 .enable = uart_enable,
107 #if CONFIG(HAVE_ACPI_TABLES)
108 .acpi_name = uart_acpi_name,
109 .acpi_fill_ssdt = uart_inject_ssdt,
110 #endif