Debian Codegraph @

src/southbridge/intel/lynxpoint/lp_gpio.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <stdint.h>
4 #include <arch/io.h>
5 #include <device/pci_ops.h>
6 #include <device/device.h>
7 #include <device/pci.h>
8 #include <gpio.h>
9
10 #include "pch.h"
11 #include "lp_gpio.h"
12
13 static u16 get_gpio_base(void)
14 {
15 #ifdef __SIMPLE_DEVICE__
16 return pci_read_config16(PCH_LPC_DEV, GPIO_BASE) & 0xfffc;
17 #else
18 return pci_read_config16(pcidev_on_root(0x1f, 0),
19 GPIO_BASE) & 0xfffc;
20 #endif
21 }
22
23 /*
24 * This function will return a number that indicates which PIRQ
25 * this GPIO maps to. If this is not a PIRQ capable GPIO then
26 * it will return -1. The GPIO to PIRQ mapping is not linear.
27 */
28 static int lp_gpio_to_pirq(int gpio)
29 {
30 switch (gpio) {
31 case 8: return 0; /* PIRQI */
32 case 9: return 1; /* PIRQJ */
33 case 10: return 2; /* PIRQK */
34 case 13: return 3; /* PIRQL */
35 case 14: return 4; /* PIRQM */
36 case 45: return 5; /* PIRQN */
37 case 46: return 6; /* PIRQO */
38 case 47: return 7; /* PIRQP */
39 case 48: return 8; /* PIRQQ */
40 case 49: return 9; /* PIRQR */
41 case 50: return 10; /* PIRQS */
42 case 51: return 11; /* PIRQT */
43 case 52: return 12; /* PIRQU */
44 case 53: return 13; /* PIRQV */
45 case 54: return 14; /* PIRQW */
46 case 55: return 15; /* PIRQX */
47 default: return -1;
48 };
49 }
50
51 void setup_pch_lp_gpios(const struct pch_lp_gpio_map map[])
52 {
53 u16 gpio_base = get_gpio_base();
54 const struct pch_lp_gpio_map *config;
55 u32 owner[3] = {0};
56 u32 route[3] = {0};
57 u32 irqen[3] = {0};
58 u32 reset[3] = {0};
59 u32 blink = 0;
60 u16 pirq2apic = 0;
61 int set, bit, gpio = 0;
62
63 for (config = map; config->conf0 != GPIO_LIST_END; config++, gpio++) {
64 if (gpio > MAX_GPIO_NUMBER)
65 break;
66
67 /* Setup Configuration registers 1 and 2 */
68 outl(config->conf0, gpio_base + GPIO_CONFIG0(gpio));
69 outl(config->conf1, gpio_base + GPIO_CONFIG1(gpio));
70
71 /* Determine set and bit based on GPIO number */
72 set = gpio >> 5;
73 bit = gpio % 32;
74
75 /* Apply settings to set specific bits */
76 owner[set] |= config->owner << bit;
77 route[set] |= config->route << bit;
78 irqen[set] |= config->irqen << bit;
79 reset[set] |= config->reset << bit;
80
81 if (set == 0)
82 blink |= config->blink << bit;
83
84 /* PIRQ to IO-APIC map */
85 if (config->pirq == GPIO_PIRQ_APIC_ROUTE) {
86 set = lp_gpio_to_pirq(gpio);
87 if (set >= 0)
88 pirq2apic |= 1 << set;
89 }
90 }
91
92 for (set = 0; set <= 2; set++) {
93 outl(owner[set], gpio_base + GPIO_OWNER(set));
94 outl(route[set], gpio_base + GPIO_ROUTE(set));
95 outl(irqen[set], gpio_base + GPIO_IRQ_IE(set));
96 outl(reset[set], gpio_base + GPIO_RESET(set));
97 }
98
99 outl(blink, gpio_base + GPIO_BLINK);
100 outl(pirq2apic, gpio_base + GPIO_PIRQ_APIC_EN);
101 }
102
103 int gpio_get(gpio_t gpio_num)
104 {
105 u16 gpio_base = get_gpio_base();
106
107 if (gpio_num > MAX_GPIO_NUMBER)
108 return 0;
109
110 return !!(inl(gpio_base + GPIO_CONFIG0(gpio_num)) & GPI_LEVEL);
111 }
112
113 void gpio_set(gpio_t gpio_num, int value)
114 {
115 u16 gpio_base = get_gpio_base();
116 u32 conf0;
117
118 if (gpio_num > MAX_GPIO_NUMBER)
119 return;
120
121 conf0 = inl(gpio_base + GPIO_CONFIG0(gpio_num));
122 conf0 &= ~GPO_LEVEL_MASK;
123 conf0 |= value << GPO_LEVEL_SHIFT;
124 outl(conf0, gpio_base + GPIO_CONFIG0(gpio_num));
125 }
126
127 int gpio_is_native(int gpio_num)
128 {
129 u16 gpio_base = get_gpio_base();
130
131 return !(inl(gpio_base + GPIO_CONFIG0(gpio_num)) & 1);
132 }
133
134 void gpio_input(gpio_t gpio)
135 {
136 // FIXME: Implement
137 }