pl061.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * linux/drivers/gpio/pl061.c
  3. *
  4. * Copyright (C) 2008, 2009 Provigent Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Driver for the ARM PrimeCell(tm) General Purpose Input/Output (PL061)
  11. *
  12. * Data sheet: ARM DDI 0190B, September 2000
  13. */
  14. #include <linux/spinlock.h>
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/list.h>
  18. #include <linux/io.h>
  19. #include <linux/ioport.h>
  20. #include <linux/irq.h>
  21. #include <linux/bitops.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/gpio.h>
  24. #include <linux/device.h>
  25. #include <linux/amba/bus.h>
  26. #include <linux/amba/pl061.h>
  27. #define GPIODIR 0x400
  28. #define GPIOIS 0x404
  29. #define GPIOIBE 0x408
  30. #define GPIOIEV 0x40C
  31. #define GPIOIE 0x410
  32. #define GPIORIS 0x414
  33. #define GPIOMIS 0x418
  34. #define GPIOIC 0x41C
  35. #define PL061_GPIO_NR 8
  36. struct pl061_gpio {
  37. /* We use a list of pl061_gpio structs for each trigger IRQ in the main
  38. * interrupts controller of the system. We need this to support systems
  39. * in which more that one PL061s are connected to the same IRQ. The ISR
  40. * interates through this list to find the source of the interrupt.
  41. */
  42. struct list_head list;
  43. /* Each of the two spinlocks protects a different set of hardware
  44. * regiters and data structurs. This decouples the code of the IRQ from
  45. * the GPIO code. This also makes the case of a GPIO routine call from
  46. * the IRQ code simpler.
  47. */
  48. spinlock_t lock; /* GPIO registers */
  49. spinlock_t irq_lock; /* IRQ registers */
  50. void __iomem *base;
  51. unsigned irq_base;
  52. struct gpio_chip gc;
  53. };
  54. static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
  55. {
  56. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  57. unsigned long flags;
  58. unsigned char gpiodir;
  59. if (offset >= gc->ngpio)
  60. return -EINVAL;
  61. spin_lock_irqsave(&chip->lock, flags);
  62. gpiodir = readb(chip->base + GPIODIR);
  63. gpiodir &= ~(1 << offset);
  64. writeb(gpiodir, chip->base + GPIODIR);
  65. spin_unlock_irqrestore(&chip->lock, flags);
  66. return 0;
  67. }
  68. static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
  69. int value)
  70. {
  71. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  72. unsigned long flags;
  73. unsigned char gpiodir;
  74. if (offset >= gc->ngpio)
  75. return -EINVAL;
  76. spin_lock_irqsave(&chip->lock, flags);
  77. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  78. gpiodir = readb(chip->base + GPIODIR);
  79. gpiodir |= 1 << offset;
  80. writeb(gpiodir, chip->base + GPIODIR);
  81. spin_unlock_irqrestore(&chip->lock, flags);
  82. return 0;
  83. }
  84. static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
  85. {
  86. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  87. return !!readb(chip->base + (1 << (offset + 2)));
  88. }
  89. static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
  90. {
  91. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  92. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  93. }
  94. static int pl061_to_irq(struct gpio_chip *gc, unsigned offset)
  95. {
  96. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  97. if (chip->irq_base == (unsigned) -1)
  98. return -EINVAL;
  99. return chip->irq_base + offset;
  100. }
  101. /*
  102. * PL061 GPIO IRQ
  103. */
  104. static void pl061_irq_disable(unsigned irq)
  105. {
  106. struct pl061_gpio *chip = get_irq_chip_data(irq);
  107. int offset = irq - chip->irq_base;
  108. unsigned long flags;
  109. u8 gpioie;
  110. spin_lock_irqsave(&chip->irq_lock, flags);
  111. gpioie = readb(chip->base + GPIOIE);
  112. gpioie &= ~(1 << offset);
  113. writeb(gpioie, chip->base + GPIOIE);
  114. spin_unlock_irqrestore(&chip->irq_lock, flags);
  115. }
  116. static void pl061_irq_enable(unsigned irq)
  117. {
  118. struct pl061_gpio *chip = get_irq_chip_data(irq);
  119. int offset = irq - chip->irq_base;
  120. unsigned long flags;
  121. u8 gpioie;
  122. spin_lock_irqsave(&chip->irq_lock, flags);
  123. gpioie = readb(chip->base + GPIOIE);
  124. gpioie |= 1 << offset;
  125. writeb(gpioie, chip->base + GPIOIE);
  126. spin_unlock_irqrestore(&chip->irq_lock, flags);
  127. }
  128. static int pl061_irq_type(unsigned irq, unsigned trigger)
  129. {
  130. struct pl061_gpio *chip = get_irq_chip_data(irq);
  131. int offset = irq - chip->irq_base;
  132. unsigned long flags;
  133. u8 gpiois, gpioibe, gpioiev;
  134. if (offset < 0 || offset > PL061_GPIO_NR)
  135. return -EINVAL;
  136. spin_lock_irqsave(&chip->irq_lock, flags);
  137. gpioiev = readb(chip->base + GPIOIEV);
  138. gpiois = readb(chip->base + GPIOIS);
  139. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  140. gpiois |= 1 << offset;
  141. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  142. gpioiev |= 1 << offset;
  143. else
  144. gpioiev &= ~(1 << offset);
  145. } else
  146. gpiois &= ~(1 << offset);
  147. writeb(gpiois, chip->base + GPIOIS);
  148. gpioibe = readb(chip->base + GPIOIBE);
  149. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  150. gpioibe |= 1 << offset;
  151. else {
  152. gpioibe &= ~(1 << offset);
  153. if (trigger & IRQ_TYPE_EDGE_RISING)
  154. gpioiev |= 1 << offset;
  155. else
  156. gpioiev &= ~(1 << offset);
  157. }
  158. writeb(gpioibe, chip->base + GPIOIBE);
  159. writeb(gpioiev, chip->base + GPIOIEV);
  160. spin_unlock_irqrestore(&chip->irq_lock, flags);
  161. return 0;
  162. }
  163. static struct irq_chip pl061_irqchip = {
  164. .name = "GPIO",
  165. .enable = pl061_irq_enable,
  166. .disable = pl061_irq_disable,
  167. .set_type = pl061_irq_type,
  168. };
  169. static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
  170. {
  171. struct list_head *chip_list = get_irq_chip_data(irq);
  172. struct list_head *ptr;
  173. struct pl061_gpio *chip;
  174. desc->chip->ack(irq);
  175. list_for_each(ptr, chip_list) {
  176. unsigned long pending;
  177. int offset;
  178. chip = list_entry(ptr, struct pl061_gpio, list);
  179. pending = readb(chip->base + GPIOMIS);
  180. writeb(pending, chip->base + GPIOIC);
  181. if (pending == 0)
  182. continue;
  183. for_each_bit(offset, &pending, PL061_GPIO_NR)
  184. generic_handle_irq(pl061_to_irq(&chip->gc, offset));
  185. }
  186. desc->chip->unmask(irq);
  187. }
  188. static int __init pl061_probe(struct amba_device *dev, struct amba_id *id)
  189. {
  190. struct pl061_platform_data *pdata;
  191. struct pl061_gpio *chip;
  192. struct list_head *chip_list;
  193. int ret, irq, i;
  194. static DECLARE_BITMAP(init_irq, NR_IRQS);
  195. pdata = dev->dev.platform_data;
  196. if (pdata == NULL)
  197. return -ENODEV;
  198. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  199. if (chip == NULL)
  200. return -ENOMEM;
  201. if (!request_mem_region(dev->res.start,
  202. resource_size(&dev->res), "pl061")) {
  203. ret = -EBUSY;
  204. goto free_mem;
  205. }
  206. chip->base = ioremap(dev->res.start, resource_size(&dev->res));
  207. if (chip->base == NULL) {
  208. ret = -ENOMEM;
  209. goto release_region;
  210. }
  211. spin_lock_init(&chip->lock);
  212. spin_lock_init(&chip->irq_lock);
  213. INIT_LIST_HEAD(&chip->list);
  214. chip->gc.direction_input = pl061_direction_input;
  215. chip->gc.direction_output = pl061_direction_output;
  216. chip->gc.get = pl061_get_value;
  217. chip->gc.set = pl061_set_value;
  218. chip->gc.to_irq = pl061_to_irq;
  219. chip->gc.base = pdata->gpio_base;
  220. chip->gc.ngpio = PL061_GPIO_NR;
  221. chip->gc.label = dev_name(&dev->dev);
  222. chip->gc.dev = &dev->dev;
  223. chip->gc.owner = THIS_MODULE;
  224. chip->irq_base = pdata->irq_base;
  225. ret = gpiochip_add(&chip->gc);
  226. if (ret)
  227. goto iounmap;
  228. /*
  229. * irq_chip support
  230. */
  231. if (chip->irq_base == (unsigned) -1)
  232. return 0;
  233. writeb(0, chip->base + GPIOIE); /* disable irqs */
  234. irq = dev->irq[0];
  235. if (irq < 0) {
  236. ret = -ENODEV;
  237. goto iounmap;
  238. }
  239. set_irq_chained_handler(irq, pl061_irq_handler);
  240. if (!test_and_set_bit(irq, init_irq)) { /* list initialized? */
  241. chip_list = kmalloc(sizeof(*chip_list), GFP_KERNEL);
  242. if (chip_list == NULL) {
  243. clear_bit(irq, init_irq);
  244. ret = -ENOMEM;
  245. goto iounmap;
  246. }
  247. INIT_LIST_HEAD(chip_list);
  248. set_irq_chip_data(irq, chip_list);
  249. } else
  250. chip_list = get_irq_chip_data(irq);
  251. list_add(&chip->list, chip_list);
  252. for (i = 0; i < PL061_GPIO_NR; i++) {
  253. if (pdata->directions & (1 << i))
  254. pl061_direction_output(&chip->gc, i,
  255. pdata->values & (1 << i));
  256. else
  257. pl061_direction_input(&chip->gc, i);
  258. set_irq_chip(i+chip->irq_base, &pl061_irqchip);
  259. set_irq_handler(i+chip->irq_base, handle_simple_irq);
  260. set_irq_flags(i+chip->irq_base, IRQF_VALID);
  261. set_irq_chip_data(i+chip->irq_base, chip);
  262. }
  263. return 0;
  264. iounmap:
  265. iounmap(chip->base);
  266. release_region:
  267. release_mem_region(dev->res.start, resource_size(&dev->res));
  268. free_mem:
  269. kfree(chip);
  270. return ret;
  271. }
  272. static struct amba_id pl061_ids[] __initdata = {
  273. {
  274. .id = 0x00041061,
  275. .mask = 0x000fffff,
  276. },
  277. { 0, 0 },
  278. };
  279. static struct amba_driver pl061_gpio_driver = {
  280. .drv = {
  281. .name = "pl061_gpio",
  282. },
  283. .id_table = pl061_ids,
  284. .probe = pl061_probe,
  285. };
  286. static int __init pl061_gpio_init(void)
  287. {
  288. return amba_driver_register(&pl061_gpio_driver);
  289. }
  290. subsys_initcall(pl061_gpio_init);
  291. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  292. MODULE_DESCRIPTION("PL061 GPIO driver");
  293. MODULE_LICENSE("GPL");