gpio.c 10 KB

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