gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <asm/io.h>
  18. #include <asm/hardware.h>
  19. #include <asm/arch/gpio.h>
  20. static const u32 pio_controller_offset[4] = {
  21. AT91_PIOA,
  22. AT91_PIOB,
  23. AT91_PIOC,
  24. AT91_PIOD,
  25. };
  26. static inline void __iomem *pin_to_controller(unsigned pin)
  27. {
  28. void __iomem *sys_base = (void __iomem *) AT91_VA_BASE_SYS;
  29. pin -= PIN_BASE;
  30. pin /= 32;
  31. if (likely(pin < BGA_GPIO_BANKS))
  32. return sys_base + pio_controller_offset[pin];
  33. return NULL;
  34. }
  35. static inline unsigned pin_to_mask(unsigned pin)
  36. {
  37. pin -= PIN_BASE;
  38. return 1 << (pin % 32);
  39. }
  40. /*--------------------------------------------------------------------------*/
  41. /* Not all hardware capabilities are exposed through these calls; they
  42. * only encapsulate the most common features and modes. (So if you
  43. * want to change signals in groups, do it directly.)
  44. *
  45. * Bootloaders will usually handle some of the pin multiplexing setup.
  46. * The intent is certainly that by the time Linux is fully booted, all
  47. * pins should have been fully initialized. These setup calls should
  48. * only be used by board setup routines, or possibly in driver probe().
  49. *
  50. * For bootloaders doing all that setup, these calls could be inlined
  51. * as NOPs so Linux won't duplicate any setup code
  52. */
  53. /*
  54. * mux the pin to the "A" internal peripheral role.
  55. */
  56. int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup)
  57. {
  58. void __iomem *pio = pin_to_controller(pin);
  59. unsigned mask = pin_to_mask(pin);
  60. if (!pio)
  61. return -EINVAL;
  62. __raw_writel(mask, pio + PIO_IDR);
  63. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  64. __raw_writel(mask, pio + PIO_ASR);
  65. __raw_writel(mask, pio + PIO_PDR);
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(at91_set_A_periph);
  69. /*
  70. * mux the pin to the "B" internal peripheral role.
  71. */
  72. int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup)
  73. {
  74. void __iomem *pio = pin_to_controller(pin);
  75. unsigned mask = pin_to_mask(pin);
  76. if (!pio)
  77. return -EINVAL;
  78. __raw_writel(mask, pio + PIO_IDR);
  79. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  80. __raw_writel(mask, pio + PIO_BSR);
  81. __raw_writel(mask, pio + PIO_PDR);
  82. return 0;
  83. }
  84. EXPORT_SYMBOL(at91_set_B_periph);
  85. /*
  86. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  87. * configure it for an input.
  88. */
  89. int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup)
  90. {
  91. void __iomem *pio = pin_to_controller(pin);
  92. unsigned mask = pin_to_mask(pin);
  93. if (!pio)
  94. return -EINVAL;
  95. __raw_writel(mask, pio + PIO_IDR);
  96. __raw_writel(mask, pio + (use_pullup ? PIO_PUER : PIO_PUDR));
  97. __raw_writel(mask, pio + PIO_ODR);
  98. __raw_writel(mask, pio + PIO_PER);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL(at91_set_gpio_input);
  102. /*
  103. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  104. * and configure it for an output.
  105. */
  106. int __init_or_module at91_set_gpio_output(unsigned pin, int value)
  107. {
  108. void __iomem *pio = pin_to_controller(pin);
  109. unsigned mask = pin_to_mask(pin);
  110. if (!pio)
  111. return -EINVAL;
  112. __raw_writel(mask, pio + PIO_IDR);
  113. __raw_writel(mask, pio + PIO_PUDR);
  114. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  115. __raw_writel(mask, pio + PIO_OER);
  116. __raw_writel(mask, pio + PIO_PER);
  117. return 0;
  118. }
  119. EXPORT_SYMBOL(at91_set_gpio_output);
  120. /*
  121. * enable/disable the glitch filter; mostly used with IRQ handling.
  122. */
  123. int __init_or_module at91_set_deglitch(unsigned pin, int is_on)
  124. {
  125. void __iomem *pio = pin_to_controller(pin);
  126. unsigned mask = pin_to_mask(pin);
  127. if (!pio)
  128. return -EINVAL;
  129. __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(at91_set_deglitch);
  133. /*
  134. * enable/disable the multi-driver; This is only valid for output and
  135. * allows the output pin to run as an open collector output.
  136. */
  137. int __init_or_module at91_set_multi_drive(unsigned pin, int is_on)
  138. {
  139. void __iomem *pio = pin_to_controller(pin);
  140. unsigned mask = pin_to_mask(pin);
  141. if (!pio)
  142. return -EINVAL;
  143. __raw_writel(mask, pio + (is_on ? PIO_MDER : PIO_MDDR));
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(at91_set_multi_drive);
  147. /*--------------------------------------------------------------------------*/
  148. /*
  149. * assuming the pin is muxed as a gpio output, set its value.
  150. */
  151. int at91_set_gpio_value(unsigned pin, int value)
  152. {
  153. void __iomem *pio = pin_to_controller(pin);
  154. unsigned mask = pin_to_mask(pin);
  155. if (!pio)
  156. return -EINVAL;
  157. __raw_writel(mask, pio + (value ? PIO_SODR : PIO_CODR));
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(at91_set_gpio_value);
  161. /*
  162. * read the pin's value (works even if it's not muxed as a gpio).
  163. */
  164. int at91_get_gpio_value(unsigned pin)
  165. {
  166. void __iomem *pio = pin_to_controller(pin);
  167. unsigned mask = pin_to_mask(pin);
  168. u32 pdsr;
  169. if (!pio)
  170. return -EINVAL;
  171. pdsr = __raw_readl(pio + PIO_PDSR);
  172. return (pdsr & mask) != 0;
  173. }
  174. EXPORT_SYMBOL(at91_get_gpio_value);
  175. /*--------------------------------------------------------------------------*/
  176. #ifdef CONFIG_PM
  177. static u32 wakeups[BGA_GPIO_BANKS];
  178. static u32 backups[BGA_GPIO_BANKS];
  179. static int gpio_irq_set_wake(unsigned pin, unsigned state)
  180. {
  181. unsigned mask = pin_to_mask(pin);
  182. pin -= PIN_BASE;
  183. pin /= 32;
  184. if (unlikely(pin >= BGA_GPIO_BANKS))
  185. return -EINVAL;
  186. if (state)
  187. wakeups[pin] |= mask;
  188. else
  189. wakeups[pin] &= ~mask;
  190. return 0;
  191. }
  192. void at91_gpio_suspend(void)
  193. {
  194. int i;
  195. for (i = 0; i < BGA_GPIO_BANKS; i++) {
  196. u32 pio = pio_controller_offset[i];
  197. /*
  198. * Note: drivers should have disabled GPIO interrupts that
  199. * aren't supposed to be wakeup sources.
  200. * But that is not much good on ARM..... disable_irq() does
  201. * not update the hardware immediately, so the hardware mask
  202. * (IMR) has the wrong value (not current, too much is
  203. * permitted).
  204. *
  205. * Our workaround is to disable all non-wakeup IRQs ...
  206. * which is exactly what correct drivers asked for in the
  207. * first place!
  208. */
  209. backups[i] = at91_sys_read(pio + PIO_IMR);
  210. at91_sys_write(pio_controller_offset[i] + PIO_IDR, backups[i]);
  211. at91_sys_write(pio_controller_offset[i] + PIO_IER, wakeups[i]);
  212. if (!wakeups[i]) {
  213. disable_irq_wake(AT91_ID_PIOA + i);
  214. at91_sys_write(AT91_PMC_PCDR, 1 << (AT91_ID_PIOA + i));
  215. } else {
  216. enable_irq_wake(AT91_ID_PIOA + i);
  217. #ifdef CONFIG_PM_DEBUG
  218. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", "ABCD"[i], wakeups[i]);
  219. #endif
  220. }
  221. }
  222. }
  223. void at91_gpio_resume(void)
  224. {
  225. int i;
  226. for (i = 0; i < BGA_GPIO_BANKS; i++) {
  227. at91_sys_write(pio_controller_offset[i] + PIO_IDR, wakeups[i]);
  228. at91_sys_write(pio_controller_offset[i] + PIO_IER, backups[i]);
  229. }
  230. at91_sys_write(AT91_PMC_PCER,
  231. (1 << AT91_ID_PIOA)
  232. | (1 << AT91_ID_PIOB)
  233. | (1 << AT91_ID_PIOC)
  234. | (1 << AT91_ID_PIOD));
  235. }
  236. #else
  237. #define gpio_irq_set_wake NULL
  238. #endif
  239. /* Several AIC controller irqs are dispatched through this GPIO handler.
  240. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  241. * at91_set_gpio_input() then maybe enable its glitch filter.
  242. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  243. * handler, though it always triggers on rising and falling edges.
  244. *
  245. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  246. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  247. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  248. */
  249. static void gpio_irq_mask(unsigned pin)
  250. {
  251. void __iomem *pio = pin_to_controller(pin);
  252. unsigned mask = pin_to_mask(pin);
  253. if (pio)
  254. __raw_writel(mask, pio + PIO_IDR);
  255. }
  256. static void gpio_irq_unmask(unsigned pin)
  257. {
  258. void __iomem *pio = pin_to_controller(pin);
  259. unsigned mask = pin_to_mask(pin);
  260. if (pio)
  261. __raw_writel(mask, pio + PIO_IER);
  262. }
  263. static int gpio_irq_type(unsigned pin, unsigned type)
  264. {
  265. return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
  266. }
  267. static struct irqchip gpio_irqchip = {
  268. .mask = gpio_irq_mask,
  269. .unmask = gpio_irq_unmask,
  270. .set_type = gpio_irq_type,
  271. .set_wake = gpio_irq_set_wake,
  272. };
  273. static void gpio_irq_handler(unsigned irq, struct irqdesc *desc, struct pt_regs *regs)
  274. {
  275. unsigned pin;
  276. struct irqdesc *gpio;
  277. void __iomem *pio;
  278. u32 isr;
  279. pio = get_irq_chip_data(irq);
  280. /* temporarily mask (level sensitive) parent IRQ */
  281. desc->chip->ack(irq);
  282. for (;;) {
  283. /* reading ISR acks the pending (edge triggered) GPIO interrupt */
  284. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  285. if (!isr)
  286. break;
  287. pin = (unsigned) get_irq_data(irq);
  288. gpio = &irq_desc[pin];
  289. while (isr) {
  290. if (isr & 1) {
  291. if (unlikely(gpio->depth)) {
  292. /*
  293. * The core ARM interrupt handler lazily disables IRQs so
  294. * another IRQ must be generated before it actually gets
  295. * here to be disabled on the GPIO controller.
  296. */
  297. gpio_irq_mask(pin);
  298. }
  299. else
  300. desc_handle_irq(pin, gpio, regs);
  301. }
  302. pin++;
  303. gpio++;
  304. isr >>= 1;
  305. }
  306. }
  307. desc->chip->unmask(irq);
  308. /* now it may re-trigger */
  309. }
  310. /* call this from board-specific init_irq */
  311. void __init at91_gpio_irq_setup(unsigned banks)
  312. {
  313. unsigned pioc, pin, id;
  314. if (banks > 4)
  315. banks = 4;
  316. for (pioc = 0, pin = PIN_BASE, id = AT91_ID_PIOA;
  317. pioc < banks;
  318. pioc++, id++) {
  319. void __iomem *controller;
  320. unsigned i;
  321. controller = (void __iomem *) AT91_VA_BASE_SYS + pio_controller_offset[pioc];
  322. __raw_writel(~0, controller + PIO_IDR);
  323. set_irq_data(id, (void *) pin);
  324. set_irq_chipdata(id, controller);
  325. for (i = 0; i < 32; i++, pin++) {
  326. /*
  327. * Can use the "simple" and not "edge" handler since it's
  328. * shorter, and the AIC handles interupts sanely.
  329. */
  330. set_irq_chip(pin, &gpio_irqchip);
  331. set_irq_handler(pin, do_simple_IRQ);
  332. set_irq_flags(pin, IRQF_VALID);
  333. }
  334. set_irq_chained_handler(id, gpio_irq_handler);
  335. }
  336. pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, banks);
  337. }