common: Drop linux/bitops.h from common header
[platform/kernel/u-boot.git] / board / keymile / common / qrio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2013 Keymile AG
4  * Valentin Longchamp <valentin.longchamp@keymile.com>
5  */
6
7 #include <common.h>
8 #include <linux/bitops.h>
9
10 #include "common.h"
11 #include "qrio.h"
12
13 /* QRIO GPIO register offsets */
14 #define DIRECT_OFF              0x18
15 #define GPRT_OFF                0x1c
16
17 int qrio_get_gpio(u8 port_off, u8 gpio_nr)
18 {
19         u32 gprt;
20
21         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
22
23         gprt = in_be32(qrio_base + port_off + GPRT_OFF);
24
25         return (gprt >> gpio_nr) & 1U;
26 }
27
28 void qrio_set_gpio(u8 port_off, u8 gpio_nr, bool value)
29 {
30         u32 gprt, mask;
31
32         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
33
34         mask = 1U << gpio_nr;
35
36         gprt = in_be32(qrio_base + port_off + GPRT_OFF);
37         if (value)
38                 gprt |= mask;
39         else
40                 gprt &= ~mask;
41
42         out_be32(qrio_base + port_off + GPRT_OFF, gprt);
43 }
44
45 void qrio_gpio_direction_output(u8 port_off, u8 gpio_nr, bool value)
46 {
47         u32 direct, mask;
48
49         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
50
51         mask = 1U << gpio_nr;
52
53         direct = in_be32(qrio_base + port_off + DIRECT_OFF);
54         direct |= mask;
55         out_be32(qrio_base + port_off + DIRECT_OFF, direct);
56
57         qrio_set_gpio(port_off, gpio_nr, value);
58 }
59
60 void qrio_gpio_direction_input(u8 port_off, u8 gpio_nr)
61 {
62         u32 direct, mask;
63
64         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
65
66         mask = 1U << gpio_nr;
67
68         direct = in_be32(qrio_base + port_off + DIRECT_OFF);
69         direct &= ~mask;
70         out_be32(qrio_base + port_off + DIRECT_OFF, direct);
71 }
72
73 void qrio_set_opendrain_gpio(u8 port_off, u8 gpio_nr, u8 val)
74 {
75         u32 direct, mask;
76
77         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
78
79         mask = 1U << gpio_nr;
80
81         direct = in_be32(qrio_base + port_off + DIRECT_OFF);
82         if (val == 0)
83                 /* set to output -> GPIO drives low */
84                 direct |= mask;
85         else
86                 /* set to input -> GPIO floating */
87                 direct &= ~mask;
88
89         out_be32(qrio_base + port_off + DIRECT_OFF, direct);
90 }
91
92 #define WDMASK_OFF      0x16
93
94 void qrio_wdmask(u8 bit, bool wden)
95 {
96         u16 wdmask;
97         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
98
99         wdmask = in_be16(qrio_base + WDMASK_OFF);
100
101         if (wden)
102                 wdmask |= (1 << bit);
103         else
104                 wdmask &= ~(1 << bit);
105
106         out_be16(qrio_base + WDMASK_OFF, wdmask);
107 }
108
109 #define PRST_OFF        0x1a
110
111 void qrio_prst(u8 bit, bool en, bool wden)
112 {
113         u16 prst;
114         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
115
116         qrio_wdmask(bit, wden);
117
118         prst = in_be16(qrio_base + PRST_OFF);
119
120         if (en)
121                 prst &= ~(1 << bit);
122         else
123                 prst |= (1 << bit);
124
125         out_be16(qrio_base + PRST_OFF, prst);
126 }
127
128 #define PRSTCFG_OFF     0x1c
129
130 void qrio_prstcfg(u8 bit, u8 mode)
131 {
132         u32 prstcfg;
133         u8 i;
134         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
135
136         prstcfg = in_be32(qrio_base + PRSTCFG_OFF);
137
138         for (i = 0; i < 2; i++) {
139                 if (mode & (1 << i))
140                         set_bit(2 * bit + i, &prstcfg);
141                 else
142                         clear_bit(2 * bit + i, &prstcfg);
143         }
144
145         out_be32(qrio_base + PRSTCFG_OFF, prstcfg);
146 }
147
148 #define CTRLH_OFF               0x02
149 #define CTRLH_WRL_BOOT          0x01
150 #define CTRLH_WRL_UNITRUN       0x02
151
152 void qrio_set_leds(void)
153 {
154         u8 ctrlh;
155         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
156
157         /* set UNIT LED to RED and BOOT LED to ON */
158         ctrlh = in_8(qrio_base + CTRLH_OFF);
159         ctrlh |= (CTRLH_WRL_BOOT | CTRLH_WRL_UNITRUN);
160         out_8(qrio_base + CTRLH_OFF, ctrlh);
161 }
162
163 #define CTRLL_OFF               0x03
164 #define CTRLL_WRB_BUFENA        0x20
165
166 void qrio_enable_app_buffer(void)
167 {
168         u8 ctrll;
169         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
170
171         /* enable application buffer */
172         ctrll = in_8(qrio_base + CTRLL_OFF);
173         ctrll |= (CTRLL_WRB_BUFENA);
174         out_8(qrio_base + CTRLL_OFF, ctrll);
175 }
176
177 #define REASON1_OFF     0x12
178 #define REASON1_CPUWD   0x01
179
180 void qrio_cpuwd_flag(bool flag)
181 {
182         u8 reason1;
183         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
184
185         reason1 = in_8(qrio_base + REASON1_OFF);
186         if (flag)
187                 reason1 |= REASON1_CPUWD;
188         else
189                 reason1 &= ~REASON1_CPUWD;
190         out_8(qrio_base + REASON1_OFF, reason1);
191 }
192
193 #define REASON0_OFF     0x13
194 #define REASON0_SWURST  0x80
195 #define REASON0_CPURST  0x40
196 #define REASON0_BPRST   0x20
197 #define REASON0_COPRST  0x10
198 #define REASON0_SWCRST  0x08
199 #define REASON0_WDRST   0x04
200 #define REASON0_KBRST   0x02
201 #define REASON0_POWUP   0x01
202 #define UNIT_RESET\
203         (REASON0_POWUP | REASON0_COPRST | REASON0_KBRST |\
204          REASON0_BPRST | REASON0_SWURST | REASON0_WDRST)
205 #define CORE_RESET      ((REASON1_CPUWD << 8) | REASON0_SWCRST)
206
207 bool qrio_reason_unitrst(void)
208 {
209         u16 reason;
210         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
211
212         reason = in_be16(qrio_base + REASON1_OFF);
213
214         return (reason & UNIT_RESET) > 0;
215 }
216
217 #define RSTCFG_OFF      0x11
218
219 void qrio_uprstreq(u8 mode)
220 {
221         u32 rstcfg;
222         void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
223
224         rstcfg = in_8(qrio_base + RSTCFG_OFF);
225
226         if (mode & UPREQ_CORE_RST)
227                 rstcfg |= UPREQ_CORE_RST;
228         else
229                 rstcfg &= ~UPREQ_CORE_RST;
230
231         out_8(qrio_base + RSTCFG_OFF, rstcfg);
232 }
233
234 /* I2C deblocking uses the algorithm defined in board/keymile/common/common.c
235  * 2 dedicated QRIO GPIOs externally pull the SCL and SDA lines
236  * For I2C only the low state is activly driven and high state is pulled-up
237  * by a resistor. Therefore the deblock GPIOs are used
238  *  -> as an active output to drive a low state
239  *  -> as an open-drain input to have a pulled-up high state
240  */
241
242 /* By default deblock GPIOs are floating */
243 void i2c_deblock_gpio_cfg(void)
244 {
245         /* set I2C bus 1 deblocking GPIOs input, but 0 value for open drain */
246         qrio_gpio_direction_input(KM_I2C_DEBLOCK_PORT,
247                                   KM_I2C_DEBLOCK_SCL);
248         qrio_gpio_direction_input(KM_I2C_DEBLOCK_PORT,
249                                   KM_I2C_DEBLOCK_SDA);
250
251         qrio_set_gpio(KM_I2C_DEBLOCK_PORT,
252                       KM_I2C_DEBLOCK_SCL, 0);
253         qrio_set_gpio(KM_I2C_DEBLOCK_PORT,
254                       KM_I2C_DEBLOCK_SDA, 0);
255 }
256
257 void set_sda(int state)
258 {
259         qrio_set_opendrain_gpio(KM_I2C_DEBLOCK_PORT,
260                                 KM_I2C_DEBLOCK_SDA, state);
261 }
262
263 void set_scl(int state)
264 {
265         qrio_set_opendrain_gpio(KM_I2C_DEBLOCK_PORT,
266                                 KM_I2C_DEBLOCK_SCL, state);
267 }
268
269 int get_sda(void)
270 {
271         return qrio_get_gpio(KM_I2C_DEBLOCK_PORT,
272                              KM_I2C_DEBLOCK_SDA);
273 }
274
275 int get_scl(void)
276 {
277         return qrio_get_gpio(KM_I2C_DEBLOCK_PORT,
278                              KM_I2C_DEBLOCK_SCL);
279 }
280