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 22e27e35ecSTony Lindgren #include <mach/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 int gpio, irq_num, fiq_count; 488435cf75SThomas Gleixner struct irq_chip *irq_chip; 4911f9562aSJanusz Krzysztofik 50b524ca74SThomas Gleixner irq_chip = irq_get_chip(gpio_to_irq(AMS_DELTA_GPIO_PIN_KEYBRD_CLK)); 5111f9562aSJanusz Krzysztofik 5211f9562aSJanusz Krzysztofik /* 5311f9562aSJanusz Krzysztofik * For each handled GPIO interrupt, keep calling its interrupt handler 5411f9562aSJanusz Krzysztofik * until the IRQ counter catches the FIQ incremented interrupt counter. 5511f9562aSJanusz Krzysztofik */ 5611f9562aSJanusz Krzysztofik for (gpio = AMS_DELTA_GPIO_PIN_KEYBRD_CLK; 5711f9562aSJanusz Krzysztofik gpio <= AMS_DELTA_GPIO_PIN_HOOK_SWITCH; gpio++) { 5811f9562aSJanusz Krzysztofik irq_num = gpio_to_irq(gpio); 5911f9562aSJanusz Krzysztofik fiq_count = fiq_buffer[FIQ_CNT_INT_00 + gpio]; 6011f9562aSJanusz Krzysztofik 6111f9562aSJanusz Krzysztofik while (irq_counter[gpio] < fiq_count) { 6211f9562aSJanusz Krzysztofik if (gpio != AMS_DELTA_GPIO_PIN_KEYBRD_CLK) { 63a51eef7eSLennert Buytenhek struct irq_data *d = irq_get_irq_data(irq_num); 64a51eef7eSLennert Buytenhek 6511f9562aSJanusz Krzysztofik /* 6611f9562aSJanusz Krzysztofik * It looks like handle_edge_irq() that 6711f9562aSJanusz Krzysztofik * OMAP GPIO edge interrupts default to, 6811f9562aSJanusz Krzysztofik * expects interrupt already unmasked. 6911f9562aSJanusz Krzysztofik */ 70a51eef7eSLennert Buytenhek if (irq_chip && irq_chip->irq_unmask) 71a51eef7eSLennert Buytenhek irq_chip->irq_unmask(d); 7211f9562aSJanusz Krzysztofik } 7311f9562aSJanusz Krzysztofik generic_handle_irq(irq_num); 7411f9562aSJanusz Krzysztofik 7511f9562aSJanusz Krzysztofik irq_counter[gpio]++; 7611f9562aSJanusz Krzysztofik } 7711f9562aSJanusz Krzysztofik } 7811f9562aSJanusz Krzysztofik return IRQ_HANDLED; 7911f9562aSJanusz Krzysztofik } 8011f9562aSJanusz Krzysztofik 8111f9562aSJanusz Krzysztofik void __init ams_delta_init_fiq(void) 8211f9562aSJanusz Krzysztofik { 8311f9562aSJanusz Krzysztofik void *fiqhandler_start; 8411f9562aSJanusz Krzysztofik unsigned int fiqhandler_length; 8511f9562aSJanusz Krzysztofik struct pt_regs FIQ_regs; 8611f9562aSJanusz Krzysztofik unsigned long val, offset; 8711f9562aSJanusz Krzysztofik int i, retval; 8811f9562aSJanusz Krzysztofik 8911f9562aSJanusz Krzysztofik fiqhandler_start = &qwerty_fiqin_start; 9011f9562aSJanusz Krzysztofik fiqhandler_length = &qwerty_fiqin_end - &qwerty_fiqin_start; 9111f9562aSJanusz Krzysztofik pr_info("Installing fiq handler from %p, length 0x%x\n", 9211f9562aSJanusz Krzysztofik fiqhandler_start, fiqhandler_length); 9311f9562aSJanusz Krzysztofik 9411f9562aSJanusz Krzysztofik retval = claim_fiq(&fh); 9511f9562aSJanusz Krzysztofik if (retval) { 9611f9562aSJanusz Krzysztofik pr_err("ams_delta_init_fiq(): couldn't claim FIQ, ret=%d\n", 9711f9562aSJanusz Krzysztofik retval); 9811f9562aSJanusz Krzysztofik return; 9911f9562aSJanusz Krzysztofik } 10011f9562aSJanusz Krzysztofik 10111f9562aSJanusz Krzysztofik retval = request_irq(INT_DEFERRED_FIQ, deferred_fiq, 102a7022d60SPaul Walmsley IRQ_TYPE_EDGE_RISING, "deferred_fiq", NULL); 10311f9562aSJanusz Krzysztofik if (retval < 0) { 10411f9562aSJanusz Krzysztofik pr_err("Failed to get deferred_fiq IRQ, ret=%d\n", retval); 10511f9562aSJanusz Krzysztofik release_fiq(&fh); 10611f9562aSJanusz Krzysztofik return; 10711f9562aSJanusz Krzysztofik } 10811f9562aSJanusz Krzysztofik /* 10911f9562aSJanusz Krzysztofik * Since no set_type() method is provided by OMAP irq chip, 11011f9562aSJanusz Krzysztofik * switch to edge triggered interrupt type manually. 11111f9562aSJanusz Krzysztofik */ 11211f9562aSJanusz Krzysztofik offset = IRQ_ILR0_REG_OFFSET + INT_DEFERRED_FIQ * 0x4; 11311f9562aSJanusz Krzysztofik val = omap_readl(DEFERRED_FIQ_IH_BASE + offset) & ~(1 << 1); 11411f9562aSJanusz Krzysztofik omap_writel(val, DEFERRED_FIQ_IH_BASE + offset); 11511f9562aSJanusz Krzysztofik 11611f9562aSJanusz Krzysztofik set_fiq_handler(fiqhandler_start, fiqhandler_length); 11711f9562aSJanusz Krzysztofik 11811f9562aSJanusz Krzysztofik /* 11911f9562aSJanusz Krzysztofik * Initialise the buffer which is shared 12011f9562aSJanusz Krzysztofik * between FIQ mode and IRQ mode 12111f9562aSJanusz Krzysztofik */ 12211f9562aSJanusz Krzysztofik fiq_buffer[FIQ_GPIO_INT_MASK] = 0; 12311f9562aSJanusz Krzysztofik fiq_buffer[FIQ_MASK] = 0; 12411f9562aSJanusz Krzysztofik fiq_buffer[FIQ_STATE] = 0; 12511f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEY] = 0; 12611f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEYS_CNT] = 0; 12711f9562aSJanusz Krzysztofik fiq_buffer[FIQ_KEYS_HICNT] = 0; 12811f9562aSJanusz Krzysztofik fiq_buffer[FIQ_TAIL_OFFSET] = 0; 12911f9562aSJanusz Krzysztofik fiq_buffer[FIQ_HEAD_OFFSET] = 0; 13011f9562aSJanusz Krzysztofik fiq_buffer[FIQ_BUF_LEN] = 256; 13111f9562aSJanusz Krzysztofik fiq_buffer[FIQ_MISSED_KEYS] = 0; 13211f9562aSJanusz Krzysztofik fiq_buffer[FIQ_BUFFER_START] = 13311f9562aSJanusz Krzysztofik (unsigned int) &fiq_buffer[FIQ_CIRC_BUFF]; 13411f9562aSJanusz Krzysztofik 13511f9562aSJanusz Krzysztofik for (i = FIQ_CNT_INT_00; i <= FIQ_CNT_INT_15; i++) 13611f9562aSJanusz Krzysztofik fiq_buffer[i] = 0; 13711f9562aSJanusz Krzysztofik 13811f9562aSJanusz Krzysztofik /* 139*566ad81fSAndrea Gelmini * FIQ mode r9 always points to the fiq_buffer, because the FIQ isr 14011f9562aSJanusz Krzysztofik * will run in an unpredictable context. The fiq_buffer is the FIQ isr's 14111f9562aSJanusz Krzysztofik * only means of communication with the IRQ level and other kernel 14211f9562aSJanusz Krzysztofik * context code. 14311f9562aSJanusz Krzysztofik */ 14411f9562aSJanusz Krzysztofik FIQ_regs.ARM_r9 = (unsigned int)fiq_buffer; 14511f9562aSJanusz Krzysztofik set_fiq_regs(&FIQ_regs); 14611f9562aSJanusz Krzysztofik 14711f9562aSJanusz Krzysztofik pr_info("request_fiq(): fiq_buffer = %p\n", fiq_buffer); 14811f9562aSJanusz Krzysztofik 14911f9562aSJanusz Krzysztofik /* 15011f9562aSJanusz Krzysztofik * Redirect GPIO interrupts to FIQ 15111f9562aSJanusz Krzysztofik */ 15211f9562aSJanusz Krzysztofik offset = IRQ_ILR0_REG_OFFSET + INT_GPIO_BANK1 * 0x4; 15311f9562aSJanusz Krzysztofik val = omap_readl(OMAP_IH1_BASE + offset) | 1; 15411f9562aSJanusz Krzysztofik omap_writel(val, OMAP_IH1_BASE + offset); 15511f9562aSJanusz Krzysztofik } 156