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