Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 1 | /* |
Bo Shen | 39b787e | 2013-08-13 14:38:32 +0800 | [diff] [blame] | 2 | * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de) |
| 5 | * |
| 6 | * Copyright (C) 2005 HP Labs |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <config.h> |
| 12 | #include <common.h> |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 13 | #include <dm.h> |
Reinhard Meyer | 86592f6 | 2010-11-07 13:26:14 +0100 | [diff] [blame] | 14 | #include <asm/io.h> |
Alexey Brodkin | 1ace402 | 2014-02-26 17:47:58 +0400 | [diff] [blame] | 15 | #include <linux/sizes.h> |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 16 | #include <asm/gpio.h> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 17 | #include <asm/arch/hardware.h> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 18 | #include <asm/arch/at91_pio.h> |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 19 | |
| 20 | #define GPIO_PER_BANK 32 |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 21 | |
Bo Shen | 4bc9b7a | 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 | 7d82d89 | 2014-05-07 16:50:45 +0800 | [diff] [blame] | 40 | printf("Error: at91_gpio: Fail to get PIO base!\n"); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 41 | return NULL; |
| 42 | } |
| 43 | } |
| 44 | |
Simon Glass | 918354b | 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 | ea8fbba | 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 | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 60 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 61 | |
Marek Vasut | 152ac5f | 2016-05-04 23:05:23 +0200 | [diff] [blame] | 62 | #if defined(CPU_HAS_PIO3) |
| 63 | if (use_pullup) |
| 64 | at91_set_pio_pulldown(port, pin, 0); |
| 65 | #endif |
| 66 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 67 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 68 | at91_set_port_pullup(at91_port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 69 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * mux the pin to the "GPIO" peripheral role. |
| 75 | */ |
| 76 | int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup) |
| 77 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 78 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 79 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 80 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 81 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 82 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 83 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 84 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 85 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 86 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 87 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * mux the pin to the "A" internal peripheral role. |
| 93 | */ |
| 94 | int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup) |
| 95 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 96 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 97 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 98 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 99 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 100 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 101 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 102 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 103 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 104 | writel(readl(&at91_port->abcdsr1) & ~mask, |
| 105 | &at91_port->abcdsr1); |
| 106 | writel(readl(&at91_port->abcdsr2) & ~mask, |
| 107 | &at91_port->abcdsr2); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 108 | #else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 109 | writel(mask, &at91_port->asr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 110 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 111 | writel(mask, &at91_port->pdr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 112 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 113 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * mux the pin to the "B" internal peripheral role. |
| 119 | */ |
| 120 | int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup) |
| 121 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 122 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 123 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 124 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 125 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 126 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 127 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 128 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 129 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 130 | writel(readl(&at91_port->abcdsr1) | mask, |
| 131 | &at91_port->abcdsr1); |
| 132 | writel(readl(&at91_port->abcdsr2) & ~mask, |
| 133 | &at91_port->abcdsr2); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 134 | #else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 135 | writel(mask, &at91_port->bsr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 136 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 137 | writel(mask, &at91_port->pdr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 138 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 139 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 143 | #if defined(CPU_HAS_PIO3) |
| 144 | /* |
| 145 | * mux the pin to the "C" internal peripheral role. |
| 146 | */ |
| 147 | int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup) |
| 148 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 149 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 150 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 151 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 152 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 153 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 154 | writel(mask, &at91_port->idr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 155 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 156 | writel(readl(&at91_port->abcdsr1) & ~mask, |
| 157 | &at91_port->abcdsr1); |
| 158 | writel(readl(&at91_port->abcdsr2) | mask, |
| 159 | &at91_port->abcdsr2); |
| 160 | writel(mask, &at91_port->pdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 161 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 162 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * mux the pin to the "D" internal peripheral role. |
| 168 | */ |
| 169 | int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup) |
| 170 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 171 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 172 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 173 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 174 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 175 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 176 | writel(mask, &at91_port->idr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 177 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 178 | writel(readl(&at91_port->abcdsr1) | mask, |
| 179 | &at91_port->abcdsr1); |
| 180 | writel(readl(&at91_port->abcdsr2) | mask, |
| 181 | &at91_port->abcdsr2); |
| 182 | writel(mask, &at91_port->pdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 183 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 184 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | #endif |
| 188 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 189 | #ifdef CONFIG_DM_GPIO |
| 190 | static bool at91_get_port_output(struct at91_port *at91_port, int offset) |
| 191 | { |
| 192 | u32 mask, val; |
| 193 | |
| 194 | mask = 1 << offset; |
| 195 | val = readl(&at91_port->osr); |
| 196 | return val & mask; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | static void at91_set_port_input(struct at91_port *at91_port, int offset, |
| 201 | int use_pullup) |
| 202 | { |
| 203 | u32 mask; |
| 204 | |
| 205 | mask = 1 << offset; |
| 206 | writel(mask, &at91_port->idr); |
| 207 | at91_set_port_pullup(at91_port, offset, use_pullup); |
| 208 | writel(mask, &at91_port->odr); |
| 209 | writel(mask, &at91_port->per); |
| 210 | } |
| 211 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 212 | /* |
| 213 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and |
| 214 | * configure it for an input. |
| 215 | */ |
| 216 | int at91_set_pio_input(unsigned port, u32 pin, int use_pullup) |
| 217 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 218 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 219 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 220 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 221 | at91_set_port_input(at91_port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 222 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 226 | static void at91_set_port_output(struct at91_port *at91_port, int offset, |
| 227 | int value) |
| 228 | { |
| 229 | u32 mask; |
| 230 | |
| 231 | mask = 1 << offset; |
| 232 | writel(mask, &at91_port->idr); |
| 233 | writel(mask, &at91_port->pudr); |
| 234 | if (value) |
| 235 | writel(mask, &at91_port->sodr); |
| 236 | else |
| 237 | writel(mask, &at91_port->codr); |
| 238 | writel(mask, &at91_port->oer); |
| 239 | writel(mask, &at91_port->per); |
| 240 | } |
| 241 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 242 | /* |
| 243 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), |
| 244 | * and configure it for an output. |
| 245 | */ |
| 246 | int at91_set_pio_output(unsigned port, u32 pin, int value) |
| 247 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 248 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 249 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 250 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 251 | at91_set_port_output(at91_port, pin, value); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 252 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * enable/disable the glitch filter. mostly used with IRQ handling. |
| 258 | */ |
| 259 | int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on) |
| 260 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 261 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 262 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 263 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 264 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 265 | mask = 1 << pin; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 266 | if (is_on) { |
| 267 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 268 | writel(mask, &at91_port->ifscdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 269 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 270 | writel(mask, &at91_port->ifer); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 271 | } else { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 272 | writel(mask, &at91_port->ifdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 273 | } |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 274 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 275 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 279 | #if defined(CPU_HAS_PIO3) |
| 280 | /* |
| 281 | * enable/disable the debounce filter. |
| 282 | */ |
| 283 | int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div) |
| 284 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 285 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 286 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 287 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 288 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 289 | mask = 1 << pin; |
| 290 | if (is_on) { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 291 | writel(mask, &at91_port->ifscer); |
| 292 | writel(div & PIO_SCDR_DIV, &at91_port->scdr); |
| 293 | writel(mask, &at91_port->ifer); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 294 | } else { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 295 | writel(mask, &at91_port->ifdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 298 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * enable/disable the pull-down. |
| 304 | * If pull-up already enabled while calling the function, we disable it. |
| 305 | */ |
| 306 | int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on) |
| 307 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 308 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 309 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 310 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 311 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 312 | mask = 1 << pin; |
Marek Vasut | 152ac5f | 2016-05-04 23:05:23 +0200 | [diff] [blame] | 313 | if (is_on) { |
| 314 | at91_set_pio_pullup(port, pin, 0); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 315 | writel(mask, &at91_port->ppder); |
Marek Vasut | 152ac5f | 2016-05-04 23:05:23 +0200 | [diff] [blame] | 316 | } else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 317 | writel(mask, &at91_port->ppddr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 318 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 319 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * disable Schmitt trigger |
| 325 | */ |
| 326 | int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin) |
| 327 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 328 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 329 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 330 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 331 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 332 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 333 | writel(readl(&at91_port->schmitt) | mask, |
| 334 | &at91_port->schmitt); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 335 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 336 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | #endif |
| 340 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 341 | /* |
| 342 | * enable/disable the multi-driver. This is only valid for output and |
| 343 | * allows the output pin to run as an open collector output. |
| 344 | */ |
| 345 | int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on) |
| 346 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 347 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 348 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 349 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 350 | if (at91_port && (pin < GPIO_PER_BANK)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 351 | mask = 1 << pin; |
| 352 | if (is_on) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 353 | writel(mask, &at91_port->mder); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 354 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 355 | writel(mask, &at91_port->mddr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 356 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 357 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 361 | static void at91_set_port_value(struct at91_port *at91_port, int offset, |
| 362 | int value) |
| 363 | { |
| 364 | u32 mask; |
| 365 | |
| 366 | mask = 1 << offset; |
| 367 | if (value) |
| 368 | writel(mask, &at91_port->sodr); |
| 369 | else |
| 370 | writel(mask, &at91_port->codr); |
| 371 | } |
| 372 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 373 | /* |
| 374 | * assuming the pin is muxed as a gpio output, set its value. |
| 375 | */ |
| 376 | int at91_set_pio_value(unsigned port, unsigned pin, int value) |
| 377 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 378 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 379 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 380 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 381 | at91_set_port_value(at91_port, pin, value); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 382 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 386 | static int at91_get_port_value(struct at91_port *at91_port, int offset) |
| 387 | { |
| 388 | u32 pdsr = 0, mask; |
| 389 | |
| 390 | mask = 1 << offset; |
| 391 | pdsr = readl(&at91_port->pdsr) & mask; |
| 392 | |
| 393 | return pdsr != 0; |
| 394 | } |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 395 | /* |
| 396 | * read the pin's value (works even if it's not muxed as a gpio). |
| 397 | */ |
| 398 | int at91_get_pio_value(unsigned port, unsigned pin) |
| 399 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 400 | struct at91_port *at91_port = at91_pio_get_port(port); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 401 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 402 | if (at91_port && (pin < GPIO_PER_BANK)) |
| 403 | return at91_get_port_value(at91_port, pin); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 404 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 405 | return 0; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 406 | } |
Bo Shen | 6edaea8 | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 407 | |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 408 | #ifndef CONFIG_DM_GPIO |
Bo Shen | 6edaea8 | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 409 | /* Common GPIO API */ |
| 410 | |
Bo Shen | 6edaea8 | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 411 | int gpio_request(unsigned gpio, const char *label) |
| 412 | { |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | int gpio_free(unsigned gpio) |
| 417 | { |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | int gpio_direction_input(unsigned gpio) |
| 422 | { |
| 423 | at91_set_pio_input(at91_gpio_to_port(gpio), |
| 424 | at91_gpio_to_pin(gpio), 0); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int gpio_direction_output(unsigned gpio, int value) |
| 429 | { |
| 430 | at91_set_pio_output(at91_gpio_to_port(gpio), |
| 431 | at91_gpio_to_pin(gpio), value); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | int gpio_get_value(unsigned gpio) |
| 436 | { |
| 437 | return at91_get_pio_value(at91_gpio_to_port(gpio), |
| 438 | at91_gpio_to_pin(gpio)); |
| 439 | } |
| 440 | |
| 441 | int gpio_set_value(unsigned gpio, int value) |
| 442 | { |
| 443 | at91_set_pio_value(at91_gpio_to_port(gpio), |
| 444 | at91_gpio_to_pin(gpio), value); |
| 445 | |
| 446 | return 0; |
| 447 | } |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 448 | #endif |
| 449 | |
| 450 | #ifdef CONFIG_DM_GPIO |
| 451 | |
| 452 | struct at91_port_priv { |
| 453 | struct at91_port *regs; |
| 454 | }; |
| 455 | |
| 456 | /* set GPIO pin 'gpio' as an input */ |
| 457 | static int at91_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 458 | { |
Axel Lin | d895821 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 459 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 460 | |
| 461 | at91_set_port_input(port->regs, offset, 0); |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /* set GPIO pin 'gpio' as an output, with polarity 'value' */ |
| 467 | static int at91_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 468 | int value) |
| 469 | { |
Axel Lin | d895821 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 470 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 471 | |
| 472 | at91_set_port_output(port->regs, offset, value); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /* read GPIO IN value of pin 'gpio' */ |
| 478 | static int at91_gpio_get_value(struct udevice *dev, unsigned offset) |
| 479 | { |
Axel Lin | d895821 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 480 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 481 | |
| 482 | return at91_get_port_value(port->regs, offset); |
| 483 | } |
| 484 | |
| 485 | /* write GPIO OUT value to pin 'gpio' */ |
| 486 | static int at91_gpio_set_value(struct udevice *dev, unsigned offset, |
| 487 | int value) |
| 488 | { |
Axel Lin | d895821 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 489 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 490 | |
| 491 | at91_set_port_value(port->regs, offset, value); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int at91_gpio_get_function(struct udevice *dev, unsigned offset) |
| 497 | { |
Axel Lin | d895821 | 2015-01-31 14:47:34 +0800 | [diff] [blame] | 498 | struct at91_port_priv *port = dev_get_priv(dev); |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 499 | |
| 500 | /* GPIOF_FUNC is not implemented yet */ |
| 501 | if (at91_get_port_output(port->regs, offset)) |
| 502 | return GPIOF_OUTPUT; |
| 503 | else |
| 504 | return GPIOF_INPUT; |
| 505 | } |
| 506 | |
| 507 | static const struct dm_gpio_ops gpio_at91_ops = { |
| 508 | .direction_input = at91_gpio_direction_input, |
| 509 | .direction_output = at91_gpio_direction_output, |
| 510 | .get_value = at91_gpio_get_value, |
| 511 | .set_value = at91_gpio_set_value, |
| 512 | .get_function = at91_gpio_get_function, |
| 513 | }; |
| 514 | |
| 515 | static int at91_gpio_probe(struct udevice *dev) |
| 516 | { |
| 517 | struct at91_port_priv *port = dev_get_priv(dev); |
| 518 | struct at91_port_platdata *plat = dev_get_platdata(dev); |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 519 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 918354b | 2014-10-29 13:08:57 -0600 | [diff] [blame] | 520 | |
| 521 | uc_priv->bank_name = plat->bank_name; |
| 522 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 523 | port->regs = (struct at91_port *)plat->base_addr; |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | U_BOOT_DRIVER(gpio_at91) = { |
| 529 | .name = "gpio_at91", |
| 530 | .id = UCLASS_GPIO, |
| 531 | .ops = &gpio_at91_ops, |
| 532 | .probe = at91_gpio_probe, |
| 533 | .priv_auto_alloc_size = sizeof(struct at91_port_priv), |
| 534 | }; |
| 535 | #endif |