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