1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
14 #include <asm/global_data.h>
18 #include <dm/pinctrl.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #define GPIO_USESEL_OFFSET(x) (x)
23 #define GPIO_IOSEL_OFFSET(x) (x + 4)
24 #define GPIO_LVL_OFFSET(x) ((x) ? (x) + 8 : 0xc)
27 #define IOPAD_MODE_MASK 0x7
28 #define IOPAD_PULL_ASSIGN_SHIFT 7
29 #define IOPAD_PULL_ASSIGN_MASK (0x3 << IOPAD_PULL_ASSIGN_SHIFT)
30 #define IOPAD_PULL_STRENGTH_SHIFT 9
31 #define IOPAD_PULL_STRENGTH_MASK (0x3 << IOPAD_PULL_STRENGTH_SHIFT)
33 static int ich6_pinctrl_set_value(uint16_t base, unsigned offset, int value)
36 setio_32(base, 1UL << offset);
38 clrio_32(base, 1UL << offset);
43 static int ich6_pinctrl_set_function(uint16_t base, unsigned offset, int func)
46 setio_32(base, 1UL << offset);
48 clrio_32(base, 1UL << offset);
53 static int ich6_pinctrl_set_direction(uint16_t base, unsigned offset, int dir)
56 setio_32(base, 1UL << offset);
58 clrio_32(base, 1UL << offset);
63 static int ich6_pinctrl_cfg_pin(s32 gpiobase, s32 iobase, int pin_node)
72 * GPIO node is not mandatory, so we only do the pinmuxing if the
75 ret = fdtdec_get_int_array(gd->fdt_blob, pin_node, "gpio-offset",
78 /* Do we want to force the GPIO mode? */
79 is_gpio = fdtdec_get_bool(gd->fdt_blob, pin_node, "mode-gpio");
81 ich6_pinctrl_set_function(GPIO_USESEL_OFFSET(gpiobase) +
82 gpio_offset[0], gpio_offset[1],
85 dir = fdtdec_get_int(gd->fdt_blob, pin_node, "direction", -1);
87 ich6_pinctrl_set_direction(GPIO_IOSEL_OFFSET(gpiobase) +
88 gpio_offset[0], gpio_offset[1],
91 val = fdtdec_get_int(gd->fdt_blob, pin_node, "output-value",
94 ich6_pinctrl_set_value(GPIO_LVL_OFFSET(gpiobase) +
95 gpio_offset[0], gpio_offset[1],
98 invert = fdtdec_get_bool(gd->fdt_blob, pin_node, "invert");
100 setio_32(gpiobase + GPI_INV, 1 << gpio_offset[1]);
101 debug("gpio %#x bit %d, is_gpio %d, dir %d, val %d, invert %d\n",
102 gpio_offset[0], gpio_offset[1], is_gpio, dir, val,
106 /* if iobase is present, let's configure the pad */
111 * The offset for the same pin for the IOBASE and GPIOBASE are
112 * different, so instead of maintaining a lookup table,
113 * the device tree should provide directly the correct
114 * value for both mapping.
116 pad_offset = fdtdec_get_int(gd->fdt_blob, pin_node,
118 if (pad_offset == -1)
121 /* compute the absolute pad address */
122 iobase_addr = iobase + pad_offset;
125 * Do we need to set a specific function mode?
126 * If someone put also 'mode-gpio', this option will
127 * be just ignored by the controller
129 val = fdtdec_get_int(gd->fdt_blob, pin_node, "mode-func", -1);
131 clrsetbits_le32(iobase_addr, IOPAD_MODE_MASK, val);
133 /* Configure the pull-up/down if needed */
134 val = fdtdec_get_int(gd->fdt_blob, pin_node, "pull-assign", -1);
136 clrsetbits_le32(iobase_addr,
137 IOPAD_PULL_ASSIGN_MASK,
138 val << IOPAD_PULL_ASSIGN_SHIFT);
140 val = fdtdec_get_int(gd->fdt_blob, pin_node, "pull-strength",
143 clrsetbits_le32(iobase_addr,
144 IOPAD_PULL_STRENGTH_MASK,
145 val << IOPAD_PULL_STRENGTH_SHIFT);
147 debug("%s: pad cfg [0x%x]: %08x\n", __func__, pad_offset,
154 static int ich6_pinctrl_probe(struct udevice *dev)
162 debug("%s: start\n", __func__);
163 ret = uclass_first_device(UCLASS_PCH, &pch);
170 * Get the memory/io base address to configure every pins.
171 * IOBASE is used to configure the mode/pads
172 * GPIOBASE is used to configure the direction and default value
174 ret = pch_get_gpio_base(pch, &gpiobase);
176 debug("%s: invalid GPIOBASE address (%08x)\n", __func__,
182 * Get the IOBASE, this is not mandatory as this is not
183 * supported by all the CPU
185 ret = pch_get_io_base(pch, &iobase);
186 if (ret && ret != -ENOSYS) {
187 debug("%s: invalid IOBASE address (%08x)\n", __func__, iobase);
191 for (pin_node = fdt_first_subnode(gd->fdt_blob, dev_of_offset(dev));
193 pin_node = fdt_next_subnode(gd->fdt_blob, pin_node)) {
194 /* Configure the pin */
195 ret = ich6_pinctrl_cfg_pin(gpiobase, iobase, pin_node);
197 debug("%s: invalid configuration for the pin %d\n",
202 debug("%s: done\n", __func__);
207 static const struct udevice_id ich6_pinctrl_match[] = {
208 { .compatible = "intel,x86-pinctrl", .data = X86_SYSCON_PINCONF },
212 U_BOOT_DRIVER(ich6_pinctrl) = {
213 .name = "ich6_pinctrl",
215 .of_match = ich6_pinctrl_match,
216 .probe = ich6_pinctrl_probe,