intel_ich6_gpio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  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. #include <common.h>
  28. #include <dm.h>
  29. #include <errno.h>
  30. #include <fdtdec.h>
  31. #include <pch.h>
  32. #include <pci.h>
  33. #include <asm/cpu.h>
  34. #include <asm/gpio.h>
  35. #include <asm/io.h>
  36. #include <asm/pci.h>
  37. DECLARE_GLOBAL_DATA_PTR;
  38. #define GPIO_PER_BANK 32
  39. struct ich6_bank_priv {
  40. /* These are I/O addresses */
  41. uint16_t use_sel;
  42. uint16_t io_sel;
  43. uint16_t lvl;
  44. u32 lvl_write_cache;
  45. bool use_lvl_write_cache;
  46. };
  47. #define GPIO_USESEL_OFFSET(x) (x)
  48. #define GPIO_IOSEL_OFFSET(x) (x + 4)
  49. #define GPIO_LVL_OFFSET(x) (x + 8)
  50. static int _ich6_gpio_set_value(struct ich6_bank_priv *bank, unsigned offset,
  51. int value)
  52. {
  53. u32 val;
  54. if (bank->use_lvl_write_cache)
  55. val = bank->lvl_write_cache;
  56. else
  57. val = inl(bank->lvl);
  58. if (value)
  59. val |= (1UL << offset);
  60. else
  61. val &= ~(1UL << offset);
  62. outl(val, bank->lvl);
  63. if (bank->use_lvl_write_cache)
  64. bank->lvl_write_cache = val;
  65. return 0;
  66. }
  67. static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir)
  68. {
  69. u32 val;
  70. if (!dir) {
  71. val = inl(base);
  72. val |= (1UL << offset);
  73. outl(val, base);
  74. } else {
  75. val = inl(base);
  76. val &= ~(1UL << offset);
  77. outl(val, base);
  78. }
  79. return 0;
  80. }
  81. static int gpio_ich6_ofdata_to_platdata(struct udevice *dev)
  82. {
  83. struct ich6_bank_platdata *plat = dev_get_platdata(dev);
  84. u32 gpiobase;
  85. int offset;
  86. int ret;
  87. ret = pch_get_gpio_base(dev->parent, &gpiobase);
  88. if (ret)
  89. return ret;
  90. offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
  91. if (offset == -1) {
  92. debug("%s: Invalid register offset %d\n", __func__, offset);
  93. return -EINVAL;
  94. }
  95. plat->offset = offset;
  96. plat->base_addr = gpiobase + offset;
  97. plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  98. "bank-name", NULL);
  99. return 0;
  100. }
  101. static int ich6_gpio_probe(struct udevice *dev)
  102. {
  103. struct ich6_bank_platdata *plat = dev_get_platdata(dev);
  104. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  105. struct ich6_bank_priv *bank = dev_get_priv(dev);
  106. const void *prop;
  107. uc_priv->gpio_count = GPIO_PER_BANK;
  108. uc_priv->bank_name = plat->bank_name;
  109. bank->use_sel = plat->base_addr;
  110. bank->io_sel = plat->base_addr + 4;
  111. bank->lvl = plat->base_addr + 8;
  112. prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  113. "use-lvl-write-cache", NULL);
  114. if (prop)
  115. bank->use_lvl_write_cache = true;
  116. else
  117. bank->use_lvl_write_cache = false;
  118. bank->lvl_write_cache = 0;
  119. return 0;
  120. }
  121. static int ich6_gpio_request(struct udevice *dev, unsigned offset,
  122. const char *label)
  123. {
  124. struct ich6_bank_priv *bank = dev_get_priv(dev);
  125. u32 tmplong;
  126. /*
  127. * Make sure that the GPIO pin we want isn't already in use for some
  128. * built-in hardware function. We have to check this for every
  129. * requested pin.
  130. */
  131. tmplong = inl(bank->use_sel);
  132. if (!(tmplong & (1UL << offset))) {
  133. debug("%s: gpio %d is reserved for internal use\n", __func__,
  134. offset);
  135. return -EPERM;
  136. }
  137. return 0;
  138. }
  139. static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
  140. {
  141. struct ich6_bank_priv *bank = dev_get_priv(dev);
  142. return _ich6_gpio_set_direction(bank->io_sel, offset, 0);
  143. }
  144. static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
  145. int value)
  146. {
  147. int ret;
  148. struct ich6_bank_priv *bank = dev_get_priv(dev);
  149. ret = _ich6_gpio_set_direction(bank->io_sel, offset, 1);
  150. if (ret)
  151. return ret;
  152. return _ich6_gpio_set_value(bank, offset, value);
  153. }
  154. static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
  155. {
  156. struct ich6_bank_priv *bank = dev_get_priv(dev);
  157. u32 tmplong;
  158. int r;
  159. tmplong = inl(bank->lvl);
  160. if (bank->use_lvl_write_cache)
  161. tmplong |= bank->lvl_write_cache;
  162. r = (tmplong & (1UL << offset)) ? 1 : 0;
  163. return r;
  164. }
  165. static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
  166. int value)
  167. {
  168. struct ich6_bank_priv *bank = dev_get_priv(dev);
  169. return _ich6_gpio_set_value(bank, offset, value);
  170. }
  171. static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
  172. {
  173. struct ich6_bank_priv *bank = dev_get_priv(dev);
  174. u32 mask = 1UL << offset;
  175. if (!(inl(bank->use_sel) & mask))
  176. return GPIOF_FUNC;
  177. if (inl(bank->io_sel) & mask)
  178. return GPIOF_INPUT;
  179. else
  180. return GPIOF_OUTPUT;
  181. }
  182. static const struct dm_gpio_ops gpio_ich6_ops = {
  183. .request = ich6_gpio_request,
  184. .direction_input = ich6_gpio_direction_input,
  185. .direction_output = ich6_gpio_direction_output,
  186. .get_value = ich6_gpio_get_value,
  187. .set_value = ich6_gpio_set_value,
  188. .get_function = ich6_gpio_get_function,
  189. };
  190. static const struct udevice_id intel_ich6_gpio_ids[] = {
  191. { .compatible = "intel,ich6-gpio" },
  192. { }
  193. };
  194. U_BOOT_DRIVER(gpio_ich6) = {
  195. .name = "gpio_ich6",
  196. .id = UCLASS_GPIO,
  197. .of_match = intel_ich6_gpio_ids,
  198. .ops = &gpio_ich6_ops,
  199. .ofdata_to_platdata = gpio_ich6_ofdata_to_platdata,
  200. .probe = ich6_gpio_probe,
  201. .priv_auto_alloc_size = sizeof(struct ich6_bank_priv),
  202. .platdata_auto_alloc_size = sizeof(struct ich6_bank_platdata),
  203. };