1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2017 Google, Inc
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <log.h>
10#include <asm/io.h>
11#include <asm/arch/pinctrl.h>
12#include <asm/arch/scu_ast2500.h>
13#include <dm/pinctrl.h>
14
15/*
16 * This driver works with very simple configuration that has the same name
17 * for group and function. This way it is compatible with the Linux Kernel
18 * driver.
19 */
20
21struct ast2500_pinctrl_priv {
22	struct ast2500_scu *scu;
23};
24
25static int ast2500_pinctrl_probe(struct udevice *dev)
26{
27	struct ast2500_pinctrl_priv *priv = dev_get_priv(dev);
28
29	priv->scu = ast_get_scu();
30
31	return 0;
32}
33
34struct ast2500_group_config {
35	char *group_name;
36	/* Control register number (1-10) */
37	unsigned reg_num;
38	/* The mask of control bits in the register */
39	u32 ctrl_bit_mask;
40};
41
42static const struct ast2500_group_config ast2500_groups[] = {
43	{ "I2C1", 8, (1 << 13) | (1 << 12) },
44	{ "I2C2", 8, (1 << 15) | (1 << 14) },
45	{ "I2C3", 8, (1 << 16) },
46	{ "I2C4", 5, (1 << 17) },
47	{ "I2C4", 5, (1 << 17) },
48	{ "I2C5", 5, (1 << 18) },
49	{ "I2C6", 5, (1 << 19) },
50	{ "I2C7", 5, (1 << 20) },
51	{ "I2C8", 5, (1 << 21) },
52	{ "I2C9", 5, (1 << 22) },
53	{ "I2C10", 5, (1 << 23) },
54	{ "I2C11", 5, (1 << 24) },
55	{ "I2C12", 5, (1 << 25) },
56	{ "I2C13", 5, (1 << 26) },
57	{ "I2C14", 5, (1 << 27) },
58	{ "MAC1LINK", 1, (1 << 0) },
59	{ "MDIO1", 3, (1 << 31) | (1 << 30) },
60	{ "MAC2LINK", 1, (1 << 1) },
61	{ "MDIO2", 5, (1 << 2) },
62	{ "SD1", 5, (1 << 0) },
63	{ "SD2", 5, (1 << 1) },
64	{ "FWSPICS1", 3, (1 << 24) },
65	{ "SPI1CS1", 1, (1 << 15) },
66};
67
68static int ast2500_pinctrl_get_groups_count(struct udevice *dev)
69{
70	debug("PINCTRL: get_(functions/groups)_count\n");
71
72	return ARRAY_SIZE(ast2500_groups);
73}
74
75static const char *ast2500_pinctrl_get_group_name(struct udevice *dev,
76						  unsigned selector)
77{
78	debug("PINCTRL: get_(function/group)_name %u\n", selector);
79
80	return ast2500_groups[selector].group_name;
81}
82
83static int ast2500_pinctrl_group_set(struct udevice *dev, unsigned selector,
84				     unsigned func_selector)
85{
86	struct ast2500_pinctrl_priv *priv = dev_get_priv(dev);
87	const struct ast2500_group_config *config;
88	u32 *ctrl_reg;
89
90	debug("PINCTRL: group_set <%u, %u>\n", selector, func_selector);
91	if (selector >= ARRAY_SIZE(ast2500_groups))
92		return -EINVAL;
93
94	config = &ast2500_groups[selector];
95	if (config->reg_num > 6)
96		ctrl_reg = &priv->scu->pinmux_ctrl1[config->reg_num - 7];
97	else
98		ctrl_reg = &priv->scu->pinmux_ctrl[config->reg_num - 1];
99
100	ast_scu_unlock(priv->scu);
101	setbits_le32(ctrl_reg, config->ctrl_bit_mask);
102	ast_scu_lock(priv->scu);
103
104	return 0;
105}
106
107static struct pinctrl_ops ast2500_pinctrl_ops = {
108	.set_state = pinctrl_generic_set_state,
109	.get_groups_count = ast2500_pinctrl_get_groups_count,
110	.get_group_name = ast2500_pinctrl_get_group_name,
111	.get_functions_count = ast2500_pinctrl_get_groups_count,
112	.get_function_name = ast2500_pinctrl_get_group_name,
113	.pinmux_group_set = ast2500_pinctrl_group_set,
114};
115
116static const struct udevice_id ast2500_pinctrl_ids[] = {
117	{ .compatible = "aspeed,ast2500-pinctrl" },
118	{ .compatible = "aspeed,g5-pinctrl" },
119	{ }
120};
121
122U_BOOT_DRIVER(pinctrl_ast2500) = {
123	.name = "aspeed_ast2500_pinctrl",
124	.id = UCLASS_PINCTRL,
125	.of_match = ast2500_pinctrl_ids,
126	.priv_auto	= sizeof(struct ast2500_pinctrl_priv),
127	.ops = &ast2500_pinctrl_ops,
128	.probe = ast2500_pinctrl_probe,
129};
130