1/* 2 * Copyright (C) 2012 3 * Gabriel Huau <contact@huau-gabriel.fr> 4 * 5 * See file CREDITS for list of people who contributed to this 6 * project. 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of 11 * the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 * MA 02111-1307 USA 22 */ 23#include <common.h> 24#include <asm/arch/s3c2440.h> 25#include <asm/gpio.h> 26#include <asm/io.h> 27 28#define GPIO_INPUT 0x0 29#define GPIO_OUTPUT 0x1 30 31/* 0x4 means that we want DAT and not CON register */ 32#define GPIO_PORT(x) ((((x) >> 5) & 0x3) + 0x4) 33#define GPIO_BIT(x) ((x) & 0x3f) 34 35/* 36 * It's how we calculate the full port address 37 * We have to get the number of the port + 1 (Port A is at 0x56000001 ...) 38 * We move it at the second digit, and finally we add 0x4 because we want 39 * to modify GPIO DAT and not CON 40 */ 41#define GPIO_FULLPORT(x) (S3C24X0_GPIO_BASE | ((GPIO_PORT(gpio) + 1) << 1)) 42 43int gpio_set_value(unsigned gpio, int value) 44{ 45 unsigned l = readl(GPIO_FULLPORT(gpio)); 46 unsigned bit; 47 unsigned port = GPIO_FULLPORT(gpio); 48 49 /* 50 * All GPIO Port have a configuration on 51 * 2 bits excepted the first GPIO (A) which 52 * have only 1 bit of configuration. 53 */ 54 if (!GPIO_PORT(gpio)) 55 bit = (0x1 << GPIO_BIT(gpio)); 56 else 57 bit = (0x3 << GPIO_BIT(gpio)); 58 59 if (value) 60 l |= bit; 61 else 62 l &= ~bit; 63 64 return writel(port, l); 65} 66 67int gpio_get_value(unsigned gpio) 68{ 69 unsigned l = readl(GPIO_FULLPORT(gpio)); 70 71 if (GPIO_PORT(gpio) == 0) /* PORT A */ 72 return (l >> GPIO_BIT(gpio)) & 0x1; 73 return (l >> GPIO_BIT(gpio)) & 0x3; 74} 75 76int gpio_request(unsigned gpio, const char *label) 77{ 78 return 0; 79} 80 81int gpio_free(unsigned gpio) 82{ 83 return 0; 84} 85 86int gpio_direction_input(unsigned gpio) 87{ 88 return writel(GPIO_FULLPORT(gpio), GPIO_INPUT << GPIO_BIT(gpio)); 89} 90 91int gpio_direction_output(unsigned gpio, int value) 92{ 93 writel(GPIO_FULLPORT(gpio), GPIO_OUTPUT << GPIO_BIT(gpio)); 94 return gpio_set_value(gpio, value); 95} 96