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 
21 struct ast2500_pinctrl_priv {
22 	struct ast2500_scu *scu;
23 };
24 
ast2500_pinctrl_probe(struct udevice * dev)25 static 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 
34 struct 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 
42 static 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 };
65 
ast2500_pinctrl_get_groups_count(struct udevice * dev)66 static int ast2500_pinctrl_get_groups_count(struct udevice *dev)
67 {
68 	debug("PINCTRL: get_(functions/groups)_count\n");
69 
70 	return ARRAY_SIZE(ast2500_groups);
71 }
72 
ast2500_pinctrl_get_group_name(struct udevice * dev,unsigned selector)73 static const char *ast2500_pinctrl_get_group_name(struct udevice *dev,
74 						  unsigned selector)
75 {
76 	debug("PINCTRL: get_(function/group)_name %u\n", selector);
77 
78 	return ast2500_groups[selector].group_name;
79 }
80 
ast2500_pinctrl_group_set(struct udevice * dev,unsigned selector,unsigned func_selector)81 static int ast2500_pinctrl_group_set(struct udevice *dev, unsigned selector,
82 				     unsigned func_selector)
83 {
84 	struct ast2500_pinctrl_priv *priv = dev_get_priv(dev);
85 	const struct ast2500_group_config *config;
86 	u32 *ctrl_reg;
87 
88 	debug("PINCTRL: group_set <%u, %u>\n", selector, func_selector);
89 	if (selector >= ARRAY_SIZE(ast2500_groups))
90 		return -EINVAL;
91 
92 	config = &ast2500_groups[selector];
93 	if (config->reg_num > 6)
94 		ctrl_reg = &priv->scu->pinmux_ctrl1[config->reg_num - 7];
95 	else
96 		ctrl_reg = &priv->scu->pinmux_ctrl[config->reg_num - 1];
97 
98 	ast_scu_unlock(priv->scu);
99 	setbits_le32(ctrl_reg, config->ctrl_bit_mask);
100 	ast_scu_lock(priv->scu);
101 
102 	return 0;
103 }
104 
105 static struct pinctrl_ops ast2500_pinctrl_ops = {
106 	.set_state = pinctrl_generic_set_state,
107 	.get_groups_count = ast2500_pinctrl_get_groups_count,
108 	.get_group_name = ast2500_pinctrl_get_group_name,
109 	.get_functions_count = ast2500_pinctrl_get_groups_count,
110 	.get_function_name = ast2500_pinctrl_get_group_name,
111 	.pinmux_group_set = ast2500_pinctrl_group_set,
112 };
113 
114 static const struct udevice_id ast2500_pinctrl_ids[] = {
115 	{ .compatible = "aspeed,ast2500-pinctrl" },
116 	{ .compatible = "aspeed,g5-pinctrl" },
117 	{ }
118 };
119 
120 U_BOOT_DRIVER(pinctrl_ast2500) = {
121 	.name = "aspeed_ast2500_pinctrl",
122 	.id = UCLASS_PINCTRL,
123 	.of_match = ast2500_pinctrl_ids,
124 	.priv_auto	= sizeof(struct ast2500_pinctrl_priv),
125 	.ops = &ast2500_pinctrl_ops,
126 	.probe = ast2500_pinctrl_probe,
127 };
128