intel_ich6_gpio.c 5.5 KB

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