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