gpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * (C) Copyright 2009 Alessandro Rubini
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/gpio.h>
  25. static unsigned long gpio_base[4] = {
  26. NOMADIK_GPIO0_BASE,
  27. NOMADIK_GPIO1_BASE,
  28. NOMADIK_GPIO2_BASE,
  29. NOMADIK_GPIO3_BASE
  30. };
  31. enum gpio_registers {
  32. GPIO_DAT = 0x00, /* data register */
  33. GPIO_DATS = 0x04, /* data set */
  34. GPIO_DATC = 0x08, /* data clear */
  35. GPIO_PDIS = 0x0c, /* pull disable */
  36. GPIO_DIR = 0x10, /* direction */
  37. GPIO_DIRS = 0x14, /* direction set */
  38. GPIO_DIRC = 0x18, /* direction clear */
  39. GPIO_AFSLA = 0x20, /* alternate function select A */
  40. GPIO_AFSLB = 0x24, /* alternate function select B */
  41. };
  42. static inline unsigned long gpio_to_base(int gpio)
  43. {
  44. return gpio_base[gpio / 32];
  45. }
  46. static inline u32 gpio_to_bit(int gpio)
  47. {
  48. return 1 << (gpio & 0x1f);
  49. }
  50. void nmk_gpio_af(int gpio, int alternate_function)
  51. {
  52. unsigned long base = gpio_to_base(gpio);
  53. u32 bit = gpio_to_bit(gpio);
  54. u32 afunc, bfunc;
  55. /* alternate function is 0..3, with one bit per register */
  56. afunc = readl(base + GPIO_AFSLA) & ~bit;
  57. bfunc = readl(base + GPIO_AFSLB) & ~bit;
  58. if (alternate_function & 1) afunc |= bit;
  59. if (alternate_function & 2) bfunc |= bit;
  60. writel(afunc, base + GPIO_AFSLA);
  61. writel(bfunc, base + GPIO_AFSLB);
  62. }
  63. void nmk_gpio_dir(int gpio, int dir)
  64. {
  65. unsigned long base = gpio_to_base(gpio);
  66. u32 bit = gpio_to_bit(gpio);
  67. if (dir)
  68. writel(bit, base + GPIO_DIRS);
  69. else
  70. writel(bit, base + GPIO_DIRC);
  71. }
  72. void nmk_gpio_set(int gpio, int val)
  73. {
  74. unsigned long base = gpio_to_base(gpio);
  75. u32 bit = gpio_to_bit(gpio);
  76. if (val)
  77. writel(bit, base + GPIO_DATS);
  78. else
  79. writel(bit, base + GPIO_DATC);
  80. }
  81. int nmk_gpio_get(int gpio)
  82. {
  83. unsigned long base = gpio_to_base(gpio);
  84. u32 bit = gpio_to_bit(gpio);
  85. return readl(base + GPIO_DAT) & bit;
  86. }