1 /* 2 * arch/arm/plat-orion/gpio.c 3 * 4 * Marvell Orion SoC GPIO handling. 5 * 6 * This file is licensed under the terms of the GNU General Public 7 * License version 2. This program is licensed "as is" without any 8 * warranty of any kind, whether express or implied. 9 */ 10 11 /* 12 * Based on (mostly copied from) plat-orion based Linux 2.6 kernel driver. 13 * Removed orion_gpiochip struct and kernel level irq handling. 14 * 15 * Dieter Kiermaier dk-arm-linux@gmx.de 16 */ 17 18 #include <common.h> 19 #include <asm/bitops.h> 20 #include <asm/arch/kirkwood.h> 21 #include <asm/arch/gpio.h> 22 23 static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)]; 24 static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)]; 25 26 void __set_direction(unsigned pin, int input) 27 { 28 u32 u; 29 30 u = readl(GPIO_IO_CONF(pin)); 31 if (input) 32 u |= 1 << (pin & 31); 33 else 34 u &= ~(1 << (pin & 31)); 35 writel(u, GPIO_IO_CONF(pin)); 36 37 u = readl(GPIO_IO_CONF(pin)); 38 } 39 40 void __set_level(unsigned pin, int high) 41 { 42 u32 u; 43 44 u = readl(GPIO_OUT(pin)); 45 if (high) 46 u |= 1 << (pin & 31); 47 else 48 u &= ~(1 << (pin & 31)); 49 writel(u, GPIO_OUT(pin)); 50 } 51 52 void __set_blinking(unsigned pin, int blink) 53 { 54 u32 u; 55 56 u = readl(GPIO_BLINK_EN(pin)); 57 if (blink) 58 u |= 1 << (pin & 31); 59 else 60 u &= ~(1 << (pin & 31)); 61 writel(u, GPIO_BLINK_EN(pin)); 62 } 63 64 int kw_gpio_is_valid(unsigned pin, int mode) 65 { 66 if (pin < GPIO_MAX) { 67 if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input)) 68 goto err_out; 69 70 if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output)) 71 goto err_out; 72 return 0; 73 } 74 75 err_out: 76 printf("%s: invalid GPIO %d\n", __func__, pin); 77 return 1; 78 } 79 80 void kw_gpio_set_valid(unsigned pin, int mode) 81 { 82 if (mode == 1) 83 mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK; 84 if (mode & GPIO_INPUT_OK) 85 __set_bit(pin, gpio_valid_input); 86 else 87 __clear_bit(pin, gpio_valid_input); 88 if (mode & GPIO_OUTPUT_OK) 89 __set_bit(pin, gpio_valid_output); 90 else 91 __clear_bit(pin, gpio_valid_output); 92 } 93 /* 94 * GENERIC_GPIO primitives. 95 */ 96 int kw_gpio_direction_input(unsigned pin) 97 { 98 if (!kw_gpio_is_valid(pin, GPIO_INPUT_OK)) 99 return 1; 100 101 /* Configure GPIO direction. */ 102 __set_direction(pin, 1); 103 104 return 0; 105 } 106 107 int kw_gpio_direction_output(unsigned pin, int value) 108 { 109 if (kw_gpio_is_valid(pin, GPIO_OUTPUT_OK) != 0) 110 { 111 printf("%s: invalid GPIO %d\n", __func__, pin); 112 return 1; 113 } 114 115 __set_blinking(pin, 0); 116 117 /* Configure GPIO output value. */ 118 __set_level(pin, value); 119 120 /* Configure GPIO direction. */ 121 __set_direction(pin, 0); 122 123 return 0; 124 } 125 126 int kw_gpio_get_value(unsigned pin) 127 { 128 int val; 129 130 if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31))) 131 val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin)); 132 else 133 val = readl(GPIO_OUT(pin)); 134 135 return (val >> (pin & 31)) & 1; 136 } 137 138 void kw_gpio_set_value(unsigned pin, int value) 139 { 140 /* Configure GPIO output value. */ 141 __set_level(pin, value); 142 } 143 144 void kw_gpio_set_blink(unsigned pin, int blink) 145 { 146 /* Set output value to zero. */ 147 __set_level(pin, 0); 148 149 /* Set blinking. */ 150 __set_blinking(pin, blink); 151 } 152