2 * Copyright (C) 2016 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
17 #include <dm/pinctrl.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #define GPIO_USESEL_OFFSET(x) (x)
22 #define GPIO_IOSEL_OFFSET(x) (x + 4)
23 #define GPIO_LVL_OFFSET(x) ((x) ? (x) + 8 : 0xc)
26 #define IOPAD_MODE_MASK 0x7
27 #define IOPAD_PULL_ASSIGN_SHIFT 7
28 #define IOPAD_PULL_ASSIGN_MASK (0x3 << IOPAD_PULL_ASSIGN_SHIFT)
29 #define IOPAD_PULL_STRENGTH_SHIFT 9
30 #define IOPAD_PULL_STRENGTH_MASK (0x3 << IOPAD_PULL_STRENGTH_SHIFT)
32 static int ich6_pinctrl_set_value(uint16_t base, unsigned offset, int value)
35 setio_32(base, 1UL << offset);
37 clrio_32(base, 1UL << offset);
42 static int ich6_pinctrl_set_function(uint16_t base, unsigned offset, int func)
45 setio_32(base, 1UL << offset);
47 clrio_32(base, 1UL << offset);
52 static int ich6_pinctrl_set_direction(uint16_t base, unsigned offset, int dir)
55 setio_32(base, 1UL << offset);
57 clrio_32(base, 1UL << offset);
62 static int ich6_pinctrl_cfg_pin(s32 gpiobase, s32 iobase, int pin_node)
71 * GPIO node is not mandatory, so we only do the pinmuxing if the
74 ret = fdtdec_get_int_array(gd->fdt_blob, pin_node, "gpio-offset",
77 /* Do we want to force the GPIO mode? */
78 is_gpio = fdtdec_get_bool(gd->fdt_blob, pin_node, "mode-gpio");
80 ich6_pinctrl_set_function(GPIO_USESEL_OFFSET(gpiobase) +
81 gpio_offset[0], gpio_offset[1],
84 dir = fdtdec_get_int(gd->fdt_blob, pin_node, "direction", -1);
86 ich6_pinctrl_set_direction(GPIO_IOSEL_OFFSET(gpiobase) +
87 gpio_offset[0], gpio_offset[1],
90 val = fdtdec_get_int(gd->fdt_blob, pin_node, "output-value",
93 ich6_pinctrl_set_value(GPIO_LVL_OFFSET(gpiobase) +
94 gpio_offset[0], gpio_offset[1],
97 invert = fdtdec_get_bool(gd->fdt_blob, pin_node, "invert");
99 setio_32(gpiobase + GPI_INV, 1 << gpio_offset[1]);
100 debug("gpio %#x bit %d, is_gpio %d, dir %d, val %d, invert %d\n",
101 gpio_offset[0], gpio_offset[1], is_gpio, dir, val,
105 /* if iobase is present, let's configure the pad */
110 * The offset for the same pin for the IOBASE and GPIOBASE are
111 * different, so instead of maintaining a lookup table,
112 * the device tree should provide directly the correct
113 * value for both mapping.
115 pad_offset = fdtdec_get_int(gd->fdt_blob, pin_node,
117 if (pad_offset == -1)
120 /* compute the absolute pad address */
121 iobase_addr = iobase + pad_offset;
124 * Do we need to set a specific function mode?
125 * If someone put also 'mode-gpio', this option will
126 * be just ignored by the controller
128 val = fdtdec_get_int(gd->fdt_blob, pin_node, "mode-func", -1);
130 clrsetbits_le32(iobase_addr, IOPAD_MODE_MASK, val);
132 /* Configure the pull-up/down if needed */
133 val = fdtdec_get_int(gd->fdt_blob, pin_node, "pull-assign", -1);
135 clrsetbits_le32(iobase_addr,
136 IOPAD_PULL_ASSIGN_MASK,
137 val << IOPAD_PULL_ASSIGN_SHIFT);
139 val = fdtdec_get_int(gd->fdt_blob, pin_node, "pull-strength",
142 clrsetbits_le32(iobase_addr,
143 IOPAD_PULL_STRENGTH_MASK,
144 val << IOPAD_PULL_STRENGTH_SHIFT);
146 debug("%s: pad cfg [0x%x]: %08x\n", __func__, pad_offset,
153 static int ich6_pinctrl_probe(struct udevice *dev)
161 debug("%s: start\n", __func__);
162 ret = uclass_first_device(UCLASS_PCH, &pch);
169 * Get the memory/io base address to configure every pins.
170 * IOBASE is used to configure the mode/pads
171 * GPIOBASE is used to configure the direction and default value
173 ret = pch_get_gpio_base(pch, &gpiobase);
175 debug("%s: invalid GPIOBASE address (%08x)\n", __func__,
181 * Get the IOBASE, this is not mandatory as this is not
182 * supported by all the CPU
184 ret = pch_get_io_base(pch, &iobase);
185 if (ret && ret != -ENOSYS) {
186 debug("%s: invalid IOBASE address (%08x)\n", __func__, iobase);
190 for (pin_node = fdt_first_subnode(gd->fdt_blob, dev_of_offset(dev));
192 pin_node = fdt_next_subnode(gd->fdt_blob, pin_node)) {
193 /* Configure the pin */
194 ret = ich6_pinctrl_cfg_pin(gpiobase, iobase, pin_node);
196 debug("%s: invalid configuration for the pin %d\n",
201 debug("%s: done\n", __func__);
206 static const struct udevice_id ich6_pinctrl_match[] = {
207 { .compatible = "intel,x86-pinctrl", .data = X86_SYSCON_PINCONF },
211 U_BOOT_DRIVER(ich6_pinctrl) = {
212 .name = "ich6_pinctrl",
214 .of_match = ich6_pinctrl_match,
215 .probe = ich6_pinctrl_probe,