aboutsummaryrefslogtreecommitdiff
path: root/drivers/serial/serial_coreboot.c
blob: b1f69f6998cfa1fc6263a087a958509016902601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: GPL-2.0+
/*
 * UART support for U-Boot when launched from Coreboot
 *
 * Copyright 2019 Google LLC
 */

#define LOG_CATGEGORY	UCLASS_SERIAL

#include <dm.h>
#include <log.h>
#include <ns16550.h>
#include <serial.h>
#include <acpi/acpi_table.h>
#include <asm/cb_sysinfo.h>

DECLARE_GLOBAL_DATA_PTR;

static int read_dbg2(struct ns16550_plat *plat)
{
	struct acpi_table_header *tab;
	struct acpi_dbg2_header *hdr;
	struct acpi_dbg2_device *dbg;
	struct acpi_gen_regaddr *addr;
	u32 *addr_size;

	log_debug("Looking for DBG2 in ACPI tables\n");
	if (!gd->acpi_start) {
		log_debug("No ACPI tables\n");
		return -ENOENT;
	}

	tab = acpi_find_table("DBG2");
	if (!tab) {
		log_debug("No DBG2 table\n");
		return -ENOENT;
	}
	hdr = container_of(tab, struct acpi_dbg2_header, header);

	/* We only use the first device, but check that there is at least one */
	if (!hdr->devices_count) {
		log_debug("No devices\n");
		return -ENOENT;
	}
	if (hdr->devices_offset >= tab->length) {
		log_debug("Invalid offset\n");
		return -EINVAL;
	}
	dbg = (void *)hdr + hdr->devices_offset;
	if (dbg->revision) {
		log_debug("Invalid revision %d\n", dbg->revision);
		return -EINVAL;
	}
	if (!dbg->address_count) {
		log_debug("No addresses\n");
		return -EINVAL;
	}
	if (dbg->port_type != ACPI_DBG2_SERIAL_PORT) {
		log_debug("Not a serial port\n");
		return -EPROTOTYPE;
	}
	if (dbg->port_subtype != ACPI_DBG2_16550_COMPATIBLE) {
		log_debug("Incompatible serial port\n");
		return -EPROTOTYPE;
	}
	if (dbg->base_address_offset >= dbg->length ||
	    dbg->address_size_offset >= dbg->length) {
		log_debug("Invalid base address/size offsets %d, %d\n",
			  dbg->base_address_offset, dbg->address_size_offset);
		return -EINVAL;
	}
	addr_size = (void *)dbg + dbg->address_size_offset;
	if (!*addr_size) {
		log_debug("Zero address size\n");
		return -EINVAL;
	}
	addr = (void *)dbg + dbg->base_address_offset;
	if (addr->space_id != ACPI_ADDRESS_SPACE_MEMORY) {
		log_debug("Incompatible space %d\n", addr->space_id);
		return -EPROTOTYPE;
	}

	plat->base = addr->addrl;

	/* ACPI_ACCESS_SIZE_DWORD_ACCESS is 3; we want 2 */
	plat->reg_shift = addr->access_size - 1;
	plat->reg_width = 4; /* coreboot sets bit_width to 0 */
	plat->clock = 1843200;
	plat->fcr = UART_FCR_DEFVAL;
	plat->flags = 0;
	log_debug("Collected UART from ACPI DBG2 table\n");

	return 0;
}

static int coreboot_of_to_plat(struct udevice *dev)
{
	struct ns16550_plat *plat = dev_get_plat(dev);
	struct cb_serial *cb_info = lib_sysinfo.serial;
	int ret = -ENOENT;

	if (cb_info) {
		plat->base = cb_info->baseaddr;
		plat->reg_shift = cb_info->regwidth == 4 ? 2 : 0;
		plat->reg_width = cb_info->regwidth;
		plat->clock = cb_info->input_hertz;
		plat->fcr = UART_FCR_DEFVAL;
		plat->flags = 0;
		if (cb_info->type == CB_SERIAL_TYPE_IO_MAPPED)
			plat->flags |= NS16550_FLAG_IO;
		ret = 0;
	} else if (IS_ENABLED(CONFIG_COREBOOT_SERIAL_FROM_DBG2)) {
		ret = read_dbg2(plat);
	}

	if (ret) {
		/*
		 * Returning an error will cause U-Boot to complain that
		 * there is no UART, which may panic. So stay silent and
		 * pray that the video console will work.
		 */
		log_debug("Cannot detect UART\n");
	}

	return 0;
}

static const struct udevice_id coreboot_serial_ids[] = {
	{ .compatible = "coreboot-serial" },
	{ },
};

U_BOOT_DRIVER(coreboot_uart) = {
	.name	= "coreboot_uart",
	.id	= UCLASS_SERIAL,
	.of_match	= coreboot_serial_ids,
	.priv_auto	= sizeof(struct ns16550),
	.plat_auto	= sizeof(struct ns16550_plat),
	.of_to_plat  = coreboot_of_to_plat,
	.probe	= ns16550_serial_probe,
	.ops	= &ns16550_serial_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};