irq-meson-gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2015 Endless Mobile, Inc.
  3. * Author: Carlo Caione <carlo@endlessm.com>
  4. * Copyright (c) 2016 BayLibre, SAS.
  5. * Author: Jerome Brunet <jbrunet@baylibre.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. * The full GNU General Public License is included in this distribution
  19. * in the file called COPYING.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/irq.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/irqchip.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #define NUM_CHANNEL 8
  30. #define MAX_INPUT_MUX 256
  31. #define REG_EDGE_POL 0x00
  32. #define REG_PIN_03_SEL 0x04
  33. #define REG_PIN_47_SEL 0x08
  34. #define REG_FILTER_SEL 0x0c
  35. #define REG_EDGE_POL_MASK(x) (BIT(x) | BIT(16 + (x)))
  36. #define REG_EDGE_POL_EDGE(x) BIT(x)
  37. #define REG_EDGE_POL_LOW(x) BIT(16 + (x))
  38. #define REG_PIN_SEL_SHIFT(x) (((x) % 4) * 8)
  39. #define REG_FILTER_SEL_SHIFT(x) ((x) * 4)
  40. struct meson_gpio_irq_params {
  41. unsigned int nr_hwirq;
  42. };
  43. static const struct meson_gpio_irq_params meson8_params = {
  44. .nr_hwirq = 134,
  45. };
  46. static const struct meson_gpio_irq_params meson8b_params = {
  47. .nr_hwirq = 119,
  48. };
  49. static const struct meson_gpio_irq_params gxbb_params = {
  50. .nr_hwirq = 133,
  51. };
  52. static const struct meson_gpio_irq_params gxl_params = {
  53. .nr_hwirq = 110,
  54. };
  55. static const struct of_device_id meson_irq_gpio_matches[] = {
  56. { .compatible = "amlogic,meson8-gpio-intc", .data = &meson8_params },
  57. { .compatible = "amlogic,meson8b-gpio-intc", .data = &meson8b_params },
  58. { .compatible = "amlogic,meson-gxbb-gpio-intc", .data = &gxbb_params },
  59. { .compatible = "amlogic,meson-gxl-gpio-intc", .data = &gxl_params },
  60. { }
  61. };
  62. struct meson_gpio_irq_controller {
  63. unsigned int nr_hwirq;
  64. void __iomem *base;
  65. u32 channel_irqs[NUM_CHANNEL];
  66. DECLARE_BITMAP(channel_map, NUM_CHANNEL);
  67. spinlock_t lock;
  68. };
  69. static void meson_gpio_irq_update_bits(struct meson_gpio_irq_controller *ctl,
  70. unsigned int reg, u32 mask, u32 val)
  71. {
  72. u32 tmp;
  73. tmp = readl_relaxed(ctl->base + reg);
  74. tmp &= ~mask;
  75. tmp |= val;
  76. writel_relaxed(tmp, ctl->base + reg);
  77. }
  78. static unsigned int meson_gpio_irq_channel_to_reg(unsigned int channel)
  79. {
  80. return (channel < 4) ? REG_PIN_03_SEL : REG_PIN_47_SEL;
  81. }
  82. static int
  83. meson_gpio_irq_request_channel(struct meson_gpio_irq_controller *ctl,
  84. unsigned long hwirq,
  85. u32 **channel_hwirq)
  86. {
  87. unsigned int reg, idx;
  88. spin_lock(&ctl->lock);
  89. /* Find a free channel */
  90. idx = find_first_zero_bit(ctl->channel_map, NUM_CHANNEL);
  91. if (idx >= NUM_CHANNEL) {
  92. spin_unlock(&ctl->lock);
  93. pr_err("No channel available\n");
  94. return -ENOSPC;
  95. }
  96. /* Mark the channel as used */
  97. set_bit(idx, ctl->channel_map);
  98. /*
  99. * Setup the mux of the channel to route the signal of the pad
  100. * to the appropriate input of the GIC
  101. */
  102. reg = meson_gpio_irq_channel_to_reg(idx);
  103. meson_gpio_irq_update_bits(ctl, reg,
  104. 0xff << REG_PIN_SEL_SHIFT(idx),
  105. hwirq << REG_PIN_SEL_SHIFT(idx));
  106. /*
  107. * Get the hwirq number assigned to this channel through
  108. * a pointer the channel_irq table. The added benifit of this
  109. * method is that we can also retrieve the channel index with
  110. * it, using the table base.
  111. */
  112. *channel_hwirq = &(ctl->channel_irqs[idx]);
  113. spin_unlock(&ctl->lock);
  114. pr_debug("hwirq %lu assigned to channel %d - irq %u\n",
  115. hwirq, idx, **channel_hwirq);
  116. return 0;
  117. }
  118. static unsigned int
  119. meson_gpio_irq_get_channel_idx(struct meson_gpio_irq_controller *ctl,
  120. u32 *channel_hwirq)
  121. {
  122. return channel_hwirq - ctl->channel_irqs;
  123. }
  124. static void
  125. meson_gpio_irq_release_channel(struct meson_gpio_irq_controller *ctl,
  126. u32 *channel_hwirq)
  127. {
  128. unsigned int idx;
  129. idx = meson_gpio_irq_get_channel_idx(ctl, channel_hwirq);
  130. clear_bit(idx, ctl->channel_map);
  131. }
  132. static int meson_gpio_irq_type_setup(struct meson_gpio_irq_controller *ctl,
  133. unsigned int type,
  134. u32 *channel_hwirq)
  135. {
  136. u32 val = 0;
  137. unsigned int idx;
  138. idx = meson_gpio_irq_get_channel_idx(ctl, channel_hwirq);
  139. /*
  140. * The controller has a filter block to operate in either LEVEL or
  141. * EDGE mode, then signal is sent to the GIC. To enable LEVEL_LOW and
  142. * EDGE_FALLING support (which the GIC does not support), the filter
  143. * block is also able to invert the input signal it gets before
  144. * providing it to the GIC.
  145. */
  146. type &= IRQ_TYPE_SENSE_MASK;
  147. if (type == IRQ_TYPE_EDGE_BOTH)
  148. return -EINVAL;
  149. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
  150. val |= REG_EDGE_POL_EDGE(idx);
  151. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING))
  152. val |= REG_EDGE_POL_LOW(idx);
  153. spin_lock(&ctl->lock);
  154. meson_gpio_irq_update_bits(ctl, REG_EDGE_POL,
  155. REG_EDGE_POL_MASK(idx), val);
  156. spin_unlock(&ctl->lock);
  157. return 0;
  158. }
  159. static unsigned int meson_gpio_irq_type_output(unsigned int type)
  160. {
  161. unsigned int sense = type & IRQ_TYPE_SENSE_MASK;
  162. type &= ~IRQ_TYPE_SENSE_MASK;
  163. /*
  164. * The polarity of the signal provided to the GIC should always
  165. * be high.
  166. */
  167. if (sense & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  168. type |= IRQ_TYPE_LEVEL_HIGH;
  169. else if (sense & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
  170. type |= IRQ_TYPE_EDGE_RISING;
  171. return type;
  172. }
  173. static int meson_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  174. {
  175. struct meson_gpio_irq_controller *ctl = data->domain->host_data;
  176. u32 *channel_hwirq = irq_data_get_irq_chip_data(data);
  177. int ret;
  178. ret = meson_gpio_irq_type_setup(ctl, type, channel_hwirq);
  179. if (ret)
  180. return ret;
  181. return irq_chip_set_type_parent(data,
  182. meson_gpio_irq_type_output(type));
  183. }
  184. static struct irq_chip meson_gpio_irq_chip = {
  185. .name = "meson-gpio-irqchip",
  186. .irq_mask = irq_chip_mask_parent,
  187. .irq_unmask = irq_chip_unmask_parent,
  188. .irq_eoi = irq_chip_eoi_parent,
  189. .irq_set_type = meson_gpio_irq_set_type,
  190. .irq_retrigger = irq_chip_retrigger_hierarchy,
  191. #ifdef CONFIG_SMP
  192. .irq_set_affinity = irq_chip_set_affinity_parent,
  193. #endif
  194. .flags = IRQCHIP_SET_TYPE_MASKED,
  195. };
  196. static int meson_gpio_irq_domain_translate(struct irq_domain *domain,
  197. struct irq_fwspec *fwspec,
  198. unsigned long *hwirq,
  199. unsigned int *type)
  200. {
  201. if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
  202. *hwirq = fwspec->param[0];
  203. *type = fwspec->param[1];
  204. return 0;
  205. }
  206. return -EINVAL;
  207. }
  208. static int meson_gpio_irq_allocate_gic_irq(struct irq_domain *domain,
  209. unsigned int virq,
  210. u32 hwirq,
  211. unsigned int type)
  212. {
  213. struct irq_fwspec fwspec;
  214. fwspec.fwnode = domain->parent->fwnode;
  215. fwspec.param_count = 3;
  216. fwspec.param[0] = 0; /* SPI */
  217. fwspec.param[1] = hwirq;
  218. fwspec.param[2] = meson_gpio_irq_type_output(type);
  219. return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  220. }
  221. static int meson_gpio_irq_domain_alloc(struct irq_domain *domain,
  222. unsigned int virq,
  223. unsigned int nr_irqs,
  224. void *data)
  225. {
  226. struct irq_fwspec *fwspec = data;
  227. struct meson_gpio_irq_controller *ctl = domain->host_data;
  228. unsigned long hwirq;
  229. u32 *channel_hwirq;
  230. unsigned int type;
  231. int ret;
  232. if (WARN_ON(nr_irqs != 1))
  233. return -EINVAL;
  234. ret = meson_gpio_irq_domain_translate(domain, fwspec, &hwirq, &type);
  235. if (ret)
  236. return ret;
  237. ret = meson_gpio_irq_request_channel(ctl, hwirq, &channel_hwirq);
  238. if (ret)
  239. return ret;
  240. ret = meson_gpio_irq_allocate_gic_irq(domain, virq,
  241. *channel_hwirq, type);
  242. if (ret < 0) {
  243. pr_err("failed to allocate gic irq %u\n", *channel_hwirq);
  244. meson_gpio_irq_release_channel(ctl, channel_hwirq);
  245. return ret;
  246. }
  247. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  248. &meson_gpio_irq_chip, channel_hwirq);
  249. return 0;
  250. }
  251. static void meson_gpio_irq_domain_free(struct irq_domain *domain,
  252. unsigned int virq,
  253. unsigned int nr_irqs)
  254. {
  255. struct meson_gpio_irq_controller *ctl = domain->host_data;
  256. struct irq_data *irq_data;
  257. u32 *channel_hwirq;
  258. if (WARN_ON(nr_irqs != 1))
  259. return;
  260. irq_domain_free_irqs_parent(domain, virq, 1);
  261. irq_data = irq_domain_get_irq_data(domain, virq);
  262. channel_hwirq = irq_data_get_irq_chip_data(irq_data);
  263. meson_gpio_irq_release_channel(ctl, channel_hwirq);
  264. }
  265. static const struct irq_domain_ops meson_gpio_irq_domain_ops = {
  266. .alloc = meson_gpio_irq_domain_alloc,
  267. .free = meson_gpio_irq_domain_free,
  268. .translate = meson_gpio_irq_domain_translate,
  269. };
  270. static int __init meson_gpio_irq_parse_dt(struct device_node *node,
  271. struct meson_gpio_irq_controller *ctl)
  272. {
  273. const struct of_device_id *match;
  274. const struct meson_gpio_irq_params *params;
  275. int ret;
  276. match = of_match_node(meson_irq_gpio_matches, node);
  277. if (!match)
  278. return -ENODEV;
  279. params = match->data;
  280. ctl->nr_hwirq = params->nr_hwirq;
  281. ret = of_property_read_variable_u32_array(node,
  282. "amlogic,channel-interrupts",
  283. ctl->channel_irqs,
  284. NUM_CHANNEL,
  285. NUM_CHANNEL);
  286. if (ret < 0) {
  287. pr_err("can't get %d channel interrupts\n", NUM_CHANNEL);
  288. return ret;
  289. }
  290. return 0;
  291. }
  292. static int __init meson_gpio_irq_of_init(struct device_node *node,
  293. struct device_node *parent)
  294. {
  295. struct irq_domain *domain, *parent_domain;
  296. struct meson_gpio_irq_controller *ctl;
  297. int ret;
  298. if (!parent) {
  299. pr_err("missing parent interrupt node\n");
  300. return -ENODEV;
  301. }
  302. parent_domain = irq_find_host(parent);
  303. if (!parent_domain) {
  304. pr_err("unable to obtain parent domain\n");
  305. return -ENXIO;
  306. }
  307. ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
  308. if (!ctl)
  309. return -ENOMEM;
  310. spin_lock_init(&ctl->lock);
  311. ctl->base = of_iomap(node, 0);
  312. if (!ctl->base) {
  313. ret = -ENOMEM;
  314. goto free_ctl;
  315. }
  316. ret = meson_gpio_irq_parse_dt(node, ctl);
  317. if (ret)
  318. goto free_channel_irqs;
  319. domain = irq_domain_create_hierarchy(parent_domain, 0, ctl->nr_hwirq,
  320. of_node_to_fwnode(node),
  321. &meson_gpio_irq_domain_ops,
  322. ctl);
  323. if (!domain) {
  324. pr_err("failed to add domain\n");
  325. ret = -ENODEV;
  326. goto free_channel_irqs;
  327. }
  328. pr_info("%d to %d gpio interrupt mux initialized\n",
  329. ctl->nr_hwirq, NUM_CHANNEL);
  330. return 0;
  331. free_channel_irqs:
  332. iounmap(ctl->base);
  333. free_ctl:
  334. kfree(ctl);
  335. return ret;
  336. }
  337. IRQCHIP_DECLARE(meson_gpio_intc, "amlogic,meson-gpio-intc",
  338. meson_gpio_irq_of_init);