pinctrl-single.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <dm/pinctrl.h>
  8. #include <linux/libfdt.h>
  9. #include <asm/io.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. struct single_pdata {
  12. fdt_addr_t base; /* first configuration register */
  13. int offset; /* index of last configuration register */
  14. u32 mask; /* configuration-value mask bits */
  15. int width; /* configuration register bit width */
  16. };
  17. struct single_fdt_pin_cfg {
  18. fdt32_t reg; /* configuration register offset */
  19. fdt32_t val; /* configuration register value */
  20. };
  21. /**
  22. * single_configure_pins() - Configure pins based on FDT data
  23. *
  24. * @dev: Pointer to single pin configuration device which is the parent of
  25. * the pins node holding the pin configuration data.
  26. * @pins: Pointer to the first element of an array of register/value pairs
  27. * of type 'struct single_fdt_pin_cfg'. Each such pair describes the
  28. * the pin to be configured and the value to be used for configuration.
  29. * This pointer points to a 'pinctrl-single,pins' property in the
  30. * device-tree.
  31. * @size: Size of the 'pins' array in bytes.
  32. * The number of register/value pairs in the 'pins' array therefore
  33. * equals to 'size / sizeof(struct single_fdt_pin_cfg)'.
  34. */
  35. static int single_configure_pins(struct udevice *dev,
  36. const struct single_fdt_pin_cfg *pins,
  37. int size)
  38. {
  39. struct single_pdata *pdata = dev->platdata;
  40. int count = size / sizeof(struct single_fdt_pin_cfg);
  41. int n, reg;
  42. u32 val;
  43. for (n = 0; n < count; n++, pins++) {
  44. reg = fdt32_to_cpu(pins->reg);
  45. if ((reg < 0) || (reg > pdata->offset)) {
  46. dev_dbg(dev, " invalid register offset 0x%08x\n", reg);
  47. continue;
  48. }
  49. reg += pdata->base;
  50. val = fdt32_to_cpu(pins->val) & pdata->mask;
  51. switch (pdata->width) {
  52. case 16:
  53. writew((readw(reg) & ~pdata->mask) | val, reg);
  54. break;
  55. case 32:
  56. writel((readl(reg) & ~pdata->mask) | val, reg);
  57. break;
  58. default:
  59. dev_warn(dev, "unsupported register width %i\n",
  60. pdata->width);
  61. continue;
  62. }
  63. dev_dbg(dev, " reg/val 0x%08x/0x%08x\n",reg, val);
  64. }
  65. return 0;
  66. }
  67. static int single_set_state(struct udevice *dev,
  68. struct udevice *config)
  69. {
  70. const void *fdt = gd->fdt_blob;
  71. const struct single_fdt_pin_cfg *prop;
  72. int len;
  73. prop = fdt_getprop(fdt, dev_of_offset(config), "pinctrl-single,pins",
  74. &len);
  75. if (prop) {
  76. dev_dbg(dev, "configuring pins for %s\n", config->name);
  77. if (len % sizeof(struct single_fdt_pin_cfg)) {
  78. dev_dbg(dev, " invalid pin configuration in fdt\n");
  79. return -FDT_ERR_BADSTRUCTURE;
  80. }
  81. single_configure_pins(dev, prop, len);
  82. len = 0;
  83. }
  84. return len;
  85. }
  86. static int single_ofdata_to_platdata(struct udevice *dev)
  87. {
  88. fdt_addr_t addr;
  89. u32 of_reg[2];
  90. int res;
  91. struct single_pdata *pdata = dev->platdata;
  92. pdata->width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  93. "pinctrl-single,register-width", 0);
  94. res = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
  95. "reg", of_reg, 2);
  96. if (res)
  97. return res;
  98. pdata->offset = of_reg[1] - pdata->width / 8;
  99. addr = devfdt_get_addr(dev);
  100. if (addr == FDT_ADDR_T_NONE) {
  101. dev_dbg(dev, "no valid base register address\n");
  102. return -EINVAL;
  103. }
  104. pdata->base = addr;
  105. pdata->mask = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  106. "pinctrl-single,function-mask",
  107. 0xffffffff);
  108. return 0;
  109. }
  110. const struct pinctrl_ops single_pinctrl_ops = {
  111. .set_state = single_set_state,
  112. };
  113. static const struct udevice_id single_pinctrl_match[] = {
  114. { .compatible = "pinctrl-single" },
  115. { /* sentinel */ }
  116. };
  117. U_BOOT_DRIVER(single_pinctrl) = {
  118. .name = "single-pinctrl",
  119. .id = UCLASS_PINCTRL,
  120. .of_match = single_pinctrl_match,
  121. .ops = &single_pinctrl_ops,
  122. .flags = DM_FLAG_PRE_RELOC,
  123. .platdata_auto_alloc_size = sizeof(struct single_pdata),
  124. .ofdata_to_platdata = single_ofdata_to_platdata,
  125. };