blob: 550de384ed51b5ad0d77f68b02f67091883a53d5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Konstantin Porotchkinaed83152016-12-08 12:22:29 +02002/*
3 * Copyright (C) 2016 Marvell International Ltd.
Konstantin Porotchkinaed83152016-12-08 12:22:29 +02004 * https://spdx.org/licenses
5 */
6
7#include <common.h>
8#include <config.h>
9#include <fdtdec.h>
10#include <errno.h>
11#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020013#include <dm/pinctrl.h>
14#include <dm/root.h>
15#include <asm/system.h>
16#include <asm/io.h>
17#include <asm/arch-armada8k/soc-info.h>
18#include "pinctrl-mvebu.h"
19
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +080020#define AP_EMMC_PHY_CTRL_REG 0x100
21#define CP_EMMC_PHY_CTRL_REG 0x424
22#define EMMC_PHY_CTRL_SDPHY_EN BIT(0)
23
24#define AP806_EMMC_CLK_PIN_ID 0
25#define AP806_EMMC_CLK_FUNC 0x1
26#define CP110_EMMC_CLK_PIN_ID 56
27#define CP110_EMMC_CLK_FUNC 0xe
28
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020029DECLARE_GLOBAL_DATA_PTR;
30
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +080031/* mvebu_pinctl_emmc_set_mux: configure sd/mmc PHY mux
32 * To enable SDIO/eMMC in Armada-APN806/CP110, need to configure PHY mux.
33 * eMMC/SD PHY register responsible for muxing between MPPs and SD/eMMC
34 * controller:
35 * - Bit0 enabled SDIO/eMMC PHY is used as a MPP muxltiplexer,
36 * - Bit0 disabled SDIO/eMMC PHY is connected to SDIO/eMMC controller
37 * If pin function is set to eMMC/SD, then configure the eMMC/SD PHY
38 * muxltiplexer register to be on SDIO/eMMC controller
39 */
40void mvebu_pinctl_emmc_set_mux(struct udevice *dev, u32 pin, u32 func)
41{
42 const void *blob = gd->fdt_blob;
43 int node = dev_of_offset(dev);
44 struct mvebu_pinctrl_priv *priv = dev_get_priv(dev);
45
46 if (!fdt_node_check_compatible(blob, node, "marvell,ap806-pinctrl")) {
47 if ((pin == AP806_EMMC_CLK_PIN_ID) &&
48 (func == AP806_EMMC_CLK_FUNC)) {
49 clrbits_le32(priv->base_reg + AP_EMMC_PHY_CTRL_REG,
50 EMMC_PHY_CTRL_SDPHY_EN);
51 }
52 } else if (!fdt_node_check_compatible(blob, node,
53 "marvell,armada-8k-cpm-pinctrl")) {
54 if ((pin == CP110_EMMC_CLK_PIN_ID) &&
55 (func == CP110_EMMC_CLK_FUNC)) {
56 clrbits_le32(priv->base_reg + CP_EMMC_PHY_CTRL_REG,
57 EMMC_PHY_CTRL_SDPHY_EN);
58 }
59 }
60}
61
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020062/*
63 * mvebu_pinctrl_set_state: configure pin functions.
64 * @dev: the pinctrl device to be configured.
65 * @config: the state to be configured.
66 * @return: 0 in success
67 */
68int mvebu_pinctrl_set_state(struct udevice *dev, struct udevice *config)
69{
70 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070071 int node = dev_of_offset(config);
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020072 struct mvebu_pinctrl_priv *priv;
73 u32 pin_arr[MVEBU_MAX_PINS_PER_BANK];
74 u32 function;
75 int i, pin_count;
76
77 priv = dev_get_priv(dev);
78
79 pin_count = fdtdec_get_int_array_count(blob, node,
80 "marvell,pins",
81 pin_arr,
82 MVEBU_MAX_PINS_PER_BANK);
83 if (pin_count <= 0) {
84 debug("Failed reading pins array for pinconfig %s (%d)\n",
85 config->name, pin_count);
86 return -EINVAL;
87 }
88
89 function = fdtdec_get_int(blob, node, "marvell,function", 0xff);
90
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +080091 /*
92 * Check if setup of PHY mux is needed for this pins group.
93 * Only the first pin id in array is tested, all the rest use the same
94 * pin function.
95 */
96 mvebu_pinctl_emmc_set_mux(dev, pin_arr[0], function);
97
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020098 for (i = 0; i < pin_count; i++) {
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +080099 int reg_offset;
100 int field_offset;
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200101 int pin = pin_arr[i];
102
103 if (function > priv->max_func) {
104 debug("Illegal function %d for pinconfig %s\n",
105 function, config->name);
106 return -EINVAL;
107 }
108
109 /* Calculate register address and bit in register */
110 reg_offset = priv->reg_direction * 4 *
111 (pin >> (PIN_REG_SHIFT));
112 field_offset = (BITS_PER_PIN) * (pin & PIN_FIELD_MASK);
113
114 clrsetbits_le32(priv->base_reg + reg_offset,
115 PIN_FUNC_MASK << field_offset,
116 (function & PIN_FUNC_MASK) << field_offset);
117 }
118
119 return 0;
120}
121
122/*
123 * mvebu_pinctrl_set_state_all: configure the entire bank pin functions.
124 * @dev: the pinctrl device to be configured.
125 * @config: the state to be configured.
126 * @return: 0 in success
127 */
128static int mvebu_pinctrl_set_state_all(struct udevice *dev,
129 struct udevice *config)
130{
131 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700132 int node = dev_of_offset(config);
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200133 struct mvebu_pinctrl_priv *priv;
134 u32 func_arr[MVEBU_MAX_PINS_PER_BANK];
135 int pin, err;
136
137 priv = dev_get_priv(dev);
138
139 err = fdtdec_get_int_array(blob, node, "pin-func",
140 func_arr, priv->pin_cnt);
141 if (err) {
142 debug("Failed reading pin functions for bank %s\n",
143 priv->bank_name);
144 return -EINVAL;
145 }
146
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +0800147 /* Check if setup of PHY mux is needed for this pins group. */
148 if (priv->pin_cnt < CP110_EMMC_CLK_PIN_ID)
149 mvebu_pinctl_emmc_set_mux(dev, AP806_EMMC_CLK_PIN_ID,
150 func_arr[AP806_EMMC_CLK_PIN_ID]);
151 else
152 mvebu_pinctl_emmc_set_mux(dev, CP110_EMMC_CLK_PIN_ID,
153 func_arr[CP110_EMMC_CLK_PIN_ID]);
154
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200155 for (pin = 0; pin < priv->pin_cnt; pin++) {
156 int reg_offset;
157 int field_offset;
158 u32 func = func_arr[pin];
159
160 /* Bypass pins with function 0xFF */
161 if (func == 0xff) {
162 debug("Warning: pin %d value is not modified ", pin);
163 debug("(kept as default)\n");
164 continue;
165 } else if (func > priv->max_func) {
166 debug("Illegal function %d for pin %d\n", func, pin);
167 return -EINVAL;
168 }
169
170 /* Calculate register address and bit in register */
171 reg_offset = priv->reg_direction * 4 *
172 (pin >> (PIN_REG_SHIFT));
173 field_offset = (BITS_PER_PIN) * (pin & PIN_FIELD_MASK);
174
175 clrsetbits_le32(priv->base_reg + reg_offset,
176 PIN_FUNC_MASK << field_offset,
177 (func & PIN_FUNC_MASK) << field_offset);
178 }
179
180 return 0;
181}
182
183int mvebu_pinctl_probe(struct udevice *dev)
184{
185 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700186 int node = dev_of_offset(dev);
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200187 struct mvebu_pinctrl_priv *priv;
188
189 priv = dev_get_priv(dev);
190 if (!priv) {
191 debug("%s: Failed to get private\n", __func__);
192 return -EINVAL;
193 }
194
Simon Glassba1dea42017-05-17 17:18:05 -0600195 priv->base_reg = devfdt_get_addr_ptr(dev);
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200196 if (priv->base_reg == (void *)FDT_ADDR_T_NONE) {
197 debug("%s: Failed to get base address\n", __func__);
198 return -EINVAL;
199 }
200
201 priv->pin_cnt = fdtdec_get_int(blob, node, "pin-count",
202 MVEBU_MAX_PINS_PER_BANK);
203 priv->max_func = fdtdec_get_int(blob, node, "max-func",
204 MVEBU_MAX_FUNC);
205 priv->bank_name = fdt_getprop(blob, node, "bank-name", NULL);
206
207 priv->reg_direction = 1;
208 if (fdtdec_get_bool(blob, node, "reverse-reg"))
209 priv->reg_direction = -1;
210
211 return mvebu_pinctrl_set_state_all(dev, dev);
212}
213
214static struct pinctrl_ops mvebu_pinctrl_ops = {
215 .set_state = mvebu_pinctrl_set_state
216};
217
218static const struct udevice_id mvebu_pinctrl_ids[] = {
219 { .compatible = "marvell,mvebu-pinctrl" },
Evan Wang14143862018-05-25 14:20:51 +0800220 { .compatible = "marvell,ap806-pinctrl" },
221 { .compatible = "marvell,armada-7k-pinctrl" },
222 { .compatible = "marvell,armada-8k-cpm-pinctrl" },
223 { .compatible = "marvell,armada-8k-cps-pinctrl" },
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200224 { }
225};
226
227U_BOOT_DRIVER(pinctrl_mvebu) = {
228 .name = "mvebu_pinctrl",
229 .id = UCLASS_PINCTRL,
230 .of_match = mvebu_pinctrl_ids,
231 .priv_auto_alloc_size = sizeof(struct mvebu_pinctrl_priv),
232 .ops = &mvebu_pinctrl_ops,
233 .probe = mvebu_pinctl_probe
234};