1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Amstrad E3 FIQ handling
4  *
5  *  Copyright (C) 2009 Janusz Krzysztofik
6  *  Copyright (c) 2006 Matt Callow
7  *  Copyright (c) 2004 Amstrad Plc
8  *  Copyright (C) 2001 RidgeRun, Inc.
9  *
10  * Parts of this code are taken from linux/arch/arm/mach-omap/irq.c
11  * in the MontaVista 2.4 kernel (and the Amstrad changes therein)
12  */
13 #include <linux/gpio/consumer.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/module.h>
18 #include <linux/io.h>
19 #include <linux/platform_data/ams-delta-fiq.h>
20 #include <linux/platform_device.h>
21 
22 #include <asm/fiq.h>
23 
24 #include "ams-delta-fiq.h"
25 #include "board-ams-delta.h"
26 
27 static struct fiq_handler fh = {
28 	.name	= "ams-delta-fiq"
29 };
30 
31 /*
32  * This buffer is shared between FIQ and IRQ contexts.
33  * The FIQ and IRQ isrs can both read and write it.
34  * It is structured as a header section several 32bit slots,
35  * followed by the circular buffer where the FIQ isr stores
36  * keystrokes received from the qwerty keyboard.  See
37  * <linux/platform_data/ams-delta-fiq.h> for details of offsets.
38  */
39 static unsigned int fiq_buffer[1024];
40 
41 static struct irq_chip *irq_chip;
42 static struct irq_data *irq_data[16];
43 static unsigned int irq_counter[16];
44 
45 static const char *pin_name[16] __initconst = {
46 	[AMS_DELTA_GPIO_PIN_KEYBRD_DATA]	= "keybrd_data",
47 	[AMS_DELTA_GPIO_PIN_KEYBRD_CLK]		= "keybrd_clk",
48 };
49 
50 static irqreturn_t deferred_fiq(int irq, void *dev_id)
51 {
52 	struct irq_data *d;
53 	int gpio, irq_num, fiq_count;
54 
55 	/*
56 	 * For each handled GPIO interrupt, keep calling its interrupt handler
57 	 * until the IRQ counter catches the FIQ incremented interrupt counter.
58 	 */
59 	for (gpio = AMS_DELTA_GPIO_PIN_KEYBRD_CLK;
60 			gpio <= AMS_DELTA_GPIO_PIN_HOOK_SWITCH; gpio++) {
61 		d = irq_data[gpio];
62 		irq_num = d->irq;
63 		fiq_count = fiq_buffer[FIQ_CNT_INT_00 + gpio];
64 
65 		if (irq_counter[gpio] < fiq_count &&
66 				gpio != AMS_DELTA_GPIO_PIN_KEYBRD_CLK) {
67 			/*
68 			 * handle_simple_irq() that OMAP GPIO edge
69 			 * interrupts default to since commit 80ac93c27441
70 			 * requires interrupt already acked and unmasked.
71 			 */
72 			if (irq_chip->irq_ack)
73 				irq_chip->irq_ack(d);
74 			if (irq_chip->irq_unmask)
75 				irq_chip->irq_unmask(d);
76 		}
77 		for (; irq_counter[gpio] < fiq_count; irq_counter[gpio]++)
78 			generic_handle_irq(irq_num);
79 	}
80 	return IRQ_HANDLED;
81 }
82 
83 void __init ams_delta_init_fiq(struct gpio_chip *chip,
84 			       struct platform_device *serio)
85 {
86 	struct gpio_desc *gpiod, *data = NULL, *clk = NULL;
87 	void *fiqhandler_start;
88 	unsigned int fiqhandler_length;
89 	struct pt_regs FIQ_regs;
90 	unsigned long val, offset;
91 	int i, retval;
92 
93 	/* Store irq_chip location for IRQ handler use */
94 	irq_chip = chip->irq.chip;
95 	if (!irq_chip) {
96 		pr_err("%s: GPIO chip %s is missing IRQ function\n", __func__,
97 		       chip->label);
98 		return;
99 	}
100 
101 	for (i = 0; i < ARRAY_SIZE(irq_data); i++) {
102 		gpiod = gpiochip_request_own_desc(chip, i, pin_name[i], 0);
103 		if (IS_ERR(gpiod)) {
104 			pr_err("%s: failed to get GPIO pin %d (%ld)\n",
105 			       __func__, i, PTR_ERR(gpiod));
106 			return;
107 		}
108 		/* Store irq_data location for IRQ handler use */
109 		irq_data[i] = irq_get_irq_data(gpiod_to_irq(gpiod));
110 
111 		/*
112 		 * FIQ handler takes full control over serio data and clk GPIO
113 		 * pins.  Initiaize them and keep requested so nobody can
114 		 * interfere.  Fail if any of those two couldn't be requested.
115 		 */
116 		switch (i) {
117 		case AMS_DELTA_GPIO_PIN_KEYBRD_DATA:
118 			data = gpiod;
119 			gpiod_direction_input(data);
120 			break;
121 		case AMS_DELTA_GPIO_PIN_KEYBRD_CLK:
122 			clk = gpiod;
123 			gpiod_direction_input(clk);
124 			break;
125 		default:
126 			gpiochip_free_own_desc(gpiod);
127 			break;
128 		}
129 	}
130 	if (!data || !clk)
131 		goto out_gpio;
132 
133 	fiqhandler_start = &qwerty_fiqin_start;
134 	fiqhandler_length = &qwerty_fiqin_end - &qwerty_fiqin_start;
135 	pr_info("Installing fiq handler from %p, length 0x%x\n",
136 			fiqhandler_start, fiqhandler_length);
137 
138 	retval = claim_fiq(&fh);
139 	if (retval) {
140 		pr_err("ams_delta_init_fiq(): couldn't claim FIQ, ret=%d\n",
141 				retval);
142 		goto out_gpio;
143 	}
144 
145 	retval = request_irq(INT_DEFERRED_FIQ, deferred_fiq,
146 			IRQ_TYPE_EDGE_RISING, "deferred_fiq", NULL);
147 	if (retval < 0) {
148 		pr_err("Failed to get deferred_fiq IRQ, ret=%d\n", retval);
149 		release_fiq(&fh);
150 		goto out_gpio;
151 	}
152 	/*
153 	 * Since no set_type() method is provided by OMAP irq chip,
154 	 * switch to edge triggered interrupt type manually.
155 	 */
156 	offset = IRQ_ILR0_REG_OFFSET +
157 			((INT_DEFERRED_FIQ - NR_IRQS_LEGACY) & 0x1f) * 0x4;
158 	val = omap_readl(DEFERRED_FIQ_IH_BASE + offset) & ~(1 << 1);
159 	omap_writel(val, DEFERRED_FIQ_IH_BASE + offset);
160 
161 	set_fiq_handler(fiqhandler_start, fiqhandler_length);
162 
163 	/*
164 	 * Initialise the buffer which is shared
165 	 * between FIQ mode and IRQ mode
166 	 */
167 	fiq_buffer[FIQ_GPIO_INT_MASK]	= 0;
168 	fiq_buffer[FIQ_MASK]		= 0;
169 	fiq_buffer[FIQ_STATE]		= 0;
170 	fiq_buffer[FIQ_KEY]		= 0;
171 	fiq_buffer[FIQ_KEYS_CNT]	= 0;
172 	fiq_buffer[FIQ_KEYS_HICNT]	= 0;
173 	fiq_buffer[FIQ_TAIL_OFFSET]	= 0;
174 	fiq_buffer[FIQ_HEAD_OFFSET]	= 0;
175 	fiq_buffer[FIQ_BUF_LEN]		= 256;
176 	fiq_buffer[FIQ_MISSED_KEYS]	= 0;
177 	fiq_buffer[FIQ_BUFFER_START]	=
178 			(unsigned int) &fiq_buffer[FIQ_CIRC_BUFF];
179 
180 	for (i = FIQ_CNT_INT_00; i <= FIQ_CNT_INT_15; i++)
181 		fiq_buffer[i] = 0;
182 
183 	/*
184 	 * FIQ mode r9 always points to the fiq_buffer, because the FIQ isr
185 	 * will run in an unpredictable context. The fiq_buffer is the FIQ isr's
186 	 * only means of communication with the IRQ level and other kernel
187 	 * context code.
188 	 */
189 	FIQ_regs.ARM_r9 = (unsigned int)fiq_buffer;
190 	set_fiq_regs(&FIQ_regs);
191 
192 	pr_info("request_fiq(): fiq_buffer = %p\n", fiq_buffer);
193 
194 	/*
195 	 * Redirect GPIO interrupts to FIQ
196 	 */
197 	offset = IRQ_ILR0_REG_OFFSET + (INT_GPIO_BANK1 - NR_IRQS_LEGACY) * 0x4;
198 	val = omap_readl(OMAP_IH1_BASE + offset) | 1;
199 	omap_writel(val, OMAP_IH1_BASE + offset);
200 
201 	/* Initialize serio device IRQ resource and platform_data */
202 	serio->resource[0].start = gpiod_to_irq(clk);
203 	serio->resource[0].end = serio->resource[0].start;
204 	serio->dev.platform_data = fiq_buffer;
205 
206 	/*
207 	 * Since FIQ handler performs handling of GPIO registers for
208 	 * "keybrd_clk" IRQ pin, ams_delta_serio driver used to set
209 	 * handle_simple_irq() as active IRQ handler for that pin to avoid
210 	 * bad interaction with gpio-omap driver.  This is no longer needed
211 	 * as handle_simple_irq() is now the default handler for OMAP GPIO
212 	 * edge interrupts.
213 	 * This comment replaces the obsolete code which has been removed
214 	 * from the ams_delta_serio driver and stands here only as a reminder
215 	 * of that dependency on gpio-omap driver behavior.
216 	 */
217 
218 	return;
219 
220 out_gpio:
221 	if (data)
222 		gpiochip_free_own_desc(data);
223 	if (clk)
224 		gpiochip_free_own_desc(clk);
225 }
226