irq-meson-gpio.c 11 KB

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