mpc8xxx_gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * GPIOs on MPC512x/8349/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. #define GPIO_ICR2 0x18
  27. struct mpc8xxx_gpio_chip {
  28. struct of_mm_gpio_chip mm_gc;
  29. spinlock_t lock;
  30. /*
  31. * shadowed data register to be able to clear/set output pins in
  32. * open drain mode safely
  33. */
  34. u32 data;
  35. struct irq_host *irq;
  36. void *of_dev_id_data;
  37. };
  38. static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
  39. {
  40. return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
  41. }
  42. static inline struct mpc8xxx_gpio_chip *
  43. to_mpc8xxx_gpio_chip(struct of_mm_gpio_chip *mm)
  44. {
  45. return container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
  46. }
  47. static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
  48. {
  49. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  50. mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
  51. }
  52. /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
  53. * defined as output cannot be determined by reading GPDAT register,
  54. * so we use shadow data register instead. The status of input pins
  55. * is determined by reading GPDAT register.
  56. */
  57. static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  58. {
  59. u32 val;
  60. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  61. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  62. val = in_be32(mm->regs + GPIO_DAT) & ~in_be32(mm->regs + GPIO_DIR);
  63. return (val | mpc8xxx_gc->data) & mpc8xxx_gpio2mask(gpio);
  64. }
  65. static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  66. {
  67. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  68. return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
  69. }
  70. static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  71. {
  72. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  73. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  74. unsigned long flags;
  75. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  76. if (val)
  77. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
  78. else
  79. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
  80. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  81. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  82. }
  83. static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  84. {
  85. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  86. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  87. unsigned long flags;
  88. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  89. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  90. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  91. return 0;
  92. }
  93. static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  94. {
  95. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  96. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  97. unsigned long flags;
  98. mpc8xxx_gpio_set(gc, gpio, val);
  99. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  100. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  101. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  102. return 0;
  103. }
  104. static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  105. {
  106. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  107. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  108. if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
  109. return irq_create_mapping(mpc8xxx_gc->irq, offset);
  110. else
  111. return -ENXIO;
  112. }
  113. static void mpc8xxx_gpio_irq_cascade(unsigned int irq, struct irq_desc *desc)
  114. {
  115. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_desc_data(desc);
  116. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  117. unsigned int mask;
  118. mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR);
  119. if (mask)
  120. generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
  121. 32 - ffs(mask)));
  122. }
  123. static void mpc8xxx_irq_unmask(unsigned int virq)
  124. {
  125. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  126. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  127. unsigned long flags;
  128. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  129. setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(virq_to_hw(virq)));
  130. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  131. }
  132. static void mpc8xxx_irq_mask(unsigned int virq)
  133. {
  134. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  135. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  136. unsigned long flags;
  137. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  138. clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(virq_to_hw(virq)));
  139. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  140. }
  141. static void mpc8xxx_irq_ack(unsigned int virq)
  142. {
  143. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  144. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  145. out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(virq_to_hw(virq)));
  146. }
  147. static int mpc8xxx_irq_set_type(unsigned int virq, unsigned int flow_type)
  148. {
  149. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  150. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  151. unsigned long flags;
  152. switch (flow_type) {
  153. case IRQ_TYPE_EDGE_FALLING:
  154. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  155. setbits32(mm->regs + GPIO_ICR,
  156. mpc8xxx_gpio2mask(virq_to_hw(virq)));
  157. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  158. break;
  159. case IRQ_TYPE_EDGE_BOTH:
  160. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  161. clrbits32(mm->regs + GPIO_ICR,
  162. mpc8xxx_gpio2mask(virq_to_hw(virq)));
  163. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. return 0;
  169. }
  170. static int mpc512x_irq_set_type(unsigned int virq, unsigned int flow_type)
  171. {
  172. struct mpc8xxx_gpio_chip *mpc8xxx_gc = get_irq_chip_data(virq);
  173. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  174. unsigned long gpio = virq_to_hw(virq);
  175. void __iomem *reg;
  176. unsigned int shift;
  177. unsigned long flags;
  178. if (gpio < 16) {
  179. reg = mm->regs + GPIO_ICR;
  180. shift = (15 - gpio) * 2;
  181. } else {
  182. reg = mm->regs + GPIO_ICR2;
  183. shift = (15 - (gpio % 16)) * 2;
  184. }
  185. switch (flow_type) {
  186. case IRQ_TYPE_EDGE_FALLING:
  187. case IRQ_TYPE_LEVEL_LOW:
  188. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  189. clrsetbits_be32(reg, 3 << shift, 2 << shift);
  190. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  191. break;
  192. case IRQ_TYPE_EDGE_RISING:
  193. case IRQ_TYPE_LEVEL_HIGH:
  194. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  195. clrsetbits_be32(reg, 3 << shift, 1 << shift);
  196. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  197. break;
  198. case IRQ_TYPE_EDGE_BOTH:
  199. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  200. clrbits32(reg, 3 << shift);
  201. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  202. break;
  203. default:
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. static struct irq_chip mpc8xxx_irq_chip = {
  209. .name = "mpc8xxx-gpio",
  210. .unmask = mpc8xxx_irq_unmask,
  211. .mask = mpc8xxx_irq_mask,
  212. .ack = mpc8xxx_irq_ack,
  213. .set_type = mpc8xxx_irq_set_type,
  214. };
  215. static int mpc8xxx_gpio_irq_map(struct irq_host *h, unsigned int virq,
  216. irq_hw_number_t hw)
  217. {
  218. struct mpc8xxx_gpio_chip *mpc8xxx_gc = h->host_data;
  219. if (mpc8xxx_gc->of_dev_id_data)
  220. mpc8xxx_irq_chip.set_type = mpc8xxx_gc->of_dev_id_data;
  221. set_irq_chip_data(virq, h->host_data);
  222. set_irq_chip_and_handler(virq, &mpc8xxx_irq_chip, handle_level_irq);
  223. set_irq_type(virq, IRQ_TYPE_NONE);
  224. return 0;
  225. }
  226. static int mpc8xxx_gpio_irq_xlate(struct irq_host *h, struct device_node *ct,
  227. const u32 *intspec, unsigned int intsize,
  228. irq_hw_number_t *out_hwirq,
  229. unsigned int *out_flags)
  230. {
  231. /* interrupt sense values coming from the device tree equal either
  232. * EDGE_FALLING or EDGE_BOTH
  233. */
  234. *out_hwirq = intspec[0];
  235. *out_flags = intspec[1];
  236. return 0;
  237. }
  238. static struct irq_host_ops mpc8xxx_gpio_irq_ops = {
  239. .map = mpc8xxx_gpio_irq_map,
  240. .xlate = mpc8xxx_gpio_irq_xlate,
  241. };
  242. static struct of_device_id mpc8xxx_gpio_ids[] __initdata = {
  243. { .compatible = "fsl,mpc8349-gpio", },
  244. { .compatible = "fsl,mpc8572-gpio", },
  245. { .compatible = "fsl,mpc8610-gpio", },
  246. { .compatible = "fsl,mpc5121-gpio", .data = mpc512x_irq_set_type, },
  247. {}
  248. };
  249. static void __init mpc8xxx_add_controller(struct device_node *np)
  250. {
  251. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  252. struct of_mm_gpio_chip *mm_gc;
  253. struct gpio_chip *gc;
  254. const struct of_device_id *id;
  255. unsigned hwirq;
  256. int ret;
  257. mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
  258. if (!mpc8xxx_gc) {
  259. ret = -ENOMEM;
  260. goto err;
  261. }
  262. spin_lock_init(&mpc8xxx_gc->lock);
  263. mm_gc = &mpc8xxx_gc->mm_gc;
  264. gc = &mm_gc->gc;
  265. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  266. gc->ngpio = MPC8XXX_GPIO_PINS;
  267. gc->direction_input = mpc8xxx_gpio_dir_in;
  268. gc->direction_output = mpc8xxx_gpio_dir_out;
  269. if (of_device_is_compatible(np, "fsl,mpc8572-gpio"))
  270. gc->get = mpc8572_gpio_get;
  271. else
  272. gc->get = mpc8xxx_gpio_get;
  273. gc->set = mpc8xxx_gpio_set;
  274. gc->to_irq = mpc8xxx_gpio_to_irq;
  275. ret = of_mm_gpiochip_add(np, mm_gc);
  276. if (ret)
  277. goto err;
  278. hwirq = irq_of_parse_and_map(np, 0);
  279. if (hwirq == NO_IRQ)
  280. goto skip_irq;
  281. mpc8xxx_gc->irq =
  282. irq_alloc_host(np, IRQ_HOST_MAP_LINEAR, MPC8XXX_GPIO_PINS,
  283. &mpc8xxx_gpio_irq_ops, MPC8XXX_GPIO_PINS);
  284. if (!mpc8xxx_gc->irq)
  285. goto skip_irq;
  286. id = of_match_node(mpc8xxx_gpio_ids, np);
  287. if (id)
  288. mpc8xxx_gc->of_dev_id_data = id->data;
  289. mpc8xxx_gc->irq->host_data = mpc8xxx_gc;
  290. /* ack and mask all irqs */
  291. out_be32(mm_gc->regs + GPIO_IER, 0xffffffff);
  292. out_be32(mm_gc->regs + GPIO_IMR, 0);
  293. set_irq_data(hwirq, mpc8xxx_gc);
  294. set_irq_chained_handler(hwirq, mpc8xxx_gpio_irq_cascade);
  295. skip_irq:
  296. return;
  297. err:
  298. pr_err("%s: registration failed with status %d\n",
  299. np->full_name, ret);
  300. kfree(mpc8xxx_gc);
  301. return;
  302. }
  303. static int __init mpc8xxx_add_gpiochips(void)
  304. {
  305. struct device_node *np;
  306. for_each_matching_node(np, mpc8xxx_gpio_ids)
  307. mpc8xxx_add_controller(np);
  308. for_each_compatible_node(np, NULL, "fsl,qoriq-gpio")
  309. mpc8xxx_add_controller(np);
  310. return 0;
  311. }
  312. arch_initcall(mpc8xxx_add_gpiochips);