blob: c57a430275a321ab410c8d2004eb72e8266fc1f4 [file] [log] [blame]
Stefan Kristianssonca9d3ab2011-11-26 19:04:49 +00001/*
2 * OpenRISC gpio driver
3 *
4 * Copyright (C) 2011 Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
5 *
6 * based on nios2 gpio driver
7 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
8 *
9 * when CONFIG_SYS_GPIO_BASE is not defined, board may provide
10 * its own driver.
11 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020012 * SPDX-License-Identifier: GPL-2.0+
Stefan Kristianssonca9d3ab2011-11-26 19:04:49 +000013 */
14
15#ifdef CONFIG_SYS_GPIO_BASE
16#include <asm/io.h>
17
18static inline int gpio_request(unsigned gpio, const char *label)
19{
20 return 0;
21}
22
23static inline int gpio_free(unsigned gpio)
24{
25 return 0;
26}
27
28static inline int gpio_get_value(unsigned gpio)
29{
30 return (readb(CONFIG_SYS_GPIO_BASE + gpio/8) >> gpio%8) & 0x1;
31}
32
33static inline void gpio_set_value(unsigned gpio, int value)
34{
35 u8 tmp = readb(CONFIG_SYS_GPIO_BASE + gpio/8);
36
37 if (value)
38 tmp |= (1 << gpio%8);
39 else
40 tmp &= ~(1 << gpio%8);
41 writeb(tmp, CONFIG_SYS_GPIO_BASE + gpio/8);
42}
43
44static inline int gpio_direction_input(unsigned gpio)
45{
46 gpio_set_value(gpio + CONFIG_SYS_GPIO_WIDTH, 0);
47
48 return 0;
49}
50
51static inline int gpio_direction_output(unsigned gpio, int value)
52{
53 gpio_set_value(gpio + CONFIG_SYS_GPIO_WIDTH, 1);
54 gpio_set_value(gpio, value);
55
56 return 0;
57}
58
59static inline int gpio_is_valid(int number)
60{
61 return ((unsigned)number) < CONFIG_SYS_GPIO_WIDTH;
62}
63#else
64extern int gpio_request(unsigned gpio, const char *label);
65extern int gpio_free(unsigned gpio);
66extern int gpio_direction_input(unsigned gpio);
67extern int gpio_direction_output(unsigned gpio, int value);
68extern int gpio_get_value(unsigned gpio);
69extern void gpio_set_value(unsigned gpio, int value);
70extern int gpio_is_valid(int number);
71#endif /* CONFIG_SYS_GPIO_BASE */