Gabriel Huau | dd15930 | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 |
| 3 | * Gabriel Huau <contact@huau-gabriel.fr> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Gabriel Huau | dd15930 | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 6 | */ |
| 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 | |
| 27 | int 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 Lin | e7fe8c5 | 2013-06-15 17:56:59 +0800 | [diff] [blame] | 48 | return writel(l, port); |
Gabriel Huau | dd15930 | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int 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 | |
| 60 | int gpio_request(unsigned gpio, const char *label) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int gpio_free(unsigned gpio) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int gpio_direction_input(unsigned gpio) |
| 71 | { |
Axel Lin | e7fe8c5 | 2013-06-15 17:56:59 +0800 | [diff] [blame] | 72 | return writel(GPIO_INPUT << GPIO_BIT(gpio), GPIO_FULLPORT(gpio)); |
Gabriel Huau | dd15930 | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int gpio_direction_output(unsigned gpio, int value) |
| 76 | { |
Axel Lin | e7fe8c5 | 2013-06-15 17:56:59 +0800 | [diff] [blame] | 77 | writel(GPIO_OUTPUT << GPIO_BIT(gpio), GPIO_FULLPORT(gpio)); |
Gabriel Huau | dd15930 | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 78 | return gpio_set_value(gpio, value); |
| 79 | } |