iomux-imx31.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/gpio.h>
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include "hardware.h"
  26. #include "iomux-mx3.h"
  27. /*
  28. * IOMUX register (base) addresses
  29. */
  30. #define IOMUX_BASE MX31_IO_ADDRESS(MX31_IOMUXC_BASE_ADDR)
  31. #define IOMUXINT_OBS1 (IOMUX_BASE + 0x000)
  32. #define IOMUXINT_OBS2 (IOMUX_BASE + 0x004)
  33. #define IOMUXGPR (IOMUX_BASE + 0x008)
  34. #define IOMUXSW_MUX_CTL (IOMUX_BASE + 0x00C)
  35. #define IOMUXSW_PAD_CTL (IOMUX_BASE + 0x154)
  36. static DEFINE_SPINLOCK(gpio_mux_lock);
  37. #define IOMUX_REG_MASK (IOMUX_PADNUM_MASK & ~0x3)
  38. static unsigned long mxc_pin_alloc_map[NB_PORTS * 32 / BITS_PER_LONG];
  39. /*
  40. * set the mode for a IOMUX pin.
  41. */
  42. int mxc_iomux_mode(unsigned int pin_mode)
  43. {
  44. u32 field, l, mode, ret = 0;
  45. void __iomem *reg;
  46. reg = IOMUXSW_MUX_CTL + (pin_mode & IOMUX_REG_MASK);
  47. field = pin_mode & 0x3;
  48. mode = (pin_mode & IOMUX_MODE_MASK) >> IOMUX_MODE_SHIFT;
  49. spin_lock(&gpio_mux_lock);
  50. l = __raw_readl(reg);
  51. l &= ~(0xff << (field * 8));
  52. l |= mode << (field * 8);
  53. __raw_writel(l, reg);
  54. spin_unlock(&gpio_mux_lock);
  55. return ret;
  56. }
  57. /*
  58. * This function configures the pad value for a IOMUX pin.
  59. */
  60. void mxc_iomux_set_pad(enum iomux_pins pin, u32 config)
  61. {
  62. u32 field, l;
  63. void __iomem *reg;
  64. pin &= IOMUX_PADNUM_MASK;
  65. reg = IOMUXSW_PAD_CTL + (pin + 2) / 3 * 4;
  66. field = (pin + 2) % 3;
  67. pr_debug("%s: reg offset = 0x%x, field = %d\n",
  68. __func__, (pin + 2) / 3, field);
  69. spin_lock(&gpio_mux_lock);
  70. l = __raw_readl(reg);
  71. l &= ~(0x1ff << (field * 10));
  72. l |= config << (field * 10);
  73. __raw_writel(l, reg);
  74. spin_unlock(&gpio_mux_lock);
  75. }
  76. /*
  77. * allocs a single pin:
  78. * - reserves the pin so that it is not claimed by another driver
  79. * - setups the iomux according to the configuration
  80. */
  81. int mxc_iomux_alloc_pin(unsigned int pin, const char *label)
  82. {
  83. unsigned pad = pin & IOMUX_PADNUM_MASK;
  84. if (pad >= (PIN_MAX + 1)) {
  85. printk(KERN_ERR "mxc_iomux: Attempt to request nonexistant pin %u for \"%s\"\n",
  86. pad, label ? label : "?");
  87. return -EINVAL;
  88. }
  89. if (test_and_set_bit(pad, mxc_pin_alloc_map)) {
  90. printk(KERN_ERR "mxc_iomux: pin %u already used. Allocation for \"%s\" failed\n",
  91. pad, label ? label : "?");
  92. return -EBUSY;
  93. }
  94. mxc_iomux_mode(pin);
  95. return 0;
  96. }
  97. int mxc_iomux_setup_multiple_pins(const unsigned int *pin_list, unsigned count,
  98. const char *label)
  99. {
  100. const unsigned int *p = pin_list;
  101. int i;
  102. int ret = -EINVAL;
  103. for (i = 0; i < count; i++) {
  104. ret = mxc_iomux_alloc_pin(*p, label);
  105. if (ret)
  106. goto setup_error;
  107. p++;
  108. }
  109. return 0;
  110. setup_error:
  111. mxc_iomux_release_multiple_pins(pin_list, i);
  112. return ret;
  113. }
  114. void mxc_iomux_release_pin(unsigned int pin)
  115. {
  116. unsigned pad = pin & IOMUX_PADNUM_MASK;
  117. if (pad < (PIN_MAX + 1))
  118. clear_bit(pad, mxc_pin_alloc_map);
  119. }
  120. void mxc_iomux_release_multiple_pins(const unsigned int *pin_list, int count)
  121. {
  122. const unsigned int *p = pin_list;
  123. int i;
  124. for (i = 0; i < count; i++) {
  125. mxc_iomux_release_pin(*p);
  126. p++;
  127. }
  128. }
  129. /*
  130. * This function enables/disables the general purpose function for a particular
  131. * signal.
  132. */
  133. void mxc_iomux_set_gpr(enum iomux_gp_func gp, bool en)
  134. {
  135. u32 l;
  136. spin_lock(&gpio_mux_lock);
  137. l = __raw_readl(IOMUXGPR);
  138. if (en)
  139. l |= gp;
  140. else
  141. l &= ~gp;
  142. __raw_writel(l, IOMUXGPR);
  143. spin_unlock(&gpio_mux_lock);
  144. }