blob: e1e2d3f805e81ec7c81071fb46e4919670c75a67 [file] [log] [blame]
Gabriel Huaudd159302012-05-02 10:49:55 +00001/*
2 * Copyright (C) 2012
3 * Gabriel Huau <contact@huau-gabriel.fr>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Gabriel Huaudd159302012-05-02 10:49:55 +00006 */
7#include <common.h>
8#include <asm/arch/s3c2440.h>
9#include <asm/gpio.h>
10#include <asm/io.h>
11
12#define GPIO_INPUT 0x0
13#define GPIO_OUTPUT 0x1
14
15/* 0x4 means that we want DAT and not CON register */
16#define GPIO_PORT(x) ((((x) >> 5) & 0x3) + 0x4)
17#define GPIO_BIT(x) ((x) & 0x3f)
18
19/*
20 * It's how we calculate the full port address
21 * We have to get the number of the port + 1 (Port A is at 0x56000001 ...)
22 * We move it at the second digit, and finally we add 0x4 because we want
23 * to modify GPIO DAT and not CON
24 */
25#define GPIO_FULLPORT(x) (S3C24X0_GPIO_BASE | ((GPIO_PORT(gpio) + 1) << 1))
26
27int gpio_set_value(unsigned gpio, int value)
28{
29 unsigned l = readl(GPIO_FULLPORT(gpio));
30 unsigned bit;
31 unsigned port = GPIO_FULLPORT(gpio);
32
33 /*
34 * All GPIO Port have a configuration on
35 * 2 bits excepted the first GPIO (A) which
36 * have only 1 bit of configuration.
37 */
38 if (!GPIO_PORT(gpio))
39 bit = (0x1 << GPIO_BIT(gpio));
40 else
41 bit = (0x3 << GPIO_BIT(gpio));
42
43 if (value)
44 l |= bit;
45 else
46 l &= ~bit;
47
Axel Line7fe8c52013-06-15 17:56:59 +080048 return writel(l, port);
Gabriel Huaudd159302012-05-02 10:49:55 +000049}
50
51int gpio_get_value(unsigned gpio)
52{
53 unsigned l = readl(GPIO_FULLPORT(gpio));
54
55 if (GPIO_PORT(gpio) == 0) /* PORT A */
56 return (l >> GPIO_BIT(gpio)) & 0x1;
57 return (l >> GPIO_BIT(gpio)) & 0x3;
58}
59
60int gpio_request(unsigned gpio, const char *label)
61{
62 return 0;
63}
64
65int gpio_free(unsigned gpio)
66{
67 return 0;
68}
69
70int gpio_direction_input(unsigned gpio)
71{
Axel Line7fe8c52013-06-15 17:56:59 +080072 return writel(GPIO_INPUT << GPIO_BIT(gpio), GPIO_FULLPORT(gpio));
Gabriel Huaudd159302012-05-02 10:49:55 +000073}
74
75int gpio_direction_output(unsigned gpio, int value)
76{
Axel Line7fe8c52013-06-15 17:56:59 +080077 writel(GPIO_OUTPUT << GPIO_BIT(gpio), GPIO_FULLPORT(gpio));
Gabriel Huaudd159302012-05-02 10:49:55 +000078 return gpio_set_value(gpio, value);
79}