111f9562aSJanusz Krzysztofik /* 211f9562aSJanusz Krzysztofik * Amstrad E3 FIQ handling 311f9562aSJanusz Krzysztofik * 411f9562aSJanusz Krzysztofik * Copyright (C) 2009 Janusz Krzysztofik 511f9562aSJanusz Krzysztofik * Copyright (c) 2006 Matt Callow 611f9562aSJanusz Krzysztofik * Copyright (c) 2004 Amstrad Plc 711f9562aSJanusz Krzysztofik * Copyright (C) 2001 RidgeRun, Inc. 811f9562aSJanusz Krzysztofik * 911f9562aSJanusz Krzysztofik * Parts of this code are taken from linux/arch/arm/mach-omap/irq.c 1011f9562aSJanusz Krzysztofik * in the MontaVista 2.4 kernel (and the Amstrad changes therein) 1111f9562aSJanusz Krzysztofik * 1211f9562aSJanusz Krzysztofik * This program is free software; you can redistribute it and/or modify it 1311f9562aSJanusz Krzysztofik * under the terms of the GNU General Public License version 2 as published by 1411f9562aSJanusz Krzysztofik * the Free Software Foundation. 1511f9562aSJanusz Krzysztofik */ 1611f9562aSJanusz Krzysztofik #include <linux/gpio.h> 1711f9562aSJanusz Krzysztofik #include <linux/interrupt.h> 1811f9562aSJanusz Krzysztofik #include <linux/irq.h> 1911f9562aSJanusz Krzysztofik #include <linux/module.h> 2011f9562aSJanusz Krzysztofik #include <linux/io.h> 2111f9562aSJanusz Krzysztofik 2211f9562aSJanusz Krzysztofik #include <plat/board-ams-delta.h> 2311f9562aSJanusz Krzysztofik 2411f9562aSJanusz Krzysztofik #include <asm/fiq.h> 252e3ee9f4STony Lindgren 2611f9562aSJanusz Krzysztofik #include <mach/ams-delta-fiq.h> 2711f9562aSJanusz Krzysztofik 2811f9562aSJanusz Krzysztofik static struct fiq_handler fh = { 2911f9562aSJanusz Krzysztofik .name = "ams-delta-fiq" 3011f9562aSJanusz Krzysztofik }; 3111f9562aSJanusz Krzysztofik 3211f9562aSJanusz Krzysztofik /* 3311f9562aSJanusz Krzysztofik * This buffer is shared between FIQ and IRQ contexts. 3411f9562aSJanusz Krzysztofik * The FIQ and IRQ isrs can both read and write it. 3511f9562aSJanusz Krzysztofik * It is structured as a header section several 32bit slots, 3611f9562aSJanusz Krzysztofik * followed by the circular buffer where the FIQ isr stores 3711f9562aSJanusz Krzysztofik * keystrokes received from the qwerty keyboard. 3811f9562aSJanusz Krzysztofik * See ams-delta-fiq.h for details of offsets. 3911f9562aSJanusz Krzysztofik */ 4011f9562aSJanusz Krzysztofik unsigned int fiq_buffer[1024]; 4111f9562aSJanusz Krzysztofik EXPORT_SYMBOL(fiq_buffer); 4211f9562aSJanusz Krzysztofik 4311f9562aSJanusz Krzysztofik static unsigned int irq_counter[16]; 4411f9562aSJanusz Krzysztofik 4511f9562aSJanusz Krzysztofik static irqreturn_t deferred_fiq(int irq, void *dev_id) 4611f9562aSJanusz Krzysztofik { 4711f9562aSJanusz Krzysztofik struct irq_desc *irq_desc; 4811f9562aSJanusz Krzysztofik struct irq_chip *irq_chip = NULL; 4911f9562aSJanusz Krzysztofik int gpio, irq_num, fiq_count; 5011f9562aSJanusz Krzysztofik 5111f9562aSJanusz Krzysztofik irq_desc = irq_to_desc(IH_GPIO_BASE); 5211f9562aSJanusz Krzysztofik if (irq_desc) 53a51eef7eSLennert Buytenhek irq_chip = irq_desc->irq_data.chip; 5411f9562aSJanusz Krzysztofik 5511f9562aSJanusz Krzysztofik /* 5611f9562aSJanusz Krzysztofik * For each handled GPIO interrupt, keep calling its interrupt handler 5711f9562aSJanusz Krzysztofik * until the IRQ counter catches the FIQ incremented interrupt counter. 5811f9562aSJanusz Krzysztofik */ 5911f9562aSJanusz Krzysztofik for (gpio = AMS_DELTA_GPIO_PIN_KEYBRD_CLK; 6011f9562aSJanusz Krzysztofik gpio <= AMS_DELTA_GPIO_PIN_HOOK_SWITCH; gpio++) { 6111f9562aSJanusz Krzysztofik irq_num = gpio_to_irq(gpio); 6211f9562aSJanusz Krzysztofik fiq_count = fiq_buffer[FIQ_CNT_INT_00 + gpio]; 6311f9562aSJanusz Krzysztofik 6411f9562aSJanusz Krzysztofik while (irq_counter[gpio] < fiq_count) { 6511f9562aSJanusz Krzysztofik if (gpio != AMS_DELTA_GPIO_PIN_KEYBRD_CLK) { 66a51eef7eSLennert Buytenhek struct irq_data *d = irq_get_irq_data(irq_num); 67a51eef7eSLennert Buytenhek 6811f9562aSJanusz Krzysztofik /* 6911f9562aSJanusz Krzysztofik * It looks like handle_edge_irq() that 7011f9562aSJanusz Krzysztofik * OMAP GPIO edge interrupts default to, 7111f9562aSJanusz Krzysztofik * expects interrupt already unmasked. 7211f9562aSJanusz Krzysztofik */ 73a51eef7eSLennert Buytenhek if (irq_chip && irq_chip->irq_unmask) 74a51eef7eSLennert Buytenhek irq_chip->irq_unmask(d); 7511f9562aSJanusz Krzysztofik } 7611f9562aSJanusz Krzysztofik generic_handle_irq(irq_num); 7711f9562aSJanusz Krzysztofik 7811f9562aSJanusz Krzysztofik irq_counter[gpio]++; 7911f9562aSJanusz Krzysztofik } 8011f9562aSJanusz Krzysztofik } 8111f9562aSJanusz Krzysztofik return IRQ_HANDLED; 8211f9562aSJanusz Krzysztofik } 8311f9562aSJanusz Krzysztofik 8411f9562aSJanusz Krzysztofik void __init ams_delta_init_fiq(void) 8511f9562aSJanusz Krzysztofik { 8611f9562aSJanusz Krzysztofik void *fiqhandler_start; 8711f9562aSJanusz Krzysztofik unsigned int fiqhandler_length; 8811f9562aSJanusz Krzysztofik struct pt_regs FIQ_regs; 8911f9562aSJanusz Krzysztofik unsigned long val, offset; 9011f9562aSJanusz Krzysztofik int i, retval; 9111f9562aSJanusz Krzysztofik 9211f9562aSJanusz Krzysztofik fiqhandler_start = &qwerty_fiqin_start; 9311f9562aSJanusz Krzysztofik fiqhandler_length = &qwerty_fiqin_end - &qwerty_fiqin_start; 9411f9562aSJanusz Krzysztofik pr_info("Installing fiq handler from %p, length 0x%x\n", 9511f9562aSJanusz Krzysztofik fiqhandler_start, fiqhandler_length); 9611f9562aSJanusz Krzysztofik 9711f9562aSJanusz Krzysztofik retval = claim_fiq(&fh); 9811f9562aSJanusz Krzysztofik if (retval) { 9911f9562aSJanusz Krzysztofik pr_err("ams_delta_init_fiq(): couldn't claim FIQ, ret=%d\n", 10011f9562aSJanusz Krzysztofik retval); 10111f9562aSJanusz Krzysztofik return; 10211f9562aSJanusz Krzysztofik } 10311f9562aSJanusz Krzysztofik 10411f9562aSJanusz Krzysztofik retval = request_irq(INT_DEFERRED_FIQ, deferred_fiq, 105*a7022d60SPaul Walmsley IRQ_TYPE_EDGE_RISING, "deferred_fiq", NULL); 10611f9562aSJanusz Krzysztofik if (retval < 0) { 10711f9562aSJanusz Krzysztofik pr_err("Failed to get deferred_fiq IRQ, ret=%d\n", retval); 10811f9562aSJanusz Krzysztofik release_fiq(&fh); 10911f9562aSJanusz Krzysztofik return; 11011f9562aSJanusz Krzysztofik } 11111f9562aSJanusz Krzysztofik /* 11211f9562aSJanusz Krzysztofik * Since no set_type() method is provided by OMAP irq chip, 11311f9562aSJanusz Krzysztofik * switch to edge triggered interrupt type manually. 11411f9562aSJanusz Krzysztofik */ 11511f9562aSJanusz Krzysztofik offset = IRQ_ILR0_REG_OFFSET + INT_DEFERRED_FIQ * 0x4; 11611f9562aSJanusz Krzysztofik val = omap_readl(DEFERRED_FIQ_IH_BASE + offset) & ~(1 << 1); 11711f9562aSJanusz Krzysztofik omap_writel(val, DEFERRED_FIQ_IH_BASE + offset); 11811f9562aSJanusz Krzysztofik 11911f9562aSJanusz Krzysztofik set_fiq_handler(fiqhandler_start, fiqhandler_length); 12011f9562aSJanusz Krzysztofik 12111f9562aSJanusz Krzysztofik /* 12211f9562aSJanusz Krzysztofik * Initialise the buffer which is shared 12311f9562aSJanusz Krzysztofik * between FIQ mode and IRQ mode 12411f9562aSJanusz Krzysztofik */ 12511f9562aSJanusz Krzysztofik fiq_buffer[FIQ_GPIO_INT_MASK] = 0; 12611f9562aSJanusz Krzysztofik fiq_buffer[FIQ_MASK] = 0; 12711f9562aSJanusz Krzysztofik fiq_buffer[FIQ_STATE] = 0; 12811f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEY] = 0; 12911f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEYS_CNT] = 0; 13011f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEYS_HICNT] = 0; 13111f9562aSJanusz Krzysztofik fiq_buffer[FIQ_TAIL_OFFSET] = 0; 13211f9562aSJanusz Krzysztofik fiq_buffer[FIQ_HEAD_OFFSET] = 0; 13311f9562aSJanusz Krzysztofik fiq_buffer[FIQ_BUF_LEN] = 256; 13411f9562aSJanusz Krzysztofik fiq_buffer[FIQ_MISSED_KEYS] = 0; 13511f9562aSJanusz Krzysztofik fiq_buffer[FIQ_BUFFER_START] = 13611f9562aSJanusz Krzysztofik (unsigned int) &fiq_buffer[FIQ_CIRC_BUFF]; 13711f9562aSJanusz Krzysztofik 13811f9562aSJanusz Krzysztofik for (i = FIQ_CNT_INT_00; i <= FIQ_CNT_INT_15; i++) 13911f9562aSJanusz Krzysztofik fiq_buffer[i] = 0; 14011f9562aSJanusz Krzysztofik 14111f9562aSJanusz Krzysztofik /* 14211f9562aSJanusz Krzysztofik * FIQ mode r9 always points to the fiq_buffer, becauses the FIQ isr 14311f9562aSJanusz Krzysztofik * will run in an unpredictable context. The fiq_buffer is the FIQ isr's 14411f9562aSJanusz Krzysztofik * only means of communication with the IRQ level and other kernel 14511f9562aSJanusz Krzysztofik * context code. 14611f9562aSJanusz Krzysztofik */ 14711f9562aSJanusz Krzysztofik FIQ_regs.ARM_r9 = (unsigned int)fiq_buffer; 14811f9562aSJanusz Krzysztofik set_fiq_regs(&FIQ_regs); 14911f9562aSJanusz Krzysztofik 15011f9562aSJanusz Krzysztofik pr_info("request_fiq(): fiq_buffer = %p\n", fiq_buffer); 15111f9562aSJanusz Krzysztofik 15211f9562aSJanusz Krzysztofik /* 15311f9562aSJanusz Krzysztofik * Redirect GPIO interrupts to FIQ 15411f9562aSJanusz Krzysztofik */ 15511f9562aSJanusz Krzysztofik offset = IRQ_ILR0_REG_OFFSET + INT_GPIO_BANK1 * 0x4; 15611f9562aSJanusz Krzysztofik val = omap_readl(OMAP_IH1_BASE + offset) | 1; 15711f9562aSJanusz Krzysztofik omap_writel(val, OMAP_IH1_BASE + offset); 15811f9562aSJanusz Krzysztofik } 159