gpio-ns9360.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * arch/arm/mach-ns9xxx/gpio-ns9360.c
  3. *
  4. * Copyright (C) 2006,2007 by Digi International Inc.
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/bug.h>
  12. #include <linux/errno.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <mach/regs-bbu.h>
  17. #include <mach/processor-ns9360.h>
  18. #include "gpio-ns9360.h"
  19. static inline int ns9360_valid_gpio(unsigned gpio)
  20. {
  21. return gpio <= 72;
  22. }
  23. static inline void __iomem *ns9360_gpio_get_gconfaddr(unsigned gpio)
  24. {
  25. if (gpio < 56)
  26. return BBU_GCONFb1(gpio / 8);
  27. else
  28. /*
  29. * this could be optimised away on
  30. * ns9750 only builds, but it isn't ...
  31. */
  32. return BBU_GCONFb2((gpio - 56) / 8);
  33. }
  34. static inline void __iomem *ns9360_gpio_get_gctrladdr(unsigned gpio)
  35. {
  36. if (gpio < 32)
  37. return BBU_GCTRL1;
  38. else if (gpio < 64)
  39. return BBU_GCTRL2;
  40. else
  41. /* this could be optimised away on ns9750 only builds */
  42. return BBU_GCTRL3;
  43. }
  44. static inline void __iomem *ns9360_gpio_get_gstataddr(unsigned gpio)
  45. {
  46. if (gpio < 32)
  47. return BBU_GSTAT1;
  48. else if (gpio < 64)
  49. return BBU_GSTAT2;
  50. else
  51. /* this could be optimised away on ns9750 only builds */
  52. return BBU_GSTAT3;
  53. }
  54. /*
  55. * each gpio can serve for 4 different purposes [0..3]. These are called
  56. * "functions" and passed in the parameter func. Functions 0-2 are always some
  57. * special things, function 3 is GPIO. If func == 3 dir specifies input or
  58. * output, and with inv you can enable an inverter (independent of func).
  59. */
  60. int __ns9360_gpio_configure(unsigned gpio, int dir, int inv, int func)
  61. {
  62. void __iomem *conf = ns9360_gpio_get_gconfaddr(gpio);
  63. u32 confval;
  64. confval = __raw_readl(conf);
  65. REGSETIM_IDX(confval, BBU_GCONFx, DIR, gpio & 7, dir);
  66. REGSETIM_IDX(confval, BBU_GCONFx, INV, gpio & 7, inv);
  67. REGSETIM_IDX(confval, BBU_GCONFx, FUNC, gpio & 7, func);
  68. __raw_writel(confval, conf);
  69. return 0;
  70. }
  71. int ns9360_gpio_configure(unsigned gpio, int inv, int func)
  72. {
  73. if (likely(ns9360_valid_gpio(gpio))) {
  74. if (func == 3) {
  75. printk(KERN_WARNING "use gpio_direction_input "
  76. "or gpio_direction_output\n");
  77. return -EINVAL;
  78. } else
  79. return __ns9360_gpio_configure(gpio, 0, inv, func);
  80. } else
  81. return -EINVAL;
  82. }
  83. EXPORT_SYMBOL(ns9360_gpio_configure);
  84. int ns9360_gpio_get_value(unsigned gpio)
  85. {
  86. void __iomem *stat = ns9360_gpio_get_gstataddr(gpio);
  87. int ret;
  88. ret = 1 & (__raw_readl(stat) >> (gpio & 31));
  89. return ret;
  90. }
  91. void ns9360_gpio_set_value(unsigned gpio, int value)
  92. {
  93. void __iomem *ctrl = ns9360_gpio_get_gctrladdr(gpio);
  94. u32 ctrlval;
  95. ctrlval = __raw_readl(ctrl);
  96. if (value)
  97. ctrlval |= 1 << (gpio & 31);
  98. else
  99. ctrlval &= ~(1 << (gpio & 31));
  100. __raw_writel(ctrlval, ctrl);
  101. }