iomux.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright (C) 2008 by Sascha Hauer <kernel@pengutronix.de>
  4. * Copyright (C) 2009 by Valentin Longchamp <valentin.longchamp@epfl.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <mach/hardware.h>
  25. #include <mach/gpio.h>
  26. #include <mach/iomux-mxc91231.h>
  27. /*
  28. * IOMUX register (base) addresses
  29. */
  30. #define IOMUX_AP_BASE MXC91231_IO_ADDRESS(MXC91231_IOMUX_AP_BASE_ADDR)
  31. #define IOMUX_COM_BASE MXC91231_IO_ADDRESS(MXC91231_IOMUX_COM_BASE_ADDR)
  32. #define IOMUXSW_AP_MUX_CTL (IOMUX_AP_BASE + 0x000)
  33. #define IOMUXSW_SP_MUX_CTL (IOMUX_COM_BASE + 0x000)
  34. #define IOMUXSW_PAD_CTL (IOMUX_COM_BASE + 0x200)
  35. #define IOMUXINT_OBS1 (IOMUX_AP_BASE + 0x600)
  36. #define IOMUXINT_OBS2 (IOMUX_AP_BASE + 0x004)
  37. static DEFINE_SPINLOCK(gpio_mux_lock);
  38. #define NB_PORTS ((PIN_MAX + 32) / 32)
  39. #define PIN_GLOBAL_NUM(pin) \
  40. (((pin & MUX_SIDE_MASK) >> MUX_SIDE_SHIFT)*PIN_AP_MAX + \
  41. ((pin & MUX_REG_MASK) >> MUX_REG_SHIFT)*4 + \
  42. ((pin & MUX_FIELD_MASK) >> MUX_FIELD_SHIFT))
  43. unsigned long mxc_pin_alloc_map[NB_PORTS * 32 / BITS_PER_LONG];
  44. /*
  45. * set the mode for a IOMUX pin.
  46. */
  47. int mxc_iomux_mode(const unsigned int pin_mode)
  48. {
  49. u32 side, field, l, mode, ret = 0;
  50. void __iomem *reg;
  51. side = (pin_mode & MUX_SIDE_MASK) >> MUX_SIDE_SHIFT;
  52. switch (side) {
  53. case MUX_SIDE_AP:
  54. reg = IOMUXSW_AP_MUX_CTL;
  55. break;
  56. case MUX_SIDE_SP:
  57. reg = IOMUXSW_SP_MUX_CTL;
  58. break;
  59. default:
  60. return -EINVAL;
  61. }
  62. reg += ((pin_mode & MUX_REG_MASK) >> MUX_REG_SHIFT) * 4;
  63. field = (pin_mode & MUX_FIELD_MASK) >> MUX_FIELD_SHIFT;
  64. mode = (pin_mode & MUX_MODE_MASK) >> MUX_MODE_SHIFT;
  65. spin_lock(&gpio_mux_lock);
  66. l = __raw_readl(reg);
  67. l &= ~(0xff << (field * 8));
  68. l |= mode << (field * 8);
  69. __raw_writel(l, reg);
  70. spin_unlock(&gpio_mux_lock);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL(mxc_iomux_mode);
  74. /*
  75. * This function configures the pad value for a IOMUX pin.
  76. */
  77. void mxc_iomux_set_pad(enum iomux_pins pin, u32 config)
  78. {
  79. u32 padgrp, field, l;
  80. void __iomem *reg;
  81. padgrp = (pin & MUX_PADGRP_MASK) >> MUX_PADGRP_SHIFT;
  82. reg = IOMUXSW_PAD_CTL + (pin + 2) / 3 * 4;
  83. field = (pin + 2) % 3;
  84. pr_debug("%s: reg offset = 0x%x, field = %d\n",
  85. __func__, (pin + 2) / 3, field);
  86. spin_lock(&gpio_mux_lock);
  87. l = __raw_readl(reg);
  88. l &= ~(0x1ff << (field * 10));
  89. l |= config << (field * 10);
  90. __raw_writel(l, reg);
  91. spin_unlock(&gpio_mux_lock);
  92. }
  93. EXPORT_SYMBOL(mxc_iomux_set_pad);
  94. /*
  95. * allocs a single pin:
  96. * - reserves the pin so that it is not claimed by another driver
  97. * - setups the iomux according to the configuration
  98. */
  99. int mxc_iomux_alloc_pin(const unsigned int pin_mode, const char *label)
  100. {
  101. unsigned pad = PIN_GLOBAL_NUM(pin_mode);
  102. if (pad >= (PIN_MAX + 1)) {
  103. printk(KERN_ERR "mxc_iomux: Attempt to request nonexistant pin %u for \"%s\"\n",
  104. pad, label ? label : "?");
  105. return -EINVAL;
  106. }
  107. if (test_and_set_bit(pad, mxc_pin_alloc_map)) {
  108. printk(KERN_ERR "mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n",
  109. pad, label ? label : "?");
  110. return -EBUSY;
  111. }
  112. mxc_iomux_mode(pin_mode);
  113. return 0;
  114. }
  115. EXPORT_SYMBOL(mxc_iomux_alloc_pin);
  116. int mxc_iomux_setup_multiple_pins(unsigned int *pin_list, unsigned count,
  117. const char *label)
  118. {
  119. unsigned int *p = pin_list;
  120. int i;
  121. int ret = -EINVAL;
  122. for (i = 0; i < count; i++) {
  123. ret = mxc_iomux_alloc_pin(*p, label);
  124. if (ret)
  125. goto setup_error;
  126. p++;
  127. }
  128. return 0;
  129. setup_error:
  130. mxc_iomux_release_multiple_pins(pin_list, i);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL(mxc_iomux_setup_multiple_pins);
  134. void mxc_iomux_release_pin(const unsigned int pin_mode)
  135. {
  136. unsigned pad = PIN_GLOBAL_NUM(pin_mode);
  137. if (pad < (PIN_MAX + 1))
  138. clear_bit(pad, mxc_pin_alloc_map);
  139. }
  140. EXPORT_SYMBOL(mxc_iomux_release_pin);
  141. void mxc_iomux_release_multiple_pins(unsigned int *pin_list, int count)
  142. {
  143. unsigned int *p = pin_list;
  144. int i;
  145. for (i = 0; i < count; i++) {
  146. mxc_iomux_release_pin(*p);
  147. p++;
  148. }
  149. }
  150. EXPORT_SYMBOL(mxc_iomux_release_multiple_pins);