1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
224e52197SDarwin Rambo /*
324e52197SDarwin Rambo * Copyright 2013 Broadcom Corporation.
424e52197SDarwin Rambo */
524e52197SDarwin Rambo
624e52197SDarwin Rambo #include <common.h>
724e52197SDarwin Rambo #include <asm/io.h>
824e52197SDarwin Rambo #include <asm/arch/sysmap.h>
924e52197SDarwin Rambo
1024e52197SDarwin Rambo #define GPIO_BASE (void *)GPIO2_BASE_ADDR
1124e52197SDarwin Rambo
1224e52197SDarwin Rambo #define GPIO_PASSWD 0x00a5a501
1324e52197SDarwin Rambo #define GPIO_PER_BANK 32
1424e52197SDarwin Rambo #define GPIO_MAX_BANK_NUM 8
1524e52197SDarwin Rambo
1624e52197SDarwin Rambo #define GPIO_BANK(gpio) ((gpio) >> 5)
1724e52197SDarwin Rambo #define GPIO_BITMASK(gpio) \
1824e52197SDarwin Rambo (1UL << ((gpio) & (GPIO_PER_BANK - 1)))
1924e52197SDarwin Rambo
2024e52197SDarwin Rambo #define GPIO_OUT_STATUS(bank) (0x00000000 + ((bank) << 2))
2124e52197SDarwin Rambo #define GPIO_IN_STATUS(bank) (0x00000020 + ((bank) << 2))
2224e52197SDarwin Rambo #define GPIO_OUT_SET(bank) (0x00000040 + ((bank) << 2))
2324e52197SDarwin Rambo #define GPIO_OUT_CLEAR(bank) (0x00000060 + ((bank) << 2))
2424e52197SDarwin Rambo #define GPIO_INT_STATUS(bank) (0x00000080 + ((bank) << 2))
2524e52197SDarwin Rambo #define GPIO_INT_MASK(bank) (0x000000a0 + ((bank) << 2))
2624e52197SDarwin Rambo #define GPIO_INT_MSKCLR(bank) (0x000000c0 + ((bank) << 2))
2724e52197SDarwin Rambo #define GPIO_CONTROL(bank) (0x00000100 + ((bank) << 2))
2824e52197SDarwin Rambo #define GPIO_PWD_STATUS(bank) (0x00000500 + ((bank) << 2))
2924e52197SDarwin Rambo
3024e52197SDarwin Rambo #define GPIO_GPPWR_OFFSET 0x00000520
3124e52197SDarwin Rambo
3224e52197SDarwin Rambo #define GPIO_GPCTR0_DBR_SHIFT 5
3324e52197SDarwin Rambo #define GPIO_GPCTR0_DBR_MASK 0x000001e0
3424e52197SDarwin Rambo
3524e52197SDarwin Rambo #define GPIO_GPCTR0_ITR_SHIFT 3
3624e52197SDarwin Rambo #define GPIO_GPCTR0_ITR_MASK 0x00000018
3724e52197SDarwin Rambo #define GPIO_GPCTR0_ITR_CMD_RISING_EDGE 0x00000001
3824e52197SDarwin Rambo #define GPIO_GPCTR0_ITR_CMD_FALLING_EDGE 0x00000002
3924e52197SDarwin Rambo #define GPIO_GPCTR0_ITR_CMD_BOTH_EDGE 0x00000003
4024e52197SDarwin Rambo
4124e52197SDarwin Rambo #define GPIO_GPCTR0_IOTR_MASK 0x00000001
4224e52197SDarwin Rambo #define GPIO_GPCTR0_IOTR_CMD_0UTPUT 0x00000000
4324e52197SDarwin Rambo #define GPIO_GPCTR0_IOTR_CMD_INPUT 0x00000001
4424e52197SDarwin Rambo
gpio_request(unsigned gpio,const char * label)4524e52197SDarwin Rambo int gpio_request(unsigned gpio, const char *label)
4624e52197SDarwin Rambo {
4724e52197SDarwin Rambo unsigned int value, off;
4824e52197SDarwin Rambo
4924e52197SDarwin Rambo writel(GPIO_PASSWD, GPIO_BASE + GPIO_GPPWR_OFFSET);
5024e52197SDarwin Rambo off = GPIO_PWD_STATUS(GPIO_BANK(gpio));
5124e52197SDarwin Rambo value = readl(GPIO_BASE + off) & ~GPIO_BITMASK(gpio);
5224e52197SDarwin Rambo writel(value, GPIO_BASE + off);
5324e52197SDarwin Rambo
5424e52197SDarwin Rambo return 0;
5524e52197SDarwin Rambo }
5624e52197SDarwin Rambo
gpio_free(unsigned gpio)5724e52197SDarwin Rambo int gpio_free(unsigned gpio)
5824e52197SDarwin Rambo {
5924e52197SDarwin Rambo unsigned int value, off;
6024e52197SDarwin Rambo
6124e52197SDarwin Rambo writel(GPIO_PASSWD, GPIO_BASE + GPIO_GPPWR_OFFSET);
6224e52197SDarwin Rambo off = GPIO_PWD_STATUS(GPIO_BANK(gpio));
6324e52197SDarwin Rambo value = readl(GPIO_BASE + off) | GPIO_BITMASK(gpio);
6424e52197SDarwin Rambo writel(value, GPIO_BASE + off);
6524e52197SDarwin Rambo
6624e52197SDarwin Rambo return 0;
6724e52197SDarwin Rambo }
6824e52197SDarwin Rambo
gpio_direction_input(unsigned gpio)6924e52197SDarwin Rambo int gpio_direction_input(unsigned gpio)
7024e52197SDarwin Rambo {
7124e52197SDarwin Rambo u32 val;
7224e52197SDarwin Rambo
7324e52197SDarwin Rambo val = readl(GPIO_BASE + GPIO_CONTROL(gpio));
7424e52197SDarwin Rambo val &= ~GPIO_GPCTR0_IOTR_MASK;
7524e52197SDarwin Rambo val |= GPIO_GPCTR0_IOTR_CMD_INPUT;
7624e52197SDarwin Rambo writel(val, GPIO_BASE + GPIO_CONTROL(gpio));
7724e52197SDarwin Rambo
7824e52197SDarwin Rambo return 0;
7924e52197SDarwin Rambo }
8024e52197SDarwin Rambo
gpio_direction_output(unsigned gpio,int value)8124e52197SDarwin Rambo int gpio_direction_output(unsigned gpio, int value)
8224e52197SDarwin Rambo {
8324e52197SDarwin Rambo int bank_id = GPIO_BANK(gpio);
8424e52197SDarwin Rambo int bitmask = GPIO_BITMASK(gpio);
8524e52197SDarwin Rambo u32 val, off;
8624e52197SDarwin Rambo
8724e52197SDarwin Rambo val = readl(GPIO_BASE + GPIO_CONTROL(gpio));
8824e52197SDarwin Rambo val &= ~GPIO_GPCTR0_IOTR_MASK;
8924e52197SDarwin Rambo val |= GPIO_GPCTR0_IOTR_CMD_0UTPUT;
9024e52197SDarwin Rambo writel(val, GPIO_BASE + GPIO_CONTROL(gpio));
9124e52197SDarwin Rambo off = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
9224e52197SDarwin Rambo
9324e52197SDarwin Rambo val = readl(GPIO_BASE + off);
9424e52197SDarwin Rambo val |= bitmask;
9524e52197SDarwin Rambo writel(val, GPIO_BASE + off);
9624e52197SDarwin Rambo
9724e52197SDarwin Rambo return 0;
9824e52197SDarwin Rambo }
9924e52197SDarwin Rambo
gpio_get_value(unsigned gpio)10024e52197SDarwin Rambo int gpio_get_value(unsigned gpio)
10124e52197SDarwin Rambo {
10224e52197SDarwin Rambo int bank_id = GPIO_BANK(gpio);
10324e52197SDarwin Rambo int bitmask = GPIO_BITMASK(gpio);
10424e52197SDarwin Rambo u32 val, off;
10524e52197SDarwin Rambo
10624e52197SDarwin Rambo /* determine the GPIO pin direction */
10724e52197SDarwin Rambo val = readl(GPIO_BASE + GPIO_CONTROL(gpio));
10824e52197SDarwin Rambo val &= GPIO_GPCTR0_IOTR_MASK;
10924e52197SDarwin Rambo
11024e52197SDarwin Rambo /* read the GPIO bank status */
11124e52197SDarwin Rambo off = (GPIO_GPCTR0_IOTR_CMD_INPUT == val) ?
11224e52197SDarwin Rambo GPIO_IN_STATUS(bank_id) : GPIO_OUT_STATUS(bank_id);
11324e52197SDarwin Rambo val = readl(GPIO_BASE + off);
11424e52197SDarwin Rambo
11524e52197SDarwin Rambo /* return the specified bit status */
11624e52197SDarwin Rambo return !!(val & bitmask);
11724e52197SDarwin Rambo }
11824e52197SDarwin Rambo
gpio_set_value(unsigned gpio,int value)11924e52197SDarwin Rambo void gpio_set_value(unsigned gpio, int value)
12024e52197SDarwin Rambo {
12124e52197SDarwin Rambo int bank_id = GPIO_BANK(gpio);
12224e52197SDarwin Rambo int bitmask = GPIO_BITMASK(gpio);
12324e52197SDarwin Rambo u32 val, off;
12424e52197SDarwin Rambo
12524e52197SDarwin Rambo /* determine the GPIO pin direction */
12624e52197SDarwin Rambo val = readl(GPIO_BASE + GPIO_CONTROL(gpio));
12724e52197SDarwin Rambo val &= GPIO_GPCTR0_IOTR_MASK;
12824e52197SDarwin Rambo
12924e52197SDarwin Rambo /* this function only applies to output pin */
13024e52197SDarwin Rambo if (GPIO_GPCTR0_IOTR_CMD_INPUT == val) {
13124e52197SDarwin Rambo printf("%s: Cannot set an input pin %d\n", __func__, gpio);
13224e52197SDarwin Rambo return;
13324e52197SDarwin Rambo }
13424e52197SDarwin Rambo
13524e52197SDarwin Rambo off = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
13624e52197SDarwin Rambo
13724e52197SDarwin Rambo val = readl(GPIO_BASE + off);
13824e52197SDarwin Rambo val |= bitmask;
13924e52197SDarwin Rambo writel(val, GPIO_BASE + off);
14024e52197SDarwin Rambo }
141