irq-meson-gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 meson_gpio_irq_params axg_params = {
  56. .nr_hwirq = 100,
  57. };
  58. static const struct of_device_id meson_irq_gpio_matches[] = {
  59. { .compatible = "amlogic,meson8-gpio-intc", .data = &meson8_params },
  60. { .compatible = "amlogic,meson8b-gpio-intc", .data = &meson8b_params },
  61. { .compatible = "amlogic,meson-gxbb-gpio-intc", .data = &gxbb_params },
  62. { .compatible = "amlogic,meson-gxl-gpio-intc", .data = &gxl_params },
  63. { .compatible = "amlogic,meson-axg-gpio-intc", .data = &axg_params },
  64. { }
  65. };
  66. struct meson_gpio_irq_controller {
  67. unsigned int nr_hwirq;
  68. void __iomem *base;
  69. u32 channel_irqs[NUM_CHANNEL];
  70. DECLARE_BITMAP(channel_map, NUM_CHANNEL);
  71. spinlock_t lock;
  72. };
  73. static void meson_gpio_irq_update_bits(struct meson_gpio_irq_controller *ctl,
  74. unsigned int reg, u32 mask, u32 val)
  75. {
  76. u32 tmp;
  77. tmp = readl_relaxed(ctl->base + reg);
  78. tmp &= ~mask;
  79. tmp |= val;
  80. writel_relaxed(tmp, ctl->base + reg);
  81. }
  82. static unsigned int meson_gpio_irq_channel_to_reg(unsigned int channel)
  83. {
  84. return (channel < 4) ? REG_PIN_03_SEL : REG_PIN_47_SEL;
  85. }
  86. static int
  87. meson_gpio_irq_request_channel(struct meson_gpio_irq_controller *ctl,
  88. unsigned long hwirq,
  89. u32 **channel_hwirq)
  90. {
  91. unsigned int reg, idx;
  92. spin_lock(&ctl->lock);
  93. /* Find a free channel */
  94. idx = find_first_zero_bit(ctl->channel_map, NUM_CHANNEL);
  95. if (idx >= NUM_CHANNEL) {
  96. spin_unlock(&ctl->lock);
  97. pr_err("No channel available\n");
  98. return -ENOSPC;
  99. }
  100. /* Mark the channel as used */
  101. set_bit(idx, ctl->channel_map);
  102. /*
  103. * Setup the mux of the channel to route the signal of the pad
  104. * to the appropriate input of the GIC
  105. */
  106. reg = meson_gpio_irq_channel_to_reg(idx);
  107. meson_gpio_irq_update_bits(ctl, reg,
  108. 0xff << REG_PIN_SEL_SHIFT(idx),
  109. hwirq << REG_PIN_SEL_SHIFT(idx));
  110. /*
  111. * Get the hwirq number assigned to this channel through
  112. * a pointer the channel_irq table. The added benifit of this
  113. * method is that we can also retrieve the channel index with
  114. * it, using the table base.
  115. */
  116. *channel_hwirq = &(ctl->channel_irqs[idx]);
  117. spin_unlock(&ctl->lock);
  118. pr_debug("hwirq %lu assigned to channel %d - irq %u\n",
  119. hwirq, idx, **channel_hwirq);
  120. return 0;
  121. }
  122. static unsigned int
  123. meson_gpio_irq_get_channel_idx(struct meson_gpio_irq_controller *ctl,
  124. u32 *channel_hwirq)
  125. {
  126. return channel_hwirq - ctl->channel_irqs;
  127. }
  128. static void
  129. meson_gpio_irq_release_channel(struct meson_gpio_irq_controller *ctl,
  130. u32 *channel_hwirq)
  131. {
  132. unsigned int idx;
  133. idx = meson_gpio_irq_get_channel_idx(ctl, channel_hwirq);
  134. clear_bit(idx, ctl->channel_map);
  135. }
  136. static int meson_gpio_irq_type_setup(struct meson_gpio_irq_controller *ctl,
  137. unsigned int type,
  138. u32 *channel_hwirq)
  139. {
  140. u32 val = 0;
  141. unsigned int idx;
  142. idx = meson_gpio_irq_get_channel_idx(ctl, channel_hwirq);
  143. /*
  144. * The controller has a filter block to operate in either LEVEL or
  145. * EDGE mode, then signal is sent to the GIC. To enable LEVEL_LOW and
  146. * EDGE_FALLING support (which the GIC does not support), the filter
  147. * block is also able to invert the input signal it gets before
  148. * providing it to the GIC.
  149. */
  150. type &= IRQ_TYPE_SENSE_MASK;
  151. if (type == IRQ_TYPE_EDGE_BOTH)
  152. return -EINVAL;
  153. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
  154. val |= REG_EDGE_POL_EDGE(idx);
  155. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING))
  156. val |= REG_EDGE_POL_LOW(idx);
  157. spin_lock(&ctl->lock);
  158. meson_gpio_irq_update_bits(ctl, REG_EDGE_POL,
  159. REG_EDGE_POL_MASK(idx), val);
  160. spin_unlock(&ctl->lock);
  161. return 0;
  162. }
  163. static unsigned int meson_gpio_irq_type_output(unsigned int type)
  164. {
  165. unsigned int sense = type & IRQ_TYPE_SENSE_MASK;
  166. type &= ~IRQ_TYPE_SENSE_MASK;
  167. /*
  168. * The polarity of the signal provided to the GIC should always
  169. * be high.
  170. */
  171. if (sense & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
  172. type |= IRQ_TYPE_LEVEL_HIGH;
  173. else if (sense & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
  174. type |= IRQ_TYPE_EDGE_RISING;
  175. return type;
  176. }
  177. static int meson_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  178. {
  179. struct meson_gpio_irq_controller *ctl = data->domain->host_data;
  180. u32 *channel_hwirq = irq_data_get_irq_chip_data(data);
  181. int ret;
  182. ret = meson_gpio_irq_type_setup(ctl, type, channel_hwirq);
  183. if (ret)
  184. return ret;
  185. return irq_chip_set_type_parent(data,
  186. meson_gpio_irq_type_output(type));
  187. }
  188. static struct irq_chip meson_gpio_irq_chip = {
  189. .name = "meson-gpio-irqchip",
  190. .irq_mask = irq_chip_mask_parent,
  191. .irq_unmask = irq_chip_unmask_parent,
  192. .irq_eoi = irq_chip_eoi_parent,
  193. .irq_set_type = meson_gpio_irq_set_type,
  194. .irq_retrigger = irq_chip_retrigger_hierarchy,
  195. #ifdef CONFIG_SMP
  196. .irq_set_affinity = irq_chip_set_affinity_parent,
  197. #endif
  198. .flags = IRQCHIP_SET_TYPE_MASKED,
  199. };
  200. static int meson_gpio_irq_domain_translate(struct irq_domain *domain,
  201. struct irq_fwspec *fwspec,
  202. unsigned long *hwirq,
  203. unsigned int *type)
  204. {
  205. if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
  206. *hwirq = fwspec->param[0];
  207. *type = fwspec->param[1];
  208. return 0;
  209. }
  210. return -EINVAL;
  211. }
  212. static int meson_gpio_irq_allocate_gic_irq(struct irq_domain *domain,
  213. unsigned int virq,
  214. u32 hwirq,
  215. unsigned int type)
  216. {
  217. struct irq_fwspec fwspec;
  218. fwspec.fwnode = domain->parent->fwnode;
  219. fwspec.param_count = 3;
  220. fwspec.param[0] = 0; /* SPI */
  221. fwspec.param[1] = hwirq;
  222. fwspec.param[2] = meson_gpio_irq_type_output(type);
  223. return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  224. }
  225. static int meson_gpio_irq_domain_alloc(struct irq_domain *domain,
  226. unsigned int virq,
  227. unsigned int nr_irqs,
  228. void *data)
  229. {
  230. struct irq_fwspec *fwspec = data;
  231. struct meson_gpio_irq_controller *ctl = domain->host_data;
  232. unsigned long hwirq;
  233. u32 *channel_hwirq;
  234. unsigned int type;
  235. int ret;
  236. if (WARN_ON(nr_irqs != 1))
  237. return -EINVAL;
  238. ret = meson_gpio_irq_domain_translate(domain, fwspec, &hwirq, &type);
  239. if (ret)
  240. return ret;
  241. ret = meson_gpio_irq_request_channel(ctl, hwirq, &channel_hwirq);
  242. if (ret)
  243. return ret;
  244. ret = meson_gpio_irq_allocate_gic_irq(domain, virq,
  245. *channel_hwirq, type);
  246. if (ret < 0) {
  247. pr_err("failed to allocate gic irq %u\n", *channel_hwirq);
  248. meson_gpio_irq_release_channel(ctl, channel_hwirq);
  249. return ret;
  250. }
  251. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  252. &meson_gpio_irq_chip, channel_hwirq);
  253. return 0;
  254. }
  255. static void meson_gpio_irq_domain_free(struct irq_domain *domain,
  256. unsigned int virq,
  257. unsigned int nr_irqs)
  258. {
  259. struct meson_gpio_irq_controller *ctl = domain->host_data;
  260. struct irq_data *irq_data;
  261. u32 *channel_hwirq;
  262. if (WARN_ON(nr_irqs != 1))
  263. return;
  264. irq_domain_free_irqs_parent(domain, virq, 1);
  265. irq_data = irq_domain_get_irq_data(domain, virq);
  266. channel_hwirq = irq_data_get_irq_chip_data(irq_data);
  267. meson_gpio_irq_release_channel(ctl, channel_hwirq);
  268. }
  269. static const struct irq_domain_ops meson_gpio_irq_domain_ops = {
  270. .alloc = meson_gpio_irq_domain_alloc,
  271. .free = meson_gpio_irq_domain_free,
  272. .translate = meson_gpio_irq_domain_translate,
  273. };
  274. static int __init meson_gpio_irq_parse_dt(struct device_node *node,
  275. struct meson_gpio_irq_controller *ctl)
  276. {
  277. const struct of_device_id *match;
  278. const struct meson_gpio_irq_params *params;
  279. int ret;
  280. match = of_match_node(meson_irq_gpio_matches, node);
  281. if (!match)
  282. return -ENODEV;
  283. params = match->data;
  284. ctl->nr_hwirq = params->nr_hwirq;
  285. ret = of_property_read_variable_u32_array(node,
  286. "amlogic,channel-interrupts",
  287. ctl->channel_irqs,
  288. NUM_CHANNEL,
  289. NUM_CHANNEL);
  290. if (ret < 0) {
  291. pr_err("can't get %d channel interrupts\n", NUM_CHANNEL);
  292. return ret;
  293. }
  294. return 0;
  295. }
  296. static int __init meson_gpio_irq_of_init(struct device_node *node,
  297. struct device_node *parent)
  298. {
  299. struct irq_domain *domain, *parent_domain;
  300. struct meson_gpio_irq_controller *ctl;
  301. int ret;
  302. if (!parent) {
  303. pr_err("missing parent interrupt node\n");
  304. return -ENODEV;
  305. }
  306. parent_domain = irq_find_host(parent);
  307. if (!parent_domain) {
  308. pr_err("unable to obtain parent domain\n");
  309. return -ENXIO;
  310. }
  311. ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
  312. if (!ctl)
  313. return -ENOMEM;
  314. spin_lock_init(&ctl->lock);
  315. ctl->base = of_iomap(node, 0);
  316. if (!ctl->base) {
  317. ret = -ENOMEM;
  318. goto free_ctl;
  319. }
  320. ret = meson_gpio_irq_parse_dt(node, ctl);
  321. if (ret)
  322. goto free_channel_irqs;
  323. domain = irq_domain_create_hierarchy(parent_domain, 0, ctl->nr_hwirq,
  324. of_node_to_fwnode(node),
  325. &meson_gpio_irq_domain_ops,
  326. ctl);
  327. if (!domain) {
  328. pr_err("failed to add domain\n");
  329. ret = -ENODEV;
  330. goto free_channel_irqs;
  331. }
  332. pr_info("%d to %d gpio interrupt mux initialized\n",
  333. ctl->nr_hwirq, NUM_CHANNEL);
  334. return 0;
  335. free_channel_irqs:
  336. iounmap(ctl->base);
  337. free_ctl:
  338. kfree(ctl);
  339. return ret;
  340. }
  341. IRQCHIP_DECLARE(meson_gpio_intc, "amlogic,meson-gpio-intc",
  342. meson_gpio_irq_of_init);