intel_ich6_gpio.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. /*
  22. * This is a GPIO driver for Intel ICH6 and later. The x86 GPIOs are accessed
  23. * through the PCI bus. Each PCI device has 256 bytes of configuration space,
  24. * consisting of a standard header and a device-specific set of registers. PCI
  25. * bus 0, device 31, function 0 gives us access to the chipset GPIOs (among
  26. * other things). Within the PCI configuration space, the GPIOBASE register
  27. * tells us where in the device's I/O region we can find more registers to
  28. * actually access the GPIOs.
  29. *
  30. * PCI bus/device/function 0:1f:0 => PCI config registers
  31. * PCI config register "GPIOBASE"
  32. * PCI I/O space + [GPIOBASE] => start of GPIO registers
  33. * GPIO registers => gpio pin function, direction, value
  34. *
  35. *
  36. * Danger Will Robinson! Bank 0 (GPIOs 0-31) seems to be fairly stable. Most
  37. * ICH versions have more, but the decoding the matrix that describes them is
  38. * absurdly complex and constantly changing. We'll provide Bank 1 and Bank 2,
  39. * but they will ONLY work for certain unspecified chipsets because the offset
  40. * from GPIOBASE changes randomly. Even then, many GPIOs are unimplemented or
  41. * reserved or subject to arcane restrictions.
  42. */
  43. #include <common.h>
  44. #include <pci.h>
  45. #include <asm/gpio.h>
  46. #include <asm/io.h>
  47. /* Where in config space is the register that points to the GPIO registers? */
  48. #define PCI_CFG_GPIOBASE 0x48
  49. #define NUM_BANKS 3
  50. /* Within the I/O space, where are the registers to control the GPIOs? */
  51. static struct {
  52. u8 use_sel;
  53. u8 io_sel;
  54. u8 lvl;
  55. } gpio_bank[NUM_BANKS] = {
  56. { 0x00, 0x04, 0x0c }, /* Bank 0 */
  57. { 0x30, 0x34, 0x38 }, /* Bank 1 */
  58. { 0x40, 0x44, 0x48 } /* Bank 2 */
  59. };
  60. static pci_dev_t dev; /* handle for 0:1f:0 */
  61. static u32 gpiobase; /* offset into I/O space */
  62. static int found_it_once; /* valid GPIO device? */
  63. static u32 lock[NUM_BANKS]; /* "lock" for access to pins */
  64. static int bad_arg(int num, int *bank, int *bitnum)
  65. {
  66. int i = num / 32;
  67. int j = num % 32;
  68. if (num < 0 || i > NUM_BANKS) {
  69. debug("%s: bogus gpio num: %d\n", __func__, num);
  70. return -1;
  71. }
  72. *bank = i;
  73. *bitnum = j;
  74. return 0;
  75. }
  76. static int mark_gpio(int bank, int bitnum)
  77. {
  78. if (lock[bank] & (1UL << bitnum)) {
  79. debug("%s: %d.%d already marked\n", __func__, bank, bitnum);
  80. return -1;
  81. }
  82. lock[bank] |= (1 << bitnum);
  83. return 0;
  84. }
  85. static void clear_gpio(int bank, int bitnum)
  86. {
  87. lock[bank] &= ~(1 << bitnum);
  88. }
  89. static int notmine(int num, int *bank, int *bitnum)
  90. {
  91. if (bad_arg(num, bank, bitnum))
  92. return -1;
  93. return !(lock[*bank] & (1UL << *bitnum));
  94. }
  95. static int gpio_init(void)
  96. {
  97. u8 tmpbyte;
  98. u16 tmpword;
  99. u32 tmplong;
  100. /* Have we already done this? */
  101. if (found_it_once)
  102. return 0;
  103. /* Where should it be? */
  104. dev = PCI_BDF(0, 0x1f, 0);
  105. /* Is the device present? */
  106. pci_read_config_word(dev, PCI_VENDOR_ID, &tmpword);
  107. if (tmpword != PCI_VENDOR_ID_INTEL) {
  108. debug("%s: wrong VendorID\n", __func__);
  109. return -1;
  110. }
  111. pci_read_config_word(dev, PCI_DEVICE_ID, &tmpword);
  112. debug("Found %04x:%04x\n", PCI_VENDOR_ID_INTEL, tmpword);
  113. /*
  114. * We'd like to validate the Device ID too, but pretty much any
  115. * value is either a) correct with slight differences, or b)
  116. * correct but undocumented. We'll have to check a bunch of other
  117. * things instead...
  118. */
  119. /* I/O should already be enabled (it's a RO bit). */
  120. pci_read_config_word(dev, PCI_COMMAND, &tmpword);
  121. if (!(tmpword & PCI_COMMAND_IO)) {
  122. debug("%s: device IO not enabled\n", __func__);
  123. return -1;
  124. }
  125. /* Header Type must be normal (bits 6-0 only; see spec.) */
  126. pci_read_config_byte(dev, PCI_HEADER_TYPE, &tmpbyte);
  127. if ((tmpbyte & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
  128. debug("%s: invalid Header type\n", __func__);
  129. return -1;
  130. }
  131. /* Base Class must be a bridge device */
  132. pci_read_config_byte(dev, PCI_CLASS_CODE, &tmpbyte);
  133. if (tmpbyte != PCI_CLASS_CODE_BRIDGE) {
  134. debug("%s: invalid class\n", __func__);
  135. return -1;
  136. }
  137. /* Sub Class must be ISA */
  138. pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &tmpbyte);
  139. if (tmpbyte != PCI_CLASS_SUB_CODE_BRIDGE_ISA) {
  140. debug("%s: invalid subclass\n", __func__);
  141. return -1;
  142. }
  143. /* Programming Interface must be 0x00 (no others exist) */
  144. pci_read_config_byte(dev, PCI_CLASS_PROG, &tmpbyte);
  145. if (tmpbyte != 0x00) {
  146. debug("%s: invalid interface type\n", __func__);
  147. return -1;
  148. }
  149. /*
  150. * GPIOBASE moved to its current offset with ICH6, but prior to
  151. * that it was unused (or undocumented). Check that it looks
  152. * okay: not all ones or zeros, and mapped to I/O space (bit 0).
  153. */
  154. pci_read_config_dword(dev, PCI_CFG_GPIOBASE, &tmplong);
  155. if (tmplong == 0x00000000 || tmplong == 0xffffffff ||
  156. !(tmplong & 0x00000001)) {
  157. debug("%s: unexpected GPIOBASE value\n", __func__);
  158. return -1;
  159. }
  160. /*
  161. * Okay, I guess we're looking at the right device. The actual
  162. * GPIO registers are in the PCI device's I/O space, starting
  163. * at the offset that we just read. Bit 0 indicates that it's
  164. * an I/O address, not a memory address, so mask that off.
  165. */
  166. gpiobase = tmplong & 0xfffffffe;
  167. /* Finally. These are the droids we're looking for. */
  168. found_it_once = 1;
  169. return 0;
  170. }
  171. int gpio_request(unsigned num, const char *label /* UNUSED */)
  172. {
  173. u32 tmplong;
  174. int i = 0, j = 0;
  175. /* Is the hardware ready? */
  176. if (gpio_init())
  177. return -1;
  178. if (bad_arg(num, &i, &j))
  179. return -1;
  180. /*
  181. * Make sure that the GPIO pin we want isn't already in use for some
  182. * built-in hardware function. We have to check this for every
  183. * requested pin.
  184. */
  185. tmplong = inl(gpiobase + gpio_bank[i].use_sel);
  186. if (!(tmplong & (1UL << j))) {
  187. debug("%s: gpio %d is reserved for internal use\n", __func__,
  188. num);
  189. return -1;
  190. }
  191. return mark_gpio(i, j);
  192. }
  193. int gpio_free(unsigned num)
  194. {
  195. int i = 0, j = 0;
  196. if (notmine(num, &i, &j))
  197. return -1;
  198. clear_gpio(i, j);
  199. return 0;
  200. }
  201. int gpio_direction_input(unsigned num)
  202. {
  203. u32 tmplong;
  204. int i = 0, j = 0;
  205. if (notmine(num, &i, &j))
  206. return -1;
  207. tmplong = inl(gpiobase + gpio_bank[i].io_sel);
  208. tmplong |= (1UL << j);
  209. outl(gpiobase + gpio_bank[i].io_sel, tmplong);
  210. return 0;
  211. }
  212. int gpio_direction_output(unsigned num, int value)
  213. {
  214. u32 tmplong;
  215. int i = 0, j = 0;
  216. if (notmine(num, &i, &j))
  217. return -1;
  218. tmplong = inl(gpiobase + gpio_bank[i].io_sel);
  219. tmplong &= ~(1UL << j);
  220. outl(gpiobase + gpio_bank[i].io_sel, tmplong);
  221. return 0;
  222. }
  223. int gpio_get_value(unsigned num)
  224. {
  225. u32 tmplong;
  226. int i = 0, j = 0;
  227. int r;
  228. if (notmine(num, &i, &j))
  229. return -1;
  230. tmplong = inl(gpiobase + gpio_bank[i].lvl);
  231. r = (tmplong & (1UL << j)) ? 1 : 0;
  232. return r;
  233. }
  234. int gpio_set_value(unsigned num, int value)
  235. {
  236. u32 tmplong;
  237. int i = 0, j = 0;
  238. if (notmine(num, &i, &j))
  239. return -1;
  240. tmplong = inl(gpiobase + gpio_bank[i].lvl);
  241. if (value)
  242. tmplong |= (1UL << j);
  243. else
  244. tmplong &= ~(1UL << j);
  245. outl(gpiobase + gpio_bank[i].lvl, tmplong);
  246. return 0;
  247. }