Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 Keymile AG |
| 4 | * Valentin Longchamp <valentin.longchamp@keymile.com> |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Aleksandar Gerasimovski | 5e773a4 | 2021-01-13 16:20:35 +0000 | [diff] [blame] | 8 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 9 | #include <linux/bitops.h> |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 10 | |
Holger Brunck | 9562687 | 2020-01-10 12:47:42 +0100 | [diff] [blame] | 11 | #include "common.h" |
| 12 | #include "qrio.h" |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 13 | |
Aleksandar Gerasimovski | 69d5b7b | 2021-01-13 16:20:51 +0000 | [diff] [blame] | 14 | /* QRIO ID register offset */ |
| 15 | #define ID_REV_OFF 0x00 |
| 16 | |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 17 | /* QRIO GPIO register offsets */ |
| 18 | #define DIRECT_OFF 0x18 |
| 19 | #define GPRT_OFF 0x1c |
| 20 | |
Aleksandar Gerasimovski | 69d5b7b | 2021-01-13 16:20:51 +0000 | [diff] [blame] | 21 | void 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 Gerasimovski | 8a78d92 | 2021-11-16 12:48:46 +0000 | [diff] [blame] | 30 | #define SLFTEST_OFF 0x06 |
| 31 | |
| 32 | bool 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 Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 43 | int 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 | |
| 54 | void 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 | |
| 71 | void 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 | |
| 86 | void 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 | |
| 99 | void 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 Longchamp | 5eb9dab | 2014-04-30 15:01:46 +0200 | [diff] [blame] | 120 | void qrio_wdmask(u8 bit, bool wden) |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 121 | { |
| 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 | |
| 137 | void 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 | |
| 156 | void qrio_prstcfg(u8 bit, u8 mode) |
| 157 | { |
Aleksandar Gerasimovski | 5e773a4 | 2021-01-13 16:20:35 +0000 | [diff] [blame] | 158 | unsigned long prstcfg; |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 159 | 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 Brunck | 9562687 | 2020-01-10 12:47:42 +0100 | [diff] [blame] | 165 | if (mode & (1 << i)) |
Aleksandar Gerasimovski | 5e773a4 | 2021-01-13 16:20:35 +0000 | [diff] [blame] | 166 | __set_bit(2 * bit + i, &prstcfg); |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 167 | else |
Aleksandar Gerasimovski | 5e773a4 | 2021-01-13 16:20:35 +0000 | [diff] [blame] | 168 | __clear_bit(2 * bit + i, &prstcfg); |
Valentin Longchamp | 42f3ed6 | 2014-01-27 11:49:05 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | out_be32(qrio_base + PRSTCFG_OFF, prstcfg); |
| 172 | } |
Stefan Bigler | 8b6f6c3 | 2014-05-02 10:48:41 +0200 | [diff] [blame] | 173 | |
| 174 | #define CTRLH_OFF 0x02 |
| 175 | #define CTRLH_WRL_BOOT 0x01 |
| 176 | #define CTRLH_WRL_UNITRUN 0x02 |
| 177 | |
| 178 | void 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 Bigler | dafc72d | 2014-05-02 10:49:27 +0200 | [diff] [blame] | 188 | |
| 189 | #define CTRLL_OFF 0x03 |
| 190 | #define CTRLL_WRB_BUFENA 0x20 |
| 191 | |
| 192 | void 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, Rainer | cacb02b | 2014-06-03 09:05:17 +0200 | [diff] [blame] | 202 | |
| 203 | #define REASON1_OFF 0x12 |
| 204 | #define REASON1_CPUWD 0x01 |
| 205 | |
| 206 | void qrio_cpuwd_flag(bool flag) |
| 207 | { |
| 208 | u8 reason1; |
| 209 | void __iomem *qrio_base = (void *)CONFIG_SYS_QRIO_BASE; |
Holger Brunck | 9562687 | 2020-01-10 12:47:42 +0100 | [diff] [blame] | 210 | |
Boschung, Rainer | cacb02b | 2014-06-03 09:05:17 +0200 | [diff] [blame] | 211 | 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, Rainer | e70e595 | 2014-06-03 09:05:19 +0200 | [diff] [blame] | 218 | |
Holger Brunck | 9562687 | 2020-01-10 12:47:42 +0100 | [diff] [blame] | 219 | #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 Boschung | 44425ea | 2020-01-10 12:47:43 +0100 | [diff] [blame] | 229 | (REASON0_POWUP | REASON0_COPRST | REASON0_KBRST |\ |
| 230 | REASON0_BPRST | REASON0_SWURST | REASON0_WDRST) |
| 231 | #define CORE_RESET ((REASON1_CPUWD << 8) | REASON0_SWCRST) |
Holger Brunck | 9562687 | 2020-01-10 12:47:42 +0100 | [diff] [blame] | 232 | |
| 233 | bool 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, Rainer | e70e595 | 2014-06-03 09:05:19 +0200 | [diff] [blame] | 243 | #define RSTCFG_OFF 0x11 |
| 244 | |
| 245 | void 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 Brunck | 9562687 | 2020-01-10 12:47:42 +0100 | [diff] [blame] | 259 | |
| 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 */ |
| 269 | void 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 | |
| 283 | void set_sda(int state) |
| 284 | { |
| 285 | qrio_set_opendrain_gpio(KM_I2C_DEBLOCK_PORT, |
| 286 | KM_I2C_DEBLOCK_SDA, state); |
| 287 | } |
| 288 | |
| 289 | void set_scl(int state) |
| 290 | { |
| 291 | qrio_set_opendrain_gpio(KM_I2C_DEBLOCK_PORT, |
| 292 | KM_I2C_DEBLOCK_SCL, state); |
| 293 | } |
| 294 | |
| 295 | int get_sda(void) |
| 296 | { |
| 297 | return qrio_get_gpio(KM_I2C_DEBLOCK_PORT, |
| 298 | KM_I2C_DEBLOCK_SDA); |
| 299 | } |
| 300 | |
| 301 | int get_scl(void) |
| 302 | { |
| 303 | return qrio_get_gpio(KM_I2C_DEBLOCK_PORT, |
| 304 | KM_I2C_DEBLOCK_SCL); |
| 305 | } |