blob: bde51eab15bb1f561bb4eaacfcc939dceddba10c [file] [log] [blame]
Masahiro Yamadab9a66b62016-02-16 17:03:48 +09001/*
2 * Copyright (C) 2016 Masahiro Yamada <yamada.masahiro@socionext.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm/device.h>
9#include <mapmem.h>
10#include <linux/bitops.h>
11#include <linux/io.h>
Masahiro Yamadabc82a132016-03-24 22:32:41 +090012#include <linux/sizes.h>
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090013#include <asm/errno.h>
14#include <asm/gpio.h>
15
16#define UNIPHIER_GPIO_PORTS_PER_BANK 8
17
18#define UNIPHIER_GPIO_REG_DATA 0 /* data */
19#define UNIPHIER_GPIO_REG_DIR 4 /* direction (1:in, 0:out) */
20
21struct uniphier_gpio_priv {
22 void __iomem *base;
23 char bank_name[16];
24};
25
26static void uniphier_gpio_offset_write(struct udevice *dev, unsigned offset,
27 unsigned reg, int value)
28{
29 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
30 u32 tmp;
31
32 tmp = readl(priv->base + reg);
33 if (value)
34 tmp |= BIT(offset);
35 else
36 tmp &= ~BIT(offset);
37 writel(tmp, priv->base + reg);
38}
39
40static int uniphier_gpio_offset_read(struct udevice *dev, unsigned offset,
41 unsigned reg)
42{
43 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
44
45 return !!(readl(priv->base + reg) & BIT(offset));
46}
47
48static int uniphier_gpio_direction_input(struct udevice *dev, unsigned offset)
49{
50 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DIR, 1);
51
52 return 0;
53}
54
55static int uniphier_gpio_direction_output(struct udevice *dev, unsigned offset,
56 int value)
57{
58 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DATA, value);
59 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DIR, 0);
60
61 return 0;
62}
63
64static int uniphier_gpio_get_value(struct udevice *dev, unsigned offset)
65{
66 return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_REG_DATA);
67}
68
69static int uniphier_gpio_set_value(struct udevice *dev, unsigned offset,
70 int value)
71{
72 uniphier_gpio_offset_write(dev, offset, UNIPHIER_GPIO_REG_DATA, value);
73
74 return 0;
75}
76
77static int uniphier_gpio_get_function(struct udevice *dev, unsigned offset)
78{
79 return uniphier_gpio_offset_read(dev, offset, UNIPHIER_GPIO_REG_DIR) ?
80 GPIOF_INPUT : GPIOF_OUTPUT;
81}
82
83static const struct dm_gpio_ops uniphier_gpio_ops = {
84 .direction_input = uniphier_gpio_direction_input,
85 .direction_output = uniphier_gpio_direction_output,
86 .get_value = uniphier_gpio_get_value,
87 .set_value = uniphier_gpio_set_value,
88 .get_function = uniphier_gpio_get_function,
89};
90
91static int uniphier_gpio_probe(struct udevice *dev)
92{
93 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
94 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090095 fdt_addr_t addr;
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090096 unsigned int tmp;
97
Masahiro Yamadabc82a132016-03-24 22:32:41 +090098 addr = dev_get_addr(dev);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +090099 if (addr == FDT_ADDR_T_NONE)
100 return -EINVAL;
101
Masahiro Yamadabc82a132016-03-24 22:32:41 +0900102 priv->base = map_sysmem(addr, SZ_8);
Masahiro Yamadab9a66b62016-02-16 17:03:48 +0900103 if (!priv->base)
104 return -ENOMEM;
105
106 uc_priv->gpio_count = UNIPHIER_GPIO_PORTS_PER_BANK;
107
108 tmp = (addr & 0xfff);
109
110 /* Unfortunately, there is a register hole at offset 0x90-0x9f. */
111 if (tmp > 0x90)
112 tmp -= 0x10;
113
114 snprintf(priv->bank_name, sizeof(priv->bank_name) - 1,
115 "port%d-", (tmp - 8) / 8);
116
117 uc_priv->bank_name = priv->bank_name;
118
119 return 0;
120}
121
122static int uniphier_gpio_remove(struct udevice *dev)
123{
124 struct uniphier_gpio_priv *priv = dev_get_priv(dev);
125
126 unmap_sysmem(priv->base);
127
128 return 0;
129}
130
131/* .data = the number of GPIO banks */
132static const struct udevice_id uniphier_gpio_match[] = {
133 { .compatible = "socionext,uniphier-gpio" },
134 { /* sentinel */ }
135};
136
137U_BOOT_DRIVER(uniphier_gpio) = {
138 .name = "uniphier_gpio",
139 .id = UCLASS_GPIO,
140 .of_match = uniphier_gpio_match,
141 .probe = uniphier_gpio_probe,
142 .remove = uniphier_gpio_remove,
143 .priv_auto_alloc_size = sizeof(struct uniphier_gpio_priv),
144 .ops = &uniphier_gpio_ops,
145};