blob: c93f245845de46f421aa37078edbeb45bd4884d0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass11cd6312016-03-11 22:07:13 -07002/*
3 * Copyright (C) 2016 Google, Inc
Simon Glass11cd6312016-03-11 22:07:13 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass11cd6312016-03-11 22:07:13 -070011#include <pch.h>
12#include <pci.h>
13#include <asm/cpu.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glass11cd6312016-03-11 22:07:13 -070015#include <asm/gpio.h>
16#include <asm/io.h>
17#include <asm/pci.h>
18#include <dm/pinctrl.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
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)
25#define GPI_INV 0x2c
26
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)
32
33static int ich6_pinctrl_set_value(uint16_t base, unsigned offset, int value)
34{
35 if (value)
36 setio_32(base, 1UL << offset);
37 else
38 clrio_32(base, 1UL << offset);
39
40 return 0;
41}
42
43static int ich6_pinctrl_set_function(uint16_t base, unsigned offset, int func)
44{
45 if (func)
46 setio_32(base, 1UL << offset);
47 else
48 clrio_32(base, 1UL << offset);
49
50 return 0;
51}
52
53static int ich6_pinctrl_set_direction(uint16_t base, unsigned offset, int dir)
54{
55 if (!dir)
56 setio_32(base, 1UL << offset);
57 else
58 clrio_32(base, 1UL << offset);
59
60 return 0;
61}
62
63static int ich6_pinctrl_cfg_pin(s32 gpiobase, s32 iobase, int pin_node)
64{
65 bool is_gpio, invert;
66 u32 gpio_offset[2];
67 int pad_offset;
68 int dir, val;
69 int ret;
70
71 /*
72 * GPIO node is not mandatory, so we only do the pinmuxing if the
73 * node exists.
74 */
75 ret = fdtdec_get_int_array(gd->fdt_blob, pin_node, "gpio-offset",
76 gpio_offset, 2);
77 if (!ret) {
78 /* Do we want to force the GPIO mode? */
79 is_gpio = fdtdec_get_bool(gd->fdt_blob, pin_node, "mode-gpio");
80 if (is_gpio)
81 ich6_pinctrl_set_function(GPIO_USESEL_OFFSET(gpiobase) +
82 gpio_offset[0], gpio_offset[1],
83 1);
84
85 dir = fdtdec_get_int(gd->fdt_blob, pin_node, "direction", -1);
86 if (dir != -1)
87 ich6_pinctrl_set_direction(GPIO_IOSEL_OFFSET(gpiobase) +
88 gpio_offset[0], gpio_offset[1],
89 dir);
90
91 val = fdtdec_get_int(gd->fdt_blob, pin_node, "output-value",
92 -1);
93 if (val != -1)
94 ich6_pinctrl_set_value(GPIO_LVL_OFFSET(gpiobase) +
95 gpio_offset[0], gpio_offset[1],
96 val);
97
98 invert = fdtdec_get_bool(gd->fdt_blob, pin_node, "invert");
99 if (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,
103 invert);
104 }
105
106 /* if iobase is present, let's configure the pad */
107 if (iobase != -1) {
Simon Glassaa0ed9d2017-01-16 07:03:42 -0700108 ulong iobase_addr;
Simon Glass11cd6312016-03-11 22:07:13 -0700109
110 /*
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.
115 */
116 pad_offset = fdtdec_get_int(gd->fdt_blob, pin_node,
117 "pad-offset", -1);
118 if (pad_offset == -1)
119 return 0;
120
121 /* compute the absolute pad address */
122 iobase_addr = iobase + pad_offset;
123
124 /*
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
128 */
129 val = fdtdec_get_int(gd->fdt_blob, pin_node, "mode-func", -1);
130 if (val != -1)
131 clrsetbits_le32(iobase_addr, IOPAD_MODE_MASK, val);
132
133 /* Configure the pull-up/down if needed */
134 val = fdtdec_get_int(gd->fdt_blob, pin_node, "pull-assign", -1);
135 if (val != -1)
136 clrsetbits_le32(iobase_addr,
137 IOPAD_PULL_ASSIGN_MASK,
138 val << IOPAD_PULL_ASSIGN_SHIFT);
139
140 val = fdtdec_get_int(gd->fdt_blob, pin_node, "pull-strength",
141 -1);
142 if (val != -1)
143 clrsetbits_le32(iobase_addr,
144 IOPAD_PULL_STRENGTH_MASK,
145 val << IOPAD_PULL_STRENGTH_SHIFT);
146
147 debug("%s: pad cfg [0x%x]: %08x\n", __func__, pad_offset,
148 readl(iobase_addr));
149 }
150
151 return 0;
152}
153
154static int ich6_pinctrl_probe(struct udevice *dev)
155{
156 struct udevice *pch;
157 int pin_node;
158 int ret;
159 u32 gpiobase;
160 u32 iobase = -1;
161
162 debug("%s: start\n", __func__);
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200163 ret = uclass_first_device_err(UCLASS_PCH, &pch);
Simon Glass11cd6312016-03-11 22:07:13 -0700164 if (ret)
165 return ret;
Simon Glass11cd6312016-03-11 22:07:13 -0700166
167 /*
168 * Get the memory/io base address to configure every pins.
169 * IOBASE is used to configure the mode/pads
170 * GPIOBASE is used to configure the direction and default value
171 */
172 ret = pch_get_gpio_base(pch, &gpiobase);
173 if (ret) {
174 debug("%s: invalid GPIOBASE address (%08x)\n", __func__,
175 gpiobase);
176 return -EINVAL;
177 }
178
179 /*
180 * Get the IOBASE, this is not mandatory as this is not
181 * supported by all the CPU
182 */
183 ret = pch_get_io_base(pch, &iobase);
184 if (ret && ret != -ENOSYS) {
185 debug("%s: invalid IOBASE address (%08x)\n", __func__, iobase);
186 return -EINVAL;
187 }
188
Simon Glassdd79d6e2017-01-17 16:52:55 -0700189 for (pin_node = fdt_first_subnode(gd->fdt_blob, dev_of_offset(dev));
Simon Glass11cd6312016-03-11 22:07:13 -0700190 pin_node > 0;
191 pin_node = fdt_next_subnode(gd->fdt_blob, pin_node)) {
192 /* Configure the pin */
193 ret = ich6_pinctrl_cfg_pin(gpiobase, iobase, pin_node);
194 if (ret != 0) {
195 debug("%s: invalid configuration for the pin %d\n",
196 __func__, pin_node);
197 return ret;
198 }
199 }
200 debug("%s: done\n", __func__);
201
202 return 0;
203}
204
205static const struct udevice_id ich6_pinctrl_match[] = {
206 { .compatible = "intel,x86-pinctrl", .data = X86_SYSCON_PINCONF },
207 { /* sentinel */ }
208};
209
210U_BOOT_DRIVER(ich6_pinctrl) = {
211 .name = "ich6_pinctrl",
212 .id = UCLASS_SYSCON,
213 .of_match = ich6_pinctrl_match,
214 .probe = ich6_pinctrl_probe,
215};