intel_ich6_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. uint16_t use_sel;
  39. uint16_t io_sel;
  40. uint16_t lvl;
  41. };
  42. #define GPIO_USESEL_OFFSET(x) (x)
  43. #define GPIO_IOSEL_OFFSET(x) (x + 4)
  44. #define GPIO_LVL_OFFSET(x) (x + 8)
  45. #define IOPAD_MODE_MASK 0x7
  46. #define IOPAD_PULL_ASSIGN_SHIFT 7
  47. #define IOPAD_PULL_ASSIGN_MASK (0x3 << IOPAD_PULL_ASSIGN_SHIFT)
  48. #define IOPAD_PULL_STRENGTH_SHIFT 9
  49. #define IOPAD_PULL_STRENGTH_MASK (0x3 << IOPAD_PULL_STRENGTH_SHIFT)
  50. /* TODO: Move this to device tree, or platform data */
  51. void ich_gpio_set_gpio_map(const struct pch_gpio_map *map)
  52. {
  53. gd->arch.gpio_map = map;
  54. }
  55. static int gpio_ich6_get_base(unsigned long base)
  56. {
  57. pci_dev_t pci_dev; /* handle for 0:1f:0 */
  58. u8 tmpbyte;
  59. u16 tmpword;
  60. u32 tmplong;
  61. /* Where should it be? */
  62. pci_dev = PCI_BDF(0, 0x1f, 0);
  63. /* Is the device present? */
  64. tmpword = x86_pci_read_config16(pci_dev, PCI_VENDOR_ID);
  65. if (tmpword != PCI_VENDOR_ID_INTEL) {
  66. debug("%s: wrong VendorID\n", __func__);
  67. return -ENODEV;
  68. }
  69. tmpword = x86_pci_read_config16(pci_dev, PCI_DEVICE_ID);
  70. debug("Found %04x:%04x\n", PCI_VENDOR_ID_INTEL, tmpword);
  71. /*
  72. * We'd like to validate the Device ID too, but pretty much any
  73. * value is either a) correct with slight differences, or b)
  74. * correct but undocumented. We'll have to check a bunch of other
  75. * things instead...
  76. */
  77. /* I/O should already be enabled (it's a RO bit). */
  78. tmpword = x86_pci_read_config16(pci_dev, PCI_COMMAND);
  79. if (!(tmpword & PCI_COMMAND_IO)) {
  80. debug("%s: device IO not enabled\n", __func__);
  81. return -ENODEV;
  82. }
  83. /* Header Type must be normal (bits 6-0 only; see spec.) */
  84. tmpbyte = x86_pci_read_config8(pci_dev, PCI_HEADER_TYPE);
  85. if ((tmpbyte & 0x7f) != PCI_HEADER_TYPE_NORMAL) {
  86. debug("%s: invalid Header type\n", __func__);
  87. return -ENODEV;
  88. }
  89. /* Base Class must be a bridge device */
  90. tmpbyte = x86_pci_read_config8(pci_dev, PCI_CLASS_CODE);
  91. if (tmpbyte != PCI_CLASS_CODE_BRIDGE) {
  92. debug("%s: invalid class\n", __func__);
  93. return -ENODEV;
  94. }
  95. /* Sub Class must be ISA */
  96. tmpbyte = x86_pci_read_config8(pci_dev, PCI_CLASS_SUB_CODE);
  97. if (tmpbyte != PCI_CLASS_SUB_CODE_BRIDGE_ISA) {
  98. debug("%s: invalid subclass\n", __func__);
  99. return -ENODEV;
  100. }
  101. /* Programming Interface must be 0x00 (no others exist) */
  102. tmpbyte = x86_pci_read_config8(pci_dev, PCI_CLASS_PROG);
  103. if (tmpbyte != 0x00) {
  104. debug("%s: invalid interface type\n", __func__);
  105. return -ENODEV;
  106. }
  107. /*
  108. * GPIOBASE moved to its current offset with ICH6, but prior to
  109. * that it was unused (or undocumented). Check that it looks
  110. * okay: not all ones or zeros.
  111. *
  112. * Note we don't need check bit0 here, because the Tunnel Creek
  113. * GPIO base address register bit0 is reserved (read returns 0),
  114. * while on the Ivybridge the bit0 is used to indicate it is an
  115. * I/O space.
  116. */
  117. tmplong = x86_pci_read_config32(pci_dev, base);
  118. if (tmplong == 0x00000000 || tmplong == 0xffffffff) {
  119. debug("%s: unexpected BASE value\n", __func__);
  120. return -ENODEV;
  121. }
  122. /*
  123. * Okay, I guess we're looking at the right device. The actual
  124. * GPIO registers are in the PCI device's I/O space, starting
  125. * at the offset that we just read. Bit 0 indicates that it's
  126. * an I/O address, not a memory address, so mask that off.
  127. */
  128. return tmplong & 0xfffc;
  129. }
  130. static int _ich6_gpio_set_value(uint16_t base, unsigned offset, int value)
  131. {
  132. u32 val;
  133. val = inl(base);
  134. if (value)
  135. val |= (1UL << offset);
  136. else
  137. val &= ~(1UL << offset);
  138. outl(val, base);
  139. return 0;
  140. }
  141. static int _ich6_gpio_set_function(uint16_t base, unsigned offset, int func)
  142. {
  143. u32 val;
  144. if (func) {
  145. val = inl(base);
  146. val |= (1UL << offset);
  147. outl(val, base);
  148. } else {
  149. val = inl(base);
  150. val &= ~(1UL << offset);
  151. outl(val, base);
  152. }
  153. return 0;
  154. }
  155. static int _ich6_gpio_set_direction(uint16_t base, unsigned offset, int dir)
  156. {
  157. u32 val;
  158. if (!dir) {
  159. val = inl(base);
  160. val |= (1UL << offset);
  161. outl(val, base);
  162. } else {
  163. val = inl(base);
  164. val &= ~(1UL << offset);
  165. outl(val, base);
  166. }
  167. return 0;
  168. }
  169. static int _gpio_ich6_pinctrl_cfg_pin(s32 gpiobase, s32 iobase, int pin_node)
  170. {
  171. u32 gpio_offset[2];
  172. int pad_offset;
  173. int val;
  174. int ret;
  175. const void *prop;
  176. /*
  177. * GPIO node is not mandatory, so we only do the
  178. * pinmuxing if the node exist.
  179. */
  180. ret = fdtdec_get_int_array(gd->fdt_blob, pin_node, "gpio-offset",
  181. gpio_offset, 2);
  182. if (!ret) {
  183. /* Do we want to force the GPIO mode? */
  184. prop = fdt_getprop(gd->fdt_blob, pin_node, "mode-gpio",
  185. NULL);
  186. if (prop)
  187. _ich6_gpio_set_function(GPIO_USESEL_OFFSET
  188. (gpiobase) +
  189. gpio_offset[0],
  190. gpio_offset[1], 1);
  191. val =
  192. fdtdec_get_int(gd->fdt_blob, pin_node, "direction", -1);
  193. if (val != -1)
  194. _ich6_gpio_set_direction(GPIO_IOSEL_OFFSET
  195. (gpiobase) +
  196. gpio_offset[0],
  197. gpio_offset[1], val);
  198. val =
  199. fdtdec_get_int(gd->fdt_blob, pin_node, "output-value", -1);
  200. if (val != -1)
  201. _ich6_gpio_set_value(GPIO_LVL_OFFSET(gpiobase)
  202. + gpio_offset[0],
  203. gpio_offset[1], val);
  204. }
  205. /* if iobase is present, let's configure the pad */
  206. if (iobase != -1) {
  207. int iobase_addr;
  208. /*
  209. * The offset for the same pin for the IOBASE and GPIOBASE are
  210. * different, so instead of maintaining a lookup table,
  211. * the device tree should provide directly the correct
  212. * value for both mapping.
  213. */
  214. pad_offset =
  215. fdtdec_get_int(gd->fdt_blob, pin_node, "pad-offset", -1);
  216. if (pad_offset == -1) {
  217. debug("%s: Invalid register io offset %d\n",
  218. __func__, pad_offset);
  219. return -EINVAL;
  220. }
  221. /* compute the absolute pad address */
  222. iobase_addr = iobase + pad_offset;
  223. /*
  224. * Do we need to set a specific function mode?
  225. * If someone put also 'mode-gpio', this option will
  226. * be just ignored by the controller
  227. */
  228. val = fdtdec_get_int(gd->fdt_blob, pin_node, "mode-func", -1);
  229. if (val != -1)
  230. clrsetbits_le32(iobase_addr, IOPAD_MODE_MASK, val);
  231. /* Configure the pull-up/down if needed */
  232. val = fdtdec_get_int(gd->fdt_blob, pin_node, "pull-assign", -1);
  233. if (val != -1)
  234. clrsetbits_le32(iobase_addr,
  235. IOPAD_PULL_ASSIGN_MASK,
  236. val << IOPAD_PULL_ASSIGN_SHIFT);
  237. val =
  238. fdtdec_get_int(gd->fdt_blob, pin_node, "pull-strength", -1);
  239. if (val != -1)
  240. clrsetbits_le32(iobase_addr,
  241. IOPAD_PULL_STRENGTH_MASK,
  242. val << IOPAD_PULL_STRENGTH_SHIFT);
  243. debug("%s: pad cfg [0x%x]: %08x\n", __func__, pad_offset,
  244. readl(iobase_addr));
  245. }
  246. return 0;
  247. }
  248. int gpio_ich6_pinctrl_init(void)
  249. {
  250. int pin_node;
  251. int node;
  252. int ret;
  253. int gpiobase;
  254. int iobase_offset;
  255. int iobase = -1;
  256. /*
  257. * Get the memory/io base address to configure every pins.
  258. * IOBASE is used to configure the mode/pads
  259. * GPIOBASE is used to configure the direction and default value
  260. */
  261. gpiobase = gpio_ich6_get_base(PCI_CFG_GPIOBASE);
  262. if (gpiobase < 0) {
  263. debug("%s: invalid GPIOBASE address (%08x)\n", __func__,
  264. gpiobase);
  265. return -EINVAL;
  266. }
  267. /* This is not an error to not have a pinctrl node */
  268. node =
  269. fdtdec_next_compatible(gd->fdt_blob, 0, COMPAT_INTEL_X86_PINCTRL);
  270. if (node <= 0) {
  271. debug("%s: no pinctrl node\n", __func__);
  272. return 0;
  273. }
  274. /*
  275. * Get the IOBASE, this is not mandatory as this is not
  276. * supported by all the CPU
  277. */
  278. iobase_offset = fdtdec_get_int(gd->fdt_blob, node, "io-base", -1);
  279. if (iobase_offset == -1) {
  280. debug("%s: io-base offset not present\n", __func__);
  281. } else {
  282. iobase = gpio_ich6_get_base(iobase_offset);
  283. if (iobase < 0) {
  284. debug("%s: invalid IOBASE address (%08x)\n", __func__,
  285. iobase);
  286. return -EINVAL;
  287. }
  288. }
  289. for (pin_node = fdt_first_subnode(gd->fdt_blob, node);
  290. pin_node > 0;
  291. pin_node = fdt_next_subnode(gd->fdt_blob, pin_node)) {
  292. /* Configure the pin */
  293. ret = _gpio_ich6_pinctrl_cfg_pin(gpiobase, iobase, pin_node);
  294. if (ret != 0) {
  295. debug("%s: invalid configuration for the pin %d\n",
  296. __func__, pin_node);
  297. return ret;
  298. }
  299. }
  300. return 0;
  301. }
  302. static int gpio_ich6_ofdata_to_platdata(struct udevice *dev)
  303. {
  304. struct ich6_bank_platdata *plat = dev_get_platdata(dev);
  305. u16 gpiobase;
  306. int offset;
  307. gpiobase = gpio_ich6_get_base(PCI_CFG_GPIOBASE);
  308. offset = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1);
  309. if (offset == -1) {
  310. debug("%s: Invalid register offset %d\n", __func__, offset);
  311. return -EINVAL;
  312. }
  313. plat->base_addr = gpiobase + offset;
  314. plat->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
  315. "bank-name", NULL);
  316. return 0;
  317. }
  318. static int ich6_gpio_probe(struct udevice *dev)
  319. {
  320. struct ich6_bank_platdata *plat = dev_get_platdata(dev);
  321. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  322. struct ich6_bank_priv *bank = dev_get_priv(dev);
  323. if (gd->arch.gpio_map) {
  324. setup_pch_gpios(plat->base_addr, gd->arch.gpio_map);
  325. gd->arch.gpio_map = NULL;
  326. }
  327. uc_priv->gpio_count = GPIO_PER_BANK;
  328. uc_priv->bank_name = plat->bank_name;
  329. bank->use_sel = plat->base_addr;
  330. bank->io_sel = plat->base_addr + 4;
  331. bank->lvl = plat->base_addr + 8;
  332. return 0;
  333. }
  334. static int ich6_gpio_request(struct udevice *dev, unsigned offset,
  335. const char *label)
  336. {
  337. struct ich6_bank_priv *bank = dev_get_priv(dev);
  338. u32 tmplong;
  339. /*
  340. * Make sure that the GPIO pin we want isn't already in use for some
  341. * built-in hardware function. We have to check this for every
  342. * requested pin.
  343. */
  344. tmplong = inl(bank->use_sel);
  345. if (!(tmplong & (1UL << offset))) {
  346. debug("%s: gpio %d is reserved for internal use\n", __func__,
  347. offset);
  348. return -EPERM;
  349. }
  350. return 0;
  351. }
  352. static int ich6_gpio_direction_input(struct udevice *dev, unsigned offset)
  353. {
  354. struct ich6_bank_priv *bank = dev_get_priv(dev);
  355. return _ich6_gpio_set_direction(inl(bank->io_sel), offset, 0);
  356. }
  357. static int ich6_gpio_direction_output(struct udevice *dev, unsigned offset,
  358. int value)
  359. {
  360. int ret;
  361. struct ich6_bank_priv *bank = dev_get_priv(dev);
  362. ret = _ich6_gpio_set_direction(inl(bank->io_sel), offset, 1);
  363. if (ret)
  364. return ret;
  365. return _ich6_gpio_set_value(bank->lvl, offset, value);
  366. }
  367. static int ich6_gpio_get_value(struct udevice *dev, unsigned offset)
  368. {
  369. struct ich6_bank_priv *bank = dev_get_priv(dev);
  370. u32 tmplong;
  371. int r;
  372. tmplong = inl(bank->lvl);
  373. r = (tmplong & (1UL << offset)) ? 1 : 0;
  374. return r;
  375. }
  376. static int ich6_gpio_set_value(struct udevice *dev, unsigned offset,
  377. int value)
  378. {
  379. struct ich6_bank_priv *bank = dev_get_priv(dev);
  380. return _ich6_gpio_set_value(bank->lvl, offset, value);
  381. }
  382. static int ich6_gpio_get_function(struct udevice *dev, unsigned offset)
  383. {
  384. struct ich6_bank_priv *bank = dev_get_priv(dev);
  385. u32 mask = 1UL << offset;
  386. if (!(inl(bank->use_sel) & mask))
  387. return GPIOF_FUNC;
  388. if (inl(bank->io_sel) & mask)
  389. return GPIOF_INPUT;
  390. else
  391. return GPIOF_OUTPUT;
  392. }
  393. static const struct dm_gpio_ops gpio_ich6_ops = {
  394. .request = ich6_gpio_request,
  395. .direction_input = ich6_gpio_direction_input,
  396. .direction_output = ich6_gpio_direction_output,
  397. .get_value = ich6_gpio_get_value,
  398. .set_value = ich6_gpio_set_value,
  399. .get_function = ich6_gpio_get_function,
  400. };
  401. static const struct udevice_id intel_ich6_gpio_ids[] = {
  402. { .compatible = "intel,ich6-gpio" },
  403. { }
  404. };
  405. U_BOOT_DRIVER(gpio_ich6) = {
  406. .name = "gpio_ich6",
  407. .id = UCLASS_GPIO,
  408. .of_match = intel_ich6_gpio_ids,
  409. .ops = &gpio_ich6_ops,
  410. .ofdata_to_platdata = gpio_ich6_ofdata_to_platdata,
  411. .probe = ich6_gpio_probe,
  412. .priv_auto_alloc_size = sizeof(struct ich6_bank_priv),
  413. .platdata_auto_alloc_size = sizeof(struct ich6_bank_platdata),
  414. };