summaryrefslogtreecommitdiff
path: root/chip/mt_scp/mt8183/gpio.c
blob: a4896aae721f5f4e9f856e986a47d1a62e692cdb (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* GPIO module */

#include "gpio.h"
#include "hooks.h"
#include "registers.h"
#include "system.h"
#include "task.h"
#include "util.h"

void gpio_set_alternate_function(uint32_t port, uint32_t mask,
				enum gpio_alternate_func func)
{
	int bit, mode_reg_index, shift;
	uint32_t mode_bits, mode_mask;

	/* Up to 8 alt functions per port */
	if (func > GPIO_ALT_FUNC_7)
		return;

	if (func == GPIO_ALT_FUNC_NONE)
		func = GPIO_ALT_FUNC_DEFAULT;

	while (mask) {
		/* 32 gpio per port */
		bit = get_next_bit(&mask);
		/* 8 gpio per mode reg */
		mode_reg_index = (port << 2) | (bit >> 3);
		/*
		 * b[3]   - write enable(?)
		 * b[2:0] - mode
		 */
		shift = (bit & 7) << 2;
		mode_bits = func << shift;
		mode_mask = ~(0xf << shift);
		AP_GPIO_MODE(mode_reg_index) = (AP_GPIO_MODE(mode_reg_index) &
						mode_mask) | mode_bits;
	}
}

test_mockable int gpio_get_level(enum gpio_signal signal)
{
	return !!(AP_GPIO_DIN(gpio_list[signal].port) &
		  gpio_list[signal].mask);
}


void gpio_set_level(enum gpio_signal signal, int value)
{
	if (value)
		AP_GPIO_DOUT(gpio_list[signal].port) |= gpio_list[signal].mask;
	else
		AP_GPIO_DOUT(gpio_list[signal].port) &= ~gpio_list[signal].mask;
}

void gpio_set_flags_by_mask(uint32_t port, uint32_t mask, uint32_t flags)
{
	/* Set input/output mode */
	if (flags & GPIO_OUTPUT) {
		/* Set level before changing to output mode */
		if (flags & GPIO_HIGH)
			AP_GPIO_DOUT(port) |= mask;
		if (flags & GPIO_LOW)
			AP_GPIO_DOUT(port) &= ~mask;
		AP_GPIO_DIR(port) |= mask;
	} else {
		AP_GPIO_DIR(port) &= ~mask;
	}

	if (flags & (GPIO_INT_F_RISING | GPIO_INT_F_HIGH))
		SCP_EINT_POLARITY_SET[port] = mask;

	if (flags & (GPIO_INT_F_FALLING | GPIO_INT_F_LOW))
		SCP_EINT_POLARITY_CLR[port] = mask;
	else
		SCP_EINT_POLARITY_SET[port] = mask;

	/* Set sensitivity register on edge trigger */
	if (flags & (GPIO_INT_F_RISING | GPIO_INT_F_FALLING))
		SCP_EINT_SENS_SET[port] = mask;
	else
		SCP_EINT_SENS_CLR[port] = mask;
}

int gpio_get_flags_by_mask(uint32_t port, uint32_t mask)
{
	/* TODO(b/120167145): implement get flags */
	return 0;
}

int gpio_enable_interrupt(enum gpio_signal signal)
{
	const struct gpio_info *g = gpio_list + signal;

	if (signal >= GPIO_IH_COUNT || !g->mask)
		return EC_ERROR_INVAL;

	SCP_EINT_MASK_CLR[g->port] = g->mask;

	return EC_SUCCESS;
}

int gpio_disable_interrupt(enum gpio_signal signal)
{
	const struct gpio_info *g = gpio_list + signal;

	if (signal >= GPIO_IH_COUNT || !g->mask)
		return EC_ERROR_INVAL;

	SCP_EINT_MASK_SET[g->port] = g->mask;

	return EC_SUCCESS;
}

int gpio_clear_pending_interrupt(enum gpio_signal signal)
{
	const struct gpio_info *g = gpio_list + signal;

	if (signal >= GPIO_IH_COUNT || !g->mask)
		return EC_ERROR_INVAL;

	SCP_EINT_ACK[g->port] = g->mask;

	return EC_SUCCESS;
}

void gpio_pre_init(void)
{
	const struct gpio_info *g = gpio_list;
	int i;
	int is_warm = system_is_reboot_warm();

	for (i = 0; i < GPIO_COUNT; i++, g++) {
		int flags = g->flags;

		if (flags & GPIO_DEFAULT)
			continue;

		if (is_warm)
			flags &= ~(GPIO_LOW | GPIO_HIGH);

		gpio_set_flags_by_mask(g->port, g->mask, flags);
	}
}

void gpio_init(void)
{
	/* Enable EINT IRQ */
	task_enable_irq(SCP_IRQ_EINT);
}
DECLARE_HOOK(HOOK_INIT, gpio_init, HOOK_PRIO_DEFAULT);

/* Interrupt handler */
void __keep gpio_interrupt(void)
{
	int bit, port;
	uint32_t pending;
	enum gpio_signal signal;

	for (port = 0; port <= MAX_EINT_PORT; port++) {
		pending = SCP_EINT_STATUS[port];

		while (pending) {
			bit = get_next_bit(&pending);
			SCP_EINT_ACK[port] = BIT(bit);
			/* Skip masked gpio */
			if (SCP_EINT_MASK_GET[port] & BIT(bit))
				continue;
			/* Call handler */
			signal = port * 32 + bit;
			if (signal < GPIO_IH_COUNT)
				gpio_irq_handlers[signal](signal);
		}
	}
}
DECLARE_IRQ(SCP_IRQ_EINT, gpio_interrupt, 1);