intel_ich6_gpio.c 7.6 KB

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