Ajay Bhargav | 0c44229 | 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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/errno.h> |
| 15 | #include "mvgpio.h" |
| 16 | #include <asm/gpio.h> |
| 17 | |
| 18 | #ifndef MV_MAX_GPIO |
| 19 | #define MV_MAX_GPIO 128 |
| 20 | #endif |
| 21 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 22 | int gpio_request(unsigned gpio, const char *label) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 23 | { |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 24 | if (gpio >= MV_MAX_GPIO) { |
| 25 | printf("%s: Invalid GPIO requested %d\n", __func__, gpio); |
| 26 | return -1; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 27 | } |
| 28 | return 0; |
| 29 | } |
| 30 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 31 | int gpio_free(unsigned gpio) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 32 | { |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 36 | int gpio_direction_input(unsigned gpio) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 37 | { |
| 38 | struct gpio_reg *gpio_reg_bank; |
| 39 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 40 | if (gpio >= MV_MAX_GPIO) { |
| 41 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 42 | return -1; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 43 | } |
| 44 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 45 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 46 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gcdr); |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 50 | int gpio_direction_output(unsigned gpio, int value) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 51 | { |
| 52 | struct gpio_reg *gpio_reg_bank; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 53 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 54 | if (gpio >= MV_MAX_GPIO) { |
| 55 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 56 | return -1; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 57 | } |
| 58 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 59 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 60 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gsdr); |
| 61 | gpio_set_value(gpio, value); |
| 62 | return 0; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 63 | } |
| 64 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 65 | int gpio_get_value(unsigned gpio) |
| 66 | { |
| 67 | struct gpio_reg *gpio_reg_bank; |
| 68 | u32 gpio_val; |
| 69 | |
| 70 | if (gpio >= MV_MAX_GPIO) { |
| 71 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
| 76 | gpio_val = readl(&gpio_reg_bank->gplr); |
| 77 | |
| 78 | return GPIO_VAL(gpio, gpio_val); |
| 79 | } |
| 80 | |
| 81 | int gpio_set_value(unsigned gpio, int value) |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 82 | { |
| 83 | struct gpio_reg *gpio_reg_bank; |
| 84 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 85 | if (gpio >= MV_MAX_GPIO) { |
| 86 | printf("%s: Invalid GPIO %d\n", __func__, gpio); |
| 87 | return -1; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 90 | gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio)); |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 91 | if (value) |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 92 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr); |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 93 | else |
Joe Hershberger | 365d607 | 2011-11-11 15:55:36 -0600 | [diff] [blame] | 94 | writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr); |
| 95 | |
| 96 | return 0; |
Ajay Bhargav | 0c44229 | 2011-08-22 17:57:38 +0530 | [diff] [blame] | 97 | } |