mpc85xx_gpio.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright 2010 eXMeritus, A Boeing Company
  4. */
  5. #ifndef POWERPC_ASM_MPC85XX_GPIO_H_
  6. #define POWERPC_ASM_MPC85XX_GPIO_H_
  7. # include <asm/immap_85xx.h>
  8. /*
  9. * The following internal functions are an MPC85XX-specific GPIO API which
  10. * allows setting and querying multiple GPIOs in a single operation.
  11. *
  12. * All of these look relatively large, but the arguments are almost always
  13. * constants, so they compile down to just a few instructions and a
  14. * memory-mapped IO operation or two.
  15. */
  16. static inline void mpc85xx_gpio_set(unsigned int mask,
  17. unsigned int dir, unsigned int val)
  18. {
  19. ccsr_gpio_t *gpio = (void *)(CONFIG_SYS_MPC85xx_GPIO_ADDR);
  20. /* First mask off the unwanted parts of "dir" and "val" */
  21. dir &= mask;
  22. val &= mask;
  23. /* Now read in the values we're supposed to preserve */
  24. dir |= (in_be32(&gpio->gpdir) & ~mask);
  25. val |= (in_be32(&gpio->gpdat) & ~mask);
  26. /*
  27. * Poke the new output values first, then set the direction. This
  28. * helps to avoid transient data when switching from input to output
  29. * and vice versa.
  30. */
  31. out_be32(&gpio->gpdat, val);
  32. out_be32(&gpio->gpdir, dir);
  33. }
  34. static inline void mpc85xx_gpio_set_in(unsigned int gpios)
  35. {
  36. mpc85xx_gpio_set(gpios, 0x00000000, 0x00000000);
  37. }
  38. static inline void mpc85xx_gpio_set_low(unsigned int gpios)
  39. {
  40. mpc85xx_gpio_set(gpios, 0xFFFFFFFF, 0x00000000);
  41. }
  42. static inline void mpc85xx_gpio_set_high(unsigned int gpios)
  43. {
  44. mpc85xx_gpio_set(gpios, 0xFFFFFFFF, 0xFFFFFFFF);
  45. }
  46. static inline unsigned int mpc85xx_gpio_get(unsigned int mask)
  47. {
  48. ccsr_gpio_t *gpio = (void *)(CONFIG_SYS_MPC85xx_GPIO_ADDR);
  49. /* Read the requested values */
  50. return in_be32(&gpio->gpdat) & mask;
  51. }
  52. /*
  53. * These implement the generic Linux GPIO API on top of the other functions
  54. * in this header.
  55. */
  56. static inline int gpio_request(unsigned gpio, const char *label)
  57. {
  58. /* Compatibility shim */
  59. return 0;
  60. }
  61. static inline int gpio_free(unsigned gpio)
  62. {
  63. /* Compatibility shim */
  64. return 0;
  65. }
  66. static inline int gpio_direction_input(unsigned gpio)
  67. {
  68. mpc85xx_gpio_set_in(1U << gpio);
  69. return 0;
  70. }
  71. static inline int gpio_direction_output(unsigned gpio, int value)
  72. {
  73. if (value)
  74. mpc85xx_gpio_set_high(1U << gpio);
  75. else
  76. mpc85xx_gpio_set_low(1U << gpio);
  77. return 0;
  78. }
  79. static inline int gpio_get_value(unsigned gpio)
  80. {
  81. return !!mpc85xx_gpio_get(1U << gpio);
  82. }
  83. static inline int gpio_set_value(unsigned gpio, int value)
  84. {
  85. if (value)
  86. mpc85xx_gpio_set_high(1U << gpio);
  87. else
  88. mpc85xx_gpio_set_low(1U << gpio);
  89. return 0;
  90. }
  91. static inline int gpio_is_valid(int gpio)
  92. {
  93. return (gpio >= 0) && (gpio < 32);
  94. }
  95. #endif /* not POWERPC_ASM_MPC85XX_GPIO_H_ */