1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (c) 2024 Nuvoton Technology Corp.
*/
#include <dm.h>
#include <asm/gpio.h>
#include <linux/io.h>
#define MAX_NR_HW_SGPIO 64
#define NPCM_CLK_MHZ 8000000
#define NPCM_IOXCFG1 0x2A
#define NPCM_IOXCTS 0x28
#define NPCM_IOXCTS_IOXIF_EN BIT(7)
#define NPCM_IOXCTS_RD_MODE GENMASK(2, 1)
#define NPCM_IOXCTS_RD_MODE_PERIODIC BIT(2)
#define NPCM_IOXCFG2 0x2B
#define NPCM_IOXCFG2_PORT GENMASK(3, 0)
#define GPIO_BANK(x) ((x) / 8)
#define GPIO_BIT(x) ((x) % 8)
struct npcm_sgpio_priv {
void __iomem *base;
u32 nin_sgpio;
u32 nout_sgpio;
u32 in_port;
u32 out_port;
};
struct npcm_sgpio_bank {
u8 rdata_reg;
u8 wdata_reg;
u8 event_config;
u8 event_status;
};
enum npcm_sgpio_reg {
READ_DATA,
WRITE_DATA,
EVENT_CFG,
EVENT_STS,
};
static const struct npcm_sgpio_bank npcm_sgpio_banks[] = {
{
.wdata_reg = 0x00,
.rdata_reg = 0x08,
.event_config = 0x10,
.event_status = 0x20,
},
{
.wdata_reg = 0x01,
.rdata_reg = 0x09,
.event_config = 0x12,
.event_status = 0x21,
},
{
.wdata_reg = 0x02,
.rdata_reg = 0x0a,
.event_config = 0x14,
.event_status = 0x22,
},
{
.wdata_reg = 0x03,
.rdata_reg = 0x0b,
.event_config = 0x16,
.event_status = 0x23,
},
{
.wdata_reg = 0x04,
.rdata_reg = 0x0c,
.event_config = 0x18,
.event_status = 0x24,
},
{
.wdata_reg = 0x05,
.rdata_reg = 0x0d,
.event_config = 0x1a,
.event_status = 0x25,
},
{
.wdata_reg = 0x06,
.rdata_reg = 0x0e,
.event_config = 0x1c,
.event_status = 0x26,
},
{
.wdata_reg = 0x07,
.rdata_reg = 0x0f,
.event_config = 0x1e,
.event_status = 0x27,
},
};
static void __iomem *bank_reg(struct npcm_sgpio_priv *gpio,
const struct npcm_sgpio_bank *bank,
const enum npcm_sgpio_reg reg)
{
switch (reg) {
case READ_DATA:
return gpio->base + bank->rdata_reg;
case WRITE_DATA:
return gpio->base + bank->wdata_reg;
case EVENT_CFG:
return gpio->base + bank->event_config;
case EVENT_STS:
return gpio->base + bank->event_status;
default:
/* actually if code runs to here, it's an error case */
printf("Getting here is an error condition\n");
return NULL;
}
}
static const struct npcm_sgpio_bank *offset_to_bank(unsigned int offset)
{
unsigned int bank = GPIO_BANK(offset);
return &npcm_sgpio_banks[bank];
}
static int npcm_sgpio_direction_input(struct udevice *dev, unsigned int offset)
{
struct npcm_sgpio_priv *priv = dev_get_priv(dev);
if (offset < priv->nout_sgpio) {
printf("Error: Offset %d is a output pin\n", offset);
return -EINVAL;
}
return 0;
}
static int npcm_sgpio_direction_output(struct udevice *dev, unsigned int offset,
int value)
{
struct npcm_sgpio_priv *priv = dev_get_priv(dev);
const struct npcm_sgpio_bank *bank = offset_to_bank(offset);
void __iomem *addr;
u8 reg = 0;
if (offset >= priv->nout_sgpio) {
printf("Error: Offset %d is a input pin\n", offset);
return -EINVAL;
}
addr = bank_reg(priv, bank, WRITE_DATA);
reg = ioread8(addr);
if (value)
reg |= BIT(GPIO_BIT(offset));
else
reg &= ~BIT(GPIO_BIT(offset));
iowrite8(reg, addr);
return 0;
}
static int npcm_sgpio_get_value(struct udevice *dev, unsigned int offset)
{
struct npcm_sgpio_priv *priv = dev_get_priv(dev);
const struct npcm_sgpio_bank *bank;
void __iomem *addr;
u8 reg;
if (offset < priv->nout_sgpio) {
bank = offset_to_bank(offset);
addr = bank_reg(priv, bank, WRITE_DATA);
} else {
offset -= priv->nout_sgpio;
bank = offset_to_bank(offset);
addr = bank_reg(priv, bank, READ_DATA);
}
reg = ioread8(addr);
return !!(reg & BIT(GPIO_BIT(offset)));
}
static int npcm_sgpio_set_value(struct udevice *dev, unsigned int offset,
int value)
{
return npcm_sgpio_direction_output(dev, offset, value);
}
static int npcm_sgpio_get_function(struct udevice *dev, unsigned int offset)
{
struct npcm_sgpio_priv *priv = dev_get_priv(dev);
if (offset < priv->nout_sgpio)
return GPIOF_OUTPUT;
return GPIOF_INPUT;
}
static void npcm_sgpio_setup_enable(struct npcm_sgpio_priv *gpio, bool enable)
{
u8 reg;
reg = ioread8(gpio->base + NPCM_IOXCTS);
reg = (reg & ~NPCM_IOXCTS_RD_MODE) | NPCM_IOXCTS_RD_MODE_PERIODIC;
if (enable)
reg |= NPCM_IOXCTS_IOXIF_EN;
else
reg &= ~NPCM_IOXCTS_IOXIF_EN;
iowrite8(reg, gpio->base + NPCM_IOXCTS);
}
static int npcm_sgpio_init_port(struct udevice *dev)
{
struct npcm_sgpio_priv *priv = dev_get_priv(dev);
u8 in_port, out_port, set_port, reg, set_clk;
npcm_sgpio_setup_enable(priv, false);
in_port = GPIO_BANK(priv->nin_sgpio);
if (GPIO_BIT(priv->nin_sgpio) > 0)
in_port += 1;
out_port = GPIO_BANK(priv->nout_sgpio);
if (GPIO_BIT(priv->nout_sgpio) > 0)
out_port += 1;
priv->in_port = in_port;
priv->out_port = out_port;
set_port = (out_port & NPCM_IOXCFG2_PORT) << 4 | (in_port & NPCM_IOXCFG2_PORT);
set_clk = 0x07;
iowrite8(set_port, priv->base + NPCM_IOXCFG2);
iowrite8(set_clk, priv->base + NPCM_IOXCFG1);
reg = ioread8(priv->base + NPCM_IOXCFG2);
return reg == set_port ? 0 : -EINVAL;
}
static const struct dm_gpio_ops npcm_sgpio_ops = {
.direction_input = npcm_sgpio_direction_input,
.direction_output = npcm_sgpio_direction_output,
.get_value = npcm_sgpio_get_value,
.set_value = npcm_sgpio_set_value,
.get_function = npcm_sgpio_get_function,
};
static int npcm_sgpio_probe(struct udevice *dev)
{
struct npcm_sgpio_priv *priv = dev_get_priv(dev);
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
int rc;
priv->base = dev_read_addr_ptr(dev);
ofnode_read_u32(dev_ofnode(dev), "nuvoton,input-ngpios", &priv->nin_sgpio);
ofnode_read_u32(dev_ofnode(dev), "nuvoton,output-ngpios", &priv->nout_sgpio);
if (priv->nin_sgpio > MAX_NR_HW_SGPIO || priv->nout_sgpio > MAX_NR_HW_SGPIO)
return -EINVAL;
rc = npcm_sgpio_init_port(dev);
if (rc < 0)
return rc;
uc_priv->gpio_count = priv->nin_sgpio + priv->nout_sgpio;
uc_priv->bank_name = dev->name;
npcm_sgpio_setup_enable(priv, true);
return 0;
}
static const struct udevice_id npcm_sgpio_match[] = {
{ .compatible = "nuvoton,npcm845-sgpio" },
{ .compatible = "nuvoton,npcm750-sgpio" },
{ }
};
U_BOOT_DRIVER(npcm_sgpio) = {
.name = "npcm_sgpio",
.id = UCLASS_GPIO,
.of_match = npcm_sgpio_match,
.probe = npcm_sgpio_probe,
.priv_auto = sizeof(struct npcm_sgpio_priv),
.ops = &npcm_sgpio_ops,
};
|