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