intel_ich6_gpio.c 5.6 KB

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