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