gpio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/ioport.h>
  27. #include <asm/hardware.h>
  28. #include <asm/irq.h>
  29. #include <asm/io.h>
  30. #include <asm/arch/regs-gpio.h>
  31. void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function)
  32. {
  33. void __iomem *base = S3C24XX_GPIO_BASE(pin);
  34. unsigned long mask;
  35. unsigned long con;
  36. unsigned long flags;
  37. if (pin < S3C2410_GPIO_BANKB) {
  38. mask = 1 << S3C2410_GPIO_OFFSET(pin);
  39. } else {
  40. mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
  41. }
  42. switch (function) {
  43. case S3C2410_GPIO_LEAVE:
  44. mask = 0;
  45. function = 0;
  46. break;
  47. case S3C2410_GPIO_INPUT:
  48. case S3C2410_GPIO_OUTPUT:
  49. case S3C2410_GPIO_SFN2:
  50. case S3C2410_GPIO_SFN3:
  51. if (pin < S3C2410_GPIO_BANKB) {
  52. function &= 1;
  53. function <<= S3C2410_GPIO_OFFSET(pin);
  54. } else {
  55. function &= 3;
  56. function <<= S3C2410_GPIO_OFFSET(pin)*2;
  57. }
  58. }
  59. /* modify the specified register wwith IRQs off */
  60. local_irq_save(flags);
  61. con = __raw_readl(base + 0x00);
  62. con &= ~mask;
  63. con |= function;
  64. __raw_writel(con, base + 0x00);
  65. local_irq_restore(flags);
  66. }
  67. EXPORT_SYMBOL(s3c2410_gpio_cfgpin);
  68. unsigned int s3c2410_gpio_getcfg(unsigned int pin)
  69. {
  70. void __iomem *base = S3C24XX_GPIO_BASE(pin);
  71. unsigned long mask;
  72. if (pin < S3C2410_GPIO_BANKB) {
  73. mask = 1 << S3C2410_GPIO_OFFSET(pin);
  74. } else {
  75. mask = 3 << S3C2410_GPIO_OFFSET(pin)*2;
  76. }
  77. return __raw_readl(base) & mask;
  78. }
  79. EXPORT_SYMBOL(s3c2410_gpio_getcfg);
  80. void s3c2410_gpio_pullup(unsigned int pin, unsigned int to)
  81. {
  82. void __iomem *base = S3C24XX_GPIO_BASE(pin);
  83. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  84. unsigned long flags;
  85. unsigned long up;
  86. if (pin < S3C2410_GPIO_BANKB)
  87. return;
  88. local_irq_save(flags);
  89. up = __raw_readl(base + 0x08);
  90. up &= ~(1L << offs);
  91. up |= to << offs;
  92. __raw_writel(up, base + 0x08);
  93. local_irq_restore(flags);
  94. }
  95. EXPORT_SYMBOL(s3c2410_gpio_pullup);
  96. void s3c2410_gpio_setpin(unsigned int pin, unsigned int to)
  97. {
  98. void __iomem *base = S3C24XX_GPIO_BASE(pin);
  99. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  100. unsigned long flags;
  101. unsigned long dat;
  102. local_irq_save(flags);
  103. dat = __raw_readl(base + 0x04);
  104. dat &= ~(1 << offs);
  105. dat |= to << offs;
  106. __raw_writel(dat, base + 0x04);
  107. local_irq_restore(flags);
  108. }
  109. EXPORT_SYMBOL(s3c2410_gpio_setpin);
  110. unsigned int s3c2410_gpio_getpin(unsigned int pin)
  111. {
  112. void __iomem *base = S3C24XX_GPIO_BASE(pin);
  113. unsigned long offs = S3C2410_GPIO_OFFSET(pin);
  114. return __raw_readl(base + 0x04) & (1<< offs);
  115. }
  116. EXPORT_SYMBOL(s3c2410_gpio_getpin);
  117. unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change)
  118. {
  119. unsigned long flags;
  120. unsigned long misccr;
  121. local_irq_save(flags);
  122. misccr = __raw_readl(S3C24XX_MISCCR);
  123. misccr &= ~clear;
  124. misccr ^= change;
  125. __raw_writel(misccr, S3C24XX_MISCCR);
  126. local_irq_restore(flags);
  127. return misccr;
  128. }
  129. EXPORT_SYMBOL(s3c2410_modify_misccr);