common: Drop log.h from common header
[platform/kernel/u-boot.git] / drivers / gpio / mscc_sgpio.c
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3  * Microsemi SoCs serial gpio driver
4  *
5  * Author: <lars.povlsen@microchip.com>
6  *
7  * Copyright (c) 2018 Microsemi Corporation
8  */
9
10 #include <common.h>
11 #include <dm.h>
12 #include <log.h>
13 #include <asm/gpio.h>
14 #include <asm/io.h>
15 #include <errno.h>
16 #include <clk.h>
17 #include <dm/device_compat.h>
18 #include <linux/err.h>
19
20 #define MSCC_SGPIOS_PER_BANK    32
21 #define MSCC_SGPIO_BANK_DEPTH   4
22
23 enum {
24         REG_INPUT_DATA,
25         REG_PORT_CONFIG,
26         REG_PORT_ENABLE,
27         REG_SIO_CONFIG,
28         REG_SIO_CLOCK,
29         MAXREG
30 };
31
32 struct mscc_sgpio_bf {
33         u8 beg;
34         u8 end;
35 };
36
37 struct mscc_sgpio_props {
38         u8 regoff[MAXREG];
39         struct mscc_sgpio_bf auto_repeat;
40         struct mscc_sgpio_bf port_width;
41         struct mscc_sgpio_bf clk_freq;
42         struct mscc_sgpio_bf bit_source;
43 };
44
45 #define __M(bf)         GENMASK((bf).end, (bf).beg)
46 #define __F(bf, x)      (__M(bf) & ((x) << (bf).beg))
47 #define __X(bf, x)      (((x) >> (bf).beg) & GENMASK(((bf).end - (bf).beg), 0))
48
49 #define MSCC_M_CFG_SIO_AUTO_REPEAT(p)           BIT(p->props->auto_repeat.beg)
50 #define MSCC_F_CFG_SIO_PORT_WIDTH(p, x)         __F(p->props->port_width, x)
51 #define MSCC_M_CFG_SIO_PORT_WIDTH(p)            __M(p->props->port_width)
52 #define MSCC_F_CLOCK_SIO_CLK_FREQ(p, x)         __F(p->props->clk_freq, x)
53 #define MSCC_M_CLOCK_SIO_CLK_FREQ(p)            __M(p->props->clk_freq)
54 #define MSCC_F_PORT_CFG_BIT_SOURCE(p, x)        __F(p->props->bit_source, x)
55 #define MSCC_X_PORT_CFG_BIT_SOURCE(p, x)        __X(p->props->bit_source, x)
56
57 const struct mscc_sgpio_props props_luton = {
58         .regoff = { 0x00, 0x09, 0x29, 0x2a, 0x2b },
59         .auto_repeat = { 5, 5 },
60         .port_width  = { 2, 3 },
61         .clk_freq    = { 0, 11 },
62         .bit_source  = { 0, 11 },
63 };
64
65 const struct mscc_sgpio_props props_ocelot = {
66         .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 },
67         .auto_repeat = { 10, 10 },
68         .port_width  = {  7, 8  },
69         .clk_freq    = {  8, 19 },
70         .bit_source  = { 12, 23 },
71 };
72
73 struct mscc_sgpio_priv {
74         u32 bitcount;
75         u32 ports;
76         u32 clock;
77         u32 mode[MSCC_SGPIOS_PER_BANK];
78         u32 __iomem *regs;
79         const struct mscc_sgpio_props *props;
80 };
81
82 static inline u32 sgpio_readl(struct mscc_sgpio_priv *priv, u32 rno, u32 off)
83 {
84         u32 __iomem *reg = &priv->regs[priv->props->regoff[rno] + off];
85
86         return readl(reg);
87 }
88
89 static inline void sgpio_writel(struct mscc_sgpio_priv *priv,
90                                 u32 val, u32 rno, u32 off)
91 {
92         u32 __iomem *reg = &priv->regs[priv->props->regoff[rno] + off];
93
94         writel(val, reg);
95 }
96
97 static void sgpio_clrsetbits(struct mscc_sgpio_priv *priv,
98                              u32 rno, u32 off, u32 clear, u32 set)
99 {
100         u32 __iomem *reg = &priv->regs[priv->props->regoff[rno] + off];
101
102         clrsetbits_le32(reg, clear, set);
103 }
104
105 static int mscc_sgpio_direction_input(struct udevice *dev, unsigned int gpio)
106 {
107         struct mscc_sgpio_priv *priv = dev_get_priv(dev);
108
109         u32 port = gpio % MSCC_SGPIOS_PER_BANK;
110         u32 bit = gpio / MSCC_SGPIOS_PER_BANK;
111
112         priv->mode[port] |= BIT(bit);
113
114         return 0;
115 }
116
117 static int mscc_sgpio_direction_output(struct udevice *dev,
118                                        unsigned int gpio, int value)
119 {
120         struct mscc_sgpio_priv *priv = dev_get_priv(dev);
121         u32 port = gpio % MSCC_SGPIOS_PER_BANK;
122         u32 bit = gpio / MSCC_SGPIOS_PER_BANK;
123         u32 mask = 3 << (3 * bit);
124
125         debug("set: port %d, bit %d, mask 0x%08x, value %d\n",
126               port, bit, mask, value);
127
128         value = (value & 3) << (3 * bit);
129         sgpio_clrsetbits(priv, REG_PORT_CONFIG, port,
130                          MSCC_F_PORT_CFG_BIT_SOURCE(priv, mask),
131                          MSCC_F_PORT_CFG_BIT_SOURCE(priv, value));
132         clrbits_le32(&priv->mode[port], BIT(bit));
133
134         return 0;
135 }
136
137 static int mscc_sgpio_get_function(struct udevice *dev, unsigned int gpio)
138 {
139         struct mscc_sgpio_priv *priv = dev_get_priv(dev);
140         u32 port = gpio % MSCC_SGPIOS_PER_BANK;
141         u32 bit = gpio / MSCC_SGPIOS_PER_BANK;
142         u32 val = priv->mode[port] & BIT(bit);
143
144         if (val)
145                 return GPIOF_INPUT;
146         else
147                 return GPIOF_OUTPUT;
148 }
149
150 static int mscc_sgpio_set_value(struct udevice *dev,
151                                 unsigned int gpio, int value)
152 {
153         return mscc_sgpio_direction_output(dev, gpio, value);
154 }
155
156 static int mscc_sgpio_get_value(struct udevice *dev, unsigned int gpio)
157 {
158         struct mscc_sgpio_priv *priv = dev_get_priv(dev);
159         u32 port = gpio % MSCC_SGPIOS_PER_BANK;
160         u32 bit = gpio / MSCC_SGPIOS_PER_BANK;
161         int ret;
162
163         if (mscc_sgpio_get_function(dev, gpio) == GPIOF_INPUT) {
164                 ret = !!(sgpio_readl(priv, REG_INPUT_DATA, bit) & BIT(port));
165         } else {
166                 u32 portval = sgpio_readl(priv, REG_PORT_CONFIG, port);
167
168                 ret = MSCC_X_PORT_CFG_BIT_SOURCE(priv, portval);
169                 ret = !!(ret & (3 << (3 * bit)));
170         }
171
172         debug("get: gpio %d, port %d, bit %d, value %d\n",
173               gpio, port, bit, ret);
174         return ret;
175 }
176
177 static int mscc_sgpio_get_count(struct udevice *dev)
178 {
179         struct ofnode_phandle_args args;
180         int count = 0, i = 0, ret;
181
182         ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3, i, &args);
183         while (ret != -ENOENT) {
184                 count += args.args[2];
185                 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
186                                                  ++i, &args);
187         }
188         return count;
189 }
190
191 static int mscc_sgpio_probe(struct udevice *dev)
192 {
193         struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
194         struct mscc_sgpio_priv *priv = dev_get_priv(dev);
195         int err, div_clock = 0, port;
196         u32 val;
197         struct clk clk;
198
199         err = clk_get_by_index(dev, 0, &clk);
200         if (!err) {
201                 err = clk_get_rate(&clk);
202                 if (IS_ERR_VALUE(err)) {
203                         dev_err(dev, "Invalid clk rate\n");
204                         return -EINVAL;
205                 }
206                 div_clock = err;
207         } else {
208                 dev_err(dev, "Failed to get clock\n");
209                 return err;
210         }
211
212         priv->props = (const struct mscc_sgpio_props *)dev_get_driver_data(dev);
213         priv->ports = dev_read_u32_default(dev, "mscc,sgpio-ports", 0xFFFFFFFF);
214         priv->clock = dev_read_u32_default(dev, "mscc,sgpio-frequency",
215                                            12500000);
216         if (priv->clock <= 0 || priv->clock > div_clock) {
217                 dev_err(dev, "Invalid frequency %d\n", priv->clock);
218                 return -EINVAL;
219         }
220
221         uc_priv->gpio_count = mscc_sgpio_get_count(dev);
222         uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios",
223                                                    uc_priv->gpio_count);
224         if (uc_priv->gpio_count < 1 || uc_priv->gpio_count >
225             (4 * MSCC_SGPIOS_PER_BANK)) {
226                 dev_err(dev, "Invalid gpio count %d\n", uc_priv->gpio_count);
227                 return -EINVAL;
228         }
229         priv->bitcount = DIV_ROUND_UP(uc_priv->gpio_count,
230                                       MSCC_SGPIOS_PER_BANK);
231         debug("probe: gpios = %d, bit-count = %d\n",
232               uc_priv->gpio_count, priv->bitcount);
233
234         priv->regs = (u32 __iomem *)dev_read_addr(dev);
235         uc_priv->bank_name = "sgpio";
236
237         sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0,
238                          MSCC_M_CFG_SIO_PORT_WIDTH(priv),
239                          MSCC_F_CFG_SIO_PORT_WIDTH(priv, priv->bitcount - 1) |
240                          MSCC_M_CFG_SIO_AUTO_REPEAT(priv));
241         val = div_clock / priv->clock;
242         debug("probe: div-clock = %d KHz, freq = %d KHz, div = %d\n",
243               div_clock / 1000, priv->clock / 1000, val);
244         sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0,
245                          MSCC_M_CLOCK_SIO_CLK_FREQ(priv),
246                          MSCC_F_CLOCK_SIO_CLK_FREQ(priv, val));
247
248         for (port = 0; port < 32; port++)
249                 sgpio_writel(priv, 0, REG_PORT_CONFIG, port);
250         sgpio_writel(priv, priv->ports, REG_PORT_ENABLE, 0);
251
252         debug("probe: sgpio regs = %p\n", priv->regs);
253
254         return 0;
255 }
256
257 static const struct dm_gpio_ops mscc_sgpio_ops = {
258         .direction_input        = mscc_sgpio_direction_input,
259         .direction_output       = mscc_sgpio_direction_output,
260         .get_function           = mscc_sgpio_get_function,
261         .get_value              = mscc_sgpio_get_value,
262         .set_value              = mscc_sgpio_set_value,
263 };
264
265 static const struct udevice_id mscc_sgpio_ids[] = {
266         { .compatible = "mscc,luton-sgpio", .data = (ulong)&props_luton },
267         { .compatible = "mscc,ocelot-sgpio", .data = (ulong)&props_ocelot },
268         { }
269 };
270
271 U_BOOT_DRIVER(gpio_mscc_sgpio) = {
272         .name                   = "mscc-sgpio",
273         .id                     = UCLASS_GPIO,
274         .of_match               = mscc_sgpio_ids,
275         .ops                    = &mscc_sgpio_ops,
276         .probe                  = mscc_sgpio_probe,
277         .priv_auto_alloc_size   = sizeof(struct mscc_sgpio_priv),
278 };