gpio-mpc8xxx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 = irq_desc_get_handler_data(desc);
  116. struct irq_chip *chip = irq_desc_get_chip(desc);
  117. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  118. unsigned int mask;
  119. mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR);
  120. if (mask)
  121. generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
  122. 32 - ffs(mask)));
  123. chip->irq_eoi(&desc->irq_data);
  124. }
  125. static void mpc8xxx_irq_unmask(struct irq_data *d)
  126. {
  127. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  128. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  129. unsigned long flags;
  130. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  131. setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  132. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  133. }
  134. static void mpc8xxx_irq_mask(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. clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  141. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  142. }
  143. static void mpc8xxx_irq_ack(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. out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  148. }
  149. static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
  150. {
  151. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  152. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  153. unsigned long flags;
  154. switch (flow_type) {
  155. case IRQ_TYPE_EDGE_FALLING:
  156. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  157. setbits32(mm->regs + GPIO_ICR,
  158. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  159. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  160. break;
  161. case IRQ_TYPE_EDGE_BOTH:
  162. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  163. clrbits32(mm->regs + GPIO_ICR,
  164. mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
  165. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. return 0;
  171. }
  172. static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
  173. {
  174. struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
  175. struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
  176. unsigned long gpio = irqd_to_hwirq(d);
  177. void __iomem *reg;
  178. unsigned int shift;
  179. unsigned long flags;
  180. if (gpio < 16) {
  181. reg = mm->regs + GPIO_ICR;
  182. shift = (15 - gpio) * 2;
  183. } else {
  184. reg = mm->regs + GPIO_ICR2;
  185. shift = (15 - (gpio % 16)) * 2;
  186. }
  187. switch (flow_type) {
  188. case IRQ_TYPE_EDGE_FALLING:
  189. case IRQ_TYPE_LEVEL_LOW:
  190. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  191. clrsetbits_be32(reg, 3 << shift, 2 << shift);
  192. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  193. break;
  194. case IRQ_TYPE_EDGE_RISING:
  195. case IRQ_TYPE_LEVEL_HIGH:
  196. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  197. clrsetbits_be32(reg, 3 << shift, 1 << shift);
  198. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  199. break;
  200. case IRQ_TYPE_EDGE_BOTH:
  201. spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
  202. clrbits32(reg, 3 << shift);
  203. spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
  204. break;
  205. default:
  206. return -EINVAL;
  207. }
  208. return 0;
  209. }
  210. static struct irq_chip mpc8xxx_irq_chip = {
  211. .name = "mpc8xxx-gpio",
  212. .irq_unmask = mpc8xxx_irq_unmask,
  213. .irq_mask = mpc8xxx_irq_mask,
  214. .irq_ack = mpc8xxx_irq_ack,
  215. .irq_set_type = mpc8xxx_irq_set_type,
  216. };
  217. static int mpc8xxx_gpio_irq_map(struct irq_host *h, unsigned int virq,
  218. irq_hw_number_t hw)
  219. {
  220. struct mpc8xxx_gpio_chip *mpc8xxx_gc = h->host_data;
  221. if (mpc8xxx_gc->of_dev_id_data)
  222. mpc8xxx_irq_chip.irq_set_type = mpc8xxx_gc->of_dev_id_data;
  223. irq_set_chip_data(virq, h->host_data);
  224. irq_set_chip_and_handler(virq, &mpc8xxx_irq_chip, handle_level_irq);
  225. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  226. return 0;
  227. }
  228. static int mpc8xxx_gpio_irq_xlate(struct irq_host *h, struct device_node *ct,
  229. const u32 *intspec, unsigned int intsize,
  230. irq_hw_number_t *out_hwirq,
  231. unsigned int *out_flags)
  232. {
  233. /* interrupt sense values coming from the device tree equal either
  234. * EDGE_FALLING or EDGE_BOTH
  235. */
  236. *out_hwirq = intspec[0];
  237. *out_flags = intspec[1];
  238. return 0;
  239. }
  240. static struct irq_host_ops mpc8xxx_gpio_irq_ops = {
  241. .map = mpc8xxx_gpio_irq_map,
  242. .xlate = mpc8xxx_gpio_irq_xlate,
  243. };
  244. static struct of_device_id mpc8xxx_gpio_ids[] __initdata = {
  245. { .compatible = "fsl,mpc8349-gpio", },
  246. { .compatible = "fsl,mpc8572-gpio", },
  247. { .compatible = "fsl,mpc8610-gpio", },
  248. { .compatible = "fsl,mpc5121-gpio", .data = mpc512x_irq_set_type, },
  249. { .compatible = "fsl,pq3-gpio", },
  250. { .compatible = "fsl,qoriq-gpio", },
  251. {}
  252. };
  253. static void __init mpc8xxx_add_controller(struct device_node *np)
  254. {
  255. struct mpc8xxx_gpio_chip *mpc8xxx_gc;
  256. struct of_mm_gpio_chip *mm_gc;
  257. struct gpio_chip *gc;
  258. const struct of_device_id *id;
  259. unsigned hwirq;
  260. int ret;
  261. mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
  262. if (!mpc8xxx_gc) {
  263. ret = -ENOMEM;
  264. goto err;
  265. }
  266. spin_lock_init(&mpc8xxx_gc->lock);
  267. mm_gc = &mpc8xxx_gc->mm_gc;
  268. gc = &mm_gc->gc;
  269. mm_gc->save_regs = mpc8xxx_gpio_save_regs;
  270. gc->ngpio = MPC8XXX_GPIO_PINS;
  271. gc->direction_input = mpc8xxx_gpio_dir_in;
  272. gc->direction_output = mpc8xxx_gpio_dir_out;
  273. if (of_device_is_compatible(np, "fsl,mpc8572-gpio"))
  274. gc->get = mpc8572_gpio_get;
  275. else
  276. gc->get = mpc8xxx_gpio_get;
  277. gc->set = mpc8xxx_gpio_set;
  278. gc->to_irq = mpc8xxx_gpio_to_irq;
  279. ret = of_mm_gpiochip_add(np, mm_gc);
  280. if (ret)
  281. goto err;
  282. hwirq = irq_of_parse_and_map(np, 0);
  283. if (hwirq == NO_IRQ)
  284. goto skip_irq;
  285. mpc8xxx_gc->irq =
  286. irq_alloc_host(np, IRQ_HOST_MAP_LINEAR, MPC8XXX_GPIO_PINS,
  287. &mpc8xxx_gpio_irq_ops, MPC8XXX_GPIO_PINS);
  288. if (!mpc8xxx_gc->irq)
  289. goto skip_irq;
  290. id = of_match_node(mpc8xxx_gpio_ids, np);
  291. if (id)
  292. mpc8xxx_gc->of_dev_id_data = id->data;
  293. mpc8xxx_gc->irq->host_data = mpc8xxx_gc;
  294. /* ack and mask all irqs */
  295. out_be32(mm_gc->regs + GPIO_IER, 0xffffffff);
  296. out_be32(mm_gc->regs + GPIO_IMR, 0);
  297. irq_set_handler_data(hwirq, mpc8xxx_gc);
  298. irq_set_chained_handler(hwirq, mpc8xxx_gpio_irq_cascade);
  299. skip_irq:
  300. return;
  301. err:
  302. pr_err("%s: registration failed with status %d\n",
  303. np->full_name, ret);
  304. kfree(mpc8xxx_gc);
  305. return;
  306. }
  307. static int __init mpc8xxx_add_gpiochips(void)
  308. {
  309. struct device_node *np;
  310. for_each_matching_node(np, mpc8xxx_gpio_ids)
  311. mpc8xxx_add_controller(np);
  312. return 0;
  313. }
  314. arch_initcall(mpc8xxx_add_gpiochips);