Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 |
| 3 | * Gabriel Huau <contact@huau-gabriel.fr> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Gabriel Huau | 5d889ae | 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> |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 11 | #include <errno.h> |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 12 | |
| 13 | #define GPIO_INPUT 0x0 |
| 14 | #define GPIO_OUTPUT 0x1 |
| 15 | |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 16 | #define S3C_GPIO_CON 0x0 |
| 17 | #define S3C_GPIO_DAT 0x4 |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 18 | |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 19 | static uint32_t s3c_gpio_get_bank_addr(unsigned gpio) |
| 20 | { |
| 21 | /* There is up to 16 pins per bank, one bank is 0x10 big. */ |
| 22 | uint32_t addr = gpio & ~0xf; |
| 23 | |
| 24 | if (addr >= 0x80 && addr != 0xd0) { /* Wrong GPIO bank. */ |
| 25 | printf("Invalid GPIO bank (bank %02x)\n", addr); |
| 26 | return 0xffffffff; |
| 27 | } |
| 28 | |
| 29 | return addr | S3C24X0_GPIO_BASE; |
| 30 | } |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 31 | |
| 32 | int gpio_set_value(unsigned gpio, int value) |
| 33 | { |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 34 | uint32_t addr = s3c_gpio_get_bank_addr(gpio); |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 35 | |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 36 | if (addr == 0xffffffff) |
| 37 | return -EINVAL; |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 38 | |
| 39 | if (value) |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 40 | setbits_le32(addr | S3C_GPIO_DAT, 1 << (gpio & 0xf)); |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 41 | else |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 42 | clrbits_le32(addr | S3C_GPIO_DAT, 1 << (gpio & 0xf)); |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 43 | |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 44 | return 0; |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int gpio_get_value(unsigned gpio) |
| 48 | { |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 49 | uint32_t addr = s3c_gpio_get_bank_addr(gpio); |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 50 | |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 51 | if (addr == 0xffffffff) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | return !!(readl(addr | S3C_GPIO_DAT) & (1 << (gpio & 0xf))); |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | int gpio_request(unsigned gpio, const char *label) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int gpio_free(unsigned gpio) |
| 63 | { |
| 64 | return 0; |
| 65 | } |
| 66 | |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 67 | static int s3c_gpio_direction(unsigned gpio, uint8_t dir) |
| 68 | { |
| 69 | uint32_t addr = s3c_gpio_get_bank_addr(gpio); |
| 70 | const uint32_t mask = 0x3 << ((gpio & 0xf) << 1); |
| 71 | const uint32_t dirm = dir << ((gpio & 0xf) << 1); |
| 72 | |
| 73 | if (addr == 0xffffffff) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | clrsetbits_le32(addr | S3C_GPIO_CON, mask, dirm); |
| 77 | return 0; |
| 78 | } |
| 79 | |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 80 | int gpio_direction_input(unsigned gpio) |
| 81 | { |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 82 | return s3c_gpio_direction(gpio, GPIO_INPUT); |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int gpio_direction_output(unsigned gpio, int value) |
| 86 | { |
Marek Vasut | 13cfd10 | 2014-10-11 18:42:57 +0200 | [diff] [blame] | 87 | return s3c_gpio_direction(gpio, GPIO_OUTPUT); |
Gabriel Huau | 5d889ae | 2012-05-02 10:49:55 +0000 | [diff] [blame] | 88 | } |