gpio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* linux/arch/arm/mach-s3c2410/gpio.c
  2. *
  3. * Copyright (c) 2004-2005 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2410 GPIO support
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Changelog
  23. * 13-Sep-2004 BJD Implemented change of MISCCR
  24. * 14-Sep-2004 BJD Added getpin call
  25. * 14-Sep-2004 BJD Fixed bug in setpin() call
  26. * 30-Sep-2004 BJD Fixed cfgpin() mask bug
  27. * 01-Oct-2004 BJD Added getcfg() to get pin configuration
  28. * 01-Oct-2004 BJD Fixed mask bug in pullup() call
  29. * 01-Oct-2004 BJD Added getirq() to turn pin into irqno
  30. * 04-Oct-2004 BJD Added irq filter controls for GPIO
  31. * 05-Nov-2004 BJD EXPORT_SYMBOL() added for all code
  32. * 13-Mar-2005 BJD Updates for __iomem
  33. * 26-Oct-2005 BJD Added generic configuration types
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/init.h>
  37. #include <linux/module.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/ioport.h>
  40. #include <asm/hardware.h>
  41. #include <asm/irq.h>
  42. #include <asm/io.h>
  43. #include <asm/arch/regs-gpio.h>
  44. void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function)
  45. {
  46. void __iomem *base = S3C2410_GPIO_BASE(pin);
  47. unsigned long mask;
  48. unsigned long con;
  49. unsigned long flags;
  50. if (pin < S3C2410_GPIO_BANKB) {
  51. mask = 1 << S3C2410_GPIO_OFFSET(pin);
  52. } else {
  53. mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
  54. }
  55. switch (function) {
  56. case S3C2410_GPIO_LEAVE:
  57. mask = 0;
  58. function = 0;
  59. break;
  60. case S3C2410_GPIO_INPUT:
  61. case S3C2410_GPIO_OUTPUT:
  62. case S3C2410_GPIO_SFN2:
  63. case S3C2410_GPIO_SFN3:
  64. if (pin < S3C2410_GPIO_BANKB) {
  65. function &= 1;
  66. function <<= S3C2410_GPIO_OFFSET(pin);
  67. } else {
  68. function &= 3;
  69. function <<= S3C2410_GPIO_OFFSET(pin)*2;
  70. }
  71. }
  72. /* modify the specified register wwith IRQs off */
  73. local_irq_save(flags);
  74. con = __raw_readl(base + 0x00);
  75. con &= ~mask;
  76. con |= function;
  77. __raw_writel(con, base + 0x00);
  78. local_irq_restore(flags);
  79. }
  80. EXPORT_SYMBOL(s3c2410_gpio_cfgpin);
  81. unsigned int s3c2410_gpio_getcfg(unsigned int pin)
  82. {
  83. void __iomem *base = S3C2410_GPIO_BASE(pin);
  84. unsigned long mask;
  85. if (pin < S3C2410_GPIO_BANKB) {
  86. mask = 1 << S3C2410_GPIO_OFFSET(pin);
  87. } else {
  88. mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
  89. }
  90. return __raw_readl(base) & mask;
  91. }
  92. EXPORT_SYMBOL(s3c2410_gpio_getcfg);
  93. void s3c2410_gpio_pullup(unsigned int pin, unsigned int to)
  94. {
  95. void __iomem *base = S3C2410_GPIO_BASE(pin);
  96. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  97. unsigned long flags;
  98. unsigned long up;
  99. if (pin < S3C2410_GPIO_BANKB)
  100. return;
  101. local_irq_save(flags);
  102. up = __raw_readl(base + 0x08);
  103. up &= ~(1L << offs);
  104. up |= to << offs;
  105. __raw_writel(up, base + 0x08);
  106. local_irq_restore(flags);
  107. }
  108. EXPORT_SYMBOL(s3c2410_gpio_pullup);
  109. void s3c2410_gpio_setpin(unsigned int pin, unsigned int to)
  110. {
  111. void __iomem *base = S3C2410_GPIO_BASE(pin);
  112. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  113. unsigned long flags;
  114. unsigned long dat;
  115. local_irq_save(flags);
  116. dat = __raw_readl(base + 0x04);
  117. dat &= ~(1 << offs);
  118. dat |= to << offs;
  119. __raw_writel(dat, base + 0x04);
  120. local_irq_restore(flags);
  121. }
  122. EXPORT_SYMBOL(s3c2410_gpio_setpin);
  123. unsigned int s3c2410_gpio_getpin(unsigned int pin)
  124. {
  125. void __iomem *base = S3C2410_GPIO_BASE(pin);
  126. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  127. return __raw_readl(base + 0x04) & (1<< offs);
  128. }
  129. EXPORT_SYMBOL(s3c2410_gpio_getpin);
  130. unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change)
  131. {
  132. unsigned long flags;
  133. unsigned long misccr;
  134. local_irq_save(flags);
  135. misccr = __raw_readl(S3C2410_MISCCR);
  136. misccr &= ~clear;
  137. misccr ^= change;
  138. __raw_writel(misccr, S3C2410_MISCCR);
  139. local_irq_restore(flags);
  140. return misccr;
  141. }
  142. EXPORT_SYMBOL(s3c2410_modify_misccr);
  143. int s3c2410_gpio_getirq(unsigned int pin)
  144. {
  145. if (pin < S3C2410_GPF0 || pin > S3C2410_GPG15_EINT23)
  146. return -1; /* not valid interrupts */
  147. if (pin < S3C2410_GPG0 && pin > S3C2410_GPF7)
  148. return -1; /* not valid pin */
  149. if (pin < S3C2410_GPF4)
  150. return (pin - S3C2410_GPF0) + IRQ_EINT0;
  151. if (pin < S3C2410_GPG0)
  152. return (pin - S3C2410_GPF4) + IRQ_EINT4;
  153. return (pin - S3C2410_GPG0) + IRQ_EINT8;
  154. }
  155. EXPORT_SYMBOL(s3c2410_gpio_getirq);
  156. int s3c2410_gpio_irqfilter(unsigned int pin, unsigned int on,
  157. unsigned int config)
  158. {
  159. void __iomem *reg = S3C2410_EINFLT0;
  160. unsigned long flags;
  161. unsigned long val;
  162. if (pin < S3C2410_GPG8 || pin > S3C2410_GPG15)
  163. return -1;
  164. config &= 0xff;
  165. pin -= S3C2410_GPG8_EINT16;
  166. reg += pin & ~3;
  167. local_irq_save(flags);
  168. /* update filter width and clock source */
  169. val = __raw_readl(reg);
  170. val &= ~(0xff << ((pin & 3) * 8));
  171. val |= config << ((pin & 3) * 8);
  172. __raw_writel(val, reg);
  173. /* update filter enable */
  174. val = __raw_readl(S3C2410_EXTINT2);
  175. val &= ~(1 << ((pin * 4) + 3));
  176. val |= on << ((pin * 4) + 3);
  177. __raw_writel(val, S3C2410_EXTINT2);
  178. local_irq_restore(flags);
  179. return 0;
  180. }
  181. EXPORT_SYMBOL(s3c2410_gpio_irqfilter);