1 /*
2  * Copyright (c) 2020 Henrik Brix Andersen <henrik@brixandersen.dk>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT xlnx_xps_gpio_1_00_a
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/irq.h>
12 #include <zephyr/sys/sys_io.h>
13 
14 #include <zephyr/drivers/gpio/gpio_utils.h>
15 
16 /* AXI GPIO v2 register offsets (See Xilinx PG144 for details) */
17 #define GPIO_DATA_OFFSET  0x0000
18 #define GPIO_TRI_OFFSET   0x0004
19 #define GPIO2_OFFSET      0x0008
20 #define GPIO2_DATA_OFFSET 0x0008
21 #define GPIO2_TRI_OFFSET  0x000c
22 #define GIER_OFFSET       0x011c
23 #define IPISR_OFFSET      0x0120
24 #define IPIER_OFFSET      0x0128
25 
26 /* GIER bit definitions */
27 #define GIER_GIE BIT(31)
28 
29 /* IPISR and IPIER bit definitions */
30 #define IPIXX_CH1_IE BIT(0)
31 #define IPIXX_CH2_IE BIT(1)
32 
33 /* Maximum number of GPIOs supported per channel */
34 #define MAX_GPIOS 32
35 
36 struct gpio_xlnx_axi_config {
37 	/* gpio_driver_config needs to be first */
38 	struct gpio_driver_config common;
39 	mm_reg_t base;
40 	uint8_t channel;
41 	bool all_inputs: 1;
42 	bool all_outputs: 1;
43 	bool interrupts_available: 1;
44 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
45 	void (*irq_config_func)(const struct device *dev);
46 #endif
47 };
48 
49 struct gpio_xlnx_axi_data {
50 	/* gpio_driver_data needs to be first */
51 	struct gpio_driver_data common;
52 	/* Shadow registers for data out and tristate */
53 	uint32_t dout;
54 	uint32_t tri;
55 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
56 	uint32_t previous_data_reading;
57 	sys_slist_t callbacks;
58 	uint32_t rising_edge_interrupts;
59 	uint32_t falling_edge_interrupts;
60 	/* Workaround to handle channel 2 interrupts from channel 1*/
61 	const struct device *other_channel_device;
62 #endif
63 };
64 
gpio_xlnx_axi_read_data(const struct device * dev)65 static inline uint32_t gpio_xlnx_axi_read_data(const struct device *dev)
66 {
67 	const struct gpio_xlnx_axi_config *config = dev->config;
68 
69 	return sys_read32(config->base + (config->channel * GPIO2_OFFSET) + GPIO_DATA_OFFSET);
70 }
71 
gpio_xlnx_axi_write_data(const struct device * dev,uint32_t val)72 static inline void gpio_xlnx_axi_write_data(const struct device *dev, uint32_t val)
73 {
74 	const struct gpio_xlnx_axi_config *config = dev->config;
75 
76 	sys_write32(val, config->base + (config->channel * GPIO2_OFFSET) + GPIO_DATA_OFFSET);
77 }
78 
gpio_xlnx_axi_write_tri(const struct device * dev,uint32_t val)79 static inline void gpio_xlnx_axi_write_tri(const struct device *dev, uint32_t val)
80 {
81 	const struct gpio_xlnx_axi_config *config = dev->config;
82 
83 	sys_write32(val, config->base + (config->channel * GPIO2_OFFSET) + GPIO_TRI_OFFSET);
84 }
85 
gpio_xlnx_axi_pin_configure(const struct device * dev,gpio_pin_t pin,gpio_flags_t flags)86 static int gpio_xlnx_axi_pin_configure(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags)
87 {
88 	const struct gpio_xlnx_axi_config *config = dev->config;
89 	struct gpio_xlnx_axi_data *data = dev->data;
90 	unsigned int key;
91 
92 	if (!(BIT(pin) & config->common.port_pin_mask)) {
93 		return -EINVAL;
94 	}
95 
96 	if (((flags & GPIO_INPUT) != 0) && ((flags & GPIO_OUTPUT) != 0)) {
97 		return -ENOTSUP;
98 	}
99 
100 	if ((flags & GPIO_SINGLE_ENDED) != 0) {
101 		return -ENOTSUP;
102 	}
103 
104 	if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0) {
105 		return -ENOTSUP;
106 	}
107 
108 	if (((flags & GPIO_INPUT) != 0) && config->all_outputs) {
109 		return -ENOTSUP;
110 	}
111 
112 	if (((flags & GPIO_OUTPUT) != 0) && config->all_inputs) {
113 		return -ENOTSUP;
114 	}
115 
116 	key = irq_lock();
117 
118 	switch (flags & GPIO_DIR_MASK) {
119 	case GPIO_INPUT:
120 		data->tri |= BIT(pin);
121 		break;
122 	case GPIO_OUTPUT:
123 		if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
124 			data->dout |= BIT(pin);
125 		} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
126 			data->dout &= ~BIT(pin);
127 		}
128 		data->tri &= ~BIT(pin);
129 		break;
130 	default:
131 		return -ENOTSUP;
132 	}
133 
134 	gpio_xlnx_axi_write_data(dev, data->dout);
135 	gpio_xlnx_axi_write_tri(dev, data->tri);
136 
137 	irq_unlock(key);
138 
139 	return 0;
140 }
141 
gpio_xlnx_axi_port_get_raw(const struct device * dev,gpio_port_value_t * value)142 static int gpio_xlnx_axi_port_get_raw(const struct device *dev, gpio_port_value_t *value)
143 {
144 	*value = gpio_xlnx_axi_read_data(dev);
145 	return 0;
146 }
147 
gpio_xlnx_axi_port_set_masked_raw(const struct device * dev,gpio_port_pins_t mask,gpio_port_value_t value)148 static int gpio_xlnx_axi_port_set_masked_raw(const struct device *dev, gpio_port_pins_t mask,
149 					     gpio_port_value_t value)
150 {
151 	struct gpio_xlnx_axi_data *data = dev->data;
152 	unsigned int key;
153 
154 	key = irq_lock();
155 	data->dout = (data->dout & ~mask) | (mask & value);
156 	gpio_xlnx_axi_write_data(dev, data->dout);
157 	irq_unlock(key);
158 
159 	return 0;
160 }
161 
gpio_xlnx_axi_port_set_bits_raw(const struct device * dev,gpio_port_pins_t pins)162 static int gpio_xlnx_axi_port_set_bits_raw(const struct device *dev, gpio_port_pins_t pins)
163 {
164 	struct gpio_xlnx_axi_data *data = dev->data;
165 	unsigned int key;
166 
167 	key = irq_lock();
168 	data->dout |= pins;
169 	gpio_xlnx_axi_write_data(dev, data->dout);
170 	irq_unlock(key);
171 
172 	return 0;
173 }
174 
gpio_xlnx_axi_port_clear_bits_raw(const struct device * dev,gpio_port_pins_t pins)175 static int gpio_xlnx_axi_port_clear_bits_raw(const struct device *dev, gpio_port_pins_t pins)
176 {
177 	struct gpio_xlnx_axi_data *data = dev->data;
178 	unsigned int key;
179 
180 	key = irq_lock();
181 	data->dout &= ~pins;
182 	gpio_xlnx_axi_write_data(dev, data->dout);
183 	irq_unlock(key);
184 
185 	return 0;
186 }
187 
gpio_xlnx_axi_port_toggle_bits(const struct device * dev,gpio_port_pins_t pins)188 static int gpio_xlnx_axi_port_toggle_bits(const struct device *dev, gpio_port_pins_t pins)
189 {
190 	struct gpio_xlnx_axi_data *data = dev->data;
191 	unsigned int key;
192 
193 	key = irq_lock();
194 	data->dout ^= pins;
195 	gpio_xlnx_axi_write_data(dev, data->dout);
196 	irq_unlock(key);
197 
198 	return 0;
199 }
200 
201 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
202 /**
203  * Enables interrupts for the given pins on the channel
204  * The axi gpio can only enable interrupts for an entire port, so we need to track
205  * the pins and modes ourselves.
206  */
gpio_xlnx_axi_pin_interrupt_configure(const struct device * dev,gpio_pin_t pin,enum gpio_int_mode mode,enum gpio_int_trig trig)207 static int gpio_xlnx_axi_pin_interrupt_configure(const struct device *dev, gpio_pin_t pin,
208 						 enum gpio_int_mode mode, enum gpio_int_trig trig)
209 {
210 	const struct gpio_xlnx_axi_config *config = dev->config;
211 	struct gpio_xlnx_axi_data *data = dev->data;
212 	const uint32_t pin_mask = BIT(pin);
213 	const uint32_t chan_mask = BIT(config->channel);
214 	unsigned int key;
215 	uint32_t enabled_interrupts;
216 
217 	if (!config->interrupts_available) {
218 		return -ENOTSUP;
219 	}
220 
221 	if ((mode & GPIO_INT_ENABLE) && !(mode & GPIO_INT_EDGE)) {
222 		/* only edge detection is supported */
223 		return -ENOTSUP;
224 	}
225 
226 	key = irq_lock();
227 
228 	data->rising_edge_interrupts &= ~pin_mask;
229 	data->falling_edge_interrupts &= ~pin_mask;
230 
231 	if (mode & GPIO_INT_ENABLE) {
232 		if (trig & GPIO_INT_HIGH_1) {
233 			data->rising_edge_interrupts |= pin_mask;
234 		}
235 		if (trig & GPIO_INT_LOW_0) {
236 			data->falling_edge_interrupts |= pin_mask;
237 		}
238 	}
239 
240 	/* if there's at least one pin interrupt enabled on the channel, enable the interrupts
241 	 * for that entire channel without changing the other channel
242 	 */
243 	enabled_interrupts = sys_read32(config->base + IPIER_OFFSET);
244 	if (data->rising_edge_interrupts || data->falling_edge_interrupts) {
245 		if (!(enabled_interrupts & chan_mask)) {
246 			/* Clear any pending interrupts and update last state before enabling
247 			 * interrupt
248 			 */
249 			if (sys_read32(config->base + IPISR_OFFSET) & chan_mask) {
250 				sys_write32(chan_mask, config->base + IPISR_OFFSET);
251 			}
252 			data->previous_data_reading = gpio_xlnx_axi_read_data(dev);
253 
254 			enabled_interrupts |= chan_mask;
255 		}
256 	} else {
257 		enabled_interrupts &= ~chan_mask;
258 	}
259 	sys_write32(enabled_interrupts, config->base + IPIER_OFFSET);
260 
261 	irq_unlock(key);
262 	return 0;
263 }
264 #endif
265 
266 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
gpio_xlnx_axi_manage_callback(const struct device * dev,struct gpio_callback * callback,bool set)267 static int gpio_xlnx_axi_manage_callback(const struct device *dev, struct gpio_callback *callback,
268 					 bool set)
269 {
270 	struct gpio_xlnx_axi_data *data = dev->data;
271 
272 	return gpio_manage_callback(&data->callbacks, callback, set);
273 }
274 #endif
275 
276 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
277 /**
278  * Returns the pins on this devices channel which changed and also have an interrupt enabled on that
279  * pin. Also clears the pending interrupt for that channel.
280  */
gpio_xlnx_axi_get_pending_int(const struct device * dev)281 static uint32_t gpio_xlnx_axi_get_pending_int(const struct device *dev)
282 {
283 	const struct gpio_xlnx_axi_config *config = dev->config;
284 	struct gpio_xlnx_axi_data *data = dev->data;
285 	const uint32_t chan_mask = BIT(config->channel);
286 	unsigned int key;
287 	uint32_t interrupt_flags;
288 	uint32_t current_data;
289 	uint32_t changed_pins;
290 	uint32_t changed_and_rising_edge;
291 	uint32_t changed_and_falling_edge;
292 	uint32_t interrupts;
293 
294 	key = irq_lock();
295 
296 	/* make sure interrupt was for this channel */
297 	interrupt_flags = sys_read32(config->base + IPISR_OFFSET);
298 
299 	if (!(interrupt_flags & chan_mask)) {
300 		irq_unlock(key);
301 		return 0;
302 	}
303 
304 	/* clear pending interrupt for the whole channel */
305 	sys_write32(chan_mask, config->base + IPISR_OFFSET);
306 
307 	/* find which pins changed and also have an interrupt enabled */
308 	current_data = gpio_xlnx_axi_read_data(dev);
309 	changed_pins = current_data ^ data->previous_data_reading;
310 	data->previous_data_reading = current_data;
311 	changed_and_rising_edge = (changed_pins & current_data);
312 	changed_and_falling_edge = (changed_pins & ~current_data);
313 	interrupts = (changed_and_rising_edge & data->rising_edge_interrupts) |
314 		     (changed_and_falling_edge & data->falling_edge_interrupts);
315 
316 	irq_unlock(key);
317 	return interrupts;
318 }
319 #endif
320 
321 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
gpio_xlnx_axi_isr(const struct device * dev)322 static void gpio_xlnx_axi_isr(const struct device *dev)
323 {
324 	struct gpio_xlnx_axi_data *data = dev->data;
325 
326 	gpio_fire_callbacks(&data->callbacks, dev, gpio_xlnx_axi_get_pending_int(dev));
327 
328 	/* Since both channels use the same interrupt, only the first channel registers the ISR.
329 	 * If the second channel is also enabled, then check for any events on it as well.
330 	 */
331 	if (data->other_channel_device) {
332 		struct gpio_xlnx_axi_data *other_data = data->other_channel_device->data;
333 
334 		gpio_fire_callbacks(&other_data->callbacks, data->other_channel_device,
335 				    gpio_xlnx_axi_get_pending_int(data->other_channel_device));
336 	}
337 }
338 #endif
339 
gpio_xlnx_axi_init(const struct device * dev)340 static int gpio_xlnx_axi_init(const struct device *dev)
341 {
342 	struct gpio_xlnx_axi_data *data = dev->data;
343 
344 	gpio_xlnx_axi_write_data(dev, data->dout);
345 	gpio_xlnx_axi_write_tri(dev, data->tri);
346 
347 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
348 	const struct gpio_xlnx_axi_config *config = dev->config;
349 
350 	if (config->irq_config_func != NULL) {
351 		/* Note: This is only called for the first channel, even if the second is enabled.
352 		 * Need to perform the setup for both channels.
353 		 * Disable all interrupts.
354 		 */
355 		sys_write32(0x0, config->base + IPIER_OFFSET);
356 
357 		/* Clear all pending interrupts */
358 		sys_write32(sys_read32(config->base + IPISR_OFFSET), config->base + IPISR_OFFSET);
359 
360 		/* Enable global interrupts for this gpio device */
361 		sys_write32(GIER_GIE, config->base + GIER_OFFSET);
362 
363 		config->irq_config_func(dev);
364 	}
365 #endif
366 	return 0;
367 }
368 
369 static DEVICE_API(gpio, gpio_xlnx_axi_driver_api) = {
370 	.pin_configure = gpio_xlnx_axi_pin_configure,
371 	.port_get_raw = gpio_xlnx_axi_port_get_raw,
372 	.port_set_masked_raw = gpio_xlnx_axi_port_set_masked_raw,
373 	.port_set_bits_raw = gpio_xlnx_axi_port_set_bits_raw,
374 	.port_clear_bits_raw = gpio_xlnx_axi_port_clear_bits_raw,
375 	.port_toggle_bits = gpio_xlnx_axi_port_toggle_bits,
376 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(interrupts)
377 	.pin_interrupt_configure = gpio_xlnx_axi_pin_interrupt_configure,
378 	.manage_callback = gpio_xlnx_axi_manage_callback,
379 	.get_pending_int = gpio_xlnx_axi_get_pending_int,
380 #endif
381 };
382 
383 #define GPIO_XLNX_AXI_GPIO2_HAS_COMPAT_STATUS_OKAY(n)                                              \
384 	UTIL_AND(DT_NODE_HAS_COMPAT(DT_INST_CHILD(n, gpio2), xlnx_xps_gpio_1_00_a_gpio2),          \
385 		 DT_NODE_HAS_STATUS_OKAY(DT_INST_CHILD(n, gpio2)))
386 
387 #define GPIO_XLNX_AXI_GPIO2_COND_INIT(n)                                                           \
388 	IF_ENABLED(UTIL_AND(DT_INST_PROP_OR(n, xlnx_is_dual, 1),                                   \
389 			    GPIO_XLNX_AXI_GPIO2_HAS_COMPAT_STATUS_OKAY(n)),                        \
390 		   (GPIO_XLNX_AXI_GPIO2_INIT(n)));
391 
392 #define GPIO_XLNX_AXI_GPIO2_INIT(n)                                                                \
393 	static struct gpio_xlnx_axi_data gpio_xlnx_axi_##n##_2_data = {                            \
394 		.dout = DT_INST_PROP_OR(n, xlnx_dout_default_2, 0),                                \
395 		.tri = DT_INST_PROP_OR(n, xlnx_tri_default_2, GENMASK(MAX_GPIOS - 1, 0)),          \
396 	};                                                                                         \
397                                                                                                    \
398 	static const struct gpio_xlnx_axi_config gpio_xlnx_axi_##n##_2_config = {                  \
399 		.common =                                                                          \
400 			{                                                                          \
401 				.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_NGPIOS(                   \
402 					DT_INST_PROP_OR(n, xlnx_gpio2_width, MAX_GPIOS)),          \
403 			},                                                                         \
404 		.base = DT_INST_REG_ADDR(n),                                                       \
405 		.channel = 1,                                                                      \
406 		.all_inputs = DT_INST_PROP_OR(n, xlnx_all_inputs_2, 0),                            \
407 		.all_outputs = DT_INST_PROP_OR(n, xlnx_all_outputs_2, 0),                          \
408 		.interrupts_available = DT_INST_NODE_HAS_PROP(n, interrupts)};                     \
409                                                                                                    \
410 	DEVICE_DT_DEFINE(DT_INST_CHILD(n, gpio2), &gpio_xlnx_axi_init, NULL,                       \
411 			 &gpio_xlnx_axi_##n##_2_data, &gpio_xlnx_axi_##n##_2_config, PRE_KERNEL_1, \
412 			 CONFIG_GPIO_INIT_PRIORITY, &gpio_xlnx_axi_driver_api);
413 
414 #define GPIO_XLNX_AXI_INIT(n)                                                                      \
415 	IF_ENABLED(DT_INST_NODE_HAS_PROP(n, interrupts),                                           \
416 		   (static void gpio_xlnx_axi_##n##_irq_config(const struct device *dev);))        \
417                                                                                                    \
418 	GPIO_XLNX_AXI_GPIO2_COND_INIT(n);                                                          \
419                                                                                                    \
420 	static struct gpio_xlnx_axi_data gpio_xlnx_axi_##n##_data = {                              \
421 		.dout = DT_INST_PROP_OR(n, xlnx_dout_default, 0),                                  \
422 		.tri = DT_INST_PROP_OR(n, xlnx_tri_default, GENMASK(MAX_GPIOS - 1, 0)),            \
423 		IF_ENABLED(UTIL_AND(UTIL_AND(DT_INST_NODE_HAS_PROP(n, interrupts),                 \
424 					     DT_INST_PROP_OR(n, xlnx_is_dual, 1)),                 \
425 				    GPIO_XLNX_AXI_GPIO2_HAS_COMPAT_STATUS_OKAY(n)),                \
426 			   (.other_channel_device = DEVICE_DT_GET(DT_INST_CHILD(n, gpio2))))};     \
427                                                                                                    \
428 	static const struct gpio_xlnx_axi_config gpio_xlnx_axi_##n##_config = {                    \
429 		.common =                                                                          \
430 			{                                                                          \
431 				.port_pin_mask = GPIO_PORT_PIN_MASK_FROM_NGPIOS(                   \
432 					DT_INST_PROP_OR(n, xlnx_gpio_width, MAX_GPIOS)),           \
433 			},                                                                         \
434 		.base = DT_INST_REG_ADDR(n),                                                       \
435 		.channel = 0,                                                                      \
436 		.all_inputs = DT_INST_PROP_OR(n, xlnx_all_inputs, 0),                              \
437 		.all_outputs = DT_INST_PROP_OR(n, xlnx_all_outputs, 0),                            \
438 		.interrupts_available = DT_INST_NODE_HAS_PROP(n, interrupts),                      \
439 		IF_ENABLED(DT_INST_NODE_HAS_PROP(n, interrupts),                                   \
440 			   (.irq_config_func = gpio_xlnx_axi_##n##_irq_config))};                  \
441                                                                                                    \
442 	IF_ENABLED(DT_INST_NODE_HAS_PROP(n, interrupts),                                           \
443 		   (static void gpio_xlnx_axi_##n##_irq_config(const struct device *dev)           \
444 		    {                                                                              \
445 			   ARG_UNUSED(dev);                                                        \
446                                                                                                    \
447 			   IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority),                  \
448 				       gpio_xlnx_axi_isr, DEVICE_DT_INST_GET(n), 0);               \
449                                                                                                    \
450 			   irq_enable(DT_INST_IRQN(n));                                            \
451 		   }))                                                                             \
452                                                                                                    \
453 	DEVICE_DT_INST_DEFINE(n, gpio_xlnx_axi_init, NULL, &gpio_xlnx_axi_##n##_data,              \
454 			      &gpio_xlnx_axi_##n##_config, PRE_KERNEL_1,                           \
455 			      CONFIG_GPIO_INIT_PRIORITY, &gpio_xlnx_axi_driver_api);
456 
457 DT_INST_FOREACH_STATUS_OKAY(GPIO_XLNX_AXI_INIT)
458