xref: /freebsd/sys/dev/amdgpio/amdgpio.h (revision 6b13d60bf49ee40626d7e3a5d5a80519f0067307)
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 
55 /* Register related macros */
56 #define	AMDGPIO_PIN_REGISTER(pin)	(pin * 4)
57 
58 #define	WAKE_INT_MASTER_REG		0xfc
59 #define	EOI_MASK			(1 << 29)
60 #define	WAKE_INT_STATUS_REG0		0x2f8
61 #define	WAKE_INT_STATUS_REG1		0x2fc
62 
63 /* Bit definition of 32 bits of each pin register */
64 #define	DB_TMR_OUT_OFF			0
65 #define	DB_TMR_OUT_UNIT_OFF		4
66 #define	DB_CNTRL_OFF			5
67 #define	DB_TMR_LARGE_OFF		7
68 #define	LEVEL_TRIG_OFF			8
69 #define	ACTIVE_LEVEL_OFF		9
70 #define	INTERRUPT_ENABLE_OFF		11
71 #define	INTERRUPT_MASK_OFF		12
72 #define	WAKE_CNTRL_OFF_S0I3		13
73 #define	WAKE_CNTRL_OFF_S3		14
74 #define	WAKE_CNTRL_OFF_S4		15
75 #define	PIN_STS_OFF			16
76 #define	DRV_STRENGTH_SEL_OFF		17
77 #define	PULL_UP_SEL_OFF			19
78 #define	PULL_UP_ENABLE_OFF		20
79 #define	PULL_DOWN_ENABLE_OFF		21
80 #define	OUTPUT_VALUE_OFF		22
81 #define	OUTPUT_ENABLE_OFF		23
82 #define	SW_CNTRL_IN_OFF			24
83 #define	SW_CNTRL_EN_OFF			25
84 #define	INTERRUPT_STS_OFF		28
85 #define	WAKE_STS_OFF			29
86 
87 #define	DB_TMR_OUT_MASK			0xFUL
88 #define	DB_CNTRL_MASK			0x3UL
89 #define	ACTIVE_LEVEL_MASK		0x3UL
90 #define	DRV_STRENGTH_SEL_MASK		0x3UL
91 
92 #define	DB_TYPE_NO_DEBOUNCE		0x0UL
93 #define	DB_TYPE_PRESERVE_LOW_GLITCH	0x1UL
94 #define	DB_TYPE_PRESERVE_HIGH_GLITCH	0x2UL
95 #define	DB_TYPE_REMOVE_GLITCH		0x3UL
96 
97 #define	EDGE_TRIGGER			0x0UL
98 #define	LEVEL_TRIGGER			0x1UL
99 
100 #define	ACTIVE_HIGH			0x0UL
101 #define	ACTIVE_LOW			0x1UL
102 #define	BOTH_EDGE			0x2UL
103 
104 #define	ENABLE_INTERRUPT		0x1UL
105 #define	DISABLE_INTERRUPT		0x0UL
106 
107 #define	ENABLE_INTERRUPT_MASK		0x0UL
108 #define	DISABLE_INTERRUPT_MASK		0x1UL
109 #define	CLR_INTR_STAT			0x1UL
110 
111 #define	BIT(bit)			(1 << bit)
112 #define	GPIO_PIN_INFO(p, n)		{ .pin_num = (p), .pin_name = (n) }
113 
114 struct pin_info {
115 	int pin_num;
116 	char *pin_name;
117 };
118 
119 /* Pins exposed to drivers */
120 static const struct pin_info kernzp_pins[] = {
121 	GPIO_PIN_INFO(0, "PIN_0"),
122 	GPIO_PIN_INFO(1, "PIN_1"),
123 	GPIO_PIN_INFO(2, "PIN_2"),
124 	GPIO_PIN_INFO(3, "PIN_3"),
125 	GPIO_PIN_INFO(4, "PIN_4"),
126 	GPIO_PIN_INFO(5, "PIN_5"),
127 	GPIO_PIN_INFO(6, "PIN_6"),
128 	GPIO_PIN_INFO(7, "PIN_7"),
129 	GPIO_PIN_INFO(8, "PIN_8"),
130 	GPIO_PIN_INFO(9, "PIN_9"),
131 	GPIO_PIN_INFO(10, "PIN_10"),
132 	GPIO_PIN_INFO(11, "PIN_11"),
133 	GPIO_PIN_INFO(12, "PIN_12"),
134 	GPIO_PIN_INFO(13, "PIN_13"),
135 	GPIO_PIN_INFO(14, "PIN_14"),
136 	GPIO_PIN_INFO(15, "PIN_15"),
137 	GPIO_PIN_INFO(16, "PIN_16"),
138 	GPIO_PIN_INFO(17, "PIN_17"),
139 	GPIO_PIN_INFO(18, "PIN_18"),
140 	GPIO_PIN_INFO(19, "PIN_19"),
141 	GPIO_PIN_INFO(20, "PIN_20"),
142 	GPIO_PIN_INFO(23, "PIN_23"),
143 	GPIO_PIN_INFO(24, "PIN_24"),
144 	GPIO_PIN_INFO(25, "PIN_25"),
145 	GPIO_PIN_INFO(26, "PIN_26"),
146 	GPIO_PIN_INFO(39, "PIN_39"),
147 	GPIO_PIN_INFO(40, "PIN_40"),
148 	GPIO_PIN_INFO(43, "PIN_43"),
149 	GPIO_PIN_INFO(46, "PIN_46"),
150 	GPIO_PIN_INFO(47, "PIN_47"),
151 	GPIO_PIN_INFO(48, "PIN_48"),
152 	GPIO_PIN_INFO(49, "PIN_49"),
153 	GPIO_PIN_INFO(50, "PIN_50"),
154 	GPIO_PIN_INFO(51, "PIN_51"),
155 	GPIO_PIN_INFO(52, "PIN_52"),
156 	GPIO_PIN_INFO(53, "PIN_53"),
157 	GPIO_PIN_INFO(54, "PIN_54"),
158 	GPIO_PIN_INFO(55, "PIN_55"),
159 	GPIO_PIN_INFO(56, "PIN_56"),
160 	GPIO_PIN_INFO(57, "PIN_57"),
161 	GPIO_PIN_INFO(58, "PIN_58"),
162 	GPIO_PIN_INFO(59, "PIN_59"),
163 	GPIO_PIN_INFO(60, "PIN_60"),
164 	GPIO_PIN_INFO(61, "PIN_61"),
165 	GPIO_PIN_INFO(62, "PIN_62"),
166 	GPIO_PIN_INFO(64, "PIN_64"),
167 	GPIO_PIN_INFO(65, "PIN_65"),
168 	GPIO_PIN_INFO(66, "PIN_66"),
169 	GPIO_PIN_INFO(68, "PIN_68"),
170 	GPIO_PIN_INFO(69, "PIN_69"),
171 	GPIO_PIN_INFO(70, "PIN_70"),
172 	GPIO_PIN_INFO(71, "PIN_71"),
173 	GPIO_PIN_INFO(72, "PIN_72"),
174 	GPIO_PIN_INFO(74, "PIN_74"),
175 	GPIO_PIN_INFO(75, "PIN_75"),
176 	GPIO_PIN_INFO(76, "PIN_76"),
177 	GPIO_PIN_INFO(84, "PIN_84"),
178 	GPIO_PIN_INFO(85, "PIN_85"),
179 	GPIO_PIN_INFO(86, "PIN_86"),
180 	GPIO_PIN_INFO(87, "PIN_87"),
181 	GPIO_PIN_INFO(88, "PIN_88"),
182 	GPIO_PIN_INFO(89, "PIN_89"),
183 	GPIO_PIN_INFO(90, "PIN_90"),
184 	GPIO_PIN_INFO(91, "PIN_91"),
185 	GPIO_PIN_INFO(92, "PIN_92"),
186 	GPIO_PIN_INFO(93, "PIN_93"),
187 	GPIO_PIN_INFO(95, "PIN_95"),
188 	GPIO_PIN_INFO(96, "PIN_96"),
189 	GPIO_PIN_INFO(97, "PIN_97"),
190 	GPIO_PIN_INFO(98, "PIN_98"),
191 	GPIO_PIN_INFO(99, "PIN_99"),
192 	GPIO_PIN_INFO(100, "PIN_100"),
193 	GPIO_PIN_INFO(101, "PIN_101"),
194 	GPIO_PIN_INFO(102, "PIN_102"),
195 	GPIO_PIN_INFO(113, "PIN_113"),
196 	GPIO_PIN_INFO(114, "PIN_114"),
197 	GPIO_PIN_INFO(115, "PIN_115"),
198 	GPIO_PIN_INFO(116, "PIN_116"),
199 	GPIO_PIN_INFO(117, "PIN_117"),
200 	GPIO_PIN_INFO(118, "PIN_118"),
201 	GPIO_PIN_INFO(119, "PIN_119"),
202 	GPIO_PIN_INFO(120, "PIN_120"),
203 	GPIO_PIN_INFO(121, "PIN_121"),
204 	GPIO_PIN_INFO(122, "PIN_122"),
205 	GPIO_PIN_INFO(126, "PIN_126"),
206 	GPIO_PIN_INFO(129, "PIN_129"),
207 	GPIO_PIN_INFO(130, "PIN_130"),
208 	GPIO_PIN_INFO(131, "PIN_131"),
209 	GPIO_PIN_INFO(132, "PIN_132"),
210 	GPIO_PIN_INFO(133, "PIN_133"),
211 	GPIO_PIN_INFO(135, "PIN_135"),
212 	GPIO_PIN_INFO(136, "PIN_136"),
213 	GPIO_PIN_INFO(137, "PIN_137"),
214 	GPIO_PIN_INFO(138, "PIN_138"),
215 	GPIO_PIN_INFO(139, "PIN_139"),
216 	GPIO_PIN_INFO(140, "PIN_140"),
217 	GPIO_PIN_INFO(141, "PIN_141"),
218 	GPIO_PIN_INFO(142, "PIN_142"),
219 	GPIO_PIN_INFO(143, "PIN_143"),
220 	GPIO_PIN_INFO(144, "PIN_144"),
221 	GPIO_PIN_INFO(145, "PIN_145"),
222 	GPIO_PIN_INFO(146, "PIN_146"),
223 	GPIO_PIN_INFO(147, "PIN_147"),
224 	GPIO_PIN_INFO(148, "PIN_148"),
225 	GPIO_PIN_INFO(166, "PIN_166"),
226 	GPIO_PIN_INFO(167, "PIN_167"),
227 	GPIO_PIN_INFO(168, "PIN_168"),
228 	GPIO_PIN_INFO(169, "PIN_169"),
229 	GPIO_PIN_INFO(170, "PIN_170"),
230 	GPIO_PIN_INFO(171, "PIN_171"),
231 	GPIO_PIN_INFO(172, "PIN_172"),
232 	GPIO_PIN_INFO(173, "PIN_173"),
233 	GPIO_PIN_INFO(174, "PIN_174"),
234 	GPIO_PIN_INFO(175, "PIN_175"),
235 	GPIO_PIN_INFO(176, "PIN_176"),
236 	GPIO_PIN_INFO(177, "PIN_177"),
237 };
238 
239 #define	AMD_GPIO_PINS_EXPOSED	nitems(kernzp_pins)
240 
241 static const unsigned i2c0_pins[] = {145, 146};
242 static const unsigned i2c1_pins[] = {147, 148};
243 static const unsigned i2c2_pins[] = {113, 114};
244 static const unsigned i2c3_pins[] = {19, 20};
245 static const unsigned i2c4_pins[] = {149, 150};
246 static const unsigned i2c5_pins[] = {151, 152};
247 
248 static const unsigned uart0_pins[] = {135, 136, 137, 138, 139};
249 static const unsigned uart1_pins[] = {140, 141, 142, 143, 144};
250 
251 struct amd_pingroup {
252 	const char *name;
253 	const unsigned *pins;
254 	unsigned npins;
255 };
256 
257 static const struct amd_pingroup kernzp_groups[] = {
258 	{
259 		.name = "i2c0",
260 		.pins = i2c0_pins,
261 		.npins = 2,
262 	},
263 	{
264 		.name = "i2c1",
265 		.pins = i2c1_pins,
266 		.npins = 2,
267 	},
268 	{
269 		.name = "i2c2",
270 		.pins = i2c2_pins,
271 		.npins = 2,
272 	},
273 	{
274 		.name = "i2c3",
275 		.pins = i2c3_pins,
276 		.npins = 2,
277 	},
278 	{
279 		.name = "i2c4",
280 		.pins = i2c4_pins,
281 		.npins = 2,
282 	},
283 	{
284 		.name = "i2c5",
285 		.pins = i2c5_pins,
286 		.npins = 2,
287 	},
288 	{
289 		.name = "uart0",
290 		.pins = uart0_pins,
291 		.npins = 5,
292 	},
293 	{
294 		.name = "uart1",
295 		.pins = uart1_pins,
296 		.npins = 5,
297 	},
298 };
299 
300 /* Macros for driver mutex locking */
301 #define	AMDGPIO_LOCK_INIT(_sc)	\
302 	mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev),	\
303 		"amdgpio", MTX_SPIN)
304 #define	AMDGPIO_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx)
305 #define	AMDGPIO_LOCK(_sc)		mtx_lock_spin(&(_sc)->sc_mtx)
306 #define	AMDGPIO_UNLOCK(_sc)		mtx_unlock_spin(&(_sc)->sc_mtx)
307 #define	AMDGPIO_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
308 #define	AMDGPIO_ASSERT_UNLOCKED(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED)
309 
310 struct amdgpio_softc {
311 	ACPI_HANDLE		sc_handle;
312 	device_t		sc_dev;
313 	device_t		sc_busdev;
314 	const char*		sc_bank_prefix;
315 	int			sc_nbanks;
316 	int			sc_npins;
317 	int			sc_ngroups;
318 	struct mtx		sc_mtx;
319 	struct resource		*sc_res[AMD_GPIO_NUM_PIN_BANK + 1];
320 	bus_space_tag_t		sc_bst;
321 	bus_space_handle_t	sc_bsh;
322 	struct gpio_pin		sc_gpio_pins[AMD_GPIO_PINS_MAX];
323 	const struct pin_info	*sc_pin_info;
324 	const struct amd_pingroup *sc_groups;
325 };
326 
327 struct amdgpio_sysctl {
328 	struct amdgpio_softc	*sc;
329 	uint32_t		pin;
330 };
331