zynq_gpio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Xilinx Zynq GPIO device driver
  3. *
  4. * Copyright (C) 2015 DAVE Embedded Systems <devel@dave.eu>
  5. *
  6. * Most of code taken from linux kernel driver (linux/drivers/gpio/gpio-zynq.c)
  7. * Copyright (C) 2009 - 2014 Xilinx, Inc.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <asm/gpio.h>
  13. #include <asm/io.h>
  14. #include <asm/errno.h>
  15. /**
  16. * zynq_gpio_get_bank_pin - Get the bank number and pin number within that bank
  17. * for a given pin in the GPIO device
  18. * @pin_num: gpio pin number within the device
  19. * @bank_num: an output parameter used to return the bank number of the gpio
  20. * pin
  21. * @bank_pin_num: an output parameter used to return pin number within a bank
  22. * for the given gpio pin
  23. *
  24. * Returns the bank number and pin offset within the bank.
  25. */
  26. static inline void zynq_gpio_get_bank_pin(unsigned int pin_num,
  27. unsigned int *bank_num,
  28. unsigned int *bank_pin_num)
  29. {
  30. switch (pin_num) {
  31. case ZYNQ_GPIO_BANK0_PIN_MIN ... ZYNQ_GPIO_BANK0_PIN_MAX:
  32. *bank_num = 0;
  33. *bank_pin_num = pin_num;
  34. break;
  35. case ZYNQ_GPIO_BANK1_PIN_MIN ... ZYNQ_GPIO_BANK1_PIN_MAX:
  36. *bank_num = 1;
  37. *bank_pin_num = pin_num - ZYNQ_GPIO_BANK1_PIN_MIN;
  38. break;
  39. case ZYNQ_GPIO_BANK2_PIN_MIN ... ZYNQ_GPIO_BANK2_PIN_MAX:
  40. *bank_num = 2;
  41. *bank_pin_num = pin_num - ZYNQ_GPIO_BANK2_PIN_MIN;
  42. break;
  43. case ZYNQ_GPIO_BANK3_PIN_MIN ... ZYNQ_GPIO_BANK3_PIN_MAX:
  44. *bank_num = 3;
  45. *bank_pin_num = pin_num - ZYNQ_GPIO_BANK3_PIN_MIN;
  46. break;
  47. default:
  48. printf("invalid GPIO pin number: %u\n", pin_num);
  49. *bank_num = 0;
  50. *bank_pin_num = 0;
  51. break;
  52. }
  53. }
  54. int gpio_is_valid(unsigned gpio)
  55. {
  56. return (gpio >= 0) && (gpio < ZYNQ_GPIO_NR_GPIOS);
  57. }
  58. static int check_gpio(unsigned gpio)
  59. {
  60. if (!gpio_is_valid(gpio)) {
  61. printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. /**
  67. * gpio_get_value - Get the state of the specified pin of GPIO device
  68. * @gpio: gpio pin number within the device
  69. *
  70. * This function reads the state of the specified pin of the GPIO device.
  71. *
  72. * Return: 0 if the pin is low, 1 if pin is high.
  73. */
  74. int gpio_get_value(unsigned gpio)
  75. {
  76. u32 data;
  77. unsigned int bank_num, bank_pin_num;
  78. if (check_gpio(gpio) < 0)
  79. return -1;
  80. zynq_gpio_get_bank_pin(gpio, &bank_num, &bank_pin_num);
  81. data = readl(ZYNQ_GPIO_BASE_ADDRESS +
  82. ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
  83. return (data >> bank_pin_num) & 1;
  84. }
  85. /**
  86. * gpio_set_value - Modify the value of the pin with specified value
  87. * @gpio: gpio pin number within the device
  88. * @value: value used to modify the value of the specified pin
  89. *
  90. * This function calculates the register offset (i.e to lower 16 bits or
  91. * upper 16 bits) based on the given pin number and sets the value of a
  92. * gpio pin to the specified value. The value is either 0 or non-zero.
  93. */
  94. int gpio_set_value(unsigned gpio, int value)
  95. {
  96. unsigned int reg_offset, bank_num, bank_pin_num;
  97. if (check_gpio(gpio) < 0)
  98. return -1;
  99. zynq_gpio_get_bank_pin(gpio, &bank_num, &bank_pin_num);
  100. if (bank_pin_num >= ZYNQ_GPIO_MID_PIN_NUM) {
  101. /* only 16 data bits in bit maskable reg */
  102. bank_pin_num -= ZYNQ_GPIO_MID_PIN_NUM;
  103. reg_offset = ZYNQ_GPIO_DATA_MSW_OFFSET(bank_num);
  104. } else {
  105. reg_offset = ZYNQ_GPIO_DATA_LSW_OFFSET(bank_num);
  106. }
  107. /*
  108. * get the 32 bit value to be written to the mask/data register where
  109. * the upper 16 bits is the mask and lower 16 bits is the data
  110. */
  111. value = !!value;
  112. value = ~(1 << (bank_pin_num + ZYNQ_GPIO_MID_PIN_NUM)) &
  113. ((value << bank_pin_num) | ZYNQ_GPIO_UPPER_MASK);
  114. writel(value, ZYNQ_GPIO_BASE_ADDRESS + reg_offset);
  115. return 0;
  116. }
  117. /**
  118. * gpio_direction_input - Set the direction of the specified GPIO pin as input
  119. * @gpio: gpio pin number within the device
  120. *
  121. * This function uses the read-modify-write sequence to set the direction of
  122. * the gpio pin as input.
  123. *
  124. * Return: -1 if invalid gpio specified, 0 if successul
  125. */
  126. int gpio_direction_input(unsigned gpio)
  127. {
  128. u32 reg;
  129. unsigned int bank_num, bank_pin_num;
  130. if (check_gpio(gpio) < 0)
  131. return -1;
  132. zynq_gpio_get_bank_pin(gpio, &bank_num, &bank_pin_num);
  133. /* bank 0 pins 7 and 8 are special and cannot be used as inputs */
  134. if (bank_num == 0 && (bank_pin_num == 7 || bank_pin_num == 8))
  135. return -1;
  136. /* clear the bit in direction mode reg to set the pin as input */
  137. reg = readl(ZYNQ_GPIO_BASE_ADDRESS + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
  138. reg &= ~BIT(bank_pin_num);
  139. writel(reg, ZYNQ_GPIO_BASE_ADDRESS + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
  140. return 0;
  141. }
  142. /**
  143. * gpio_direction_output - Set the direction of the specified GPIO pin as output
  144. * @gpio: gpio pin number within the device
  145. * @value: value to be written to specified pin
  146. *
  147. * This function sets the direction of specified GPIO pin as output, configures
  148. * the Output Enable register for the pin and uses zynq_gpio_set to set
  149. * the value of the pin to the value specified.
  150. *
  151. * Return: 0 always
  152. */
  153. int gpio_direction_output(unsigned gpio, int value)
  154. {
  155. u32 reg;
  156. unsigned int bank_num, bank_pin_num;
  157. if (check_gpio(gpio) < 0)
  158. return -1;
  159. zynq_gpio_get_bank_pin(gpio, &bank_num, &bank_pin_num);
  160. /* set the GPIO pin as output */
  161. reg = readl(ZYNQ_GPIO_BASE_ADDRESS + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
  162. reg |= BIT(bank_pin_num);
  163. writel(reg, ZYNQ_GPIO_BASE_ADDRESS + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
  164. /* configure the output enable reg for the pin */
  165. reg = readl(ZYNQ_GPIO_BASE_ADDRESS + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
  166. reg |= BIT(bank_pin_num);
  167. writel(reg, ZYNQ_GPIO_BASE_ADDRESS + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
  168. /* set the state of the pin */
  169. gpio_set_value(gpio, value);
  170. return 0;
  171. }
  172. /**
  173. * Request a gpio before using it.
  174. *
  175. * NOTE: Argument 'label' is unused.
  176. */
  177. int gpio_request(unsigned gpio, const char *label)
  178. {
  179. if (check_gpio(gpio) < 0)
  180. return -1;
  181. return 0;
  182. }
  183. /**
  184. * Reset and free the gpio after using it.
  185. */
  186. int gpio_free(unsigned gpio)
  187. {
  188. return 0;
  189. }