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