xref: /freebsd/sys/dev/amdgpio/amdgpio.h (revision a4d738d7830576e5a62317bb67cc2a949cc7ba5a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018 Advanced Micro Devices
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifdef	DEBUG
30 #define	dprintf(fmt, args...) do {	\
31 	printf("%s(): ", __func__);	\
32 	printf(fmt,##args);		\
33 } while (0)
34 #else
35 #define	dprintf(fmt, args...)
36 #endif
37 
38 #define	AMD_GPIO_PREFIX			"AMDGPIO"
39 
40 #define	AMD_GPIO_NUM_PIN_BANK		4
41 #define	AMD_GPIO_PINS_PER_BANK		64
42 #define	AMD_GPIO_PINS_MAX		256 /* 4 banks * 64 pins */
43 
44 /* Number of pins in each bank */
45 #define	AMD_GPIO_PINS_BANK0		63
46 #define	AMD_GPIO_PINS_BANK1		64
47 #define	AMD_GPIO_PINS_BANK2		56
48 #define	AMD_GPIO_PINS_BANK3		32
49 #define	AMD_GPIO_PIN_PRESENT		(AMD_GPIO_PINS_BANK0 + \
50 					AMD_GPIO_PINS_BANK1 + \
51 					AMD_GPIO_PINS_BANK2 + \
52 					AMD_GPIO_PINS_BANK3)
53 #define	AMDGPIO_DEFAULT_CAPS		(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
54     GPIO_PIN_PULLDOWN | GPIO_PIN_PULLUP)
55 
56 /* Register related macros */
57 #define	AMDGPIO_PIN_REGISTER(pin)	(pin * 4)
58 
59 #define	WAKE_INT_MASTER_REG		0xfc
60 #define	EOI_MASK			(1 << 29)
61 #define	WAKE_INT_STATUS_REG0		0x2f8
62 #define	WAKE_INT_STATUS_REG1		0x2fc
63 
64 /* Bit definition of 32 bits of each pin register */
65 #define	DB_TMR_OUT_OFF			0
66 #define	DB_TMR_OUT_UNIT_OFF		4
67 #define	DB_CNTRL_OFF			5
68 #define	DB_TMR_LARGE_OFF		7
69 #define	LEVEL_TRIG_OFF			8
70 #define	ACTIVE_LEVEL_OFF		9
71 #define	INTERRUPT_ENABLE_OFF		11
72 #define	INTERRUPT_MASK_OFF		12
73 #define	WAKE_CNTRL_OFF_S0I3		13
74 #define	WAKE_CNTRL_OFF_S3		14
75 #define	WAKE_CNTRL_OFF_S4		15
76 #define	PIN_STS_OFF			16
77 #define	DRV_STRENGTH_SEL_OFF		17
78 #define	PULL_UP_SEL_OFF			19
79 #define	PULL_UP_ENABLE_OFF		20
80 #define	PULL_DOWN_ENABLE_OFF		21
81 #define	OUTPUT_VALUE_OFF		22
82 #define	OUTPUT_ENABLE_OFF		23
83 #define	SW_CNTRL_IN_OFF			24
84 #define	SW_CNTRL_EN_OFF			25
85 #define	INTERRUPT_STS_OFF		28
86 #define	WAKE_STS_OFF			29
87 
88 #define	UNSERVICED_INTERRUPT_MASK			\
89 	((1 << INTERRUPT_STS_OFF) | (1 << WAKE_STS_OFF))
90 
91 #define	DB_TMR_OUT_MASK			0xFUL
92 #define	DB_CNTRL_MASK			0x3UL
93 #define	ACTIVE_LEVEL_MASK		0x3UL
94 #define	DRV_STRENGTH_SEL_MASK		0x3UL
95 
96 #define	DB_TYPE_NO_DEBOUNCE		0x0UL
97 #define	DB_TYPE_PRESERVE_LOW_GLITCH	0x1UL
98 #define	DB_TYPE_PRESERVE_HIGH_GLITCH	0x2UL
99 #define	DB_TYPE_REMOVE_GLITCH		0x3UL
100 
101 #define	EDGE_TRIGGER			0x0UL
102 #define	LEVEL_TRIGGER			0x1UL
103 
104 #define	ACTIVE_HIGH			0x0UL
105 #define	ACTIVE_LOW			0x1UL
106 #define	BOTH_EDGE			0x2UL
107 
108 #define	ENABLE_INTERRUPT		0x1UL
109 #define	DISABLE_INTERRUPT		0x0UL
110 
111 #define	ENABLE_INTERRUPT_MASK		0x0UL
112 #define	DISABLE_INTERRUPT_MASK		0x1UL
113 #define	CLR_INTR_STAT			0x1UL
114 
115 #define	BIT(bit)			(1 << bit)
116 #define	GPIO_PIN_INFO(p, n)		{ .pin_num = (p), .pin_name = (n) }
117 
118 struct pin_info {
119 	int pin_num;
120 	char *pin_name;
121 };
122 
123 /* Pins exposed to drivers */
124 static const struct pin_info kernzp_pins[] = {
125 	GPIO_PIN_INFO(0, "PIN_0"),
126 	GPIO_PIN_INFO(1, "PIN_1"),
127 	GPIO_PIN_INFO(2, "PIN_2"),
128 	GPIO_PIN_INFO(3, "PIN_3"),
129 	GPIO_PIN_INFO(4, "PIN_4"),
130 	GPIO_PIN_INFO(5, "PIN_5"),
131 	GPIO_PIN_INFO(6, "PIN_6"),
132 	GPIO_PIN_INFO(7, "PIN_7"),
133 	GPIO_PIN_INFO(8, "PIN_8"),
134 	GPIO_PIN_INFO(9, "PIN_9"),
135 	GPIO_PIN_INFO(10, "PIN_10"),
136 	GPIO_PIN_INFO(11, "PIN_11"),
137 	GPIO_PIN_INFO(12, "PIN_12"),
138 	GPIO_PIN_INFO(13, "PIN_13"),
139 	GPIO_PIN_INFO(14, "PIN_14"),
140 	GPIO_PIN_INFO(15, "PIN_15"),
141 	GPIO_PIN_INFO(16, "PIN_16"),
142 	GPIO_PIN_INFO(17, "PIN_17"),
143 	GPIO_PIN_INFO(18, "PIN_18"),
144 	GPIO_PIN_INFO(19, "PIN_19"),
145 	GPIO_PIN_INFO(20, "PIN_20"),
146 	GPIO_PIN_INFO(23, "PIN_23"),
147 	GPIO_PIN_INFO(24, "PIN_24"),
148 	GPIO_PIN_INFO(25, "PIN_25"),
149 	GPIO_PIN_INFO(26, "PIN_26"),
150 	GPIO_PIN_INFO(39, "PIN_39"),
151 	GPIO_PIN_INFO(40, "PIN_40"),
152 	GPIO_PIN_INFO(43, "PIN_43"),
153 	GPIO_PIN_INFO(46, "PIN_46"),
154 	GPIO_PIN_INFO(47, "PIN_47"),
155 	GPIO_PIN_INFO(48, "PIN_48"),
156 	GPIO_PIN_INFO(49, "PIN_49"),
157 	GPIO_PIN_INFO(50, "PIN_50"),
158 	GPIO_PIN_INFO(51, "PIN_51"),
159 	GPIO_PIN_INFO(52, "PIN_52"),
160 	GPIO_PIN_INFO(53, "PIN_53"),
161 	GPIO_PIN_INFO(54, "PIN_54"),
162 	GPIO_PIN_INFO(55, "PIN_55"),
163 	GPIO_PIN_INFO(56, "PIN_56"),
164 	GPIO_PIN_INFO(57, "PIN_57"),
165 	GPIO_PIN_INFO(58, "PIN_58"),
166 	GPIO_PIN_INFO(59, "PIN_59"),
167 	GPIO_PIN_INFO(60, "PIN_60"),
168 	GPIO_PIN_INFO(61, "PIN_61"),
169 	GPIO_PIN_INFO(62, "PIN_62"),
170 	GPIO_PIN_INFO(64, "PIN_64"),
171 	GPIO_PIN_INFO(65, "PIN_65"),
172 	GPIO_PIN_INFO(66, "PIN_66"),
173 	GPIO_PIN_INFO(68, "PIN_68"),
174 	GPIO_PIN_INFO(69, "PIN_69"),
175 	GPIO_PIN_INFO(70, "PIN_70"),
176 	GPIO_PIN_INFO(71, "PIN_71"),
177 	GPIO_PIN_INFO(72, "PIN_72"),
178 	GPIO_PIN_INFO(74, "PIN_74"),
179 	GPIO_PIN_INFO(75, "PIN_75"),
180 	GPIO_PIN_INFO(76, "PIN_76"),
181 	GPIO_PIN_INFO(84, "PIN_84"),
182 	GPIO_PIN_INFO(85, "PIN_85"),
183 	GPIO_PIN_INFO(86, "PIN_86"),
184 	GPIO_PIN_INFO(87, "PIN_87"),
185 	GPIO_PIN_INFO(88, "PIN_88"),
186 	GPIO_PIN_INFO(89, "PIN_89"),
187 	GPIO_PIN_INFO(90, "PIN_90"),
188 	GPIO_PIN_INFO(91, "PIN_91"),
189 	GPIO_PIN_INFO(92, "PIN_92"),
190 	GPIO_PIN_INFO(93, "PIN_93"),
191 	GPIO_PIN_INFO(95, "PIN_95"),
192 	GPIO_PIN_INFO(96, "PIN_96"),
193 	GPIO_PIN_INFO(97, "PIN_97"),
194 	GPIO_PIN_INFO(98, "PIN_98"),
195 	GPIO_PIN_INFO(99, "PIN_99"),
196 	GPIO_PIN_INFO(100, "PIN_100"),
197 	GPIO_PIN_INFO(101, "PIN_101"),
198 	GPIO_PIN_INFO(102, "PIN_102"),
199 	GPIO_PIN_INFO(113, "PIN_113"),
200 	GPIO_PIN_INFO(114, "PIN_114"),
201 	GPIO_PIN_INFO(115, "PIN_115"),
202 	GPIO_PIN_INFO(116, "PIN_116"),
203 	GPIO_PIN_INFO(117, "PIN_117"),
204 	GPIO_PIN_INFO(118, "PIN_118"),
205 	GPIO_PIN_INFO(119, "PIN_119"),
206 	GPIO_PIN_INFO(120, "PIN_120"),
207 	GPIO_PIN_INFO(121, "PIN_121"),
208 	GPIO_PIN_INFO(122, "PIN_122"),
209 	GPIO_PIN_INFO(126, "PIN_126"),
210 	GPIO_PIN_INFO(129, "PIN_129"),
211 	GPIO_PIN_INFO(130, "PIN_130"),
212 	GPIO_PIN_INFO(131, "PIN_131"),
213 	GPIO_PIN_INFO(132, "PIN_132"),
214 	GPIO_PIN_INFO(133, "PIN_133"),
215 	GPIO_PIN_INFO(135, "PIN_135"),
216 	GPIO_PIN_INFO(136, "PIN_136"),
217 	GPIO_PIN_INFO(137, "PIN_137"),
218 	GPIO_PIN_INFO(138, "PIN_138"),
219 	GPIO_PIN_INFO(139, "PIN_139"),
220 	GPIO_PIN_INFO(140, "PIN_140"),
221 	GPIO_PIN_INFO(141, "PIN_141"),
222 	GPIO_PIN_INFO(142, "PIN_142"),
223 	GPIO_PIN_INFO(143, "PIN_143"),
224 	GPIO_PIN_INFO(144, "PIN_144"),
225 	GPIO_PIN_INFO(145, "PIN_145"),
226 	GPIO_PIN_INFO(146, "PIN_146"),
227 	GPIO_PIN_INFO(147, "PIN_147"),
228 	GPIO_PIN_INFO(148, "PIN_148"),
229 	GPIO_PIN_INFO(166, "PIN_166"),
230 	GPIO_PIN_INFO(167, "PIN_167"),
231 	GPIO_PIN_INFO(168, "PIN_168"),
232 	GPIO_PIN_INFO(169, "PIN_169"),
233 	GPIO_PIN_INFO(170, "PIN_170"),
234 	GPIO_PIN_INFO(171, "PIN_171"),
235 	GPIO_PIN_INFO(172, "PIN_172"),
236 	GPIO_PIN_INFO(173, "PIN_173"),
237 	GPIO_PIN_INFO(174, "PIN_174"),
238 	GPIO_PIN_INFO(175, "PIN_175"),
239 	GPIO_PIN_INFO(176, "PIN_176"),
240 	GPIO_PIN_INFO(177, "PIN_177"),
241 };
242 
243 #define	AMD_GPIO_PINS_EXPOSED	nitems(kernzp_pins)
244 
245 static const unsigned i2c0_pins[] = {145, 146};
246 static const unsigned i2c1_pins[] = {147, 148};
247 static const unsigned i2c2_pins[] = {113, 114};
248 static const unsigned i2c3_pins[] = {19, 20};
249 static const unsigned i2c4_pins[] = {149, 150};
250 static const unsigned i2c5_pins[] = {151, 152};
251 
252 static const unsigned uart0_pins[] = {135, 136, 137, 138, 139};
253 static const unsigned uart1_pins[] = {140, 141, 142, 143, 144};
254 
255 struct amd_pingroup {
256 	const char *name;
257 	const unsigned *pins;
258 	unsigned npins;
259 };
260 
261 static const struct amd_pingroup kernzp_groups[] = {
262 	{
263 		.name = "i2c0",
264 		.pins = i2c0_pins,
265 		.npins = 2,
266 	},
267 	{
268 		.name = "i2c1",
269 		.pins = i2c1_pins,
270 		.npins = 2,
271 	},
272 	{
273 		.name = "i2c2",
274 		.pins = i2c2_pins,
275 		.npins = 2,
276 	},
277 	{
278 		.name = "i2c3",
279 		.pins = i2c3_pins,
280 		.npins = 2,
281 	},
282 	{
283 		.name = "i2c4",
284 		.pins = i2c4_pins,
285 		.npins = 2,
286 	},
287 	{
288 		.name = "i2c5",
289 		.pins = i2c5_pins,
290 		.npins = 2,
291 	},
292 	{
293 		.name = "uart0",
294 		.pins = uart0_pins,
295 		.npins = 5,
296 	},
297 	{
298 		.name = "uart1",
299 		.pins = uart1_pins,
300 		.npins = 5,
301 	},
302 };
303 
304 /* Macros for driver mutex locking */
305 #define	AMDGPIO_LOCK_INIT(_sc)	\
306 	mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev),	\
307 		"amdgpio", MTX_SPIN)
308 #define	AMDGPIO_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx)
309 #define	AMDGPIO_LOCK(_sc)		mtx_lock_spin(&(_sc)->sc_mtx)
310 #define	AMDGPIO_UNLOCK(_sc)		mtx_unlock_spin(&(_sc)->sc_mtx)
311 #define	AMDGPIO_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
312 #define	AMDGPIO_ASSERT_UNLOCKED(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED)
313 
314 struct amdgpio_softc {
315 	ACPI_HANDLE		sc_handle;
316 	device_t		sc_dev;
317 	device_t		sc_busdev;
318 	const char*		sc_bank_prefix;
319 	int			sc_nbanks;
320 	int			sc_npins;
321 	int			sc_ngroups;
322 	struct mtx		sc_mtx;
323 	struct resource		*sc_res[2];
324 	bus_space_tag_t		sc_bst;
325 	bus_space_handle_t	sc_bsh;
326 	struct gpio_pin		sc_gpio_pins[AMD_GPIO_PINS_MAX];
327 	const struct pin_info	*sc_pin_info;
328 	const struct amd_pingroup *sc_groups;
329 	void			*sc_intr_handle;
330 };
331 
332 struct amdgpio_sysctl {
333 	struct amdgpio_softc	*sc;
334 	uint32_t		pin;
335 };
336