1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Nexell
4 * DeokJin, Lee <truevirtue@nexell.co.kr>
11 #include <asm/global_data.h>
15 DECLARE_GLOBAL_DATA_PTR;
18 u32 data; /* Data register */
19 u32 outputenb; /* Output Enable register */
20 u32 detmode[2]; /* Detect Mode Register */
21 u32 intenb; /* Interrupt Enable Register */
22 u32 det; /* Event Detect Register */
23 u32 pad; /* Pad Status Register */
26 struct nx_alive_gpio_regs {
27 u32 pwrgate; /* Power Gating Register */
28 u32 reserved0[28]; /* Reserved0 */
29 u32 outputenb_reset;/* Alive GPIO Output Enable Reset Register */
30 u32 outputenb; /* Alive GPIO Output Enable Register */
31 u32 outputenb_read; /* Alive GPIO Output Read Register */
32 u32 reserved1[3]; /* Reserved1 */
33 u32 pad_reset; /* Alive GPIO Output Reset Register */
34 u32 data; /* Alive GPIO Output Register */
35 u32 pad_read; /* Alive GPIO Pad Read Register */
36 u32 reserved2[33]; /* Reserved2 */
37 u32 pad; /* Alive GPIO Input Value Register */
43 const char *bank_name;
46 static int nx_alive_gpio_is_check(struct udevice *dev)
48 struct nx_gpio_plat *plat = dev_get_plat(dev);
49 const char *bank_name = plat->bank_name;
51 if (!strcmp(bank_name, "gpio_alv"))
57 static int nx_alive_gpio_direction_input(struct udevice *dev, unsigned int pin)
59 struct nx_gpio_plat *plat = dev_get_plat(dev);
60 struct nx_alive_gpio_regs *const regs = plat->regs;
62 setbits_le32(®s->outputenb_reset, 1 << pin);
67 static int nx_alive_gpio_direction_output(struct udevice *dev, unsigned int pin,
70 struct nx_gpio_plat *plat = dev_get_plat(dev);
71 struct nx_alive_gpio_regs *const regs = plat->regs;
74 setbits_le32(®s->data, 1 << pin);
76 setbits_le32(®s->pad_reset, 1 << pin);
78 setbits_le32(®s->outputenb, 1 << pin);
83 static int nx_alive_gpio_get_value(struct udevice *dev, unsigned int pin)
85 struct nx_gpio_plat *plat = dev_get_plat(dev);
86 struct nx_alive_gpio_regs *const regs = plat->regs;
87 unsigned int mask = 1UL << pin;
90 value = (readl(®s->pad_read) & mask) >> pin;
95 static int nx_alive_gpio_set_value(struct udevice *dev, unsigned int pin,
98 struct nx_gpio_plat *plat = dev_get_plat(dev);
99 struct nx_alive_gpio_regs *const regs = plat->regs;
102 setbits_le32(®s->data, 1 << pin);
104 clrbits_le32(®s->pad_reset, 1 << pin);
109 static int nx_alive_gpio_get_function(struct udevice *dev, unsigned int pin)
111 struct nx_gpio_plat *plat = dev_get_plat(dev);
112 struct nx_alive_gpio_regs *const regs = plat->regs;
113 unsigned int mask = (1UL << pin);
116 output = readl(®s->outputenb_read) & mask;
124 static int nx_gpio_direction_input(struct udevice *dev, unsigned int pin)
126 struct nx_gpio_plat *plat = dev_get_plat(dev);
127 struct nx_gpio_regs *const regs = plat->regs;
129 if (nx_alive_gpio_is_check(dev))
130 return nx_alive_gpio_direction_input(dev, pin);
132 clrbits_le32(®s->outputenb, 1 << pin);
137 static int nx_gpio_direction_output(struct udevice *dev, unsigned int pin,
140 struct nx_gpio_plat *plat = dev_get_plat(dev);
141 struct nx_gpio_regs *const regs = plat->regs;
143 if (nx_alive_gpio_is_check(dev))
144 return nx_alive_gpio_direction_output(dev, pin, val);
147 setbits_le32(®s->data, 1 << pin);
149 clrbits_le32(®s->data, 1 << pin);
151 setbits_le32(®s->outputenb, 1 << pin);
156 static int nx_gpio_get_value(struct udevice *dev, unsigned int pin)
158 struct nx_gpio_plat *plat = dev_get_plat(dev);
159 struct nx_gpio_regs *const regs = plat->regs;
160 unsigned int mask = 1UL << pin;
163 if (nx_alive_gpio_is_check(dev))
164 return nx_alive_gpio_get_value(dev, pin);
166 value = (readl(®s->pad) & mask) >> pin;
171 static int nx_gpio_set_value(struct udevice *dev, unsigned int pin, int val)
173 struct nx_gpio_plat *plat = dev_get_plat(dev);
174 struct nx_gpio_regs *const regs = plat->regs;
176 if (nx_alive_gpio_is_check(dev))
177 return nx_alive_gpio_set_value(dev, pin, val);
180 setbits_le32(®s->data, 1 << pin);
182 clrbits_le32(®s->data, 1 << pin);
187 static int nx_gpio_get_function(struct udevice *dev, unsigned int pin)
189 struct nx_gpio_plat *plat = dev_get_plat(dev);
190 struct nx_gpio_regs *const regs = plat->regs;
191 unsigned int mask = (1UL << pin);
194 if (nx_alive_gpio_is_check(dev))
195 return nx_alive_gpio_get_function(dev, pin);
197 output = readl(®s->outputenb) & mask;
205 static int nx_gpio_probe(struct udevice *dev)
207 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
208 struct nx_gpio_plat *plat = dev_get_plat(dev);
210 uc_priv->gpio_count = plat->gpio_count;
211 uc_priv->bank_name = plat->bank_name;
216 static int nx_gpio_of_to_plat(struct udevice *dev)
218 struct nx_gpio_plat *plat = dev_get_plat(dev);
220 plat->regs = map_physmem(devfdt_get_addr(dev),
221 sizeof(struct nx_gpio_regs),
223 plat->gpio_count = dev_read_s32_default(dev, "nexell,gpio-bank-width",
225 plat->bank_name = dev_read_string(dev, "gpio-bank-name");
230 static const struct dm_gpio_ops nx_gpio_ops = {
231 .direction_input = nx_gpio_direction_input,
232 .direction_output = nx_gpio_direction_output,
233 .get_value = nx_gpio_get_value,
234 .set_value = nx_gpio_set_value,
235 .get_function = nx_gpio_get_function,
238 static const struct udevice_id nx_gpio_ids[] = {
239 { .compatible = "nexell,nexell-gpio" },
243 U_BOOT_DRIVER(nx_gpio) = {
246 .of_match = nx_gpio_ids,
248 .of_to_plat = nx_gpio_of_to_plat,
249 .plat_auto = sizeof(struct nx_gpio_plat),
250 .probe = nx_gpio_probe,