pl061.c 8.8 KB

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