1447976abSJianlong Huang // SPDX-License-Identifier: GPL-2.0
2447976abSJianlong Huang /*
3447976abSJianlong Huang * Pinctrl / GPIO driver for StarFive JH7110 SoC sys controller
4447976abSJianlong Huang *
5447976abSJianlong Huang * Copyright (C) 2022 Emil Renner Berthing <kernel@esmil.dk>
6447976abSJianlong Huang * Copyright (C) 2022 StarFive Technology Co., Ltd.
7447976abSJianlong Huang */
8447976abSJianlong Huang
9447976abSJianlong Huang #include <linux/bits.h>
10447976abSJianlong Huang #include <linux/clk.h>
11447976abSJianlong Huang #include <linux/gpio/driver.h>
12447976abSJianlong Huang #include <linux/io.h>
13447976abSJianlong Huang #include <linux/mod_devicetable.h>
14447976abSJianlong Huang #include <linux/module.h>
15447976abSJianlong Huang #include <linux/mutex.h>
16447976abSJianlong Huang #include <linux/platform_device.h>
17447976abSJianlong Huang #include <linux/reset.h>
18447976abSJianlong Huang #include <linux/spinlock.h>
19447976abSJianlong Huang
20447976abSJianlong Huang #include <linux/pinctrl/pinctrl.h>
21447976abSJianlong Huang #include <linux/pinctrl/pinmux.h>
22447976abSJianlong Huang
23447976abSJianlong Huang #include <dt-bindings/pinctrl/starfive,jh7110-pinctrl.h>
24447976abSJianlong Huang
25447976abSJianlong Huang #include "../core.h"
26447976abSJianlong Huang #include "../pinctrl-utils.h"
27447976abSJianlong Huang #include "../pinmux.h"
28447976abSJianlong Huang #include "../pinconf.h"
29447976abSJianlong Huang #include "pinctrl-starfive-jh7110.h"
30447976abSJianlong Huang
31447976abSJianlong Huang #define JH7110_SYS_NGPIO 64
32447976abSJianlong Huang #define JH7110_SYS_GC_BASE 0
33447976abSJianlong Huang
34*64061b67SHal Feng #define JH7110_SYS_REGS_NUM 174
35*64061b67SHal Feng
36447976abSJianlong Huang /* registers */
37447976abSJianlong Huang #define JH7110_SYS_DOEN 0x000
38447976abSJianlong Huang #define JH7110_SYS_DOUT 0x040
39447976abSJianlong Huang #define JH7110_SYS_GPI 0x080
40447976abSJianlong Huang #define JH7110_SYS_GPIOIN 0x118
41447976abSJianlong Huang
42447976abSJianlong Huang #define JH7110_SYS_GPIOEN 0x0dc
43447976abSJianlong Huang #define JH7110_SYS_GPIOIS0 0x0e0
44447976abSJianlong Huang #define JH7110_SYS_GPIOIS1 0x0e4
45447976abSJianlong Huang #define JH7110_SYS_GPIOIC0 0x0e8
46447976abSJianlong Huang #define JH7110_SYS_GPIOIC1 0x0ec
47447976abSJianlong Huang #define JH7110_SYS_GPIOIBE0 0x0f0
48447976abSJianlong Huang #define JH7110_SYS_GPIOIBE1 0x0f4
49447976abSJianlong Huang #define JH7110_SYS_GPIOIEV0 0x0f8
50447976abSJianlong Huang #define JH7110_SYS_GPIOIEV1 0x0fc
51447976abSJianlong Huang #define JH7110_SYS_GPIOIE0 0x100
52447976abSJianlong Huang #define JH7110_SYS_GPIOIE1 0x104
53447976abSJianlong Huang #define JH7110_SYS_GPIORIS0 0x108
54447976abSJianlong Huang #define JH7110_SYS_GPIORIS1 0x10c
55447976abSJianlong Huang #define JH7110_SYS_GPIOMIS0 0x110
56447976abSJianlong Huang #define JH7110_SYS_GPIOMIS1 0x114
57447976abSJianlong Huang
58447976abSJianlong Huang #define JH7110_SYS_GPO_PDA_0_74_CFG 0x120
59447976abSJianlong Huang #define JH7110_SYS_GPO_PDA_89_94_CFG 0x284
60447976abSJianlong Huang
61447976abSJianlong Huang static const struct pinctrl_pin_desc jh7110_sys_pins[] = {
62447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO0, "GPIO0"),
63447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO1, "GPIO1"),
64447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO2, "GPIO2"),
65447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO3, "GPIO3"),
66447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO4, "GPIO4"),
67447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO5, "GPIO5"),
68447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO6, "GPIO6"),
69447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO7, "GPIO7"),
70447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO8, "GPIO8"),
71447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO9, "GPIO9"),
72447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO10, "GPIO10"),
73447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO11, "GPIO11"),
74447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO12, "GPIO12"),
75447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO13, "GPIO13"),
76447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO14, "GPIO14"),
77447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO15, "GPIO15"),
78447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO16, "GPIO16"),
79447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO17, "GPIO17"),
80447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO18, "GPIO18"),
81447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO19, "GPIO19"),
82447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO20, "GPIO20"),
83447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO21, "GPIO21"),
84447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO22, "GPIO22"),
85447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO23, "GPIO23"),
86447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO24, "GPIO24"),
87447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO25, "GPIO25"),
88447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO26, "GPIO26"),
89447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO27, "GPIO27"),
90447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO28, "GPIO28"),
91447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO29, "GPIO29"),
92447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO30, "GPIO30"),
93447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO31, "GPIO31"),
94447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO32, "GPIO32"),
95447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO33, "GPIO33"),
96447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO34, "GPIO34"),
97447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO35, "GPIO35"),
98447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO36, "GPIO36"),
99447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO37, "GPIO37"),
100447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO38, "GPIO38"),
101447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO39, "GPIO39"),
102447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO40, "GPIO40"),
103447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO41, "GPIO41"),
104447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO42, "GPIO42"),
105447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO43, "GPIO43"),
106447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO44, "GPIO44"),
107447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO45, "GPIO45"),
108447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO46, "GPIO46"),
109447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO47, "GPIO47"),
110447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO48, "GPIO48"),
111447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO49, "GPIO49"),
112447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO50, "GPIO50"),
113447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO51, "GPIO51"),
114447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO52, "GPIO52"),
115447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO53, "GPIO53"),
116447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO54, "GPIO54"),
117447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO55, "GPIO55"),
118447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO56, "GPIO56"),
119447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO57, "GPIO57"),
120447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO58, "GPIO58"),
121447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO59, "GPIO59"),
122447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO60, "GPIO60"),
123447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO61, "GPIO61"),
124447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO62, "GPIO62"),
125447976abSJianlong Huang PINCTRL_PIN(PAD_GPIO63, "GPIO63"),
126447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_CLK, "SD0_CLK"),
127447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_CMD, "SD0_CMD"),
128447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_DATA0, "SD0_DATA0"),
129447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_DATA1, "SD0_DATA1"),
130447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_DATA2, "SD0_DATA2"),
131447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_DATA3, "SD0_DATA3"),
132447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_DATA4, "SD0_DATA4"),
133447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_DATA5, "SD0_DATA5"),
134447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_DATA6, "SD0_DATA6"),
135447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_DATA7, "SD0_DATA7"),
136447976abSJianlong Huang PINCTRL_PIN(PAD_SD0_STRB, "SD0_STRB"),
137447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_MDC, "GMAC1_MDC"),
138447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_MDIO, "GMAC1_MDIO"),
139447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_RXD0, "GMAC1_RXD0"),
140447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_RXD1, "GMAC1_RXD1"),
141447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_RXD2, "GMAC1_RXD2"),
142447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_RXD3, "GMAC1_RXD3"),
143447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_RXDV, "GMAC1_RXDV"),
144447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_RXC, "GMAC1_RXC"),
145447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_TXD0, "GMAC1_TXD0"),
146447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_TXD1, "GMAC1_TXD1"),
147447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_TXD2, "GMAC1_TXD2"),
148447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_TXD3, "GMAC1_TXD3"),
149447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_TXEN, "GMAC1_TXEN"),
150447976abSJianlong Huang PINCTRL_PIN(PAD_GMAC1_TXC, "GMAC1_TXC"),
151447976abSJianlong Huang PINCTRL_PIN(PAD_QSPI_SCLK, "QSPI_SCLK"),
152447976abSJianlong Huang PINCTRL_PIN(PAD_QSPI_CS0, "QSPI_CS0"),
153447976abSJianlong Huang PINCTRL_PIN(PAD_QSPI_DATA0, "QSPI_DATA0"),
154447976abSJianlong Huang PINCTRL_PIN(PAD_QSPI_DATA1, "QSPI_DATA1"),
155447976abSJianlong Huang PINCTRL_PIN(PAD_QSPI_DATA2, "QSPI_DATA2"),
156447976abSJianlong Huang PINCTRL_PIN(PAD_QSPI_DATA3, "QSPI_DATA3"),
157447976abSJianlong Huang };
158447976abSJianlong Huang
159447976abSJianlong Huang struct jh7110_func_sel {
160447976abSJianlong Huang u16 offset;
161447976abSJianlong Huang u8 shift;
162447976abSJianlong Huang u8 max;
163447976abSJianlong Huang };
164447976abSJianlong Huang
165447976abSJianlong Huang static const struct jh7110_func_sel
166447976abSJianlong Huang jh7110_sys_func_sel[ARRAY_SIZE(jh7110_sys_pins)] = {
167447976abSJianlong Huang [PAD_GMAC1_RXC] = { 0x29c, 0, 1 },
168447976abSJianlong Huang [PAD_GPIO10] = { 0x29c, 2, 3 },
169447976abSJianlong Huang [PAD_GPIO11] = { 0x29c, 5, 3 },
170447976abSJianlong Huang [PAD_GPIO12] = { 0x29c, 8, 3 },
171447976abSJianlong Huang [PAD_GPIO13] = { 0x29c, 11, 3 },
172447976abSJianlong Huang [PAD_GPIO14] = { 0x29c, 14, 3 },
173447976abSJianlong Huang [PAD_GPIO15] = { 0x29c, 17, 3 },
174447976abSJianlong Huang [PAD_GPIO16] = { 0x29c, 20, 3 },
175447976abSJianlong Huang [PAD_GPIO17] = { 0x29c, 23, 3 },
176447976abSJianlong Huang [PAD_GPIO18] = { 0x29c, 26, 3 },
177447976abSJianlong Huang [PAD_GPIO19] = { 0x29c, 29, 3 },
178447976abSJianlong Huang
179447976abSJianlong Huang [PAD_GPIO20] = { 0x2a0, 0, 3 },
180447976abSJianlong Huang [PAD_GPIO21] = { 0x2a0, 3, 3 },
181447976abSJianlong Huang [PAD_GPIO22] = { 0x2a0, 6, 3 },
182447976abSJianlong Huang [PAD_GPIO23] = { 0x2a0, 9, 3 },
183447976abSJianlong Huang [PAD_GPIO24] = { 0x2a0, 12, 3 },
184447976abSJianlong Huang [PAD_GPIO25] = { 0x2a0, 15, 3 },
185447976abSJianlong Huang [PAD_GPIO26] = { 0x2a0, 18, 3 },
186447976abSJianlong Huang [PAD_GPIO27] = { 0x2a0, 21, 3 },
187447976abSJianlong Huang [PAD_GPIO28] = { 0x2a0, 24, 3 },
188447976abSJianlong Huang [PAD_GPIO29] = { 0x2a0, 27, 3 },
189447976abSJianlong Huang
190447976abSJianlong Huang [PAD_GPIO30] = { 0x2a4, 0, 3 },
191447976abSJianlong Huang [PAD_GPIO31] = { 0x2a4, 3, 3 },
192447976abSJianlong Huang [PAD_GPIO32] = { 0x2a4, 6, 3 },
193447976abSJianlong Huang [PAD_GPIO33] = { 0x2a4, 9, 3 },
194447976abSJianlong Huang [PAD_GPIO34] = { 0x2a4, 12, 3 },
195447976abSJianlong Huang [PAD_GPIO35] = { 0x2a4, 15, 3 },
196447976abSJianlong Huang [PAD_GPIO36] = { 0x2a4, 17, 3 },
197447976abSJianlong Huang [PAD_GPIO37] = { 0x2a4, 20, 3 },
198447976abSJianlong Huang [PAD_GPIO38] = { 0x2a4, 23, 3 },
199447976abSJianlong Huang [PAD_GPIO39] = { 0x2a4, 26, 3 },
200447976abSJianlong Huang [PAD_GPIO40] = { 0x2a4, 29, 3 },
201447976abSJianlong Huang
202447976abSJianlong Huang [PAD_GPIO41] = { 0x2a8, 0, 3 },
203447976abSJianlong Huang [PAD_GPIO42] = { 0x2a8, 3, 3 },
204447976abSJianlong Huang [PAD_GPIO43] = { 0x2a8, 6, 3 },
205447976abSJianlong Huang [PAD_GPIO44] = { 0x2a8, 9, 3 },
206447976abSJianlong Huang [PAD_GPIO45] = { 0x2a8, 12, 3 },
207447976abSJianlong Huang [PAD_GPIO46] = { 0x2a8, 15, 3 },
208447976abSJianlong Huang [PAD_GPIO47] = { 0x2a8, 18, 3 },
209447976abSJianlong Huang [PAD_GPIO48] = { 0x2a8, 21, 3 },
210447976abSJianlong Huang [PAD_GPIO49] = { 0x2a8, 24, 3 },
211447976abSJianlong Huang [PAD_GPIO50] = { 0x2a8, 27, 3 },
212447976abSJianlong Huang [PAD_GPIO51] = { 0x2a8, 30, 3 },
213447976abSJianlong Huang
214447976abSJianlong Huang [PAD_GPIO52] = { 0x2ac, 0, 3 },
215447976abSJianlong Huang [PAD_GPIO53] = { 0x2ac, 2, 3 },
216447976abSJianlong Huang [PAD_GPIO54] = { 0x2ac, 4, 3 },
217447976abSJianlong Huang [PAD_GPIO55] = { 0x2ac, 6, 3 },
218447976abSJianlong Huang [PAD_GPIO56] = { 0x2ac, 9, 3 },
219447976abSJianlong Huang [PAD_GPIO57] = { 0x2ac, 12, 3 },
220447976abSJianlong Huang [PAD_GPIO58] = { 0x2ac, 15, 3 },
221447976abSJianlong Huang [PAD_GPIO59] = { 0x2ac, 18, 3 },
222447976abSJianlong Huang [PAD_GPIO60] = { 0x2ac, 21, 3 },
223447976abSJianlong Huang [PAD_GPIO61] = { 0x2ac, 24, 3 },
224447976abSJianlong Huang [PAD_GPIO62] = { 0x2ac, 27, 3 },
225447976abSJianlong Huang [PAD_GPIO63] = { 0x2ac, 30, 3 },
226447976abSJianlong Huang
227447976abSJianlong Huang [PAD_GPIO6] = { 0x2b0, 0, 3 },
228447976abSJianlong Huang [PAD_GPIO7] = { 0x2b0, 2, 3 },
229447976abSJianlong Huang [PAD_GPIO8] = { 0x2b0, 5, 3 },
230447976abSJianlong Huang [PAD_GPIO9] = { 0x2b0, 8, 3 },
231447976abSJianlong Huang };
232447976abSJianlong Huang
233447976abSJianlong Huang struct jh7110_vin_group_sel {
234447976abSJianlong Huang u16 offset;
235447976abSJianlong Huang u8 shift;
236447976abSJianlong Huang u8 group;
237447976abSJianlong Huang };
238447976abSJianlong Huang
239447976abSJianlong Huang static const struct jh7110_vin_group_sel
240447976abSJianlong Huang jh7110_sys_vin_group_sel[ARRAY_SIZE(jh7110_sys_pins)] = {
241447976abSJianlong Huang [PAD_GPIO6] = { 0x2b4, 21, 0 },
242447976abSJianlong Huang [PAD_GPIO7] = { 0x2b4, 18, 0 },
243447976abSJianlong Huang [PAD_GPIO8] = { 0x2b4, 15, 0 },
244447976abSJianlong Huang [PAD_GPIO9] = { 0x2b0, 11, 0 },
245447976abSJianlong Huang [PAD_GPIO10] = { 0x2b0, 20, 0 },
246447976abSJianlong Huang [PAD_GPIO11] = { 0x2b0, 23, 0 },
247447976abSJianlong Huang [PAD_GPIO12] = { 0x2b0, 26, 0 },
248447976abSJianlong Huang [PAD_GPIO13] = { 0x2b0, 29, 0 },
249447976abSJianlong Huang [PAD_GPIO14] = { 0x2b4, 0, 0 },
250447976abSJianlong Huang [PAD_GPIO15] = { 0x2b4, 3, 0 },
251447976abSJianlong Huang [PAD_GPIO16] = { 0x2b4, 6, 0 },
252447976abSJianlong Huang [PAD_GPIO17] = { 0x2b4, 9, 0 },
253447976abSJianlong Huang [PAD_GPIO18] = { 0x2b4, 12, 0 },
254447976abSJianlong Huang [PAD_GPIO19] = { 0x2b0, 14, 0 },
255447976abSJianlong Huang [PAD_GPIO20] = { 0x2b0, 17, 0 },
256447976abSJianlong Huang
257447976abSJianlong Huang [PAD_GPIO21] = { 0x2b4, 21, 1 },
258447976abSJianlong Huang [PAD_GPIO22] = { 0x2b4, 18, 1 },
259447976abSJianlong Huang [PAD_GPIO23] = { 0x2b4, 15, 1 },
260447976abSJianlong Huang [PAD_GPIO24] = { 0x2b0, 11, 1 },
261447976abSJianlong Huang [PAD_GPIO25] = { 0x2b0, 20, 1 },
262447976abSJianlong Huang [PAD_GPIO26] = { 0x2b0, 23, 1 },
263447976abSJianlong Huang [PAD_GPIO27] = { 0x2b0, 26, 1 },
264447976abSJianlong Huang [PAD_GPIO28] = { 0x2b0, 29, 1 },
265447976abSJianlong Huang [PAD_GPIO29] = { 0x2b4, 0, 1 },
266447976abSJianlong Huang [PAD_GPIO30] = { 0x2b4, 3, 1 },
267447976abSJianlong Huang [PAD_GPIO31] = { 0x2b4, 6, 1 },
268447976abSJianlong Huang [PAD_GPIO32] = { 0x2b4, 9, 1 },
269447976abSJianlong Huang [PAD_GPIO33] = { 0x2b4, 12, 1 },
270447976abSJianlong Huang [PAD_GPIO34] = { 0x2b0, 14, 1 },
271447976abSJianlong Huang [PAD_GPIO35] = { 0x2b0, 17, 1 },
272447976abSJianlong Huang
273447976abSJianlong Huang [PAD_GPIO36] = { 0x2b4, 21, 2 },
274447976abSJianlong Huang [PAD_GPIO37] = { 0x2b4, 18, 2 },
275447976abSJianlong Huang [PAD_GPIO38] = { 0x2b4, 15, 2 },
276447976abSJianlong Huang [PAD_GPIO39] = { 0x2b0, 11, 2 },
277447976abSJianlong Huang [PAD_GPIO40] = { 0x2b0, 20, 2 },
278447976abSJianlong Huang [PAD_GPIO41] = { 0x2b0, 23, 2 },
279447976abSJianlong Huang [PAD_GPIO42] = { 0x2b0, 26, 2 },
280447976abSJianlong Huang [PAD_GPIO43] = { 0x2b0, 29, 2 },
281447976abSJianlong Huang [PAD_GPIO44] = { 0x2b4, 0, 2 },
282447976abSJianlong Huang [PAD_GPIO45] = { 0x2b4, 3, 2 },
283447976abSJianlong Huang [PAD_GPIO46] = { 0x2b4, 6, 2 },
284447976abSJianlong Huang [PAD_GPIO47] = { 0x2b4, 9, 2 },
285447976abSJianlong Huang [PAD_GPIO48] = { 0x2b4, 12, 2 },
286447976abSJianlong Huang [PAD_GPIO49] = { 0x2b0, 14, 2 },
287447976abSJianlong Huang [PAD_GPIO50] = { 0x2b0, 17, 2 },
288447976abSJianlong Huang };
289447976abSJianlong Huang
jh7110_set_function(struct jh7110_pinctrl * sfp,unsigned int pin,u32 func)290447976abSJianlong Huang static void jh7110_set_function(struct jh7110_pinctrl *sfp,
291447976abSJianlong Huang unsigned int pin, u32 func)
292447976abSJianlong Huang {
293447976abSJianlong Huang const struct jh7110_func_sel *fs = &jh7110_sys_func_sel[pin];
294447976abSJianlong Huang unsigned long flags;
295447976abSJianlong Huang void __iomem *reg;
296447976abSJianlong Huang u32 mask;
297447976abSJianlong Huang
298447976abSJianlong Huang if (!fs->offset)
299447976abSJianlong Huang return;
300447976abSJianlong Huang
301447976abSJianlong Huang if (func > fs->max)
302447976abSJianlong Huang return;
303447976abSJianlong Huang
304447976abSJianlong Huang reg = sfp->base + fs->offset;
305447976abSJianlong Huang func = func << fs->shift;
306447976abSJianlong Huang mask = 0x3U << fs->shift;
307447976abSJianlong Huang
308447976abSJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
309447976abSJianlong Huang func |= readl_relaxed(reg) & ~mask;
310447976abSJianlong Huang writel_relaxed(func, reg);
311447976abSJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
312447976abSJianlong Huang }
313447976abSJianlong Huang
jh7110_set_vin_group(struct jh7110_pinctrl * sfp,unsigned int pin)314447976abSJianlong Huang static void jh7110_set_vin_group(struct jh7110_pinctrl *sfp,
315447976abSJianlong Huang unsigned int pin)
316447976abSJianlong Huang {
317447976abSJianlong Huang const struct jh7110_vin_group_sel *gs = &jh7110_sys_vin_group_sel[pin];
318447976abSJianlong Huang unsigned long flags;
319447976abSJianlong Huang void __iomem *reg;
320447976abSJianlong Huang u32 mask;
321447976abSJianlong Huang u32 grp;
322447976abSJianlong Huang
323447976abSJianlong Huang if (!gs->offset)
324447976abSJianlong Huang return;
325447976abSJianlong Huang
326447976abSJianlong Huang reg = sfp->base + gs->offset;
327447976abSJianlong Huang grp = gs->group << gs->shift;
328447976abSJianlong Huang mask = 0x3U << gs->shift;
329447976abSJianlong Huang
330447976abSJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
331447976abSJianlong Huang grp |= readl_relaxed(reg) & ~mask;
332447976abSJianlong Huang writel_relaxed(grp, reg);
333447976abSJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
334447976abSJianlong Huang }
335447976abSJianlong Huang
jh7110_sys_set_one_pin_mux(struct jh7110_pinctrl * sfp,unsigned int pin,unsigned int din,u32 dout,u32 doen,u32 func)336447976abSJianlong Huang static int jh7110_sys_set_one_pin_mux(struct jh7110_pinctrl *sfp,
337447976abSJianlong Huang unsigned int pin,
338447976abSJianlong Huang unsigned int din, u32 dout,
339447976abSJianlong Huang u32 doen, u32 func)
340447976abSJianlong Huang {
341447976abSJianlong Huang if (pin < sfp->gc.ngpio && func == 0)
342447976abSJianlong Huang jh7110_set_gpiomux(sfp, pin, din, dout, doen);
343447976abSJianlong Huang
344447976abSJianlong Huang jh7110_set_function(sfp, pin, func);
345447976abSJianlong Huang
346447976abSJianlong Huang if (pin < sfp->gc.ngpio && func == 2)
347447976abSJianlong Huang jh7110_set_vin_group(sfp, pin);
348447976abSJianlong Huang
349447976abSJianlong Huang return 0;
350447976abSJianlong Huang }
351447976abSJianlong Huang
jh7110_sys_get_padcfg_base(struct jh7110_pinctrl * sfp,unsigned int pin)352447976abSJianlong Huang static int jh7110_sys_get_padcfg_base(struct jh7110_pinctrl *sfp,
353447976abSJianlong Huang unsigned int pin)
354447976abSJianlong Huang {
355447976abSJianlong Huang if (pin < PAD_GMAC1_MDC)
356447976abSJianlong Huang return JH7110_SYS_GPO_PDA_0_74_CFG;
357447976abSJianlong Huang else if (pin > PAD_GMAC1_TXC && pin <= PAD_QSPI_DATA3)
358447976abSJianlong Huang return JH7110_SYS_GPO_PDA_89_94_CFG;
359447976abSJianlong Huang else
360447976abSJianlong Huang return -1;
361447976abSJianlong Huang }
362447976abSJianlong Huang
jh7110_sys_irq_handler(struct irq_desc * desc)363447976abSJianlong Huang static void jh7110_sys_irq_handler(struct irq_desc *desc)
364447976abSJianlong Huang {
365447976abSJianlong Huang struct jh7110_pinctrl *sfp = jh7110_from_irq_desc(desc);
366447976abSJianlong Huang struct irq_chip *chip = irq_desc_get_chip(desc);
367447976abSJianlong Huang unsigned long mis;
368447976abSJianlong Huang unsigned int pin;
369447976abSJianlong Huang
370447976abSJianlong Huang chained_irq_enter(chip, desc);
371447976abSJianlong Huang
372447976abSJianlong Huang mis = readl_relaxed(sfp->base + JH7110_SYS_GPIOMIS0);
373447976abSJianlong Huang for_each_set_bit(pin, &mis, 32)
374447976abSJianlong Huang generic_handle_domain_irq(sfp->gc.irq.domain, pin);
375447976abSJianlong Huang
376447976abSJianlong Huang mis = readl_relaxed(sfp->base + JH7110_SYS_GPIOMIS1);
377447976abSJianlong Huang for_each_set_bit(pin, &mis, 32)
378447976abSJianlong Huang generic_handle_domain_irq(sfp->gc.irq.domain, pin + 32);
379447976abSJianlong Huang
380447976abSJianlong Huang chained_irq_exit(chip, desc);
381447976abSJianlong Huang }
382447976abSJianlong Huang
jh7110_sys_init_hw(struct gpio_chip * gc)383447976abSJianlong Huang static int jh7110_sys_init_hw(struct gpio_chip *gc)
384447976abSJianlong Huang {
385447976abSJianlong Huang struct jh7110_pinctrl *sfp = container_of(gc,
386447976abSJianlong Huang struct jh7110_pinctrl, gc);
387447976abSJianlong Huang
388447976abSJianlong Huang /* mask all GPIO interrupts */
389447976abSJianlong Huang writel(0U, sfp->base + JH7110_SYS_GPIOIE0);
390447976abSJianlong Huang writel(0U, sfp->base + JH7110_SYS_GPIOIE1);
391447976abSJianlong Huang /* clear edge interrupt flags */
392447976abSJianlong Huang writel(~0U, sfp->base + JH7110_SYS_GPIOIC0);
393447976abSJianlong Huang writel(~0U, sfp->base + JH7110_SYS_GPIOIC1);
394447976abSJianlong Huang /* enable GPIO interrupts */
395447976abSJianlong Huang writel(1U, sfp->base + JH7110_SYS_GPIOEN);
396447976abSJianlong Huang return 0;
397447976abSJianlong Huang }
398447976abSJianlong Huang
399447976abSJianlong Huang static const struct jh7110_gpio_irq_reg jh7110_sys_irq_reg = {
400447976abSJianlong Huang .is_reg_base = JH7110_SYS_GPIOIS0,
401447976abSJianlong Huang .ic_reg_base = JH7110_SYS_GPIOIC0,
402447976abSJianlong Huang .ibe_reg_base = JH7110_SYS_GPIOIBE0,
403447976abSJianlong Huang .iev_reg_base = JH7110_SYS_GPIOIEV0,
404447976abSJianlong Huang .ie_reg_base = JH7110_SYS_GPIOIE0,
405447976abSJianlong Huang .ris_reg_base = JH7110_SYS_GPIORIS0,
406447976abSJianlong Huang .mis_reg_base = JH7110_SYS_GPIOMIS0,
407447976abSJianlong Huang };
408447976abSJianlong Huang
409447976abSJianlong Huang static const struct jh7110_pinctrl_soc_info jh7110_sys_pinctrl_info = {
410447976abSJianlong Huang .pins = jh7110_sys_pins,
411447976abSJianlong Huang .npins = ARRAY_SIZE(jh7110_sys_pins),
412447976abSJianlong Huang .ngpios = JH7110_SYS_NGPIO,
413447976abSJianlong Huang .gc_base = JH7110_SYS_GC_BASE,
414447976abSJianlong Huang .dout_reg_base = JH7110_SYS_DOUT,
415447976abSJianlong Huang .dout_mask = GENMASK(6, 0),
416447976abSJianlong Huang .doen_reg_base = JH7110_SYS_DOEN,
417447976abSJianlong Huang .doen_mask = GENMASK(5, 0),
418447976abSJianlong Huang .gpi_reg_base = JH7110_SYS_GPI,
419447976abSJianlong Huang .gpi_mask = GENMASK(6, 0),
420447976abSJianlong Huang .gpioin_reg_base = JH7110_SYS_GPIOIN,
421447976abSJianlong Huang .irq_reg = &jh7110_sys_irq_reg,
422*64061b67SHal Feng .nsaved_regs = JH7110_SYS_REGS_NUM,
423447976abSJianlong Huang .jh7110_set_one_pin_mux = jh7110_sys_set_one_pin_mux,
424447976abSJianlong Huang .jh7110_get_padcfg_base = jh7110_sys_get_padcfg_base,
425447976abSJianlong Huang .jh7110_gpio_irq_handler = jh7110_sys_irq_handler,
426447976abSJianlong Huang .jh7110_gpio_init_hw = jh7110_sys_init_hw,
427447976abSJianlong Huang };
428447976abSJianlong Huang
429447976abSJianlong Huang static const struct of_device_id jh7110_sys_pinctrl_of_match[] = {
430447976abSJianlong Huang {
431447976abSJianlong Huang .compatible = "starfive,jh7110-sys-pinctrl",
432447976abSJianlong Huang .data = &jh7110_sys_pinctrl_info,
433447976abSJianlong Huang },
434447976abSJianlong Huang { /* sentinel */ }
435447976abSJianlong Huang };
436447976abSJianlong Huang MODULE_DEVICE_TABLE(of, jh7110_sys_pinctrl_of_match);
437447976abSJianlong Huang
438447976abSJianlong Huang static struct platform_driver jh7110_sys_pinctrl_driver = {
439447976abSJianlong Huang .probe = jh7110_pinctrl_probe,
440447976abSJianlong Huang .driver = {
441447976abSJianlong Huang .name = "starfive-jh7110-sys-pinctrl",
442447976abSJianlong Huang .of_match_table = jh7110_sys_pinctrl_of_match,
443*64061b67SHal Feng .pm = pm_sleep_ptr(&jh7110_pinctrl_pm_ops),
444447976abSJianlong Huang },
445447976abSJianlong Huang };
446447976abSJianlong Huang module_platform_driver(jh7110_sys_pinctrl_driver);
447447976abSJianlong Huang
448447976abSJianlong Huang MODULE_DESCRIPTION("Pinctrl driver for the StarFive JH7110 SoC sys controller");
449447976abSJianlong Huang MODULE_AUTHOR("Emil Renner Berthing <kernel@esmil.dk>");
450447976abSJianlong Huang MODULE_AUTHOR("Jianlong Huang <jianlong.huang@starfivetech.com>");
451447976abSJianlong Huang MODULE_LICENSE("GPL");
452