1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Nexell
4 * DeokJin, Lee <truevirtue@nexell.co.kr>
14 DECLARE_GLOBAL_DATA_PTR;
17 u32 data; /* Data register */
18 u32 outputenb; /* Output Enable register */
19 u32 detmode[2]; /* Detect Mode Register */
20 u32 intenb; /* Interrupt Enable Register */
21 u32 det; /* Event Detect Register */
22 u32 pad; /* Pad Status Register */
25 struct nx_alive_gpio_regs {
26 u32 pwrgate; /* Power Gating Register */
27 u32 reserved0[28]; /* Reserved0 */
28 u32 outputenb_reset;/* Alive GPIO Output Enable Reset Register */
29 u32 outputenb; /* Alive GPIO Output Enable Register */
30 u32 outputenb_read; /* Alive GPIO Output Read Register */
31 u32 reserved1[3]; /* Reserved1 */
32 u32 pad_reset; /* Alive GPIO Output Reset Register */
33 u32 data; /* Alive GPIO Output Register */
34 u32 pad_read; /* Alive GPIO Pad Read Register */
35 u32 reserved2[33]; /* Reserved2 */
36 u32 pad; /* Alive GPIO Input Value Register */
42 const char *bank_name;
45 static int nx_alive_gpio_is_check(struct udevice *dev)
47 struct nx_gpio_plat *plat = dev_get_plat(dev);
48 const char *bank_name = plat->bank_name;
50 if (!strcmp(bank_name, "gpio_alv"))
56 static int nx_alive_gpio_direction_input(struct udevice *dev, unsigned int pin)
58 struct nx_gpio_plat *plat = dev_get_plat(dev);
59 struct nx_alive_gpio_regs *const regs = plat->regs;
61 setbits_le32(®s->outputenb_reset, 1 << pin);
66 static int nx_alive_gpio_direction_output(struct udevice *dev, unsigned int pin,
69 struct nx_gpio_plat *plat = dev_get_plat(dev);
70 struct nx_alive_gpio_regs *const regs = plat->regs;
73 setbits_le32(®s->data, 1 << pin);
75 setbits_le32(®s->pad_reset, 1 << pin);
77 setbits_le32(®s->outputenb, 1 << pin);
82 static int nx_alive_gpio_get_value(struct udevice *dev, unsigned int pin)
84 struct nx_gpio_plat *plat = dev_get_plat(dev);
85 struct nx_alive_gpio_regs *const regs = plat->regs;
86 unsigned int mask = 1UL << pin;
89 value = (readl(®s->pad_read) & mask) >> pin;
94 static int nx_alive_gpio_set_value(struct udevice *dev, unsigned int pin,
97 struct nx_gpio_plat *plat = dev_get_plat(dev);
98 struct nx_alive_gpio_regs *const regs = plat->regs;
101 setbits_le32(®s->data, 1 << pin);
103 clrbits_le32(®s->pad_reset, 1 << pin);
108 static int nx_alive_gpio_get_function(struct udevice *dev, unsigned int pin)
110 struct nx_gpio_plat *plat = dev_get_plat(dev);
111 struct nx_alive_gpio_regs *const regs = plat->regs;
112 unsigned int mask = (1UL << pin);
115 output = readl(®s->outputenb_read) & mask;
123 static int nx_gpio_direction_input(struct udevice *dev, unsigned int pin)
125 struct nx_gpio_plat *plat = dev_get_plat(dev);
126 struct nx_gpio_regs *const regs = plat->regs;
128 if (nx_alive_gpio_is_check(dev))
129 return nx_alive_gpio_direction_input(dev, pin);
131 clrbits_le32(®s->outputenb, 1 << pin);
136 static int nx_gpio_direction_output(struct udevice *dev, unsigned int pin,
139 struct nx_gpio_plat *plat = dev_get_plat(dev);
140 struct nx_gpio_regs *const regs = plat->regs;
142 if (nx_alive_gpio_is_check(dev))
143 return nx_alive_gpio_direction_output(dev, pin, val);
146 setbits_le32(®s->data, 1 << pin);
148 clrbits_le32(®s->data, 1 << pin);
150 setbits_le32(®s->outputenb, 1 << pin);
155 static int nx_gpio_get_value(struct udevice *dev, unsigned int pin)
157 struct nx_gpio_plat *plat = dev_get_plat(dev);
158 struct nx_gpio_regs *const regs = plat->regs;
159 unsigned int mask = 1UL << pin;
162 if (nx_alive_gpio_is_check(dev))
163 return nx_alive_gpio_get_value(dev, pin);
165 value = (readl(®s->pad) & mask) >> pin;
170 static int nx_gpio_set_value(struct udevice *dev, unsigned int pin, int val)
172 struct nx_gpio_plat *plat = dev_get_plat(dev);
173 struct nx_gpio_regs *const regs = plat->regs;
175 if (nx_alive_gpio_is_check(dev))
176 return nx_alive_gpio_set_value(dev, pin, val);
179 setbits_le32(®s->data, 1 << pin);
181 clrbits_le32(®s->data, 1 << pin);
186 static int nx_gpio_get_function(struct udevice *dev, unsigned int pin)
188 struct nx_gpio_plat *plat = dev_get_plat(dev);
189 struct nx_gpio_regs *const regs = plat->regs;
190 unsigned int mask = (1UL << pin);
193 if (nx_alive_gpio_is_check(dev))
194 return nx_alive_gpio_get_function(dev, pin);
196 output = readl(®s->outputenb) & mask;
204 static int nx_gpio_probe(struct udevice *dev)
206 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
207 struct nx_gpio_plat *plat = dev_get_plat(dev);
209 uc_priv->gpio_count = plat->gpio_count;
210 uc_priv->bank_name = plat->bank_name;
215 static int nx_gpio_of_to_plat(struct udevice *dev)
217 struct nx_gpio_plat *plat = dev_get_plat(dev);
219 plat->regs = map_physmem(devfdt_get_addr(dev),
220 sizeof(struct nx_gpio_regs),
222 plat->gpio_count = dev_read_s32_default(dev, "nexell,gpio-bank-width",
224 plat->bank_name = dev_read_string(dev, "gpio-bank-name");
229 static const struct dm_gpio_ops nx_gpio_ops = {
230 .direction_input = nx_gpio_direction_input,
231 .direction_output = nx_gpio_direction_output,
232 .get_value = nx_gpio_get_value,
233 .set_value = nx_gpio_set_value,
234 .get_function = nx_gpio_get_function,
237 static const struct udevice_id nx_gpio_ids[] = {
238 { .compatible = "nexell,nexell-gpio" },
242 U_BOOT_DRIVER(nx_gpio) = {
245 .of_match = nx_gpio_ids,
247 .of_to_plat = nx_gpio_of_to_plat,
248 .plat_auto = sizeof(struct nx_gpio_plat),
249 .probe = nx_gpio_probe,