blob: e79ce5113d848702738fc2a3e2f06010287aa3a7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thomas Abrahamcf18bff2016-04-23 22:18:08 +05302/*
3 * Exynos pinctrl driver common code.
4 * Copyright (C) 2016 Samsung Electronics
5 * Thomas Abraham <thomas.ab@samsung.com>
Thomas Abrahamcf18bff2016-04-23 22:18:08 +05306 */
7
Dzmitry Sankouski0024e442021-10-17 13:45:40 +03008#include <log.h>
Thomas Abrahamcf18bff2016-04-23 22:18:08 +05309#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053013#include <asm/io.h>
14#include "pinctrl-exynos.h"
15
16DECLARE_GLOBAL_DATA_PTR;
17
Sam Protsenko2fb6c4f2023-11-30 14:13:49 -060018/* CON, DAT, PUD, DRV */
19const struct samsung_pin_bank_type bank_type_alive = {
20 .fld_width = { 4, 1, 2, 2, },
21 .reg_offset = { 0x00, 0x04, 0x08, 0x0c, },
22};
23
Sam Protsenko0aea23b2023-11-30 14:13:50 -060024static const char * const exynos_pinctrl_props[PINCFG_TYPE_NUM] = {
25 [PINCFG_TYPE_FUNC] = "samsung,pin-function",
26 [PINCFG_TYPE_DAT] = "samsung,pin-val",
27 [PINCFG_TYPE_PUD] = "samsung,pin-pud",
28 [PINCFG_TYPE_DRV] = "samsung,pin-drv",
29};
30
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053031/**
32 * exynos_pinctrl_setup_peri: setup pinctrl for a peripheral.
33 * conf: soc specific pin configuration data array
34 * num_conf: number of configurations in the conf array.
35 * base: base address of the pin controller.
36 */
37void exynos_pinctrl_setup_peri(struct exynos_pinctrl_config_data *conf,
38 unsigned int num_conf, unsigned long base)
39{
40 unsigned int idx, val;
41
42 for (idx = 0; idx < num_conf; idx++) {
43 val = readl(base + conf[idx].offset);
44 val &= ~(conf[idx].mask);
45 val |= conf[idx].value;
46 writel(val, base + conf[idx].offset);
47 }
48}
49
Sam Protsenko18357b32023-11-30 14:13:47 -060050static void parse_pin(const char *pin_name, u32 *pin, char *bank_name)
51{
52 u32 idx = 0;
53
54 /*
55 * The format of the pin name is <bank_name name>-<pin_number>.
56 * Example: gpa0-4 (gpa0 is the bank_name name and 4 is the pin number.
57 */
58 while (pin_name[idx] != '-') {
59 bank_name[idx] = pin_name[idx];
60 idx++;
61 }
62 bank_name[idx] = '\0';
63 *pin = pin_name[++idx] - '0';
64}
65
Sam Protsenko3132b922023-11-30 14:13:48 -060066/* given a bank name, find out the pin bank structure */
67static const struct samsung_pin_bank_data *get_bank(struct udevice *dev,
68 const char *bank_name)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053069{
70 struct exynos_pinctrl_priv *priv = dev_get_priv(dev);
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030071 const struct samsung_pin_ctrl *pin_ctrl_array = priv->pin_ctrl;
72 const struct samsung_pin_bank_data *bank_data;
Sam Protsenko3132b922023-11-30 14:13:48 -060073 u32 nr_banks, pin_ctrl_idx = 0, idx = 0;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053074
75 /* lookup the pin bank data using the pin bank name */
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030076 while (true) {
Sam Protsenko769abf72023-11-30 14:13:46 -060077 const struct samsung_pin_ctrl *pin_ctrl =
78 &pin_ctrl_array[pin_ctrl_idx];
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030079
80 nr_banks = pin_ctrl->nr_banks;
81 if (!nr_banks)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053082 break;
83
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030084 bank_data = pin_ctrl->pin_banks;
85 for (idx = 0; idx < nr_banks; idx++) {
86 debug("pinctrl[%d] bank_data[%d] name is: %s\n",
87 pin_ctrl_idx, idx, bank_data[idx].name);
Sam Protsenko3132b922023-11-30 14:13:48 -060088 if (!strcmp(bank_name, bank_data[idx].name))
89 return &bank_data[idx];
Dzmitry Sankouski0024e442021-10-17 13:45:40 +030090 }
91 pin_ctrl_idx++;
92 }
93
Sam Protsenko3132b922023-11-30 14:13:48 -060094 return NULL;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +053095}
96
Sam Protsenko2fb6c4f2023-11-30 14:13:49 -060097static void exynos_pinctrl_set_pincfg(unsigned long reg_base, u32 pin_num,
98 u32 val, enum pincfg_type pincfg,
99 const struct samsung_pin_bank_type *type)
100{
101 u32 width = type->fld_width[pincfg];
102 u32 reg_offset = type->reg_offset[pincfg];
103 u32 mask = (1 << width) - 1;
104 u32 shift = pin_num * width;
105 u32 data;
106
107 data = readl(reg_base + reg_offset);
108 data &= ~(mask << shift);
109 data |= val << shift;
110 writel(data, reg_base + reg_offset);
111}
112
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530113/**
114 * exynos_pinctrl_set_state: configure a pin state.
115 * dev: the pinctrl device to be configured.
116 * config: the state to be configured.
117 */
118int exynos_pinctrl_set_state(struct udevice *dev, struct udevice *config)
119{
Sam Protsenko3132b922023-11-30 14:13:48 -0600120 struct exynos_pinctrl_priv *priv = dev_get_priv(dev);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530121 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700122 int node = dev_of_offset(config);
Simon Glassb0ea7402016-10-02 17:59:28 -0600123 unsigned int count, idx, pin_num;
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600124 unsigned int pinvals[PINCFG_TYPE_NUM];
Sam Protsenko2fb6c4f2023-11-30 14:13:49 -0600125 unsigned long reg;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530126 const char *name;
127
128 /*
129 * refer to the following document for the pinctrl bindings
130 * linux/Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt
131 */
Masahiro Yamadadb090a22016-10-17 20:43:01 +0900132 count = fdt_stringlist_count(fdt, node, "samsung,pins");
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530133 if (count <= 0)
134 return -EINVAL;
135
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600136 for (idx = 0; idx < PINCFG_TYPE_NUM; ++idx) {
137 pinvals[idx] = fdtdec_get_int(fdt, node,
138 exynos_pinctrl_props[idx], -1);
139 }
140 pinvals[PINCFG_TYPE_DAT] = -1; /* ignore GPIO data register */
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530141
142 for (idx = 0; idx < count; idx++) {
Sam Protsenko3132b922023-11-30 14:13:48 -0600143 const struct samsung_pin_bank_data *bank;
144 char bank_name[10];
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600145 int pincfg;
Sam Protsenko3132b922023-11-30 14:13:48 -0600146
Simon Glassb0ea7402016-10-02 17:59:28 -0600147 name = fdt_stringlist_get(fdt, node, "samsung,pins", idx, NULL);
148 if (!name)
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530149 continue;
Sam Protsenko3132b922023-11-30 14:13:48 -0600150 parse_pin(name, &pin_num, bank_name);
151 bank = get_bank(dev, bank_name);
152 reg = priv->base + bank->offset;
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530153
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600154 for (pincfg = 0; pincfg < PINCFG_TYPE_NUM; ++pincfg) {
155 unsigned int val = pinvals[pincfg];
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530156
Sam Protsenko0aea23b2023-11-30 14:13:50 -0600157 if (val != -1)
158 exynos_pinctrl_set_pincfg(reg, pin_num, val,
159 pincfg, bank->type);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530160 }
161 }
162
163 return 0;
164}
165
166int exynos_pinctrl_probe(struct udevice *dev)
167{
168 struct exynos_pinctrl_priv *priv;
169 fdt_addr_t base;
170
171 priv = dev_get_priv(dev);
172 if (!priv)
173 return -EINVAL;
174
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900175 base = dev_read_addr(dev);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530176 if (base == FDT_ADDR_T_NONE)
177 return -EINVAL;
178
179 priv->base = base;
180 priv->pin_ctrl = (struct samsung_pin_ctrl *)dev_get_driver_data(dev) +
Simon Glass99382ba2020-12-16 21:20:25 -0700181 dev_seq(dev);
Thomas Abrahamcf18bff2016-04-23 22:18:08 +0530182
183 return 0;
184}