blob: 97b51371145e81de9f268a33f001c72d6f63ff98 [file] [log] [blame]
Stephen Warrenefad6cf2012-08-05 16:07:21 +00001/*
2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stephen Warrenefad6cf2012-08-05 16:07:21 +00006 */
7
8#include <common.h>
9#include <asm/gpio.h>
10#include <asm/io.h>
11
12inline int gpio_is_valid(unsigned gpio)
13{
14 return (gpio < BCM2835_GPIO_COUNT);
15}
16
17int gpio_request(unsigned gpio, const char *label)
18{
19 return !gpio_is_valid(gpio);
20}
21
22int gpio_free(unsigned gpio)
23{
24 return 0;
25}
26
27int gpio_direction_input(unsigned gpio)
28{
29 struct bcm2835_gpio_regs *reg =
30 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
31 unsigned val;
32
33 val = readl(&reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
34 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
35 val |= (BCM2835_GPIO_INPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
36 writel(val, &reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
37
38 return 0;
39}
40
41int gpio_direction_output(unsigned gpio, int value)
42{
43 struct bcm2835_gpio_regs *reg =
44 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
45 unsigned val;
46
47 gpio_set_value(gpio, value);
48
49 val = readl(&reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
50 val &= ~(BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio));
51 val |= (BCM2835_GPIO_OUTPUT << BCM2835_GPIO_FSEL_SHIFT(gpio));
52 writel(val, &reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
53
54 return 0;
55}
56
57int gpio_get_value(unsigned gpio)
58{
59 struct bcm2835_gpio_regs *reg =
60 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
61 unsigned val;
62
63 val = readl(&reg->gplev[BCM2835_GPIO_COMMON_BANK(gpio)]);
64
65 return (val >> BCM2835_GPIO_COMMON_SHIFT(gpio)) & 0x1;
66}
67
68int gpio_set_value(unsigned gpio, int value)
69{
70 struct bcm2835_gpio_regs *reg =
71 (struct bcm2835_gpio_regs *)BCM2835_GPIO_BASE;
72 u32 *output_reg = value ? reg->gpset : reg->gpclr;
73
74 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio),
75 &output_reg[BCM2835_GPIO_COMMON_BANK(gpio)]);
76
77 return 0;
78}