blob: b4d5be97dad72c6881a0b0f97c87ebd2984134ee [file] [log] [blame]
Simon Glass7e589bc2019-12-06 21:42:54 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 Google LLC
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass7e589bc2019-12-06 21:42:54 -070011#include <p2sb.h>
12#include <pch.h>
13#include <pci.h>
14#include <syscon.h>
15#include <asm/cpu.h>
16#include <asm/gpio.h>
17#include <asm/intel_pinctrl.h>
18#include <asm/intel_pinctrl_defs.h>
19#include <asm/io.h>
20#include <asm/pci.h>
21#include <asm/arch/gpio.h>
22#include <dt-bindings/gpio/x86-gpio.h>
23
24static int intel_gpio_direction_input(struct udevice *dev, uint offset)
25{
26 struct udevice *pinctrl = dev_get_parent(dev);
Simon Glass4916f452020-07-07 21:32:19 -060027 uint config_offset;
28
29 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
Simon Glass7e589bc2019-12-06 21:42:54 -070030
31 pcr_clrsetbits32(pinctrl, config_offset,
32 PAD_CFG0_MODE_MASK | PAD_CFG0_TX_STATE |
33 PAD_CFG0_RX_DISABLE,
34 PAD_CFG0_MODE_GPIO | PAD_CFG0_TX_DISABLE);
35
36 return 0;
37}
38
39static int intel_gpio_direction_output(struct udevice *dev, uint offset,
40 int value)
41{
42 struct udevice *pinctrl = dev_get_parent(dev);
Simon Glass4916f452020-07-07 21:32:19 -060043 uint config_offset;
44
45 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
Simon Glass7e589bc2019-12-06 21:42:54 -070046
Wolfgang Wallnerb840c392020-02-03 11:38:04 +010047 pcr_clrsetbits32(pinctrl, config_offset,
Simon Glass7e589bc2019-12-06 21:42:54 -070048 PAD_CFG0_MODE_MASK | PAD_CFG0_RX_STATE |
Wolfgang Wallner28c62682020-02-03 11:38:05 +010049 PAD_CFG0_TX_DISABLE | PAD_CFG0_TX_STATE,
Simon Glass7e589bc2019-12-06 21:42:54 -070050 PAD_CFG0_MODE_GPIO | PAD_CFG0_RX_DISABLE |
51 (value ? PAD_CFG0_TX_STATE : 0));
52
53 return 0;
54}
55
56static int intel_gpio_get_value(struct udevice *dev, uint offset)
57{
58 struct udevice *pinctrl = dev_get_parent(dev);
59 uint mode, rx_tx;
60 u32 reg;
61
62 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
63 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
64 if (!mode) {
65 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
66 if (rx_tx == PAD_CFG0_TX_DISABLE)
Wolfgang Wallnerea86e722020-02-03 11:38:06 +010067 return reg & PAD_CFG0_RX_STATE ? 1 : 0;
Simon Glass7e589bc2019-12-06 21:42:54 -070068 else if (rx_tx == PAD_CFG0_RX_DISABLE)
Wolfgang Wallnerea86e722020-02-03 11:38:06 +010069 return reg & PAD_CFG0_TX_STATE ? 1 : 0;
Simon Glass7e589bc2019-12-06 21:42:54 -070070 }
71
72 return 0;
73}
74
Simon Glass4916f452020-07-07 21:32:19 -060075static int intel_gpio_set_value(struct udevice *dev, unsigned int offset,
76 int value)
Simon Glass7e589bc2019-12-06 21:42:54 -070077{
78 struct udevice *pinctrl = dev_get_parent(dev);
Simon Glass4916f452020-07-07 21:32:19 -060079 uint config_offset;
80
81 config_offset = intel_pinctrl_get_config_reg_offset(pinctrl, offset);
Simon Glass7e589bc2019-12-06 21:42:54 -070082
Wolfgang Wallnerb840c392020-02-03 11:38:04 +010083 pcr_clrsetbits32(pinctrl, config_offset, PAD_CFG0_TX_STATE,
Simon Glass7e589bc2019-12-06 21:42:54 -070084 value ? PAD_CFG0_TX_STATE : 0);
85
86 return 0;
87}
88
89static int intel_gpio_get_function(struct udevice *dev, uint offset)
90{
91 struct udevice *pinctrl = dev_get_parent(dev);
92 uint mode, rx_tx;
93 u32 reg;
94
95 reg = intel_pinctrl_get_config_reg(pinctrl, offset);
96 mode = (reg & PAD_CFG0_MODE_MASK) >> PAD_CFG0_MODE_SHIFT;
97 if (!mode) {
98 rx_tx = reg & (PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE);
99 if (rx_tx == PAD_CFG0_TX_DISABLE)
100 return GPIOF_INPUT;
101 else if (rx_tx == PAD_CFG0_RX_DISABLE)
102 return GPIOF_OUTPUT;
103 }
104
105 return GPIOF_FUNC;
106}
107
108static int intel_gpio_xlate(struct udevice *orig_dev, struct gpio_desc *desc,
109 struct ofnode_phandle_args *args)
110{
111 struct udevice *pinctrl, *dev;
112 int gpio, ret;
113
114 /*
115 * GPIO numbers are global in the device tree so it doesn't matter
116 * which one is used
117 */
118 gpio = args->args[0];
119 ret = intel_pinctrl_get_pad(gpio, &pinctrl, &desc->offset);
120 if (ret)
121 return log_msg_ret("bad", ret);
122 device_find_first_child(pinctrl, &dev);
123 if (!dev)
124 return log_msg_ret("no child", -ENOENT);
125 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
126 desc->dev = dev;
127
128 return 0;
129}
130
131static int intel_gpio_probe(struct udevice *dev)
132{
133 return 0;
134}
135
136static int intel_gpio_ofdata_to_platdata(struct udevice *dev)
137{
138 struct gpio_dev_priv *upriv = dev_get_uclass_priv(dev);
139 struct intel_pinctrl_priv *pinctrl_priv = dev_get_priv(dev->parent);
140 const struct pad_community *comm = pinctrl_priv->comm;
141
142 upriv->gpio_count = comm->last_pad - comm->first_pad + 1;
143 upriv->bank_name = dev->name;
144
145 return 0;
146}
147
148static const struct dm_gpio_ops gpio_intel_ops = {
149 .direction_input = intel_gpio_direction_input,
150 .direction_output = intel_gpio_direction_output,
151 .get_value = intel_gpio_get_value,
152 .set_value = intel_gpio_set_value,
153 .get_function = intel_gpio_get_function,
154 .xlate = intel_gpio_xlate,
155};
156
157static const struct udevice_id intel_intel_gpio_ids[] = {
158 { .compatible = "intel,gpio" },
159 { }
160};
161
162U_BOOT_DRIVER(gpio_intel) = {
163 .name = "gpio_intel",
164 .id = UCLASS_GPIO,
165 .of_match = intel_intel_gpio_ids,
166 .ops = &gpio_intel_ops,
167 .ofdata_to_platdata = intel_gpio_ofdata_to_platdata,
168 .probe = intel_gpio_probe,
169};