Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Exynos pinctrl driver common code. |
| 4 | * Copyright (C) 2016 Samsung Electronics |
| 5 | * Thomas Abraham <thomas.ab@samsung.com> |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 8 | #include <log.h> |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include "pinctrl-exynos.h" |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | /** |
| 19 | * exynos_pinctrl_setup_peri: setup pinctrl for a peripheral. |
| 20 | * conf: soc specific pin configuration data array |
| 21 | * num_conf: number of configurations in the conf array. |
| 22 | * base: base address of the pin controller. |
| 23 | */ |
| 24 | void exynos_pinctrl_setup_peri(struct exynos_pinctrl_config_data *conf, |
| 25 | unsigned int num_conf, unsigned long base) |
| 26 | { |
| 27 | unsigned int idx, val; |
| 28 | |
| 29 | for (idx = 0; idx < num_conf; idx++) { |
| 30 | val = readl(base + conf[idx].offset); |
| 31 | val &= ~(conf[idx].mask); |
| 32 | val |= conf[idx].value; |
| 33 | writel(val, base + conf[idx].offset); |
| 34 | } |
| 35 | } |
| 36 | |
Sam Protsenko | 18357b3 | 2023-11-30 14:13:47 -0600 | [diff] [blame^] | 37 | static void parse_pin(const char *pin_name, u32 *pin, char *bank_name) |
| 38 | { |
| 39 | u32 idx = 0; |
| 40 | |
| 41 | /* |
| 42 | * The format of the pin name is <bank_name name>-<pin_number>. |
| 43 | * Example: gpa0-4 (gpa0 is the bank_name name and 4 is the pin number. |
| 44 | */ |
| 45 | while (pin_name[idx] != '-') { |
| 46 | bank_name[idx] = pin_name[idx]; |
| 47 | idx++; |
| 48 | } |
| 49 | bank_name[idx] = '\0'; |
| 50 | *pin = pin_name[++idx] - '0'; |
| 51 | } |
| 52 | |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 53 | /* given a pin-name, return the address of pin config registers */ |
| 54 | static unsigned long pin_to_bank_base(struct udevice *dev, const char *pin_name, |
| 55 | u32 *pin) |
| 56 | { |
| 57 | struct exynos_pinctrl_priv *priv = dev_get_priv(dev); |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 58 | const struct samsung_pin_ctrl *pin_ctrl_array = priv->pin_ctrl; |
| 59 | const struct samsung_pin_bank_data *bank_data; |
| 60 | u32 nr_banks, pin_ctrl_idx = 0, idx = 0, bank_base; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 61 | char bank[10]; |
| 62 | |
Sam Protsenko | 18357b3 | 2023-11-30 14:13:47 -0600 | [diff] [blame^] | 63 | parse_pin(pin_name, pin, bank); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 64 | |
| 65 | /* lookup the pin bank data using the pin bank name */ |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 66 | while (true) { |
Sam Protsenko | 769abf7 | 2023-11-30 14:13:46 -0600 | [diff] [blame] | 67 | const struct samsung_pin_ctrl *pin_ctrl = |
| 68 | &pin_ctrl_array[pin_ctrl_idx]; |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 69 | |
| 70 | nr_banks = pin_ctrl->nr_banks; |
| 71 | if (!nr_banks) |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 72 | break; |
| 73 | |
Dzmitry Sankouski | 0024e44 | 2021-10-17 13:45:40 +0300 | [diff] [blame] | 74 | bank_data = pin_ctrl->pin_banks; |
| 75 | for (idx = 0; idx < nr_banks; idx++) { |
| 76 | debug("pinctrl[%d] bank_data[%d] name is: %s\n", |
| 77 | pin_ctrl_idx, idx, bank_data[idx].name); |
| 78 | if (!strcmp(bank, bank_data[idx].name)) { |
| 79 | bank_base = priv->base + bank_data[idx].offset; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | pin_ctrl_idx++; |
| 84 | } |
| 85 | |
| 86 | return bank_base; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * exynos_pinctrl_set_state: configure a pin state. |
| 91 | * dev: the pinctrl device to be configured. |
| 92 | * config: the state to be configured. |
| 93 | */ |
| 94 | int exynos_pinctrl_set_state(struct udevice *dev, struct udevice *config) |
| 95 | { |
| 96 | const void *fdt = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 97 | int node = dev_of_offset(config); |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 98 | unsigned int count, idx, pin_num; |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 99 | unsigned int pinfunc, pinpud, pindrv; |
| 100 | unsigned long reg, value; |
| 101 | const char *name; |
| 102 | |
| 103 | /* |
| 104 | * refer to the following document for the pinctrl bindings |
| 105 | * linux/Documentation/devicetree/bindings/pinctrl/samsung-pinctrl.txt |
| 106 | */ |
Masahiro Yamada | db090a2 | 2016-10-17 20:43:01 +0900 | [diff] [blame] | 107 | count = fdt_stringlist_count(fdt, node, "samsung,pins"); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 108 | if (count <= 0) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | pinfunc = fdtdec_get_int(fdt, node, "samsung,pin-function", -1); |
| 112 | pinpud = fdtdec_get_int(fdt, node, "samsung,pin-pud", -1); |
| 113 | pindrv = fdtdec_get_int(fdt, node, "samsung,pin-drv", -1); |
| 114 | |
| 115 | for (idx = 0; idx < count; idx++) { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 116 | name = fdt_stringlist_get(fdt, node, "samsung,pins", idx, NULL); |
| 117 | if (!name) |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 118 | continue; |
| 119 | reg = pin_to_bank_base(dev, name, &pin_num); |
| 120 | |
| 121 | if (pinfunc != -1) { |
| 122 | value = readl(reg + PIN_CON); |
| 123 | value &= ~(0xf << (pin_num << 2)); |
| 124 | value |= (pinfunc << (pin_num << 2)); |
| 125 | writel(value, reg + PIN_CON); |
| 126 | } |
| 127 | |
| 128 | if (pinpud != -1) { |
| 129 | value = readl(reg + PIN_PUD); |
| 130 | value &= ~(0x3 << (pin_num << 1)); |
| 131 | value |= (pinpud << (pin_num << 1)); |
| 132 | writel(value, reg + PIN_PUD); |
| 133 | } |
| 134 | |
| 135 | if (pindrv != -1) { |
| 136 | value = readl(reg + PIN_DRV); |
| 137 | value &= ~(0x3 << (pin_num << 1)); |
| 138 | value |= (pindrv << (pin_num << 1)); |
| 139 | writel(value, reg + PIN_DRV); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int exynos_pinctrl_probe(struct udevice *dev) |
| 147 | { |
| 148 | struct exynos_pinctrl_priv *priv; |
| 149 | fdt_addr_t base; |
| 150 | |
| 151 | priv = dev_get_priv(dev); |
| 152 | if (!priv) |
| 153 | return -EINVAL; |
| 154 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 155 | base = dev_read_addr(dev); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 156 | if (base == FDT_ADDR_T_NONE) |
| 157 | return -EINVAL; |
| 158 | |
| 159 | priv->base = base; |
| 160 | priv->pin_ctrl = (struct samsung_pin_ctrl *)dev_get_driver_data(dev) + |
Simon Glass | 99382ba | 2020-12-16 21:20:25 -0700 | [diff] [blame] | 161 | dev_seq(dev); |
Thomas Abraham | cf18bff | 2016-04-23 22:18:08 +0530 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |