Haavard Skinnemoen | ab0df36 | 2008-08-29 21:09:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006, 2008 Atmel Corporation |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | #ifndef __AVR32_PORTMUX_PIO_H__ |
| 23 | #define __AVR32_PORTMUX_PIO_H__ |
| 24 | |
| 25 | #include <asm/io.h> |
| 26 | |
| 27 | /* PIO register offsets */ |
| 28 | #define PIO_PER 0x0000 |
| 29 | #define PIO_PDR 0x0004 |
| 30 | #define PIO_PSR 0x0008 |
| 31 | #define PIO_OER 0x0010 |
| 32 | #define PIO_ODR 0x0014 |
| 33 | #define PIO_OSR 0x0018 |
| 34 | #define PIO_IFER 0x0020 |
| 35 | #define PIO_IFDR 0x0024 |
| 36 | #define PIO_ISFR 0x0028 |
| 37 | #define PIO_SODR 0x0030 |
| 38 | #define PIO_CODR 0x0034 |
| 39 | #define PIO_ODSR 0x0038 |
| 40 | #define PIO_PDSR 0x003c |
| 41 | #define PIO_IER 0x0040 |
| 42 | #define PIO_IDR 0x0044 |
| 43 | #define PIO_IMR 0x0048 |
| 44 | #define PIO_ISR 0x004c |
| 45 | #define PIO_MDER 0x0050 |
| 46 | #define PIO_MDDR 0x0054 |
| 47 | #define PIO_MDSR 0x0058 |
| 48 | #define PIO_PUDR 0x0060 |
| 49 | #define PIO_PUER 0x0064 |
| 50 | #define PIO_PUSR 0x0068 |
| 51 | #define PIO_ASR 0x0070 |
| 52 | #define PIO_BSR 0x0074 |
| 53 | #define PIO_ABSR 0x0078 |
| 54 | #define PIO_OWER 0x00a0 |
| 55 | #define PIO_OWDR 0x00a4 |
| 56 | #define PIO_OWSR 0x00a8 |
| 57 | |
| 58 | /* Hardware register access */ |
| 59 | #define pio_readl(base, reg) \ |
| 60 | __raw_readl((void *)base + PIO_##reg) |
| 61 | #define pio_writel(base, reg, value) \ |
| 62 | __raw_writel((value), (void *)base + PIO_##reg) |
| 63 | |
| 64 | /* Portmux API starts here. See doc/README.AVR32-port-muxing */ |
| 65 | |
| 66 | enum portmux_function { |
| 67 | PORTMUX_FUNC_A, |
| 68 | PORTMUX_FUNC_B, |
| 69 | }; |
| 70 | |
| 71 | /* Pull-down, buskeeper and drive strength are not supported */ |
| 72 | #define PORTMUX_DIR_INPUT (0 << 0) |
| 73 | #define PORTMUX_DIR_OUTPUT (1 << 0) |
| 74 | #define PORTMUX_INIT_LOW (0 << 1) |
| 75 | #define PORTMUX_INIT_HIGH (1 << 1) |
| 76 | #define PORTMUX_PULL_UP (1 << 2) |
| 77 | #define PORTMUX_PULL_DOWN (0) |
| 78 | #define PORTMUX_BUSKEEPER PORTMUX_PULL_UP |
| 79 | #define PORTMUX_DRIVE_MIN (0) |
| 80 | #define PORTMUX_DRIVE_LOW (0) |
| 81 | #define PORTMUX_DRIVE_HIGH (0) |
| 82 | #define PORTMUX_DRIVE_MAX (0) |
| 83 | #define PORTMUX_OPEN_DRAIN (1 << 3) |
| 84 | |
| 85 | void portmux_select_peripheral(void *port, unsigned long pin_mask, |
| 86 | enum portmux_function func, unsigned long flags); |
| 87 | void portmux_select_gpio(void *port, unsigned long pin_mask, |
| 88 | unsigned long flags); |
| 89 | |
| 90 | /* Internal helper functions */ |
| 91 | |
| 92 | static inline void __pio_set_output_value(void *port, unsigned int pin, |
| 93 | int value) |
| 94 | { |
| 95 | /* |
| 96 | * value will usually be constant, but it's pretty cheap |
| 97 | * either way. |
| 98 | */ |
| 99 | if (value) |
| 100 | pio_writel(port, SODR, 1 << pin); |
| 101 | else |
| 102 | pio_writel(port, CODR, 1 << pin); |
| 103 | } |
| 104 | |
| 105 | static inline int __pio_get_input_value(void *port, unsigned int pin) |
| 106 | { |
| 107 | return (pio_readl(port, PDSR) >> pin) & 1; |
| 108 | } |
| 109 | |
| 110 | void pio_set_output_value(unsigned int pin, int value); |
| 111 | int pio_get_input_value(unsigned int pin); |
| 112 | |
| 113 | /* GPIO API starts here */ |
| 114 | |
| 115 | /* |
| 116 | * GCC doesn't realize that the constant case is extremely trivial, |
| 117 | * so we need to help it make the right decision by using |
| 118 | * always_inline. |
| 119 | */ |
| 120 | __attribute__((always_inline)) |
| 121 | static inline void gpio_set_value(unsigned int pin, int value) |
| 122 | { |
| 123 | if (__builtin_constant_p(pin)) |
| 124 | __pio_set_output_value(pio_pin_to_port(pin), pin & 0x1f, value); |
| 125 | else |
| 126 | pio_set_output_value(pin, value); |
| 127 | } |
| 128 | |
| 129 | __attribute__((always_inline)) |
| 130 | static inline int gpio_get_value(unsigned int pin) |
| 131 | { |
| 132 | if (__builtin_constant_p(pin)) |
| 133 | return __pio_get_input_value(pio_pin_to_port(pin), pin & 0x1f); |
| 134 | else |
| 135 | return pio_get_input_value(pin); |
| 136 | } |
| 137 | |
| 138 | #endif /* __AVR32_PORTMUX_PIO_H__ */ |