1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
211f9562aSJanusz Krzysztofik /*
311f9562aSJanusz Krzysztofik * Amstrad E3 FIQ handling
411f9562aSJanusz Krzysztofik *
511f9562aSJanusz Krzysztofik * Copyright (C) 2009 Janusz Krzysztofik
611f9562aSJanusz Krzysztofik * Copyright (c) 2006 Matt Callow
711f9562aSJanusz Krzysztofik * Copyright (c) 2004 Amstrad Plc
811f9562aSJanusz Krzysztofik * Copyright (C) 2001 RidgeRun, Inc.
911f9562aSJanusz Krzysztofik *
1011f9562aSJanusz Krzysztofik * Parts of this code are taken from linux/arch/arm/mach-omap/irq.c
1111f9562aSJanusz Krzysztofik * in the MontaVista 2.4 kernel (and the Amstrad changes therein)
1211f9562aSJanusz Krzysztofik */
1397abda99SJanusz Krzysztofik #include <linux/gpio/consumer.h>
145923ea6cSLinus Walleij #include <linux/gpio/machine.h>
1597abda99SJanusz Krzysztofik #include <linux/gpio/driver.h>
1611f9562aSJanusz Krzysztofik #include <linux/interrupt.h>
1711f9562aSJanusz Krzysztofik #include <linux/irq.h>
1811f9562aSJanusz Krzysztofik #include <linux/module.h>
1911f9562aSJanusz Krzysztofik #include <linux/io.h>
20dc8fbeb0SJanusz Krzysztofik #include <linux/platform_data/ams-delta-fiq.h>
21a617b36bSJanusz Krzysztofik #include <linux/platform_device.h>
2211f9562aSJanusz Krzysztofik
2311f9562aSJanusz Krzysztofik #include <asm/fiq.h>
24*7e0a9e62SArnd Bergmann #include <linux/soc/ti/omap1-io.h>
252e3ee9f4STony Lindgren
26*7e0a9e62SArnd Bergmann #include "hardware.h"
27dc8fbeb0SJanusz Krzysztofik #include "ams-delta-fiq.h"
280a48a413SJanusz Krzysztofik #include "board-ams-delta.h"
2911f9562aSJanusz Krzysztofik
3011f9562aSJanusz Krzysztofik static struct fiq_handler fh = {
3111f9562aSJanusz Krzysztofik .name = "ams-delta-fiq"
3211f9562aSJanusz Krzysztofik };
3311f9562aSJanusz Krzysztofik
3411f9562aSJanusz Krzysztofik /*
3511f9562aSJanusz Krzysztofik * This buffer is shared between FIQ and IRQ contexts.
3611f9562aSJanusz Krzysztofik * The FIQ and IRQ isrs can both read and write it.
3711f9562aSJanusz Krzysztofik * It is structured as a header section several 32bit slots,
3811f9562aSJanusz Krzysztofik * followed by the circular buffer where the FIQ isr stores
39dc8fbeb0SJanusz Krzysztofik * keystrokes received from the qwerty keyboard. See
40dc8fbeb0SJanusz Krzysztofik * <linux/platform_data/ams-delta-fiq.h> for details of offsets.
4111f9562aSJanusz Krzysztofik */
425f73861fSJanusz Krzysztofik static unsigned int fiq_buffer[1024];
4311f9562aSJanusz Krzysztofik
4497abda99SJanusz Krzysztofik static struct irq_chip *irq_chip;
4597abda99SJanusz Krzysztofik static struct irq_data *irq_data[16];
4611f9562aSJanusz Krzysztofik static unsigned int irq_counter[16];
4711f9562aSJanusz Krzysztofik
48a32d5ce1SJanusz Krzysztofik static const char *pin_name[16] __initconst = {
49a32d5ce1SJanusz Krzysztofik [AMS_DELTA_GPIO_PIN_KEYBRD_DATA] = "keybrd_data",
50a32d5ce1SJanusz Krzysztofik [AMS_DELTA_GPIO_PIN_KEYBRD_CLK] = "keybrd_clk",
51a32d5ce1SJanusz Krzysztofik };
52a32d5ce1SJanusz Krzysztofik
deferred_fiq(int irq,void * dev_id)5311f9562aSJanusz Krzysztofik static irqreturn_t deferred_fiq(int irq, void *dev_id)
5411f9562aSJanusz Krzysztofik {
5597abda99SJanusz Krzysztofik struct irq_data *d;
5611f9562aSJanusz Krzysztofik int gpio, irq_num, fiq_count;
5711f9562aSJanusz Krzysztofik
5811f9562aSJanusz Krzysztofik /*
5911f9562aSJanusz Krzysztofik * For each handled GPIO interrupt, keep calling its interrupt handler
6011f9562aSJanusz Krzysztofik * until the IRQ counter catches the FIQ incremented interrupt counter.
6111f9562aSJanusz Krzysztofik */
6211f9562aSJanusz Krzysztofik for (gpio = AMS_DELTA_GPIO_PIN_KEYBRD_CLK;
6311f9562aSJanusz Krzysztofik gpio <= AMS_DELTA_GPIO_PIN_HOOK_SWITCH; gpio++) {
6497abda99SJanusz Krzysztofik d = irq_data[gpio];
6597abda99SJanusz Krzysztofik irq_num = d->irq;
6611f9562aSJanusz Krzysztofik fiq_count = fiq_buffer[FIQ_CNT_INT_00 + gpio];
6711f9562aSJanusz Krzysztofik
68baf64250SJanusz Krzysztofik if (irq_counter[gpio] < fiq_count &&
69baf64250SJanusz Krzysztofik gpio != AMS_DELTA_GPIO_PIN_KEYBRD_CLK) {
7011f9562aSJanusz Krzysztofik /*
71baf64250SJanusz Krzysztofik * handle_simple_irq() that OMAP GPIO edge
72baf64250SJanusz Krzysztofik * interrupts default to since commit 80ac93c27441
73baf64250SJanusz Krzysztofik * requires interrupt already acked and unmasked.
7411f9562aSJanusz Krzysztofik */
75fa8397e4SJanusz Krzysztofik if (!WARN_ON_ONCE(!irq_chip->irq_unmask))
76a51eef7eSLennert Buytenhek irq_chip->irq_unmask(d);
7711f9562aSJanusz Krzysztofik }
78baf64250SJanusz Krzysztofik for (; irq_counter[gpio] < fiq_count; irq_counter[gpio]++)
79baf64250SJanusz Krzysztofik generic_handle_irq(irq_num);
8011f9562aSJanusz Krzysztofik }
8111f9562aSJanusz Krzysztofik return IRQ_HANDLED;
8211f9562aSJanusz Krzysztofik }
8311f9562aSJanusz Krzysztofik
ams_delta_init_fiq(struct gpio_chip * chip,struct platform_device * serio)84a617b36bSJanusz Krzysztofik void __init ams_delta_init_fiq(struct gpio_chip *chip,
85a617b36bSJanusz Krzysztofik struct platform_device *serio)
8611f9562aSJanusz Krzysztofik {
87a32d5ce1SJanusz Krzysztofik struct gpio_desc *gpiod, *data = NULL, *clk = NULL;
8811f9562aSJanusz Krzysztofik void *fiqhandler_start;
8911f9562aSJanusz Krzysztofik unsigned int fiqhandler_length;
9011f9562aSJanusz Krzysztofik struct pt_regs FIQ_regs;
9111f9562aSJanusz Krzysztofik unsigned long val, offset;
9211f9562aSJanusz Krzysztofik int i, retval;
9311f9562aSJanusz Krzysztofik
9497abda99SJanusz Krzysztofik /* Store irq_chip location for IRQ handler use */
9597abda99SJanusz Krzysztofik irq_chip = chip->irq.chip;
9697abda99SJanusz Krzysztofik if (!irq_chip) {
9797abda99SJanusz Krzysztofik pr_err("%s: GPIO chip %s is missing IRQ function\n", __func__,
9897abda99SJanusz Krzysztofik chip->label);
9997abda99SJanusz Krzysztofik return;
10097abda99SJanusz Krzysztofik }
10197abda99SJanusz Krzysztofik
10297abda99SJanusz Krzysztofik for (i = 0; i < ARRAY_SIZE(irq_data); i++) {
1035923ea6cSLinus Walleij gpiod = gpiochip_request_own_desc(chip, i, pin_name[i],
1045923ea6cSLinus Walleij GPIO_ACTIVE_HIGH, GPIOD_IN);
10597abda99SJanusz Krzysztofik if (IS_ERR(gpiod)) {
10697abda99SJanusz Krzysztofik pr_err("%s: failed to get GPIO pin %d (%ld)\n",
10797abda99SJanusz Krzysztofik __func__, i, PTR_ERR(gpiod));
10897abda99SJanusz Krzysztofik return;
10997abda99SJanusz Krzysztofik }
11097abda99SJanusz Krzysztofik /* Store irq_data location for IRQ handler use */
11197abda99SJanusz Krzysztofik irq_data[i] = irq_get_irq_data(gpiod_to_irq(gpiod));
11297abda99SJanusz Krzysztofik
113a32d5ce1SJanusz Krzysztofik /*
114a32d5ce1SJanusz Krzysztofik * FIQ handler takes full control over serio data and clk GPIO
115ed2b6b12SJonathan Neuschäfer * pins. Initialize them and keep requested so nobody can
116a32d5ce1SJanusz Krzysztofik * interfere. Fail if any of those two couldn't be requested.
117a32d5ce1SJanusz Krzysztofik */
118a32d5ce1SJanusz Krzysztofik switch (i) {
119a32d5ce1SJanusz Krzysztofik case AMS_DELTA_GPIO_PIN_KEYBRD_DATA:
120a32d5ce1SJanusz Krzysztofik data = gpiod;
121a32d5ce1SJanusz Krzysztofik gpiod_direction_input(data);
122a32d5ce1SJanusz Krzysztofik break;
123a32d5ce1SJanusz Krzysztofik case AMS_DELTA_GPIO_PIN_KEYBRD_CLK:
124a32d5ce1SJanusz Krzysztofik clk = gpiod;
125a32d5ce1SJanusz Krzysztofik gpiod_direction_input(clk);
126a32d5ce1SJanusz Krzysztofik break;
127a32d5ce1SJanusz Krzysztofik default:
12897abda99SJanusz Krzysztofik gpiochip_free_own_desc(gpiod);
129a32d5ce1SJanusz Krzysztofik break;
13097abda99SJanusz Krzysztofik }
131a32d5ce1SJanusz Krzysztofik }
132a32d5ce1SJanusz Krzysztofik if (!data || !clk)
133a32d5ce1SJanusz Krzysztofik goto out_gpio;
13497abda99SJanusz Krzysztofik
13511f9562aSJanusz Krzysztofik fiqhandler_start = &qwerty_fiqin_start;
13611f9562aSJanusz Krzysztofik fiqhandler_length = &qwerty_fiqin_end - &qwerty_fiqin_start;
13711f9562aSJanusz Krzysztofik pr_info("Installing fiq handler from %p, length 0x%x\n",
13811f9562aSJanusz Krzysztofik fiqhandler_start, fiqhandler_length);
13911f9562aSJanusz Krzysztofik
14011f9562aSJanusz Krzysztofik retval = claim_fiq(&fh);
14111f9562aSJanusz Krzysztofik if (retval) {
14211f9562aSJanusz Krzysztofik pr_err("ams_delta_init_fiq(): couldn't claim FIQ, ret=%d\n",
14311f9562aSJanusz Krzysztofik retval);
144a32d5ce1SJanusz Krzysztofik goto out_gpio;
14511f9562aSJanusz Krzysztofik }
14611f9562aSJanusz Krzysztofik
14711f9562aSJanusz Krzysztofik retval = request_irq(INT_DEFERRED_FIQ, deferred_fiq,
148a7022d60SPaul Walmsley IRQ_TYPE_EDGE_RISING, "deferred_fiq", NULL);
14911f9562aSJanusz Krzysztofik if (retval < 0) {
15011f9562aSJanusz Krzysztofik pr_err("Failed to get deferred_fiq IRQ, ret=%d\n", retval);
15111f9562aSJanusz Krzysztofik release_fiq(&fh);
152a32d5ce1SJanusz Krzysztofik goto out_gpio;
15311f9562aSJanusz Krzysztofik }
15411f9562aSJanusz Krzysztofik /*
15511f9562aSJanusz Krzysztofik * Since no set_type() method is provided by OMAP irq chip,
15611f9562aSJanusz Krzysztofik * switch to edge triggered interrupt type manually.
15711f9562aSJanusz Krzysztofik */
158ef5bdccfSJanusz Krzysztofik offset = IRQ_ILR0_REG_OFFSET +
159ef5bdccfSJanusz Krzysztofik ((INT_DEFERRED_FIQ - NR_IRQS_LEGACY) & 0x1f) * 0x4;
16011f9562aSJanusz Krzysztofik val = omap_readl(DEFERRED_FIQ_IH_BASE + offset) & ~(1 << 1);
16111f9562aSJanusz Krzysztofik omap_writel(val, DEFERRED_FIQ_IH_BASE + offset);
16211f9562aSJanusz Krzysztofik
16311f9562aSJanusz Krzysztofik set_fiq_handler(fiqhandler_start, fiqhandler_length);
16411f9562aSJanusz Krzysztofik
16511f9562aSJanusz Krzysztofik /*
16611f9562aSJanusz Krzysztofik * Initialise the buffer which is shared
16711f9562aSJanusz Krzysztofik * between FIQ mode and IRQ mode
16811f9562aSJanusz Krzysztofik */
16911f9562aSJanusz Krzysztofik fiq_buffer[FIQ_GPIO_INT_MASK] = 0;
17011f9562aSJanusz Krzysztofik fiq_buffer[FIQ_MASK] = 0;
17111f9562aSJanusz Krzysztofik fiq_buffer[FIQ_STATE] = 0;
17211f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEY] = 0;
17311f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEYS_CNT] = 0;
17411f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEYS_HICNT] = 0;
17511f9562aSJanusz Krzysztofik fiq_buffer[FIQ_TAIL_OFFSET] = 0;
17611f9562aSJanusz Krzysztofik fiq_buffer[FIQ_HEAD_OFFSET] = 0;
17711f9562aSJanusz Krzysztofik fiq_buffer[FIQ_BUF_LEN] = 256;
17811f9562aSJanusz Krzysztofik fiq_buffer[FIQ_MISSED_KEYS] = 0;
17911f9562aSJanusz Krzysztofik fiq_buffer[FIQ_BUFFER_START] =
18011f9562aSJanusz Krzysztofik (unsigned int) &fiq_buffer[FIQ_CIRC_BUFF];
18111f9562aSJanusz Krzysztofik
18211f9562aSJanusz Krzysztofik for (i = FIQ_CNT_INT_00; i <= FIQ_CNT_INT_15; i++)
18311f9562aSJanusz Krzysztofik fiq_buffer[i] = 0;
18411f9562aSJanusz Krzysztofik
18511f9562aSJanusz Krzysztofik /*
186566ad81fSAndrea Gelmini * FIQ mode r9 always points to the fiq_buffer, because the FIQ isr
18711f9562aSJanusz Krzysztofik * will run in an unpredictable context. The fiq_buffer is the FIQ isr's
18811f9562aSJanusz Krzysztofik * only means of communication with the IRQ level and other kernel
18911f9562aSJanusz Krzysztofik * context code.
19011f9562aSJanusz Krzysztofik */
19111f9562aSJanusz Krzysztofik FIQ_regs.ARM_r9 = (unsigned int)fiq_buffer;
19211f9562aSJanusz Krzysztofik set_fiq_regs(&FIQ_regs);
19311f9562aSJanusz Krzysztofik
19411f9562aSJanusz Krzysztofik pr_info("request_fiq(): fiq_buffer = %p\n", fiq_buffer);
19511f9562aSJanusz Krzysztofik
19611f9562aSJanusz Krzysztofik /*
19711f9562aSJanusz Krzysztofik * Redirect GPIO interrupts to FIQ
19811f9562aSJanusz Krzysztofik */
199ef5bdccfSJanusz Krzysztofik offset = IRQ_ILR0_REG_OFFSET + (INT_GPIO_BANK1 - NR_IRQS_LEGACY) * 0x4;
20011f9562aSJanusz Krzysztofik val = omap_readl(OMAP_IH1_BASE + offset) | 1;
20111f9562aSJanusz Krzysztofik omap_writel(val, OMAP_IH1_BASE + offset);
202a32d5ce1SJanusz Krzysztofik
2035f73861fSJanusz Krzysztofik /* Initialize serio device IRQ resource and platform_data */
204a617b36bSJanusz Krzysztofik serio->resource[0].start = gpiod_to_irq(clk);
205a617b36bSJanusz Krzysztofik serio->resource[0].end = serio->resource[0].start;
2065f73861fSJanusz Krzysztofik serio->dev.platform_data = fiq_buffer;
207a617b36bSJanusz Krzysztofik
208a617b36bSJanusz Krzysztofik /*
209a617b36bSJanusz Krzysztofik * Since FIQ handler performs handling of GPIO registers for
210a617b36bSJanusz Krzysztofik * "keybrd_clk" IRQ pin, ams_delta_serio driver used to set
211a617b36bSJanusz Krzysztofik * handle_simple_irq() as active IRQ handler for that pin to avoid
212a617b36bSJanusz Krzysztofik * bad interaction with gpio-omap driver. This is no longer needed
213a617b36bSJanusz Krzysztofik * as handle_simple_irq() is now the default handler for OMAP GPIO
214a617b36bSJanusz Krzysztofik * edge interrupts.
215a617b36bSJanusz Krzysztofik * This comment replaces the obsolete code which has been removed
216a617b36bSJanusz Krzysztofik * from the ams_delta_serio driver and stands here only as a reminder
217a617b36bSJanusz Krzysztofik * of that dependency on gpio-omap driver behavior.
218a617b36bSJanusz Krzysztofik */
219a617b36bSJanusz Krzysztofik
220a32d5ce1SJanusz Krzysztofik return;
221a32d5ce1SJanusz Krzysztofik
222a32d5ce1SJanusz Krzysztofik out_gpio:
223a32d5ce1SJanusz Krzysztofik if (data)
224a32d5ce1SJanusz Krzysztofik gpiochip_free_own_desc(data);
225a32d5ce1SJanusz Krzysztofik if (clk)
226a32d5ce1SJanusz Krzysztofik gpiochip_free_own_desc(clk);
22711f9562aSJanusz Krzysztofik }
228