blob: 12e7197daf7bba0ffe541f3f8a11a9115dbc10bd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ajay Bhargav0c442292011-08-22 17:57:38 +05302/*
3 * (C) Copyright 2011
4 * eInfochips Ltd. <www.einfochips.com>
Ajay Bhargavc7c47ca2016-12-21 13:28:06 +05305 * Written-by: Ajay Bhargav <contact@8051projects.net>
Ajay Bhargav0c442292011-08-22 17:57:38 +05306 *
7 * (C) Copyright 2010
8 * Marvell Semiconductor <www.marvell.com>
Ajay Bhargav0c442292011-08-22 17:57:38 +05309 */
10
11#include <common.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Ajay Bhargav0c442292011-08-22 17:57:38 +053013#include <asm/io.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090014#include <linux/errno.h>
Ajay Bhargav0c442292011-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 Hershberger365d6072011-11-11 15:55:36 -060022int gpio_request(unsigned gpio, const char *label)
Ajay Bhargav0c442292011-08-22 17:57:38 +053023{
Joe Hershberger365d6072011-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 Bhargav0c442292011-08-22 17:57:38 +053027 }
28 return 0;
29}
30
Joe Hershberger365d6072011-11-11 15:55:36 -060031int gpio_free(unsigned gpio)
Ajay Bhargav0c442292011-08-22 17:57:38 +053032{
Ajay Bhargav0c442292011-08-22 17:57:38 +053033 return 0;
34}
35
Joe Hershberger365d6072011-11-11 15:55:36 -060036int gpio_direction_input(unsigned gpio)
Ajay Bhargav0c442292011-08-22 17:57:38 +053037{
38 struct gpio_reg *gpio_reg_bank;
39
Joe Hershberger365d6072011-11-11 15:55:36 -060040 if (gpio >= MV_MAX_GPIO) {
41 printf("%s: Invalid GPIO %d\n", __func__, gpio);
42 return -1;
Ajay Bhargav0c442292011-08-22 17:57:38 +053043 }
44
Joe Hershberger365d6072011-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 Bhargav0c442292011-08-22 17:57:38 +053047 return 0;
48}
49
Joe Hershberger365d6072011-11-11 15:55:36 -060050int gpio_direction_output(unsigned gpio, int value)
Ajay Bhargav0c442292011-08-22 17:57:38 +053051{
52 struct gpio_reg *gpio_reg_bank;
Ajay Bhargav0c442292011-08-22 17:57:38 +053053
Joe Hershberger365d6072011-11-11 15:55:36 -060054 if (gpio >= MV_MAX_GPIO) {
55 printf("%s: Invalid GPIO %d\n", __func__, gpio);
56 return -1;
Ajay Bhargav0c442292011-08-22 17:57:38 +053057 }
58
Joe Hershberger365d6072011-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);
62 return 0;
Ajay Bhargav0c442292011-08-22 17:57:38 +053063}
64
Joe Hershberger365d6072011-11-11 15:55:36 -060065int 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
81int gpio_set_value(unsigned gpio, int value)
Ajay Bhargav0c442292011-08-22 17:57:38 +053082{
83 struct gpio_reg *gpio_reg_bank;
84
Joe Hershberger365d6072011-11-11 15:55:36 -060085 if (gpio >= MV_MAX_GPIO) {
86 printf("%s: Invalid GPIO %d\n", __func__, gpio);
87 return -1;
Ajay Bhargav0c442292011-08-22 17:57:38 +053088 }
89
Joe Hershberger365d6072011-11-11 15:55:36 -060090 gpio_reg_bank = get_gpio_base(GPIO_TO_REG(gpio));
Ajay Bhargav0c442292011-08-22 17:57:38 +053091 if (value)
Joe Hershberger365d6072011-11-11 15:55:36 -060092 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpsr);
Ajay Bhargav0c442292011-08-22 17:57:38 +053093 else
Joe Hershberger365d6072011-11-11 15:55:36 -060094 writel(GPIO_TO_BIT(gpio), &gpio_reg_bank->gpcr);
95
96 return 0;
Ajay Bhargav0c442292011-08-22 17:57:38 +053097}