mpc85xx_gpio.h 2.6 KB

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