gpio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. void (*set_int_level)(struct gpio_chip *chip, unsigned offset, int value);
  40. int (*get_int_level)(struct gpio_chip *chip, unsigned offset);
  41. void (*set_int_status)(struct gpio_chip *chip, unsigned offset, int value);
  42. int (*get_int_status)(struct gpio_chip *chip, unsigned offset);
  43. };
  44. struct mpmc_device dev3;
  45. static struct resource rb532_gpio_reg0_res[] = {
  46. {
  47. .name = "gpio_reg0",
  48. .start = REGBASE + GPIOBASE,
  49. .end = REGBASE + GPIOBASE + sizeof(struct rb532_gpio_reg) - 1,
  50. .flags = IORESOURCE_MEM,
  51. }
  52. };
  53. static struct resource rb532_dev3_ctl_res[] = {
  54. {
  55. .name = "dev3_ctl",
  56. .start = REGBASE + DEV3BASE,
  57. .end = REGBASE + DEV3BASE + sizeof(struct dev_reg) - 1,
  58. .flags = IORESOURCE_MEM,
  59. }
  60. };
  61. void set_434_reg(unsigned reg_offs, unsigned bit, unsigned len, unsigned val)
  62. {
  63. unsigned long flags;
  64. unsigned data;
  65. unsigned i = 0;
  66. spin_lock_irqsave(&dev3.lock, flags);
  67. data = readl(IDT434_REG_BASE + reg_offs);
  68. for (i = 0; i != len; ++i) {
  69. if (val & (1 << i))
  70. data |= (1 << (i + bit));
  71. else
  72. data &= ~(1 << (i + bit));
  73. }
  74. writel(data, (IDT434_REG_BASE + reg_offs));
  75. spin_unlock_irqrestore(&dev3.lock, flags);
  76. }
  77. EXPORT_SYMBOL(set_434_reg);
  78. unsigned get_434_reg(unsigned reg_offs)
  79. {
  80. return readl(IDT434_REG_BASE + reg_offs);
  81. }
  82. EXPORT_SYMBOL(get_434_reg);
  83. void set_latch_u5(unsigned char or_mask, unsigned char nand_mask)
  84. {
  85. unsigned long flags;
  86. spin_lock_irqsave(&dev3.lock, flags);
  87. dev3.state = (dev3.state | or_mask) & ~nand_mask;
  88. writel(dev3.state, &dev3.base);
  89. spin_unlock_irqrestore(&dev3.lock, flags);
  90. }
  91. EXPORT_SYMBOL(set_latch_u5);
  92. unsigned char get_latch_u5(void)
  93. {
  94. return dev3.state;
  95. }
  96. EXPORT_SYMBOL(get_latch_u5);
  97. /*
  98. * Return GPIO level */
  99. static int rb532_gpio_get(struct gpio_chip *chip, unsigned offset)
  100. {
  101. u32 mask = 1 << offset;
  102. struct rb532_gpio_chip *gpch;
  103. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  104. return readl(gpch->regbase + GPIOD) & mask;
  105. }
  106. /*
  107. * Set output GPIO level
  108. */
  109. static void rb532_gpio_set(struct gpio_chip *chip,
  110. unsigned offset, int value)
  111. {
  112. unsigned long flags;
  113. u32 mask = 1 << offset;
  114. u32 tmp;
  115. struct rb532_gpio_chip *gpch;
  116. void __iomem *gpvr;
  117. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  118. gpvr = gpch->regbase + GPIOD;
  119. local_irq_save(flags);
  120. tmp = readl(gpvr);
  121. if (value)
  122. tmp |= mask;
  123. else
  124. tmp &= ~mask;
  125. writel(tmp, gpvr);
  126. local_irq_restore(flags);
  127. }
  128. /*
  129. * Set GPIO direction to input
  130. */
  131. static int rb532_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  132. {
  133. unsigned long flags;
  134. u32 mask = 1 << offset;
  135. u32 value;
  136. struct rb532_gpio_chip *gpch;
  137. void __iomem *gpdr;
  138. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  139. gpdr = gpch->regbase + GPIOCFG;
  140. local_irq_save(flags);
  141. value = readl(gpdr);
  142. value &= ~mask;
  143. writel(value, gpdr);
  144. local_irq_restore(flags);
  145. return 0;
  146. }
  147. /*
  148. * Set GPIO direction to output
  149. */
  150. static int rb532_gpio_direction_output(struct gpio_chip *chip,
  151. unsigned offset, int value)
  152. {
  153. unsigned long flags;
  154. u32 mask = 1 << offset;
  155. u32 tmp;
  156. struct rb532_gpio_chip *gpch;
  157. void __iomem *gpdr;
  158. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  159. writel(mask, gpch->regbase + GPIOD);
  160. gpdr = gpch->regbase + GPIOCFG;
  161. local_irq_save(flags);
  162. tmp = readl(gpdr);
  163. tmp |= mask;
  164. writel(tmp, gpdr);
  165. local_irq_restore(flags);
  166. return 0;
  167. }
  168. /*
  169. * Set the GPIO interrupt level
  170. */
  171. static void rb532_gpio_set_int_level(struct gpio_chip *chip,
  172. unsigned offset, int value)
  173. {
  174. unsigned long flags;
  175. u32 mask = 1 << offset;
  176. u32 tmp;
  177. struct rb532_gpio_chip *gpch;
  178. void __iomem *gpil;
  179. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  180. gpil = gpch->regbase + GPIOILEVEL;
  181. local_irq_save(flags);
  182. tmp = readl(gpil);
  183. if (value)
  184. tmp |= mask;
  185. else
  186. tmp &= ~mask;
  187. writel(tmp, gpil);
  188. local_irq_restore(flags);
  189. }
  190. /*
  191. * Get the GPIO interrupt level
  192. */
  193. static int rb532_gpio_get_int_level(struct gpio_chip *chip, unsigned offset)
  194. {
  195. u32 mask = 1 << offset;
  196. struct rb532_gpio_chip *gpch;
  197. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  198. return readl(gpch->regbase + GPIOILEVEL) & mask;
  199. }
  200. /*
  201. * Set the GPIO interrupt status
  202. */
  203. static void rb532_gpio_set_int_status(struct gpio_chip *chip,
  204. unsigned offset, int value)
  205. {
  206. unsigned long flags;
  207. u32 mask = 1 << offset;
  208. u32 tmp;
  209. struct rb532_gpio_chip *gpch;
  210. void __iomem *gpis;
  211. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  212. gpis = gpch->regbase + GPIOISTAT;
  213. local_irq_save(flags);
  214. tmp = readl(gpis);
  215. if (value)
  216. tmp |= mask;
  217. else
  218. tmp &= ~mask;
  219. writel(tmp, gpis);
  220. local_irq_restore(flags);
  221. }
  222. /*
  223. * Get the GPIO interrupt status
  224. */
  225. static int rb532_gpio_get_int_status(struct gpio_chip *chip, unsigned offset)
  226. {
  227. u32 mask = 1 << offset;
  228. struct rb532_gpio_chip *gpch;
  229. gpch = container_of(chip, struct rb532_gpio_chip, chip);
  230. return readl(gpch->regbase + GPIOISTAT) & mask;
  231. }
  232. static struct rb532_gpio_chip rb532_gpio_chip[] = {
  233. [0] = {
  234. .chip = {
  235. .label = "gpio0",
  236. .direction_input = rb532_gpio_direction_input,
  237. .direction_output = rb532_gpio_direction_output,
  238. .get = rb532_gpio_get,
  239. .set = rb532_gpio_set,
  240. .base = 0,
  241. .ngpio = 32,
  242. },
  243. .get_int_level = rb532_gpio_get_int_level,
  244. .set_int_level = rb532_gpio_set_int_level,
  245. .get_int_status = rb532_gpio_get_int_status,
  246. .set_int_status = rb532_gpio_set_int_status,
  247. },
  248. };
  249. int __init rb532_gpio_init(void)
  250. {
  251. struct resource *r;
  252. r = rb532_gpio_reg0_res;
  253. rb532_gpio_chip->regbase = ioremap_nocache(r->start, r->end - r->start);
  254. if (!rb532_gpio_chip->regbase) {
  255. printk(KERN_ERR "rb532: cannot remap GPIO register 0\n");
  256. return -ENXIO;
  257. }
  258. /* Register our GPIO chip */
  259. gpiochip_add(&rb532_gpio_chip->chip);
  260. r = rb532_dev3_ctl_res;
  261. dev3.base = ioremap_nocache(r->start, r->end - r->start);
  262. if (!dev3.base) {
  263. printk(KERN_ERR "rb532: cannot remap device controller 3\n");
  264. return -ENXIO;
  265. }
  266. return 0;
  267. }
  268. arch_initcall(rb532_gpio_init);