pinctrl-mvebu.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Marvell International Ltd.
  4. * https://spdx.org/licenses
  5. */
  6. #include <common.h>
  7. #include <config.h>
  8. #include <fdtdec.h>
  9. #include <errno.h>
  10. #include <dm.h>
  11. #include <dm/pinctrl.h>
  12. #include <dm/root.h>
  13. #include <asm/system.h>
  14. #include <asm/io.h>
  15. #include <asm/arch-armada8k/soc-info.h>
  16. #include "pinctrl-mvebu.h"
  17. #define AP_EMMC_PHY_CTRL_REG 0x100
  18. #define CP_EMMC_PHY_CTRL_REG 0x424
  19. #define EMMC_PHY_CTRL_SDPHY_EN BIT(0)
  20. #define AP806_EMMC_CLK_PIN_ID 0
  21. #define AP806_EMMC_CLK_FUNC 0x1
  22. #define CP110_EMMC_CLK_PIN_ID 56
  23. #define CP110_EMMC_CLK_FUNC 0xe
  24. DECLARE_GLOBAL_DATA_PTR;
  25. /* mvebu_pinctl_emmc_set_mux: configure sd/mmc PHY mux
  26. * To enable SDIO/eMMC in Armada-APN806/CP110, need to configure PHY mux.
  27. * eMMC/SD PHY register responsible for muxing between MPPs and SD/eMMC
  28. * controller:
  29. * - Bit0 enabled SDIO/eMMC PHY is used as a MPP muxltiplexer,
  30. * - Bit0 disabled SDIO/eMMC PHY is connected to SDIO/eMMC controller
  31. * If pin function is set to eMMC/SD, then configure the eMMC/SD PHY
  32. * muxltiplexer register to be on SDIO/eMMC controller
  33. */
  34. void mvebu_pinctl_emmc_set_mux(struct udevice *dev, u32 pin, u32 func)
  35. {
  36. const void *blob = gd->fdt_blob;
  37. int node = dev_of_offset(dev);
  38. struct mvebu_pinctrl_priv *priv = dev_get_priv(dev);
  39. if (!fdt_node_check_compatible(blob, node, "marvell,ap806-pinctrl")) {
  40. if ((pin == AP806_EMMC_CLK_PIN_ID) &&
  41. (func == AP806_EMMC_CLK_FUNC)) {
  42. clrbits_le32(priv->base_reg + AP_EMMC_PHY_CTRL_REG,
  43. EMMC_PHY_CTRL_SDPHY_EN);
  44. }
  45. } else if (!fdt_node_check_compatible(blob, node,
  46. "marvell,armada-8k-cpm-pinctrl")) {
  47. if ((pin == CP110_EMMC_CLK_PIN_ID) &&
  48. (func == CP110_EMMC_CLK_FUNC)) {
  49. clrbits_le32(priv->base_reg + CP_EMMC_PHY_CTRL_REG,
  50. EMMC_PHY_CTRL_SDPHY_EN);
  51. }
  52. }
  53. }
  54. /*
  55. * mvebu_pinctrl_set_state: configure pin functions.
  56. * @dev: the pinctrl device to be configured.
  57. * @config: the state to be configured.
  58. * @return: 0 in success
  59. */
  60. int mvebu_pinctrl_set_state(struct udevice *dev, struct udevice *config)
  61. {
  62. const void *blob = gd->fdt_blob;
  63. int node = dev_of_offset(config);
  64. struct mvebu_pinctrl_priv *priv;
  65. u32 pin_arr[MVEBU_MAX_PINS_PER_BANK];
  66. u32 function;
  67. int i, pin_count;
  68. priv = dev_get_priv(dev);
  69. pin_count = fdtdec_get_int_array_count(blob, node,
  70. "marvell,pins",
  71. pin_arr,
  72. MVEBU_MAX_PINS_PER_BANK);
  73. if (pin_count <= 0) {
  74. debug("Failed reading pins array for pinconfig %s (%d)\n",
  75. config->name, pin_count);
  76. return -EINVAL;
  77. }
  78. function = fdtdec_get_int(blob, node, "marvell,function", 0xff);
  79. /*
  80. * Check if setup of PHY mux is needed for this pins group.
  81. * Only the first pin id in array is tested, all the rest use the same
  82. * pin function.
  83. */
  84. mvebu_pinctl_emmc_set_mux(dev, pin_arr[0], function);
  85. for (i = 0; i < pin_count; i++) {
  86. int reg_offset;
  87. int field_offset;
  88. int pin = pin_arr[i];
  89. if (function > priv->max_func) {
  90. debug("Illegal function %d for pinconfig %s\n",
  91. function, config->name);
  92. return -EINVAL;
  93. }
  94. /* Calculate register address and bit in register */
  95. reg_offset = priv->reg_direction * 4 *
  96. (pin >> (PIN_REG_SHIFT));
  97. field_offset = (BITS_PER_PIN) * (pin & PIN_FIELD_MASK);
  98. clrsetbits_le32(priv->base_reg + reg_offset,
  99. PIN_FUNC_MASK << field_offset,
  100. (function & PIN_FUNC_MASK) << field_offset);
  101. }
  102. return 0;
  103. }
  104. /*
  105. * mvebu_pinctrl_set_state_all: configure the entire bank pin functions.
  106. * @dev: the pinctrl device to be configured.
  107. * @config: the state to be configured.
  108. * @return: 0 in success
  109. */
  110. static int mvebu_pinctrl_set_state_all(struct udevice *dev,
  111. struct udevice *config)
  112. {
  113. const void *blob = gd->fdt_blob;
  114. int node = dev_of_offset(config);
  115. struct mvebu_pinctrl_priv *priv;
  116. u32 func_arr[MVEBU_MAX_PINS_PER_BANK];
  117. int pin, err;
  118. priv = dev_get_priv(dev);
  119. err = fdtdec_get_int_array(blob, node, "pin-func",
  120. func_arr, priv->pin_cnt);
  121. if (err) {
  122. debug("Failed reading pin functions for bank %s\n",
  123. priv->bank_name);
  124. return -EINVAL;
  125. }
  126. /* Check if setup of PHY mux is needed for this pins group. */
  127. if (priv->pin_cnt < CP110_EMMC_CLK_PIN_ID)
  128. mvebu_pinctl_emmc_set_mux(dev, AP806_EMMC_CLK_PIN_ID,
  129. func_arr[AP806_EMMC_CLK_PIN_ID]);
  130. else
  131. mvebu_pinctl_emmc_set_mux(dev, CP110_EMMC_CLK_PIN_ID,
  132. func_arr[CP110_EMMC_CLK_PIN_ID]);
  133. for (pin = 0; pin < priv->pin_cnt; pin++) {
  134. int reg_offset;
  135. int field_offset;
  136. u32 func = func_arr[pin];
  137. /* Bypass pins with function 0xFF */
  138. if (func == 0xff) {
  139. debug("Warning: pin %d value is not modified ", pin);
  140. debug("(kept as default)\n");
  141. continue;
  142. } else if (func > priv->max_func) {
  143. debug("Illegal function %d for pin %d\n", func, pin);
  144. return -EINVAL;
  145. }
  146. /* Calculate register address and bit in register */
  147. reg_offset = priv->reg_direction * 4 *
  148. (pin >> (PIN_REG_SHIFT));
  149. field_offset = (BITS_PER_PIN) * (pin & PIN_FIELD_MASK);
  150. clrsetbits_le32(priv->base_reg + reg_offset,
  151. PIN_FUNC_MASK << field_offset,
  152. (func & PIN_FUNC_MASK) << field_offset);
  153. }
  154. return 0;
  155. }
  156. int mvebu_pinctl_probe(struct udevice *dev)
  157. {
  158. const void *blob = gd->fdt_blob;
  159. int node = dev_of_offset(dev);
  160. struct mvebu_pinctrl_priv *priv;
  161. priv = dev_get_priv(dev);
  162. if (!priv) {
  163. debug("%s: Failed to get private\n", __func__);
  164. return -EINVAL;
  165. }
  166. priv->base_reg = devfdt_get_addr_ptr(dev);
  167. if (priv->base_reg == (void *)FDT_ADDR_T_NONE) {
  168. debug("%s: Failed to get base address\n", __func__);
  169. return -EINVAL;
  170. }
  171. priv->pin_cnt = fdtdec_get_int(blob, node, "pin-count",
  172. MVEBU_MAX_PINS_PER_BANK);
  173. priv->max_func = fdtdec_get_int(blob, node, "max-func",
  174. MVEBU_MAX_FUNC);
  175. priv->bank_name = fdt_getprop(blob, node, "bank-name", NULL);
  176. priv->reg_direction = 1;
  177. if (fdtdec_get_bool(blob, node, "reverse-reg"))
  178. priv->reg_direction = -1;
  179. return mvebu_pinctrl_set_state_all(dev, dev);
  180. }
  181. static struct pinctrl_ops mvebu_pinctrl_ops = {
  182. .set_state = mvebu_pinctrl_set_state
  183. };
  184. static const struct udevice_id mvebu_pinctrl_ids[] = {
  185. { .compatible = "marvell,mvebu-pinctrl" },
  186. { .compatible = "marvell,ap806-pinctrl" },
  187. { .compatible = "marvell,armada-7k-pinctrl" },
  188. { .compatible = "marvell,armada-8k-cpm-pinctrl" },
  189. { .compatible = "marvell,armada-8k-cps-pinctrl" },
  190. { }
  191. };
  192. U_BOOT_DRIVER(pinctrl_mvebu) = {
  193. .name = "mvebu_pinctrl",
  194. .id = UCLASS_PINCTRL,
  195. .of_match = mvebu_pinctrl_ids,
  196. .priv_auto_alloc_size = sizeof(struct mvebu_pinctrl_priv),
  197. .ops = &mvebu_pinctrl_ops,
  198. .probe = mvebu_pinctl_probe
  199. };