| 1 |
|
| 2 |
|
| 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> |
| 8 |
|
| 9 |
uintptr_t get_uart_base(unsigned int idx) |
| 10 |
{ |
| 11 |
size_t num_ctrlrs; |
| 12 |
const struct soc_uart_ctrlr_info *ctrlr = soc_get_uart_ctrlr_info(&num_ctrlrs); |
| 13 |
|
| 14 |
if (idx >= num_ctrlrs) |
| 15 |
return 0; |
| 16 |
|
| 17 |
return ctrlr[idx].base; |
| 18 |
} |
| 19 |
|
| 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) |
| 22 |
{ |
| 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; |
| 28 |
} |
| 29 |
} |
| 30 |
return CB_ERR; |
| 31 |
} |
| 32 |
|
| 33 |
static enum cb_err get_uart_aoac_device(uintptr_t base, unsigned int *aoac_dev) |
| 34 |
{ |
| 35 |
unsigned int idx; |
| 36 |
size_t num_ctrlrs; |
| 37 |
const struct soc_uart_ctrlr_info *ctrlr = soc_get_uart_ctrlr_info(&num_ctrlrs); |
| 38 |
|
| 39 |
if (get_uart_idx(base, ctrlr, num_ctrlrs, &idx) == CB_ERR) |
| 40 |
return CB_ERR; |
| 41 |
|
| 42 |
*aoac_dev = ctrlr[idx].aoac_device; |
| 43 |
return CB_SUCCESS; |
| 44 |
} |
| 45 |
|
| 46 |
void set_uart_config(unsigned int idx) |
| 47 |
{ |
| 48 |
size_t num_ctrlrs; |
| 49 |
const struct soc_uart_ctrlr_info *ctrlr = soc_get_uart_ctrlr_info(&num_ctrlrs); |
| 50 |
|
| 51 |
if (idx >= num_ctrlrs) |
| 52 |
return; |
| 53 |
|
| 54 |
gpio_configure_pads(ctrlr[idx].mux, 2); |
| 55 |
} |
| 56 |
|
| 57 |
#if CONFIG(HAVE_ACPI_TABLES) |
| 58 |
static const char *uart_acpi_name(const struct device *dev) |
| 59 |
{ |
| 60 |
unsigned int idx; |
| 61 |
size_t num_ctrlrs; |
| 62 |
const struct soc_uart_ctrlr_info *ctrlr = soc_get_uart_ctrlr_info(&num_ctrlrs); |
| 63 |
|
| 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; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
static void uart_inject_ssdt(const struct device *dev) |
| 72 |
{ |
| 73 |
acpigen_write_scope(acpi_device_path(dev)); |
| 74 |
acpigen_write_store_int_to_namestr(acpi_device_status(dev), "STAT"); |
| 75 |
acpigen_pop_len(); |
| 76 |
} |
| 77 |
#endif |
| 78 |
|
| 79 |
|
| 80 |
static void uart_enable(struct device *dev) |
| 81 |
{ |
| 82 |
unsigned int dev_id; |
| 83 |
|
| 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; |
| 87 |
} |
| 88 |
|
| 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); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static void uart_read_resources(struct device *dev) |
| 98 |
{ |
| 99 |
mmio_range(dev, 0, dev->path.mmio.addr, 4 * KiB); |
| 100 |
} |
| 101 |
|
| 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 |
| 111 |
}; |