gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Miscellaneous functions for IDT EB434 board
  3. *
  4. * Copyright 2004 IDT Inc. (rischelp@idt.com)
  5. * Copyright 2006 Phil Sutter <n0-1@freewrt.org>
  6. * Copyright 2007 Florian Fainelli <florian@openwrt.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  16. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  19. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 675 Mass Ave, Cambridge, MA 02139, USA.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/gpio.h>
  34. #include <asm/mach-rc32434/rb.h>
  35. #include <asm/mach-rc32434/gpio.h>
  36. struct rb532_gpio_chip {
  37. struct gpio_chip chip;
  38. void __iomem *regbase;
  39. };
  40. struct mpmc_device dev3;
  41. static struct resource rb532_gpio_reg0_res[] = {
  42. {
  43. .name = "gpio_reg0",
  44. .start = REGBASE + GPIOBASE,
  45. .end = REGBASE + GPIOBASE + sizeof(struct rb532_gpio_reg) - 1,
  46. .flags = IORESOURCE_MEM,
  47. }
  48. };
  49. static struct resource rb532_dev3_ctl_res[] = {
  50. {
  51. .name = "dev3_ctl",
  52. .start = REGBASE + DEV3BASE,
  53. .end = REGBASE + DEV3BASE + sizeof(struct dev_reg) - 1,
  54. .flags = IORESOURCE_MEM,
  55. }
  56. };
  57. void set_434_reg(unsigned reg_offs, unsigned bit, unsigned len, unsigned val)
  58. {
  59. unsigned long flags;
  60. unsigned data;
  61. unsigned i = 0;
  62. spin_lock_irqsave(&dev3.lock, flags);
  63. data = readl(IDT434_REG_BASE + reg_offs);
  64. for (i = 0; i != len; ++i) {
  65. if (val & (1 << i))
  66. data |= (1 << (i + bit));
  67. else
  68. data &= ~(1 << (i + bit));
  69. }
  70. writel(data, (IDT434_REG_BASE + reg_offs));
  71. spin_unlock_irqrestore(&dev3.lock, flags);
  72. }
  73. EXPORT_SYMBOL(set_434_reg);
  74. unsigned get_434_reg(unsigned reg_offs)
  75. {
  76. return readl(IDT434_REG_BASE + reg_offs);
  77. }
  78. EXPORT_SYMBOL(get_434_reg);
  79. void set_latch_u5(unsigned char or_mask, unsigned char nand_mask)
  80. {
  81. unsigned long flags;
  82. spin_lock_irqsave(&dev3.lock, flags);
  83. dev3.state = (dev3.state | or_mask) & ~nand_mask;
  84. writel(dev3.state, &dev3.base);
  85. spin_unlock_irqrestore(&dev3.lock, flags);
  86. }
  87. EXPORT_SYMBOL(set_latch_u5);
  88. unsigned char get_latch_u5(void)
  89. {
  90. return dev3.state;
  91. }
  92. EXPORT_SYMBOL(get_latch_u5);
  93. /* rb532_set_bit - sanely set a bit
  94. *
  95. * bitval: new value for the bit
  96. * offset: bit index in the 4 byte address range
  97. * ioaddr: 4 byte aligned address being altered
  98. */
  99. static inline void rb532_set_bit(unsigned bitval,
  100. unsigned offset, void __iomem *ioaddr)
  101. {
  102. unsigned long flags;
  103. u32 val;
  104. bitval = !!bitval; /* map parameter to {0,1} */
  105. local_irq_save(flags);
  106. val = readl(ioaddr);
  107. val &= ~( ~bitval << offset ); /* unset bit if bitval == 0 */
  108. val |= ( bitval << offset ); /* set bit if bitval == 1 */
  109. writel(val, ioaddr);
  110. local_irq_restore(flags);
  111. }
  112. /* rb532_get_bit - read a bit
  113. *
  114. * returns the boolean state of the bit, which may be > 1
  115. */
  116. static inline int rb532_get_bit(unsigned offset, void __iomem *ioaddr)
  117. {
  118. return (readl(ioaddr) & (1 << offset));
  119. }
  120. /*
  121. * Return GPIO level */
  122. static int rb532_gpio_get(struct gpio_chip *chip, unsigned offset)
  123. {
  124. struct rb532_gpio_chip *gpch;
  125. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  126. return rb532_get_bit(offset, gpch->regbase + GPIOD);
  127. }
  128. /*
  129. * Set output GPIO level
  130. */
  131. static void rb532_gpio_set(struct gpio_chip *chip,
  132. unsigned offset, int value)
  133. {
  134. struct rb532_gpio_chip *gpch;
  135. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  136. rb532_set_bit(value, offset, gpch->regbase + GPIOD);
  137. }
  138. /*
  139. * Set GPIO direction to input
  140. */
  141. static int rb532_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  142. {
  143. struct rb532_gpio_chip *gpch;
  144. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  145. if (rb532_get_bit(offset, gpch->regbase + GPIOFUNC))
  146. return 1; /* alternate function, GPIOCFG is ignored */
  147. rb532_set_bit(0, offset, gpch->regbase + GPIOCFG);
  148. return 0;
  149. }
  150. /*
  151. * Set GPIO direction to output
  152. */
  153. static int rb532_gpio_direction_output(struct gpio_chip *chip,
  154. unsigned offset, int value)
  155. {
  156. struct rb532_gpio_chip *gpch;
  157. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  158. if (rb532_get_bit(offset, gpch->regbase + GPIOFUNC))
  159. return 1; /* alternate function, GPIOCFG is ignored */
  160. /* set the initial output value */
  161. rb532_set_bit(value, offset, gpch->regbase + GPIOD);
  162. rb532_set_bit(1, offset, gpch->regbase + GPIOCFG);
  163. return 0;
  164. }
  165. static struct rb532_gpio_chip rb532_gpio_chip[] = {
  166. [0] = {
  167. .chip = {
  168. .label = "gpio0",
  169. .direction_input = rb532_gpio_direction_input,
  170. .direction_output = rb532_gpio_direction_output,
  171. .get = rb532_gpio_get,
  172. .set = rb532_gpio_set,
  173. .base = 0,
  174. .ngpio = 32,
  175. },
  176. },
  177. };
  178. /*
  179. * Set GPIO interrupt level
  180. */
  181. void rb532_gpio_set_ilevel(int bit, unsigned gpio)
  182. {
  183. rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOILEVEL);
  184. }
  185. EXPORT_SYMBOL(rb532_gpio_set_ilevel);
  186. /*
  187. * Set GPIO interrupt status
  188. */
  189. void rb532_gpio_set_istat(int bit, unsigned gpio)
  190. {
  191. rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOISTAT);
  192. }
  193. EXPORT_SYMBOL(rb532_gpio_set_istat);
  194. /*
  195. * Configure GPIO alternate function
  196. */
  197. static void rb532_gpio_set_func(int bit, unsigned gpio)
  198. {
  199. rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOFUNC);
  200. }
  201. int __init rb532_gpio_init(void)
  202. {
  203. struct resource *r;
  204. r = rb532_gpio_reg0_res;
  205. rb532_gpio_chip->regbase = ioremap_nocache(r->start, r->end - r->start);
  206. if (!rb532_gpio_chip->regbase) {
  207. printk(KERN_ERR "rb532: cannot remap GPIO register 0\n");
  208. return -ENXIO;
  209. }
  210. /* Register our GPIO chip */
  211. gpiochip_add(&rb532_gpio_chip->chip);
  212. r = rb532_dev3_ctl_res;
  213. dev3.base = ioremap_nocache(r->start, r->end - r->start);
  214. if (!dev3.base) {
  215. printk(KERN_ERR "rb532: cannot remap device controller 3\n");
  216. return -ENXIO;
  217. }
  218. /* configure CF_GPIO_NUM as CFRDY IRQ source */
  219. rb532_gpio_set_func(0, CF_GPIO_NUM);
  220. rb532_gpio_direction_input(&rb532_gpio_chip->chip, CF_GPIO_NUM);
  221. rb532_gpio_set_ilevel(1, CF_GPIO_NUM);
  222. rb532_gpio_set_istat(0, CF_GPIO_NUM);
  223. return 0;
  224. }
  225. arch_initcall(rb532_gpio_init);