mpc8xxx_gpio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * GPIOs on MPC8349/8572/8610 and compatible
  3. *
  4. * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/gpio.h>
  17. #include <linux/slab.h>
  18. #include <linux/irq.h>
  19. #define MPC8XXX_GPIO_PINS 32
  20. #define GPIO_DIR 0x00
  21. #define GPIO_ODR 0x04
  22. #define GPIO_DAT 0x08
  23. #define GPIO_IER 0x0c
  24. #define GPIO_IMR 0x10
  25. #define GPIO_ICR 0x14
  26. struct mpc8xxx_gpio_chip {
  27. struct of_mm_gpio_chip mm_gc;
  28. spinlock_t lock;
  29. /*
  30. * shadowed data register to be able to clear/set output pins in
  31. * open drain mode safely
  32. */
  33. u32 data;
  34. struct irq_host *irq;
  35. };
  36. static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
  37. {
  38. return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
  39. }
  40. static inline struct mpc8xxx_gpio_chip *
  41. to_mpc8xxx_gpio_chip(struct of_mm_gpio_chip *mm)
  42. {
  43. return container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
  44. }
  45. static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
  46. {
  47. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  48. mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
  49. }
  50. /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
  51. * defined as output cannot be determined by reading GPDAT register,
  52. * so we use shadow data register instead. The status of input pins
  53. * is determined by reading GPDAT register.
  54. */
  55. static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  56. {
  57. u32 val;
  58. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  59. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  60. val = in_be32(mm->regs + GPIO_DAT) & ~in_be32(mm->regs + GPIO_DIR);
  61. return (val | mpc8xxx_gc->data) & mpc8xxx_gpio2mask(gpio);
  62. }
  63. static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  64. {
  65. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  66. return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
  67. }
  68. static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  69. {
  70. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  71. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  72. unsigned long flags;
  73. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  74. if (val)
  75. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
  76. else
  77. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
  78. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  79. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  80. }
  81. static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  82. {
  83. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  84. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  85. unsigned long flags;
  86. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  87. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  88. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  89. return 0;
  90. }
  91. static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  92. {
  93. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  94. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  95. unsigned long flags;
  96. mpc8xxx_gpio_set(gc, gpio, val);
  97. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  98. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  99. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  100. return 0;
  101. }
  102. static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  103. {
  104. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  105. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  106. if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
  107. return irq_create_mapping(mpc8xxx_gc->irq, offset);
  108. else
  109. return -ENXIO;
  110. }
  111. static void mpc8xxx_gpio_irq_cascade(unsigned int irq, struct irq_desc *desc)
  112. {
  113. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_desc_data(desc);
  114. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  115. unsigned int mask;
  116. mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR);
  117. if (mask)
  118. generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
  119. 32 - ffs(mask)));
  120. }
  121. static void mpc8xxx_irq_unmask(unsigned int virq)
  122. {
  123. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  124. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  125. unsigned long flags;
  126. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  127. setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(virq_to_hw(virq)));
  128. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  129. }
  130. static void mpc8xxx_irq_mask(unsigned int virq)
  131. {
  132. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  133. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  134. unsigned long flags;
  135. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  136. clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(virq_to_hw(virq)));
  137. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  138. }
  139. static void mpc8xxx_irq_ack(unsigned int virq)
  140. {
  141. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  142. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  143. out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(virq_to_hw(virq)));
  144. }
  145. static int mpc8xxx_irq_set_type(unsigned int virq, unsigned int flow_type)
  146. {
  147. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  148. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  149. unsigned long flags;
  150. switch (flow_type) {
  151. case IRQ_TYPE_EDGE_FALLING:
  152. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  153. setbits32(mm->regs + GPIO_ICR,
  154. mpc8xxx_gpio2mask(virq_to_hw(virq)));
  155. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  156. break;
  157. case IRQ_TYPE_EDGE_BOTH:
  158. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  159. clrbits32(mm->regs + GPIO_ICR,
  160. mpc8xxx_gpio2mask(virq_to_hw(virq)));
  161. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. return 0;
  167. }
  168. static struct irq_chip mpc8xxx_irq_chip = {
  169. .name = "mpc8xxx-gpio",
  170. .unmask = mpc8xxx_irq_unmask,
  171. .mask = mpc8xxx_irq_mask,
  172. .ack = mpc8xxx_irq_ack,
  173. .set_type = mpc8xxx_irq_set_type,
  174. };
  175. static int mpc8xxx_gpio_irq_map(struct irq_host *h, unsigned int virq,
  176. irq_hw_number_t hw)
  177. {
  178. set_irq_chip_data(virq, h->host_data);
  179. set_irq_chip_and_handler(virq, &mpc8xxx_irq_chip, handle_level_irq);
  180. set_irq_type(virq, IRQ_TYPE_NONE);
  181. return 0;
  182. }
  183. static int mpc8xxx_gpio_irq_xlate(struct irq_host *h, struct device_node *ct,
  184. const u32 *intspec, unsigned int intsize,
  185. irq_hw_number_t *out_hwirq,
  186. unsigned int *out_flags)
  187. {
  188. /* interrupt sense values coming from the device tree equal either
  189. * EDGE_FALLING or EDGE_BOTH
  190. */
  191. *out_hwirq = intspec[0];
  192. *out_flags = intspec[1];
  193. return 0;
  194. }
  195. static struct irq_host_ops mpc8xxx_gpio_irq_ops = {
  196. .map = mpc8xxx_gpio_irq_map,
  197. .xlate = mpc8xxx_gpio_irq_xlate,
  198. };
  199. static void __init mpc8xxx_add_controller(struct device_node *np)
  200. {
  201. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  202. struct of_mm_gpio_chip *mm_gc;
  203. struct gpio_chip *gc;
  204. unsigned hwirq;
  205. int ret;
  206. mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
  207. if (!mpc8xxx_gc) {
  208. ret = -ENOMEM;
  209. goto err;
  210. }
  211. spin_lock_init(&mpc8xxx_gc->lock);
  212. mm_gc = &mpc8xxx_gc->mm_gc;
  213. gc = &mm_gc->gc;
  214. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  215. gc->ngpio = MPC8XXX_GPIO_PINS;
  216. gc->direction_input = mpc8xxx_gpio_dir_in;
  217. gc->direction_output = mpc8xxx_gpio_dir_out;
  218. if (of_device_is_compatible(np, "fsl,mpc8572-gpio"))
  219. gc->get = mpc8572_gpio_get;
  220. else
  221. gc->get = mpc8xxx_gpio_get;
  222. gc->set = mpc8xxx_gpio_set;
  223. gc->to_irq = mpc8xxx_gpio_to_irq;
  224. ret = of_mm_gpiochip_add(np, mm_gc);
  225. if (ret)
  226. goto err;
  227. hwirq = irq_of_parse_and_map(np, 0);
  228. if (hwirq == NO_IRQ)
  229. goto skip_irq;
  230. mpc8xxx_gc->irq =
  231. irq_alloc_host(np, IRQ_HOST_MAP_LINEAR, MPC8XXX_GPIO_PINS,
  232. &mpc8xxx_gpio_irq_ops, MPC8XXX_GPIO_PINS);
  233. if (!mpc8xxx_gc->irq)
  234. goto skip_irq;
  235. mpc8xxx_gc->irq->host_data = mpc8xxx_gc;
  236. /* ack and mask all irqs */
  237. out_be32(mm_gc->regs + GPIO_IER, 0xffffffff);
  238. out_be32(mm_gc->regs + GPIO_IMR, 0);
  239. set_irq_data(hwirq, mpc8xxx_gc);
  240. set_irq_chained_handler(hwirq, mpc8xxx_gpio_irq_cascade);
  241. skip_irq:
  242. return;
  243. err:
  244. pr_err("%s: registration failed with status %d\n",
  245. np->full_name, ret);
  246. kfree(mpc8xxx_gc);
  247. return;
  248. }
  249. static int __init mpc8xxx_add_gpiochips(void)
  250. {
  251. struct device_node *np;
  252. for_each_compatible_node(np, NULL, "fsl,mpc8349-gpio")
  253. mpc8xxx_add_controller(np);
  254. for_each_compatible_node(np, NULL, "fsl,mpc8572-gpio")
  255. mpc8xxx_add_controller(np);
  256. for_each_compatible_node(np, NULL, "fsl,mpc8610-gpio")
  257. mpc8xxx_add_controller(np);
  258. return 0;
  259. }
  260. arch_initcall(mpc8xxx_add_gpiochips);