blob: 0d5fa4ceb9ca38ebfa2a129722aeb3628e680d74 [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
Konstantin Porotchkinaed83152016-12-08 12:22:29 +02007#include <config.h>
8#include <fdtdec.h>
9#include <errno.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.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>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020019#include "pinctrl-mvebu.h"
20
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +080021#define AP_EMMC_PHY_CTRL_REG 0x100
22#define CP_EMMC_PHY_CTRL_REG 0x424
23#define EMMC_PHY_CTRL_SDPHY_EN BIT(0)
24
25#define AP806_EMMC_CLK_PIN_ID 0
26#define AP806_EMMC_CLK_FUNC 0x1
27#define CP110_EMMC_CLK_PIN_ID 56
28#define CP110_EMMC_CLK_FUNC 0xe
29
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020030DECLARE_GLOBAL_DATA_PTR;
31
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +080032/* mvebu_pinctl_emmc_set_mux: configure sd/mmc PHY mux
33 * To enable SDIO/eMMC in Armada-APN806/CP110, need to configure PHY mux.
34 * eMMC/SD PHY register responsible for muxing between MPPs and SD/eMMC
35 * controller:
36 * - Bit0 enabled SDIO/eMMC PHY is used as a MPP muxltiplexer,
37 * - Bit0 disabled SDIO/eMMC PHY is connected to SDIO/eMMC controller
38 * If pin function is set to eMMC/SD, then configure the eMMC/SD PHY
39 * muxltiplexer register to be on SDIO/eMMC controller
40 */
41void mvebu_pinctl_emmc_set_mux(struct udevice *dev, u32 pin, u32 func)
42{
43 const void *blob = gd->fdt_blob;
44 int node = dev_of_offset(dev);
45 struct mvebu_pinctrl_priv *priv = dev_get_priv(dev);
46
47 if (!fdt_node_check_compatible(blob, node, "marvell,ap806-pinctrl")) {
48 if ((pin == AP806_EMMC_CLK_PIN_ID) &&
49 (func == AP806_EMMC_CLK_FUNC)) {
50 clrbits_le32(priv->base_reg + AP_EMMC_PHY_CTRL_REG,
51 EMMC_PHY_CTRL_SDPHY_EN);
52 }
53 } else if (!fdt_node_check_compatible(blob, node,
Konstantin Porotchkin08e8f702022-07-25 14:13:02 +020054 "marvell,armada-8k-cpm-pinctrl") ||
55 !fdt_node_check_compatible(blob, node,
56 "marvell,armada-7k-pinctrl")) {
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +080057 if ((pin == CP110_EMMC_CLK_PIN_ID) &&
58 (func == CP110_EMMC_CLK_FUNC)) {
59 clrbits_le32(priv->base_reg + CP_EMMC_PHY_CTRL_REG,
60 EMMC_PHY_CTRL_SDPHY_EN);
61 }
62 }
63}
64
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020065/*
66 * mvebu_pinctrl_set_state: configure pin functions.
67 * @dev: the pinctrl device to be configured.
68 * @config: the state to be configured.
69 * @return: 0 in success
70 */
71int mvebu_pinctrl_set_state(struct udevice *dev, struct udevice *config)
72{
73 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -070074 int node = dev_of_offset(config);
Konstantin Porotchkinaed83152016-12-08 12:22:29 +020075 struct mvebu_pinctrl_priv *priv;
76 u32 pin_arr[MVEBU_MAX_PINS_PER_BANK];
77 u32 function;
78 int i, pin_count;
79
80 priv = dev_get_priv(dev);
81
82 pin_count = fdtdec_get_int_array_count(blob, node,
83 "marvell,pins",
84 pin_arr,
85 MVEBU_MAX_PINS_PER_BANK);
86 if (pin_count <= 0) {
87 debug("Failed reading pins array for pinconfig %s (%d)\n",
88 config->name, pin_count);
89 return -EINVAL;
90 }
91
92 function = fdtdec_get_int(blob, node, "marvell,function", 0xff);
93
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +080094 /*
95 * Check if setup of PHY mux is needed for this pins group.
96 * Only the first pin id in array is tested, all the rest use the same
97 * pin function.
98 */
99 mvebu_pinctl_emmc_set_mux(dev, pin_arr[0], function);
100
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200101 for (i = 0; i < pin_count; i++) {
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +0800102 int reg_offset;
103 int field_offset;
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200104 int pin = pin_arr[i];
105
106 if (function > priv->max_func) {
107 debug("Illegal function %d for pinconfig %s\n",
108 function, config->name);
109 return -EINVAL;
110 }
111
112 /* Calculate register address and bit in register */
113 reg_offset = priv->reg_direction * 4 *
114 (pin >> (PIN_REG_SHIFT));
115 field_offset = (BITS_PER_PIN) * (pin & PIN_FIELD_MASK);
116
117 clrsetbits_le32(priv->base_reg + reg_offset,
118 PIN_FUNC_MASK << field_offset,
119 (function & PIN_FUNC_MASK) << field_offset);
120 }
121
122 return 0;
123}
124
125/*
126 * mvebu_pinctrl_set_state_all: configure the entire bank pin functions.
127 * @dev: the pinctrl device to be configured.
128 * @config: the state to be configured.
129 * @return: 0 in success
130 */
131static int mvebu_pinctrl_set_state_all(struct udevice *dev,
132 struct udevice *config)
133{
134 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700135 int node = dev_of_offset(config);
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200136 struct mvebu_pinctrl_priv *priv;
137 u32 func_arr[MVEBU_MAX_PINS_PER_BANK];
138 int pin, err;
139
140 priv = dev_get_priv(dev);
141
142 err = fdtdec_get_int_array(blob, node, "pin-func",
143 func_arr, priv->pin_cnt);
144 if (err) {
145 debug("Failed reading pin functions for bank %s\n",
146 priv->bank_name);
147 return -EINVAL;
148 }
149
Konstantin Porotchkin3ee25512018-05-25 14:20:52 +0800150 /* Check if setup of PHY mux is needed for this pins group. */
151 if (priv->pin_cnt < CP110_EMMC_CLK_PIN_ID)
152 mvebu_pinctl_emmc_set_mux(dev, AP806_EMMC_CLK_PIN_ID,
153 func_arr[AP806_EMMC_CLK_PIN_ID]);
154 else
155 mvebu_pinctl_emmc_set_mux(dev, CP110_EMMC_CLK_PIN_ID,
156 func_arr[CP110_EMMC_CLK_PIN_ID]);
157
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200158 for (pin = 0; pin < priv->pin_cnt; pin++) {
159 int reg_offset;
160 int field_offset;
161 u32 func = func_arr[pin];
162
163 /* Bypass pins with function 0xFF */
164 if (func == 0xff) {
165 debug("Warning: pin %d value is not modified ", pin);
166 debug("(kept as default)\n");
167 continue;
168 } else if (func > priv->max_func) {
169 debug("Illegal function %d for pin %d\n", func, pin);
170 return -EINVAL;
171 }
172
173 /* Calculate register address and bit in register */
174 reg_offset = priv->reg_direction * 4 *
175 (pin >> (PIN_REG_SHIFT));
176 field_offset = (BITS_PER_PIN) * (pin & PIN_FIELD_MASK);
177
178 clrsetbits_le32(priv->base_reg + reg_offset,
179 PIN_FUNC_MASK << field_offset,
180 (func & PIN_FUNC_MASK) << field_offset);
181 }
182
183 return 0;
184}
185
186int mvebu_pinctl_probe(struct udevice *dev)
187{
188 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700189 int node = dev_of_offset(dev);
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200190 struct mvebu_pinctrl_priv *priv;
191
192 priv = dev_get_priv(dev);
193 if (!priv) {
194 debug("%s: Failed to get private\n", __func__);
195 return -EINVAL;
196 }
197
Masahiro Yamada32822d02020-08-04 14:14:43 +0900198 priv->base_reg = dev_read_addr_ptr(dev);
Ovidiu Panaita633f002020-08-03 22:17:35 +0300199 if (!priv->base_reg) {
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200200 debug("%s: Failed to get base address\n", __func__);
201 return -EINVAL;
202 }
203
204 priv->pin_cnt = fdtdec_get_int(blob, node, "pin-count",
205 MVEBU_MAX_PINS_PER_BANK);
206 priv->max_func = fdtdec_get_int(blob, node, "max-func",
207 MVEBU_MAX_FUNC);
208 priv->bank_name = fdt_getprop(blob, node, "bank-name", NULL);
209
210 priv->reg_direction = 1;
211 if (fdtdec_get_bool(blob, node, "reverse-reg"))
212 priv->reg_direction = -1;
213
214 return mvebu_pinctrl_set_state_all(dev, dev);
215}
216
217static struct pinctrl_ops mvebu_pinctrl_ops = {
218 .set_state = mvebu_pinctrl_set_state
219};
220
221static const struct udevice_id mvebu_pinctrl_ids[] = {
222 { .compatible = "marvell,mvebu-pinctrl" },
Evan Wang14143862018-05-25 14:20:51 +0800223 { .compatible = "marvell,ap806-pinctrl" },
224 { .compatible = "marvell,armada-7k-pinctrl" },
225 { .compatible = "marvell,armada-8k-cpm-pinctrl" },
226 { .compatible = "marvell,armada-8k-cps-pinctrl" },
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200227 { }
228};
229
230U_BOOT_DRIVER(pinctrl_mvebu) = {
231 .name = "mvebu_pinctrl",
232 .id = UCLASS_PINCTRL,
233 .of_match = mvebu_pinctrl_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700234 .priv_auto = sizeof(struct mvebu_pinctrl_priv),
Konstantin Porotchkinaed83152016-12-08 12:22:29 +0200235 .ops = &mvebu_pinctrl_ops,
236 .probe = mvebu_pinctl_probe
237};