intel_ich6_gpio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012 The Chromium OS Authors.
  4. */
  5. /*
  6. * This is a GPIO driver for Intel ICH6 and later. The x86 GPIOs are accessed
  7. * through the PCI bus. Each PCI device has 256 bytes of configuration space,
  8. * consisting of a standard header and a device-specific set of registers. PCI
  9. * bus 0, device 31, function 0 gives us access to the chipset GPIOs (among
  10. * other things). Within the PCI configuration space, the GPIOBASE register
  11. * tells us where in the device's I/O region we can find more registers to
  12. * actually access the GPIOs.
  13. *
  14. * PCI bus/device/function 0:1f:0 => PCI config registers
  15. * PCI config register "GPIOBASE"
  16. * PCI I/O space + [GPIOBASE] => start of GPIO registers
  17. * GPIO registers => gpio pin function, direction, value
  18. *
  19. *
  20. * Danger Will Robinson! Bank 0 (GPIOs 0-31) seems to be fairly stable. Most
  21. * ICH versions have more, but the decoding the matrix that describes them is
  22. * absurdly complex and constantly changing. We'll provide Bank 1 and Bank 2,
  23. * but they will ONLY work for certain unspecified chipsets because the offset
  24. * from GPIOBASE changes randomly. Even then, many GPIOs are unimplemented or
  25. * reserved or subject to arcane restrictions.
  26. */
  27. #define LOG_CATEGORY UCLASS_GPIO
  28. #include <common.h>
  29. #include <dm.h>
  30. #include <errno.h>
  31. #include <fdtdec.h>
  32. #include <log.h>
  33. #include <pch.h>
  34. #include <pci.h>
  35. #include <asm/cpu.h>
  36. #include <asm/global_data.h>
  37. #include <asm/gpio.h>
  38. #include <asm/io.h>
  39. #include <asm/pci.h>
  40. DECLARE_GLOBAL_DATA_PTR;
  41. #define GPIO_PER_BANK 32
  42. struct ich6_bank_priv {
  43. /* These are I/O addresses */
  44. uint16_t use_sel;
  45. uint16_t io_sel;
  46. uint16_t lvl;
  47. u32 lvl_write_cache;
  48. bool use_lvl_write_cache;
  49. };
  50. #define GPIO_USESEL_OFFSET(x) (x)
  51. #define GPIO_IOSEL_OFFSET(x) (x + 4)
  52. #define GPIO_LVL_OFFSET(x) (x + 8)
  53. static int _ich6_gpio_set_value(struct ich6_bank_priv *bank, unsigned offset,
  54. int value)
  55. {
  56. u32 val;
  57. if (bank->use_lvl_write_cache)
  58. val = bank->lvl_write_cache;
  59. else
  60. val = inl(bank->lvl);
  61. if (value)
  62. val |= (1UL << offset);
  63. else
  64. val &= ~(1UL << offset);
  65. outl(val, bank->lvl);
  66. if (bank->use_lvl_write_cache)
  67. bank->lvl_write_cache = val;
  68. return 0;
  69. }
  70. static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir)
  71. {
  72. u32 val;
  73. if (!dir) {
  74. val = inl(base);
  75. val |= (1UL << offset);
  76. outl(val, base);
  77. } else {
  78. val = inl(base);
  79. val &= ~(1UL << offset);
  80. outl(val, base);
  81. }
  82. return 0;
  83. }
  84. static int gpio_ich6_of_to_plat(struct udevice *dev)
  85. {
  86. struct ich6_bank_plat *plat = dev_get_plat(dev);
  87. u32 gpiobase;
  88. int offset;
  89. int ret;
  90. ret = pch_get_gpio_base(dev->parent, &gpiobase);
  91. if (ret)
  92. return ret;
  93. offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
  94. if (offset == -1) {
  95. debug("%s: Invalid register offset %d\n", __func__, offset);
  96. return -EINVAL;
  97. }
  98. plat->offset = offset;
  99. plat->base_addr = gpiobase + offset;
  100. plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  101. "bank-name", NULL);
  102. return 0;
  103. }
  104. static int ich6_gpio_probe(struct udevice *dev)
  105. {
  106. struct ich6_bank_plat *plat = dev_get_plat(dev);
  107. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  108. struct ich6_bank_priv *bank = dev_get_priv(dev);
  109. const void *prop;
  110. uc_priv->gpio_count = GPIO_PER_BANK;
  111. uc_priv->bank_name = plat->bank_name;
  112. bank->use_sel = plat->base_addr;
  113. bank->io_sel = plat->base_addr + 4;
  114. bank->lvl = plat->base_addr + 8;
  115. prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  116. "use-lvl-write-cache", NULL);
  117. if (prop)
  118. bank->use_lvl_write_cache = true;
  119. else
  120. bank->use_lvl_write_cache = false;
  121. bank->lvl_write_cache = 0;
  122. return 0;
  123. }
  124. static int ich6_gpio_request(struct udevice *dev, unsigned offset,
  125. const char *label)
  126. {
  127. struct ich6_bank_priv *bank = dev_get_priv(dev);
  128. u32 tmplong;
  129. /*
  130. * Make sure that the GPIO pin we want isn't already in use for some
  131. * built-in hardware function. We have to check this for every
  132. * requested pin.
  133. */
  134. tmplong = inl(bank->use_sel);
  135. if (!(tmplong & (1UL << offset))) {
  136. log_debug("gpio %d is reserved for internal use\n", offset);
  137. return -EPERM;
  138. }
  139. return 0;
  140. }
  141. static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
  142. {
  143. struct ich6_bank_priv *bank = dev_get_priv(dev);
  144. return _ich6_gpio_set_direction(bank->io_sel, offset, 0);
  145. }
  146. static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
  147. int value)
  148. {
  149. int ret;
  150. struct ich6_bank_priv *bank = dev_get_priv(dev);
  151. ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1);
  152. if (ret)
  153. return ret;
  154. return _ich6_gpio_set_value(bank, offset, value);
  155. }
  156. static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
  157. {
  158. struct ich6_bank_priv *bank = dev_get_priv(dev);
  159. u32 tmplong;
  160. int r;
  161. tmplong = inl(bank->lvl);
  162. if (bank->use_lvl_write_cache)
  163. tmplong |= bank->lvl_write_cache;
  164. r = (tmplong & (1UL << offset)) ? 1 : 0;
  165. return r;
  166. }
  167. static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
  168. int value)
  169. {
  170. struct ich6_bank_priv *bank = dev_get_priv(dev);
  171. return _ich6_gpio_set_value(bank, offset, value);
  172. }
  173. static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
  174. {
  175. struct ich6_bank_priv *bank = dev_get_priv(dev);
  176. u32 mask = 1UL << offset;
  177. if (!(inl(bank->use_sel) & mask))
  178. return GPIOF_FUNC;
  179. if (inl(bank->io_sel) & mask)
  180. return GPIOF_INPUT;
  181. else
  182. return GPIOF_OUTPUT;
  183. }
  184. static const struct dm_gpio_ops gpio_ich6_ops = {
  185. .request = ich6_gpio_request,
  186. .direction_input = ich6_gpio_direction_input,
  187. .direction_output = ich6_gpio_direction_output,
  188. .get_value = ich6_gpio_get_value,
  189. .set_value = ich6_gpio_set_value,
  190. .get_function = ich6_gpio_get_function,
  191. };
  192. static const struct udevice_id intel_ich6_gpio_ids[] = {
  193. { .compatible = "intel,ich6-gpio" },
  194. { }
  195. };
  196. U_BOOT_DRIVER(gpio_ich6) = {
  197. .name = "gpio_ich6",
  198. .id = UCLASS_GPIO,
  199. .of_match = intel_ich6_gpio_ids,
  200. .ops = &gpio_ich6_ops,
  201. .of_to_plat = gpio_ich6_of_to_plat,
  202. .probe = ich6_gpio_probe,
  203. .priv_auto = sizeof(struct ich6_bank_priv),
  204. .plat_auto = sizeof(struct ich6_bank_plat),
  205. };