xref: /openbmc/u-boot/drivers/gpio/mvgpio.c (revision 365d6070)
10c442298SAjay Bhargav /*
20c442298SAjay Bhargav  * (C) Copyright 2011
30c442298SAjay Bhargav  * eInfochips Ltd. <www.einfochips.com>
40c442298SAjay Bhargav  * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
50c442298SAjay Bhargav  *
60c442298SAjay Bhargav  * (C) Copyright 2010
70c442298SAjay Bhargav  * Marvell Semiconductor <www.marvell.com>
80c442298SAjay Bhargav  *
90c442298SAjay Bhargav  * See file CREDITS for list of people who contributed to this
100c442298SAjay Bhargav  * project.
110c442298SAjay Bhargav  *
120c442298SAjay Bhargav  * This program is free software; you can redistribute it and/or
130c442298SAjay Bhargav  * modify it under the terms of the GNU General Public License as
140c442298SAjay Bhargav  * published by the Free Software Foundation; either version 2 of
150c442298SAjay Bhargav  * the License, or (at your option) any later version.
160c442298SAjay Bhargav  *
170c442298SAjay Bhargav  * This program is distributed in the hope that it will be useful,
180c442298SAjay Bhargav  * but WITHOUT ANY WARRANTY; without even the implied warranty of
190c442298SAjay Bhargav  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
200c442298SAjay Bhargav  * GNU General Public License for more details.
210c442298SAjay Bhargav  *
220c442298SAjay Bhargav  * You should have received a copy of the GNU General Public License
230c442298SAjay Bhargav  * along with this program; if not, write to the Free Software
240c442298SAjay Bhargav  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
250c442298SAjay Bhargav  * MA 02110-1301 USA
260c442298SAjay Bhargav  */
270c442298SAjay Bhargav 
280c442298SAjay Bhargav #include <common.h>
290c442298SAjay Bhargav #include <asm/io.h>
300c442298SAjay Bhargav #include <asm/errno.h>
310c442298SAjay Bhargav #include "mvgpio.h"
320c442298SAjay Bhargav #include <asm/gpio.h>
330c442298SAjay Bhargav 
340c442298SAjay Bhargav #ifndef MV_MAX_GPIO
350c442298SAjay Bhargav #define MV_MAX_GPIO	128
360c442298SAjay Bhargav #endif
370c442298SAjay Bhargav 
38*365d6070SJoe Hershberger int gpio_request(unsigned gpio, const char *label)
390c442298SAjay Bhargav {
40*365d6070SJoe Hershberger 	if (gpio >= MV_MAX_GPIO) {
41*365d6070SJoe Hershberger 		printf("%s: Invalid GPIO requested %d\n", __func__, gpio);
42*365d6070SJoe Hershberger 		return -1;
430c442298SAjay Bhargav 	}
440c442298SAjay Bhargav 	return 0;
450c442298SAjay Bhargav }
460c442298SAjay Bhargav 
47*365d6070SJoe Hershberger int gpio_free(unsigned gpio)
480c442298SAjay Bhargav {
490c442298SAjay Bhargav 	return 0;
500c442298SAjay Bhargav }
510c442298SAjay Bhargav 
52*365d6070SJoe Hershberger int gpio_direction_input(unsigned gpio)
530c442298SAjay Bhargav {
540c442298SAjay Bhargav 	struct gpio_reg *gpio_reg_bank;
550c442298SAjay Bhargav 
56*365d6070SJoe Hershberger 	if (gpio >= MV_MAX_GPIO) {
57*365d6070SJoe Hershberger 		printf("%s: Invalid GPIO %d\n", __func__, gpio);
58*365d6070SJoe Hershberger 		return -1;
590c442298SAjay Bhargav 	}
600c442298SAjay Bhargav 
61*365d6070SJoe Hershberger 	gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
62*365d6070SJoe Hershberger 	writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gcdr);
630c442298SAjay Bhargav 	return 0;
640c442298SAjay Bhargav }
650c442298SAjay Bhargav 
66*365d6070SJoe Hershberger int gpio_direction_output(unsigned gpio, int value)
670c442298SAjay Bhargav {
680c442298SAjay Bhargav 	struct gpio_reg *gpio_reg_bank;
690c442298SAjay Bhargav 
70*365d6070SJoe Hershberger 	if (gpio >= MV_MAX_GPIO) {
71*365d6070SJoe Hershberger 		printf("%s: Invalid GPIO %d\n", __func__, gpio);
72*365d6070SJoe Hershberger 		return -1;
730c442298SAjay Bhargav 	}
740c442298SAjay Bhargav 
75*365d6070SJoe Hershberger 	gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
76*365d6070SJoe Hershberger 	writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gsdr);
77*365d6070SJoe Hershberger 	gpio_set_value(gpio, value);
78*365d6070SJoe Hershberger 	return 0;
79*365d6070SJoe Hershberger }
80*365d6070SJoe Hershberger 
81*365d6070SJoe Hershberger int gpio_get_value(unsigned gpio)
82*365d6070SJoe Hershberger {
83*365d6070SJoe Hershberger 	struct gpio_reg *gpio_reg_bank;
84*365d6070SJoe Hershberger 	u32 gpio_val;
85*365d6070SJoe Hershberger 
86*365d6070SJoe Hershberger 	if (gpio >= MV_MAX_GPIO) {
87*365d6070SJoe Hershberger 		printf("%s: Invalid GPIO %d\n", __func__, gpio);
88*365d6070SJoe Hershberger 		return -1;
89*365d6070SJoe Hershberger 	}
90*365d6070SJoe Hershberger 
91*365d6070SJoe Hershberger 	gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
92*365d6070SJoe Hershberger 	gpio_val = readl(&gpio_reg_bank->gplr);
93*365d6070SJoe Hershberger 
94*365d6070SJoe Hershberger 	return GPIO_VAL(gpio, gpio_val);
95*365d6070SJoe Hershberger }
96*365d6070SJoe Hershberger 
97*365d6070SJoe Hershberger int gpio_set_value(unsigned gpio, int value)
98*365d6070SJoe Hershberger {
99*365d6070SJoe Hershberger 	struct gpio_reg *gpio_reg_bank;
100*365d6070SJoe Hershberger 
101*365d6070SJoe Hershberger 	if (gpio >= MV_MAX_GPIO) {
102*365d6070SJoe Hershberger 		printf("%s: Invalid GPIO %d\n", __func__, gpio);
103*365d6070SJoe Hershberger 		return -1;
104*365d6070SJoe Hershberger 	}
105*365d6070SJoe Hershberger 
106*365d6070SJoe Hershberger 	gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
1070c442298SAjay Bhargav 	if (value)
108*365d6070SJoe Hershberger 		writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr);
1090c442298SAjay Bhargav 	else
110*365d6070SJoe Hershberger 		writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr);
111*365d6070SJoe Hershberger 
112*365d6070SJoe Hershberger 	return 0;
1130c442298SAjay Bhargav }
114