Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | ae691e5 | 2009-01-21 17:24:49 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering |
Stefan Roese | ae691e5 | 2009-01-21 17:24:49 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <asm/io.h> |
| 8 | #include "vct.h" |
| 9 | |
| 10 | /* |
| 11 | * Find out to which of the 2 gpio modules the pin specified in the |
| 12 | * argument belongs: |
| 13 | * GPIO_MODULE yields 0 for pins 0 to 31, |
| 14 | * 1 for pins 32 to 63 |
| 15 | */ |
| 16 | #define GPIO_MODULE(pin) ((pin) >> 5) |
| 17 | |
| 18 | /* |
| 19 | * Bit position within a 32-bit peripheral register (where every |
| 20 | * bit is one bitslice) |
| 21 | */ |
| 22 | #define MASK(pin) (1 << ((pin) & 0x1F)) |
| 23 | #define BASE_ADDR(mod) module_base[mod] |
| 24 | |
| 25 | /* |
| 26 | * Lookup table for transforming gpio module number 0 to 2 to |
| 27 | * address offsets |
| 28 | */ |
| 29 | static u32 module_base[] = { |
| 30 | GPIO1_BASE, |
| 31 | GPIO2_BASE |
| 32 | }; |
| 33 | |
| 34 | static void clrsetbits(u32 addr, u32 and_mask, u32 or_mask) |
| 35 | { |
| 36 | reg_write(addr, (reg_read(addr) & ~and_mask) | or_mask); |
| 37 | } |
| 38 | |
| 39 | int vct_gpio_dir(int pin, int dir) |
| 40 | { |
| 41 | u32 gpio_base; |
| 42 | |
| 43 | gpio_base = BASE_ADDR(GPIO_MODULE(pin)); |
| 44 | |
| 45 | if (dir == 0) |
| 46 | clrsetbits(GPIO_SWPORTA_DDR(gpio_base), MASK(pin), 0); |
| 47 | else |
| 48 | clrsetbits(GPIO_SWPORTA_DDR(gpio_base), 0, MASK(pin)); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | void vct_gpio_set(int pin, int val) |
| 54 | { |
| 55 | u32 gpio_base; |
| 56 | |
| 57 | gpio_base = BASE_ADDR(GPIO_MODULE(pin)); |
| 58 | |
| 59 | if (val == 0) |
| 60 | clrsetbits(GPIO_SWPORTA_DR(gpio_base), MASK(pin), 0); |
| 61 | else |
| 62 | clrsetbits(GPIO_SWPORTA_DR(gpio_base), 0, MASK(pin)); |
| 63 | } |
| 64 | |
| 65 | int vct_gpio_get(int pin) |
| 66 | { |
| 67 | u32 gpio_base; |
| 68 | u32 value; |
| 69 | |
| 70 | gpio_base = BASE_ADDR(GPIO_MODULE(pin)); |
| 71 | value = reg_read(GPIO_EXT_PORTA(gpio_base)); |
| 72 | |
| 73 | return ((value & MASK(pin)) ? 1 : 0); |
| 74 | } |