gpio-mpc8xxx.c 10 KB

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