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
)
12 const struct soc_uart_ctrlr_info
*ctrlr
= soc_get_uart_ctrlr_info(&num_ctrlrs
);
14 if (idx
>= num_ctrlrs
)
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
)
24 for (i
= 0; i
< num_ctrlrs
; i
++) {
25 if (base
== ctrlr
[i
].base
) {
33 static enum cb_err
get_uart_aoac_device(uintptr_t base
, unsigned int *aoac_dev
)
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
)
42 *aoac_dev
= ctrlr
[idx
].aoac_device
;
46 void set_uart_config(unsigned int idx
)
49 const struct soc_uart_ctrlr_info
*ctrlr
= soc_get_uart_ctrlr_info(&num_ctrlrs
);
51 if (idx
>= num_ctrlrs
)
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
)
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
;
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 */
79 /* Even though this is called enable, it gets called for both enabled and disabled devices. */
80 static void uart_enable(struct device
*dev
)
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
));
90 power_on_aoac_device(dev_id
);
91 wait_for_aoac_enabled(dev_id
);
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
,