pinctrl-imx.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <mapmem.h>
  8. #include <linux/io.h>
  9. #include <linux/err.h>
  10. #include <dm/device.h>
  11. #include <dm/pinctrl.h>
  12. #include "pinctrl-imx.h"
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static int imx_pinctrl_set_state(struct udevice *dev, struct udevice *config)
  15. {
  16. struct imx_pinctrl_priv *priv = dev_get_priv(dev);
  17. struct imx_pinctrl_soc_info *info = priv->info;
  18. int node = config->of_offset;
  19. const struct fdt_property *prop;
  20. u32 *pin_data;
  21. int npins, size, pin_size;
  22. int mux_reg, conf_reg, input_reg, input_val, mux_mode, config_val;
  23. int i, j = 0;
  24. dev_dbg(dev, "%s: %s\n", __func__, config->name);
  25. if (info->flags & SHARE_MUX_CONF_REG)
  26. pin_size = SHARE_FSL_PIN_SIZE;
  27. else
  28. pin_size = FSL_PIN_SIZE;
  29. prop = fdt_getprop(gd->fdt_blob, node, "fsl,pins", &size);
  30. if (!prop) {
  31. dev_err(dev, "No fsl,pins property in node %s\n", config->name);
  32. return -EINVAL;
  33. }
  34. if (!size || size % pin_size) {
  35. dev_err(dev, "Invalid fsl,pins property in node %s\n",
  36. config->name);
  37. return -EINVAL;
  38. }
  39. pin_data = devm_kzalloc(dev, size, 0);
  40. if (!pin_data)
  41. return -ENOMEM;
  42. if (fdtdec_get_int_array(gd->fdt_blob, node, "fsl,pins",
  43. pin_data, size >> 2)) {
  44. dev_err(dev, "Error reading pin data.\n");
  45. return -EINVAL;
  46. }
  47. npins = size / pin_size;
  48. /*
  49. * Refer to linux documentation for details:
  50. * Documentation/devicetree/bindings/pinctrl/fsl,imx-pinctrl.txt
  51. */
  52. for (i = 0; i < npins; i++) {
  53. mux_reg = pin_data[j++];
  54. if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg)
  55. mux_reg = -1;
  56. if (info->flags & SHARE_MUX_CONF_REG) {
  57. conf_reg = mux_reg;
  58. } else {
  59. conf_reg = pin_data[j++];
  60. if (!(info->flags & ZERO_OFFSET_VALID) && !conf_reg)
  61. conf_reg = -1;
  62. }
  63. if ((mux_reg == -1) || (conf_reg == -1)) {
  64. dev_err(dev, "Error mux_reg or conf_reg\n");
  65. return -EINVAL;
  66. }
  67. input_reg = pin_data[j++];
  68. mux_mode = pin_data[j++];
  69. input_val = pin_data[j++];
  70. config_val = pin_data[j++];
  71. dev_dbg(dev, "mux_reg 0x%x, conf_reg 0x%x, input_reg 0x%x, "
  72. "mux_mode 0x%x, input_val 0x%x, config_val 0x%x\n",
  73. mux_reg, conf_reg, input_reg, mux_mode, input_val,
  74. config_val);
  75. if (config_val & IMX_PAD_SION)
  76. mux_mode |= IOMUXC_CONFIG_SION;
  77. config_val &= ~IMX_PAD_SION;
  78. /* Set Mux */
  79. if (info->flags & SHARE_MUX_CONF_REG) {
  80. clrsetbits_le32(info->base + mux_reg, 0x7 << 20,
  81. mux_mode << 20);
  82. } else {
  83. writel(mux_mode, info->base + mux_reg);
  84. }
  85. dev_dbg(dev, "write mux: offset 0x%x val 0x%x\n", mux_reg,
  86. mux_mode);
  87. /*
  88. * Set select input
  89. *
  90. * If the select input value begins with 0xff, it's a quirky
  91. * select input and the value should be interpreted as below.
  92. * 31 23 15 7 0
  93. * | 0xff | shift | width | select |
  94. * It's used to work around the problem that the select
  95. * input for some pin is not implemented in the select
  96. * input register but in some general purpose register.
  97. * We encode the select input value, width and shift of
  98. * the bit field into input_val cell of pin function ID
  99. * in device tree, and then decode them here for setting
  100. * up the select input bits in general purpose register.
  101. */
  102. if (input_val >> 24 == 0xff) {
  103. u32 val = input_val;
  104. u8 select = val & 0xff;
  105. u8 width = (val >> 8) & 0xff;
  106. u8 shift = (val >> 16) & 0xff;
  107. u32 mask = ((1 << width) - 1) << shift;
  108. /*
  109. * The input_reg[i] here is actually some IOMUXC general
  110. * purpose register, not regular select input register.
  111. */
  112. val = readl(info->base + input_reg);
  113. val &= ~mask;
  114. val |= select << shift;
  115. writel(val, info->base + input_reg);
  116. } else if (input_reg) {
  117. /*
  118. * Regular select input register can never be at offset
  119. * 0, and we only print register value for regular case.
  120. */
  121. if (info->input_sel_base)
  122. writel(input_val, info->input_sel_base +
  123. input_reg);
  124. else
  125. writel(input_val, info->base + input_reg);
  126. dev_dbg(dev, "select_input: offset 0x%x val 0x%x\n",
  127. input_reg, input_val);
  128. }
  129. /* Set config */
  130. if (!(config_val & IMX_NO_PAD_CTL)) {
  131. if (info->flags & SHARE_MUX_CONF_REG) {
  132. clrsetbits_le32(info->base + conf_reg, 0xffff,
  133. config_val);
  134. } else {
  135. writel(config_val, info->base + conf_reg);
  136. }
  137. dev_dbg(dev, "write config: offset 0x%x val 0x%x\n",
  138. conf_reg, config_val);
  139. }
  140. }
  141. return 0;
  142. }
  143. const struct pinctrl_ops imx_pinctrl_ops = {
  144. .set_state = imx_pinctrl_set_state,
  145. };
  146. int imx_pinctrl_probe(struct udevice *dev,
  147. struct imx_pinctrl_soc_info *info)
  148. {
  149. struct imx_pinctrl_priv *priv = dev_get_priv(dev);
  150. int node = dev->of_offset, ret;
  151. struct fdtdec_phandle_args arg;
  152. fdt_addr_t addr;
  153. fdt_size_t size;
  154. if (!info) {
  155. dev_err(dev, "wrong pinctrl info\n");
  156. return -EINVAL;
  157. }
  158. priv->dev = dev;
  159. priv->info = info;
  160. addr = fdtdec_get_addr_size(gd->fdt_blob, dev->of_offset, "reg", &size);
  161. if (addr == FDT_ADDR_T_NONE)
  162. return -EINVAL;
  163. info->base = map_sysmem(addr, size);
  164. if (!info->base)
  165. return -ENOMEM;
  166. priv->info = info;
  167. /*
  168. * Refer to linux documentation for details:
  169. * Documentation/devicetree/bindings/pinctrl/fsl,imx7d-pinctrl.txt
  170. */
  171. if (fdtdec_get_bool(gd->fdt_blob, node, "fsl,input-sel")) {
  172. ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
  173. node, "fsl,input-sel",
  174. NULL, 0, 0, &arg);
  175. if (ret) {
  176. dev_err(dev, "iomuxc fsl,input-sel property not found\n");
  177. return -EINVAL;
  178. }
  179. addr = fdtdec_get_addr_size(gd->fdt_blob, arg.node, "reg",
  180. &size);
  181. if (addr == FDT_ADDR_T_NONE)
  182. return -EINVAL;
  183. info->input_sel_base = map_sysmem(addr, size);
  184. if (!info->input_sel_base)
  185. return -ENOMEM;
  186. }
  187. dev_dbg(dev, "initialized IMX pinctrl driver\n");
  188. return 0;
  189. }
  190. int imx_pinctrl_remove(struct udevice *dev)
  191. {
  192. struct imx_pinctrl_priv *priv = dev_get_priv(dev);
  193. struct imx_pinctrl_soc_info *info = priv->info;
  194. if (info->input_sel_base)
  195. unmap_sysmem(info->input_sel_base);
  196. if (info->base)
  197. unmap_sysmem(info->base);
  198. return 0;
  199. }