intel_ich6_gpio.c 13 KB

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