intel_ich6_gpio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <pci.h>
  32. #include <asm/gpio.h>
  33. #include <asm/io.h>
  34. #define GPIO_PER_BANK 32
  35. /* Where in config space is the register that points to the GPIO registers? */
  36. #define PCI_CFG_GPIOBASE 0x48
  37. struct ich6_bank_priv {
  38. /* These are I/O addresses */
  39. uint32_t use_sel;
  40. uint32_t io_sel;
  41. uint32_t lvl;
  42. };
  43. static int gpio_ich6_ofdata_to_platdata(struct udevice *dev)
  44. {
  45. struct ich6_bank_platdata *plat = dev_get_platdata(dev);
  46. pci_dev_t pci_dev; /* handle for 0:1f:0 */
  47. u8 tmpbyte;
  48. u16 tmpword;
  49. u32 tmplong;
  50. u32 gpiobase;
  51. int offset;
  52. /* Where should it be? */
  53. pci_dev = PCI_BDF(0, 0x1f, 0);
  54. /* Is the device present? */
  55. pci_read_config_word(pci_dev, PCI_VENDOR_ID, &tmpword);
  56. if (tmpword != PCI_VENDOR_ID_INTEL) {
  57. debug("%s: wrong VendorID\n", __func__);
  58. return -ENODEV;
  59. }
  60. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &tmpword);
  61. debug("Found %04x:%04x\n", PCI_VENDOR_ID_INTEL, tmpword);
  62. /*
  63. * We'd like to validate the Device ID too, but pretty much any
  64. * value is either a) correct with slight differences, or b)
  65. * correct but undocumented. We'll have to check a bunch of other
  66. * things instead...
  67. */
  68. /* I/O should already be enabled (it's a RO bit). */
  69. pci_read_config_word(pci_dev, PCI_COMMAND, &tmpword);
  70. if (!(tmpword & PCI_COMMAND_IO)) {
  71. debug("%s: device IO not enabled\n", __func__);
  72. return -ENODEV;
  73. }
  74. /* Header Type must be normal (bits 6-0 only; see spec.) */
  75. pci_read_config_byte(pci_dev, PCI_HEADER_TYPE, &tmpbyte);
  76. if ((tmpbyte & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
  77. debug("%s: invalid Header type\n", __func__);
  78. return -ENODEV;
  79. }
  80. /* Base Class must be a bridge device */
  81. pci_read_config_byte(pci_dev, PCI_CLASS_CODE, &tmpbyte);
  82. if (tmpbyte != PCI_CLASS_CODE_BRIDGE) {
  83. debug("%s: invalid class\n", __func__);
  84. return -ENODEV;
  85. }
  86. /* Sub Class must be ISA */
  87. pci_read_config_byte(pci_dev, PCI_CLASS_SUB_CODE, &tmpbyte);
  88. if (tmpbyte != PCI_CLASS_SUB_CODE_BRIDGE_ISA) {
  89. debug("%s: invalid subclass\n", __func__);
  90. return -ENODEV;
  91. }
  92. /* Programming Interface must be 0x00 (no others exist) */
  93. pci_read_config_byte(pci_dev, PCI_CLASS_PROG, &tmpbyte);
  94. if (tmpbyte != 0x00) {
  95. debug("%s: invalid interface type\n", __func__);
  96. return -ENODEV;
  97. }
  98. /*
  99. * GPIOBASE moved to its current offset with ICH6, but prior to
  100. * that it was unused (or undocumented). Check that it looks
  101. * okay: not all ones or zeros, and mapped to I/O space (bit 0).
  102. */
  103. pci_read_config_dword(pci_dev, PCI_CFG_GPIOBASE, &tmplong);
  104. if (tmplong == 0x00000000 || tmplong == 0xffffffff ||
  105. !(tmplong & 0x00000001)) {
  106. debug("%s: unexpected GPIOBASE value\n", __func__);
  107. return -ENODEV;
  108. }
  109. /*
  110. * Okay, I guess we're looking at the right device. The actual
  111. * GPIO registers are in the PCI device's I/O space, starting
  112. * at the offset that we just read. Bit 0 indicates that it's
  113. * an I/O address, not a memory address, so mask that off.
  114. */
  115. gpiobase = tmplong & 0xfffffffe;
  116. offset = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1);
  117. if (offset == -1) {
  118. debug("%s: Invalid register offset %d\n", __func__, offset);
  119. return -EINVAL;
  120. }
  121. plat->base_addr = gpiobase + offset;
  122. plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
  123. "bank-name", NULL);
  124. return 0;
  125. }
  126. int ich6_gpio_probe(struct udevice *dev)
  127. {
  128. struct ich6_bank_platdata *plat = dev_get_platdata(dev);
  129. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  130. struct ich6_bank_priv *bank = dev_get_priv(dev);
  131. uc_priv->gpio_count = GPIO_PER_BANK;
  132. uc_priv->bank_name = plat->bank_name;
  133. bank->use_sel = plat->base_addr;
  134. bank->io_sel = plat->base_addr + 4;
  135. bank->lvl = plat->base_addr + 8;
  136. return 0;
  137. }
  138. int ich6_gpio_request(struct udevice *dev, unsigned offset, const char *label)
  139. {
  140. struct ich6_bank_priv *bank = dev_get_priv(dev);
  141. u32 tmplong;
  142. /*
  143. * Make sure that the GPIO pin we want isn't already in use for some
  144. * built-in hardware function. We have to check this for every
  145. * requested pin.
  146. */
  147. tmplong = inl(bank->use_sel);
  148. if (!(tmplong & (1UL << offset))) {
  149. debug("%s: gpio %d is reserved for internal use\n", __func__,
  150. offset);
  151. return -EPERM;
  152. }
  153. return 0;
  154. }
  155. static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
  156. {
  157. struct ich6_bank_priv *bank = dev_get_priv(dev);
  158. u32 tmplong;
  159. tmplong = inl(bank->io_sel);
  160. tmplong |= (1UL << offset);
  161. outl(bank->io_sel, tmplong);
  162. return 0;
  163. }
  164. static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
  165. int value)
  166. {
  167. struct ich6_bank_priv *bank = dev_get_priv(dev);
  168. u32 tmplong;
  169. tmplong = inl(bank->io_sel);
  170. tmplong &= ~(1UL << offset);
  171. outl(bank->io_sel, tmplong);
  172. return 0;
  173. }
  174. static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
  175. {
  176. struct ich6_bank_priv *bank = dev_get_priv(dev);
  177. u32 tmplong;
  178. int r;
  179. tmplong = inl(bank->lvl);
  180. r = (tmplong & (1UL << offset)) ? 1 : 0;
  181. return r;
  182. }
  183. static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
  184. int value)
  185. {
  186. struct ich6_bank_priv *bank = dev_get_priv(dev);
  187. u32 tmplong;
  188. tmplong = inl(bank->lvl);
  189. if (value)
  190. tmplong |= (1UL << offset);
  191. else
  192. tmplong &= ~(1UL << offset);
  193. outl(bank->lvl, tmplong);
  194. return 0;
  195. }
  196. static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
  197. {
  198. struct ich6_bank_priv *bank = dev_get_priv(dev);
  199. u32 mask = 1UL << offset;
  200. if (!(inl(bank->use_sel) & mask))
  201. return GPIOF_FUNC;
  202. if (inl(bank->io_sel) & mask)
  203. return GPIOF_INPUT;
  204. else
  205. return GPIOF_OUTPUT;
  206. }
  207. static const struct dm_gpio_ops gpio_ich6_ops = {
  208. .request = ich6_gpio_request,
  209. .direction_input = ich6_gpio_direction_input,
  210. .direction_output = ich6_gpio_direction_output,
  211. .get_value = ich6_gpio_get_value,
  212. .set_value = ich6_gpio_set_value,
  213. .get_function = ich6_gpio_get_function,
  214. };
  215. static const struct udevice_id intel_ich6_gpio_ids[] = {
  216. { .compatible = "intel,ich6-gpio" },
  217. { }
  218. };
  219. U_BOOT_DRIVER(gpio_ich6) = {
  220. .name = "gpio_ich6",
  221. .id = UCLASS_GPIO,
  222. .of_match = intel_ich6_gpio_ids,
  223. .ops = &gpio_ich6_ops,
  224. .ofdata_to_platdata = gpio_ich6_ofdata_to_platdata,
  225. .probe = ich6_gpio_probe,
  226. .priv_auto_alloc_size = sizeof(struct ich6_bank_priv),
  227. .platdata_auto_alloc_size = sizeof(struct ich6_bank_platdata),
  228. };