aboutsummaryrefslogtreecommitdiff
path: root/src/soc/intel/skylake/gpio.c
blob: 9f7b409e5c88ddb9ceaefafc6ae6ca9c264ae089 (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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2014 Google Inc.
 * Copyright (C) 2015 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdint.h>
#include <string.h>
#include <arch/io.h>
#include <device/device.h>
#include <device/pci.h>
#include <gpio.h>
#include <soc/pcr.h>
#include <soc/iomap.h>
#include <soc/pm.h>

/* Keep the ordering intact GPP_A ~ G, GPD.
 * As the gpio/smi functions get_smi_status() and
 * enable_gpio_groupsmi() depends on this ordering.
 */
static const GPIO_GROUP_INFO gpio_group_info[] = {
	/* GPP_A */
	{
		.community = PID_GPIOCOM0,
		.padcfgoffset = R_PCH_PCR_GPIO_GPP_A_PADCFG_OFFSET,
		.padpergroup = V_PCH_GPIO_GPP_A_PAD_MAX,
		.smistsoffset = R_PCH_PCR_GPIO_GPP_A_SMI_STS,
		.smienoffset = R_PCH_PCR_GPIO_GPP_A_SMI_EN,
	},
	/* GPP_B */
	{
		.community = PID_GPIOCOM0,
		.padcfgoffset = R_PCH_PCR_GPIO_GPP_B_PADCFG_OFFSET,
		.padpergroup = V_PCH_GPIO_GPP_B_PAD_MAX,
		.smistsoffset = R_PCH_PCR_GPIO_GPP_B_SMI_STS,
		.smienoffset = R_PCH_PCR_GPIO_GPP_B_SMI_EN,
	},
	/* GPP_C */
	{
		.community = PID_GPIOCOM1,
		.padcfgoffset = R_PCH_PCR_GPIO_GPP_C_PADCFG_OFFSET,
		.padpergroup = V_PCH_GPIO_GPP_C_PAD_MAX,
		.smistsoffset = R_PCH_PCR_GPIO_GPP_C_SMI_STS,
		.smienoffset = R_PCH_PCR_GPIO_GPP_C_SMI_EN,
	},
	/* GPP_D */
	{
		.community = PID_GPIOCOM1,
		.padcfgoffset = R_PCH_PCR_GPIO_GPP_D_PADCFG_OFFSET,
		.padpergroup = V_PCH_GPIO_GPP_D_PAD_MAX,
		.smistsoffset = R_PCH_PCR_GPIO_GPP_D_SMI_STS,
		.smienoffset = R_PCH_PCR_GPIO_GPP_D_SMI_EN,
	},
	/* GPP_E */
	{
		.community = PID_GPIOCOM1,
		.padcfgoffset = R_PCH_PCR_GPIO_GPP_E_PADCFG_OFFSET,
		.padpergroup = V_PCH_GPIO_GPP_E_PAD_MAX,
		.smistsoffset = R_PCH_PCR_GPIO_GPP_E_SMI_STS,
		.smienoffset = R_PCH_PCR_GPIO_GPP_E_SMI_EN,
	},
	/* GPP_F */
	{
		.community = PID_GPIOCOM3,
		.padcfgoffset = R_PCH_PCR_GPIO_GPP_F_PADCFG_OFFSET,
		.padpergroup = V_PCH_GPIO_GPP_F_PAD_MAX,
		.smistsoffset = NO_REGISTER_PROPERTY,
		.smienoffset = NO_REGISTER_PROPERTY,
	},
	/* GPP_G */
	{
		.community = PID_GPIOCOM3,
		.padcfgoffset = R_PCH_PCR_GPIO_GPP_G_PADCFG_OFFSET,
		.padpergroup = V_PCH_GPIO_GPP_G_PAD_MAX,
		.smistsoffset = NO_REGISTER_PROPERTY,
		.smienoffset = NO_REGISTER_PROPERTY,
	},
	/* GPP_H */
	{
		.community = PID_GPIOCOM2,
		.padcfgoffset = R_PCH_PCR_GPIO_GPD_PADCFG_OFFSET,
		.padpergroup = V_PCH_GPIO_GPD_PAD_MAX,
		.smistsoffset = NO_REGISTER_PROPERTY,
		.smienoffset = NO_REGISTER_PROPERTY,
	},
};

/*
 * SPT has 7 GPIO communities named as GPP_A to GPP_G.
 * Each community has 24 GPIO PIN.
 * Below formula to calculate GPIO Pin from GPIO PAD.
 * PIN# = GROUP_PAD# + GROUP# * 24
 * ====================================
 * Community || Group#
 * ====================================
 * GPP_A	||	0
 * GPP_B	||	1
 * GPP_C	||	2
 * GPP_D	||	3
 * GPP_E	||	4
 * GPP_F	||	5
 * GPP_G	||	6
 */
static u32 get_padnumber_from_gpiopad(GPIO_PAD gpiopad)
{
	return (u32) GPIO_GET_PAD_NUMBER(gpiopad);
}

static u32 get_groupindex_from_gpiopad(GPIO_PAD gpiopad)
{
	return (u32) GPIO_GET_GROUP_INDEX_FROM_PAD(gpiopad);
}

static int read_write_gpio_pad_reg(u32 gpiopad, u8 dwreg, u32 mask, int write,
			    u32 *readwriteval)
{
	u32 padcfgreg;
	u32 gpiogroupinfolength;
	u32 groupindex;
	u32 padnumber;

	groupindex = get_groupindex_from_gpiopad(gpiopad);
	padnumber = get_padnumber_from_gpiopad(gpiopad);

	gpiogroupinfolength = sizeof(gpio_group_info) / sizeof(GPIO_GROUP_INFO);

	/* Check if group argument exceeds GPIO GROUP INFO array */
	if ((u32) groupindex >= gpiogroupinfolength)
		return -1;
	/* Check if legal pin number */
	if (padnumber >= gpio_group_info[groupindex].padpergroup)
		return -1;
	/* Create Pad Configuration register offset */
	padcfgreg = 0x8 * padnumber + gpio_group_info[groupindex].padcfgoffset;
	if (dwreg == 1)
		padcfgreg += 0x4;
	if (write) {
		pcr_andthenor32(gpio_group_info[groupindex].community,
				padcfgreg, (u32) (~mask),
				(u32) (*readwriteval & mask));
	} else {
		pcr_read32(gpio_group_info[groupindex].community, padcfgreg,
			   readwriteval);
		*readwriteval &= mask;
	}

	return 0;
}

static int convert_gpio_num_to_pad(gpio_t gpionum)
{
	int group_pad_num = 0;
	int gpio_group = 0;
	u32 gpio_pad = 0;

	group_pad_num = (gpionum % MAX_GPIO_PIN_PER_GROUP);
	gpio_group = (gpionum / MAX_GPIO_PIN_PER_GROUP);

	switch (gpio_group) {
	case GPIO_LP_GROUP_A:
		gpio_pad = GPIO_LP_GROUP_GPP_A;
		break;

	case GPIO_LP_GROUP_B:
		gpio_pad = GPIO_LP_GROUP_GPP_B;
		break;

	case GPIO_LP_GROUP_C:
		gpio_pad = GPIO_LP_GROUP_GPP_C;
		break;

	case GPIO_LP_GROUP_D:
		gpio_pad = GPIO_LP_GROUP_GPP_D;
		break;

	case GPIO_LP_GROUP_E:
		gpio_pad = GPIO_LP_GROUP_GPP_E;
		break;

	case GPIO_LP_GROUP_F:
		gpio_pad = GPIO_LP_GROUP_GPP_F;
		break;

	case GPIO_LP_GROUP_G:
		gpio_pad = GPIO_LP_GROUP_GPP_G;
		break;
	default:
		return -1;
		break;
	}
	gpio_pad = (gpio_pad << GPIO_GROUP_SHIFT) + group_pad_num;

	return gpio_pad;
}

int gpio_get(gpio_t gpio_num)
{
	u32 gpiopad = 0;
	u32 outputvalue = 0;
	int status = 0;

	if (gpio_num > MAX_GPIO_NUMBER)
		return 0;

	gpiopad = convert_gpio_num_to_pad(gpio_num);
	if (gpiopad < 0)
		return -1;

	status = read_write_gpio_pad_reg(gpiopad,
					 0,
					 B_PCH_GPIO_TX_STATE,
					 READ, &outputvalue);
	outputvalue >>= N_PCH_GPIO_TX_STATE;
	return outputvalue;
}

void gpio_set(gpio_t gpio_num, int value)
{
	int status = 0;
	u32 gpiopad = 0;
	u32 outputvalue = 0;

	if (gpio_num > MAX_GPIO_NUMBER)
		return;

	gpiopad = convert_gpio_num_to_pad(gpio_num);
	if (gpiopad < 0)
		return;

	outputvalue = value;

	status = read_write_gpio_pad_reg(gpiopad,
					 0,
					 B_PCH_GPIO_TX_STATE,
					 WRITE, &outputvalue);
}

void clear_all_smi(void)
{
	u32 gpiogroupinfolength;
	u32 gpioindex = 0;

	gpiogroupinfolength = sizeof(gpio_group_info) / sizeof(GPIO_GROUP_INFO);

	for (gpioindex = 0; gpioindex < gpiogroupinfolength; gpioindex++) {
		/*Check if group has GPI SMI register */
		if (gpio_group_info[gpioindex].smistsoffset ==
		    NO_REGISTER_PROPERTY)
			continue;
		/* Clear all GPI SMI Status bits by writing '1' */
		pcr_write32(gpio_group_info[gpioindex].community,
			    gpio_group_info[gpioindex].smistsoffset,
			    0xFFFFFFFF);
	}
}

void get_smi_status(u32 status[GPIO_COMMUNITY_MAX])
{
	u32 num_of_communities;
	u32 gpioindex;
	u32 outputvalue = 0;

	num_of_communities = ARRAY_SIZE(gpio_group_info);

	for (gpioindex = 0; gpioindex < num_of_communities; gpioindex++) {
		/*Check if group has GPI SMI register */
		if (gpio_group_info[gpioindex].smistsoffset ==
		    NO_REGISTER_PROPERTY)
			continue;
		/* Read SMI status register */
		pcr_read32(gpio_group_info[gpioindex].community,
			   gpio_group_info[gpioindex].smistsoffset,
			   &outputvalue);
		status[gpioindex] = outputvalue;
	}
}

void enable_all_smi(void)
{
	u32 gpiogroupinfolength;
	u32 gpioindex = 0;

	gpiogroupinfolength = sizeof(gpio_group_info) / sizeof(GPIO_GROUP_INFO);

	for (gpioindex = 0; gpioindex < gpiogroupinfolength; gpioindex++) {
		/*Check if group has GPI SMI register */
		if (gpio_group_info[gpioindex].smienoffset ==
		    NO_REGISTER_PROPERTY)
			continue;
		/* Set all GPI SMI Enable bits by writing '1' */
		pcr_write32(gpio_group_info[gpioindex].community,
			    gpio_group_info[gpioindex].smienoffset,
			    0xFFFFFFFF);
	}
}

void enable_gpio_groupsmi(gpio_t gpio_num, u32 mask)
{
	u32 gpioindex = 0;
	u32 smien = 0;

	if (gpio_num > MAX_GPIO_NUMBER)
		return;

	gpioindex = (gpio_num / MAX_GPIO_PIN_PER_GROUP);

	pcr_read32(gpio_group_info[gpioindex].community,
		   gpio_group_info[gpioindex].smienoffset, &smien);
	smien |= mask;
	/* Set all GPI SMI Enable bits by writing '1' */
	pcr_write32(gpio_group_info[gpioindex].community,
		    gpio_group_info[gpioindex].smienoffset, smien);
}