intel_ich6_gpio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <pci.h>
  29. #include <asm/gpio.h>
  30. #include <asm/io.h>
  31. /* Where in config space is the register that points to the GPIO registers? */
  32. #define PCI_CFG_GPIOBASE 0x48
  33. #define NUM_BANKS 3
  34. /* Within the I/O space, where are the registers to control the GPIOs? */
  35. static struct {
  36. u8 use_sel;
  37. u8 io_sel;
  38. u8 lvl;
  39. } gpio_bank[NUM_BANKS] = {
  40. { 0x00, 0x04, 0x0c }, /* Bank 0 */
  41. { 0x30, 0x34, 0x38 }, /* Bank 1 */
  42. { 0x40, 0x44, 0x48 } /* Bank 2 */
  43. };
  44. static pci_dev_t dev; /* handle for 0:1f:0 */
  45. static u32 gpiobase; /* offset into I/O space */
  46. static int found_it_once; /* valid GPIO device? */
  47. static u32 lock[NUM_BANKS]; /* "lock" for access to pins */
  48. static int bad_arg(int num, int *bank, int *bitnum)
  49. {
  50. int i = num / 32;
  51. int j = num % 32;
  52. if (num < 0 || i > NUM_BANKS) {
  53. debug("%s: bogus gpio num: %d\n", __func__, num);
  54. return -1;
  55. }
  56. *bank = i;
  57. *bitnum = j;
  58. return 0;
  59. }
  60. static int mark_gpio(int bank, int bitnum)
  61. {
  62. if (lock[bank] & (1UL << bitnum)) {
  63. debug("%s: %d.%d already marked\n", __func__, bank, bitnum);
  64. return -1;
  65. }
  66. lock[bank] |= (1 << bitnum);
  67. return 0;
  68. }
  69. static void clear_gpio(int bank, int bitnum)
  70. {
  71. lock[bank] &= ~(1 << bitnum);
  72. }
  73. static int notmine(int num, int *bank, int *bitnum)
  74. {
  75. if (bad_arg(num, bank, bitnum))
  76. return -1;
  77. return !(lock[*bank] & (1UL << *bitnum));
  78. }
  79. static int gpio_init(void)
  80. {
  81. u8 tmpbyte;
  82. u16 tmpword;
  83. u32 tmplong;
  84. /* Have we already done this? */
  85. if (found_it_once)
  86. return 0;
  87. /* Where should it be? */
  88. dev = PCI_BDF(0, 0x1f, 0);
  89. /* Is the device present? */
  90. pci_read_config_word(dev, PCI_VENDOR_ID, &tmpword);
  91. if (tmpword != PCI_VENDOR_ID_INTEL) {
  92. debug("%s: wrong VendorID\n", __func__);
  93. return -1;
  94. }
  95. pci_read_config_word(dev, PCI_DEVICE_ID, &tmpword);
  96. debug("Found %04x:%04x\n", PCI_VENDOR_ID_INTEL, tmpword);
  97. /*
  98. * We'd like to validate the Device ID too, but pretty much any
  99. * value is either a) correct with slight differences, or b)
  100. * correct but undocumented. We'll have to check a bunch of other
  101. * things instead...
  102. */
  103. /* I/O should already be enabled (it's a RO bit). */
  104. pci_read_config_word(dev, PCI_COMMAND, &tmpword);
  105. if (!(tmpword & PCI_COMMAND_IO)) {
  106. debug("%s: device IO not enabled\n", __func__);
  107. return -1;
  108. }
  109. /* Header Type must be normal (bits 6-0 only; see spec.) */
  110. pci_read_config_byte(dev, PCI_HEADER_TYPE, &tmpbyte);
  111. if ((tmpbyte & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
  112. debug("%s: invalid Header type\n", __func__);
  113. return -1;
  114. }
  115. /* Base Class must be a bridge device */
  116. pci_read_config_byte(dev, PCI_CLASS_CODE, &tmpbyte);
  117. if (tmpbyte != PCI_CLASS_CODE_BRIDGE) {
  118. debug("%s: invalid class\n", __func__);
  119. return -1;
  120. }
  121. /* Sub Class must be ISA */
  122. pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &tmpbyte);
  123. if (tmpbyte != PCI_CLASS_SUB_CODE_BRIDGE_ISA) {
  124. debug("%s: invalid subclass\n", __func__);
  125. return -1;
  126. }
  127. /* Programming Interface must be 0x00 (no others exist) */
  128. pci_read_config_byte(dev, PCI_CLASS_PROG, &tmpbyte);
  129. if (tmpbyte != 0x00) {
  130. debug("%s: invalid interface type\n", __func__);
  131. return -1;
  132. }
  133. /*
  134. * GPIOBASE moved to its current offset with ICH6, but prior to
  135. * that it was unused (or undocumented). Check that it looks
  136. * okay: not all ones or zeros, and mapped to I/O space (bit 0).
  137. */
  138. pci_read_config_dword(dev, PCI_CFG_GPIOBASE, &tmplong);
  139. if (tmplong == 0x00000000 || tmplong == 0xffffffff ||
  140. !(tmplong & 0x00000001)) {
  141. debug("%s: unexpected GPIOBASE value\n", __func__);
  142. return -1;
  143. }
  144. /*
  145. * Okay, I guess we're looking at the right device. The actual
  146. * GPIO registers are in the PCI device's I/O space, starting
  147. * at the offset that we just read. Bit 0 indicates that it's
  148. * an I/O address, not a memory address, so mask that off.
  149. */
  150. gpiobase = tmplong & 0xfffffffe;
  151. /* Finally. These are the droids we're looking for. */
  152. found_it_once = 1;
  153. return 0;
  154. }
  155. int gpio_request(unsigned num, const char *label /* UNUSED */)
  156. {
  157. u32 tmplong;
  158. int i = 0, j = 0;
  159. /* Is the hardware ready? */
  160. if (gpio_init())
  161. return -1;
  162. if (bad_arg(num, &i, &j))
  163. return -1;
  164. /*
  165. * Make sure that the GPIO pin we want isn't already in use for some
  166. * built-in hardware function. We have to check this for every
  167. * requested pin.
  168. */
  169. tmplong = inl(gpiobase + gpio_bank[i].use_sel);
  170. if (!(tmplong & (1UL << j))) {
  171. debug("%s: gpio %d is reserved for internal use\n", __func__,
  172. num);
  173. return -1;
  174. }
  175. return mark_gpio(i, j);
  176. }
  177. int gpio_free(unsigned num)
  178. {
  179. int i = 0, j = 0;
  180. if (notmine(num, &i, &j))
  181. return -1;
  182. clear_gpio(i, j);
  183. return 0;
  184. }
  185. int gpio_direction_input(unsigned num)
  186. {
  187. u32 tmplong;
  188. int i = 0, j = 0;
  189. if (notmine(num, &i, &j))
  190. return -1;
  191. tmplong = inl(gpiobase + gpio_bank[i].io_sel);
  192. tmplong |= (1UL << j);
  193. outl(gpiobase + gpio_bank[i].io_sel, tmplong);
  194. return 0;
  195. }
  196. int gpio_direction_output(unsigned num, int value)
  197. {
  198. u32 tmplong;
  199. int i = 0, j = 0;
  200. if (notmine(num, &i, &j))
  201. return -1;
  202. tmplong = inl(gpiobase + gpio_bank[i].io_sel);
  203. tmplong &= ~(1UL << j);
  204. outl(gpiobase + gpio_bank[i].io_sel, tmplong);
  205. return 0;
  206. }
  207. int gpio_get_value(unsigned num)
  208. {
  209. u32 tmplong;
  210. int i = 0, j = 0;
  211. int r;
  212. if (notmine(num, &i, &j))
  213. return -1;
  214. tmplong = inl(gpiobase + gpio_bank[i].lvl);
  215. r = (tmplong & (1UL << j)) ? 1 : 0;
  216. return r;
  217. }
  218. int gpio_set_value(unsigned num, int value)
  219. {
  220. u32 tmplong;
  221. int i = 0, j = 0;
  222. if (notmine(num, &i, &j))
  223. return -1;
  224. tmplong = inl(gpiobase + gpio_bank[i].lvl);
  225. if (value)
  226. tmplong |= (1UL << j);
  227. else
  228. tmplong &= ~(1UL << j);
  229. outl(gpiobase + gpio_bank[i].lvl, tmplong);
  230. return 0;
  231. }