Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 2 | /* |
Bo Shen | f0adeaa | 2013-08-13 14:38:32 +0800 | [diff] [blame] | 3 | * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com> |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de) |
| 6 | * |
| 7 | * Copyright (C) 2005 HP Labs |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <config.h> |
Wenyou Yang | e29b82b | 2017-03-23 12:46:21 +0800 | [diff] [blame] | 11 | #include <clk.h> |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Reinhard Meyer | b06208c | 2010-11-07 13:26:14 +0100 | [diff] [blame] | 14 | #include <asm/io.h> |
Alexey Brodkin | 267d8e2 | 2014-02-26 17:47:58 +0400 | [diff] [blame] | 15 | #include <linux/sizes.h> |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 16 | #include <asm/gpio.h> |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 17 | #include <asm/arch/hardware.h> |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 18 | #include <asm/arch/at91_pio.h> |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 19 | |
| 20 | #define GPIO_PER_BANK 32 |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 21 | |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 22 | static struct at91_port *at91_pio_get_port(unsigned port) |
| 23 | { |
| 24 | switch (port) { |
| 25 | case AT91_PIO_PORTA: |
| 26 | return (struct at91_port *)ATMEL_BASE_PIOA; |
| 27 | case AT91_PIO_PORTB: |
| 28 | return (struct at91_port *)ATMEL_BASE_PIOB; |
| 29 | case AT91_PIO_PORTC: |
| 30 | return (struct at91_port *)ATMEL_BASE_PIOC; |
| 31 | #if (ATMEL_PIO_PORTS > 3) |
| 32 | case AT91_PIO_PORTD: |
| 33 | return (struct at91_port *)ATMEL_BASE_PIOD; |
| 34 | #if (ATMEL_PIO_PORTS > 4) |
| 35 | case AT91_PIO_PORTE: |
| 36 | return (struct at91_port *)ATMEL_BASE_PIOE; |
| 37 | #endif |
| 38 | #endif |
| 39 | default: |
Wu, Josh | 383543a | 2014-05-07 16:50:45 +0800 | [diff] [blame] | 40 | printf("Error: at91_gpio: Fail to get PIO base!\n"); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 41 | return NULL; |
| 42 | } |
| 43 | } |
| 44 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 45 | static void at91_set_port_pullup(struct at91_port *at91_port, unsigned offset, |
| 46 | int use_pullup) |
| 47 | { |
| 48 | u32 mask; |
| 49 | |
| 50 | mask = 1 << offset; |
| 51 | if (use_pullup) |
| 52 | writel(mask, &at91_port->puer); |
| 53 | else |
| 54 | writel(mask, &at91_port->pudr); |
| 55 | writel(mask, &at91_port->per); |
| 56 | } |
| 57 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 58 | int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup) |
| 59 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 60 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 61 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 62 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 63 | at91_set_port_pullup(at91_port, pin, use_pullup); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 64 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * mux the pin to the "GPIO" peripheral role. |
| 70 | */ |
| 71 | int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup) |
| 72 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 73 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 74 | u32 mask; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 75 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 76 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 77 | mask = 1 << pin; |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 78 | writel(mask, &at91_port->idr); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 79 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 80 | writel(mask, &at91_port->per); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 81 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 82 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * mux the pin to the "A" internal peripheral role. |
| 88 | */ |
| 89 | int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup) |
| 90 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 91 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 92 | u32 mask; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 93 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 94 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 95 | mask = 1 << pin; |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 96 | writel(mask, &at91_port->idr); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 97 | at91_set_pio_pullup(port, pin, use_pullup); |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 98 | writel(mask, &at91_port->mux.pio2.asr); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 99 | writel(mask, &at91_port->pdr); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 100 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 101 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * mux the pin to the "B" internal peripheral role. |
| 107 | */ |
| 108 | int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup) |
| 109 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 110 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 111 | u32 mask; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 112 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 113 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 114 | mask = 1 << pin; |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 115 | writel(mask, &at91_port->idr); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 116 | at91_set_pio_pullup(port, pin, use_pullup); |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 117 | writel(mask, &at91_port->mux.pio2.bsr); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 118 | writel(mask, &at91_port->pdr); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 119 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 120 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 124 | /* |
| 125 | * mux the pin to the "A" internal peripheral role. |
| 126 | */ |
| 127 | int at91_pio3_set_a_periph(unsigned port, unsigned pin, int use_pullup) |
| 128 | { |
| 129 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 130 | u32 mask; |
| 131 | |
| 132 | if (at91_port && (pin < GPIO_PER_BANK)) { |
| 133 | mask = 1 << pin; |
| 134 | writel(mask, &at91_port->idr); |
| 135 | at91_set_pio_pullup(port, pin, use_pullup); |
| 136 | writel(readl(&at91_port->mux.pio3.abcdsr1) & ~mask, |
| 137 | &at91_port->mux.pio3.abcdsr1); |
| 138 | writel(readl(&at91_port->mux.pio3.abcdsr2) & ~mask, |
| 139 | &at91_port->mux.pio3.abcdsr2); |
| 140 | |
| 141 | writel(mask, &at91_port->pdr); |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * mux the pin to the "B" internal peripheral role. |
| 149 | */ |
| 150 | int at91_pio3_set_b_periph(unsigned port, unsigned pin, int use_pullup) |
| 151 | { |
| 152 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 153 | u32 mask; |
| 154 | |
| 155 | if (at91_port && (pin < GPIO_PER_BANK)) { |
| 156 | mask = 1 << pin; |
| 157 | writel(mask, &at91_port->idr); |
| 158 | at91_set_pio_pullup(port, pin, use_pullup); |
| 159 | writel(readl(&at91_port->mux.pio3.abcdsr1) | mask, |
| 160 | &at91_port->mux.pio3.abcdsr1); |
| 161 | writel(readl(&at91_port->mux.pio3.abcdsr2) & ~mask, |
| 162 | &at91_port->mux.pio3.abcdsr2); |
| 163 | |
| 164 | writel(mask, &at91_port->pdr); |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 169 | /* |
| 170 | * mux the pin to the "C" internal peripheral role. |
| 171 | */ |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 172 | int at91_pio3_set_c_periph(unsigned port, unsigned pin, int use_pullup) |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 173 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 174 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 175 | u32 mask; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 176 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 177 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 178 | mask = 1 << pin; |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 179 | writel(mask, &at91_port->idr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 180 | at91_set_pio_pullup(port, pin, use_pullup); |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 181 | writel(readl(&at91_port->mux.pio3.abcdsr1) & ~mask, |
| 182 | &at91_port->mux.pio3.abcdsr1); |
| 183 | writel(readl(&at91_port->mux.pio3.abcdsr2) | mask, |
| 184 | &at91_port->mux.pio3.abcdsr2); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 185 | writel(mask, &at91_port->pdr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 186 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 187 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * mux the pin to the "D" internal peripheral role. |
| 193 | */ |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 194 | int at91_pio3_set_d_periph(unsigned port, unsigned pin, int use_pullup) |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 195 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 196 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 197 | u32 mask; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 198 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 199 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 200 | mask = 1 << pin; |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 201 | writel(mask, &at91_port->idr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 202 | at91_set_pio_pullup(port, pin, use_pullup); |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 203 | writel(readl(&at91_port->mux.pio3.abcdsr1) | mask, |
| 204 | &at91_port->mux.pio3.abcdsr1); |
| 205 | writel(readl(&at91_port->mux.pio3.abcdsr2) | mask, |
| 206 | &at91_port->mux.pio3.abcdsr2); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 207 | writel(mask, &at91_port->pdr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 208 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 209 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 212 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 213 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 214 | static bool at91_get_port_output(struct at91_port *at91_port, int offset) |
| 215 | { |
| 216 | u32 mask, val; |
| 217 | |
| 218 | mask = 1 << offset; |
| 219 | val = readl(&at91_port->osr); |
| 220 | return val & mask; |
| 221 | } |
Zixun LI | 6e39950 | 2024-11-13 11:10:22 +0100 | [diff] [blame] | 222 | |
| 223 | static bool at91_is_port_gpio(struct at91_port *at91_port, int offset) |
| 224 | { |
| 225 | u32 mask, val; |
| 226 | |
| 227 | mask = 1 << offset; |
| 228 | val = readl(&at91_port->psr); |
| 229 | return !!(val & mask); |
| 230 | } |
Zixun LI | 760e4dd | 2024-11-13 11:10:54 +0100 | [diff] [blame] | 231 | |
| 232 | static void at91_set_port_multi_drive(struct at91_port *at91_port, int offset, int is_on) |
| 233 | { |
| 234 | u32 mask; |
| 235 | |
| 236 | mask = 1 << offset; |
| 237 | if (is_on) |
| 238 | writel(mask, &at91_port->mder); |
| 239 | else |
| 240 | writel(mask, &at91_port->mddr); |
| 241 | } |
Zixun LI | 10766aa | 2024-11-13 11:11:09 +0100 | [diff] [blame^] | 242 | |
| 243 | static bool at91_get_port_multi_drive(struct at91_port *at91_port, int offset) |
| 244 | { |
| 245 | u32 mask, val; |
| 246 | |
| 247 | mask = 1 << offset; |
| 248 | val = readl(&at91_port->mdsr); |
| 249 | return !!(val & mask); |
| 250 | } |
| 251 | |
| 252 | static bool at91_get_port_pullup(struct at91_port *at91_port, int offset) |
| 253 | { |
| 254 | u32 mask, val; |
| 255 | |
| 256 | mask = 1 << offset; |
| 257 | val = readl(&at91_port->pusr); |
| 258 | return !(val & mask); |
| 259 | } |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 260 | #endif |
| 261 | |
| 262 | static void at91_set_port_input(struct at91_port *at91_port, int offset, |
| 263 | int use_pullup) |
| 264 | { |
| 265 | u32 mask; |
| 266 | |
| 267 | mask = 1 << offset; |
| 268 | writel(mask, &at91_port->idr); |
| 269 | at91_set_port_pullup(at91_port, offset, use_pullup); |
| 270 | writel(mask, &at91_port->odr); |
| 271 | writel(mask, &at91_port->per); |
| 272 | } |
| 273 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 274 | /* |
| 275 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and |
| 276 | * configure it for an input. |
| 277 | */ |
| 278 | int at91_set_pio_input(unsigned port, u32 pin, int use_pullup) |
| 279 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 280 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 281 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 282 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 283 | at91_set_port_input(at91_port, pin, use_pullup); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 284 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 288 | static void at91_set_port_output(struct at91_port *at91_port, int offset, |
| 289 | int value) |
| 290 | { |
| 291 | u32 mask; |
| 292 | |
| 293 | mask = 1 << offset; |
| 294 | writel(mask, &at91_port->idr); |
| 295 | writel(mask, &at91_port->pudr); |
| 296 | if (value) |
| 297 | writel(mask, &at91_port->sodr); |
| 298 | else |
| 299 | writel(mask, &at91_port->codr); |
| 300 | writel(mask, &at91_port->oer); |
| 301 | writel(mask, &at91_port->per); |
| 302 | } |
| 303 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 304 | /* |
| 305 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), |
| 306 | * and configure it for an output. |
| 307 | */ |
| 308 | int at91_set_pio_output(unsigned port, u32 pin, int value) |
| 309 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 310 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 311 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 312 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 313 | at91_set_port_output(at91_port, pin, value); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 314 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * enable/disable the glitch filter. mostly used with IRQ handling. |
| 320 | */ |
| 321 | int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on) |
| 322 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 323 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 324 | u32 mask; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 325 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 326 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 327 | mask = 1 << pin; |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 328 | if (is_on) |
| 329 | writel(mask, &at91_port->ifer); |
| 330 | else |
| 331 | writel(mask, &at91_port->ifdr); |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * enable/disable the glitch filter. mostly used with IRQ handling. |
| 339 | */ |
| 340 | int at91_pio3_set_pio_deglitch(unsigned port, unsigned pin, int is_on) |
| 341 | { |
| 342 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 343 | u32 mask; |
| 344 | |
| 345 | if (at91_port && (pin < GPIO_PER_BANK)) { |
| 346 | mask = 1 << pin; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 347 | if (is_on) { |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 348 | writel(mask, &at91_port->mux.pio3.ifscdr); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 349 | writel(mask, &at91_port->ifer); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 350 | } else { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 351 | writel(mask, &at91_port->ifdr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 354 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 358 | /* |
| 359 | * enable/disable the debounce filter. |
| 360 | */ |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 361 | int at91_pio3_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div) |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 362 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 363 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 364 | u32 mask; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 365 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 366 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 367 | mask = 1 << pin; |
| 368 | if (is_on) { |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 369 | writel(mask, &at91_port->mux.pio3.ifscer); |
| 370 | writel(div & PIO_SCDR_DIV, &at91_port->mux.pio3.scdr); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 371 | writel(mask, &at91_port->ifer); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 372 | } else { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 373 | writel(mask, &at91_port->ifdr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 374 | } |
| 375 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 376 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * enable/disable the pull-down. |
| 382 | * If pull-up already enabled while calling the function, we disable it. |
| 383 | */ |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 384 | int at91_pio3_set_pio_pulldown(unsigned port, unsigned pin, int is_on) |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 385 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 386 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 387 | u32 mask; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 388 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 389 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 390 | mask = 1 << pin; |
Marek Vasut | ba91bd5 | 2016-05-04 23:05:23 +0200 | [diff] [blame] | 391 | if (is_on) { |
| 392 | at91_set_pio_pullup(port, pin, 0); |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 393 | writel(mask, &at91_port->mux.pio3.ppder); |
Marek Vasut | ba91bd5 | 2016-05-04 23:05:23 +0200 | [diff] [blame] | 394 | } else |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 395 | writel(mask, &at91_port->mux.pio3.ppddr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 396 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 397 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 398 | return 0; |
| 399 | } |
| 400 | |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 401 | int at91_pio3_set_pio_pullup(unsigned port, unsigned pin, int use_pullup) |
| 402 | { |
| 403 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 404 | |
| 405 | if (use_pullup) |
| 406 | at91_pio3_set_pio_pulldown(port, pin, 0); |
| 407 | |
| 408 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 409 | at91_set_port_pullup(at91_port, pin, use_pullup); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 414 | /* |
| 415 | * disable Schmitt trigger |
| 416 | */ |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 417 | int at91_pio3_set_pio_disable_schmitt_trig(unsigned port, unsigned pin) |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 418 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 419 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 420 | u32 mask; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 421 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 422 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 423 | mask = 1 << pin; |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 424 | writel(readl(&at91_port->schmitt) | mask, |
| 425 | &at91_port->schmitt); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 426 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 427 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * enable/disable the multi-driver. This is only valid for output and |
| 433 | * allows the output pin to run as an open collector output. |
| 434 | */ |
| 435 | int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on) |
| 436 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 437 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 438 | u32 mask; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 439 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 440 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 441 | mask = 1 << pin; |
| 442 | if (is_on) |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 443 | writel(mask, &at91_port->mder); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 444 | else |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 445 | writel(mask, &at91_port->mddr); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 446 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 447 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 451 | static void at91_set_port_value(struct at91_port *at91_port, int offset, |
| 452 | int value) |
| 453 | { |
| 454 | u32 mask; |
| 455 | |
| 456 | mask = 1 << offset; |
| 457 | if (value) |
| 458 | writel(mask, &at91_port->sodr); |
| 459 | else |
| 460 | writel(mask, &at91_port->codr); |
| 461 | } |
| 462 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 463 | /* |
| 464 | * assuming the pin is muxed as a gpio output, set its value. |
| 465 | */ |
| 466 | int at91_set_pio_value(unsigned port, unsigned pin, int value) |
| 467 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 468 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 469 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 470 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 471 | at91_set_port_value(at91_port, pin, value); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 472 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 476 | static int at91_get_port_value(struct at91_port *at91_port, int offset) |
| 477 | { |
| 478 | u32 pdsr = 0, mask; |
| 479 | |
| 480 | mask = 1 << offset; |
| 481 | pdsr = readl(&at91_port->pdsr) & mask; |
| 482 | |
| 483 | return pdsr != 0; |
| 484 | } |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 485 | /* |
| 486 | * read the pin's value (works even if it's not muxed as a gpio). |
| 487 | */ |
| 488 | int at91_get_pio_value(unsigned port, unsigned pin) |
| 489 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 490 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 491 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 492 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 493 | return at91_get_port_value(at91_port, pin); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 494 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 495 | return 0; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 496 | } |
Bo Shen | ad1d2ac | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 497 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 498 | #if !CONFIG_IS_ENABLED(DM_GPIO) |
Bo Shen | ad1d2ac | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 499 | /* Common GPIO API */ |
| 500 | |
Bo Shen | ad1d2ac | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 501 | int gpio_request(unsigned gpio, const char *label) |
| 502 | { |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | int gpio_free(unsigned gpio) |
| 507 | { |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | int gpio_direction_input(unsigned gpio) |
| 512 | { |
| 513 | at91_set_pio_input(at91_gpio_to_port(gpio), |
| 514 | at91_gpio_to_pin(gpio), 0); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | int gpio_direction_output(unsigned gpio, int value) |
| 519 | { |
| 520 | at91_set_pio_output(at91_gpio_to_port(gpio), |
| 521 | at91_gpio_to_pin(gpio), value); |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | int gpio_get_value(unsigned gpio) |
| 526 | { |
| 527 | return at91_get_pio_value(at91_gpio_to_port(gpio), |
| 528 | at91_gpio_to_pin(gpio)); |
| 529 | } |
| 530 | |
| 531 | int gpio_set_value(unsigned gpio, int value) |
| 532 | { |
| 533 | at91_set_pio_value(at91_gpio_to_port(gpio), |
| 534 | at91_gpio_to_pin(gpio), value); |
| 535 | |
| 536 | return 0; |
| 537 | } |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 538 | #endif |
| 539 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 540 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 541 | |
| 542 | struct at91_port_priv { |
| 543 | struct at91_port *regs; |
| 544 | }; |
| 545 | |
| 546 | /* set GPIO pin 'gpio' as an input */ |
| 547 | static int at91_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 548 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 549 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 550 | |
| 551 | at91_set_port_input(port->regs, offset, 0); |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 557 | static int at91_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 558 | int value) |
| 559 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 560 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 561 | |
| 562 | at91_set_port_output(port->regs, offset, value); |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* read GPIO IN value of pin 'gpio' */ |
| 568 | static int at91_gpio_get_value(struct udevice *dev, unsigned offset) |
| 569 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 570 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 571 | |
| 572 | return at91_get_port_value(port->regs, offset); |
| 573 | } |
| 574 | |
| 575 | /* write GPIO OUT value to pin 'gpio' */ |
| 576 | static int at91_gpio_set_value(struct udevice *dev, unsigned offset, |
| 577 | int value) |
| 578 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 579 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 580 | |
| 581 | at91_set_port_value(port->regs, offset, value); |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static int at91_gpio_get_function(struct udevice *dev, unsigned offset) |
| 587 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 588 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 589 | |
Zixun LI | 6e39950 | 2024-11-13 11:10:22 +0100 | [diff] [blame] | 590 | if (!at91_is_port_gpio(port->regs, offset)) |
| 591 | return GPIOF_FUNC; |
| 592 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 593 | if (at91_get_port_output(port->regs, offset)) |
| 594 | return GPIOF_OUTPUT; |
| 595 | else |
| 596 | return GPIOF_INPUT; |
| 597 | } |
| 598 | |
Zixun LI | 760e4dd | 2024-11-13 11:10:54 +0100 | [diff] [blame] | 599 | static int at91_gpio_set_flags(struct udevice *dev, unsigned int offset, |
| 600 | ulong flags) |
| 601 | { |
| 602 | struct at91_port_priv *port = dev_get_priv(dev); |
| 603 | ulong supported_mask; |
| 604 | |
| 605 | supported_mask = GPIOD_OPEN_DRAIN | GPIOD_MASK_DIR | GPIOD_PULL_UP; |
| 606 | if (flags & ~supported_mask) |
| 607 | return -ENOTSUPP; |
| 608 | |
| 609 | if (flags & GPIOD_IS_OUT) { |
| 610 | if (flags & GPIOD_OPEN_DRAIN) |
| 611 | at91_set_port_multi_drive(port->regs, offset, true); |
| 612 | else |
| 613 | at91_set_port_multi_drive(port->regs, offset, false); |
| 614 | |
| 615 | at91_set_port_output(port->regs, offset, flags & GPIOD_IS_OUT_ACTIVE); |
| 616 | |
| 617 | } else if (flags & GPIOD_IS_IN) { |
| 618 | at91_set_port_input(port->regs, offset, false); |
| 619 | } |
| 620 | if (flags & GPIOD_PULL_UP) |
| 621 | at91_set_port_pullup(port->regs, offset, true); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Zixun LI | 10766aa | 2024-11-13 11:11:09 +0100 | [diff] [blame^] | 626 | static int at91_gpio_get_flags(struct udevice *dev, unsigned int offset, |
| 627 | ulong *flagsp) |
| 628 | { |
| 629 | struct at91_port_priv *port = dev_get_priv(dev); |
| 630 | ulong dir_flags = 0; |
| 631 | |
| 632 | if (at91_get_port_output(port->regs, offset)) { |
| 633 | dir_flags |= GPIOD_IS_OUT; |
| 634 | |
| 635 | if (at91_get_port_multi_drive(port->regs, offset)) |
| 636 | dir_flags |= GPIOD_OPEN_DRAIN; |
| 637 | |
| 638 | if (at91_get_port_value(port->regs, offset)) |
| 639 | dir_flags |= GPIOD_IS_OUT_ACTIVE; |
| 640 | } else { |
| 641 | dir_flags |= GPIOD_IS_IN; |
| 642 | } |
| 643 | |
| 644 | if (at91_get_port_pullup(port->regs, offset)) |
| 645 | dir_flags |= GPIOD_PULL_UP; |
| 646 | |
| 647 | *flagsp = dir_flags; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
James Byrne | b7e33d3 | 2019-11-26 11:52:04 +0000 | [diff] [blame] | 652 | static const char *at91_get_bank_name(uint32_t base_addr) |
| 653 | { |
| 654 | switch (base_addr) { |
| 655 | case ATMEL_BASE_PIOA: |
| 656 | return "PIOA"; |
| 657 | case ATMEL_BASE_PIOB: |
| 658 | return "PIOB"; |
| 659 | case ATMEL_BASE_PIOC: |
| 660 | return "PIOC"; |
| 661 | #if (ATMEL_PIO_PORTS > 3) |
| 662 | case ATMEL_BASE_PIOD: |
| 663 | return "PIOD"; |
| 664 | #if (ATMEL_PIO_PORTS > 4) |
| 665 | case ATMEL_BASE_PIOE: |
| 666 | return "PIOE"; |
| 667 | #endif |
| 668 | #endif |
| 669 | } |
| 670 | |
| 671 | return "undefined"; |
| 672 | } |
| 673 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 674 | static const struct dm_gpio_ops gpio_at91_ops = { |
| 675 | .direction_input = at91_gpio_direction_input, |
| 676 | .direction_output = at91_gpio_direction_output, |
| 677 | .get_value = at91_gpio_get_value, |
| 678 | .set_value = at91_gpio_set_value, |
| 679 | .get_function = at91_gpio_get_function, |
Zixun LI | 760e4dd | 2024-11-13 11:10:54 +0100 | [diff] [blame] | 680 | .set_flags = at91_gpio_set_flags, |
Zixun LI | 10766aa | 2024-11-13 11:11:09 +0100 | [diff] [blame^] | 681 | .get_flags = at91_gpio_get_flags, |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 682 | }; |
| 683 | |
| 684 | static int at91_gpio_probe(struct udevice *dev) |
| 685 | { |
| 686 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 687 | struct at91_port_plat *plat = dev_get_plat(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 688 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Wenyou Yang | e29b82b | 2017-03-23 12:46:21 +0800 | [diff] [blame] | 689 | struct clk clk; |
| 690 | int ret; |
| 691 | |
| 692 | ret = clk_get_by_index(dev, 0, &clk); |
| 693 | if (ret) |
| 694 | return ret; |
| 695 | |
| 696 | ret = clk_enable(&clk); |
| 697 | if (ret) |
| 698 | return ret; |
| 699 | |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 700 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Masahiro Yamada | 5150bc7 | 2020-08-04 14:14:41 +0900 | [diff] [blame] | 701 | plat->base_addr = dev_read_addr(dev); |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 702 | #endif |
James Byrne | b7e33d3 | 2019-11-26 11:52:04 +0000 | [diff] [blame] | 703 | plat->bank_name = at91_get_bank_name(plat->base_addr); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 704 | port->regs = (struct at91_port *)plat->base_addr; |
| 705 | |
James Byrne | b7e33d3 | 2019-11-26 11:52:04 +0000 | [diff] [blame] | 706 | uc_priv->bank_name = plat->bank_name; |
| 707 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 708 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 712 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 713 | static const struct udevice_id at91_gpio_ids[] = { |
| 714 | { .compatible = "atmel,at91rm9200-gpio" }, |
| 715 | { } |
| 716 | }; |
| 717 | #endif |
| 718 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 719 | U_BOOT_DRIVER(atmel_at91rm9200_gpio) = { |
| 720 | .name = "atmel_at91rm9200_gpio", |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 721 | .id = UCLASS_GPIO, |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 722 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 723 | .of_match = at91_gpio_ids, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 724 | .plat_auto = sizeof(struct at91_port_plat), |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 725 | #endif |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 726 | .ops = &gpio_at91_ops, |
| 727 | .probe = at91_gpio_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 728 | .priv_auto = sizeof(struct at91_port_priv), |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 729 | }; |
| 730 | #endif |