blob: 8d72ab8c4a1573f6ebcab077b1a8926e16278fa9 [file] [log] [blame]
Masahiro Yamadab9a66b62016-02-16 17:03:48 +09001/*
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +09002 * Copyright (C) 2016-2017 Socionext Inc.
Masahiro Yamada4e3d8402016-07-19 21:56:13 +09003 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadab9a66b62016-02-16 17:03:48 +09004 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -06009#include <dm.h>
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090010#include <linux/bitops.h>
11#include <linux/io.h>
Masahiro Yamadabc82a132016-03-24 22:32:41 +090012#include <linux/sizes.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090013#include <linux/errno.h>
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090014#include <asm/global_data.h>
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090015#include <asm/gpio.h>
Masahiro Yamadae9986a42017-11-25 00:25:34 +090016#include <dt-bindings/gpio/uniphier-gpio.h>
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090017
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090018#define UNIPHIER_GPIO_PORT_DATA 0x0 /* data */
19#define UNIPHIER_GPIO_PORT_DIR 0x4 /* direction (1:in, 0:out) */
20#define UNIPHIER_GPIO_IRQ_EN 0x90 /* irq enable */
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090021
22struct uniphier_gpio_priv {
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090023 void __iomem *regs;
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090024};
25
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090026static unsigned int uniphier_gpio_bank_to_reg(unsigned int bank)
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090027{
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090028 unsigned int reg;
29
30 reg = (bank + 1) * 8;
31
32 /*
33 * Unfortunately, the GPIO port registers are not contiguous because
34 * offset 0x90-0x9f is used for IRQ. Add 0x10 when crossing the region.
35 */
36 if (reg >= UNIPHIER_GPIO_IRQ_EN)
37 reg += 0x10;
38
39 return reg;
40}
41
42static void uniphier_gpio_get_bank_and_mask(unsigned int offset,
43 unsigned int *bank, u32 *mask)
44{
45 *bank = offset / UNIPHIER_GPIO_LINES_PER_BANK;
46 *mask = BIT(offset % UNIPHIER_GPIO_LINES_PER_BANK);
47}
48
49static void uniphier_gpio_reg_update(struct uniphier_gpio_priv *priv,
50 unsigned int reg, u32 mask, u32 val)
51{
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090052 u32 tmp;
53
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090054 tmp = readl(priv->regs + reg);
55 tmp &= ~mask;
56 tmp |= mask & val;
57 writel(tmp, priv->regs + reg);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090058}
59
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090060static void uniphier_gpio_bank_write(struct udevice *dev, unsigned int bank,
61 unsigned int reg, u32 mask, u32 val)
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090062{
63 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
64
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090065 if (!mask)
66 return;
67
68 uniphier_gpio_reg_update(priv, uniphier_gpio_bank_to_reg(bank) + reg,
69 mask, val);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090070}
71
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090072static void uniphier_gpio_offset_write(struct udevice *dev, unsigned int offset,
73 unsigned int reg, int val)
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090074{
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090075 unsigned int bank;
76 u32 mask;
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090077
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090078 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
79
80 uniphier_gpio_bank_write(dev, bank, reg, mask, val ? mask : 0);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090081}
82
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090083static int uniphier_gpio_offset_read(struct udevice *dev,
84 unsigned int offset, unsigned int reg)
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090085{
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090086 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
87 unsigned int bank, reg_offset;
88 u32 mask;
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090089
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090090 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
91 reg_offset = uniphier_gpio_bank_to_reg(bank) + reg;
92
93 return !!(readl(priv->regs + reg_offset) & mask);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090094}
95
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090096static int uniphier_gpio_get_function(struct udevice *dev, unsigned int offset)
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090097{
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +090098 return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DIR) ?
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090099 GPIOF_INPUT : GPIOF_OUTPUT;
100}
101
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +0900102static int uniphier_gpio_direction_input(struct udevice *dev,
103 unsigned int offset)
104{
105 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 1);
106
107 return 0;
108}
109
110static int uniphier_gpio_direction_output(struct udevice *dev,
111 unsigned int offset, int value)
112{
113 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value);
114 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DIR, 0);
115
116 return 0;
117}
118
119static int uniphier_gpio_get_value(struct udevice *dev, unsigned int offset)
120{
121 return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_PORT_DATA);
122}
123
124static int uniphier_gpio_set_value(struct udevice *dev,
125 unsigned int offset, int value)
126{
127 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_PORT_DATA, value);
128
129 return 0;
130}
131
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900132static const struct dm_gpio_ops uniphier_gpio_ops = {
133 .direction_input = uniphier_gpio_direction_input,
134 .direction_output = uniphier_gpio_direction_output,
135 .get_value = uniphier_gpio_get_value,
136 .set_value = uniphier_gpio_set_value,
137 .get_function = uniphier_gpio_get_function,
138};
139
140static int uniphier_gpio_probe(struct udevice *dev)
141{
142 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
143 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900144 fdt_addr_t addr;
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900145
Simon Glassa821c4a2017-05-17 17:18:05 -0600146 addr = devfdt_get_addr(dev);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900147 if (addr == FDT_ADDR_T_NONE)
148 return -EINVAL;
149
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +0900150 priv->regs = devm_ioremap(dev, addr, SZ_512);
151 if (!priv->regs)
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900152 return -ENOMEM;
153
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +0900154 uc_priv->gpio_count = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
155 "ngpios", 0);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900156
157 return 0;
158}
159
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900160static const struct udevice_id uniphier_gpio_match[] = {
161 { .compatible = "socionext,uniphier-gpio" },
162 { /* sentinel */ }
163};
164
165U_BOOT_DRIVER(uniphier_gpio) = {
Masahiro Yamadac5fb1c22017-10-13 19:21:51 +0900166 .name = "uniphier-gpio",
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900167 .id = UCLASS_GPIO,
168 .of_match = uniphier_gpio_match,
169 .probe = uniphier_gpio_probe,
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900170 .priv_auto_alloc_size = sizeof(struct uniphier_gpio_priv),
171 .ops = &uniphier_gpio_ops,
172};