pinmux.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <common.h>
  27. #include <asm/arch/hardware.h>
  28. #include <asm/io.h>
  29. #include <asm/arch/davinci_misc.h>
  30. /*
  31. * Change the setting of a pin multiplexer field.
  32. *
  33. * Takes an array of pinmux settings similar to:
  34. *
  35. * struct pinmux_config uart_pins[] = {
  36. * { &davinci_syscfg_regs->pinmux[8], 2, 7 },
  37. * { &davinci_syscfg_regs->pinmux[9], 2, 0 }
  38. * };
  39. *
  40. * Stepping through the array, each pinmux[n] register has the given value
  41. * set in the pin mux field specified.
  42. *
  43. * The number of pins in the array must be passed (ARRAY_SIZE can provide
  44. * this value conveniently).
  45. *
  46. * Returns 0 if all field numbers and values are in the correct range,
  47. * else returns -1.
  48. */
  49. int davinci_configure_pin_mux(const struct pinmux_config *pins,
  50. const int n_pins)
  51. {
  52. int i;
  53. /* check for invalid pinmux values */
  54. for (i = 0; i < n_pins; i++) {
  55. if (pins[i].field >= PIN_MUX_NUM_FIELDS ||
  56. (pins[i].value & ~PIN_MUX_FIELD_MASK) != 0)
  57. return -1;
  58. }
  59. /* configure the pinmuxes */
  60. for (i = 0; i < n_pins; i++) {
  61. const int offset = pins[i].field * PIN_MUX_FIELD_SIZE;
  62. const unsigned int value = pins[i].value << offset;
  63. const unsigned int mask = PIN_MUX_FIELD_MASK << offset;
  64. const dv_reg *mux = pins[i].mux;
  65. writel(value | (readl(mux) & (~mask)), mux);
  66. }
  67. return 0;
  68. }
  69. /*
  70. * Configure multiple pinmux resources.
  71. *
  72. * Takes an pinmux_resource array of pinmux_config and pin counts:
  73. *
  74. * const struct pinmux_resource pinmuxes[] = {
  75. * PINMUX_ITEM(uart_pins),
  76. * PINMUX_ITEM(i2c_pins),
  77. * };
  78. *
  79. * The number of items in the array must be passed (ARRAY_SIZE can provide
  80. * this value conveniently).
  81. *
  82. * Each item entry is configured in the defined order. If configuration
  83. * of any item fails, -1 is returned and none of the following items are
  84. * configured. On success, 0 is returned.
  85. */
  86. int davinci_configure_pin_mux_items(const struct pinmux_resource *item,
  87. const int n_items)
  88. {
  89. int i;
  90. for (i = 0; i < n_items; i++) {
  91. if (davinci_configure_pin_mux(item[i].pins,
  92. item[i].n_pins) != 0)
  93. return -1;
  94. }
  95. return 0;
  96. }