blob: ed5e472034313767092656d657a9d80474d4a582 [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
Aleksandar Gerasimovski69d5b7b2021-01-13 16:20:51 +000014/* QRIO ID register offset */
15#define ID_REV_OFF 0x00
16
Valentin Longchamp42f3ed62014-01-27 11:49:05 +010017/* QRIO GPIO register offsets */
18#define DIRECT_OFF 0x18
19#define GPRT_OFF 0x1c
20
Aleksandar Gerasimovski69d5b7b2021-01-13 16:20:51 +000021void show_qrio(void)
22{
23 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
24 u16 id_rev = in_be16(qrio_base + ID_REV_OFF);
25
26 printf("QRIO: id = %u, revision = %u\n",
27 (id_rev >> 8) & 0xff, id_rev & 0xff);
28}
29
Aleksandar Gerasimovski8a78d922021-11-16 12:48:46 +000030#define SLFTEST_OFF 0x06
31
32bool qrio_get_selftest_pin(void)
33{
34 u8 slftest;
35
36 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
37
38 slftest = in_8(qrio_base + SLFTEST_OFF);
39
40 return (slftest & 1) > 0;
41}
42
Valentin Longchamp42f3ed62014-01-27 11:49:05 +010043int qrio_get_gpio(u8 port_off, u8 gpio_nr)
44{
45 u32 gprt;
46
47 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
48
49 gprt = in_be32(qrio_base + port_off + GPRT_OFF);
50
51 return (gprt >> gpio_nr) & 1U;
52}
53
54void qrio_set_gpio(u8 port_off, u8 gpio_nr, bool value)
55{
56 u32 gprt, mask;
57
58 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
59
60 mask = 1U << gpio_nr;
61
62 gprt = in_be32(qrio_base + port_off + GPRT_OFF);
63 if (value)
64 gprt |= mask;
65 else
66 gprt &= ~mask;
67
68 out_be32(qrio_base + port_off + GPRT_OFF, gprt);
69}
70
71void qrio_gpio_direction_output(u8 port_off, u8 gpio_nr, bool value)
72{
73 u32 direct, mask;
74
75 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
76
77 mask = 1U << gpio_nr;
78
79 direct = in_be32(qrio_base + port_off + DIRECT_OFF);
80 direct |= mask;
81 out_be32(qrio_base + port_off + DIRECT_OFF, direct);
82
83 qrio_set_gpio(port_off, gpio_nr, value);
84}
85
86void qrio_gpio_direction_input(u8 port_off, u8 gpio_nr)
87{
88 u32 direct, mask;
89
90 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
91
92 mask = 1U << gpio_nr;
93
94 direct = in_be32(qrio_base + port_off + DIRECT_OFF);
95 direct &= ~mask;
96 out_be32(qrio_base + port_off + DIRECT_OFF, direct);
97}
98
99void qrio_set_opendrain_gpio(u8 port_off, u8 gpio_nr, u8 val)
100{
101 u32 direct, mask;
102
103 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
104
105 mask = 1U << gpio_nr;
106
107 direct = in_be32(qrio_base + port_off + DIRECT_OFF);
108 if (val == 0)
109 /* set to output -> GPIO drives low */
110 direct |= mask;
111 else
112 /* set to input -> GPIO floating */
113 direct &= ~mask;
114
115 out_be32(qrio_base + port_off + DIRECT_OFF, direct);
116}
117
118#define WDMASK_OFF 0x16
119
Valentin Longchamp5eb9dab2014-04-30 15:01:46 +0200120void qrio_wdmask(u8 bit, bool wden)
Valentin Longchamp42f3ed62014-01-27 11:49:05 +0100121{
122 u16 wdmask;
123 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
124
125 wdmask = in_be16(qrio_base + WDMASK_OFF);
126
127 if (wden)
128 wdmask |= (1 << bit);
129 else
130 wdmask &= ~(1 << bit);
131
132 out_be16(qrio_base + WDMASK_OFF, wdmask);
133}
134
135#define PRST_OFF 0x1a
136
137void qrio_prst(u8 bit, bool en, bool wden)
138{
139 u16 prst;
140 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
141
142 qrio_wdmask(bit, wden);
143
144 prst = in_be16(qrio_base + PRST_OFF);
145
146 if (en)
147 prst &= ~(1 << bit);
148 else
149 prst |= (1 << bit);
150
151 out_be16(qrio_base + PRST_OFF, prst);
152}
153
154#define PRSTCFG_OFF 0x1c
155
156void qrio_prstcfg(u8 bit, u8 mode)
157{
Aleksandar Gerasimovski5e773a42021-01-13 16:20:35 +0000158 unsigned long prstcfg;
Valentin Longchamp42f3ed62014-01-27 11:49:05 +0100159 u8 i;
160 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
161
162 prstcfg = in_be32(qrio_base + PRSTCFG_OFF);
163
164 for (i = 0; i < 2; i++) {
Holger Brunck95626872020-01-10 12:47:42 +0100165 if (mode & (1 << i))
Aleksandar Gerasimovski5e773a42021-01-13 16:20:35 +0000166 __set_bit(2 * bit + i, &prstcfg);
Valentin Longchamp42f3ed62014-01-27 11:49:05 +0100167 else
Aleksandar Gerasimovski5e773a42021-01-13 16:20:35 +0000168 __clear_bit(2 * bit + i, &prstcfg);
Valentin Longchamp42f3ed62014-01-27 11:49:05 +0100169 }
170
171 out_be32(qrio_base + PRSTCFG_OFF, prstcfg);
172}
Stefan Bigler8b6f6c32014-05-02 10:48:41 +0200173
174#define CTRLH_OFF 0x02
175#define CTRLH_WRL_BOOT 0x01
176#define CTRLH_WRL_UNITRUN 0x02
177
178void qrio_set_leds(void)
179{
180 u8 ctrlh;
181 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
182
183 /* set UNIT LED to RED and BOOT LED to ON */
184 ctrlh = in_8(qrio_base + CTRLH_OFF);
185 ctrlh |= (CTRLH_WRL_BOOT | CTRLH_WRL_UNITRUN);
186 out_8(qrio_base + CTRLH_OFF, ctrlh);
187}
Stefan Biglerdafc72d2014-05-02 10:49:27 +0200188
189#define CTRLL_OFF 0x03
190#define CTRLL_WRB_BUFENA 0x20
191
192void qrio_enable_app_buffer(void)
193{
194 u8 ctrll;
195 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
196
197 /* enable application buffer */
198 ctrll = in_8(qrio_base + CTRLL_OFF);
199 ctrll |= (CTRLL_WRB_BUFENA);
200 out_8(qrio_base + CTRLL_OFF, ctrll);
201}
Boschung, Rainercacb02b2014-06-03 09:05:17 +0200202
203#define REASON1_OFF 0x12
204#define REASON1_CPUWD 0x01
205
206void qrio_cpuwd_flag(bool flag)
207{
208 u8 reason1;
209 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
Holger Brunck95626872020-01-10 12:47:42 +0100210
Boschung, Rainercacb02b2014-06-03 09:05:17 +0200211 reason1 = in_8(qrio_base + REASON1_OFF);
212 if (flag)
213 reason1 |= REASON1_CPUWD;
214 else
215 reason1 &= ~REASON1_CPUWD;
216 out_8(qrio_base + REASON1_OFF, reason1);
217}
Boschung, Rainere70e5952014-06-03 09:05:19 +0200218
Holger Brunck95626872020-01-10 12:47:42 +0100219#define REASON0_OFF 0x13
220#define REASON0_SWURST 0x80
221#define REASON0_CPURST 0x40
222#define REASON0_BPRST 0x20
223#define REASON0_COPRST 0x10
224#define REASON0_SWCRST 0x08
225#define REASON0_WDRST 0x04
226#define REASON0_KBRST 0x02
227#define REASON0_POWUP 0x01
228#define UNIT_RESET\
Rainer Boschung44425ea2020-01-10 12:47:43 +0100229 (REASON0_POWUP | REASON0_COPRST | REASON0_KBRST |\
230 REASON0_BPRST | REASON0_SWURST | REASON0_WDRST)
231#define CORE_RESET ((REASON1_CPUWD << 8) | REASON0_SWCRST)
Holger Brunck95626872020-01-10 12:47:42 +0100232
233bool qrio_reason_unitrst(void)
234{
235 u16 reason;
236 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
237
238 reason = in_be16(qrio_base + REASON1_OFF);
239
240 return (reason & UNIT_RESET) > 0;
241}
242
Boschung, Rainere70e5952014-06-03 09:05:19 +0200243#define RSTCFG_OFF 0x11
244
245void qrio_uprstreq(u8 mode)
246{
247 u32 rstcfg;
248 void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE;
249
250 rstcfg = in_8(qrio_base + RSTCFG_OFF);
251
252 if (mode & UPREQ_CORE_RST)
253 rstcfg |= UPREQ_CORE_RST;
254 else
255 rstcfg &= ~UPREQ_CORE_RST;
256
257 out_8(qrio_base + RSTCFG_OFF, rstcfg);
258}
Holger Brunck95626872020-01-10 12:47:42 +0100259
260/* I2C deblocking uses the algorithm defined in board/keymile/common/common.c
261 * 2 dedicated QRIO GPIOs externally pull the SCL and SDA lines
262 * For I2C only the low state is activly driven and high state is pulled-up
263 * by a resistor. Therefore the deblock GPIOs are used
264 * -> as an active output to drive a low state
265 * -> as an open-drain input to have a pulled-up high state
266 */
267
268/* By default deblock GPIOs are floating */
269void i2c_deblock_gpio_cfg(void)
270{
271 /* set I2C bus 1 deblocking GPIOs input, but 0 value for open drain */
272 qrio_gpio_direction_input(KM_I2C_DEBLOCK_PORT,
273 KM_I2C_DEBLOCK_SCL);
274 qrio_gpio_direction_input(KM_I2C_DEBLOCK_PORT,
275 KM_I2C_DEBLOCK_SDA);
276
277 qrio_set_gpio(KM_I2C_DEBLOCK_PORT,
278 KM_I2C_DEBLOCK_SCL, 0);
279 qrio_set_gpio(KM_I2C_DEBLOCK_PORT,
280 KM_I2C_DEBLOCK_SDA, 0);
281}
282
283void set_sda(int state)
284{
285 qrio_set_opendrain_gpio(KM_I2C_DEBLOCK_PORT,
286 KM_I2C_DEBLOCK_SDA, state);
287}
288
289void set_scl(int state)
290{
291 qrio_set_opendrain_gpio(KM_I2C_DEBLOCK_PORT,
292 KM_I2C_DEBLOCK_SCL, state);
293}
294
295int get_sda(void)
296{
297 return qrio_get_gpio(KM_I2C_DEBLOCK_PORT,
298 KM_I2C_DEBLOCK_SDA);
299}
300
301int get_scl(void)
302{
303 return qrio_get_gpio(KM_I2C_DEBLOCK_PORT,
304 KM_I2C_DEBLOCK_SCL);
305}