pinmux.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * DaVinci pinmux functions.
  3. *
  4. * Copyright (C) 2009 Nick Thompson, GE Fanuc Ltd, <nick.thompson@gefanuc.com>
  5. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  6. * Copyright (C) 2008 Lyrtech <www.lyrtech.com>
  7. * Copyright (C) 2004 Texas Instruments.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <asm/arch/hardware.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/davinci_misc.h>
  15. /*
  16. * Change the setting of a pin multiplexer field.
  17. *
  18. * Takes an array of pinmux settings similar to:
  19. *
  20. * struct pinmux_config uart_pins[] = {
  21. * { &davinci_syscfg_regs->pinmux[8], 2, 7 },
  22. * { &davinci_syscfg_regs->pinmux[9], 2, 0 }
  23. * };
  24. *
  25. * Stepping through the array, each pinmux[n] register has the given value
  26. * set in the pin mux field specified.
  27. *
  28. * The number of pins in the array must be passed (ARRAY_SIZE can provide
  29. * this value conveniently).
  30. *
  31. * Returns 0 if all field numbers and values are in the correct range,
  32. * else returns -1.
  33. */
  34. int davinci_configure_pin_mux(const struct pinmux_config *pins,
  35. const int n_pins)
  36. {
  37. int i;
  38. /* check for invalid pinmux values */
  39. for (i = 0; i < n_pins; i++) {
  40. if (pins[i].field >= PIN_MUX_NUM_FIELDS ||
  41. (pins[i].value & ~PIN_MUX_FIELD_MASK) != 0)
  42. return -1;
  43. }
  44. /* configure the pinmuxes */
  45. for (i = 0; i < n_pins; i++) {
  46. const int offset = pins[i].field * PIN_MUX_FIELD_SIZE;
  47. const unsigned int value = pins[i].value << offset;
  48. const unsigned int mask = PIN_MUX_FIELD_MASK << offset;
  49. const dv_reg *mux = pins[i].mux;
  50. writel(value | (readl(mux) & (~mask)), mux);
  51. }
  52. return 0;
  53. }
  54. /*
  55. * Configure multiple pinmux resources.
  56. *
  57. * Takes an pinmux_resource array of pinmux_config and pin counts:
  58. *
  59. * const struct pinmux_resource pinmuxes[] = {
  60. * PINMUX_ITEM(uart_pins),
  61. * PINMUX_ITEM(i2c_pins),
  62. * };
  63. *
  64. * The number of items in the array must be passed (ARRAY_SIZE can provide
  65. * this value conveniently).
  66. *
  67. * Each item entry is configured in the defined order. If configuration
  68. * of any item fails, -1 is returned and none of the following items are
  69. * configured. On success, 0 is returned.
  70. */
  71. int davinci_configure_pin_mux_items(const struct pinmux_resource *item,
  72. const int n_items)
  73. {
  74. int i;
  75. for (i = 0; i < n_items; i++) {
  76. if (davinci_configure_pin_mux(item[i].pins,
  77. item[i].n_pins) != 0)
  78. return -1;
  79. }
  80. return 0;
  81. }