183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 2745df68dSPeng Fan /* 3745df68dSPeng Fan * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com> 4745df68dSPeng Fan */ 5745df68dSPeng Fan 6745df68dSPeng Fan #include <common.h> 7745df68dSPeng Fan #include <mapmem.h> 8745df68dSPeng Fan #include <linux/io.h> 9745df68dSPeng Fan #include <linux/err.h> 109d922450SSimon Glass #include <dm.h> 11745df68dSPeng Fan #include <dm/pinctrl.h> 12745df68dSPeng Fan 13745df68dSPeng Fan #include "pinctrl-imx.h" 14745df68dSPeng Fan 15745df68dSPeng Fan DECLARE_GLOBAL_DATA_PTR; 16745df68dSPeng Fan 17745df68dSPeng Fan static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config) 18745df68dSPeng Fan { 19745df68dSPeng Fan struct imx_pinctrl_priv *priv = dev_get_priv(dev); 20745df68dSPeng Fan struct imx_pinctrl_soc_info *info = priv->info; 21e160f7d4SSimon Glass int node = dev_of_offset(config); 22745df68dSPeng Fan const struct fdt_property *prop; 23745df68dSPeng Fan u32 *pin_data; 24745df68dSPeng Fan int npins, size, pin_size; 25745df68dSPeng Fan int mux_reg, conf_reg, input_reg, input_val, mux_mode, config_val; 264aa9d4d0SPeng Fan u32 mux_shift = info->mux_mask ? ffs(info->mux_mask) - 1 : 0; 27745df68dSPeng Fan int i, j = 0; 28745df68dSPeng Fan 29745df68dSPeng Fan dev_dbg(dev, "%s: %s\n", __func__, config->name); 30745df68dSPeng Fan 31*38b6686fSPeng Fan if (info->flags & IMX8_USE_SCU) 32*38b6686fSPeng Fan pin_size = SHARE_IMX8_PIN_SIZE; 33*38b6686fSPeng Fan else if (info->flags & SHARE_MUX_CONF_REG) 34745df68dSPeng Fan pin_size = SHARE_FSL_PIN_SIZE; 35745df68dSPeng Fan else 36745df68dSPeng Fan pin_size = FSL_PIN_SIZE; 37745df68dSPeng Fan 38745df68dSPeng Fan prop = fdt_getprop(gd->fdt_blob, node, "fsl,pins", &size); 39745df68dSPeng Fan if (!prop) { 40745df68dSPeng Fan dev_err(dev, "No fsl,pins property in node %s\n", config->name); 41745df68dSPeng Fan return -EINVAL; 42745df68dSPeng Fan } 43745df68dSPeng Fan 44745df68dSPeng Fan if (!size || size % pin_size) { 45745df68dSPeng Fan dev_err(dev, "Invalid fsl,pins property in node %s\n", 46745df68dSPeng Fan config->name); 47745df68dSPeng Fan return -EINVAL; 48745df68dSPeng Fan } 49745df68dSPeng Fan 50745df68dSPeng Fan pin_data = devm_kzalloc(dev, size, 0); 51745df68dSPeng Fan if (!pin_data) 52745df68dSPeng Fan return -ENOMEM; 53745df68dSPeng Fan 54745df68dSPeng Fan if (fdtdec_get_int_array(gd->fdt_blob, node, "fsl,pins", 55745df68dSPeng Fan pin_data, size >> 2)) { 56745df68dSPeng Fan dev_err(dev, "Error reading pin data.\n"); 57c1d1e9d6SPeng Fan devm_kfree(dev, pin_data); 58745df68dSPeng Fan return -EINVAL; 59745df68dSPeng Fan } 60745df68dSPeng Fan 61745df68dSPeng Fan npins = size / pin_size; 62745df68dSPeng Fan 63*38b6686fSPeng Fan if (info->flags & IMX8_USE_SCU) { 64*38b6686fSPeng Fan imx_pinctrl_scu_conf_pins(info, pin_data, npins); 65*38b6686fSPeng Fan } else { 66745df68dSPeng Fan /* 67745df68dSPeng Fan * Refer to linux documentation for details: 68745df68dSPeng Fan * Documentation/devicetree/bindings/pinctrl/fsl,imx-pinctrl.txt 69745df68dSPeng Fan */ 70745df68dSPeng Fan for (i = 0; i < npins; i++) { 71745df68dSPeng Fan mux_reg = pin_data[j++]; 72745df68dSPeng Fan 73745df68dSPeng Fan if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg) 74745df68dSPeng Fan mux_reg = -1; 75745df68dSPeng Fan 76745df68dSPeng Fan if (info->flags & SHARE_MUX_CONF_REG) { 77745df68dSPeng Fan conf_reg = mux_reg; 78745df68dSPeng Fan } else { 79745df68dSPeng Fan conf_reg = pin_data[j++]; 80*38b6686fSPeng Fan if (!(info->flags & ZERO_OFFSET_VALID) && 81*38b6686fSPeng Fan !conf_reg) 82745df68dSPeng Fan conf_reg = -1; 83745df68dSPeng Fan } 84745df68dSPeng Fan 85745df68dSPeng Fan if ((mux_reg == -1) || (conf_reg == -1)) { 86745df68dSPeng Fan dev_err(dev, "Error mux_reg or conf_reg\n"); 87c1d1e9d6SPeng Fan devm_kfree(dev, pin_data); 88745df68dSPeng Fan return -EINVAL; 89745df68dSPeng Fan } 90745df68dSPeng Fan 91745df68dSPeng Fan input_reg = pin_data[j++]; 92745df68dSPeng Fan mux_mode = pin_data[j++]; 93745df68dSPeng Fan input_val = pin_data[j++]; 94745df68dSPeng Fan config_val = pin_data[j++]; 95745df68dSPeng Fan 96*38b6686fSPeng Fan dev_dbg(dev, "mux_reg 0x%x, conf_reg 0x%x, " 97*38b6686fSPeng Fan "input_reg 0x%x, mux_mode 0x%x, " 98*38b6686fSPeng Fan "input_val 0x%x, config_val 0x%x\n", 99*38b6686fSPeng Fan mux_reg, conf_reg, input_reg, mux_mode, 100*38b6686fSPeng Fan input_val, config_val); 101745df68dSPeng Fan 102745df68dSPeng Fan if (config_val & IMX_PAD_SION) 103745df68dSPeng Fan mux_mode |= IOMUXC_CONFIG_SION; 104745df68dSPeng Fan 105745df68dSPeng Fan config_val &= ~IMX_PAD_SION; 106745df68dSPeng Fan 107745df68dSPeng Fan /* Set Mux */ 108745df68dSPeng Fan if (info->flags & SHARE_MUX_CONF_REG) { 109*38b6686fSPeng Fan clrsetbits_le32(info->base + mux_reg, 110*38b6686fSPeng Fan info->mux_mask, 1114aa9d4d0SPeng Fan mux_mode << mux_shift); 112745df68dSPeng Fan } else { 113745df68dSPeng Fan writel(mux_mode, info->base + mux_reg); 114745df68dSPeng Fan } 115745df68dSPeng Fan 116*38b6686fSPeng Fan dev_dbg(dev, "write mux: offset 0x%x val 0x%x\n", 117*38b6686fSPeng Fan mux_reg, mux_mode); 118745df68dSPeng Fan 119745df68dSPeng Fan /* 120745df68dSPeng Fan * Set select input 121745df68dSPeng Fan * 122*38b6686fSPeng Fan * If the select input value begins with 0xff, 123*38b6686fSPeng Fan * it's a quirky select input and the value should 124*38b6686fSPeng Fan * be interpreted as below. 125745df68dSPeng Fan * 31 23 15 7 0 126745df68dSPeng Fan * | 0xff | shift | width | select | 127*38b6686fSPeng Fan * It's used to work around the problem that the 128*38b6686fSPeng Fan * select input for some pin is not implemented in 129*38b6686fSPeng Fan * the select input register but in some general 130*38b6686fSPeng Fan * purpose register. We encode the select input 131*38b6686fSPeng Fan * value, width and shift of the bit field into 132*38b6686fSPeng Fan * input_val cell of pin function ID in device tree, 133*38b6686fSPeng Fan * and then decode them here for setting up the select 134*38b6686fSPeng Fan * input bits in general purpose register. 135745df68dSPeng Fan */ 136745df68dSPeng Fan 137745df68dSPeng Fan if (input_val >> 24 == 0xff) { 138745df68dSPeng Fan u32 val = input_val; 139745df68dSPeng Fan u8 select = val & 0xff; 140745df68dSPeng Fan u8 width = (val >> 8) & 0xff; 141745df68dSPeng Fan u8 shift = (val >> 16) & 0xff; 142745df68dSPeng Fan u32 mask = ((1 << width) - 1) << shift; 143745df68dSPeng Fan /* 144*38b6686fSPeng Fan * The input_reg[i] here is actually some 145*38b6686fSPeng Fan * IOMUXC general purpose register, not 146*38b6686fSPeng Fan * regular select input register. 147745df68dSPeng Fan */ 148745df68dSPeng Fan val = readl(info->base + input_reg); 149745df68dSPeng Fan val &= ~mask; 150745df68dSPeng Fan val |= select << shift; 151745df68dSPeng Fan writel(val, info->base + input_reg); 152745df68dSPeng Fan } else if (input_reg) { 153745df68dSPeng Fan /* 154*38b6686fSPeng Fan * Regular select input register can never be 155*38b6686fSPeng Fan * at offset 0, and we only print register 156*38b6686fSPeng Fan * value for regular case. 157745df68dSPeng Fan */ 158745df68dSPeng Fan if (info->input_sel_base) 159*38b6686fSPeng Fan writel(input_val, 160*38b6686fSPeng Fan info->input_sel_base + 161745df68dSPeng Fan input_reg); 162745df68dSPeng Fan else 163*38b6686fSPeng Fan writel(input_val, 164*38b6686fSPeng Fan info->base + input_reg); 165745df68dSPeng Fan 166*38b6686fSPeng Fan dev_dbg(dev, "select_input: offset 0x%x val " 167*38b6686fSPeng Fan "0x%x\n", input_reg, input_val); 168745df68dSPeng Fan } 169745df68dSPeng Fan 170745df68dSPeng Fan /* Set config */ 171745df68dSPeng Fan if (!(config_val & IMX_NO_PAD_CTL)) { 172745df68dSPeng Fan if (info->flags & SHARE_MUX_CONF_REG) { 1734aa9d4d0SPeng Fan clrsetbits_le32(info->base + conf_reg, 174*38b6686fSPeng Fan ~info->mux_mask, 175*38b6686fSPeng Fan config_val); 176745df68dSPeng Fan } else { 177*38b6686fSPeng Fan writel(config_val, 178*38b6686fSPeng Fan info->base + conf_reg); 179745df68dSPeng Fan } 180745df68dSPeng Fan 181*38b6686fSPeng Fan dev_dbg(dev, "write config: offset 0x%x val " 182*38b6686fSPeng Fan "0x%x\n", conf_reg, config_val); 183*38b6686fSPeng Fan } 184745df68dSPeng Fan } 185745df68dSPeng Fan } 186745df68dSPeng Fan 187c1d1e9d6SPeng Fan devm_kfree(dev, pin_data); 188c1d1e9d6SPeng Fan 189745df68dSPeng Fan return 0; 190745df68dSPeng Fan } 191745df68dSPeng Fan 192745df68dSPeng Fan const struct pinctrl_ops imx_pinctrl_ops = { 193745df68dSPeng Fan .set_state = imx_pinctrl_set_state, 194745df68dSPeng Fan }; 195745df68dSPeng Fan 196745df68dSPeng Fan int imx_pinctrl_probe(struct udevice *dev, 197745df68dSPeng Fan struct imx_pinctrl_soc_info *info) 198745df68dSPeng Fan { 199745df68dSPeng Fan struct imx_pinctrl_priv *priv = dev_get_priv(dev); 200e160f7d4SSimon Glass int node = dev_of_offset(dev), ret; 201745df68dSPeng Fan struct fdtdec_phandle_args arg; 202745df68dSPeng Fan fdt_addr_t addr; 203745df68dSPeng Fan fdt_size_t size; 204745df68dSPeng Fan 205745df68dSPeng Fan if (!info) { 206745df68dSPeng Fan dev_err(dev, "wrong pinctrl info\n"); 207745df68dSPeng Fan return -EINVAL; 208745df68dSPeng Fan } 209745df68dSPeng Fan 210745df68dSPeng Fan priv->dev = dev; 211745df68dSPeng Fan priv->info = info; 212745df68dSPeng Fan 213*38b6686fSPeng Fan if (info->flags & IMX8_USE_SCU) 214*38b6686fSPeng Fan return 0; 215*38b6686fSPeng Fan 216e160f7d4SSimon Glass addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg", 217e160f7d4SSimon Glass &size); 218745df68dSPeng Fan 219745df68dSPeng Fan if (addr == FDT_ADDR_T_NONE) 220745df68dSPeng Fan return -EINVAL; 221745df68dSPeng Fan 222745df68dSPeng Fan info->base = map_sysmem(addr, size); 223745df68dSPeng Fan if (!info->base) 224745df68dSPeng Fan return -ENOMEM; 225745df68dSPeng Fan priv->info = info; 226745df68dSPeng Fan 2274aa9d4d0SPeng Fan info->mux_mask = fdtdec_get_int(gd->fdt_blob, node, "fsl,mux_mask", 0); 228745df68dSPeng Fan /* 229745df68dSPeng Fan * Refer to linux documentation for details: 230745df68dSPeng Fan * Documentation/devicetree/bindings/pinctrl/fsl,imx7d-pinctrl.txt 231745df68dSPeng Fan */ 232745df68dSPeng Fan if (fdtdec_get_bool(gd->fdt_blob, node, "fsl,input-sel")) { 233745df68dSPeng Fan ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, 234745df68dSPeng Fan node, "fsl,input-sel", 235745df68dSPeng Fan NULL, 0, 0, &arg); 236745df68dSPeng Fan if (ret) { 237745df68dSPeng Fan dev_err(dev, "iomuxc fsl,input-sel property not found\n"); 238745df68dSPeng Fan return -EINVAL; 239745df68dSPeng Fan } 240745df68dSPeng Fan 241745df68dSPeng Fan addr = fdtdec_get_addr_size(gd->fdt_blob, arg.node, "reg", 242745df68dSPeng Fan &size); 243745df68dSPeng Fan if (addr == FDT_ADDR_T_NONE) 244745df68dSPeng Fan return -EINVAL; 245745df68dSPeng Fan 246745df68dSPeng Fan info->input_sel_base = map_sysmem(addr, size); 247745df68dSPeng Fan if (!info->input_sel_base) 248745df68dSPeng Fan return -ENOMEM; 249745df68dSPeng Fan } 250745df68dSPeng Fan 2515a6f8d7bSStefan Agner dev_dbg(dev, "initialized IMX pinctrl driver\n"); 252745df68dSPeng Fan 253745df68dSPeng Fan return 0; 254745df68dSPeng Fan } 255745df68dSPeng Fan 256745df68dSPeng Fan int imx_pinctrl_remove(struct udevice *dev) 257745df68dSPeng Fan { 258745df68dSPeng Fan struct imx_pinctrl_priv *priv = dev_get_priv(dev); 259745df68dSPeng Fan struct imx_pinctrl_soc_info *info = priv->info; 260745df68dSPeng Fan 261*38b6686fSPeng Fan if (info->flags & IMX8_USE_SCU) 262*38b6686fSPeng Fan return 0; 263*38b6686fSPeng Fan 264745df68dSPeng Fan if (info->input_sel_base) 265745df68dSPeng Fan unmap_sysmem(info->input_sel_base); 266745df68dSPeng Fan if (info->base) 267745df68dSPeng Fan unmap_sysmem(info->base); 268745df68dSPeng Fan 269745df68dSPeng Fan return 0; 270745df68dSPeng Fan } 271