summaryrefslogtreecommitdiff
path: root/src/ec/kontron/kempld/kempld.c
blob: 19376c7961d72ccddebd3aabb9641634baea025c (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
/* SPDX-License-Identifier: GPL-2.0-only */

#include <console/console.h>
#include <device/device.h>

#include "chip.h"
#include "kempld.h"
#include "kempld_internal.h"

static void kempld_uart_read_resources(struct device *dev)
{
	static const unsigned int io_addr[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };

	const struct ec_kontron_kempld_config *const config = dev->chip_info;

	struct resource *const res_io = new_resource(dev, 0);
	struct resource *const res_irq = new_resource(dev, 1);
	const unsigned int uart = dev->path.generic.subid;
	if (!config || !res_io || !res_irq || uart >= KEMPLD_NUM_UARTS)
		return;

	const enum kempld_uart_io io = config->uart[uart].io;
	if (io >= ARRAY_SIZE(io_addr)) {
		printk(BIOS_ERR, "KEMPLD: Bad io value '%d' for UART#%u\n.",
		       io, uart);
		dev->enabled = false;
		return;
	}

	const int irq = config->uart[uart].irq;
	if (irq >= 16) {
		printk(BIOS_ERR, "KEMPLD: Bad irq value '%d' for UART#%u\n.",
		       irq, uart);
		dev->enabled = false;
		return;
	}

	res_io->base = io_addr[io];
	res_io->size = 8;
	res_io->flags = IORESOURCE_IO | IORESOURCE_FIXED |
			IORESOURCE_STORED | IORESOURCE_ASSIGNED;
	res_irq->base = irq;
	res_irq->size = 1;
	res_irq->flags = IORESOURCE_IO | IORESOURCE_FIXED |
			 IORESOURCE_STORED | IORESOURCE_ASSIGNED;

	if (kempld_get_mutex(100) < 0)
		return;

	const uint8_t reg = uart ? KEMPLD_UART_1 : KEMPLD_UART_0;
	const uint8_t val = kempld_read8(reg);
	kempld_write8(reg, (val & ~(KEMPLD_UART_IO_MASK | KEMPLD_UART_IRQ_MASK))
			   | io << KEMPLD_UART_IO_SHIFT
			   | irq << KEMPLD_UART_IRQ_SHIFT);

	kempld_release_mutex();
}

static void kempld_uart_enable_resources(struct device *dev)
{
	if (kempld_get_mutex(100) < 0)
		return;

	const unsigned int uart = dev->path.generic.subid;
	const uint8_t reg = uart ? KEMPLD_UART_1 : KEMPLD_UART_0;
	kempld_write8(reg, kempld_read8(reg) | KEMPLD_UART_ENABLE);

	kempld_release_mutex();
}

static struct device_operations kempld_uart_ops = {
	.read_resources   = kempld_uart_read_resources,
	.enable_resources = kempld_uart_enable_resources,
};

static void kempld_enable_dev(struct device *const dev)
{
	if (dev->path.type == DEVICE_PATH_GENERIC) {
		switch (dev->path.generic.id) {
		case 0:
			if (dev->path.generic.subid < KEMPLD_NUM_UARTS) {
				dev->ops = &kempld_uart_ops;
				break;
			}
			/* Fall through. */
		case 1:
			if (dev->path.generic.subid == 0) {
				kempld_i2c_device_init(dev);
				break;
			}
			/* Fall through. */
		default:
			printk(BIOS_WARNING,
			       "KEMPLD: Spurious device %s.\n",
			       dev_path(dev));
			break;
		}
	}
}

struct chip_operations ec_kontron_kempld_ops = {
	CHIP_NAME("Kontron KEMPLD")
	.enable_dev = kempld_enable_dev,
};