summaryrefslogtreecommitdiff
path: root/src/soc/mediatek/mt8186/gpio.c
blob: c96f1ea2acdc4888832cb669ad8bf3fa09612a30 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* SPDX-License-Identifier: GPL-2.0-only */

/*
 * This file is created based on MT8186 Functional Specification
 * Chapter number: 5.1
 */

#include <device/mmio.h>
#include <gpio.h>

static const struct gpio_drv_info gpio_driving_info[] = {
	[0] = { 0x0, 27, 3, },
	[1] = { 0x0, 27, 3, },
	[2] = { 0x0, 27, 3, },
	[3] = { 0x0, 27, 3, },
	[4] = { 0x10, 0, 3, },
	[5] = { 0x10, 0, 3, },
	[6] = { 0x0, 9, 3, },
	[7] = { 0x0, 9, 3, },
	[8] = { 0x0, 9, 3, },
	[9] = { 0x0, 12, 3, },
	[10] = { 0x0, 0, 3, },
	[11] = { 0x0, 3, 3, },
	[12] = { 0x0, 6, 3, },
	[13] = { 0x0, 0, 3, },
	[14] = { 0x0, 3, 3, },
	[15] = { 0x10, 3, 3, },
	[16] = { 0x10, 3, 3, },
	[17] = { 0x0, 21, 3, },
	[18] = { 0x10, 0, 3, },
	[19] = { 0x0, 27, 3, },
	[20] = { 0x0, 24, 3, },
	[21] = { 0x0, 27, 3, },
	[22] = { 0x0, 24, 3, },
	[23] = { 0x10, 0, 3, },
	[24] = { 0x10, 9, 3, },
	[25] = { 0x10, 0, 3, },
	[26] = { 0x0, 27, 3, },
	[27] = { 0x0, 27, 3, },
	[28] = { 0x10, 0, 3, },
	[29] = { 0x10, 0, 3, },
	[30] = { 0x10, 0, 3, },
	[31] = { 0x10, 3, 3, },
	[32] = { 0x0, 6, 3, },
	[33] = { 0x0, 6, 3, },
	[34] = { 0x0, 3, 3, },
	[35] = { 0x0, 3, 3, },
	[36] = { 0x0, 9, 3, },
	[37] = { 0x0, 9, 3, },
	[38] = { 0x0, 12, 3, },
	[39] = { 0x0, 9, 3, },
	[40] = { 0x0, 15, 3, },
	[41] = { 0x0, 12, 3, },
	[42] = { 0x0, 12, 3, },
	[43] = { 0x0, 12, 3, },
	[44] = { 0x10, 6, 3, },
	[45] = { 0x10, 3, 3, },
	[46] = { 0x10, 3, 3, },
	[47] = { 0x10, 6, 3, },
	[48] = { 0x0, 15, 3, },
	[49] = { 0x0, 15, 3, },
	[50] = { 0x0, 15, 3, },
	[51] = { 0x0, 18, 3, },
	[52] = { 0x10, 3, 3, },
	[53] = { 0x10, 6, 3, },
	[54] = { 0x10, 3, 3, },
	[55] = { 0x10, 3, 3, },
	[56] = { 0x10, 3, 3, },
	[57] = { 0x10, 6, 3, },
	[58] = { 0x10, 6, 3, },
	[59] = { 0x10, 3, 3, },
	[60] = { 0x0, 24, 3, },
	[61] = { 0x0, 24, 3, },
	[62] = { 0x0, 24, 3, },
	[63] = { 0x0, 24, 3, },
	[64] = { 0x0, 27, 3, },
	[65] = { 0x0, 27, 3, },
	[66] = { 0x0, 27, 3, },
	[67] = { 0x10, 0, 3, },
	[68] = { 0x0, 0, 3, },
	[69] = { 0x0, 3, 3, },
	[70] = { 0x10, 3, 3, },
	[71] = { 0x0, 6, 3, },
	[72] = { 0x0, 9, 3, },
	[73] = { 0x0, 12, 3, },
	[74] = { 0x0, 15, 3, },
	[75] = { 0x0, 18, 3, },
	[76] = { 0x0, 21, 3, },
	[77] = { 0x0, 24, 3, },
	[78] = { 0x0, 27, 3, },
	[79] = { 0x0, 24, 3, },
	[80] = { 0x0, 24, 3, },
	[81] = { 0x0, 21, 3, },
	[82] = { 0x0, 21, 3, },
	[83] = { 0x10, 3, 3, },
	[84] = { 0x0, 6, 3, },
	[85] = { 0x0, 9, 3, },
	[86] = { 0x0, 12, 3, },
	[87] = { 0x0, 15, 3, },
	[88] = { 0x0, 18, 3, },
	[89] = { 0x0, 21, 3, },
	[90] = { 0x0, 27, 3, },
	[91] = { 0x10, 0, 3, },
	[92] = { 0x10, 0, 3, },
	[93] = { 0x10, 0, 3, },
	[94] = { 0x10, 0, 3, },
	[95] = { 0x10, 3, 3, },
	[96] = { 0x0, 9, 3, },
	[97] = { 0x0, 6, 3, },
	[98] = { 0x0, 3, 3, },
	[99] = { 0x0, 18, 3, },
	[100] = { 0x0, 18, 3, },
	[101] = { 0x0, 21, 3, },
	[102] = { 0x0, 21, 3, },
	[103] = { 0x0, 21, 3, },
	[104] = { 0x0, 21, 3, },
	[105] = { 0x0, 24, 3, },
	[106] = { 0x0, 24, 3, },
	[107] = { 0x0, 24, 3, },
	[108] = { 0x0, 24, 3, },
	[109] = { 0x0, 27, 3, },
	[110] = { 0x0, 27, 3, },
	[111] = { 0x0, 27, 3, },
	[112] = { 0x0, 27, 3, },
	[113] = { 0x10, 0, 3, },
	[114] = { 0x10, 0, 3, },
	[115] = { 0x10, 6, 3, },
	[116] = { 0x10, 9, 3, },
	[117] = { 0x10, 6, 3, },
	[118] = { 0x10, 6, 3, },
	[119] = { 0x0, 18, 3, },
	[120] = { 0x0, 9, 3, },
	[121] = { 0x0, 12, 3, },
	[122] = { 0x0, 15, 3, },
	[123] = { 0x0, 21, 3, },
	[124] = { 0x0, 0, 3, },
	[125] = { 0x0, 3, 3, },
	[126] = { 0x0, 6, 3, },
	[127] = { 0x10, 12, 3, },
	[128] = { 0x10, 18, 3, },
	[129] = { 0x10, 12, 3, },
	[130] = { 0x10, 18, 3, },
	[131] = { 0x10, 15, 3, },
	[132] = { 0x10, 21, 3, },
	[133] = { 0x10, 15, 3, },
	[134] = { 0x10, 21, 3, },
	[135] = { 0x10, 6, 3, },
	[136] = { 0x10, 15, 3, },
	[137] = { 0x10, 18, 3, },
	[138] = { 0x10, 24, 3, },
	[139] = { 0x0, 21, 3, },
	[140] = { 0x0, 24, 3, },
	[141] = { 0x10, 15, 3, },
	[142] = { 0x10, 21, 3, },
	[143] = { 0x10, 9, 3, },
	[144] = { 0x10, 18, 3, },
	[145] = { 0x10, 12, 3, },
	[146] = { 0x10, 21, 3, },
	[147] = { 0x0, 12, 3, },
	[148] = { 0x0, 12, 3, },
	[149] = { 0x0, 12, 3, },
	[150] = { 0x0, 15, 3, },
	[151] = { 0x0, 15, 3, },
	[152] = { 0x0, 9, 3, },
	[153] = { 0x0, 15, 3, },
	[154] = { 0x0, 15, 3, },
	[155] = { 0x0, 18, 3, },
	[156] = { 0x0, 18, 3, },
	[157] = { 0x0, 0, 3, },
	[158] = { 0x0, 0, 3, },
	[159] = { 0x0, 0, 3, },
	[160] = { 0x0, 0, 3, },
	[161] = { 0x0, 6, 3, },
	[162] = { 0x0, 3, 3, },
	[163] = { 0x10, 12, 3, },
	/* 164 is unimplemented */
	[165] = { 0x10, 6, 3, },
	[166] = { 0x10, 6, 3, },
	[167] = { 0x10, 9, 3, },
	[168] = { 0x10, 6, 3, },
	[169] = { 0x10, 12, 3, },
	[170] = { 0x10, 9, 3, },
	[171] = { 0x10, 9, 3, },
	[172] = { 0x10, 9, 3, },
	[173] = { 0x10, 6, 3, },
	[174] = { 0x0, 9, 3, },
	[175] = { 0x0, 12, 3, },
	[176] = { 0x0, 0, 3, },
	[177] = { 0x0, 3, 3, },
	[178] = { 0x0, 6, 3, },
	[179] = { 0x0, 15, 3, },
	[180] = { 0x0, 18, 3, },
	[181] = { 0x0, 21, 3, },
	[182] = { 0x0, 24, 3, },
	[183] = { 0x0, 27, 3, },
	[184] = { 0x10, 0, 3, },
};

_Static_assert(ARRAY_SIZE(gpio_driving_info) == GPIO_NUM,
	       "gpio_driving_info array size not match");

/* Unimplemented GPIOs are intentionally omitted here with width=0 */
static const struct gpio_drv_info gpio_driving_adv_info[GPIO_NUM] = {
	[127] = { 0x30, 0, 3, },
	[128] = { 0x30, 6, 3, },
	[129] = { 0x30, 0, 3, },
	[130] = { 0x30, 6, 3, },
	[131] = { 0x30, 3, 3, },
	[132] = { 0x30, 9, 3, },
	[133] = { 0x30, 0, 3, },
	[134] = { 0x30, 6, 3, },
	[135] = { 0x20, 0, 3, },
	[136] = { 0x20, 9, 3, },
	[137] = { 0x30, 3, 3, },
	[138] = { 0x30, 9, 3, },
	[139] = { 0x20, 0, 3, },
	[140] = { 0x20, 3, 3, },
	[141] = { 0x30, 3, 3, },
	[142] = { 0x30, 9, 3, },
	[143] = { 0x20, 3, 3, },
	[144] = { 0x20, 12, 3, },
	[145] = { 0x20, 6, 3, },
	[146] = { 0x20, 15, 3, },
};

static void *gpio_find_reg_addr(gpio_t gpio)
{
	void *reg_addr;
	switch (gpio.base & 0x0f) {
	case 1:
		reg_addr = (void *)IOCFG_LT_BASE;
		break;
	case 2:
		reg_addr = (void *)IOCFG_LM_BASE;
		break;
	case 3:
		reg_addr = (void *)IOCFG_LB_BASE;
		break;
	case 4:
		reg_addr = (void *)IOCFG_BL_BASE;
		break;
	case 5:
		reg_addr = (void *)IOCFG_RB_BASE;
		break;
	case 6:
		reg_addr = (void *)IOCFG_RT_BASE;
		break;
	default:
		reg_addr = NULL;
		break;
	}

	return reg_addr;
}

static void gpio_set_spec_pull_pupd(gpio_t gpio, enum pull_enable enable,
				    enum pull_select select)
{
	void *reg1;
	void *reg2;
	int bit = gpio.bit;

	reg1 = gpio_find_reg_addr(gpio) + gpio.offset;
	reg2 = reg1 + (gpio.base & 0xf0);

	if (enable == GPIO_PULL_ENABLE) {
		if (select == GPIO_PULL_DOWN)
			setbits32(reg1, BIT(bit));
		else
			clrbits32(reg1, BIT(bit));
	}

	if (enable == GPIO_PULL_ENABLE) {
		setbits32(reg2, 1 << bit);
	} else {
		clrbits32(reg2, 1 << bit);
		clrbits32(reg2 + 0x010, BIT(bit));
	}
}

static void gpio_set_pull_pu_pd(gpio_t gpio, enum pull_enable enable,
				enum pull_select select)
{
	void *reg1;
	void *reg2;
	int bit = gpio.bit;

	reg1 = gpio_find_reg_addr(gpio) + gpio.offset;
	reg2 = reg1 - (gpio.base & 0xf0);

	if (enable == GPIO_PULL_ENABLE) {
		if (select == GPIO_PULL_DOWN) {
			clrbits32(reg1, BIT(bit));
			setbits32(reg2, BIT(bit));
		} else {
			clrbits32(reg2, BIT(bit));
			setbits32(reg1, BIT(bit));
		}
	} else {
		clrbits32(reg1, BIT(bit));
		clrbits32(reg2, BIT(bit));
	}
}

void gpio_set_pull(gpio_t gpio, enum pull_enable enable,
		   enum pull_select select)
{
	if (gpio.flag)
		gpio_set_spec_pull_pupd(gpio, enable, select);
	else
		gpio_set_pull_pu_pd(gpio, enable, select);
}

static inline bool is_valid_drv(uint8_t drv)
{
	return drv <= GPIO_DRV_16_MA;
}

static inline bool is_valid_drv_adv(enum gpio_drv_adv drv)
{
	return drv <= GPIO_DRV_ADV_1_MA && drv >= GPIO_DRV_ADV_125_UA;
}

int gpio_set_driving(gpio_t gpio, uint8_t drv)
{
	uint32_t mask;
	const struct gpio_drv_info *info = &gpio_driving_info[gpio.id];
	const struct gpio_drv_info *adv_info = &gpio_driving_adv_info[gpio.id];
	void *reg, *reg_adv;

	if (!is_valid_drv(drv))
		return -1;

	reg = gpio_find_reg_addr(gpio) + info->offset;
	reg_adv = gpio_find_reg_addr(gpio) + adv_info->offset;

	if (info->width == 0)
		return -1;

	mask = BIT(info->width) - 1;
	/* Check setting value is not beyond width */
	if ((uint32_t)drv > mask)
		return -1;

	clrsetbits32(reg, mask << info->shift, drv << info->shift);

	/* Disable EH if supported */
	if (adv_info->width != 0)
		clrbits32(reg_adv, BIT(adv_info->shift));

	return 0;
}

int gpio_get_driving(gpio_t gpio)
{
	const struct gpio_drv_info *info = &gpio_driving_info[gpio.id];
	void *reg;

	reg = gpio_find_reg_addr(gpio) + info->offset;
	if (info->width == 0)
		return -1;

	return (read32(reg) >> info->shift) & (BIT(info->width) - 1);
}

int gpio_set_driving_adv(gpio_t gpio, enum gpio_drv_adv drv)
{
	uint32_t mask;
	const struct gpio_drv_info *adv_info = &gpio_driving_adv_info[gpio.id];
	void *reg_adv;

	if (!is_valid_drv_adv(drv))
		return -1;

	reg_adv = gpio_find_reg_addr(gpio) + adv_info->offset;

	if (adv_info->width == 0)
		return -1;

	/* Not include EH bit (the lowest bit) */
	if ((uint32_t)drv > (BIT(adv_info->width - 1) - 1))
		return -1;

	mask = BIT(adv_info->width) - 1;
	/* EH enable */
	drv = (drv << 1) | BIT(0);

	clrsetbits32(reg_adv, mask << adv_info->shift, drv << adv_info->shift);

	return 0;
}

int gpio_get_driving_adv(gpio_t gpio)
{
	const struct gpio_drv_info *adv_info = &gpio_driving_adv_info[gpio.id];
	void *reg_adv;
	uint32_t drv;

	reg_adv = gpio_find_reg_addr(gpio) + adv_info->offset;
	if (adv_info->width == 0)
		return -1;

	drv = (read32(reg_adv) >> adv_info->shift) & (BIT(adv_info->width) - 1);

	/* Drop EH bit */
	return drv >> 1;
}