blob: 12e7197daf7bba0ffe541f3f8a11a9115dbc10bd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +05302/*
3 * (C) Copyright 2011
4 * eInfochips Ltd. <www.einfochips.com>
Ajay Bhargav0ad0b612016-12-21 13:28:06 +05305 * Written-by: Ajay Bhargav <contact@8051projects.net>
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +05306 *
7 * (C) Copyright 2010
8 * Marvell Semiconductor <www.marvell.com>
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +05309 */
10
11#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053013#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090014#include <linux/errno.h>
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053015#include "mvgpio.h"
16#include <asm/gpio.h>
17
18#ifndef MV_MAX_GPIO
19#define MV_MAX_GPIO 128
20#endif
21
Joe Hershbergerf8928f12011-11-11 15:55:36 -060022int gpio_request(unsigned gpio, const char *label)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053023{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060024 if (gpio >= MV_MAX_GPIO) {
25 printf("%s: Invalid GPIO requested %d\n", __func__, gpio);
26 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053027 }
28 return 0;
29}
30
Joe Hershbergerf8928f12011-11-11 15:55:36 -060031int gpio_free(unsigned gpio)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053032{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060033 return 0;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053034}
35
Joe Hershbergerf8928f12011-11-11 15:55:36 -060036int gpio_direction_input(unsigned gpio)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053037{
38 struct gpio_reg *gpio_reg_bank;
39
Joe Hershbergerf8928f12011-11-11 15:55:36 -060040 if (gpio >= MV_MAX_GPIO) {
41 printf("%s: Invalid GPIO %d\n", __func__, gpio);
42 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053043 }
44
Joe Hershbergerf8928f12011-11-11 15:55:36 -060045 gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
46 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gcdr);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053047 return 0;
48}
49
Joe Hershbergerf8928f12011-11-11 15:55:36 -060050int gpio_direction_output(unsigned gpio, int value)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053051{
52 struct gpio_reg *gpio_reg_bank;
53
Joe Hershbergerf8928f12011-11-11 15:55:36 -060054 if (gpio >= MV_MAX_GPIO) {
55 printf("%s: Invalid GPIO %d\n", __func__, gpio);
56 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053057 }
58
Joe Hershbergerf8928f12011-11-11 15:55:36 -060059 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);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053062 return 0;
63}
64
Joe Hershbergerf8928f12011-11-11 15:55:36 -060065int gpio_get_value(unsigned gpio)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053066{
67 struct gpio_reg *gpio_reg_bank;
Joe Hershbergerf8928f12011-11-11 15:55:36 -060068 u32 gpio_val;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053069
Joe Hershbergerf8928f12011-11-11 15:55:36 -060070 if (gpio >= MV_MAX_GPIO) {
71 printf("%s: Invalid GPIO %d\n", __func__, gpio);
72 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053073 }
74
Joe Hershbergerf8928f12011-11-11 15:55:36 -060075 gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
76 gpio_val = readl(&gpio_reg_bank->gplr);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053077
Joe Hershbergerf8928f12011-11-11 15:55:36 -060078 return GPIO_VAL(gpio, gpio_val);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053079}
80
Joe Hershbergerf8928f12011-11-11 15:55:36 -060081int gpio_set_value(unsigned gpio, int value)
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053082{
83 struct gpio_reg *gpio_reg_bank;
84
Joe Hershbergerf8928f12011-11-11 15:55:36 -060085 if (gpio >= MV_MAX_GPIO) {
86 printf("%s: Invalid GPIO %d\n", __func__, gpio);
87 return -1;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053088 }
89
Joe Hershbergerf8928f12011-11-11 15:55:36 -060090 gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053091 if (value)
Joe Hershbergerf8928f12011-11-11 15:55:36 -060092 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr);
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053093 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -060094 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr);
95
96 return 0;
Ajay Bhargavf56ba5d2011-08-22 17:57:38 +053097}