stm32_gpio.c 7.02 KB
Newer Older
Yann Gautier's avatar
Yann Gautier committed
1
/*
2
 * Copyright (c) 2016-2019, STMicroelectronics - All Rights Reserved
Yann Gautier's avatar
Yann Gautier committed
3
4
5
6
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

7
8
#include <assert.h>
#include <errno.h>
Yann Gautier's avatar
Yann Gautier committed
9
#include <stdbool.h>
10

11
12
13
14
#include <libfdt.h>

#include <platform_def.h>

15
16
17
#include <common/bl_common.h>
#include <common/debug.h>
#include <drivers/st/stm32_gpio.h>
Yann Gautier's avatar
Yann Gautier committed
18
#include <drivers/st/stm32mp_clkfunc.h>
19
#include <lib/mmio.h>
20
#include <lib/utils_def.h>
Yann Gautier's avatar
Yann Gautier committed
21

22
23
24
25
26
27
28
29
30
31
32
#define DT_GPIO_BANK_SHIFT	12
#define DT_GPIO_BANK_MASK	GENMASK(16, 12)
#define DT_GPIO_PIN_SHIFT	8
#define DT_GPIO_PIN_MASK	GENMASK(11, 8)
#define DT_GPIO_MODE_MASK	GENMASK(7, 0)

/*******************************************************************************
 * This function gets GPIO bank node in DT.
 * Returns node offset if status is okay in DT, else return 0
 ******************************************************************************/
static int ckeck_gpio_bank(void *fdt, uint32_t bank, int pinctrl_node)
Yann Gautier's avatar
Yann Gautier committed
33
{
34
35
	int pinctrl_subnode;
	uint32_t bank_offset = stm32_get_gpio_bank_offset(bank);
Yann Gautier's avatar
Yann Gautier committed
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	fdt_for_each_subnode(pinctrl_subnode, fdt, pinctrl_node) {
		const fdt32_t *cuint;

		if (fdt_getprop(fdt, pinctrl_subnode,
				"gpio-controller", NULL) == NULL) {
			continue;
		}

		cuint = fdt_getprop(fdt, pinctrl_subnode, "reg", NULL);
		if (cuint == NULL) {
			continue;
		}

		if ((fdt32_to_cpu(*cuint) == bank_offset) &&
		    (fdt_get_status(pinctrl_subnode) != DT_DISABLED)) {
			return pinctrl_subnode;
		}
Yann Gautier's avatar
Yann Gautier committed
54
55
	}

56
	return 0;
Yann Gautier's avatar
Yann Gautier committed
57
58
}

59
60
61
62
63
64
/*******************************************************************************
 * This function gets the pin settings from DT information.
 * When analyze and parsing is done, set the GPIO registers.
 * Returns 0 on success and a negative FDT error code on failure.
 ******************************************************************************/
static int dt_set_gpio_config(void *fdt, int node, uint8_t status)
Yann Gautier's avatar
Yann Gautier committed
65
{
66
67
68
69
70
71
	const fdt32_t *cuint, *slewrate;
	int len;
	int pinctrl_node;
	uint32_t i;
	uint32_t speed = GPIO_SPEED_LOW;
	uint32_t pull = GPIO_NO_PULL;
Yann Gautier's avatar
Yann Gautier committed
72

73
74
75
	cuint = fdt_getprop(fdt, node, "pinmux", &len);
	if (cuint == NULL) {
		return -FDT_ERR_NOTFOUND;
Yann Gautier's avatar
Yann Gautier committed
76
77
	}

78
79
80
81
82
83
84
85
86
87
88
89
90
91
	pinctrl_node = fdt_parent_offset(fdt, fdt_parent_offset(fdt, node));
	if (pinctrl_node < 0) {
		return -FDT_ERR_NOTFOUND;
	}

	slewrate = fdt_getprop(fdt, node, "slew-rate", NULL);
	if (slewrate != NULL) {
		speed = fdt32_to_cpu(*slewrate);
	}

	if (fdt_getprop(fdt, node, "bias-pull-up", NULL) != NULL) {
		pull = GPIO_PULL_UP;
	} else if (fdt_getprop(fdt, node, "bias-pull-down", NULL) != NULL) {
		pull = GPIO_PULL_DOWN;
Yann Gautier's avatar
Yann Gautier committed
92
	} else {
93
		VERBOSE("No bias configured in node %d\n", node);
Yann Gautier's avatar
Yann Gautier committed
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
	for (i = 0U; i < ((uint32_t)len / sizeof(uint32_t)); i++) {
		uint32_t pincfg;
		uint32_t bank;
		uint32_t pin;
		uint32_t mode;
		uint32_t alternate = GPIO_ALTERNATE_(0);
		int bank_node;
		int clk;

		pincfg = fdt32_to_cpu(*cuint);
		cuint++;

		bank = (pincfg & DT_GPIO_BANK_MASK) >> DT_GPIO_BANK_SHIFT;

		pin = (pincfg & DT_GPIO_PIN_MASK) >> DT_GPIO_PIN_SHIFT;

		mode = pincfg & DT_GPIO_MODE_MASK;

		switch (mode) {
		case 0:
			mode = GPIO_MODE_INPUT;
			break;
		case 1 ... 16:
			alternate = mode - 1U;
			mode = GPIO_MODE_ALTERNATE;
			break;
		case 17:
			mode = GPIO_MODE_ANALOG;
			break;
		default:
			mode = GPIO_MODE_OUTPUT;
			break;
		}

		if (fdt_getprop(fdt, node, "drive-open-drain", NULL) != NULL) {
			mode |= GPIO_OPEN_DRAIN;
		}

		bank_node = ckeck_gpio_bank(fdt, bank, pinctrl_node);
		if (bank_node == 0) {
			ERROR("PINCTRL inconsistent in DT\n");
			panic();
		}

		clk = fdt_get_clock_id(bank_node);
		if (clk < 0) {
			return -FDT_ERR_NOTFOUND;
		}

		/* Platform knows the clock: assert it is okay */
		assert((unsigned long)clk == stm32_get_gpio_bank_clock(bank));

		set_gpio(bank, pin, mode, speed, pull, alternate, status);
	}

	return 0;
}

/*******************************************************************************
 * This function gets the pin settings from DT information.
 * When analyze and parsing is done, set the GPIO registers.
 * Returns 0 on success and a negative FDT/ERRNO error code on failure.
 ******************************************************************************/
int dt_set_pinctrl_config(int node)
{
	const fdt32_t *cuint;
	int lenp = 0;
	uint32_t i;
	uint8_t status = fdt_get_status(node);
	void *fdt;

	if (fdt_get_address(&fdt) == 0) {
168
		return -FDT_ERR_NOTFOUND;
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
	}

	if (status == DT_DISABLED) {
		return -FDT_ERR_NOTFOUND;
	}

	cuint = fdt_getprop(fdt, node, "pinctrl-0", &lenp);
	if (cuint == NULL) {
		return -FDT_ERR_NOTFOUND;
	}

	for (i = 0; i < ((uint32_t)lenp / 4U); i++) {
		int p_node, p_subnode;

		p_node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint));
		if (p_node < 0) {
			return -FDT_ERR_NOTFOUND;
		}

		fdt_for_each_subnode(p_subnode, fdt, p_node) {
			int ret = dt_set_gpio_config(fdt, p_subnode, status);

			if (ret < 0) {
				return ret;
			}
		}

		cuint++;
	}

	return 0;
}

void set_gpio(uint32_t bank, uint32_t pin, uint32_t mode, uint32_t speed,
	      uint32_t pull, uint32_t alternate, uint8_t status)
{
	uintptr_t base = stm32_get_gpio_bank_base(bank);
	unsigned long clock = stm32_get_gpio_bank_clock(bank);

	assert(pin <= GPIO_PIN_MAX);

210
	stm32mp_clk_enable(clock);
211

212
	mmio_clrbits_32(base + GPIO_MODE_OFFSET,
Yann Gautier's avatar
Yann Gautier committed
213
			((uint32_t)GPIO_MODE_MASK << (pin << 1)));
214
	mmio_setbits_32(base + GPIO_MODE_OFFSET,
Yann Gautier's avatar
Yann Gautier committed
215
216
217
			(mode & ~GPIO_OPEN_DRAIN) << (pin << 1));

	if ((mode & GPIO_OPEN_DRAIN) != 0U) {
218
219
220
		mmio_setbits_32(base + GPIO_TYPE_OFFSET, BIT(pin));
	} else {
		mmio_clrbits_32(base + GPIO_TYPE_OFFSET, BIT(pin));
Yann Gautier's avatar
Yann Gautier committed
221
222
	}

223
	mmio_clrbits_32(base + GPIO_SPEED_OFFSET,
Yann Gautier's avatar
Yann Gautier committed
224
			((uint32_t)GPIO_SPEED_MASK << (pin << 1)));
225
	mmio_setbits_32(base + GPIO_SPEED_OFFSET, speed << (pin << 1));
Yann Gautier's avatar
Yann Gautier committed
226

227
	mmio_clrbits_32(base + GPIO_PUPD_OFFSET,
Yann Gautier's avatar
Yann Gautier committed
228
			((uint32_t)GPIO_PULL_MASK << (pin << 1)));
229
	mmio_setbits_32(base + GPIO_PUPD_OFFSET, pull << (pin << 1));
Yann Gautier's avatar
Yann Gautier committed
230
231

	if (pin < GPIO_ALT_LOWER_LIMIT) {
232
		mmio_clrbits_32(base + GPIO_AFRL_OFFSET,
Yann Gautier's avatar
Yann Gautier committed
233
				((uint32_t)GPIO_ALTERNATE_MASK << (pin << 2)));
234
		mmio_setbits_32(base + GPIO_AFRL_OFFSET,
Yann Gautier's avatar
Yann Gautier committed
235
236
				alternate << (pin << 2));
	} else {
237
		mmio_clrbits_32(base + GPIO_AFRH_OFFSET,
Yann Gautier's avatar
Yann Gautier committed
238
239
				((uint32_t)GPIO_ALTERNATE_MASK <<
				 ((pin - GPIO_ALT_LOWER_LIMIT) << 2)));
240
		mmio_setbits_32(base + GPIO_AFRH_OFFSET,
Yann Gautier's avatar
Yann Gautier committed
241
242
243
244
245
				alternate << ((pin - GPIO_ALT_LOWER_LIMIT) <<
					      2));
	}

	VERBOSE("GPIO %u mode set to 0x%x\n", bank,
246
		mmio_read_32(base + GPIO_MODE_OFFSET));
Yann Gautier's avatar
Yann Gautier committed
247
	VERBOSE("GPIO %u speed set to 0x%x\n", bank,
248
		mmio_read_32(base + GPIO_SPEED_OFFSET));
Yann Gautier's avatar
Yann Gautier committed
249
	VERBOSE("GPIO %u mode pull to 0x%x\n", bank,
250
		mmio_read_32(base + GPIO_PUPD_OFFSET));
Yann Gautier's avatar
Yann Gautier committed
251
	VERBOSE("GPIO %u mode alternate low to 0x%x\n", bank,
252
		mmio_read_32(base + GPIO_AFRL_OFFSET));
Yann Gautier's avatar
Yann Gautier committed
253
	VERBOSE("GPIO %u mode alternate high to 0x%x\n", bank,
254
		mmio_read_32(base + GPIO_AFRH_OFFSET));
255

256
	stm32mp_clk_disable(clock);
257
258
259
260
261
}

void set_gpio_secure_cfg(uint32_t bank, uint32_t pin, bool secure)
{
	uintptr_t base = stm32_get_gpio_bank_base(bank);
262
	unsigned long clock = stm32_get_gpio_bank_clock(bank);
263
264
265

	assert(pin <= GPIO_PIN_MAX);

266
	stm32mp_clk_enable(clock);
267
268
269
270
271
272
273

	if (secure) {
		mmio_setbits_32(base + GPIO_SECR_OFFSET, BIT(pin));
	} else {
		mmio_clrbits_32(base + GPIO_SECR_OFFSET, BIT(pin));
	}

274
	stm32mp_clk_disable(clock);
Yann Gautier's avatar
Yann Gautier committed
275
}