blob: 107c3fcb10a5ab3a42d90778c2848197f6320ead [file] [log] [blame]
Masahiro Yamada2dbca982016-02-16 17:03:48 +09001/*
Masahiro Yamada932ac852017-10-13 19:21:51 +09002 * Copyright (C) 2016-2017 Socionext Inc.
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +09003 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada2dbca982016-02-16 17:03:48 +09004 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Masahiro Yamada2dbca982016-02-16 17:03:48 +090010#include <linux/bitops.h>
11#include <linux/io.h>
Masahiro Yamadabfa3d8b2016-03-24 22:32:41 +090012#include <linux/sizes.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Masahiro Yamada932ac852017-10-13 19:21:51 +090014#include <asm/global_data.h>
Masahiro Yamada2dbca982016-02-16 17:03:48 +090015#include <asm/gpio.h>
16
Masahiro Yamada932ac852017-10-13 19:21:51 +090017#define UNIPHIER_GPIO_LINES_PER_BANK 8
Masahiro Yamada2dbca982016-02-16 17:03:48 +090018
Masahiro Yamada932ac852017-10-13 19:21:51 +090019#define UNIPHIER_GPIO_PORT_DATA 0x0 /* data */
20#define UNIPHIER_GPIO_PORT_DIR 0x4 /* direction (1:in, 0:out) */
21#define UNIPHIER_GPIO_IRQ_EN 0x90 /* irq enable */
Masahiro Yamada2dbca982016-02-16 17:03:48 +090022
23struct uniphier_gpio_priv {
Masahiro Yamada932ac852017-10-13 19:21:51 +090024 void __iomem *regs;
Masahiro Yamada2dbca982016-02-16 17:03:48 +090025};
26
Masahiro Yamada932ac852017-10-13 19:21:51 +090027static unsigned int uniphier_gpio_bank_to_reg(unsigned int bank)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090028{
Masahiro Yamada932ac852017-10-13 19:21:51 +090029 unsigned int reg;
30
31 reg = (bank + 1) * 8;
32
33 /*
34 * Unfortunately, the GPIO port registers are not contiguous because
35 * offset 0x90-0x9f is used for IRQ. Add 0x10 when crossing the region.
36 */
37 if (reg >= UNIPHIER_GPIO_IRQ_EN)
38 reg += 0x10;
39
40 return reg;
41}
42
43static void uniphier_gpio_get_bank_and_mask(unsigned int offset,
44 unsigned int *bank, u32 *mask)
45{
46 *bank = offset / UNIPHIER_GPIO_LINES_PER_BANK;
47 *mask = BIT(offset % UNIPHIER_GPIO_LINES_PER_BANK);
48}
49
50static void uniphier_gpio_reg_update(struct uniphier_gpio_priv *priv,
51 unsigned int reg, u32 mask, u32 val)
52{
Masahiro Yamada2dbca982016-02-16 17:03:48 +090053 u32 tmp;
54
Masahiro Yamada932ac852017-10-13 19:21:51 +090055 tmp = readl(priv->regs + reg);
56 tmp &= ~mask;
57 tmp |= mask & val;
58 writel(tmp, priv->regs + reg);
Masahiro Yamada2dbca982016-02-16 17:03:48 +090059}
60
Masahiro Yamada932ac852017-10-13 19:21:51 +090061static void uniphier_gpio_bank_write(struct udevice *dev, unsigned int bank,
62 unsigned int reg, u32 mask, u32 val)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090063{
64 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
65
Masahiro Yamada932ac852017-10-13 19:21:51 +090066 if (!mask)
67 return;
68
69 uniphier_gpio_reg_update(priv, uniphier_gpio_bank_to_reg(bank) + reg,
70 mask, val);
Masahiro Yamada2dbca982016-02-16 17:03:48 +090071}
72
Masahiro Yamada932ac852017-10-13 19:21:51 +090073static void uniphier_gpio_offset_write(struct udevice *dev, unsigned int offset,
74 unsigned int reg, int val)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090075{
Masahiro Yamada932ac852017-10-13 19:21:51 +090076 unsigned int bank;
77 u32 mask;
Masahiro Yamada2dbca982016-02-16 17:03:48 +090078
Masahiro Yamada932ac852017-10-13 19:21:51 +090079 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
80
81 uniphier_gpio_bank_write(dev, bank, reg, mask, val ? mask : 0);
Masahiro Yamada2dbca982016-02-16 17:03:48 +090082}
83
Masahiro Yamada932ac852017-10-13 19:21:51 +090084static int uniphier_gpio_offset_read(struct udevice *dev,
85 unsigned int offset, unsigned int reg)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090086{
Masahiro Yamada932ac852017-10-13 19:21:51 +090087 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
88 unsigned int bank, reg_offset;
89 u32 mask;
Masahiro Yamada2dbca982016-02-16 17:03:48 +090090
Masahiro Yamada932ac852017-10-13 19:21:51 +090091 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
92 reg_offset = uniphier_gpio_bank_to_reg(bank) + reg;
93
94 return !!(readl(priv->regs + reg_offset) & mask);
Masahiro Yamada2dbca982016-02-16 17:03:48 +090095}
96
Masahiro Yamada932ac852017-10-13 19:21:51 +090097static int uniphier_gpio_get_function(struct udevice *dev, unsigned int offset)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090098{
Masahiro Yamada932ac852017-10-13 19:21:51 +090099 return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DIR) ?
100 GPIOF_INPUT : GPIOF_OUTPUT;
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900101}
102
Masahiro Yamada932ac852017-10-13 19:21:51 +0900103static int uniphier_gpio_direction_input(struct udevice *dev,
104 unsigned int offset)
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900105{
Masahiro Yamada932ac852017-10-13 19:21:51 +0900106 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 1);
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900107
108 return 0;
109}
110
Masahiro Yamada932ac852017-10-13 19:21:51 +0900111static int uniphier_gpio_direction_output(struct udevice *dev,
112 unsigned int offset, int value)
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900113{
Masahiro Yamada932ac852017-10-13 19:21:51 +0900114 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value);
115 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 0);
116
117 return 0;
118}
119
120static int uniphier_gpio_get_value(struct udevice *dev, unsigned int offset)
121{
122 return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DATA);
123}
124
125static int uniphier_gpio_set_value(struct udevice *dev,
126 unsigned int offset, int value)
127{
128 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value);
129
130 return 0;
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900131}
132
133static const struct dm_gpio_ops uniphier_gpio_ops = {
134 .direction_input = uniphier_gpio_direction_input,
135 .direction_output = uniphier_gpio_direction_output,
136 .get_value = uniphier_gpio_get_value,
137 .set_value = uniphier_gpio_set_value,
138 .get_function = uniphier_gpio_get_function,
139};
140
141static int uniphier_gpio_probe(struct udevice *dev)
142{
143 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
144 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900145 fdt_addr_t addr;
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900146
Simon Glassba1dea42017-05-17 17:18:05 -0600147 addr = devfdt_get_addr(dev);
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900148 if (addr == FDT_ADDR_T_NONE)
149 return -EINVAL;
150
Masahiro Yamada932ac852017-10-13 19:21:51 +0900151 priv->regs = devm_ioremap(dev, addr, SZ_512);
152 if (!priv->regs)
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900153 return -ENOMEM;
154
Masahiro Yamada932ac852017-10-13 19:21:51 +0900155 uc_priv->gpio_count = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
156 "ngpios", 0);
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900157
158 return 0;
159}
160
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900161static const struct udevice_id uniphier_gpio_match[] = {
162 { .compatible = "socionext,uniphier-gpio" },
163 { /* sentinel */ }
164};
165
166U_BOOT_DRIVER(uniphier_gpio) = {
Masahiro Yamada932ac852017-10-13 19:21:51 +0900167 .name = "uniphier-gpio",
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900168 .id = UCLASS_GPIO,
169 .of_match = uniphier_gpio_match,
170 .probe = uniphier_gpio_probe,
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900171 .priv_auto_alloc_size = sizeof(struct uniphier_gpio_priv),
172 .ops = &uniphier_gpio_ops,
173};