sandbox.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <fdtdec.h>
  8. #include <malloc.h>
  9. #include <asm/gpio.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. /* Flags for each GPIO */
  12. #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
  13. #define GPIOF_HIGH (1 << 1) /* Currently set high */
  14. #define GPIOF_RESERVED (1 << 2) /* Is in use / requested */
  15. struct gpio_state {
  16. const char *label; /* label given by requester */
  17. u8 flags; /* flags (GPIOF_...) */
  18. };
  19. /* Access routines for GPIO state */
  20. static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
  21. {
  22. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  23. struct gpio_state *state = dev_get_priv(dev);
  24. if (offset >= uc_priv->gpio_count) {
  25. static u8 invalid_flags;
  26. printf("sandbox_gpio: error: invalid gpio %u\n", offset);
  27. return &invalid_flags;
  28. }
  29. return &state[offset].flags;
  30. }
  31. static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
  32. {
  33. return (*get_gpio_flags(dev, offset) & flag) != 0;
  34. }
  35. static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
  36. int value)
  37. {
  38. u8 *gpio = get_gpio_flags(dev, offset);
  39. if (value)
  40. *gpio |= flag;
  41. else
  42. *gpio &= ~flag;
  43. return 0;
  44. }
  45. static int check_reserved(struct udevice *dev, unsigned offset,
  46. const char *func)
  47. {
  48. if (!get_gpio_flag(dev, offset, GPIOF_RESERVED)) {
  49. printf("sandbox_gpio: %s: error: offset %u not reserved\n",
  50. func, offset);
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. /*
  56. * Back-channel sandbox-internal-only access to GPIO state
  57. */
  58. int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
  59. {
  60. if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
  61. debug("sandbox_gpio: get_value on output gpio %u\n", offset);
  62. return get_gpio_flag(dev, offset, GPIOF_HIGH);
  63. }
  64. int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  65. {
  66. return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
  67. }
  68. int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
  69. {
  70. return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
  71. }
  72. int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
  73. {
  74. return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
  75. }
  76. /*
  77. * These functions implement the public interface within U-Boot
  78. */
  79. /* set GPIO port 'offset' as an input */
  80. static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
  81. {
  82. debug("%s: offset:%u\n", __func__, offset);
  83. if (check_reserved(dev, offset, __func__))
  84. return -1;
  85. return sandbox_gpio_set_direction(dev, offset, 0);
  86. }
  87. /* set GPIO port 'offset' as an output, with polarity 'value' */
  88. static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
  89. int value)
  90. {
  91. debug("%s: offset:%u, value = %d\n", __func__, offset, value);
  92. if (check_reserved(dev, offset, __func__))
  93. return -1;
  94. return sandbox_gpio_set_direction(dev, offset, 1) |
  95. sandbox_gpio_set_value(dev, offset, value);
  96. }
  97. /* read GPIO IN value of port 'offset' */
  98. static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
  99. {
  100. debug("%s: offset:%u\n", __func__, offset);
  101. if (check_reserved(dev, offset, __func__))
  102. return -1;
  103. return sandbox_gpio_get_value(dev, offset);
  104. }
  105. /* write GPIO OUT value to port 'offset' */
  106. static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
  107. {
  108. debug("%s: offset:%u, value = %d\n", __func__, offset, value);
  109. if (check_reserved(dev, offset, __func__))
  110. return -1;
  111. if (!sandbox_gpio_get_direction(dev, offset)) {
  112. printf("sandbox_gpio: error: set_value on input gpio %u\n",
  113. offset);
  114. return -1;
  115. }
  116. return sandbox_gpio_set_value(dev, offset, value);
  117. }
  118. static int sb_gpio_request(struct udevice *dev, unsigned offset,
  119. const char *label)
  120. {
  121. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  122. struct gpio_state *state = dev_get_priv(dev);
  123. debug("%s: offset:%u, label:%s\n", __func__, offset, label);
  124. if (offset >= uc_priv->gpio_count) {
  125. printf("sandbox_gpio: error: invalid gpio %u\n", offset);
  126. return -1;
  127. }
  128. if (get_gpio_flag(dev, offset, GPIOF_RESERVED)) {
  129. printf("sandbox_gpio: error: gpio %u already reserved\n",
  130. offset);
  131. return -1;
  132. }
  133. state[offset].label = label;
  134. return set_gpio_flag(dev, offset, GPIOF_RESERVED, 1);
  135. }
  136. static int sb_gpio_free(struct udevice *dev, unsigned offset)
  137. {
  138. struct gpio_state *state = dev_get_priv(dev);
  139. debug("%s: offset:%u\n", __func__, offset);
  140. if (check_reserved(dev, offset, __func__))
  141. return -1;
  142. state[offset].label = NULL;
  143. return set_gpio_flag(dev, offset, GPIOF_RESERVED, 0);
  144. }
  145. static int sb_gpio_get_state(struct udevice *dev, unsigned int offset,
  146. char *buf, int bufsize)
  147. {
  148. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  149. struct gpio_state *state = dev_get_priv(dev);
  150. const char *label;
  151. label = state[offset].label;
  152. snprintf(buf, bufsize, "%s%d: %s: %d [%c]%s%s",
  153. uc_priv->bank_name ? uc_priv->bank_name : "", offset,
  154. sandbox_gpio_get_direction(dev, offset) ? "out" : " in",
  155. sandbox_gpio_get_value(dev, offset),
  156. get_gpio_flag(dev, offset, GPIOF_RESERVED) ? 'x' : ' ',
  157. label ? " " : "",
  158. label ? label : "");
  159. return 0;
  160. }
  161. static const struct dm_gpio_ops gpio_sandbox_ops = {
  162. .request = sb_gpio_request,
  163. .free = sb_gpio_free,
  164. .direction_input = sb_gpio_direction_input,
  165. .direction_output = sb_gpio_direction_output,
  166. .get_value = sb_gpio_get_value,
  167. .set_value = sb_gpio_set_value,
  168. .get_state = sb_gpio_get_state,
  169. };
  170. static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
  171. {
  172. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  173. uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  174. "num-gpios", 0);
  175. uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
  176. "gpio-bank-name", NULL);
  177. return 0;
  178. }
  179. static int gpio_sandbox_probe(struct udevice *dev)
  180. {
  181. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  182. if (dev->of_offset == -1) {
  183. /* Tell the uclass how many GPIOs we have */
  184. uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
  185. }
  186. dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
  187. return 0;
  188. }
  189. static const struct udevice_id sandbox_gpio_ids[] = {
  190. { .compatible = "sandbox,gpio" },
  191. { }
  192. };
  193. U_BOOT_DRIVER(gpio_sandbox) = {
  194. .name = "gpio_sandbox",
  195. .id = UCLASS_GPIO,
  196. .of_match = sandbox_gpio_ids,
  197. .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
  198. .probe = gpio_sandbox_probe,
  199. .ops = &gpio_sandbox_ops,
  200. };