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