gpio.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/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. #ifdef CONFIG_PM
  176. static u32 wakeups[BGA_GPIO_BANKS];
  177. static u32 backups[BGA_GPIO_BANKS];
  178. static int gpio_irq_set_wake(unsigned pin, unsigned state)
  179. {
  180. unsigned mask = pin_to_mask(pin);
  181. pin -= PIN_BASE;
  182. pin /= 32;
  183. if (unlikely(pin >= BGA_GPIO_BANKS))
  184. return -EINVAL;
  185. if (state)
  186. wakeups[pin] |= mask;
  187. else
  188. wakeups[pin] &= ~mask;
  189. return 0;
  190. }
  191. void at91_gpio_suspend(void)
  192. {
  193. int i;
  194. for (i = 0; i < BGA_GPIO_BANKS; i++) {
  195. u32 pio = pio_controller_offset[i];
  196. /*
  197. * Note: drivers should have disabled GPIO interrupts that
  198. * aren't supposed to be wakeup sources.
  199. * But that is not much good on ARM..... disable_irq() does
  200. * not update the hardware immediately, so the hardware mask
  201. * (IMR) has the wrong value (not current, too much is
  202. * permitted).
  203. *
  204. * Our workaround is to disable all non-wakeup IRQs ...
  205. * which is exactly what correct drivers asked for in the
  206. * first place!
  207. */
  208. backups[i] = at91_sys_read(pio + PIO_IMR);
  209. at91_sys_write(pio_controller_offset[i] + PIO_IDR, backups[i]);
  210. at91_sys_write(pio_controller_offset[i] + PIO_IER, wakeups[i]);
  211. if (!wakeups[i]) {
  212. disable_irq_wake(AT91_ID_PIOA + i);
  213. at91_sys_write(AT91_PMC_PCDR, 1 << (AT91_ID_PIOA + i));
  214. } else {
  215. enable_irq_wake(AT91_ID_PIOA + i);
  216. #ifdef CONFIG_PM_DEBUG
  217. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n", "ABCD"[i], wakeups[i]);
  218. #endif
  219. }
  220. }
  221. }
  222. void at91_gpio_resume(void)
  223. {
  224. int i;
  225. for (i = 0; i < BGA_GPIO_BANKS; i++) {
  226. at91_sys_write(pio_controller_offset[i] + PIO_IDR, wakeups[i]);
  227. at91_sys_write(pio_controller_offset[i] + PIO_IER, backups[i]);
  228. }
  229. at91_sys_write(AT91_PMC_PCER,
  230. (1 << AT91_ID_PIOA)
  231. | (1 << AT91_ID_PIOB)
  232. | (1 << AT91_ID_PIOC)
  233. | (1 << AT91_ID_PIOD));
  234. }
  235. #else
  236. #define gpio_irq_set_wake NULL
  237. #endif
  238. /* Several AIC controller irqs are dispatched through this GPIO handler.
  239. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  240. * at91_set_gpio_input() then maybe enable its glitch filter.
  241. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  242. * handler, though it always triggers on rising and falling edges.
  243. *
  244. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  245. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  246. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  247. */
  248. static void gpio_irq_mask(unsigned pin)
  249. {
  250. void __iomem *pio = pin_to_controller(pin);
  251. unsigned mask = pin_to_mask(pin);
  252. if (pio)
  253. __raw_writel(mask, pio + PIO_IDR);
  254. }
  255. static void gpio_irq_unmask(unsigned pin)
  256. {
  257. void __iomem *pio = pin_to_controller(pin);
  258. unsigned mask = pin_to_mask(pin);
  259. if (pio)
  260. __raw_writel(mask, pio + PIO_IER);
  261. }
  262. static int gpio_irq_type(unsigned pin, unsigned type)
  263. {
  264. return (type == IRQT_BOTHEDGE) ? 0 : -EINVAL;
  265. }
  266. static struct irqchip gpio_irqchip = {
  267. .mask = gpio_irq_mask,
  268. .unmask = gpio_irq_unmask,
  269. .set_type = gpio_irq_type,
  270. .set_wake = gpio_irq_set_wake,
  271. };
  272. static void gpio_irq_handler(unsigned irq, struct irqdesc *desc, struct pt_regs *regs)
  273. {
  274. unsigned pin;
  275. struct irqdesc *gpio;
  276. void __iomem *pio;
  277. u32 isr;
  278. pio = desc->base;
  279. /* temporarily mask (level sensitive) parent IRQ */
  280. desc->chip->ack(irq);
  281. for (;;) {
  282. /* reading ISR acks the pending (edge triggered) GPIO interrupt */
  283. isr = __raw_readl(pio + PIO_ISR) & __raw_readl(pio + PIO_IMR);
  284. if (!isr)
  285. break;
  286. pin = (unsigned) desc->data;
  287. gpio = &irq_desc[pin];
  288. while (isr) {
  289. if (isr & 1) {
  290. if (unlikely(gpio->disable_depth)) {
  291. /*
  292. * The core ARM interrupt handler lazily disables IRQs so
  293. * another IRQ must be generated before it actually gets
  294. * here to be disabled on the GPIO controller.
  295. */
  296. gpio_irq_mask(pin);
  297. }
  298. else
  299. gpio->handle(pin, gpio, regs);
  300. }
  301. pin++;
  302. gpio++;
  303. isr >>= 1;
  304. }
  305. }
  306. desc->chip->unmask(irq);
  307. /* now it may re-trigger */
  308. }
  309. /* call this from board-specific init_irq */
  310. void __init at91_gpio_irq_setup(unsigned banks)
  311. {
  312. unsigned pioc, pin, id;
  313. if (banks > 4)
  314. banks = 4;
  315. for (pioc = 0, pin = PIN_BASE, id = AT91_ID_PIOA;
  316. pioc < banks;
  317. pioc++, id++) {
  318. void __iomem *controller;
  319. unsigned i;
  320. controller = (void __iomem *) AT91_VA_BASE_SYS + pio_controller_offset[pioc];
  321. __raw_writel(~0, controller + PIO_IDR);
  322. set_irq_data(id, (void *) pin);
  323. set_irq_chipdata(id, controller);
  324. for (i = 0; i < 32; i++, pin++) {
  325. /*
  326. * Can use the "simple" and not "edge" handler since it's
  327. * shorter, and the AIC handles interupts sanely.
  328. */
  329. set_irq_chip(pin, &gpio_irqchip);
  330. set_irq_handler(pin, do_simple_IRQ);
  331. set_irq_flags(pin, IRQF_VALID);
  332. }
  333. set_irq_chained_handler(id, gpio_irq_handler);
  334. }
  335. pr_info("AT91: %d gpio irqs in %d banks\n", pin - PIN_BASE, banks);
  336. }