ams-delta-fiq.c 6.6 KB

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