gpio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. */
  34. #include <linux/kernel.h>
  35. #include <linux/init.h>
  36. #include <linux/module.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/ioport.h>
  39. #include <asm/hardware.h>
  40. #include <asm/irq.h>
  41. #include <asm/io.h>
  42. #include <asm/arch/regs-gpio.h>
  43. void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function)
  44. {
  45. void __iomem *base = S3C2410_GPIO_BASE(pin);
  46. unsigned long mask;
  47. unsigned long con;
  48. unsigned long flags;
  49. if (pin < S3C2410_GPIO_BANKB) {
  50. mask = 1 << S3C2410_GPIO_OFFSET(pin);
  51. } else {
  52. mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
  53. }
  54. local_irq_save(flags);
  55. con = __raw_readl(base + 0x00);
  56. con &= ~mask;
  57. con |= function;
  58. __raw_writel(con, base + 0x00);
  59. local_irq_restore(flags);
  60. }
  61. EXPORT_SYMBOL(s3c2410_gpio_cfgpin);
  62. unsigned int s3c2410_gpio_getcfg(unsigned int pin)
  63. {
  64. void __iomem *base = S3C2410_GPIO_BASE(pin);
  65. unsigned long mask;
  66. if (pin < S3C2410_GPIO_BANKB) {
  67. mask = 1 << S3C2410_GPIO_OFFSET(pin);
  68. } else {
  69. mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
  70. }
  71. return __raw_readl(base) & mask;
  72. }
  73. EXPORT_SYMBOL(s3c2410_gpio_getcfg);
  74. void s3c2410_gpio_pullup(unsigned int pin, unsigned int to)
  75. {
  76. void __iomem *base = S3C2410_GPIO_BASE(pin);
  77. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  78. unsigned long flags;
  79. unsigned long up;
  80. if (pin < S3C2410_GPIO_BANKB)
  81. return;
  82. local_irq_save(flags);
  83. up = __raw_readl(base + 0x08);
  84. up &= ~(1L << offs);
  85. up |= to << offs;
  86. __raw_writel(up, base + 0x08);
  87. local_irq_restore(flags);
  88. }
  89. EXPORT_SYMBOL(s3c2410_gpio_pullup);
  90. void s3c2410_gpio_setpin(unsigned int pin, unsigned int to)
  91. {
  92. void __iomem *base = S3C2410_GPIO_BASE(pin);
  93. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  94. unsigned long flags;
  95. unsigned long dat;
  96. local_irq_save(flags);
  97. dat = __raw_readl(base + 0x04);
  98. dat &= ~(1 << offs);
  99. dat |= to << offs;
  100. __raw_writel(dat, base + 0x04);
  101. local_irq_restore(flags);
  102. }
  103. EXPORT_SYMBOL(s3c2410_gpio_setpin);
  104. unsigned int s3c2410_gpio_getpin(unsigned int pin)
  105. {
  106. void __iomem *base = S3C2410_GPIO_BASE(pin);
  107. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  108. return __raw_readl(base + 0x04) & (1<< offs);
  109. }
  110. EXPORT_SYMBOL(s3c2410_gpio_getpin);
  111. unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change)
  112. {
  113. unsigned long flags;
  114. unsigned long misccr;
  115. local_irq_save(flags);
  116. misccr = __raw_readl(S3C2410_MISCCR);
  117. misccr &= ~clear;
  118. misccr ^= change;
  119. __raw_writel(misccr, S3C2410_MISCCR);
  120. local_irq_restore(flags);
  121. return misccr;
  122. }
  123. EXPORT_SYMBOL(s3c2410_modify_misccr);
  124. int s3c2410_gpio_getirq(unsigned int pin)
  125. {
  126. if (pin < S3C2410_GPF0 || pin > S3C2410_GPG15_EINT23)
  127. return -1; /* not valid interrupts */
  128. if (pin < S3C2410_GPG0 && pin > S3C2410_GPF7)
  129. return -1; /* not valid pin */
  130. if (pin < S3C2410_GPF4)
  131. return (pin - S3C2410_GPF0) + IRQ_EINT0;
  132. if (pin < S3C2410_GPG0)
  133. return (pin - S3C2410_GPF4) + IRQ_EINT4;
  134. return (pin - S3C2410_GPG0) + IRQ_EINT8;
  135. }
  136. EXPORT_SYMBOL(s3c2410_gpio_getirq);
  137. int s3c2410_gpio_irqfilter(unsigned int pin, unsigned int on,
  138. unsigned int config)
  139. {
  140. void __iomem *reg = S3C2410_EINFLT0;
  141. unsigned long flags;
  142. unsigned long val;
  143. if (pin < S3C2410_GPG8 || pin > S3C2410_GPG15)
  144. return -1;
  145. config &= 0xff;
  146. pin -= S3C2410_GPG8_EINT16;
  147. reg += pin & ~3;
  148. local_irq_save(flags);
  149. /* update filter width and clock source */
  150. val = __raw_readl(reg);
  151. val &= ~(0xff << ((pin & 3) * 8));
  152. val |= config << ((pin & 3) * 8);
  153. __raw_writel(val, reg);
  154. /* update filter enable */
  155. val = __raw_readl(S3C2410_EXTINT2);
  156. val &= ~(1 << ((pin * 4) + 3));
  157. val |= on << ((pin * 4) + 3);
  158. __raw_writel(val, S3C2410_EXTINT2);
  159. local_irq_restore(flags);
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(s3c2410_gpio_irqfilter);