gpio-mpc8xxx.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/of_irq.h>
  17. #include <linux/gpio.h>
  18. #include <linux/slab.h>
  19. #include <linux/irq.h>
  20. #define MPC8XXX_GPIO_PINS 32
  21. #define GPIO_DIR 0x00
  22. #define GPIO_ODR 0x04
  23. #define GPIO_DAT 0x08
  24. #define GPIO_IER 0x0c
  25. #define GPIO_IMR 0x10
  26. #define GPIO_ICR 0x14
  27. #define GPIO_ICR2 0x18
  28. struct mpc8xxx_gpio_chip {
  29. struct of_mm_gpio_chip mm_gc;
  30. spinlock_t lock;
  31. /*
  32. * shadowed data register to be able to clear/set output pins in
  33. * open drain mode safely
  34. */
  35. u32 data;
  36. struct irq_domain *irq;
  37. const void *of_dev_id_data;
  38. };
  39. static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
  40. {
  41. return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
  42. }
  43. static inline struct mpc8xxx_gpio_chip *
  44. to_mpc8xxx_gpio_chip(struct of_mm_gpio_chip *mm)
  45. {
  46. return container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
  47. }
  48. static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
  49. {
  50. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  51. mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
  52. }
  53. /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
  54. * defined as output cannot be determined by reading GPDAT register,
  55. * so we use shadow data register instead. The status of input pins
  56. * is determined by reading GPDAT register.
  57. */
  58. static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  59. {
  60. u32 val;
  61. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  62. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  63. val = in_be32(mm->regs + GPIO_DAT) & ~in_be32(mm->regs + GPIO_DIR);
  64. return (val | mpc8xxx_gc->data) & mpc8xxx_gpio2mask(gpio);
  65. }
  66. static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  67. {
  68. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  69. return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
  70. }
  71. static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  72. {
  73. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  74. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  75. unsigned long flags;
  76. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  77. if (val)
  78. mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
  79. else
  80. mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
  81. out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
  82. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  83. }
  84. static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  85. {
  86. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  87. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  88. unsigned long flags;
  89. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  90. clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  91. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  92. return 0;
  93. }
  94. static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  95. {
  96. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  97. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  98. unsigned long flags;
  99. mpc8xxx_gpio_set(gc, gpio, val);
  100. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  101. setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
  102. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  103. return 0;
  104. }
  105. static int mpc5121_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  106. {
  107. /* GPIO 28..31 are input only on MPC5121 */
  108. if (gpio >= 28)
  109. return -EINVAL;
  110. return mpc8xxx_gpio_dir_out(gc, gpio, val);
  111. }
  112. static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  113. {
  114. struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
  115. struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
  116. if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
  117. return irq_create_mapping(mpc8xxx_gc->irq, offset);
  118. else
  119. return -ENXIO;
  120. }
  121. static void mpc8xxx_gpio_irq_cascade(unsigned int irq, struct irq_desc *desc)
  122. {
  123. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
  124. struct irq_chip *chip = irq_desc_get_chip(desc);
  125. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  126. unsigned int mask;
  127. mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR);
  128. if (mask)
  129. generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
  130. 32 - ffs(mask)));
  131. if (chip->irq_eoi)
  132. chip->irq_eoi(&desc->irq_data);
  133. }
  134. static void mpc8xxx_irq_unmask(struct irq_data *d)
  135. {
  136. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  137. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  138. unsigned long flags;
  139. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  140. setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  141. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  142. }
  143. static void mpc8xxx_irq_mask(struct irq_data *d)
  144. {
  145. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  146. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  147. unsigned long flags;
  148. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  149. clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  150. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  151. }
  152. static void mpc8xxx_irq_ack(struct irq_data *d)
  153. {
  154. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  155. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  156. out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  157. }
  158. static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  159. {
  160. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  161. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  162. unsigned long flags;
  163. switch (flow_type) {
  164. case IRQ_TYPE_EDGE_FALLING:
  165. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  166. setbits32(mm->regs + GPIO_ICR,
  167. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  168. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  169. break;
  170. case IRQ_TYPE_EDGE_BOTH:
  171. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  172. clrbits32(mm->regs + GPIO_ICR,
  173. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  174. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. return 0;
  180. }
  181. static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  182. {
  183. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  184. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  185. unsigned long gpio = irqd_to_hwirq(d);
  186. void __iomem *reg;
  187. unsigned int shift;
  188. unsigned long flags;
  189. if (gpio < 16) {
  190. reg = mm->regs + GPIO_ICR;
  191. shift = (15 - gpio) * 2;
  192. } else {
  193. reg = mm->regs + GPIO_ICR2;
  194. shift = (15 - (gpio % 16)) * 2;
  195. }
  196. switch (flow_type) {
  197. case IRQ_TYPE_EDGE_FALLING:
  198. case IRQ_TYPE_LEVEL_LOW:
  199. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  200. clrsetbits_be32(reg, 3 << shift, 2 << shift);
  201. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  202. break;
  203. case IRQ_TYPE_EDGE_RISING:
  204. case IRQ_TYPE_LEVEL_HIGH:
  205. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  206. clrsetbits_be32(reg, 3 << shift, 1 << shift);
  207. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  208. break;
  209. case IRQ_TYPE_EDGE_BOTH:
  210. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  211. clrbits32(reg, 3 << shift);
  212. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  213. break;
  214. default:
  215. return -EINVAL;
  216. }
  217. return 0;
  218. }
  219. static struct irq_chip mpc8xxx_irq_chip = {
  220. .name = "mpc8xxx-gpio",
  221. .irq_unmask = mpc8xxx_irq_unmask,
  222. .irq_mask = mpc8xxx_irq_mask,
  223. .irq_ack = mpc8xxx_irq_ack,
  224. .irq_set_type = mpc8xxx_irq_set_type,
  225. };
  226. static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  227. irq_hw_number_t hw)
  228. {
  229. struct mpc8xxx_gpio_chip *mpc8xxx_gc = h->host_data;
  230. if (mpc8xxx_gc->of_dev_id_data)
  231. mpc8xxx_irq_chip.irq_set_type = mpc8xxx_gc->of_dev_id_data;
  232. irq_set_chip_data(virq, h->host_data);
  233. irq_set_chip_and_handler(virq, &mpc8xxx_irq_chip, handle_level_irq);
  234. return 0;
  235. }
  236. static struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
  237. .map = mpc8xxx_gpio_irq_map,
  238. .xlate = irq_domain_xlate_twocell,
  239. };
  240. static struct of_device_id mpc8xxx_gpio_ids[] __initdata = {
  241. { .compatible = "fsl,mpc8349-gpio", },
  242. { .compatible = "fsl,mpc8572-gpio", },
  243. { .compatible = "fsl,mpc8610-gpio", },
  244. { .compatible = "fsl,mpc5121-gpio", .data = mpc512x_irq_set_type, },
  245. { .compatible = "fsl,pq3-gpio", },
  246. { .compatible = "fsl,qoriq-gpio", },
  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 = of_device_is_compatible(np, "fsl,mpc5121-gpio") ?
  269. mpc5121_gpio_dir_out : mpc8xxx_gpio_dir_out;
  270. gc->get = of_device_is_compatible(np, "fsl,mpc8572-gpio") ?
  271. mpc8572_gpio_get : mpc8xxx_gpio_get;
  272. gc->set = mpc8xxx_gpio_set;
  273. gc->to_irq = mpc8xxx_gpio_to_irq;
  274. ret = of_mm_gpiochip_add(np, mm_gc);
  275. if (ret)
  276. goto err;
  277. hwirq = irq_of_parse_and_map(np, 0);
  278. if (hwirq == NO_IRQ)
  279. goto skip_irq;
  280. mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
  281. &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
  282. if (!mpc8xxx_gc->irq)
  283. goto skip_irq;
  284. id = of_match_node(mpc8xxx_gpio_ids, np);
  285. if (id)
  286. mpc8xxx_gc->of_dev_id_data = id->data;
  287. /* ack and mask all irqs */
  288. out_be32(mm_gc->regs + GPIO_IER, 0xffffffff);
  289. out_be32(mm_gc->regs + GPIO_IMR, 0);
  290. irq_set_handler_data(hwirq, mpc8xxx_gc);
  291. irq_set_chained_handler(hwirq, mpc8xxx_gpio_irq_cascade);
  292. skip_irq:
  293. return;
  294. err:
  295. pr_err("%s: registration failed with status %d\n",
  296. np->full_name, ret);
  297. kfree(mpc8xxx_gc);
  298. return;
  299. }
  300. static int __init mpc8xxx_add_gpiochips(void)
  301. {
  302. struct device_node *np;
  303. for_each_matching_node(np, mpc8xxx_gpio_ids)
  304. mpc8xxx_add_controller(np);
  305. return 0;
  306. }
  307. arch_initcall(mpc8xxx_add_gpiochips);