blob: 033fb4b60ee6e9dc4b0bf85c330c50056abc4f1c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada2dbca982016-02-16 17:03:48 +09002/*
Masahiro Yamada932ac852017-10-13 19:21:51 +09003 * Copyright (C) 2016-2017 Socionext Inc.
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +09004 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada2dbca982016-02-16 17:03:48 +09005 */
6
Simon Glass11c89f32017-05-17 17:18:03 -06007#include <dm.h>
Masahiro Yamada2dbca982016-02-16 17:03:48 +09008#include <linux/bitops.h>
9#include <linux/io.h>
Masahiro Yamadabfa3d8b2016-03-24 22:32:41 +090010#include <linux/sizes.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Masahiro Yamada932ac852017-10-13 19:21:51 +090012#include <asm/global_data.h>
Masahiro Yamada2dbca982016-02-16 17:03:48 +090013#include <asm/gpio.h>
Masahiro Yamadade10c662017-11-25 00:25:34 +090014#include <dt-bindings/gpio/uniphier-gpio.h>
Masahiro Yamada2dbca982016-02-16 17:03:48 +090015
Masahiro Yamada932ac852017-10-13 19:21:51 +090016#define UNIPHIER_GPIO_PORT_DATA 0x0 /* data */
17#define UNIPHIER_GPIO_PORT_DIR 0x4 /* direction (1:in, 0:out) */
18#define UNIPHIER_GPIO_IRQ_EN 0x90 /* irq enable */
Masahiro Yamada2dbca982016-02-16 17:03:48 +090019
20struct uniphier_gpio_priv {
Masahiro Yamada932ac852017-10-13 19:21:51 +090021 void __iomem *regs;
Masahiro Yamada2dbca982016-02-16 17:03:48 +090022};
23
Masahiro Yamada932ac852017-10-13 19:21:51 +090024static unsigned int uniphier_gpio_bank_to_reg(unsigned int bank)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090025{
Masahiro Yamada932ac852017-10-13 19:21:51 +090026 unsigned int reg;
27
28 reg = (bank + 1) * 8;
29
30 /*
31 * Unfortunately, the GPIO port registers are not contiguous because
32 * offset 0x90-0x9f is used for IRQ. Add 0x10 when crossing the region.
33 */
34 if (reg >= UNIPHIER_GPIO_IRQ_EN)
35 reg += 0x10;
36
37 return reg;
38}
39
40static void uniphier_gpio_get_bank_and_mask(unsigned int offset,
41 unsigned int *bank, u32 *mask)
42{
43 *bank = offset / UNIPHIER_GPIO_LINES_PER_BANK;
44 *mask = BIT(offset % UNIPHIER_GPIO_LINES_PER_BANK);
45}
46
47static void uniphier_gpio_reg_update(struct uniphier_gpio_priv *priv,
48 unsigned int reg, u32 mask, u32 val)
49{
Masahiro Yamada2dbca982016-02-16 17:03:48 +090050 u32 tmp;
51
Masahiro Yamada932ac852017-10-13 19:21:51 +090052 tmp = readl(priv->regs + reg);
53 tmp &= ~mask;
54 tmp |= mask & val;
55 writel(tmp, priv->regs + reg);
Masahiro Yamada2dbca982016-02-16 17:03:48 +090056}
57
Masahiro Yamada932ac852017-10-13 19:21:51 +090058static void uniphier_gpio_bank_write(struct udevice *dev, unsigned int bank,
59 unsigned int reg, u32 mask, u32 val)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090060{
61 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
62
Masahiro Yamada932ac852017-10-13 19:21:51 +090063 if (!mask)
64 return;
65
66 uniphier_gpio_reg_update(priv, uniphier_gpio_bank_to_reg(bank) + reg,
67 mask, val);
Masahiro Yamada2dbca982016-02-16 17:03:48 +090068}
69
Masahiro Yamada932ac852017-10-13 19:21:51 +090070static void uniphier_gpio_offset_write(struct udevice *dev, unsigned int offset,
71 unsigned int reg, int val)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090072{
Masahiro Yamada932ac852017-10-13 19:21:51 +090073 unsigned int bank;
74 u32 mask;
Masahiro Yamada2dbca982016-02-16 17:03:48 +090075
Masahiro Yamada932ac852017-10-13 19:21:51 +090076 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
77
78 uniphier_gpio_bank_write(dev, bank, reg, mask, val ? mask : 0);
Masahiro Yamada2dbca982016-02-16 17:03:48 +090079}
80
Masahiro Yamada932ac852017-10-13 19:21:51 +090081static int uniphier_gpio_offset_read(struct udevice *dev,
82 unsigned int offset, unsigned int reg)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090083{
Masahiro Yamada932ac852017-10-13 19:21:51 +090084 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
85 unsigned int bank, reg_offset;
86 u32 mask;
Masahiro Yamada2dbca982016-02-16 17:03:48 +090087
Masahiro Yamada932ac852017-10-13 19:21:51 +090088 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
89 reg_offset = uniphier_gpio_bank_to_reg(bank) + reg;
90
91 return !!(readl(priv->regs + reg_offset) & mask);
Masahiro Yamada2dbca982016-02-16 17:03:48 +090092}
93
Masahiro Yamada932ac852017-10-13 19:21:51 +090094static int uniphier_gpio_get_function(struct udevice *dev, unsigned int offset)
Masahiro Yamada2dbca982016-02-16 17:03:48 +090095{
Masahiro Yamada932ac852017-10-13 19:21:51 +090096 return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DIR) ?
97 GPIOF_INPUT : GPIOF_OUTPUT;
Masahiro Yamada2dbca982016-02-16 17:03:48 +090098}
99
Masahiro Yamada932ac852017-10-13 19:21:51 +0900100static int uniphier_gpio_direction_input(struct udevice *dev,
101 unsigned int offset)
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900102{
Masahiro Yamada932ac852017-10-13 19:21:51 +0900103 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 1);
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900104
105 return 0;
106}
107
Masahiro Yamada932ac852017-10-13 19:21:51 +0900108static int uniphier_gpio_direction_output(struct udevice *dev,
109 unsigned int offset, int value)
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900110{
Masahiro Yamada932ac852017-10-13 19:21:51 +0900111 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value);
112 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 0);
113
114 return 0;
115}
116
117static int uniphier_gpio_get_value(struct udevice *dev, unsigned int offset)
118{
119 return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DATA);
120}
121
122static int uniphier_gpio_set_value(struct udevice *dev,
123 unsigned int offset, int value)
124{
125 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value);
126
127 return 0;
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900128}
129
130static const struct dm_gpio_ops uniphier_gpio_ops = {
131 .direction_input = uniphier_gpio_direction_input,
132 .direction_output = uniphier_gpio_direction_output,
133 .get_value = uniphier_gpio_get_value,
134 .set_value = uniphier_gpio_set_value,
135 .get_function = uniphier_gpio_get_function,
136};
137
138static int uniphier_gpio_probe(struct udevice *dev)
139{
140 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
141 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900142 fdt_addr_t addr;
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900143
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900144 addr = dev_read_addr(dev);
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900145 if (addr == FDT_ADDR_T_NONE)
146 return -EINVAL;
147
Masahiro Yamada932ac852017-10-13 19:21:51 +0900148 priv->regs = devm_ioremap(dev, addr, SZ_512);
149 if (!priv->regs)
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900150 return -ENOMEM;
151
Masahiro Yamada932ac852017-10-13 19:21:51 +0900152 uc_priv->gpio_count = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
153 "ngpios", 0);
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900154
155 return 0;
156}
157
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900158static const struct udevice_id uniphier_gpio_match[] = {
159 { .compatible = "socionext,uniphier-gpio" },
160 { /* sentinel */ }
161};
162
163U_BOOT_DRIVER(uniphier_gpio) = {
Masahiro Yamada932ac852017-10-13 19:21:51 +0900164 .name = "uniphier-gpio",
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900165 .id = UCLASS_GPIO,
166 .of_match = uniphier_gpio_match,
167 .probe = uniphier_gpio_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700168 .priv_auto = sizeof(struct uniphier_gpio_priv),
Masahiro Yamada2dbca982016-02-16 17:03:48 +0900169 .ops = &uniphier_gpio_ops,
170};