blob: 23bfa7c18d2c6c114c0f6102fe4886e19e257c26 [file] [log] [blame]
Georgii Staroselskiib9928562018-09-11 13:31:07 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2018 Emlid Limited
4 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Georgii Staroselskiib9928562018-09-11 13:31:07 +03009#include <dm/pinctrl.h>
10#include <dm/read.h>
11#include <regmap.h>
12#include <syscon.h>
13#include <asm/cpu.h>
14#include <asm/scu.h>
15#include <linux/io.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060016#include <linux/printk.h>
Georgii Staroselskiib9928562018-09-11 13:31:07 +030017
18#define BUFCFG_OFFSET 0x100
19
20#define MRFLD_FAMILY_LEN 0x400
21
22/* These are taken from Linux kernel */
23#define MRFLD_PINMODE_MASK 0x07
24
25#define pin_to_bufno(f, p) ((p) - (f)->pin_base)
26
27struct mrfld_family {
28 unsigned int family_number;
29 unsigned int pin_base;
30 size_t npins;
31 void __iomem *regs;
32};
33
34#define MRFLD_FAMILY(b, s, e) \
35 { \
36 .family_number = (b), \
37 .pin_base = (s), \
38 .npins = (e) - (s) + 1, \
39 }
40
Andy Shevchenko6b7655f2021-10-27 14:28:09 +030041/* Now we only support SD/SDIO and I2C families of pins */
Georgii Staroselskiib9928562018-09-11 13:31:07 +030042static struct mrfld_family mrfld_families[] = {
Andy Shevchenko6b7655f2021-10-27 14:28:09 +030043 MRFLD_FAMILY(3, 37, 56),
Georgii Staroselskiib9928562018-09-11 13:31:07 +030044 MRFLD_FAMILY(7, 101, 114),
45};
46
47struct mrfld_pinctrl {
48 const struct mrfld_family *families;
49 size_t nfamilies;
50};
51
52static const struct mrfld_family *
53mrfld_get_family(struct mrfld_pinctrl *mp, unsigned int pin)
54{
55 const struct mrfld_family *family;
56 unsigned int i;
57
58 for (i = 0; i < mp->nfamilies; i++) {
59 family = &mp->families[i];
60 if (pin >= family->pin_base &&
61 pin < family->pin_base + family->npins)
62 return family;
63 }
64
65 pr_err("failed to find family for pin %u\n", pin);
66 return NULL;
67}
68
69static void __iomem *
70mrfld_get_bufcfg(struct mrfld_pinctrl *pinctrl, unsigned int pin)
71{
72 const struct mrfld_family *family;
73 unsigned int bufno;
74
75 family = mrfld_get_family(pinctrl, pin);
76 if (!family)
77 return NULL;
78
79 bufno = pin_to_bufno(family, pin);
80
81 return family->regs + BUFCFG_OFFSET + bufno * 4;
82}
83
84static void
85mrfld_setup_families(void *base_addr,
86 struct mrfld_family *families, unsigned int nfam)
87{
88 for (int i = 0; i < nfam; i++) {
89 struct mrfld_family *family = &families[i];
90
91 family->regs = base_addr +
92 family->family_number * MRFLD_FAMILY_LEN;
93 }
94}
95
96static int mrfld_pinconfig_protected(unsigned int pin, u32 mask, u32 bits)
97{
98 struct mrfld_pinctrl *pinctrl;
99 struct udevice *dev;
100 void __iomem *bufcfg;
101 u32 v, value;
102 int ret;
103
104 ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &dev);
105 if (ret)
106 return ret;
107
108 pinctrl = dev_get_priv(dev);
109
110 bufcfg = mrfld_get_bufcfg(pinctrl, pin);
111 if (!bufcfg)
112 return -EINVAL;
113
114 value = readl(bufcfg);
115
116 v = (value & ~mask) | (bits & mask);
117
118 debug("scu: v: 0x%x p: 0x%x bits: %d, mask: %d bufcfg: 0x%p\n",
119 v, (u32)bufcfg, bits, mask, bufcfg);
120
Andy Shevchenkoc02ad502021-10-27 17:23:38 +0300121 return scu_ipc_raw_command(IPCMSG_INDIRECT_WRITE, 0, &v, 4, NULL, 0, (u32)bufcfg, 0);
Georgii Staroselskiib9928562018-09-11 13:31:07 +0300122}
123
Andy Shevchenko6b7655f2021-10-27 14:28:09 +0300124static int mrfld_pinconfig(unsigned int pin, u32 mask, u32 bits)
125{
126 struct mrfld_pinctrl *pinctrl;
127 struct udevice *dev;
128 void __iomem *bufcfg;
129 u32 v, value;
130 int ret;
131
132 ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &dev);
133 if (ret)
134 return ret;
135
136 pinctrl = dev_get_priv(dev);
137
138 bufcfg = mrfld_get_bufcfg(pinctrl, pin);
139 if (!bufcfg)
140 return -EINVAL;
141
142 value = readl(bufcfg);
143 v = (value & ~mask) | (bits & mask);
144 writel(v, bufcfg);
145
146 debug("v: 0x%x p: 0x%x bits: %d, mask: %d bufcfg: 0x%p\n",
147 v, (u32)bufcfg, bits, mask, bufcfg);
148
149 return 0;
150}
151
Georgii Staroselskiib9928562018-09-11 13:31:07 +0300152static int mrfld_pinctrl_cfg_pin(ofnode pin_node)
153{
154 bool is_protected;
155 int pad_offset;
156 int mode;
157 u32 mask;
158 int ret;
159
Georgii Staroselskiib9928562018-09-11 13:31:07 +0300160 pad_offset = ofnode_read_s32_default(pin_node, "pad-offset", -1);
161 if (pad_offset == -1)
162 return -EINVAL;
163
164 mode = ofnode_read_s32_default(pin_node, "mode-func", -1);
165 if (mode == -1)
166 return -EINVAL;
167
168 mask = MRFLD_PINMODE_MASK;
169
170 /* We don't support modes not in range 0..7 */
171 if (mode & ~mask)
172 return -ENOTSUPP;
173
Andy Shevchenko69d18a32021-10-27 17:23:37 +0300174 is_protected = ofnode_read_bool(pin_node, "protected");
Andy Shevchenko6b7655f2021-10-27 14:28:09 +0300175 if (is_protected)
176 ret = mrfld_pinconfig_protected(pad_offset, mask, mode);
177 else
178 ret = mrfld_pinconfig(pad_offset, mask, mode);
Andy Shevchenkoc02ad502021-10-27 17:23:38 +0300179 if (ret)
180 pr_err("Failed to set mode for pin %u (%d)\n", pad_offset, ret);
Georgii Staroselskiib9928562018-09-11 13:31:07 +0300181
182 return ret;
183}
184
185static int tangier_pinctrl_probe(struct udevice *dev)
186{
187 void *base_addr = syscon_get_first_range(X86_SYSCON_PINCONF);
188 struct mrfld_pinctrl *pinctrl = dev_get_priv(dev);
189 ofnode pin_node;
190 int ret;
191
192 mrfld_setup_families(base_addr, mrfld_families,
193 ARRAY_SIZE(mrfld_families));
194
195 pinctrl->families = mrfld_families;
196 pinctrl->nfamilies = ARRAY_SIZE(mrfld_families);
197
198 ofnode_for_each_subnode(pin_node, dev_ofnode(dev)) {
199 ret = mrfld_pinctrl_cfg_pin(pin_node);
200 if (ret) {
201 pr_err("%s: invalid configuration for the pin %ld\n",
202 __func__, pin_node.of_offset);
203 }
204 }
205
206 return 0;
207}
208
209static const struct udevice_id tangier_pinctrl_match[] = {
210 { .compatible = "intel,pinctrl-tangier", .data = X86_SYSCON_PINCONF },
211 { /* sentinel */ }
212};
213
214U_BOOT_DRIVER(tangier_pinctrl) = {
215 .name = "tangier_pinctrl",
216 .id = UCLASS_SYSCON,
217 .of_match = tangier_pinctrl_match,
218 .probe = tangier_pinctrl_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700219 .priv_auto = sizeof(struct mrfld_pinctrl),
Georgii Staroselskiib9928562018-09-11 13:31:07 +0300220};