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> |
| 11 | #include <common.h> |
Wenyou Yang | e29b82b | 2017-03-23 12:46:21 +0800 | [diff] [blame] | 12 | #include <clk.h> |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 13 | #include <dm.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 | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 213 | #ifdef CONFIG_DM_GPIO |
| 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 | } |
| 222 | #endif |
| 223 | |
| 224 | static void at91_set_port_input(struct at91_port *at91_port, int offset, |
| 225 | int use_pullup) |
| 226 | { |
| 227 | u32 mask; |
| 228 | |
| 229 | mask = 1 << offset; |
| 230 | writel(mask, &at91_port->idr); |
| 231 | at91_set_port_pullup(at91_port, offset, use_pullup); |
| 232 | writel(mask, &at91_port->odr); |
| 233 | writel(mask, &at91_port->per); |
| 234 | } |
| 235 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 236 | /* |
| 237 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and |
| 238 | * configure it for an input. |
| 239 | */ |
| 240 | int at91_set_pio_input(unsigned port, u32 pin, int use_pullup) |
| 241 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 242 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 243 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 244 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 245 | at91_set_port_input(at91_port, pin, use_pullup); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 246 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 250 | static void at91_set_port_output(struct at91_port *at91_port, int offset, |
| 251 | int value) |
| 252 | { |
| 253 | u32 mask; |
| 254 | |
| 255 | mask = 1 << offset; |
| 256 | writel(mask, &at91_port->idr); |
| 257 | writel(mask, &at91_port->pudr); |
| 258 | if (value) |
| 259 | writel(mask, &at91_port->sodr); |
| 260 | else |
| 261 | writel(mask, &at91_port->codr); |
| 262 | writel(mask, &at91_port->oer); |
| 263 | writel(mask, &at91_port->per); |
| 264 | } |
| 265 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 266 | /* |
| 267 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), |
| 268 | * and configure it for an output. |
| 269 | */ |
| 270 | int at91_set_pio_output(unsigned port, u32 pin, int value) |
| 271 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 272 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 273 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 274 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 275 | at91_set_port_output(at91_port, pin, value); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 276 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * enable/disable the glitch filter. mostly used with IRQ handling. |
| 282 | */ |
| 283 | int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on) |
| 284 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 285 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 286 | u32 mask; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 287 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 288 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 289 | mask = 1 << pin; |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 290 | if (is_on) |
| 291 | writel(mask, &at91_port->ifer); |
| 292 | else |
| 293 | writel(mask, &at91_port->ifdr); |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * enable/disable the glitch filter. mostly used with IRQ handling. |
| 301 | */ |
| 302 | int at91_pio3_set_pio_deglitch(unsigned port, unsigned pin, int is_on) |
| 303 | { |
| 304 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 305 | u32 mask; |
| 306 | |
| 307 | if (at91_port && (pin < GPIO_PER_BANK)) { |
| 308 | mask = 1 << pin; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 309 | if (is_on) { |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 310 | writel(mask, &at91_port->mux.pio3.ifscdr); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 311 | writel(mask, &at91_port->ifer); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 312 | } else { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 313 | writel(mask, &at91_port->ifdr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 316 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 320 | /* |
| 321 | * enable/disable the debounce filter. |
| 322 | */ |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 323 | 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] | 324 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 325 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 326 | u32 mask; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 327 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 328 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 329 | mask = 1 << pin; |
| 330 | if (is_on) { |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 331 | writel(mask, &at91_port->mux.pio3.ifscer); |
| 332 | writel(div & PIO_SCDR_DIV, &at91_port->mux.pio3.scdr); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 333 | writel(mask, &at91_port->ifer); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 334 | } else { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 335 | writel(mask, &at91_port->ifdr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 338 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * enable/disable the pull-down. |
| 344 | * If pull-up already enabled while calling the function, we disable it. |
| 345 | */ |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 346 | 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] | 347 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 348 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 349 | u32 mask; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 350 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 351 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 352 | mask = 1 << pin; |
Marek Vasut | ba91bd5 | 2016-05-04 23:05:23 +0200 | [diff] [blame] | 353 | if (is_on) { |
| 354 | at91_set_pio_pullup(port, pin, 0); |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 355 | writel(mask, &at91_port->mux.pio3.ppder); |
Marek Vasut | ba91bd5 | 2016-05-04 23:05:23 +0200 | [diff] [blame] | 356 | } else |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 357 | writel(mask, &at91_port->mux.pio3.ppddr); |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 358 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 359 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 363 | int at91_pio3_set_pio_pullup(unsigned port, unsigned pin, int use_pullup) |
| 364 | { |
| 365 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 366 | |
| 367 | if (use_pullup) |
| 368 | at91_pio3_set_pio_pulldown(port, pin, 0); |
| 369 | |
| 370 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 371 | at91_set_port_pullup(at91_port, pin, use_pullup); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 376 | /* |
| 377 | * disable Schmitt trigger |
| 378 | */ |
Wenyou Yang | 4a92a3e | 2017-03-23 12:44:36 +0800 | [diff] [blame] | 379 | int at91_pio3_set_pio_disable_schmitt_trig(unsigned port, unsigned pin) |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 380 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 381 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 382 | u32 mask; |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 383 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 384 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 0ac1345 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 385 | mask = 1 << pin; |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 386 | writel(readl(&at91_port->schmitt) | mask, |
| 387 | &at91_port->schmitt); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 388 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 389 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * enable/disable the multi-driver. This is only valid for output and |
| 395 | * allows the output pin to run as an open collector output. |
| 396 | */ |
| 397 | int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on) |
| 398 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 399 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 400 | u32 mask; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 401 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 402 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 403 | mask = 1 << pin; |
| 404 | if (is_on) |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 405 | writel(mask, &at91_port->mder); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 406 | else |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 407 | writel(mask, &at91_port->mddr); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 408 | } |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 409 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 413 | static void at91_set_port_value(struct at91_port *at91_port, int offset, |
| 414 | int value) |
| 415 | { |
| 416 | u32 mask; |
| 417 | |
| 418 | mask = 1 << offset; |
| 419 | if (value) |
| 420 | writel(mask, &at91_port->sodr); |
| 421 | else |
| 422 | writel(mask, &at91_port->codr); |
| 423 | } |
| 424 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 425 | /* |
| 426 | * assuming the pin is muxed as a gpio output, set its value. |
| 427 | */ |
| 428 | int at91_set_pio_value(unsigned port, unsigned pin, int value) |
| 429 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 430 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 431 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 432 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 433 | at91_set_port_value(at91_port, pin, value); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 434 | |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 438 | static int at91_get_port_value(struct at91_port *at91_port, int offset) |
| 439 | { |
| 440 | u32 pdsr = 0, mask; |
| 441 | |
| 442 | mask = 1 << offset; |
| 443 | pdsr = readl(&at91_port->pdsr) & mask; |
| 444 | |
| 445 | return pdsr != 0; |
| 446 | } |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 447 | /* |
| 448 | * read the pin's value (works even if it's not muxed as a gpio). |
| 449 | */ |
| 450 | int at91_get_pio_value(unsigned port, unsigned pin) |
| 451 | { |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 452 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 453 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 454 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 455 | return at91_get_port_value(at91_port, pin); |
Bo Shen | 02d8814 | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 456 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 457 | return 0; |
Jens Scharsig | 8d06546 | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 458 | } |
Bo Shen | ad1d2ac | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 459 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 460 | #ifndef CONFIG_DM_GPIO |
Bo Shen | ad1d2ac | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 461 | /* Common GPIO API */ |
| 462 | |
Bo Shen | ad1d2ac | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 463 | int gpio_request(unsigned gpio, const char *label) |
| 464 | { |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | int gpio_free(unsigned gpio) |
| 469 | { |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | int gpio_direction_input(unsigned gpio) |
| 474 | { |
| 475 | at91_set_pio_input(at91_gpio_to_port(gpio), |
| 476 | at91_gpio_to_pin(gpio), 0); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | int gpio_direction_output(unsigned gpio, int value) |
| 481 | { |
| 482 | at91_set_pio_output(at91_gpio_to_port(gpio), |
| 483 | at91_gpio_to_pin(gpio), value); |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | int gpio_get_value(unsigned gpio) |
| 488 | { |
| 489 | return at91_get_pio_value(at91_gpio_to_port(gpio), |
| 490 | at91_gpio_to_pin(gpio)); |
| 491 | } |
| 492 | |
| 493 | int gpio_set_value(unsigned gpio, int value) |
| 494 | { |
| 495 | at91_set_pio_value(at91_gpio_to_port(gpio), |
| 496 | at91_gpio_to_pin(gpio), value); |
| 497 | |
| 498 | return 0; |
| 499 | } |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 500 | #endif |
| 501 | |
| 502 | #ifdef CONFIG_DM_GPIO |
| 503 | |
| 504 | struct at91_port_priv { |
| 505 | struct at91_port *regs; |
| 506 | }; |
| 507 | |
| 508 | /* set GPIO pin 'gpio' as an input */ |
| 509 | static int at91_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 510 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 511 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 512 | |
| 513 | at91_set_port_input(port->regs, offset, 0); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 519 | static int at91_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 520 | int value) |
| 521 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 522 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 523 | |
| 524 | at91_set_port_output(port->regs, offset, value); |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /* read GPIO IN value of pin 'gpio' */ |
| 530 | static int at91_gpio_get_value(struct udevice *dev, unsigned offset) |
| 531 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 532 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 533 | |
| 534 | return at91_get_port_value(port->regs, offset); |
| 535 | } |
| 536 | |
| 537 | /* write GPIO OUT value to pin 'gpio' */ |
| 538 | static int at91_gpio_set_value(struct udevice *dev, unsigned offset, |
| 539 | int value) |
| 540 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 541 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 542 | |
| 543 | at91_set_port_value(port->regs, offset, value); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int at91_gpio_get_function(struct udevice *dev, unsigned offset) |
| 549 | { |
Axel Lin | 98b9b37 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 550 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 551 | |
| 552 | /* GPIOF_FUNC is not implemented yet */ |
| 553 | if (at91_get_port_output(port->regs, offset)) |
| 554 | return GPIOF_OUTPUT; |
| 555 | else |
| 556 | return GPIOF_INPUT; |
| 557 | } |
| 558 | |
| 559 | static const struct dm_gpio_ops gpio_at91_ops = { |
| 560 | .direction_input = at91_gpio_direction_input, |
| 561 | .direction_output = at91_gpio_direction_output, |
| 562 | .get_value = at91_gpio_get_value, |
| 563 | .set_value = at91_gpio_set_value, |
| 564 | .get_function = at91_gpio_get_function, |
| 565 | }; |
| 566 | |
| 567 | static int at91_gpio_probe(struct udevice *dev) |
| 568 | { |
| 569 | struct at91_port_priv *port = dev_get_priv(dev); |
| 570 | struct at91_port_platdata *plat = dev_get_platdata(dev); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 571 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Wenyou Yang | e29b82b | 2017-03-23 12:46:21 +0800 | [diff] [blame] | 572 | struct clk clk; |
| 573 | int ret; |
| 574 | |
| 575 | ret = clk_get_by_index(dev, 0, &clk); |
| 576 | if (ret) |
| 577 | return ret; |
| 578 | |
| 579 | ret = clk_enable(&clk); |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | |
| 583 | clk_free(&clk); |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 584 | |
| 585 | uc_priv->bank_name = plat->bank_name; |
| 586 | uc_priv->gpio_count = GPIO_PER_BANK; |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 587 | |
| 588 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 589 | plat->base_addr = (uint32_t)devfdt_get_addr_ptr(dev); |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 590 | #endif |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 591 | port->regs = (struct at91_port *)plat->base_addr; |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 596 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 597 | static const struct udevice_id at91_gpio_ids[] = { |
| 598 | { .compatible = "atmel,at91rm9200-gpio" }, |
| 599 | { } |
| 600 | }; |
| 601 | #endif |
| 602 | |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 603 | U_BOOT_DRIVER(gpio_at91) = { |
| 604 | .name = "gpio_at91", |
| 605 | .id = UCLASS_GPIO, |
Wenyou Yang | eff6d8a | 2017-03-23 12:46:20 +0800 | [diff] [blame] | 606 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 607 | .of_match = at91_gpio_ids, |
| 608 | .platdata_auto_alloc_size = sizeof(struct at91_port_platdata), |
| 609 | #endif |
Simon Glass | 4d71788 | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 610 | .ops = &gpio_at91_ops, |
| 611 | .probe = at91_gpio_probe, |
| 612 | .priv_auto_alloc_size = sizeof(struct at91_port_priv), |
| 613 | }; |
| 614 | #endif |