Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 |
| 3 | * eInfochips Ltd. <www.einfochips.com> |
| 4 | * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com> |
| 5 | * |
| 6 | * (C) Copyright 2010 |
| 7 | * Marvell Semiconductor <www.marvell.com> |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 25 | * MA 02110-1301 USA |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <asm/io.h> |
| 30 | #include <asm/errno.h> |
| 31 | #include "mvgpio.h" |
| 32 | #include <asm/gpio.h> |
| 33 | |
| 34 | #ifndef MV_MAX_GPIO |
| 35 | #define MV_MAX_GPIO 128 |
| 36 | #endif |
| 37 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 38 | int gpio_request(unsigned gpio, const char *label) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 39 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 40 | if (gpio >= MV_MAX_GPIO) { |
| 41 | printf("%s: Invalid GPIO requested %d\n", __func__, gpio); |
| 42 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 47 | int gpio_free(unsigned gpio) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 48 | { |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 49 | return 0; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 50 | } |
| 51 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 52 | int gpio_direction_input(unsigned gpio) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 53 | { |
| 54 | struct gpio_reg *gpio_reg_bank; |
| 55 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 56 | if (gpio >= MV_MAX_GPIO) { |
| 57 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 58 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 59 | } |
| 60 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 61 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 62 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gcdr); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 66 | int gpio_direction_output(unsigned gpio, int value) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 67 | { |
| 68 | struct gpio_reg *gpio_reg_bank; |
| 69 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 70 | if (gpio >= MV_MAX_GPIO) { |
| 71 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 72 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 73 | } |
| 74 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 75 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 76 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gsdr); |
| 77 | gpio_set_value(gpio, value); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 81 | int gpio_get_value(unsigned gpio) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 82 | { |
| 83 | struct gpio_reg *gpio_reg_bank; |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 84 | u32 gpio_val; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 85 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 86 | if (gpio >= MV_MAX_GPIO) { |
| 87 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 88 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 89 | } |
| 90 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 91 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 92 | gpio_val = readl(&gpio_reg_bank->gplr); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 93 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 94 | return GPIO_VAL(gpio, gpio_val); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 95 | } |
| 96 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 97 | int gpio_set_value(unsigned gpio, int value) |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 98 | { |
| 99 | struct gpio_reg *gpio_reg_bank; |
| 100 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 101 | if (gpio >= MV_MAX_GPIO) { |
| 102 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 103 | return -1; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 104 | } |
| 105 | |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 106 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 107 | if (value) |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 108 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr); |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 109 | else |
Joe Hershberger | f8928f1 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 110 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr); |
| 111 | |
| 112 | return 0; |
Ajay Bhargav | f56ba5d | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 113 | } |