blob: b87238b649487b075553408b1269ca1c02b9bab6 [file] [log] [blame]
Nico Huber016ef9e2017-09-19 14:13:34 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2017 secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <console/console.h>
17#include <device/device.h>
18
19#include "chip.h"
20#include "kempld.h"
21#include "kempld_internal.h"
22
23static void kempld_uart_read_resources(struct device *dev)
24{
25 static const unsigned int io_addr[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
26
27 const struct ec_kontron_kempld_config *const config = dev->chip_info;
28
29 struct resource *const res_io = new_resource(dev, 0);
30 struct resource *const res_irq = new_resource(dev, 1);
31 const unsigned int uart = dev->path.generic.subid;
32 if (!config || !res_io || !res_irq || uart >= KEMPLD_NUM_UARTS)
33 return;
34
35 const enum kempld_uart_io io = config->uart[uart].io;
36 if (io >= ARRAY_SIZE(io_addr)) {
37 printk(BIOS_ERR, "KEMPLD: Bad io value '%d' for UART#%u\n.",
38 io, uart);
39 dev->enabled = false;
40 return;
41 }
42
43 const int irq = config->uart[uart].irq;
44 if (irq >= 16) {
45 printk(BIOS_ERR, "KEMPLD: Bad irq value '%d' for UART#%u\n.",
46 irq, uart);
47 dev->enabled = false;
48 return;
49 }
50
51 res_io->base = io_addr[io];
52 res_io->size = 8;
53 res_io->flags = IORESOURCE_IO | IORESOURCE_FIXED |
54 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
55 res_irq->base = irq;
56 res_irq->size = 1;
57 res_irq->flags = IORESOURCE_IO | IORESOURCE_FIXED |
58 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
59
60 if (kempld_get_mutex(100) < 0)
61 return;
62
63 const uint8_t reg = uart ? KEMPLD_UART_1 : KEMPLD_UART_0;
64 const uint8_t val = kempld_read8(reg);
65 kempld_write8(reg, (val & ~(KEMPLD_UART_IO_MASK | KEMPLD_UART_IRQ_MASK))
66 | io << KEMPLD_UART_IO_SHIFT
67 | irq << KEMPLD_UART_IRQ_SHIFT);
68
69 kempld_release_mutex();
70}
71
72static void kempld_uart_enable_resources(struct device *dev)
73{
74 if (kempld_get_mutex(100) < 0)
75 return;
76
77 const unsigned int uart = dev->path.generic.subid;
78 const uint8_t reg = uart ? KEMPLD_UART_1 : KEMPLD_UART_0;
79 kempld_write8(reg, kempld_read8(reg) | KEMPLD_UART_ENABLE);
80
81 kempld_release_mutex();
82}
83
84static struct device_operations kempld_uart_ops = {
85 .read_resources = kempld_uart_read_resources,
86 .enable_resources = kempld_uart_enable_resources,
87};
88
89static void kempld_enable_dev(struct device *const dev)
90{
91 if (dev->path.type == DEVICE_PATH_GENERIC) {
92 switch (dev->path.generic.id) {
93 case 0:
94 if (dev->path.generic.subid < KEMPLD_NUM_UARTS) {
95 dev->ops = &kempld_uart_ops;
96 break;
97 }
Nico Huber5fb34e82019-09-19 16:35:28 +020098 /* Fall through. */
Nico Huber016ef9e2017-09-19 14:13:34 +020099 case 1:
100 if (dev->path.generic.subid == 0) {
101 kempld_i2c_device_init(dev);
102 break;
103 }
Nico Huber5fb34e82019-09-19 16:35:28 +0200104 /* Fall through. */
Nico Huber016ef9e2017-09-19 14:13:34 +0200105 default:
106 printk(BIOS_WARNING,
107 "KEMPLD: Spurious device %s.\n",
108 dev_path(dev));
109 break;
110 }
111 }
112}
113
114struct chip_operations ec_kontron_kempld_ops = {
115 CHIP_NAME("Kontron KEMPLD")
116 .enable_dev = kempld_enable_dev,
117};