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> |
Reinhard Meyer | 86592f6 | 2010-11-07 13:26:14 +0100 | [diff] [blame] | 13 | #include <asm/io.h> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 14 | #include <asm/sizes.h> |
| 15 | #include <asm/arch/hardware.h> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 16 | #include <asm/arch/at91_pio.h> |
Andreas Bießmann | 9ecc922 | 2013-11-29 12:13:43 +0100 | [diff] [blame] | 17 | #include <asm/arch/gpio.h> |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 18 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 19 | static struct at91_port *at91_pio_get_port(unsigned port) |
| 20 | { |
| 21 | switch (port) { |
| 22 | case AT91_PIO_PORTA: |
| 23 | return (struct at91_port *)ATMEL_BASE_PIOA; |
| 24 | case AT91_PIO_PORTB: |
| 25 | return (struct at91_port *)ATMEL_BASE_PIOB; |
| 26 | case AT91_PIO_PORTC: |
| 27 | return (struct at91_port *)ATMEL_BASE_PIOC; |
| 28 | #if (ATMEL_PIO_PORTS > 3) |
| 29 | case AT91_PIO_PORTD: |
| 30 | return (struct at91_port *)ATMEL_BASE_PIOD; |
| 31 | #if (ATMEL_PIO_PORTS > 4) |
| 32 | case AT91_PIO_PORTE: |
| 33 | return (struct at91_port *)ATMEL_BASE_PIOE; |
| 34 | #endif |
| 35 | #endif |
| 36 | default: |
| 37 | return NULL; |
| 38 | } |
| 39 | } |
| 40 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 41 | int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup) |
| 42 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 43 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 44 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 45 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 46 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 47 | mask = 1 << pin; |
| 48 | if (use_pullup) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 49 | writel(1 << pin, &at91_port->puer); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 50 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 51 | writel(1 << pin, &at91_port->pudr); |
| 52 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 53 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 54 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * mux the pin to the "GPIO" peripheral role. |
| 60 | */ |
| 61 | int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup) |
| 62 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 63 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 64 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 65 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 66 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 67 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 68 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 69 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 70 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 71 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 72 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * mux the pin to the "A" internal peripheral role. |
| 78 | */ |
| 79 | int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup) |
| 80 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 81 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 82 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 83 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 84 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 85 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 86 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 87 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 88 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 89 | writel(readl(&at91_port->abcdsr1) & ~mask, |
| 90 | &at91_port->abcdsr1); |
| 91 | writel(readl(&at91_port->abcdsr2) & ~mask, |
| 92 | &at91_port->abcdsr2); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 93 | #else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 94 | writel(mask, &at91_port->asr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 95 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 96 | writel(mask, &at91_port->pdr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 97 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 98 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * mux the pin to the "B" internal peripheral role. |
| 104 | */ |
| 105 | int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup) |
| 106 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 107 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 108 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 109 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 110 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 111 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 112 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 113 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 114 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 115 | writel(readl(&at91_port->abcdsr1) | mask, |
| 116 | &at91_port->abcdsr1); |
| 117 | writel(readl(&at91_port->abcdsr2) & ~mask, |
| 118 | &at91_port->abcdsr2); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 119 | #else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 120 | writel(mask, &at91_port->bsr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 121 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 122 | writel(mask, &at91_port->pdr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 123 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 124 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 128 | #if defined(CPU_HAS_PIO3) |
| 129 | /* |
| 130 | * mux the pin to the "C" internal peripheral role. |
| 131 | */ |
| 132 | int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup) |
| 133 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 134 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 135 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 136 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 137 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 138 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 139 | writel(mask, &at91_port->idr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 140 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 141 | writel(readl(&at91_port->abcdsr1) & ~mask, |
| 142 | &at91_port->abcdsr1); |
| 143 | writel(readl(&at91_port->abcdsr2) | mask, |
| 144 | &at91_port->abcdsr2); |
| 145 | writel(mask, &at91_port->pdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 146 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 147 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * mux the pin to the "D" internal peripheral role. |
| 153 | */ |
| 154 | int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup) |
| 155 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 156 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 157 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 158 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 159 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 160 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 161 | writel(mask, &at91_port->idr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 162 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 163 | writel(readl(&at91_port->abcdsr1) | mask, |
| 164 | &at91_port->abcdsr1); |
| 165 | writel(readl(&at91_port->abcdsr2) | mask, |
| 166 | &at91_port->abcdsr2); |
| 167 | writel(mask, &at91_port->pdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 168 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 169 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | #endif |
| 173 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 174 | /* |
| 175 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and |
| 176 | * configure it for an input. |
| 177 | */ |
| 178 | int at91_set_pio_input(unsigned port, u32 pin, int use_pullup) |
| 179 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 180 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 181 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 182 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 183 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 184 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 185 | writel(mask, &at91_port->idr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 186 | at91_set_pio_pullup(port, pin, use_pullup); |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 187 | writel(mask, &at91_port->odr); |
| 188 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 189 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 190 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * mux the pin to the gpio controller (instead of "A" or "B" peripheral), |
| 196 | * and configure it for an output. |
| 197 | */ |
| 198 | int at91_set_pio_output(unsigned port, u32 pin, int value) |
| 199 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 200 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 201 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 202 | |
Reinhard Meyer | 372f278 | 2010-11-03 15:47:20 +0100 | [diff] [blame] | 203 | if ((port < ATMEL_PIO_PORTS) && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 204 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 205 | writel(mask, &at91_port->idr); |
| 206 | writel(mask, &at91_port->pudr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 207 | if (value) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 208 | writel(mask, &at91_port->sodr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 209 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 210 | writel(mask, &at91_port->codr); |
| 211 | writel(mask, &at91_port->oer); |
| 212 | writel(mask, &at91_port->per); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 213 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 214 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * enable/disable the glitch filter. mostly used with IRQ handling. |
| 220 | */ |
| 221 | int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on) |
| 222 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 223 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 224 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 225 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 226 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 227 | mask = 1 << pin; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 228 | if (is_on) { |
| 229 | #if defined(CPU_HAS_PIO3) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 230 | writel(mask, &at91_port->ifscdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 231 | #endif |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 232 | writel(mask, &at91_port->ifer); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 233 | } else { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 234 | writel(mask, &at91_port->ifdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 235 | } |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 236 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 237 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 241 | #if defined(CPU_HAS_PIO3) |
| 242 | /* |
| 243 | * enable/disable the debounce filter. |
| 244 | */ |
| 245 | int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div) |
| 246 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 247 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 248 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 249 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 250 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 251 | mask = 1 << pin; |
| 252 | if (is_on) { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 253 | writel(mask, &at91_port->ifscer); |
| 254 | writel(div & PIO_SCDR_DIV, &at91_port->scdr); |
| 255 | writel(mask, &at91_port->ifer); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 256 | } else { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 257 | writel(mask, &at91_port->ifdr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 260 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * enable/disable the pull-down. |
| 266 | * If pull-up already enabled while calling the function, we disable it. |
| 267 | */ |
| 268 | int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on) |
| 269 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 270 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 271 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 272 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 273 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 274 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 275 | writel(mask, &at91_port->pudr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 276 | if (is_on) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 277 | writel(mask, &at91_port->ppder); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 278 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 279 | writel(mask, &at91_port->ppddr); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 280 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 281 | |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * disable Schmitt trigger |
| 287 | */ |
| 288 | int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin) |
| 289 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 290 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 291 | u32 mask; |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 292 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 293 | if (at91_port && (pin < 32)) { |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 294 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 295 | writel(readl(&at91_port->schmitt) | mask, |
| 296 | &at91_port->schmitt); |
Bo Shen | 2b3b1c6 | 2012-05-20 15:50:00 +0000 | [diff] [blame] | 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 | #endif |
| 302 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 303 | /* |
| 304 | * enable/disable the multi-driver. This is only valid for output and |
| 305 | * allows the output pin to run as an open collector output. |
| 306 | */ |
| 307 | int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on) |
| 308 | { |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 309 | struct at91_port *at91_port = at91_pio_get_port(port); |
| 310 | u32 mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 311 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 312 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 313 | mask = 1 << pin; |
| 314 | if (is_on) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 315 | writel(mask, &at91_port->mder); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 316 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 317 | writel(mask, &at91_port->mddr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 318 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 319 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * assuming the pin is muxed as a gpio output, set its value. |
| 325 | */ |
| 326 | int at91_set_pio_value(unsigned port, unsigned pin, int value) |
| 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; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 330 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 331 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 332 | mask = 1 << pin; |
| 333 | if (value) |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 334 | writel(mask, &at91_port->sodr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 335 | else |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 336 | writel(mask, &at91_port->codr); |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 337 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 338 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * read the pin's value (works even if it's not muxed as a gpio). |
| 344 | */ |
| 345 | int at91_get_pio_value(unsigned port, unsigned pin) |
| 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 pdsr = 0, mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 349 | |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 350 | if (at91_port && (pin < 32)) { |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 351 | mask = 1 << pin; |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 352 | pdsr = readl(&at91_port->pdsr) & mask; |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 353 | } |
Bo Shen | 4bc9b7a | 2013-08-22 15:24:40 +0800 | [diff] [blame] | 354 | |
Jens Scharsig | ea8fbba | 2010-02-03 22:46:16 +0100 | [diff] [blame] | 355 | return pdsr != 0; |
| 356 | } |
Bo Shen | 6edaea8 | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 357 | |
| 358 | /* Common GPIO API */ |
| 359 | |
Bo Shen | 6edaea8 | 2013-08-13 14:38:31 +0800 | [diff] [blame] | 360 | int gpio_request(unsigned gpio, const char *label) |
| 361 | { |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | int gpio_free(unsigned gpio) |
| 366 | { |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int gpio_direction_input(unsigned gpio) |
| 371 | { |
| 372 | at91_set_pio_input(at91_gpio_to_port(gpio), |
| 373 | at91_gpio_to_pin(gpio), 0); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | int gpio_direction_output(unsigned gpio, int value) |
| 378 | { |
| 379 | at91_set_pio_output(at91_gpio_to_port(gpio), |
| 380 | at91_gpio_to_pin(gpio), value); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | int gpio_get_value(unsigned gpio) |
| 385 | { |
| 386 | return at91_get_pio_value(at91_gpio_to_port(gpio), |
| 387 | at91_gpio_to_pin(gpio)); |
| 388 | } |
| 389 | |
| 390 | int gpio_set_value(unsigned gpio, int value) |
| 391 | { |
| 392 | at91_set_pio_value(at91_gpio_to_port(gpio), |
| 393 | at91_gpio_to_pin(gpio), value); |
| 394 | |
| 395 | return 0; |
| 396 | } |