pinmux.c 2.3 KB

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