gpio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * linux/arch/arm/mach-at91rm9200/gpio.c
  3. *
  4. * Copyright (C) 2005 HP Labs
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/module.h>
  15. #include <asm/io.h>
  16. #include <asm/mach/irq.h>
  17. #include <asm/arch/hardware.h>
  18. #include <asm/arch/gpio.h>
  19. static const u32 pio_controller_offset[4] = {
  20. AT91_PIOA,
  21. AT91_PIOB,
  22. AT91_PIOC,
  23. AT91_PIOD,
  24. };
  25. static inline void __iomem *pin_to_controller(unsigned pin)
  26. {
  27. void __iomem *sys_base = (void __iomem *) AT91_VA_BASE_SYS;
  28. pin -= PIN_BASE;
  29. pin /= 32;
  30. if (likely(pin < BGA_GPIO_BANKS))
  31. return sys_base + pio_controller_offset[pin];
  32. return NULL;
  33. }
  34. static inline unsigned pin_to_mask(unsigned pin)
  35. {
  36. pin -= PIN_BASE;
  37. return 1 << (pin % 32);
  38. }
  39. /*--------------------------------------------------------------------------*/
  40. /* Not all hardware capabilities are exposed through these calls; they
  41. * only encapsulate the most common features and modes. (So if you
  42. * want to change signals in groups, do it directly.)
  43. *
  44. * Bootloaders will usually handle some of the pin multiplexing setup.
  45. * The intent is certainly that by the time Linux is fully booted, all
  46. * pins should have been fully initialized. These setup calls should
  47. * only be used by board setup routines, or possibly in driver probe().
  48. *
  49. * For bootloaders doing all that setup, these calls could be inlined
  50. * as NOPs so Linux won't duplicate any setup code
  51. */
  52. /*
  53. * mux the pin to the "A" internal peripheral role.
  54. */
  55. int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
  56. {
  57. void __iomem *pio = pin_to_controller(pin);
  58. unsigned mask = pin_to_mask(pin);
  59. if (!pio)
  60. return -EINVAL;
  61. __raw_writel(mask, pio + PIO_IDR);
  62. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  63. __raw_writel(mask, pio + PIO_ASR);
  64. __raw_writel(mask, pio + PIO_PDR);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(at91_set_A_periph);
  68. /*
  69. * mux the pin to the "B" internal peripheral role.
  70. */
  71. int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
  72. {
  73. void __iomem *pio = pin_to_controller(pin);
  74. unsigned mask = pin_to_mask(pin);
  75. if (!pio)
  76. return -EINVAL;
  77. __raw_writel(mask, pio + PIO_IDR);
  78. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  79. __raw_writel(mask, pio + PIO_BSR);
  80. __raw_writel(mask, pio + PIO_PDR);
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(at91_set_B_periph);
  84. /*
  85. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  86. * configure it for an input.
  87. */
  88. int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
  89. {
  90. void __iomem *pio = pin_to_controller(pin);
  91. unsigned mask = pin_to_mask(pin);
  92. if (!pio)
  93. return -EINVAL;
  94. __raw_writel(mask, pio + PIO_IDR);
  95. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  96. __raw_writel(mask, pio + PIO_ODR);
  97. __raw_writel(mask, pio + PIO_PER);
  98. return 0;
  99. }
  100. EXPORT_SYMBOL(at91_set_gpio_input);
  101. /*
  102. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  103. * and configure it for an output.
  104. */
  105. int __init_or_module at91_set_gpio_output(unsigned pin, int value)
  106. {
  107. void __iomem *pio = pin_to_controller(pin);
  108. unsigned mask = pin_to_mask(pin);
  109. if (!pio)
  110. return -EINVAL;
  111. __raw_writel(mask, pio + PIO_IDR);
  112. __raw_writel(mask, pio + PIO_PUDR);
  113. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  114. __raw_writel(mask, pio + PIO_OER);
  115. __raw_writel(mask, pio + PIO_PER);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(at91_set_gpio_output);
  119. /*
  120. * enable/disable the glitch filter; mostly used with IRQ handling.
  121. */
  122. int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
  123. {
  124. void __iomem *pio = pin_to_controller(pin);
  125. unsigned mask = pin_to_mask(pin);
  126. if (!pio)
  127. return -EINVAL;
  128. __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(at91_set_deglitch);
  132. /*
  133. * enable/disable the multi-driver; This is only valid for output and
  134. * allows the output pin to run as an open collector output.
  135. */
  136. int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
  137. {
  138. void __iomem *pio = pin_to_controller(pin);
  139. unsigned mask = pin_to_mask(pin);
  140. if (!pio)
  141. return -EINVAL;
  142. __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(at91_set_multi_drive);
  146. /*--------------------------------------------------------------------------*/
  147. /*
  148. * assuming the pin is muxed as a gpio output, set its value.
  149. */
  150. int at91_set_gpio_value(unsigned pin, int value)
  151. {
  152. void __iomem *pio = pin_to_controller(pin);
  153. unsigned mask = pin_to_mask(pin);
  154. if (!pio)
  155. return -EINVAL;
  156. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(at91_set_gpio_value);
  160. /*
  161. * read the pin's value (works even if it's not muxed as a gpio).
  162. */
  163. int at91_get_gpio_value(unsigned pin)
  164. {
  165. void __iomem *pio = pin_to_controller(pin);
  166. unsigned mask = pin_to_mask(pin);
  167. u32 pdsr;
  168. if (!pio)
  169. return -EINVAL;
  170. pdsr = __raw_readl(pio + PIO_PDSR);
  171. return (pdsr & mask) != 0;
  172. }
  173. EXPORT_SYMBOL(at91_get_gpio_value);
  174. /*--------------------------------------------------------------------------*/
  175. /* Several AIC controller irqs are dispatched through this GPIO handler.
  176. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  177. * at91_set_gpio_input() then maybe enable its glitch filter.
  178. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  179. * handler, though it always triggers on rising and falling edges.
  180. *
  181. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  182. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  183. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  184. */
  185. static void gpio_irq_mask(unsigned pin)
  186. {
  187. void __iomem *pio = pin_to_controller(pin);
  188. unsigned mask = pin_to_mask(pin);
  189. if (pio)
  190. __raw_writel(mask, pio + PIO_IDR);
  191. }
  192. static void gpio_irq_unmask(unsigned pin)
  193. {
  194. void __iomem *pio = pin_to_controller(pin);
  195. unsigned mask = pin_to_mask(pin);
  196. if (pio)
  197. __raw_writel(mask, pio + PIO_IER);
  198. }
  199. static int gpio_irq_type(unsigned pin, unsigned type)
  200. {
  201. return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
  202. }
  203. static struct irqchip gpio_irqchip = {
  204. .mask = gpio_irq_mask,
  205. .unmask = gpio_irq_unmask,
  206. .set_type = gpio_irq_type,
  207. };
  208. static void gpio_irq_handler(unsigned irq, struct irqdesc *desc, struct pt_regs *regs)
  209. {
  210. unsigned pin;
  211. struct irqdesc *gpio;
  212. void __iomem *pio;
  213. u32 isr;
  214. pio = desc->base;
  215. /* temporarily mask (level sensitive) parent IRQ */
  216. desc->chip->ack(irq);
  217. for (;;) {
  218. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  219. if (!isr)
  220. break;
  221. pin = (unsigned) desc->data;
  222. gpio = &irq_desc[pin];
  223. while (isr) {
  224. if (isr & 1) {
  225. if (unlikely(gpio->disable_depth)) {
  226. /*
  227. * The core ARM interrupt handler lazily disables IRQs so
  228. * another IRQ must be generated before it actually gets
  229. * here to be disabled on the GPIO controller.
  230. */
  231. gpio_irq_mask(pin);
  232. }
  233. else
  234. gpio->handle(pin, gpio, regs);
  235. }
  236. pin++;
  237. gpio++;
  238. isr >>= 1;
  239. }
  240. }
  241. desc->chip->unmask(irq);
  242. /* now it may re-trigger */
  243. }
  244. /* call this from board-specific init_irq */
  245. void __init at91_gpio_irq_setup(unsigned banks)
  246. {
  247. unsigned pioc, pin, id;
  248. if (banks > 4)
  249. banks = 4;
  250. for (pioc = 0, pin = PIN_BASE, id = AT91_ID_PIOA;
  251. pioc < banks;
  252. pioc++, id++) {
  253. void __iomem *controller;
  254. unsigned i;
  255. controller = (void __iomem *) AT91_VA_BASE_SYS + pio_controller_offset[pioc];
  256. __raw_writel(~0, controller + PIO_IDR);
  257. set_irq_data(id, (void *) pin);
  258. set_irq_chipdata(id, controller);
  259. for (i = 0; i < 32; i++, pin++) {
  260. set_irq_chip(pin, &gpio_irqchip);
  261. set_irq_handler(pin, do_simple_IRQ);
  262. set_irq_flags(pin, IRQF_VALID);
  263. }
  264. set_irq_chained_handler(id, gpio_irq_handler);
  265. /* enable the PIO peripheral clock */
  266. at91_sys_write(AT91_PMC_PCER, 1 << id);
  267. }
  268. pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, banks);
  269. }