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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-26 GuEe-GUI first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "dev_pin_dm.h"
#define PL061_DIR 0x400
#define PL061_IS 0x404
#define PL061_IBE 0x408
#define PL061_IEV 0x40c
#define PL061_IE 0x410
#define PL061_RIS 0x414
#define PL061_MIS 0x418
#define PL061_IC 0x41c
#define PL061_GPIO_NR 8
struct pl061
{
struct rt_device_pin parent;
int irq;
void *base;
struct rt_clk *pclk;
struct rt_spinlock spinlock;
};
#define raw_to_pl061(raw) rt_container_of(raw, struct pl061, parent)
rt_inline rt_uint8_t pl061_read(struct pl061 *pl061, int offset)
{
return HWREG8(pl061->base + offset);
}
rt_inline void pl061_write(struct pl061 *pl061, int offset, rt_uint8_t value)
{
HWREG8(pl061->base + offset) = value;
}
static void pl061_isr(int irqno, void *param)
{
rt_uint8_t mask = 0;
rt_ubase_t pending, level;
struct pl061 *pl061 = (struct pl061 *)param;
level = rt_spin_lock_irqsave(&pl061->spinlock);
pending = pl061_read(pl061, PL061_MIS);
rt_spin_unlock_irqrestore(&pl061->spinlock, level);
if (pending)
{
for (int pin = 0; pin < PL061_GPIO_NR; ++pin)
{
if (pending & RT_BIT(pin))
{
mask |= RT_BIT(pin);
pin_pic_handle_isr(&pl061->parent, pin);
}
}
level = rt_spin_lock_irqsave(&pl061->spinlock);
pl061_write(pl061, PL061_IC, mask);
rt_spin_unlock_irqrestore(&pl061->spinlock, level);
}
}
static void pl061_pin_mode(struct rt_device *device, rt_base_t pin, rt_uint8_t mode)
{
struct pl061 *pl061 = raw_to_pl061(device);
if (pin >= 0 && pin < PL061_GPIO_NR)
{
rt_base_t level = rt_spin_lock_irqsave(&pl061->spinlock);
switch (mode)
{
case PIN_MODE_OUTPUT:
pl061_write(pl061, RT_BIT(pin + 2), 1 << pin);
pl061_write(pl061, PL061_DIR, pl061_read(pl061, PL061_DIR) | RT_BIT(pin));
/*
* gpio value is set again, because pl061 doesn't allow to set value
* of a gpio pin before configuring it in OUT mode.
*/
pl061_write(pl061, RT_BIT(pin + 2), 1 << pin);
break;
case PIN_MODE_INPUT:
pl061_write(pl061, PL061_DIR, pl061_read(pl061, PL061_DIR) & ~RT_BIT(pin));
break;
default:
break;
}
rt_spin_unlock_irqrestore(&pl061->spinlock, level);
}
}
static void pl061_pin_write(struct rt_device *device, rt_base_t pin, rt_uint8_t value)
{
struct pl061 *pl061 = raw_to_pl061(device);
if (pin >= 0 && pin < PL061_GPIO_NR)
{
pl061_write(pl061, RT_BIT(pin + 2), !!value << pin);
}
}
static rt_ssize_t pl061_pin_read(struct rt_device *device, rt_base_t pin)
{
rt_int8_t value = -RT_EINVAL;
struct pl061 *pl061 = raw_to_pl061(device);
if (pin >= 0 && pin < PL061_GPIO_NR)
{
value = !!pl061_read(pl061, RT_BIT(pin + 2));
}
return value;
}
static rt_err_t pl061_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint8_t enabled)
{
rt_err_t err = RT_EOK;
struct pl061 *pl061 = raw_to_pl061(device);
if (pin >= 0 && pin < PL061_GPIO_NR)
{
rt_uint8_t gpioie, mask = RT_BIT(pin);
rt_ubase_t level = rt_spin_lock_irqsave(&pl061->spinlock);
if (enabled)
{
gpioie = pl061_read(pl061, PL061_IE) | mask;
}
else
{
gpioie = pl061_read(pl061, PL061_IE) & ~mask;
}
pl061_write(pl061, PL061_IE, gpioie);
rt_spin_unlock_irqrestore(&pl061->spinlock, level);
}
else
{
err = -RT_EINVAL;
}
return err;
}
static rt_err_t pl061_pin_irq_mode(struct rt_device *device, rt_base_t pin, rt_uint8_t mode)
{
rt_err_t err = RT_EOK;
struct pl061 *pl061 = raw_to_pl061(device);
if (pin >= 0 && pin < PL061_GPIO_NR)
{
rt_uint8_t gpiois, gpioibe, gpioiev, bit = RT_BIT(pin);
rt_ubase_t level = rt_spin_lock_irqsave(&pl061->spinlock);
gpioiev = pl061_read(pl061, PL061_IEV);
gpiois = pl061_read(pl061, PL061_IS);
gpioibe = pl061_read(pl061, PL061_IBE);
if (mode == PIN_IRQ_MODE_HIGH_LEVEL || mode == PIN_IRQ_MODE_LOW_LEVEL)
{
rt_bool_t polarity = (mode == PIN_IRQ_MODE_HIGH_LEVEL);
/* Disable edge detection */
gpioibe &= ~bit;
/* Enable level detection */
gpiois |= bit;
/* Select polarity */
if (polarity)
{
gpioiev |= bit;
}
else
{
gpioiev &= ~bit;
}
}
else if (mode == PIN_IRQ_MODE_RISING_FALLING)
{
/* Disable level detection */
gpiois &= ~bit;
/* Select both edges, setting this makes PL061_EV be ignored */
gpioibe |= bit;
}
else if (mode == PIN_IRQ_MODE_RISING || mode == PIN_IRQ_MODE_FALLING)
{
rt_bool_t rising = (mode == PIN_IRQ_MODE_RISING);
/* Disable level detection */
gpiois &= ~bit;
/* Clear detection on both edges */
gpioibe &= ~bit;
/* Select edge */
if (rising)
{
gpioiev |= bit;
}
else
{
gpioiev &= ~bit;
}
}
else
{
/* No trigger: disable everything */
gpiois &= ~bit;
gpioibe &= ~bit;
gpioiev &= ~bit;
}
pl061_write(pl061, PL061_IS, gpiois);
pl061_write(pl061, PL061_IBE, gpioibe);
pl061_write(pl061, PL061_IEV, gpioiev);
rt_spin_unlock_irqrestore(&pl061->spinlock, level);
}
else
{
err = -RT_EINVAL;
}
return err;
}
static const struct rt_pin_ops pl061_pin_ops =
{
.pin_mode = pl061_pin_mode,
.pin_write = pl061_pin_write,
.pin_read = pl061_pin_read,
.pin_irq_enable = pl061_pin_irq_enable,
.pin_irq_mode = pl061_pin_irq_mode,
};
static rt_err_t pl061_probe(struct rt_platform_device *pdev)
{
rt_err_t err;
struct rt_device *dev = &pdev->parent;
struct pl061 *pl061 = rt_calloc(1, sizeof(*pl061));
if (!pl061)
{
return -RT_ENOMEM;
}
pl061->base = rt_dm_dev_iomap(dev, 0);
if (!pl061->base)
{
err = -RT_EIO;
goto _fail;
}
pl061->irq = rt_dm_dev_get_irq(dev, 0);
if (pl061->irq < 0)
{
err = pl061->irq;
goto _fail;
}
pl061->pclk = rt_clk_get_by_name(dev, "apb_pclk");
if (rt_is_err(pl061->pclk))
{
err = rt_ptr_err(pl061->pclk);
goto _fail;
}
if ((err = rt_clk_prepare_enable(pl061->pclk)))
{
goto _fail;
}
rt_dm_dev_bind_fwdata(dev, RT_NULL, &pl061->parent);
rt_spin_lock_init(&pl061->spinlock);
pl061->parent.ops = &pl061_pin_ops;
pin_api_init(&pl061->parent, PL061_GPIO_NR);
pin_pic_init(&pl061->parent, pl061->irq);
rt_hw_interrupt_install(pl061->irq, pl061_isr, pl061, "gpio-pl061");
rt_hw_interrupt_umask(pl061->irq);
return RT_EOK;
_fail:
if (pl061->base)
{
rt_iounmap(pl061->base);
}
if (!rt_is_err_or_null(pl061->pclk))
{
rt_clk_disable_unprepare(pl061->pclk);
rt_clk_put(pl061->pclk);
}
rt_free(pl061);
return err;
}
static const struct rt_ofw_node_id pl061_ofw_ids[] =
{
{ .compatible = "arm,pl061" },
{ /* sentinel */ }
};
static struct rt_platform_driver pl061_driver =
{
.name = "pin-pl061",
.ids = pl061_ofw_ids,
.probe = pl061_probe,
};
static int pl061_drv_register(void)
{
rt_platform_driver_register(&pl061_driver);
return 0;
}
INIT_SUBSYS_EXPORT(pl061_drv_register);
|