gpio-mpc8xxx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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_domain *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. u32 out_mask, out_shadow;
  63. out_mask = in_be32(mm->regs + GPIO_DIR);
  64. val = in_be32(mm->regs + GPIO_DAT) & ~out_mask;
  65. out_shadow = mpc8xxx_gc->data & out_mask;
  66. return (val | out_shadow) & mpc8xxx_gpio2mask(gpio);
  67. }
  68. static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  69. {
  70. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  71. return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
  72. }
  73. static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  74. {
  75. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  76. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  77. unsigned long flags;
  78. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  79. if (val)
  80. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
  81. else
  82. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
  83. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  84. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  85. }
  86. static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  87. {
  88. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  89. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  90. unsigned long flags;
  91. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  92. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  93. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  94. return 0;
  95. }
  96. static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  97. {
  98. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  99. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  100. unsigned long flags;
  101. mpc8xxx_gpio_set(gc, gpio, val);
  102. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  103. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  104. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  105. return 0;
  106. }
  107. static int mpc5121_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  108. {
  109. /* GPIO 28..31 are input only on MPC5121 */
  110. if (gpio >= 28)
  111. return -EINVAL;
  112. return mpc8xxx_gpio_dir_out(gc, gpio, val);
  113. }
  114. static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  115. {
  116. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  117. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  118. if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
  119. return irq_create_mapping(mpc8xxx_gc->irq, offset);
  120. else
  121. return -ENXIO;
  122. }
  123. static void mpc8xxx_gpio_irq_cascade(unsigned int irq, struct irq_desc *desc)
  124. {
  125. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
  126. struct irq_chip *chip = irq_desc_get_chip(desc);
  127. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  128. unsigned int mask;
  129. mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR);
  130. if (mask)
  131. generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
  132. 32 - ffs(mask)));
  133. if (chip->irq_eoi)
  134. chip->irq_eoi(&desc->irq_data);
  135. }
  136. static void mpc8xxx_irq_unmask(struct irq_data *d)
  137. {
  138. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  139. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  140. unsigned long flags;
  141. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  142. setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  143. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  144. }
  145. static void mpc8xxx_irq_mask(struct irq_data *d)
  146. {
  147. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  148. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  149. unsigned long flags;
  150. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  151. clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  152. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  153. }
  154. static void mpc8xxx_irq_ack(struct irq_data *d)
  155. {
  156. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  157. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  158. out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  159. }
  160. static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  161. {
  162. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  163. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  164. unsigned long flags;
  165. switch (flow_type) {
  166. case IRQ_TYPE_EDGE_FALLING:
  167. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  168. setbits32(mm->regs + GPIO_ICR,
  169. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  170. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  171. break;
  172. case IRQ_TYPE_EDGE_BOTH:
  173. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  174. clrbits32(mm->regs + GPIO_ICR,
  175. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  176. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  177. break;
  178. default:
  179. return -EINVAL;
  180. }
  181. return 0;
  182. }
  183. static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  184. {
  185. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  186. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  187. unsigned long gpio = irqd_to_hwirq(d);
  188. void __iomem *reg;
  189. unsigned int shift;
  190. unsigned long flags;
  191. if (gpio < 16) {
  192. reg = mm->regs + GPIO_ICR;
  193. shift = (15 - gpio) * 2;
  194. } else {
  195. reg = mm->regs + GPIO_ICR2;
  196. shift = (15 - (gpio % 16)) * 2;
  197. }
  198. switch (flow_type) {
  199. case IRQ_TYPE_EDGE_FALLING:
  200. case IRQ_TYPE_LEVEL_LOW:
  201. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  202. clrsetbits_be32(reg, 3 << shift, 2 << shift);
  203. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  204. break;
  205. case IRQ_TYPE_EDGE_RISING:
  206. case IRQ_TYPE_LEVEL_HIGH:
  207. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  208. clrsetbits_be32(reg, 3 << shift, 1 << shift);
  209. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  210. break;
  211. case IRQ_TYPE_EDGE_BOTH:
  212. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  213. clrbits32(reg, 3 << shift);
  214. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  215. break;
  216. default:
  217. return -EINVAL;
  218. }
  219. return 0;
  220. }
  221. static struct irq_chip mpc8xxx_irq_chip = {
  222. .name = "mpc8xxx-gpio",
  223. .irq_unmask = mpc8xxx_irq_unmask,
  224. .irq_mask = mpc8xxx_irq_mask,
  225. .irq_ack = mpc8xxx_irq_ack,
  226. .irq_set_type = mpc8xxx_irq_set_type,
  227. };
  228. static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  229. irq_hw_number_t hw)
  230. {
  231. struct mpc8xxx_gpio_chip *mpc8xxx_gc = h->host_data;
  232. if (mpc8xxx_gc->of_dev_id_data)
  233. mpc8xxx_irq_chip.irq_set_type = mpc8xxx_gc->of_dev_id_data;
  234. irq_set_chip_data(virq, h->host_data);
  235. irq_set_chip_and_handler(virq, &mpc8xxx_irq_chip, handle_level_irq);
  236. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  237. return 0;
  238. }
  239. static struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
  240. .map = mpc8xxx_gpio_irq_map,
  241. .xlate = irq_domain_xlate_twocell,
  242. };
  243. static struct of_device_id mpc8xxx_gpio_ids[] __initdata = {
  244. { .compatible = "fsl,mpc8349-gpio", },
  245. { .compatible = "fsl,mpc8572-gpio", },
  246. { .compatible = "fsl,mpc8610-gpio", },
  247. { .compatible = "fsl,mpc5121-gpio", .data = mpc512x_irq_set_type, },
  248. { .compatible = "fsl,pq3-gpio", },
  249. { .compatible = "fsl,qoriq-gpio", },
  250. {}
  251. };
  252. static void __init mpc8xxx_add_controller(struct device_node *np)
  253. {
  254. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  255. struct of_mm_gpio_chip *mm_gc;
  256. struct gpio_chip *gc;
  257. const struct of_device_id *id;
  258. unsigned hwirq;
  259. int ret;
  260. mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
  261. if (!mpc8xxx_gc) {
  262. ret = -ENOMEM;
  263. goto err;
  264. }
  265. spin_lock_init(&mpc8xxx_gc->lock);
  266. mm_gc = &mpc8xxx_gc->mm_gc;
  267. gc = &mm_gc->gc;
  268. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  269. gc->ngpio = MPC8XXX_GPIO_PINS;
  270. gc->direction_input = mpc8xxx_gpio_dir_in;
  271. gc->direction_output = of_device_is_compatible(np, "fsl,mpc5121-gpio") ?
  272. mpc5121_gpio_dir_out : mpc8xxx_gpio_dir_out;
  273. gc->get = of_device_is_compatible(np, "fsl,mpc8572-gpio") ?
  274. mpc8572_gpio_get : mpc8xxx_gpio_get;
  275. gc->set = mpc8xxx_gpio_set;
  276. gc->to_irq = mpc8xxx_gpio_to_irq;
  277. ret = of_mm_gpiochip_add(np, mm_gc);
  278. if (ret)
  279. goto err;
  280. hwirq = irq_of_parse_and_map(np, 0);
  281. if (hwirq == NO_IRQ)
  282. goto skip_irq;
  283. mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
  284. &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
  285. if (!mpc8xxx_gc->irq)
  286. goto skip_irq;
  287. id = of_match_node(mpc8xxx_gpio_ids, np);
  288. if (id)
  289. mpc8xxx_gc->of_dev_id_data = id->data;
  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. irq_set_handler_data(hwirq, mpc8xxx_gc);
  294. irq_set_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. return 0;
  309. }
  310. arch_initcall(mpc8xxx_add_gpiochips);