gpio-ixp4xx.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // IXP4 GPIO driver
  4. // Copyright (C) 2019 Linus Walleij <[email protected]>
  5. //
  6. // based on previous work and know-how from:
  7. // Deepak Saxena <[email protected]>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/io.h>
  10. #include <linux/irq.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/irqchip.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/bitops.h>
  16. #define IXP4XX_REG_GPOUT 0x00
  17. #define IXP4XX_REG_GPOE 0x04
  18. #define IXP4XX_REG_GPIN 0x08
  19. #define IXP4XX_REG_GPIS 0x0C
  20. #define IXP4XX_REG_GPIT1 0x10
  21. #define IXP4XX_REG_GPIT2 0x14
  22. #define IXP4XX_REG_GPCLK 0x18
  23. #define IXP4XX_REG_GPDBSEL 0x1C
  24. /*
  25. * The hardware uses 3 bits to indicate interrupt "style".
  26. * we clear and set these three bits accordingly. The lower 24
  27. * bits in two registers (GPIT1 and GPIT2) are used to set up
  28. * the style for 8 lines each for a total of 16 GPIO lines.
  29. */
  30. #define IXP4XX_GPIO_STYLE_ACTIVE_HIGH 0x0
  31. #define IXP4XX_GPIO_STYLE_ACTIVE_LOW 0x1
  32. #define IXP4XX_GPIO_STYLE_RISING_EDGE 0x2
  33. #define IXP4XX_GPIO_STYLE_FALLING_EDGE 0x3
  34. #define IXP4XX_GPIO_STYLE_TRANSITIONAL 0x4
  35. #define IXP4XX_GPIO_STYLE_MASK GENMASK(2, 0)
  36. #define IXP4XX_GPIO_STYLE_SIZE 3
  37. /**
  38. * struct ixp4xx_gpio - IXP4 GPIO state container
  39. * @dev: containing device for this instance
  40. * @fwnode: the fwnode for this GPIO chip
  41. * @gc: gpiochip for this instance
  42. * @base: remapped I/O-memory base
  43. * @irq_edge: Each bit represents an IRQ: 1: edge-triggered,
  44. * 0: level triggered
  45. */
  46. struct ixp4xx_gpio {
  47. struct device *dev;
  48. struct fwnode_handle *fwnode;
  49. struct gpio_chip gc;
  50. void __iomem *base;
  51. unsigned long long irq_edge;
  52. };
  53. static void ixp4xx_gpio_irq_ack(struct irq_data *d)
  54. {
  55. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  56. struct ixp4xx_gpio *g = gpiochip_get_data(gc);
  57. __raw_writel(BIT(d->hwirq), g->base + IXP4XX_REG_GPIS);
  58. }
  59. static void ixp4xx_gpio_mask_irq(struct irq_data *d)
  60. {
  61. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  62. irq_chip_mask_parent(d);
  63. gpiochip_disable_irq(gc, d->hwirq);
  64. }
  65. static void ixp4xx_gpio_irq_unmask(struct irq_data *d)
  66. {
  67. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  68. struct ixp4xx_gpio *g = gpiochip_get_data(gc);
  69. /* ACK when unmasking if not edge-triggered */
  70. if (!(g->irq_edge & BIT(d->hwirq)))
  71. ixp4xx_gpio_irq_ack(d);
  72. gpiochip_enable_irq(gc, d->hwirq);
  73. irq_chip_unmask_parent(d);
  74. }
  75. static int ixp4xx_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  76. {
  77. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  78. struct ixp4xx_gpio *g = gpiochip_get_data(gc);
  79. int line = d->hwirq;
  80. unsigned long flags;
  81. u32 int_style;
  82. u32 int_reg;
  83. u32 val;
  84. switch (type) {
  85. case IRQ_TYPE_EDGE_BOTH:
  86. irq_set_handler_locked(d, handle_edge_irq);
  87. int_style = IXP4XX_GPIO_STYLE_TRANSITIONAL;
  88. g->irq_edge |= BIT(d->hwirq);
  89. break;
  90. case IRQ_TYPE_EDGE_RISING:
  91. irq_set_handler_locked(d, handle_edge_irq);
  92. int_style = IXP4XX_GPIO_STYLE_RISING_EDGE;
  93. g->irq_edge |= BIT(d->hwirq);
  94. break;
  95. case IRQ_TYPE_EDGE_FALLING:
  96. irq_set_handler_locked(d, handle_edge_irq);
  97. int_style = IXP4XX_GPIO_STYLE_FALLING_EDGE;
  98. g->irq_edge |= BIT(d->hwirq);
  99. break;
  100. case IRQ_TYPE_LEVEL_HIGH:
  101. irq_set_handler_locked(d, handle_level_irq);
  102. int_style = IXP4XX_GPIO_STYLE_ACTIVE_HIGH;
  103. g->irq_edge &= ~BIT(d->hwirq);
  104. break;
  105. case IRQ_TYPE_LEVEL_LOW:
  106. irq_set_handler_locked(d, handle_level_irq);
  107. int_style = IXP4XX_GPIO_STYLE_ACTIVE_LOW;
  108. g->irq_edge &= ~BIT(d->hwirq);
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. if (line >= 8) {
  114. /* pins 8-15 */
  115. line -= 8;
  116. int_reg = IXP4XX_REG_GPIT2;
  117. } else {
  118. /* pins 0-7 */
  119. int_reg = IXP4XX_REG_GPIT1;
  120. }
  121. raw_spin_lock_irqsave(&g->gc.bgpio_lock, flags);
  122. /* Clear the style for the appropriate pin */
  123. val = __raw_readl(g->base + int_reg);
  124. val &= ~(IXP4XX_GPIO_STYLE_MASK << (line * IXP4XX_GPIO_STYLE_SIZE));
  125. __raw_writel(val, g->base + int_reg);
  126. __raw_writel(BIT(line), g->base + IXP4XX_REG_GPIS);
  127. /* Set the new style */
  128. val = __raw_readl(g->base + int_reg);
  129. val |= (int_style << (line * IXP4XX_GPIO_STYLE_SIZE));
  130. __raw_writel(val, g->base + int_reg);
  131. /* Force-configure this line as an input */
  132. val = __raw_readl(g->base + IXP4XX_REG_GPOE);
  133. val |= BIT(d->hwirq);
  134. __raw_writel(val, g->base + IXP4XX_REG_GPOE);
  135. raw_spin_unlock_irqrestore(&g->gc.bgpio_lock, flags);
  136. /* This parent only accept level high (asserted) */
  137. return irq_chip_set_type_parent(d, IRQ_TYPE_LEVEL_HIGH);
  138. }
  139. static const struct irq_chip ixp4xx_gpio_irqchip = {
  140. .name = "IXP4GPIO",
  141. .irq_ack = ixp4xx_gpio_irq_ack,
  142. .irq_mask = ixp4xx_gpio_mask_irq,
  143. .irq_unmask = ixp4xx_gpio_irq_unmask,
  144. .irq_set_type = ixp4xx_gpio_irq_set_type,
  145. .flags = IRQCHIP_IMMUTABLE,
  146. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  147. };
  148. static int ixp4xx_gpio_child_to_parent_hwirq(struct gpio_chip *gc,
  149. unsigned int child,
  150. unsigned int child_type,
  151. unsigned int *parent,
  152. unsigned int *parent_type)
  153. {
  154. /* All these interrupts are level high in the CPU */
  155. *parent_type = IRQ_TYPE_LEVEL_HIGH;
  156. /* GPIO lines 0..12 have dedicated IRQs */
  157. if (child == 0) {
  158. *parent = 6;
  159. return 0;
  160. }
  161. if (child == 1) {
  162. *parent = 7;
  163. return 0;
  164. }
  165. if (child >= 2 && child <= 12) {
  166. *parent = child + 17;
  167. return 0;
  168. }
  169. return -EINVAL;
  170. }
  171. static int ixp4xx_gpio_probe(struct platform_device *pdev)
  172. {
  173. unsigned long flags;
  174. struct device *dev = &pdev->dev;
  175. struct device_node *np = dev->of_node;
  176. struct irq_domain *parent;
  177. struct resource *res;
  178. struct ixp4xx_gpio *g;
  179. struct gpio_irq_chip *girq;
  180. struct device_node *irq_parent;
  181. int ret;
  182. g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL);
  183. if (!g)
  184. return -ENOMEM;
  185. g->dev = dev;
  186. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  187. g->base = devm_ioremap_resource(dev, res);
  188. if (IS_ERR(g->base))
  189. return PTR_ERR(g->base);
  190. irq_parent = of_irq_find_parent(np);
  191. if (!irq_parent) {
  192. dev_err(dev, "no IRQ parent node\n");
  193. return -ENODEV;
  194. }
  195. parent = irq_find_host(irq_parent);
  196. if (!parent) {
  197. dev_err(dev, "no IRQ parent domain\n");
  198. return -ENODEV;
  199. }
  200. g->fwnode = of_node_to_fwnode(np);
  201. /*
  202. * Make sure GPIO 14 and 15 are NOT used as clocks but GPIO on
  203. * specific machines.
  204. */
  205. if (of_machine_is_compatible("dlink,dsm-g600-a") ||
  206. of_machine_is_compatible("iom,nas-100d"))
  207. __raw_writel(0x0, g->base + IXP4XX_REG_GPCLK);
  208. /*
  209. * This is a very special big-endian ARM issue: when the IXP4xx is
  210. * run in big endian mode, all registers in the machine are switched
  211. * around to the CPU-native endianness. As you see mostly in the
  212. * driver we use __raw_readl()/__raw_writel() to access the registers
  213. * in the appropriate order. With the GPIO library we need to specify
  214. * byte order explicitly, so this flag needs to be set when compiling
  215. * for big endian.
  216. */
  217. #if defined(CONFIG_CPU_BIG_ENDIAN)
  218. flags = BGPIOF_BIG_ENDIAN_BYTE_ORDER;
  219. #else
  220. flags = 0;
  221. #endif
  222. /* Populate and register gpio chip */
  223. ret = bgpio_init(&g->gc, dev, 4,
  224. g->base + IXP4XX_REG_GPIN,
  225. g->base + IXP4XX_REG_GPOUT,
  226. NULL,
  227. NULL,
  228. g->base + IXP4XX_REG_GPOE,
  229. flags);
  230. if (ret) {
  231. dev_err(dev, "unable to init generic GPIO\n");
  232. return ret;
  233. }
  234. g->gc.ngpio = 16;
  235. g->gc.label = "IXP4XX_GPIO_CHIP";
  236. /*
  237. * TODO: when we have migrated to device tree and all GPIOs
  238. * are fetched using phandles, set this to -1 to get rid of
  239. * the fixed gpiochip base.
  240. */
  241. g->gc.base = 0;
  242. g->gc.parent = &pdev->dev;
  243. g->gc.owner = THIS_MODULE;
  244. girq = &g->gc.irq;
  245. gpio_irq_chip_set_chip(girq, &ixp4xx_gpio_irqchip);
  246. girq->fwnode = g->fwnode;
  247. girq->parent_domain = parent;
  248. girq->child_to_parent_hwirq = ixp4xx_gpio_child_to_parent_hwirq;
  249. girq->handler = handle_bad_irq;
  250. girq->default_type = IRQ_TYPE_NONE;
  251. ret = devm_gpiochip_add_data(dev, &g->gc, g);
  252. if (ret) {
  253. dev_err(dev, "failed to add SoC gpiochip\n");
  254. return ret;
  255. }
  256. platform_set_drvdata(pdev, g);
  257. dev_info(dev, "IXP4 GPIO registered\n");
  258. return 0;
  259. }
  260. static const struct of_device_id ixp4xx_gpio_of_match[] = {
  261. {
  262. .compatible = "intel,ixp4xx-gpio",
  263. },
  264. {},
  265. };
  266. static struct platform_driver ixp4xx_gpio_driver = {
  267. .driver = {
  268. .name = "ixp4xx-gpio",
  269. .of_match_table = of_match_ptr(ixp4xx_gpio_of_match),
  270. },
  271. .probe = ixp4xx_gpio_probe,
  272. };
  273. builtin_platform_driver(ixp4xx_gpio_driver);